From ca1d4172dba2b3272a9ec9f3feadd0ed4002111d Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Fri, 1 May 2026 07:21:30 -0500 Subject: [PATCH] Add ulfius webserver support to macos native target --- src/mesh/raspihttp/PiWebServer.cpp | 5 ++- src/mesh/raspihttp/PiWebServer.h | 6 +++- src/platform/portduino/PortduinoGlue.cpp | 39 ++++++++++++++++++++++-- variants/native/portduino/platformio.ini | 34 +++++++++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/mesh/raspihttp/PiWebServer.cpp b/src/mesh/raspihttp/PiWebServer.cpp index 3e9dbe8c2..8aa357b27 100644 --- a/src/mesh/raspihttp/PiWebServer.cpp +++ b/src/mesh/raspihttp/PiWebServer.cpp @@ -34,7 +34,10 @@ Author: Marc Philipp Hammermann mail: marchammermann@googlemail.com */ -#ifdef PORTDUINO_LINUX_HARDWARE +// Mirrors the guard in PiWebServer.h — see comment there. macOS Homebrew +// provides ulfius + deps; Linux pulls them via apt. Either way, this +// translation unit only compiles when the headers are present. +#ifdef ARCH_PORTDUINO #if __has_include() #include "PiWebServer.h" #include "NodeDB.h" diff --git a/src/mesh/raspihttp/PiWebServer.h b/src/mesh/raspihttp/PiWebServer.h index 74b094f8c..24b7de4b1 100644 --- a/src/mesh/raspihttp/PiWebServer.h +++ b/src/mesh/raspihttp/PiWebServer.h @@ -1,5 +1,9 @@ #pragma once -#ifdef PORTDUINO_LINUX_HARDWARE +// Portduino webserver is built whenever the ulfius headers are reachable, +// not only on Linux. macOS users can `brew install ulfius` to enable it; +// without ulfius the entire body is skipped and main.cpp's matching +// __has_include guard avoids referencing the type. +#ifdef ARCH_PORTDUINO #if __has_include() #include "PhoneAPI.h" #include "ulfius-cfg.h" diff --git a/src/platform/portduino/PortduinoGlue.cpp b/src/platform/portduino/PortduinoGlue.cpp index eeb56240d..d79351f2f 100644 --- a/src/platform/portduino/PortduinoGlue.cpp +++ b/src/platform/portduino/PortduinoGlue.cpp @@ -33,6 +33,15 @@ #include #endif +#ifdef __APPLE__ +// Used by getMacAddr()'s macOS fallback to read the en0 link-layer address. +// `getifaddrs()` is the BSD-portable way; `` provides the +// `sockaddr_dl` cast and the `LLADDR()` macro that points at the 6-byte MAC. +#include +#include +#include +#endif + #include "platform/portduino/USBHal.h" portduino_config_struct portduino_config; @@ -156,9 +165,35 @@ void getMacAddr(uint8_t *dmac) dmac[3] = di.bdaddr.b[2]; dmac[4] = di.bdaddr.b[1]; dmac[5] = di.bdaddr.b[0]; +#elif defined(__APPLE__) + // No BlueZ on macOS, but we can fall back to the host's primary + // network interface MAC. `en0` is Wi-Fi on every shipping Mac + // (Ethernet, when present, is en1 or higher), which gives the user + // the same kind of stable, host-derived identifier that the BlueZ + // path provides on Linux. If en0 isn't found or has no MAC, dmac is + // left untouched and the caller's "Blank MAC Address not allowed!" + // check will still fire — preserving existing behavior for users + // who deliberately rely on --hwid or YAML override. + struct ifaddrs *ifap = nullptr; + if (getifaddrs(&ifap) == 0) { + for (struct ifaddrs *p = ifap; p != nullptr; p = p->ifa_next) { + if (p->ifa_addr == nullptr || p->ifa_addr->sa_family != AF_LINK) { + continue; + } + if (strcmp(p->ifa_name, "en0") != 0) { + continue; + } + auto *sdl = reinterpret_cast(p->ifa_addr); + if (sdl->sdl_alen == 6) { + memcpy(dmac, LLADDR(sdl), 6); + break; + } + } + freeifaddrs(ifap); + } #else - // No BlueZ on non-Linux hosts (e.g. macOS). Leave dmac at its default; - // the caller can override via the --hwid CLI flag or the YAML config. + // No platform-specific MAC source; leave dmac at its default. Caller + // can override via the --hwid CLI flag or the YAML config. (void)dmac; #endif } diff --git a/variants/native/portduino/platformio.ini b/variants/native/portduino/platformio.ini index e493da77b..93dea2ca8 100644 --- a/variants/native/portduino/platformio.ini +++ b/variants/native/portduino/platformio.ini @@ -125,6 +125,8 @@ test_testing_command = ; ; Prerequisites (Homebrew): ; brew install platformio yaml-cpp libuv openssl@3 libusb argp-standalone pkg-config +; # Optional: enable the HTTP API (PiWebServer) on macOS: +; brew install ulfius ; ; The macOS-side patches now live upstream: ; * meshtastic/platform-native — `String.h`-shadow shim, `-Wno-enum-constexpr-conversion`, @@ -192,6 +194,13 @@ build_flags = ${portduino_base.build_flags_common} -DHAS_SCREEN=0 -DMESHTASTIC_EXCLUDE_SCREEN=1 !pkg-config --libs openssl --silence-errors || : + ; PiWebServer (src/mesh/raspihttp/PiWebServer.cpp) auto-engages when ulfius + ; headers are reachable via `#if __has_include()`. The `|| :` + ; tail keeps the build green when the user hasn't run `brew install ulfius` + ; — they just don't get the HTTP API in that case. + !pkg-config --cflags --libs libulfius --silence-errors || : + !pkg-config --cflags --libs liborcania --silence-errors || : + !pkg-config --cflags --libs libyder --silence-errors || : ; src/input/Linux*.{cpp,h} drive evdev (``) which doesn't exist ; on macOS. graphics/Panel_sdl.* and graphics/TFTDisplay.cpp pull LovyanGFX ; (which we lib_ignore on macOS for the issue). Neither is needed @@ -206,3 +215,28 @@ build_src_filter = ${native_base.build_src_filter} lib_ignore = ${portduino_base.lib_ignore} LovyanGFX + +; --------------------------------------------------------------------------- +; Same as [env:native-macos] but built with AddressSanitizer for catching +; use-after-free, leaks, and OOB access during local development. Headless +; (no SDL/X11/libinput) so it stays cheap to build. Mirrors the shape of +; [env:native-tft-debug] but without the TFT/X11 dependencies. +; +; pio run -e native-macos-debug +; .pio/build/native-macos-debug/meshtasticd -s +; +; ASan runtime tuning (set in the shell before launching): +; ASAN_OPTIONS=detect_leaks=1:halt_on_error=0:abort_on_error=1 +; MallocStackLogging=1 # macOS: nicer stack traces in malloc reports +; --------------------------------------------------------------------------- +[env:native-macos-debug] +extends = native_base +build_type = debug +build_unflags = ${env:native-macos.build_unflags} +build_flags = ${env:native-macos.build_flags} + -O0 + -g + -fsanitize=address + -fno-omit-frame-pointer +build_src_filter = ${env:native-macos.build_src_filter} +lib_ignore = ${env:native-macos.lib_ignore}