totp_scene_generate_token.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "totp_scene_generate_token.h"
  7. #include "../../../types/token_info.h"
  8. #include "../../../types/common.h"
  9. #include "../../constants.h"
  10. #include "../../../services/config/config.h"
  11. #include "../../scene_director.h"
  12. #include "../token_menu/totp_scene_token_menu.h"
  13. #include "../../../features_config.h"
  14. #include "../../../workers/generate_totp_code/generate_totp_code.h"
  15. #include "../../../workers/usb_type_code/usb_type_code.h"
  16. #ifdef TOTP_BADBT_TYPE_ENABLED
  17. #include "../../../workers/bt_type_code/bt_type_code.h"
  18. #endif
  19. #include "../../fonts/mode-nine/mode-nine.h"
  20. #define PROGRESS_BAR_MARGIN (3)
  21. #define PROGRESS_BAR_HEIGHT (4)
  22. typedef struct {
  23. uint8_t progress_bar_x;
  24. uint8_t progress_bar_width;
  25. uint8_t code_total_length;
  26. uint8_t code_offset_x;
  27. uint8_t code_offset_x_inc;
  28. uint8_t code_offset_y;
  29. } UiPrecalculatedDimensions;
  30. typedef struct {
  31. uint16_t current_token_index;
  32. char last_code[TOTP_TOKEN_DIGITS_MAX_COUNT + 1];
  33. TokenInfo* current_token;
  34. TotpUsbTypeCodeWorkerContext* usb_type_code_worker_context;
  35. NotificationMessage const** notification_sequence_new_token;
  36. NotificationMessage const** notification_sequence_automation;
  37. FuriMutex* last_code_update_sync;
  38. TotpGenerateCodeWorkerContext* generate_code_worker_context;
  39. UiPrecalculatedDimensions ui_precalculated_dimensions;
  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) {
  109. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  110. if(scene_state->current_token_index < plugin_state->tokens_count) {
  111. scene_state->current_token =
  112. list_element_at(plugin_state->tokens_list, scene_state->current_token_index)->data;
  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 SceneState* const scene_state) {
  118. uint8_t code_length = scene_state->current_token->digits;
  119. uint8_t offset_x = scene_state->ui_precalculated_dimensions.code_offset_x;
  120. uint8_t char_width = modeNine_15ptFontInfo.charInfo[0].width;
  121. uint8_t offset_x_inc = scene_state->ui_precalculated_dimensions.code_offset_x_inc;
  122. for(uint8_t i = 0; i < code_length; i++) {
  123. char ch = scene_state->last_code[i];
  124. if(ch >= modeNine_15ptFontInfo.startChar && ch <= modeNine_15ptFontInfo.endChar) {
  125. uint8_t char_index = ch - modeNine_15ptFontInfo.startChar;
  126. canvas_draw_xbm(
  127. canvas,
  128. offset_x,
  129. scene_state->ui_precalculated_dimensions.code_offset_y,
  130. char_width,
  131. modeNine_15ptFontInfo.height,
  132. &modeNine_15ptFontInfo.data[modeNine_15ptFontInfo.charInfo[char_index].offset]);
  133. }
  134. offset_x += offset_x_inc;
  135. }
  136. }
  137. static void on_new_token_code_generated(bool time_left, void* context) {
  138. const PluginState* plugin_state = context;
  139. SceneState* scene_state = plugin_state->current_scene_state;
  140. uint8_t char_width = modeNine_15ptFontInfo.charInfo[0].width;
  141. scene_state->ui_precalculated_dimensions.code_total_length =
  142. scene_state->current_token->digits * (char_width + modeNine_15ptFontInfo.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_x_inc =
  146. char_width + modeNine_15ptFontInfo.spacePixels;
  147. scene_state->ui_precalculated_dimensions.code_offset_y =
  148. SCREEN_HEIGHT_CENTER - (modeNine_15ptFontInfo.height >> 1);
  149. if(time_left) {
  150. notification_message(
  151. plugin_state->notification_app,
  152. get_notification_sequence_new_token(plugin_state, plugin_state->current_scene_state));
  153. }
  154. }
  155. static void on_code_lifetime_updated_generated(float code_lifetime_percent, void* context) {
  156. SceneState* scene_state = context;
  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. }
  165. void totp_scene_generate_token_activate(
  166. PluginState* plugin_state,
  167. const GenerateTokenSceneContext* context) {
  168. if(!plugin_state->token_list_loaded) {
  169. TokenLoadingResult token_load_result = totp_config_file_load_tokens(plugin_state);
  170. if(token_load_result != TokenLoadingResultSuccess) {
  171. DialogMessage* message = dialog_message_alloc();
  172. dialog_message_set_buttons(message, NULL, "Okay", NULL);
  173. if(token_load_result == TokenLoadingResultWarning) {
  174. dialog_message_set_text(
  175. message,
  176. "Unable to load some tokens\nPlease review conf file",
  177. SCREEN_WIDTH_CENTER,
  178. SCREEN_HEIGHT_CENTER,
  179. AlignCenter,
  180. AlignCenter);
  181. } else if(token_load_result == TokenLoadingResultError) {
  182. dialog_message_set_text(
  183. message,
  184. "Unable to load tokens\nPlease review conf file",
  185. SCREEN_WIDTH_CENTER,
  186. SCREEN_HEIGHT_CENTER,
  187. AlignCenter,
  188. AlignCenter);
  189. }
  190. dialog_message_show(plugin_state->dialogs_app, message);
  191. dialog_message_free(message);
  192. }
  193. }
  194. SceneState* scene_state = malloc(sizeof(SceneState));
  195. furi_check(scene_state != NULL);
  196. if(context == NULL || context->current_token_index > plugin_state->tokens_count) {
  197. scene_state->current_token_index = 0;
  198. } else {
  199. scene_state->current_token_index = context->current_token_index;
  200. }
  201. plugin_state->current_scene_state = scene_state;
  202. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  203. scene_state->last_code_update_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  204. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  205. scene_state->usb_type_code_worker_context = totp_usb_type_code_worker_start(
  206. scene_state->last_code,
  207. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  208. scene_state->last_code_update_sync);
  209. }
  210. #ifdef TOTP_BADBT_TYPE_ENABLED
  211. if(plugin_state->automation_method & AutomationMethodBadBt) {
  212. if(plugin_state->bt_type_code_worker_context == NULL) {
  213. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  214. }
  215. totp_bt_type_code_worker_start(
  216. plugin_state->bt_type_code_worker_context,
  217. scene_state->last_code,
  218. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  219. scene_state->last_code_update_sync);
  220. }
  221. #endif
  222. scene_state->generate_code_worker_context = totp_generate_code_worker_start(
  223. scene_state->last_code,
  224. &scene_state->current_token,
  225. scene_state->last_code_update_sync,
  226. plugin_state->timezone_offset,
  227. plugin_state->iv);
  228. totp_generate_code_worker_set_code_generated_handler(
  229. scene_state->generate_code_worker_context, &on_new_token_code_generated, plugin_state);
  230. totp_generate_code_worker_set_lifetime_changed_handler(
  231. scene_state->generate_code_worker_context,
  232. &on_code_lifetime_updated_generated,
  233. scene_state);
  234. update_totp_params(plugin_state);
  235. }
  236. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  237. if(plugin_state->tokens_count == 0) {
  238. canvas_draw_str_aligned(
  239. canvas,
  240. SCREEN_WIDTH_CENTER,
  241. SCREEN_HEIGHT_CENTER - 10,
  242. AlignCenter,
  243. AlignCenter,
  244. "Token list is empty");
  245. canvas_draw_str_aligned(
  246. canvas,
  247. SCREEN_WIDTH_CENTER,
  248. SCREEN_HEIGHT_CENTER + 10,
  249. AlignCenter,
  250. AlignCenter,
  251. "Press OK button to add");
  252. return;
  253. }
  254. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  255. canvas_set_font(canvas, FontPrimary);
  256. uint16_t token_name_width = canvas_string_width(canvas, scene_state->current_token->name);
  257. if(SCREEN_WIDTH - token_name_width > 18) {
  258. canvas_draw_str_aligned(
  259. canvas,
  260. SCREEN_WIDTH_CENTER,
  261. SCREEN_HEIGHT_CENTER - 20,
  262. AlignCenter,
  263. AlignCenter,
  264. scene_state->current_token->name);
  265. } else {
  266. canvas_draw_str_aligned(
  267. canvas,
  268. 9,
  269. SCREEN_HEIGHT_CENTER - 20,
  270. AlignLeft,
  271. AlignCenter,
  272. scene_state->current_token->name);
  273. canvas_set_color(canvas, ColorWhite);
  274. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  275. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  276. canvas_set_color(canvas, ColorBlack);
  277. }
  278. draw_totp_code(canvas, scene_state);
  279. canvas_draw_box(
  280. canvas,
  281. scene_state->ui_precalculated_dimensions.progress_bar_x,
  282. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  283. scene_state->ui_precalculated_dimensions.progress_bar_width,
  284. PROGRESS_BAR_HEIGHT);
  285. if(plugin_state->tokens_count > 1) {
  286. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  287. canvas_draw_icon(
  288. canvas, SCREEN_WIDTH - 9, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  289. }
  290. #ifdef TOTP_AUTOMATION_ICONS_ENABLED
  291. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  292. canvas_draw_icon(
  293. canvas,
  294. #ifdef TOTP_BADBT_TYPE_ENABLED
  295. SCREEN_WIDTH_CENTER -
  296. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  297. #else
  298. SCREEN_WIDTH_CENTER - 15,
  299. #endif
  300. SCREEN_HEIGHT_CENTER + 12,
  301. &I_hid_usb_31x9);
  302. }
  303. #ifdef TOTP_BADBT_TYPE_ENABLED
  304. if(plugin_state->automation_method & AutomationMethodBadBt &&
  305. plugin_state->bt_type_code_worker_context != NULL &&
  306. plugin_state->bt_type_code_worker_context->is_advertising) {
  307. canvas_draw_icon(
  308. canvas,
  309. SCREEN_WIDTH_CENTER +
  310. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  311. SCREEN_HEIGHT_CENTER + 12,
  312. &I_hid_ble_31x9);
  313. }
  314. #endif
  315. #endif
  316. }
  317. bool totp_scene_generate_token_handle_event(
  318. const PluginEvent* const event,
  319. PluginState* plugin_state) {
  320. if(event->type != EventTypeKey) {
  321. return true;
  322. }
  323. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  324. return false;
  325. }
  326. SceneState* scene_state;
  327. if(event->input.type == InputTypeLong) {
  328. if(event->input.key == InputKeyDown &&
  329. plugin_state->automation_method & AutomationMethodBadUsb) {
  330. scene_state = (SceneState*)plugin_state->current_scene_state;
  331. totp_usb_type_code_worker_notify(
  332. scene_state->usb_type_code_worker_context,
  333. TotpUsbTypeCodeWorkerEventType,
  334. scene_state->current_token->automation_features);
  335. notification_message(
  336. plugin_state->notification_app,
  337. get_notification_sequence_automation(plugin_state, scene_state));
  338. return true;
  339. }
  340. #ifdef TOTP_BADBT_TYPE_ENABLED
  341. else if(
  342. event->input.key == InputKeyUp &&
  343. plugin_state->automation_method & AutomationMethodBadBt) {
  344. scene_state = (SceneState*)plugin_state->current_scene_state;
  345. totp_bt_type_code_worker_notify(
  346. plugin_state->bt_type_code_worker_context,
  347. TotpBtTypeCodeWorkerEventType,
  348. scene_state->current_token->automation_features);
  349. notification_message(
  350. plugin_state->notification_app,
  351. get_notification_sequence_automation(plugin_state, scene_state));
  352. return true;
  353. }
  354. #endif
  355. }
  356. if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
  357. return true;
  358. }
  359. scene_state = (SceneState*)plugin_state->current_scene_state;
  360. switch(event->input.key) {
  361. case InputKeyUp:
  362. break;
  363. case InputKeyDown:
  364. break;
  365. case InputKeyRight:
  366. totp_roll_value_uint16_t(
  367. &scene_state->current_token_index,
  368. 1,
  369. 0,
  370. plugin_state->tokens_count - 1,
  371. RollOverflowBehaviorRoll);
  372. update_totp_params(plugin_state);
  373. break;
  374. case InputKeyLeft:
  375. totp_roll_value_uint16_t(
  376. &scene_state->current_token_index,
  377. -1,
  378. 0,
  379. plugin_state->tokens_count - 1,
  380. RollOverflowBehaviorRoll);
  381. update_totp_params(plugin_state);
  382. break;
  383. case InputKeyOk:
  384. if(plugin_state->tokens_count == 0) {
  385. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  386. } else {
  387. TokenMenuSceneContext ctx = {.current_token_index = scene_state->current_token_index};
  388. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, &ctx);
  389. }
  390. break;
  391. case InputKeyBack:
  392. break;
  393. default:
  394. break;
  395. }
  396. return true;
  397. }
  398. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  399. if(plugin_state->current_scene_state == NULL) return;
  400. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  401. totp_generate_code_worker_stop(scene_state->generate_code_worker_context);
  402. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  403. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  404. }
  405. #ifdef TOTP_BADBT_TYPE_ENABLED
  406. if(plugin_state->automation_method & AutomationMethodBadBt) {
  407. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  408. }
  409. #endif
  410. if(scene_state->notification_sequence_new_token != NULL) {
  411. free(scene_state->notification_sequence_new_token);
  412. }
  413. if(scene_state->notification_sequence_automation != NULL) {
  414. free(scene_state->notification_sequence_automation);
  415. }
  416. furi_mutex_free(scene_state->last_code_update_sync);
  417. free(scene_state);
  418. plugin_state->current_scene_state = NULL;
  419. }