Restore Keymind features after LoRa OTA merge
This commit is contained in:
1 parent
4c0dbe707d
commit
4f8f36354e
45 files changed
+1320
-235
No files matched your search
+44
-11
@@ -774,6 +774,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
_prefs->ota_advert_interval = 0;
|
||||
_prefs->ota_max_hops = 3;
|
||||
#endif
|
||||
_prefs->telemetry_access = TELEMETRY_ACCESS_ALL;
|
||||
// A remainder larger than the smallest legacy MQTT gap (864) means an old fork
|
||||
// file with the zero-filled gap; detect and recover it below. Anything smaller
|
||||
// (upstream/flex 5-byte tails, or the ~384-byte keymind retry tail) takes the
|
||||
@@ -957,9 +958,12 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
if (file.available() >= (int)sizeof(_prefs->flood_channel_data_max_hops)) {
|
||||
file.read((uint8_t *)&_prefs->flood_channel_data_max_hops, sizeof(_prefs->flood_channel_data_max_hops));
|
||||
}
|
||||
if (file.available() >= (int)sizeof(_prefs->telemetry_access)) {
|
||||
file.read((uint8_t *)&_prefs->telemetry_access, sizeof(_prefs->telemetry_access));
|
||||
}
|
||||
}
|
||||
#if defined(ENABLE_OTA)
|
||||
// OTA config starts at 674. Guard every appended field so older files keep defaults.
|
||||
// OTA config starts at 675, after telemetry_access. Guard every appended field so older files keep defaults.
|
||||
if (file.available() >= (int)sizeof(_prefs->ota_autofetch)) {
|
||||
file.read((uint8_t *)&_prefs->ota_autofetch, sizeof(_prefs->ota_autofetch));
|
||||
}
|
||||
@@ -981,7 +985,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
if (file.available() >= (int)sizeof(_prefs->ota_max_hops)) {
|
||||
file.read((uint8_t *)&_prefs->ota_max_hops, sizeof(_prefs->ota_max_hops));
|
||||
}
|
||||
// next: 810
|
||||
// next: 811
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1044,6 +1048,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
_prefs->battery_alert_enabled = constrain(_prefs->battery_alert_enabled, 0, 1);
|
||||
_prefs->direct_retry_recent_enabled = constrain(_prefs->direct_retry_recent_enabled, 0, 1);
|
||||
_prefs->flood_channel_data_enabled = constrain(_prefs->flood_channel_data_enabled, 0, 1);
|
||||
_prefs->telemetry_access = constrain(_prefs->telemetry_access, 0, 1);
|
||||
if (_prefs->flood_channel_block_max_hops != FLOOD_CHANNEL_BLOCK_HOPS_ALL
|
||||
&& (_prefs->flood_channel_block_max_hops < 1 || _prefs->flood_channel_block_max_hops > 7)) {
|
||||
_prefs->flood_channel_block_max_hops = FLOOD_CHANNEL_BLOCK_HOPS_ALL;
|
||||
@@ -1163,15 +1168,16 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
|
||||
file.write((uint8_t *)&_prefs->flood_channel_data_enabled, sizeof(_prefs->flood_channel_data_enabled));
|
||||
file.write((uint8_t *)&_prefs->flood_channel_block_max_hops, sizeof(_prefs->flood_channel_block_max_hops));
|
||||
file.write((uint8_t *)&_prefs->flood_channel_data_max_hops, sizeof(_prefs->flood_channel_data_max_hops));
|
||||
file.write((uint8_t *)&_prefs->telemetry_access, sizeof(_prefs->telemetry_access)); // 674
|
||||
#if defined(ENABLE_OTA)
|
||||
file.write((uint8_t *)&_prefs->ota_autofetch, sizeof(_prefs->ota_autofetch)); // 674
|
||||
file.write((uint8_t *)&_prefs->ota_autoinstall, sizeof(_prefs->ota_autoinstall)); // 675
|
||||
file.write((uint8_t *)&_prefs->ota_signer_count, sizeof(_prefs->ota_signer_count)); // 676
|
||||
file.write((uint8_t *)_prefs->ota_signers, sizeof(_prefs->ota_signers)); // 677
|
||||
file.write((uint8_t *)&_prefs->ota_checkpoint_blocks, sizeof(_prefs->ota_checkpoint_blocks)); // 805
|
||||
file.write((uint8_t *)&_prefs->ota_advert_interval, sizeof(_prefs->ota_advert_interval)); // 807
|
||||
file.write((uint8_t *)&_prefs->ota_max_hops, sizeof(_prefs->ota_max_hops)); // 809
|
||||
// next: 810
|
||||
file.write((uint8_t *)&_prefs->ota_autofetch, sizeof(_prefs->ota_autofetch)); // 675
|
||||
file.write((uint8_t *)&_prefs->ota_autoinstall, sizeof(_prefs->ota_autoinstall)); // 676
|
||||
file.write((uint8_t *)&_prefs->ota_signer_count, sizeof(_prefs->ota_signer_count)); // 677
|
||||
file.write((uint8_t *)_prefs->ota_signers, sizeof(_prefs->ota_signers)); // 678
|
||||
file.write((uint8_t *)&_prefs->ota_checkpoint_blocks, sizeof(_prefs->ota_checkpoint_blocks)); // 806
|
||||
file.write((uint8_t *)&_prefs->ota_advert_interval, sizeof(_prefs->ota_advert_interval)); // 808
|
||||
file.write((uint8_t *)&_prefs->ota_max_hops, sizeof(_prefs->ota_max_hops)); // 810
|
||||
// next: 811
|
||||
#endif
|
||||
|
||||
file.close();
|
||||
@@ -1528,6 +1534,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
} else {
|
||||
strcpy(reply, "ERR: clock cannot go backwards");
|
||||
}
|
||||
#ifdef ESP_PLATFORM
|
||||
} else if (memcmp(command, "memory", 6) == 0) {
|
||||
sprintf(reply, "Free: %d, Min: %d, Max: %d, Queue: %d, IntFree: %d, IntMax: %d, PSRAM: %d/%d",
|
||||
ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap(),
|
||||
@@ -1536,15 +1543,27 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
(int)heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL),
|
||||
(int)heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
|
||||
(int)heap_caps_get_total_size(MALLOC_CAP_SPIRAM));
|
||||
#endif
|
||||
} else if (memcmp(command, "start ota", 9) == 0 && (command[9] == 0 || command[9] == ' ')) {
|
||||
// Manual OTA: bring up the ElegantOTA server for a hand-uploaded binary.
|
||||
// Manual OTA: bring up the board's browser server for a hand-uploaded binary.
|
||||
if (!_board->startOTAUpdate(_prefs->node_name, reply)) {
|
||||
strcpy(reply, "Error");
|
||||
}
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(LIGHTWEIGHT_WIFI_OTA)
|
||||
else {
|
||||
// Keep WiFi up, but release MQTT/TLS heap while the browser uploader runs.
|
||||
_callbacks->setBridgeState(false);
|
||||
}
|
||||
#endif
|
||||
} else if (memcmp(command, "stop ota", 8) == 0 && (command[8] == 0 || command[8] == ' ')) {
|
||||
if (!_board->stopOTAUpdate(reply)) {
|
||||
strcpy(reply, "Error");
|
||||
}
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(LIGHTWEIGHT_WIFI_OTA)
|
||||
else if (_prefs->bridge_enabled) {
|
||||
_callbacks->setBridgeState(true);
|
||||
}
|
||||
#endif
|
||||
} else if (memcmp(command, "clock", 5) == 0) {
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
DateTime dt = DateTime(now);
|
||||
@@ -1881,6 +1900,18 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
_prefs->allow_read_only = memcmp(&config[16], "on", 2) == 0;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "telemetry.access ", 17) == 0) {
|
||||
if (strcmp(&config[17], "all") == 0) {
|
||||
_prefs->telemetry_access = TELEMETRY_ACCESS_ALL;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (strcmp(&config[17], "acl") == 0) {
|
||||
_prefs->telemetry_access = TELEMETRY_ACCESS_ACL;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "ERROR: telemetry.access must be all or acl");
|
||||
}
|
||||
} else if (memcmp(config, "flood.advert.interval ", 22) == 0) {
|
||||
int hours = _atoi(&config[22]);
|
||||
if ((hours > 0 && hours < 3) || (hours > 168)) {
|
||||
@@ -2509,6 +2540,8 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
sprintf(reply, "> %d", (uint32_t) _prefs->multi_acks);
|
||||
} else if (memcmp(config, "allow.read.only", 15) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->allow_read_only ? "on" : "off");
|
||||
} else if (memcmp(config, "telemetry.access", 16) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->telemetry_access == TELEMETRY_ACCESS_ACL ? "acl" : "all");
|
||||
} else if (memcmp(config, "flood.advert.interval", 21) == 0) {
|
||||
sprintf(reply, "> %d", ((uint32_t) _prefs->flood_advert_interval));
|
||||
} else if (memcmp(config, "advert.interval", 15) == 0) {
|
||||
|
||||
@@ -99,6 +99,9 @@
|
||||
#define DIRECT_RETRY_PREFS_MAGIC_0 0xD1
|
||||
#define DIRECT_RETRY_PREFS_MAGIC_1 0x52
|
||||
|
||||
#define TELEMETRY_ACCESS_ALL 0
|
||||
#define TELEMETRY_ACCESS_ACL 1
|
||||
|
||||
struct NodePrefs { // persisted to file
|
||||
float airtime_factor;
|
||||
char node_name[32];
|
||||
@@ -176,10 +179,11 @@ struct NodePrefs { // persisted to file
|
||||
uint8_t flood_channel_data_enabled;
|
||||
uint8_t flood_channel_block_max_hops;
|
||||
uint8_t flood_channel_data_max_hops;
|
||||
uint8_t telemetry_access;
|
||||
|
||||
// NOTE: observer settings (MQTT/WiFi/timezone/SNMP/alert) were moved out of
|
||||
// NodePrefs into MQTTPrefs (persisted to /mqtt_prefs) so this struct stays
|
||||
// aligned with upstream (plus the keymind retry/flood tail above). See
|
||||
// aligned with upstream (plus the keymind retry/flood/telemetry tail above). See
|
||||
// struct MQTTPrefs below.
|
||||
|
||||
#if defined(ENABLE_OTA)
|
||||
|
||||
+260
-1
@@ -3,7 +3,266 @@
|
||||
#include "ESP32Board.h"
|
||||
#include <target.h>
|
||||
|
||||
#if defined(ADMIN_PASSWORD) && !defined(DISABLE_WIFI_OTA) // Repeater or Room Server only
|
||||
#if defined(ADMIN_PASSWORD) && defined(LIGHTWEIGHT_WIFI_OTA)
|
||||
#include <WiFi.h>
|
||||
#include <Update.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <strings.h>
|
||||
|
||||
static bool lightweight_ota_started_ap;
|
||||
|
||||
static const char LIGHTWEIGHT_OTA_PAGE[] = R"HTML(<!doctype html>
|
||||
<html><head><meta name="viewport" content="width=device-width"><title>MeshCore OTA</title>
|
||||
<style>body{font-family:sans-serif;max-width:32rem;margin:3rem auto;padding:0 1rem}button,input{font-size:1rem;margin:.5rem 0}pre{white-space:pre-wrap}</style></head>
|
||||
<body><h2>MeshCore firmware update</h2><input id="file" type="file" accept=".bin,application/octet-stream"><br>
|
||||
<button id="upload">Upload and reboot</button><pre id="status"></pre><script>
|
||||
const f=document.getElementById('file'),s=document.getElementById('status'),b=document.getElementById('upload');
|
||||
b.onclick=()=>{if(!f.files.length){s.textContent='Select a firmware .bin file.';return}b.disabled=true;
|
||||
const x=new XMLHttpRequest();x.open('POST','/update');x.setRequestHeader('Content-Type','application/octet-stream');
|
||||
x.upload.onprogress=e=>{if(e.lengthComputable)s.textContent='Uploading '+Math.round(e.loaded*100/e.total)+'%'};
|
||||
x.onload=()=>{s.textContent=x.responseText;b.disabled=false};x.onerror=()=>{s.textContent='Upload failed';b.disabled=false};x.send(f.files[0])};
|
||||
</script></body></html>)HTML";
|
||||
|
||||
class LightweightOTAServer {
|
||||
WiFiServer server{80};
|
||||
TaskHandle_t task = nullptr;
|
||||
volatile bool running = false;
|
||||
ESP32Board* board = nullptr;
|
||||
|
||||
static void sendResponse(WiFiClient& client, int code, const char* reason,
|
||||
const char* content_type, const char* body, size_t length) {
|
||||
client.printf("HTTP/1.1 %d %s\r\nContent-Type: %s\r\nContent-Length: %u\r\n"
|
||||
"Connection: close\r\nCache-Control: no-store\r\n\r\n",
|
||||
code, reason, content_type, static_cast<unsigned>(length));
|
||||
if (length) client.write(reinterpret_cast<const uint8_t*>(body), length);
|
||||
}
|
||||
|
||||
bool readLine(WiFiClient& client, char* line, size_t capacity) {
|
||||
size_t length = 0;
|
||||
uint32_t deadline = millis() + 5000;
|
||||
while (running && client.connected()) {
|
||||
while (client.available()) {
|
||||
int c = client.read();
|
||||
if (c < 0) break;
|
||||
if (c == '\n') {
|
||||
if (length && line[length - 1] == '\r') length--;
|
||||
line[length] = 0;
|
||||
return true;
|
||||
}
|
||||
if (length + 1 < capacity) line[length++] = static_cast<char>(c);
|
||||
}
|
||||
if (static_cast<int32_t>(millis() - deadline) >= 0) break;
|
||||
delay(1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void sendPage(WiFiClient& client) {
|
||||
sendResponse(client, 200, "OK", "text/html", LIGHTWEIGHT_OTA_PAGE,
|
||||
strlen(LIGHTWEIGHT_OTA_PAGE));
|
||||
}
|
||||
|
||||
void sendLog(WiFiClient& client) {
|
||||
File log = SPIFFS.open("/packet_log", FILE_READ);
|
||||
if (!log) {
|
||||
static const char missing[] = "packet log not found";
|
||||
sendResponse(client, 404, "Not Found", "text/plain", missing, sizeof(missing) - 1);
|
||||
return;
|
||||
}
|
||||
|
||||
client.printf("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %u\r\n"
|
||||
"Connection: close\r\nCache-Control: no-store\r\n\r\n",
|
||||
static_cast<unsigned>(log.size()));
|
||||
uint8_t buf[1024];
|
||||
while (running && log.available() && client.connected()) {
|
||||
size_t count = log.read(buf, sizeof(buf));
|
||||
if (!count || client.write(buf, count) != count) break;
|
||||
}
|
||||
log.close();
|
||||
}
|
||||
|
||||
void sendUpdateError(WiFiClient& client, int code, const char* reason) {
|
||||
char error[96];
|
||||
snprintf(error, sizeof(error), "OTA error: %s", Update.errorString());
|
||||
sendResponse(client, code, reason, "text/plain", error, strlen(error));
|
||||
}
|
||||
|
||||
void receiveUpdate(WiFiClient& client, size_t total) {
|
||||
if (total == 0) {
|
||||
static const char empty[] = "empty firmware image";
|
||||
sendResponse(client, 400, "Bad Request", "text/plain", empty, sizeof(empty) - 1);
|
||||
return;
|
||||
}
|
||||
|
||||
board->setInhibitSleep(true);
|
||||
if (!Update.begin(total, U_FLASH)) {
|
||||
sendUpdateError(client, 400, "Bad Request");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t buf[1024];
|
||||
size_t received_total = 0;
|
||||
uint32_t deadline = millis() + 15000;
|
||||
while (running && received_total < total && client.connected()) {
|
||||
int available = client.available();
|
||||
if (available <= 0) {
|
||||
if (static_cast<int32_t>(millis() - deadline) >= 0) break;
|
||||
delay(1);
|
||||
continue;
|
||||
}
|
||||
size_t wanted = total - received_total;
|
||||
if (wanted > sizeof(buf)) wanted = sizeof(buf);
|
||||
if (wanted > static_cast<size_t>(available)) wanted = static_cast<size_t>(available);
|
||||
int received = client.read(buf, wanted);
|
||||
if (received <= 0) break;
|
||||
deadline = millis() + 15000;
|
||||
if (Update.write(buf, static_cast<size_t>(received)) != static_cast<size_t>(received)) {
|
||||
sendUpdateError(client, 500, "Internal Server Error");
|
||||
Update.abort();
|
||||
return;
|
||||
}
|
||||
received_total += static_cast<size_t>(received);
|
||||
}
|
||||
|
||||
if (received_total != total) {
|
||||
Update.abort();
|
||||
static const char incomplete[] = "firmware upload incomplete";
|
||||
sendResponse(client, 408, "Request Timeout", "text/plain", incomplete, sizeof(incomplete) - 1);
|
||||
return;
|
||||
}
|
||||
if (!Update.end()) {
|
||||
sendUpdateError(client, 500, "Internal Server Error");
|
||||
return;
|
||||
}
|
||||
|
||||
static const char success[] = "OK - firmware installed; rebooting";
|
||||
sendResponse(client, 200, "OK", "text/plain", success, sizeof(success) - 1);
|
||||
delay(500);
|
||||
client.stop();
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
void handleClient(WiFiClient& client) {
|
||||
char line[256];
|
||||
if (!readLine(client, line, sizeof(line))) return;
|
||||
bool get_page = strncmp(line, "GET / ", 6) == 0 || strncmp(line, "GET /update ", 12) == 0;
|
||||
bool get_log = strncmp(line, "GET /log ", 9) == 0;
|
||||
bool post_update = strncmp(line, "POST /update ", 13) == 0;
|
||||
|
||||
size_t content_length = 0;
|
||||
do {
|
||||
if (!readLine(client, line, sizeof(line))) return;
|
||||
if (strncasecmp(line, "Content-Length:", 15) == 0) {
|
||||
content_length = static_cast<size_t>(strtoul(line + 15, nullptr, 10));
|
||||
}
|
||||
} while (line[0]);
|
||||
|
||||
if (get_page) sendPage(client);
|
||||
else if (get_log) sendLog(client);
|
||||
else if (post_update) receiveUpdate(client, content_length);
|
||||
else {
|
||||
static const char missing[] = "not found";
|
||||
sendResponse(client, 404, "Not Found", "text/plain", missing, sizeof(missing) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void taskEntry(void* arg) {
|
||||
LightweightOTAServer* self = static_cast<LightweightOTAServer*>(arg);
|
||||
while (self->running) {
|
||||
WiFiClient client = self->server.available();
|
||||
if (client) {
|
||||
client.setTimeout(15000);
|
||||
self->handleClient(client);
|
||||
client.stop();
|
||||
} else {
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
self->task = nullptr;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
public:
|
||||
bool begin(ESP32Board* owner) {
|
||||
if (running) return true;
|
||||
if (task != nullptr) return false;
|
||||
board = owner;
|
||||
server.begin();
|
||||
server.setNoDelay(true);
|
||||
running = true;
|
||||
if (xTaskCreate(taskEntry, "ota-http", 6144, this, 4, &task) != pdPASS) {
|
||||
running = false;
|
||||
server.stop();
|
||||
task = nullptr;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void end() {
|
||||
running = false;
|
||||
server.stop();
|
||||
for (unsigned i = 0; task != nullptr && i < 100; i++) delay(10);
|
||||
board = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
static LightweightOTAServer lightweight_ota_server;
|
||||
|
||||
bool ESP32Board::startOTAUpdate(const char* id, char reply[]) {
|
||||
(void)id;
|
||||
inhibit_sleep = true;
|
||||
|
||||
IPAddress ip;
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
ip = WiFi.localIP();
|
||||
} else {
|
||||
if (!lightweight_ota_started_ap) {
|
||||
lightweight_ota_started_ap = WiFi.softAP("MeshCore-OTA", nullptr);
|
||||
}
|
||||
if (!lightweight_ota_started_ap) {
|
||||
inhibit_sleep = false;
|
||||
strcpy(reply, "ERR: OTA WiFi failed");
|
||||
return false;
|
||||
}
|
||||
ip = WiFi.softAPIP();
|
||||
}
|
||||
|
||||
if (ota_server == nullptr) {
|
||||
if (!lightweight_ota_server.begin(this)) {
|
||||
if (lightweight_ota_started_ap) WiFi.softAPdisconnect(true);
|
||||
lightweight_ota_started_ap = false;
|
||||
inhibit_sleep = false;
|
||||
strcpy(reply, "ERR: OTA server failed");
|
||||
return false;
|
||||
}
|
||||
ota_server = &lightweight_ota_server;
|
||||
}
|
||||
|
||||
snprintf(reply, 160, "Started: http://%s/update", ip.toString().c_str());
|
||||
MESH_DEBUG_PRINTLN("startOTAUpdate: %s", reply);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ESP32Board::stopOTAUpdate(char reply[]) {
|
||||
if (ota_server == nullptr) {
|
||||
strcpy(reply, "OK - OTA not running");
|
||||
return true;
|
||||
}
|
||||
|
||||
lightweight_ota_server.end();
|
||||
ota_server = nullptr;
|
||||
if (lightweight_ota_started_ap) WiFi.softAPdisconnect(true);
|
||||
lightweight_ota_started_ap = false;
|
||||
inhibit_sleep = false;
|
||||
strcpy(reply, "OK - OTA stopped");
|
||||
MESH_DEBUG_PRINTLN("stopOTAUpdate: %s", reply);
|
||||
return true;
|
||||
}
|
||||
|
||||
#elif defined(ADMIN_PASSWORD) && !defined(DISABLE_WIFI_OTA) // Repeater or Room Server only
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
@@ -16,13 +16,19 @@
|
||||
#include "esp_system.h"
|
||||
#include <driver/rtc_io.h>
|
||||
|
||||
#if !defined(LIGHTWEIGHT_WIFI_OTA)
|
||||
class AsyncWebServer;
|
||||
#endif
|
||||
|
||||
class ESP32Board : public mesh::MainBoard {
|
||||
protected:
|
||||
uint8_t startup_reason;
|
||||
bool inhibit_sleep = false;
|
||||
#if defined(LIGHTWEIGHT_WIFI_OTA)
|
||||
void* ota_server = nullptr;
|
||||
#else
|
||||
AsyncWebServer* ota_server = nullptr;
|
||||
#endif
|
||||
static inline portMUX_TYPE sleepMux = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
public:
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "JWTHelper.h"
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <SHA256.h>
|
||||
#include <string.h>
|
||||
@@ -196,3 +199,4 @@ size_t JWTHelper::createPayload(
|
||||
return base64UrlEncode((uint8_t*)jsonBuffer, len, output, outputSize);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
#include "MeshCore.h"
|
||||
#include "Identity.h"
|
||||
|
||||
@@ -85,3 +87,5 @@ private:
|
||||
);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "MQTTMessageBuilder.h"
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <cstring>
|
||||
#include <math.h>
|
||||
@@ -435,4 +438,6 @@ void MQTTMessageBuilder::packetToHex(mesh::Packet* packet, char* hex, size_t hex
|
||||
|
||||
// Convert serialized packet to hex
|
||||
bytesToHex(raw_buf, raw_len, hex, hex_size);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
#include "MeshCore.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <Mesh.h>
|
||||
@@ -226,3 +228,5 @@ private:
|
||||
*/
|
||||
static void packetToHex(mesh::Packet* packet, char* hex, size_t hex_size);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,175 @@
|
||||
#include "SensorManager.h"
|
||||
|
||||
#if ENV_INCLUDE_GPS
|
||||
#ifndef GPS_TELEMETRY_CACHE_INTERVAL_SEC
|
||||
#define GPS_TELEMETRY_CACHE_INTERVAL_SEC (2UL * 60UL * 60UL)
|
||||
#endif
|
||||
#ifndef GPS_TELEMETRY_MAX_ACQUIRE_SEC
|
||||
#define GPS_TELEMETRY_MAX_ACQUIRE_SEC (15UL * 60UL)
|
||||
#endif
|
||||
#ifndef GPS_TELEMETRY_STABLE_SEC
|
||||
#define GPS_TELEMETRY_STABLE_SEC 30UL
|
||||
#endif
|
||||
#ifndef GPS_TELEMETRY_HOLD_SEC
|
||||
#define GPS_TELEMETRY_HOLD_SEC (2UL * 60UL * 60UL)
|
||||
#endif
|
||||
#ifndef GPS_TELEMETRY_MAX_STALE_SEC
|
||||
#define GPS_TELEMETRY_MAX_STALE_SEC (12UL * 60UL * 60UL)
|
||||
#endif
|
||||
#ifndef GPS_TELEMETRY_STABLE_RADIUS_M
|
||||
#define GPS_TELEMETRY_STABLE_RADIUS_M 100.0f
|
||||
#endif
|
||||
|
||||
static bool millisDue(unsigned long now, unsigned long deadline) {
|
||||
return (long)(now - deadline) >= 0;
|
||||
}
|
||||
|
||||
static float gpsDistanceSquaredMeters(float lat1, float lon1, float lat2, float lon2) {
|
||||
static const float METERS_PER_DEGREE = 111320.0f;
|
||||
float x = (lon2 - lon1) * METERS_PER_DEGREE;
|
||||
float y = (lat2 - lat1) * METERS_PER_DEGREE;
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
bool SensorManager::gpsTelemetryHoldActive(unsigned long now) const {
|
||||
return gps_hold_until != 0 && !millisDue(now, gps_hold_until);
|
||||
}
|
||||
|
||||
bool SensorManager::gpsTelemetryCacheFresh(unsigned long now) const {
|
||||
return gps_cache_valid &&
|
||||
(unsigned long)(now - gps_cache_updated_at) <= GPS_TELEMETRY_MAX_STALE_SEC * 1000UL;
|
||||
}
|
||||
|
||||
void SensorManager::updateGpsTelemetryCache(float lat, float lon, float altitude, unsigned long now) {
|
||||
gps_cache_lat = lat;
|
||||
gps_cache_lon = lon;
|
||||
gps_cache_altitude = altitude;
|
||||
gps_cache_updated_at = now;
|
||||
gps_cache_valid = true;
|
||||
}
|
||||
|
||||
void SensorManager::maybeStopGpsForTelemetry(unsigned long now) {
|
||||
#if defined(PERSISTANT_GPS) || defined(FORCE_GPS_ALIVE)
|
||||
(void)now;
|
||||
#else
|
||||
if (telemetryGpsActive() && !gps_user_enabled && !gps_acquiring && !gpsTelemetryHoldActive(now)) {
|
||||
telemetryGpsStop();
|
||||
gps_next_cache_update_at = now + GPS_TELEMETRY_CACHE_INTERVAL_SEC * 1000UL;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void SensorManager::beginGpsTelemetryAcquisition(unsigned long now) {
|
||||
if (!telemetryGpsDetected() || gps_acquiring) return;
|
||||
|
||||
gps_acquiring = true;
|
||||
gps_acquire_has_fix = false;
|
||||
gps_acquire_started_at = now;
|
||||
gps_stable_started_at = now;
|
||||
gps_weighted_lat = 0;
|
||||
gps_weighted_lon = 0;
|
||||
gps_weighted_altitude = 0;
|
||||
gps_weight_sum = 0;
|
||||
gps_weight_count = 0;
|
||||
if (!telemetryGpsActive()) telemetryGpsStart();
|
||||
}
|
||||
|
||||
void SensorManager::finishGpsTelemetryAcquisition(unsigned long now, bool use_weighted_average) {
|
||||
if (use_weighted_average && gps_weight_sum > 0) {
|
||||
updateGpsTelemetryCache(gps_weighted_lat / gps_weight_sum,
|
||||
gps_weighted_lon / gps_weight_sum,
|
||||
gps_weighted_altitude / gps_weight_sum,
|
||||
now);
|
||||
}
|
||||
gps_acquiring = false;
|
||||
gps_next_cache_update_at = now + GPS_TELEMETRY_CACHE_INTERVAL_SEC * 1000UL;
|
||||
maybeStopGpsForTelemetry(now);
|
||||
}
|
||||
|
||||
bool SensorManager::queryGpsTelemetry(uint8_t requester_permissions, CayenneLPP& telemetry) {
|
||||
if (!(requester_permissions & TELEM_PERM_LOCATION) || !telemetryGpsDetected()) return false;
|
||||
|
||||
unsigned long now = millis();
|
||||
gps_hold_until = now + GPS_TELEMETRY_HOLD_SEC * 1000UL;
|
||||
if (!telemetryGpsActive()) telemetryGpsStart();
|
||||
if (!gpsTelemetryCacheFresh(now) && !gps_acquiring) beginGpsTelemetryAcquisition(now);
|
||||
|
||||
if (!gpsTelemetryCacheFresh(now)) return false;
|
||||
telemetry.addGPS(TELEM_CHANNEL_SELF, gps_cache_lat, gps_cache_lon, gps_cache_altitude);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SensorManager::processGpsTelemetryFix(float lat, float lon, float altitude, unsigned long now) {
|
||||
if (!gps_acquiring) {
|
||||
if (gps_user_enabled || gpsTelemetryHoldActive(now)) {
|
||||
updateGpsTelemetryCache(lat, lon, altitude, now);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!gps_acquire_has_fix) {
|
||||
gps_acquire_has_fix = true;
|
||||
gps_stable_started_at = now;
|
||||
gps_stable_origin_lat = lat;
|
||||
gps_stable_origin_lon = lon;
|
||||
}
|
||||
|
||||
static const float STABLE_RADIUS_M2 =
|
||||
GPS_TELEMETRY_STABLE_RADIUS_M * GPS_TELEMETRY_STABLE_RADIUS_M;
|
||||
if (gpsDistanceSquaredMeters(gps_stable_origin_lat, gps_stable_origin_lon, lat, lon) > STABLE_RADIUS_M2) {
|
||||
updateGpsTelemetryCache(lat, lon, altitude, now);
|
||||
finishGpsTelemetryAcquisition(now, false);
|
||||
return;
|
||||
}
|
||||
|
||||
float weight = (float)++gps_weight_count;
|
||||
gps_weighted_lat += lat * weight;
|
||||
gps_weighted_lon += lon * weight;
|
||||
gps_weighted_altitude += altitude * weight;
|
||||
gps_weight_sum += weight;
|
||||
if (millisDue(now, gps_stable_started_at + GPS_TELEMETRY_STABLE_SEC * 1000UL)) {
|
||||
finishGpsTelemetryAcquisition(now, true);
|
||||
}
|
||||
}
|
||||
|
||||
void SensorManager::loopGpsTelemetry(unsigned long now) {
|
||||
if (!gps_user_enabled && !gpsTelemetryHoldActive(now) && !gps_acquiring) {
|
||||
maybeStopGpsForTelemetry(now);
|
||||
}
|
||||
if (gps_location_access_available && telemetryGpsDetected() && !gps_user_enabled &&
|
||||
!gpsTelemetryHoldActive(now) && !gps_acquiring &&
|
||||
(gps_next_cache_update_at == 0 || millisDue(now, gps_next_cache_update_at))) {
|
||||
beginGpsTelemetryAcquisition(now);
|
||||
}
|
||||
if (gps_acquiring && millisDue(now, gps_acquire_started_at + GPS_TELEMETRY_MAX_ACQUIRE_SEC * 1000UL)) {
|
||||
finishGpsTelemetryAcquisition(now, gps_acquire_has_fix && gps_weight_sum > 0);
|
||||
}
|
||||
}
|
||||
|
||||
void SensorManager::setGpsTelemetryUserEnabled(bool enabled) {
|
||||
gps_user_enabled = enabled;
|
||||
unsigned long now = millis();
|
||||
if (enabled) {
|
||||
if (telemetryGpsDetected() && !telemetryGpsActive()) telemetryGpsStart();
|
||||
} else {
|
||||
maybeStopGpsForTelemetry(now);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void SensorManager::setTelemetryLocationAccessAvailable(bool available) {
|
||||
#if ENV_INCLUDE_GPS
|
||||
unsigned long now = millis();
|
||||
if (gps_location_access_available == available) return;
|
||||
|
||||
gps_location_access_available = available;
|
||||
if (available) {
|
||||
gps_next_cache_update_at = 0;
|
||||
} else if (gps_acquiring && !gps_user_enabled && !gpsTelemetryHoldActive(now)) {
|
||||
gps_acquiring = false;
|
||||
maybeStopGpsForTelemetry(now);
|
||||
}
|
||||
#else
|
||||
(void)available;
|
||||
#endif
|
||||
}
|
||||
@@ -11,6 +11,47 @@
|
||||
#define TELEM_CHANNEL_SELF 1 // LPP data channel for 'self' device
|
||||
|
||||
class SensorManager {
|
||||
#if ENV_INCLUDE_GPS
|
||||
bool gps_cache_valid = false;
|
||||
bool gps_location_access_available = false;
|
||||
bool gps_user_enabled = false;
|
||||
bool gps_acquiring = false;
|
||||
bool gps_acquire_has_fix = false;
|
||||
float gps_cache_lat = 0;
|
||||
float gps_cache_lon = 0;
|
||||
float gps_cache_altitude = 0;
|
||||
float gps_stable_origin_lat = 0;
|
||||
float gps_stable_origin_lon = 0;
|
||||
float gps_weighted_lat = 0;
|
||||
float gps_weighted_lon = 0;
|
||||
float gps_weighted_altitude = 0;
|
||||
float gps_weight_sum = 0;
|
||||
uint16_t gps_weight_count = 0;
|
||||
unsigned long gps_cache_updated_at = 0;
|
||||
unsigned long gps_next_cache_update_at = 0;
|
||||
unsigned long gps_hold_until = 0;
|
||||
unsigned long gps_acquire_started_at = 0;
|
||||
unsigned long gps_stable_started_at = 0;
|
||||
|
||||
bool gpsTelemetryHoldActive(unsigned long now) const;
|
||||
bool gpsTelemetryCacheFresh(unsigned long now) const;
|
||||
void beginGpsTelemetryAcquisition(unsigned long now);
|
||||
void finishGpsTelemetryAcquisition(unsigned long now, bool use_weighted_average);
|
||||
void updateGpsTelemetryCache(float lat, float lon, float altitude, unsigned long now);
|
||||
void maybeStopGpsForTelemetry(unsigned long now);
|
||||
|
||||
protected:
|
||||
virtual bool telemetryGpsDetected() const { return false; }
|
||||
virtual bool telemetryGpsActive() const { return false; }
|
||||
virtual void telemetryGpsStart() { }
|
||||
virtual void telemetryGpsStop() { }
|
||||
bool queryGpsTelemetry(uint8_t requester_permissions, CayenneLPP& telemetry);
|
||||
void processGpsTelemetryFix(float lat, float lon, float altitude, unsigned long now);
|
||||
void loopGpsTelemetry(unsigned long now);
|
||||
void setGpsTelemetryUserEnabled(bool enabled);
|
||||
bool isGpsTelemetryUserEnabled() const { return gps_user_enabled; }
|
||||
#endif
|
||||
|
||||
public:
|
||||
double node_lat, node_lon; // modify these, if you want to affect Advert location
|
||||
double node_altitude; // altitude in meters
|
||||
@@ -20,6 +61,7 @@ public:
|
||||
virtual bool begin() { return false; }
|
||||
virtual bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) { return false; }
|
||||
virtual void loop() { }
|
||||
virtual void setTelemetryLocationAccessAvailable(bool available);
|
||||
virtual int getNumSettings() const { return 0; }
|
||||
virtual const char* getSettingName(int i) const { return NULL; }
|
||||
virtual const char* getSettingValue(int i) const { return NULL; }
|
||||
|
||||
@@ -689,9 +689,9 @@ bool EnvironmentSensorManager::begin() {
|
||||
bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
|
||||
next_available_channel = TELEM_CHANNEL_SELF + 1;
|
||||
|
||||
if (requester_permissions & TELEM_PERM_LOCATION && gps_active) {
|
||||
telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
|
||||
}
|
||||
#if ENV_INCLUDE_GPS
|
||||
queryGpsTelemetry(requester_permissions, telemetry);
|
||||
#endif
|
||||
|
||||
if (requester_permissions & TELEM_PERM_ENVIRONMENT) {
|
||||
for (int i = 0; i < _active_sensor_count; i++) {
|
||||
@@ -726,7 +726,7 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const {
|
||||
int settings = 0;
|
||||
#if ENV_INCLUDE_GPS
|
||||
if (gps_detected && i == settings++) {
|
||||
return gps_active ? "1" : "0";
|
||||
return isGpsTelemetryUserEnabled() ? "1" : "0";
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
@@ -735,11 +735,7 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const {
|
||||
bool EnvironmentSensorManager::setSettingValue(const char* name, const char* value) {
|
||||
#if ENV_INCLUDE_GPS
|
||||
if (gps_detected && strcmp(name, "gps") == 0) {
|
||||
if (strcmp(value, "0") == 0) {
|
||||
stop_gps();
|
||||
} else {
|
||||
start_gps();
|
||||
}
|
||||
setGpsTelemetryUserEnabled(strcmp(value, "0") != 0);
|
||||
return true;
|
||||
}
|
||||
if (strcmp(name, "gps_interval") == 0) {
|
||||
@@ -784,6 +780,7 @@ void EnvironmentSensorManager::initBasicGPS() {
|
||||
MESH_DEBUG_PRINTLN("GPS detected");
|
||||
#ifdef PERSISTANT_GPS
|
||||
gps_active = true;
|
||||
setGpsTelemetryUserEnabled(true);
|
||||
return;
|
||||
#endif
|
||||
} else {
|
||||
@@ -876,6 +873,7 @@ bool EnvironmentSensorManager::gpsIsAwake(uint8_t ioPin){
|
||||
#endif
|
||||
|
||||
void EnvironmentSensorManager::start_gps() {
|
||||
if (gps_active) return;
|
||||
gps_active = true;
|
||||
#ifdef RAK_WISBLOCK_GPS
|
||||
pinMode(gpsResetPin, OUTPUT);
|
||||
@@ -911,32 +909,43 @@ void EnvironmentSensorManager::stop_gps() {
|
||||
void EnvironmentSensorManager::loop() {
|
||||
|
||||
#if ENV_INCLUDE_GPS
|
||||
static long next_gps_update = 0;
|
||||
static unsigned long next_gps_update = 0;
|
||||
unsigned long now = millis();
|
||||
loopGpsTelemetry(now);
|
||||
|
||||
if (gps_active) {
|
||||
_location->loop();
|
||||
}
|
||||
if (millis() > next_gps_update) {
|
||||
if ((long)(now - next_gps_update) >= 0) {
|
||||
|
||||
if(gps_active){
|
||||
#ifdef RAK_WISBLOCK_GPS
|
||||
if ((i2cGPSFlag || serialGPSFlag) && _location->isValid()) {
|
||||
node_lat = ((double)_location->getLatitude())/1000000.;
|
||||
node_lon = ((double)_location->getLongitude())/1000000.;
|
||||
float gps_lat = ((float)_location->getLatitude()) / 1000000.0f;
|
||||
float gps_lon = ((float)_location->getLongitude()) / 1000000.0f;
|
||||
float gps_altitude = ((float)_location->getAltitude()) / 1000.0f;
|
||||
node_lat = gps_lat;
|
||||
node_lon = gps_lon;
|
||||
MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
|
||||
node_altitude = ((double)_location->getAltitude()) / 1000.0;
|
||||
node_altitude = gps_altitude;
|
||||
MESH_DEBUG_PRINTLN("lat %f lon %f alt %f", node_lat, node_lon, node_altitude);
|
||||
processGpsTelemetryFix(gps_lat, gps_lon, gps_altitude, now);
|
||||
}
|
||||
#else
|
||||
if (_location->isValid()) {
|
||||
node_lat = ((double)_location->getLatitude())/1000000.;
|
||||
node_lon = ((double)_location->getLongitude())/1000000.;
|
||||
float gps_lat = ((float)_location->getLatitude()) / 1000000.0f;
|
||||
float gps_lon = ((float)_location->getLongitude()) / 1000000.0f;
|
||||
float gps_altitude = ((float)_location->getAltitude()) / 1000.0f;
|
||||
node_lat = gps_lat;
|
||||
node_lon = gps_lon;
|
||||
MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
|
||||
node_altitude = ((double)_location->getAltitude()) / 1000.0;
|
||||
node_altitude = gps_altitude;
|
||||
MESH_DEBUG_PRINTLN("lat %f lon %f alt %f", node_lat, node_lon, node_altitude);
|
||||
processGpsTelemetryFix(gps_lat, gps_lon, gps_altitude, now);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
next_gps_update = millis() + (gps_update_interval_sec * 1000);
|
||||
next_gps_update = now + (gps_update_interval_sec * 1000);
|
||||
}
|
||||
#endif
|
||||
#if ENV_INCLUDE_BME680_BSEC
|
||||
|
||||
@@ -27,6 +27,10 @@ protected:
|
||||
LocationProvider* _location;
|
||||
void start_gps();
|
||||
void stop_gps();
|
||||
bool telemetryGpsDetected() const override { return gps_detected; }
|
||||
bool telemetryGpsActive() const override { return gps_active; }
|
||||
void telemetryGpsStart() override { start_gps(); }
|
||||
void telemetryGpsStop() override { stop_gps(); }
|
||||
void initBasicGPS();
|
||||
#ifdef RAK_BOARD
|
||||
void rakGPSInit();
|
||||
|
||||
Reference in new issue
Block a user