From 98c8eaaaf0e52bdaf5911e1d8a2f094ec78f1136 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 24 Apr 2022 16:12:25 -0500 Subject: [PATCH] Moved sender short name method into protobuf module (#1398) * Moved sender short name method into protobuf module * Correct factory reset code for NRF (from 1.2) * Use the correct fs abstraction Co-authored-by: Ben Meadors --- src/mesh/NodeDB.cpp | 8 ++++--- src/mesh/ProtobufModule.h | 11 +++++++++ src/modules/Telemetry/DeviceTelemetry.cpp | 15 +----------- src/modules/Telemetry/DeviceTelemetry.h | 1 - .../Telemetry/EnvironmentTelemetry.cpp | 23 ++++--------------- 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index ad113e8a..95bc6bf3 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -95,14 +95,16 @@ bool NodeDB::resetRadioConfig() nvs_flash_erase(); #endif #ifdef NRF52_SERIES + // first, remove the "/prefs" (this removes most prefs) FSCom.rmdir_r("/prefs"); - + // second, install default state (this will deal with the duplicate mac address issue) + installDefaultDeviceState(); + // third, write to disk + saveToDisk(); Bluefruit.begin(); - DEBUG_MSG("Clearing bluetooth bonds!\n"); bond_print_list(BLE_GAP_ROLE_PERIPH); bond_print_list(BLE_GAP_ROLE_CENTRAL); - Bluefruit.Periph.clearBonds(); Bluefruit.Central.clearBonds(); #endif diff --git a/src/mesh/ProtobufModule.h b/src/mesh/ProtobufModule.h index dad0eade..4e411c2a 100644 --- a/src/mesh/ProtobufModule.h +++ b/src/mesh/ProtobufModule.h @@ -48,6 +48,17 @@ template class ProtobufModule : protected SinglePortModule return p; } + /** + * Gets the short name from the sender of the mesh packet + * Returns "???" if unknown sender + */ + const char *getSenderShortName(const MeshPacket &mp) + { + auto node = nodeDB.getNode(getFrom(&mp)); + const char *sender = (node) ? node->user.short_name : "???"; + return sender; + } + private: /** Called to handle a particular incoming message diff --git a/src/modules/Telemetry/DeviceTelemetry.cpp b/src/modules/Telemetry/DeviceTelemetry.cpp index 23c8a42a..4568521c 100644 --- a/src/modules/Telemetry/DeviceTelemetry.cpp +++ b/src/modules/Telemetry/DeviceTelemetry.cpp @@ -27,7 +27,7 @@ int32_t DeviceTelemetryModule::runOnce() bool DeviceTelemetryModule::handleReceivedProtobuf(const MeshPacket &mp, Telemetry *t) { if (t->which_variant == Telemetry_device_metrics_tag) { - String sender = getSenderName(mp); + const char *sender = getSenderShortName(mp); DEBUG_MSG("-----------------------------------------\n"); DEBUG_MSG("Device Telemetry: Received data from %s\n", sender); @@ -44,19 +44,6 @@ bool DeviceTelemetryModule::handleReceivedProtobuf(const MeshPacket &mp, Telemet return false; // Let others look at this message also if they want } -String DeviceTelemetryModule::getSenderName(const MeshPacket &mp) -{ - String sender; - - auto node = nodeDB.getNode(getFrom(&mp)); - if (node) { - sender = node->user.short_name; - } else { - sender = "UNK"; - } - return sender; -} - bool DeviceTelemetryModule::sendOurTelemetry(NodeNum dest, bool wantReplies) { Telemetry t; diff --git a/src/modules/Telemetry/DeviceTelemetry.h b/src/modules/Telemetry/DeviceTelemetry.h index 67c7ac30..c224a2c1 100644 --- a/src/modules/Telemetry/DeviceTelemetry.h +++ b/src/modules/Telemetry/DeviceTelemetry.h @@ -27,7 +27,6 @@ class DeviceTelemetryModule : private concurrency::OSThread, public ProtobufModu bool sendOurTelemetry(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false); private: - String getSenderName(const MeshPacket &mp); bool firstTime = 1; const MeshPacket *lastMeasurementPacket; }; diff --git a/src/modules/Telemetry/EnvironmentTelemetry.cpp b/src/modules/Telemetry/EnvironmentTelemetry.cpp index 675969fd..0257fe31 100644 --- a/src/modules/Telemetry/EnvironmentTelemetry.cpp +++ b/src/modules/Telemetry/EnvironmentTelemetry.cpp @@ -149,19 +149,6 @@ int32_t EnvironmentTelemetryModule::runOnce() #endif } -String GetSenderName(const MeshPacket &mp) -{ - String sender; - - auto node = nodeDB.getNode(getFrom(&mp)); - if (node) { - sender = node->user.short_name; - } else { - sender = "UNK"; - } - return sender; -} - uint32_t GetTimeSinceMeshPacket(const MeshPacket *mp) { uint32_t now = getTime(); @@ -198,7 +185,7 @@ void EnvironmentTelemetryModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiSt Telemetry lastMeasurement; uint32_t agoSecs = GetTimeSinceMeshPacket(lastMeasurementPacket); - String lastSender = GetSenderName(*lastMeasurementPacket); + const char *lastSender = getSenderShortName(*lastMeasurementPacket); auto &p = lastMeasurementPacket->decoded; if (!pb_decode_from_bytes(p.payload.bytes, p.payload.size, Telemetry_fields, &lastMeasurement)) { @@ -213,16 +200,16 @@ void EnvironmentTelemetryModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiSt if (radioConfig.preferences.telemetry_module_environment_display_fahrenheit) { last_temp = String(CelsiusToFahrenheit(lastMeasurement.variant.environment_metrics.temperature), 0) + "°F"; } - display->drawString(x, y += fontHeight(FONT_MEDIUM) - 2, "From: " + lastSender + "(" + String(agoSecs) + "s)"); - display->drawString(x, y += fontHeight(FONT_SMALL) - 2,"Temp/Hum: " + last_temp + " / " + String(lastMeasurement.variant.environment_metrics.relative_humidity, 0) + "%"); + display->drawString(x, y += fontHeight(FONT_MEDIUM) - 2, "From: " + String(lastSender) + "(" + String(agoSecs) + "s)"); + display->drawString(x, y += fontHeight(FONT_SMALL) - 2, "Temp/Hum: " + last_temp + " / " + String(lastMeasurement.variant.environment_metrics.relative_humidity, 0) + "%"); if (lastMeasurement.variant.environment_metrics.barometric_pressure != 0) - display->drawString(x, y += fontHeight(FONT_SMALL),"Press: " + String(lastMeasurement.variant.environment_metrics.barometric_pressure, 0) + "hPA"); + display->drawString(x, y += fontHeight(FONT_SMALL), "Press: " + String(lastMeasurement.variant.environment_metrics.barometric_pressure, 0) + "hPA"); } bool EnvironmentTelemetryModule::handleReceivedProtobuf(const MeshPacket &mp, Telemetry *t) { if (t->which_variant == Telemetry_environment_metrics_tag) { - String sender = GetSenderName(mp); + const char *sender = getSenderShortName(mp); DEBUG_MSG("-----------------------------------------\n"); DEBUG_MSG("Environment Telemetry: Received data from %s\n", sender);