updater.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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
  25. status_update_cb(const char* message, const uint8_t progress, bool failed, void* context) {
  26. UpdaterMainView* main_view = context;
  27. updater_main_model_set_state(main_view, message, progress, failed);
  28. }
  29. Updater* updater_alloc(const char* arg) {
  30. Updater* updater = malloc(sizeof(Updater));
  31. if(arg) {
  32. string_init_set_str(updater->startup_arg, arg);
  33. string_replace_str(updater->startup_arg, "/any/", "/ext/");
  34. } else {
  35. string_init(updater->startup_arg);
  36. }
  37. updater->storage = furi_record_open("storage");
  38. updater->notification = furi_record_open("notification");
  39. updater->gui = furi_record_open("gui");
  40. updater->view_dispatcher = view_dispatcher_alloc();
  41. updater->scene_manager = scene_manager_alloc(&updater_scene_handlers, updater);
  42. view_dispatcher_enable_queue(updater->view_dispatcher);
  43. view_dispatcher_set_event_callback_context(updater->view_dispatcher, updater);
  44. view_dispatcher_set_custom_event_callback(
  45. updater->view_dispatcher, updater_custom_event_callback);
  46. view_dispatcher_set_navigation_event_callback(
  47. updater->view_dispatcher, updater_back_event_callback);
  48. view_dispatcher_set_tick_event_callback(
  49. updater->view_dispatcher, updater_tick_event_callback, UPDATER_APP_TICK);
  50. view_dispatcher_attach_to_gui(
  51. updater->view_dispatcher, updater->gui, ViewDispatcherTypeFullscreen);
  52. updater->main_view = updater_main_alloc();
  53. view_dispatcher_add_view(
  54. updater->view_dispatcher, UpdaterViewMain, updater_main_get_view(updater->main_view));
  55. #ifndef FURI_RAM_EXEC
  56. updater->widget = widget_alloc();
  57. view_dispatcher_add_view(
  58. updater->view_dispatcher, UpdaterViewWidget, widget_get_view(updater->widget));
  59. #endif
  60. #ifdef FURI_RAM_EXEC
  61. if(true) {
  62. #else
  63. FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
  64. if(!arg && ((boot_mode == FuriHalRtcBootModePreUpdate) ||
  65. (boot_mode == FuriHalRtcBootModePostUpdate))) {
  66. #endif
  67. updater->update_task = update_task_alloc();
  68. update_task_set_progress_cb(updater->update_task, status_update_cb, updater->main_view);
  69. scene_manager_next_scene(updater->scene_manager, UpdaterSceneMain);
  70. } else {
  71. #ifndef FURI_RAM_EXEC
  72. scene_manager_next_scene(updater->scene_manager, UpdaterSceneLoadCfg);
  73. #endif
  74. }
  75. return updater;
  76. }
  77. void updater_free(Updater* updater) {
  78. furi_assert(updater);
  79. string_clear(updater->startup_arg);
  80. if(updater->update_task) {
  81. update_task_set_progress_cb(updater->update_task, NULL, NULL);
  82. update_task_free(updater->update_task);
  83. }
  84. view_dispatcher_remove_view(updater->view_dispatcher, UpdaterViewMain);
  85. updater_main_free(updater->main_view);
  86. #ifndef FURI_RAM_EXEC
  87. view_dispatcher_remove_view(updater->view_dispatcher, UpdaterViewWidget);
  88. widget_free(updater->widget);
  89. #endif
  90. view_dispatcher_free(updater->view_dispatcher);
  91. scene_manager_free(updater->scene_manager);
  92. furi_record_close("gui");
  93. furi_record_close("storage");
  94. furi_record_close("notification");
  95. free(updater);
  96. }
  97. int32_t updater_srv(void* p) {
  98. const char* cfgpath = p;
  99. Updater* updater = updater_alloc(cfgpath);
  100. view_dispatcher_run(updater->view_dispatcher);
  101. updater_free(updater);
  102. return 0;
  103. }