Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 {
96  if (p->tx_insert_idx >= UDP_TX_BUFFER_SIZE) {
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 }
uint16_t
unsigned short uint16_t
Definition: types.h:16
udp_send_message
void udp_send_message(struct udp_periph *p, long fd)
Send a message.
Definition: udp_arch.c:144
udp_periph_init
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
udp_check_free_space
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
udp_getch
uint8_t udp_getch(struct udp_periph *p)
Get the last character from the receive buffer.
Definition: udp_arch.c:99
udp_put_byte
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
udp_char_available
int udp_char_available(struct udp_periph *p)
Get number of bytes available in receive buffer.
Definition: udp_arch.c:83
udp_periph
Definition: udp.h:38
uint8_t
unsigned char uint8_t
Definition: types.h:14
fd
int fd
Definition: serial.c:26
udp_arch_periph_init
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
udp.h
arch independent UDP API
udp_put_buffer
void WEAK udp_put_buffer(struct udp_periph *p, long fd, const uint8_t *data, uint16_t len)
Definition: udp.c:104
UDP_TX_BUFFER_SIZE
#define UDP_TX_BUFFER_SIZE
Definition: udp.h:36
p
static float p[2][2]
Definition: ins_alt_float.c:268