desktop_scene_pin_input.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopPinInputEventUnlocked);
  51. } else {
  52. uint32_t pin_fails = furi_hal_rtc_get_pin_fails();
  53. furi_hal_rtc_set_pin_fails(pin_fails + 1);
  54. view_dispatcher_send_custom_event(
  55. desktop->view_dispatcher, DesktopPinInputEventUnlockFailed);
  56. }
  57. }
  58. static void desktop_scene_pin_input_timer_callback(TimerHandle_t timer) {
  59. Desktop* desktop = pvTimerGetTimerID(timer);
  60. view_dispatcher_send_custom_event(
  61. desktop->view_dispatcher, DesktopPinInputEventResetWrongPinLabel);
  62. }
  63. void desktop_scene_pin_input_on_enter(void* context) {
  64. Desktop* desktop = (Desktop*)context;
  65. desktop_view_pin_input_set_context(desktop->pin_input_view, desktop);
  66. desktop_view_pin_input_set_back_callback(
  67. desktop->pin_input_view, desktop_scene_pin_input_back_callback);
  68. desktop_view_pin_input_set_timeout_callback(
  69. desktop->pin_input_view, desktop_scene_pin_input_back_callback);
  70. desktop_view_pin_input_set_done_callback(
  71. desktop->pin_input_view, desktop_scene_pin_input_done_callback);
  72. DesktopScenePinInputState* state = malloc(sizeof(DesktopScenePinInputState));
  73. state->timer =
  74. xTimerCreate(NULL, 10000, pdFALSE, desktop, desktop_scene_pin_input_timer_callback);
  75. scene_manager_set_scene_state(desktop->scene_manager, DesktopScenePinInput, (uint32_t)state);
  76. desktop_view_pin_input_hide_pin(desktop->pin_input_view, true);
  77. desktop_view_pin_input_set_label_button(desktop->pin_input_view, "OK");
  78. desktop_view_pin_input_set_label_secondary(desktop->pin_input_view, 44, 25, "Enter PIN:");
  79. desktop_view_pin_input_set_pin_position(desktop->pin_input_view, 64, 37);
  80. desktop_view_pin_input_reset_pin(desktop->pin_input_view);
  81. view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdPinInput);
  82. }
  83. bool desktop_scene_pin_input_on_event(void* context, SceneManagerEvent event) {
  84. Desktop* desktop = (Desktop*)context;
  85. bool consumed = false;
  86. uint32_t pin_timeout = 0;
  87. if(event.type == SceneManagerEventTypeCustom) {
  88. switch(event.event) {
  89. case DesktopPinInputEventUnlockFailed:
  90. pin_timeout = desktop_pin_lock_get_fail_timeout();
  91. if(pin_timeout > 0) {
  92. desktop_pin_lock_error_notify();
  93. scene_manager_set_scene_state(
  94. desktop->scene_manager, DesktopScenePinTimeout, pin_timeout);
  95. scene_manager_next_scene(desktop->scene_manager, DesktopScenePinTimeout);
  96. } else {
  97. desktop_scene_locked_light_red(true);
  98. desktop_view_pin_input_set_label_primary(desktop->pin_input_view, 0, 0, NULL);
  99. desktop_view_pin_input_set_label_secondary(
  100. desktop->pin_input_view, 25, 25, "Wrong PIN try again:");
  101. desktop_scene_pin_input_set_timer(desktop, true, WRONG_PIN_HEADER_TIMEOUT);
  102. desktop_view_pin_input_reset_pin(desktop->pin_input_view);
  103. }
  104. consumed = true;
  105. break;
  106. case DesktopPinInputEventResetWrongPinLabel:
  107. desktop_scene_locked_light_red(false);
  108. desktop_view_pin_input_set_label_primary(desktop->pin_input_view, 0, 0, NULL);
  109. desktop_view_pin_input_set_label_secondary(
  110. desktop->pin_input_view, 44, 25, "Enter PIN:");
  111. consumed = true;
  112. break;
  113. case DesktopPinInputEventUnlocked:
  114. desktop_unlock(desktop);
  115. consumed = true;
  116. break;
  117. case DesktopPinInputEventBack:
  118. scene_manager_search_and_switch_to_previous_scene(
  119. desktop->scene_manager, DesktopSceneLocked);
  120. notification_message(desktop->notification, &sequence_display_backlight_off);
  121. consumed = true;
  122. break;
  123. }
  124. }
  125. return consumed;
  126. }
  127. void desktop_scene_pin_input_on_exit(void* context) {
  128. Desktop* desktop = (Desktop*)context;
  129. desktop_scene_locked_light_red(false);
  130. DesktopScenePinInputState* state = (DesktopScenePinInputState*)scene_manager_get_scene_state(
  131. desktop->scene_manager, DesktopScenePinInput);
  132. xTimerStop(state->timer, portMAX_DELAY);
  133. while(xTimerIsTimerActive(state->timer)) {
  134. furi_delay_tick(1);
  135. }
  136. xTimerDelete(state->timer, portMAX_DELAY);
  137. free(state);
  138. }