mirror of
https://github.com/meshtastic/firmware.git
synced 2025-04-28 02:32:09 +00:00
Add MACAddress to config.yaml (#5506)
Some checks are pending
CI / setup (check) (push) Waiting to run
CI / setup (esp32) (push) Waiting to run
CI / setup (esp32c3) (push) Waiting to run
CI / setup (esp32c6) (push) Waiting to run
CI / setup (esp32s3) (push) Waiting to run
CI / setup (nrf52840) (push) Waiting to run
CI / setup (rp2040) (push) Waiting to run
CI / setup (stm32) (push) Waiting to run
CI / check (push) Blocked by required conditions
CI / build-esp32 (push) Blocked by required conditions
CI / build-esp32-s3 (push) Blocked by required conditions
CI / build-esp32-c3 (push) Blocked by required conditions
CI / build-esp32-c6 (push) Blocked by required conditions
CI / build-nrf52 (push) Blocked by required conditions
CI / build-rpi2040 (push) Blocked by required conditions
CI / build-stm32 (push) Blocked by required conditions
CI / package-raspbian (push) Waiting to run
CI / package-raspbian-armv7l (push) Waiting to run
CI / package-native (push) Waiting to run
CI / after-checks (push) Blocked by required conditions
CI / gather-artifacts (esp32) (push) Blocked by required conditions
CI / gather-artifacts (esp32c3) (push) Blocked by required conditions
CI / gather-artifacts (esp32c6) (push) Blocked by required conditions
CI / gather-artifacts (esp32s3) (push) Blocked by required conditions
CI / gather-artifacts (nrf52840) (push) Blocked by required conditions
CI / gather-artifacts (rp2040) (push) Blocked by required conditions
CI / gather-artifacts (stm32) (push) Blocked by required conditions
CI / release-artifacts (push) Blocked by required conditions
CI / release-firmware (esp32) (push) Blocked by required conditions
CI / release-firmware (esp32c3) (push) Blocked by required conditions
CI / release-firmware (esp32c6) (push) Blocked by required conditions
CI / release-firmware (esp32s3) (push) Blocked by required conditions
CI / release-firmware (nrf52840) (push) Blocked by required conditions
CI / release-firmware (rp2040) (push) Blocked by required conditions
CI / release-firmware (stm32) (push) Blocked by required conditions
Flawfinder Scan / Flawfinder (push) Waiting to run
Some checks are pending
CI / setup (check) (push) Waiting to run
CI / setup (esp32) (push) Waiting to run
CI / setup (esp32c3) (push) Waiting to run
CI / setup (esp32c6) (push) Waiting to run
CI / setup (esp32s3) (push) Waiting to run
CI / setup (nrf52840) (push) Waiting to run
CI / setup (rp2040) (push) Waiting to run
CI / setup (stm32) (push) Waiting to run
CI / check (push) Blocked by required conditions
CI / build-esp32 (push) Blocked by required conditions
CI / build-esp32-s3 (push) Blocked by required conditions
CI / build-esp32-c3 (push) Blocked by required conditions
CI / build-esp32-c6 (push) Blocked by required conditions
CI / build-nrf52 (push) Blocked by required conditions
CI / build-rpi2040 (push) Blocked by required conditions
CI / build-stm32 (push) Blocked by required conditions
CI / package-raspbian (push) Waiting to run
CI / package-raspbian-armv7l (push) Waiting to run
CI / package-native (push) Waiting to run
CI / after-checks (push) Blocked by required conditions
CI / gather-artifacts (esp32) (push) Blocked by required conditions
CI / gather-artifacts (esp32c3) (push) Blocked by required conditions
CI / gather-artifacts (esp32c6) (push) Blocked by required conditions
CI / gather-artifacts (esp32s3) (push) Blocked by required conditions
CI / gather-artifacts (nrf52840) (push) Blocked by required conditions
CI / gather-artifacts (rp2040) (push) Blocked by required conditions
CI / gather-artifacts (stm32) (push) Blocked by required conditions
CI / release-artifacts (push) Blocked by required conditions
CI / release-firmware (esp32) (push) Blocked by required conditions
CI / release-firmware (esp32c3) (push) Blocked by required conditions
CI / release-firmware (esp32c6) (push) Blocked by required conditions
CI / release-firmware (esp32s3) (push) Blocked by required conditions
CI / release-firmware (nrf52840) (push) Blocked by required conditions
CI / release-firmware (rp2040) (push) Blocked by required conditions
CI / release-firmware (stm32) (push) Blocked by required conditions
Flawfinder Scan / Flawfinder (push) Waiting to run
* Add MACAddress to config.yaml * Better error handling on native, including failing to launch with blank MAC Address and real hardware. * Re-arrange Mac Address handling and add MACAddressSource * Bump portduino to remove macaddr function there --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
parent
b99e57a6fa
commit
4a34bf648f
@ -1,6 +1,6 @@
|
|||||||
; The Portduino based sim environment on top of any host OS, all hardware will be simulated
|
; The Portduino based sim environment on top of any host OS, all hardware will be simulated
|
||||||
[portduino_base]
|
[portduino_base]
|
||||||
platform = https://github.com/meshtastic/platform-native.git#bcd02436cfca91f7d28ad0f7dab977c6aaa781af
|
platform = https://github.com/meshtastic/platform-native.git#7fcee253a928535ff8b142704035b4b982f7e2d2
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
|
||||||
build_src_filter =
|
build_src_filter =
|
||||||
|
@ -161,3 +161,5 @@ General:
|
|||||||
MaxNodes: 200
|
MaxNodes: 200
|
||||||
MaxMessageQueue: 100
|
MaxMessageQueue: 100
|
||||||
ConfigDirectory: /etc/meshtasticd/config.d/
|
ConfigDirectory: /etc/meshtasticd/config.d/
|
||||||
|
# MACAddress: AA:BB:CC:DD:EE:FF
|
||||||
|
# MACAddressSource: eth0
|
@ -5,22 +5,27 @@
|
|||||||
#include "sleep.h"
|
#include "sleep.h"
|
||||||
#include "target_specific.h"
|
#include "target_specific.h"
|
||||||
|
|
||||||
#include <Utility.h>
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include "PortduinoGlue.h"
|
#include "PortduinoGlue.h"
|
||||||
#include "api/ServerAPI.h"
|
#include "api/ServerAPI.h"
|
||||||
#include "linux/gpio/LinuxGPIOPin.h"
|
#include "linux/gpio/LinuxGPIOPin.h"
|
||||||
|
#include "meshUtils.h"
|
||||||
#include "yaml-cpp/yaml.h"
|
#include "yaml-cpp/yaml.h"
|
||||||
|
#include <Utility.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <bluetooth/bluetooth.h>
|
||||||
|
#include <bluetooth/hci.h>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
std::map<configNames, int> settingsMap;
|
std::map<configNames, int> settingsMap;
|
||||||
std::map<configNames, std::string> settingsStrings;
|
std::map<configNames, std::string> settingsStrings;
|
||||||
std::ofstream traceFile;
|
std::ofstream traceFile;
|
||||||
char *configPath = nullptr;
|
char *configPath = nullptr;
|
||||||
|
char *optionMac = nullptr;
|
||||||
|
|
||||||
// FIXME - move setBluetoothEnable into a HALPlatform class
|
// FIXME - move setBluetoothEnable into a HALPlatform class
|
||||||
void setBluetoothEnable(bool enable)
|
void setBluetoothEnable(bool enable)
|
||||||
@ -49,6 +54,10 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
|
|||||||
case 'c':
|
case 'c':
|
||||||
configPath = arg;
|
configPath = arg;
|
||||||
break;
|
break;
|
||||||
|
case 'h':
|
||||||
|
optionMac = arg;
|
||||||
|
break;
|
||||||
|
|
||||||
case ARGP_KEY_ARG:
|
case ARGP_KEY_ARG:
|
||||||
return 0;
|
return 0;
|
||||||
default:
|
default:
|
||||||
@ -61,6 +70,7 @@ void portduinoCustomInit()
|
|||||||
{
|
{
|
||||||
static struct argp_option options[] = {{"port", 'p', "PORT", 0, "The TCP port to use."},
|
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."},
|
{"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}};
|
{0}};
|
||||||
static void *childArguments;
|
static void *childArguments;
|
||||||
static char doc[] = "Meshtastic native build.";
|
static char doc[] = "Meshtastic native build.";
|
||||||
@ -70,6 +80,51 @@ void portduinoCustomInit()
|
|||||||
portduinoAddArguments(child, childArguments);
|
portduinoAddArguments(child, childArguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getMacAddr(uint8_t *dmac)
|
||||||
|
{
|
||||||
|
// 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 {
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** apps run under portduino can optionally define a portduinoSetup() to
|
/** 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,
|
* use portduino specific init code (such as gpioBind) to setup portduino on their host machine,
|
||||||
* before running 'arduino' code.
|
* before running 'arduino' code.
|
||||||
@ -114,10 +169,20 @@ void portduinoSetup()
|
|||||||
std::cout << "Unable to use " << configPath << " as config file" << std::endl;
|
std::cout << "Unable to use " << configPath << " as config file" << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
} else if (access("config.yaml", R_OK) == 0 && loadConfig("config.yaml")) {
|
} else if (access("config.yaml", R_OK) == 0) {
|
||||||
|
if (loadConfig("config.yaml")) {
|
||||||
std::cout << "Using local config.yaml as config file" << std::endl;
|
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")) {
|
} 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;
|
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 {
|
} else {
|
||||||
std::cout << "No 'config.yaml' found, running simulated." << std::endl;
|
std::cout << "No 'config.yaml' found, running simulated." << std::endl;
|
||||||
settingsMap[maxnodes] = 200; // Default to 200 nodes
|
settingsMap[maxnodes] = 200; // Default to 200 nodes
|
||||||
@ -138,6 +203,14 @@ void portduinoSetup()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t dmac[6];
|
||||||
|
getMacAddr(dmac);
|
||||||
|
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.
|
// Rather important to set this, if not running simulated.
|
||||||
randomSeed(time(NULL));
|
randomSeed(time(NULL));
|
||||||
|
|
||||||
@ -410,10 +483,27 @@ bool loadConfig(const char *configPath)
|
|||||||
settingsStrings[webserverrootpath] = (yamlConfig["Webserver"]["RootPath"]).as<std::string>("");
|
settingsStrings[webserverrootpath] = (yamlConfig["Webserver"]["RootPath"]).as<std::string>("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (yamlConfig["General"]) {
|
||||||
settingsMap[maxnodes] = (yamlConfig["General"]["MaxNodes"]).as<int>(200);
|
settingsMap[maxnodes] = (yamlConfig["General"]["MaxNodes"]).as<int>(200);
|
||||||
settingsMap[maxtophone] = (yamlConfig["General"]["MaxMessageQueue"]).as<int>(100);
|
settingsMap[maxtophone] = (yamlConfig["General"]["MaxMessageQueue"]).as<int>(100);
|
||||||
settingsStrings[config_directory] = (yamlConfig["General"]["ConfigDirectory"]).as<std::string>("");
|
settingsStrings[config_directory] = (yamlConfig["General"]["ConfigDirectory"]).as<std::string>("");
|
||||||
|
if ((yamlConfig["General"]["MACAddress"]).as<std::string>("") != "" &&
|
||||||
|
(yamlConfig["General"]["MACAddressSource"]).as<std::string>("") != "") {
|
||||||
|
std::cout << "Cannot set both MACAddress and MACAddressSource!" << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
settingsStrings[mac_address] = (yamlConfig["General"]["MACAddress"]).as<std::string>("");
|
||||||
|
if ((yamlConfig["General"]["MACAddressSource"]).as<std::string>("") != "") {
|
||||||
|
std::ifstream infile("/sys/class/net/" + (yamlConfig["General"]["MACAddressSource"]).as<std::string>("") +
|
||||||
|
"/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());
|
||||||
|
}
|
||||||
} catch (YAML::Exception &e) {
|
} catch (YAML::Exception &e) {
|
||||||
std::cout << "*** Exception " << e.what() << std::endl;
|
std::cout << "*** Exception " << e.what() << std::endl;
|
||||||
return false;
|
return false;
|
||||||
@ -426,3 +516,19 @@ 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;
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -59,7 +59,8 @@ enum configNames {
|
|||||||
maxtophone,
|
maxtophone,
|
||||||
maxnodes,
|
maxnodes,
|
||||||
ascii_logs,
|
ascii_logs,
|
||||||
config_directory
|
config_directory,
|
||||||
|
mac_address
|
||||||
};
|
};
|
||||||
enum { no_screen, x11, st7789, st7735, st7735s, st7796, ili9341, ili9342, ili9488, hx8357d };
|
enum { no_screen, x11, st7789, st7735, st7735s, st7796, ili9341, ili9342, ili9488, hx8357d };
|
||||||
enum { no_touchscreen, xpt2046, stmpe610, gt911, ft5x06 };
|
enum { no_touchscreen, xpt2046, stmpe610, gt911, ft5x06 };
|
||||||
@ -71,3 +72,5 @@ extern std::ofstream traceFile;
|
|||||||
int initGPIOPin(int pinNum, std::string gpioChipname);
|
int initGPIOPin(int pinNum, std::string gpioChipname);
|
||||||
bool loadConfig(const char *configPath);
|
bool loadConfig(const char *configPath);
|
||||||
static bool ends_with(std::string_view str, std::string_view suffix);
|
static bool ends_with(std::string_view str, std::string_view suffix);
|
||||||
|
void getMacAddr(uint8_t *dmac);
|
||||||
|
bool MAC_from_string(std::string mac_str, uint8_t *dmac);
|
Loading…
Reference in New Issue
Block a user