u2f_scene_main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  29. static void u2f_scene_main_timer_callback(void* context) {
  30. furi_assert(context);
  31. U2fApp* app = context;
  32. view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventTimeout);
  33. }
  34. bool u2f_scene_main_on_event(void* context, SceneManagerEvent event) {
  35. furi_assert(context);
  36. U2fApp* app = context;
  37. bool consumed = false;
  38. if(event.type == SceneManagerEventTypeCustom) {
  39. if(event.event == U2fCustomEventConnect) {
  40. osTimerStop(app->timer);
  41. u2f_view_set_state(app->u2f_view, U2fMsgIdle);
  42. } else if(event.event == U2fCustomEventDisconnect) {
  43. osTimerStop(app->timer);
  44. app->event_cur = U2fCustomEventNone;
  45. u2f_view_set_state(app->u2f_view, U2fMsgNotConnected);
  46. } else if((event.event == U2fCustomEventRegister) || (event.event == U2fCustomEventAuth)) {
  47. osTimerStart(app->timer, U2F_REQUEST_TIMEOUT);
  48. if(app->event_cur == U2fCustomEventNone) {
  49. app->event_cur = event.event;
  50. if(event.event == U2fCustomEventRegister)
  51. u2f_view_set_state(app->u2f_view, U2fMsgRegister);
  52. else if(event.event == U2fCustomEventAuth)
  53. u2f_view_set_state(app->u2f_view, U2fMsgAuth);
  54. notification_message(app->notifications, &sequence_display_on);
  55. notification_message(app->notifications, &sequence_single_vibro);
  56. }
  57. notification_message(app->notifications, &sequence_blink_blue_10);
  58. } else if(event.event == U2fCustomEventWink) {
  59. notification_message(app->notifications, &sequence_blink_green_10);
  60. } else if(event.event == U2fCustomEventAuthSuccess) {
  61. DOLPHIN_DEED(DolphinDeedU2fAuthorized);
  62. osTimerStart(app->timer, U2F_SUCCESS_TIMEOUT);
  63. app->event_cur = U2fCustomEventNone;
  64. u2f_view_set_state(app->u2f_view, U2fMsgSuccess);
  65. } else if(event.event == U2fCustomEventTimeout) {
  66. app->event_cur = U2fCustomEventNone;
  67. u2f_view_set_state(app->u2f_view, U2fMsgIdle);
  68. } else if(event.event == U2fCustomEventConfirm) {
  69. if(app->event_cur != U2fCustomEventNone) {
  70. u2f_confirm_user_present(app->u2f_instance);
  71. }
  72. }
  73. consumed = true;
  74. } else if(event.type == SceneManagerEventTypeTick) {
  75. }
  76. return consumed;
  77. }
  78. void u2f_scene_main_on_enter(void* context) {
  79. U2fApp* app = context;
  80. app->timer = osTimerNew(u2f_scene_main_timer_callback, osTimerOnce, app, NULL);
  81. app->u2f_instance = u2f_alloc();
  82. app->u2f_ready = u2f_init(app->u2f_instance);
  83. if(app->u2f_ready == true) {
  84. u2f_set_event_callback(app->u2f_instance, u2f_scene_main_event_callback, app);
  85. app->u2f_hid = u2f_hid_start(app->u2f_instance);
  86. u2f_view_set_ok_callback(app->u2f_view, u2f_scene_main_ok_callback, app);
  87. } else {
  88. u2f_free(app->u2f_instance);
  89. u2f_view_set_state(app->u2f_view, U2fMsgError);
  90. }
  91. view_dispatcher_switch_to_view(app->view_dispatcher, U2fAppViewMain);
  92. }
  93. void u2f_scene_main_on_exit(void* context) {
  94. U2fApp* app = context;
  95. osTimerStop(app->timer);
  96. osTimerDelete(app->timer);
  97. if(app->u2f_ready == true) {
  98. u2f_hid_stop(app->u2f_hid);
  99. u2f_free(app->u2f_instance);
  100. }
  101. }