app-loader.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "app-loader.h"
  2. #define APP_LOADER_TAG "app_loader"
  3. typedef struct {
  4. FuriThread* thread;
  5. const FlipperApplication* current_app;
  6. string_t args;
  7. Cli* cli;
  8. } AppLoaderState;
  9. static AppLoaderState state;
  10. // TODO add mutex for contex
  11. static void app_loader_menu_callback(void* _ctx) {
  12. furi_assert(_ctx);
  13. const FlipperApplication* flipper_app = (FlipperApplication*)_ctx;
  14. furi_assert(flipper_app->app);
  15. furi_assert(flipper_app->name);
  16. if(furi_thread_get_state(state.thread) != FuriThreadStateStopped) {
  17. FURI_LOG_E(APP_LOADER_TAG, "Can't start app. %s is running", state.current_app->name);
  18. return;
  19. }
  20. api_hal_power_insomnia_enter();
  21. state.current_app = flipper_app;
  22. FURI_LOG_I(APP_LOADER_TAG, "Starting furi application: %s", state.current_app->name);
  23. furi_thread_set_name(state.thread, flipper_app->name);
  24. furi_thread_set_stack_size(state.thread, flipper_app->stack_size);
  25. furi_thread_set_context(state.thread, NULL);
  26. furi_thread_set_callback(state.thread, flipper_app->app);
  27. furi_thread_start(state.thread);
  28. }
  29. static void app_loader_cli_callback(Cli* cli, string_t args, void* _ctx) {
  30. furi_assert(_ctx);
  31. const FlipperApplication* flipper_app = (FlipperApplication*)_ctx;
  32. furi_assert(flipper_app->app);
  33. furi_assert(flipper_app->name);
  34. if(furi_thread_get_state(state.thread) != FuriThreadStateStopped) {
  35. printf("Can't start, furi application is running");
  36. return;
  37. }
  38. api_hal_power_insomnia_enter();
  39. state.current_app = flipper_app;
  40. printf("Starting furi application %s", state.current_app->name);
  41. furi_thread_set_name(state.thread, flipper_app->name);
  42. furi_thread_set_stack_size(state.thread, flipper_app->stack_size);
  43. furi_thread_set_callback(state.thread, flipper_app->app);
  44. furi_thread_start(state.thread);
  45. }
  46. bool app_loader_start(const char* name, const char* args) {
  47. furi_assert(name);
  48. const FlipperApplication* flipper_app = NULL;
  49. // Search for application
  50. for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
  51. if(!strcmp(FLIPPER_APPS[i].name, name)) {
  52. flipper_app = &FLIPPER_APPS[i];
  53. break;
  54. }
  55. }
  56. if(!flipper_app) {
  57. FURI_LOG_E(APP_LOADER_TAG, "Can't find application with name %s", name);
  58. return false;
  59. }
  60. if(furi_thread_get_state(state.thread) != FuriThreadStateStopped) {
  61. FURI_LOG_E(APP_LOADER_TAG, "Can't start app. %s is running", state.current_app->name);
  62. return false;
  63. }
  64. state.current_app = flipper_app;
  65. if(args) {
  66. string_set_str(state.args, args);
  67. string_strim(state.args);
  68. FURI_LOG_I(APP_LOADER_TAG, "Start %s app with args: %s", name, args);
  69. } else {
  70. string_clean(state.args);
  71. FURI_LOG_I(APP_LOADER_TAG, "Start %s app with no args", name);
  72. }
  73. furi_thread_set_name(state.thread, flipper_app->name);
  74. furi_thread_set_stack_size(state.thread, flipper_app->stack_size);
  75. furi_thread_set_context(state.thread, (void*)string_get_cstr(state.args));
  76. furi_thread_set_callback(state.thread, flipper_app->app);
  77. return furi_thread_start(state.thread);
  78. }
  79. void app_loader_thread_state_callback(FuriThreadState thread_state, void* context) {
  80. furi_assert(context);
  81. if(thread_state == FuriThreadStateStopped) {
  82. FURI_LOG_I(
  83. APP_LOADER_TAG,
  84. "Application thread stopped, heap leaked: %d",
  85. furi_thread_get_heap_size(state.thread));
  86. api_hal_power_insomnia_exit();
  87. }
  88. }
  89. int32_t app_loader(void* p) {
  90. FURI_LOG_I(APP_LOADER_TAG, "Starting");
  91. state.thread = furi_thread_alloc();
  92. furi_thread_enable_heap_trace(state.thread);
  93. furi_thread_set_state_context(state.thread, &state);
  94. furi_thread_set_state_callback(state.thread, app_loader_thread_state_callback);
  95. string_init(state.args);
  96. ValueMutex* menu_mutex = furi_record_open("menu");
  97. state.cli = furi_record_open("cli");
  98. // Main menu
  99. FURI_LOG_I(APP_LOADER_TAG, "Building main menu");
  100. with_value_mutex(
  101. menu_mutex, (Menu * menu) {
  102. for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
  103. // Add menu item
  104. menu_item_add(
  105. menu,
  106. menu_item_alloc_function(
  107. FLIPPER_APPS[i].name,
  108. assets_icons_get(FLIPPER_APPS[i].icon),
  109. app_loader_menu_callback,
  110. (void*)&FLIPPER_APPS[i]));
  111. // Add cli command
  112. string_t cli_name;
  113. string_init_set_str(cli_name, "app_");
  114. string_cat_str(cli_name, FLIPPER_APPS[i].name);
  115. cli_add_command(
  116. state.cli,
  117. string_get_cstr(cli_name),
  118. app_loader_cli_callback,
  119. (void*)&FLIPPER_APPS[i]);
  120. string_clear(cli_name);
  121. }
  122. });
  123. // Plugins
  124. FURI_LOG_I(APP_LOADER_TAG, "Building plugins menu");
  125. with_value_mutex(
  126. menu_mutex, (Menu * menu) {
  127. MenuItem* menu_plugins =
  128. menu_item_alloc_menu("Plugins", assets_icons_get(A_Plugins_14));
  129. for(size_t i = 0; i < FLIPPER_PLUGINS_COUNT; i++) {
  130. // Add menu item
  131. menu_item_subitem_add(
  132. menu_plugins,
  133. menu_item_alloc_function(
  134. FLIPPER_PLUGINS[i].name,
  135. assets_icons_get(FLIPPER_PLUGINS[i].icon),
  136. app_loader_menu_callback,
  137. (void*)&FLIPPER_PLUGINS[i]));
  138. // Add cli command
  139. string_t cli_name;
  140. string_init_set_str(cli_name, "app_");
  141. string_cat_str(cli_name, FLIPPER_PLUGINS[i].name);
  142. cli_add_command(
  143. state.cli,
  144. string_get_cstr(cli_name),
  145. app_loader_cli_callback,
  146. (void*)&FLIPPER_PLUGINS[i]);
  147. string_clear(cli_name);
  148. }
  149. menu_item_add(menu, menu_plugins);
  150. });
  151. // Debug
  152. FURI_LOG_I(APP_LOADER_TAG, "Building debug menu");
  153. with_value_mutex(
  154. menu_mutex, (Menu * menu) {
  155. MenuItem* menu_debug =
  156. menu_item_alloc_menu("Debug tools", assets_icons_get(A_Settings_14));
  157. for(size_t i = 0; i < FLIPPER_DEBUG_APPS_COUNT; i++) {
  158. // Add menu item
  159. menu_item_subitem_add(
  160. menu_debug,
  161. menu_item_alloc_function(
  162. FLIPPER_DEBUG_APPS[i].name,
  163. assets_icons_get(FLIPPER_DEBUG_APPS[i].icon),
  164. app_loader_menu_callback,
  165. (void*)&FLIPPER_DEBUG_APPS[i]));
  166. // Add cli command
  167. string_t cli_name;
  168. string_init_set_str(cli_name, "app_");
  169. string_cat_str(cli_name, FLIPPER_DEBUG_APPS[i].name);
  170. cli_add_command(
  171. state.cli,
  172. string_get_cstr(cli_name),
  173. app_loader_cli_callback,
  174. (void*)&FLIPPER_DEBUG_APPS[i]);
  175. string_clear(cli_name);
  176. }
  177. menu_item_add(menu, menu_debug);
  178. });
  179. // Call on start hooks
  180. for(size_t i = 0; i < FLIPPER_ON_SYSTEM_START_COUNT; i++) {
  181. (*FLIPPER_ON_SYSTEM_START[i])();
  182. }
  183. FURI_LOG_I(APP_LOADER_TAG, "Started");
  184. while(1) {
  185. osThreadSuspend(osThreadGetId());
  186. }
  187. return 0;
  188. }