From 09ba198bb434f9410ab47ff8f2960e0071e445a3 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Wed, 19 Jun 2024 10:53:56 -0600 Subject: [PATCH 1/3] Warnings if unable to config interface --- RF24Gateway.cpp | 7 ++++++- examples/ncurses/RF24Gateway_ncurses.cpp | 16 ++++++++++++++-- examples/ncursesInt/RF24Gateway_ncursesInt.cpp | 16 ++++++++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/RF24Gateway.cpp b/RF24Gateway.cpp index 62e4952..a84acac 100644 --- a/RF24Gateway.cpp +++ b/RF24Gateway.cpp @@ -209,7 +209,12 @@ int ESBGateway::allocateTunDevice(char* dev, int fla // close(fd); //#if (RF24GATEWAY_DEBUG_LEVEL >= 1) std::cerr << "RF24Gw: Error: enabling TUNSETIFF" << std::endl; - std::cerr << "RF24Gw: If changing from TAP/TUN, run 'sudo ip link delete tun_nrf24' to remove the interface" << std::endl; + uint32_t UID = getuid(); + if (UID) { + std::cout << "Not running as root, preconfigure the interface as follows, where 'pi' is your username" << std::endl; + std::cout << "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue" << std::endl; + std::cout << "sudo ifconfig tun_nrf24 10.10.2.2/24" << std::endl; + } return -1; //#endif } diff --git a/examples/ncurses/RF24Gateway_ncurses.cpp b/examples/ncurses/RF24Gateway_ncurses.cpp index 7e0f360..4a7f01e 100644 --- a/examples/ncurses/RF24Gateway_ncurses.cpp +++ b/examples/ncurses/RF24Gateway_ncurses.cpp @@ -531,7 +531,8 @@ void drawCfg(bool isConf) sleep(1); wattron(win, COLOR_PAIR(1)); - mvwprintw(win, 5, 1, isConf ? "IP Configuration\n" : "**Interface Not Configured:**\n"); + mvwprintw(win, 4, 1, isConf ? "IP Configuration\n" : "**Interface Not Configured:**\n"); + mvwprintw(win, 5, 1, "IP 10.10.2.2 and Subnet Mask 255.255.255.0 is default\n"); wattroff(win, COLOR_PAIR(1)); mvwprintw(win, 6, 1, "Enter IP Address: \n"); refresh(); @@ -546,7 +547,18 @@ void drawCfg(bool isConf) if (strlen(ip) >= 6 && strlen(mask) >= 7) { - gw.setIP(ip, mask); + if(gw.setIP(ip, mask) < 0){ + mvwprintw(win, 8, 1, "Unable to set IP/Subnet \n"); + uint32_t UID = getuid(); + if (UID) { + mvwprintw(win, 9, 1, "Not running as root, configure as follows, where 'pi' is your username:\n"); + mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue\n"); + mvwprintw(win, 11, 1, "sudo ifconfig tun_nrf24 10.10.2.2/24\n"); + } + refresh(); + sleep(10); + } + } else { diff --git a/examples/ncursesInt/RF24Gateway_ncursesInt.cpp b/examples/ncursesInt/RF24Gateway_ncursesInt.cpp index 3f87b5c..2bfddc7 100644 --- a/examples/ncursesInt/RF24Gateway_ncursesInt.cpp +++ b/examples/ncursesInt/RF24Gateway_ncursesInt.cpp @@ -559,7 +559,8 @@ void drawCfg(bool isConf) sleep(1); wattron(win, COLOR_PAIR(1)); - mvwprintw(win, 5, 1, isConf ? "IP Configuration\n" : "**Interface Not Configured:**\n"); + mvwprintw(win, 4, 1, isConf ? "IP Configuration\n" : "**Interface Not Configured:**\n"); + mvwprintw(win, 5, 1, "IP 10.10.2.2 and Subnet Mask 255.255.255.0 is default\n"); wattroff(win, COLOR_PAIR(1)); mvwprintw(win, 6, 1, "Enter IP Address: \n"); refresh(); @@ -574,7 +575,18 @@ void drawCfg(bool isConf) if (strlen(ip) >= 6 && strlen(mask) >= 7) { - gw.setIP(ip, mask); + if(gw.setIP(ip, mask) < 0){ + mvwprintw(win, 8, 1, "Unable to set IP/Subnet \n"); + uint32_t UID = getuid(); + if (UID) { + mvwprintw(win, 9, 1, "Not running as root, configure as follows, where 'pi' is your username:\n"); + mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue\n"); + mvwprintw(win, 11, 1, "sudo ifconfig tun_nrf24 10.10.2.2/24\n"); + } + refresh(); + sleep(10); + } + } else { From 11f0d14e8583478a48e903c0171eb973ce314e62 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Wed, 19 Jun 2024 10:57:38 -0600 Subject: [PATCH 2/3] clang format --- examples/ncurses/RF24Gateway_ncurses.cpp | 21 +++++++++---------- .../ncursesInt/RF24Gateway_ncursesInt.cpp | 21 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/examples/ncurses/RF24Gateway_ncurses.cpp b/examples/ncurses/RF24Gateway_ncurses.cpp index 4a7f01e..54a91cf 100644 --- a/examples/ncurses/RF24Gateway_ncurses.cpp +++ b/examples/ncurses/RF24Gateway_ncurses.cpp @@ -547,18 +547,17 @@ void drawCfg(bool isConf) if (strlen(ip) >= 6 && strlen(mask) >= 7) { - if(gw.setIP(ip, mask) < 0){ - mvwprintw(win, 8, 1, "Unable to set IP/Subnet \n"); - uint32_t UID = getuid(); - if (UID) { - mvwprintw(win, 9, 1, "Not running as root, configure as follows, where 'pi' is your username:\n"); - mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue\n"); - mvwprintw(win, 11, 1, "sudo ifconfig tun_nrf24 10.10.2.2/24\n"); - } - refresh(); - sleep(10); + if (gw.setIP(ip, mask) < 0) { + mvwprintw(win, 8, 1, "Unable to set IP/Subnet \n"); + uint32_t UID = getuid(); + if (UID) { + mvwprintw(win, 9, 1, "Not running as root, configure as follows, where 'pi' is your username:\n"); + mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue\n"); + mvwprintw(win, 11, 1, "sudo ifconfig tun_nrf24 10.10.2.2/24\n"); + } + refresh(); + sleep(10); } - } else { diff --git a/examples/ncursesInt/RF24Gateway_ncursesInt.cpp b/examples/ncursesInt/RF24Gateway_ncursesInt.cpp index 2bfddc7..c09aca4 100644 --- a/examples/ncursesInt/RF24Gateway_ncursesInt.cpp +++ b/examples/ncursesInt/RF24Gateway_ncursesInt.cpp @@ -575,18 +575,17 @@ void drawCfg(bool isConf) if (strlen(ip) >= 6 && strlen(mask) >= 7) { - if(gw.setIP(ip, mask) < 0){ - mvwprintw(win, 8, 1, "Unable to set IP/Subnet \n"); - uint32_t UID = getuid(); - if (UID) { - mvwprintw(win, 9, 1, "Not running as root, configure as follows, where 'pi' is your username:\n"); - mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue\n"); - mvwprintw(win, 11, 1, "sudo ifconfig tun_nrf24 10.10.2.2/24\n"); - } - refresh(); - sleep(10); + if (gw.setIP(ip, mask) < 0) { + mvwprintw(win, 8, 1, "Unable to set IP/Subnet \n"); + uint32_t UID = getuid(); + if (UID) { + mvwprintw(win, 9, 1, "Not running as root, configure as follows, where 'pi' is your username:\n"); + mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue\n"); + mvwprintw(win, 11, 1, "sudo ifconfig tun_nrf24 10.10.2.2/24\n"); + } + refresh(); + sleep(10); } - } else { From 1445413c2d11c683356eacebe86975a215036bba Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Wed, 19 Jun 2024 11:42:02 -0600 Subject: [PATCH 3/3] Use getlogin() to get user for commands - Suggestion per @2bndy5 --- RF24Gateway.cpp | 4 ++-- examples/ncurses/RF24Gateway_ncurses.cpp | 4 ++-- examples/ncursesInt/RF24Gateway_ncursesInt.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/RF24Gateway.cpp b/RF24Gateway.cpp index a84acac..2add724 100644 --- a/RF24Gateway.cpp +++ b/RF24Gateway.cpp @@ -211,8 +211,8 @@ int ESBGateway::allocateTunDevice(char* dev, int fla 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, where 'pi' is your username" << std::endl; - std::cout << "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue" << std::endl; + 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; diff --git a/examples/ncurses/RF24Gateway_ncurses.cpp b/examples/ncurses/RF24Gateway_ncurses.cpp index 54a91cf..955f1e5 100644 --- a/examples/ncurses/RF24Gateway_ncurses.cpp +++ b/examples/ncurses/RF24Gateway_ncurses.cpp @@ -551,8 +551,8 @@ void drawCfg(bool isConf) mvwprintw(win, 8, 1, "Unable to set IP/Subnet \n"); uint32_t UID = getuid(); if (UID) { - mvwprintw(win, 9, 1, "Not running as root, configure as follows, where 'pi' is your username:\n"); - mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue\n"); + mvwprintw(win, 9, 1, "Not running as root, configure as follows:\n"); + mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user %s multi_queue\n", getlogin()); mvwprintw(win, 11, 1, "sudo ifconfig tun_nrf24 10.10.2.2/24\n"); } refresh(); diff --git a/examples/ncursesInt/RF24Gateway_ncursesInt.cpp b/examples/ncursesInt/RF24Gateway_ncursesInt.cpp index c09aca4..e846ec7 100644 --- a/examples/ncursesInt/RF24Gateway_ncursesInt.cpp +++ b/examples/ncursesInt/RF24Gateway_ncursesInt.cpp @@ -579,8 +579,8 @@ void drawCfg(bool isConf) mvwprintw(win, 8, 1, "Unable to set IP/Subnet \n"); uint32_t UID = getuid(); if (UID) { - mvwprintw(win, 9, 1, "Not running as root, configure as follows, where 'pi' is your username:\n"); - mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue\n"); + mvwprintw(win, 9, 1, "Not running as root, configure as follows:\n"); + mvwprintw(win, 10, 1, "sudo ip tuntap add dev tun_nrf24 mode tun user %s multi_queue\n", getlogin()); mvwprintw(win, 11, 1, "sudo ifconfig tun_nrf24 10.10.2.2/24\n"); } refresh();