totp_scene_generate_token.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. uint16_t current_token_index;
  24. char last_code[TOTP_TOKEN_DIGITS_MAX_COUNT + 1];
  25. TokenInfo* current_token;
  26. TotpUsbTypeCodeWorkerContext* usb_type_code_worker_context;
  27. NotificationMessage const** notification_sequence_new_token;
  28. NotificationMessage const** notification_sequence_automation;
  29. FuriMutex* last_code_update_sync;
  30. TotpGenerateCodeWorkerContext* generate_code_worker_context;
  31. uint8_t progress_bar_x;
  32. uint8_t progress_bar_width;
  33. } SceneState;
  34. static const NotificationSequence*
  35. get_notification_sequence_new_token(const PluginState* plugin_state, SceneState* scene_state) {
  36. if(scene_state->notification_sequence_new_token == NULL) {
  37. uint8_t i = 0;
  38. uint8_t length = 4;
  39. if(plugin_state->notification_method & NotificationMethodVibro) {
  40. length += 2;
  41. }
  42. if(plugin_state->notification_method & NotificationMethodSound) {
  43. length += 2;
  44. }
  45. scene_state->notification_sequence_new_token = malloc(sizeof(void*) * length);
  46. furi_check(scene_state->notification_sequence_new_token != NULL);
  47. scene_state->notification_sequence_new_token[i++] = &message_display_backlight_on;
  48. scene_state->notification_sequence_new_token[i++] = &message_green_255;
  49. if(plugin_state->notification_method & NotificationMethodVibro) {
  50. scene_state->notification_sequence_new_token[i++] = &message_vibro_on;
  51. }
  52. if(plugin_state->notification_method & NotificationMethodSound) {
  53. scene_state->notification_sequence_new_token[i++] = &message_note_c5;
  54. }
  55. scene_state->notification_sequence_new_token[i++] = &message_delay_50;
  56. if(plugin_state->notification_method & NotificationMethodVibro) {
  57. scene_state->notification_sequence_new_token[i++] = &message_vibro_off;
  58. }
  59. if(plugin_state->notification_method & NotificationMethodSound) {
  60. scene_state->notification_sequence_new_token[i++] = &message_sound_off;
  61. }
  62. scene_state->notification_sequence_new_token[i++] = NULL;
  63. }
  64. return (NotificationSequence*)scene_state->notification_sequence_new_token;
  65. }
  66. static const NotificationSequence*
  67. get_notification_sequence_automation(const PluginState* plugin_state, SceneState* scene_state) {
  68. if(scene_state->notification_sequence_automation == NULL) {
  69. uint8_t i = 0;
  70. uint8_t length = 3;
  71. if(plugin_state->notification_method & NotificationMethodVibro) {
  72. length += 2;
  73. }
  74. if(plugin_state->notification_method & NotificationMethodSound) {
  75. length += 6;
  76. }
  77. scene_state->notification_sequence_automation = malloc(sizeof(void*) * length);
  78. furi_check(scene_state->notification_sequence_automation != NULL);
  79. scene_state->notification_sequence_automation[i++] = &message_blue_255;
  80. if(plugin_state->notification_method & NotificationMethodVibro) {
  81. scene_state->notification_sequence_automation[i++] = &message_vibro_on;
  82. }
  83. if(plugin_state->notification_method & NotificationMethodSound) {
  84. scene_state->notification_sequence_automation[i++] = &message_note_d5; //-V525
  85. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  86. scene_state->notification_sequence_automation[i++] = &message_note_e4;
  87. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  88. scene_state->notification_sequence_automation[i++] = &message_note_f3;
  89. }
  90. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  91. if(plugin_state->notification_method & NotificationMethodVibro) {
  92. scene_state->notification_sequence_automation[i++] = &message_vibro_off;
  93. }
  94. if(plugin_state->notification_method & NotificationMethodSound) {
  95. scene_state->notification_sequence_automation[i++] = &message_sound_off;
  96. }
  97. scene_state->notification_sequence_automation[i++] = NULL;
  98. }
  99. return (NotificationSequence*)scene_state->notification_sequence_automation;
  100. }
  101. static void update_totp_params(PluginState* const plugin_state) {
  102. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  103. if(scene_state->current_token_index < plugin_state->tokens_count) {
  104. scene_state->current_token =
  105. list_element_at(plugin_state->tokens_list, scene_state->current_token_index)->data;
  106. totp_generate_code_worker_notify(
  107. scene_state->generate_code_worker_context, TotpGenerateCodeWorkerEventForceUpdate);
  108. }
  109. }
  110. static void draw_totp_code(Canvas* const canvas, const SceneState* const scene_state) {
  111. uint8_t code_length = scene_state->current_token->digits;
  112. uint8_t char_width = modeNine_15ptFontInfo.charInfo[0].width;
  113. uint8_t total_length = code_length * (char_width + modeNine_15ptFontInfo.spacePixels);
  114. uint8_t offset_x = (SCREEN_WIDTH - total_length) >> 1;
  115. uint8_t offset_x_inc = char_width + modeNine_15ptFontInfo.spacePixels;
  116. uint8_t offset_y = SCREEN_HEIGHT_CENTER - (modeNine_15ptFontInfo.height >> 1);
  117. for(uint8_t i = 0; i < code_length; i++) {
  118. char ch = scene_state->last_code[i];
  119. uint8_t char_index = ch - modeNine_15ptFontInfo.startChar;
  120. canvas_draw_xbm(
  121. canvas,
  122. offset_x,
  123. offset_y,
  124. char_width,
  125. modeNine_15ptFontInfo.height,
  126. &modeNine_15ptFontInfo.data[modeNine_15ptFontInfo.charInfo[char_index].offset]);
  127. offset_x += offset_x_inc;
  128. }
  129. }
  130. static void on_new_token_code_generated(bool time_left, void* context) {
  131. if(time_left) {
  132. PluginState* plugin_state = context;
  133. notification_message(
  134. plugin_state->notification_app,
  135. get_notification_sequence_new_token(plugin_state, plugin_state->current_scene_state));
  136. }
  137. }
  138. static void on_code_lifetime_updated_generated(float code_lifetime_percent, void* context) {
  139. SceneState* scene_state = context;
  140. scene_state->progress_bar_width =
  141. (uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * code_lifetime_percent);
  142. scene_state->progress_bar_x =
  143. ((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) - scene_state->progress_bar_width) >> 1) +
  144. PROGRESS_BAR_MARGIN;
  145. }
  146. void totp_scene_generate_token_activate(
  147. PluginState* plugin_state,
  148. const GenerateTokenSceneContext* context) {
  149. if(!plugin_state->token_list_loaded) {
  150. TokenLoadingResult token_load_result = totp_config_file_load_tokens(plugin_state);
  151. if(token_load_result != TokenLoadingResultSuccess) {
  152. DialogMessage* message = dialog_message_alloc();
  153. dialog_message_set_buttons(message, NULL, "Okay", NULL);
  154. if(token_load_result == TokenLoadingResultWarning) {
  155. dialog_message_set_text(
  156. message,
  157. "Unable to load some tokens\nPlease review conf file",
  158. SCREEN_WIDTH_CENTER,
  159. SCREEN_HEIGHT_CENTER,
  160. AlignCenter,
  161. AlignCenter);
  162. } else if(token_load_result == TokenLoadingResultError) {
  163. dialog_message_set_text(
  164. message,
  165. "Unable to load tokens\nPlease review conf file",
  166. SCREEN_WIDTH_CENTER,
  167. SCREEN_HEIGHT_CENTER,
  168. AlignCenter,
  169. AlignCenter);
  170. }
  171. dialog_message_show(plugin_state->dialogs_app, message);
  172. dialog_message_free(message);
  173. }
  174. }
  175. SceneState* scene_state = malloc(sizeof(SceneState));
  176. furi_check(scene_state != NULL);
  177. if(context == NULL || context->current_token_index > plugin_state->tokens_count) {
  178. scene_state->current_token_index = 0;
  179. } else {
  180. scene_state->current_token_index = context->current_token_index;
  181. }
  182. plugin_state->current_scene_state = scene_state;
  183. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  184. scene_state->last_code_update_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  185. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  186. scene_state->usb_type_code_worker_context = totp_usb_type_code_worker_start(
  187. scene_state->last_code,
  188. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  189. scene_state->last_code_update_sync);
  190. }
  191. #ifdef TOTP_BADBT_TYPE_ENABLED
  192. if(plugin_state->automation_method & AutomationMethodBadBt) {
  193. if(plugin_state->bt_type_code_worker_context == NULL) {
  194. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  195. }
  196. totp_bt_type_code_worker_start(
  197. plugin_state->bt_type_code_worker_context,
  198. scene_state->last_code,
  199. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  200. scene_state->last_code_update_sync);
  201. }
  202. #endif
  203. scene_state->generate_code_worker_context = totp_generate_code_worker_start(
  204. scene_state->last_code,
  205. &scene_state->current_token,
  206. scene_state->last_code_update_sync,
  207. plugin_state->timezone_offset,
  208. plugin_state->iv);
  209. totp_generate_code_worker_set_code_generated_handler(
  210. scene_state->generate_code_worker_context, &on_new_token_code_generated, plugin_state);
  211. totp_generate_code_worker_set_lifetime_changed_handler(
  212. scene_state->generate_code_worker_context,
  213. &on_code_lifetime_updated_generated,
  214. scene_state);
  215. update_totp_params(plugin_state);
  216. }
  217. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  218. if(plugin_state->tokens_count == 0) {
  219. canvas_draw_str_aligned(
  220. canvas,
  221. SCREEN_WIDTH_CENTER,
  222. SCREEN_HEIGHT_CENTER - 10,
  223. AlignCenter,
  224. AlignCenter,
  225. "Token list is empty");
  226. canvas_draw_str_aligned(
  227. canvas,
  228. SCREEN_WIDTH_CENTER,
  229. SCREEN_HEIGHT_CENTER + 10,
  230. AlignCenter,
  231. AlignCenter,
  232. "Press OK button to add");
  233. return;
  234. }
  235. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  236. canvas_set_font(canvas, FontPrimary);
  237. uint16_t token_name_width = canvas_string_width(canvas, scene_state->current_token->name);
  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. scene_state->current_token->name);
  246. } else {
  247. canvas_draw_str_aligned(
  248. canvas,
  249. 9,
  250. SCREEN_HEIGHT_CENTER - 20,
  251. AlignLeft,
  252. AlignCenter,
  253. scene_state->current_token->name);
  254. canvas_set_color(canvas, ColorWhite);
  255. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  256. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  257. canvas_set_color(canvas, ColorBlack);
  258. }
  259. draw_totp_code(canvas, scene_state);
  260. canvas_draw_box(
  261. canvas,
  262. scene_state->progress_bar_x,
  263. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  264. scene_state->progress_bar_width,
  265. PROGRESS_BAR_HEIGHT);
  266. if(plugin_state->tokens_count > 1) {
  267. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  268. canvas_draw_icon(
  269. canvas, SCREEN_WIDTH - 9, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  270. }
  271. #ifdef TOTP_AUTOMATION_ICONS_ENABLED
  272. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  273. canvas_draw_icon(
  274. canvas,
  275. #ifdef TOTP_BADBT_TYPE_ENABLED
  276. SCREEN_WIDTH_CENTER -
  277. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  278. #else
  279. SCREEN_WIDTH_CENTER - 15,
  280. #endif
  281. SCREEN_HEIGHT_CENTER + 12,
  282. &I_hid_usb_31x9);
  283. }
  284. #ifdef TOTP_BADBT_TYPE_ENABLED
  285. if(plugin_state->automation_method & AutomationMethodBadBt &&
  286. plugin_state->bt_type_code_worker_context != NULL &&
  287. plugin_state->bt_type_code_worker_context->is_advertising) {
  288. canvas_draw_icon(
  289. canvas,
  290. SCREEN_WIDTH_CENTER +
  291. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  292. SCREEN_HEIGHT_CENTER + 12,
  293. &I_hid_ble_31x9);
  294. }
  295. #endif
  296. #endif
  297. }
  298. bool totp_scene_generate_token_handle_event(
  299. const PluginEvent* const event,
  300. PluginState* plugin_state) {
  301. if(event->type != EventTypeKey) {
  302. return true;
  303. }
  304. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  305. return false;
  306. }
  307. SceneState* scene_state;
  308. if(event->input.type == InputTypeLong) {
  309. if(event->input.key == InputKeyDown &&
  310. plugin_state->automation_method & AutomationMethodBadUsb) {
  311. scene_state = (SceneState*)plugin_state->current_scene_state;
  312. totp_usb_type_code_worker_notify(
  313. scene_state->usb_type_code_worker_context,
  314. TotpUsbTypeCodeWorkerEventType,
  315. scene_state->current_token->automation_features);
  316. notification_message(
  317. plugin_state->notification_app,
  318. get_notification_sequence_automation(plugin_state, scene_state));
  319. return true;
  320. }
  321. #ifdef TOTP_BADBT_TYPE_ENABLED
  322. else if(
  323. event->input.key == InputKeyUp &&
  324. plugin_state->automation_method & AutomationMethodBadBt) {
  325. scene_state = (SceneState*)plugin_state->current_scene_state;
  326. totp_bt_type_code_worker_notify(
  327. plugin_state->bt_type_code_worker_context,
  328. TotpBtTypeCodeWorkerEventType,
  329. scene_state->current_token->automation_features);
  330. notification_message(
  331. plugin_state->notification_app,
  332. get_notification_sequence_automation(plugin_state, scene_state));
  333. return true;
  334. }
  335. #endif
  336. }
  337. if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
  338. return true;
  339. }
  340. scene_state = (SceneState*)plugin_state->current_scene_state;
  341. switch(event->input.key) {
  342. case InputKeyUp:
  343. break;
  344. case InputKeyDown:
  345. break;
  346. case InputKeyRight:
  347. totp_roll_value_uint16_t(
  348. &scene_state->current_token_index,
  349. 1,
  350. 0,
  351. plugin_state->tokens_count - 1,
  352. RollOverflowBehaviorRoll);
  353. update_totp_params(plugin_state);
  354. break;
  355. case InputKeyLeft:
  356. totp_roll_value_uint16_t(
  357. &scene_state->current_token_index,
  358. -1,
  359. 0,
  360. plugin_state->tokens_count - 1,
  361. RollOverflowBehaviorRoll);
  362. update_totp_params(plugin_state);
  363. break;
  364. case InputKeyOk:
  365. if(plugin_state->tokens_count == 0) {
  366. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  367. } else {
  368. TokenMenuSceneContext ctx = {.current_token_index = scene_state->current_token_index};
  369. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, &ctx);
  370. }
  371. break;
  372. case InputKeyBack:
  373. break;
  374. default:
  375. break;
  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. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  384. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  385. }
  386. #ifdef TOTP_BADBT_TYPE_ENABLED
  387. if(plugin_state->automation_method & AutomationMethodBadBt) {
  388. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  389. }
  390. #endif
  391. if(scene_state->notification_sequence_new_token != NULL) {
  392. free(scene_state->notification_sequence_new_token);
  393. }
  394. if(scene_state->notification_sequence_automation != NULL) {
  395. free(scene_state->notification_sequence_automation);
  396. }
  397. furi_mutex_free(scene_state->last_code_update_sync);
  398. free(scene_state);
  399. plugin_state->current_scene_state = NULL;
  400. }