flipperzero-firmware/core/furi/value-expanders.c
DrZlo13 8f9b2513ff
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 19:09:26 +03:00

178 lines
5.2 KiB
C

#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
const osMutexAttr_t value_mutex_attr = {
.name = NULL, .attr_bits = 0, .cb_mem = NULL, .cb_size = 0U};
composer->mutex = osMutexNew(&value_mutex_attr);
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;
}