u2f_scene_main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_on);
  57. notification_message(app->notifications, &sequence_single_vibro);
  58. }
  59. notification_message(app->notifications, &sequence_blink_blue_10);
  60. } else if(event.event == U2fCustomEventWink) {
  61. notification_message(app->notifications, &sequence_blink_green_10);
  62. } else if(event.event == U2fCustomEventAuthSuccess) {
  63. DOLPHIN_DEED(DolphinDeedU2fAuthorized);
  64. osTimerStart(app->timer, U2F_SUCCESS_TIMEOUT);
  65. app->event_cur = U2fCustomEventNone;
  66. u2f_view_set_state(app->u2f_view, U2fMsgSuccess);
  67. } else if(event.event == U2fCustomEventTimeout) {
  68. app->event_cur = U2fCustomEventNone;
  69. u2f_view_set_state(app->u2f_view, U2fMsgIdle);
  70. } else if(event.event == U2fCustomEventConfirm) {
  71. if(app->event_cur != U2fCustomEventNone) {
  72. u2f_confirm_user_present(app->u2f_instance);
  73. }
  74. } else if(event.event == U2fCustomEventDataError) {
  75. osTimerStop(app->timer);
  76. u2f_view_set_state(app->u2f_view, U2fMsgError);
  77. }
  78. consumed = true;
  79. }
  80. return consumed;
  81. }
  82. void u2f_scene_main_on_enter(void* context) {
  83. U2fApp* app = context;
  84. app->timer = osTimerNew(u2f_scene_main_timer_callback, osTimerOnce, app, NULL);
  85. app->u2f_instance = u2f_alloc();
  86. app->u2f_ready = u2f_init(app->u2f_instance);
  87. if(app->u2f_ready == true) {
  88. u2f_set_event_callback(app->u2f_instance, u2f_scene_main_event_callback, app);
  89. app->u2f_hid = u2f_hid_start(app->u2f_instance);
  90. u2f_view_set_ok_callback(app->u2f_view, u2f_scene_main_ok_callback, app);
  91. } else {
  92. u2f_free(app->u2f_instance);
  93. u2f_view_set_state(app->u2f_view, U2fMsgError);
  94. }
  95. view_dispatcher_switch_to_view(app->view_dispatcher, U2fAppViewMain);
  96. }
  97. void u2f_scene_main_on_exit(void* context) {
  98. U2fApp* app = context;
  99. osTimerStop(app->timer);
  100. osTimerDelete(app->timer);
  101. if(app->u2f_ready == true) {
  102. u2f_hid_stop(app->u2f_hid);
  103. u2f_free(app->u2f_instance);
  104. }
  105. }