Implement UI scaling and more (#501)
**New Features** * Runtime font accessors and new symbol fonts for text, launcher, statusbar, and shared icons. * Added font height base setting to device.properties * Text fonts now have 3 sizes: small, default, large **Improvements** * Renamed `UiScale` to `UiDensity` * Statusbar, toolbar and many UI components now compute heights and spacing from fonts/density. * SSD1306 initialization sequence refined for more stable startup. * Multiple image assets replaced by symbol-font rendering. * Many layout improvements related to density, font scaling and icon scaling * Updated folder name capitalization for newer style
This commit is contained in:
committed by
GitHub
parent
72c9b2b113
commit
9a11e6f47b
@@ -0,0 +1,142 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <queue>
|
||||
|
||||
#include <tactility/concurrent/dispatcher.h>
|
||||
|
||||
#include "tactility/error.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <tactility/concurrent/event_group.h>
|
||||
#include <tactility/concurrent/mutex.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#define TAG "Dispatcher"
|
||||
|
||||
static constexpr EventBits_t BACKPRESSURE_WARNING_COUNT = 100U;
|
||||
static constexpr EventBits_t WAIT_FLAG = 1U;
|
||||
|
||||
struct QueuedItem {
|
||||
DispatcherCallback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
struct DispatcherData {
|
||||
Mutex mutex = { 0 };
|
||||
std::queue<QueuedItem> queue = {};
|
||||
EventGroupHandle_t eventGroup = nullptr;
|
||||
std::atomic<bool> shutdown{false}; // TODO: Use EventGroup
|
||||
|
||||
DispatcherData() {
|
||||
event_group_construct(&eventGroup);
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~DispatcherData() {
|
||||
event_group_destruct(&eventGroup);
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
#define dispatcher_data(handle) static_cast<DispatcherData*>(handle)
|
||||
|
||||
extern "C" {
|
||||
|
||||
DispatcherHandle_t dispatcher_alloc(void) {
|
||||
return new DispatcherData();
|
||||
}
|
||||
|
||||
void dispatcher_free(DispatcherHandle_t dispatcher) {
|
||||
auto* data = dispatcher_data(dispatcher);
|
||||
data->shutdown.store(true, std::memory_order_release);
|
||||
mutex_lock(&data->mutex);
|
||||
mutex_unlock(&data->mutex);
|
||||
delete data;
|
||||
}
|
||||
|
||||
error_t dispatcher_dispatch_timed(DispatcherHandle_t dispatcher, void* callbackContext, DispatcherCallback callback, TickType_t timeout) {
|
||||
auto* data = dispatcher_data(dispatcher);
|
||||
|
||||
// Mutate
|
||||
if (!mutex_try_lock(&data->mutex, timeout)) {
|
||||
#ifdef ESP_PLATFORM
|
||||
LOG_E(TAG, "Mutex acquisition timeout");
|
||||
#endif
|
||||
return ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (data->shutdown.load(std::memory_order_acquire)) {
|
||||
mutex_unlock(&data->mutex);
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
data->queue.push({
|
||||
.callback = callback,
|
||||
.context = callbackContext
|
||||
});
|
||||
|
||||
if (data->queue.size() == BACKPRESSURE_WARNING_COUNT) {
|
||||
#ifdef ESP_PLATFORM
|
||||
LOG_W(TAG, "Backpressure: You're not consuming fast enough (100 queued)");
|
||||
#endif
|
||||
}
|
||||
|
||||
mutex_unlock(&data->mutex);
|
||||
|
||||
if (event_group_set(data->eventGroup, WAIT_FLAG) != ERROR_NONE) {
|
||||
#ifdef ESP_PLATFORM
|
||||
LOG_E(TAG, "Failed to set flag");
|
||||
#endif
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t dispatcher_consume_timed(DispatcherHandle_t dispatcher, TickType_t timeout) {
|
||||
auto* data = dispatcher_data(dispatcher);
|
||||
|
||||
// TODO: keep track of time and consider the timeout input as total timeout
|
||||
|
||||
// Wait for signal
|
||||
error_t error = event_group_wait(data->eventGroup, WAIT_FLAG, false, true, nullptr, timeout);
|
||||
if (error != ERROR_NONE) {
|
||||
if (error == ERROR_TIMEOUT) {
|
||||
return ERROR_TIMEOUT;
|
||||
} else {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
}
|
||||
|
||||
if (data->shutdown.load(std::memory_order_acquire)) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Mutate
|
||||
bool processing = true;
|
||||
do {
|
||||
if (mutex_try_lock(&data->mutex, 10)) {
|
||||
if (!data->queue.empty()) {
|
||||
// Make a copy, so it's thread-safe when we unlock
|
||||
auto entry = data->queue.front();
|
||||
data->queue.pop();
|
||||
processing = !data->queue.empty();
|
||||
// Don't keep lock as callback might be slow and we want to allow dispatch in the meanwhile
|
||||
mutex_unlock(&data->mutex);
|
||||
entry.callback(entry.context);
|
||||
} else {
|
||||
processing = false;
|
||||
mutex_unlock(&data->mutex);
|
||||
}
|
||||
} else {
|
||||
#ifdef ESP_PLATFORM
|
||||
LOG_W(TAG, "Mutex acquisition timeout");
|
||||
#endif
|
||||
}
|
||||
|
||||
} while (processing && !data->shutdown.load(std::memory_order_acquire));
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <tactility/concurrent/event_group.h>
|
||||
#include <tactility/error.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
error_t event_group_set(EventGroupHandle_t eventGroup, uint32_t inFlags) {
|
||||
if (xPortInIsrContext() == pdTRUE) {
|
||||
BaseType_t yield = pdFALSE;
|
||||
if (xEventGroupSetBitsFromISR(eventGroup, inFlags, &yield) == pdFAIL) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
portYIELD_FROM_ISR(yield);
|
||||
} else {
|
||||
xEventGroupSetBits(eventGroup, inFlags);
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t event_group_clear(EventGroupHandle_t eventGroup, uint32_t flags) {
|
||||
if (xPortInIsrContext() == pdTRUE) {
|
||||
if (xEventGroupClearBitsFromISR(eventGroup, flags) == pdFAIL) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
portYIELD_FROM_ISR(pdTRUE);
|
||||
} else {
|
||||
xEventGroupClearBits(eventGroup, flags);
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
uint32_t event_group_get(EventGroupHandle_t eventGroup) {
|
||||
if (xPortInIsrContext() == pdTRUE) {
|
||||
return xEventGroupGetBitsFromISR(eventGroup);
|
||||
} else {
|
||||
return xEventGroupGetBits(eventGroup);
|
||||
}
|
||||
}
|
||||
|
||||
error_t event_group_wait(
|
||||
EventGroupHandle_t eventGroup,
|
||||
uint32_t inFlags,
|
||||
bool awaitAll,
|
||||
bool clearOnExit,
|
||||
uint32_t* outFlags,
|
||||
TickType_t timeout
|
||||
) {
|
||||
if (xPortInIsrContext() == pdTRUE) {
|
||||
return ERROR_ISR_STATUS;
|
||||
}
|
||||
|
||||
uint32_t result_flags = xEventGroupWaitBits(
|
||||
eventGroup,
|
||||
inFlags,
|
||||
clearOnExit ? pdTRUE : pdFALSE,
|
||||
awaitAll ? pdTRUE : pdFALSE,
|
||||
timeout
|
||||
);
|
||||
|
||||
auto invalid_flags = awaitAll
|
||||
? ((inFlags & result_flags) != inFlags) // await all
|
||||
: ((inFlags & result_flags) == 0U); // await any
|
||||
|
||||
if (invalid_flags) {
|
||||
const uint32_t matched = inFlags & result_flags;
|
||||
if (matched == 0U) {
|
||||
return ERROR_TIMEOUT;
|
||||
}
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (outFlags != nullptr) {
|
||||
*outFlags = result_flags;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,278 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/concurrent/thread.h>
|
||||
#include <tactility/concurrent/mutex.h>
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/delay.h>
|
||||
#include <tactility/log.h>
|
||||
#include <tactility/time.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
static const size_t LOCAL_STORAGE_SELF_POINTER_INDEX = 0;
|
||||
static const char* TAG = "Thread";
|
||||
|
||||
struct Thread {
|
||||
TaskHandle_t taskHandle = nullptr;
|
||||
ThreadState state = THREAD_STATE_STOPPED;
|
||||
thread_main_fn_t mainFunction = nullptr;
|
||||
void* mainFunctionContext = nullptr;
|
||||
int32_t callbackResult = 0;
|
||||
thread_state_callback_t stateCallback = nullptr;
|
||||
void* stateCallbackContext = nullptr;
|
||||
std::string name = "unnamed";
|
||||
enum ThreadPriority priority = THREAD_PRIORITY_NORMAL;
|
||||
struct Mutex mutex = { 0 };
|
||||
configSTACK_DEPTH_TYPE stackSize = 4096;
|
||||
portBASE_TYPE affinity = -1;
|
||||
|
||||
Thread() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~Thread() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
|
||||
void lock() { mutex_lock(&mutex); }
|
||||
|
||||
void unlock() { mutex_unlock(&mutex); }
|
||||
};
|
||||
|
||||
static void thread_set_state_internal(Thread* thread, ThreadState newState) {
|
||||
thread->lock();
|
||||
thread->state = newState;
|
||||
auto cb = thread->stateCallback;
|
||||
auto cb_ctx = thread->stateCallbackContext;
|
||||
thread->unlock();
|
||||
if (cb) {
|
||||
cb(newState, cb_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void thread_main_body(void* context) {
|
||||
check(context != nullptr);
|
||||
auto* thread = static_cast<Thread*>(context);
|
||||
|
||||
// Save Thread instance pointer to task local storage
|
||||
check(pvTaskGetThreadLocalStoragePointer(nullptr, LOCAL_STORAGE_SELF_POINTER_INDEX) == nullptr);
|
||||
vTaskSetThreadLocalStoragePointer(nullptr, LOCAL_STORAGE_SELF_POINTER_INDEX, thread);
|
||||
|
||||
LOG_I(TAG, "Starting %s", thread->name.c_str()); // No need to lock as we don't allow mutation after thread start
|
||||
check(thread->state == THREAD_STATE_STARTING);
|
||||
thread_set_state_internal(thread, THREAD_STATE_RUNNING);
|
||||
|
||||
int32_t result = thread->mainFunction(thread->mainFunctionContext);
|
||||
thread->lock();
|
||||
thread->callbackResult = result;
|
||||
thread->unlock();
|
||||
|
||||
check(thread->state == THREAD_STATE_RUNNING);
|
||||
thread_set_state_internal(thread, THREAD_STATE_STOPPED);
|
||||
LOG_I(TAG, "Stopped %s", thread->name.c_str()); // No need to lock as we don't allow mutation after thread start
|
||||
|
||||
vTaskSetThreadLocalStoragePointer(nullptr, LOCAL_STORAGE_SELF_POINTER_INDEX, nullptr);
|
||||
|
||||
thread->lock();
|
||||
thread->taskHandle = nullptr;
|
||||
thread->unlock();
|
||||
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
Thread* thread_alloc(void) {
|
||||
auto* thread = new(std::nothrow) Thread();
|
||||
if (thread == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return thread;
|
||||
}
|
||||
|
||||
Thread* thread_alloc_full(
|
||||
const char* name,
|
||||
configSTACK_DEPTH_TYPE stack_size,
|
||||
thread_main_fn_t function,
|
||||
void* function_context,
|
||||
portBASE_TYPE affinity
|
||||
) {
|
||||
auto* thread = new(std::nothrow) Thread();
|
||||
if (thread != nullptr) {
|
||||
thread_set_name(thread, name);
|
||||
thread_set_stack_size(thread, stack_size);
|
||||
thread_set_main_function(thread, function, function_context);
|
||||
thread_set_affinity(thread, affinity);
|
||||
}
|
||||
return thread;
|
||||
}
|
||||
|
||||
void thread_free(Thread* thread) {
|
||||
check(thread);
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
check(thread->taskHandle == nullptr);
|
||||
delete thread;
|
||||
}
|
||||
|
||||
void thread_set_name(Thread* thread, const char* name) {
|
||||
check(name != nullptr);
|
||||
thread->lock();
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
thread->name = name;
|
||||
thread->unlock();
|
||||
}
|
||||
|
||||
void thread_set_stack_size(Thread* thread, size_t stack_size) {
|
||||
thread->lock();
|
||||
check(stack_size > 0);
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
thread->stackSize = stack_size;
|
||||
thread->unlock();
|
||||
}
|
||||
|
||||
void thread_set_affinity(Thread* thread, portBASE_TYPE affinity) {
|
||||
thread->lock();
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
thread->affinity = affinity;
|
||||
thread->unlock();
|
||||
}
|
||||
|
||||
void thread_set_main_function(Thread* thread, thread_main_fn_t function, void* context) {
|
||||
thread->lock();
|
||||
check(function != nullptr);
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
thread->mainFunction = function;
|
||||
thread->mainFunctionContext = context;
|
||||
thread->unlock();
|
||||
}
|
||||
|
||||
void thread_set_priority(Thread* thread, enum ThreadPriority priority) {
|
||||
thread->lock();
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
thread->priority = priority;
|
||||
thread->unlock();
|
||||
}
|
||||
|
||||
void thread_set_state_callback(Thread* thread, thread_state_callback_t callback, void* context) {
|
||||
thread->lock();
|
||||
check(callback != nullptr);
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
thread->stateCallback = callback;
|
||||
thread->stateCallbackContext = context;
|
||||
thread->unlock();
|
||||
}
|
||||
|
||||
ThreadState thread_get_state(Thread* thread) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
thread->lock();
|
||||
ThreadState state = thread->state;
|
||||
thread->unlock();
|
||||
return state;
|
||||
}
|
||||
|
||||
error_t thread_start(Thread* thread) {
|
||||
thread->lock();
|
||||
check(thread->mainFunction != nullptr);
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
check(thread->stackSize);
|
||||
thread->unlock();
|
||||
|
||||
thread_set_state_internal(thread, THREAD_STATE_STARTING);
|
||||
|
||||
thread->lock();
|
||||
uint32_t stack_depth = thread->stackSize / sizeof(StackType_t);
|
||||
enum ThreadPriority priority = thread->priority;
|
||||
portBASE_TYPE affinity = thread->affinity;
|
||||
thread->unlock();
|
||||
|
||||
BaseType_t result;
|
||||
if (affinity != -1) {
|
||||
#ifdef ESP_PLATFORM
|
||||
result = xTaskCreatePinnedToCore(
|
||||
thread_main_body,
|
||||
thread->name.c_str(),
|
||||
stack_depth,
|
||||
thread,
|
||||
(UBaseType_t)priority,
|
||||
&thread->taskHandle,
|
||||
affinity
|
||||
);
|
||||
#else
|
||||
result = xTaskCreate(
|
||||
thread_main_body,
|
||||
thread->name.c_str(),
|
||||
stack_depth,
|
||||
thread,
|
||||
(UBaseType_t)priority,
|
||||
&thread->taskHandle
|
||||
);
|
||||
#endif
|
||||
} else {
|
||||
result = xTaskCreate(
|
||||
thread_main_body,
|
||||
thread->name.c_str(),
|
||||
stack_depth,
|
||||
thread,
|
||||
(UBaseType_t)priority,
|
||||
&thread->taskHandle
|
||||
);
|
||||
}
|
||||
|
||||
if (result != pdPASS) {
|
||||
thread_set_state_internal(thread, THREAD_STATE_STOPPED);
|
||||
thread->lock();
|
||||
thread->taskHandle = nullptr;
|
||||
thread->unlock();
|
||||
return ERROR_UNDEFINED;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t thread_join(Thread* thread, TickType_t timeout, TickType_t poll_interval) {
|
||||
check(thread_get_current() != thread);
|
||||
|
||||
TickType_t start_ticks = get_ticks();
|
||||
while (thread_get_task_handle(thread)) {
|
||||
delay_ticks(poll_interval);
|
||||
if (get_ticks() - start_ticks > timeout) {
|
||||
return ERROR_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
TaskHandle_t thread_get_task_handle(Thread* thread) {
|
||||
thread->lock();
|
||||
auto* handle = thread->taskHandle;
|
||||
thread->unlock();
|
||||
return handle;
|
||||
}
|
||||
|
||||
int32_t thread_get_return_code(Thread* thread) {
|
||||
thread->lock();
|
||||
check(thread->state == THREAD_STATE_STOPPED);
|
||||
auto result = thread->callbackResult;
|
||||
thread->unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t thread_get_stack_space(Thread* thread) {
|
||||
if (xPortInIsrContext() == pdTRUE) {
|
||||
return 0;
|
||||
}
|
||||
thread->lock();
|
||||
check(thread->state == THREAD_STATE_RUNNING);
|
||||
auto result = uxTaskGetStackHighWaterMark(thread->taskHandle) * sizeof(StackType_t);
|
||||
thread->unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
Thread* thread_get_current(void) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
return (Thread*)pvTaskGetThreadLocalStoragePointer(nullptr, LOCAL_STORAGE_SELF_POINTER_INDEX);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/concurrent/timer.h>
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/freertos/timers.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Timer {
|
||||
TimerHandle_t handle;
|
||||
timer_callback_t callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
static void timer_callback_internal(TimerHandle_t xTimer) {
|
||||
struct Timer* timer = (struct Timer*)pvTimerGetTimerID(xTimer);
|
||||
if (timer != NULL && timer->callback != NULL) {
|
||||
timer->callback(timer->context);
|
||||
}
|
||||
}
|
||||
|
||||
struct Timer* timer_alloc(enum TimerType type, TickType_t ticks, timer_callback_t callback, void* context) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
check(callback != NULL);
|
||||
|
||||
struct Timer* timer = (struct Timer*)malloc(sizeof(struct Timer));
|
||||
if (timer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
timer->callback = callback;
|
||||
timer->context = context;
|
||||
|
||||
BaseType_t auto_reload = (type == TIMER_TYPE_ONCE) ? pdFALSE : pdTRUE;
|
||||
timer->handle = xTimerCreate(NULL, ticks, auto_reload, timer, timer_callback_internal);
|
||||
|
||||
if (timer->handle == NULL) {
|
||||
free(timer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return timer;
|
||||
}
|
||||
|
||||
void timer_free(struct Timer* timer) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
check(timer != NULL);
|
||||
// MAX_TICKS or a reasonable timeout for the timer command queue
|
||||
xTimerDelete(timer->handle, portMAX_DELAY);
|
||||
free(timer);
|
||||
}
|
||||
|
||||
error_t timer_start(struct Timer* timer) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
check(timer != NULL);
|
||||
return (xTimerStart(timer->handle, portMAX_DELAY) == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
error_t timer_stop(struct Timer* timer) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
check(timer != NULL);
|
||||
return (xTimerStop(timer->handle, portMAX_DELAY) == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
error_t timer_reset_with_interval(struct Timer* timer, TickType_t interval) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
check(timer != NULL);
|
||||
if (xTimerChangePeriod(timer->handle, interval, portMAX_DELAY) != pdPASS) {
|
||||
return ERROR_TIMEOUT;
|
||||
}
|
||||
return (xTimerReset(timer->handle, portMAX_DELAY) == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
error_t timer_reset(struct Timer* timer) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
check(timer != NULL);
|
||||
return (xTimerReset(timer->handle, portMAX_DELAY) == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
bool timer_is_running(struct Timer* timer) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
check(timer != NULL);
|
||||
return xTimerIsTimerActive(timer->handle) != pdFALSE;
|
||||
}
|
||||
|
||||
TickType_t timer_get_expiry_time(struct Timer* timer) {
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
check(timer != NULL);
|
||||
return xTimerGetExpiryTime(timer->handle);
|
||||
}
|
||||
|
||||
error_t timer_set_pending_callback(struct Timer* timer, timer_pending_callback_t callback, void* context, uint32_t arg, TickType_t timeout) {
|
||||
(void)timer; // Unused in this implementation but kept for API consistency if needed later
|
||||
BaseType_t result;
|
||||
if (xPortInIsrContext() == pdTRUE) {
|
||||
check(timeout == 0);
|
||||
result = xTimerPendFunctionCallFromISR(callback, context, arg, NULL);
|
||||
} else {
|
||||
result = xTimerPendFunctionCall(callback, context, arg, timeout);
|
||||
}
|
||||
return (result == pdPASS) ? ERROR_NONE : ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
void timer_set_callback_priority(struct Timer* timer, enum ThreadPriority priority) {
|
||||
(void)timer; // Unused in this implementation but kept for API consistency if needed later
|
||||
check(xPortInIsrContext() == pdFALSE);
|
||||
|
||||
TaskHandle_t task_handle = xTimerGetTimerDaemonTaskHandle();
|
||||
check(task_handle != NULL);
|
||||
|
||||
vTaskPrioritySet(task_handle, (UBaseType_t)priority);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <cstdlib>
|
||||
#include <tactility/freertos/task.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
static const auto* TAG = "Kernel";
|
||||
|
||||
static void log_memory_info() {
|
||||
#ifdef ESP_PLATFORM
|
||||
LOG_E(TAG, "Default memory caps:");
|
||||
LOG_E(TAG, " Total: %" PRIu64, static_cast<uint64_t>(heap_caps_get_total_size(MALLOC_CAP_DEFAULT)));
|
||||
LOG_E(TAG, " Free: %" PRIu64, static_cast<uint64_t>(heap_caps_get_free_size(MALLOC_CAP_DEFAULT)));
|
||||
LOG_E(TAG, " Min free: %" PRIu64, static_cast<uint64_t>(heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT)));
|
||||
LOG_E(TAG, "Internal memory caps:");
|
||||
LOG_E(TAG, " Total: %" PRIu64, static_cast<uint64_t>(heap_caps_get_total_size(MALLOC_CAP_INTERNAL)));
|
||||
LOG_E(TAG, " Free: %" PRIu64, static_cast<uint64_t>(heap_caps_get_free_size(MALLOC_CAP_INTERNAL)));
|
||||
LOG_E(TAG, " Min free: %" PRIu64, static_cast<uint64_t>(heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL)));
|
||||
#endif
|
||||
}
|
||||
|
||||
static void log_task_info() {
|
||||
const char* name = pcTaskGetName(nullptr);
|
||||
const char* safe_name = name ? name : "main";
|
||||
LOG_E(TAG, "Task: %s", safe_name);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
__attribute__((noreturn)) void __crash(void) {
|
||||
log_task_info();
|
||||
log_memory_info();
|
||||
// TODO: Add breakpoint when debugger is attached.
|
||||
#ifdef ESP_PLATFORM
|
||||
esp_system_abort("System halted. Connect debugger for more info.");
|
||||
#else
|
||||
exit(1);
|
||||
#endif
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <sys/errno.h>
|
||||
#include <vector>
|
||||
|
||||
#define TAG "device"
|
||||
|
||||
struct DeviceInternal {
|
||||
/** Address of the API exposed by the device instance. */
|
||||
struct Driver* driver = nullptr;
|
||||
/** The driver data for this device (e.g. a mutex) */
|
||||
void* driver_data = nullptr;
|
||||
/** The mutex for device operations */
|
||||
struct Mutex mutex {};
|
||||
/** The device state */
|
||||
struct {
|
||||
int start_result = 0;
|
||||
bool started : 1 = false;
|
||||
bool added : 1 = false;
|
||||
} state;
|
||||
/** Attached child devices */
|
||||
std::vector<Device*> children {};
|
||||
};
|
||||
|
||||
struct DeviceLedger {
|
||||
std::vector<Device*> devices;
|
||||
Mutex mutex { 0 };
|
||||
|
||||
DeviceLedger() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~DeviceLedger() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
static DeviceLedger& get_ledger() {
|
||||
static DeviceLedger ledger;
|
||||
return ledger;
|
||||
}
|
||||
|
||||
#define ledger get_ledger()
|
||||
|
||||
extern "C" {
|
||||
|
||||
#define ledger_lock() mutex_lock(&ledger.mutex)
|
||||
#define ledger_unlock() mutex_unlock(&ledger.mutex)
|
||||
|
||||
#define lock_internal(internal) mutex_lock(&internal->mutex)
|
||||
#define unlock_internal(internal) mutex_unlock(&internal->mutex)
|
||||
|
||||
error_t device_construct(Device* device) {
|
||||
device->internal = new(std::nothrow) DeviceInternal;
|
||||
if (device->internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
LOG_D(TAG, "construct %s", device->name);
|
||||
mutex_construct(&device->internal->mutex);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t device_destruct(Device* device) {
|
||||
lock_internal(device->internal);
|
||||
|
||||
auto* internal = device->internal;
|
||||
|
||||
if (internal->state.started || internal->state.added) {
|
||||
unlock_internal(device->internal);
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
if (!internal->children.empty()) {
|
||||
unlock_internal(device->internal);
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
LOG_D(TAG, "destruct %s", device->name);
|
||||
|
||||
device->internal = nullptr;
|
||||
mutex_unlock(&internal->mutex);
|
||||
delete internal;
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
/** Add a child to the list of children */
|
||||
static void device_add_child(struct Device* device, struct Device* child) {
|
||||
device_lock(device);
|
||||
check(device->internal->state.added);
|
||||
device->internal->children.push_back(child);
|
||||
device_unlock(device);
|
||||
}
|
||||
|
||||
/** Remove a child from the list of children */
|
||||
static void device_remove_child(struct Device* device, struct Device* child) {
|
||||
device_lock(device);
|
||||
const auto iterator = std::ranges::find(device->internal->children, child);
|
||||
if (iterator != device->internal->children.end()) {
|
||||
device->internal->children.erase(iterator);
|
||||
}
|
||||
device_unlock(device);
|
||||
}
|
||||
|
||||
error_t device_add(Device* device) {
|
||||
LOG_D(TAG, "add %s", device->name);
|
||||
|
||||
// Already added
|
||||
if (device->internal->state.started || device->internal->state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Add to ledger
|
||||
ledger_lock();
|
||||
ledger.devices.push_back(device);
|
||||
ledger_unlock();
|
||||
|
||||
// Add self to parent's children list
|
||||
auto* parent = device->parent;
|
||||
if (parent != nullptr) {
|
||||
device_add_child(parent, device);
|
||||
}
|
||||
|
||||
device->internal->state.added = true;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t device_remove(Device* device) {
|
||||
LOG_D(TAG, "remove %s", device->name);
|
||||
|
||||
if (device->internal->state.started || !device->internal->state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Remove self from parent's children list
|
||||
auto* parent = device->parent;
|
||||
if (parent != nullptr) {
|
||||
device_remove_child(parent, device);
|
||||
}
|
||||
|
||||
ledger_lock();
|
||||
const auto iterator = std::ranges::find(ledger.devices, device);
|
||||
if (iterator == ledger.devices.end()) {
|
||||
ledger_unlock();
|
||||
goto failed_ledger_lookup;
|
||||
}
|
||||
ledger.devices.erase(iterator);
|
||||
ledger_unlock();
|
||||
|
||||
device->internal->state.added = false;
|
||||
return ERROR_NONE;
|
||||
|
||||
failed_ledger_lookup:
|
||||
|
||||
// Re-add to parent
|
||||
if (parent != nullptr) {
|
||||
device_add_child(parent, device);
|
||||
}
|
||||
|
||||
return ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
error_t device_start(Device* device) {
|
||||
LOG_I(TAG, "start %s", device->name);
|
||||
if (!device->internal->state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (device->internal->driver == nullptr) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Already started
|
||||
if (device->internal->state.started) {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t bind_error = driver_bind(device->internal->driver, device);
|
||||
device->internal->state.started = (bind_error == ERROR_NONE);
|
||||
device->internal->state.start_result = bind_error;
|
||||
return bind_error == ERROR_NONE ? ERROR_NONE : ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t device_stop(struct Device* device) {
|
||||
LOG_I(TAG, "stop %s", device->name);
|
||||
if (!device->internal->state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!device->internal->state.started) {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
if (driver_unbind(device->internal->driver, device) != ERROR_NONE) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
device->internal->state.started = false;
|
||||
device->internal->state.start_result = 0;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t device_construct_add(struct Device* device, const char* compatible) {
|
||||
struct Driver* driver = driver_find_compatible(compatible);
|
||||
if (driver == nullptr) {
|
||||
LOG_E(TAG, "Can't find driver '%s' for device '%s'", compatible, device->name);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t error = device_construct(device);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to construct device %s: %s", device->name, error_to_string(error));
|
||||
goto on_construct_error;
|
||||
}
|
||||
|
||||
device_set_driver(device, driver);
|
||||
|
||||
error = device_add(device);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to add device %s: %s", device->name, error_to_string(error));
|
||||
goto on_add_error;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
|
||||
on_add_error:
|
||||
device_destruct(device);
|
||||
on_construct_error:
|
||||
return error;
|
||||
}
|
||||
|
||||
error_t device_construct_add_start(struct Device* device, const char* compatible) {
|
||||
error_t error = device_construct_add(device, compatible);
|
||||
if (error != ERROR_NONE) {
|
||||
goto on_construct_add_error;
|
||||
}
|
||||
|
||||
error = device_start(device);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to start device %s: %s", device->name, error_to_string(error));
|
||||
goto on_start_error;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
|
||||
on_start_error:
|
||||
device_remove(device);
|
||||
device_destruct(device);
|
||||
on_construct_add_error:
|
||||
return error;
|
||||
}
|
||||
|
||||
void device_set_parent(Device* device, Device* parent) {
|
||||
assert(!device->internal->state.started);
|
||||
device->parent = parent;
|
||||
}
|
||||
|
||||
Device* device_get_parent(struct Device* device) {
|
||||
return device->parent;
|
||||
}
|
||||
|
||||
void device_set_driver(struct Device* device, struct Driver* driver) {
|
||||
device->internal->driver = driver;
|
||||
}
|
||||
|
||||
struct Driver* device_get_driver(struct Device* device) {
|
||||
return device->internal->driver;
|
||||
}
|
||||
|
||||
bool device_is_ready(const struct Device* device) {
|
||||
return device->internal->state.started;
|
||||
}
|
||||
|
||||
bool device_is_compatible(const struct Device* device, const char* compatible) {
|
||||
if (device->internal->driver == nullptr) return false;
|
||||
return driver_is_compatible(device->internal->driver, compatible);
|
||||
}
|
||||
|
||||
void device_set_driver_data(struct Device* device, void* driver_data) {
|
||||
device->internal->driver_data = driver_data;
|
||||
}
|
||||
|
||||
void* device_get_driver_data(struct Device* device) {
|
||||
return device->internal->driver_data;
|
||||
}
|
||||
|
||||
bool device_is_added(const struct Device* device) {
|
||||
return device->internal->state.added;
|
||||
}
|
||||
|
||||
void device_lock(struct Device* device) {
|
||||
mutex_lock(&device->internal->mutex);
|
||||
}
|
||||
|
||||
bool device_try_lock(struct Device* device, TickType_t timeout) {
|
||||
return mutex_try_lock(&device->internal->mutex, timeout);
|
||||
}
|
||||
|
||||
void device_unlock(struct Device* device) {
|
||||
mutex_unlock(&device->internal->mutex);
|
||||
}
|
||||
|
||||
const struct DeviceType* device_get_type(struct Device* device) {
|
||||
return device->internal->driver ? device->internal->driver->device_type : NULL;
|
||||
}
|
||||
|
||||
void device_for_each(void* callback_context, bool(*on_device)(Device* device, void* context)) {
|
||||
ledger_lock();
|
||||
for (auto* device : ledger.devices) {
|
||||
if (!on_device(device, callback_context)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ledger_unlock();
|
||||
}
|
||||
|
||||
void device_for_each_child(Device* device, void* callbackContext, bool(*on_device)(struct Device* device, void* context)) {
|
||||
for (auto* child_device : device->internal->children) {
|
||||
if (!on_device(child_device, callbackContext)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void device_for_each_of_type(const DeviceType* type, void* callbackContext, bool(*on_device)(Device* device, void* context)) {
|
||||
ledger_lock();
|
||||
for (auto* device : ledger.devices) {
|
||||
auto* driver = device->internal->driver;
|
||||
if (driver != nullptr) {
|
||||
if (driver->device_type == type) {
|
||||
if (!on_device(device, callbackContext)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ledger_unlock();
|
||||
}
|
||||
|
||||
bool device_exists_of_type(const DeviceType* type) {
|
||||
bool found = false;
|
||||
ledger_lock();
|
||||
for (auto* device : ledger.devices) {
|
||||
auto* driver = device->internal->driver;
|
||||
if (driver != nullptr && driver->device_type == type) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ledger_unlock();
|
||||
return found;
|
||||
}
|
||||
|
||||
Device* device_find_by_name(const char* name) {
|
||||
Device* found = nullptr;
|
||||
ledger_lock();
|
||||
for (auto* device : ledger.devices) {
|
||||
if (device->name != nullptr && std::strcmp(device->name, name) == 0) {
|
||||
found = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ledger_unlock();
|
||||
return found;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,176 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <cstring>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/concurrent/mutex.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#define TAG "driver"
|
||||
|
||||
struct DriverInternal {
|
||||
int use_count = 0;
|
||||
bool destroying = false;
|
||||
};
|
||||
|
||||
struct DriverLedger {
|
||||
std::vector<Driver*> drivers;
|
||||
Mutex mutex {};
|
||||
|
||||
DriverLedger() { mutex_construct(&mutex); }
|
||||
~DriverLedger() { mutex_destruct(&mutex); }
|
||||
|
||||
void lock() { mutex_lock(&mutex); }
|
||||
void unlock() { mutex_unlock(&mutex); }
|
||||
};
|
||||
|
||||
static DriverLedger ledger;
|
||||
|
||||
extern "C" {
|
||||
|
||||
error_t driver_construct(Driver* driver) {
|
||||
driver->internal = new(std::nothrow) DriverInternal;
|
||||
if (driver->internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t driver_destruct(Driver* driver) {
|
||||
auto* internal = driver->internal;
|
||||
|
||||
if (driver->owner == nullptr) {
|
||||
return ERROR_NOT_ALLOWED;
|
||||
}
|
||||
if (internal->use_count != 0 || internal->destroying) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
internal->destroying = true;
|
||||
|
||||
// Nullify internal reference before deletion to prevent use-after-free
|
||||
driver->internal = nullptr;
|
||||
delete internal;
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t driver_add(Driver* driver) {
|
||||
LOG_I(TAG, "add %s", driver->name);
|
||||
ledger.lock();
|
||||
ledger.drivers.push_back(driver);
|
||||
ledger.unlock();
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t driver_remove(Driver* driver) {
|
||||
LOG_I(TAG, "remove %s", driver->name);
|
||||
|
||||
if (driver->owner == nullptr) return ERROR_NOT_ALLOWED;
|
||||
|
||||
ledger.lock();
|
||||
const auto iterator = std::ranges::find(ledger.drivers, driver);
|
||||
if (iterator == ledger.drivers.end()) {
|
||||
ledger.unlock();
|
||||
return ERROR_NOT_FOUND;
|
||||
}
|
||||
ledger.drivers.erase(iterator);
|
||||
ledger.unlock();
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t driver_construct_add(struct Driver* driver) {
|
||||
if (driver_construct(driver) != ERROR_NONE) return ERROR_RESOURCE;
|
||||
if (driver_add(driver) != ERROR_NONE) return ERROR_RESOURCE;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t driver_remove_destruct(struct Driver* driver) {
|
||||
if (driver_remove(driver) != ERROR_NONE) return ERROR_RESOURCE;
|
||||
if (driver_destruct(driver) != ERROR_NONE) return ERROR_RESOURCE;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
bool driver_is_compatible(Driver* driver, const char* compatible) {
|
||||
if (compatible == nullptr || driver->compatible == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const char** compatible_iterator = driver->compatible;
|
||||
while (*compatible_iterator != nullptr) {
|
||||
if (strcmp(*compatible_iterator, compatible) == 0) {
|
||||
return true;
|
||||
}
|
||||
compatible_iterator++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Driver* driver_find_compatible(const char* compatible) {
|
||||
ledger.lock();
|
||||
Driver* result = nullptr;
|
||||
for (auto* driver : ledger.drivers) {
|
||||
if (driver_is_compatible(driver, compatible)) {
|
||||
result = driver;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ledger.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
error_t driver_bind(Driver* driver, Device* device) {
|
||||
error_t error = ERROR_NONE;
|
||||
if (driver->internal->destroying || !device_is_added(device)) {
|
||||
error = ERROR_INVALID_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (driver->start_device != nullptr) {
|
||||
error = driver->start_device(device);
|
||||
if (error != ERROR_NONE) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
driver->internal->use_count++;
|
||||
|
||||
LOG_I(TAG, "bound %s to %s", driver->name, device->name);
|
||||
return ERROR_NONE;
|
||||
|
||||
error:
|
||||
return error;
|
||||
}
|
||||
|
||||
error_t driver_unbind(Driver* driver, Device* device) {
|
||||
error_t error = ERROR_NONE;
|
||||
if (driver->internal->destroying || !device_is_added(device)) {
|
||||
error = ERROR_INVALID_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (driver->stop_device != nullptr) {
|
||||
error = driver->stop_device(device);
|
||||
if (error != ERROR_NONE) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
driver->internal->use_count--;
|
||||
|
||||
LOG_I(TAG, "unbound %s from %s", driver->name, device->name);
|
||||
|
||||
return ERROR_NONE;
|
||||
|
||||
error:
|
||||
return error;
|
||||
}
|
||||
|
||||
const struct DeviceType* driver_get_device_type(struct Driver* driver) {
|
||||
return driver->device_type;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,142 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/gpio_controller.h>
|
||||
|
||||
#include <tactility/concurrent/mutex.h>
|
||||
#include <tactility/drivers/gpio_descriptor.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <new>
|
||||
|
||||
#define GPIO_INTERNAL_API(driver) ((struct GpioControllerApi*)(driver)->api)
|
||||
|
||||
extern "C" {
|
||||
|
||||
struct GpioControllerData {
|
||||
struct Mutex mutex {};
|
||||
uint32_t pin_count;
|
||||
struct GpioDescriptor* descriptors = nullptr;
|
||||
|
||||
explicit GpioControllerData(uint32_t pin_count) : pin_count(pin_count) {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
error_t init_descriptors(Device* device, void* controller_context) {
|
||||
descriptors = (struct GpioDescriptor*)calloc(pin_count, sizeof(struct 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].owner_type = GPIO_OWNER_NONE;
|
||||
descriptors[i].controller_context = controller_context;
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
~GpioControllerData() {
|
||||
if (descriptors != nullptr) {
|
||||
free(descriptors);
|
||||
}
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
struct GpioDescriptor* gpio_descriptor_acquire(
|
||||
struct Device* controller,
|
||||
gpio_pin_t pin_number,
|
||||
enum GpioOwnerType owner
|
||||
) {
|
||||
check(owner != GPIO_OWNER_NONE);
|
||||
|
||||
auto* data = (struct GpioControllerData*)device_get_driver_data(controller);
|
||||
|
||||
mutex_lock(&data->mutex);
|
||||
if (pin_number >= data->pin_count) {
|
||||
mutex_unlock(&data->mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct GpioDescriptor* desc = &data->descriptors[pin_number];
|
||||
if (desc->owner_type != GPIO_OWNER_NONE) {
|
||||
mutex_unlock(&data->mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
desc->owner_type = owner;
|
||||
mutex_unlock(&data->mutex);
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
error_t gpio_descriptor_release(struct GpioDescriptor* descriptor) {
|
||||
descriptor->owner_type = GPIO_OWNER_NONE;
|
||||
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);
|
||||
*count = data->pin_count;
|
||||
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);
|
||||
if (!data) return ERROR_OUT_OF_MEMORY;
|
||||
|
||||
if (data->init_descriptors(device, controller_context) != ERROR_NONE) {
|
||||
delete data;
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
device_set_driver_data(device, data);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t gpio_controller_deinit_descriptors(struct Device* device) {
|
||||
auto* data = static_cast<struct GpioControllerData*>(device_get_driver_data(device));
|
||||
delete data;
|
||||
device_set_driver_data(device, nullptr);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t gpio_descriptor_set_level(struct 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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
*pin = descriptor->pin;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t gpio_descriptor_get_native_pin_number(struct 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) {
|
||||
*owner_type = descriptor->owner_type;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
const struct DeviceType GPIO_CONTROLLER_TYPE {
|
||||
.name = "gpio-controller"
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/error.h>
|
||||
|
||||
#define I2C_DRIVER_API(driver) ((struct I2cControllerApi*)driver->api)
|
||||
|
||||
extern "C" {
|
||||
|
||||
error_t i2c_controller_read(Device* device, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2C_DRIVER_API(driver)->read(device, address, data, dataSize, timeout);
|
||||
}
|
||||
|
||||
error_t i2c_controller_write(Device* device, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2C_DRIVER_API(driver)->write(device, address, data, dataSize, timeout);
|
||||
}
|
||||
|
||||
error_t i2c_controller_write_read(Device* device, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2C_DRIVER_API(driver)->write_read(device, address, writeData, writeDataSize, readData, readDataSize, timeout);
|
||||
}
|
||||
|
||||
error_t i2c_controller_read_register(Device* device, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2C_DRIVER_API(driver)->read_register(device, address, reg, data, dataSize, timeout);
|
||||
}
|
||||
|
||||
error_t i2c_controller_write_register(Device* device, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2C_DRIVER_API(driver)->write_register(device, address, reg, data, dataSize, timeout);
|
||||
}
|
||||
|
||||
error_t i2c_controller_write_register_array(Device* device, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
if (dataSize % 2 != 0) {
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
for (int i = 0; i < dataSize; i += 2) {
|
||||
error_t error = I2C_DRIVER_API(driver)->write_register(device, address, data[i], &data[i + 1], 1, timeout);
|
||||
if (error != ERROR_NONE) return error;
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t i2c_controller_has_device_at_address(Device* device, uint8_t address, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
uint8_t message[2] = { 0, 0 };
|
||||
return I2C_DRIVER_API(driver)->write(device, address, message, 2, timeout);
|
||||
}
|
||||
|
||||
const struct DeviceType I2C_CONTROLLER_TYPE {
|
||||
.name = "i2c-controller"
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/i2s_controller.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/device.h>
|
||||
|
||||
#define I2S_DRIVER_API(driver) ((struct I2sControllerApi*)driver->api)
|
||||
|
||||
extern "C" {
|
||||
|
||||
error_t i2s_controller_read(Device* device, void* data, size_t dataSize, size_t* bytesRead, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2S_DRIVER_API(driver)->read(device, data, dataSize, bytesRead, timeout);
|
||||
}
|
||||
|
||||
error_t i2s_controller_write(Device* device, const void* data, size_t dataSize, size_t* bytesWritten, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2S_DRIVER_API(driver)->write(device, data, dataSize, bytesWritten, timeout);
|
||||
}
|
||||
|
||||
error_t i2s_controller_set_config(Device* device, const struct I2sConfig* config) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2S_DRIVER_API(driver)->set_config(device, config);
|
||||
}
|
||||
|
||||
error_t i2s_controller_get_config(Device* device, struct I2sConfig* config) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2S_DRIVER_API(driver)->get_config(device, config);
|
||||
}
|
||||
|
||||
error_t i2s_controller_reset(struct Device* device) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2S_DRIVER_API(driver)->reset(device);
|
||||
}
|
||||
|
||||
const struct DeviceType I2S_CONTROLLER_TYPE {
|
||||
.name = "i2s-controller"
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/root.h>
|
||||
#include <cstring>
|
||||
|
||||
extern "C" {
|
||||
|
||||
bool root_is_model(const struct Device* device, const char* buffer) {
|
||||
auto* config = static_cast<const RootConfig*>(device->config);
|
||||
return strcmp(config->model, buffer) == 0;
|
||||
}
|
||||
|
||||
Driver root_driver = {
|
||||
.name = "root",
|
||||
.compatible = (const char*[]) { "root", nullptr },
|
||||
.start_device = nullptr,
|
||||
.stop_device = nullptr,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.owner = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/spi_controller.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/device.h>
|
||||
|
||||
#define SPI_DRIVER_API(driver) ((struct SpiControllerApi*)driver->api)
|
||||
|
||||
extern "C" {
|
||||
|
||||
error_t spi_controller_lock(struct Device* device) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return SPI_DRIVER_API(driver)->lock(device);
|
||||
}
|
||||
|
||||
error_t spi_controller_try_lock(struct Device* device, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return SPI_DRIVER_API(driver)->try_lock(device, timeout);
|
||||
}
|
||||
|
||||
error_t spi_controller_unlock(struct Device* device) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return SPI_DRIVER_API(driver)->unlock(device);
|
||||
}
|
||||
|
||||
const struct DeviceType SPI_CONTROLLER_TYPE {
|
||||
.name = "spi-controller"
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/uart_controller.h>
|
||||
#include <tactility/error.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/time.h>
|
||||
|
||||
#define UART_DRIVER_API(driver) ((struct UartControllerApi*)driver->api)
|
||||
|
||||
extern "C" {
|
||||
|
||||
error_t uart_controller_read_byte(struct Device* device, uint8_t* out, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->read_byte(device, out, timeout);
|
||||
}
|
||||
|
||||
error_t uart_controller_write_byte(struct Device* device, uint8_t out, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->write_byte(device, out, timeout);
|
||||
}
|
||||
|
||||
error_t uart_controller_write_bytes(struct Device* device, const uint8_t* buffer, size_t buffer_size, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->write_bytes(device, buffer, buffer_size, timeout);
|
||||
}
|
||||
|
||||
error_t uart_controller_read_bytes(struct Device* device, uint8_t* buffer, size_t buffer_size, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->read_bytes(device, buffer, buffer_size, timeout);
|
||||
}
|
||||
|
||||
error_t uart_controller_read_until(struct Device* device, uint8_t* buffer, size_t buffer_size, uint8_t until_byte, bool add_null_terminator, size_t* bytes_read, TickType_t timeout) {
|
||||
size_t read_count = 0;
|
||||
TickType_t start_time = get_ticks();
|
||||
uint8_t* buffer_write_ptr = buffer;
|
||||
uint8_t* buffer_limit = buffer + buffer_size;
|
||||
if (add_null_terminator) {
|
||||
buffer_limit--;
|
||||
}
|
||||
TickType_t timeout_left = timeout;
|
||||
error_t result = ERROR_NONE;
|
||||
|
||||
while (buffer_write_ptr < buffer_limit) {
|
||||
result = uart_controller_read_byte(device, buffer_write_ptr, timeout_left);
|
||||
if (result != ERROR_NONE) {
|
||||
break;
|
||||
}
|
||||
|
||||
read_count++;
|
||||
|
||||
if (*buffer_write_ptr == until_byte) {
|
||||
if (add_null_terminator) {
|
||||
buffer_write_ptr++;
|
||||
*buffer_write_ptr = 0x00U;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
buffer_write_ptr++;
|
||||
|
||||
TickType_t now = get_ticks();
|
||||
if (now > (start_time + timeout)) {
|
||||
result = ERROR_TIMEOUT;
|
||||
break;
|
||||
} else {
|
||||
timeout_left = timeout - (now - start_time);
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes_read != nullptr) {
|
||||
*bytes_read = read_count;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
error_t uart_controller_get_available(struct Device* device, size_t* available) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->get_available(device, available);
|
||||
}
|
||||
|
||||
error_t uart_controller_set_config(struct Device* device, const struct UartConfig* config) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->set_config(device, config);
|
||||
}
|
||||
|
||||
error_t uart_controller_get_config(struct Device* device, struct UartConfig* config) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->get_config(device, config);
|
||||
}
|
||||
|
||||
error_t uart_controller_open(struct Device* device) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->open(device);
|
||||
}
|
||||
|
||||
error_t uart_controller_close(struct Device* device) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->close(device);
|
||||
}
|
||||
|
||||
bool uart_controller_is_open(struct Device* device) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->is_open(device);
|
||||
}
|
||||
|
||||
error_t uart_controller_flush_input(struct Device* device) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return UART_DRIVER_API(driver)->flush_input(device);
|
||||
}
|
||||
|
||||
const struct DeviceType UART_CONTROLLER_TYPE {
|
||||
.name = "uart-controller"
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/error.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
const char* error_to_string(error_t error) {
|
||||
switch (error) {
|
||||
case ERROR_NONE:
|
||||
return "no error";
|
||||
case ERROR_UNDEFINED:
|
||||
return "undefined";
|
||||
case ERROR_INVALID_STATE:
|
||||
return "invalid state";
|
||||
case ERROR_INVALID_ARGUMENT:
|
||||
return "invalid argument";
|
||||
case ERROR_MISSING_PARAMETER:
|
||||
return "missing parameter";
|
||||
case ERROR_NOT_FOUND:
|
||||
return "not found";
|
||||
case ERROR_ISR_STATUS:
|
||||
return "ISR status";
|
||||
case ERROR_RESOURCE:
|
||||
return "resource";
|
||||
case ERROR_TIMEOUT:
|
||||
return "timeout";
|
||||
case ERROR_OUT_OF_MEMORY:
|
||||
return "out of memory";
|
||||
case ERROR_NOT_SUPPORTED:
|
||||
return "not supported";
|
||||
case ERROR_NOT_ALLOWED:
|
||||
return "not allowed";
|
||||
case ERROR_BUFFER_OVERFLOW:
|
||||
return "buffer overflow";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "tactility/dts.h"
|
||||
#include <tactility/kernel_init.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TAG "kernel"
|
||||
|
||||
extern const struct ModuleSymbol KERNEL_SYMBOLS[];
|
||||
|
||||
static error_t start() {
|
||||
extern Driver root_driver;
|
||||
if (driver_construct_add(&root_driver) != ERROR_NONE) return ERROR_RESOURCE;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop() {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
struct Module root_module = {
|
||||
.name = "kernel",
|
||||
.start = start,
|
||||
.stop = stop,
|
||||
.symbols = (const struct ModuleSymbol*)KERNEL_SYMBOLS,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
error_t kernel_init(struct Module* platform_module, struct Module* device_module, struct DtsDevice dts_devices[]) {
|
||||
LOG_I(TAG, "init");
|
||||
|
||||
if (module_construct_add_start(&root_module) != ERROR_NONE) {
|
||||
LOG_E(TAG, "root module init failed");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (module_construct_add_start(platform_module) != ERROR_NONE) {
|
||||
LOG_E(TAG, "platform module init failed");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (device_module != nullptr) {
|
||||
if (module_construct_add_start(device_module) != ERROR_NONE) {
|
||||
LOG_E(TAG, "device module init failed");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
}
|
||||
|
||||
if (dts_devices) {
|
||||
DtsDevice* dts_device = dts_devices;
|
||||
while (dts_device->device != nullptr) {
|
||||
if (dts_device->status == DTS_DEVICE_STATUS_OKAY) {
|
||||
if (device_construct_add_start(dts_device->device, dts_device->compatible) != ERROR_NONE) {
|
||||
LOG_E(TAG, "kernel_init failed to construct+add+start device: %s (%s)", dts_device->device->name, dts_device->compatible);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
} else if (dts_device->status == DTS_DEVICE_STATUS_DISABLED) {
|
||||
if (device_construct_add(dts_device->device, dts_device->compatible) != ERROR_NONE) {
|
||||
LOG_E(TAG, "kernel_init failed to construct+add device: %s (%s)", dts_device->device->name, dts_device->compatible);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
} else {
|
||||
check(false, "DTS status not implemented");
|
||||
}
|
||||
dts_device++;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_I(TAG, "init done");
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,168 @@
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/gpio_controller.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/drivers/i2s_controller.h>
|
||||
#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/module.h>
|
||||
|
||||
/**
|
||||
* 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++.
|
||||
* For example: wrong header includes.
|
||||
*/
|
||||
const struct ModuleSymbol KERNEL_SYMBOLS[] = {
|
||||
// device
|
||||
DEFINE_MODULE_SYMBOL(device_construct),
|
||||
DEFINE_MODULE_SYMBOL(device_destruct),
|
||||
DEFINE_MODULE_SYMBOL(device_add),
|
||||
DEFINE_MODULE_SYMBOL(device_remove),
|
||||
DEFINE_MODULE_SYMBOL(device_start),
|
||||
DEFINE_MODULE_SYMBOL(device_stop),
|
||||
DEFINE_MODULE_SYMBOL(device_construct_add),
|
||||
DEFINE_MODULE_SYMBOL(device_construct_add_start),
|
||||
DEFINE_MODULE_SYMBOL(device_set_parent),
|
||||
DEFINE_MODULE_SYMBOL(device_get_parent),
|
||||
DEFINE_MODULE_SYMBOL(device_set_driver),
|
||||
DEFINE_MODULE_SYMBOL(device_get_driver),
|
||||
DEFINE_MODULE_SYMBOL(device_set_driver_data),
|
||||
DEFINE_MODULE_SYMBOL(device_get_driver_data),
|
||||
DEFINE_MODULE_SYMBOL(device_is_added),
|
||||
DEFINE_MODULE_SYMBOL(device_is_ready),
|
||||
DEFINE_MODULE_SYMBOL(device_is_compatible),
|
||||
DEFINE_MODULE_SYMBOL(device_lock),
|
||||
DEFINE_MODULE_SYMBOL(device_try_lock),
|
||||
DEFINE_MODULE_SYMBOL(device_unlock),
|
||||
DEFINE_MODULE_SYMBOL(device_get_type),
|
||||
DEFINE_MODULE_SYMBOL(device_for_each),
|
||||
DEFINE_MODULE_SYMBOL(device_for_each_child),
|
||||
DEFINE_MODULE_SYMBOL(device_for_each_of_type),
|
||||
DEFINE_MODULE_SYMBOL(device_exists_of_type),
|
||||
// driver
|
||||
DEFINE_MODULE_SYMBOL(driver_construct),
|
||||
DEFINE_MODULE_SYMBOL(driver_destruct),
|
||||
DEFINE_MODULE_SYMBOL(driver_add),
|
||||
DEFINE_MODULE_SYMBOL(driver_remove),
|
||||
DEFINE_MODULE_SYMBOL(driver_construct_add),
|
||||
DEFINE_MODULE_SYMBOL(driver_remove_destruct),
|
||||
DEFINE_MODULE_SYMBOL(driver_bind),
|
||||
DEFINE_MODULE_SYMBOL(driver_unbind),
|
||||
DEFINE_MODULE_SYMBOL(driver_is_compatible),
|
||||
DEFINE_MODULE_SYMBOL(driver_find_compatible),
|
||||
DEFINE_MODULE_SYMBOL(driver_get_device_type),
|
||||
// drivers/gpio_controller
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_acquire),
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_release),
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_set_level),
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_level),
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_set_flags),
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_flags),
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_native_pin_number),
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_pin_number),
|
||||
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_owner_type),
|
||||
DEFINE_MODULE_SYMBOL(gpio_controller_get_pin_count),
|
||||
DEFINE_MODULE_SYMBOL(gpio_controller_init_descriptors),
|
||||
DEFINE_MODULE_SYMBOL(gpio_controller_deinit_descriptors),
|
||||
DEFINE_MODULE_SYMBOL(GPIO_CONTROLLER_TYPE),
|
||||
// drivers/i2c_controller
|
||||
DEFINE_MODULE_SYMBOL(i2c_controller_read),
|
||||
DEFINE_MODULE_SYMBOL(i2c_controller_write),
|
||||
DEFINE_MODULE_SYMBOL(i2c_controller_write_read),
|
||||
DEFINE_MODULE_SYMBOL(i2c_controller_read_register),
|
||||
DEFINE_MODULE_SYMBOL(i2c_controller_write_register),
|
||||
DEFINE_MODULE_SYMBOL(i2c_controller_write_register_array),
|
||||
DEFINE_MODULE_SYMBOL(i2c_controller_has_device_at_address),
|
||||
DEFINE_MODULE_SYMBOL(I2C_CONTROLLER_TYPE),
|
||||
// drivers/i2s_controller
|
||||
DEFINE_MODULE_SYMBOL(i2s_controller_read),
|
||||
DEFINE_MODULE_SYMBOL(i2s_controller_write),
|
||||
DEFINE_MODULE_SYMBOL(i2s_controller_set_config),
|
||||
DEFINE_MODULE_SYMBOL(i2s_controller_get_config),
|
||||
DEFINE_MODULE_SYMBOL(i2s_controller_reset),
|
||||
DEFINE_MODULE_SYMBOL(I2S_CONTROLLER_TYPE),
|
||||
// drivers/root
|
||||
DEFINE_MODULE_SYMBOL(root_is_model),
|
||||
// drivers/spi_controller
|
||||
DEFINE_MODULE_SYMBOL(spi_controller_lock),
|
||||
DEFINE_MODULE_SYMBOL(spi_controller_try_lock),
|
||||
DEFINE_MODULE_SYMBOL(spi_controller_unlock),
|
||||
// drivers/uart_controller
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_open),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_close),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_is_open),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_read_byte),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_read_bytes),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_read_until),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_write_byte),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_write_bytes),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_set_config),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_get_config),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_get_available),
|
||||
DEFINE_MODULE_SYMBOL(uart_controller_flush_input),
|
||||
DEFINE_MODULE_SYMBOL(UART_CONTROLLER_TYPE),
|
||||
// concurrent/dispatcher
|
||||
DEFINE_MODULE_SYMBOL(dispatcher_alloc),
|
||||
DEFINE_MODULE_SYMBOL(dispatcher_free),
|
||||
DEFINE_MODULE_SYMBOL(dispatcher_dispatch_timed),
|
||||
DEFINE_MODULE_SYMBOL(dispatcher_consume_timed),
|
||||
// concurrent/event_group
|
||||
DEFINE_MODULE_SYMBOL(event_group_set),
|
||||
DEFINE_MODULE_SYMBOL(event_group_clear),
|
||||
DEFINE_MODULE_SYMBOL(event_group_get),
|
||||
DEFINE_MODULE_SYMBOL(event_group_wait),
|
||||
// concurrent/thread
|
||||
DEFINE_MODULE_SYMBOL(thread_alloc),
|
||||
DEFINE_MODULE_SYMBOL(thread_alloc_full),
|
||||
DEFINE_MODULE_SYMBOL(thread_free),
|
||||
DEFINE_MODULE_SYMBOL(thread_set_name),
|
||||
DEFINE_MODULE_SYMBOL(thread_set_stack_size),
|
||||
DEFINE_MODULE_SYMBOL(thread_set_affinity),
|
||||
DEFINE_MODULE_SYMBOL(thread_set_main_function),
|
||||
DEFINE_MODULE_SYMBOL(thread_set_priority),
|
||||
DEFINE_MODULE_SYMBOL(thread_set_state_callback),
|
||||
DEFINE_MODULE_SYMBOL(thread_get_state),
|
||||
DEFINE_MODULE_SYMBOL(thread_start),
|
||||
DEFINE_MODULE_SYMBOL(thread_join),
|
||||
DEFINE_MODULE_SYMBOL(thread_get_task_handle),
|
||||
DEFINE_MODULE_SYMBOL(thread_get_return_code),
|
||||
DEFINE_MODULE_SYMBOL(thread_get_stack_space),
|
||||
DEFINE_MODULE_SYMBOL(thread_get_current),
|
||||
// concurrent/timer
|
||||
DEFINE_MODULE_SYMBOL(timer_alloc),
|
||||
DEFINE_MODULE_SYMBOL(timer_free),
|
||||
DEFINE_MODULE_SYMBOL(timer_start),
|
||||
DEFINE_MODULE_SYMBOL(timer_stop),
|
||||
DEFINE_MODULE_SYMBOL(timer_reset_with_interval),
|
||||
DEFINE_MODULE_SYMBOL(timer_reset),
|
||||
DEFINE_MODULE_SYMBOL(timer_is_running),
|
||||
DEFINE_MODULE_SYMBOL(timer_get_expiry_time),
|
||||
DEFINE_MODULE_SYMBOL(timer_set_pending_callback),
|
||||
DEFINE_MODULE_SYMBOL(timer_set_callback_priority),
|
||||
// error
|
||||
DEFINE_MODULE_SYMBOL(error_to_string),
|
||||
// log
|
||||
#ifndef ESP_PLATFORM
|
||||
DEFINE_MODULE_SYMBOL(log_generic),
|
||||
#endif
|
||||
// module
|
||||
DEFINE_MODULE_SYMBOL(module_construct),
|
||||
DEFINE_MODULE_SYMBOL(module_destruct),
|
||||
DEFINE_MODULE_SYMBOL(module_add),
|
||||
DEFINE_MODULE_SYMBOL(module_remove),
|
||||
DEFINE_MODULE_SYMBOL(module_construct_add_start),
|
||||
DEFINE_MODULE_SYMBOL(module_start),
|
||||
DEFINE_MODULE_SYMBOL(module_stop),
|
||||
DEFINE_MODULE_SYMBOL(module_is_started),
|
||||
DEFINE_MODULE_SYMBOL(module_resolve_symbol),
|
||||
DEFINE_MODULE_SYMBOL(module_resolve_symbol_global),
|
||||
// terminator
|
||||
MODULE_SYMBOL_TERMINATOR
|
||||
};
|
||||
@@ -0,0 +1,77 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef ESP_PLATFORM
|
||||
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
static const char* get_log_color(LogLevel level) {
|
||||
using enum LogLevel;
|
||||
switch (level) {
|
||||
case LOG_LEVEL_ERROR:
|
||||
return "\033[1;31m";
|
||||
case LOG_LEVEL_WARNING:
|
||||
return "\033[1;33m";
|
||||
case LOG_LEVEL_INFO:
|
||||
return "\033[32m";
|
||||
case LOG_LEVEL_DEBUG:
|
||||
return "\033[36m";
|
||||
case LOG_LEVEL_VERBOSE:
|
||||
return "\033[37m";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static inline char get_log_prefix(LogLevel level) {
|
||||
using enum LogLevel;
|
||||
switch (level) {
|
||||
case LOG_LEVEL_ERROR:
|
||||
return 'E';
|
||||
case LOG_LEVEL_WARNING:
|
||||
return 'W';
|
||||
case LOG_LEVEL_INFO:
|
||||
return 'I';
|
||||
case LOG_LEVEL_DEBUG:
|
||||
return 'D';
|
||||
case LOG_LEVEL_VERBOSE:
|
||||
return 'V';
|
||||
default:
|
||||
return '?';
|
||||
}
|
||||
}
|
||||
|
||||
static uint64_t get_log_timestamp() {
|
||||
static uint64_t base = 0U;
|
||||
static std::once_flag init_flag;
|
||||
std::call_once(init_flag, []() {
|
||||
timeval time {};
|
||||
gettimeofday(&time, nullptr);
|
||||
base = ((uint64_t)time.tv_sec * 1000U) + (time.tv_usec / 1000U);
|
||||
});
|
||||
timeval time {};
|
||||
gettimeofday(&time, nullptr);
|
||||
uint64_t now = ((uint64_t)time.tv_sec * 1000U) + (time.tv_usec / 1000U);
|
||||
return now - base;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void log_generic(enum LogLevel level, const char* tag, const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
printf("%s %c (%" PRIu64 ") %s ", get_log_color(level), get_log_prefix(level), get_log_timestamp(), tag);
|
||||
vprintf(format, args);
|
||||
printf("\033[0m\n");
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,118 @@
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <new>
|
||||
#include <tactility/concurrent/mutex.h>
|
||||
#include <tactility/module.h>
|
||||
#include <vector>
|
||||
|
||||
#define TAG "module"
|
||||
|
||||
struct ModuleInternal {
|
||||
bool started = false;
|
||||
};
|
||||
|
||||
struct ModuleLedger {
|
||||
std::vector<struct Module*> modules;
|
||||
struct Mutex mutex {};
|
||||
|
||||
ModuleLedger() { mutex_construct(&mutex); }
|
||||
~ModuleLedger() { mutex_destruct(&mutex); }
|
||||
};
|
||||
|
||||
static ModuleLedger ledger;
|
||||
|
||||
extern "C" {
|
||||
|
||||
error_t module_construct(struct 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) {
|
||||
delete static_cast<ModuleInternal*>(module->internal);
|
||||
module->internal = nullptr;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t module_add(struct 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) {
|
||||
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) {
|
||||
LOG_I(TAG, "start %s", module->name);
|
||||
|
||||
auto* internal = static_cast<ModuleInternal*>(module->internal);
|
||||
if (internal->started) return ERROR_NONE;
|
||||
|
||||
error_t error = module->start();
|
||||
internal->started = (error == ERROR_NONE);
|
||||
return error;
|
||||
}
|
||||
|
||||
bool module_is_started(struct Module* module) {
|
||||
return static_cast<ModuleInternal*>(module->internal)->started;
|
||||
}
|
||||
|
||||
error_t module_stop(struct Module* module) {
|
||||
LOG_I(TAG, "stop %s", module->name);
|
||||
|
||||
auto* internal = static_cast<ModuleInternal*>(module->internal);
|
||||
if (!internal->started) return ERROR_NONE;
|
||||
|
||||
error_t error = module->stop();
|
||||
if (error != ERROR_NONE) {
|
||||
return error;
|
||||
}
|
||||
|
||||
internal->started = false;
|
||||
return error;
|
||||
}
|
||||
|
||||
error_t module_construct_add_start(struct Module* module) {
|
||||
error_t error = module_construct(module);
|
||||
if (error != ERROR_NONE) return error;
|
||||
error = module_add(module);
|
||||
if (error != ERROR_NONE) return error;
|
||||
return module_start(module);
|
||||
}
|
||||
|
||||
bool module_resolve_symbol(Module* module, const char* symbol_name, uintptr_t* symbol_address) {
|
||||
if (!module_is_started(module)) return false;
|
||||
auto* symbol_ptr = module->symbols;
|
||||
if (symbol_ptr == nullptr) return false;
|
||||
while (symbol_ptr->name != nullptr) {
|
||||
if (strcmp(symbol_ptr->name, symbol_name) == 0) {
|
||||
*symbol_address = reinterpret_cast<uintptr_t>(symbol_ptr->symbol);
|
||||
return true;
|
||||
}
|
||||
symbol_ptr++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool module_resolve_symbol_global(const char* symbol_name, uintptr_t* symbol_address) {
|
||||
mutex_lock(&ledger.mutex);
|
||||
for (auto* module : ledger.modules) {
|
||||
if (!module_is_started(module))
|
||||
continue;
|
||||
if (module_resolve_symbol(module, symbol_name, symbol_address)) {
|
||||
mutex_unlock(&ledger.mutex);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&ledger.mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user