2020-10-13 11:22:43 +03:00
|
|
|
#include "flipper.h"
|
2020-10-29 10:58:19 +03:00
|
|
|
#include "api-hal-task.h"
|
2020-08-24 18:31:22 +03:00
|
|
|
|
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
|
|
|
|
|
2020-10-05 13:17:34 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-10-08 17:37:19 +03:00
|
|
|
#define DEFAULT_STACK_SIZE 2048 // Stack size in bytes
|
2020-10-29 10:13:56 +03:00
|
|
|
#define MAX_TASK_COUNT 10
|
2020-10-05 13:17:34 +03:00
|
|
|
#define INVALID_TASK_ID UINT16_MAX
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
static StaticTask_t task_info_buffer[MAX_TASK_COUNT];
|
|
|
|
static StackType_t stack_buffer[MAX_TASK_COUNT][DEFAULT_STACK_SIZE / 4];
|
|
|
|
static FuriApp task_buffer[MAX_TASK_COUNT];
|
|
|
|
|
|
|
|
static size_t current_buffer_idx = 0;
|
|
|
|
|
2020-10-05 13:17:34 +03:00
|
|
|
uint16_t furiac_get_task_id_by_name(const char* app_name) {
|
|
|
|
for(size_t i = 0; i < MAX_TASK_RECORDS; i++) {
|
|
|
|
if(strcmp(task_buffer[i].name, app_name) == 0) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INVALID_TASK_ID;
|
|
|
|
}
|
|
|
|
|
2020-10-08 17:37:19 +03:00
|
|
|
void furiac_wait_libs(const FlipperAppLibrary* libs) {
|
2020-10-11 14:03:05 +03:00
|
|
|
for(uint8_t i = 0; i < libs->count; i++) {
|
2020-10-08 17:37:19 +03:00
|
|
|
uint16_t app_id = furiac_get_task_id_by_name(libs->name[i]);
|
2020-10-05 13:17:34 +03:00
|
|
|
|
|
|
|
if(app_id == INVALID_TASK_ID) {
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] Invalid library name %s\n", lib_name);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
while(!task_buffer[app_id].ready) {
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] waiting for library \"%s\"\n", lib_name);
|
|
|
|
#endif
|
|
|
|
osDelay(50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 18:31:22 +03:00
|
|
|
// find task pointer by handle
|
|
|
|
FuriApp* find_task(TaskHandle_t handler) {
|
|
|
|
FuriApp* res = NULL;
|
|
|
|
for(size_t i = 0; i < MAX_TASK_COUNT; i++) {
|
|
|
|
if(task_equal(task_buffer[i].handler, handler)) {
|
|
|
|
res = &task_buffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
FuriApp* furiac_start(FlipperApplication app, const char* name, void* param) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] start %s\n", name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// TODO check first free item (.handler == NULL) and use it
|
|
|
|
|
|
|
|
if(current_buffer_idx >= MAX_TASK_COUNT) {
|
2020-09-30 02:18:30 +03:00
|
|
|
// max task count exceed
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] max task count exceed\n");
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:17:34 +03:00
|
|
|
// application ready
|
|
|
|
task_buffer[current_buffer_idx].ready = false;
|
|
|
|
|
2020-08-24 18:31:22 +03:00
|
|
|
// create task on static stack memory
|
2020-10-02 09:32:11 +03:00
|
|
|
task_buffer[current_buffer_idx].handler = xTaskCreateStatic(
|
|
|
|
(TaskFunction_t)app,
|
|
|
|
(const char* const)name,
|
|
|
|
DEFAULT_STACK_SIZE / 4, // freertos specify stack size in words
|
|
|
|
(void* const)param,
|
|
|
|
tskIDLE_PRIORITY + 3, // normal priority
|
|
|
|
stack_buffer[current_buffer_idx],
|
|
|
|
&task_info_buffer[current_buffer_idx]);
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// save task
|
|
|
|
task_buffer[current_buffer_idx].application = app;
|
|
|
|
task_buffer[current_buffer_idx].prev_name = NULL;
|
|
|
|
task_buffer[current_buffer_idx].prev = NULL;
|
|
|
|
task_buffer[current_buffer_idx].records_count = 0;
|
|
|
|
task_buffer[current_buffer_idx].name = name;
|
|
|
|
|
|
|
|
current_buffer_idx++;
|
|
|
|
|
|
|
|
return &task_buffer[current_buffer_idx - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furiac_kill(FuriApp* app) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] kill %s\n", app->name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// check handler
|
|
|
|
if(app == NULL || app->handler == NULL) return false;
|
|
|
|
|
|
|
|
// kill task
|
|
|
|
vTaskDelete(app->handler);
|
|
|
|
|
|
|
|
// cleanup its registry
|
|
|
|
// TODO realy free memory
|
|
|
|
app->handler = NULL;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void furiac_exit(void* param) {
|
|
|
|
// get current task handler
|
|
|
|
FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
|
|
|
|
|
|
|
|
// run prev
|
|
|
|
if(current_task != NULL) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] exit %s\n", current_task->name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
if(current_task->prev != NULL) {
|
|
|
|
furiac_start(current_task->prev, current_task->prev_name, param);
|
|
|
|
} else {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] no prev\n");
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup registry
|
|
|
|
// TODO realy free memory
|
|
|
|
current_task->handler = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// kill itself
|
2020-09-30 02:18:30 +03:00
|
|
|
vTaskDelete(NULL);
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furiac_switch(FlipperApplication app, char* name, void* param) {
|
|
|
|
// get current task handler
|
|
|
|
FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
|
|
|
|
|
|
|
|
if(current_task == NULL) {
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] no current task found\n");
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|
|
|
|
|
2020-09-30 02:18:30 +03:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] switch %s to %s\n", current_task->name, name);
|
|
|
|
#endif
|
2020-08-24 18:31:22 +03:00
|
|
|
|
|
|
|
// run next
|
|
|
|
FuriApp* next = furiac_start(app, name, param);
|
|
|
|
|
|
|
|
if(next != NULL) {
|
|
|
|
// save current application pointer as prev
|
|
|
|
next->prev = current_task->application;
|
|
|
|
next->prev_name = current_task->name;
|
|
|
|
|
|
|
|
// kill itself
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
2020-10-05 13:17:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// set task to ready state
|
|
|
|
void furiac_ready() {
|
|
|
|
/*
|
|
|
|
TODO:
|
|
|
|
Currently i think that better way is to use application name
|
|
|
|
and restrict applications to "one task per application"
|
|
|
|
*/
|
|
|
|
FuriApp* app = find_task(xTaskGetCurrentTaskHandle());
|
|
|
|
|
|
|
|
if(app == NULL) {
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] cannot find task to set ready state\n");
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] task is ready\n");
|
|
|
|
#endif
|
|
|
|
app->ready = true;
|
|
|
|
}
|
2020-08-24 18:31:22 +03:00
|
|
|
}
|