Remove old HAL components and refactored GPS-related code (#583)
- Added generic GPS/GNSS support with device detection, configuration, and persistent settings. - Improved device and module lifecycle management. - Added flexible filesystem locking support for displays and storage. - Improved display-idle and keyboard backlight handling. - Updated architecture, driver, module, testing, and licensing documentation. - Removed old HAL device and related code.
This commit is contained in:
committed by
GitHub
parent
29e80cfd65
commit
2a2558b29a
@@ -0,0 +1,57 @@
|
||||
#include <gps/gps.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
const char* gps_model_to_string(GpsModel model) {
|
||||
switch (model) {
|
||||
case GPS_MODEL_AG3335: return "AG3335";
|
||||
case GPS_MODEL_AG3352: return "AG3352";
|
||||
case GPS_MODEL_ATGM336H: return "ATGM336H";
|
||||
case GPS_MODEL_LS20031: return "LS20031";
|
||||
case GPS_MODEL_MTK: return "MTK";
|
||||
case GPS_MODEL_MTK_L76B: return "MTK_L76B";
|
||||
case GPS_MODEL_MTK_PA1616S: return "MTK_PA1616S";
|
||||
case GPS_MODEL_UBLOX6: return "UBLOX6";
|
||||
case GPS_MODEL_UBLOX7: return "UBLOX7";
|
||||
case GPS_MODEL_UBLOX8: return "UBLOX8";
|
||||
case GPS_MODEL_UBLOX9: return "UBLOX9";
|
||||
case GPS_MODEL_UBLOX10: return "UBLOX10";
|
||||
case GPS_MODEL_UC6580: return "UC6580";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
error_t gps_event_subscribe(Device* device, GpsSubscription* sub) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return static_cast<const GpsApi*>(driver->api)->event_subscribe(device, sub);
|
||||
}
|
||||
|
||||
error_t gps_event_unsubscribe(Device* device, GpsSubscription* sub) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return static_cast<const GpsApi*>(driver->api)->event_unsubscribe(device, sub);
|
||||
}
|
||||
|
||||
error_t gps_event_await(Device* device, GpsSubscription* sub, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return static_cast<const GpsApi*>(driver->api)->event_await(device, sub, timeout);
|
||||
}
|
||||
|
||||
GpsState gps_get_state(Device* device) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return static_cast<const GpsApi*>(driver->api)->get_state(device);
|
||||
}
|
||||
|
||||
error_t gps_get_model_name(Device* device, char* model_name, size_t buffer_size) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return static_cast<const GpsApi*>(driver->api)->get_model_name(device, model_name, buffer_size);
|
||||
}
|
||||
|
||||
const DeviceType GPS_TYPE {
|
||||
.name = "gps"
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,167 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <gps/private/gps_ledger.h>
|
||||
|
||||
#include <gps/gps.h>
|
||||
#include <gps/gps_settings.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
#include <vector>
|
||||
|
||||
constexpr auto* TAG = "gps_ledger";
|
||||
|
||||
struct GpsConfig {
|
||||
uint32_t baud_rate;
|
||||
enum GpsModel model;
|
||||
};
|
||||
|
||||
// A GPS_TYPE device the ledger constructed for a persisted GpsConfiguration. Device is the first
|
||||
// member so `reinterpret_cast<GpsLedgerEntry*>(device)` is safe when a Device* obtained from the
|
||||
// device tree needs to be freed.
|
||||
struct GpsLedgerEntry {
|
||||
Device device {};
|
||||
GpsConfig config {};
|
||||
// device->name points into this buffer - must outlive the device (device->name only stores a
|
||||
// pointer, it doesn't copy).
|
||||
char name[16] {};
|
||||
};
|
||||
|
||||
// Unique across every device the ledger creates, so device names ("gpsN") never collide.
|
||||
static uint32_t next_device_index = 0;
|
||||
|
||||
static bool device_matches_configuration(Device* device, const GpsConfiguration& configuration) {
|
||||
auto* parent = device_get_parent(device);
|
||||
if (parent == nullptr || strcmp(parent->name, configuration.uart_name) != 0) {
|
||||
return false;
|
||||
}
|
||||
const auto* config = static_cast<const GpsConfig*>(device->config);
|
||||
return config->baud_rate == configuration.baud_rate && config->model == configuration.model;
|
||||
}
|
||||
|
||||
// Constructs+adds (not started) a GPS_TYPE device wired to `configuration`'s named UART, tagged
|
||||
// DEVICE_FLAG_DYNAMIC so the ledger recognizes it as its own on a later sync.
|
||||
static bool create_device(const GpsConfiguration& configuration) {
|
||||
Device* uart = nullptr;
|
||||
if (device_get_by_name(configuration.uart_name, &uart) != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to find device %s", configuration.uart_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto* entry = new(std::nothrow) GpsLedgerEntry();
|
||||
if (entry == nullptr) {
|
||||
device_put(uart);
|
||||
return false;
|
||||
}
|
||||
entry->config = GpsConfig { .baud_rate = configuration.baud_rate, .model = configuration.model };
|
||||
snprintf(entry->name, sizeof(entry->name), "gps%u", (unsigned)next_device_index++);
|
||||
|
||||
auto* device = &entry->device;
|
||||
device->address = 0;
|
||||
device->name = entry->name;
|
||||
device->config = &entry->config;
|
||||
device->parent = nullptr;
|
||||
device->flags = DEVICE_FLAG_DYNAMIC;
|
||||
device->internal = nullptr;
|
||||
|
||||
bool ok = false;
|
||||
if (device_construct(device) == ERROR_NONE) {
|
||||
device_set_parent(device, uart);
|
||||
|
||||
Driver* driver = driver_find_compatible("tactility,gps-generic");
|
||||
if (driver != nullptr) {
|
||||
device_set_driver(device, driver);
|
||||
ok = device_add(device) == ERROR_NONE;
|
||||
if (!ok) {
|
||||
LOG_E(TAG, "Failed to add %s", device->name);
|
||||
}
|
||||
} else {
|
||||
LOG_E(TAG, "No driver registered for tactility,gps-generic");
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
device_destruct(device);
|
||||
}
|
||||
} else {
|
||||
LOG_E(TAG, "Failed to construct %s", device->name);
|
||||
}
|
||||
|
||||
device_put(uart);
|
||||
|
||||
if (!ok) {
|
||||
delete entry;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
// Stops (if needed), removes and destructs a ledger-owned device, and frees its entry.
|
||||
static void destroy_device(Device* device) {
|
||||
if (device_is_ready(device)) {
|
||||
device_stop(device);
|
||||
}
|
||||
device_remove(device);
|
||||
device_destruct(device);
|
||||
delete reinterpret_cast<GpsLedgerEntry*>(device);
|
||||
}
|
||||
|
||||
static bool is_ledger_owned(const Device* device) {
|
||||
return !(device->flags & DEVICE_FLAG_DTS) && (device->flags & DEVICE_FLAG_DYNAMIC);
|
||||
}
|
||||
|
||||
// Collects the ledger-owned GPS_TYPE devices. device_remove()/device_stop() must not run while
|
||||
// device_for_each_of_type() holds the device ledger lock, so callers process the result afterwards.
|
||||
static std::vector<Device*> collect_owned_devices() {
|
||||
std::vector<Device*> owned;
|
||||
device_for_each_of_type(&GPS_TYPE, &owned, [](Device* device, void* context) {
|
||||
if (is_ledger_owned(device)) {
|
||||
static_cast<std::vector<Device*>*>(context)->push_back(device);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return owned;
|
||||
}
|
||||
|
||||
void gps_ledger_sync() {
|
||||
std::vector<GpsConfiguration> configurations;
|
||||
gps_settings_for_each_configuration(&configurations, [](const GpsConfiguration* configuration, size_t, void* context) {
|
||||
static_cast<std::vector<GpsConfiguration>*>(context)->push_back(*configuration);
|
||||
});
|
||||
|
||||
std::vector<bool> matched(configurations.size(), false);
|
||||
std::vector<Device*> stale;
|
||||
|
||||
for (auto* device : collect_owned_devices()) {
|
||||
bool found = false;
|
||||
for (size_t i = 0; i < configurations.size(); i++) {
|
||||
if (!matched[i] && device_matches_configuration(device, configurations[i])) {
|
||||
matched[i] = true;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
stale.push_back(device);
|
||||
}
|
||||
}
|
||||
|
||||
// Configuration disappeared - stop, destruct and drop the device that was created for it.
|
||||
for (auto* device : stale) {
|
||||
destroy_device(device);
|
||||
}
|
||||
|
||||
// New configuration - create a (not started) device for it.
|
||||
for (size_t i = 0; i < configurations.size(); i++) {
|
||||
if (!matched[i]) {
|
||||
create_device(configurations[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gps_ledger_clear() {
|
||||
for (auto* device : collect_owned_devices()) {
|
||||
destroy_device(device);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <gps/gps_settings.h>
|
||||
#include <gps/private/gps_ledger.h>
|
||||
|
||||
#include <tactility/filesystem/file_mutex.h>
|
||||
#include <tactility/log.h>
|
||||
#include <tactility/service/service_paths.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
constexpr auto* TAG = "gps_settings";
|
||||
|
||||
// Storage key for the persisted configuration file (services would use their own service ID for
|
||||
// this; gps_settings has no service backing it, so it defines its own).
|
||||
constexpr auto* GPS_SETTINGS_STORAGE_ID = "gps";
|
||||
|
||||
// region Configuration persistence
|
||||
|
||||
// Recursively creates every missing directory component of `path` (best-effort - mkdir() failures
|
||||
// other than "already exists" are surfaced later, when the actual config file open fails).
|
||||
static void ensure_directory_exists(const char* path) {
|
||||
char buffer[224];
|
||||
std::strncpy(buffer, path, sizeof(buffer) - 1);
|
||||
buffer[sizeof(buffer) - 1] = '\0';
|
||||
|
||||
for (char* p = buffer + 1; *p != '\0'; p++) {
|
||||
if (*p == '/') {
|
||||
*p = '\0';
|
||||
mkdir(buffer, 0777);
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
mkdir(buffer, 0777);
|
||||
}
|
||||
|
||||
static bool get_configuration_path(char* out_path, size_t out_path_size) {
|
||||
return service_paths_get_user_data_path(GPS_SETTINGS_STORAGE_ID, "config.bin", out_path, out_path_size) == ERROR_NONE;
|
||||
}
|
||||
|
||||
// Holds the lock (if any) that `path` needs for the lifetime of the guard - see file_find_lock().
|
||||
class FileLockGuard {
|
||||
FileMutex mutex;
|
||||
bool locked;
|
||||
public:
|
||||
explicit FileLockGuard(const char* path) {
|
||||
file_mutex_get(&mutex, path);
|
||||
file_mutex_lock(&mutex);
|
||||
locked = true;
|
||||
}
|
||||
|
||||
~FileLockGuard() {
|
||||
unlock();
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
if (locked) {
|
||||
file_mutex_unlock(&mutex);
|
||||
locked = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void gps_settings_for_each_configuration(void* context, void (*on_configuration)(const GpsConfiguration* configuration, size_t index, void* context)) {
|
||||
char path[224];
|
||||
if (!get_configuration_path(path, sizeof(path))) {
|
||||
return;
|
||||
}
|
||||
|
||||
FileLockGuard lock(path);
|
||||
|
||||
FILE* file = fopen(path, "rb");
|
||||
if (file == nullptr) {
|
||||
return; // No configurations saved yet
|
||||
}
|
||||
|
||||
GpsConfiguration configuration;
|
||||
size_t index = 0;
|
||||
while (fread(&configuration, sizeof(configuration), 1, file) == 1) {
|
||||
on_configuration(&configuration, index, context);
|
||||
index++;
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
static void collect_configuration(const GpsConfiguration* configuration, size_t, void* context) {
|
||||
static_cast<std::vector<GpsConfiguration>*>(context)->push_back(*configuration);
|
||||
}
|
||||
|
||||
static void load_configurations(std::vector<GpsConfiguration>& out) {
|
||||
gps_settings_for_each_configuration(&out, collect_configuration);
|
||||
}
|
||||
|
||||
static error_t write_configurations(const std::vector<GpsConfiguration>& configurations) {
|
||||
char directory[224];
|
||||
if (service_paths_get_user_data_directory(GPS_SETTINGS_STORAGE_ID, directory, sizeof(directory)) != ERROR_NONE) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
char path[256];
|
||||
if (!get_configuration_path(path, sizeof(path))) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
FileLockGuard lock(path);
|
||||
|
||||
ensure_directory_exists(directory);
|
||||
|
||||
FILE* file = fopen(path, "wb");
|
||||
if (file == nullptr) {
|
||||
LOG_E(TAG, "Failed to open %s for writing", path);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
for (auto& configuration : configurations) {
|
||||
if (fwrite(&configuration, sizeof(configuration), 1, file) != 1) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
if (!ok) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
gps_ledger_sync();
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
error_t gps_settings_add_configuration(const GpsConfiguration* configuration) {
|
||||
std::vector<GpsConfiguration> configurations;
|
||||
load_configurations(configurations);
|
||||
configurations.push_back(*configuration);
|
||||
|
||||
error_t error = write_configurations(configurations);
|
||||
if (error != ERROR_NONE) {
|
||||
return error;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t gps_settings_remove_configuration_at(size_t index) {
|
||||
std::vector<GpsConfiguration> configurations;
|
||||
load_configurations(configurations);
|
||||
|
||||
if (index >= configurations.size()) {
|
||||
return ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
configurations.erase(configurations.begin() + static_cast<ptrdiff_t>(index));
|
||||
|
||||
error_t error = write_configurations(configurations);
|
||||
if (error != ERROR_NONE) {
|
||||
return error;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <gps/gps_module.h>
|
||||
#include <gps/private/gps_ledger.h>
|
||||
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
static error_t start() {
|
||||
// Materializes devices for configurations persisted in previous sessions.
|
||||
gps_ledger_sync();
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop() {
|
||||
gps_ledger_clear();
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
Module gps_module = {
|
||||
.name = "gps",
|
||||
.start = start,
|
||||
.stop = stop,
|
||||
.drivers = nullptr,
|
||||
.symbols = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user