Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MACAddress to config.yaml #5506

Merged
merged 8 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions arch/portduino/portduino.ini
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -36,4 +36,4 @@ build_flags =
-lstdc++fs
-lbluetooth
-lgpiod
-lyaml-cpp
-lyaml-cpp
3 changes: 2 additions & 1 deletion bin/config-dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@ Webserver:
General:
MaxNodes: 200
MaxMessageQueue: 100
ConfigDirectory: /etc/meshtasticd/config.d/
ConfigDirectory: /etc/meshtasticd/config.d/
# MACAddress: AA:BB:CC:DD:EE:FF
48 changes: 43 additions & 5 deletions src/platform/portduino/PortduinoGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -113,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
Expand All @@ -137,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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about: "MAC address should be set in 'config.yaml'"? Then it's easier to figure out how to fix it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Still a WIP. I'll finalize the error message once the new options are available.

exit(EXIT_FAILURE);
}

// Rather important to set this, if not running simulated.
randomSeed(time(NULL));

Expand Down Expand Up @@ -412,7 +445,12 @@ bool loadConfig(const char *configPath)
settingsMap[maxnodes] = (yamlConfig["General"]["MaxNodes"]).as<int>(200);
settingsMap[maxtophone] = (yamlConfig["General"]["MaxMessageQueue"]).as<int>(100);
settingsStrings[config_directory] = (yamlConfig["General"]["ConfigDirectory"]).as<std::string>("");
settingsStrings[mac_address] = (yamlConfig["General"]["MACAddress"]).as<std::string>("");

// 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;
Expand All @@ -424,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;
}
}
4 changes: 3 additions & 1 deletion src/platform/portduino/PortduinoGlue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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);
Loading