bad_kb_app.c 16 KB

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