furi_ac.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "furi.h"
  2. #include "cmsis_os.h"
  3. // TODO: this file contains printf, that not implemented on uC target
  4. #ifdef FURI_DEBUG
  5. #include <stdio.h>
  6. #endif
  7. #include <string.h>
  8. #define DEFAULT_STACK_SIZE 1024 // Stack size in bytes
  9. #define MAX_TASK_COUNT 8
  10. #define INVALID_TASK_ID UINT16_MAX
  11. static StaticTask_t task_info_buffer[MAX_TASK_COUNT];
  12. static StackType_t stack_buffer[MAX_TASK_COUNT][DEFAULT_STACK_SIZE / 4];
  13. static FuriApp task_buffer[MAX_TASK_COUNT];
  14. static size_t current_buffer_idx = 0;
  15. uint16_t furiac_get_task_id_by_name(const char* app_name) {
  16. for(size_t i = 0; i < MAX_TASK_RECORDS; i++) {
  17. if(strcmp(task_buffer[i].name, app_name) == 0) return i;
  18. }
  19. return INVALID_TASK_ID;
  20. }
  21. void furiac_wait_libs(const char* libs) {
  22. char* lib_rest = NULL;
  23. char* lib_name = strtok_r((char*)libs, " ", &lib_rest);
  24. while(lib_name != NULL) {
  25. // trim library name
  26. for(uint16_t i = 0; i < strlen(lib_name); i++) {
  27. if(lib_name[i] == ' ') {
  28. lib_name[i] = 0;
  29. }
  30. }
  31. uint16_t app_id = furiac_get_task_id_by_name(lib_name);
  32. if(app_id == INVALID_TASK_ID) {
  33. #ifdef FURI_DEBUG
  34. printf("[FURIAC] Invalid library name %s\n", lib_name);
  35. #endif
  36. } else {
  37. while(!task_buffer[app_id].ready) {
  38. #ifdef FURI_DEBUG
  39. printf("[FURIAC] waiting for library \"%s\"\n", lib_name);
  40. #endif
  41. osDelay(50);
  42. }
  43. }
  44. lib_name = strtok_r(NULL, " ", &lib_rest);
  45. }
  46. }
  47. // find task pointer by handle
  48. FuriApp* find_task(TaskHandle_t handler) {
  49. FuriApp* res = NULL;
  50. for(size_t i = 0; i < MAX_TASK_COUNT; i++) {
  51. if(task_equal(task_buffer[i].handler, handler)) {
  52. res = &task_buffer[i];
  53. }
  54. }
  55. return res;
  56. }
  57. FuriApp* furiac_start(FlipperApplication app, const char* name, void* param) {
  58. #ifdef FURI_DEBUG
  59. printf("[FURIAC] start %s\n", name);
  60. #endif
  61. // TODO check first free item (.handler == NULL) and use it
  62. if(current_buffer_idx >= MAX_TASK_COUNT) {
  63. // max task count exceed
  64. #ifdef FURI_DEBUG
  65. printf("[FURIAC] max task count exceed\n");
  66. #endif
  67. return NULL;
  68. }
  69. // application ready
  70. task_buffer[current_buffer_idx].ready = false;
  71. // create task on static stack memory
  72. task_buffer[current_buffer_idx].handler = xTaskCreateStatic(
  73. (TaskFunction_t)app,
  74. (const char* const)name,
  75. DEFAULT_STACK_SIZE / 4, // freertos specify stack size in words
  76. (void* const)param,
  77. tskIDLE_PRIORITY + 3, // normal priority
  78. stack_buffer[current_buffer_idx],
  79. &task_info_buffer[current_buffer_idx]);
  80. // save task
  81. task_buffer[current_buffer_idx].application = app;
  82. task_buffer[current_buffer_idx].prev_name = NULL;
  83. task_buffer[current_buffer_idx].prev = NULL;
  84. task_buffer[current_buffer_idx].records_count = 0;
  85. task_buffer[current_buffer_idx].name = name;
  86. current_buffer_idx++;
  87. return &task_buffer[current_buffer_idx - 1];
  88. }
  89. bool furiac_kill(FuriApp* app) {
  90. #ifdef FURI_DEBUG
  91. printf("[FURIAC] kill %s\n", app->name);
  92. #endif
  93. // check handler
  94. if(app == NULL || app->handler == NULL) return false;
  95. // kill task
  96. vTaskDelete(app->handler);
  97. // cleanup its registry
  98. // TODO realy free memory
  99. app->handler = NULL;
  100. return true;
  101. }
  102. void furiac_exit(void* param) {
  103. // get current task handler
  104. FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
  105. // run prev
  106. if(current_task != NULL) {
  107. #ifdef FURI_DEBUG
  108. printf("[FURIAC] exit %s\n", current_task->name);
  109. #endif
  110. if(current_task->prev != NULL) {
  111. furiac_start(current_task->prev, current_task->prev_name, param);
  112. } else {
  113. #ifdef FURI_DEBUG
  114. printf("[FURIAC] no prev\n");
  115. #endif
  116. }
  117. // cleanup registry
  118. // TODO realy free memory
  119. current_task->handler = NULL;
  120. }
  121. // kill itself
  122. vTaskDelete(NULL);
  123. }
  124. void furiac_switch(FlipperApplication app, char* name, void* param) {
  125. // get current task handler
  126. FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
  127. if(current_task == NULL) {
  128. #ifdef FURI_DEBUG
  129. printf("[FURIAC] no current task found\n");
  130. #endif
  131. }
  132. #ifdef FURI_DEBUG
  133. printf("[FURIAC] switch %s to %s\n", current_task->name, name);
  134. #endif
  135. // run next
  136. FuriApp* next = furiac_start(app, name, param);
  137. if(next != NULL) {
  138. // save current application pointer as prev
  139. next->prev = current_task->application;
  140. next->prev_name = current_task->name;
  141. // kill itself
  142. vTaskDelete(NULL);
  143. }
  144. }
  145. // set task to ready state
  146. void furiac_ready() {
  147. /*
  148. TODO:
  149. Currently i think that better way is to use application name
  150. and restrict applications to "one task per application"
  151. */
  152. FuriApp* app = find_task(xTaskGetCurrentTaskHandle());
  153. if(app == NULL) {
  154. #ifdef FURI_DEBUG
  155. printf("[FURIAC] cannot find task to set ready state\n");
  156. #endif
  157. } else {
  158. #ifdef FURI_DEBUG
  159. printf("[FURIAC] task is ready\n");
  160. #endif
  161. app->ready = true;
  162. }
  163. }