totp_app.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <dialogs/dialogs.h>
  6. #include <stdlib.h>
  7. #include <flipper_format/flipper_format.h>
  8. #include <notification/notification.h>
  9. #include <notification/notification_messages.h>
  10. #include "services/base32/base32.h"
  11. #include "services/list/list.h"
  12. #include "services/config/config.h"
  13. #include "types/plugin_state.h"
  14. #include "types/token_info.h"
  15. #include "types/plugin_event.h"
  16. #include "types/event_type.h"
  17. #include "types/common.h"
  18. #include "scenes/scene_director.h"
  19. #include "services/ui/constants.h"
  20. #include "services/crypto/crypto.h"
  21. #define IDLE_TIMEOUT 60000
  22. static void render_callback(Canvas* const canvas, void* ctx) {
  23. PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
  24. if (plugin_state != NULL && !plugin_state->changing_scene) {
  25. totp_scene_director_render(canvas, plugin_state);
  26. }
  27. release_mutex((ValueMutex*)ctx, plugin_state);
  28. }
  29. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  30. furi_assert(event_queue);
  31. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  32. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  33. }
  34. static void totp_state_init(PluginState* const plugin_state) {
  35. plugin_state->gui = furi_record_open(RECORD_GUI);
  36. plugin_state->notification = furi_record_open(RECORD_NOTIFICATION);
  37. plugin_state->dialogs = furi_record_open(RECORD_DIALOGS);
  38. totp_config_file_load_base(plugin_state);
  39. totp_scene_director_init_scenes(plugin_state);
  40. if (plugin_state->crypto_verify_data == NULL) {
  41. DialogMessage* message = dialog_message_alloc();
  42. dialog_message_set_buttons(message, "No", NULL, "Yes");
  43. dialog_message_set_text(message, "Would you like to setup PIN?", SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER, AlignCenter, AlignCenter);
  44. DialogMessageButton dialog_result = dialog_message_show(plugin_state->dialogs, message);
  45. dialog_message_free(message);
  46. if (dialog_result == DialogMessageButtonRight) {
  47. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  48. } else {
  49. totp_crypto_seed_iv(plugin_state, NULL, 0);
  50. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  51. }
  52. } else if (plugin_state->pin_set) {
  53. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  54. } else {
  55. totp_crypto_seed_iv(plugin_state, NULL, 0);
  56. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  57. }
  58. }
  59. static void dispose_plugin_state(PluginState* plugin_state) {
  60. totp_scene_director_deactivate_active_scene(plugin_state);
  61. totp_scene_director_dispose(plugin_state);
  62. furi_record_close(RECORD_GUI);
  63. furi_record_close(RECORD_NOTIFICATION);
  64. furi_record_close(RECORD_DIALOGS);
  65. ListNode* node = plugin_state->tokens_list;
  66. ListNode* tmp;
  67. while (node != NULL) {
  68. tmp = node->next;
  69. TokenInfo* tokenInfo = (TokenInfo*)node->data;
  70. token_info_free(tokenInfo);
  71. free(node);
  72. node = tmp;
  73. }
  74. if (plugin_state->crypto_verify_data != NULL) {
  75. free(plugin_state->crypto_verify_data);
  76. }
  77. free(plugin_state);
  78. }
  79. int32_t totp_app() {
  80. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  81. PluginState* plugin_state = malloc(sizeof(PluginState));
  82. totp_state_init(plugin_state);
  83. ValueMutex state_mutex;
  84. if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) {
  85. FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n");
  86. dispose_plugin_state(plugin_state);
  87. return 255;
  88. }
  89. // Set system callbacks
  90. ViewPort* view_port = view_port_alloc();
  91. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  92. view_port_input_callback_set(view_port, input_callback, event_queue);
  93. // Open GUI and register view_port
  94. gui_add_view_port(plugin_state->gui, view_port, GuiLayerFullscreen);
  95. PluginEvent event;
  96. bool processing = true;
  97. uint32_t last_user_interaction_time = furi_get_tick();
  98. while(processing) {
  99. if (plugin_state->changing_scene) continue;
  100. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  101. PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex);
  102. if(event_status == FuriStatusOk) {
  103. if (event.type == EventTypeKey) {
  104. last_user_interaction_time = furi_get_tick();
  105. }
  106. processing = totp_scene_director_handle_event(&event, plugin_state);
  107. } else if (plugin_state->current_scene != TotpSceneAuthentication && furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
  108. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  109. }
  110. view_port_update(view_port);
  111. release_mutex(&state_mutex, plugin_state);
  112. }
  113. view_port_enabled_set(view_port, false);
  114. gui_remove_view_port(plugin_state->gui, view_port);
  115. view_port_free(view_port);
  116. furi_message_queue_free(event_queue);
  117. delete_mutex(&state_mutex);
  118. dispose_plugin_state(plugin_state);
  119. return 0;
  120. }