desktop_scene_pin_input.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_lock.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(desktop_pin_lock_verify(&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_timeout = 0;
  85. if(event.type == SceneManagerEventTypeCustom) {
  86. switch(event.event) {
  87. case DesktopPinInputEventUnlockFailed:
  88. pin_timeout = desktop_pin_lock_get_fail_timeout();
  89. if(pin_timeout > 0) {
  90. desktop_pin_lock_error_notify();
  91. scene_manager_set_scene_state(
  92. desktop->scene_manager, DesktopScenePinTimeout, pin_timeout);
  93. scene_manager_next_scene(desktop->scene_manager, DesktopScenePinTimeout);
  94. } else {
  95. desktop_scene_locked_light_red(true);
  96. desktop_view_pin_input_set_label_primary(desktop->pin_input_view, 0, 0, NULL);
  97. desktop_view_pin_input_set_label_secondary(
  98. desktop->pin_input_view, 25, 25, "Wrong PIN try again:");
  99. desktop_scene_pin_input_set_timer(desktop, true, WRONG_PIN_HEADER_TIMEOUT);
  100. desktop_view_pin_input_reset_pin(desktop->pin_input_view);
  101. }
  102. consumed = true;
  103. break;
  104. case DesktopPinInputEventResetWrongPinLabel:
  105. desktop_scene_locked_light_red(false);
  106. desktop_view_pin_input_set_label_primary(desktop->pin_input_view, 0, 0, NULL);
  107. desktop_view_pin_input_set_label_secondary(
  108. desktop->pin_input_view, 44, 25, "Enter PIN:");
  109. consumed = true;
  110. break;
  111. case DesktopPinInputEventUnlocked:
  112. desktop_pin_unlock(&desktop->settings);
  113. desktop_unlock(desktop);
  114. consumed = true;
  115. break;
  116. case DesktopPinInputEventBack:
  117. scene_manager_search_and_switch_to_previous_scene(
  118. desktop->scene_manager, DesktopSceneLocked);
  119. notification_message(desktop->notification, &sequence_display_backlight_off);
  120. consumed = true;
  121. break;
  122. }
  123. }
  124. return consumed;
  125. }
  126. void desktop_scene_pin_input_on_exit(void* context) {
  127. Desktop* desktop = (Desktop*)context;
  128. desktop_scene_locked_light_red(false);
  129. DesktopScenePinInputState* state = (DesktopScenePinInputState*)scene_manager_get_scene_state(
  130. desktop->scene_manager, DesktopScenePinInput);
  131. xTimerStop(state->timer, portMAX_DELAY);
  132. while(xTimerIsTimerActive(state->timer)) {
  133. furi_hal_delay_ms(1);
  134. }
  135. xTimerDelete(state->timer, portMAX_DELAY);
  136. free(state);
  137. }