diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index e5a8b6b3..df945d45 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -758,62 +758,6 @@ void MyMesh::removeNeighbor(const uint8_t *pubkey, int key_len) { #endif } -void MyMesh::gpsGetStatus(char * reply) { - LocationProvider * l = sensors.getLocationProvider(); - if (l != NULL) { - bool enabled = l->isEnabled(); // is EN pin on ? - bool active = gpsGetState(); // is enabled at SensorManager level ? - bool fix = l->isValid(); // has fix ? - int sats = l->satellitesCount(); - if (enabled) { - sprintf(reply, "on, %s, %s, %d sats", - active?"active":"deactivated", - fix?"fix":"no fix", - sats); - } else { - strcpy(reply, "off"); - } - } else { - strcpy(reply, "Can't find GPS"); - } -} - -bool MyMesh::gpsGetState() { - int num = sensors.getNumSettings(); - for (int i = 0; i < num; i++) { - if (strcmp(sensors.getSettingName(i), "gps") == 0) { - return !strcmp(sensors.getSettingValue(i), "1"); - } - } - return false; -} - -void MyMesh::gpsSetState(bool value) { - // toggle GPS on/off - int num = sensors.getNumSettings(); - for (int i = 0; i < num; i++) { - if (strcmp(sensors.getSettingName(i), "gps") == 0) { - sensors.setSettingValue("gps", value?"1":"0"); - break; - } - } -} - -void MyMesh::gpsStart() { - gpsSetState(true); -} - -void MyMesh::gpsStop() { - gpsSetState(false); -} - -void MyMesh::gpsSyncTime() { - LocationProvider * l = sensors.getLocationProvider(); - if (l != NULL) { - l->syncTime(); - } -} - void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) { self_id = new_id; #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 880d1043..05a8d13b 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -142,9 +142,6 @@ protected: void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override; bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override; - bool gpsGetState(); - void gpsSetState(bool value); - public: MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables); @@ -179,12 +176,6 @@ public: void formatNeighborsReply(char *reply) override; void removeNeighbor(const uint8_t* pubkey, int key_len) override; - // Gps mgmt cli callbacks - void gpsGetStatus(char * reply) override; - void gpsStart() override; - void gpsStop() override; - void gpsSyncTime() override; - mesh::LocalIdentity& getSelfId() override { return self_id; } void saveIdentity(const mesh::LocalIdentity& new_id) override; diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index e674cbc2..ee029ec3 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -128,6 +128,27 @@ void CommonCLI::savePrefs() { _callbacks->savePrefs(); } +const char* CommonCLI::sensorGetCustomVar(const char* key) { + int num = sensors.getNumSettings(); + for (int i = 0; i < num; i++) { + if (strcmp(sensors.getSettingName(i), key) == 0) { + return sensors.getSettingValue(i); + } + } + return NULL; +} + +bool CommonCLI::sensorSetCustomVar(const char* key, const char* value) { + int num = sensors.getNumSettings(); + for (int i = 0; i < num; i++) { + if (strcmp(sensors.getSettingName(i), key) == 0) { + sensors.setSettingValue(key, value); + return true; + } + } + return false; +} + void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, char* reply) { if (memcmp(command, "reboot", 6) == 0) { _board->reboot(); // doesn't return @@ -401,18 +422,60 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch sprintf(reply, "%s (Build: %s)", _callbacks->getFirmwareVer(), _callbacks->getBuildDate()); } else if (memcmp(command, "board", 5) == 0) { sprintf(reply, "%s", _board->getManufacturerName()); + } else if (memcmp(command, "sensor get ", 11) == 0) { + const char* key = command + 11; + const char* val = sensorGetCustomVar(key); + if (val != NULL) { + strcpy(reply, val); + } else { + strcpy(reply, "can't find custom var"); + } + } else if (memcmp(command, "sensor set ", 11) == 0) { + const char* args = &command[11]; + const char* value = strchr(args,' ') + 1; + char key [value-args+1]; + strncpy(key, args, value-args-1); + if (sensorSetCustomVar(key, value)) { + strcpy(reply, "ok"); + } else { + strcpy(reply, "can't find custom var"); + } #if ENV_INCLUDE_GPS == 1 } else if (memcmp(command, "gps on", 6) == 0) { - _callbacks->gpsStart(); - strcpy(reply, "ok"); + if (sensorSetCustomVar("gps", "1")) { + strcpy(reply, "ok"); + } else { + strcpy(reply, "gps toggle not found"); + } } else if (memcmp(command, "gps off", 7) == 0) { - _callbacks->gpsStop(); - strcpy(reply, "ok"); + if (sensorSetCustomVar("gps", "0")) { + strcpy(reply, "ok"); + } else { + strcpy(reply, "gps toggle not found"); + } } else if (memcmp(command, "gps sync", 8) == 0) { - _callbacks->gpsSyncTime(); - strcpy(reply, "Waiting fix ..."); + LocationProvider * l = sensors.getLocationProvider(); + if (l != NULL) { + l->syncTime(); + } } else if (memcmp(command, "gps", 3) == 0) { - _callbacks->gpsGetStatus(reply); + LocationProvider * l = sensors.getLocationProvider(); + if (l != NULL) { + bool enabled = l->isEnabled(); // is EN pin on ? + bool fix = l->isValid(); // has fix ? + int sats = l->satellitesCount(); + bool active = !strcmp(sensorGetCustomVar("gps"), "1"); + if (enabled) { + sprintf(reply, "on, %s, %s, %d sats", + active?"active":"deactivated", + fix?"fix":"no fix", + sats); + } else { + strcpy(reply, "off"); + } + } else { + strcpy(reply, "Can't find GPS"); + } #endif } else if (memcmp(command, "log start", 9) == 0) { _callbacks->setLoggingOn(true); diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 08e5f988..d3e3a19d 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -2,6 +2,7 @@ #include "Mesh.h" #include +#include struct NodePrefs { // persisted to file float airtime_factor; @@ -50,10 +51,6 @@ public: virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0; virtual void clearStats() = 0; virtual void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) = 0; - virtual void gpsGetStatus(char * reply) {} - virtual void gpsStart() {} - virtual void gpsStop() {} - virtual void gpsSyncTime() {} }; class CommonCLI { @@ -67,6 +64,9 @@ class CommonCLI { void savePrefs(); void loadPrefsInt(FILESYSTEM* _fs, const char* filename); + const char* sensorGetCustomVar(const char* key); + bool sensorSetCustomVar(const char* key, const char* value); + public: CommonCLI(mesh::MainBoard& board, mesh::RTCClock& rtc, NodePrefs* prefs, CommonCLICallbacks* callbacks) : _board(&board), _rtc(&rtc), _prefs(prefs), _callbacks(callbacks) { }