gps: move the gps CLI to GpsPrefs and convert all targets

This commit is contained in:
Florent committed 2026-11-08 08:47:34 -04:00
1 parent 81afafcb35
commit dea38936d7
69 files changed
+381 -301

No files matched your search

+2 -74
View File
@@ -349,65 +349,11 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
*(dp-1) = 0; // remove last CR
}
}
} else if (_prefs->getGpsPrefs()->handleCommand(command, *_sensors, reply)) {
if (_prefs->getGpsPrefs()->isDirty()) savePrefs();
} else if (memcmp(command, "region", 6) == 0) {
handleRegionCmd(command, reply);
#if ENV_INCLUDE_GPS == 1
} else if (memcmp(command, "gps on", 6) == 0) {
if (_sensors->setSettingValue("gps", "1")) {
_prefs->gps_enabled = 1;
savePrefs();
strcpy(reply, "ok");
} else {
strcpy(reply, "gps toggle not found");
}
} else if (memcmp(command, "gps off", 7) == 0) {
if (_sensors->setSettingValue("gps", "0")) {
_prefs->gps_enabled = 0;
savePrefs();
strcpy(reply, "ok");
} else {
strcpy(reply, "gps toggle not found");
}
} else if (memcmp(command, "gps period ", 11) == 0) {
char* end = nullptr;
unsigned long period = strtoul(command + 11, &end, 10);
if (command[11] == '\0' || *end != '\0' || period > 86400) {
strcpy(reply, "gps period must be 0-86400 seconds");
} else {
char period_value[11];
snprintf(period_value, sizeof(period_value), "%lu", period);
if (_sensors->setSettingValue("gps_period", period_value) &&
_sensors->setSettingValue("gps", "1")) {
_prefs->gps_period = (uint32_t)period;
_prefs->gps_enabled = 1;
savePrefs();
strcpy(reply, "ok");
} else {
strcpy(reply, "gps provider not found");
}
}
} else if (memcmp(command, "gps sync", 8) == 0) {
LocationProvider * l = _sensors->getLocationProvider();
if (l != NULL) {
l->syncTime();
strcpy(reply, "ok");
} else {
strcpy(reply, "gps provider not found");
}
} else if (strcmp(command, "gps diag") == 0) {
LocationProvider * l = _sensors->getLocationProvider();
if (l != NULL) {
const char* setting = _sensors->getSettingByKey("gps");
bool requested = setting != NULL && strcmp(setting, "1") == 0;
int prefix_len = snprintf(reply, 160, "req:%u ", requested ? 1U : 0U);
if (prefix_len < 0 || prefix_len >= 160) {
reply[159] = 0;
} else {
l->formatDiagnostics(reply + prefix_len, 160 - prefix_len);
}
} else {
strcpy(reply, "gps provider not found");
}
} else if (memcmp(command, "gps setloc", 10) == 0) {
_prefs->node_lat = _sensors->node_lat;
_prefs->node_lon = _sensors->node_lon;
@@ -443,24 +389,6 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
} else {
strcpy(reply, "error");
}
} else if (memcmp(command, "gps", 3) == 0) {
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(_sensors->getSettingByKey("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, "powersaving on", 14) == 0) {
#if defined(NRF52_PLATFORM)
+9 -3
View File
@@ -7,6 +7,7 @@
#include <helpers/RegionMap.h>
#include <helpers/ConfigSerializer.h>
#include <helpers/CommonRadioPrefs.h>
#include <helpers/CommonGpsPrefs.h>
#include <helpers/DynamicConfigSerializer.h>
#if defined(WITH_RS232_BRIDGE) || defined(WITH_ESPNOW_BRIDGE)
@@ -153,7 +154,7 @@ private:
};
BridgePrefs bridge;
class GPSPrefs : public ConfigSerializer {
class GPSPrefs : public CommonGpsPrefs {
NodePrefs* _parent;
protected:
void structure() override {
@@ -164,6 +165,10 @@ private:
}
public:
GPSPrefs(NodePrefs* parent) : _parent(parent) { }
bool isGpsEnabled() const override { return _parent->gps_enabled; }
void setGpsEnabled(bool enabled) override { _parent->gps_enabled = enabled; markDirty(); }
uint32_t getGpsPeriod() const override { return _parent->gps_period; }
void setGpsPeriod(uint32_t seconds) override { _parent->gps_period = seconds; markDirty(); }
};
GPSPrefs gps;
@@ -236,10 +241,11 @@ public:
}
CommonRadioPrefs* getRadioPrefs() { return &radio; }
CommonGpsPrefs* getGpsPrefs() { return &gps; }
KeyValueStore* getCustom() { return &custom; }
bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || custom.isDirty(); }
void clearDirty() override { ConfigSerializer::clearDirty(); radio.clearDirty(); custom.clearDirty(); }
bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || gps.isDirty() || custom.isDirty(); }
void clearDirty() override { ConfigSerializer::clearDirty(); radio.clearDirty(); gps.clearDirty(); custom.clearDirty(); }
};
class CommonCLICallbacks {
+110
View File
@@ -0,0 +1,110 @@
#include "CommonGpsPrefs.h"
#include <helpers/sensors/LocationProvider.h>
bool CommonGpsPrefs::setGpsEnabled(SensorManager& sensors, bool enabled) {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) return false;
location->setGpsOn(enabled);
setGpsEnabled(enabled);
return true;
}
bool CommonGpsPrefs::setGpsPeriod(SensorManager& sensors, uint32_t seconds) {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) return false;
location->setGpsPeriod(seconds);
setGpsPeriod(seconds);
return true;
}
bool CommonGpsPrefs::apply(SensorManager& sensors) const {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) return false;
location->setGpsPeriod(getGpsPeriod());
location->setGpsOn(isGpsEnabled());
return true;
}
bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, char* reply) {
if (strcmp(command, "gps") == 0) {
LocationProvider* location = sensors.getLocationProvider();
char mode[32];
if (getGpsPeriod() == 0) {
strcpy(mode, "tracking");
} else {
snprintf(mode, sizeof(mode), "periodic (%lus)",
(unsigned long)getGpsPeriod());
}
if (location == nullptr) {
strcpy(reply, "gps provider not found");
} else if (location->isEnabled()) {
sprintf(reply, "on, %s, %s, %d sats, mode: %s",
isGpsEnabled() ? "active" : "deactivated",
location->isValid() ? "fix" : "no fix",
location->satellitesCount(), mode);
} else {
snprintf(reply, 160, "off, mode: %s", mode);
}
return true;
}
if (memcmp(command, "gps on", 6) == 0) {
if (!setGpsEnabled(sensors, true)) {
strcpy(reply, "gps toggle not found");
} else {
strcpy(reply, "ok");
}
return true;
}
if (memcmp(command, "gps off", 7) == 0) {
if (!setGpsEnabled(sensors, false)) {
strcpy(reply, "gps toggle not found");
} else {
strcpy(reply, "ok");
}
return true;
}
if (strcmp(command, "gps period") == 0) {
if (getGpsPeriod() == 0) {
strcpy(reply, "> 0 (continuous)");
} else {
sprintf(reply, "> %lu", (unsigned long)getGpsPeriod());
}
return true;
}
if (memcmp(command, "gps period ", 11) == 0) {
char* end = nullptr;
unsigned long period = strtoul(command + 11, &end, 10);
if (command[11] == '\0' || *end != '\0' || period > 86400) {
strcpy(reply, "gps period must be 0-86400 seconds");
} else {
if (!setGpsPeriod(sensors, (uint32_t)period) ||
!setGpsEnabled(sensors, true)) {
strcpy(reply, "gps provider not found");
} else {
strcpy(reply, "ok");
}
}
return true;
}
if (memcmp(command, "gps sync", 8) == 0) {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) {
strcpy(reply, "gps provider not found");
} else {
location->syncTime();
strcpy(reply, "ok");
}
return true;
}
if (strcmp(command, "gps diag") == 0) {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) {
strcpy(reply, "gps provider not found");
} else {
int prefix = snprintf(reply, 160, "req:%u ", isGpsEnabled() ? 1U : 0U);
if (prefix >= 0 && prefix < 160) location->formatDiagnostics(reply + prefix, 160 - prefix);
}
return true;
}
return false;
}
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include "ConfigSerializer.h"
#include "SensorManager.h"
class CommonGpsPrefs : public ConfigSerializer {
bool _is_dirty = false;
protected:
CommonGpsPrefs() = default;
public:
void markDirty() { _is_dirty = true; }
void clearDirty() { _is_dirty = false; }
bool isDirty() const { return _is_dirty; }
virtual bool isGpsEnabled() const = 0;
virtual void setGpsEnabled(bool enabled) = 0;
virtual uint32_t getGpsPeriod() const = 0;
virtual void setGpsPeriod(uint32_t seconds) = 0;
bool setGpsEnabled(SensorManager& sensors, bool enabled);
bool setGpsPeriod(SensorManager& sensors, uint32_t seconds);
bool apply(SensorManager& sensors) const;
bool handleCommand(const char* command, SensorManager& sensors, char* reply);
};
@@ -650,7 +650,7 @@ bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, Cayen
int EnvironmentSensorManager::getNumSettings() const {
int settings = 0;
#if ENV_INCLUDE_GPS
if (gps_detected) settings += 2; // GPS enablement and periodic acquisition
if (gps_detected) settings += 1; // GPS enablement
#endif
return settings;
}
@@ -661,9 +661,6 @@ const char* EnvironmentSensorManager::getSettingName(int i) const {
if (gps_detected && i == settings++) {
return "gps";
}
if (gps_detected && i == settings++) {
return "gps_period";
}
#endif
return NULL;
}
@@ -674,12 +671,6 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const {
if (gps_detected && i == settings++) {
return _location->isGpsOn() ? "1" : "0";
}
if (gps_detected && i == settings++) {
static char period[11];
snprintf(period, sizeof(period), "%lu",
(unsigned long)_location->getGpsPeriod());
return period;
}
#endif
return NULL;
}
@@ -694,13 +685,6 @@ bool EnvironmentSensorManager::setSettingValue(const char* name, const char* val
}
return true;
}
if (gps_detected && strcmp(name, "gps_period") == 0) {
char* end = nullptr;
unsigned long period = strtoul(value, &end, 10);
if (*value == '\0' || *end != '\0' || period > 86400) return false;
_location->setGpsPeriod((uint32_t)period);
return true;
}
if (strcmp(name, "gps_interval") == 0) {
uint32_t interval_seconds = atoi(value);
gps_update_interval_sec = interval_seconds > 0 ? interval_seconds : 1;