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) {
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() + 750;
473 while (!txQueue.empty() && network.external_queue.size() == 0) {
475 uint32_t queueSize = txQueue.size();
476 if (millis() > txQueueTimer && 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;
499 uint32_t RF24_STR = 0x34324652;
500 uint32_t ARP_BC = 0xFFFFFFFF;
503 uint32_t rf24_Verification;
508 memcpy(&macData.rf24_Addr, tmp + 4, 2);
509 memcpy(&macData.rf24_Verification, tmp, 4);
511 if (macData.rf24_Verification == RF24_STR) {
512 const uint16_t other_node = macData.rf24_Addr;
513 RF24NetworkHeader header( other_node, EXTERNAL_DATA_TYPE);
514 ok = network.write(header, &msgTx->message, msgTx->size);
516 else if (macData.rf24_Verification == ARP_BC) {
517 RF24NetworkHeader header( 00, EXTERNAL_DATA_TYPE);
518 if (msgTx->size <= 42) {
519 if (thisNodeAddress == 00) {
521 uint32_t arp_timeout = millis();
523 ok = network.multicast(header, &msgTx->message, msgTx->size, 1);
524 while (millis() - arp_timeout < 5) {
525 uint8_t returnVal = network.update();
526 if (returnVal == NETWORK_OVERRUN) {
529 else if (returnVal == NETWORK_CORRUPTION) {
533 network.multicast(header, &msgTx->message, msgTx->size, 1);
534 arp_timeout = millis();
535 while (millis() - arp_timeout < 15) {
536 uint8_t returnVal = network.update();
537 if (returnVal == NETWORK_OVERRUN) {
540 else if (returnVal == NETWORK_CORRUPTION) {
544 network.multicast(header, &msgTx->message, msgTx->size, 1);
547 ok = network.write(header, &msgTx->message, msgTx->size);
554 uint8_t lastOctet = tmp[19];
557 RF24NetworkHeader header(00, EXTERNAL_DATA_TYPE);
558 bool sendData =
false;
560 struct in_addr ipDestination;
561 memcpy(&ipDestination.s_addr, &tmp[16], 4);
563 if ((getLocalIP().s_addr & 0x00FFFFFF) == (ipDestination.s_addr & 0x00FFFFFF)) {
564 if ((meshAddr = mesh.getAddress(lastOctet)) > 0) {
565 header.to_node = meshAddr;
569 if (thisNodeID > 0) {
575 else if (thisNodeID > 0) {
578 else if (routingTableSize > 0) {
579 for (
int i = 0; i < routingTableSize; i++) {
580 struct in_addr network;
581 network.s_addr = routingStruct[i].ip.s_addr & routingStruct[i].mask.s_addr;
582 struct in_addr destNet;
583 destNet.s_addr = ipDestination.s_addr & routingStruct[i].mask.s_addr;
585 if (destNet.s_addr == network.s_addr) {
586 uint8_t toNode = routingStruct[i].gw.s_addr >> 24;
588 if ((netAddr = mesh.getAddress(toNode)) > 0) {
589 header.to_node = netAddr;
598 ok = network.write(header, msgTx->message, msgTx->size);
619template<
class mesh_t,
class network_t,
class radio_t>
620void ESBGateway<mesh_t, network_t, radio_t>::handleRX(uint32_t waitDelay)
623 struct timeval selectTimeout;
624 uint8_t buffer[MAX_PAYLOAD_SIZE];
628 FD_SET(tunFd, &socketSet);
630 selectTimeout.tv_sec = 0;
631 selectTimeout.tv_usec = waitDelay * 1000;
633 uint32_t tunTimeout = millis();
635 while (select(tunFd + 1, &socketSet, NULL, NULL, &selectTimeout) != 0) {
636 if (FD_ISSET(tunFd, &socketSet)) {
637 if ((nread = read(tunFd, buffer, MAX_PAYLOAD_SIZE)) >= 0) {
638#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
639 std::cout <<
"Tun: Successfully read " << nread <<
" bytes from tun device" << std::endl;
641#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
642 std::cout <<
"TunRead: " << std::endl;
643 for (
int i = 0; i < nread; i++)
645 printf(
":%0x :", buffer[i]);
647 std::cout << std::endl;
650 memcpy(&msg.message, &buffer, nread);
655#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
656 std::cerr <<
"Tun: Error while reading from tun/tap interface." << std::endl;
660 if (millis() - tunTimeout > 750) {
668template<
class mesh_t,
class network_t,
class radio_t>
669void ESBGateway<mesh_t, network_t, radio_t>::handleTX()
672 if (rxQueue.size() < 1)
676 msgStruct* msg = &rxQueue.front();
678 if (msg->size > MAX_PAYLOAD_SIZE)
688 size_t writtenBytes = write(tunFd, &msg->message, msg->size);
689 if (writtenBytes != msg->size)
692#if RF24GATEWAY_DEBUG_LEVEL >= 1
693 printf(
"Tun: Less bytes written %d to tun/tap device then requested %d.", writtenBytes, msg->size);
698#if (RF24GATEWAY_DEBUG_LEVEL >= 1)
699 std::cout <<
"Tun: Successfully wrote " << writtenBytes <<
" bytes to tun device" << std::endl;
703#if (RF24GATEWAY_DEBUG_LEVEL >= 3)
705 std::cout <<
"TunRead: " << std::endl;
706 for (
size_t i = 0; i < msg->size; i++) {
709 std::cout << std::endl;
732template<
class mesh_t,
class network_t,
class radio_t>
733void ESBGateway<mesh_t, network_t, radio_t>::setupSocket()
736 const char* myAddr =
"127.0.0.1";
738 addr.sin_family = AF_INET;
739 ret = inet_aton(myAddr, &addr.sin_addr);
744 addr.sin_port = htons(32001);
746 s = socket(PF_INET, SOCK_DGRAM, 0);
755template<
class mesh_t,
class network_t,
class radio_t>
759 uint8_t buffer[MAX_PAYLOAD_SIZE + 11];
761 memcpy(&buffer[0], &nodeID, 1);
762 memcpy(&buffer[1], &frame.header, 8);
763 memcpy(&buffer[9], &frame.message_size, 2);
764 memcpy(&buffer[11], &frame.message_buffer, frame.message_size);
766 int ret = sendto(s, &buffer, frame.message_size + 11, 0, (
struct sockaddr*)&addr,
sizeof(addr));