app-loader.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "app-loader.h"
  2. #include "api-hal-delay.h"
  3. #define APP_LOADER_TAG "app_loader"
  4. typedef struct {
  5. FuriThread* thread;
  6. const FlipperApplication* current_app;
  7. string_t args;
  8. Cli* cli;
  9. size_t free_heap_size;
  10. } AppLoaderState;
  11. static AppLoaderState state;
  12. // TODO add mutex for contex
  13. static void app_loader_menu_callback(void* _ctx) {
  14. furi_assert(_ctx);
  15. const FlipperApplication* flipper_app = (FlipperApplication*)_ctx;
  16. furi_assert(flipper_app->app);
  17. furi_assert(flipper_app->name);
  18. if(furi_thread_get_state(state.thread) != FuriThreadStateStopped) {
  19. FURI_LOG_E(APP_LOADER_TAG, "Can't start app. %s is running", state.current_app->name);
  20. return;
  21. }
  22. api_hal_power_insomnia_enter();
  23. state.current_app = flipper_app;
  24. FURI_LOG_I(APP_LOADER_TAG, "Starting furi application: %s", state.current_app->name);
  25. furi_thread_set_name(state.thread, flipper_app->name);
  26. furi_thread_set_stack_size(state.thread, flipper_app->stack_size);
  27. furi_thread_set_context(state.thread, NULL);
  28. furi_thread_set_callback(state.thread, flipper_app->app);
  29. furi_thread_start(state.thread);
  30. }
  31. static void app_loader_cli_callback(Cli* cli, string_t args, void* _ctx) {
  32. furi_assert(_ctx);
  33. const FlipperApplication* flipper_app = (FlipperApplication*)_ctx;
  34. furi_assert(flipper_app->app);
  35. furi_assert(flipper_app->name);
  36. if(furi_thread_get_state(state.thread) != FuriThreadStateStopped) {
  37. printf("Can't start, furi application is running");
  38. return;
  39. }
  40. api_hal_power_insomnia_enter();
  41. state.current_app = flipper_app;
  42. printf("Starting furi application %s", state.current_app->name);
  43. furi_thread_set_name(state.thread, flipper_app->name);
  44. furi_thread_set_stack_size(state.thread, flipper_app->stack_size);
  45. furi_thread_set_callback(state.thread, flipper_app->app);
  46. furi_thread_start(state.thread);
  47. }
  48. bool app_loader_start(const char* name, const char* args) {
  49. furi_assert(name);
  50. const FlipperApplication* flipper_app = NULL;
  51. // Search for application
  52. for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
  53. if(!strcmp(FLIPPER_APPS[i].name, name)) {
  54. flipper_app = &FLIPPER_APPS[i];
  55. break;
  56. }
  57. }
  58. if(!flipper_app) {
  59. FURI_LOG_E(APP_LOADER_TAG, "Can't find application with name %s", name);
  60. return false;
  61. }
  62. if(furi_thread_get_state(state.thread) != FuriThreadStateStopped) {
  63. FURI_LOG_E(APP_LOADER_TAG, "Can't start app. %s is running", state.current_app->name);
  64. return false;
  65. }
  66. state.current_app = flipper_app;
  67. if(args) {
  68. string_set_str(state.args, args);
  69. string_strim(state.args);
  70. FURI_LOG_I(APP_LOADER_TAG, "Start %s app with args: %s", name, args);
  71. } else {
  72. string_clean(state.args);
  73. FURI_LOG_I(APP_LOADER_TAG, "Start %s app with no args", name);
  74. }
  75. furi_thread_set_name(state.thread, flipper_app->name);
  76. furi_thread_set_stack_size(state.thread, flipper_app->stack_size);
  77. furi_thread_set_context(state.thread, (void*)string_get_cstr(state.args));
  78. furi_thread_set_callback(state.thread, flipper_app->app);
  79. return furi_thread_start(state.thread);
  80. }
  81. void app_loader_thread_state_callback(FuriThreadState thread_state, void* context) {
  82. furi_assert(context);
  83. AppLoaderState* state = context;
  84. if(thread_state == FuriThreadStateRunning) {
  85. state->free_heap_size = xPortGetFreeHeapSize();
  86. } else if(thread_state == FuriThreadStateStopped) {
  87. /*
  88. * Current Leak Sanitizer assumes that memory is allocated and freed
  89. * inside one thread. Timers are allocated in one task, but freed in
  90. * Timer-Task thread, and xTimerDelete() just put command to queue.
  91. * To avoid some bad cases there are few fixes:
  92. * 1) delay for Timer to process commands
  93. * 2) there are 'heap diff' which shows difference in heap before task
  94. * started and after task completed. In process of leakage monitoring
  95. * both values should be taken into account.
  96. */
  97. delay(20);
  98. int heap_diff = state->free_heap_size - xPortGetFreeHeapSize();
  99. FURI_LOG_I(
  100. APP_LOADER_TAG,
  101. "Application thread stopped. Heap allocation balance: %d. Thread allocation balance: %d.",
  102. heap_diff,
  103. furi_thread_get_heap_size(state->thread));
  104. api_hal_power_insomnia_exit();
  105. }
  106. }
  107. int32_t app_loader(void* p) {
  108. FURI_LOG_I(APP_LOADER_TAG, "Starting");
  109. state.thread = furi_thread_alloc();
  110. furi_thread_enable_heap_trace(state.thread);
  111. furi_thread_set_state_context(state.thread, &state);
  112. furi_thread_set_state_callback(state.thread, app_loader_thread_state_callback);
  113. string_init(state.args);
  114. ValueMutex* menu_mutex = furi_record_open("menu");
  115. state.cli = furi_record_open("cli");
  116. // Main menu
  117. FURI_LOG_I(APP_LOADER_TAG, "Building main menu");
  118. with_value_mutex(
  119. menu_mutex, (Menu * menu) {
  120. for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
  121. // Add menu item
  122. menu_item_add(
  123. menu,
  124. menu_item_alloc_function(
  125. FLIPPER_APPS[i].name,
  126. icon_animation_alloc(FLIPPER_APPS[i].icon),
  127. app_loader_menu_callback,
  128. (void*)&FLIPPER_APPS[i]));
  129. // Add cli command
  130. string_t cli_name;
  131. string_init_set_str(cli_name, "app_");
  132. string_cat_str(cli_name, FLIPPER_APPS[i].name);
  133. cli_add_command(
  134. state.cli,
  135. string_get_cstr(cli_name),
  136. app_loader_cli_callback,
  137. (void*)&FLIPPER_APPS[i]);
  138. string_clear(cli_name);
  139. }
  140. });
  141. // Plugins
  142. FURI_LOG_I(APP_LOADER_TAG, "Building plugins menu");
  143. with_value_mutex(
  144. menu_mutex, (Menu * menu) {
  145. MenuItem* menu_plugins =
  146. menu_item_alloc_menu("Plugins", icon_animation_alloc(&A_Plugins_14));
  147. for(size_t i = 0; i < FLIPPER_PLUGINS_COUNT; i++) {
  148. // Add menu item
  149. menu_item_subitem_add(
  150. menu_plugins,
  151. menu_item_alloc_function(
  152. FLIPPER_PLUGINS[i].name,
  153. icon_animation_alloc(FLIPPER_PLUGINS[i].icon),
  154. app_loader_menu_callback,
  155. (void*)&FLIPPER_PLUGINS[i]));
  156. // Add cli command
  157. string_t cli_name;
  158. string_init_set_str(cli_name, "app_");
  159. string_cat_str(cli_name, FLIPPER_PLUGINS[i].name);
  160. cli_add_command(
  161. state.cli,
  162. string_get_cstr(cli_name),
  163. app_loader_cli_callback,
  164. (void*)&FLIPPER_PLUGINS[i]);
  165. string_clear(cli_name);
  166. }
  167. menu_item_add(menu, menu_plugins);
  168. });
  169. // Debug
  170. FURI_LOG_I(APP_LOADER_TAG, "Building debug menu");
  171. with_value_mutex(
  172. menu_mutex, (Menu * menu) {
  173. MenuItem* menu_debug =
  174. menu_item_alloc_menu("Debug tools", icon_animation_alloc(&A_Settings_14));
  175. for(size_t i = 0; i < FLIPPER_DEBUG_APPS_COUNT; i++) {
  176. // Add menu item
  177. menu_item_subitem_add(
  178. menu_debug,
  179. menu_item_alloc_function(
  180. FLIPPER_DEBUG_APPS[i].name,
  181. icon_animation_alloc(FLIPPER_DEBUG_APPS[i].icon),
  182. app_loader_menu_callback,
  183. (void*)&FLIPPER_DEBUG_APPS[i]));
  184. // Add cli command
  185. string_t cli_name;
  186. string_init_set_str(cli_name, "app_");
  187. string_cat_str(cli_name, FLIPPER_DEBUG_APPS[i].name);
  188. cli_add_command(
  189. state.cli,
  190. string_get_cstr(cli_name),
  191. app_loader_cli_callback,
  192. (void*)&FLIPPER_DEBUG_APPS[i]);
  193. string_clear(cli_name);
  194. }
  195. menu_item_add(menu, menu_debug);
  196. });
  197. // Settings
  198. FURI_LOG_I(APP_LOADER_TAG, "Building settings menu");
  199. with_value_mutex(
  200. menu_mutex, (Menu * menu) {
  201. MenuItem* menu_debug =
  202. menu_item_alloc_menu("Settings", icon_animation_alloc(&A_Settings_14));
  203. for(size_t i = 0; i < FLIPPER_SETTINGS_APPS_COUNT; i++) {
  204. // Add menu item
  205. menu_item_subitem_add(
  206. menu_debug,
  207. menu_item_alloc_function(
  208. FLIPPER_SETTINGS_APPS[i].name,
  209. icon_animation_alloc(FLIPPER_SETTINGS_APPS[i].icon),
  210. app_loader_menu_callback,
  211. (void*)&FLIPPER_SETTINGS_APPS[i]));
  212. }
  213. menu_item_add(menu, menu_debug);
  214. });
  215. // Call on start hooks
  216. for(size_t i = 0; i < FLIPPER_ON_SYSTEM_START_COUNT; i++) {
  217. (*FLIPPER_ON_SYSTEM_START[i])();
  218. }
  219. FURI_LOG_I(APP_LOADER_TAG, "Started");
  220. while(1) {
  221. osThreadSuspend(osThreadGetId());
  222. }
  223. return 0;
  224. }