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>
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>
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>
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>
348 gotInterrupt =
false;
352 else if (radio.rxFifoFull()) {
366template<
class mesh_t,
class network_t,
class radio_t>
370 while (mesh.update()) {
371 if (!thisNodeAddress) {
377 while (network.update()) {
382 while (network.external_queue.size() > 0) {
383 f = network.external_queue.front();
387 unsigned int bytesRead = f.message_size;
390 memcpy(&msg.message, &f.message_buffer, bytesRead);
391 msg.size = bytesRead;
393#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
394 std::cout <<
"Radio: Received " << bytesRead <<
" bytes ... " << std::endl;
396#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
398 std::cout <<
"TunRead: " << std::endl;
399 for (
size_t i = 0; i < msg.size; i++) {
401 printf(
":%0x :", msg.message[i]);
403 std::cout << std::endl;
412 network.external_queue.pop();
418template<
class mesh_t,
class network_t,
class radio_t>
419struct in_addr
ESBGateway<mesh_t, network_t, radio_t>::getLocalIP()
421 struct ifaddrs *ifap, *ifa;
423 char host[NI_MAXHOST];
424 struct in_addr myNet;
425 memset(&myNet, 0,
sizeof(myNet));
428 for (ifa = ifap, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
429 if (std::string(
"tun_nrf24").compare(ifa->ifa_name) != 0 || ifa->ifa_addr == NULL) {
430 if (ifa->ifa_next == NULL) {
438 family = ifa->ifa_addr->sa_family;
441 if (family == AF_INET) {
442 s = getnameinfo(ifa->ifa_addr,
sizeof(
struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
444 myNet.s_addr = ntohl(inet_network(host));
456template<
class mesh_t,
class network_t,
class radio_t>
461 while (!txQueue.empty() && network.external_queue.size() == 0) {
463 msgStruct* msgTx = &txQueue.front();
465#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
466 std::cout <<
"Radio: Sending " << msgTx->size <<
" bytes ... ";
467 std::cout << std::endl;
469#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
475 std::uint8_t* tmp = msgTx->message;
479 uint32_t RF24_STR = 0x34324652;
480 uint32_t ARP_BC = 0xFFFFFFFF;
483 uint32_t rf24_Verification;
488 memcpy(&macData.rf24_Addr, tmp + 4, 2);
489 memcpy(&macData.rf24_Verification, tmp, 4);
491 if (macData.rf24_Verification == RF24_STR) {
492 const uint16_t other_node = macData.rf24_Addr;
493 RF24NetworkHeader header( other_node, EXTERNAL_DATA_TYPE);
494 ok = network.write(header, &msgTx->message, msgTx->size);
496 else if (macData.rf24_Verification == ARP_BC) {
497 RF24NetworkHeader header( 00, EXTERNAL_DATA_TYPE);
498 if (msgTx->size <= 42) {
499 if (thisNodeAddress == 00) {
501 uint32_t arp_timeout = millis();
503 ok = network.multicast(header, &msgTx->message, msgTx->size, 1);
504 while (millis() - arp_timeout < 5) {
507 network.multicast(header, &msgTx->message, msgTx->size, 1);
508 arp_timeout = millis();
509 while (millis() - arp_timeout < 15) {
512 network.multicast(header, &msgTx->message, msgTx->size, 1);
515 ok = network.write(header, &msgTx->message, msgTx->size);
522 uint8_t lastOctet = tmp[19];
525 RF24NetworkHeader header(00, EXTERNAL_DATA_TYPE);
526 bool sendData =
false;
528 struct in_addr ipDestination;
529 memcpy(&ipDestination.s_addr, &tmp[16], 4);
531 if ((getLocalIP().s_addr & 0x00FFFFFF) == (ipDestination.s_addr & 0x00FFFFFF)) {
532 if ((meshAddr = mesh.getAddress(lastOctet)) > 0) {
533 header.to_node = meshAddr;
537 if (thisNodeID > 0) {
543 else if (thisNodeID > 0) {
546 else if (routingTableSize > 0) {
547 for (
int i = 0; i < routingTableSize; i++) {
548 struct in_addr network;
549 network.s_addr = routingStruct[i].ip.s_addr & routingStruct[i].mask.s_addr;
550 struct in_addr destNet;
551 destNet.s_addr = ipDestination.s_addr & routingStruct[i].mask.s_addr;
553 if (destNet.s_addr == network.s_addr) {
554 uint8_t toNode = routingStruct[i].gw.s_addr >> 24;
556 if ((netAddr = mesh.getAddress(toNode)) > 0) {
557 header.to_node = netAddr;
566 ok = network.write(header, msgTx->message, msgTx->size);
587template<
class mesh_t,
class network_t,
class radio_t>
591 struct timeval selectTimeout;
592 uint8_t buffer[MAX_PAYLOAD_SIZE];
596 FD_SET(tunFd, &socketSet);
598 selectTimeout.tv_sec = 0;
599 selectTimeout.tv_usec = waitDelay * 1000;
601 if (select(tunFd + 1, &socketSet, NULL, NULL, &selectTimeout) != 0) {
602 if (FD_ISSET(tunFd, &socketSet)) {
603 if ((nread = read(tunFd, buffer, MAX_PAYLOAD_SIZE)) >= 0) {
605#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
606 std::cout <<
"Tun: Successfully read " << nread <<
" bytes from tun device" << std::endl;
608#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
609 std::cout <<
"TunRead: " << std::endl;
610 for (
int i = 0; i < nread; i++)
612 printf(
":%0x :", buffer[i]);
614 std::cout << std::endl;
617 memcpy(&msg.message, &buffer, nread);
619 if (txQueue.size() < 10) {
627#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
628 std::cerr <<
"Tun: Error while reading from tun/tap interface." << std::endl;
637template<
class mesh_t,
class network_t,
class radio_t>
641 if (rxQueue.size() < 1)
645 msgStruct* msg = &rxQueue.front();
647 if (msg->size > MAX_PAYLOAD_SIZE)
657 size_t writtenBytes = write(tunFd, &msg->message, msg->size);
658 if (writtenBytes != msg->size)
661#if RF24GATEWAY_DEBUG_LEVEL >= 1
662 printf(
"Tun: Less bytes written %d to tun/tap device then requested %d.", writtenBytes, msg->size);
667#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
668 std::cout <<
"Tun: Successfully wrote " << writtenBytes <<
" bytes to tun device" << std::endl;
672#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
674 std::cout <<
"TunRead: " << std::endl;
675 for (
size_t i = 0; i < msg->size; i++) {
678 std::cout << std::endl;
701template<
class mesh_t,
class network_t,
class radio_t>
705 const char* myAddr =
"127.0.0.1";
707 addr.sin_family = AF_INET;
708 ret = inet_aton(myAddr, &addr.sin_addr);
713 addr.sin_port = htons(32001);
715 s = socket(PF_INET, SOCK_DGRAM, 0);
724template<
class mesh_t,
class network_t,
class radio_t>
728 uint8_t buffer[MAX_PAYLOAD_SIZE + 11];
730 memcpy(&buffer[0], &nodeID, 1);
731 memcpy(&buffer[1], &frame.header, 8);
732 memcpy(&buffer[9], &frame.message_size, 2);
733 memcpy(&buffer[11], &frame.message_buffer, frame.message_size);
735 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)