totp_scene_generate_token.c 18 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/active_font.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. 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. } SceneState;
  39. static const NotificationSequence*
  40. get_notification_sequence_new_token(const PluginState* plugin_state, SceneState* scene_state) {
  41. if(scene_state->notification_sequence_new_token == NULL) {
  42. uint8_t i = 0;
  43. uint8_t length = 4;
  44. if(plugin_state->notification_method & NotificationMethodVibro) {
  45. length += 2;
  46. }
  47. if(plugin_state->notification_method & NotificationMethodSound) {
  48. length += 2;
  49. }
  50. scene_state->notification_sequence_new_token = malloc(sizeof(void*) * length);
  51. furi_check(scene_state->notification_sequence_new_token != NULL);
  52. scene_state->notification_sequence_new_token[i++] = &message_display_backlight_on;
  53. scene_state->notification_sequence_new_token[i++] = &message_green_255;
  54. if(plugin_state->notification_method & NotificationMethodVibro) {
  55. scene_state->notification_sequence_new_token[i++] = &message_vibro_on;
  56. }
  57. if(plugin_state->notification_method & NotificationMethodSound) {
  58. scene_state->notification_sequence_new_token[i++] = &message_note_c5;
  59. }
  60. scene_state->notification_sequence_new_token[i++] = &message_delay_50;
  61. if(plugin_state->notification_method & NotificationMethodVibro) {
  62. scene_state->notification_sequence_new_token[i++] = &message_vibro_off;
  63. }
  64. if(plugin_state->notification_method & NotificationMethodSound) {
  65. scene_state->notification_sequence_new_token[i++] = &message_sound_off;
  66. }
  67. scene_state->notification_sequence_new_token[i++] = NULL;
  68. }
  69. return (NotificationSequence*)scene_state->notification_sequence_new_token;
  70. }
  71. static const NotificationSequence*
  72. get_notification_sequence_automation(const PluginState* plugin_state, SceneState* scene_state) {
  73. if(scene_state->notification_sequence_automation == NULL) {
  74. uint8_t i = 0;
  75. uint8_t length = 3;
  76. if(plugin_state->notification_method & NotificationMethodVibro) {
  77. length += 2;
  78. }
  79. if(plugin_state->notification_method & NotificationMethodSound) {
  80. length += 6;
  81. }
  82. scene_state->notification_sequence_automation = malloc(sizeof(void*) * length);
  83. furi_check(scene_state->notification_sequence_automation != NULL);
  84. scene_state->notification_sequence_automation[i++] = &message_blue_255;
  85. if(plugin_state->notification_method & NotificationMethodVibro) {
  86. scene_state->notification_sequence_automation[i++] = &message_vibro_on;
  87. }
  88. if(plugin_state->notification_method & NotificationMethodSound) {
  89. scene_state->notification_sequence_automation[i++] = &message_note_d5; //-V525
  90. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  91. scene_state->notification_sequence_automation[i++] = &message_note_e4;
  92. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  93. scene_state->notification_sequence_automation[i++] = &message_note_f3;
  94. }
  95. scene_state->notification_sequence_automation[i++] = &message_delay_50;
  96. if(plugin_state->notification_method & NotificationMethodVibro) {
  97. scene_state->notification_sequence_automation[i++] = &message_vibro_off;
  98. }
  99. if(plugin_state->notification_method & NotificationMethodSound) {
  100. scene_state->notification_sequence_automation[i++] = &message_sound_off;
  101. }
  102. scene_state->notification_sequence_automation[i++] = NULL;
  103. }
  104. return (NotificationSequence*)scene_state->notification_sequence_automation;
  105. }
  106. static void update_totp_params(PluginState* const plugin_state, size_t token_index) {
  107. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  108. TokenInfoIteratorContext* iterator_context =
  109. totp_config_get_token_iterator_context(plugin_state);
  110. if(totp_token_info_iterator_go_to(iterator_context, token_index)) {
  111. totp_generate_code_worker_notify(
  112. scene_state->generate_code_worker_context, TotpGenerateCodeWorkerEventForceUpdate);
  113. }
  114. }
  115. static void draw_totp_code(Canvas* const canvas, const PluginState* const plugin_state) {
  116. const SceneState* scene_state = plugin_state->current_scene_state;
  117. const TokenInfoIteratorContext* iterator_context =
  118. totp_config_get_token_iterator_context(plugin_state);
  119. uint8_t code_length = totp_token_info_iterator_get_current_token(iterator_context)->digits;
  120. uint8_t offset_x = scene_state->ui_precalculated_dimensions.code_offset_x;
  121. uint8_t char_width = TOTP_CODE_FONT_INFO.charInfo[0].width;
  122. uint8_t offset_x_inc = scene_state->ui_precalculated_dimensions.code_offset_x_inc;
  123. for(uint8_t i = 0; i < code_length; i++) {
  124. char ch = scene_state->last_code[i];
  125. if(ch >= TOTP_CODE_FONT_INFO.startChar && ch <= TOTP_CODE_FONT_INFO.endChar) {
  126. uint8_t char_index = ch - TOTP_CODE_FONT_INFO.startChar;
  127. canvas_draw_xbm(
  128. canvas,
  129. offset_x,
  130. scene_state->ui_precalculated_dimensions.code_offset_y,
  131. char_width,
  132. TOTP_CODE_FONT_INFO.height,
  133. &TOTP_CODE_FONT_INFO.data[TOTP_CODE_FONT_INFO.charInfo[char_index].offset]);
  134. }
  135. offset_x += offset_x_inc;
  136. }
  137. }
  138. static void on_new_token_code_generated(bool time_left, void* context) {
  139. const PluginState* plugin_state = context;
  140. const TokenInfoIteratorContext* iterator_context =
  141. totp_config_get_token_iterator_context(plugin_state);
  142. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  143. return;
  144. }
  145. SceneState* scene_state = plugin_state->current_scene_state;
  146. const TokenInfo* current_token = totp_token_info_iterator_get_current_token(iterator_context);
  147. uint8_t char_width = TOTP_CODE_FONT_INFO.charInfo[0].width;
  148. scene_state->ui_precalculated_dimensions.code_total_length =
  149. current_token->digits * (char_width + TOTP_CODE_FONT_INFO.spacePixels);
  150. scene_state->ui_precalculated_dimensions.code_offset_x =
  151. (SCREEN_WIDTH - scene_state->ui_precalculated_dimensions.code_total_length) >> 1;
  152. scene_state->ui_precalculated_dimensions.code_offset_x_inc =
  153. char_width + TOTP_CODE_FONT_INFO.spacePixels;
  154. scene_state->ui_precalculated_dimensions.code_offset_y =
  155. SCREEN_HEIGHT_CENTER - (TOTP_CODE_FONT_INFO.height >> 1);
  156. if(time_left) {
  157. notification_message(
  158. plugin_state->notification_app,
  159. get_notification_sequence_new_token(plugin_state, plugin_state->current_scene_state));
  160. }
  161. }
  162. static void on_code_lifetime_updated_generated(float code_lifetime_percent, void* context) {
  163. SceneState* scene_state = context;
  164. scene_state->ui_precalculated_dimensions.progress_bar_width =
  165. (uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * code_lifetime_percent);
  166. scene_state->ui_precalculated_dimensions.progress_bar_x =
  167. ((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) -
  168. scene_state->ui_precalculated_dimensions.progress_bar_width) >>
  169. 1) +
  170. PROGRESS_BAR_MARGIN;
  171. }
  172. void totp_scene_generate_token_activate(PluginState* plugin_state) {
  173. SceneState* scene_state = malloc(sizeof(SceneState));
  174. furi_check(scene_state != NULL);
  175. plugin_state->current_scene_state = scene_state;
  176. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  177. scene_state->last_code_update_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  178. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  179. scene_state->usb_type_code_worker_context = totp_usb_type_code_worker_start(
  180. scene_state->last_code, TokenDigitsCountMax + 1, scene_state->last_code_update_sync);
  181. }
  182. #ifdef TOTP_BADBT_TYPE_ENABLED
  183. if(plugin_state->automation_method & AutomationMethodBadBt) {
  184. if(plugin_state->bt_type_code_worker_context == NULL) {
  185. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  186. }
  187. totp_bt_type_code_worker_start(
  188. plugin_state->bt_type_code_worker_context,
  189. scene_state->last_code,
  190. TokenDigitsCountMax + 1,
  191. scene_state->last_code_update_sync);
  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. totp_generate_code_worker_set_code_generated_handler(
  203. scene_state->generate_code_worker_context, &on_new_token_code_generated, plugin_state);
  204. totp_generate_code_worker_set_lifetime_changed_handler(
  205. scene_state->generate_code_worker_context,
  206. &on_code_lifetime_updated_generated,
  207. scene_state);
  208. update_totp_params(
  209. plugin_state, totp_token_info_iterator_get_current_token_index(iterator_context));
  210. }
  211. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  212. const TokenInfoIteratorContext* iterator_context =
  213. totp_config_get_token_iterator_context(plugin_state);
  214. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  215. canvas_draw_str_aligned(
  216. canvas,
  217. SCREEN_WIDTH_CENTER,
  218. SCREEN_HEIGHT_CENTER - 10,
  219. AlignCenter,
  220. AlignCenter,
  221. "Token list is empty");
  222. canvas_draw_str_aligned(
  223. canvas,
  224. SCREEN_WIDTH_CENTER,
  225. SCREEN_HEIGHT_CENTER + 10,
  226. AlignCenter,
  227. AlignCenter,
  228. "Press OK button to add");
  229. return;
  230. }
  231. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  232. canvas_set_font(canvas, FontPrimary);
  233. const char* token_name_cstr =
  234. furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name);
  235. uint16_t token_name_width = canvas_string_width(canvas, token_name_cstr);
  236. if(SCREEN_WIDTH - token_name_width > 18) {
  237. canvas_draw_str_aligned(
  238. canvas,
  239. SCREEN_WIDTH_CENTER,
  240. SCREEN_HEIGHT_CENTER - 20,
  241. AlignCenter,
  242. AlignCenter,
  243. token_name_cstr);
  244. } else {
  245. canvas_draw_str_aligned(
  246. canvas, 9, SCREEN_HEIGHT_CENTER - 20, AlignLeft, AlignCenter, token_name_cstr);
  247. canvas_set_color(canvas, ColorWhite);
  248. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  249. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  250. canvas_set_color(canvas, ColorBlack);
  251. }
  252. draw_totp_code(canvas, plugin_state);
  253. canvas_draw_box(
  254. canvas,
  255. scene_state->ui_precalculated_dimensions.progress_bar_x,
  256. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  257. scene_state->ui_precalculated_dimensions.progress_bar_width,
  258. PROGRESS_BAR_HEIGHT);
  259. if(totp_token_info_iterator_get_total_count(iterator_context) > 1) {
  260. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  261. canvas_draw_icon(
  262. canvas, SCREEN_WIDTH - 8, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  263. }
  264. #ifdef TOTP_AUTOMATION_ICONS_ENABLED
  265. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  266. canvas_draw_icon(
  267. canvas,
  268. #ifdef TOTP_BADBT_TYPE_ENABLED
  269. SCREEN_WIDTH_CENTER -
  270. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  271. #else
  272. SCREEN_WIDTH_CENTER - 15,
  273. #endif
  274. SCREEN_HEIGHT_CENTER + 12,
  275. &I_hid_usb_31x9);
  276. }
  277. #ifdef TOTP_BADBT_TYPE_ENABLED
  278. if(plugin_state->automation_method & AutomationMethodBadBt &&
  279. plugin_state->bt_type_code_worker_context != NULL &&
  280. totp_bt_type_code_worker_is_advertising(plugin_state->bt_type_code_worker_context)) {
  281. canvas_draw_icon(
  282. canvas,
  283. SCREEN_WIDTH_CENTER +
  284. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  285. SCREEN_HEIGHT_CENTER + 12,
  286. &I_hid_ble_31x9);
  287. }
  288. #endif
  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. plugin_state->notification_app,
  313. get_notification_sequence_automation(plugin_state, scene_state));
  314. return true;
  315. }
  316. #ifdef TOTP_BADBT_TYPE_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. plugin_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. 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. }