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>
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);
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 std::cerr <<
"RF24Gw: If changing from TAP/TUN, run 'sudo ip link delete tun_nrf24' to remove the interface" << std::endl;
218 if (ioctl(fd, TUNSETPERSIST, 1) < 0) {
219#if (DEBUG_LEVEL >= 1)
220 std::cerr <<
"RF24Gw: Error: enabling TUNSETPERSIST" << std::endl;
227 sap.sa_family = ARPHRD_ETHER;
228 ((
char*)sap.sa_data)[4] = address;
229 ((
char*)sap.sa_data)[5] = address >> 8;
230 ((
char*)sap.sa_data)[0] = 0x52;
231 ((
char*)sap.sa_data)[1] = 0x46;
232 ((
char*)sap.sa_data)[2] = 0x32;
233 ((
char*)sap.sa_data)[3] = 0x34;
236 memcpy((
char*)&ifr.ifr_hwaddr, (
char*)&sap,
sizeof(
struct sockaddr));
238 if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
240 fprintf(stderr,
"RF24Gw: Failed to set MAC address\n");
245 strcpy(dev, ifr.ifr_name);
251template<
class mesh_t,
class network_t,
class radio_t>
255 struct sockaddr_in sin;
256 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
258 fprintf(stderr,
"Could not get socket.\n");
262 sin.sin_family = AF_INET;
264 inet_aton(ip_addr, &sin.sin_addr);
265 strncpy(ifr.ifr_name, tunName, IFNAMSIZ);
267 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
268 fprintf(stderr,
"ifdown: shutdown ");
269 perror(ifr.ifr_name);
274 #define IRFFLAGS ifr_flags
276 #define IRFFLAGS ifr_flagshigh
279 if (!(ifr.IRFFLAGS & IFF_UP)) {
281 ifr.IRFFLAGS |= IFF_UP;
282 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
283 fprintf(stderr,
"ifup: failed ");
284 perror(ifr.ifr_name);
289 memcpy(&ifr.ifr_addr, &sin,
sizeof(
struct sockaddr));
292 if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
293 fprintf(stderr,
"Cannot set IP address. ");
294 perror(ifr.ifr_name);
298 inet_aton(mask, &sin.sin_addr);
299 memcpy(&ifr.ifr_addr, &sin,
sizeof(
struct sockaddr));
301 if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
302 fprintf(stderr,
"Cannot define subnet mask for this device");
303 perror(ifr.ifr_name);
313template<
class mesh_t,
class network_t,
class radio_t>
321template<
class mesh_t,
class network_t,
class radio_t>
338template<
class mesh_t,
class network_t,
class radio_t>
343 gotInterrupt =
false;
347 else if (radio.rxFifoFull()) {
361template<
class mesh_t,
class network_t,
class radio_t>
365 while (mesh.update()) {
366 if (!thisNodeAddress) {
372 while (network.update()) {
377 while (network.external_queue.size() > 0) {
378 f = network.external_queue.front();
382 unsigned int bytesRead = f.message_size;
385 memcpy(&msg.message, &f.message_buffer, bytesRead);
386 msg.size = bytesRead;
388#if (DEBUG_LEVEL >= 1)
389 std::cout <<
"Radio: Received " << bytesRead <<
" bytes ... " << std::endl;
391#if (DEBUG_LEVEL >= 3)
393 std::cout <<
"TunRead: " << std::endl;
394 for (
size_t i = 0; i < msg.size; i++) {
396 printf(
":%0x :", msg.message[i]);
398 std::cout << std::endl;
407 network.external_queue.pop();
413template<
class mesh_t,
class network_t,
class radio_t>
414struct in_addr
ESBGateway<mesh_t, network_t, radio_t>::getLocalIP()
416 struct ifaddrs *ifap, *ifa;
418 char host[NI_MAXHOST];
419 struct in_addr myNet;
422 for (ifa = ifap, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
423 if (std::string(
"tun_nrf24").compare(ifa->ifa_name) != 0 || ifa->ifa_addr == NULL) {
424 if (ifa->ifa_next == NULL) {
432 family = ifa->ifa_addr->sa_family;
435 if (family == AF_INET) {
436 s = getnameinfo(ifa->ifa_addr,
sizeof(
struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
438 myNet.s_addr = ntohl(inet_network(host));
450template<
class mesh_t,
class network_t,
class radio_t>
455 while (!txQueue.empty() && network.external_queue.size() == 0) {
457 msgStruct* msgTx = &txQueue.front();
459#if (DEBUG_LEVEL >= 1)
460 std::cout <<
"Radio: Sending " << msgTx->size <<
" bytes ... ";
461 std::cout << std::endl;
463#if (DEBUG_LEVEL >= 3)
469 std::uint8_t* tmp = msgTx->message;
473 uint32_t RF24_STR = 0x34324652;
474 uint32_t ARP_BC = 0xFFFFFFFF;
477 uint32_t rf24_Verification;
482 memcpy(&macData.rf24_Addr, tmp + 4, 2);
483 memcpy(&macData.rf24_Verification, tmp, 4);
485 if (macData.rf24_Verification == RF24_STR) {
486 const uint16_t other_node = macData.rf24_Addr;
487 RF24NetworkHeader header( other_node, EXTERNAL_DATA_TYPE);
488 ok = network.write(header, &msgTx->message, msgTx->size);
490 else if (macData.rf24_Verification == ARP_BC) {
491 RF24NetworkHeader header( 00, EXTERNAL_DATA_TYPE);
492 if (msgTx->size <= 42) {
493 if (thisNodeAddress == 00) {
495 uint32_t arp_timeout = millis();
497 ok = network.multicast(header, &msgTx->message, msgTx->size, 1);
498 while (millis() - arp_timeout < 5) {
501 network.multicast(header, &msgTx->message, msgTx->size, 1);
502 arp_timeout = millis();
503 while (millis() - arp_timeout < 15) {
506 network.multicast(header, &msgTx->message, msgTx->size, 1);
509 ok = network.write(header, &msgTx->message, msgTx->size);
516 uint8_t lastOctet = tmp[19];
519 RF24NetworkHeader header(00, EXTERNAL_DATA_TYPE);
520 bool sendData =
false;
522 struct in_addr ipDestination;
523 memcpy(&ipDestination.s_addr, &tmp[16], 4);
525 if ((getLocalIP().s_addr & 0x00FFFFFF) == (ipDestination.s_addr & 0x00FFFFFF)) {
526 if ((meshAddr = mesh.getAddress(lastOctet)) > 0) {
527 header.to_node = meshAddr;
531 if (thisNodeID > 0) {
537 else if (thisNodeID > 0) {
540 else if (routingTableSize > 0) {
541 for (
int i = 0; i < routingTableSize; i++) {
542 struct in_addr network;
543 network.s_addr = routingStruct[i].ip.s_addr & routingStruct[i].mask.s_addr;
544 struct in_addr destNet;
545 destNet.s_addr = ipDestination.s_addr & routingStruct[i].mask.s_addr;
547 if (destNet.s_addr == network.s_addr) {
548 uint8_t toNode = routingStruct[i].gw.s_addr >> 24;
550 if ((netAddr = mesh.getAddress(toNode)) > 0) {
551 header.to_node = netAddr;
560 ok = network.write(header, msgTx->message, msgTx->size);
581template<
class mesh_t,
class network_t,
class radio_t>
585 struct timeval selectTimeout;
586 uint8_t buffer[MAX_PAYLOAD_SIZE];
590 FD_SET(tunFd, &socketSet);
592 selectTimeout.tv_sec = 0;
593 selectTimeout.tv_usec = waitDelay * 1000;
595 if (select(tunFd + 1, &socketSet, NULL, NULL, &selectTimeout) != 0) {
596 if (FD_ISSET(tunFd, &socketSet)) {
597 if ((nread = read(tunFd, buffer, MAX_PAYLOAD_SIZE)) >= 0) {
599#if (DEBUG_LEVEL >= 1)
600 std::cout <<
"Tun: Successfully read " << nread <<
" bytes from tun device" << std::endl;
602#if (DEBUG_LEVEL >= 3)
603 std::cout <<
"TunRead: " << std::endl;
604 for (
int i = 0; i < nread; i++)
606 printf(
":%0x :", buffer[i]);
608 std::cout << std::endl;
611 memcpy(&msg.message, &buffer, nread);
613 if (txQueue.size() < 10) {
621#if (DEBUG_LEVEL >= 1)
622 std::cerr <<
"Tun: Error while reading from tun/tap interface." << std::endl;
631template<
class mesh_t,
class network_t,
class radio_t>
635 if (rxQueue.size() < 1)
639 msgStruct* msg = &rxQueue.front();
641 if (msg->size > MAX_PAYLOAD_SIZE)
651 size_t writtenBytes = write(tunFd, &msg->message, msg->size);
652 if (writtenBytes != msg->size)
656 printf(
"Tun: Less bytes written %d to tun/tap device then requested %d.", writtenBytes, msg->size);
661#if (DEBUG_LEVEL >= 1)
662 std::cout <<
"Tun: Successfully wrote " << writtenBytes <<
" bytes to tun device" << std::endl;
666#if (DEBUG_LEVEL >= 3)
668 std::cout <<
"TunRead: " << std::endl;
669 for (
size_t i = 0; i < msg->size; i++) {
672 std::cout << std::endl;
695template<
class mesh_t,
class network_t,
class radio_t>
699 const char* myAddr =
"127.0.0.1";
701 addr.sin_family = AF_INET;
702 ret = inet_aton(myAddr, &addr.sin_addr);
707 addr.sin_port = htons(32001);
709 s = socket(PF_INET, SOCK_DGRAM, 0);
718template<
class mesh_t,
class network_t,
class radio_t>
722 uint8_t buffer[MAX_PAYLOAD_SIZE + 11];
724 memcpy(&buffer[0], &nodeID, 1);
725 memcpy(&buffer[1], &frame.header, 8);
726 memcpy(&buffer[9], &frame.message_size, 2);
727 memcpy(&buffer[11], &frame.message_buffer, frame.message_size);
729 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)