u2f_scene_main.c 3.4 KB

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