u2f_scene_main.c 5.1 KB

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