u2f_scene_main.c 4.5 KB

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