diff --git a/applications/main/bad_kb/bad_kb_app.c b/applications/main/bad_kb/bad_kb_app.c index 8b694cadd..2f5ee75ff 100644 --- a/applications/main/bad_kb/bad_kb_app.c +++ b/applications/main/bad_kb/bad_kb_app.c @@ -72,17 +72,6 @@ static void bad_kb_save_settings(BadKbApp* app) { storage_file_free(settings_file); } -void bad_kb_set_name(BadKbApp* app, const char* fmt, ...) { - furi_assert(app); - - va_list args; - va_start(args, fmt); - - vsnprintf(app->name, BAD_KB_ADV_NAME_MAX_LEN, fmt, args); - - va_end(args); -} - BadKbApp* bad_kb_app_alloc(char* arg) { BadKbApp* app = malloc(sizeof(BadKbApp)); @@ -113,6 +102,8 @@ BadKbApp* bad_kb_app_alloc(char* arg) { view_dispatcher_set_navigation_event_callback( app->view_dispatcher, bad_kb_app_back_event_callback); + app->connection_init = false; + Bt* bt = furi_record_open(RECORD_BT); app->bt = bt; app->is_bt = XTREME_SETTINGS()->bad_bt; @@ -158,6 +149,7 @@ BadKbApp* bad_kb_app_alloc(char* arg) { app->error = BadKbAppErrorCloseRpc; scene_manager_next_scene(app->scene_manager, BadKbSceneError); } else { + bad_kb_connection_init(app); if(!furi_string_empty(app->file_path)) { app->bad_kb_script = bad_kb_script_open(app->file_path, app->is_bt ? app->bt : NULL); bad_kb_script_set_keyboard_layout(app->bad_kb_script, app->keyboard_layout); @@ -179,6 +171,8 @@ void bad_kb_app_free(BadKbApp* app) { app->bad_kb_script = NULL; } + bad_kb_connection_deinit(app); + // Views view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewWork); bad_kb_free(app->bad_kb_view); @@ -205,10 +199,9 @@ void bad_kb_app_free(BadKbApp* app) { view_dispatcher_free(app->view_dispatcher); scene_manager_free(app->scene_manager); - // restores bt config - // BtProfile have already been switched to the previous one - // so we directly modify the right profile - bad_kb_connection_deinit(app->bt, true); + // Restore bt config + // BtProfile has already been switched to the previous one + // So we directly modify the right profile if(strcmp(app->bt_old_config.name, app->name) != 0) { furi_hal_bt_set_profile_adv_name(FuriHalBtProfileHidKeyboard, app->bt_old_config.name); } diff --git a/applications/main/bad_kb/bad_kb_app.h b/applications/main/bad_kb/bad_kb_app.h index e75a94651..af64be253 100644 --- a/applications/main/bad_kb/bad_kb_app.h +++ b/applications/main/bad_kb/bad_kb_app.h @@ -6,8 +6,6 @@ extern "C" { typedef struct BadKbApp BadKbApp; -void bad_kb_set_name(BadKbApp* app, const char* fmt, ...); - #ifdef __cplusplus } #endif diff --git a/applications/main/bad_kb/bad_kb_app_i.h b/applications/main/bad_kb/bad_kb_app_i.h index 38cae44c5..b16eb159d 100644 --- a/applications/main/bad_kb/bad_kb_app_i.h +++ b/applications/main/bad_kb/bad_kb_app_i.h @@ -68,6 +68,10 @@ struct BadKbApp { BadKbScript* bad_kb_script; bool is_bt; + + bool connection_init; + FuriHalUsbInterface* usb_prev_mode; + GapPairing bt_prev_mode; }; typedef enum { diff --git a/applications/main/bad_kb/bad_kb_script.c b/applications/main/bad_kb/bad_kb_script.c index 018dbb51b..d6cb6fae6 100644 --- a/applications/main/bad_kb/bad_kb_script.c +++ b/applications/main/bad_kb/bad_kb_script.c @@ -11,6 +11,8 @@ #include +#include "bad_kb_app_i.h" + #define HID_BT_KEYS_STORAGE_PATH EXT_PATH("apps/Tools/.bt_hid.keys") #define TAG "BadKB" @@ -164,10 +166,6 @@ static const uint8_t numpad_keys[10] = { HID_KEYPAD_9, }; -FuriHalUsbInterface* usb_mode_prev = NULL; -GapPairing bt_mode_prev = GapPairingNone; -bool usb_initialized = false; -bool bt_initialized = false; uint8_t bt_timeout = 0; static LevelRssiRange bt_remote_rssi_range(Bt* bt) { @@ -617,43 +615,55 @@ static void bad_kb_usb_hid_state_callback(bool state, void* context) { } } -void bad_kb_connection_init(Bt* bt) { - if(bt && !bt_initialized) { - bt_timeout = bt_hid_delays[LevelRssi39_0]; - bt_disconnect(bt); - furi_delay_ms(200); - bt_keys_storage_set_storage_path(bt, HID_BT_KEYS_STORAGE_PATH); - furi_assert(bt_set_profile(bt, BtProfileHidKeyboard)); - bt_mode_prev = bt_get_profile_pairing_method(bt); - bt_set_profile_pairing_method(bt, GapPairingNone); +void bad_kb_config_switch_mode(BadKbApp* app) { + bad_kb_script_close(app->bad_kb_script); + app->bad_kb_script = bad_kb_script_open(app->file_path, app->is_bt ? app->bt : NULL); + bad_kb_script_set_keyboard_layout(app->bad_kb_script, app->keyboard_layout); + scene_manager_previous_scene(app->scene_manager); + if(app->is_bt) { furi_hal_bt_start_advertising(); - // disable peer key adding to bt SRAM storage - bt_disable_peer_key_update(bt); - bt_initialized = true; - } - if(!bt && !usb_initialized) { - usb_mode_prev = furi_hal_usb_get_config(); - usb_initialized = true; + scene_manager_next_scene(app->scene_manager, BadKbSceneConfigBt); + } else { + furi_hal_bt_stop_advertising(); + scene_manager_next_scene(app->scene_manager, BadKbSceneConfigUsb); } } -void bad_kb_connection_deinit(Bt* bt, bool reset_bt) { - if(bt_initialized && reset_bt && bt) { - // bt_hid_hold_while_keyboard_buffer_full(6, 3000); // release all keys - bt_disconnect(bt); // stop ble - furi_delay_ms(200); // Wait 2nd core to update nvm storage - bt_keys_storage_set_default_path(bt); - bt_set_profile_pairing_method(bt, bt_mode_prev); - // fails if ble radio stack isn't ready when switching profile - // if it happens, maybe we should increase the delay after bt_disconnect - bt_set_profile(bt, BtProfileSerial); - bt_enable_peer_key_update(bt); // starts saving peer keys (bounded devices) - bt_initialized = false; - } - if(usb_initialized) { - furi_hal_usb_set_config(usb_mode_prev, NULL); - usb_initialized = false; +void bad_kb_connection_init(BadKbApp* app) { + app->usb_prev_mode = furi_hal_usb_get_config(); + furi_hal_usb_set_config(NULL, NULL); + + bt_timeout = bt_hid_delays[LevelRssi39_0]; + bt_disconnect(app->bt); + // furi_delay_ms(200); + bt_keys_storage_set_storage_path(app->bt, HID_BT_KEYS_STORAGE_PATH); + bt_set_profile(app->bt, BtProfileHidKeyboard); + app->bt_prev_mode = bt_get_profile_pairing_method(app->bt); + bt_set_profile_pairing_method(app->bt, GapPairingNone); + bt_disable_peer_key_update(app->bt); // disable peer key adding to bt SRAM storage + if(app->is_bt) { + furi_hal_bt_start_advertising(); + } else { + furi_hal_bt_stop_advertising(); } + + app->connection_init = true; +} + +void bad_kb_connection_deinit(BadKbApp* app) { + if(!app->connection_init) return; + + furi_hal_usb_set_config(app->usb_prev_mode, NULL); + + // bt_hid_hold_while_keyboard_buffer_full(6, 3000); // release all keys + bt_disconnect(app->bt); // stop ble + // furi_delay_ms(200); // Wait 2nd core to update nvm storage + bt_keys_storage_set_default_path(app->bt); + bt_set_profile_pairing_method(app->bt, app->bt_prev_mode); + // fails if ble radio stack isn't ready when switching profile + // if it happens, maybe we should increase the delay after bt_disconnect + bt_set_profile(app->bt, BtProfileSerial); + bt_enable_peer_key_update(app->bt); // starts saving peer keys (bounded devices) } static int32_t bad_kb_worker(void* context) { @@ -662,7 +672,6 @@ static int32_t bad_kb_worker(void* context) { BadKbWorkerState worker_state = BadKbStateInit; int32_t delay_val = 0; - bad_kb_connection_init(bad_kb->bt); if(bad_kb->bt) { bt_set_status_changed_callback(bad_kb->bt, bad_kb_bt_hid_state_callback, bad_kb); } else { @@ -848,7 +857,6 @@ static int32_t bad_kb_worker(void* context) { } else { furi_hal_hid_set_state_callback(NULL, NULL); } - bad_kb_connection_deinit(bad_kb->bt, false); storage_file_close(script_file); storage_file_free(script_file); diff --git a/applications/main/bad_kb/bad_kb_script.h b/applications/main/bad_kb/bad_kb_script.h index fde99556e..5699e4189 100644 --- a/applications/main/bad_kb/bad_kb_script.h +++ b/applications/main/bad_kb/bad_kb_script.h @@ -7,6 +7,8 @@ extern "C" { #include #include +typedef struct BadKbApp BadKbApp; + typedef struct BadKbScript BadKbScript; typedef enum { @@ -32,7 +34,11 @@ typedef struct { char error[64]; } BadKbState; -void bad_kb_connection_deinit(Bt* bt, bool reset_bt); +void bad_kb_config_switch_mode(BadKbApp* app); + +void bad_kb_connection_init(BadKbApp* app); + +void bad_kb_connection_deinit(BadKbApp* app); BadKbScript* bad_kb_script_open(FuriString* file_path, Bt* bt); diff --git a/applications/main/bad_kb/scenes/bad_kb_scene_config_bt.c b/applications/main/bad_kb/scenes/bad_kb_scene_config_bt.c index b3b0ec2bb..1a867f877 100644 --- a/applications/main/bad_kb/scenes/bad_kb_scene_config_bt.c +++ b/applications/main/bad_kb/scenes/bad_kb_scene_config_bt.c @@ -56,23 +56,11 @@ bool bad_kb_scene_config_bt_on_event(void* context, SceneManagerEvent event) { if(event.event == VarItemListIndexKeyboardLayout) { scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigLayout); } else if(event.event == VarItemListIndexConnection) { - bad_kb_script_close(bad_kb->bad_kb_script); - bad_kb_connection_deinit(bad_kb->bt, true); - bad_kb->bad_kb_script = - bad_kb_script_open(bad_kb->file_path, bad_kb->is_bt ? bad_kb->bt : NULL); - bad_kb_script_set_keyboard_layout(bad_kb->bad_kb_script, bad_kb->keyboard_layout); - scene_manager_previous_scene(bad_kb->scene_manager); - if(bad_kb->is_bt) { - scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigBt); - } else { - scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigUsb); - } + bad_kb_config_switch_mode(bad_kb); } else if(event.event == VarItemListIndexAdvertisementName) { scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigName); } else if(event.event == VarItemListIndexMacAddress) { scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigMac); - // } else { - // furi_crash("Unknown key type"); } } diff --git a/applications/main/bad_kb/scenes/bad_kb_scene_config_usb.c b/applications/main/bad_kb/scenes/bad_kb_scene_config_usb.c index 2fa264e7c..d3ff35fc8 100644 --- a/applications/main/bad_kb/scenes/bad_kb_scene_config_usb.c +++ b/applications/main/bad_kb/scenes/bad_kb_scene_config_usb.c @@ -50,19 +50,7 @@ bool bad_kb_scene_config_usb_on_event(void* context, SceneManagerEvent event) { if(event.event == VarItemListIndexKeyboardLayout) { scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigLayout); } else if(event.event == VarItemListIndexConnection) { - bad_kb_script_close(bad_kb->bad_kb_script); - bad_kb_connection_deinit(bad_kb->bt, true); - bad_kb->bad_kb_script = - bad_kb_script_open(bad_kb->file_path, bad_kb->is_bt ? bad_kb->bt : NULL); - bad_kb_script_set_keyboard_layout(bad_kb->bad_kb_script, bad_kb->keyboard_layout); - scene_manager_previous_scene(bad_kb->scene_manager); - if(bad_kb->is_bt) { - scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigBt); - } else { - scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigUsb); - } - // } else { - // furi_crash("Unknown key type"); + bad_kb_config_switch_mode(bad_kb); } } diff --git a/applications/main/bad_kb/scenes/bad_kb_scene_file_select.c b/applications/main/bad_kb/scenes/bad_kb_scene_file_select.c index bb2ddf6ca..ba238f390 100644 --- a/applications/main/bad_kb/scenes/bad_kb_scene_file_select.c +++ b/applications/main/bad_kb/scenes/bad_kb_scene_file_select.c @@ -22,7 +22,6 @@ static bool bad_kb_file_select(BadKbApp* bad_kb) { void bad_kb_scene_file_select_on_enter(void* context) { BadKbApp* bad_kb = context; - furi_hal_usb_disable(); if(bad_kb->bad_kb_script) { bad_kb_script_close(bad_kb->bad_kb_script); bad_kb->bad_kb_script = NULL; @@ -35,7 +34,6 @@ void bad_kb_scene_file_select_on_enter(void* context) { scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneWork); } else { - furi_hal_usb_enable(); view_dispatcher_stop(bad_kb->view_dispatcher); } }