-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
udp_arch.c
248 lines (218 loc) · 6.19 KB
/
udp_arch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright (C) 2014 Freek van Tienen <freek.v.tienen@gmail.com>
*
* This file is part of Paparazzi.
*
* Paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* Paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/** @file arch/linux/mcu_periph/udp_arch.c
* linux UDP handling
*/
#include "mcu_periph/udp.h"
#include "udp_socket.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <sys/select.h>
#include "rt_priority.h"
#ifndef UDP_THREAD_PRIO
#define UDP_THREAD_PRIO 10
#endif
static void *udp_thread(void *data __attribute__((unused)));
static pthread_mutex_t udp_mutex = PTHREAD_MUTEX_INITIALIZER;
void udp_arch_init(void)
{
pthread_mutex_init(&udp_mutex, NULL);
#ifdef USE_UDP0
UDP0Init();
#endif
#ifdef USE_UDP1
UDP1Init();
#endif
#ifdef USE_UDP2
UDP2Init();
#endif
pthread_t tid;
if (pthread_create(&tid, NULL, udp_thread, NULL) != 0) {
fprintf(stderr, "udp_arch_init: Could not create UDP reading thread.\n");
return;
}
}
/**
* Initialize the UDP peripheral.
* Allocate UdpSocket struct and create and bind the UDP socket.
*/
void udp_arch_periph_init(struct udp_periph *p, char *host, int port_out, int port_in, bool broadcast)
{
struct UdpSocket *sock = malloc(sizeof(struct UdpSocket));
udp_socket_create(sock, host, port_out, port_in, broadcast);
p->network = (void *)sock;
}
/**
* Get number of bytes available in receive buffer.
* @param p pointer to UDP peripheral
* @return number of bytes available in receive buffer
*/
uint16_t udp_char_available(struct udp_periph *p)
{
pthread_mutex_lock(&udp_mutex);
int16_t available = p->rx_insert_idx - p->rx_extract_idx;
if (available < 0) {
available += UDP_RX_BUFFER_SIZE;
}
pthread_mutex_unlock(&udp_mutex);
return (uint16_t)available;
}
/**
* Get the last character from the receive buffer.
* @param p pointer to UDP peripheral
* @return last byte
*/
uint8_t udp_getch(struct udp_periph *p)
{
pthread_mutex_lock(&udp_mutex);
uint8_t ret = p->rx_buf[p->rx_extract_idx];
p->rx_extract_idx = (p->rx_extract_idx + 1) % UDP_RX_BUFFER_SIZE;
pthread_mutex_unlock(&udp_mutex);
return ret;
}
/**
* Read bytes from UDP
*/
void udp_receive(struct udp_periph *p)
{
if (p == NULL) return;
if (p->network == NULL) return;
int16_t i;
int16_t available = UDP_RX_BUFFER_SIZE - udp_char_available(p);
uint8_t buf[UDP_RX_BUFFER_SIZE];
struct UdpSocket *sock = (struct UdpSocket *) p->network;
if (available <= 0) {
return; // No space
}
socklen_t slen = sizeof(struct sockaddr_in);
ssize_t byte_read = recvfrom(sock->sockfd, buf, available, MSG_DONTWAIT,
(struct sockaddr *)&sock->addr_in, &slen);
pthread_mutex_lock(&udp_mutex);
if (byte_read > 0) {
for (i = 0; i < byte_read; i++) {
p->rx_buf[p->rx_insert_idx] = buf[i];
p->rx_insert_idx = (p->rx_insert_idx + 1) % UDP_RX_BUFFER_SIZE;
}
}
pthread_mutex_unlock(&udp_mutex);
}
/**
* Send a message
*/
void udp_send_message(struct udp_periph *p, long fd __attribute__((unused)))
{
if (p == NULL) return;
if (p->network == NULL) return;
struct UdpSocket *sock = (struct UdpSocket *) p->network;
if (p->tx_insert_idx > 0) {
ssize_t bytes_sent = sendto(sock->sockfd, p->tx_buf, p->tx_insert_idx, MSG_DONTWAIT,
(struct sockaddr *)&sock->addr_out, sizeof(sock->addr_out));
if (bytes_sent != p->tx_insert_idx) {
if (bytes_sent < 0) {
perror("udp_send_message failed");
}
else {
fprintf(stderr, "udp_send_message: only sent %d bytes instead of %d\n",
(int)bytes_sent, p->tx_insert_idx);
}
}
p->tx_insert_idx = 0;
}
}
/**
* Send a packet from another buffer
*/
void udp_send_raw(struct udp_periph *p, long fd __attribute__((unused)), uint8_t *buffer, uint16_t size)
{
if (p == NULL) return;
if (p->network == NULL) return;
struct UdpSocket *sock = (struct UdpSocket *) p->network;
ssize_t test __attribute__((unused)) = sendto(sock->sockfd, buffer, size, MSG_DONTWAIT,
(struct sockaddr *)&sock->addr_out, sizeof(sock->addr_out));
}
/**
* check for new udp packets to receive or send.
*/
static void *udp_thread(void *data __attribute__((unused)))
{
get_rt_prio(UDP_THREAD_PRIO);
/* file descriptor list */
fd_set socks_master;
/* maximum file descriptor number */
int fdmax = 0;
/* clear the fd list */
FD_ZERO(&socks_master);
/* add used sockets */
int fd __attribute__((unused));
#if USE_UDP0
fd = ((struct UdpSocket *)udp0.network)->sockfd;
FD_SET(fd, &socks_master);
if (fd > fdmax) {
fdmax =fd;
}
#endif
#if USE_UDP1
fd = ((struct UdpSocket *)udp1.network)->sockfd;
FD_SET(fd, &socks_master);
if (fd > fdmax) {
fdmax =fd;
}
#endif
#if USE_UDP2
fd = ((struct UdpSocket *)udp2.network)->sockfd;
FD_SET(fd, &socks_master);
if (fd > fdmax) {
fdmax =fd;
}
#endif
/* socks to be read, modified after each select */
fd_set socks;
while (1) {
/* reset list of socks to check */
socks = socks_master;
if (select(fdmax + 1, &socks, NULL, NULL, NULL) < 0) {
fprintf(stderr, "udp_thread: select failed!");
}
else {
#if USE_UDP0
fd = ((struct UdpSocket *)udp0.network)->sockfd;
if (FD_ISSET(fd, &socks)) {
udp_receive(&udp0);
}
#endif
#if USE_UDP1
fd = ((struct UdpSocket *)udp1.network)->sockfd;
if (FD_ISSET(fd, &socks)) {
udp_receive(&udp1);
}
#endif
#if USE_UDP2
fd = ((struct UdpSocket *)udp2.network)->sockfd;
if (FD_ISSET(fd, &socks)) {
udp_receive(&udp2);
}
#endif
}
}
return 0;
}