Integrates Mike Carper's transmission-reliability work (direct retries with SNR-adaptive backoff and CR, flood retry controls, alt-path replies, flood channel gates) plus its upstream/dev base (as of 2026-07-06) on top of the observer stack. The flex branch is untouched; this branch is the experimental integration line. Conflict-resolution decisions, for future re-syncs (git rerere is enabled and has recorded these): - NodePrefs adopts keymind/upstream member order + retry/flood tail. Member order is in-memory only: /com_prefs stays field-by-field with the same canonical file order both sides already share through offset 294; keymind appends the retry tail at 295+ (new canonical size 676). Flex fleet files load unchanged; retry fields default via direct_retry_prefs_magic. - loadPrefsInt keeps the fork's legacy MQTT-gap recovery but moves its detection boundary from 'extra > 5' to 'extra > LEGACY_MQTT_GAP_3SLOT (864)' so keymind-size tails take the normal read path. COM_PREFS_TAIL_BYTES retired. - Dispatcher/RadioLibWrappers: fork watchdog additions + keymind TX overrides and CAD busy counter are unioned; RxReservePacketManager::queueOutbound follows upstream's new bool return (false when shedding). - CommonCLI: fork observer dispatch + versioned /mqtt_prefs machinery kept; keymind CLI (retry/flood/radioat) taken; duplicate CAD/FEM handlers and sanitise lines deduped (kept keymind placements to minimize future diff). - MyMesh (repeater/room): fork alerter/OTA/observer wiring kept; keymind scheduled-radio system replaces the old pending_* temp-radio members; applySavedRadioParams() replaces raw setParams at startup. - ESP32Board: fork manifest-OTA + keymind powerOff/deepSleep and stopOTAUpdate unioned; startOTAUpdate keeps fork's STA-aware IP with keymind's idempotency guard. - build.sh taken wholesale from keymind (fleet builds use GH Actions). Verified: Heltec_v3_repeater_observer_mqtt, Heltec_v3_repeater, heltec_v4_repeater_observer_mqtt, Heltec_v3_room_server_observer_mqtt build; host migration harness (13 MQTT + non-MQTT scenarios) passes with updated expectations (676-byte /com_prefs; odd-size files normalize on save, not load); native unit tests 13/13.
277 lines
7.1 KiB
C++
277 lines
7.1 KiB
C++
#pragma once
|
|
|
|
#include <MeshCore.h>
|
|
#include <Arduino.h>
|
|
|
|
#ifndef USER_BTN_PRESSED
|
|
#define USER_BTN_PRESSED LOW
|
|
#endif
|
|
|
|
#if defined(ESP_PLATFORM)
|
|
|
|
#include <rom/rtc.h>
|
|
#include <sys/time.h>
|
|
#include <Wire.h>
|
|
#include "soc/rtc.h"
|
|
#include "esp_system.h"
|
|
#include <driver/rtc_io.h>
|
|
|
|
class AsyncWebServer;
|
|
|
|
class ESP32Board : public mesh::MainBoard {
|
|
protected:
|
|
uint8_t startup_reason;
|
|
bool inhibit_sleep = false;
|
|
AsyncWebServer* ota_server = nullptr;
|
|
static inline portMUX_TYPE sleepMux = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
public:
|
|
void begin() {
|
|
// for future use, sub-classes SHOULD call this from their begin()
|
|
startup_reason = BD_STARTUP_NORMAL;
|
|
|
|
#ifdef ESP32_CPU_FREQ
|
|
setCpuFrequencyMhz(ESP32_CPU_FREQ);
|
|
#endif
|
|
|
|
#ifdef PIN_VBAT_READ
|
|
// battery read support
|
|
pinMode(PIN_VBAT_READ, INPUT);
|
|
adcAttachPin(PIN_VBAT_READ);
|
|
#endif
|
|
|
|
#ifdef P_LORA_TX_LED
|
|
pinMode(P_LORA_TX_LED, OUTPUT);
|
|
digitalWrite(P_LORA_TX_LED, LOW);
|
|
#endif
|
|
|
|
#if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
|
|
#if PIN_BOARD_SDA >= 0 && PIN_BOARD_SCL >= 0
|
|
Wire.begin(PIN_BOARD_SDA, PIN_BOARD_SCL);
|
|
#endif
|
|
#else
|
|
Wire.begin();
|
|
#endif
|
|
}
|
|
|
|
// Temperature from ESP32 MCU
|
|
float getMCUTemperature() override {
|
|
uint32_t raw = 0;
|
|
|
|
// To get and average the temperature so it is more accurate, especially in low temperature
|
|
for (int i = 0; i < 4; i++) {
|
|
raw += temperatureRead();
|
|
}
|
|
|
|
return raw / 4;
|
|
}
|
|
|
|
virtual void powerOff() override;
|
|
void enterDeepSleep(uint32_t secs);
|
|
|
|
uint32_t getIRQGpio() override {
|
|
#ifdef P_LORA_DIO_1
|
|
return P_LORA_DIO_1; // default for SX1262
|
|
#else
|
|
return -1;
|
|
#endif
|
|
}
|
|
|
|
void sleep(uint32_t secs) override {
|
|
// Skip if not allow to sleep
|
|
if (inhibit_sleep) {
|
|
delay(1); // Give MCU to OTA to run
|
|
return;
|
|
}
|
|
|
|
// Configure timer wakeup
|
|
if (secs > 0) {
|
|
esp_sleep_enable_timer_wakeup(secs * 1000000ULL); // Wake up periodically to do scheduled jobs
|
|
}
|
|
|
|
uint32_t irqGpio = getIRQGpio();
|
|
if (irqGpio == static_cast<uint32_t>(-1)) {
|
|
if (secs > 0) {
|
|
esp_light_sleep_start();
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Set GPIO wakeup
|
|
gpio_num_t wakeupPin = (gpio_num_t)irqGpio;
|
|
|
|
// Disable CPU interrupt servicing
|
|
portENTER_CRITICAL(&sleepMux);
|
|
|
|
// Skip sleep if there is a LoRa packet
|
|
if (gpio_get_level(wakeupPin) == HIGH) {
|
|
portEXIT_CRITICAL(&sleepMux);
|
|
delay(1);
|
|
return;
|
|
}
|
|
|
|
// Configure GPIO wakeup
|
|
esp_sleep_enable_gpio_wakeup();
|
|
gpio_wakeup_enable((gpio_num_t)wakeupPin, GPIO_INTR_HIGH_LEVEL); // Wake up when receiving a LoRa packet
|
|
|
|
// MCU enters light sleep
|
|
esp_light_sleep_start();
|
|
|
|
// Avoid ISR flood during wakeup due to HIGH LEVEL interrupt
|
|
gpio_wakeup_disable(wakeupPin);
|
|
gpio_set_intr_type(wakeupPin, GPIO_INTR_POSEDGE);
|
|
|
|
// Enable CPU interrupt servicing
|
|
portEXIT_CRITICAL(&sleepMux);
|
|
}
|
|
|
|
uint8_t getStartupReason() const override { return startup_reason; }
|
|
|
|
#if defined(P_LORA_TX_LED)
|
|
void onBeforeTransmit() override {
|
|
digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED on
|
|
}
|
|
void onAfterTransmit() override {
|
|
digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED off
|
|
}
|
|
#elif defined(P_LORA_TX_NEOPIXEL_LED)
|
|
#define NEOPIXEL_BRIGHTNESS 64 // white brightness (max 255)
|
|
|
|
void onBeforeTransmit() override {
|
|
neopixelWrite(P_LORA_TX_NEOPIXEL_LED, NEOPIXEL_BRIGHTNESS, NEOPIXEL_BRIGHTNESS, NEOPIXEL_BRIGHTNESS); // turn TX neopixel on (White)
|
|
}
|
|
void onAfterTransmit() override {
|
|
neopixelWrite(P_LORA_TX_NEOPIXEL_LED, 0, 0, 0); // turn TX neopixel off
|
|
}
|
|
#endif
|
|
|
|
uint16_t getBattMilliVolts() override {
|
|
#ifdef PIN_VBAT_READ
|
|
analogReadResolution(12);
|
|
|
|
uint32_t raw = 0;
|
|
for (int i = 0; i < 4; i++) {
|
|
raw += analogReadMilliVolts(PIN_VBAT_READ);
|
|
}
|
|
raw = raw / 4;
|
|
|
|
return (2 * raw);
|
|
#else
|
|
return 0; // not supported
|
|
#endif
|
|
}
|
|
|
|
const char* getManufacturerName() const override {
|
|
return "Generic ESP32";
|
|
}
|
|
|
|
void reboot() override {
|
|
esp_restart();
|
|
}
|
|
|
|
bool startOTAUpdate(const char* id, char reply[]) override;
|
|
bool stopOTAUpdate(char reply[]) override;
|
|
bool otaFromManifest(const char* current_ver, bool dry_run, char reply[]) override;
|
|
// Heavy body (TLS + JSON / HTTPUpdate). Runs in a dedicated large-stack task
|
|
// spawned by otaFromManifest() — public only so that task entry point can call
|
|
// it; not meant to be invoked directly.
|
|
bool otaFromManifestImpl(const char* current_ver, bool dry_run, char reply[]);
|
|
|
|
bool isUsbDataConnected() override {
|
|
#if defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
|
|
return (bool)Serial;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
void setInhibitSleep(bool inhibit) {
|
|
inhibit_sleep = inhibit;
|
|
}
|
|
|
|
uint32_t getResetReason() const override {
|
|
return esp_reset_reason();
|
|
}
|
|
|
|
// https://docs.espressif.com/projects/esp-idf/en/v4.4.7/esp32/api-reference/system/system.html
|
|
const char* getResetReasonString(uint32_t reason) {
|
|
switch (reason) {
|
|
case ESP_RST_UNKNOWN:
|
|
return "Unknown or first boot";
|
|
case ESP_RST_POWERON:
|
|
return "Power-on reset";
|
|
case ESP_RST_EXT:
|
|
return "External reset";
|
|
case ESP_RST_SW:
|
|
return "Software reset";
|
|
case ESP_RST_PANIC:
|
|
return "Panic / exception reset";
|
|
case ESP_RST_INT_WDT:
|
|
return "Interrupt watchdog reset";
|
|
case ESP_RST_TASK_WDT:
|
|
return "Task watchdog reset";
|
|
case ESP_RST_WDT:
|
|
return "Other watchdog reset";
|
|
case ESP_RST_DEEPSLEEP:
|
|
return "Wake from deep sleep";
|
|
case ESP_RST_BROWNOUT:
|
|
return "Brownout (low voltage)";
|
|
case ESP_RST_SDIO:
|
|
return "SDIO reset";
|
|
default:
|
|
static char buf[40];
|
|
snprintf(buf, sizeof(buf), "Unknown reset reason (%d)", reason);
|
|
return buf;
|
|
}
|
|
}
|
|
};
|
|
|
|
static RTC_NOINIT_ATTR uint32_t _rtc_backup_time;
|
|
static RTC_NOINIT_ATTR uint32_t _rtc_backup_magic;
|
|
#define RTC_BACKUP_MAGIC 0xAA55CC33
|
|
#define RTC_TIME_MIN 1772323200 // 1 Mar 2026
|
|
|
|
class ESP32RTCClock : public mesh::RTCClock {
|
|
public:
|
|
ESP32RTCClock() { }
|
|
void begin() {
|
|
esp_reset_reason_t reason = esp_reset_reason();
|
|
if (reason == ESP_RST_DEEPSLEEP) {
|
|
return; // ESP-IDF preserves system time across deep sleep
|
|
}
|
|
// All other resets (power-on, crash, WDT, brownout) lose system time.
|
|
// Restore from RTC backup if valid, otherwise use hardcoded seed.
|
|
struct timeval tv;
|
|
if (_rtc_backup_magic == RTC_BACKUP_MAGIC && _rtc_backup_time > RTC_TIME_MIN) {
|
|
tv.tv_sec = _rtc_backup_time;
|
|
} else {
|
|
tv.tv_sec = RTC_TIME_MIN;
|
|
}
|
|
tv.tv_usec = 0;
|
|
settimeofday(&tv, NULL);
|
|
}
|
|
uint32_t getCurrentTime() override {
|
|
time_t _now;
|
|
time(&_now);
|
|
return _now;
|
|
}
|
|
void setCurrentTime(uint32_t time) override {
|
|
struct timeval tv;
|
|
tv.tv_sec = time;
|
|
tv.tv_usec = 0;
|
|
settimeofday(&tv, NULL);
|
|
_rtc_backup_time = time;
|
|
_rtc_backup_magic = RTC_BACKUP_MAGIC;
|
|
}
|
|
void tick() override {
|
|
time_t now;
|
|
time(&now);
|
|
if (now > RTC_TIME_MIN && (uint32_t)now != _rtc_backup_time) {
|
|
_rtc_backup_time = (uint32_t)now;
|
|
_rtc_backup_magic = RTC_BACKUP_MAGIC;
|
|
}
|
|
}
|
|
};
|
|
|
|
#endif
|