cli_gps: remove callbacks and add generic sensor set/get.

This commit is contained in:
Florent de Lamotte 2025-10-06 10:25:10 +02:00
parent e4f2d63b0a
commit 7be65c148e
4 changed files with 74 additions and 76 deletions

View file

@ -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);

View file

@ -2,6 +2,7 @@
#include "Mesh.h"
#include <helpers/IdentityStore.h>
#include <target.h>
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) { }