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 "../helpers/pin.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(RECORD_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(RECORD_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(desktop_pin_compare(&desktop->settings.pin_code, pin_code)) {
  50. furi_hal_rtc_set_pin_fails(0);
  51. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopPinInputEventUnlocked);
  52. } else {
  53. uint32_t pin_fails = furi_hal_rtc_get_pin_fails();
  54. furi_hal_rtc_set_pin_fails(pin_fails + 1);
  55. view_dispatcher_send_custom_event(
  56. desktop->view_dispatcher, DesktopPinInputEventUnlockFailed);
  57. }
  58. }
  59. static void desktop_scene_pin_input_timer_callback(TimerHandle_t timer) {
  60. Desktop* desktop = pvTimerGetTimerID(timer);
  61. view_dispatcher_send_custom_event(
  62. desktop->view_dispatcher, DesktopPinInputEventResetWrongPinLabel);
  63. }
  64. void desktop_scene_pin_input_on_enter(void* context) {
  65. Desktop* desktop = (Desktop*)context;
  66. desktop_view_pin_input_set_context(desktop->pin_input_view, desktop);
  67. desktop_view_pin_input_set_back_callback(
  68. desktop->pin_input_view, desktop_scene_pin_input_back_callback);
  69. desktop_view_pin_input_set_timeout_callback(
  70. desktop->pin_input_view, desktop_scene_pin_input_back_callback);
  71. desktop_view_pin_input_set_done_callback(
  72. desktop->pin_input_view, desktop_scene_pin_input_done_callback);
  73. DesktopScenePinInputState* state = malloc(sizeof(DesktopScenePinInputState));
  74. state->timer =
  75. xTimerCreate(NULL, 10000, pdFALSE, desktop, desktop_scene_pin_input_timer_callback);
  76. scene_manager_set_scene_state(desktop->scene_manager, DesktopScenePinInput, (uint32_t)state);
  77. desktop_view_pin_input_hide_pin(desktop->pin_input_view, true);
  78. desktop_view_pin_input_set_label_button(desktop->pin_input_view, "OK");
  79. desktop_view_pin_input_set_label_secondary(desktop->pin_input_view, 44, 25, "Enter PIN:");
  80. desktop_view_pin_input_set_pin_position(desktop->pin_input_view, 64, 37);
  81. desktop_view_pin_input_reset_pin(desktop->pin_input_view);
  82. view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdPinInput);
  83. }
  84. bool desktop_scene_pin_input_on_event(void* context, SceneManagerEvent event) {
  85. Desktop* desktop = (Desktop*)context;
  86. bool consumed = false;
  87. uint32_t pin_timeout = 0;
  88. if(event.type == SceneManagerEventTypeCustom) {
  89. switch(event.event) {
  90. case DesktopPinInputEventUnlockFailed:
  91. pin_timeout = desktop_pin_lock_get_fail_timeout();
  92. if(pin_timeout > 0) {
  93. desktop_pin_lock_error_notify();
  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_backlight_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_delay_tick(1);
  136. }
  137. xTimerDelete(state->timer, portMAX_DELAY);
  138. free(state);
  139. }