totp_scene_generate_token.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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_TYPE_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, TokenDigitsCountMax + 1, scene_state->last_code_update_sync);
  175. }
  176. scene_state->active_font = available_fonts[plugin_state->active_font_index];
  177. scene_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
  178. #ifdef TOTP_BADBT_TYPE_ENABLED
  179. if(plugin_state->automation_method & AutomationMethodBadBt) {
  180. if(plugin_state->bt_type_code_worker_context == NULL) {
  181. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  182. }
  183. totp_bt_type_code_worker_start(
  184. plugin_state->bt_type_code_worker_context,
  185. scene_state->last_code,
  186. TokenDigitsCountMax + 1,
  187. scene_state->last_code_update_sync);
  188. }
  189. #endif
  190. const TokenInfoIteratorContext* iterator_context =
  191. totp_config_get_token_iterator_context(plugin_state);
  192. scene_state->generate_code_worker_context = totp_generate_code_worker_start(
  193. scene_state->last_code,
  194. totp_token_info_iterator_get_current_token(iterator_context),
  195. scene_state->last_code_update_sync,
  196. plugin_state->timezone_offset,
  197. plugin_state->iv,
  198. plugin_state->crypto_version,
  199. plugin_state->crypto_key_slot);
  200. totp_generate_code_worker_set_code_generated_handler(
  201. scene_state->generate_code_worker_context, &on_new_token_code_generated, plugin_state);
  202. totp_generate_code_worker_set_lifetime_changed_handler(
  203. scene_state->generate_code_worker_context,
  204. &on_code_lifetime_updated_generated,
  205. plugin_state);
  206. update_totp_params(
  207. plugin_state, totp_token_info_iterator_get_current_token_index(iterator_context));
  208. }
  209. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  210. const TokenInfoIteratorContext* iterator_context =
  211. totp_config_get_token_iterator_context(plugin_state);
  212. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  213. canvas_draw_str_aligned(
  214. canvas,
  215. SCREEN_WIDTH_CENTER,
  216. SCREEN_HEIGHT_CENTER - 10,
  217. AlignCenter,
  218. AlignCenter,
  219. "Token list is empty");
  220. canvas_draw_str_aligned(
  221. canvas,
  222. SCREEN_WIDTH_CENTER,
  223. SCREEN_HEIGHT_CENTER + 10,
  224. AlignCenter,
  225. AlignCenter,
  226. "Press OK button to add");
  227. return;
  228. }
  229. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  230. canvas_set_font(canvas, FontPrimary);
  231. const char* token_name_cstr =
  232. furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name);
  233. uint16_t token_name_width = canvas_string_width(canvas, token_name_cstr);
  234. if(SCREEN_WIDTH - token_name_width > 18) {
  235. canvas_draw_str_aligned(
  236. canvas,
  237. SCREEN_WIDTH_CENTER,
  238. SCREEN_HEIGHT_CENTER - 20,
  239. AlignCenter,
  240. AlignCenter,
  241. token_name_cstr);
  242. } else {
  243. canvas_draw_str_aligned(
  244. canvas, 9, SCREEN_HEIGHT_CENTER - 20, AlignLeft, AlignCenter, token_name_cstr);
  245. canvas_set_color(canvas, ColorWhite);
  246. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  247. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  248. canvas_set_color(canvas, ColorBlack);
  249. }
  250. draw_totp_code(canvas, plugin_state);
  251. canvas_draw_box(
  252. canvas,
  253. scene_state->ui_precalculated_dimensions.progress_bar_x,
  254. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  255. scene_state->ui_precalculated_dimensions.progress_bar_width,
  256. PROGRESS_BAR_HEIGHT);
  257. if(totp_token_info_iterator_get_total_count(iterator_context) > 1) {
  258. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  259. canvas_draw_icon(
  260. canvas, SCREEN_WIDTH - 8, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  261. }
  262. #ifdef TOTP_AUTOMATION_ICONS_ENABLED
  263. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  264. canvas_draw_icon(
  265. canvas,
  266. #ifdef TOTP_BADBT_TYPE_ENABLED
  267. SCREEN_WIDTH_CENTER -
  268. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  269. #else
  270. SCREEN_WIDTH_CENTER - 15,
  271. #endif
  272. SCREEN_HEIGHT_CENTER + 12,
  273. &I_hid_usb_31x9);
  274. }
  275. #ifdef TOTP_BADBT_TYPE_ENABLED
  276. if(plugin_state->automation_method & AutomationMethodBadBt &&
  277. plugin_state->bt_type_code_worker_context != NULL &&
  278. totp_bt_type_code_worker_is_advertising(plugin_state->bt_type_code_worker_context)) {
  279. canvas_draw_icon(
  280. canvas,
  281. SCREEN_WIDTH_CENTER +
  282. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  283. SCREEN_HEIGHT_CENTER + 12,
  284. &I_hid_ble_31x9);
  285. }
  286. #endif
  287. #endif
  288. }
  289. bool totp_scene_generate_token_handle_event(
  290. const PluginEvent* const event,
  291. PluginState* plugin_state) {
  292. if(event->type != EventTypeKey) {
  293. return true;
  294. }
  295. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  296. return false;
  297. }
  298. SceneState* scene_state;
  299. if(event->input.type == InputTypeLong) {
  300. if(event->input.key == InputKeyDown &&
  301. plugin_state->automation_method & AutomationMethodBadUsb) {
  302. scene_state = (SceneState*)plugin_state->current_scene_state;
  303. const TokenInfoIteratorContext* iterator_context =
  304. totp_config_get_token_iterator_context(plugin_state);
  305. totp_usb_type_code_worker_notify(
  306. scene_state->usb_type_code_worker_context,
  307. TotpUsbTypeCodeWorkerEventType,
  308. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  309. notification_message(
  310. scene_state->notification_app,
  311. get_notification_sequence_automation(plugin_state, scene_state));
  312. return true;
  313. }
  314. #ifdef TOTP_BADBT_TYPE_ENABLED
  315. else if(
  316. event->input.key == InputKeyUp &&
  317. plugin_state->automation_method & AutomationMethodBadBt) {
  318. scene_state = (SceneState*)plugin_state->current_scene_state;
  319. const TokenInfoIteratorContext* iterator_context =
  320. totp_config_get_token_iterator_context(plugin_state);
  321. totp_bt_type_code_worker_notify(
  322. plugin_state->bt_type_code_worker_context,
  323. TotpBtTypeCodeWorkerEventType,
  324. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  325. notification_message(
  326. scene_state->notification_app,
  327. get_notification_sequence_automation(plugin_state, scene_state));
  328. return true;
  329. }
  330. #endif
  331. } else if(event->input.type == InputTypePress || event->input.type == InputTypeRepeat) {
  332. switch(event->input.key) {
  333. case InputKeyUp:
  334. break;
  335. case InputKeyDown:
  336. break;
  337. case InputKeyRight: {
  338. const TokenInfoIteratorContext* iterator_context =
  339. totp_config_get_token_iterator_context(plugin_state);
  340. size_t current_token_index =
  341. totp_token_info_iterator_get_current_token_index(iterator_context);
  342. totp_roll_value_size_t(
  343. &current_token_index,
  344. 1,
  345. 0,
  346. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  347. RollOverflowBehaviorRoll);
  348. update_totp_params(plugin_state, current_token_index);
  349. break;
  350. }
  351. case InputKeyLeft: {
  352. const TokenInfoIteratorContext* iterator_context =
  353. totp_config_get_token_iterator_context(plugin_state);
  354. size_t current_token_index =
  355. totp_token_info_iterator_get_current_token_index(iterator_context);
  356. totp_roll_value_size_t(
  357. &current_token_index,
  358. -1,
  359. 0,
  360. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  361. RollOverflowBehaviorRoll);
  362. update_totp_params(plugin_state, current_token_index);
  363. break;
  364. }
  365. case InputKeyOk:
  366. break;
  367. case InputKeyBack:
  368. break;
  369. default:
  370. break;
  371. }
  372. } else if(event->input.type == InputTypeRelease && event->input.key == InputKeyOk) {
  373. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  374. }
  375. return true;
  376. }
  377. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  378. if(plugin_state->current_scene_state == NULL) return;
  379. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  380. totp_generate_code_worker_stop(scene_state->generate_code_worker_context);
  381. furi_record_close(RECORD_NOTIFICATION);
  382. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  383. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  384. }
  385. #ifdef TOTP_BADBT_TYPE_ENABLED
  386. if(plugin_state->automation_method & AutomationMethodBadBt) {
  387. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  388. }
  389. #endif
  390. if(scene_state->notification_sequence_new_token != NULL) {
  391. free(scene_state->notification_sequence_new_token);
  392. }
  393. if(scene_state->notification_sequence_automation != NULL) {
  394. free(scene_state->notification_sequence_automation);
  395. }
  396. furi_mutex_free(scene_state->last_code_update_sync);
  397. free(scene_state);
  398. plugin_state->current_scene_state = NULL;
  399. }