-
Notifications
You must be signed in to change notification settings - Fork 35
/
RF24Gateway.cpp
744 lines (623 loc) · 23 KB
/
RF24Gateway.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/**
* @file RF24Gateway.cpp
*
* Class definitions for RF24Gateway
*/
#include "RF24Gateway.h"
#include "RF24Mesh/RF24Mesh_config.h"
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
ESBGateway<mesh_t, network_t, radio_t>::ESBGateway(radio_t& _radio, network_t& _network, mesh_t& _mesh) : radio(_radio), network(_network), mesh(_mesh)
{
gotInterrupt = false;
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::begin(uint8_t nodeID, uint8_t _channel, rf24_datarate_e data_rate)
{
mesh_enabled = true;
begin(true, mesh_enabled, 0, nodeID, data_rate, _channel);
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::begin(uint16_t address, uint8_t _channel, rf24_datarate_e data_rate, bool meshEnable, uint8_t nodeID)
{
begin(0, mesh_enabled, address, nodeID, data_rate, _channel);
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
bool ESBGateway<mesh_t, network_t, radio_t>::begin(bool configTUN, bool meshEnable, uint16_t address, uint8_t mesh_nodeID, rf24_datarate_e data_rate, uint8_t _channel)
{
#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
printf("GW Begin\n");
printf("Config Device address 0%o nodeID %d\n", address, mesh_nodeID);
#endif
config_TUN = configTUN;
/// FIX
channel = _channel; // 97;
dataRate = data_rate;
configDevice(address);
mesh_enabled = meshEnable;
thisNodeID = mesh_nodeID;
thisNodeAddress = address;
if (meshEnable) {
// GW radio channel setting takes precedence over mesh_default_channel
if (channel == 97 && MESH_DEFAULT_CHANNEL != 97) {
channel = MESH_DEFAULT_CHANNEL;
}
if (!thisNodeAddress && !mesh_nodeID) {
mesh.setNodeID(0);
}
else {
if (!mesh_nodeID) {
mesh_nodeID = 253;
}
mesh.setNodeID(mesh_nodeID); // Try not to conflict with any low-numbered node-ids
}
mesh.begin(channel, data_rate);
thisNodeAddress = mesh.mesh_address;
}
else {
radio.begin();
delay(5);
const uint16_t this_node = address;
radio.setDataRate(dataRate);
radio.setChannel(channel);
network.begin(/*node address*/ this_node);
thisNodeAddress = this_node;
}
network.multicastRelay = 1;
//#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
radio.printDetails();
//#endif
setupSocket();
loadRoutingTable();
return true;
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::loadRoutingTable()
{
std::ifstream infile("routing.txt", std::ifstream::in);
if (!infile) {
return;
}
std::string str;
std::string ip, mask, gw;
uint16_t count = 0;
std::string space = " ";
while (std::getline(infile, str)) {
size_t startLen = 0;
size_t subLen = str.find(space);
if (subLen != std::string::npos) {
ip = str.substr(0, subLen);
}
else {
continue;
}
startLen = subLen + 1;
subLen = str.find(space, startLen);
if (subLen != std::string::npos) {
subLen -= (startLen);
mask = str.substr(startLen, subLen);
}
else {
continue;
}
startLen = startLen + subLen + 1;
subLen = str.length() - (startLen);
gw = str.substr(startLen, subLen);
routingStruct[count].ip.s_addr = ntohl(inet_network(ip.c_str()));
routingStruct[count].mask.s_addr = ntohl(inet_network(mask.c_str()));
routingStruct[count].gw.s_addr = ntohl(inet_network(gw.c_str()));
count++;
if (count >= 256) {
break;
}
}
routingTableSize = count;
// for(int i=0; i<count; i++){
// std::cout << inet_ntoa(routingStruct[i].ip) << ";" << inet_ntoa(routingStruct[i].mask) << ";" << inet_ntoa(routingStruct[i].gw) << std::endl;
// }
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
bool ESBGateway<mesh_t, network_t, radio_t>::meshEnabled()
{
return mesh_enabled;
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
int ESBGateway<mesh_t, network_t, radio_t>::configDevice(uint16_t address)
{
std::string tunTapDevice = "tun_nrf24";
strcpy(tunName, tunTapDevice.c_str());
int flags;
if (config_TUN) {
flags = IFF_TUN | IFF_NO_PI | IFF_MULTI_QUEUE;
}
else {
flags = IFF_TAP | IFF_NO_PI | IFF_MULTI_QUEUE;
}
tunFd = allocateTunDevice(tunName, flags, address);
#if RF24GATEWAY_DEBUG_LEVEL >= 1
if (tunFd >= 0) {
std::cout << "RF24Gw: Successfully attached to tun/tap device " << tunTapDevice << std::endl;
}
else {
std::cerr << "RF24Gw: Error allocating tun/tap interface: " << tunFd << std::endl;
exit(1);
}
#endif
return tunFd;
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
int ESBGateway<mesh_t, network_t, radio_t>::allocateTunDevice(char* dev, int flags, uint16_t address)
{
struct ifreq ifr;
int fd;
// open the device
if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
return fd;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags; // IFF_TUN or IFF_TAP, plus maybe IFF_NO_PI
if (*dev) {
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
// Create device
if (ioctl(fd, TUNSETIFF, (void*)&ifr) < 0) {
// close(fd);
//#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
std::cerr << "RF24Gw: Error: enabling TUNSETIFF" << std::endl;
uint32_t UID = getuid();
if (UID) {
std::cout << "Not running as root, preconfigure the interface as follows" << std::endl;
std::cout << "sudo ip tuntap add dev tun_nrf24 mode tun user " << getlogin() << " multi_queue" << std::endl;
std::cout << "sudo ifconfig tun_nrf24 10.10.2.2/24" << std::endl;
}
return -1;
//#endif
}
// Make persistent
if (ioctl(fd, TUNSETPERSIST, 1) < 0) {
#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
std::cerr << "RF24Gw: Error: enabling TUNSETPERSIST" << std::endl;
#endif
return -1;
}
if (!config_TUN) {
struct sockaddr sap;
sap.sa_family = ARPHRD_ETHER;
((char*)sap.sa_data)[4] = address;
((char*)sap.sa_data)[5] = address >> 8;
((char*)sap.sa_data)[0] = 0x52;
((char*)sap.sa_data)[1] = 0x46;
((char*)sap.sa_data)[2] = 0x32;
((char*)sap.sa_data)[3] = 0x34;
// printf("Address 0%o first %u last %u\n",address,sap.sa_data[0],sap.sa_data[1]);
memcpy((char*)&ifr.ifr_hwaddr, (char*)&sap, sizeof(struct sockaddr));
if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
#if RF24GATEWAY_DEBUG_LEVEL >= 1
fprintf(stderr, "RF24Gw: Failed to set MAC address\n");
#endif
}
}
strcpy(dev, ifr.ifr_name);
return fd;
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
int ESBGateway<mesh_t, network_t, radio_t>::setIP(char* ip_addr, char* mask)
{
struct ifreq ifr;
struct sockaddr_in sin;
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
fprintf(stderr, "Could not get socket.\n");
return -1;
}
sin.sin_family = AF_INET;
// inet_aton(ip_addr,&sin.sin_addr.s_addr);
inet_aton(ip_addr, &sin.sin_addr);
strncpy(ifr.ifr_name, tunName, IFNAMSIZ);
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
fprintf(stderr, "ifdown: shutdown ");
perror(ifr.ifr_name);
return -1;
}
#ifdef ifr_flags
#define IRFFLAGS ifr_flags
#else /* Present on kFreeBSD */
#define IRFFLAGS ifr_flagshigh
#endif
if (!(ifr.IRFFLAGS & IFF_UP)) {
// fprintf(stdout, "Device is currently down..setting up.-- %u\n", ifr.IRFFLAGS);
ifr.IRFFLAGS |= IFF_UP;
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
fprintf(stderr, "ifup: failed ");
perror(ifr.ifr_name);
return -1;
}
}
memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
// Set interface address
if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
fprintf(stderr, "Cannot set IP address. ");
perror(ifr.ifr_name);
return -1;
}
inet_aton(mask, &sin.sin_addr);
memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
fprintf(stderr, "Cannot define subnet mask for this device");
perror(ifr.ifr_name);
return -1;
}
#undef IRFFLAGS
return 0;
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::interrupts(bool enable)
{
// No longer required
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::update(bool interrupts)
{
if (interrupts) {
gotInterrupt = true;
}
else {
handleRadioIn();
handleTX();
handleRX();
handleRadioOut();
}
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::poll(uint32_t waitDelay)
{
if (gotInterrupt) {
gotInterrupt = false;
handleRadioIn();
handleTX();
}
else if (radio.rxFifoFull()) {
fifoCleared = true;
handleRadioIn();
handleRadioOut();
}
else {
delay(waitDelay);
}
handleRX();
handleRadioOut();
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::handleRadioIn()
{
if (mesh_enabled) {
while (mesh.update()) {
if (!thisNodeAddress) {
mesh.DHCP();
}
}
}
else {
while (network.update()) {
}
}
RF24NetworkFrame f;
while (network.external_queue.size() > 0) {
f = network.external_queue.front();
msgStruct msg;
unsigned int bytesRead = f.message_size;
if (bytesRead > 0) {
memcpy(&msg.message, &f.message_buffer, bytesRead);
msg.size = bytesRead;
#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
std::cout << "Radio: Received " << bytesRead << " bytes ... " << std::endl;
#endif
#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
// printPayload(msg.getPayloadStr(),"radio RX");
std::cout << "TunRead: " << std::endl;
for (size_t i = 0; i < msg.size; i++) {
// std::cout << std::hex << buffer[i];
printf(":%0x :", msg.message[i]);
}
std::cout << std::endl;
#endif
rxQueue.push(msg);
}
else {
// std::cerr << "Radio: Error reading data from radio. Read '" << bytesRead << "' Bytes." << std::endl;
}
network.external_queue.pop();
}
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
struct in_addr ESBGateway<mesh_t, network_t, radio_t>::getLocalIP()
{
struct ifaddrs *ifap, *ifa;
int family, s, n;
char host[NI_MAXHOST];
struct in_addr myNet;
memset(&myNet, 0, sizeof(myNet));
getifaddrs(&ifap);
for (ifa = ifap, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (std::string("tun_nrf24").compare(ifa->ifa_name) != 0 || ifa->ifa_addr == NULL) {
if (ifa->ifa_next == NULL) {
break;
}
else {
continue;
}
}
family = ifa->ifa_addr->sa_family;
// This is an IPv4 interface, get the IP
if (family == AF_INET) {
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s == 0) {
myNet.s_addr = ntohl(inet_network(host));
freeifaddrs(ifap);
return myNet;
}
}
}
freeifaddrs(ifap);
return myNet;
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::handleRadioOut()
{
bool ok = 0;
while (!txQueue.empty() && network.external_queue.size() == 0) {
msgStruct* msgTx = &txQueue.front();
#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
std::cout << "Radio: Sending " << msgTx->size << " bytes ... ";
std::cout << std::endl;
#endif
#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
// PrintDebug == 1 does not have an endline.
// printPayload(msg.getPayloadStr(),"radio TX");
#endif
std::uint8_t* tmp = msgTx->message;
if (!config_TUN) { // TAP can use RF24Mesh for address assignment, but will still use ARP for address resolution
uint32_t RF24_STR = 0x34324652; // Identifies the mac as an RF24 mac
uint32_t ARP_BC = 0xFFFFFFFF; // Broadcast address
struct macStruct
{
uint32_t rf24_Verification;
uint16_t rf24_Addr;
};
macStruct macData;
memcpy(&macData.rf24_Addr, tmp + 4, 2);
memcpy(&macData.rf24_Verification, tmp, 4);
if (macData.rf24_Verification == RF24_STR) {
const uint16_t other_node = macData.rf24_Addr;
RF24NetworkHeader header(/*to node*/ other_node, EXTERNAL_DATA_TYPE);
ok = network.write(header, &msgTx->message, msgTx->size);
}
else if (macData.rf24_Verification == ARP_BC) {
RF24NetworkHeader header(/*to node*/ 00, EXTERNAL_DATA_TYPE); // Set to master node, will be modified by RF24Network if multi-casting
if (msgTx->size <= 42) {
if (thisNodeAddress == 00) { // Master Node
uint32_t arp_timeout = millis();
ok = network.multicast(header, &msgTx->message, msgTx->size, 1); // Send to Level 1
while (millis() - arp_timeout < 5) {
network.update();
}
network.multicast(header, &msgTx->message, msgTx->size, 1); // Send to Level 1
arp_timeout = millis();
while (millis() - arp_timeout < 15) {
network.update();
}
network.multicast(header, &msgTx->message, msgTx->size, 1); // Send to Level 1
}
else {
ok = network.write(header, &msgTx->message, msgTx->size);
}
}
}
}
else { // TUN always needs to use RF24Mesh for address assignment AND resolution
uint8_t lastOctet = tmp[19];
int16_t meshAddr;
RF24NetworkHeader header(00, EXTERNAL_DATA_TYPE);
bool sendData = false;
struct in_addr ipDestination;
memcpy(&ipDestination.s_addr, &tmp[16], 4);
if ((getLocalIP().s_addr & 0x00FFFFFF) == (ipDestination.s_addr & 0x00FFFFFF)) { // Is inside the RF24Mesh network
if ((meshAddr = mesh.getAddress(lastOctet)) > 0) {
header.to_node = meshAddr;
sendData = true;
}
else {
if (thisNodeID > 0) { // If IP is in mesh range, address lookup fails, and this is not master,
sendData = true; // send to 00 anyway in case destination is master, or the lookup just failed
}
// printf("Could not find matching mesh nodeID for IP ending in %d\n",lastOctet);
}
}
else if (thisNodeID > 0) { // If not master, send to master for routing etc. if target not within mesh
sendData = true;
}
else if (routingTableSize > 0) {
for (int i = 0; i < routingTableSize; i++) {
struct in_addr network;
network.s_addr = routingStruct[i].ip.s_addr & routingStruct[i].mask.s_addr;
struct in_addr destNet;
destNet.s_addr = ipDestination.s_addr & routingStruct[i].mask.s_addr;
// printf("network %s destNet: %s\n",inet_ntoa(network),inet_ntoa(destNet));
if (destNet.s_addr == network.s_addr) {
uint8_t toNode = routingStruct[i].gw.s_addr >> 24;
int16_t netAddr = 0;
if ((netAddr = mesh.getAddress(toNode)) > 0) {
header.to_node = netAddr;
sendData = true;
break;
}
}
}
}
if (sendData) {
ok = network.write(header, msgTx->message, msgTx->size);
// std::cout << "SendData " << header.to_node << std::endl;
}
}
// delay( rf24_min(msgTx->size/48,20));
txQueue.pop();
// printf("Addr: 0%#x\n",macData.rf24_Addr);
// printf("Verif: 0%#x\n",macData.rf24_Verification);
if (ok) {
// std::cout << "ok." << std::endl;
}
else {
// std::cerr << "failed." << std::endl;
}
} // End Tx
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::handleRX(uint32_t waitDelay)
{
fd_set socketSet;
struct timeval selectTimeout;
uint8_t buffer[MAX_PAYLOAD_SIZE];
int nread;
FD_ZERO(&socketSet);
FD_SET(tunFd, &socketSet);
selectTimeout.tv_sec = 0;
selectTimeout.tv_usec = waitDelay * 1000;
if (select(tunFd + 1, &socketSet, NULL, NULL, &selectTimeout) != 0) {
if (FD_ISSET(tunFd, &socketSet)) {
if ((nread = read(tunFd, buffer, MAX_PAYLOAD_SIZE)) >= 0) {
#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
std::cout << "Tun: Successfully read " << nread << " bytes from tun device" << std::endl;
#endif
#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
std::cout << "TunRead: " << std::endl;
for (int i = 0; i < nread; i++)
{
printf(":%0x :", buffer[i]);
}
std::cout << std::endl;
#endif
msgStruct msg;
memcpy(&msg.message, &buffer, nread);
msg.size = nread;
if (txQueue.size() < 10) {
txQueue.push(msg);
}
else {
droppedIncoming++;
}
}
else {
#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
std::cerr << "Tun: Error while reading from tun/tap interface." << std::endl;
#endif
}
}
}
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::handleTX()
{
if (rxQueue.size() < 1)
{
return;
}
msgStruct* msg = &rxQueue.front();
if (msg->size > MAX_PAYLOAD_SIZE)
{
// printf("*****WTF OVER *****");
rxQueue.pop();
return;
}
if (msg->size > 0)
{
size_t writtenBytes = write(tunFd, &msg->message, msg->size);
if (writtenBytes != msg->size)
{
// std::cerr << "Tun: Less bytes written to tun/tap device then requested." << std::endl;
#if RF24GATEWAY_DEBUG_LEVEL >= 1
printf("Tun: Less bytes written %d to tun/tap device then requested %d.", writtenBytes, msg->size);
#endif
}
else
{
#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
std::cout << "Tun: Successfully wrote " << writtenBytes << " bytes to tun device" << std::endl;
#endif
}
#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
// printPayload(msg.message,"tun write");
std::cout << "TunRead: " << std::endl;
for (size_t i = 0; i < msg->size; i++) {
// printf(":%0x :",msg->message[i]);
}
std::cout << std::endl;
#endif
}
rxQueue.pop();
}
/***************************************************************************************
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::printPayload(std::string buffer, std::string debugMsg)
{
}
*/
/***************************************************************************************
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::printPayload(char* buffer, int nread, std::string debugMsg)
{
}
*/
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::setupSocket()
{
int ret;
const char* myAddr = "127.0.0.1";
addr.sin_family = AF_INET;
ret = inet_aton(myAddr, &addr.sin_addr);
if (ret == 0) {
perror("inet_aton");
exit(1);
}
addr.sin_port = htons(32001);
// buf = "Hello UDP";
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s == -1) {
perror("socket");
exit(1);
}
}
/***************************************************************************************/
template<class mesh_t, class network_t, class radio_t>
void ESBGateway<mesh_t, network_t, radio_t>::sendUDP(uint8_t nodeID, RF24NetworkFrame frame)
{
uint8_t buffer[MAX_PAYLOAD_SIZE + 11];
memcpy(&buffer[0], &nodeID, 1);
memcpy(&buffer[1], &frame.header, 8);
memcpy(&buffer[9], &frame.message_size, 2);
memcpy(&buffer[11], &frame.message_buffer, frame.message_size);
int ret = sendto(s, &buffer, frame.message_size + 11, 0, (struct sockaddr*)&addr, sizeof(addr));
if (ret == -1)
{
perror("sendto");
exit(1);
}
}
// ensure the compiler is aware of the possible datatype for the template class
template class ESBGateway<ESBMesh<ESBNetwork<RF24>, RF24>, ESBNetwork<RF24>, RF24>;