bad_bt_app.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "bad_bt_app.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 <bt/bt_service/bt.h>
  9. #define BAD_BT_SETTINGS_FILE_NAME ".badbt.settings"
  10. #define BAD_BT_APP_PATH_BOUND_KEYS_FOLDER EXT_PATH("badbt")
  11. #define BAD_BT_APP_PATH_BOUND_KEYS_FILE BAD_BT_APP_PATH_BOUND_KEYS_FOLDER "/.badbt.keys"
  12. #define BAD_BT_SETTINGS_PATH BAD_BT_APP_BASE_CONFIG_FOLDER "/" BAD_BT_SETTINGS_FILE_NAME
  13. static bool bad_bt_app_custom_event_callback(void* context, uint32_t event) {
  14. furi_assert(context);
  15. BadBtApp* app = context;
  16. return scene_manager_handle_custom_event(app->scene_manager, event);
  17. }
  18. static bool bad_bt_app_back_event_callback(void* context) {
  19. furi_assert(context);
  20. BadBtApp* app = context;
  21. return scene_manager_handle_back_event(app->scene_manager);
  22. }
  23. static void bad_bt_app_tick_event_callback(void* context) {
  24. furi_assert(context);
  25. BadBtApp* app = context;
  26. scene_manager_handle_tick_event(app->scene_manager);
  27. }
  28. static void bad_bt_load_settings(BadBtApp* app) {
  29. furi_string_reset(app->keyboard_layout);
  30. strcpy(app->config.bt_name, "");
  31. memcpy(
  32. app->config.bt_mac,
  33. furi_hal_bt_get_profile_mac_addr(FuriHalBtProfileHidKeyboard),
  34. BAD_BT_MAC_ADDRESS_LEN);
  35. Storage* storage = furi_record_open(RECORD_STORAGE);
  36. FlipperFormat* file = flipper_format_file_alloc(storage);
  37. if(flipper_format_file_open_existing(file, BAD_BT_SETTINGS_PATH)) {
  38. FuriString* tmp_str = furi_string_alloc();
  39. if(!flipper_format_read_string(file, "Keyboard_Layout", app->keyboard_layout)) {
  40. furi_string_reset(app->keyboard_layout);
  41. }
  42. if(!flipper_format_read_bool(file, "BT_Remember", &(app->bt_remember), 1)) {
  43. app->bt_remember = false;
  44. }
  45. if(flipper_format_read_string(file, "Bt_Name", tmp_str) && !furi_string_empty(tmp_str)) {
  46. strcpy(app->config.bt_name, furi_string_get_cstr(tmp_str));
  47. } else {
  48. strcpy(app->config.bt_name, "");
  49. }
  50. if(!flipper_format_read_hex(
  51. file, "Bt_Mac", (uint8_t*)&app->config.bt_mac, BAD_BT_MAC_ADDRESS_LEN)) {
  52. memcpy(
  53. app->config.bt_mac,
  54. furi_hal_bt_get_profile_mac_addr(FuriHalBtProfileHidKeyboard),
  55. BAD_BT_MAC_ADDRESS_LEN);
  56. }
  57. furi_string_free(tmp_str);
  58. flipper_format_file_close(file);
  59. }
  60. flipper_format_free(file);
  61. if(!furi_string_empty(app->keyboard_layout)) {
  62. FileInfo layout_file_info;
  63. FS_Error file_check_err = storage_common_stat(
  64. storage, furi_string_get_cstr(app->keyboard_layout), &layout_file_info);
  65. if(file_check_err != FSE_OK) {
  66. furi_string_reset(app->keyboard_layout);
  67. return;
  68. }
  69. if(layout_file_info.size != 256) {
  70. furi_string_reset(app->keyboard_layout);
  71. }
  72. }
  73. furi_record_close(RECORD_STORAGE);
  74. }
  75. static void bad_bt_save_settings(BadBtApp* app) {
  76. Storage* storage = furi_record_open(RECORD_STORAGE);
  77. FlipperFormat* file = flipper_format_file_alloc(storage);
  78. if(flipper_format_file_open_always(file, BAD_BT_SETTINGS_PATH)) {
  79. flipper_format_write_string(file, "Keyboard_Layout", app->keyboard_layout);
  80. flipper_format_write_bool(file, "BT_Remember", &(app->bt_remember), 1);
  81. flipper_format_write_string_cstr(file, "Bt_Name", app->config.bt_name);
  82. flipper_format_write_hex(
  83. file, "Bt_Mac", (uint8_t*)&app->config.bt_mac, BAD_BT_MAC_ADDRESS_LEN);
  84. flipper_format_file_close(file);
  85. }
  86. flipper_format_free(file);
  87. furi_record_close(RECORD_STORAGE);
  88. }
  89. void bad_bt_reload_worker(BadBtApp* app) {
  90. bad_bt_script_close(app->bad_bt_script);
  91. app->bad_bt_script = bad_bt_script_open(app->file_path, app->bt, app);
  92. bad_bt_script_set_keyboard_layout(app->bad_bt_script, app->keyboard_layout);
  93. }
  94. void bad_kb_config_refresh_menu(BadBtApp* app) {
  95. scene_manager_next_scene(app->scene_manager, BadBtSceneConfig);
  96. scene_manager_previous_scene(app->scene_manager);
  97. }
  98. int32_t bad_bt_config_switch_mode(BadBtApp* app) {
  99. bad_bt_reload_worker(app);
  100. furi_hal_bt_start_advertising();
  101. bad_kb_config_refresh_menu(app);
  102. return 0;
  103. }
  104. void bad_bt_config_switch_remember_mode(BadBtApp* app) {
  105. if(app->bt_remember) {
  106. furi_hal_bt_set_profile_pairing_method(
  107. FuriHalBtProfileHidKeyboard, GapPairingPinCodeVerifyYesNo);
  108. bt_set_profile_mac_address(app->bt, (uint8_t*)&BAD_BT_BOUND_MAC_ADDRESS);
  109. bt_enable_peer_key_update(app->bt);
  110. } else {
  111. furi_hal_bt_set_profile_pairing_method(FuriHalBtProfileHidKeyboard, GapPairingNone);
  112. bt_set_profile_mac_address(app->bt, app->config.bt_mac);
  113. bt_disable_peer_key_update(app->bt);
  114. }
  115. bad_bt_reload_worker(app);
  116. }
  117. int32_t bad_bt_connection_init(BadBtApp* app) {
  118. // Set original name and mac address in prev config
  119. strcpy(
  120. app->prev_config.bt_name, furi_hal_bt_get_profile_adv_name(FuriHalBtProfileHidKeyboard));
  121. memcpy(app->prev_config.bt_mac, furi_hal_version_get_ble_mac(), BAD_BT_MAC_ADDRESS_LEN);
  122. bt_timeout = bt_hid_delays[LevelRssi39_0];
  123. bt_disconnect(app->bt);
  124. // Wait 2nd core to update nvm storage
  125. furi_delay_ms(200);
  126. bt_keys_storage_set_storage_path(app->bt, BAD_BT_APP_PATH_BOUND_KEYS_FILE);
  127. if(strcmp(app->config.bt_name, "") != 0) {
  128. furi_hal_bt_set_profile_adv_name(FuriHalBtProfileHidKeyboard, app->config.bt_name);
  129. }
  130. if(app->bt_remember) {
  131. furi_hal_bt_set_profile_mac_addr(
  132. FuriHalBtProfileHidKeyboard, (uint8_t*)&BAD_BT_BOUND_MAC_ADDRESS);
  133. furi_hal_bt_set_profile_pairing_method(
  134. FuriHalBtProfileHidKeyboard, GapPairingPinCodeVerifyYesNo);
  135. } else {
  136. if(memcmp(
  137. app->config.bt_mac, (uint8_t*)&BAD_BT_EMPTY_MAC_ADDRESS, BAD_BT_MAC_ADDRESS_LEN) !=
  138. 0) {
  139. furi_hal_bt_set_profile_mac_addr(FuriHalBtProfileHidKeyboard, app->config.bt_mac);
  140. }
  141. furi_hal_bt_set_profile_pairing_method(FuriHalBtProfileHidKeyboard, GapPairingNone);
  142. }
  143. bt_set_profile(app->bt, BtProfileHidKeyboard);
  144. if(strcmp(app->config.bt_name, "") == 0) {
  145. strcpy(app->config.bt_name, furi_hal_bt_get_profile_adv_name(FuriHalBtProfileHidKeyboard));
  146. }
  147. if(memcmp(app->config.bt_mac, (uint8_t*)&BAD_BT_EMPTY_MAC_ADDRESS, BAD_BT_MAC_ADDRESS_LEN) ==
  148. 0) {
  149. memcpy(
  150. app->config.bt_mac,
  151. furi_hal_bt_get_profile_mac_addr(FuriHalBtProfileHidKeyboard),
  152. BAD_BT_MAC_ADDRESS_LEN);
  153. }
  154. furi_hal_bt_start_advertising();
  155. if(app->bt_remember) {
  156. bt_enable_peer_key_update(app->bt);
  157. } else {
  158. bt_disable_peer_key_update(app->bt);
  159. }
  160. return 0;
  161. }
  162. void bad_bt_connection_deinit(BadBtApp* app) {
  163. bt_disconnect(app->bt);
  164. // Wait 2nd core to update nvm storage
  165. furi_delay_ms(200);
  166. bt_keys_storage_set_default_path(app->bt);
  167. furi_hal_bt_set_profile_adv_name(FuriHalBtProfileHidKeyboard, app->prev_config.bt_name);
  168. furi_hal_bt_set_profile_mac_addr(FuriHalBtProfileHidKeyboard, app->prev_config.bt_mac);
  169. furi_hal_bt_set_profile_pairing_method(
  170. FuriHalBtProfileHidKeyboard, GapPairingPinCodeVerifyYesNo);
  171. bt_set_profile(app->bt, BtProfileSerial);
  172. bt_enable_peer_key_update(app->bt);
  173. }
  174. BadBtApp* bad_bt_app_alloc(char* arg) {
  175. BadBtApp* app = malloc(sizeof(BadBtApp));
  176. app->bad_bt_script = NULL;
  177. app->file_path = furi_string_alloc();
  178. app->keyboard_layout = furi_string_alloc();
  179. if(arg && strlen(arg)) {
  180. furi_string_set(app->file_path, arg);
  181. }
  182. Storage* storage = furi_record_open(RECORD_STORAGE);
  183. storage_simply_mkdir(storage, BAD_BT_APP_BASE_CONFIG_FOLDER);
  184. furi_record_close(RECORD_STORAGE);
  185. bad_bt_load_settings(app);
  186. app->gui = furi_record_open(RECORD_GUI);
  187. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  188. app->dialogs = furi_record_open(RECORD_DIALOGS);
  189. app->view_dispatcher = view_dispatcher_alloc();
  190. view_dispatcher_enable_queue(app->view_dispatcher);
  191. app->scene_manager = scene_manager_alloc(&bad_bt_scene_handlers, app);
  192. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  193. view_dispatcher_set_tick_event_callback(
  194. app->view_dispatcher, bad_bt_app_tick_event_callback, 500);
  195. view_dispatcher_set_custom_event_callback(
  196. app->view_dispatcher, bad_bt_app_custom_event_callback);
  197. view_dispatcher_set_navigation_event_callback(
  198. app->view_dispatcher, bad_bt_app_back_event_callback);
  199. Bt* bt = furi_record_open(RECORD_BT);
  200. app->bt = bt;
  201. app->bt->suppress_pin_screen = true;
  202. // Custom Widget
  203. app->widget = widget_alloc();
  204. view_dispatcher_add_view(
  205. app->view_dispatcher, BadBtAppViewError, widget_get_view(app->widget));
  206. app->var_item_list = variable_item_list_alloc();
  207. view_dispatcher_add_view(
  208. app->view_dispatcher, BadBtAppViewConfig, variable_item_list_get_view(app->var_item_list));
  209. app->bad_bt_view = bad_bt_alloc();
  210. view_dispatcher_add_view(
  211. app->view_dispatcher, BadBtAppViewWork, bad_bt_get_view(app->bad_bt_view));
  212. app->text_input = text_input_alloc();
  213. view_dispatcher_add_view(
  214. app->view_dispatcher, BadBtAppViewConfigName, text_input_get_view(app->text_input));
  215. app->byte_input = byte_input_alloc();
  216. view_dispatcher_add_view(
  217. app->view_dispatcher, BadBtAppViewConfigMac, byte_input_get_view(app->byte_input));
  218. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  219. app->conn_init_thread = furi_thread_alloc_ex(
  220. "BadBtConnInit", 1024, (FuriThreadCallback)bad_bt_connection_init, app);
  221. furi_thread_start(app->conn_init_thread);
  222. if(!furi_string_empty(app->file_path)) {
  223. app->bad_bt_script = bad_bt_script_open(app->file_path, app->bt, app);
  224. bad_bt_script_set_keyboard_layout(app->bad_bt_script, app->keyboard_layout);
  225. scene_manager_next_scene(app->scene_manager, BadBtSceneWork);
  226. } else {
  227. furi_string_set(app->file_path, BAD_BT_APP_BASE_FOLDER);
  228. scene_manager_next_scene(app->scene_manager, BadBtSceneFileSelect);
  229. }
  230. return app;
  231. }
  232. void bad_bt_app_free(BadBtApp* app) {
  233. furi_assert(app);
  234. if(app->bad_bt_script) {
  235. bad_bt_script_close(app->bad_bt_script);
  236. app->bad_bt_script = NULL;
  237. }
  238. // Views
  239. view_dispatcher_remove_view(app->view_dispatcher, BadBtAppViewWork);
  240. bad_bt_free(app->bad_bt_view);
  241. // Custom Widget
  242. view_dispatcher_remove_view(app->view_dispatcher, BadBtAppViewError);
  243. widget_free(app->widget);
  244. // Variable item list
  245. view_dispatcher_remove_view(app->view_dispatcher, BadBtAppViewConfig);
  246. variable_item_list_free(app->var_item_list);
  247. // Text Input
  248. view_dispatcher_remove_view(app->view_dispatcher, BadBtAppViewConfigName);
  249. text_input_free(app->text_input);
  250. // Byte Input
  251. view_dispatcher_remove_view(app->view_dispatcher, BadBtAppViewConfigMac);
  252. byte_input_free(app->byte_input);
  253. // View dispatcher
  254. view_dispatcher_free(app->view_dispatcher);
  255. scene_manager_free(app->scene_manager);
  256. // Restore bt config
  257. app->bt->suppress_pin_screen = false;
  258. if(app->conn_init_thread) {
  259. furi_thread_join(app->conn_init_thread);
  260. furi_thread_free(app->conn_init_thread);
  261. bad_bt_connection_deinit(app);
  262. }
  263. // Close records
  264. furi_record_close(RECORD_GUI);
  265. furi_record_close(RECORD_NOTIFICATION);
  266. furi_record_close(RECORD_DIALOGS);
  267. furi_record_close(RECORD_BT);
  268. bad_bt_save_settings(app);
  269. furi_string_free(app->file_path);
  270. furi_string_free(app->keyboard_layout);
  271. free(app);
  272. }
  273. int32_t bad_bt_app(void* p) {
  274. BadBtApp* bad_bt_app = bad_bt_app_alloc((char*)p);
  275. view_dispatcher_run(bad_bt_app->view_dispatcher);
  276. bad_bt_app_free(bad_bt_app);
  277. return 0;
  278. }