Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
udp.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freek van tienen <freek.v.tienen@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
28 #include "mcu_periph/udp.h"
29 
30 #include <string.h>
31 
32 /* Print the configurations */
33 #if USE_UDP0
34 struct udp_periph udp0;
35 PRINT_CONFIG_VAR(UDP0_HOST)
36 PRINT_CONFIG_VAR(UDP0_PORT_OUT)
37 PRINT_CONFIG_VAR(UDP0_PORT_IN)
38 PRINT_CONFIG_VAR(UDP0_BROADCAST)
39 #endif // USE_UDP0
40 
41 #if USE_UDP1
42 struct udp_periph udp1;
43 PRINT_CONFIG_VAR(UDP1_HOST)
44 PRINT_CONFIG_VAR(UDP1_PORT_OUT)
45 PRINT_CONFIG_VAR(UDP1_PORT_IN)
46 PRINT_CONFIG_VAR(UDP1_BROADCAST)
47 #endif // USE_UDP1
48 
49 #if USE_UDP2
50 struct udp_periph udp2;
51 PRINT_CONFIG_VAR(UDP2_HOST)
52 PRINT_CONFIG_VAR(UDP2_PORT_OUT)
53 PRINT_CONFIG_VAR(UDP2_PORT_IN)
54 PRINT_CONFIG_VAR(UDP2_BROADCAST)
55 #endif // USE_UDP2
56 
60 void udp_periph_init(struct udp_periph *p, char *host, int port_out, int port_in, bool broadcast)
61 {
62  p->rx_insert_idx = 0;
63  p->rx_extract_idx = 0;
64  p->tx_insert_idx = 0;
65  p->device.periph = (void *)p;
66  p->device.check_free_space = (check_free_space_t) udp_check_free_space;
67  p->device.put_byte = (put_byte_t) udp_put_byte;
68  p->device.put_buffer = (put_buffer_t) udp_put_buffer;
69  p->device.send_message = (send_message_t) udp_send_message;
70  p->device.char_available = (char_available_t) udp_char_available;
71  p->device.get_byte = (get_byte_t) udp_getch;
72 
73  // Arch dependent initialization
74  udp_arch_periph_init(p, host, port_out, port_in, broadcast);
75 }
76 
83 bool WEAK udp_check_free_space(struct udp_periph *p, long *fd __attribute__((unused)), uint16_t len)
84 {
85  return (UDP_TX_BUFFER_SIZE - p->tx_insert_idx) >= len;
86 }
87 
93 void WEAK udp_put_byte(struct udp_periph *p, long fd __attribute__((unused)), uint8_t data)
94 {
96  return; // no room
97  }
98 
99  p->tx_buf[p->tx_insert_idx] = data;
100  p->tx_insert_idx++;
101 }
102 
103 void WEAK udp_put_buffer(struct udp_periph *p, long fd __attribute__((unused)), const uint8_t *data, uint16_t len)
104 {
105  if (p->tx_insert_idx + len >= UDP_TX_BUFFER_SIZE) {
106  return; // no room
107  }
108 
109  memcpy(&(p->tx_buf[p->tx_insert_idx]), data, len);
110  p->tx_insert_idx += len;
111 }
unsigned short uint16_t
Definition: types.h:16
void udp_periph_init(struct udp_periph *p, char *host, int port_out, int port_in, bool broadcast)
Initialize the UDP peripheral.
Definition: udp.c:60
void WEAK udp_put_byte(struct udp_periph *p, long fd, uint8_t data)
Add one data byte to the tx buffer.
Definition: udp.c:93
uint8_t tx_buf[UDP_TX_BUFFER_SIZE]
Transmit buffer.
Definition: udp.h:44
void udp_send_message(struct udp_periph *p, long fd)
Send a message.
Definition: udp_arch.c:144
uint16_t rx_extract_idx
Definition: udp.h:42
uint16_t udp_char_available(struct udp_periph *p)
Get number of bytes available in receive buffer.
Definition: udp_arch.c:83
uint16_t tx_insert_idx
Definition: udp.h:45
void WEAK udp_put_buffer(struct udp_periph *p, long fd, const uint8_t *data, uint16_t len)
Definition: udp.c:103
Definition: udp.h:38
#define UDP_TX_BUFFER_SIZE
Definition: udp.h:36
unsigned char uint8_t
Definition: types.h:14
int fd
Definition: serial.c:26
uint16_t rx_insert_idx
Definition: udp.h:41
static float p[2][2]
uint8_t udp_getch(struct udp_periph *p)
Get the last character from the receive buffer.
Definition: udp_arch.c:99
bool WEAK udp_check_free_space(struct udp_periph *p, long *fd, uint16_t len)
Check if there is enough free space in the transmit buffer.
Definition: udp.c:83
void udp_arch_periph_init(struct udp_periph *p, char *host, int port_out, int port_in, bool broadcast)
Initialize the UDP peripheral.
Definition: udp_arch.c:71
struct link_device device
Generic device interface.
Definition: udp.h:49
arch independent UDP API