7#include "RF24Gateway.h"
8#include "RF24Mesh/RF24Mesh_config.h"
12template<
class mesh_t,
class network_t,
class radio_t>
20template<
class mesh_t,
class network_t,
class radio_t>
24 begin(
true, mesh_enabled, 0, nodeID, data_rate, _channel);
29template<
class mesh_t,
class network_t,
class radio_t>
32 begin(0, mesh_enabled, address, nodeID, data_rate, _channel);
37template<
class mesh_t,
class network_t,
class radio_t>
40#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
42 printf(
"Config Device address 0%o nodeID %d\n", address, mesh_nodeID);
44 config_TUN = configTUN;
52 configDevice(address);
53 mesh_enabled = meshEnable;
54 thisNodeID = mesh_nodeID;
55 thisNodeAddress = address;
59 if (channel == 97 && MESH_DEFAULT_CHANNEL != 97) {
60 channel = MESH_DEFAULT_CHANNEL;
63 if (!thisNodeAddress && !mesh_nodeID) {
70 mesh.setNodeID(mesh_nodeID);
72 mesh.begin(channel, data_rate);
73 thisNodeAddress = mesh.mesh_address;
78 const uint16_t this_node = address;
79 radio.setDataRate(dataRate);
80 radio.setChannel(channel);
82 network.begin( this_node);
83 thisNodeAddress = this_node;
85 network.multicastRelay = 1;
99template<
class mesh_t,
class network_t,
class radio_t>
100void ESBGateway<mesh_t, network_t, radio_t>::loadRoutingTable()
102 std::ifstream infile(
"routing.txt", std::ifstream::in);
108 std::string ip, mask, gw;
110 std::string space =
" ";
112 while (std::getline(infile, str)) {
114 size_t subLen = str.find(space);
115 if (subLen != std::string::npos) {
116 ip = str.substr(0, subLen);
121 startLen = subLen + 1;
122 subLen = str.find(space, startLen);
123 if (subLen != std::string::npos) {
124 subLen -= (startLen);
125 mask = str.substr(startLen, subLen);
130 startLen = startLen + subLen + 1;
131 subLen = str.length() - (startLen);
132 gw = str.substr(startLen, subLen);
134 routingStruct[count].ip.s_addr = ntohl(inet_network(ip.c_str()));
135 routingStruct[count].mask.s_addr = ntohl(inet_network(mask.c_str()));
136 routingStruct[count].gw.s_addr = ntohl(inet_network(gw.c_str()));
143 routingTableSize = count;
152template<
class mesh_t,
class network_t,
class radio_t>
160template<
class mesh_t,
class network_t,
class radio_t>
161int ESBGateway<mesh_t, network_t, radio_t>::configDevice(uint16_t address)
163 std::string tunTapDevice =
"tun_nrf24";
164 strcpy(tunName, tunTapDevice.c_str());
168 flags = IFF_TUN | IFF_NO_PI | IFF_MULTI_QUEUE;
171 flags = IFF_TAP | IFF_NO_PI | IFF_MULTI_QUEUE;
173 tunFd = allocateTunDevice(tunName, flags, address);
174#if RF24GATEWAY_DEBUG_LEVEL >= 1
176 std::cout <<
"RF24Gw: Successfully attached to tun/tap device " << tunTapDevice << std::endl;
179 std::cerr <<
"RF24Gw: Error allocating tun/tap interface: " << tunFd << std::endl;
188template<
class mesh_t,
class network_t,
class radio_t>
189int ESBGateway<mesh_t, network_t, radio_t>::allocateTunDevice(
char* dev,
int flags, uint16_t address)
195 if ((fd = open(
"/dev/net/tun", O_RDWR)) < 0) {
199 memset(&ifr, 0,
sizeof(ifr));
201 ifr.ifr_flags = flags;
204 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
208 if (ioctl(fd, TUNSETIFF, (
void*)&ifr) < 0) {
211 std::cerr <<
"RF24Gw: Error: enabling TUNSETIFF" << std::endl;
212 uint32_t UID = getuid();
214 std::cout <<
"Not running as root, preconfigure the interface as follows" << std::endl;
215 std::cout <<
"sudo ip tuntap add dev tun_nrf24 mode tun user " << getlogin() <<
" multi_queue" << std::endl;
216 std::cout <<
"sudo ifconfig tun_nrf24 10.10.2.2/24" << std::endl;
223 if (ioctl(fd, TUNSETPERSIST, 1) < 0) {
224#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
225 std::cerr <<
"RF24Gw: Error: enabling TUNSETPERSIST" << std::endl;
232 sap.sa_family = ARPHRD_ETHER;
233 ((
char*)sap.sa_data)[4] = address;
234 ((
char*)sap.sa_data)[5] = address >> 8;
235 ((
char*)sap.sa_data)[0] = 0x52;
236 ((
char*)sap.sa_data)[1] = 0x46;
237 ((
char*)sap.sa_data)[2] = 0x32;
238 ((
char*)sap.sa_data)[3] = 0x34;
241 memcpy((
char*)&ifr.ifr_hwaddr, (
char*)&sap,
sizeof(
struct sockaddr));
243 if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
244#if RF24GATEWAY_DEBUG_LEVEL >= 1
245 fprintf(stderr,
"RF24Gw: Failed to set MAC address\n");
250 strcpy(dev, ifr.ifr_name);
256template<
class mesh_t,
class network_t,
class radio_t>
260 struct sockaddr_in sin;
261 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
263 fprintf(stderr,
"Could not get socket.\n");
267 sin.sin_family = AF_INET;
269 inet_aton(ip_addr, &sin.sin_addr);
270 strncpy(ifr.ifr_name, tunName, IFNAMSIZ);
272 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
273 fprintf(stderr,
"ifdown: shutdown ");
274 perror(ifr.ifr_name);
279 #define IRFFLAGS ifr_flags
281 #define IRFFLAGS ifr_flagshigh
284 if (!(ifr.IRFFLAGS & IFF_UP)) {
286 ifr.IRFFLAGS |= IFF_UP;
287 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
288 fprintf(stderr,
"ifup: failed ");
289 perror(ifr.ifr_name);
294 memcpy(&ifr.ifr_addr, &sin,
sizeof(
struct sockaddr));
297 if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
298 fprintf(stderr,
"Cannot set IP address. ");
299 perror(ifr.ifr_name);
303 inet_aton(mask, &sin.sin_addr);
304 memcpy(&ifr.ifr_addr, &sin,
sizeof(
struct sockaddr));
306 if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
307 fprintf(stderr,
"Cannot define subnet mask for this device");
308 perror(ifr.ifr_name);
318template<
class mesh_t,
class network_t,
class radio_t>
326template<
class mesh_t,
class network_t,
class radio_t>
343template<
class mesh_t,
class network_t,
class radio_t>
346 if (!gotInterrupt && radio.available()) {
354 gotInterrupt =
false;
363template<
class mesh_t,
class network_t,
class radio_t>
364void ESBGateway<mesh_t, network_t, radio_t>::handleRadioIn()
367 uint8_t returnVal = 0;
370 while ((returnVal = mesh.update())) {
371 if (returnVal == NETWORK_OVERRUN) {
374 else if (returnVal == NETWORK_CORRUPTION) {
377 if (!thisNodeAddress) {
383 while ((returnVal = network.update())) {
384 if (returnVal == NETWORK_OVERRUN) {
387 else if (returnVal == NETWORK_CORRUPTION) {
394 while (network.external_queue.size() > 0) {
395 f = network.external_queue.front();
399 if (f.message_size > 0 && f.message_size <= MAX_PAYLOAD_SIZE) {
400 memcpy(&msg.message, &f.message_buffer, f.message_size);
401 msg.size = f.message_size;
403#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
404 std::cout <<
"Radio: Received " << f.message_size <<
" bytes ... " << std::endl;
406#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
408 std::cout <<
"TunRead: " << std::endl;
409 for (
size_t i = 0; i < msg.size; i++) {
411 printf(
":%0x :", msg.message[i]);
413 std::cout << std::endl;
422 network.external_queue.pop();
428template<
class mesh_t,
class network_t,
class radio_t>
429struct in_addr
ESBGateway<mesh_t, network_t, radio_t>::getLocalIP()
431 struct ifaddrs *ifap, *ifa;
433 char host[NI_MAXHOST];
434 struct in_addr myNet;
435 memset(&myNet, 0,
sizeof(myNet));
438 for (ifa = ifap, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
439 if (std::string(
"tun_nrf24").compare(ifa->ifa_name) != 0 || ifa->ifa_addr == NULL) {
440 if (ifa->ifa_next == NULL) {
448 family = ifa->ifa_addr->sa_family;
451 if (family == AF_INET) {
452 s = getnameinfo(ifa->ifa_addr,
sizeof(
struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
454 myNet.s_addr = ntohl(inet_network(host));
466template<
class mesh_t,
class network_t,
class radio_t>
467void ESBGateway<mesh_t, network_t, radio_t>::handleRadioOut()
471 uint32_t txQueueTimer = millis();
473 while (!txQueue.empty() && network.external_queue.size() == 0) {
475 uint32_t queueSize = txQueue.size();
476 if (millis() - txQueueTimer > 750 && queueSize >= 10) {
477 for (uint32_t i = 0; i < queueSize; i++) {
483 msgStruct* msgTx = &txQueue.front();
485#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
486 std::cout <<
"Radio: Sending " << msgTx->size <<
" bytes ... ";
487 std::cout << std::endl;
489#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
495 std::uint8_t* tmp = msgTx->message;
498 if (msgTx->size < 6) {
505 uint32_t RF24_STR = 0x34324652;
506 uint32_t ARP_BC = 0xFFFFFFFF;
509 uint32_t rf24_Verification;
514 memcpy(&macData.rf24_Addr, tmp + 4, 2);
515 memcpy(&macData.rf24_Verification, tmp, 4);
517 if (macData.rf24_Verification == RF24_STR) {
518 const uint16_t other_node = macData.rf24_Addr;
519 RF24NetworkHeader header( other_node, EXTERNAL_DATA_TYPE);
520 ok = network.write(header, &msgTx->message, msgTx->size);
522 else if (macData.rf24_Verification == ARP_BC) {
523 RF24NetworkHeader header( 00, EXTERNAL_DATA_TYPE);
524 if (msgTx->size <= 42) {
525 if (thisNodeAddress == 00) {
527 uint32_t arp_timeout = millis();
529 ok = network.multicast(header, &msgTx->message, msgTx->size, 1);
530 while (millis() - arp_timeout < 5) {
531 uint8_t returnVal = network.update();
532 if (returnVal == NETWORK_OVERRUN) {
535 else if (returnVal == NETWORK_CORRUPTION) {
539 network.multicast(header, &msgTx->message, msgTx->size, 1);
540 arp_timeout = millis();
541 while (millis() - arp_timeout < 15) {
542 uint8_t returnVal = network.update();
543 if (returnVal == NETWORK_OVERRUN) {
546 else if (returnVal == NETWORK_CORRUPTION) {
550 network.multicast(header, &msgTx->message, msgTx->size, 1);
553 ok = network.write(header, &msgTx->message, msgTx->size);
561 if (msgTx->size < 20) {
566 uint8_t lastOctet = tmp[19];
569 RF24NetworkHeader header(00, EXTERNAL_DATA_TYPE);
570 bool sendData =
false;
572 struct in_addr ipDestination;
573 memcpy(&ipDestination.s_addr, &tmp[16], 4);
575 if ((getLocalIP().s_addr & 0x00FFFFFF) == (ipDestination.s_addr & 0x00FFFFFF)) {
576 if ((meshAddr = mesh.getAddress(lastOctet)) > 0) {
577 header.to_node = meshAddr;
581 if (thisNodeID > 0) {
587 else if (thisNodeID > 0) {
590 else if (routingTableSize > 0) {
591 for (
int i = 0; i < routingTableSize; i++) {
592 struct in_addr network;
593 network.s_addr = routingStruct[i].ip.s_addr & routingStruct[i].mask.s_addr;
594 struct in_addr destNet;
595 destNet.s_addr = ipDestination.s_addr & routingStruct[i].mask.s_addr;
597 if (destNet.s_addr == network.s_addr) {
598 uint8_t toNode = routingStruct[i].gw.s_addr >> 24;
600 if ((netAddr = mesh.getAddress(toNode)) > 0) {
601 header.to_node = netAddr;
610 ok = network.write(header, msgTx->message, msgTx->size);
631template<
class mesh_t,
class network_t,
class radio_t>
632void ESBGateway<mesh_t, network_t, radio_t>::handleRX(uint32_t waitDelay)
635 struct timeval selectTimeout;
636 uint8_t buffer[MAX_PAYLOAD_SIZE];
640 FD_SET(tunFd, &socketSet);
642 selectTimeout.tv_sec = 0;
643 selectTimeout.tv_usec = waitDelay * 1000;
645 uint32_t tunTimeout = millis();
647 while (select(tunFd + 1, &socketSet, NULL, NULL, &selectTimeout) != 0) {
648 if (FD_ISSET(tunFd, &socketSet)) {
649 if ((nread = read(tunFd, buffer, MAX_PAYLOAD_SIZE)) >= 0) {
650#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
651 std::cout <<
"Tun: Successfully read " << nread <<
" bytes from tun device" << std::endl;
653#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
654 std::cout <<
"TunRead: " << std::endl;
655 for (
int i = 0; i < nread; i++)
657 printf(
":%0x :", buffer[i]);
659 std::cout << std::endl;
662 memcpy(&msg.message, &buffer, nread);
667#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
668 std::cerr <<
"Tun: Error while reading from tun/tap interface." << std::endl;
672 if (millis() - tunTimeout > 750) {
680template<
class mesh_t,
class network_t,
class radio_t>
681void ESBGateway<mesh_t, network_t, radio_t>::handleTX()
684 if (rxQueue.size() < 1)
688 msgStruct* msg = &rxQueue.front();
690 if (msg->size > MAX_PAYLOAD_SIZE)
700 size_t writtenBytes = write(tunFd, &msg->message, msg->size);
701 if (writtenBytes != msg->size)
704#if RF24GATEWAY_DEBUG_LEVEL >= 1
705 printf(
"Tun: Less bytes written %d to tun/tap device then requested %d.", writtenBytes, msg->size);
710#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
711 std::cout <<
"Tun: Successfully wrote " << writtenBytes <<
" bytes to tun device" << std::endl;
715#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
717 std::cout <<
"TunRead: " << std::endl;
718 for (
size_t i = 0; i < msg->size; i++) {
721 std::cout << std::endl;
744template<
class mesh_t,
class network_t,
class radio_t>
745void ESBGateway<mesh_t, network_t, radio_t>::setupSocket()
748 const char* myAddr =
"127.0.0.1";
750 addr.sin_family = AF_INET;
751 ret = inet_aton(myAddr, &addr.sin_addr);
756 addr.sin_port = htons(32001);
758 s = socket(PF_INET, SOCK_DGRAM, 0);
767template<
class mesh_t,
class network_t,
class radio_t>
771 uint8_t buffer[MAX_PAYLOAD_SIZE + 11];
773 if (frame.message_size > MAX_PAYLOAD_SIZE) {
776 memcpy(&buffer[0], &nodeID, 1);
777 memcpy(&buffer[1], &frame.header, 8);
778 memcpy(&buffer[9], &frame.message_size, 2);
779 memcpy(&buffer[11], &frame.message_buffer, frame.message_size);
781 int ret = sendto(s, &buffer, frame.message_size + 11, 0, (
struct sockaddr*)&addr,
sizeof(addr));
int setIP(char *ip_addr, char *mask)
void interrupts(bool enable=1)
void poll(uint32_t waitDelay=3)
ESBGateway(radio_t &_radio, network_t &_network, mesh_t &_mesh)
void begin(uint8_t nodeID=0, uint8_t channel=97, rf24_datarate_e data_rate=RF24_1MBPS)
void update(bool interrupts=0)
void sendUDP(uint8_t nodeID, RF24NetworkFrame frame)