[FL-872] Furi, API-HAL, App-Loader cleanup and improvements (#334)

* Furi: replace obsolete furiac_exit with osThreadExit, drop obsolete apis and test. Rename systemd to flipper and move to separate file, cleanup. ApiHal: Rename timebase to os and move freertos hooks there, move insomnia api to power module.
* Furi: new thread helper
* Furi: cleanup thread documentation
* Flipper, AppLoader: update to use FuriThread. Update tasks signatures to match FuriThreadCallback signature.
* F4: rename API_HAL_TIMEBASE_DEBUG to API_HAL_OS_DEBUG
* Applications: rename FuriApplication to FlipperApplication, use FuriThreadCallback signature for apps.
* C++ app template sample, new exit method
This commit is contained in:
あく
2021-02-12 20:24:34 +03:00
committed by GitHub
parent 7c5de59f53
commit b835d7a451
67 changed files with 906 additions and 1494 deletions

26
core/flipper.c Normal file
View File

@@ -0,0 +1,26 @@
#include "flipper.h"
#include <applications.h>
#include <furi.h>
void flipper_init() {
printf("[flipper] Build date:" BUILD_DATE ". "
"Git Commit:" GIT_COMMIT ". "
"Git Branch:" GIT_BRANCH ". "
"Commit Number:" GIT_BRANCH_NUM "\r\n");
printf("[flipper] starting services\r\n");
for(size_t i = 0; i < FLIPPER_SERVICES_COUNT; i++) {
printf("[flipper] starting service %s\r\n", FLIPPER_SERVICES[i].name);
FuriThread* thread = furi_thread_alloc();
furi_thread_set_name(thread, FLIPPER_SERVICES[i].name);
furi_thread_set_stack_size(thread, FLIPPER_SERVICES[i].stack_size);
furi_thread_set_callback(thread, FLIPPER_SERVICES[i].app);
furi_thread_start(thread);
}
printf("[flipper] services startup complete\r\n");
}

3
core/flipper.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void flipper_init();

View File

@@ -1,12 +1,4 @@
#include "furi.h"
#include <applications.h>
// for testing purpose
uint32_t exitcode = 0;
void set_exitcode(uint32_t _exitcode) {
exitcode = _exitcode;
}
void furi_init() {
gpio_api_init();
@@ -14,25 +6,3 @@ void furi_init() {
furi_record_init();
furi_stdglue_init();
}
int systemd() {
furi_init();
printf("[systemd] furi initialized\r\n");
// FURI startup
for(size_t i = 0; i < FLIPPER_SERVICES_size(); i++) {
printf("[systemd] starting service %s\r\n", FLIPPER_SERVICES[i].name);
osThreadAttr_t* attr = furi_alloc(sizeof(osThreadAttr_t));
attr->name = FLIPPER_SERVICES[i].name;
attr->stack_size = FLIPPER_SERVICES[i].stack_size;
osThreadNew(FLIPPER_SERVICES[i].app, NULL, attr);
}
printf("[systemd] all services started\r\n");
while(1) {
osThreadSuspend(osThreadGetId());
}
printf("[systemd] === Bye from Flipper Zero! ===\r\n");
return (int)exitcode;
}

View File

@@ -3,12 +3,11 @@
#include <cmsis_os2.h>
#include <furi/check.h>
#include <furi/event.h>
#include <furi/memmgr.h>
#include <furi/pubsub.h>
#include <furi/record.h>
#include <furi/stdglue.h>
#include <furi/value-expanders.h>
#include <furi/thread.h>
#include <furi/valuemutex.h>
#include <api-hal/api-gpio.h>
@@ -21,7 +20,7 @@
extern "C" {
#endif
#define furiac_exit(ptr) osThreadExit()
void furi_init();
#ifdef __cplusplus
}

View File

@@ -1,24 +0,0 @@
#include "event.h"
#include <string.h>
bool init_event(Event* event) {
event->semaphore_id = osSemaphoreNew(1, 0, NULL);
return event->semaphore_id != NULL;
}
bool delete_event(Event* event) {
return osSemaphoreDelete(event->semaphore_id) == osOK;
}
void signal_event(Event* event) {
// Ignore the result, as we do not care about repeated event signalling.
osSemaphoreRelease(event->semaphore_id);
}
void wait_event(Event* event) {
wait_event_with_timeout(event, osWaitForever);
}
bool wait_event_with_timeout(Event* event, uint32_t timeout_ms) {
return osSemaphoreAcquire(event->semaphore_id, timeout_ms) == osOK;
}

View File

@@ -1,44 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <cmsis_os2.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
osSemaphoreId_t semaphore_id;
} Event;
/*
Creates Event.
*/
bool init_event(Event* event);
/*
Free resources allocated by `init_event`.
This function doesn't free the memory occupied by `Event` itself.
*/
bool delete_event(Event* event);
/*
Signals the event.
If the event is already in "signalled" state, nothing happens.
*/
void signal_event(Event* event);
/*
Waits until the event is signalled.
*/
void wait_event(Event* event);
/*
Waits with a timeout until the event is signalled.
*/
bool wait_event_with_timeout(Event* event, uint32_t timeout_ms);
#ifdef __cplusplus
}
#endif

122
core/furi/thread.c Normal file
View File

@@ -0,0 +1,122 @@
#include "thread.h"
#include "memmgr.h"
#include "check.h"
#include <m-string.h>
struct FuriThread {
FuriThreadState state;
int32_t ret;
FuriThreadCallback callback;
void* context;
FuriThreadStateCallback state_callback;
void* state_context;
osThreadAttr_t attr;
osThreadId_t id;
};
void furi_thread_set_state(FuriThread* thread, FuriThreadState state) {
furi_assert(thread);
thread->state = state;
if(thread->state_callback) {
thread->state_callback(state, thread->state_context);
}
}
void furi_thread_body(void* context) {
furi_assert(context);
FuriThread* thread = context;
furi_assert(thread->state == FuriThreadStateStarting);
furi_thread_set_state(thread, FuriThreadStateRunning);
thread->ret = thread->callback(thread->context);
furi_assert(thread->state == FuriThreadStateRunning);
furi_thread_set_state(thread, FuriThreadStateStopped);
osThreadExit();
}
FuriThread* furi_thread_alloc() {
FuriThread* thread = furi_alloc(sizeof(FuriThread));
return thread;
}
void furi_thread_free(FuriThread* thread) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
if(thread->attr.name) free((void*)thread->attr.name);
free(thread);
}
void furi_thread_set_name(FuriThread* thread, const char* name) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
if(thread->attr.name) free((void*)thread->attr.name);
thread->attr.name = strdup(name);
}
void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
thread->attr.stack_size = stack_size;
}
void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
thread->callback = callback;
}
void furi_thread_set_context(FuriThread* thread, void* context) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
thread->context = context;
}
void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
thread->state_callback = callback;
}
void furi_thread_set_state_context(FuriThread* thread, void* context) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
thread->state_context = context;
}
bool furi_thread_start(FuriThread* thread) {
furi_assert(thread);
furi_assert(thread->callback);
furi_assert(thread->state == FuriThreadStateStopped);
furi_thread_set_state(thread, FuriThreadStateStarting);
thread->id = osThreadNew(furi_thread_body, thread, &thread->attr);
if(thread->id) {
return true;
} else {
furi_assert(thread->state == FuriThreadStateStarting);
furi_thread_set_state(thread, FuriThreadStateStopped);
return false;
}
}
osStatus_t furi_thread_terminate(FuriThread* thread) {
furi_assert(thread);
osStatus_t ret = osThreadTerminate(thread->id);
if(ret == osOK) {
furi_thread_set_state(thread, FuriThreadStateStopped);
}
return ret;
}
osStatus_t furi_thread_join(FuriThread* thread) {
furi_assert(thread);
return osThreadJoin(thread->id);
}

102
core/furi/thread.h Normal file
View File

@@ -0,0 +1,102 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <cmsis_os2.h>
#ifdef __cplusplus
extern "C" {
#endif
/* FuriThreadState */
typedef enum {
FuriThreadStateStopped,
FuriThreadStateStarting,
FuriThreadStateRunning,
} FuriThreadState;
/* FuriThread anonymous structure */
typedef struct FuriThread FuriThread;
/* FuriThreadCallback
* Your callback to run in new thread
* @warning don't use osThreadExit
*/
typedef int32_t (*FuriThreadCallback)(void* context);
/* FuriThread state change calback
* called upon thread state change
* @param state - new thread state
* @param context - callback context
*/
typedef void (*FuriThreadStateCallback)(FuriThreadState state, void* context);
/* Allocate FuriThread
* @return FuriThread instance
*/
FuriThread* furi_thread_alloc();
/* Release FuriThread
* @param thread - FuriThread instance
*/
void furi_thread_free(FuriThread* thread);
/* Set FuriThread name
* @param thread - FuriThread instance
* @param name - string
*/
void furi_thread_set_name(FuriThread* thread, const char* name);
/* Set FuriThread stack size
* @param thread - FuriThread instance
* @param stack_size - stack size in bytes
*/
void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
/* Set FuriThread callback
* @param thread - FuriThread instance
* @param callback - FuriThreadCallback, called upon thread run
*/
void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
/* Set FuriThread context
* @param thread - FuriThread instance
* @param context - pointer to context for thread callback
*/
void furi_thread_set_context(FuriThread* thread, void* context);
/* Set FuriThread state change callback
* @param thread - FuriThread instance
* @param callack - state change callback
*/
void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
/* Set FuriThread state change context
* @param thread - FuriThread instance
* @param context - pointer to context
*/
void furi_thread_set_state_context(FuriThread* thread, void* context);
/* Start FuriThread
* @param thread - FuriThread instance
* @return true on success
*/
bool furi_thread_start(FuriThread* thread);
/* Treminate FuriThread
* @param thread - FuriThread instance
* @return osStatus_t
* @warning terminating statefull thread is dangerous
* use only if you know what you doing
*/
osStatus_t furi_thread_terminate(FuriThread* thread);
/* Join FuriThread
* @param thread - FuriThread instance
* @return osStatus_t
*/
osStatus_t furi_thread_join(FuriThread* thread);
#ifdef __cplusplus
}
#endif

View File

@@ -1,174 +0,0 @@
#include "value-expanders.h"
bool init_composer(ValueComposer* composer, void* value) {
if(!init_mutex(&composer->value, value, 0)) return false;
for(size_t i = 0; i < sizeof(composer->layers) / sizeof(composer->layers[0]); i++) {
list_composer_cb_init(composer->layers[i]);
}
// mutex without name,
// no attributes (unfortunatly robust mutex is not supported by FreeRTOS),
// with dynamic memory allocation
composer->mutex = osMutexNew(NULL);
if(composer->mutex == NULL) return false;
if(!init_event(&composer->request)) return false;
return true;
}
bool delete_composer(ValueComposer* composer) {
if(osMutexAcquire(composer->mutex, osWaitForever) == osOK) {
bool result = true;
result &= delete_mutex(&composer->value);
for(size_t i = 0; i < sizeof(composer->layers) / sizeof(composer->layers[0]); i++) {
list_composer_cb_clear(composer->layers[i]);
}
result &= osMutexDelete(composer->mutex) == osOK;
return result;
} else {
return false;
}
}
ValueComposerHandle*
add_compose_layer(ValueComposer* composer, ValueComposerCallback cb, void* ctx, UiLayer layer) {
if(osMutexAcquire(composer->mutex, osWaitForever) == osOK) {
// put uninitialized item to the list
ValueComposerHandle* handle = list_composer_cb_push_raw(composer->layers[layer]);
handle->cb = cb;
handle->ctx = ctx;
handle->layer = layer;
handle->composer = composer;
// TODO unregister handle on app exit
//flapp_on_exit(remove_compose_layer, handle);
osMutexRelease(composer->mutex);
// Layers changed, request composition
signal_event(&composer->request);
return handle;
} else {
return NULL;
}
}
bool remove_compose_layer(ValueComposerHandle* handle) {
ValueComposer* composer = handle->composer;
if(osMutexAcquire(composer->mutex, osWaitForever) == osOK) {
bool result = false;
// iterate over items
list_composer_cb_it_t it;
for(list_composer_cb_it(it, composer->layers[handle->layer]); !list_composer_cb_end_p(it);
list_composer_cb_next(it)) {
const ValueComposerHandle* item = list_composer_cb_cref(it);
// if the iterator is equal to our element
if(item == handle) {
list_composer_cb_remove(composer->layers[handle->layer], it);
result = true;
break;
}
}
osMutexRelease(composer->mutex);
// Layers changed, request composition
signal_event(&composer->request);
return result;
} else {
return false;
}
}
void request_compose(ValueComposerHandle* handle) {
ValueComposer* composer = handle->composer;
signal_event(&composer->request);
}
void perform_compose(
ValueComposer* composer,
ValueComposerCallback start_cb,
ValueComposerCallback end_cb,
void* ctx) {
if(!wait_event_with_timeout(&composer->request, 0)) return;
void* state = acquire_mutex(&composer->value, 0);
if(state == NULL) return;
if(start_cb != NULL) start_cb(ctx, state);
perform_compose_internal(composer, state);
if(end_cb != NULL) end_cb(ctx, state);
release_mutex(&composer->value, state);
}
void perform_compose_internal(ValueComposer* composer, void* state) {
if(osMutexAcquire(composer->mutex, osWaitForever) == osOK) {
// Compose all levels for now
for(size_t i = 0; i < sizeof(composer->layers) / sizeof(composer->layers[0]); i++) {
// iterate over items
list_composer_cb_it_t it;
for(list_composer_cb_it(it, composer->layers[i]); !list_composer_cb_end_p(it);
list_composer_cb_next(it)) {
const ValueComposerHandle* h = list_composer_cb_cref(it);
h->cb(h->ctx, state);
}
}
osMutexRelease(composer->mutex);
}
}
void COPY_COMPOSE(void* ctx, void* state) {
read_mutex((ValueMutex*)ctx, state, 0, osWaitForever);
}
bool init_managed(ValueManager* managed, void* value, size_t size) {
if(!init_pubsub(&managed->pubsub)) return false;
if(!init_mutex(&managed->value, value, size)) {
delete_pubsub(&managed->pubsub);
return false;
}
return true;
}
bool delete_managed(ValueManager* managed) {
bool result = true;
result &= delete_mutex(&managed->value);
result &= delete_pubsub(&managed->pubsub);
return result;
}
bool write_managed(ValueManager* managed, void* data, size_t len, uint32_t timeout) {
void* value = acquire_mutex(&managed->value, timeout);
if(value == NULL) return false;
memcpy(value, data, len);
notify_pubsub(&managed->pubsub, value);
if(!release_mutex(&managed->value, value)) return false;
return true;
}
bool commit_managed(ValueManager* managed, void* value) {
if(value != managed->value.value) return false;
notify_pubsub(&managed->pubsub, value);
if(!release_mutex(&managed->value, value)) return false;
return true;
}

View File

@@ -1,115 +0,0 @@
#pragma once
#include "valuemutex.h"
#include "pubsub.h"
#include "event.h"
#include <m-list.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
== Value composer ==
*/
typedef struct ValueComposer ValueComposer;
typedef void (*ValueComposerCallback)(void* ctx, void* state);
typedef enum { UiLayerBelowNotify, UiLayerNotify, UiLayerAboveNotify } UiLayer;
typedef struct {
ValueComposerCallback cb;
void* ctx;
UiLayer layer;
ValueComposer* composer;
} ValueComposerHandle;
LIST_DEF(list_composer_cb, ValueComposerHandle, M_POD_OPLIST);
struct ValueComposer {
ValueMutex value;
list_composer_cb_t layers[3];
osMutexId_t mutex;
Event request;
};
void COPY_COMPOSE(void* ctx, void* state);
bool init_composer(ValueComposer* composer, void* value);
/*
Free resources allocated by `init_composer`.
This function doesn't free the memory occupied by `ValueComposer` itself.
*/
bool delete_composer(ValueComposer* composer);
ValueComposerHandle*
add_compose_layer(ValueComposer* composer, ValueComposerCallback cb, void* ctx, UiLayer layer);
bool remove_compose_layer(ValueComposerHandle* handle);
void request_compose(ValueComposerHandle* handle);
/*
Perform composition if requested.
`start_cb` and `end_cb` will be called before and after all layer callbacks, respectively.
Both `start_cb` and `end_cb` can be NULL. They can be used to set initial state (e.g. clear screen)
and commit the final state.
*/
void perform_compose(
ValueComposer* composer,
ValueComposerCallback start_cb,
ValueComposerCallback end_cb,
void* ctx);
/*
Perform composition.
This function should be called with value mutex acquired.
This function is here for convenience, so that developers can write their own compose loops.
See `perform_compose` function body for an example.
*/
void perform_compose_internal(ValueComposer* composer, void* state);
// See [LED](LED-API) or [Display](Display-API) API for examples.
/*
== ValueManager ==
More complicated concept is ValueManager.
It is like ValueMutex, but user can subscribe to value updates.
First of all you can use value and pubsub part as showing above:
aquire/release mutex, read value, subscribe/unsubscribe pubsub.
There are two specific methods for ValueManager: write_managed, commit_managed
*/
typedef struct {
ValueMutex value;
PubSub pubsub;
} ValueManager;
bool init_managed(ValueManager* managed, void* value, size_t size);
/*
Free resources allocated by `init_managed`.
This function doesn't free the memory occupied by `ValueManager` itself.
*/
bool delete_managed(ValueManager* managed);
/*
acquire value, changes it and send notify with current value.
*/
bool write_managed(ValueManager* managed, void* data, size_t len, uint32_t timeout);
/*
commit_managed works as `release_mutex` but send notify with current value.
*/
bool commit_managed(ValueManager* managed, void* value);
#ifdef __cplusplus
}
#endif