-
Notifications
You must be signed in to change notification settings - Fork 261
/
udp_port.cpp
374 lines (313 loc) · 9.85 KB
/
udp_port.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/****************************************************************************
*
* Copyright (c) 2018 MAVlink Development Team. All rights reserved.
* Author: Hannes Diethelm, <hannes.diethelm@gmail.com>
* Trent Lukaczyk, <aerialhedgehog@gmail.com>
* Jaycee Lock, <jaycee.lock@gmail.com>
* Lorenz Meier, <lm@inf.ethz.ch>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file udp_port.cpp
*
* @brief UDP interface functions
*
* Functions for opening, closing, reading and writing via UDP ports
*
* @author Hannes Diethelm, <hannes.diethelm@gmail.com>
* @author Trent Lukaczyk, <aerialhedgehog@gmail.com>
* @author Jaycee Lock, <jaycee.lock@gmail.com>
* @author Lorenz Meier, <lm@inf.ethz.ch>
*
*/
// ------------------------------------------------------------------------------
// Includes
// ------------------------------------------------------------------------------
#include "udp_port.h"
// ----------------------------------------------------------------------------------
// UDP Port Manager Class
// ----------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Con/De structors
// ------------------------------------------------------------------------------
UDP_Port::
UDP_Port(const char *target_ip_, int udp_port_)
{
initialize_defaults();
target_ip = target_ip_;
rx_port = udp_port_;
is_open = false;
}
UDP_Port::
UDP_Port()
{
initialize_defaults();
}
UDP_Port::
~UDP_Port()
{
// destroy mutex
pthread_mutex_destroy(&lock);
}
void
UDP_Port::
initialize_defaults()
{
// Initialize attributes
target_ip = "127.0.0.1";
rx_port = 14550;
tx_port = -1;
is_open = false;
debug = false;
sock = -1;
// Start mutex
int result = pthread_mutex_init(&lock, NULL);
if ( result != 0 )
{
printf("\n mutex init failed\n");
throw 1;
}
}
// ------------------------------------------------------------------------------
// Read from UDP
// ------------------------------------------------------------------------------
int
UDP_Port::
read_message(mavlink_message_t &message)
{
uint8_t cp;
mavlink_status_t status;
uint8_t msgReceived = false;
// --------------------------------------------------------------------------
// READ FROM PORT
// --------------------------------------------------------------------------
// this function locks the port during read
int result = _read_port(cp);
// --------------------------------------------------------------------------
// PARSE MESSAGE
// --------------------------------------------------------------------------
if (result > 0)
{
// the parsing
msgReceived = mavlink_parse_char(MAVLINK_COMM_1, cp, &message, &status);
// check for dropped packets
if ( (lastStatus.packet_rx_drop_count != status.packet_rx_drop_count) && debug )
{
printf("ERROR: DROPPED %d PACKETS\n", status.packet_rx_drop_count);
unsigned char v=cp;
fprintf(stderr,"%02x ", v);
}
lastStatus = status;
}
// Couldn't read from port
else
{
fprintf(stderr, "ERROR: Could not read, res = %d, errno = %d : %m\n", result, errno);
}
// --------------------------------------------------------------------------
// DEBUGGING REPORTS
// --------------------------------------------------------------------------
if(msgReceived && debug)
{
// Report info
printf("Received message from UDP with ID #%d (sys:%d|comp:%d):\n", message.msgid, message.sysid, message.compid);
fprintf(stderr,"Received UDP data: ");
unsigned int i;
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
// check message is write length
unsigned int messageLength = mavlink_msg_to_send_buffer(buffer, &message);
// message length error
if (messageLength > MAVLINK_MAX_PACKET_LEN)
{
fprintf(stderr, "\nFATAL ERROR: MESSAGE LENGTH IS LARGER THAN BUFFER SIZE\n");
}
// print out the buffer
else
{
for (i=0; i<messageLength; i++)
{
unsigned char v=buffer[i];
fprintf(stderr,"%02x ", v);
}
fprintf(stderr,"\n");
}
}
// Done!
return msgReceived;
}
// ------------------------------------------------------------------------------
// Write to UDP
// ------------------------------------------------------------------------------
int
UDP_Port::
write_message(const mavlink_message_t &message)
{
char buf[300];
// Translate message to buffer
unsigned len = mavlink_msg_to_send_buffer((uint8_t*)buf, &message);
// Write buffer to UDP port, locks port while writing
int bytesWritten = _write_port(buf,len);
if(bytesWritten < 0){
fprintf(stderr, "ERROR: Could not write, res = %d, errno = %d : %m\n", bytesWritten, errno);
}
return bytesWritten;
}
// ------------------------------------------------------------------------------
// Open UDP Port
// ------------------------------------------------------------------------------
/**
* throws EXIT_FAILURE if could not open the port
*/
void
UDP_Port::
start()
{
// --------------------------------------------------------------------------
// OPEN PORT
// --------------------------------------------------------------------------
/* Create socket */
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0)
{
perror("error socket failed");
throw EXIT_FAILURE;
}
/* Bind the socket to rx_port - necessary to receive packets */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(target_ip);;
addr.sin_port = htons(rx_port);
if (bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr)))
{
perror("error bind failed");
close(sock);
sock = -1;
throw EXIT_FAILURE;
}
/*if (fcntl(sock, F_SETFL, O_NONBLOCK | O_ASYNC) < 0)
{
fprintf(stderr, "error setting nonblocking: %s\n", strerror(errno));
close(sock);
sock = -1;
throw EXIT_FAILURE;
}*/
// --------------------------------------------------------------------------
// CONNECTED!
// --------------------------------------------------------------------------
printf("Listening to %s:%i\n", target_ip, rx_port);
lastStatus.packet_rx_drop_count = 0;
is_open = true;
printf("\n");
return;
}
// ------------------------------------------------------------------------------
// Close UDP Port
// ------------------------------------------------------------------------------
void
UDP_Port::
stop()
{
printf("CLOSE PORT\n");
int result = close(sock);
sock = -1;
if ( result )
{
fprintf(stderr,"WARNING: Error on port close (%i)\n", result );
}
is_open = false;
printf("\n");
}
// ------------------------------------------------------------------------------
// Read Port with Lock
// ------------------------------------------------------------------------------
int
UDP_Port::
_read_port(uint8_t &cp)
{
socklen_t len;
// Lock
pthread_mutex_lock(&lock);
int result = -1;
if(buff_ptr < buff_len){
cp=buff[buff_ptr];
buff_ptr++;
result=1;
}else{
struct sockaddr_in addr;
len = sizeof(struct sockaddr_in);
result = recvfrom(sock, &buff, BUFF_LEN, 0, (struct sockaddr *)&addr, &len);
if(tx_port < 0){
if(strcmp(inet_ntoa(addr.sin_addr), target_ip) == 0){
tx_port = ntohs(addr.sin_port);
printf("Got first packet, sending to %s:%i\n", target_ip, rx_port);
}else{
printf("ERROR: Got packet from %s:%i but listening on %s\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), target_ip);
}
}
if(result > 0){
buff_len=result;
buff_ptr=0;
cp=buff[buff_ptr];
buff_ptr++;
//printf("recvfrom: %i %i\n", result, cp);
}
}
// Unlock
pthread_mutex_unlock(&lock);
return result;
}
// ------------------------------------------------------------------------------
// Write Port with Lock
// ------------------------------------------------------------------------------
int
UDP_Port::
_write_port(char *buf, unsigned len)
{
// Lock
pthread_mutex_lock(&lock);
// Write packet via UDP link
int bytesWritten = 0;
if(tx_port > 0){
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(target_ip);
addr.sin_port = htons(tx_port);
bytesWritten = sendto(sock, buf, len, 0, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
//printf("sendto: %i\n", bytesWritten);
}else{
printf("ERROR: Sending before first packet received!\n");
bytesWritten = -1;
}
// Unlock
pthread_mutex_unlock(&lock);
return bytesWritten;
}