2020-08-24 15:31:22 +00:00
|
|
|
#include "furi.h"
|
|
|
|
#include "cmsis_os.h"
|
|
|
|
|
2020-08-26 08:43:18 +00:00
|
|
|
// TODO: this file contains printf, that not implemented on uC target
|
2020-08-24 15:31:22 +00:00
|
|
|
|
2020-08-26 08:43:18 +00:00
|
|
|
#ifdef FURI_DEBUG
|
2020-08-24 15:31:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2020-10-05 10:17:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-08-24 15:31:22 +00:00
|
|
|
#define DEFAULT_STACK_SIZE 1024 // Stack size in bytes
|
|
|
|
#define MAX_TASK_COUNT 8
|
2020-10-05 10:17:34 +00:00
|
|
|
#define INVALID_TASK_ID UINT16_MAX
|
2020-08-24 15:31:22 +00: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 10:17:34 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void furiac_wait_libs(const char* libs) {
|
|
|
|
char* lib_rest = NULL;
|
|
|
|
char* lib_name = strtok_r((char*)libs, " ", &lib_rest);
|
|
|
|
|
|
|
|
while(lib_name != NULL) {
|
|
|
|
// trim library name
|
|
|
|
for(uint16_t i = 0; i < strlen(lib_name); i++) {
|
|
|
|
if(lib_name[i] == ' ') {
|
|
|
|
lib_name[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t app_id = furiac_get_task_id_by_name(lib_name);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lib_name = strtok_r(NULL, " ", &lib_rest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:31:22 +00: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-29 23:18:30 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] start %s\n", name);
|
|
|
|
#endif
|
2020-08-24 15:31:22 +00:00
|
|
|
|
|
|
|
// TODO check first free item (.handler == NULL) and use it
|
|
|
|
|
|
|
|
if(current_buffer_idx >= MAX_TASK_COUNT) {
|
2020-09-29 23:18:30 +00:00
|
|
|
// max task count exceed
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] max task count exceed\n");
|
|
|
|
#endif
|
2020-08-24 15:31:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-05 10:17:34 +00:00
|
|
|
// application ready
|
|
|
|
task_buffer[current_buffer_idx].ready = false;
|
|
|
|
|
2020-08-24 15:31:22 +00:00
|
|
|
// create task on static stack memory
|
2020-10-02 06:32:11 +00: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 15:31:22 +00: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-29 23:18:30 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] kill %s\n", app->name);
|
|
|
|
#endif
|
2020-08-24 15:31:22 +00: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-29 23:18:30 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] exit %s\n", current_task->name);
|
|
|
|
#endif
|
2020-08-24 15:31:22 +00:00
|
|
|
|
|
|
|
if(current_task->prev != NULL) {
|
|
|
|
furiac_start(current_task->prev, current_task->prev_name, param);
|
|
|
|
} else {
|
2020-09-29 23:18:30 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] no prev\n");
|
|
|
|
#endif
|
2020-08-24 15:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup registry
|
|
|
|
// TODO realy free memory
|
|
|
|
current_task->handler = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// kill itself
|
2020-09-29 23:18:30 +00:00
|
|
|
vTaskDelete(NULL);
|
2020-08-24 15:31:22 +00: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-29 23:18:30 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] no current task found\n");
|
|
|
|
#endif
|
2020-08-24 15:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
printf("[FURIAC] switch %s to %s\n", current_task->name, name);
|
|
|
|
#endif
|
2020-08-24 15:31:22 +00: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 10:17:34 +00: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 15:31:22 +00:00
|
|
|
}
|