bad_kb_app.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. if(!flipper_format_read_string(file, "Keyboard_Layout", app->keyboard_layout)) {
  34. furi_string_reset(app->keyboard_layout);
  35. flipper_format_rewind(file);
  36. }
  37. if(!flipper_format_read_bool(file, "Is_Bt", &app->is_bt, 1)) {
  38. app->is_bt = false;
  39. flipper_format_rewind(file);
  40. }
  41. if(!flipper_format_read_bool(file, "Bt_Remember", &app->bt_remember, 1)) {
  42. app->bt_remember = false;
  43. flipper_format_rewind(file);
  44. }
  45. if(flipper_format_read_string(file, "Bt_Name", tmp_str)) {
  46. strlcpy(cfg->ble.name, furi_string_get_cstr(tmp_str), sizeof(cfg->ble.name));
  47. } else {
  48. cfg->ble.name[0] = '\0';
  49. flipper_format_rewind(file);
  50. }
  51. if(!flipper_format_read_hex(
  52. file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac))) {
  53. memset(cfg->ble.mac, 0, sizeof(cfg->ble.mac));
  54. flipper_format_rewind(file);
  55. }
  56. if(flipper_format_read_string(file, "Usb_Manuf", tmp_str)) {
  57. strlcpy(cfg->usb.manuf, furi_string_get_cstr(tmp_str), sizeof(cfg->usb.manuf));
  58. } else {
  59. cfg->usb.manuf[0] = '\0';
  60. flipper_format_rewind(file);
  61. }
  62. if(flipper_format_read_string(file, "Usb_Product", tmp_str)) {
  63. strlcpy(cfg->usb.product, furi_string_get_cstr(tmp_str), sizeof(cfg->usb.product));
  64. } else {
  65. cfg->usb.product[0] = '\0';
  66. flipper_format_rewind(file);
  67. }
  68. if(!flipper_format_read_uint32(file, "Usb_Vid", &cfg->usb.vid, 1)) {
  69. cfg->usb.vid = 0;
  70. flipper_format_rewind(file);
  71. }
  72. if(!flipper_format_read_uint32(file, "Usb_Pid", &cfg->usb.pid, 1)) {
  73. cfg->usb.pid = 0;
  74. flipper_format_rewind(file);
  75. }
  76. furi_string_free(tmp_str);
  77. flipper_format_file_close(file);
  78. }
  79. flipper_format_free(file);
  80. if(!furi_string_empty(app->keyboard_layout)) {
  81. FileInfo layout_file_info;
  82. FS_Error file_check_err = storage_common_stat(
  83. storage, furi_string_get_cstr(app->keyboard_layout), &layout_file_info);
  84. if(file_check_err != FSE_OK) {
  85. furi_string_reset(app->keyboard_layout);
  86. return;
  87. }
  88. if(layout_file_info.size != 256) {
  89. furi_string_reset(app->keyboard_layout);
  90. }
  91. }
  92. furi_record_close(RECORD_STORAGE);
  93. }
  94. static void bad_kb_save_settings(BadKbApp* app) {
  95. BadKbConfig* cfg = &app->config;
  96. Storage* storage = furi_record_open(RECORD_STORAGE);
  97. FlipperFormat* file = flipper_format_file_alloc(storage);
  98. if(flipper_format_file_open_always(file, BAD_KB_SETTINGS_PATH)) {
  99. flipper_format_write_string(file, "Keyboard_Layout", app->keyboard_layout);
  100. flipper_format_write_bool(file, "Is_Bt", &app->is_bt, 1);
  101. flipper_format_write_bool(file, "Bt_Remember", &app->bt_remember, 1);
  102. flipper_format_write_string_cstr(file, "Bt_Name", cfg->ble.name);
  103. flipper_format_write_hex(file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac));
  104. flipper_format_write_string_cstr(file, "Usb_Manuf", cfg->usb.manuf);
  105. flipper_format_write_string_cstr(file, "Usb_Product", cfg->usb.product);
  106. flipper_format_write_uint32(file, "Usb_Vid", &cfg->usb.vid, 1);
  107. flipper_format_write_uint32(file, "Usb_Pid", &cfg->usb.pid, 1);
  108. flipper_format_file_close(file);
  109. }
  110. flipper_format_free(file);
  111. furi_record_close(RECORD_STORAGE);
  112. }
  113. void bad_kb_app_show_loading_popup(BadKbApp* app, bool show) {
  114. if(show) {
  115. // Raise timer priority so that animations can play
  116. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  117. view_dispatcher_switch_to_view(app->view_dispatcher, BadKbAppViewLoading);
  118. } else {
  119. // Restore default timer priority
  120. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  121. }
  122. }
  123. int32_t bad_kb_conn_apply(BadKbApp* app) {
  124. if(app->is_bt) {
  125. bt_timeout = bt_hid_delays[LevelRssi39_0];
  126. bt_disconnect(app->bt);
  127. furi_delay_ms(200);
  128. bt_keys_storage_set_storage_path(app->bt, BAD_KB_KEYS_PATH);
  129. // Setup new config
  130. BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
  131. memcpy(&app->cur_ble_cfg, &cfg->ble, sizeof(cfg->ble));
  132. app->cur_ble_cfg.bonding = app->bt_remember;
  133. if(app->bt_remember) {
  134. app->cur_ble_cfg.pairing = GapPairingPinCodeVerifyYesNo;
  135. } else {
  136. app->cur_ble_cfg.pairing = GapPairingNone;
  137. memcpy(app->cur_ble_cfg.mac, BAD_KB_BOUND_MAC, sizeof(BAD_KB_BOUND_MAC));
  138. }
  139. // Set profile
  140. app->ble_hid = bt_profile_start(app->bt, ble_profile_hid, &app->cur_ble_cfg);
  141. furi_check(app->ble_hid);
  142. // Advertise even if BT is off in settings
  143. furi_hal_bt_start_advertising();
  144. app->conn_mode = BadKbConnModeBt;
  145. } else {
  146. // Unlock RPC connections
  147. furi_hal_usb_unlock();
  148. // Context will apply with set_config only if pointer address is different, so we use a copy
  149. FuriHalUsbHidConfig* cur_usb_cfg = malloc(sizeof(FuriHalUsbHidConfig));
  150. // Setup new config
  151. BadKbConfig* cfg = app->set_usb_id ? &app->id_config : &app->config;
  152. memcpy(cur_usb_cfg, &cfg->usb, sizeof(cfg->usb));
  153. // Set profile
  154. furi_check(furi_hal_usb_set_config(&usb_hid, cur_usb_cfg));
  155. if(app->cur_usb_cfg) free(app->cur_usb_cfg);
  156. app->cur_usb_cfg = cur_usb_cfg;
  157. app->conn_mode = BadKbConnModeUsb;
  158. }
  159. return 0;
  160. }
  161. void bad_kb_conn_reset(BadKbApp* app) {
  162. if(app->conn_mode == BadKbConnModeBt) {
  163. bt_disconnect(app->bt);
  164. furi_delay_ms(200);
  165. bt_keys_storage_set_default_path(app->bt);
  166. furi_check(bt_profile_restore_default(app->bt));
  167. } else if(app->conn_mode == BadKbConnModeUsb) {
  168. // TODO: maybe also restore USB context?
  169. furi_check(furi_hal_usb_set_config(app->prev_usb_mode, NULL));
  170. }
  171. app->conn_mode = BadKbConnModeNone;
  172. }
  173. void bad_kb_config_adjust(BadKbConfig* cfg) {
  174. // Avoid empty name
  175. if(cfg->ble.name[0] == '\0') {
  176. snprintf(
  177. cfg->ble.name, sizeof(cfg->ble.name), "Control %s", furi_hal_version_get_name_ptr());
  178. }
  179. const uint8_t* normal_mac = furi_hal_version_get_ble_mac();
  180. uint8_t empty_mac[sizeof(cfg->ble.mac)] = {0};
  181. uint8_t default_mac[sizeof(cfg->ble.mac)] = {0x6c, 0x7a, 0xd8, 0xac, 0x57, 0x72}; //furi_hal_bt
  182. if(memcmp(cfg->ble.mac, empty_mac, sizeof(cfg->ble.mac)) == 0 ||
  183. memcmp(cfg->ble.mac, normal_mac, sizeof(cfg->ble.mac)) == 0 ||
  184. memcmp(cfg->ble.mac, default_mac, sizeof(cfg->ble.mac)) == 0) {
  185. memcpy(cfg->ble.mac, normal_mac, sizeof(cfg->ble.mac));
  186. cfg->ble.mac[2]++;
  187. }
  188. // Use defaults if vid or pid are unset
  189. if(cfg->usb.vid == 0) cfg->usb.vid = 0x046D;
  190. if(cfg->usb.pid == 0) cfg->usb.pid = 0xC529;
  191. }
  192. void bad_kb_config_refresh(BadKbApp* app) {
  193. bt_set_status_changed_callback(app->bt, NULL, NULL);
  194. furi_hal_hid_set_state_callback(NULL, NULL);
  195. if(app->bad_kb_script) {
  196. furi_thread_flags_set(furi_thread_get_id(app->bad_kb_script->thread), WorkerEvtDisconnect);
  197. }
  198. if(app->conn_init_thread) {
  199. furi_thread_join(app->conn_init_thread);
  200. }
  201. bool apply = false;
  202. if(app->is_bt) {
  203. BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
  204. bad_kb_config_adjust(cfg);
  205. if(app->conn_mode != BadKbConnModeBt) {
  206. apply = true;
  207. bad_kb_conn_reset(app);
  208. } else {
  209. BleProfileHidParams* cur = &app->cur_ble_cfg;
  210. apply = apply || cfg->ble.bonding != app->bt_remember;
  211. apply = apply || strncmp(cfg->ble.name, cur->name, sizeof(cfg->ble.name));
  212. apply = apply || memcmp(cfg->ble.mac, cur->mac, sizeof(cfg->ble.mac));
  213. }
  214. } else {
  215. BadKbConfig* cfg = app->set_usb_id ? &app->id_config : &app->config;
  216. bad_kb_config_adjust(cfg);
  217. if(app->conn_mode != BadKbConnModeUsb) {
  218. apply = true;
  219. bad_kb_conn_reset(app);
  220. } else {
  221. FuriHalUsbHidConfig* cur = app->cur_usb_cfg;
  222. apply = apply || cfg->usb.vid != cur->vid;
  223. apply = apply || cfg->usb.pid != cur->pid;
  224. apply = apply || strncmp(cfg->usb.manuf, cur->manuf, sizeof(cur->manuf));
  225. apply = apply || strncmp(cfg->usb.product, cur->product, sizeof(cur->product));
  226. }
  227. }
  228. if(apply) {
  229. bad_kb_conn_apply(app);
  230. }
  231. if(app->bad_kb_script) {
  232. BadKbScript* script = app->bad_kb_script;
  233. script->st.is_bt = app->is_bt;
  234. script->bt = app->is_bt ? app->bt : NULL;
  235. bool connected;
  236. if(app->is_bt) {
  237. bt_set_status_changed_callback(app->bt, bad_kb_bt_hid_state_callback, script);
  238. connected = furi_hal_bt_is_connected();
  239. } else {
  240. furi_hal_hid_set_state_callback(bad_kb_usb_hid_state_callback, script);
  241. connected = furi_hal_hid_is_connected();
  242. }
  243. if(connected) {
  244. furi_thread_flags_set(furi_thread_get_id(script->thread), WorkerEvtConnect);
  245. }
  246. }
  247. // Reload config page
  248. scene_manager_next_scene(app->scene_manager, BadKbSceneConfig);
  249. scene_manager_previous_scene(app->scene_manager);
  250. }
  251. BadKbApp* bad_kb_app_alloc(char* arg) {
  252. BadKbApp* app = malloc(sizeof(BadKbApp));
  253. app->bad_kb_script = NULL;
  254. app->file_path = furi_string_alloc();
  255. app->keyboard_layout = furi_string_alloc();
  256. if(arg && strlen(arg)) {
  257. furi_string_set(app->file_path, arg);
  258. }
  259. bad_kb_load_settings(app);
  260. app->gui = furi_record_open(RECORD_GUI);
  261. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  262. app->dialogs = furi_record_open(RECORD_DIALOGS);
  263. app->view_dispatcher = view_dispatcher_alloc();
  264. view_dispatcher_enable_queue(app->view_dispatcher);
  265. app->scene_manager = scene_manager_alloc(&bad_kb_scene_handlers, app);
  266. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  267. view_dispatcher_set_tick_event_callback(
  268. app->view_dispatcher, bad_kb_app_tick_event_callback, 250);
  269. view_dispatcher_set_custom_event_callback(
  270. app->view_dispatcher, bad_kb_app_custom_event_callback);
  271. view_dispatcher_set_navigation_event_callback(
  272. app->view_dispatcher, bad_kb_app_back_event_callback);
  273. Bt* bt = furi_record_open(RECORD_BT);
  274. app->bt = bt;
  275. app->bt->suppress_pin_screen = true;
  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. }