desktop_scene_pin_input.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/scene_manager.h>
  4. #include <gui/view_stack.h>
  5. #include <stdint.h>
  6. #include <portmacro.h>
  7. #include <notification/notification.h>
  8. #include <notification/notification_messages.h>
  9. #include "../desktop.h"
  10. #include "../desktop_i.h"
  11. #include "../animations/animation_manager.h"
  12. #include "../views/desktop_events.h"
  13. #include "../views/desktop_view_pin_input.h"
  14. #include "../desktop_helpers.h"
  15. #include "desktop_scene.h"
  16. #include "desktop_scene_i.h"
  17. #define WRONG_PIN_HEADER_TIMEOUT 3000
  18. #define INPUT_PIN_VIEW_TIMEOUT 15000
  19. typedef struct {
  20. TimerHandle_t timer;
  21. } DesktopScenePinInputState;
  22. static void desktop_scene_locked_light_red(bool value) {
  23. NotificationApp* app = furi_record_open("notification");
  24. if(value) {
  25. notification_message(app, &sequence_set_only_red_255);
  26. } else {
  27. notification_message(app, &sequence_reset_red);
  28. }
  29. furi_record_close("notification");
  30. }
  31. static void
  32. desktop_scene_pin_input_set_timer(Desktop* desktop, bool enable, TickType_t new_period) {
  33. furi_assert(desktop);
  34. DesktopScenePinInputState* state = (DesktopScenePinInputState*)scene_manager_get_scene_state(
  35. desktop->scene_manager, DesktopScenePinInput);
  36. furi_assert(state);
  37. if(enable) {
  38. xTimerChangePeriod(state->timer, new_period, portMAX_DELAY);
  39. } else {
  40. xTimerStop(state->timer, portMAX_DELAY);
  41. }
  42. }
  43. static void desktop_scene_pin_input_back_callback(void* context) {
  44. Desktop* desktop = (Desktop*)context;
  45. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopPinInputEventBack);
  46. }
  47. static void desktop_scene_pin_input_done_callback(const PinCode* pin_code, void* context) {
  48. Desktop* desktop = (Desktop*)context;
  49. if(pins_are_equal(&desktop->settings.pin_code, pin_code)) {
  50. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopPinInputEventUnlocked);
  51. } else {
  52. view_dispatcher_send_custom_event(
  53. desktop->view_dispatcher, DesktopPinInputEventUnlockFailed);
  54. }
  55. }
  56. static void desktop_scene_pin_input_timer_callback(TimerHandle_t timer) {
  57. Desktop* desktop = pvTimerGetTimerID(timer);
  58. view_dispatcher_send_custom_event(
  59. desktop->view_dispatcher, DesktopPinInputEventResetWrongPinLabel);
  60. }
  61. void desktop_scene_pin_input_on_enter(void* context) {
  62. Desktop* desktop = (Desktop*)context;
  63. desktop_view_pin_input_set_context(desktop->pin_input_view, desktop);
  64. desktop_view_pin_input_set_back_callback(
  65. desktop->pin_input_view, desktop_scene_pin_input_back_callback);
  66. desktop_view_pin_input_set_timeout_callback(
  67. desktop->pin_input_view, desktop_scene_pin_input_back_callback);
  68. desktop_view_pin_input_set_done_callback(
  69. desktop->pin_input_view, desktop_scene_pin_input_done_callback);
  70. DesktopScenePinInputState* state = malloc(sizeof(DesktopScenePinInputState));
  71. state->timer =
  72. xTimerCreate(NULL, 10000, pdFALSE, desktop, desktop_scene_pin_input_timer_callback);
  73. scene_manager_set_scene_state(desktop->scene_manager, DesktopScenePinInput, (uint32_t)state);
  74. desktop_view_pin_input_hide_pin(desktop->pin_input_view, true);
  75. desktop_view_pin_input_set_label_button(desktop->pin_input_view, "OK");
  76. desktop_view_pin_input_set_label_secondary(desktop->pin_input_view, 44, 25, "Enter PIN:");
  77. desktop_view_pin_input_set_pin_position(desktop->pin_input_view, 64, 37);
  78. desktop_view_pin_input_reset_pin(desktop->pin_input_view);
  79. view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdPinInput);
  80. }
  81. bool desktop_scene_pin_input_on_event(void* context, SceneManagerEvent event) {
  82. Desktop* desktop = (Desktop*)context;
  83. bool consumed = false;
  84. uint32_t pin_fails = 0;
  85. if(event.type == SceneManagerEventTypeCustom) {
  86. switch(event.event) {
  87. case DesktopPinInputEventUnlockFailed:
  88. pin_fails = furi_hal_rtc_get_pin_fails();
  89. pin_fails++;
  90. furi_hal_rtc_set_pin_fails(pin_fails);
  91. uint32_t pin_timeout = desktop_helpers_get_pin_fail_timeout(pin_fails);
  92. if(pin_timeout > 0) {
  93. desktop_helpers_emit_error_notification();
  94. scene_manager_set_scene_state(
  95. desktop->scene_manager, DesktopScenePinTimeout, pin_timeout);
  96. scene_manager_next_scene(desktop->scene_manager, DesktopScenePinTimeout);
  97. } else {
  98. desktop_scene_locked_light_red(true);
  99. desktop_view_pin_input_set_label_primary(desktop->pin_input_view, 0, 0, NULL);
  100. desktop_view_pin_input_set_label_secondary(
  101. desktop->pin_input_view, 25, 25, "Wrong PIN try again:");
  102. desktop_scene_pin_input_set_timer(desktop, true, WRONG_PIN_HEADER_TIMEOUT);
  103. desktop_view_pin_input_reset_pin(desktop->pin_input_view);
  104. }
  105. consumed = true;
  106. break;
  107. case DesktopPinInputEventResetWrongPinLabel:
  108. desktop_scene_locked_light_red(false);
  109. desktop_view_pin_input_set_label_primary(desktop->pin_input_view, 0, 0, NULL);
  110. desktop_view_pin_input_set_label_secondary(
  111. desktop->pin_input_view, 44, 25, "Enter PIN:");
  112. consumed = true;
  113. break;
  114. case DesktopPinInputEventUnlocked:
  115. desktop_unlock(desktop);
  116. consumed = true;
  117. break;
  118. case DesktopPinInputEventBack:
  119. scene_manager_search_and_switch_to_previous_scene(
  120. desktop->scene_manager, DesktopSceneLocked);
  121. notification_message(desktop->notification, &sequence_display_off);
  122. consumed = true;
  123. break;
  124. }
  125. }
  126. return consumed;
  127. }
  128. void desktop_scene_pin_input_on_exit(void* context) {
  129. Desktop* desktop = (Desktop*)context;
  130. desktop_scene_locked_light_red(false);
  131. DesktopScenePinInputState* state = (DesktopScenePinInputState*)scene_manager_get_scene_state(
  132. desktop->scene_manager, DesktopScenePinInput);
  133. xTimerStop(state->timer, portMAX_DELAY);
  134. while(xTimerIsTimerActive(state->timer)) {
  135. furi_hal_delay_ms(1);
  136. }
  137. xTimerDelete(state->timer, portMAX_DELAY);
  138. free(state);
  139. }