diff --git a/build.sh b/build.sh index 8fa3613b1..c07a94444 100755 --- a/build.sh +++ b/build.sh @@ -1623,7 +1623,14 @@ apply_esp32_lora_ota_size_profile() { # defaults because they are installed over USB. export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DWEBCONFIG_DISABLED=1" if is_mqtt_bridge_target "$env_name"; then - export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DLIGHTWEIGHT_WIFI_OTA=1" + export MESHCORE_PORTABLE_NANO_LIBC=1 + export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DLIGHTWEIGHT_WIFI_OTA=1 -DPORTABLE_MQTT_OBSERVER=1 -DPORTABLE_ESP32_MINIMAL_CLI=1 -DPORTABLE_ESP32_NANO_LIBC=1" + append_platformio_build_unflags "-DWITH_SNMP=1 -DMQTT_DEBUG=1 -DMQTT_MEMORY_DEBUG=1 -DDISPLAY_CLASS=SSD1306Display -DENV_INCLUDE_GPS=1 -DENV_INCLUDE_AHTX0=1 -DENV_INCLUDE_BME280=1 -DENV_INCLUDE_BMP280=1 -DENV_INCLUDE_SHTC3=1 -DENV_INCLUDE_SHT4X=1 -DENV_INCLUDE_LPS22HB=1 -DENV_INCLUDE_INA3221=1 -DENV_INCLUDE_INA219=1 -DENV_INCLUDE_INA226=1 -DENV_INCLUDE_INA260=1 -DENV_INCLUDE_MLX90614=1 -DENV_INCLUDE_VL53L0X=1 -DENV_INCLUDE_BME680=1 -DENV_INCLUDE_BMP085=1 -DENV_INCLUDE_RAK12035=1 -DENV_INCLUDE_BME680_BSEC=1" + elif [[ "${env_name,,}" == *bridge_espnow* ]]; then + export MESHCORE_PORTABLE_NANO_LIBC=1 + append_platformio_build_unflags "-DLIGHTWEIGHT_WIFI_OTA=1" + export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -ULIGHTWEIGHT_WIFI_OTA -DDISABLE_WIFI_OTA=1 -DPORTABLE_ESP32_MINIMAL_CLI=1 -DPORTABLE_ESP32_NANO_LIBC=1" + append_platformio_build_unflags "-DDISPLAY_CLASS=SSD1306Display -DENV_INCLUDE_GPS=1 -DENV_INCLUDE_AHTX0=1 -DENV_INCLUDE_BME280=1 -DENV_INCLUDE_BMP280=1 -DENV_INCLUDE_SHTC3=1 -DENV_INCLUDE_SHT4X=1 -DENV_INCLUDE_LPS22HB=1 -DENV_INCLUDE_INA3221=1 -DENV_INCLUDE_INA219=1 -DENV_INCLUDE_INA226=1 -DENV_INCLUDE_INA260=1 -DENV_INCLUDE_MLX90614=1 -DENV_INCLUDE_VL53L0X=1 -DENV_INCLUDE_BME680=1 -DENV_INCLUDE_BMP085=1 -DENV_INCLUDE_RAK12035=1 -DENV_INCLUDE_BME680_BSEC=1" else append_platformio_build_unflags "-DLIGHTWEIGHT_WIFI_OTA=1" export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -ULIGHTWEIGHT_WIFI_OTA -DDISABLE_WIFI_OTA=1" @@ -1982,6 +1989,7 @@ build_firmware() { fi export PLATFORMIO_BUILD_FLAGS="${original_platformio_build_flags} -DFIRMWARE_BUILD_DATE='\"${firmware_build_date}\"' -DFIRMWARE_BUILD_EPOCH=${firmware_build_epoch} -DFIRMWARE_VERSION='\"${embedded_version_string}\"' -DOTA_VARIANT='\"${env_name}\"'${mota_target_flag}" + unset MESHCORE_PORTABLE_NANO_LIBC disable_debug_flags apply_debug_overrides apply_mqtt_bridge_override @@ -2001,6 +2009,7 @@ build_firmware() { fi restore_platformio_build_flags "$had_platformio_build_flags" "$original_platformio_build_flags" + unset MESHCORE_PORTABLE_NANO_LIBC if [ "$had_platformio_build_unflags" -eq 1 ]; then export PLATFORMIO_BUILD_UNFLAGS="$original_platformio_build_unflags" else diff --git a/docs/ota_protocol.md b/docs/ota_protocol.md index 9825cc326..e698c74ee 100644 --- a/docs/ota_protocol.md +++ b/docs/ota_protocol.md @@ -116,6 +116,19 @@ while that sibling disables optional external environmental-sensor drivers for L GPS and other board-native telemetry remain enabled. RP2040 and STM32 targets are not offered because those platforms do not yet have a safe bootloader/apply path. +Two WiFi-heavy non-companion profiles need additional reductions to remain portable. MQTT observer builds +keep MQTT/TLS and their WiFi pull-updater, but omit WebConfig, SNMP, debug logging, SSD1306 display support, +GPS, and optional external sensor drivers. Their compact CLI keeps observer controls and uses UTC or fixed UTC/GMT +offsets instead of the full named-timezone table. Built-in TLS presets keep their pinned CA roots; the 66 KB +general CA bundle for custom TLS brokers is omitted, so portable observers use a built-in preset or a custom +non-TLS broker. Portable observers also use compact C-library formatting and generic ESP-IDF/mbedTLS error +text; error codes and MQTT status remain available. Classic T-Beam observers retain AXP192/AXP2101 radio +and GPS rail setup plus battery-voltage readings, but omit unrelated PMU policy. ESP-NOW bridge builds keep +the ESP-NOW bridge but likewise omit SSD1306 display support, GPS, optional external sensors, and the general +configuration parser. These reductions do +not apply to companion builds. Ordinary repeater builds remain sensor-enabled; only explicitly named +`*_lora_ota_no_external_sensors` siblings omit sensors for LoRa distribution. + > **Implementer note:** the bootloader (and any non-Arduino consumer) MUST locate the body extent by > scanning for `EndF`, never by trusting a stored size - see the bootloader contract in Section 12. diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 4d0d81130..6645ddb15 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -848,6 +848,7 @@ bool MyMesh::allowPacketForward(const mesh::Packet *packet) { if (!trace && packet->getRouteType() == ROUTE_TYPE_FLOOD && packet->getPathHashCount() >= _prefs.flood_max_unscoped) return false; if (packet->getPayloadType() == PAYLOAD_TYPE_ADVERT && packet->getPathHashCount() >= _prefs.flood_max_advert) return false; +#if !defined(PORTABLE_MQTT_OBSERVER) if (!_prefs.flood_channel_data_enabled && packet->getPayloadType() == PAYLOAD_TYPE_GRP_DATA && floodChannelDataHopApplies(packet)) { @@ -857,6 +858,7 @@ bool MyMesh::allowPacketForward(const mesh::Packet *packet) { } if (shouldBlockFloodPacketForward(packet)) return false; if (shouldBlockFloodChannelForward(packet)) return false; +#endif } if (packet->isRouteFlood() && packet->getPayloadType() != PAYLOAD_TYPE_TRACE && recv_pkt_region == NULL) { @@ -880,11 +882,13 @@ bool MyMesh::allowPacketForward(const mesh::Packet *packet) { // Moderation has rate-counter side effects, so evaluate it only after every // other forwarding gate has accepted the packet. Quota is then spent only // for a message this repeater will actually retransmit. +#if !defined(PORTABLE_MQTT_OBSERVER) if (packet->isRouteFlood() && shouldBlockFloodGroupTextForward(packet)) return false; // Clock evidence is collected only after every forwarding filter accepts the // packet. Blocked regions, moderated users, hop/type rules, and looped packets // therefore cannot influence the estimate. recordAcceptedFloodClockSample(packet); +#endif return true; } @@ -1242,6 +1246,11 @@ static uint16_t getRetryLogPreambleLength(const mesh::Packet* packet, uint16_t d void MyMesh::onDirectRetryEvent(const char* event, const mesh::Packet* packet, uint32_t delay_millis, uint8_t retry_attempt, const uint8_t* target_hash, uint8_t target_hash_len, int16_t payload_type) { +#if defined(PORTABLE_MQTT_OBSERVER) + (void)event; (void)packet; (void)delay_millis; (void)retry_attempt; + (void)target_hash; (void)target_hash_len; (void)payload_type; + return; +#endif char type_label[8]; char target_label[(MAX_HASH_SIZE * 2) + 1]; const char* route_label = packet != NULL ? (packet->isRouteDirect() ? "D" : "F") : "D"; @@ -1832,6 +1841,10 @@ bool MyMesh::formatFloodRetryHeard(char* dest, size_t dest_len, const mesh::Pack } void MyMesh::onFloodRetryEvent(const char* event, const mesh::Packet* packet, uint32_t delay_millis, uint8_t retry_attempt) { +#if defined(PORTABLE_MQTT_OBSERVER) + (void)event; (void)packet; (void)delay_millis; (void)retry_attempt; + return; +#endif if (event == NULL) { return; } @@ -2125,9 +2138,11 @@ void MyMesh::expireRecentRepeatersIfDue() { } mesh::DispatcherAction MyMesh::onRecvPacket(mesh::Packet* pkt) { +#if !defined(PORTABLE_MQTT_OBSERVER) if (pkt->getRouteType() == ROUTE_TYPE_FLOOD) { applyFloodChannelScope(pkt); } +#endif if (pkt->getRouteType() == ROUTE_TYPE_TRANSPORT_FLOOD) { recv_pkt_region = region_map.findMatch(pkt, REGION_DENY_FLOOD); } else if (pkt->getRouteType() == ROUTE_TYPE_FLOOD) { @@ -2582,11 +2597,13 @@ void MyMesh::begin(FILESYSTEM *fs) { acl.load(_fs, self_id); // TODO: key_store.begin(); region_map.load(_fs); +#if !defined(PORTABLE_MQTT_OBSERVER) loadFloodChannelBlocks(); loadFloodPacketFilters(); loadFloodChannelScopes(); loadFloodGroupModeration(); loadClockSyncPrefs(); +#endif // establish default-scope { @@ -6542,6 +6559,14 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char * mesh::cli::normalizeCommandVerb(command); +#if defined(PORTABLE_MQTT_OBSERVER) + // The portable observer exposes its MQTT/WiFi/update controls through + // CommonCLI. Omit the repeater's large remote-administration command tree; + // its mesh behavior remains fixed by the selected build profile. + _cli.handleCommand(sender_timestamp, command, reply); + return; +#endif + if (sender && !sender->isAdmin()) { bool allowed = (sender->isRegionMgr() && isRegionMgrAllowed(command)) || (sender->isFilterMgr() && isFilterMgrAllowed(command)); @@ -6885,8 +6910,10 @@ void MyMesh::loop() { // MQTT processing runs in a separate FreeRTOS task on Core 0, so we don't call bridge.loop() here mesh::Mesh::loop(); checkRxInactivityWatchdog(); +#if !defined(PORTABLE_MQTT_OBSERVER) checkBatteryAlert(); expireRecentRepeatersIfDue(); +#endif #if defined(WITH_BRIDGE) && !defined(WITH_MQTT_BRIDGE) // MQTT runs its own task; serial and ESP-NOW bridges remain cooperative. @@ -6908,7 +6935,9 @@ void MyMesh::loop() { updateAdvertTimer(); // schedule next local advert } +#if !defined(PORTABLE_MQTT_OBSERVER) processScheduledRadioSettings(); +#endif #if defined(WITH_MQTT_BRIDGE) && defined(OTA_MANIFEST_BASE) if (_ota_update_at && millisHasNowPassed(_ota_update_at)) { // deferred `ota update` @@ -6952,8 +6981,10 @@ void MyMesh::loop() { uint32_t now = millis(); uptime_millis += now - last_millis; last_millis = now; +#if !defined(PORTABLE_MQTT_OBSERVER) checkGpsClockSyncOverride(); checkClockSync(); +#endif #ifdef WITH_MQTT_BRIDGE _alerter.onLoop(now); diff --git a/platformio.ini b/platformio.ini index 72beb1a88..b8da29047 100644 --- a/platformio.ini +++ b/platformio.ini @@ -68,6 +68,7 @@ monitor_filters = esp32_exception_decoder ; it needs a custom bootloader; RP2040/STM32 have no A/B path yet.) extra_scripts = pre:scripts/generate_webconfig_html.py + pre:scripts/portable_esp32_link.py merge-bin.py post:tools/mota/pio_endf.py build_flags = ${arduino_base.build_flags} @@ -81,6 +82,7 @@ lib_deps = ${arduino_base.lib_deps} bblanchon/ArduinoJson build_src_filter = ${arduino_base.build_src_filter} + + + + + diff --git a/scripts/generate_cert_bundle.py b/scripts/generate_cert_bundle.py index 4b81cb789..f4f8f158a 100644 --- a/scripts/generate_cert_bundle.py +++ b/scripts/generate_cert_bundle.py @@ -177,9 +177,20 @@ class InputError(RuntimeError): super(InputError, self).__init__(e) -def main(): - - bundle = CertificateBundle() +def main(): + + # Portable observer presets pin their required roots directly in + # MQTTPresets.h. Keep the embedded bundle structurally valid but empty so + # custom TLS trust stores do not consume 66 KB of the legacy app slot. + if "observer_mqtt" in env.subst("$PIOENV").lower(): + os.makedirs(binary_dir, exist_ok=True) + output_file = os.path.join(binary_dir, ca_bundle_bin_file) + with open(output_file, 'wb') as f: + f.write(struct.pack('>H', 0)) + status('Created empty portable-observer certificate bundle') + return + + bundle = CertificateBundle() try: cert_source = env.GetProjectOption("board_ssl_cert_source") diff --git a/scripts/portable_esp32_link.py b/scripts/portable_esp32_link.py new file mode 100644 index 000000000..e55af1d91 --- /dev/null +++ b/scripts/portable_esp32_link.py @@ -0,0 +1,27 @@ +Import("env") + +import os + + +def has_define(name): + for define in env.get("CPPDEFINES", []): + if define == name: + return True + if isinstance(define, str) and define.startswith(name + "="): + return True + if isinstance(define, (tuple, list)) and define and define[0] == name: + return True + return False + + +build_flags = str(env.get("BUILD_FLAGS", "")) + " " + os.environ.get("PLATFORMIO_BUILD_FLAGS", "") +pio_env = env.subst("$PIOENV").lower() + +if (has_define("PORTABLE_ESP32_NANO_LIBC") or + "PORTABLE_ESP32_NANO_LIBC" in build_flags or + os.environ.get("MESHCORE_PORTABLE_NANO_LIBC") == "1" or + "observer_mqtt" in pio_env or "bridge_espnow" in pio_env): + # newlib-nano provides the same C ABI with a substantially smaller printf + # implementation. These profiles omit display, GPS, external sensors, and + # the general CLI paths that require floating-point printf. + env.Append(LINKFLAGS=["--specs=nano.specs"]) diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 19c0f847b..82c549d1d 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -2265,6 +2265,13 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep #endif // Observer/MQTT/WiFi/timezone/alert/SNMP commands live in CommonCLI_Observer.cpp. if (handleObserverSetCmd(sender_timestamp, config, reply)) return; +#if defined(PORTABLE_ESP32_MINIMAL_CLI) + // Size-constrained ESP32 images keep role-specific controls but omit the + // large general configuration parser. Radio and mesh defaults remain fixed + // by the selected build profile. + sprintf(reply, "unknown portable config: %s", config); + return; +#else if (isAdvancedRetryConfig(config) && !_callbacks->supportsAdvancedRetryConfig()) { strcpy(reply, "Error, unsupported on this role"); return; @@ -3090,6 +3097,7 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep } else { sprintf(reply, "unknown config: %s", config); } +#endif } void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* reply) { @@ -3104,6 +3112,10 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep #endif // Observer/MQTT/WiFi/timezone/alert/SNMP commands live in CommonCLI_Observer.cpp. if (handleObserverGetCmd(sender_timestamp, config, reply)) return; +#if defined(PORTABLE_ESP32_MINIMAL_CLI) + sprintf(reply, "unknown portable config: %s", config); + return; +#else if (isAdvancedRetryConfig(config) && !_callbacks->supportsAdvancedRetryConfig()) { strcpy(reply, "Error, unsupported on this role"); return; @@ -3393,6 +3405,7 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep } else { sprintf(reply, "??: %s", config); } +#endif } void CommonCLI::handleDelCmd(char* command, char* reply) { diff --git a/src/helpers/CommonCLI_Observer.cpp b/src/helpers/CommonCLI_Observer.cpp index 47010cd17..e78922f47 100644 --- a/src/helpers/CommonCLI_Observer.cpp +++ b/src/helpers/CommonCLI_Observer.cpp @@ -717,6 +717,9 @@ bool CommonCLI::handleObserverGetCmd(uint32_t sender_timestamp, const char* conf uint32_t minutes = (_mqtt_prefs.mqtt_status_interval + 29999) / 60000; sprintf(reply, "> %u minutes (%lu ms)", minutes, (unsigned long)_mqtt_prefs.mqtt_status_interval); } else if (memcmp(config, "mqtt.ntp.diag", 13) == 0 && (config[13] == '\0' || config[13] == ' ')) { +#if defined(PORTABLE_MQTT_OBSERVER) + strcpy(reply, "Error: NTP diagnostics omitted from portable build"); +#else #ifdef ESP_PLATFORM // Connectivity probe across all configured NTP servers; never updates the clock. // Serial console (sender_timestamp == 0) gets a detailed table; LoRa gets a compact list. @@ -729,6 +732,7 @@ bool CommonCLI::handleObserverGetCmd(uint32_t sender_timestamp, const char* conf } #else strcpy(reply, "Error: not supported on this platform"); +#endif #endif } else if (memcmp(config, "mqtt.ntp", 8) == 0 && (config[8] == '\0' || config[8] == ' ')) { sprintf(reply, "> %s", MQTTBridge::effectiveNtpPrimary(&_mqtt_prefs)); diff --git a/src/helpers/JWTHelper.cpp b/src/helpers/JWTHelper.cpp index 37d601ef3..b28732986 100644 --- a/src/helpers/JWTHelper.cpp +++ b/src/helpers/JWTHelper.cpp @@ -79,12 +79,15 @@ bool JWTHelper::createAuthToken( ed25519_sign(signature, (const unsigned char*)signingInput, signingInputLen, public_key, private_key); - // Verify the signature locally + // The ordinary profile verifies locally as a diagnostic. Portable observers + // omit this redundant second curve operation; the broker verifies every JWT. +#if !defined(PORTABLE_MQTT_OBSERVER) int verify_result = ed25519_verify(signature, (const unsigned char*)signingInput, signingInputLen, public_key); if (verify_result != 1) { if (Serial.availableForWrite() > 0) Serial.println("JWTHelper: Signature verification failed!"); return false; } +#endif // Convert signature to hex char signatureHex[129]; diff --git a/src/helpers/MQTTMessageBuilder.cpp b/src/helpers/MQTTMessageBuilder.cpp index 1025cce2a..e834630bc 100644 --- a/src/helpers/MQTTMessageBuilder.cpp +++ b/src/helpers/MQTTMessageBuilder.cpp @@ -22,8 +22,11 @@ void MQTTMessageBuilder::formatIsoTimestampForMqtt(time_t now, long usec, Timezo else if (usec > 999999) usec = 999999; struct tm* tm_info = gmtime(&now); if (tm_info) { - size_t n = strftime(buffer, buffer_size, "%Y-%m-%dT%H:%M:%S", tm_info); - if (n > 0 && snprintf(buffer + n, buffer_size - n, ".%06ld+00:00", usec) > 0) { + int n = snprintf(buffer, buffer_size, "%04d-%02d-%02dT%02d:%02d:%02d", + tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday, + tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec); + if (n > 0 && static_cast(n) < buffer_size && + snprintf(buffer + n, buffer_size - n, ".%06ld+00:00", usec) > 0) { return; } } @@ -257,8 +260,10 @@ int MQTTMessageBuilder::buildPacketJSON( char time_str[16]; char date_str[16]; if (utc_timeinfo) { - strftime(time_str, sizeof(time_str), "%H:%M:%S", utc_timeinfo); - strftime(date_str, sizeof(date_str), "%d/%m/%Y", utc_timeinfo); + snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d", + utc_timeinfo->tm_hour, utc_timeinfo->tm_min, utc_timeinfo->tm_sec); + snprintf(date_str, sizeof(date_str), "%02d/%02d/%04d", + utc_timeinfo->tm_mday, utc_timeinfo->tm_mon + 1, utc_timeinfo->tm_year + 1900); } else { strcpy(time_str, "12:00:00"); strcpy(date_str, "01/01/2024"); @@ -334,8 +339,10 @@ int MQTTMessageBuilder::buildPacketJSONFromRaw( char time_str[16]; char date_str[16]; if (utc_timeinfo) { - strftime(time_str, sizeof(time_str), "%H:%M:%S", utc_timeinfo); - strftime(date_str, sizeof(date_str), "%d/%m/%Y", utc_timeinfo); + snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d", + utc_timeinfo->tm_hour, utc_timeinfo->tm_min, utc_timeinfo->tm_sec); + snprintf(date_str, sizeof(date_str), "%02d/%02d/%04d", + utc_timeinfo->tm_mday, utc_timeinfo->tm_mon + 1, utc_timeinfo->tm_year + 1900); } else { strcpy(time_str, "12:00:00"); strcpy(date_str, "01/01/2024"); diff --git a/src/helpers/MQTTPresets.h b/src/helpers/MQTTPresets.h index ed25337a3..8b5fdbff9 100644 --- a/src/helpers/MQTTPresets.h +++ b/src/helpers/MQTTPresets.h @@ -55,8 +55,16 @@ static inline bool mqttPresetNeedsSlotCredentials(const MQTTPresetDef* preset) { (!preset->userpass_username || !preset->userpass_password); } +// Number of built-in presets +static const int MQTT_PRESET_COUNT = 25; + +// Keep the certificate and preset tables in one translation unit. Defining +// these as header-local constants created a complete flash copy in every MQTT +// source file that referenced a preset. +#ifdef MQTT_PRESETS_IMPLEMENTATION + // Google Trust Services - GTS Root R4 (used by LetsMesh Analyzer) -static const char GTS_ROOT_R4[] PROGMEM = +extern const char GTS_ROOT_R4[] PROGMEM = "-----BEGIN CERTIFICATE-----\n" "MIIDejCCAmKgAwIBAgIQf+UwvzMTQ77dghYQST2KGzANBgkqhkiG9w0BAQsFADBX\n" "MQswCQYDVQQGEwJCRTEZMBcGA1UEChMQR2xvYmFsU2lnbiBudi1zYTEQMA4GA1UE\n" @@ -80,7 +88,7 @@ static const char GTS_ROOT_R4[] PROGMEM = "-----END CERTIFICATE-----\n"; // ISRG Root X1 (used by MeshMapper - Let's Encrypt root CA) -static const char ISRG_ROOT_X1[] PROGMEM = +extern const char ISRG_ROOT_X1[] PROGMEM = "-----BEGIN CERTIFICATE-----\n" "MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" @@ -113,11 +121,8 @@ static const char ISRG_ROOT_X1[] PROGMEM = "emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n" "-----END CERTIFICATE-----\n"; -// Number of built-in presets -static const int MQTT_PRESET_COUNT = 25; - // Built-in preset definitions (stored in flash) -static const MQTTPresetDef MQTT_PRESETS[MQTT_PRESET_COUNT] = { +extern const MQTTPresetDef MQTT_PRESETS[MQTT_PRESET_COUNT] = { // name url server rootCA auth topic keepalive tls enabled interval user pass { "analyzer-us", "wss://mqtt-us-v1.letsmesh.net:443/mqtt", "mqtt-us-v1.letsmesh.net", GTS_ROOT_R4, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr }, { "analyzer-eu", "wss://mqtt-eu-v1.letsmesh.net:443/mqtt", "mqtt-eu-v1.letsmesh.net", GTS_ROOT_R4, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr }, @@ -146,6 +151,14 @@ static const MQTTPresetDef MQTT_PRESETS[MQTT_PRESET_COUNT] = { { "flmesh", "wss://mcmqtt.jntconnections.com:443", "mcmqtt.jntconnections.com", GTS_ROOT_R4, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr }, }; +#else + +extern const char GTS_ROOT_R4[] PROGMEM; +extern const char ISRG_ROOT_X1[] PROGMEM; +extern const MQTTPresetDef MQTT_PRESETS[MQTT_PRESET_COUNT]; + +#endif + // Find a preset by name, returns nullptr if not found static const MQTTPresetDef* findMQTTPreset(const char* name) { if (!name || name[0] == '\0') return nullptr; diff --git a/src/helpers/bridges/ESPNowBridge.cpp b/src/helpers/bridges/ESPNowBridge.cpp index b292666f3..f39f073ce 100644 --- a/src/helpers/bridges/ESPNowBridge.cpp +++ b/src/helpers/bridges/ESPNowBridge.cpp @@ -1,6 +1,5 @@ #include "ESPNowBridge.h" -#include #include #ifdef WITH_ESPNOW_BRIDGE @@ -40,20 +39,32 @@ void ESPNowBridge::begin() { if (_initialized) return; - // Initialize WiFi in station mode - WiFi.mode(WIFI_STA); + // ESP-NOW only needs the ESP-IDF station interface. Avoid Arduino's WiFi + // facade here: it pulls DHCP, DNS, scanning, AP, and event plumbing that an + // ESP-NOW-only bridge never uses. + wifi_init_config_t wifi_config = WIFI_INIT_CONFIG_DEFAULT(); + if (esp_wifi_init(&wifi_config) != ESP_OK || + esp_wifi_set_storage(WIFI_STORAGE_RAM) != ESP_OK || + esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK || + esp_wifi_start() != ESP_OK) { + esp_wifi_stop(); + esp_wifi_deinit(); + return; + } // Set Wi-Fi channel if (esp_wifi_set_channel(_prefs->bridge_channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) { BRIDGE_DEBUG_PRINTLN("Error setting WIFI channel to %d\n", _prefs->bridge_channel); - WiFi.mode(WIFI_OFF); + esp_wifi_stop(); + esp_wifi_deinit(); return; } // Initialize ESP-NOW if (esp_now_init() != ESP_OK) { BRIDGE_DEBUG_PRINTLN("Error initializing ESP-NOW\n"); - WiFi.mode(WIFI_OFF); + esp_wifi_stop(); + esp_wifi_deinit(); return; } @@ -63,7 +74,8 @@ void ESPNowBridge::begin() { esp_now_register_recv_cb(nullptr); esp_now_register_send_cb(nullptr); esp_now_deinit(); - WiFi.mode(WIFI_OFF); + esp_wifi_stop(); + esp_wifi_deinit(); return; } @@ -79,7 +91,8 @@ void ESPNowBridge::begin() { esp_now_register_recv_cb(nullptr); esp_now_register_send_cb(nullptr); esp_now_deinit(); - WiFi.mode(WIFI_OFF); + esp_wifi_stop(); + esp_wifi_deinit(); return; } @@ -105,8 +118,9 @@ void ESPNowBridge::end() { BRIDGE_DEBUG_PRINTLN("Error deinitializing ESP-NOW\n"); } - // Turn off WiFi - WiFi.mode(WIFI_OFF); + // Turn off the ESP-IDF WiFi interface. + esp_wifi_stop(); + esp_wifi_deinit(); // Update bridge state _initialized = false; diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 92da9ec99..1cb5c5b7e 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -1,3 +1,4 @@ +#define MQTT_PRESETS_IMPLEMENTATION #include "MQTTBridge.h" #include "../MQTTMessageBuilder.h" #include "../TxtDataHelpers.h" @@ -1024,6 +1025,7 @@ void MQTTBridge::mqttTaskLoop() { _ntp_force_done = true; // set last so the waiter sees a consistent result } +#if !defined(PORTABLE_MQTT_OBSERVER) // Process a CLI-requested NTP connectivity diagnostic (queued from Core 1). // Probe-only - never touches the system clock. if (_ntp_diag_requested) { @@ -1042,6 +1044,7 @@ void MQTTBridge::mqttTaskLoop() { // queue work that shares and overwrites the result fields above. _ntp_estimate_requested = false; } +#endif // Deferred slot setup: wait until NTP is synced so JWT tokens get valid timestamps. // This avoids wasted TLS handshakes that get rejected due to bad token times. @@ -1427,6 +1430,11 @@ void MQTTBridge::setupSlot(int index) { // crts array while a prior slot's TLS handshake may still be reading it. bool needs_tls = (strncmp(slot.broker_uri, "mqtts://", 8) == 0 || strncmp(slot.broker_uri, "wss://", 6) == 0); +#if defined(PORTABLE_MQTT_OBSERVER) + // Portable images keep pinned roots for built-in presets. The general CA + // bundle is intentionally left unreferenced so the linker can discard it. + (void)needs_tls; +#else if (needs_tls) { if (!s_ca_bundle_loaded) { size_t bundle_len = 0; @@ -1455,6 +1463,7 @@ void MQTTBridge::setupSlot(int index) { } else { MQTT_DEBUG_PRINTLN("MQTT%d custom broker uses non-TLS transport", index + 1); } +#endif // Custom slot authentication: JWT if audience is set, else username/password if (slot.audience[0] != '\0') { @@ -3327,6 +3336,24 @@ bool MQTTBridge::ntpDiag(char* reply, size_t reply_size, bool verbose) { // heap allocation - the caller then passes these into Timezone::setRules() on // an existing Timezone object. bool MQTTBridge::timezoneRulesFromString(const char* tz_string, TimeChangeRule& dst_out, TimeChangeRule& std_out) { +#if defined(PORTABLE_MQTT_OBSERVER) + // Portable observers publish UTC timestamps, so retain UTC and fixed UTC/GMT + // offsets without carrying the full named-zone/DST lookup table. + int offset = 0; + if (strcmp(tz_string, "UTC") == 0 || strcmp(tz_string, "GMT") == 0) { + offset = 0; + } else if (strncmp(tz_string, "UTC", 3) == 0 || strncmp(tz_string, "GMT", 3) == 0) { + offset = atoi(tz_string + 3); + } else if (tz_string[0] == '+' || tz_string[0] == '-') { + offset = atoi(tz_string); + } else { + return false; + } + TimeChangeRule fixed = {"UTC", Last, Sun, Mar, 0, offset * 60}; + dst_out = fixed; + std_out = fixed; + return true; +#else // GCC refuses to implicitly build a TimeChangeRule temporary from a bare // braced-init-list on the right-hand side of operator= (the aggregate has a // char[6] member). Name the type explicitly so a proper temporary is formed. @@ -3448,6 +3475,7 @@ bool MQTTBridge::timezoneRulesFromString(const char* tz_string, TimeChangeRule& MQTT_DEBUG_PRINTLN("Unknown timezone: %s", tz_string); return false; } +#endif } // --------------------------------------------------------------------------- diff --git a/src/helpers/esp32/PortableErrorStrings.cpp b/src/helpers/esp32/PortableErrorStrings.cpp new file mode 100644 index 000000000..79c9f3d90 --- /dev/null +++ b/src/helpers/esp32/PortableErrorStrings.cpp @@ -0,0 +1,25 @@ +#if defined(ESP_PLATFORM) && defined(PORTABLE_MQTT_OBSERVER) + +#include +#include + +extern "C" void mbedtls_strerror(int, char* buffer, size_t buffer_size) { + if (buffer_size == 0) return; + const char message[] = "mbedTLS error"; + std::strncpy(buffer, message, buffer_size - 1); + buffer[buffer_size - 1] = '\0'; +} + +extern "C" const char* esp_err_to_name(esp_err_t) { + return "ESP error"; +} + +extern "C" const char* esp_err_to_name_r(esp_err_t, char* buffer, size_t buffer_size) { + if (buffer_size == 0) return buffer; + const char message[] = "ESP error"; + std::strncpy(buffer, message, buffer_size - 1); + buffer[buffer_size - 1] = '\0'; + return buffer; +} + +#endif diff --git a/src/helpers/esp32/TBeamBoard.cpp b/src/helpers/esp32/TBeamBoard.cpp index 5f708d71d..6f835ce89 100644 --- a/src/helpers/esp32/TBeamBoard.cpp +++ b/src/helpers/esp32/TBeamBoard.cpp @@ -123,6 +123,43 @@ void TBeamBoard::printPMU() } #endif +#if defined(PORTABLE_MQTT_OBSERVER) && !defined(TBEAM_SUPREME_SX1262) +static bool pmuRead(uint8_t reg, uint8_t& value) { + PMU_WIRE_PORT.beginTransmission(I2C_PMU_ADD); + PMU_WIRE_PORT.write(reg); + if (PMU_WIRE_PORT.endTransmission(false) != 0 || + PMU_WIRE_PORT.requestFrom(I2C_PMU_ADD, 1) != 1) return false; + value = PMU_WIRE_PORT.read(); + return true; +} + +static bool pmuWrite(uint8_t reg, uint8_t value) { + PMU_WIRE_PORT.beginTransmission(I2C_PMU_ADD); + PMU_WIRE_PORT.write(reg); + PMU_WIRE_PORT.write(value); + return PMU_WIRE_PORT.endTransmission() == 0; +} + +static bool pmuUpdate(uint8_t reg, uint8_t clear_mask, uint8_t set_mask) { + uint8_t value; + return pmuRead(reg, value) && pmuWrite(reg, (value & ~clear_mask) | set_mask); +} + +bool TBeamBoard::power_init() { + PMU_WIRE_PORT.begin(PIN_BOARD_SDA, PIN_BOARD_SCL); + if (!pmuRead(0x03, pmu_model)) return false; + + if (pmu_model == 0x4A) { + // AXP2101: ALDO2 and ALDO3 at 3.3 V, plus battery voltage ADC. + return pmuUpdate(0x93, 0x1F, 28) && pmuUpdate(0x94, 0x1F, 28) && + pmuUpdate(0x90, 0, 0x06) && pmuUpdate(0x30, 0, 0x01); + } + + // AXP192: LDO2 and LDO3 at 3.3 V, plus battery voltage ADC. + return pmuWrite(0x28, 0xFF) && pmuUpdate(0x12, 0, 0x0C) && + pmuUpdate(0x82, 0, 0x80); +} +#else bool TBeamBoard::power_init() { if (!PMU) { @@ -284,6 +321,7 @@ bool TBeamBoard::power_init() PMU->setPowerKeyPressOffTime(XPOWERS_POWEROFF_4S); return true; } +#endif #pragma region "Debug code" // void TBeamBoard::radiotype_detect(){ diff --git a/src/helpers/esp32/TBeamBoard.h b/src/helpers/esp32/TBeamBoard.h index 543226d66..d338eee2a 100644 --- a/src/helpers/esp32/TBeamBoard.h +++ b/src/helpers/esp32/TBeamBoard.h @@ -84,11 +84,18 @@ // Include headers AFTER pin definitions so ESP32Board::sleep() can use P_LORA_DIO_1 #include #include -#include "XPowersLib.h" #include "helpers/ESP32Board.h" +#if !defined(PORTABLE_MQTT_OBSERVER) || defined(TBEAM_SUPREME_SX1262) +#include "XPowersLib.h" +#endif + class TBeamBoard : public ESP32Board { +#if defined(PORTABLE_MQTT_OBSERVER) && !defined(TBEAM_SUPREME_SX1262) +uint8_t pmu_model = 0; +#else XPowersLibInterface *PMU = NULL; +#endif //PhysicalLayer * pl; //RadioType * radio = NULL; // int radioVersions = 2; @@ -131,7 +138,20 @@ public: #endif uint16_t getBattMilliVolts(){ +#if defined(PORTABLE_MQTT_OBSERVER) && !defined(TBEAM_SUPREME_SX1262) + uint8_t high_reg = pmu_model == 0x4A ? 0x34 : 0x78; + uint8_t low_reg = high_reg + 1; + PMU_WIRE_PORT.beginTransmission(I2C_PMU_ADD); + PMU_WIRE_PORT.write(high_reg); + if (PMU_WIRE_PORT.endTransmission(false) != 0 || + PMU_WIRE_PORT.requestFrom(I2C_PMU_ADD, 2) != 2) return 0; + uint16_t high = PMU_WIRE_PORT.read(); + uint16_t low = PMU_WIRE_PORT.read(); + if (pmu_model == 0x4A) return ((high & 0x1F) << 8) | low; + return (uint16_t)((((high << 4) | (low & 0x0F)) * 11) / 10); +#else return PMU->getBattVoltage(); +#endif } const char* getManufacturerName() const{ diff --git a/variants/lilygo_tlora_v2_1/platformio.ini b/variants/lilygo_tlora_v2_1/platformio.ini index c2860d230..d0eedf224 100644 --- a/variants/lilygo_tlora_v2_1/platformio.ini +++ b/variants/lilygo_tlora_v2_1/platformio.ini @@ -2,7 +2,6 @@ [LilyGo_TLora_V2_1_1_6_core] extends = esp32_base board = ttgo-lora32-v1 ; LILYGO T-LoRa V2.1-1.6 ESP32 with SX1276 -build_unflags = -Os build_type = release ; Set build type to release board_build.partitions = min_spiffs.csv ; get around 4mb flash limit build_flags = diff --git a/variants/station_g2/platformio.ini b/variants/station_g2/platformio.ini index ac119693c..0ccc0fc5a 100644 --- a/variants/station_g2/platformio.ini +++ b/variants/station_g2/platformio.ini @@ -188,10 +188,7 @@ board_ssl_cert_source = adafruit-full board_build.embed_files = src/certs/x509_crt_bundle.bin build_flags = ${Station_G2.build_flags} - ; MQTT observers retain the full WebConfig portal and large 16 MB layout, but - ; update over WiFi rather than distributing firmware over LoRa. - -ULIGHTWEIGHT_WIFI_OTA - -UWEBCONFIG_DISABLED + ; Portable MQTT observers use CLI setup and the lightweight WiFi updater. -UENABLE_OTA -D DISABLE_LORA_OTA=1 -UMESH_DEBUG @@ -331,10 +328,7 @@ board_ssl_cert_source = adafruit-full board_build.embed_files = src/certs/x509_crt_bundle.bin build_flags = ${Station_G2.build_flags} - ; MQTT observers retain the full WebConfig portal and large 16 MB layout, but - ; update over WiFi rather than distributing firmware over LoRa. - -ULIGHTWEIGHT_WIFI_OTA - -UWEBCONFIG_DISABLED + ; Portable MQTT observers use CLI setup and the lightweight WiFi updater. -UENABLE_OTA -D DISABLE_LORA_OTA=1 -UMESH_DEBUG