updater_scene_main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <applications.h>
  4. #include <storage/storage.h>
  5. #include "updater/updater_i.h"
  6. #include "updater/views/updater_main.h"
  7. #include "updater_scene.h"
  8. static void sd_mount_callback(const void* message, void* context) {
  9. Updater* updater = context;
  10. const StorageEvent* new_event = message;
  11. switch(new_event->type) {
  12. case StorageEventTypeCardMount:
  13. view_dispatcher_send_custom_event(updater->view_dispatcher, UpdaterCustomEventStartUpdate);
  14. break;
  15. case StorageEventTypeCardUnmount:
  16. view_dispatcher_send_custom_event(updater->view_dispatcher, UpdaterCustomEventSdUnmounted);
  17. break;
  18. default:
  19. break;
  20. }
  21. }
  22. void updater_scene_main_on_enter(void* context) {
  23. Updater* updater = (Updater*)context;
  24. notification_message(updater->notification, &sequence_display_backlight_enforce_on);
  25. UpdaterMainView* main_view = updater->main_view;
  26. FuriPubSubSubscription* sub =
  27. furi_pubsub_subscribe(storage_get_pubsub(updater->storage), &sd_mount_callback, updater);
  28. updater_main_set_storage_pubsub(main_view, sub);
  29. /* FIXME: there's a misbehavior in storage subsystem. If we produce heavy load on it before it
  30. * fires an SD card event, it'll never do that until the load is lifted. Meanwhile SD card icon
  31. * will be missing from UI, however, /ext will be fully operational. So, until it's fixed, this
  32. * should remain commented out. */
  33. // If (somehow) we started after SD card is mounted, initiate update immediately
  34. if(storage_sd_status(updater->storage) == FSE_OK) {
  35. view_dispatcher_send_custom_event(updater->view_dispatcher, UpdaterCustomEventStartUpdate);
  36. }
  37. updater_main_set_view_dispatcher(main_view, updater->view_dispatcher);
  38. view_dispatcher_switch_to_view(updater->view_dispatcher, UpdaterViewMain);
  39. }
  40. static void updater_scene_cancel_update() {
  41. update_operation_disarm();
  42. furi_hal_power_reset();
  43. }
  44. bool updater_scene_main_on_event(void* context, SceneManagerEvent event) {
  45. Updater* updater = (Updater*)context;
  46. bool consumed = false;
  47. if(event.type == SceneManagerEventTypeTick) {
  48. if(!update_task_is_running(updater->update_task)) {
  49. if(updater->idle_ticks++ >= (UPDATE_DELAY_OPERATION_ERROR / UPDATER_APP_TICK)) {
  50. updater_scene_cancel_update();
  51. }
  52. } else {
  53. updater->idle_ticks = 0;
  54. }
  55. } else if(event.type == SceneManagerEventTypeCustom) {
  56. switch(event.event) {
  57. case UpdaterCustomEventStartUpdate:
  58. case UpdaterCustomEventRetryUpdate:
  59. if(!update_task_is_running(updater->update_task) &&
  60. (update_task_get_state(updater->update_task)->stage != UpdateTaskStageCompleted))
  61. update_task_start(updater->update_task);
  62. consumed = true;
  63. break;
  64. case UpdaterCustomEventCancelUpdate:
  65. if(!update_task_is_running(updater->update_task)) {
  66. updater_scene_cancel_update();
  67. }
  68. consumed = true;
  69. break;
  70. case UpdaterCustomEventSdUnmounted:
  71. // TODO: error out, stop worker (it's probably dead actually)
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. return consumed;
  78. }
  79. void updater_scene_main_on_exit(void* context) {
  80. Updater* updater = (Updater*)context;
  81. notification_message(updater->notification, &sequence_display_backlight_enforce_auto);
  82. furi_pubsub_unsubscribe(
  83. storage_get_pubsub(updater->storage), updater_main_get_storage_pubsub(updater->main_view));
  84. scene_manager_set_scene_state(updater->scene_manager, UpdaterSceneMain, 0);
  85. }