From d6bd300b99a281645a9708f9d1b6d1e9a98341e6 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Wed, 4 Dec 2024 12:12:33 -0600 Subject: [PATCH 1/4] Add MACAddress to config.yaml --- arch/portduino/portduino.ini | 4 ++-- bin/config-dist.yaml | 3 ++- src/platform/portduino/PortduinoGlue.cpp | 21 +++++++++++++++++++++ src/platform/portduino/PortduinoGlue.h | 4 +++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/arch/portduino/portduino.ini b/arch/portduino/portduino.ini index 04fd6db092..28f4beeac9 100644 --- a/arch/portduino/portduino.ini +++ b/arch/portduino/portduino.ini @@ -1,6 +1,6 @@ ; The Portduino based sim environment on top of any host OS, all hardware will be simulated [portduino_base] -platform = https://github.com/meshtastic/platform-native.git#bcd02436cfca91f7d28ad0f7dab977c6aaa781af +platform = https://github.com/meshtastic/platform-native.git#c9d8072fca0fbd5fb630a261452d7057b571d1db framework = arduino build_src_filter = @@ -36,4 +36,4 @@ build_flags = -lstdc++fs -lbluetooth -lgpiod - -lyaml-cpp + -lyaml-cpp \ No newline at end of file diff --git a/bin/config-dist.yaml b/bin/config-dist.yaml index 77680cc63f..663021af5d 100644 --- a/bin/config-dist.yaml +++ b/bin/config-dist.yaml @@ -160,4 +160,5 @@ Webserver: General: MaxNodes: 200 MaxMessageQueue: 100 - ConfigDirectory: /etc/meshtasticd/config.d/ \ No newline at end of file + ConfigDirectory: /etc/meshtasticd/config.d/ +# MACAddress: AA:BB:CC:DD:EE:FF \ No newline at end of file diff --git a/src/platform/portduino/PortduinoGlue.cpp b/src/platform/portduino/PortduinoGlue.cpp index a4485e91ff..b6aa2e2590 100644 --- a/src/platform/portduino/PortduinoGlue.cpp +++ b/src/platform/portduino/PortduinoGlue.cpp @@ -69,6 +69,22 @@ void portduinoCustomInit() portduinoAddArguments(child, childArguments); } +void getMacAddr(uint8_t *dmac) +{ + if (settingsStrings[mac_address].length() > 11) { + dmac[0] = std::stoi(settingsStrings[mac_address].substr(0, 2), nullptr, 16); + dmac[1] = std::stoi(settingsStrings[mac_address].substr(2, 2), nullptr, 16); + dmac[2] = std::stoi(settingsStrings[mac_address].substr(4, 2), nullptr, 16); + dmac[3] = std::stoi(settingsStrings[mac_address].substr(6, 2), nullptr, 16); + dmac[4] = std::stoi(settingsStrings[mac_address].substr(8, 2), nullptr, 16); + dmac[5] = std::stoi(settingsStrings[mac_address].substr(10, 2), nullptr, 16); + std::cout << settingsStrings[mac_address] << std::endl; + exit; + } else { + _getMacAddr(dmac); + } +} + /** apps run under portduino can optionally define a portduinoSetup() to * use portduino specific init code (such as gpioBind) to setup portduino on their host machine, * before running 'arduino' code. @@ -412,7 +428,12 @@ bool loadConfig(const char *configPath) settingsMap[maxnodes] = (yamlConfig["General"]["MaxNodes"]).as(200); settingsMap[maxtophone] = (yamlConfig["General"]["MaxMessageQueue"]).as(100); settingsStrings[config_directory] = (yamlConfig["General"]["ConfigDirectory"]).as(""); + settingsStrings[mac_address] = (yamlConfig["General"]["MACAddress"]).as(""); + // https://stackoverflow.com/a/20326454 + settingsStrings[mac_address].erase( + std::remove(settingsStrings[mac_address].begin(), settingsStrings[mac_address].end(), ':'), + settingsStrings[mac_address].end()); } catch (YAML::Exception &e) { std::cout << "*** Exception " << e.what() << std::endl; return false; diff --git a/src/platform/portduino/PortduinoGlue.h b/src/platform/portduino/PortduinoGlue.h index 1e0223c486..f372fb0ab3 100644 --- a/src/platform/portduino/PortduinoGlue.h +++ b/src/platform/portduino/PortduinoGlue.h @@ -59,7 +59,8 @@ enum configNames { maxtophone, maxnodes, ascii_logs, - config_directory + config_directory, + mac_address }; enum { no_screen, x11, st7789, st7735, st7735s, st7796, ili9341, ili9342, ili9488, hx8357d }; enum { no_touchscreen, xpt2046, stmpe610, gt911, ft5x06 }; @@ -71,3 +72,4 @@ extern std::ofstream traceFile; int initGPIOPin(int pinNum, std::string gpioChipname); bool loadConfig(const char *configPath); static bool ends_with(std::string_view str, std::string_view suffix); +void getMacAddr(uint8_t *dmac); \ No newline at end of file From bd38ad3e4953be2f7ffa88d575cf6496b9c00da1 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Wed, 4 Dec 2024 14:09:03 -0600 Subject: [PATCH 2/4] Better error handling on native, including failing to launch with blank MAC Address and real hardware. --- src/platform/portduino/PortduinoGlue.cpp | 27 +++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/platform/portduino/PortduinoGlue.cpp b/src/platform/portduino/PortduinoGlue.cpp index b6aa2e2590..667ee4c020 100644 --- a/src/platform/portduino/PortduinoGlue.cpp +++ b/src/platform/portduino/PortduinoGlue.cpp @@ -129,10 +129,20 @@ void portduinoSetup() std::cout << "Unable to use " << configPath << " as config file" << std::endl; exit(EXIT_FAILURE); } - } else if (access("config.yaml", R_OK) == 0 && loadConfig("config.yaml")) { - std::cout << "Using local config.yaml as config file" << std::endl; - } else if (access("/etc/meshtasticd/config.yaml", R_OK) == 0 && loadConfig("/etc/meshtasticd/config.yaml")) { - std::cout << "Using /etc/meshtasticd/config.yaml as config file" << std::endl; + } else if (access("config.yaml", R_OK) == 0) { + if (loadConfig("config.yaml")) { + std::cout << "Using local config.yaml as config file" << std::endl; + } else { + std::cout << "Unable to use local config.yaml as config file" << std::endl; + exit(EXIT_FAILURE); + } + } else if (access("/etc/meshtasticd/config.yaml", R_OK) == 0) { + if (loadConfig("/etc/meshtasticd/config.yaml")) { + std::cout << "Using /etc/meshtasticd/config.yaml as config file" << std::endl; + } else { + std::cout << "Unable to use /etc/meshtasticd/config.yaml as config file" << std::endl; + exit(EXIT_FAILURE); + } } else { std::cout << "No 'config.yaml' found, running simulated." << std::endl; settingsMap[maxnodes] = 200; // Default to 200 nodes @@ -153,6 +163,13 @@ void portduinoSetup() } } + uint8_t dmac[6]; + getMacAddr(dmac); + if (dmac[0] == 128 && dmac[1] == 0 && dmac[2] == 0 && dmac[3] == 0 && dmac[4] == 0 && dmac[5] == 1) { + std::cout << "*** Blank MAC Address not allowed! " << std::endl; + exit(EXIT_FAILURE); + } + // Rather important to set this, if not running simulated. randomSeed(time(NULL)); @@ -445,4 +462,4 @@ bool loadConfig(const char *configPath) static bool ends_with(std::string_view str, std::string_view suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; -} +} \ No newline at end of file From 0da54c74e5eedc82255605844245c0ad3350c553 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Fri, 6 Dec 2024 21:28:40 -0600 Subject: [PATCH 3/4] Re-arrange Mac Address handling and add MACAddressSource --- bin/config-dist.yaml | 3 +- src/platform/portduino/PortduinoGlue.cpp | 112 ++++++++++++++++++----- src/platform/portduino/PortduinoGlue.h | 3 +- 3 files changed, 94 insertions(+), 24 deletions(-) diff --git a/bin/config-dist.yaml b/bin/config-dist.yaml index 663021af5d..cb25b36e7a 100644 --- a/bin/config-dist.yaml +++ b/bin/config-dist.yaml @@ -161,4 +161,5 @@ General: MaxNodes: 200 MaxMessageQueue: 100 ConfigDirectory: /etc/meshtasticd/config.d/ -# MACAddress: AA:BB:CC:DD:EE:FF \ No newline at end of file +# MACAddress: AA:BB:CC:DD:EE:FF +# MACAddressSource: eth0 \ No newline at end of file diff --git a/src/platform/portduino/PortduinoGlue.cpp b/src/platform/portduino/PortduinoGlue.cpp index 9c2e6585f1..b6a017d9fd 100644 --- a/src/platform/portduino/PortduinoGlue.cpp +++ b/src/platform/portduino/PortduinoGlue.cpp @@ -5,22 +5,27 @@ #include "sleep.h" #include "target_specific.h" -#include -#include - #include "PortduinoGlue.h" #include "api/ServerAPI.h" #include "linux/gpio/LinuxGPIOPin.h" +#include "meshUtils.h" #include "yaml-cpp/yaml.h" +#include +#include +#include +#include #include +#include #include #include +#include #include std::map settingsMap; std::map settingsStrings; std::ofstream traceFile; char *configPath = nullptr; +char *optionMac = nullptr; // FIXME - move setBluetoothEnable into a HALPlatform class void setBluetoothEnable(bool enable) @@ -49,6 +54,10 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) case 'c': configPath = arg; break; + case 'h': + optionMac = arg; + break; + case ARGP_KEY_ARG: return 0; default: @@ -61,6 +70,7 @@ void portduinoCustomInit() { static struct argp_option options[] = {{"port", 'p', "PORT", 0, "The TCP port to use."}, {"config", 'c', "CONFIG_PATH", 0, "Full path of the .yaml config file to use."}, + {"hwid", 'h', "HWID", 0, "The mac address to assign to this virtual machine"}, {0}}; static void *childArguments; static char doc[] = "Meshtastic native build."; @@ -72,17 +82,46 @@ void portduinoCustomInit() void getMacAddr(uint8_t *dmac) { - if (settingsStrings[mac_address].length() > 11) { - dmac[0] = std::stoi(settingsStrings[mac_address].substr(0, 2), nullptr, 16); - dmac[1] = std::stoi(settingsStrings[mac_address].substr(2, 2), nullptr, 16); - dmac[2] = std::stoi(settingsStrings[mac_address].substr(4, 2), nullptr, 16); - dmac[3] = std::stoi(settingsStrings[mac_address].substr(6, 2), nullptr, 16); - dmac[4] = std::stoi(settingsStrings[mac_address].substr(8, 2), nullptr, 16); - dmac[5] = std::stoi(settingsStrings[mac_address].substr(10, 2), nullptr, 16); + // We should store this value, and short-circuit all this if it's already been set. + if (optionMac != nullptr && strlen(optionMac) > 0) { + if (strlen(optionMac) >= 12) { + MAC_from_string(optionMac, dmac); + std::cout << optionMac << std::endl; + } else { + uint32_t hwId = sscanf(optionMac, "%u", &hwId); + dmac[0] = 0x80; + dmac[1] = 0; + dmac[2] = hwId >> 24; + dmac[3] = hwId >> 16; + dmac[4] = hwId >> 8; + dmac[5] = hwId & 0xff; + } + } else if (settingsStrings[mac_address].length() > 11) { + MAC_from_string(settingsStrings[mac_address], dmac); std::cout << settingsStrings[mac_address] << std::endl; exit; } else { - _getMacAddr(dmac); + + struct hci_dev_info di; + di.dev_id = 0; + bdaddr_t bdaddr; + char addr[18]; + int btsock; + btsock = socket(AF_BLUETOOTH, SOCK_RAW, 1); + if (btsock < 0) { // If anything fails, just return with the default value + return; + } + + if (ioctl(btsock, HCIGETDEVINFO, (void *)&di)) { + return; + } + + dmac[0] = di.bdaddr.b[5]; + dmac[1] = di.bdaddr.b[4]; + dmac[2] = di.bdaddr.b[3]; + dmac[3] = di.bdaddr.b[2]; + dmac[4] = di.bdaddr.b[1]; + dmac[5] = di.bdaddr.b[0]; } } @@ -166,11 +205,12 @@ void portduinoSetup() uint8_t dmac[6]; getMacAddr(dmac); - if (dmac[0] == 128 && dmac[1] == 0 && dmac[2] == 0 && dmac[3] == 0 && dmac[4] == 0 && dmac[5] == 1) { - std::cout << "*** Blank MAC Address not allowed! " << std::endl; + if (dmac[0] == 0 && dmac[1] == 0 && dmac[2] == 0 && dmac[3] == 0 && dmac[4] == 0 && dmac[5] == 0) { + std::cout << "*** Blank MAC Address not allowed!" << std::endl; + std::cout << "Please set a MAC Address in config.yaml using either MACAddress or MACAddressSource." << std::endl; exit(EXIT_FAILURE); } - + printBytes("MAC Address: ", dmac, 6); // Rather important to set this, if not running simulated. randomSeed(time(NULL)); @@ -443,15 +483,27 @@ bool loadConfig(const char *configPath) settingsStrings[webserverrootpath] = (yamlConfig["Webserver"]["RootPath"]).as(""); } - settingsMap[maxnodes] = (yamlConfig["General"]["MaxNodes"]).as(200); - settingsMap[maxtophone] = (yamlConfig["General"]["MaxMessageQueue"]).as(100); - settingsStrings[config_directory] = (yamlConfig["General"]["ConfigDirectory"]).as(""); - settingsStrings[mac_address] = (yamlConfig["General"]["MACAddress"]).as(""); + if (yamlConfig["General"]) { + settingsMap[maxnodes] = (yamlConfig["General"]["MaxNodes"]).as(200); + settingsMap[maxtophone] = (yamlConfig["General"]["MaxMessageQueue"]).as(100); + settingsStrings[config_directory] = (yamlConfig["General"]["ConfigDirectory"]).as(""); + if ((yamlConfig["General"]["MACAddress"]).as("") != "" && + (yamlConfig["General"]["MACAddressSource"]).as("") != "") { + std::cout << "Cannot set both MACAddress and MACAddressSource!" << std::endl; + exit(EXIT_FAILURE); + } + settingsStrings[mac_address] = (yamlConfig["General"]["MACAddress"]).as(""); + if ((yamlConfig["General"]["MACAddressSource"]).as("") != "") { + std::ifstream infile("/sys/class/net/" + (yamlConfig["General"]["MACAddressSource"]).as("") + + "/address"); + std::getline(infile, settingsStrings[mac_address]); + } - // https://stackoverflow.com/a/20326454 - settingsStrings[mac_address].erase( - std::remove(settingsStrings[mac_address].begin(), settingsStrings[mac_address].end(), ':'), - settingsStrings[mac_address].end()); + // https://stackoverflow.com/a/20326454 + settingsStrings[mac_address].erase( + std::remove(settingsStrings[mac_address].begin(), settingsStrings[mac_address].end(), ':'), + settingsStrings[mac_address].end()); + } } catch (YAML::Exception &e) { std::cout << "*** Exception " << e.what() << std::endl; return false; @@ -463,4 +515,20 @@ bool loadConfig(const char *configPath) static bool ends_with(std::string_view str, std::string_view suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; +} + +bool MAC_from_string(std::string mac_str, uint8_t *dmac) +{ + mac_str.erase(std::remove(mac_str.begin(), mac_str.end(), ':'), mac_str.end()); + if (mac_str.length() == 12) { + dmac[0] = std::stoi(settingsStrings[mac_address].substr(0, 2), nullptr, 16); + dmac[1] = std::stoi(settingsStrings[mac_address].substr(2, 2), nullptr, 16); + dmac[2] = std::stoi(settingsStrings[mac_address].substr(4, 2), nullptr, 16); + dmac[3] = std::stoi(settingsStrings[mac_address].substr(6, 2), nullptr, 16); + dmac[4] = std::stoi(settingsStrings[mac_address].substr(8, 2), nullptr, 16); + dmac[5] = std::stoi(settingsStrings[mac_address].substr(10, 2), nullptr, 16); + return true; + } else { + return false; + } } \ No newline at end of file diff --git a/src/platform/portduino/PortduinoGlue.h b/src/platform/portduino/PortduinoGlue.h index f372fb0ab3..01541eeede 100644 --- a/src/platform/portduino/PortduinoGlue.h +++ b/src/platform/portduino/PortduinoGlue.h @@ -72,4 +72,5 @@ extern std::ofstream traceFile; int initGPIOPin(int pinNum, std::string gpioChipname); bool loadConfig(const char *configPath); static bool ends_with(std::string_view str, std::string_view suffix); -void getMacAddr(uint8_t *dmac); \ No newline at end of file +void getMacAddr(uint8_t *dmac); +bool MAC_from_string(std::string mac_str, uint8_t *dmac); \ No newline at end of file From ad1cde109944476d1fe999979faeade2929761d1 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sat, 7 Dec 2024 09:55:26 -0600 Subject: [PATCH 4/4] Bump portduino to remove macaddr function there --- arch/portduino/portduino.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/portduino/portduino.ini b/arch/portduino/portduino.ini index 28f4beeac9..946a1489b6 100644 --- a/arch/portduino/portduino.ini +++ b/arch/portduino/portduino.ini @@ -1,6 +1,6 @@ ; The Portduino based sim environment on top of any host OS, all hardware will be simulated [portduino_base] -platform = https://github.com/meshtastic/platform-native.git#c9d8072fca0fbd5fb630a261452d7057b571d1db +platform = https://github.com/meshtastic/platform-native.git#7fcee253a928535ff8b142704035b4b982f7e2d2 framework = arduino build_src_filter = @@ -36,4 +36,4 @@ build_flags = -lstdc++fs -lbluetooth -lgpiod - -lyaml-cpp \ No newline at end of file + -lyaml-cpp