From 339ee035aaab998d39e87632d2bfb80a1e8c97a7 Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Sun, 13 Jul 2025 15:30:49 +1000 Subject: [PATCH] * simple_sensor: handleCustomCommand() hook --- examples/simple_sensor/SensorMesh.cpp | 7 ++++++- examples/simple_sensor/SensorMesh.h | 1 + examples/simple_sensor/main.cpp | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index d542407d..2943fd90 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -257,7 +257,7 @@ uint8_t SensorMesh::handleRequest(uint16_t perms, uint32_t sender_timestamp, uin memcpy(&start_secs_ago, &payload[0], 4); memcpy(&end_secs_ago, &payload[4], 4); uint8_t res1 = payload[8]; // reserved for future (extra query params) - uint8_t res2 = payload[8]; + uint8_t res2 = payload[9]; MinMaxAvg data[8]; int n; @@ -460,6 +460,11 @@ void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* r command += 3; } + // first, see if this is a custom-handled CLI command (ie. in main.cpp) + if (handleCustomCommand(sender_timestamp, command, reply)) { + return; // command has been handled + } + // handle sensor-specific CLI commands if (memcmp(command, "setperm ", 8) == 0) { // format: setperm {pubkey-hex} {permissions-int16} char* hex = &command[8]; diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index e0527fa0..6edbfc26 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -118,6 +118,7 @@ protected: virtual void onSensorDataRead() = 0; // for app to implement virtual int querySeriesData(uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg dest[], int max_num) = 0; // for app to implement + virtual bool handleCustomCommand(uint32_t sender_timestamp, char* command, char* reply) { return false; } // Mesh overrides float getAirtimeBudgetFactor() const override; diff --git a/examples/simple_sensor/main.cpp b/examples/simple_sensor/main.cpp index d1dd67c5..c9e282a2 100644 --- a/examples/simple_sensor/main.cpp +++ b/examples/simple_sensor/main.cpp @@ -30,6 +30,14 @@ protected: battery_data.calcMinMaxAvg(getRTCClock(), start_secs_ago, end_secs_ago, &dest[0], TELEM_CHANNEL_SELF, LPP_VOLTAGE); return 1; } + + bool handleCustomCommand(uint32_t sender_timestamp, char* command, char* reply) override { + if (strcmp(command, "magic") == 0) { // example 'custom' command handling + strcpy(reply, "**Magic now done**"); + return true; // handled + } + return false; // not handled + } /* ======================================================================= */ };