Merge remote-tracking branch 'review/observer-firmware' into keymindCascade
# Conflicts: # .github/workflows/build-observer-firmwares.yml # .gitignore # MQTT_IMPLEMENTATION.md # MQTT_INTERNALS.md # README.md # STABILITY_TESTABILITY_HANDOFF.md # docs/cli_commands.md # examples/companion_radio/MyMesh.cpp # examples/simple_repeater/MyMesh.cpp # examples/simple_repeater/UITask.cpp # examples/simple_room_server/MyMesh.cpp # examples/simple_room_server/UITask.cpp # examples/simple_sensor/SensorMesh.cpp # platformio.ini # scripts/generate_webconfig_html.py # scripts/webconfig_mock_server.py # src/helpers/CommonCLI.cpp # src/helpers/CommonCLI.h # src/helpers/JWTHelper.cpp # src/helpers/MQTTDefaults.h # src/helpers/MQTTMessageBuilder.cpp # src/helpers/MQTTPayloadBuilder.h # src/helpers/MQTTPresets.h # src/helpers/bridges/MQTTBridge.h # src/helpers/esp32/WebConfigServer.cpp # src/helpers/esp32/WebConfigServer.h # src/helpers/ui/SH1106Display.cpp # variants/lilygo_tbeam_SX1262/platformio.ini # variants/lilygo_tbeam_SX1276/platformio.ini # variants/lilygo_tlora_v2_1/platformio.ini # variants/thinknode_m7/platformio.ini # webui/index.html
This commit is contained in:
commit
a04a284a3f
74 files changed
+7857
-2483
No files matched your search
@@ -1353,7 +1353,6 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
|
||||
send_unscoped = false;
|
||||
|
||||
// defaults
|
||||
memset(&_prefs, 0, sizeof(_prefs));
|
||||
_prefs.airtime_factor = 1.0; // one half
|
||||
strcpy(_prefs.node_name, "NONAME");
|
||||
_prefs.freq = LORA_FREQ;
|
||||
@@ -1540,6 +1539,7 @@ void MyMesh::begin(bool has_display) {
|
||||
#endif
|
||||
_webconfig = new WebConfigServer(this, web_mqtt_prefs, false,
|
||||
self_id.pub_key, FIRMWARE_VERSION,
|
||||
FIRMWARE_BUILD_DATE,
|
||||
"companion", board.getManufacturerName());
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <cstdint> // For uint8_t, uint32_t
|
||||
|
||||
#define TELEM_MODE_DENY 0
|
||||
|
||||
+361
-140
@@ -18,6 +18,9 @@
|
||||
#include <sys/types.h>
|
||||
extern "C" caddr_t _sbrk(int increment);
|
||||
#endif
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
#include <helpers/MQTTConnectionPolicy.h> // kSyncedClockEpoch
|
||||
#endif
|
||||
|
||||
/* ------------------------------ Config -------------------------------- */
|
||||
|
||||
@@ -1057,6 +1060,19 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
|
||||
}
|
||||
|
||||
void MyMesh::logTx(mesh::Packet *pkt, int len) {
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
if (neighbor_discover_active && pkt == neighbor_discover_request
|
||||
&& neighbor_discover_next < neighbor_discover_count) {
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_next];
|
||||
if (entry.status == ND_QUEUED) {
|
||||
entry.status = ND_PENDING;
|
||||
neighbor_discover_queried_count++;
|
||||
neighbor_discover_request = NULL;
|
||||
neighbor_discover_until = futureMillis(neighborDiscoverQueryTimeoutMs());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// MQTT bridge: always feed TX packets - bridge decides based on mqtt.tx setting
|
||||
if (mqtt_bridge) mqtt_bridge->sendPacket(pkt);
|
||||
@@ -1086,6 +1102,18 @@ void MyMesh::logTx(mesh::Packet *pkt, int len) {
|
||||
}
|
||||
|
||||
void MyMesh::logTxFail(mesh::Packet *pkt, int len) {
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
if (neighbor_discover_active && pkt == neighbor_discover_request
|
||||
&& neighbor_discover_next < neighbor_discover_count) {
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_next];
|
||||
if (entry.status == ND_QUEUED) {
|
||||
entry.status = ND_SEND_FAILED;
|
||||
neighbor_discover_request = NULL;
|
||||
neighbor_discover_until = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (_logging) {
|
||||
File f = openAppend(PACKET_LOG_FILE);
|
||||
if (f) {
|
||||
@@ -2473,9 +2501,9 @@ int MyMesh::searchPeersByHash(const uint8_t *hash) {
|
||||
// distinct from real ACL indices.
|
||||
if (neighbor_discover_active) {
|
||||
for (int i = 0; i < neighbor_discover_count && n < MAX_CLIENTS; i++) {
|
||||
auto& nb = neighbours[neighbor_discover[i].neighbour_idx];
|
||||
if (acl.getClient(nb.id.pub_key, PUB_KEY_SIZE) != nullptr) continue;
|
||||
if (nb.heard_timestamp > 0 && nb.id.isHashMatch(hash)) {
|
||||
auto& entry = neighbor_discover[i];
|
||||
if (acl.getClient(entry.id.pub_key, PUB_KEY_SIZE) != nullptr) continue;
|
||||
if (entry.heard_timestamp > 0 && entry.id.isHashMatch(hash)) {
|
||||
matching_peer_indexes[n++] = NEIGHBOR_DISCOVER_PEER_BASE + i;
|
||||
}
|
||||
}
|
||||
@@ -2496,7 +2524,7 @@ void MyMesh::getPeerSharedSecret(uint8_t *dest_secret, int peer_idx) {
|
||||
if (neighbor_discover_active && i >= NEIGHBOR_DISCOVER_PEER_BASE) {
|
||||
int oi = i - NEIGHBOR_DISCOVER_PEER_BASE;
|
||||
if (oi >= 0 && oi < neighbor_discover_count) {
|
||||
self_id.calcSharedSecret(dest_secret, neighbours[neighbor_discover[oi].neighbour_idx].id);
|
||||
self_id.calcSharedSecret(dest_secret, neighbor_discover[oi].id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -2579,8 +2607,8 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
||||
// scope-query response from it lands here -- match it against the overlay.
|
||||
if (neighbor_discover_active && type == PAYLOAD_TYPE_RESPONSE) {
|
||||
for (int oi = 0; oi < neighbor_discover_count; oi++) {
|
||||
auto& nb = neighbours[neighbor_discover[oi].neighbour_idx];
|
||||
if (client->id.matches(nb.id) && handleNeighborDiscoverResponse(oi, data, len)) {
|
||||
if (client->id.matches(neighbor_discover[oi].id)
|
||||
&& handleNeighborDiscoverResponse(oi, data, len)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -3086,12 +3114,20 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
neighbor_discover_count = 0;
|
||||
neighbor_discover_next = 0;
|
||||
neighbor_discover_publish_count = 0;
|
||||
neighbor_discover_queried_count = 0;
|
||||
neighbor_discover_json_size = 0;
|
||||
neighbor_discover_truncated = false;
|
||||
neighbor_discover_active = false;
|
||||
neighbor_table_refresh_active = false;
|
||||
neighbor_table_refresh_periodic = false;
|
||||
neighbor_discover_until = 0;
|
||||
neighbor_discover_request = NULL;
|
||||
next_neighbors_publish = 0;
|
||||
self_scopes_buf[0] = 0;
|
||||
self_default_scope_buf[0] = 0;
|
||||
neighbor_discover_origin[0] = 0;
|
||||
#endif
|
||||
|
||||
memset(default_scope.key, 0, sizeof(default_scope.key));
|
||||
@@ -9554,7 +9590,7 @@ bool MyMesh::startWebConfig(bool force_ap, char* reply) {
|
||||
owns_wifi = false;
|
||||
#endif
|
||||
_webconfig = new WebConfigServer(this, mqtt_prefs, owns_wifi,
|
||||
self_id.pub_key, getFirmwareVer(), getRole(),
|
||||
self_id.pub_key, getFirmwareVer(), getBuildDate(), getRole(),
|
||||
_cli.getBoard()->getManufacturerName());
|
||||
if (!_webconfig) {
|
||||
strcpy(reply, "Err: not enough memory for webconfig");
|
||||
@@ -9742,16 +9778,21 @@ void MyMesh::buildStatsJson(char* buf, size_t buf_size) {
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
MQTTBridge::SlotStatusSnapshot status;
|
||||
if (!MQTTBridge::getSlotStatusSnapshot(i, &status)) continue;
|
||||
char filter[24] = "";
|
||||
if (status.filter_mask != MQTTPacketFilter::kAllPacketTypes) {
|
||||
snprintf(filter, sizeof(filter), ",\"filt\":%u",
|
||||
(unsigned)status.filter_mask);
|
||||
}
|
||||
int written;
|
||||
if (status.has_publish_counts) {
|
||||
written = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\",\"ok\":%lu,\"err\":%lu}",
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\",\"ok\":%lu,\"err\":%lu%s}",
|
||||
first ? "" : ",", i + 1, status.name, status.state,
|
||||
status.publish_ok, status.publish_err);
|
||||
status.publish_ok, status.publish_err, filter);
|
||||
} else {
|
||||
written = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\"}",
|
||||
first ? "" : ",", i + 1, status.name, status.state);
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\"%s}",
|
||||
first ? "" : ",", i + 1, status.name, status.state, filter);
|
||||
}
|
||||
if (written < 0 || written >= static_cast<int>(buf_size - pos)) break;
|
||||
pos += written;
|
||||
@@ -10757,7 +10798,7 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
|
||||
}
|
||||
#elif defined(WITH_MQTT_BRIDGE)
|
||||
} else if (memcmp(command, "discover.scopes", 15) == 0) {
|
||||
strcpy(reply, "Err - not supported (requires PSRAM)");
|
||||
strcpy(reply, "Err - neighbors not enabled in this build");
|
||||
#endif
|
||||
} else{
|
||||
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
|
||||
@@ -11184,7 +11225,11 @@ void MyMesh::getLocalScopes(char* buf, size_t len) {
|
||||
|
||||
// Client side of the anon-regions request (the server side is handleAnonRegionsReq).
|
||||
// Inner payload: {tag(4)}{ANON_REQ_TYPE_REGIONS}{0x00 = zero-hop reply path}.
|
||||
bool MyMesh::sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag) {
|
||||
mesh::Packet* MyMesh::sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag) {
|
||||
// RxReservePacketManager keeps a four-packet emergency floor. Preflight one
|
||||
// extra free packet so its void queue API cannot silently shed this request.
|
||||
if (_mgr->getFreeCount() < NEIGHBOR_DISCOVER_MIN_FREE_PACKETS) return NULL;
|
||||
|
||||
uint8_t secret[PUB_KEY_SIZE];
|
||||
self_id.calcSharedSecret(secret, target);
|
||||
|
||||
@@ -11195,8 +11240,92 @@ bool MyMesh::sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag) {
|
||||
inner[5] = 0x00; // request a zero-hop reply path
|
||||
|
||||
mesh::Packet* pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, target, secret, inner, sizeof(inner));
|
||||
if (!pkt) return false;
|
||||
if (!pkt) return NULL;
|
||||
sendDirect(pkt, NULL, 0, 0);
|
||||
return pkt;
|
||||
}
|
||||
|
||||
bool MyMesh::cancelNeighborDiscoverRequest() {
|
||||
if (!neighbor_discover_request) return false;
|
||||
for (int i = _mgr->getOutboundTotal() - 1; i >= 0; i--) {
|
||||
if (_mgr->getOutboundByIdx(i) == neighbor_discover_request) {
|
||||
mesh::Packet* pkt = _mgr->removeOutboundByIdx(i);
|
||||
if (pkt) releasePacket(pkt);
|
||||
neighbor_discover_request = NULL;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// This timer starts after the request finishes transmitting. Allow the server
|
||||
// delay, the responder's full CAD deferral window plus one maximum retry
|
||||
// overshoot, and airtime for one priority-0 packet ahead of the response plus
|
||||
// the response itself. The radio estimate scales with SF, bandwidth, coding
|
||||
// rate, and preamble.
|
||||
uint32_t MyMesh::neighborDiscoverQueryTimeoutMs() const {
|
||||
uint32_t response_airtime = _radio->getEstAirtimeFor(MAX_PACKET_PAYLOAD + 2);
|
||||
return SERVER_RESPONSE_DELAY + getCADFailMaxDuration() + 360UL
|
||||
+ response_airtime * 2UL;
|
||||
}
|
||||
|
||||
void MyMesh::resetNeighborDiscoverJsonBudget() {
|
||||
getLocalScopes(self_scopes_buf, sizeof(self_scopes_buf));
|
||||
{
|
||||
// No default region means this node floods unscoped, i.e. the wildcard.
|
||||
RegionEntry* def = region_map.getDefaultRegion();
|
||||
const char* def_name = (def && def->name[0]) ? def->name : "*";
|
||||
if (*def_name == '#') def_name++; // match how self.scopes renders names
|
||||
strncpy(self_default_scope_buf, def_name, sizeof(self_default_scope_buf) - 1);
|
||||
self_default_scope_buf[sizeof(self_default_scope_buf) - 1] = 0;
|
||||
}
|
||||
MQTTBridge::getEffectiveMqttOrigin(
|
||||
_prefs.node_name, _cli.getObserverPrefs(),
|
||||
neighbor_discover_origin, sizeof(neighbor_discover_origin));
|
||||
|
||||
char self_pubkey_hex[65];
|
||||
mesh::Utils::toHex(self_pubkey_hex, self_id.pub_key, PUB_KEY_SIZE);
|
||||
char timestamp[40];
|
||||
MQTTMessageBuilder::formatIsoTimestampForMqtt(
|
||||
getRTCClock()->getCurrentTime(), 0, nullptr, timestamp, sizeof(timestamp));
|
||||
|
||||
neighbor_discover_publish_count = 0;
|
||||
neighbor_discover_queried_count = 0;
|
||||
neighbor_discover_truncated = false;
|
||||
neighbor_discover_json_size = MQTTMessageBuilder::measureNeighborsMessageBase(
|
||||
neighbor_discover_origin, self_pubkey_hex, timestamp, self_scopes_buf,
|
||||
self_default_scope_buf, neighbor_discover_count);
|
||||
}
|
||||
|
||||
// Account for one terminal result. The base measurement reserves maximum-width
|
||||
// progress metadata; UINT32_MAX likewise reserves the widest heard-age value.
|
||||
// If this result cannot fit, stop before transmitting another scope request.
|
||||
bool MyMesh::completeNeighborDiscoverEntry() {
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_next];
|
||||
char pubkey_hex[65];
|
||||
mesh::Utils::toHex(pubkey_hex, entry.id.pub_key, PUB_KEY_SIZE);
|
||||
|
||||
MQTTMessageBuilder::NeighborsMessageEntry measured = {
|
||||
pubkey_hex,
|
||||
entry.snr / 4.0f,
|
||||
UINT32_MAX,
|
||||
entry.scopes,
|
||||
entry.status == ND_RESPONDED ? "responded"
|
||||
: (entry.status == ND_SEND_FAILED ? "send_failed" : "timeout")
|
||||
};
|
||||
size_t added = MQTTMessageBuilder::measureNeighborsMessageEntry(measured);
|
||||
if (neighbor_discover_publish_count > 0) added++; // array comma
|
||||
|
||||
if (neighbor_discover_json_size + added >= MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE ||
|
||||
neighbor_discover_publish_count >= MQTTBridge::NEIGHBORS_MAX_PUBLISH_ENTRIES) {
|
||||
neighbor_discover_truncated = true;
|
||||
finishNeighborDiscover();
|
||||
return false;
|
||||
}
|
||||
|
||||
neighbor_discover_json_size += added;
|
||||
neighbor_discover_publish_count++;
|
||||
neighbor_discover_next++;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -11218,15 +11347,46 @@ bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data
|
||||
memcpy(entry.scopes, &data[8], scope_len);
|
||||
entry.scopes[scope_len] = 0;
|
||||
entry.status = ND_RESPONDED;
|
||||
// A zero-hop reply is proof we heard this neighbour now, so re-stamp both the
|
||||
// snapshot and the live table; a stamp taken before time sync heals here.
|
||||
entry.heard_timestamp = getRTCClock()->getCurrentTime();
|
||||
touchNeighbourHeard(entry.id, entry.heard_timestamp);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Publish-ordering: most recently heard first, then stronger SNR, then pubkey.
|
||||
// The JSON builder drops the tail if the buffer fills, so the head must be the
|
||||
// most useful entries.
|
||||
// Refresh a live neighbour's heard time only: a scope reply carries no advert
|
||||
// timestamp or SNR to update.
|
||||
void MyMesh::touchNeighbourHeard(const mesh::Identity& id, uint32_t heard_timestamp) {
|
||||
#if MAX_NEIGHBOURS
|
||||
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
|
||||
if (id.matches(neighbours[i].id)) {
|
||||
neighbours[i].heard_timestamp = heard_timestamp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// A heard age is a wall-clock delta, so it only means something when both stamps
|
||||
// share a clock epoch. An entry heard before the clock was set holds the unset
|
||||
// default, which a synced clock turns into a ~2-year age; report those as
|
||||
// unknown instead. See UPSTREAM_BUGS.md for the monotonic fix.
|
||||
static bool neighborHeardAgeUsable(uint32_t heard_timestamp, uint32_t now_secs) {
|
||||
if (heard_timestamp == 0 || now_secs < heard_timestamp) return false;
|
||||
// Never synced: the stamp shares this clock's boot epoch, so the delta holds.
|
||||
if (now_secs < MQTTConnectionPolicy::kSyncedClockEpoch) return true;
|
||||
return heard_timestamp >= MQTTConnectionPolicy::kSyncedClockEpoch;
|
||||
}
|
||||
|
||||
// Publish-ordering: usable ages first, then most recently heard, then stronger
|
||||
// SNR, then pubkey. The JSON builder drops the tail if the buffer fills, so the
|
||||
// head must be the most useful entries.
|
||||
static bool neighborPublishEntryComesBefore(
|
||||
const MQTTMessageBuilder::NeighborsMessageEntry& lhs,
|
||||
const MQTTMessageBuilder::NeighborsMessageEntry& rhs) {
|
||||
if (lhs.heard_unknown != rhs.heard_unknown) {
|
||||
return !lhs.heard_unknown;
|
||||
}
|
||||
if (lhs.heard_secs_ago != rhs.heard_secs_ago) {
|
||||
return lhs.heard_secs_ago < rhs.heard_secs_ago; // newer first
|
||||
}
|
||||
@@ -11237,176 +11397,219 @@ static bool neighborPublishEntryComesBefore(
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
// Neighbors allocations prefer PSRAM where it exists and otherwise come from
|
||||
// internal DRAM, so MQTT_NEIGHBORS_WITHOUT_PSRAM boards can build the table too.
|
||||
#if defined(BOARD_HAS_PSRAM)
|
||||
static const uint32_t kNeighborsAllocCaps = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
|
||||
#else
|
||||
static const uint32_t kNeighborsAllocCaps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
|
||||
#endif
|
||||
|
||||
static void* neighborsAlloc(size_t size) {
|
||||
if (size == 0) return nullptr;
|
||||
void* p = heap_caps_malloc(size, kNeighborsAllocCaps);
|
||||
#if defined(BOARD_HAS_PSRAM)
|
||||
if (!p) p = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
#endif
|
||||
return p;
|
||||
}
|
||||
|
||||
static void neighborsFree(void* ptr) {
|
||||
if (ptr) heap_caps_free(ptr);
|
||||
}
|
||||
|
||||
// ArduinoJson v7 JsonDocument has no real capacity cap (DynamicJsonDocument(N)
|
||||
// is a no-op shim). Keep the pool off internal DRAM and soft-cap peak growth to
|
||||
// the publish buffer size. used only rises on allocate -- conservative for this
|
||||
// single-shot doc (overflow path removes+breaks, so no further growth after free).
|
||||
// is a no-op shim), so soft-cap peak pool growth to NEIGHBORS_DOC_POOL_BUDGET.
|
||||
// used only rises on allocate - conservative for this single-shot doc (overflow
|
||||
// path removes+breaks, so no further growth after free).
|
||||
struct NeighborsDocAllocator : ArduinoJson::Allocator {
|
||||
size_t used = 0;
|
||||
static const size_t kBudget = MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE;
|
||||
static const size_t kBudget = MQTTBridge::NEIGHBORS_DOC_POOL_BUDGET;
|
||||
|
||||
void* allocate(size_t size) override {
|
||||
if (used >= kBudget || size > kBudget - used) return nullptr;
|
||||
void* p = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
void* p = neighborsAlloc(size);
|
||||
if (p) used += size;
|
||||
return p;
|
||||
}
|
||||
|
||||
void deallocate(void* ptr) override {
|
||||
heap_caps_free(ptr);
|
||||
neighborsFree(ptr);
|
||||
}
|
||||
|
||||
void* reallocate(void* ptr, size_t new_size) override {
|
||||
size_t old_size = ptr ? heap_caps_get_allocated_size(ptr) : 0;
|
||||
size_t next_used = (used >= old_size) ? (used - old_size) : 0;
|
||||
if (next_used >= kBudget || new_size > kBudget - next_used) return nullptr;
|
||||
void* p = heap_caps_realloc(ptr, new_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
void* p = heap_caps_realloc(ptr, new_size, kNeighborsAllocCaps);
|
||||
#if defined(BOARD_HAS_PSRAM)
|
||||
if (!p) p = heap_caps_realloc(ptr, new_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
#endif
|
||||
if (p) used = next_used + new_size;
|
||||
return p;
|
||||
}
|
||||
};
|
||||
#else
|
||||
static void* neighborsAlloc(size_t size) { return size ? malloc(size) : nullptr; }
|
||||
static void neighborsFree(void* ptr) { free(ptr); }
|
||||
#endif
|
||||
|
||||
// Build the neighbors-table JSON and hand it to the bridge, then reschedule.
|
||||
void MyMesh::finishNeighborDiscover() {
|
||||
getLocalScopes(self_scopes_buf, sizeof(self_scopes_buf));
|
||||
|
||||
char self_pubkey_hex[65];
|
||||
mesh::Utils::toHex(self_pubkey_hex, self_id.pub_key, PUB_KEY_SIZE);
|
||||
|
||||
char origin[32];
|
||||
MQTTBridge::getEffectiveMqttOrigin(_prefs.node_name, _cli.getObserverPrefs(), origin, sizeof(origin));
|
||||
|
||||
char timestamp[40];
|
||||
MQTTMessageBuilder::formatIsoTimestampForMqtt(getRTCClock()->getCurrentTime(), 0, nullptr, timestamp, sizeof(timestamp));
|
||||
|
||||
struct NeighborPublishWorkspace {
|
||||
char pubkey_hex[MAX_NEIGHBOURS][65];
|
||||
MQTTMessageBuilder::NeighborsMessageEntry entries[MAX_NEIGHBOURS];
|
||||
};
|
||||
#if defined(ESP_PLATFORM)
|
||||
auto* publish_workspace = static_cast<NeighborPublishWorkspace*>(
|
||||
heap_caps_malloc(sizeof(NeighborPublishWorkspace), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
||||
#else
|
||||
auto* publish_workspace = static_cast<NeighborPublishWorkspace*>(
|
||||
malloc(sizeof(NeighborPublishWorkspace)));
|
||||
#endif
|
||||
if (!publish_workspace) {
|
||||
neighbor_discover_active = false;
|
||||
neighbor_discover_count = 0;
|
||||
if (_cli.getObserverPrefs()->mqtt_neighbors_enabled) {
|
||||
next_neighbors_publish = futureMillis(_cli.getObserverPrefs()->mqtt_neighbors_interval);
|
||||
}
|
||||
return;
|
||||
}
|
||||
auto& pubkey_hex = publish_workspace->pubkey_hex;
|
||||
auto& entries = publish_workspace->entries;
|
||||
uint32_t now_secs = getRTCClock()->getCurrentTime();
|
||||
// The entry table plus one hex string each reaches ~4.5 KB at MAX_NEIGHBOURS,
|
||||
// which does not fit the mesh loop task's 8 KB stack, so both share a single
|
||||
// heap block sized to this pass. Publishing is skipped if either alloc fails.
|
||||
const int publish_count = neighbor_discover_publish_count;
|
||||
const size_t hex_size = PUB_KEY_SIZE * 2 + 1;
|
||||
const size_t entries_bytes =
|
||||
sizeof(MQTTMessageBuilder::NeighborsMessageEntry) * publish_count;
|
||||
void* scratch = neighborsAlloc(entries_bytes + hex_size * publish_count);
|
||||
char* json_buf = (char*)neighborsAlloc(MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE);
|
||||
|
||||
for (int i = 0; i < neighbor_discover_count; i++) {
|
||||
auto& nb = neighbours[neighbor_discover[i].neighbour_idx];
|
||||
mesh::Utils::toHex(pubkey_hex[i], nb.id.pub_key, PUB_KEY_SIZE);
|
||||
entries[i].pubkey_hex = pubkey_hex[i];
|
||||
entries[i].snr = nb.snr / 4.0f;
|
||||
entries[i].heard_secs_ago = (nb.heard_timestamp > 0 && now_secs >= nb.heard_timestamp)
|
||||
? (now_secs - nb.heard_timestamp) : 0;
|
||||
entries[i].scopes = neighbor_discover[i].scopes;
|
||||
switch (neighbor_discover[i].status) {
|
||||
case ND_RESPONDED: entries[i].status = "responded"; break;
|
||||
case ND_SEND_FAILED: entries[i].status = "send_failed"; break;
|
||||
default: entries[i].status = "timeout"; break;
|
||||
if (json_buf && (scratch || publish_count == 0)) {
|
||||
auto* entries = (MQTTMessageBuilder::NeighborsMessageEntry*)scratch;
|
||||
char* pubkey_hex = (char*)scratch + entries_bytes;
|
||||
uint32_t now_secs = getRTCClock()->getCurrentTime();
|
||||
|
||||
for (int i = 0; i < publish_count; i++) {
|
||||
auto& entry = neighbor_discover[i];
|
||||
char* hex = &pubkey_hex[i * hex_size];
|
||||
mesh::Utils::toHex(hex, entry.id.pub_key, PUB_KEY_SIZE);
|
||||
entries[i].pubkey_hex = hex;
|
||||
entries[i].snr = entry.snr / 4.0f;
|
||||
bool heard_known = neighborHeardAgeUsable(entry.heard_timestamp, now_secs);
|
||||
entries[i].heard_unknown = !heard_known;
|
||||
entries[i].heard_secs_ago = heard_known ? (now_secs - entry.heard_timestamp) : 0;
|
||||
entries[i].scopes = entry.scopes;
|
||||
switch (entry.status) {
|
||||
case ND_RESPONDED: entries[i].status = "responded"; break;
|
||||
case ND_SEND_FAILED: entries[i].status = "send_failed"; break;
|
||||
default: entries[i].status = "timeout"; break;
|
||||
}
|
||||
}
|
||||
|
||||
// insertion sort: most useful first (JSON builder drops the tail on overflow)
|
||||
for (int i = 1; i < publish_count; i++) {
|
||||
MQTTMessageBuilder::NeighborsMessageEntry entry = entries[i];
|
||||
int j = i;
|
||||
while (j > 0 && neighborPublishEntryComesBefore(entry, entries[j - 1])) {
|
||||
entries[j] = entries[j - 1];
|
||||
j--;
|
||||
}
|
||||
entries[j] = entry;
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
NeighborsDocAllocator doc_alloc;
|
||||
JsonDocument doc(&doc_alloc);
|
||||
#else
|
||||
JsonDocument doc;
|
||||
#endif
|
||||
int json_len = MQTTMessageBuilder::buildNeighborsMessage(
|
||||
doc, neighbor_discover_origin, self_pubkey_hex, timestamp, self_scopes_buf,
|
||||
self_default_scope_buf, entries, publish_count,
|
||||
json_buf, MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE,
|
||||
neighbor_discover_count, neighbor_discover_queried_count,
|
||||
neighbor_discover_truncated);
|
||||
|
||||
if (json_len > 0 && mqtt_bridge) {
|
||||
mqtt_bridge->requestPublishNeighbors(json_buf, (size_t)json_len);
|
||||
}
|
||||
}
|
||||
|
||||
// insertion sort: most useful first (JSON builder drops the tail on overflow)
|
||||
for (int i = 1; i < neighbor_discover_count; i++) {
|
||||
MQTTMessageBuilder::NeighborsMessageEntry entry = entries[i];
|
||||
int j = i;
|
||||
while (j > 0 && neighborPublishEntryComesBefore(entry, entries[j - 1])) {
|
||||
entries[j] = entries[j - 1];
|
||||
j--;
|
||||
}
|
||||
entries[j] = entry;
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
char* json_buf = (char*)heap_caps_malloc(MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE, MALLOC_CAP_SPIRAM);
|
||||
#else
|
||||
char* json_buf = (char*)malloc(MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE);
|
||||
#endif
|
||||
if (!json_buf) {
|
||||
#if defined(ESP_PLATFORM)
|
||||
heap_caps_free(publish_workspace);
|
||||
#else
|
||||
free(publish_workspace);
|
||||
#endif
|
||||
neighbor_discover_active = false;
|
||||
neighbor_discover_count = 0;
|
||||
if (_cli.getObserverPrefs()->mqtt_neighbors_enabled) {
|
||||
next_neighbors_publish = futureMillis(_cli.getObserverPrefs()->mqtt_neighbors_interval);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
NeighborsDocAllocator doc_alloc;
|
||||
JsonDocument doc(&doc_alloc);
|
||||
#else
|
||||
JsonDocument doc;
|
||||
#endif
|
||||
int json_len = MQTTMessageBuilder::buildNeighborsMessage(
|
||||
doc, origin, self_pubkey_hex, timestamp, self_scopes_buf,
|
||||
entries, neighbor_discover_count,
|
||||
json_buf, MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE);
|
||||
|
||||
if (json_len > 0 && mqtt_bridge) {
|
||||
mqtt_bridge->requestPublishNeighbors(json_buf, (size_t)json_len);
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
heap_caps_free(json_buf);
|
||||
heap_caps_free(publish_workspace);
|
||||
#else
|
||||
free(json_buf);
|
||||
free(publish_workspace);
|
||||
#endif
|
||||
neighborsFree(scratch);
|
||||
neighborsFree(json_buf);
|
||||
|
||||
neighbor_discover_active = false;
|
||||
neighbor_discover_count = 0;
|
||||
neighbor_discover_next = 0;
|
||||
neighbor_discover_publish_count = 0;
|
||||
neighbor_discover_queried_count = 0;
|
||||
neighbor_discover_json_size = 0;
|
||||
neighbor_discover_truncated = false;
|
||||
neighbor_discover_until = 0;
|
||||
neighbor_discover_request = NULL;
|
||||
if (_cli.getObserverPrefs()->mqtt_neighbors_enabled) {
|
||||
next_neighbors_publish = futureMillis(_cli.getObserverPrefs()->mqtt_neighbors_interval);
|
||||
}
|
||||
}
|
||||
|
||||
// Advance the scope-query phase; publish once all entries resolve or the window
|
||||
// times out (stragglers marked ND_TIMEOUT).
|
||||
// Advance the newest-first scope-query phase. Keep only one request in flight so
|
||||
// its responder gets a clear reply opportunity and the packet pool stays free.
|
||||
void MyMesh::loopNeighborDiscover() {
|
||||
if (!neighbor_discover_active) return;
|
||||
|
||||
bool all_done = true;
|
||||
for (int i = 0; i < neighbor_discover_count; i++) {
|
||||
if (neighbor_discover[i].status == ND_PENDING) { all_done = false; break; }
|
||||
if (neighbor_discover_next >= neighbor_discover_count) {
|
||||
finishNeighborDiscover();
|
||||
return;
|
||||
}
|
||||
if (!all_done && !millisHasNowPassed(neighbor_discover_until)) return;
|
||||
if (!all_done) {
|
||||
for (int i = 0; i < neighbor_discover_count; i++) {
|
||||
if (neighbor_discover[i].status == ND_PENDING) neighbor_discover[i].status = ND_TIMEOUT;
|
||||
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_next];
|
||||
if (entry.status == ND_QUEUED) {
|
||||
if (!millisHasNowPassed(neighbor_discover_until)) return;
|
||||
if (cancelNeighborDiscoverRequest()) {
|
||||
entry.status = ND_SEND_FAILED;
|
||||
completeNeighborDiscoverEntry();
|
||||
return;
|
||||
}
|
||||
if (isCurrentOutbound(neighbor_discover_request)) {
|
||||
neighbor_discover_until = futureMillis(neighborDiscoverQueryTimeoutMs());
|
||||
return;
|
||||
}
|
||||
neighbor_discover_request = NULL; // packet manager already shed it
|
||||
entry.status = ND_SEND_FAILED;
|
||||
completeNeighborDiscoverEntry();
|
||||
return;
|
||||
}
|
||||
if (entry.status == ND_PENDING) {
|
||||
if (!millisHasNowPassed(neighbor_discover_until)) return;
|
||||
entry.status = ND_TIMEOUT;
|
||||
completeNeighborDiscoverEntry();
|
||||
return;
|
||||
}
|
||||
if (entry.status == ND_RESPONDED || entry.status == ND_SEND_FAILED
|
||||
|| entry.status == ND_TIMEOUT) {
|
||||
completeNeighborDiscoverEntry();
|
||||
return;
|
||||
}
|
||||
if (entry.status != ND_UNSENT) {
|
||||
neighbor_discover_next++;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t tag;
|
||||
mesh::Packet* request = sendAnonRegionsReq(entry.id, tag);
|
||||
if (request) {
|
||||
entry.tag = tag;
|
||||
entry.status = ND_QUEUED;
|
||||
neighbor_discover_request = request;
|
||||
neighbor_discover_until = futureMillis(NEIGHBOR_DISCOVER_QUEUE_TIMEOUT_MS);
|
||||
} else {
|
||||
entry.status = ND_SEND_FAILED;
|
||||
completeNeighborDiscoverEntry();
|
||||
}
|
||||
finishNeighborDiscover();
|
||||
}
|
||||
|
||||
// Shared precondition for starting a discovery: PSRAM present + bridge running.
|
||||
// Shared precondition for starting a discovery: usable buffers + bridge running.
|
||||
// PSRAM builds size their neighbors buffers for PSRAM, so a board whose PSRAM
|
||||
// failed to init must not silently spend that much internal DRAM here.
|
||||
// MQTT_NEIGHBORS_WITHOUT_PSRAM builds are already sized for internal DRAM.
|
||||
bool MyMesh::neighborDiscoverReady(char* reply) {
|
||||
#if defined(ESP_PLATFORM)
|
||||
#if defined(ESP_PLATFORM) && defined(BOARD_HAS_PSRAM)
|
||||
if (!psramFound()) { strcpy(reply, "Err - PSRAM not available"); return false; }
|
||||
#endif
|
||||
if (!mqtt_bridge || !mqtt_bridge->isRunning()) { strcpy(reply, "Err - MQTT bridge not running"); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
// Snapshot the neighbor table into the overlay and fire one anon-regions query
|
||||
// per heard neighbour; arm the 30s scope-query window.
|
||||
// Snapshot the neighbor table newest-first. loopNeighborDiscover() emits one
|
||||
// anon-regions query at a time so hidden responders do not reply as a burst.
|
||||
bool MyMesh::startNeighborDiscover(char* reply) {
|
||||
if (neighbor_discover_active) {
|
||||
strcpy(reply, "Err - neighbor discover already active");
|
||||
@@ -11416,30 +11619,48 @@ bool MyMesh::startNeighborDiscover(char* reply) {
|
||||
return false; // reply already set
|
||||
}
|
||||
|
||||
getLocalScopes(self_scopes_buf, sizeof(self_scopes_buf));
|
||||
neighbor_discover_count = 0;
|
||||
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
|
||||
if (neighbours[i].heard_timestamp > 0) {
|
||||
neighbor_discover[neighbor_discover_count].neighbour_idx = (uint8_t)i;
|
||||
neighbor_discover[neighbor_discover_count].scopes[0] = 0;
|
||||
neighbor_discover[neighbor_discover_count].status = ND_PENDING;
|
||||
uint32_t tag;
|
||||
if (sendAnonRegionsReq(neighbours[i].id, tag)) {
|
||||
neighbor_discover[neighbor_discover_count].tag = tag;
|
||||
} else {
|
||||
neighbor_discover[neighbor_discover_count].status = ND_SEND_FAILED;
|
||||
}
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_count];
|
||||
entry.id = neighbours[i].id;
|
||||
entry.heard_timestamp = neighbours[i].heard_timestamp;
|
||||
entry.snr = neighbours[i].snr;
|
||||
entry.scopes[0] = 0;
|
||||
entry.tag = 0;
|
||||
entry.status = ND_UNSENT;
|
||||
neighbor_discover_count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Query the freshest/strongest entries first; pubkey makes ties deterministic.
|
||||
for (int i = 1; i < neighbor_discover_count; i++) {
|
||||
NeighborDiscoverEntry entry = neighbor_discover[i];
|
||||
int j = i;
|
||||
while (j > 0) {
|
||||
auto& rhs = neighbor_discover[j - 1];
|
||||
bool before = entry.heard_timestamp > rhs.heard_timestamp
|
||||
|| (entry.heard_timestamp == rhs.heard_timestamp && entry.snr > rhs.snr)
|
||||
|| (entry.heard_timestamp == rhs.heard_timestamp && entry.snr == rhs.snr
|
||||
&& memcmp(entry.id.pub_key, rhs.id.pub_key, PUB_KEY_SIZE) < 0);
|
||||
if (!before) break;
|
||||
neighbor_discover[j] = neighbor_discover[j - 1];
|
||||
j--;
|
||||
}
|
||||
neighbor_discover[j] = entry;
|
||||
}
|
||||
|
||||
neighbor_discover_next = 0;
|
||||
resetNeighborDiscoverJsonBudget();
|
||||
neighbor_discover_active = true;
|
||||
neighbor_discover_until = futureMillis(NEIGHBOR_DISCOVER_TIMEOUT_MS);
|
||||
neighbor_discover_until = 0;
|
||||
neighbor_discover_request = NULL;
|
||||
|
||||
if (neighbor_discover_count == 0) {
|
||||
finishNeighborDiscover();
|
||||
strcpy(reply, "OK - neighbor discover started (0 neighbors, self only)");
|
||||
} else {
|
||||
loopNeighborDiscover(); // queue the first request now
|
||||
sprintf(reply, "OK - neighbor discover started (%u neighbors)", (unsigned)neighbor_discover_count);
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -499,40 +499,58 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
|
||||
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
// Neighbor-scope discovery: a snapshot of the neighbor table overlaid with an
|
||||
// in-flight anon-regions query per neighbor, published to the MQTT neighbors
|
||||
// topic once every neighbor has responded or the window times out.
|
||||
// anon-regions query per neighbor, published to the MQTT neighbors topic once
|
||||
// every neighbor has responded or timed out.
|
||||
enum NeighborDiscoverStatus : uint8_t {
|
||||
ND_PENDING = 1,
|
||||
ND_RESPONDED = 2,
|
||||
ND_TIMEOUT = 3,
|
||||
ND_SEND_FAILED = 4,
|
||||
ND_UNSENT = 0,
|
||||
ND_QUEUED = 1,
|
||||
ND_PENDING = 2,
|
||||
ND_RESPONDED = 3,
|
||||
ND_TIMEOUT = 4,
|
||||
ND_SEND_FAILED = 5,
|
||||
};
|
||||
struct NeighborDiscoverEntry {
|
||||
uint8_t neighbour_idx; // index into neighbours[]
|
||||
mesh::Identity id; // immutable snapshot: neighbour table can change mid-pass
|
||||
uint32_t heard_timestamp;
|
||||
int8_t snr; // multiplied by 4
|
||||
uint32_t tag; // anon-regions request tag we're waiting on
|
||||
char scopes[96]; // scope names from the response
|
||||
uint8_t status; // NeighborDiscoverStatus
|
||||
};
|
||||
NeighborDiscoverEntry neighbor_discover[MAX_NEIGHBOURS];
|
||||
uint8_t neighbor_discover_count;
|
||||
uint8_t neighbor_discover_next; // newest-first entry currently being queried
|
||||
uint8_t neighbor_discover_publish_count; // completed prefix that fits the JSON buffer
|
||||
uint8_t neighbor_discover_queried_count; // requests confirmed transmitted
|
||||
size_t neighbor_discover_json_size;
|
||||
bool neighbor_discover_truncated;
|
||||
bool neighbor_discover_active; // scope-query phase in flight
|
||||
bool neighbor_table_refresh_active; // zero-hop table refresh (stage 1) in flight
|
||||
bool neighbor_table_refresh_periodic; // that refresh was kicked by the periodic timer
|
||||
unsigned long neighbor_discover_until; // scope-query timeout deadline
|
||||
unsigned long neighbor_discover_until; // current queue or response deadline
|
||||
mesh::Packet* neighbor_discover_request; // request awaiting TX completion
|
||||
unsigned long next_neighbors_publish; // periodic publish deadline (0 = fire ASAP)
|
||||
char self_scopes_buf[96];
|
||||
char self_default_scope_buf[31];
|
||||
char neighbor_discover_origin[32];
|
||||
|
||||
bool sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag);
|
||||
mesh::Packet* sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag);
|
||||
bool cancelNeighborDiscoverRequest();
|
||||
uint32_t neighborDiscoverQueryTimeoutMs() const;
|
||||
bool completeNeighborDiscoverEntry();
|
||||
void resetNeighborDiscoverJsonBudget();
|
||||
bool neighborDiscoverReady(char* reply);
|
||||
bool startNeighborDiscover(char* reply);
|
||||
void loopNeighborDiscover();
|
||||
void finishNeighborDiscover();
|
||||
bool handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data, size_t len);
|
||||
void touchNeighbourHeard(const mesh::Identity& id, uint32_t heard_timestamp);
|
||||
void getLocalScopes(char* buf, size_t len);
|
||||
// Overlay peer indices are offset by this base so onPeerDataRecv can tell a
|
||||
// discovery response apart from a normal ACL-client index.
|
||||
static const int NEIGHBOR_DISCOVER_PEER_BASE = 1000;
|
||||
static const unsigned long NEIGHBOR_DISCOVER_TIMEOUT_MS = 30000;
|
||||
static const unsigned long NEIGHBOR_DISCOVER_QUEUE_TIMEOUT_MS = 29000;
|
||||
static const int NEIGHBOR_DISCOVER_MIN_FREE_PACKETS = 5;
|
||||
#endif
|
||||
|
||||
bool extractDirectRetryPrefix(const mesh::Packet* packet, uint8_t* prefix, uint8_t& prefix_len) const;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#ifdef DISPLAY_CLASS
|
||||
#include "UITask.h"
|
||||
static UITask ui_task(board, display);
|
||||
static bool display_ready = false;
|
||||
#endif
|
||||
|
||||
#ifdef ETHERNET_ENABLED
|
||||
@@ -76,7 +77,8 @@ void setup() {
|
||||
#endif
|
||||
|
||||
#ifdef DISPLAY_CLASS
|
||||
if (display.begin()) {
|
||||
display_ready = display.begin();
|
||||
if (display_ready) {
|
||||
display.startFrame();
|
||||
display.setCursor(0, 0);
|
||||
display.print("Please wait...");
|
||||
@@ -157,7 +159,9 @@ void setup() {
|
||||
the_mesh.begin(fs);
|
||||
|
||||
#ifdef DISPLAY_CLASS
|
||||
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
|
||||
if (display_ready) {
|
||||
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ETHERNET_ENABLED
|
||||
@@ -245,7 +249,7 @@ void loop() {
|
||||
the_mesh.loop();
|
||||
sensors.loop();
|
||||
#ifdef DISPLAY_CLASS
|
||||
ui_task.loop();
|
||||
if (display_ready) ui_task.loop();
|
||||
#endif
|
||||
rtc_clock.tick();
|
||||
|
||||
|
||||
@@ -17,6 +17,9 @@ static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint32_t delay_ms = 1000UL << shift;
|
||||
return delay_ms > 30000UL ? 30000UL : delay_ms;
|
||||
}
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
#include <helpers/MQTTConnectionPolicy.h> // kSyncedClockEpoch
|
||||
#endif
|
||||
|
||||
#define ANON_REQ_TYPE_REGIONS 0x01 // client side of the anon-regions scope query (neighbors feature)
|
||||
|
||||
@@ -301,6 +304,19 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
|
||||
}
|
||||
}
|
||||
void MyMesh::logTx(mesh::Packet *pkt, int len) {
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
if (neighbor_discover_active && pkt == neighbor_discover_request
|
||||
&& neighbor_discover_next < neighbor_discover_count) {
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_next];
|
||||
if (entry.status == ND_QUEUED) {
|
||||
entry.status = ND_PENDING;
|
||||
neighbor_discover_queried_count++;
|
||||
neighbor_discover_request = NULL;
|
||||
neighbor_discover_until = futureMillis(neighborDiscoverQueryTimeoutMs());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// MQTT bridge: always feed TX packets - bridge decides based on mqtt.tx setting
|
||||
if (_prefs.bridge_enabled && bridge) bridge->sendPacket(pkt);
|
||||
@@ -324,6 +340,18 @@ void MyMesh::logTx(mesh::Packet *pkt, int len) {
|
||||
}
|
||||
}
|
||||
void MyMesh::logTxFail(mesh::Packet *pkt, int len) {
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
if (neighbor_discover_active && pkt == neighbor_discover_request
|
||||
&& neighbor_discover_next < neighbor_discover_count) {
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_next];
|
||||
if (entry.status == ND_QUEUED) {
|
||||
entry.status = ND_SEND_FAILED;
|
||||
neighbor_discover_request = NULL;
|
||||
neighbor_discover_until = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (_logging) {
|
||||
File f = openAppend(PACKET_LOG_FILE);
|
||||
if (f) {
|
||||
@@ -681,12 +709,12 @@ int MyMesh::searchPeersByHash(const uint8_t *hash) {
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
if (neighbor_discover_active) {
|
||||
for (int i = 0; i < neighbor_discover_count && n < MAX_CLIENTS; i++) {
|
||||
auto& nb = neighbours[neighbor_discover[i].neighbour_idx];
|
||||
auto& entry = neighbor_discover[i];
|
||||
// ACL clients already have a matching peer entry and shared secret. Adding
|
||||
// a second overlay entry would decrypt first and intercept their normal
|
||||
// CLI/request traffic for the duration of discovery.
|
||||
if (acl.getClient(nb.id.pub_key, PUB_KEY_SIZE) != nullptr) continue;
|
||||
if (nb.heard_timestamp > 0 && nb.id.isHashMatch(hash)) {
|
||||
if (acl.getClient(entry.id.pub_key, PUB_KEY_SIZE) != nullptr) continue;
|
||||
if (entry.heard_timestamp > 0 && entry.id.isHashMatch(hash)) {
|
||||
matching_peer_indexes[n++] = NEIGHBOR_DISCOVER_PEER_BASE + i;
|
||||
}
|
||||
}
|
||||
@@ -706,7 +734,7 @@ void MyMesh::getPeerSharedSecret(uint8_t *dest_secret, int peer_idx) {
|
||||
if (neighbor_discover_active && i >= NEIGHBOR_DISCOVER_PEER_BASE) {
|
||||
int oi = i - NEIGHBOR_DISCOVER_PEER_BASE;
|
||||
if (oi >= 0 && oi < neighbor_discover_count) {
|
||||
self_id.calcSharedSecret(dest_secret, neighbours[neighbor_discover[oi].neighbour_idx].id);
|
||||
self_id.calcSharedSecret(dest_secret, neighbor_discover[oi].id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -743,8 +771,8 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
||||
// scope-query response from it lands here -- match it against the overlay.
|
||||
if (neighbor_discover_active && type == PAYLOAD_TYPE_RESPONSE) {
|
||||
for (int oi = 0; oi < neighbor_discover_count; oi++) {
|
||||
auto& nb = neighbours[neighbor_discover[oi].neighbour_idx];
|
||||
if (client->id.matches(nb.id) && handleNeighborDiscoverResponse(oi, data, len)) {
|
||||
if (client->id.matches(neighbor_discover[oi].id)
|
||||
&& handleNeighborDiscoverResponse(oi, data, len)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1130,7 +1158,6 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
#endif
|
||||
|
||||
// defaults
|
||||
memset(&_prefs, 0, sizeof(_prefs));
|
||||
_prefs.airtime_factor = 1.0; // one half
|
||||
_prefs.rx_delay_base = 0.0f; // off by default, was 10.0
|
||||
_prefs.tx_delay_factor = 0.5f; // was 0.25f;
|
||||
@@ -1195,12 +1222,20 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
pending_discover_tag = 0;
|
||||
pending_discover_until = 0;
|
||||
neighbor_discover_count = 0;
|
||||
neighbor_discover_next = 0;
|
||||
neighbor_discover_publish_count = 0;
|
||||
neighbor_discover_queried_count = 0;
|
||||
neighbor_discover_json_size = 0;
|
||||
neighbor_discover_truncated = false;
|
||||
neighbor_discover_active = false;
|
||||
neighbor_table_refresh_active = false;
|
||||
neighbor_table_refresh_periodic = false;
|
||||
neighbor_discover_until = 0;
|
||||
neighbor_discover_request = NULL;
|
||||
next_neighbors_publish = 0;
|
||||
self_scopes_buf[0] = 0;
|
||||
self_default_scope_buf[0] = 0;
|
||||
neighbor_discover_origin[0] = 0;
|
||||
memset(neighbours, 0, sizeof(neighbours));
|
||||
#endif
|
||||
|
||||
@@ -1563,7 +1598,7 @@ bool MyMesh::startWebConfig(bool force_ap, char* reply) {
|
||||
owns_wifi = false;
|
||||
#endif
|
||||
_webconfig = new WebConfigServer(this, mqtt_prefs, owns_wifi,
|
||||
self_id.pub_key, getFirmwareVer(), getRole(),
|
||||
self_id.pub_key, getFirmwareVer(), getBuildDate(), getRole(),
|
||||
_cli.getBoard()->getManufacturerName());
|
||||
if (!_webconfig) {
|
||||
strcpy(reply, "Err: not enough memory for webconfig");
|
||||
@@ -2038,7 +2073,7 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
|
||||
}
|
||||
#elif defined(WITH_MQTT_BRIDGE)
|
||||
} else if (memcmp(command, "discover.scopes", 15) == 0) {
|
||||
strcpy(reply, "Err - not supported (requires PSRAM)");
|
||||
strcpy(reply, "Err - neighbors not enabled in this build");
|
||||
#endif
|
||||
} else if (strncmp(command, "room.post", 9) == 0) {
|
||||
char* msg = command + 9;
|
||||
@@ -2430,7 +2465,11 @@ void MyMesh::getLocalScopes(char* buf, size_t len) {
|
||||
|
||||
// Client side of the anon-regions request (the server side is handleAnonRegionsReq).
|
||||
// Inner payload: {tag(4)}{ANON_REQ_TYPE_REGIONS}{0x00 = zero-hop reply path}.
|
||||
bool MyMesh::sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag) {
|
||||
mesh::Packet* MyMesh::sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag) {
|
||||
// RxReservePacketManager keeps a four-packet emergency floor. Preflight one
|
||||
// extra free packet so its void queue API cannot silently shed this request.
|
||||
if (_mgr->getFreeCount() < NEIGHBOR_DISCOVER_MIN_FREE_PACKETS) return NULL;
|
||||
|
||||
uint8_t secret[PUB_KEY_SIZE];
|
||||
self_id.calcSharedSecret(secret, target);
|
||||
|
||||
@@ -2441,8 +2480,92 @@ bool MyMesh::sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag) {
|
||||
inner[5] = 0x00; // request a zero-hop reply path
|
||||
|
||||
mesh::Packet* pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, target, secret, inner, sizeof(inner));
|
||||
if (!pkt) return false;
|
||||
if (!pkt) return NULL;
|
||||
sendDirect(pkt, NULL, 0, 0);
|
||||
return pkt;
|
||||
}
|
||||
|
||||
bool MyMesh::cancelNeighborDiscoverRequest() {
|
||||
if (!neighbor_discover_request) return false;
|
||||
for (int i = _mgr->getOutboundTotal() - 1; i >= 0; i--) {
|
||||
if (_mgr->getOutboundByIdx(i) == neighbor_discover_request) {
|
||||
mesh::Packet* pkt = _mgr->removeOutboundByIdx(i);
|
||||
if (pkt) releasePacket(pkt);
|
||||
neighbor_discover_request = NULL;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// This timer starts after the request finishes transmitting. Allow the server
|
||||
// delay, the responder's full CAD deferral window plus one maximum retry
|
||||
// overshoot, and airtime for one priority-0 packet ahead of the response plus
|
||||
// the response itself. The radio estimate scales with SF, bandwidth, coding
|
||||
// rate, and preamble.
|
||||
uint32_t MyMesh::neighborDiscoverQueryTimeoutMs() const {
|
||||
uint32_t response_airtime = _radio->getEstAirtimeFor(MAX_PACKET_PAYLOAD + 2);
|
||||
return SERVER_RESPONSE_DELAY + getCADFailMaxDuration() + 360UL
|
||||
+ response_airtime * 2UL;
|
||||
}
|
||||
|
||||
void MyMesh::resetNeighborDiscoverJsonBudget() {
|
||||
getLocalScopes(self_scopes_buf, sizeof(self_scopes_buf));
|
||||
{
|
||||
// No default region means this node floods unscoped, i.e. the wildcard.
|
||||
RegionEntry* def = region_map.getDefaultRegion();
|
||||
const char* def_name = (def && def->name[0]) ? def->name : "*";
|
||||
if (*def_name == '#') def_name++; // match how self.scopes renders names
|
||||
strncpy(self_default_scope_buf, def_name, sizeof(self_default_scope_buf) - 1);
|
||||
self_default_scope_buf[sizeof(self_default_scope_buf) - 1] = 0;
|
||||
}
|
||||
MQTTBridge::getEffectiveMqttOrigin(
|
||||
_prefs.node_name, _cli.getObserverPrefs(),
|
||||
neighbor_discover_origin, sizeof(neighbor_discover_origin));
|
||||
|
||||
char self_pubkey_hex[65];
|
||||
mesh::Utils::toHex(self_pubkey_hex, self_id.pub_key, PUB_KEY_SIZE);
|
||||
char timestamp[40];
|
||||
MQTTMessageBuilder::formatIsoTimestampForMqtt(
|
||||
getRTCClock()->getCurrentTime(), 0, nullptr, timestamp, sizeof(timestamp));
|
||||
|
||||
neighbor_discover_publish_count = 0;
|
||||
neighbor_discover_queried_count = 0;
|
||||
neighbor_discover_truncated = false;
|
||||
neighbor_discover_json_size = MQTTMessageBuilder::measureNeighborsMessageBase(
|
||||
neighbor_discover_origin, self_pubkey_hex, timestamp, self_scopes_buf,
|
||||
self_default_scope_buf, neighbor_discover_count);
|
||||
}
|
||||
|
||||
// Account for one terminal result. The base measurement reserves maximum-width
|
||||
// progress metadata; UINT32_MAX likewise reserves the widest heard-age value.
|
||||
// If this result cannot fit, stop before transmitting another scope request.
|
||||
bool MyMesh::completeNeighborDiscoverEntry() {
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_next];
|
||||
char pubkey_hex[65];
|
||||
mesh::Utils::toHex(pubkey_hex, entry.id.pub_key, PUB_KEY_SIZE);
|
||||
|
||||
MQTTMessageBuilder::NeighborsMessageEntry measured = {
|
||||
pubkey_hex,
|
||||
entry.snr / 4.0f,
|
||||
UINT32_MAX,
|
||||
entry.scopes,
|
||||
entry.status == ND_RESPONDED ? "responded"
|
||||
: (entry.status == ND_SEND_FAILED ? "send_failed" : "timeout")
|
||||
};
|
||||
size_t added = MQTTMessageBuilder::measureNeighborsMessageEntry(measured);
|
||||
if (neighbor_discover_publish_count > 0) added++; // array comma
|
||||
|
||||
if (neighbor_discover_json_size + added >= MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE ||
|
||||
neighbor_discover_publish_count >= MQTTBridge::NEIGHBORS_MAX_PUBLISH_ENTRIES) {
|
||||
neighbor_discover_truncated = true;
|
||||
finishNeighborDiscover();
|
||||
return false;
|
||||
}
|
||||
|
||||
neighbor_discover_json_size += added;
|
||||
neighbor_discover_publish_count++;
|
||||
neighbor_discover_next++;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2464,15 +2587,44 @@ bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data
|
||||
memcpy(entry.scopes, &data[8], scope_len);
|
||||
entry.scopes[scope_len] = 0;
|
||||
entry.status = ND_RESPONDED;
|
||||
// A zero-hop reply is proof we heard this neighbour now, so re-stamp both the
|
||||
// snapshot and the live table; a stamp taken before time sync heals here.
|
||||
entry.heard_timestamp = getRTCClock()->getCurrentTime();
|
||||
touchNeighbourHeard(entry.id, entry.heard_timestamp);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Publish-ordering: most recently heard first, then stronger SNR, then pubkey.
|
||||
// The JSON builder drops the tail if the buffer fills, so the head must be the
|
||||
// most useful entries.
|
||||
// Refresh a live neighbour's heard time only: a scope reply carries no advert
|
||||
// timestamp or SNR to update.
|
||||
void MyMesh::touchNeighbourHeard(const mesh::Identity& id, uint32_t heard_timestamp) {
|
||||
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
|
||||
if (id.matches(neighbours[i].id)) {
|
||||
neighbours[i].heard_timestamp = heard_timestamp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A heard age is a wall-clock delta, so it only means something when both stamps
|
||||
// share a clock epoch. An entry heard before the clock was set holds the unset
|
||||
// default, which a synced clock turns into a ~2-year age; report those as
|
||||
// unknown instead. See UPSTREAM_BUGS.md for the monotonic fix.
|
||||
static bool neighborHeardAgeUsable(uint32_t heard_timestamp, uint32_t now_secs) {
|
||||
if (heard_timestamp == 0 || now_secs < heard_timestamp) return false;
|
||||
// Never synced: the stamp shares this clock's boot epoch, so the delta holds.
|
||||
if (now_secs < MQTTConnectionPolicy::kSyncedClockEpoch) return true;
|
||||
return heard_timestamp >= MQTTConnectionPolicy::kSyncedClockEpoch;
|
||||
}
|
||||
|
||||
// Publish-ordering: usable ages first, then most recently heard, then stronger
|
||||
// SNR, then pubkey. The JSON builder drops the tail if the buffer fills, so the
|
||||
// head must be the most useful entries.
|
||||
static bool neighborPublishEntryComesBefore(
|
||||
const MQTTMessageBuilder::NeighborsMessageEntry& lhs,
|
||||
const MQTTMessageBuilder::NeighborsMessageEntry& rhs) {
|
||||
if (lhs.heard_unknown != rhs.heard_unknown) {
|
||||
return !lhs.heard_unknown;
|
||||
}
|
||||
if (lhs.heard_secs_ago != rhs.heard_secs_ago) {
|
||||
return lhs.heard_secs_ago < rhs.heard_secs_ago; // newer first
|
||||
}
|
||||
@@ -2483,176 +2635,219 @@ static bool neighborPublishEntryComesBefore(
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
// Neighbors allocations prefer PSRAM where it exists and otherwise come from
|
||||
// internal DRAM, so MQTT_NEIGHBORS_WITHOUT_PSRAM boards can build the table too.
|
||||
#if defined(BOARD_HAS_PSRAM)
|
||||
static const uint32_t kNeighborsAllocCaps = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
|
||||
#else
|
||||
static const uint32_t kNeighborsAllocCaps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
|
||||
#endif
|
||||
|
||||
static void* neighborsAlloc(size_t size) {
|
||||
if (size == 0) return nullptr;
|
||||
void* p = heap_caps_malloc(size, kNeighborsAllocCaps);
|
||||
#if defined(BOARD_HAS_PSRAM)
|
||||
if (!p) p = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
#endif
|
||||
return p;
|
||||
}
|
||||
|
||||
static void neighborsFree(void* ptr) {
|
||||
if (ptr) heap_caps_free(ptr);
|
||||
}
|
||||
|
||||
// ArduinoJson v7 JsonDocument has no real capacity cap (DynamicJsonDocument(N)
|
||||
// is a no-op shim). Keep the pool off internal DRAM and soft-cap peak growth to
|
||||
// the publish buffer size. used only rises on allocate -- conservative for this
|
||||
// single-shot doc (overflow path removes+breaks, so no further growth after free).
|
||||
struct NeighborsDocAllocator : ArduinoJson::Allocator {
|
||||
size_t used = 0;
|
||||
static const size_t kBudget = MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE;
|
||||
static const size_t kBudget = MQTTBridge::NEIGHBORS_DOC_POOL_BUDGET;
|
||||
|
||||
void* allocate(size_t size) override {
|
||||
if (used >= kBudget || size > kBudget - used) return nullptr;
|
||||
void* p = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
void* p = neighborsAlloc(size);
|
||||
if (p) used += size;
|
||||
return p;
|
||||
}
|
||||
|
||||
void deallocate(void* ptr) override {
|
||||
heap_caps_free(ptr);
|
||||
neighborsFree(ptr);
|
||||
}
|
||||
|
||||
void* reallocate(void* ptr, size_t new_size) override {
|
||||
size_t old_size = ptr ? heap_caps_get_allocated_size(ptr) : 0;
|
||||
size_t next_used = (used >= old_size) ? (used - old_size) : 0;
|
||||
if (next_used >= kBudget || new_size > kBudget - next_used) return nullptr;
|
||||
void* p = heap_caps_realloc(ptr, new_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
void* p = heap_caps_realloc(ptr, new_size, kNeighborsAllocCaps);
|
||||
#if defined(BOARD_HAS_PSRAM)
|
||||
if (!p) p = heap_caps_realloc(ptr, new_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
#endif
|
||||
if (p) used = next_used + new_size;
|
||||
return p;
|
||||
}
|
||||
};
|
||||
#else
|
||||
static void* neighborsAlloc(size_t size) { return size ? malloc(size) : nullptr; }
|
||||
static void neighborsFree(void* ptr) { free(ptr); }
|
||||
#endif
|
||||
|
||||
// Build the neighbors-table JSON and hand it to the bridge, then reschedule.
|
||||
void MyMesh::finishNeighborDiscover() {
|
||||
getLocalScopes(self_scopes_buf, sizeof(self_scopes_buf));
|
||||
|
||||
char self_pubkey_hex[65];
|
||||
mesh::Utils::toHex(self_pubkey_hex, self_id.pub_key, PUB_KEY_SIZE);
|
||||
|
||||
char origin[32];
|
||||
MQTTBridge::getEffectiveMqttOrigin(_prefs.node_name, _cli.getObserverPrefs(), origin, sizeof(origin));
|
||||
|
||||
char timestamp[40];
|
||||
MQTTMessageBuilder::formatIsoTimestampForMqtt(getRTCClock()->getCurrentTime(), 0, nullptr, timestamp, sizeof(timestamp));
|
||||
|
||||
struct NeighborPublishWorkspace {
|
||||
char pubkey_hex[MAX_NEIGHBOURS][65];
|
||||
MQTTMessageBuilder::NeighborsMessageEntry entries[MAX_NEIGHBOURS];
|
||||
};
|
||||
#if defined(ESP_PLATFORM)
|
||||
auto* publish_workspace = static_cast<NeighborPublishWorkspace*>(
|
||||
heap_caps_malloc(sizeof(NeighborPublishWorkspace), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
||||
#else
|
||||
auto* publish_workspace = static_cast<NeighborPublishWorkspace*>(
|
||||
malloc(sizeof(NeighborPublishWorkspace)));
|
||||
#endif
|
||||
if (!publish_workspace) {
|
||||
neighbor_discover_active = false;
|
||||
neighbor_discover_count = 0;
|
||||
if (_cli.getObserverPrefs()->mqtt_neighbors_enabled) {
|
||||
next_neighbors_publish = futureMillis(_cli.getObserverPrefs()->mqtt_neighbors_interval);
|
||||
}
|
||||
return;
|
||||
}
|
||||
auto& pubkey_hex = publish_workspace->pubkey_hex;
|
||||
auto& entries = publish_workspace->entries;
|
||||
uint32_t now_secs = getRTCClock()->getCurrentTime();
|
||||
// The entry table plus one hex string each reaches ~4.5 KB at MAX_NEIGHBOURS,
|
||||
// which does not fit the mesh loop task's 8 KB stack, so both share a single
|
||||
// heap block sized to this pass. Publishing is skipped if either alloc fails.
|
||||
const int publish_count = neighbor_discover_publish_count;
|
||||
const size_t hex_size = PUB_KEY_SIZE * 2 + 1;
|
||||
const size_t entries_bytes =
|
||||
sizeof(MQTTMessageBuilder::NeighborsMessageEntry) * publish_count;
|
||||
void* scratch = neighborsAlloc(entries_bytes + hex_size * publish_count);
|
||||
char* json_buf = (char*)neighborsAlloc(MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE);
|
||||
|
||||
for (int i = 0; i < neighbor_discover_count; i++) {
|
||||
auto& nb = neighbours[neighbor_discover[i].neighbour_idx];
|
||||
mesh::Utils::toHex(pubkey_hex[i], nb.id.pub_key, PUB_KEY_SIZE);
|
||||
entries[i].pubkey_hex = pubkey_hex[i];
|
||||
entries[i].snr = nb.snr / 4.0f;
|
||||
entries[i].heard_secs_ago = (nb.heard_timestamp > 0 && now_secs >= nb.heard_timestamp)
|
||||
? (now_secs - nb.heard_timestamp) : 0;
|
||||
entries[i].scopes = neighbor_discover[i].scopes;
|
||||
switch (neighbor_discover[i].status) {
|
||||
case ND_RESPONDED: entries[i].status = "responded"; break;
|
||||
case ND_SEND_FAILED: entries[i].status = "send_failed"; break;
|
||||
default: entries[i].status = "timeout"; break;
|
||||
if (json_buf && (scratch || publish_count == 0)) {
|
||||
auto* entries = (MQTTMessageBuilder::NeighborsMessageEntry*)scratch;
|
||||
char* pubkey_hex = (char*)scratch + entries_bytes;
|
||||
uint32_t now_secs = getRTCClock()->getCurrentTime();
|
||||
|
||||
for (int i = 0; i < publish_count; i++) {
|
||||
auto& entry = neighbor_discover[i];
|
||||
char* hex = &pubkey_hex[i * hex_size];
|
||||
mesh::Utils::toHex(hex, entry.id.pub_key, PUB_KEY_SIZE);
|
||||
entries[i].pubkey_hex = hex;
|
||||
entries[i].snr = entry.snr / 4.0f;
|
||||
bool heard_known = neighborHeardAgeUsable(entry.heard_timestamp, now_secs);
|
||||
entries[i].heard_unknown = !heard_known;
|
||||
entries[i].heard_secs_ago = heard_known ? (now_secs - entry.heard_timestamp) : 0;
|
||||
entries[i].scopes = entry.scopes;
|
||||
switch (entry.status) {
|
||||
case ND_RESPONDED: entries[i].status = "responded"; break;
|
||||
case ND_SEND_FAILED: entries[i].status = "send_failed"; break;
|
||||
default: entries[i].status = "timeout"; break;
|
||||
}
|
||||
}
|
||||
|
||||
// insertion sort: most useful first (JSON builder drops the tail on overflow)
|
||||
for (int i = 1; i < publish_count; i++) {
|
||||
MQTTMessageBuilder::NeighborsMessageEntry entry = entries[i];
|
||||
int j = i;
|
||||
while (j > 0 && neighborPublishEntryComesBefore(entry, entries[j - 1])) {
|
||||
entries[j] = entries[j - 1];
|
||||
j--;
|
||||
}
|
||||
entries[j] = entry;
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
NeighborsDocAllocator doc_alloc;
|
||||
JsonDocument doc(&doc_alloc);
|
||||
#else
|
||||
JsonDocument doc;
|
||||
#endif
|
||||
int json_len = MQTTMessageBuilder::buildNeighborsMessage(
|
||||
doc, neighbor_discover_origin, self_pubkey_hex, timestamp, self_scopes_buf,
|
||||
self_default_scope_buf, entries, publish_count,
|
||||
json_buf, MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE,
|
||||
neighbor_discover_count, neighbor_discover_queried_count,
|
||||
neighbor_discover_truncated);
|
||||
|
||||
if (json_len > 0 && bridge) {
|
||||
bridge->requestPublishNeighbors(json_buf, (size_t)json_len);
|
||||
}
|
||||
}
|
||||
|
||||
// insertion sort: most useful first (JSON builder drops the tail on overflow)
|
||||
for (int i = 1; i < neighbor_discover_count; i++) {
|
||||
MQTTMessageBuilder::NeighborsMessageEntry entry = entries[i];
|
||||
int j = i;
|
||||
while (j > 0 && neighborPublishEntryComesBefore(entry, entries[j - 1])) {
|
||||
entries[j] = entries[j - 1];
|
||||
j--;
|
||||
}
|
||||
entries[j] = entry;
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
char* json_buf = (char*)heap_caps_malloc(MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE, MALLOC_CAP_SPIRAM);
|
||||
#else
|
||||
char* json_buf = (char*)malloc(MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE);
|
||||
#endif
|
||||
if (!json_buf) {
|
||||
#if defined(ESP_PLATFORM)
|
||||
heap_caps_free(publish_workspace);
|
||||
#else
|
||||
free(publish_workspace);
|
||||
#endif
|
||||
neighbor_discover_active = false;
|
||||
neighbor_discover_count = 0;
|
||||
if (_cli.getObserverPrefs()->mqtt_neighbors_enabled) {
|
||||
next_neighbors_publish = futureMillis(_cli.getObserverPrefs()->mqtt_neighbors_interval);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
NeighborsDocAllocator doc_alloc;
|
||||
JsonDocument doc(&doc_alloc);
|
||||
#else
|
||||
JsonDocument doc;
|
||||
#endif
|
||||
int json_len = MQTTMessageBuilder::buildNeighborsMessage(
|
||||
doc, origin, self_pubkey_hex, timestamp, self_scopes_buf,
|
||||
entries, neighbor_discover_count,
|
||||
json_buf, MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE);
|
||||
|
||||
if (json_len > 0 && bridge) {
|
||||
bridge->requestPublishNeighbors(json_buf, (size_t)json_len);
|
||||
}
|
||||
|
||||
#if defined(ESP_PLATFORM)
|
||||
heap_caps_free(json_buf);
|
||||
heap_caps_free(publish_workspace);
|
||||
#else
|
||||
free(json_buf);
|
||||
free(publish_workspace);
|
||||
#endif
|
||||
neighborsFree(scratch);
|
||||
neighborsFree(json_buf);
|
||||
|
||||
neighbor_discover_active = false;
|
||||
neighbor_discover_count = 0;
|
||||
neighbor_discover_next = 0;
|
||||
neighbor_discover_publish_count = 0;
|
||||
neighbor_discover_queried_count = 0;
|
||||
neighbor_discover_json_size = 0;
|
||||
neighbor_discover_truncated = false;
|
||||
neighbor_discover_until = 0;
|
||||
neighbor_discover_request = NULL;
|
||||
if (_cli.getObserverPrefs()->mqtt_neighbors_enabled) {
|
||||
next_neighbors_publish = futureMillis(_cli.getObserverPrefs()->mqtt_neighbors_interval);
|
||||
}
|
||||
}
|
||||
|
||||
// Advance the scope-query phase; publish once all entries resolve or the window
|
||||
// times out (stragglers marked ND_TIMEOUT).
|
||||
// Advance the newest-first scope-query phase. Keep only one request in flight so
|
||||
// its responder gets a clear reply opportunity and the packet pool stays free.
|
||||
void MyMesh::loopNeighborDiscover() {
|
||||
if (!neighbor_discover_active) return;
|
||||
|
||||
bool all_done = true;
|
||||
for (int i = 0; i < neighbor_discover_count; i++) {
|
||||
if (neighbor_discover[i].status == ND_PENDING) { all_done = false; break; }
|
||||
if (neighbor_discover_next >= neighbor_discover_count) {
|
||||
finishNeighborDiscover();
|
||||
return;
|
||||
}
|
||||
if (!all_done && !millisHasNowPassed(neighbor_discover_until)) return;
|
||||
if (!all_done) {
|
||||
for (int i = 0; i < neighbor_discover_count; i++) {
|
||||
if (neighbor_discover[i].status == ND_PENDING) neighbor_discover[i].status = ND_TIMEOUT;
|
||||
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_next];
|
||||
if (entry.status == ND_QUEUED) {
|
||||
if (!millisHasNowPassed(neighbor_discover_until)) return;
|
||||
if (cancelNeighborDiscoverRequest()) {
|
||||
entry.status = ND_SEND_FAILED;
|
||||
completeNeighborDiscoverEntry();
|
||||
return;
|
||||
}
|
||||
if (isCurrentOutbound(neighbor_discover_request)) {
|
||||
neighbor_discover_until = futureMillis(neighborDiscoverQueryTimeoutMs());
|
||||
return;
|
||||
}
|
||||
neighbor_discover_request = NULL; // packet manager already shed it
|
||||
entry.status = ND_SEND_FAILED;
|
||||
completeNeighborDiscoverEntry();
|
||||
return;
|
||||
}
|
||||
if (entry.status == ND_PENDING) {
|
||||
if (!millisHasNowPassed(neighbor_discover_until)) return;
|
||||
entry.status = ND_TIMEOUT;
|
||||
completeNeighborDiscoverEntry();
|
||||
return;
|
||||
}
|
||||
if (entry.status == ND_RESPONDED || entry.status == ND_SEND_FAILED
|
||||
|| entry.status == ND_TIMEOUT) {
|
||||
completeNeighborDiscoverEntry();
|
||||
return;
|
||||
}
|
||||
if (entry.status != ND_UNSENT) {
|
||||
neighbor_discover_next++;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t tag;
|
||||
mesh::Packet* request = sendAnonRegionsReq(entry.id, tag);
|
||||
if (request) {
|
||||
entry.tag = tag;
|
||||
entry.status = ND_QUEUED;
|
||||
neighbor_discover_request = request;
|
||||
neighbor_discover_until = futureMillis(NEIGHBOR_DISCOVER_QUEUE_TIMEOUT_MS);
|
||||
} else {
|
||||
entry.status = ND_SEND_FAILED;
|
||||
completeNeighborDiscoverEntry();
|
||||
}
|
||||
finishNeighborDiscover();
|
||||
}
|
||||
|
||||
// Shared precondition for starting a discovery: PSRAM present + bridge running.
|
||||
// Shared precondition for starting a discovery: usable buffers + bridge running.
|
||||
// PSRAM builds size their neighbors buffers for PSRAM, so a board whose PSRAM
|
||||
// failed to init must not silently spend that much internal DRAM here.
|
||||
// MQTT_NEIGHBORS_WITHOUT_PSRAM builds are already sized for internal DRAM.
|
||||
bool MyMesh::neighborDiscoverReady(char* reply) {
|
||||
#if defined(ESP_PLATFORM)
|
||||
#if defined(ESP_PLATFORM) && defined(BOARD_HAS_PSRAM)
|
||||
if (!psramFound()) { strcpy(reply, "Err - PSRAM not available"); return false; }
|
||||
#endif
|
||||
if (!bridge || !bridge->isRunning()) { strcpy(reply, "Err - MQTT bridge not running"); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
// Snapshot the neighbor table into the overlay and fire one anon-regions query
|
||||
// per heard neighbour; arm the 30s scope-query window.
|
||||
// Snapshot the neighbor table newest-first. loopNeighborDiscover() emits one
|
||||
// anon-regions query at a time so hidden responders do not reply as a burst.
|
||||
bool MyMesh::startNeighborDiscover(char* reply) {
|
||||
if (neighbor_discover_active) {
|
||||
strcpy(reply, "Err - neighbor discover already active");
|
||||
@@ -2662,30 +2857,48 @@ bool MyMesh::startNeighborDiscover(char* reply) {
|
||||
return false; // reply already set
|
||||
}
|
||||
|
||||
getLocalScopes(self_scopes_buf, sizeof(self_scopes_buf));
|
||||
neighbor_discover_count = 0;
|
||||
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
|
||||
if (neighbours[i].heard_timestamp > 0) {
|
||||
neighbor_discover[neighbor_discover_count].neighbour_idx = (uint8_t)i;
|
||||
neighbor_discover[neighbor_discover_count].scopes[0] = 0;
|
||||
neighbor_discover[neighbor_discover_count].status = ND_PENDING;
|
||||
uint32_t tag;
|
||||
if (sendAnonRegionsReq(neighbours[i].id, tag)) {
|
||||
neighbor_discover[neighbor_discover_count].tag = tag;
|
||||
} else {
|
||||
neighbor_discover[neighbor_discover_count].status = ND_SEND_FAILED;
|
||||
}
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[neighbor_discover_count];
|
||||
entry.id = neighbours[i].id;
|
||||
entry.heard_timestamp = neighbours[i].heard_timestamp;
|
||||
entry.snr = neighbours[i].snr;
|
||||
entry.scopes[0] = 0;
|
||||
entry.tag = 0;
|
||||
entry.status = ND_UNSENT;
|
||||
neighbor_discover_count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Query the freshest/strongest entries first; pubkey makes ties deterministic.
|
||||
for (int i = 1; i < neighbor_discover_count; i++) {
|
||||
NeighborDiscoverEntry entry = neighbor_discover[i];
|
||||
int j = i;
|
||||
while (j > 0) {
|
||||
auto& rhs = neighbor_discover[j - 1];
|
||||
bool before = entry.heard_timestamp > rhs.heard_timestamp
|
||||
|| (entry.heard_timestamp == rhs.heard_timestamp && entry.snr > rhs.snr)
|
||||
|| (entry.heard_timestamp == rhs.heard_timestamp && entry.snr == rhs.snr
|
||||
&& memcmp(entry.id.pub_key, rhs.id.pub_key, PUB_KEY_SIZE) < 0);
|
||||
if (!before) break;
|
||||
neighbor_discover[j] = neighbor_discover[j - 1];
|
||||
j--;
|
||||
}
|
||||
neighbor_discover[j] = entry;
|
||||
}
|
||||
|
||||
neighbor_discover_next = 0;
|
||||
resetNeighborDiscoverJsonBudget();
|
||||
neighbor_discover_active = true;
|
||||
neighbor_discover_until = futureMillis(NEIGHBOR_DISCOVER_TIMEOUT_MS);
|
||||
neighbor_discover_until = 0;
|
||||
neighbor_discover_request = NULL;
|
||||
|
||||
if (neighbor_discover_count == 0) {
|
||||
finishNeighborDiscover();
|
||||
strcpy(reply, "OK - neighbor discover started (0 neighbors, self only)");
|
||||
} else {
|
||||
loopNeighborDiscover(); // queue the first request now
|
||||
sprintf(reply, "OK - neighbor discover started (%u neighbors)", (unsigned)neighbor_discover_count);
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -180,37 +180,55 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
|
||||
uint32_t pending_discover_tag;
|
||||
unsigned long pending_discover_until;
|
||||
enum NeighborDiscoverStatus : uint8_t {
|
||||
ND_PENDING = 1,
|
||||
ND_RESPONDED = 2,
|
||||
ND_TIMEOUT = 3,
|
||||
ND_SEND_FAILED = 4,
|
||||
ND_UNSENT = 0,
|
||||
ND_QUEUED = 1,
|
||||
ND_PENDING = 2,
|
||||
ND_RESPONDED = 3,
|
||||
ND_TIMEOUT = 4,
|
||||
ND_SEND_FAILED = 5,
|
||||
};
|
||||
struct NeighborDiscoverEntry {
|
||||
uint8_t neighbour_idx;
|
||||
mesh::Identity id;
|
||||
uint32_t heard_timestamp;
|
||||
int8_t snr;
|
||||
uint32_t tag;
|
||||
char scopes[96];
|
||||
uint8_t status;
|
||||
};
|
||||
NeighborDiscoverEntry neighbor_discover[MAX_NEIGHBOURS];
|
||||
uint8_t neighbor_discover_count;
|
||||
uint8_t neighbor_discover_next;
|
||||
uint8_t neighbor_discover_publish_count;
|
||||
uint8_t neighbor_discover_queried_count; // requests confirmed transmitted
|
||||
size_t neighbor_discover_json_size;
|
||||
bool neighbor_discover_truncated;
|
||||
bool neighbor_discover_active;
|
||||
bool neighbor_table_refresh_active;
|
||||
bool neighbor_table_refresh_periodic;
|
||||
unsigned long neighbor_discover_until;
|
||||
mesh::Packet* neighbor_discover_request;
|
||||
unsigned long next_neighbors_publish;
|
||||
char self_scopes_buf[96];
|
||||
char self_default_scope_buf[31];
|
||||
char neighbor_discover_origin[32];
|
||||
|
||||
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
|
||||
void sendNodeDiscoverReq();
|
||||
bool sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag);
|
||||
mesh::Packet* sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag);
|
||||
bool cancelNeighborDiscoverRequest();
|
||||
uint32_t neighborDiscoverQueryTimeoutMs() const;
|
||||
bool completeNeighborDiscoverEntry();
|
||||
void resetNeighborDiscoverJsonBudget();
|
||||
bool neighborDiscoverReady(char* reply);
|
||||
bool startNeighborDiscover(char* reply);
|
||||
void loopNeighborDiscover();
|
||||
void finishNeighborDiscover();
|
||||
bool handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data, size_t len);
|
||||
void touchNeighbourHeard(const mesh::Identity& id, uint32_t heard_timestamp);
|
||||
void getLocalScopes(char* buf, size_t len);
|
||||
static const int NEIGHBOR_DISCOVER_PEER_BASE = 1000;
|
||||
static const unsigned long NEIGHBOR_DISCOVER_TIMEOUT_MS = 30000;
|
||||
static const unsigned long NEIGHBOR_DISCOVER_QUEUE_TIMEOUT_MS = 29000;
|
||||
static const int NEIGHBOR_DISCOVER_MIN_FREE_PACKETS = 5;
|
||||
#endif
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
MQTTBridge* bridge;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#ifdef DISPLAY_CLASS
|
||||
#include "UITask.h"
|
||||
static UITask ui_task(display);
|
||||
static bool display_ready = false;
|
||||
#endif
|
||||
|
||||
StdRNG fast_rng;
|
||||
@@ -45,7 +46,8 @@ void setup() {
|
||||
#endif
|
||||
|
||||
#ifdef DISPLAY_CLASS
|
||||
if (display.begin()) {
|
||||
display_ready = display.begin();
|
||||
if (display_ready) {
|
||||
display.startFrame();
|
||||
display.setCursor(0, 0);
|
||||
display.print("Please wait...");
|
||||
@@ -113,7 +115,9 @@ void setup() {
|
||||
the_mesh.begin(fs);
|
||||
|
||||
#ifdef DISPLAY_CLASS
|
||||
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
|
||||
if (display_ready) {
|
||||
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ETHERNET_ENABLED
|
||||
@@ -179,7 +183,7 @@ void loop() {
|
||||
the_mesh.loop();
|
||||
sensors.loop();
|
||||
#ifdef DISPLAY_CLASS
|
||||
ui_task.loop();
|
||||
if (display_ready) ui_task.loop();
|
||||
#endif
|
||||
rtc_clock.tick();
|
||||
#ifdef HAS_EXTERNAL_WATCHDOG
|
||||
|
||||
@@ -915,7 +915,6 @@ SensorMesh::SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::Millise
|
||||
radio_apply_failures = 0;
|
||||
|
||||
// defaults
|
||||
memset(&_prefs, 0, sizeof(_prefs));
|
||||
_prefs.airtime_factor = 1.0; // one half
|
||||
_prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0;
|
||||
_prefs.tx_delay_factor = 0.5f; // was 0.25f
|
||||
|
||||
Reference in new issue
Block a user