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 "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[TOTP_TOKEN_DIGITS_MAX_COUNT + 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,
  181. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  182. scene_state->last_code_update_sync);
  183. }
  184. #ifdef TOTP_BADBT_TYPE_ENABLED
  185. if(plugin_state->automation_method & AutomationMethodBadBt) {
  186. if(plugin_state->bt_type_code_worker_context == NULL) {
  187. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  188. }
  189. totp_bt_type_code_worker_start(
  190. plugin_state->bt_type_code_worker_context,
  191. scene_state->last_code,
  192. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  193. scene_state->last_code_update_sync);
  194. }
  195. #endif
  196. const TokenInfoIteratorContext* iterator_context =
  197. totp_config_get_token_iterator_context(plugin_state);
  198. scene_state->generate_code_worker_context = totp_generate_code_worker_start(
  199. scene_state->last_code,
  200. totp_token_info_iterator_get_current_token(iterator_context),
  201. scene_state->last_code_update_sync,
  202. plugin_state->timezone_offset,
  203. plugin_state->iv);
  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. scene_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. #ifdef TOTP_AUTOMATION_ICONS_ENABLED
  267. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  268. canvas_draw_icon(
  269. canvas,
  270. #ifdef TOTP_BADBT_TYPE_ENABLED
  271. SCREEN_WIDTH_CENTER -
  272. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  273. #else
  274. SCREEN_WIDTH_CENTER - 15,
  275. #endif
  276. SCREEN_HEIGHT_CENTER + 12,
  277. &I_hid_usb_31x9);
  278. }
  279. #ifdef TOTP_BADBT_TYPE_ENABLED
  280. if(plugin_state->automation_method & AutomationMethodBadBt &&
  281. plugin_state->bt_type_code_worker_context != NULL &&
  282. totp_bt_type_code_worker_is_advertising(plugin_state->bt_type_code_worker_context)) {
  283. canvas_draw_icon(
  284. canvas,
  285. SCREEN_WIDTH_CENTER +
  286. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  287. SCREEN_HEIGHT_CENTER + 12,
  288. &I_hid_ble_31x9);
  289. }
  290. #endif
  291. #endif
  292. }
  293. bool totp_scene_generate_token_handle_event(
  294. const PluginEvent* const event,
  295. PluginState* plugin_state) {
  296. if(event->type != EventTypeKey) {
  297. return true;
  298. }
  299. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  300. return false;
  301. }
  302. SceneState* scene_state;
  303. if(event->input.type == InputTypeLong) {
  304. if(event->input.key == InputKeyDown &&
  305. plugin_state->automation_method & AutomationMethodBadUsb) {
  306. scene_state = (SceneState*)plugin_state->current_scene_state;
  307. const TokenInfoIteratorContext* iterator_context =
  308. totp_config_get_token_iterator_context(plugin_state);
  309. totp_usb_type_code_worker_notify(
  310. scene_state->usb_type_code_worker_context,
  311. TotpUsbTypeCodeWorkerEventType,
  312. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  313. notification_message(
  314. plugin_state->notification_app,
  315. get_notification_sequence_automation(plugin_state, scene_state));
  316. return true;
  317. }
  318. #ifdef TOTP_BADBT_TYPE_ENABLED
  319. else if(
  320. event->input.key == InputKeyUp &&
  321. plugin_state->automation_method & AutomationMethodBadBt) {
  322. scene_state = (SceneState*)plugin_state->current_scene_state;
  323. const TokenInfoIteratorContext* iterator_context =
  324. totp_config_get_token_iterator_context(plugin_state);
  325. totp_bt_type_code_worker_notify(
  326. plugin_state->bt_type_code_worker_context,
  327. TotpBtTypeCodeWorkerEventType,
  328. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  329. notification_message(
  330. plugin_state->notification_app,
  331. get_notification_sequence_automation(plugin_state, scene_state));
  332. return true;
  333. }
  334. #endif
  335. } else if(event->input.type == InputTypePress || event->input.type == InputTypeRepeat) {
  336. switch(event->input.key) {
  337. case InputKeyUp:
  338. break;
  339. case InputKeyDown:
  340. break;
  341. case InputKeyRight: {
  342. const TokenInfoIteratorContext* iterator_context =
  343. totp_config_get_token_iterator_context(plugin_state);
  344. size_t current_token_index =
  345. totp_token_info_iterator_get_current_token_index(iterator_context);
  346. totp_roll_value_size_t(
  347. &current_token_index,
  348. 1,
  349. 0,
  350. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  351. RollOverflowBehaviorRoll);
  352. update_totp_params(plugin_state, current_token_index);
  353. break;
  354. }
  355. case InputKeyLeft: {
  356. const TokenInfoIteratorContext* iterator_context =
  357. totp_config_get_token_iterator_context(plugin_state);
  358. size_t current_token_index =
  359. totp_token_info_iterator_get_current_token_index(iterator_context);
  360. totp_roll_value_size_t(
  361. &current_token_index,
  362. -1,
  363. 0,
  364. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  365. RollOverflowBehaviorRoll);
  366. update_totp_params(plugin_state, current_token_index);
  367. break;
  368. }
  369. case InputKeyOk:
  370. break;
  371. case InputKeyBack:
  372. break;
  373. default:
  374. break;
  375. }
  376. } else if(event->input.type == InputTypeRelease && event->input.key == InputKeyOk) {
  377. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  378. }
  379. return true;
  380. }
  381. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  382. if(plugin_state->current_scene_state == NULL) return;
  383. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  384. totp_generate_code_worker_stop(scene_state->generate_code_worker_context);
  385. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  386. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  387. }
  388. #ifdef TOTP_BADBT_TYPE_ENABLED
  389. if(plugin_state->automation_method & AutomationMethodBadBt) {
  390. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  391. }
  392. #endif
  393. if(scene_state->notification_sequence_new_token != NULL) {
  394. free(scene_state->notification_sequence_new_token);
  395. }
  396. if(scene_state->notification_sequence_automation != NULL) {
  397. free(scene_state->notification_sequence_automation);
  398. }
  399. furi_mutex_free(scene_state->last_code_update_sync);
  400. free(scene_state);
  401. plugin_state->current_scene_state = NULL;
  402. }