totp_scene_generate_token.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. 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 = totp_config_get_token_iterator_context(plugin_state);
  109. if(totp_token_info_iterator_go_to(iterator_context, token_index)) {
  110. totp_generate_code_worker_notify(
  111. scene_state->generate_code_worker_context, TotpGenerateCodeWorkerEventForceUpdate);
  112. }
  113. }
  114. static void draw_totp_code(Canvas* const canvas, const PluginState* const plugin_state) {
  115. const SceneState* scene_state = plugin_state->current_scene_state;
  116. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  117. uint8_t code_length =
  118. totp_token_info_iterator_get_current_token(iterator_context)->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. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  141. const TokenInfo* current_token = totp_token_info_iterator_get_current_token(iterator_context);
  142. uint8_t char_width = modeNine_15ptFontInfo.charInfo[0].width;
  143. scene_state->ui_precalculated_dimensions.code_total_length =
  144. current_token->digits * (char_width + modeNine_15ptFontInfo.spacePixels);
  145. scene_state->ui_precalculated_dimensions.code_offset_x =
  146. (SCREEN_WIDTH - scene_state->ui_precalculated_dimensions.code_total_length) >> 1;
  147. scene_state->ui_precalculated_dimensions.code_offset_x_inc =
  148. char_width + modeNine_15ptFontInfo.spacePixels;
  149. scene_state->ui_precalculated_dimensions.code_offset_y =
  150. SCREEN_HEIGHT_CENTER - (modeNine_15ptFontInfo.height >> 1);
  151. if(time_left) {
  152. notification_message(
  153. plugin_state->notification_app,
  154. get_notification_sequence_new_token(plugin_state, plugin_state->current_scene_state));
  155. }
  156. }
  157. static void on_code_lifetime_updated_generated(float code_lifetime_percent, void* context) {
  158. SceneState* scene_state = context;
  159. scene_state->ui_precalculated_dimensions.progress_bar_width =
  160. (uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * code_lifetime_percent);
  161. scene_state->ui_precalculated_dimensions.progress_bar_x =
  162. ((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) -
  163. scene_state->ui_precalculated_dimensions.progress_bar_width) >>
  164. 1) +
  165. PROGRESS_BAR_MARGIN;
  166. }
  167. void totp_scene_generate_token_activate(PluginState* plugin_state) {
  168. SceneState* scene_state = malloc(sizeof(SceneState));
  169. furi_check(scene_state != NULL);
  170. plugin_state->current_scene_state = scene_state;
  171. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  172. scene_state->last_code_update_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  173. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  174. scene_state->usb_type_code_worker_context = totp_usb_type_code_worker_start(
  175. scene_state->last_code,
  176. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  177. scene_state->last_code_update_sync);
  178. }
  179. #ifdef TOTP_BADBT_TYPE_ENABLED
  180. if(plugin_state->automation_method & AutomationMethodBadBt) {
  181. if(plugin_state->bt_type_code_worker_context == NULL) {
  182. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  183. }
  184. totp_bt_type_code_worker_start(
  185. plugin_state->bt_type_code_worker_context,
  186. scene_state->last_code,
  187. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  188. scene_state->last_code_update_sync);
  189. }
  190. #endif
  191. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  192. scene_state->generate_code_worker_context = totp_generate_code_worker_start(
  193. scene_state->last_code,
  194. totp_token_info_iterator_get_current_token(iterator_context),
  195. scene_state->last_code_update_sync,
  196. plugin_state->timezone_offset,
  197. plugin_state->iv);
  198. totp_generate_code_worker_set_code_generated_handler(
  199. scene_state->generate_code_worker_context, &on_new_token_code_generated, plugin_state);
  200. totp_generate_code_worker_set_lifetime_changed_handler(
  201. scene_state->generate_code_worker_context,
  202. &on_code_lifetime_updated_generated,
  203. scene_state);
  204. update_totp_params(plugin_state, totp_token_info_iterator_get_current_token_index(iterator_context));
  205. }
  206. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  207. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  208. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  209. canvas_draw_str_aligned(
  210. canvas,
  211. SCREEN_WIDTH_CENTER,
  212. SCREEN_HEIGHT_CENTER - 10,
  213. AlignCenter,
  214. AlignCenter,
  215. "Token list is empty");
  216. canvas_draw_str_aligned(
  217. canvas,
  218. SCREEN_WIDTH_CENTER,
  219. SCREEN_HEIGHT_CENTER + 10,
  220. AlignCenter,
  221. AlignCenter,
  222. "Press OK button to add");
  223. return;
  224. }
  225. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  226. canvas_set_font(canvas, FontPrimary);
  227. const char* token_name_cstr = furi_string_get_cstr(
  228. totp_token_info_iterator_get_current_token(iterator_context)->name);
  229. uint16_t token_name_width = canvas_string_width(canvas, token_name_cstr);
  230. if(SCREEN_WIDTH - token_name_width > 18) {
  231. canvas_draw_str_aligned(
  232. canvas,
  233. SCREEN_WIDTH_CENTER,
  234. SCREEN_HEIGHT_CENTER - 20,
  235. AlignCenter,
  236. AlignCenter,
  237. token_name_cstr);
  238. } else {
  239. canvas_draw_str_aligned(
  240. canvas, 9, SCREEN_HEIGHT_CENTER - 20, AlignLeft, AlignCenter, token_name_cstr);
  241. canvas_set_color(canvas, ColorWhite);
  242. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  243. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  244. canvas_set_color(canvas, ColorBlack);
  245. }
  246. draw_totp_code(canvas, plugin_state);
  247. canvas_draw_box(
  248. canvas,
  249. scene_state->ui_precalculated_dimensions.progress_bar_x,
  250. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  251. scene_state->ui_precalculated_dimensions.progress_bar_width,
  252. PROGRESS_BAR_HEIGHT);
  253. if(totp_token_info_iterator_get_total_count(iterator_context) > 1) {
  254. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  255. canvas_draw_icon(
  256. canvas, SCREEN_WIDTH - 9, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  257. }
  258. #ifdef TOTP_AUTOMATION_ICONS_ENABLED
  259. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  260. canvas_draw_icon(
  261. canvas,
  262. #ifdef TOTP_BADBT_TYPE_ENABLED
  263. SCREEN_WIDTH_CENTER -
  264. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  265. #else
  266. SCREEN_WIDTH_CENTER - 15,
  267. #endif
  268. SCREEN_HEIGHT_CENTER + 12,
  269. &I_hid_usb_31x9);
  270. }
  271. #ifdef TOTP_BADBT_TYPE_ENABLED
  272. if(plugin_state->automation_method & AutomationMethodBadBt &&
  273. plugin_state->bt_type_code_worker_context != NULL &&
  274. totp_bt_type_code_worker_is_advertising(plugin_state->bt_type_code_worker_context)) {
  275. canvas_draw_icon(
  276. canvas,
  277. SCREEN_WIDTH_CENTER +
  278. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  279. SCREEN_HEIGHT_CENTER + 12,
  280. &I_hid_ble_31x9);
  281. }
  282. #endif
  283. #endif
  284. }
  285. bool totp_scene_generate_token_handle_event(
  286. const PluginEvent* const event,
  287. PluginState* plugin_state) {
  288. if(event->type != EventTypeKey) {
  289. return true;
  290. }
  291. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  292. return false;
  293. }
  294. SceneState* scene_state;
  295. if(event->input.type == InputTypeLong) {
  296. if(event->input.key == InputKeyDown &&
  297. plugin_state->automation_method & AutomationMethodBadUsb) {
  298. scene_state = (SceneState*)plugin_state->current_scene_state;
  299. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  300. totp_usb_type_code_worker_notify(
  301. scene_state->usb_type_code_worker_context,
  302. TotpUsbTypeCodeWorkerEventType,
  303. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  304. notification_message(
  305. plugin_state->notification_app,
  306. get_notification_sequence_automation(plugin_state, scene_state));
  307. return true;
  308. }
  309. #ifdef TOTP_BADBT_TYPE_ENABLED
  310. else if(
  311. event->input.key == InputKeyUp &&
  312. plugin_state->automation_method & AutomationMethodBadBt) {
  313. scene_state = (SceneState*)plugin_state->current_scene_state;
  314. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  315. totp_bt_type_code_worker_notify(
  316. plugin_state->bt_type_code_worker_context,
  317. TotpBtTypeCodeWorkerEventType,
  318. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  319. notification_message(
  320. plugin_state->notification_app,
  321. get_notification_sequence_automation(plugin_state, scene_state));
  322. return true;
  323. }
  324. #endif
  325. }
  326. if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
  327. return true;
  328. }
  329. switch(event->input.key) {
  330. case InputKeyUp:
  331. break;
  332. case InputKeyDown:
  333. break;
  334. case InputKeyRight: {
  335. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  336. size_t current_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  337. totp_roll_value_size_t(
  338. &current_token_index,
  339. 1,
  340. 0,
  341. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  342. RollOverflowBehaviorRoll);
  343. update_totp_params(plugin_state, current_token_index);
  344. break;
  345. }
  346. case InputKeyLeft: {
  347. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  348. size_t current_token_index = 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. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  360. break;
  361. case InputKeyBack:
  362. break;
  363. default:
  364. break;
  365. }
  366. return true;
  367. }
  368. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  369. if(plugin_state->current_scene_state == NULL) return;
  370. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  371. totp_generate_code_worker_stop(scene_state->generate_code_worker_context);
  372. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  373. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  374. }
  375. #ifdef TOTP_BADBT_TYPE_ENABLED
  376. if(plugin_state->automation_method & AutomationMethodBadBt) {
  377. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  378. }
  379. #endif
  380. if(scene_state->notification_sequence_new_token != NULL) {
  381. free(scene_state->notification_sequence_new_token);
  382. }
  383. if(scene_state->notification_sequence_automation != NULL) {
  384. free(scene_state->notification_sequence_automation);
  385. }
  386. furi_mutex_free(scene_state->last_code_update_sync);
  387. free(scene_state);
  388. plugin_state->current_scene_state = NULL;
  389. }