2020-10-13 11:22:43 +03:00
|
|
|
#include "furi-deprecated.h"
|
2020-08-24 18:31:22 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-08-26 11:43:18 +03:00
|
|
|
// TODO: this file contains printf, that not implemented on uC target
|
2020-08-24 18:31:22 +03:00
|
|
|
|
2020-08-26 11:43:18 +03:00
|
|
|
#ifdef FURI_DEBUG
|
2020-08-24 18:31:22 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_RECORD_COUNT 32
|
|
|
|
|
|
|
|
static FuriRecord records[MAX_RECORD_COUNT];
|
|
|
|
static size_t current_buffer_idx = 0;
|
|
|
|
|
|
|
|
// find record pointer by name
|
|
|
|
static FuriRecord* find_record(const char* name) {
|
|
|
|
if(name == NULL) return NULL;
|
|
|
|
|
|
|
|
FuriRecord* res = NULL;
|
|
|
|
for(size_t i = 0; i < MAX_RECORD_COUNT; i++) {
|
|
|
|
if(records[i].name != NULL && strcmp(name, records[i].name) == 0) {
|
|
|
|
res = &records[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-09-01 13:34:23 +03:00
|
|
|
// TODO: change open-create to only open
|
2020-10-13 11:22:43 +03:00
|
|
|
bool furi_create_deprecated(const char* name, void* value, size_t size) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] creating %s record\n", name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
2020-09-01 13:34:23 +03:00
|
|
|
FuriRecord* record = find_record(name);
|
|
|
|
|
|
|
|
if(record != NULL) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] record already exist\n");
|
|
|
|
#endif
|
2020-09-01 13:34:23 +03:00
|
|
|
|
|
|
|
record->value = value;
|
|
|
|
record->size = size;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// record not exist, create new
|
|
|
|
|
2020-08-24 18:31:22 +03:00
|
|
|
if(current_buffer_idx >= MAX_RECORD_COUNT) {
|
2020-09-30 02:18:30 +03:00
|
|
|
// max record count exceed
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] create: max record count exceed\n");
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
records[current_buffer_idx].mute_counter = 0;
|
2020-09-30 02:18:30 +03:00
|
|
|
records[current_buffer_idx].mutex =
|
|
|
|
xSemaphoreCreateMutexStatic(&records[current_buffer_idx].mutex_buffer);
|
2020-08-24 18:31:22 +03:00
|
|
|
records[current_buffer_idx].value = value;
|
|
|
|
records[current_buffer_idx].size = size;
|
|
|
|
records[current_buffer_idx].name = name;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
|
|
|
|
records[current_buffer_idx].subscribers[i].allocated = false;
|
2020-09-01 13:34:23 +03:00
|
|
|
records[current_buffer_idx].subscribers[i].ctx = NULL;
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
current_buffer_idx++;
|
|
|
|
|
2020-08-24 18:31:22 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-13 11:22:43 +03:00
|
|
|
FuriRecordSubscriber* furi_open_deprecated(
|
2020-10-02 09:32:11 +03:00
|
|
|
const char* name,
|
|
|
|
bool solo,
|
|
|
|
bool no_mute,
|
|
|
|
FlipperRecordCallback value_callback,
|
|
|
|
FlipperRecordStateCallback state_callback,
|
|
|
|
void* ctx) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] opening %s record\n", name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// get furi record by name
|
|
|
|
FuriRecord* record = find_record(name);
|
|
|
|
|
|
|
|
if(record == NULL) {
|
2020-09-30 02:18:30 +03:00
|
|
|
// cannot find record
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] cannot find record %s\n", name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
2020-09-01 13:34:23 +03:00
|
|
|
// create record if not exist
|
2020-10-13 11:22:43 +03:00
|
|
|
if(!furi_create_deprecated(name, NULL, 0)) {
|
2020-09-01 13:34:23 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
record = find_record(name);
|
|
|
|
|
|
|
|
if(record == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// allocate subscriber
|
|
|
|
FuriRecordSubscriber* subscriber = NULL;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
|
2020-08-26 21:32:22 +03:00
|
|
|
if(!record->subscribers[i].allocated) {
|
|
|
|
subscriber = &record->subscribers[i];
|
2020-08-24 18:31:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(subscriber == NULL) {
|
2020-09-30 02:18:30 +03:00
|
|
|
// cannot add subscriber (full)
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] open: cannot add subscriber (full)\n");
|
|
|
|
#endif
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
return NULL;
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// increase mute_counter
|
|
|
|
if(solo) {
|
|
|
|
record->mute_counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set all parameters
|
|
|
|
subscriber->allocated = true;
|
|
|
|
subscriber->mute_counter = record->mute_counter;
|
|
|
|
subscriber->no_mute = no_mute;
|
|
|
|
subscriber->cb = value_callback;
|
|
|
|
subscriber->state_cb = state_callback;
|
2020-08-26 21:32:22 +03:00
|
|
|
subscriber->record = record;
|
2020-09-01 13:34:23 +03:00
|
|
|
subscriber->ctx = ctx;
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// register record in application
|
|
|
|
FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
if(current_task != NULL) {
|
|
|
|
current_task->records[current_task->records_count] = record;
|
|
|
|
current_task->records_count++;
|
|
|
|
} else {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] open: no current task\n");
|
|
|
|
#endif
|
2020-08-26 21:32:22 +03:00
|
|
|
}
|
2020-08-24 18:31:22 +03:00
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
return subscriber;
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
void furi_close(FuriRecordSubscriber* handler) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] closing %s record\n", handler->record->name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// deallocate subscriber
|
2020-08-26 21:32:22 +03:00
|
|
|
handler->allocated = false;
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// set mute counter to next max value
|
|
|
|
uint8_t max_mute_counter = 0;
|
|
|
|
for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
|
|
|
|
if(handler->record->subscribers[i].allocated) {
|
|
|
|
if(handler->record->subscribers[i].mute_counter > max_mute_counter) {
|
|
|
|
max_mute_counter = handler->record->subscribers[i].mute_counter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler->record->mute_counter = max_mute_counter;
|
|
|
|
}
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
static void furi_notify(FuriRecordSubscriber* handler, const void* value, size_t size) {
|
2020-08-24 18:31:22 +03:00
|
|
|
for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
|
|
|
|
if(handler->record->subscribers[i].allocated) {
|
|
|
|
if(handler->record->subscribers[i].cb != NULL) {
|
2020-09-01 13:34:23 +03:00
|
|
|
handler->record->subscribers[i].cb(
|
2020-09-30 02:18:30 +03:00
|
|
|
value, size, handler->record->subscribers[i].ctx);
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
void* furi_take(FuriRecordSubscriber* handler) {
|
2020-09-01 13:34:23 +03:00
|
|
|
if(handler == NULL || handler->record == NULL) return NULL;
|
2020-08-24 18:31:22 +03:00
|
|
|
|
2020-09-30 02:18:30 +03:00
|
|
|
if(xSemaphoreTake(handler->record->mutex, portMAX_DELAY) == pdTRUE) {
|
2020-09-07 17:35:18 +03:00
|
|
|
return handler->record->value;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
void furi_give(FuriRecordSubscriber* handler) {
|
2020-09-01 13:34:23 +03:00
|
|
|
if(handler == NULL || handler->record == NULL) return;
|
|
|
|
|
2020-09-07 17:35:18 +03:00
|
|
|
xSemaphoreGive(handler->record->mutex);
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
2020-09-01 13:34:23 +03:00
|
|
|
void furi_commit(FuriRecordSubscriber* handler) {
|
|
|
|
if(handler == NULL || handler->record == NULL) return;
|
|
|
|
|
|
|
|
furi_notify(handler, handler->record->value, handler->record->size);
|
2020-09-17 12:58:45 +03:00
|
|
|
furi_give(handler);
|
2020-09-01 13:34:23 +03:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
bool furi_read(FuriRecordSubscriber* handler, void* value, size_t size) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] read from %s\n", handler->record->name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
if(handler == NULL || handler->record == NULL || value == NULL) return false;
|
|
|
|
|
|
|
|
if(size > handler->record->size) return false;
|
|
|
|
|
|
|
|
// return false if read from pipe
|
|
|
|
if(handler->record->value == NULL) return false;
|
|
|
|
|
|
|
|
furi_take(handler);
|
|
|
|
memcpy(value, handler->record->value, size);
|
|
|
|
furi_notify(handler, value, size);
|
2020-09-17 12:58:45 +03:00
|
|
|
furi_give(handler);
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
bool furi_write(FuriRecordSubscriber* handler, const void* value, size_t size) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] write to %s\n", handler->record->name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
if(handler == NULL || handler->record == NULL || value == NULL) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
2020-10-02 09:32:11 +03:00
|
|
|
printf(
|
|
|
|
"[FURI] write: null param %x %x\n",
|
|
|
|
(uint32_t)(size_t)handler,
|
|
|
|
(uint32_t)(size_t)value);
|
2020-09-30 02:18:30 +03:00
|
|
|
#endif
|
2020-08-26 21:32:22 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// check if closed
|
2020-08-26 21:32:22 +03:00
|
|
|
if(!handler->allocated) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] write: handler closed\n");
|
|
|
|
#endif
|
2020-08-26 21:32:22 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-24 18:31:22 +03:00
|
|
|
|
2020-08-26 21:32:22 +03:00
|
|
|
if(handler->record->value != NULL && size > handler->record->size) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] write: wrong size %d\n", (uint32_t)size);
|
|
|
|
#endif
|
2020-08-26 21:32:22 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// check mute
|
2020-09-30 02:18:30 +03:00
|
|
|
if(handler->record->mute_counter != handler->mute_counter && !handler->no_mute) {
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURI] write: muted\n");
|
|
|
|
#endif
|
2020-08-26 21:32:22 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-24 18:31:22 +03:00
|
|
|
|
2020-09-17 12:58:45 +03:00
|
|
|
furi_take(handler);
|
2020-08-24 18:31:22 +03:00
|
|
|
if(handler->record->value != NULL) {
|
|
|
|
// real write to value
|
|
|
|
memcpy(handler->record->value, value, size);
|
|
|
|
|
|
|
|
// notify subscribers
|
|
|
|
furi_notify(handler, handler->record->value, handler->record->size);
|
|
|
|
} else {
|
|
|
|
furi_notify(handler, value, size);
|
|
|
|
}
|
2020-09-17 12:58:45 +03:00
|
|
|
furi_give(handler);
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|