New kernel drivers, filesystem API, and more (#513)

* **New Features**
  * BMI270 6-axis IMU driver added; new unified filesystem abstraction for mounted filesystems.
  * Public Wi‑Fi API surface (no implementation yet)
  * SDMMC driver added (kernel drive$)
  * expanded GPIO interrupt/callback support
* **Improvements**
  * M5Stack Tab5: revamped GPIO/power initialization and IMU integration.
  * LVGL updates including device fontSize configuration.
  * Updated all code related to SD card device/fs handling
  * Rename LilyGO T-HMI S3 to LilyGO T-HMI
* **Bug Fixes**
  * Simplified and consolidated SD card handling and mount discovery.
This commit is contained in:
Ken Van Hoeylandt
2026-03-07 16:13:39 +01:00
committed by GitHub
parent 2de35b2d2d
commit aa7530e515
88 changed files with 2792 additions and 846 deletions
@@ -7,10 +7,9 @@ extern "C" {
#endif
#include <tactility/device.h>
#include <stdbool.h>
#include <stdint.h>
#define GPIO_FLAGS_MASK 0x1f
#define GPIO_FLAGS_MASK 0xff
#define GPIO_PIN_NONE -1
@@ -27,6 +26,7 @@ extern "C" {
#define GPIO_FLAG_INTERRUPT_BITMASK (0b111 << 5) // 3 bits to hold the values [0, 5]
#define GPIO_FLAG_INTERRUPT_FROM_OPTIONS(options) (gpio_int_type_t)((options & GPIO_FLAG_INTERRUPT_BITMASK) >> 5)
#define GPIO_FLAG_INTERRUPT_TO_OPTIONS(options, interrupt) (options | (interrupt << 5))
#define GPIO_FLAG_HIGH_IMPEDANCE (1 << 8)
typedef enum {
GPIO_INTERRUPT_DISABLE = 0,
@@ -52,6 +52,36 @@ struct GpioControllerApi {
* @return ERROR_NONE if successful
*/
error_t (*get_native_pin_number)(struct GpioDescriptor* descriptor, void* pin_number);
/**
* @brief Adds a callback to be called when a GPIO interrupt occurs.
* @param[in] descriptor the pin descriptor
* @param[in] callback the callback function
* @param[in] arg the argument to pass to the callback
* @return ERROR_NONE if successful, ERROR_NOT_SUPPORTED if not implemented
*/
error_t (*add_callback)(struct GpioDescriptor* descriptor, void (*callback)(void*), void* arg);
/**
* @brief Removes a callback from a GPIO interrupt.
* @param[in] descriptor the pin descriptor
* @return ERROR_NONE if successful, ERROR_NOT_SUPPORTED if not implemented
*/
error_t (*remove_callback)(struct GpioDescriptor* descriptor);
/**
* @brief Enables the interrupt for a GPIO pin.
* @param[in] descriptor the pin descriptor
* @return ERROR_NONE if successful, ERROR_NOT_SUPPORTED if not implemented
*/
error_t (*enable_interrupt)(struct GpioDescriptor* descriptor);
/**
* @brief Disables the interrupt for a GPIO pin.
* @param[in] descriptor the pin descriptor
* @return ERROR_NONE if successful, ERROR_NOT_SUPPORTED if not implemented
*/
error_t (*disable_interrupt)(struct GpioDescriptor* descriptor);
};
struct GpioDescriptor* gpio_descriptor_acquire(
@@ -118,6 +148,36 @@ error_t gpio_descriptor_get_flags(struct GpioDescriptor* descriptor, gpio_flags_
*/
error_t gpio_descriptor_get_native_pin_number(struct GpioDescriptor* descriptor, void* pin_number);
/**
* @brief Adds a callback to be called when a GPIO interrupt occurs.
* @param[in] descriptor the pin descriptor
* @param[in] callback the callback function
* @param[in] arg the argument to pass to the callback
* @return ERROR_NONE if successful, ERROR_NOT_SUPPORTED if not implemented
*/
error_t gpio_descriptor_add_callback(struct GpioDescriptor* descriptor, void (*callback)(void*), void* arg);
/**
* @brief Removes a callback from a GPIO interrupt.
* @param[in] descriptor the pin descriptor
* @return ERROR_NONE if successful, ERROR_NOT_SUPPORTED if not implemented
*/
error_t gpio_descriptor_remove_callback(struct GpioDescriptor* descriptor);
/**
* @brief Enables the interrupt for a GPIO pin.
* @param[in] descriptor the pin descriptor
* @return ERROR_NONE if successful, ERROR_NOT_SUPPORTED if not implemented
*/
error_t gpio_descriptor_enable_interrupt(struct GpioDescriptor* descriptor);
/**
* @brief Disables the interrupt for a GPIO pin.
* @param[in] descriptor the pin descriptor
* @return ERROR_NONE if successful, ERROR_NOT_SUPPORTED if not implemented
*/
error_t gpio_descriptor_disable_interrupt(struct GpioDescriptor* descriptor);
/**
* @brief Gets the number of pins supported by the controller.
* @param[in] device the GPIO controller device
@@ -141,6 +201,15 @@ error_t gpio_controller_init_descriptors(struct Device* device, uint32_t pin_cou
*/
error_t gpio_controller_deinit_descriptors(struct Device* device);
/**
* Unlike other drivers, a GPIO controller's internal data is created and set by gpio_controller_init_descriptors()
* This means that the specific controller implementation cannot set the device's driver data, as it's already set by the GPIO controller base coded
* When calling init descriptors, the caller can pass a controller_context, which is an optional pointer that holds the implementation's internal data.
* @param device the GPIO controller device
* @return the context void pointer
*/
void* gpio_controller_get_controller_context(struct Device* device);
extern const struct DeviceType GPIO_CONTROLLER_TYPE;
#ifdef __cplusplus
@@ -11,6 +11,11 @@ struct GpioDescriptor {
gpio_pin_t pin;
/** @brief Current owner */
enum GpioOwnerType owner_type;
/** @brief Implementation-specific context (e.g. from esp32 controller internally) */
/**
* @brief Implementation-specific context (e.g. from esp32 controller internally)
* Unlike other drivers, a GPIO controller's internal data is created and set by gpio_controller_init_descriptors()
* This means that the specific controller implementation cannot set the device's driver data, as it's already set by the GPIO controller base coded.
* When calling init descriptors, the caller can pass a controller_context, which is an optional pointer that holds the implementation's internal data.
*/
void* controller_context;
};
@@ -0,0 +1,205 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tactility/error.h>
#ifdef __cplusplus
extern "C" {
#endif
struct Device;
enum WifiAuthenticationType {
WIFI_AUTHENTICATION_TYPE_OPEN = 0,
WIFI_AUTHENTICATION_TYPE_WEP,
WIFI_AUTHENTICATION_TYPE_WPA_PSK,
WIFI_AUTHENTICATION_TYPE_WPA2_PSK,
WIFI_AUTHENTICATION_TYPE_WPA_WPA2_PSK,
WIFI_AUTHENTICATION_TYPE_WPA2_ENTERPRISE,
WIFI_AUTHENTICATION_TYPE_WPA3_PSK,
WIFI_AUTHENTICATION_TYPE_WPA2_WPA3_PSK,
WIFI_AUTHENTICATION_TYPE_WAPI_PSK,
WIFI_AUTHENTICATION_TYPE_OWE,
WIFI_AUTHENTICATION_TYPE_WPA3_ENT_192,
WIFI_AUTHENTICATION_TYPE_WPA3_EXT_PSK,
WIFI_AUTHENTICATION_TYPE_WPA3_EXT_PSK_MIXED_MODE,
WIFI_AUTHENTICATION_TYPE_MAX
};
struct WifiApRecord {
char ssid[33]; // 32 bytes + null terminator
int8_t rssi;
int32_t channel;
enum WifiAuthenticationType authentication_type;
};
enum WifiRadioState {
WIFI_RADIO_STATE_OFF,
WIFI_RADIO_STATE_ON_PENDING,
WIFI_RADIO_STATE_ON,
WIFI_RADIO_STATE_OFF_PENDING,
};
enum WifiStationState {
WIFI_STATION_STATE_DISCONNECTED,
WIFI_STATION_STATE_CONNECTION_PENDING,
WIFI_STATION_STATE_CONNECTED
};
enum WifiAccessPointState {
WIFI_ACCESS_POINT_STATE_STARTED,
WIFI_ACCESS_POINT_STATE_STOPPED,
};
enum WifiEventType {
/** Radio state changed */
WIFI_EVENT_TYPE_RADIO_STATE_CHANGED,
/** WifiStationState changed */
WIFI_EVENT_TYPE_STATION_STATE_CHANGED,
/** WifiAccessPointState changed */
WIFI_EVENT_TYPE_STATION_CONNECTION_RESULT,
/** WifiAccessPointState changed */
WIFI_EVENT_TYPE_ACCESS_POINT_STATE_CHANGED,
/** Started scanning for access points */
WIFI_EVENT_TYPE_SCAN_STARTED,
/** Finished scanning for access points */
WIFI_EVENT_TYPE_SCAN_FINISHED,
};
enum WifiStationConnectionError {
WIFI_STATION_CONNECTION_ERROR_NONE,
/** Wrong password */
WIFI_STATION_CONNECTION_ERROR_WRONG_CREDENTIALS,
/** Failed to connect in a timely manner */
WIFI_STATION_CONNECTION_ERROR_TIMEOUT,
/** SSID not found */
WIFI_STATION_CONNECTION_ERROR_TARGET_NOT_FOUND,
};
struct WifiEvent {
enum WifiEventType type;
union {
enum WifiRadioState radio_state;
enum WifiStationState station_state;
enum WifiAccessPointState access_point_state;
enum WifiStationConnectionError connection_error;
};
};
typedef void (*WifiEventCallback)(struct Device* device, void* callback_context, struct WifiEvent event);
struct WifiApi {
/**
* Get the radio state of the device.
* @param[in] device the wifi device
* @param[out] state the radio state
* @return ERROR_NONE on success
*/
error_t (*get_radio_state)(struct Device* device, enum WifiRadioState* state);
/**
* Get the station state of the device.
* @param[in] device the wifi device
* @param[out] state the station state
* @return ERROR_NONE on success
*/
error_t (*get_station_state)(struct Device* device, enum WifiStationState* state);
/**
* Get the access point state of the device.
* @param[in] device the wifi device
* @param[out] state the access point state
* @return ERROR_NONE on success
*/
error_t (*get_access_point_state)(struct Device* device, enum WifiAccessPointState* state);
/**
* Check if the device is currently scanning for access points.
* @param[in] device the wifi device
* @return true when scanning
*/
bool (*is_scanning)(struct Device* device);
/**
* Start a scan for access points.
* @param[in] device the wifi device
* @return ERROR_NONE on success
*/
error_t (*scan)(struct Device* device);
/**
* Get the scan results of the device.
* @param[in] device the wifi device
* @param[out] results the buffer to store the scan results
* @param[in, out] num_results the number of scan results: it's first used as input to determine the size of the buffer, and then as output to get the actual number of results
* @return ERROR_NONE on success
*/
error_t (*get_scan_results)(struct Device* device, struct WifiApRecord* results, size_t* num_results);
/**
* Get the IPv4 address of the device.
* @param[in] device the device
* @param[out] ipv4 the buffer to store the IPv4 address (must be at least 16 bytes, will be null-terminated)
* @return ERROR_NONE on success
*/
error_t (*station_get_ipv4_address)(struct Device* device, char* ipv4);
/**
* Get the SSID of the access point the device is currently connected to.
* @param[in] device the wifi device
* @param[out] ssid the buffer to store the SSID (must be at least 33 bytes, will be null-terminated)
* @return ERROR_NONE on success
*/
error_t (*station_get_target_ssid)(struct Device* device, char* ssid);
/**
* Connect to an access point.
* @param[in] device the wifi device
* @param[in] ssid the SSID of the access point
* @param[in] password the password of the access point
* @param[in] channel the Wi-Fi channel to connect to (0 means "any" / no preference)
* @return ERROR_NONE on success
*/
error_t (*station_connect)(struct Device* device, const char* ssid, const char* password, int32_t channel);
/**
* Disconnect from the current access point.
* @param[in] device the wifi device
* @return ERROR_NONE on success
*/
error_t (*station_disconnect)(struct Device* device);
/**
* Get the RSSI of the current access point.
* @param[in] device the wifi device
* @param[out] rssi the buffer to store the RSSI
* @return ERROR_NONE on success
*/
error_t (*station_get_rssi)(struct Device* device, int32_t* rssi);
/**
* Add a WifiEvent callback.
* @param[in] device the wifi device
* @param[in] callback_context the context to pass to the callback
* @param[in] callback the callback function
* @return ERROR_NONE on success
*/
error_t (*add_event_callback)(struct Device* device, void* callback_context, WifiEventCallback callback);
/**
* Remove a WifiEvent callback.
* @param[in] device the wifi device
* @param[in] callback the callback function
* @return ERROR_NONE on success
*/
error_t (*remove_event_callback)(struct Device* device, WifiEventCallback callback);
};
extern const struct DeviceType WIFI_TYPE;
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <tactility/error.h>
#ifdef __cplusplus
extern "C" {
#endif
struct FileSystem;
/**
* @brief File system API.
*
* Provides a set of function pointers to interact with a specific file system implementation.
*/
struct FileSystemApi {
/**
* @brief Mounts the file system.
* @param[in] data file system private data
* @return ERROR_NONE on success, or an error code
*/
error_t (*mount)(void* data);
/**
* @brief Unmounts the file system.
* @param[in] data file system private data
* @return ERROR_NONE on success, or an error code
*/
error_t (*unmount)(void* data);
/**
* @brief Checks if the file system is mounted.
* @param[in] data file system private data
* @return true if mounted, false otherwise
*/
bool (*is_mounted)(void* data);
/**
* @brief Gets the mount path.
* @param[in] data file system private data
* @param[out] out_path buffer to store the path
* @param[in] out_path_size size of the output buffer
* @return ERROR_NONE on success, or an error code
*/
error_t (*get_path)(void* data, char* out_path, size_t out_path_size);
};
/**
* @brief Registers a new file system.
* @param[in] fs_api the file system API implementation
* @param[in] data private data for the file system
* @return the registered FileSystem object
*/
struct FileSystem* file_system_add(const struct FileSystemApi* fs_api, void* data);
/**
* @brief Removes a registered file system.
* @note The file system must be unmounted before removal.
* @param[in] fs the FileSystem object to remove
*/
void file_system_remove(struct FileSystem* fs);
/**
* @brief Iterates over all registered file systems.
* @param[in] callback_context context passed to the callback
* @param[in] callback function called for each file system. Return true to continue, false to stop.
*/
void file_system_for_each(void* callback_context, bool (*callback)(struct FileSystem* fs, void* context));
/**
* @brief Mounts the file system.
* @param[in] fs the FileSystem object
* @return ERROR_NONE on success, or an error code
*/
error_t file_system_mount(struct FileSystem* fs);
/**
* @warning Unmounting can fail (e.g. when the device is busy), so you might need to retry it.
* @brief Unmounts the file system.
* @param[in] fs the FileSystem object
* @return ERROR_NONE on success, or an error code
*/
error_t file_system_unmount(struct FileSystem* fs);
/**
* @brief Checks if the file system is mounted.
* @param[in] fs the FileSystem object
* @return true if mounted, false otherwise
*/
bool file_system_is_mounted(struct FileSystem* fs);
/**
* @brief Gets the mount path.
* @param[in] fs the FileSystem object
* @param[out] out_path buffer to store the path
* @param[in] out_path_size size of the output buffer
* @return ERROR_NONE on success, or an error code
*/
error_t file_system_get_path(struct FileSystem* fs, char* out_path, size_t out_path_size);
#ifdef __cplusplus
}
#endif
@@ -118,6 +118,7 @@ error_t module_construct_add_start(struct Module* module);
/**
* @brief Check if the module is started.
* Can be used when module isn't constructed yet.
* @param module module to check
* @return true if the module is started, false otherwise
*/
@@ -14,22 +14,25 @@
extern "C" {
struct GpioControllerData {
struct Mutex mutex {};
Mutex mutex {};
uint32_t pin_count;
struct GpioDescriptor* descriptors = nullptr;
GpioDescriptor* descriptors = nullptr;
void* controller_context;
explicit GpioControllerData(uint32_t pin_count) : pin_count(pin_count) {
explicit GpioControllerData(
uint32_t pin_count, void* controller_context
) : pin_count(pin_count), controller_context(controller_context) {
mutex_construct(&mutex);
}
error_t init_descriptors(Device* device, void* controller_context) {
descriptors = (struct GpioDescriptor*)calloc(pin_count, sizeof(struct GpioDescriptor));
error_t init_descriptors(Device* device) {
descriptors = static_cast<GpioDescriptor*>(calloc(pin_count, sizeof(GpioDescriptor)));
if (!descriptors) return ERROR_OUT_OF_MEMORY;
for (uint32_t i = 0; i < pin_count; ++i) {
descriptors[i].controller = device;
descriptors[i].pin = (gpio_pin_t)i;
descriptors[i].pin = static_cast<gpio_pin_t>(i);
descriptors[i].owner_type = GPIO_OWNER_NONE;
descriptors[i].controller_context = controller_context;
descriptors[i].controller_context = this->controller_context;
}
return ERROR_NONE;
}
@@ -42,14 +45,14 @@ struct GpioControllerData {
}
};
struct GpioDescriptor* gpio_descriptor_acquire(
struct Device* controller,
GpioDescriptor* gpio_descriptor_acquire(
Device* controller,
gpio_pin_t pin_number,
enum GpioOwnerType owner
GpioOwnerType owner
) {
check(owner != GPIO_OWNER_NONE);
auto* data = (struct GpioControllerData*)device_get_driver_data(controller);
auto* data = static_cast<struct GpioControllerData*>(device_get_driver_data(controller));
mutex_lock(&data->mutex);
if (pin_number >= data->pin_count) {
@@ -57,7 +60,7 @@ struct GpioDescriptor* gpio_descriptor_acquire(
return nullptr;
}
struct GpioDescriptor* desc = &data->descriptors[pin_number];
GpioDescriptor* desc = &data->descriptors[pin_number];
if (desc->owner_type != GPIO_OWNER_NONE) {
mutex_unlock(&data->mutex);
return nullptr;
@@ -69,22 +72,27 @@ struct GpioDescriptor* gpio_descriptor_acquire(
return desc;
}
error_t gpio_descriptor_release(struct GpioDescriptor* descriptor) {
error_t gpio_descriptor_release(GpioDescriptor* descriptor) {
auto* data = static_cast<struct GpioControllerData*>(device_get_driver_data(descriptor->controller));
mutex_lock(&data->mutex);
descriptor->owner_type = GPIO_OWNER_NONE;
mutex_unlock(&data->mutex);
return ERROR_NONE;
}
error_t gpio_controller_get_pin_count(struct Device* device, uint32_t* count) {
auto* data = (struct GpioControllerData*)device_get_driver_data(device);
error_t gpio_controller_get_pin_count(Device* device, uint32_t* count) {
auto* data = static_cast<struct GpioControllerData*>(device_get_driver_data(device));
mutex_lock(&data->mutex);
*count = data->pin_count;
mutex_unlock(&data->mutex);
return ERROR_NONE;
}
error_t gpio_controller_init_descriptors(struct Device* device, uint32_t pin_count, void* controller_context) {
auto* data = new(std::nothrow) GpioControllerData(pin_count);
error_t gpio_controller_init_descriptors(Device* device, uint32_t pin_count, void* controller_context) {
auto* data = new(std::nothrow) GpioControllerData(pin_count, controller_context);
if (!data) return ERROR_OUT_OF_MEMORY;
if (data->init_descriptors(device, controller_context) != ERROR_NONE) {
if (data->init_descriptors(device) != ERROR_NONE) {
delete data;
return ERROR_OUT_OF_MEMORY;
}
@@ -93,49 +101,82 @@ error_t gpio_controller_init_descriptors(struct Device* device, uint32_t pin_cou
return ERROR_NONE;
}
error_t gpio_controller_deinit_descriptors(struct Device* device) {
error_t gpio_controller_deinit_descriptors(Device* device) {
auto* data = static_cast<struct GpioControllerData*>(device_get_driver_data(device));
delete data;
device_set_driver_data(device, nullptr);
delete data;
return ERROR_NONE;
}
error_t gpio_descriptor_set_level(struct GpioDescriptor* descriptor, bool high) {
void* gpio_controller_get_controller_context(Device* device) {
auto* data = static_cast<struct GpioControllerData*>(device_get_driver_data(device));
return data->controller_context;
}
error_t gpio_descriptor_set_level(GpioDescriptor* descriptor, bool high) {
const auto* driver = device_get_driver(descriptor->controller);
return GPIO_INTERNAL_API(driver)->set_level(descriptor, high);
}
error_t gpio_descriptor_get_level(struct GpioDescriptor* descriptor, bool* high) {
error_t gpio_descriptor_get_level(GpioDescriptor* descriptor, bool* high) {
const auto* driver = device_get_driver(descriptor->controller);
return GPIO_INTERNAL_API(driver)->get_level(descriptor, high);
}
error_t gpio_descriptor_set_flags(struct GpioDescriptor* descriptor, gpio_flags_t flags) {
error_t gpio_descriptor_set_flags(GpioDescriptor* descriptor, gpio_flags_t flags) {
const auto* driver = device_get_driver(descriptor->controller);
return GPIO_INTERNAL_API(driver)->set_flags(descriptor, flags);
}
error_t gpio_descriptor_get_flags(struct GpioDescriptor* descriptor, gpio_flags_t* flags) {
error_t gpio_descriptor_get_flags(GpioDescriptor* descriptor, gpio_flags_t* flags) {
const auto* driver = device_get_driver(descriptor->controller);
return GPIO_INTERNAL_API(driver)->get_flags(descriptor, flags);
}
error_t gpio_descriptor_get_pin_number(struct GpioDescriptor* descriptor, gpio_pin_t* pin) {
error_t gpio_descriptor_get_pin_number(GpioDescriptor* descriptor, gpio_pin_t* pin) {
*pin = descriptor->pin;
return ERROR_NONE;
}
error_t gpio_descriptor_get_native_pin_number(struct GpioDescriptor* descriptor, void* pin_number) {
error_t gpio_descriptor_get_native_pin_number(GpioDescriptor* descriptor, void* pin_number) {
const auto* driver = device_get_driver(descriptor->controller);
return GPIO_INTERNAL_API(driver)->get_native_pin_number(descriptor, pin_number);
}
error_t gpio_descriptor_get_owner_type(struct GpioDescriptor* descriptor, GpioOwnerType* owner_type) {
error_t gpio_descriptor_add_callback(GpioDescriptor* descriptor, void (*callback)(void*), void* arg) {
const auto* driver = device_get_driver(descriptor->controller);
auto* api = GPIO_INTERNAL_API(driver);
if (!api->add_callback) return ERROR_NOT_SUPPORTED;
return api->add_callback(descriptor, callback, arg);
}
error_t gpio_descriptor_remove_callback(GpioDescriptor* descriptor) {
const auto* driver = device_get_driver(descriptor->controller);
auto* api = GPIO_INTERNAL_API(driver);
if (!api->remove_callback) return ERROR_NOT_SUPPORTED;
return api->remove_callback(descriptor);
}
error_t gpio_descriptor_enable_interrupt(GpioDescriptor* descriptor) {
const auto* driver = device_get_driver(descriptor->controller);
auto* api = GPIO_INTERNAL_API(driver);
if (!api->enable_interrupt) return ERROR_NOT_SUPPORTED;
return api->enable_interrupt(descriptor);
}
error_t gpio_descriptor_disable_interrupt(GpioDescriptor* descriptor) {
const auto* driver = device_get_driver(descriptor->controller);
auto* api = GPIO_INTERNAL_API(driver);
if (!api->disable_interrupt) return ERROR_NOT_SUPPORTED;
return api->disable_interrupt(descriptor);
}
error_t gpio_descriptor_get_owner_type(GpioDescriptor* descriptor, GpioOwnerType* owner_type) {
*owner_type = descriptor->owner_type;
return ERROR_NONE;
}
const struct DeviceType GPIO_CONTROLLER_TYPE {
const DeviceType GPIO_CONTROLLER_TYPE {
.name = "gpio-controller"
};
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: Apache-2.0
#include <algorithm>
#include <tactility/concurrent/mutex.h>
#include <tactility/concurrent/recursive_mutex.h>
#include <tactility/filesystem/file_system.h>
#include <vector>
// Define the internal FileSystem structure
struct FileSystem {
const FileSystemApi* api;
void* data;
};
// Global list of file systems and its mutex
struct FileSystemsLedger {
std::vector<FileSystem*> file_systems;
// Use recursive mutex so that file_system_for_each() can lock within the callback
RecursiveMutex mutex {};
FileSystemsLedger() { recursive_mutex_construct(&mutex); }
~FileSystemsLedger() { recursive_mutex_destruct(&mutex); }
void lock() { recursive_mutex_lock(&mutex); }
bool is_locked() { return recursive_mutex_is_locked(&mutex); }
void unlock() { recursive_mutex_unlock(&mutex); }
};
static FileSystemsLedger& get_ledger() {
static FileSystemsLedger ledger;
return ledger;
}
extern "C" {
FileSystem* file_system_add(const FileSystemApi* fs_api, void* data) {
auto& ledger = get_ledger();
check(!ledger.is_locked()); // ensure file_system_for_each() doesn't add a filesystem while iterating
ledger.lock();
auto* fs = new(std::nothrow) struct FileSystem();
check(fs != nullptr);
fs->api = fs_api;
fs->data = data;
ledger.file_systems.push_back(fs);
ledger.unlock();
return fs;
}
void file_system_remove(FileSystem* fs) {
check(!file_system_is_mounted(fs));
auto& ledger = get_ledger();
check(!ledger.is_locked()); // ensure file_system_for_each() doesn't remove a filesystem while iterating
ledger.lock();
auto it = std::ranges::find(ledger.file_systems, fs);
if (it != ledger.file_systems.end()) {
ledger.file_systems.erase(it);
delete fs;
}
ledger.unlock();
}
void file_system_for_each(void* callback_context, bool (*callback)(FileSystem* fs, void* context)) {
auto& ledger = get_ledger();
ledger.lock();
for (auto* fs : ledger.file_systems) {
if (!callback(fs, callback_context)) break;
}
ledger.unlock();
}
error_t file_system_mount(FileSystem* fs) {
// Assuming 'device' is accessible or passed via a different mechanism
// as it's required by the FileSystemApi signatures.
return fs->api->mount(fs->data);
}
error_t file_system_unmount(FileSystem* fs) {
return fs->api->unmount(fs->data);
}
bool file_system_is_mounted(FileSystem* fs) {
return fs->api->is_mounted(fs->data);
}
error_t file_system_get_path(FileSystem* fs, char* out_path, size_t out_path_size) {
return fs->api->get_path(fs->data, out_path, out_path_size);
}
}
+14 -5
View File
@@ -1,3 +1,7 @@
#include <tactility/concurrent/dispatcher.h>
#include <tactility/concurrent/event_group.h>
#include <tactility/concurrent/thread.h>
#include <tactility/concurrent/timer.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/gpio_controller.h>
@@ -6,14 +10,14 @@
#include <tactility/drivers/root.h>
#include <tactility/drivers/spi_controller.h>
#include <tactility/drivers/uart_controller.h>
#include <tactility/concurrent/dispatcher.h>
#include <tactility/concurrent/event_group.h>
#include <tactility/concurrent/thread.h>
#include <tactility/concurrent/timer.h>
#include <tactility/error.h>
#include <tactility/log.h>
#include <tactility/filesystem/file_system.h>
#include <tactility/module.h>
#ifndef ESP_PLATFORM
#include <tactility/log.h>
#endif
/**
* This file is a C file instead of C++, so we can import all headers as C code.
* The intent is to catch errors that only show up when compiling as C and not as C++.
@@ -153,6 +157,11 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
DEFINE_MODULE_SYMBOL(timer_set_callback_priority),
// error
DEFINE_MODULE_SYMBOL(error_to_string),
// file system
DEFINE_MODULE_SYMBOL(file_system_mount),
DEFINE_MODULE_SYMBOL(file_system_unmount),
DEFINE_MODULE_SYMBOL(file_system_is_mounted),
DEFINE_MODULE_SYMBOL(file_system_get_path),
// log
#ifndef ESP_PLATFORM
DEFINE_MODULE_SYMBOL(log_generic),
+13 -10
View File
@@ -12,8 +12,8 @@ struct ModuleInternal {
};
struct ModuleLedger {
std::vector<struct Module*> modules;
struct Mutex mutex {};
std::vector<Module*> modules;
Mutex mutex {};
ModuleLedger() { mutex_construct(&mutex); }
~ModuleLedger() { mutex_destruct(&mutex); }
@@ -23,36 +23,37 @@ static ModuleLedger ledger;
extern "C" {
error_t module_construct(struct Module* module) {
error_t module_construct(Module* module) {
module->internal = new (std::nothrow) ModuleInternal();
if (module->internal == nullptr) return ERROR_OUT_OF_MEMORY;
return ERROR_NONE;
}
error_t module_destruct(struct Module* module) {
error_t module_destruct(Module* module) {
delete static_cast<ModuleInternal*>(module->internal);
module->internal = nullptr;
return ERROR_NONE;
}
error_t module_add(struct Module* module) {
error_t module_add(Module* module) {
mutex_lock(&ledger.mutex);
ledger.modules.push_back(module);
mutex_unlock(&ledger.mutex);
return ERROR_NONE;
}
error_t module_remove(struct Module* module) {
error_t module_remove(Module* module) {
mutex_lock(&ledger.mutex);
ledger.modules.erase(std::remove(ledger.modules.begin(), ledger.modules.end(), module), ledger.modules.end());
mutex_unlock(&ledger.mutex);
return ERROR_NONE;
}
error_t module_start(struct Module* module) {
error_t module_start(Module* module) {
LOG_I(TAG, "start %s", module->name);
auto* internal = static_cast<ModuleInternal*>(module->internal);
auto* internal = module->internal;
if (internal == nullptr) return ERROR_INVALID_STATE;
if (internal->started) return ERROR_NONE;
error_t error = module->start();
@@ -61,13 +62,15 @@ error_t module_start(struct Module* module) {
}
bool module_is_started(struct Module* module) {
return static_cast<ModuleInternal*>(module->internal)->started;
auto* internal = module->internal;
return internal != nullptr && internal->started;
}
error_t module_stop(struct Module* module) {
LOG_I(TAG, "stop %s", module->name);
auto* internal = static_cast<ModuleInternal*>(module->internal);
auto* internal = module->internal;
if (internal == nullptr) return ERROR_INVALID_STATE;
if (!internal->started) return ERROR_NONE;
error_t error = module->stop();