bad_kb_app.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include "bad_kb_app_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <storage/storage.h>
  5. #include <lib/toolbox/path.h>
  6. #include <lib/flipper_format/flipper_format.h>
  7. #include <bt/bt_service/bt_i.h>
  8. #include "helpers/ducky_script_i.h"
  9. // Adjusts to serial MAC +2 in app init
  10. uint8_t BAD_KB_BOUND_MAC[GAP_MAC_ADDR_SIZE] = {0};
  11. static bool bad_kb_app_custom_event_callback(void* context, uint32_t event) {
  12. furi_assert(context);
  13. BadKbApp* app = context;
  14. return scene_manager_handle_custom_event(app->scene_manager, event);
  15. }
  16. static bool bad_kb_app_back_event_callback(void* context) {
  17. furi_assert(context);
  18. BadKbApp* app = context;
  19. return scene_manager_handle_back_event(app->scene_manager);
  20. }
  21. static void bad_kb_app_tick_event_callback(void* context) {
  22. furi_assert(context);
  23. BadKbApp* app = context;
  24. scene_manager_handle_tick_event(app->scene_manager);
  25. }
  26. void bad_kb_load_settings(BadKbApp* app) {
  27. furi_string_reset(app->keyboard_layout);
  28. BadKbConfig* cfg = &app->config;
  29. Storage* storage = furi_record_open(RECORD_STORAGE);
  30. FlipperFormat* file = flipper_format_file_alloc(storage);
  31. if(flipper_format_file_open_existing(file, BAD_KB_SETTINGS_PATH)) {
  32. FuriString* tmp_str = furi_string_alloc();
  33. uint32_t tmp_uint = 0;
  34. if(!flipper_format_read_string(file, "Keyboard_Layout", app->keyboard_layout)) {
  35. furi_string_reset(app->keyboard_layout);
  36. flipper_format_rewind(file);
  37. }
  38. if(!flipper_format_read_bool(file, "Is_Bt", &app->is_bt, 1)) {
  39. app->is_bt = false;
  40. flipper_format_rewind(file);
  41. }
  42. if(!flipper_format_read_bool(file, "Bt_Remember", &cfg->ble.bonding, 1)) {
  43. cfg->ble.bonding = false;
  44. flipper_format_rewind(file);
  45. }
  46. if(!flipper_format_read_uint32(file, "Bt_Pairing", &tmp_uint, 1)) {
  47. tmp_uint = GapPairingNone;
  48. flipper_format_rewind(file);
  49. }
  50. cfg->ble.pairing = tmp_uint;
  51. if(flipper_format_read_string(file, "Bt_Name", tmp_str)) {
  52. strlcpy(cfg->ble.name, furi_string_get_cstr(tmp_str), sizeof(cfg->ble.name));
  53. } else {
  54. cfg->ble.name[0] = '\0';
  55. flipper_format_rewind(file);
  56. }
  57. if(!flipper_format_read_hex(
  58. file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac))) {
  59. memset(cfg->ble.mac, 0, sizeof(cfg->ble.mac));
  60. flipper_format_rewind(file);
  61. }
  62. if(flipper_format_read_string(file, "Usb_Manuf", tmp_str)) {
  63. strlcpy(cfg->usb.manuf, furi_string_get_cstr(tmp_str), sizeof(cfg->usb.manuf));
  64. } else {
  65. cfg->usb.manuf[0] = '\0';
  66. flipper_format_rewind(file);
  67. }
  68. if(flipper_format_read_string(file, "Usb_Product", tmp_str)) {
  69. strlcpy(cfg->usb.product, furi_string_get_cstr(tmp_str), sizeof(cfg->usb.product));
  70. } else {
  71. cfg->usb.product[0] = '\0';
  72. flipper_format_rewind(file);
  73. }
  74. if(!flipper_format_read_uint32(file, "Usb_Vid", &cfg->usb.vid, 1)) {
  75. cfg->usb.vid = 0;
  76. flipper_format_rewind(file);
  77. }
  78. if(!flipper_format_read_uint32(file, "Usb_Pid", &cfg->usb.pid, 1)) {
  79. cfg->usb.pid = 0;
  80. flipper_format_rewind(file);
  81. }
  82. furi_string_free(tmp_str);
  83. flipper_format_file_close(file);
  84. }
  85. flipper_format_free(file);
  86. if(!furi_string_empty(app->keyboard_layout)) {
  87. FileInfo layout_file_info;
  88. FS_Error file_check_err = storage_common_stat(
  89. storage, furi_string_get_cstr(app->keyboard_layout), &layout_file_info);
  90. if(file_check_err != FSE_OK) {
  91. furi_string_reset(app->keyboard_layout);
  92. return;
  93. }
  94. if(layout_file_info.size != 256) {
  95. furi_string_reset(app->keyboard_layout);
  96. }
  97. }
  98. furi_record_close(RECORD_STORAGE);
  99. }
  100. static void bad_kb_save_settings(BadKbApp* app) {
  101. BadKbConfig* cfg = &app->config;
  102. Storage* storage = furi_record_open(RECORD_STORAGE);
  103. FlipperFormat* file = flipper_format_file_alloc(storage);
  104. if(flipper_format_file_open_always(file, BAD_KB_SETTINGS_PATH)) {
  105. uint32_t tmp_uint = 0;
  106. flipper_format_write_string(file, "Keyboard_Layout", app->keyboard_layout);
  107. flipper_format_write_bool(file, "Is_Bt", &app->is_bt, 1);
  108. flipper_format_write_bool(file, "Bt_Remember", &cfg->ble.bonding, 1);
  109. tmp_uint = cfg->ble.pairing;
  110. flipper_format_write_uint32(file, "Bt_Pairing", &tmp_uint, 1);
  111. flipper_format_write_string_cstr(file, "Bt_Name", cfg->ble.name);
  112. flipper_format_write_hex(file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac));
  113. flipper_format_write_string_cstr(file, "Usb_Manuf", cfg->usb.manuf);
  114. flipper_format_write_string_cstr(file, "Usb_Product", cfg->usb.product);
  115. flipper_format_write_uint32(file, "Usb_Vid", &cfg->usb.vid, 1);
  116. flipper_format_write_uint32(file, "Usb_Pid", &cfg->usb.pid, 1);
  117. flipper_format_file_close(file);
  118. }
  119. flipper_format_free(file);
  120. furi_record_close(RECORD_STORAGE);
  121. }
  122. void bad_kb_app_show_loading_popup(BadKbApp* app, bool show) {
  123. if(show) {
  124. // Raise timer priority so that animations can play
  125. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  126. view_dispatcher_switch_to_view(app->view_dispatcher, BadKbAppViewLoading);
  127. } else {
  128. // Restore default timer priority
  129. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  130. }
  131. }
  132. int32_t bad_kb_conn_apply(BadKbApp* app) {
  133. if(app->is_bt) {
  134. // Setup profile config
  135. BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
  136. memcpy(&app->cur_ble_cfg, &cfg->ble, sizeof(cfg->ble));
  137. if(app->cur_ble_cfg.bonding) {
  138. // Hardcode mac for remember mode
  139. // Change in config copy to preserve user choice for non-remember mode
  140. memcpy(app->cur_ble_cfg.mac, BAD_KB_BOUND_MAC, sizeof(BAD_KB_BOUND_MAC));
  141. }
  142. // Prepare for new profile
  143. bt_timeout = bt_hid_delays[LevelRssi39_0];
  144. bt_disconnect(app->bt);
  145. furi_delay_ms(200);
  146. bt_keys_storage_set_storage_path(app->bt, BAD_KB_KEYS_PATH);
  147. // Set profile
  148. app->ble_hid = bt_profile_start(app->bt, ble_profile_hid, &app->cur_ble_cfg);
  149. furi_check(app->ble_hid);
  150. // Advertise even if BT is off in settings
  151. furi_hal_bt_start_advertising();
  152. app->conn_mode = BadKbConnModeBt;
  153. } else {
  154. // Unlock RPC connections
  155. furi_hal_usb_unlock();
  156. // Context will apply with set_config only if pointer address is different, so we use a copy
  157. FuriHalUsbHidConfig* cur_usb_cfg = malloc(sizeof(FuriHalUsbHidConfig));
  158. // Setup new config
  159. BadKbConfig* cfg = app->set_usb_id ? &app->id_config : &app->config;
  160. memcpy(cur_usb_cfg, &cfg->usb, sizeof(cfg->usb));
  161. // Set profile
  162. furi_check(furi_hal_usb_set_config(&usb_hid, cur_usb_cfg));
  163. if(app->cur_usb_cfg) free(app->cur_usb_cfg);
  164. app->cur_usb_cfg = cur_usb_cfg;
  165. app->conn_mode = BadKbConnModeUsb;
  166. }
  167. return 0;
  168. }
  169. void bad_kb_conn_reset(BadKbApp* app) {
  170. if(app->conn_mode == BadKbConnModeBt) {
  171. bt_disconnect(app->bt);
  172. furi_delay_ms(200);
  173. bt_keys_storage_set_default_path(app->bt);
  174. furi_check(bt_profile_restore_default(app->bt));
  175. } else if(app->conn_mode == BadKbConnModeUsb) {
  176. // TODO: maybe also restore USB context?
  177. furi_check(furi_hal_usb_set_config(app->prev_usb_mode, NULL));
  178. }
  179. app->conn_mode = BadKbConnModeNone;
  180. }
  181. void bad_kb_config_adjust(BadKbConfig* cfg) {
  182. // Avoid empty name
  183. if(cfg->ble.name[0] == '\0') {
  184. snprintf(
  185. cfg->ble.name, sizeof(cfg->ble.name), "Control %s", furi_hal_version_get_name_ptr());
  186. }
  187. const uint8_t* normal_mac = furi_hal_version_get_ble_mac();
  188. uint8_t empty_mac[sizeof(cfg->ble.mac)] = {0};
  189. uint8_t default_mac[sizeof(cfg->ble.mac)] = {0x6c, 0x7a, 0xd8, 0xac, 0x57, 0x72}; //furi_hal_bt
  190. if(memcmp(cfg->ble.mac, empty_mac, sizeof(cfg->ble.mac)) == 0 ||
  191. memcmp(cfg->ble.mac, normal_mac, sizeof(cfg->ble.mac)) == 0 ||
  192. memcmp(cfg->ble.mac, default_mac, sizeof(cfg->ble.mac)) == 0) {
  193. memcpy(cfg->ble.mac, normal_mac, sizeof(cfg->ble.mac));
  194. cfg->ble.mac[2]++;
  195. }
  196. // Use defaults if vid or pid are unset
  197. if(cfg->usb.vid == 0) cfg->usb.vid = 0x046D;
  198. if(cfg->usb.pid == 0) cfg->usb.pid = 0xC529;
  199. }
  200. void bad_kb_config_refresh(BadKbApp* app) {
  201. bt_set_status_changed_callback(app->bt, NULL, NULL);
  202. furi_hal_hid_set_state_callback(NULL, NULL);
  203. if(app->bad_kb_script) {
  204. furi_thread_flags_set(furi_thread_get_id(app->bad_kb_script->thread), WorkerEvtDisconnect);
  205. }
  206. if(app->conn_init_thread) {
  207. furi_thread_join(app->conn_init_thread);
  208. }
  209. bool apply = false;
  210. if(app->is_bt) {
  211. BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
  212. bad_kb_config_adjust(cfg);
  213. if(app->conn_mode != BadKbConnModeBt) {
  214. apply = true;
  215. bad_kb_conn_reset(app);
  216. } else {
  217. BleProfileHidParams* cur = &app->cur_ble_cfg;
  218. apply = apply || cfg->ble.bonding != cur->bonding;
  219. apply = apply || cfg->ble.pairing != cur->pairing;
  220. apply = apply || strncmp(cfg->ble.name, cur->name, sizeof(cfg->ble.name));
  221. apply = apply || memcmp(cfg->ble.mac, cur->mac, sizeof(cfg->ble.mac));
  222. }
  223. } else {
  224. BadKbConfig* cfg = app->set_usb_id ? &app->id_config : &app->config;
  225. bad_kb_config_adjust(cfg);
  226. if(app->conn_mode != BadKbConnModeUsb) {
  227. apply = true;
  228. bad_kb_conn_reset(app);
  229. } else {
  230. FuriHalUsbHidConfig* cur = app->cur_usb_cfg;
  231. apply = apply || cfg->usb.vid != cur->vid;
  232. apply = apply || cfg->usb.pid != cur->pid;
  233. apply = apply || strncmp(cfg->usb.manuf, cur->manuf, sizeof(cur->manuf));
  234. apply = apply || strncmp(cfg->usb.product, cur->product, sizeof(cur->product));
  235. }
  236. }
  237. if(apply) {
  238. bad_kb_conn_apply(app);
  239. }
  240. if(app->bad_kb_script) {
  241. BadKbScript* script = app->bad_kb_script;
  242. script->st.is_bt = app->is_bt;
  243. script->bt = app->is_bt ? app->bt : NULL;
  244. bool connected;
  245. if(app->is_bt) {
  246. bt_set_status_changed_callback(app->bt, bad_kb_bt_hid_state_callback, script);
  247. connected = furi_hal_bt_is_connected();
  248. } else {
  249. furi_hal_hid_set_state_callback(bad_kb_usb_hid_state_callback, script);
  250. connected = furi_hal_hid_is_connected();
  251. }
  252. if(connected) {
  253. furi_thread_flags_set(furi_thread_get_id(script->thread), WorkerEvtConnect);
  254. }
  255. }
  256. // Reload config page
  257. scene_manager_next_scene(app->scene_manager, BadKbSceneConfig);
  258. scene_manager_previous_scene(app->scene_manager);
  259. }
  260. BadKbApp* bad_kb_app_alloc(char* arg) {
  261. BadKbApp* app = malloc(sizeof(BadKbApp));
  262. app->bad_kb_script = NULL;
  263. app->file_path = furi_string_alloc();
  264. app->keyboard_layout = furi_string_alloc();
  265. if(arg && strlen(arg)) {
  266. furi_string_set(app->file_path, arg);
  267. }
  268. bad_kb_load_settings(app);
  269. app->gui = furi_record_open(RECORD_GUI);
  270. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  271. app->dialogs = furi_record_open(RECORD_DIALOGS);
  272. app->view_dispatcher = view_dispatcher_alloc();
  273. view_dispatcher_enable_queue(app->view_dispatcher);
  274. app->scene_manager = scene_manager_alloc(&bad_kb_scene_handlers, app);
  275. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  276. view_dispatcher_set_tick_event_callback(
  277. app->view_dispatcher, bad_kb_app_tick_event_callback, 250);
  278. view_dispatcher_set_custom_event_callback(
  279. app->view_dispatcher, bad_kb_app_custom_event_callback);
  280. view_dispatcher_set_navigation_event_callback(
  281. app->view_dispatcher, bad_kb_app_back_event_callback);
  282. Bt* bt = furi_record_open(RECORD_BT);
  283. app->bt = bt;
  284. app->bt->suppress_pin_screen = true;
  285. bad_kb_config_adjust(&app->config);
  286. // Save prev config
  287. app->prev_usb_mode = furi_hal_usb_get_config();
  288. // Adjust BT remember MAC to be serial MAC +2
  289. memcpy(BAD_KB_BOUND_MAC, furi_hal_version_get_ble_mac(), sizeof(BAD_KB_BOUND_MAC));
  290. BAD_KB_BOUND_MAC[2] += 2;
  291. // Custom Widget
  292. app->widget = widget_alloc();
  293. view_dispatcher_add_view(
  294. app->view_dispatcher, BadKbAppViewWidget, widget_get_view(app->widget));
  295. app->var_item_list = variable_item_list_alloc();
  296. view_dispatcher_add_view(
  297. app->view_dispatcher,
  298. BadKbAppViewVarItemList,
  299. variable_item_list_get_view(app->var_item_list));
  300. app->bad_kb_view = bad_kb_view_alloc();
  301. view_dispatcher_add_view(
  302. app->view_dispatcher, BadKbAppViewWork, bad_kb_view_get_view(app->bad_kb_view));
  303. app->text_input = text_input_alloc();
  304. view_dispatcher_add_view(
  305. app->view_dispatcher, BadKbAppViewTextInput, text_input_get_view(app->text_input));
  306. app->byte_input = byte_input_alloc();
  307. view_dispatcher_add_view(
  308. app->view_dispatcher, BadKbAppViewByteInput, byte_input_get_view(app->byte_input));
  309. app->loading = loading_alloc();
  310. view_dispatcher_add_view(
  311. app->view_dispatcher, BadKbAppViewLoading, loading_get_view(app->loading));
  312. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  313. app->conn_mode = BadKbConnModeNone;
  314. app->conn_init_thread =
  315. furi_thread_alloc_ex("BadKbConnInit", 1024, (FuriThreadCallback)bad_kb_conn_apply, app);
  316. furi_thread_start(app->conn_init_thread);
  317. if(!furi_string_empty(app->file_path)) {
  318. app->bad_kb_script = bad_kb_script_open(app->file_path, app->is_bt ? app->bt : NULL, app);
  319. bad_kb_script_set_keyboard_layout(app->bad_kb_script, app->keyboard_layout);
  320. scene_manager_next_scene(app->scene_manager, BadKbSceneWork);
  321. } else {
  322. furi_string_set(app->file_path, BAD_USB_APP_BASE_FOLDER);
  323. scene_manager_next_scene(app->scene_manager, BadKbSceneFileSelect);
  324. }
  325. return app;
  326. }
  327. void bad_kb_app_free(BadKbApp* app) {
  328. furi_assert(app);
  329. if(app->bad_kb_script) {
  330. bad_kb_script_close(app->bad_kb_script);
  331. app->bad_kb_script = NULL;
  332. }
  333. // Views
  334. view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewWork);
  335. bad_kb_view_free(app->bad_kb_view);
  336. // Custom Widget
  337. view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewWidget);
  338. widget_free(app->widget);
  339. // Variable item list
  340. view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewVarItemList);
  341. variable_item_list_free(app->var_item_list);
  342. // Text Input
  343. view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewTextInput);
  344. text_input_free(app->text_input);
  345. // Byte Input
  346. view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewByteInput);
  347. byte_input_free(app->byte_input);
  348. // Loading
  349. view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewLoading);
  350. loading_free(app->loading);
  351. // View dispatcher
  352. view_dispatcher_free(app->view_dispatcher);
  353. scene_manager_free(app->scene_manager);
  354. // Restore connection config
  355. app->bt->suppress_pin_screen = false;
  356. if(app->conn_init_thread) {
  357. furi_thread_join(app->conn_init_thread);
  358. furi_thread_free(app->conn_init_thread);
  359. app->conn_init_thread = NULL;
  360. }
  361. bad_kb_conn_reset(app);
  362. if(app->cur_usb_cfg) free(app->cur_usb_cfg);
  363. // Close records
  364. furi_record_close(RECORD_GUI);
  365. furi_record_close(RECORD_NOTIFICATION);
  366. furi_record_close(RECORD_DIALOGS);
  367. furi_record_close(RECORD_BT);
  368. bad_kb_save_settings(app);
  369. furi_string_free(app->file_path);
  370. furi_string_free(app->keyboard_layout);
  371. free(app);
  372. }
  373. int32_t bad_kb_app(void* p) {
  374. BadKbApp* bad_kb_app = bad_kb_app_alloc((char*)p);
  375. view_dispatcher_run(bad_kb_app->view_dispatcher);
  376. bad_kb_app_free(bad_kb_app);
  377. return 0;
  378. }