u2f_scene_main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "../u2f_app_i.h"
  2. #include "../views/u2f_view.h"
  3. #include <dolphin/dolphin.h>
  4. #include "furi_hal.h"
  5. #include "../u2f.h"
  6. #define U2F_REQUEST_TIMEOUT 500
  7. #define U2F_SUCCESS_TIMEOUT 3000
  8. static void u2f_scene_main_ok_callback(InputType type, void* context) {
  9. UNUSED(type);
  10. furi_assert(context);
  11. U2fApp* app = context;
  12. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventConfirm);
  13. }
  14. static void u2f_scene_main_event_callback(U2fNotifyEvent evt, void* context) {
  15. furi_assert(context);
  16. U2fApp* app = context;
  17. if(evt == U2fNotifyRegister)
  18. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventRegister);
  19. else if(evt == U2fNotifyAuth)
  20. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventAuth);
  21. else if(evt == U2fNotifyAuthSuccess)
  22. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventAuthSuccess);
  23. else if(evt == U2fNotifyWink)
  24. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventWink);
  25. else if(evt == U2fNotifyConnect)
  26. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventConnect);
  27. else if(evt == U2fNotifyDisconnect)
  28. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventDisconnect);
  29. else if(evt == U2fNotifyError)
  30. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventDataError);
  31. }
  32. static void u2f_scene_main_timer_callback(void* context) {
  33. furi_assert(context);
  34. U2fApp* app = context;
  35. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventTimeout);
  36. }
  37. bool u2f_scene_main_on_event(void* context, SceneManagerEvent event) {
  38. furi_assert(context);
  39. U2fApp* app = context;
  40. bool consumed = false;
  41. if(event.type == SceneManagerEventTypeCustom) {
  42. if(event.event == U2fCustomEventConnect) {
  43. osTimerStop(app->timer);
  44. u2f_view_set_state(app->u2f_view, U2fMsgIdle);
  45. } else if(event.event == U2fCustomEventDisconnect) {
  46. osTimerStop(app->timer);
  47. app->event_cur = U2fCustomEventNone;
  48. u2f_view_set_state(app->u2f_view, U2fMsgNotConnected);
  49. } else if((event.event == U2fCustomEventRegister) || (event.event == U2fCustomEventAuth)) {
  50. osTimerStart(app->timer, U2F_REQUEST_TIMEOUT);
  51. if(app->event_cur == U2fCustomEventNone) {
  52. app->event_cur = event.event;
  53. if(event.event == U2fCustomEventRegister)
  54. u2f_view_set_state(app->u2f_view, U2fMsgRegister);
  55. else if(event.event == U2fCustomEventAuth)
  56. u2f_view_set_state(app->u2f_view, U2fMsgAuth);
  57. notification_message(app->notifications, &sequence_display_backlight_on);
  58. notification_message(app->notifications, &sequence_single_vibro);
  59. }
  60. notification_message(app->notifications, &sequence_blink_magenta_10);
  61. } else if(event.event == U2fCustomEventWink) {
  62. notification_message(app->notifications, &sequence_blink_magenta_10);
  63. } else if(event.event == U2fCustomEventAuthSuccess) {
  64. notification_message_block(app->notifications, &sequence_set_green_255);
  65. DOLPHIN_DEED(DolphinDeedU2fAuthorized);
  66. osTimerStart(app->timer, U2F_SUCCESS_TIMEOUT);
  67. app->event_cur = U2fCustomEventNone;
  68. u2f_view_set_state(app->u2f_view, U2fMsgSuccess);
  69. } else if(event.event == U2fCustomEventTimeout) {
  70. notification_message_block(app->notifications, &sequence_reset_rgb);
  71. app->event_cur = U2fCustomEventNone;
  72. u2f_view_set_state(app->u2f_view, U2fMsgIdle);
  73. } else if(event.event == U2fCustomEventConfirm) {
  74. if(app->event_cur != U2fCustomEventNone) {
  75. u2f_confirm_user_present(app->u2f_instance);
  76. }
  77. } else if(event.event == U2fCustomEventDataError) {
  78. notification_message(app->notifications, &sequence_set_red_255);
  79. osTimerStop(app->timer);
  80. u2f_view_set_state(app->u2f_view, U2fMsgError);
  81. }
  82. consumed = true;
  83. }
  84. return consumed;
  85. }
  86. void u2f_scene_main_on_enter(void* context) {
  87. U2fApp* app = context;
  88. app->timer = osTimerNew(u2f_scene_main_timer_callback, osTimerOnce, app, NULL);
  89. app->u2f_instance = u2f_alloc();
  90. app->u2f_ready = u2f_init(app->u2f_instance);
  91. if(app->u2f_ready == true) {
  92. u2f_set_event_callback(app->u2f_instance, u2f_scene_main_event_callback, app);
  93. app->u2f_hid = u2f_hid_start(app->u2f_instance);
  94. u2f_view_set_ok_callback(app->u2f_view, u2f_scene_main_ok_callback, app);
  95. } else {
  96. u2f_free(app->u2f_instance);
  97. u2f_view_set_state(app->u2f_view, U2fMsgError);
  98. }
  99. view_dispatcher_switch_to_view(app->view_dispatcher, U2fAppViewMain);
  100. }
  101. void u2f_scene_main_on_exit(void* context) {
  102. U2fApp* app = context;
  103. notification_message_block(app->notifications, &sequence_reset_rgb);
  104. osTimerStop(app->timer);
  105. osTimerDelete(app->timer);
  106. if(app->u2f_ready == true) {
  107. u2f_hid_stop(app->u2f_hid);
  108. u2f_free(app->u2f_instance);
  109. }
  110. }