Paparazzi UAS  v5.15_devel-230-gc96ce27
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 int WEAK udp_check_free_space(struct udp_periph *p, long *fd __attribute__((unused)), uint16_t len)
84 {
85  int available = UDP_TX_BUFFER_SIZE - p->tx_insert_idx;
86  return available >= len ? available : 0;
87 }
88 
94 void WEAK udp_put_byte(struct udp_periph *p, long fd __attribute__((unused)), uint8_t data)
95 {
97  return; // no room
98  }
99 
100  p->tx_buf[p->tx_insert_idx] = data;
101  p->tx_insert_idx++;
102 }
103 
104 void WEAK udp_put_buffer(struct udp_periph *p, long fd __attribute__((unused)), const uint8_t *data, uint16_t len)
105 {
106  if (p->tx_insert_idx + len >= UDP_TX_BUFFER_SIZE) {
107  return; // no room
108  }
109 
110  memcpy(&(p->tx_buf[p->tx_insert_idx]), data, len);
111  p->tx_insert_idx += len;
112 }
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
int 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 WEAK udp_put_byte(struct udp_periph *p, long fd, uint8_t data)
Add one data byte to the tx buffer.
Definition: udp.c:94
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
int 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:104
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
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