updater.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "scenes/updater_scene.h"
  2. #include "updater_i.h"
  3. #include <storage/storage.h>
  4. #include <gui/view_dispatcher.h>
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include <portmacro.h>
  8. #include <stdint.h>
  9. static bool updater_custom_event_callback(void* context, uint32_t event) {
  10. furi_assert(context);
  11. Updater* updater = (Updater*)context;
  12. return scene_manager_handle_custom_event(updater->scene_manager, event);
  13. }
  14. static void updater_tick_event_callback(void* context) {
  15. furi_assert(context);
  16. Updater* app = context;
  17. scene_manager_handle_tick_event(app->scene_manager);
  18. }
  19. static bool updater_back_event_callback(void* context) {
  20. furi_assert(context);
  21. Updater* updater = (Updater*)context;
  22. return scene_manager_handle_back_event(updater->scene_manager);
  23. }
  24. static void status_update_cb(
  25. const char* message,
  26. const uint8_t progress,
  27. const uint8_t idx_stage,
  28. const uint8_t total_stages,
  29. bool failed,
  30. void* context) {
  31. UpdaterMainView* main_view = context;
  32. updater_main_model_set_state(main_view, message, progress, idx_stage, total_stages, failed);
  33. }
  34. Updater* updater_alloc(const char* arg) {
  35. Updater* updater = malloc(sizeof(Updater));
  36. if(arg) {
  37. string_init_set_str(updater->startup_arg, arg);
  38. string_replace_str(updater->startup_arg, "/any/", "/ext/");
  39. } else {
  40. string_init(updater->startup_arg);
  41. }
  42. updater->storage = furi_record_open("storage");
  43. updater->notification = furi_record_open("notification");
  44. updater->gui = furi_record_open("gui");
  45. updater->view_dispatcher = view_dispatcher_alloc();
  46. updater->scene_manager = scene_manager_alloc(&updater_scene_handlers, updater);
  47. view_dispatcher_enable_queue(updater->view_dispatcher);
  48. view_dispatcher_set_event_callback_context(updater->view_dispatcher, updater);
  49. view_dispatcher_set_custom_event_callback(
  50. updater->view_dispatcher, updater_custom_event_callback);
  51. view_dispatcher_set_navigation_event_callback(
  52. updater->view_dispatcher, updater_back_event_callback);
  53. view_dispatcher_set_tick_event_callback(
  54. updater->view_dispatcher, updater_tick_event_callback, UPDATER_APP_TICK);
  55. view_dispatcher_attach_to_gui(
  56. updater->view_dispatcher,
  57. updater->gui,
  58. arg ? ViewDispatcherTypeFullscreen : ViewDispatcherTypeWindow);
  59. updater->main_view = updater_main_alloc();
  60. view_dispatcher_add_view(
  61. updater->view_dispatcher, UpdaterViewMain, updater_main_get_view(updater->main_view));
  62. #ifndef FURI_RAM_EXEC
  63. updater->widget = widget_alloc();
  64. view_dispatcher_add_view(
  65. updater->view_dispatcher, UpdaterViewWidget, widget_get_view(updater->widget));
  66. #endif
  67. #ifdef FURI_RAM_EXEC
  68. if(true) {
  69. #else
  70. FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
  71. if(!arg && ((boot_mode == FuriHalRtcBootModePreUpdate) ||
  72. (boot_mode == FuriHalRtcBootModePostUpdate))) {
  73. #endif
  74. updater->update_task = update_task_alloc();
  75. update_task_set_progress_cb(updater->update_task, status_update_cb, updater->main_view);
  76. scene_manager_next_scene(updater->scene_manager, UpdaterSceneMain);
  77. } else {
  78. #ifndef FURI_RAM_EXEC
  79. scene_manager_next_scene(updater->scene_manager, UpdaterSceneLoadCfg);
  80. #endif
  81. }
  82. return updater;
  83. }
  84. void updater_free(Updater* updater) {
  85. furi_assert(updater);
  86. string_clear(updater->startup_arg);
  87. if(updater->update_task) {
  88. update_task_set_progress_cb(updater->update_task, NULL, NULL);
  89. update_task_free(updater->update_task);
  90. }
  91. view_dispatcher_remove_view(updater->view_dispatcher, UpdaterViewMain);
  92. updater_main_free(updater->main_view);
  93. #ifndef FURI_RAM_EXEC
  94. view_dispatcher_remove_view(updater->view_dispatcher, UpdaterViewWidget);
  95. widget_free(updater->widget);
  96. #endif
  97. view_dispatcher_free(updater->view_dispatcher);
  98. scene_manager_free(updater->scene_manager);
  99. furi_record_close("gui");
  100. furi_record_close("storage");
  101. furi_record_close("notification");
  102. free(updater);
  103. }
  104. int32_t updater_srv(void* p) {
  105. const char* cfgpath = p;
  106. Updater* updater = updater_alloc(cfgpath);
  107. view_dispatcher_run(updater->view_dispatcher);
  108. updater_free(updater);
  109. return 0;
  110. }