From 7adff025fedb7d74c08c57fb77d55ca5f298e929 Mon Sep 17 00:00:00 2001 From: Ben Batt Date: Wed, 22 Jul 2026 21:12:14 +1000 Subject: [PATCH] Save room topic to a new "/room_prefs" file --- examples/simple_room_server/MyMesh.cpp | 46 ++++++++++++++++++++++++++ examples/simple_room_server/MyMesh.h | 8 ++--- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index 51e107b5e..faa59eaf4 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -714,6 +714,7 @@ void MyMesh::begin(FILESYSTEM *fs) { mesh::Mesh::begin(); _fs = fs; // load persisted prefs + loadRoomPrefs(); _cli.loadPrefs(_fs); acl.load(_fs, self_id); @@ -747,6 +748,10 @@ void MyMesh::begin(FILESYSTEM *fs) { updateAdvertTimer(); updateFloodAdvertTimer(); + if (strlen(topic) > 0) { + topic_timestamp = getRTCClock()->getCurrentTimeUnique(); + } + board.setAdcMultiplier(_prefs.adc_multiplier); #if ENV_INCLUDE_GPS == 1 @@ -754,6 +759,45 @@ void MyMesh::begin(FILESYSTEM *fs) { #endif } +#define PREFS_FILE_PATH "/room_prefs" + +void MyMesh::loadRoomPrefs() { +#if defined(RP2040_PLATFORM) + File file = _fs->open(PREFS_FILE_PATH, "r"); +#else + File file = _fs->open(PREFS_FILE_PATH); +#endif + if (file) { + file.read((uint8_t *)&topic[0], sizeof(topic)); // 0 + topic[MAX_TOPIC_TEXT_LEN] = '\0'; + // next: 144 + + file.close(); + } +} + +void MyMesh::saveRoomPrefs() { +#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) + _fs->remove(PREFS_FILE_PATH); + File file = _fs->open(PREFS_FILE_PATH, FILE_O_WRITE); +#elif defined(RP2040_PLATFORM) + File file = _fs->open(PREFS_FILE_PATH, "w"); +#else + File file = _fs->open(PREFS_FILE_PATH, "w", true); +#endif + if (file) { + file.write((uint8_t *)&topic[0], sizeof(topic)); // 0 + // next: 144 + + file.close(); + } +} + +void MyMesh::savePrefs() { + saveRoomPrefs(); + _cli.savePrefs(_fs); +} + void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) { if (scope.isNull()) { sendFlood(pkt, delay_millis, path_hash_size); @@ -964,6 +1008,8 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply strcpy(topic, new_topic); topic_timestamp = getRTCClock()->getCurrentTimeUnique(); + saveRoomPrefs(); + sprintf(reply, "New topic set with length %d", len); return; diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 73fe5ab23..398ffea7d 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -134,6 +134,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { File openAppend(const char* fname); int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len); + void loadRoomPrefs(); + void saveRoomPrefs(); + protected: float getAirtimeBudgetFactor() const override { return _prefs.airtime_factor; @@ -194,13 +197,10 @@ public: return &_prefs; } - void savePrefs() override { - _cli.savePrefs(_fs); - } - void sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size); // CommonCLICallbacks + void savePrefs() override; void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override; bool formatFileSystem() override; void sendSelfAdvertisement(int delay_millis, bool flood) override;