fix: harden firmware builds and runtime reliability
This commit is contained in:
1 parent
66c9e3f73b
commit
bd3a94bddb
71 files changed
+2102
-466
No files matched your search
@@ -1273,8 +1273,8 @@ void MyMesh::onControlDataRecv(mesh::Packet *packet) {
|
||||
}
|
||||
int i = 0;
|
||||
out_frame[i++] = PUSH_CODE_CONTROL_DATA;
|
||||
out_frame[i++] = (int8_t)(_radio->getLastSNR() * 4);
|
||||
out_frame[i++] = (int8_t)(_radio->getLastRSSI());
|
||||
out_frame[i++] = (int8_t)(packet->getSNR() * 4);
|
||||
out_frame[i++] = (int8_t)packet->getRSSI();
|
||||
out_frame[i++] = packet->path_len;
|
||||
memcpy(&out_frame[i], packet->payload, packet->payload_len);
|
||||
i += packet->payload_len;
|
||||
@@ -1293,8 +1293,8 @@ void MyMesh::onRawDataRecv(mesh::Packet *packet) {
|
||||
}
|
||||
int i = 0;
|
||||
out_frame[i++] = PUSH_CODE_RAW_DATA;
|
||||
out_frame[i++] = (int8_t)(_radio->getLastSNR() * 4);
|
||||
out_frame[i++] = (int8_t)(_radio->getLastRSSI());
|
||||
out_frame[i++] = (int8_t)(packet->getSNR() * 4);
|
||||
out_frame[i++] = (int8_t)packet->getRSSI();
|
||||
out_frame[i++] = 0xFF; // reserved (possibly path_len in future)
|
||||
memcpy(&out_frame[i], packet->payload, packet->payload_len);
|
||||
i += packet->payload_len;
|
||||
@@ -1477,9 +1477,10 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
|
||||
_prefs.powersaving_policy_version = 0;
|
||||
_prefs.wifi_enabled = 1;
|
||||
memset(_prefs.bluetooth_name, 0, sizeof(_prefs.bluetooth_name));
|
||||
#if defined(MESH_DUAL_CDC_LOGGING)
|
||||
// Keep the primary CDC stream exclusively framed unless the owner opts in
|
||||
// to the separate plaintext logging interface and reboots.
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
// Keep Full Companion's primary stream exclusively framed on a fresh
|
||||
// install. Dual-CDC builds can add a diagnostics port; single-TTY builds
|
||||
// switch the primary stream into the text terminal before emitting logs.
|
||||
_prefs.usb_logging_enabled = 0;
|
||||
#else
|
||||
_prefs.usb_logging_enabled = 1;
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#include <Mesh.h>
|
||||
#include "MyMesh.h"
|
||||
#include "CompanionWiFi.h"
|
||||
#if MESH_PACKET_LOGGING
|
||||
#include <helpers/SerialPacketLog.h>
|
||||
#endif
|
||||
|
||||
#ifdef ESP32_PLATFORM
|
||||
#include "esp_bt.h"
|
||||
@@ -291,6 +294,7 @@ static char usb_terminal_line[MAX_TRANS_UNIT * 2 + 32];
|
||||
static size_t usb_terminal_line_len = 0;
|
||||
static bool usb_terminal_discard_line = false;
|
||||
static bool usb_terminal_disconnect_armed = false;
|
||||
static bool usb_logging_terminal_mode = false;
|
||||
#if COMPANION_FEATURE_USB_MOTA_SOURCE
|
||||
static bool usb_mota_mode = false;
|
||||
static char usb_mota_line[32];
|
||||
@@ -345,9 +349,15 @@ static void enterUsbTerminalMode() {
|
||||
clearUsbTerminalLine();
|
||||
usb_terminal_discard_line = false;
|
||||
usb_terminal_disconnect_armed = isUsbTerminalDataConnected();
|
||||
usb_logging_terminal_mode = false;
|
||||
the_mesh.enterTerminalMode();
|
||||
}
|
||||
|
||||
static void enterUsbLoggingTerminalMode() {
|
||||
enterUsbTerminalMode();
|
||||
usb_logging_terminal_mode = true;
|
||||
}
|
||||
|
||||
static void leaveUsbTerminalMode(bool acknowledge) {
|
||||
if (acknowledge) {
|
||||
Serial.print("\r\nOK - Binary mode\r\n");
|
||||
@@ -357,6 +367,7 @@ static void leaveUsbTerminalMode(bool acknowledge) {
|
||||
clearUsbTerminalLine();
|
||||
usb_terminal_discard_line = false;
|
||||
usb_terminal_disconnect_armed = false;
|
||||
usb_logging_terminal_mode = false;
|
||||
}
|
||||
|
||||
#if COMPANION_FEATURE_USB_MOTA_SOURCE
|
||||
@@ -445,6 +456,26 @@ static void serviceUsbTerminal() {
|
||||
serviceUsbMota();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
// A saved logging-on preference makes the one available TTY behave like a
|
||||
// logging repeater: plaintext diagnostics plus an input-capable CLI. Put the
|
||||
// Companion interface into passthrough before it can mix framed traffic with
|
||||
// logs. `set usb.logging off` remains available here and returns this TTY to
|
||||
// Binary Companion after its command reply, even on USB-UART bridges that
|
||||
// cannot detect a host disconnect.
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
if (!mesh::hasDedicatedUsbLoggingPort()) {
|
||||
if (mesh::isUsbLoggingEnabled()) {
|
||||
if (!the_mesh.isTerminalMode()) {
|
||||
enterUsbLoggingTerminalMode();
|
||||
return;
|
||||
}
|
||||
usb_logging_terminal_mode = true;
|
||||
} else if (usb_logging_terminal_mode && the_mesh.isTerminalMode()) {
|
||||
leaveUsbTerminalMode(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (!the_mesh.isTerminalMode()) {
|
||||
if (usb_serial_interface.takeControlSequence()) {
|
||||
@@ -491,6 +522,13 @@ static void serviceUsbTerminal() {
|
||||
Serial.print("\r\n");
|
||||
the_mesh.handleTerminalCommand(usb_terminal_line);
|
||||
clearUsbTerminalLine();
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
if (usb_logging_terminal_mode
|
||||
&& !mesh::isUsbLoggingEnabled()) {
|
||||
leaveUsbTerminalMode(true);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
Serial.print("> ");
|
||||
return; // service at most one command per mesh loop
|
||||
}
|
||||
@@ -1025,6 +1063,9 @@ void halt() {
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
#if MESH_PACKET_LOGGING
|
||||
mesh::serialLogBegin();
|
||||
#endif
|
||||
mesh::beginUsbLoggingPort();
|
||||
board.begin();
|
||||
|
||||
@@ -1238,6 +1279,14 @@ void setup() {
|
||||
usb_serial_interface.setConnectedCheck([]() { return (bool)Serial; });
|
||||
#endif
|
||||
interface_manager.addInterface(InterfaceType::USB, &usb_serial_interface);
|
||||
#if defined(COMPANION_RADIO_FULL)
|
||||
if (!mesh::hasDedicatedUsbLoggingPort()
|
||||
&& mesh::isUsbLoggingEnabled()) {
|
||||
// Apply a saved single-TTY logging preference before the dispatcher can
|
||||
// emit its first framed Companion response on this interface.
|
||||
enterUsbLoggingTerminalMode();
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// add ethernet interface
|
||||
|
||||
@@ -5,8 +5,12 @@
|
||||
#include <stdlib.h> // for qsort()
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#include <helpers/ClockSyncUtils.h>
|
||||
#include <helpers/DatagramPayloadLimits.h>
|
||||
#include <helpers/FloodFilterPolicy.h>
|
||||
#include <helpers/RegionNameUtils.h>
|
||||
#if MESH_PACKET_LOGGING
|
||||
#include <helpers/SerialPacketLog.h>
|
||||
#endif
|
||||
#if defined(USE_LR2021)
|
||||
#include <helpers/radiolib/LR2021SideDetectorConfig.h>
|
||||
#endif
|
||||
@@ -96,6 +100,14 @@ extern "C" caddr_t _sbrk(int increment);
|
||||
|
||||
#define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
|
||||
|
||||
// createDatagram() rejects a reply beyond this plaintext limit after the MAC
|
||||
// and worst-case cipher padding are added. Exporters must stay within it or no
|
||||
// reply is transmitted.
|
||||
static constexpr size_t MAX_ANON_REPLY_LEN =
|
||||
DatagramPayloadLimits::maxPlaintext(
|
||||
MAX_PACKET_PAYLOAD, CIPHER_MAC_SIZE, CIPHER_BLOCK_SIZE);
|
||||
static_assert(MAX_ANON_REPLY_LEN >= 8, "anonymous reply prefix must fit");
|
||||
|
||||
#define ANON_REQ_TYPE_REGIONS 0x01
|
||||
#define ANON_REQ_TYPE_OWNER 0x02
|
||||
#define ANON_REQ_TYPE_BASIC 0x03 // just remote clock
|
||||
@@ -472,7 +484,8 @@ static void formatFixed3(char* dest, size_t dest_len, float value) {
|
||||
snprintf(dest, dest_len, "%ld.%03ld", whole, decimals);
|
||||
}
|
||||
|
||||
void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr) {
|
||||
void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr,
|
||||
int16_t rssi) {
|
||||
#if MAX_NEIGHBOURS // check if neighbours enabled
|
||||
// find existing neighbour, else use least recently updated
|
||||
uint32_t oldest_timestamp = 0xFFFFFFFF;
|
||||
@@ -496,6 +509,11 @@ void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float sn
|
||||
neighbour->advert_timestamp = timestamp;
|
||||
neighbour->heard_timestamp = getRTCClock()->getCurrentTime();
|
||||
neighbour->snr = (int8_t)(snr * 4);
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
neighbour->rssi = rssi;
|
||||
#else
|
||||
(void)rssi;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -606,7 +624,9 @@ uint8_t MyMesh::handleAnonRegionsReq(const mesh::Identity& sender, uint32_t send
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
memcpy(&reply_data[4], &now, 4); // include our clock (for easy clock sync, and packet hash uniqueness)
|
||||
|
||||
return 8 + region_map.exportNamesTo((char *) &reply_data[8], sizeof(reply_data) - 12, REGION_DENY_FLOOD); // reply length
|
||||
return 8 + region_map.exportNamesTo(
|
||||
(char *)&reply_data[8], MAX_ANON_REPLY_LEN - 8,
|
||||
REGION_DENY_FLOOD); // reply length
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1026,12 +1046,16 @@ const char *MyMesh::getLogDateTime() {
|
||||
void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
|
||||
#if MESH_PACKET_LOGGING
|
||||
if (mesh::isUsbLoggingEnabled()) {
|
||||
// Logging builds prefer backpressure over silently losing a packet record.
|
||||
Stream& logging_port = mesh::usbLoggingPort();
|
||||
logging_port.print(getLogDateTime());
|
||||
logging_port.print(" RAW: ");
|
||||
mesh::Utils::printHex(logging_port, raw, len);
|
||||
logging_port.println();
|
||||
mesh::SerialLogLine<> line;
|
||||
#if MESH_PACKET_LOGGING_COMPACT
|
||||
line.printf("R");
|
||||
line.hex(raw, len);
|
||||
line.flush(mesh::usbLoggingPort(), false);
|
||||
#else
|
||||
line.printf("%s RAW: ", getLogDateTime());
|
||||
line.hex(raw, len);
|
||||
line.flush(mesh::usbLoggingPort());
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -2584,7 +2608,7 @@ void MyMesh::onAdvertRecv(mesh::Packet *packet, const mesh::Identity &id, uint32
|
||||
if (packet->getPathHashCount() == 0 && !isShare(packet)) {
|
||||
AdvertDataParser parser(app_data, app_data_len);
|
||||
if (parser.isValid() && parser.getType() == ADV_TYPE_REPEATER) { // just keep neigbouring Repeaters
|
||||
putNeighbour(id, timestamp, packet->getSNR());
|
||||
putNeighbour(id, timestamp, packet->getSNR(), packet->getRSSI());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2612,7 +2636,8 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
||||
if (neighbor_discover_active && i >= NEIGHBOR_DISCOVER_PEER_BASE) {
|
||||
int oi = i - NEIGHBOR_DISCOVER_PEER_BASE;
|
||||
if (type == PAYLOAD_TYPE_RESPONSE && oi >= 0 && oi < neighbor_discover_count) {
|
||||
handleNeighborDiscoverResponse(oi, data, len);
|
||||
handleNeighborDiscoverResponse(
|
||||
oi, data, len, packet->getSNR(), packet->getRSSI());
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -2628,7 +2653,8 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
||||
if (neighbor_discover_active && type == PAYLOAD_TYPE_RESPONSE) {
|
||||
for (int oi = 0; oi < neighbor_discover_count; oi++) {
|
||||
if (client->id.matches(neighbor_discover[oi].id)
|
||||
&& handleNeighborDiscoverResponse(oi, data, len)) {
|
||||
&& handleNeighborDiscoverResponse(
|
||||
oi, data, len, packet->getSNR(), packet->getRSSI())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -3103,7 +3129,8 @@ void MyMesh::onControlDataRecv(mesh::Packet* packet) {
|
||||
if (id.matches(self_id)) {
|
||||
return;
|
||||
}
|
||||
putNeighbour(id, rtc_clock.getCurrentTime(), packet->getSNR());
|
||||
putNeighbour(id, rtc_clock.getCurrentTime(), packet->getSNR(),
|
||||
packet->getRSSI());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11884,6 +11911,7 @@ bool MyMesh::completeNeighborDiscoverEntry() {
|
||||
entry.status == ND_RESPONDED ? "responded"
|
||||
: (entry.status == ND_SEND_FAILED ? "send_failed" : "timeout")
|
||||
};
|
||||
measured.rssi = entry.rssi;
|
||||
size_t added = MQTTMessageBuilder::measureNeighborsMessageEntry(measured);
|
||||
if (neighbor_discover_publish_count > 0) added++; // array comma
|
||||
|
||||
@@ -11902,7 +11930,9 @@ bool MyMesh::completeNeighborDiscoverEntry() {
|
||||
|
||||
// Match a RESPONSE against the pending overlay entry by tag; copy its scope
|
||||
// string (payload after the 8-byte {tag}{clock} header) into the entry.
|
||||
bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data, size_t len) {
|
||||
bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx,
|
||||
const uint8_t* data, size_t len,
|
||||
float snr, int16_t rssi) {
|
||||
if (overlay_idx < 0 || overlay_idx >= neighbor_discover_count) return false;
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[overlay_idx];
|
||||
if (entry.status != ND_PENDING || len < 8) return false;
|
||||
@@ -11919,19 +11949,24 @@ bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data
|
||||
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.
|
||||
// snapshot and live table with this packet's measurements. A stamp taken
|
||||
// before time sync also heals here.
|
||||
entry.heard_timestamp = getRTCClock()->getCurrentTime();
|
||||
touchNeighbourHeard(entry.id, entry.heard_timestamp);
|
||||
entry.snr = (int8_t)(snr * 4);
|
||||
entry.rssi = rssi;
|
||||
touchNeighbourHeard(entry.id, entry.heard_timestamp, snr, rssi);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
void MyMesh::touchNeighbourHeard(const mesh::Identity& id,
|
||||
uint32_t heard_timestamp, float snr,
|
||||
int16_t rssi) {
|
||||
#if MAX_NEIGHBOURS
|
||||
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
|
||||
if (id.matches(neighbours[i].id)) {
|
||||
neighbours[i].heard_timestamp = heard_timestamp;
|
||||
neighbours[i].snr = (int8_t)(snr * 4);
|
||||
neighbours[i].rssi = rssi;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -12054,6 +12089,7 @@ void MyMesh::finishNeighborDiscover() {
|
||||
mesh::Utils::toHex(hex, entry.id.pub_key, PUB_KEY_SIZE);
|
||||
entries[i].pubkey_hex = hex;
|
||||
entries[i].snr = entry.snr / 4.0f;
|
||||
entries[i].rssi = entry.rssi;
|
||||
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;
|
||||
@@ -12197,6 +12233,7 @@ bool MyMesh::startNeighborDiscover(char* reply) {
|
||||
entry.id = neighbours[i].id;
|
||||
entry.heard_timestamp = neighbours[i].heard_timestamp;
|
||||
entry.snr = neighbours[i].snr;
|
||||
entry.rssi = neighbours[i].rssi;
|
||||
entry.scopes[0] = 0;
|
||||
entry.tag = 0;
|
||||
entry.status = ND_UNSENT;
|
||||
|
||||
@@ -131,6 +131,9 @@ struct NeighbourInfo {
|
||||
uint32_t advert_timestamp;
|
||||
uint32_t heard_timestamp;
|
||||
int8_t snr; // multiplied by 4, user should divide to get float value
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
int16_t rssi; // dBm from the last packet heard from this neighbour
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifndef FIRMWARE_BUILD_DATE
|
||||
@@ -539,6 +542,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
|
||||
mesh::Identity id; // immutable snapshot: neighbour table can change mid-pass
|
||||
uint32_t heard_timestamp;
|
||||
int8_t snr; // multiplied by 4
|
||||
int16_t rssi; // dBm from the last packet heard from this neighbour
|
||||
uint32_t tag; // anon-regions request tag we're waiting on
|
||||
char scopes[96]; // scope names from the response
|
||||
uint8_t status; // NeighborDiscoverStatus
|
||||
@@ -569,8 +573,10 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
|
||||
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);
|
||||
bool handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data,
|
||||
size_t len, float snr, int16_t rssi);
|
||||
void touchNeighbourHeard(const mesh::Identity& id, uint32_t heard_timestamp,
|
||||
float snr, int16_t rssi);
|
||||
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.
|
||||
@@ -606,7 +612,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
|
||||
void formatFloodRetryPath(char* dest, size_t dest_len, const mesh::Packet* packet) const;
|
||||
bool handleClientPathCommand(ClientInfo* sender, char* command, char* reply);
|
||||
bool formatFloodRetryHeard(char* dest, size_t dest_len, const mesh::Packet* packet) const;
|
||||
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
|
||||
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr,
|
||||
int16_t rssi);
|
||||
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood);
|
||||
uint8_t handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
|
||||
uint8_t handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#include <Arduino.h> // needed for PlatformIO
|
||||
#include <Mesh.h>
|
||||
#include <helpers/IdentityGeneration.h>
|
||||
#if MESH_PACKET_LOGGING
|
||||
#include <helpers/SerialPacketLog.h>
|
||||
#endif
|
||||
|
||||
#include "MyMesh.h"
|
||||
#if defined(ESP32_PLATFORM)
|
||||
@@ -69,6 +72,9 @@ static unsigned long userBtnDownAt = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
#if MESH_PACKET_LOGGING
|
||||
mesh::serialLogBegin();
|
||||
#endif
|
||||
delay(1000);
|
||||
|
||||
board.begin();
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#include "MyMesh.h"
|
||||
#include <helpers/radiolib/RxBoostedGainDefaults.h>
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#if MESH_PACKET_LOGGING
|
||||
#include <helpers/SerialPacketLog.h>
|
||||
#endif
|
||||
#if MESH_ENABLE_ROOM_FLOOD_RULE_ENGINE
|
||||
#include <helpers/FloodFilterPolicy.h>
|
||||
#endif
|
||||
@@ -265,12 +268,10 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t
|
||||
void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
|
||||
#if MESH_PACKET_LOGGING
|
||||
if (mesh::isUsbLoggingEnabled()) {
|
||||
// Logging builds prefer backpressure over silently losing a packet record.
|
||||
Stream& logging_port = mesh::usbLoggingPort();
|
||||
logging_port.print(getLogDateTime());
|
||||
logging_port.print(" RAW: ");
|
||||
mesh::Utils::printHex(logging_port, raw, len);
|
||||
logging_port.println();
|
||||
mesh::SerialLogLine<> line;
|
||||
line.printf("%s RAW: ", getLogDateTime());
|
||||
line.hex(raw, len);
|
||||
line.flush(mesh::usbLoggingPort());
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -770,7 +771,8 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
||||
if (neighbor_discover_active && i >= NEIGHBOR_DISCOVER_PEER_BASE) {
|
||||
int oi = i - NEIGHBOR_DISCOVER_PEER_BASE;
|
||||
if (type == PAYLOAD_TYPE_RESPONSE && oi >= 0 && oi < neighbor_discover_count) {
|
||||
handleNeighborDiscoverResponse(oi, data, len);
|
||||
handleNeighborDiscoverResponse(
|
||||
oi, data, len, packet->getSNR(), packet->getRSSI());
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -786,7 +788,8 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
|
||||
if (neighbor_discover_active && type == PAYLOAD_TYPE_RESPONSE) {
|
||||
for (int oi = 0; oi < neighbor_discover_count; oi++) {
|
||||
if (client->id.matches(neighbor_discover[oi].id)
|
||||
&& handleNeighborDiscoverResponse(oi, data, len)) {
|
||||
&& handleNeighborDiscoverResponse(
|
||||
oi, data, len, packet->getSNR(), packet->getRSSI())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1051,7 +1054,7 @@ void MyMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
|
||||
if (packet->getPathHashCount() == 0 && !is_share) {
|
||||
AdvertDataParser parser(app_data, app_data_len);
|
||||
if (parser.isValid() && parser.getType() == ADV_TYPE_REPEATER) {
|
||||
putNeighbour(id, timestamp, packet->getSNR());
|
||||
putNeighbour(id, timestamp, packet->getSNR(), packet->getRSSI());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1066,7 +1069,8 @@ void MyMesh::onGroupPacketRecv(mesh::Packet* packet) {
|
||||
#define CTL_TYPE_NODE_DISCOVER_REQ 0x80
|
||||
#define CTL_TYPE_NODE_DISCOVER_RESP 0x90
|
||||
|
||||
void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr) {
|
||||
void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr,
|
||||
int16_t rssi) {
|
||||
// find existing neighbour, else use least recently updated
|
||||
uint32_t oldest_timestamp = 0xFFFFFFFF;
|
||||
NeighbourInfo *neighbour = &neighbours[0];
|
||||
@@ -1089,6 +1093,7 @@ void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float sn
|
||||
neighbour->advert_timestamp = timestamp;
|
||||
neighbour->heard_timestamp = getRTCClock()->getCurrentTime();
|
||||
neighbour->snr = (int8_t)(snr * 4);
|
||||
neighbour->rssi = rssi;
|
||||
}
|
||||
|
||||
void MyMesh::onControlDataRecv(mesh::Packet* packet) {
|
||||
@@ -1120,7 +1125,8 @@ void MyMesh::onControlDataRecv(mesh::Packet* packet) {
|
||||
if (id.matches(self_id)) {
|
||||
return;
|
||||
}
|
||||
putNeighbour(id, getRTCClock()->getCurrentTime(), packet->getSNR());
|
||||
putNeighbour(id, getRTCClock()->getCurrentTime(), packet->getSNR(),
|
||||
packet->getRSSI());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2712,6 +2718,7 @@ bool MyMesh::completeNeighborDiscoverEntry() {
|
||||
entry.status == ND_RESPONDED ? "responded"
|
||||
: (entry.status == ND_SEND_FAILED ? "send_failed" : "timeout")
|
||||
};
|
||||
measured.rssi = entry.rssi;
|
||||
size_t added = MQTTMessageBuilder::measureNeighborsMessageEntry(measured);
|
||||
if (neighbor_discover_publish_count > 0) added++; // array comma
|
||||
|
||||
@@ -2730,7 +2737,9 @@ bool MyMesh::completeNeighborDiscoverEntry() {
|
||||
|
||||
// Match a RESPONSE against the pending overlay entry by tag; copy its scope
|
||||
// string (payload after the 8-byte {tag}{clock} header) into the entry.
|
||||
bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data, size_t len) {
|
||||
bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx,
|
||||
const uint8_t* data, size_t len,
|
||||
float snr, int16_t rssi) {
|
||||
if (overlay_idx < 0 || overlay_idx >= neighbor_discover_count) return false;
|
||||
NeighborDiscoverEntry& entry = neighbor_discover[overlay_idx];
|
||||
if (entry.status != ND_PENDING || len < 8) return false;
|
||||
@@ -2747,18 +2756,23 @@ bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data
|
||||
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.
|
||||
// snapshot and live table with this packet's measurements. A stamp taken
|
||||
// before time sync also heals here.
|
||||
entry.heard_timestamp = getRTCClock()->getCurrentTime();
|
||||
touchNeighbourHeard(entry.id, entry.heard_timestamp);
|
||||
entry.snr = (int8_t)(snr * 4);
|
||||
entry.rssi = rssi;
|
||||
touchNeighbourHeard(entry.id, entry.heard_timestamp, snr, rssi);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
void MyMesh::touchNeighbourHeard(const mesh::Identity& id,
|
||||
uint32_t heard_timestamp, float snr,
|
||||
int16_t rssi) {
|
||||
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
|
||||
if (id.matches(neighbours[i].id)) {
|
||||
neighbours[i].heard_timestamp = heard_timestamp;
|
||||
neighbours[i].snr = (int8_t)(snr * 4);
|
||||
neighbours[i].rssi = rssi;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -2880,6 +2894,7 @@ void MyMesh::finishNeighborDiscover() {
|
||||
mesh::Utils::toHex(hex, entry.id.pub_key, PUB_KEY_SIZE);
|
||||
entries[i].pubkey_hex = hex;
|
||||
entries[i].snr = entry.snr / 4.0f;
|
||||
entries[i].rssi = entry.rssi;
|
||||
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;
|
||||
@@ -3023,6 +3038,7 @@ bool MyMesh::startNeighborDiscover(char* reply) {
|
||||
entry.id = neighbours[i].id;
|
||||
entry.heard_timestamp = neighbours[i].heard_timestamp;
|
||||
entry.snr = neighbours[i].snr;
|
||||
entry.rssi = neighbours[i].rssi;
|
||||
entry.scopes[0] = 0;
|
||||
entry.tag = 0;
|
||||
entry.status = ND_UNSENT;
|
||||
|
||||
@@ -145,6 +145,7 @@ struct NeighbourInfo {
|
||||
uint32_t advert_timestamp;
|
||||
uint32_t heard_timestamp;
|
||||
int8_t snr; // multiplied by 4, user should divide to get float value
|
||||
int16_t rssi; // dBm from the last packet heard from this neighbour
|
||||
};
|
||||
|
||||
class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
|
||||
@@ -218,6 +219,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
|
||||
mesh::Identity id;
|
||||
uint32_t heard_timestamp;
|
||||
int8_t snr;
|
||||
int16_t rssi;
|
||||
uint32_t tag;
|
||||
char scopes[96];
|
||||
uint8_t status;
|
||||
@@ -239,7 +241,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
|
||||
char self_default_scope_buf[31];
|
||||
char neighbor_discover_origin[32];
|
||||
|
||||
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
|
||||
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr,
|
||||
int16_t rssi);
|
||||
void sendNodeDiscoverReq();
|
||||
mesh::Packet* sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag);
|
||||
bool cancelNeighborDiscoverRequest();
|
||||
@@ -250,8 +253,10 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
|
||||
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);
|
||||
bool handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data,
|
||||
size_t len, float snr, int16_t rssi);
|
||||
void touchNeighbourHeard(const mesh::Identity& id, uint32_t heard_timestamp,
|
||||
float snr, int16_t rssi);
|
||||
void getLocalScopes(char* buf, size_t len);
|
||||
static const int NEIGHBOR_DISCOVER_PEER_BASE = 1000;
|
||||
static const unsigned long NEIGHBOR_DISCOVER_QUEUE_TIMEOUT_MS = 29000;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#include <Arduino.h> // needed for PlatformIO
|
||||
#include <Mesh.h>
|
||||
#include <helpers/IdentityGeneration.h>
|
||||
#if MESH_PACKET_LOGGING
|
||||
#include <helpers/SerialPacketLog.h>
|
||||
#endif
|
||||
|
||||
#include "MyMesh.h"
|
||||
|
||||
@@ -37,6 +40,9 @@ unsigned long POWERSAVING_FIRSTSLEEP_SECS = 120; // The first sleep (if enabled)
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
#if MESH_PACKET_LOGGING
|
||||
mesh::serialLogBegin();
|
||||
#endif
|
||||
delay(1000);
|
||||
|
||||
board.begin();
|
||||
|
||||
Reference in new issue
Block a user