forked from dragino/RadioHead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RH_TCP.cpp
304 lines (270 loc) · 7.36 KB
/
RH_TCP.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
// RH_TCP.cpp
//
// Copyright (C) 2014 Mike McCauley
// $Id: RH_TCP.cpp,v 1.6 2017/01/12 23:58:00 mikem Exp $
#include <RadioHead.h>
// This can only build on Linux and compatible systems
#if (RH_PLATFORM == RH_PLATFORM_UNIX)
#include <RH_TCP.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <string>
RH_TCP::RH_TCP(const char* server)
: _server(server),
_rxBufLen(0),
_rxBufValid(false),
_socket(-1)
{
}
bool RH_TCP::init()
{
if (!connectToServer())
return false;
return sendThisAddress(_thisAddress);
}
bool RH_TCP::connectToServer()
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; // Allow IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // Stream socket
hints.ai_flags = AI_PASSIVE; // For wildcard IP address
hints.ai_protocol = 0; // Any protocol
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
std::string server(_server);
std::string port("4000");
size_t indexOfSeparator = server.find_first_of(':');
if (indexOfSeparator != std::string::npos)
{
port = server.substr(indexOfSeparator+1);
server.erase(indexOfSeparator);
}
s = getaddrinfo(server.c_str(), port.c_str(), &hints, &result);
if (s != 0)
{
fprintf(stderr, "RH_TCP::connect getaddrinfo failed: %s\n", gai_strerror(s));
return false;
}
// getaddrinfo() returns a list of address structures.
// Try each address until we successfully connect(2).
// If socket(2) (or connect(2)) fails, we (close the socket
// and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next)
{
_socket = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (_socket == -1)
continue;
if (connect(_socket, rp->ai_addr, rp->ai_addrlen) == 0)
break; /* Success */
close(_socket);
}
if (rp == NULL)
{ /* No address succeeded */
fprintf(stderr, "RH_TCP::connect could not connect to %s\n", _server);
return false;
}
freeaddrinfo(result); /* No longer needed */
// Now make the socket non-blocking
int on = 1;
int rc = ioctl(_socket, FIONBIO, (char *)&on);
if (rc < 0)
{
fprintf(stderr,"RH_TCP::init failed to set socket non-blocking: %s\n", strerror(errno));
close(_socket);
_socket = -1;
return false;
}
return true;
}
void RH_TCP::clearRxBuf()
{
_rxBufValid = false;
_rxBufLen = 0;
}
void RH_TCP::checkForEvents()
{
#define RH_TCP_SOCKETBUF_LEN 500
static uint8_t socketBuf[RH_TCP_SOCKETBUF_LEN]; // Room for several messages
static uint16_t socketBufLen = 0;
// Read at most the amount of space we have left in the buffer
ssize_t count = read(_socket, socketBuf + socketBufLen, sizeof(socketBuf) - socketBufLen);
if (count < 0)
{
if (errno != EAGAIN)
{
fprintf(stderr,"RH_TCP::checkForEvents read error: %s\n", strerror(errno));
exit(1);
}
}
else if (count == 0)
{
// End of file
fprintf(stderr,"RH_TCP::checkForEvents unexpected end of file on read\n");
exit(1);
}
else
{
socketBufLen += count;
while (socketBufLen >= 5)
{
RHTcpTypeMessage* message = ((RHTcpTypeMessage*)socketBuf);
uint32_t len = ntohl(message->length);
uint32_t messageLen = len + sizeof(message->length);
if (len > sizeof(socketBuf) - sizeof(message->length))
{
// Bogus length
fprintf(stderr, "RH_TCP::checkForEvents read ridiculous length: %d. Corrupt message stream? Aborting\n", len);
exit(1);
}
if (socketBufLen >= len + sizeof(message->length))
{
// Got at least all of this message
if (message->type == RH_TCP_MESSAGE_TYPE_PACKET && len >= 5)
{
// REVISIT: need to check if we are actually receiving?
// Its a new packet, extract the headers and payload
RHTcpPacket* packet = ((RHTcpPacket*)socketBuf);
_rxHeaderTo = packet->to;
_rxHeaderFrom = packet->from;
_rxHeaderId = packet->id;
_rxHeaderFlags = packet->flags;
uint32_t payloadLen = len - 5;
if (payloadLen <= sizeof(_rxBuf))
{
// Enough room in our receiver buffer
memcpy(_rxBuf, packet->payload, payloadLen);
_rxBufLen = payloadLen;
_rxBufFull = true;
}
}
// check for other message types here
// Now remove the used message by copying the trailing bytes (maybe start of a new message?)
// to the top of the buffer
memcpy(socketBuf, socketBuf + messageLen, sizeof(socketBuf) - messageLen);
socketBufLen -= messageLen;
}
}
}
}
void RH_TCP::validateRxBuf()
{
// The headers have already been extracted
if (_promiscuous ||
_rxHeaderTo == _thisAddress ||
_rxHeaderTo == RH_BROADCAST_ADDRESS)
{
_rxGood++;
_rxBufValid = true;
}
}
bool RH_TCP::available()
{
if (_socket < 0)
return false;
checkForEvents();
if (_rxBufFull)
{
validateRxBuf();
_rxBufFull= false;
}
return _rxBufValid;
}
// Block until something is available
void RH_TCP::waitAvailable()
{
waitAvailableTimeout(0); // 0 = Wait forever
}
// Block until something is available or timeout expires
bool RH_TCP::waitAvailableTimeout(uint16_t timeout)
{
int max_fd;
fd_set input;
int result;
FD_ZERO(&input);
FD_SET(_socket, &input);
max_fd = _socket + 1;
if (timeout)
{
struct timeval timer;
// Timeout is in milliseconds
timer.tv_sec = timeout / 1000;
timer.tv_usec = (timeout % 1000) * 1000;
result = select(max_fd, &input, NULL, NULL, &timer);
}
else
{
result = select(max_fd, &input, NULL, NULL, NULL);
}
if (result < 0)
fprintf(stderr, "RH_TCP::waitAvailableTimeout: select failed %s\n", strerror(errno));
return result > 0;
}
bool RH_TCP::recv(uint8_t* buf, uint8_t* len)
{
if (!available())
return false;
if (buf && len)
{
if (*len > _rxBufLen)
*len = _rxBufLen;
memcpy(buf, _rxBuf, *len);
}
clearRxBuf();
return true;
}
bool RH_TCP::send(const uint8_t* data, uint8_t len)
{
if (!waitCAD())
return false; // Check channel activity (prob not possible for this driver?)
bool ret = sendPacket(data, len);
delay(10); // Wait for transmit to succeed. REVISIT: depends on length and speed
return ret;
}
uint8_t RH_TCP::maxMessageLength()
{
return RH_TCP_MAX_MESSAGE_LEN;
}
void RH_TCP::setThisAddress(uint8_t address)
{
RHGenericDriver::setThisAddress(address);
sendThisAddress(_thisAddress);
}
bool RH_TCP::sendThisAddress(uint8_t thisAddress)
{
if (_socket < 0)
return false;
RHTcpThisAddress m;
m.length = htonl(2);
m.type = RH_TCP_MESSAGE_TYPE_THISADDRESS;
m.thisAddress = thisAddress;
ssize_t sent = write(_socket, &m, sizeof(m));
return sent > 0;
}
bool RH_TCP::sendPacket(const uint8_t* data, uint8_t len)
{
if (_socket < 0)
return false;
RHTcpPacket m;
m.length = htonl(len + 4);
m.type = RH_TCP_MESSAGE_TYPE_PACKET;
m.to = _txHeaderTo;
m.from = _txHeaderFrom;
m.id = _txHeaderId;
m.flags = _txHeaderFlags;
memcpy(m.payload, data, len);
ssize_t sent = write(_socket, &m, len + 8);
return sent > 0;
}
#endif