totp_scene_generate_token.c 18 KB

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