totp_scene_generate_token.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include <gui/gui.h>
  2. #include <notification/notification.h>
  3. #include <notification/notification_messages.h>
  4. #include <totp_icons.h>
  5. #include <roll_value.h>
  6. #include <available_fonts.h>
  7. #include "totp_scene_generate_token.h"
  8. #include "../../canvas_extensions.h"
  9. #include "../../../types/token_info.h"
  10. #include "../../../types/common.h"
  11. #include "../../constants.h"
  12. #include "../../../services/config/config.h"
  13. #include "../../scene_director.h"
  14. #include "../token_menu/totp_scene_token_menu.h"
  15. #include "../../../features_config.h"
  16. #include "../../../workers/generate_totp_code/generate_totp_code.h"
  17. #include "../../../workers/usb_type_code/usb_type_code.h"
  18. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  19. #include "../../../workers/bt_type_code/bt_type_code.h"
  20. #endif
  21. #define PROGRESS_BAR_MARGIN (3)
  22. #define PROGRESS_BAR_HEIGHT (4)
  23. typedef struct {
  24. uint8_t progress_bar_x;
  25. uint8_t progress_bar_width;
  26. uint8_t code_total_length;
  27. uint8_t code_offset_x;
  28. uint8_t code_offset_y;
  29. } UiPrecalculatedDimensions;
  30. typedef struct {
  31. char last_code[TokenDigitsCountMax + 1];
  32. TotpUsbTypeCodeWorkerContext* usb_type_code_worker_context;
  33. NotificationMessage const** notification_sequence_new_token;
  34. NotificationMessage const** notification_sequence_automation;
  35. FuriMutex* last_code_update_sync;
  36. TotpGenerateCodeWorkerContext* generate_code_worker_context;
  37. UiPrecalculatedDimensions ui_precalculated_dimensions;
  38. const FONT_INFO* active_font;
  39. NotificationApp* notification_app;
  40. } SceneState;
  41. static const NotificationSequence*
  42. get_notification_sequence_new_token(const PluginState* plugin_state, SceneState* scene_state) {
  43. if(scene_state->notification_sequence_new_token == NULL) {
  44. uint8_t i = 0;
  45. uint8_t length = 4;
  46. if(plugin_state->notification_method & NotificationMethodVibro) {
  47. length += 2;
  48. }
  49. if(plugin_state->notification_method & NotificationMethodSound) {
  50. length += 2;
  51. }
  52. scene_state->notification_sequence_new_token = malloc(sizeof(void*) * length);
  53. furi_check(scene_state->notification_sequence_new_token != NULL);
  54. scene_state->notification_sequence_new_token[i++] = &message_display_backlight_on;
  55. scene_state->notification_sequence_new_token[i++] = &message_green_255;
  56. if(plugin_state->notification_method & NotificationMethodVibro) {
  57. scene_state->notification_sequence_new_token[i++] = &message_vibro_on;
  58. }
  59. if(plugin_state->notification_method & NotificationMethodSound) {
  60. scene_state->notification_sequence_new_token[i++] = &message_note_c5;
  61. }
  62. scene_state->notification_sequence_new_token[i++] = &message_delay_50;
  63. if(plugin_state->notification_method & NotificationMethodVibro) {
  64. scene_state->notification_sequence_new_token[i++] = &message_vibro_off;
  65. }
  66. if(plugin_state->notification_method & NotificationMethodSound) {
  67. scene_state->notification_sequence_new_token[i++] = &message_sound_off;
  68. }
  69. scene_state->notification_sequence_new_token[i++] = NULL;
  70. }
  71. return (NotificationSequence*)scene_state->notification_sequence_new_token;
  72. }
  73. static const NotificationSequence*
  74. get_notification_sequence_automation(const PluginState* plugin_state, SceneState* scene_state) {
  75. if(scene_state->notification_sequence_automation == NULL) {
  76. uint8_t i = 0;
  77. uint8_t length = 3;
  78. if(plugin_state->notification_method & NotificationMethodVibro) {
  79. length += 2;
  80. }
  81. if(plugin_state->notification_method & NotificationMethodSound) {
  82. length += 6;
  83. }
  84. scene_state->notification_sequence_automation = malloc(sizeof(void*) * length);
  85. furi_check(scene_state->notification_sequence_automation != NULL);
  86. scene_state->notification_sequence_automation[i++] = &message_blue_255;
  87. if(plugin_state->notification_method & NotificationMethodVibro) {
  88. scene_state->notification_sequence_automation[i++] = &message_vibro_on;
  89. }
  90. if(plugin_state->notification_method & NotificationMethodSound) {
  91. scene_state->notification_sequence_automation[i++] = &message_note_d5; //-V525
  92. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  93. scene_state->notification_sequence_automation[i++] = &message_note_e4;
  94. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  95. scene_state->notification_sequence_automation[i++] = &message_note_f3;
  96. }
  97. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  98. if(plugin_state->notification_method & NotificationMethodVibro) {
  99. scene_state->notification_sequence_automation[i++] = &message_vibro_off;
  100. }
  101. if(plugin_state->notification_method & NotificationMethodSound) {
  102. scene_state->notification_sequence_automation[i++] = &message_sound_off;
  103. }
  104. scene_state->notification_sequence_automation[i++] = NULL;
  105. }
  106. return (NotificationSequence*)scene_state->notification_sequence_automation;
  107. }
  108. static void update_totp_params(PluginState* const plugin_state, size_t token_index) {
  109. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  110. TokenInfoIteratorContext* iterator_context =
  111. totp_config_get_token_iterator_context(plugin_state);
  112. if(totp_token_info_iterator_go_to(iterator_context, token_index)) {
  113. totp_generate_code_worker_notify(
  114. scene_state->generate_code_worker_context, TotpGenerateCodeWorkerEventForceUpdate);
  115. }
  116. }
  117. static void draw_totp_code(Canvas* const canvas, const PluginState* const plugin_state) {
  118. const SceneState* scene_state = plugin_state->current_scene_state;
  119. const TokenInfoIteratorContext* iterator_context =
  120. totp_config_get_token_iterator_context(plugin_state);
  121. uint8_t code_length = totp_token_info_iterator_get_current_token(iterator_context)->digits;
  122. canvas_draw_str_ex(
  123. canvas,
  124. scene_state->ui_precalculated_dimensions.code_offset_x,
  125. scene_state->ui_precalculated_dimensions.code_offset_y,
  126. scene_state->last_code,
  127. code_length,
  128. scene_state->active_font);
  129. }
  130. static void on_new_token_code_generated(bool time_left, void* context) {
  131. PluginState* const plugin_state = context;
  132. const TokenInfoIteratorContext* iterator_context =
  133. totp_config_get_token_iterator_context(plugin_state);
  134. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  135. return;
  136. }
  137. SceneState* scene_state = plugin_state->current_scene_state;
  138. const TokenInfo* current_token = totp_token_info_iterator_get_current_token(iterator_context);
  139. const FONT_INFO* const font = scene_state->active_font;
  140. uint8_t char_width = font->charInfo[0].width;
  141. scene_state->ui_precalculated_dimensions.code_total_length =
  142. current_token->digits * (char_width + font->spacePixels);
  143. scene_state->ui_precalculated_dimensions.code_offset_x =
  144. (SCREEN_WIDTH - scene_state->ui_precalculated_dimensions.code_total_length) >> 1;
  145. scene_state->ui_precalculated_dimensions.code_offset_y =
  146. SCREEN_HEIGHT_CENTER - (font->height >> 1);
  147. if(time_left) {
  148. notification_message(
  149. scene_state->notification_app,
  150. get_notification_sequence_new_token(plugin_state, scene_state));
  151. }
  152. totp_scene_director_force_redraw(plugin_state);
  153. }
  154. static void on_code_lifetime_updated_generated(float code_lifetime_percent, void* context) {
  155. PluginState* const plugin_state = context;
  156. SceneState* scene_state = plugin_state->current_scene_state;
  157. scene_state->ui_precalculated_dimensions.progress_bar_width =
  158. (uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * code_lifetime_percent);
  159. scene_state->ui_precalculated_dimensions.progress_bar_x =
  160. ((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) -
  161. scene_state->ui_precalculated_dimensions.progress_bar_width) >>
  162. 1) +
  163. PROGRESS_BAR_MARGIN;
  164. totp_scene_director_force_redraw(plugin_state);
  165. }
  166. void totp_scene_generate_token_activate(PluginState* plugin_state) {
  167. SceneState* scene_state = malloc(sizeof(SceneState));
  168. furi_check(scene_state != NULL);
  169. plugin_state->current_scene_state = scene_state;
  170. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  171. scene_state->last_code_update_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  172. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  173. scene_state->usb_type_code_worker_context = totp_usb_type_code_worker_start(
  174. scene_state->last_code,
  175. TokenDigitsCountMax + 1,
  176. scene_state->last_code_update_sync,
  177. plugin_state->automation_kb_layout);
  178. }
  179. scene_state->active_font = available_fonts[plugin_state->active_font_index];
  180. scene_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
  181. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  182. if(plugin_state->automation_method & AutomationMethodBadBt) {
  183. if(plugin_state->bt_type_code_worker_context == NULL) {
  184. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  185. }
  186. totp_bt_type_code_worker_start(
  187. plugin_state->bt_type_code_worker_context,
  188. scene_state->last_code,
  189. TokenDigitsCountMax + 1,
  190. scene_state->last_code_update_sync,
  191. plugin_state->automation_kb_layout);
  192. }
  193. #endif
  194. const TokenInfoIteratorContext* iterator_context =
  195. totp_config_get_token_iterator_context(plugin_state);
  196. scene_state->generate_code_worker_context = totp_generate_code_worker_start(
  197. scene_state->last_code,
  198. totp_token_info_iterator_get_current_token(iterator_context),
  199. scene_state->last_code_update_sync,
  200. plugin_state->timezone_offset,
  201. plugin_state->iv,
  202. plugin_state->crypto_version,
  203. plugin_state->crypto_key_slot);
  204. totp_generate_code_worker_set_code_generated_handler(
  205. scene_state->generate_code_worker_context, &on_new_token_code_generated, plugin_state);
  206. totp_generate_code_worker_set_lifetime_changed_handler(
  207. scene_state->generate_code_worker_context,
  208. &on_code_lifetime_updated_generated,
  209. plugin_state);
  210. update_totp_params(
  211. plugin_state, totp_token_info_iterator_get_current_token_index(iterator_context));
  212. }
  213. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  214. const TokenInfoIteratorContext* iterator_context =
  215. totp_config_get_token_iterator_context(plugin_state);
  216. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  217. canvas_draw_str_aligned(
  218. canvas,
  219. SCREEN_WIDTH_CENTER,
  220. SCREEN_HEIGHT_CENTER - 10,
  221. AlignCenter,
  222. AlignCenter,
  223. "Token list is empty");
  224. canvas_draw_str_aligned(
  225. canvas,
  226. SCREEN_WIDTH_CENTER,
  227. SCREEN_HEIGHT_CENTER + 10,
  228. AlignCenter,
  229. AlignCenter,
  230. "Press OK button to add");
  231. return;
  232. }
  233. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  234. canvas_set_font(canvas, FontPrimary);
  235. const char* token_name_cstr =
  236. furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name);
  237. uint16_t token_name_width = canvas_string_width(canvas, token_name_cstr);
  238. if(SCREEN_WIDTH - token_name_width > 18) {
  239. canvas_draw_str_aligned(
  240. canvas,
  241. SCREEN_WIDTH_CENTER,
  242. SCREEN_HEIGHT_CENTER - 20,
  243. AlignCenter,
  244. AlignCenter,
  245. token_name_cstr);
  246. } else {
  247. canvas_draw_str_aligned(
  248. canvas, 9, SCREEN_HEIGHT_CENTER - 20, AlignLeft, AlignCenter, token_name_cstr);
  249. canvas_set_color(canvas, ColorWhite);
  250. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  251. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  252. canvas_set_color(canvas, ColorBlack);
  253. }
  254. draw_totp_code(canvas, plugin_state);
  255. canvas_draw_box(
  256. canvas,
  257. scene_state->ui_precalculated_dimensions.progress_bar_x,
  258. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  259. scene_state->ui_precalculated_dimensions.progress_bar_width,
  260. PROGRESS_BAR_HEIGHT);
  261. if(totp_token_info_iterator_get_total_count(iterator_context) > 1) {
  262. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  263. canvas_draw_icon(
  264. canvas, SCREEN_WIDTH - 8, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  265. }
  266. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  267. canvas_draw_icon(
  268. canvas,
  269. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  270. SCREEN_WIDTH_CENTER -
  271. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  272. #else
  273. SCREEN_WIDTH_CENTER - 15,
  274. #endif
  275. SCREEN_HEIGHT_CENTER + 12,
  276. &I_hid_usb_31x9);
  277. }
  278. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  279. if(plugin_state->automation_method & AutomationMethodBadBt &&
  280. plugin_state->bt_type_code_worker_context != NULL &&
  281. totp_bt_type_code_worker_is_advertising(plugin_state->bt_type_code_worker_context)) {
  282. canvas_draw_icon(
  283. canvas,
  284. SCREEN_WIDTH_CENTER +
  285. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  286. SCREEN_HEIGHT_CENTER + 12,
  287. &I_hid_ble_31x9);
  288. }
  289. #endif
  290. }
  291. bool totp_scene_generate_token_handle_event(
  292. const PluginEvent* const event,
  293. PluginState* plugin_state) {
  294. if(event->type != EventTypeKey) {
  295. return true;
  296. }
  297. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  298. return false;
  299. }
  300. SceneState* scene_state;
  301. if(event->input.type == InputTypeLong) {
  302. if(event->input.key == InputKeyDown &&
  303. plugin_state->automation_method & AutomationMethodBadUsb) {
  304. scene_state = (SceneState*)plugin_state->current_scene_state;
  305. const TokenInfoIteratorContext* iterator_context =
  306. totp_config_get_token_iterator_context(plugin_state);
  307. totp_usb_type_code_worker_notify(
  308. scene_state->usb_type_code_worker_context,
  309. TotpUsbTypeCodeWorkerEventType,
  310. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  311. notification_message(
  312. scene_state->notification_app,
  313. get_notification_sequence_automation(plugin_state, scene_state));
  314. return true;
  315. }
  316. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  317. else if(
  318. event->input.key == InputKeyUp &&
  319. plugin_state->automation_method & AutomationMethodBadBt) {
  320. scene_state = (SceneState*)plugin_state->current_scene_state;
  321. const TokenInfoIteratorContext* iterator_context =
  322. totp_config_get_token_iterator_context(plugin_state);
  323. totp_bt_type_code_worker_notify(
  324. plugin_state->bt_type_code_worker_context,
  325. TotpBtTypeCodeWorkerEventType,
  326. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  327. notification_message(
  328. scene_state->notification_app,
  329. get_notification_sequence_automation(plugin_state, scene_state));
  330. return true;
  331. }
  332. #endif
  333. } else if(event->input.type == InputTypePress || event->input.type == InputTypeRepeat) {
  334. switch(event->input.key) {
  335. case InputKeyUp:
  336. break;
  337. case InputKeyDown:
  338. break;
  339. case InputKeyRight: {
  340. const TokenInfoIteratorContext* iterator_context =
  341. totp_config_get_token_iterator_context(plugin_state);
  342. size_t current_token_index =
  343. totp_token_info_iterator_get_current_token_index(iterator_context);
  344. totp_roll_value_size_t(
  345. &current_token_index,
  346. 1,
  347. 0,
  348. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  349. RollOverflowBehaviorRoll);
  350. update_totp_params(plugin_state, current_token_index);
  351. break;
  352. }
  353. case InputKeyLeft: {
  354. const TokenInfoIteratorContext* iterator_context =
  355. totp_config_get_token_iterator_context(plugin_state);
  356. size_t current_token_index =
  357. totp_token_info_iterator_get_current_token_index(iterator_context);
  358. totp_roll_value_size_t(
  359. &current_token_index,
  360. -1,
  361. 0,
  362. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  363. RollOverflowBehaviorRoll);
  364. update_totp_params(plugin_state, current_token_index);
  365. break;
  366. }
  367. case InputKeyOk:
  368. break;
  369. case InputKeyBack:
  370. break;
  371. default:
  372. break;
  373. }
  374. } else if(event->input.type == InputTypeRelease && event->input.key == InputKeyOk) {
  375. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  376. }
  377. return true;
  378. }
  379. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  380. if(plugin_state->current_scene_state == NULL) return;
  381. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  382. totp_generate_code_worker_stop(scene_state->generate_code_worker_context);
  383. furi_record_close(RECORD_NOTIFICATION);
  384. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  385. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  386. }
  387. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  388. if(plugin_state->automation_method & AutomationMethodBadBt) {
  389. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  390. }
  391. #endif
  392. if(scene_state->notification_sequence_new_token != NULL) {
  393. free(scene_state->notification_sequence_new_token);
  394. }
  395. if(scene_state->notification_sequence_automation != NULL) {
  396. free(scene_state->notification_sequence_automation);
  397. }
  398. furi_mutex_free(scene_state->last_code_update_sync);
  399. free(scene_state);
  400. plugin_state->current_scene_state = NULL;
  401. }