fap_loader_app.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "fap_loader_app.h"
  2. #include <furi.h>
  3. #include <assets_icons.h>
  4. #include <gui/gui.h>
  5. #include <gui/view_dispatcher.h>
  6. #include <gui/modules/loading.h>
  7. #include <dialogs/dialogs.h>
  8. #include <toolbox/path.h>
  9. #include <flipper_application/flipper_application.h>
  10. #include <loader/firmware_api/firmware_api.h>
  11. #define TAG "FapLoader"
  12. struct FapLoader {
  13. FlipperApplication* app;
  14. Storage* storage;
  15. DialogsApp* dialogs;
  16. Gui* gui;
  17. FuriString* fap_path;
  18. ViewDispatcher* view_dispatcher;
  19. Loading* loading;
  20. };
  21. volatile bool fap_loader_debug_active = false;
  22. bool fap_loader_load_name_and_icon(
  23. FuriString* path,
  24. Storage* storage,
  25. uint8_t** icon_ptr,
  26. FuriString* item_name) {
  27. FlipperApplication* app = flipper_application_alloc(storage, firmware_api_interface);
  28. FlipperApplicationPreloadStatus preload_res =
  29. flipper_application_preload_manifest(app, furi_string_get_cstr(path));
  30. bool load_success = false;
  31. if(preload_res == FlipperApplicationPreloadStatusSuccess) {
  32. const FlipperApplicationManifest* manifest = flipper_application_get_manifest(app);
  33. if(manifest->has_icon) {
  34. memcpy(*icon_ptr, manifest->icon, FAP_MANIFEST_MAX_ICON_SIZE);
  35. }
  36. furi_string_set(item_name, manifest->name);
  37. load_success = true;
  38. } else {
  39. FURI_LOG_E(TAG, "FAP Loader failed to preload %s", furi_string_get_cstr(path));
  40. load_success = false;
  41. }
  42. flipper_application_free(app);
  43. return load_success;
  44. }
  45. static bool fap_loader_item_callback(
  46. FuriString* path,
  47. void* context,
  48. uint8_t** icon_ptr,
  49. FuriString* item_name) {
  50. FapLoader* fap_loader = context;
  51. furi_assert(fap_loader);
  52. return fap_loader_load_name_and_icon(path, fap_loader->storage, icon_ptr, item_name);
  53. }
  54. static bool fap_loader_run_selected_app(FapLoader* loader) {
  55. furi_assert(loader);
  56. FuriString* error_message;
  57. error_message = furi_string_alloc_set("unknown error");
  58. bool file_selected = false;
  59. bool show_error = true;
  60. do {
  61. file_selected = true;
  62. loader->app = flipper_application_alloc(loader->storage, firmware_api_interface);
  63. size_t start = furi_get_tick();
  64. FURI_LOG_I(TAG, "FAP Loader is loading %s", furi_string_get_cstr(loader->fap_path));
  65. FlipperApplicationPreloadStatus preload_res =
  66. flipper_application_preload(loader->app, furi_string_get_cstr(loader->fap_path));
  67. if(preload_res != FlipperApplicationPreloadStatusSuccess) {
  68. const char* err_msg = flipper_application_preload_status_to_string(preload_res);
  69. furi_string_printf(error_message, "Preload failed: %s", err_msg);
  70. FURI_LOG_E(
  71. TAG,
  72. "FAP Loader failed to preload %s: %s",
  73. furi_string_get_cstr(loader->fap_path),
  74. err_msg);
  75. break;
  76. }
  77. FURI_LOG_I(TAG, "FAP Loader is mapping");
  78. FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(loader->app);
  79. if(load_status != FlipperApplicationLoadStatusSuccess) {
  80. const char* err_msg = flipper_application_load_status_to_string(load_status);
  81. furi_string_printf(error_message, "Load failed: %s", err_msg);
  82. FURI_LOG_E(
  83. TAG,
  84. "FAP Loader failed to map to memory %s: %s",
  85. furi_string_get_cstr(loader->fap_path),
  86. err_msg);
  87. break;
  88. }
  89. FURI_LOG_I(TAG, "Loaded in %ums", (size_t)(furi_get_tick() - start));
  90. FURI_LOG_I(TAG, "FAP Loader is starting app");
  91. FuriThread* thread = flipper_application_spawn(loader->app, NULL);
  92. /* This flag is set by the debugger - to break on app start */
  93. if(fap_loader_debug_active) {
  94. FURI_LOG_W(TAG, "Triggering BP for debugger");
  95. /* After hitting this, you can set breakpoints in your .fap's code
  96. * Note that you have to toggle breakpoints that were set before */
  97. __asm volatile("bkpt 0");
  98. }
  99. FuriString* app_name = furi_string_alloc();
  100. path_extract_filename_no_ext(furi_string_get_cstr(loader->fap_path), app_name);
  101. furi_thread_set_appid(thread, furi_string_get_cstr(app_name));
  102. furi_string_free(app_name);
  103. furi_thread_start(thread);
  104. furi_thread_join(thread);
  105. show_error = false;
  106. int ret = furi_thread_get_return_code(thread);
  107. FURI_LOG_I(TAG, "FAP app returned: %i", ret);
  108. } while(0);
  109. if(show_error) {
  110. DialogMessage* message = dialog_message_alloc();
  111. dialog_message_set_header(message, "Error", 64, 0, AlignCenter, AlignTop);
  112. dialog_message_set_buttons(message, NULL, NULL, NULL);
  113. FuriString* buffer;
  114. buffer = furi_string_alloc();
  115. furi_string_printf(buffer, "%s", furi_string_get_cstr(error_message));
  116. furi_string_replace(buffer, ":", "\n");
  117. dialog_message_set_text(
  118. message, furi_string_get_cstr(buffer), 64, 32, AlignCenter, AlignCenter);
  119. dialog_message_show(loader->dialogs, message);
  120. dialog_message_free(message);
  121. furi_string_free(buffer);
  122. }
  123. furi_string_free(error_message);
  124. if(file_selected) {
  125. flipper_application_free(loader->app);
  126. }
  127. return file_selected;
  128. }
  129. static bool fap_loader_select_app(FapLoader* loader) {
  130. const DialogsFileBrowserOptions browser_options = {
  131. .extension = ".fap",
  132. .skip_assets = true,
  133. .icon = &I_unknown_10px,
  134. .hide_ext = true,
  135. .item_loader_callback = fap_loader_item_callback,
  136. .item_loader_context = loader,
  137. .base_path = EXT_PATH("apps"),
  138. };
  139. return dialog_file_browser_show(
  140. loader->dialogs, loader->fap_path, loader->fap_path, &browser_options);
  141. }
  142. static FapLoader* fap_loader_alloc(const char* path) {
  143. FapLoader* loader = malloc(sizeof(FapLoader)); //-V799
  144. loader->fap_path = furi_string_alloc_set(path);
  145. loader->storage = furi_record_open(RECORD_STORAGE);
  146. loader->dialogs = furi_record_open(RECORD_DIALOGS);
  147. loader->gui = furi_record_open(RECORD_GUI);
  148. loader->view_dispatcher = view_dispatcher_alloc();
  149. loader->loading = loading_alloc();
  150. view_dispatcher_attach_to_gui(
  151. loader->view_dispatcher, loader->gui, ViewDispatcherTypeFullscreen);
  152. view_dispatcher_add_view(loader->view_dispatcher, 0, loading_get_view(loader->loading));
  153. return loader;
  154. } //-V773
  155. static void fap_loader_free(FapLoader* loader) {
  156. view_dispatcher_remove_view(loader->view_dispatcher, 0);
  157. loading_free(loader->loading);
  158. view_dispatcher_free(loader->view_dispatcher);
  159. furi_string_free(loader->fap_path);
  160. furi_record_close(RECORD_GUI);
  161. furi_record_close(RECORD_DIALOGS);
  162. furi_record_close(RECORD_STORAGE);
  163. free(loader);
  164. }
  165. int32_t fap_loader_app(void* p) {
  166. FapLoader* loader;
  167. if(p) {
  168. loader = fap_loader_alloc((const char*)p);
  169. view_dispatcher_switch_to_view(loader->view_dispatcher, 0);
  170. fap_loader_run_selected_app(loader);
  171. } else {
  172. loader = fap_loader_alloc(EXT_PATH("apps"));
  173. while(fap_loader_select_app(loader)) {
  174. view_dispatcher_switch_to_view(loader->view_dispatcher, 0);
  175. fap_loader_run_selected_app(loader);
  176. };
  177. }
  178. fap_loader_free(loader);
  179. return 0;
  180. }