From a19598c44daff27ae91203156210757b1386a7a8 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Wed, 17 Apr 2024 02:45:43 +0100 Subject: [PATCH] BadKB: Improve BT Remember handling --- applications/main/bad_kb/bad_kb_app.c | 27 ++++++++++--------- applications/main/bad_kb/bad_kb_app_i.h | 1 - .../main/bad_kb/helpers/ducky_script.c | 3 +++ .../main/bad_kb/scenes/bad_kb_scene_config.c | 20 +++++++++----- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/applications/main/bad_kb/bad_kb_app.c b/applications/main/bad_kb/bad_kb_app.c index 5b2e86b2e..be9fc020f 100644 --- a/applications/main/bad_kb/bad_kb_app.c +++ b/applications/main/bad_kb/bad_kb_app.c @@ -47,8 +47,8 @@ void bad_kb_load_settings(BadKbApp* app) { flipper_format_rewind(file); } - if(!flipper_format_read_bool(file, "Bt_Remember", &app->bt_remember, 1)) { - app->bt_remember = false; + if(!flipper_format_read_bool(file, "Bt_Remember", &cfg->ble.bonding, 1)) { + cfg->ble.bonding = false; flipper_format_rewind(file); } @@ -117,7 +117,7 @@ static void bad_kb_save_settings(BadKbApp* app) { if(flipper_format_file_open_always(file, BAD_KB_SETTINGS_PATH)) { flipper_format_write_string(file, "Keyboard_Layout", app->keyboard_layout); flipper_format_write_bool(file, "Is_Bt", &app->is_bt, 1); - flipper_format_write_bool(file, "Bt_Remember", &app->bt_remember, 1); + flipper_format_write_bool(file, "Bt_Remember", &cfg->ble.bonding, 1); flipper_format_write_string_cstr(file, "Bt_Name", cfg->ble.name); flipper_format_write_hex(file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac)); flipper_format_write_string_cstr(file, "Usb_Manuf", cfg->usb.manuf); @@ -143,23 +143,24 @@ void bad_kb_app_show_loading_popup(BadKbApp* app, bool show) { int32_t bad_kb_conn_apply(BadKbApp* app) { if(app->is_bt) { - bt_timeout = bt_hid_delays[LevelRssi39_0]; - bt_disconnect(app->bt); - furi_delay_ms(200); - bt_keys_storage_set_storage_path(app->bt, BAD_KB_KEYS_PATH); - - // Setup new config + // Setup profile config BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config; memcpy(&app->cur_ble_cfg, &cfg->ble, sizeof(cfg->ble)); - app->cur_ble_cfg.bonding = app->bt_remember; - // TODO: Allow GapPairingNone with BT Remember ON - if(app->bt_remember) { + if(app->cur_ble_cfg.bonding) { app->cur_ble_cfg.pairing = GapPairingPinCodeVerifyYesNo; + // Hardcode mac for remember mode + // Change in config copy to preserve user choice for non-remember mode memcpy(app->cur_ble_cfg.mac, BAD_KB_BOUND_MAC, sizeof(BAD_KB_BOUND_MAC)); } else { app->cur_ble_cfg.pairing = GapPairingNone; } + // Prepare for new profile + bt_timeout = bt_hid_delays[LevelRssi39_0]; + bt_disconnect(app->bt); + furi_delay_ms(200); + bt_keys_storage_set_storage_path(app->bt, BAD_KB_KEYS_PATH); + // Set profile app->ble_hid = bt_profile_start(app->bt, ble_profile_hid, &app->cur_ble_cfg); furi_check(app->ble_hid); @@ -247,7 +248,7 @@ void bad_kb_config_refresh(BadKbApp* app) { bad_kb_conn_reset(app); } else { BleProfileHidParams* cur = &app->cur_ble_cfg; - apply = apply || cfg->ble.bonding != app->bt_remember; + apply = apply || cfg->ble.bonding != cur->bonding; apply = apply || strncmp(cfg->ble.name, cur->name, sizeof(cfg->ble.name)); apply = apply || memcmp(cfg->ble.mac, cur->mac, sizeof(cfg->ble.mac)); } diff --git a/applications/main/bad_kb/bad_kb_app_i.h b/applications/main/bad_kb/bad_kb_app_i.h index e009f0e60..a7d3844ac 100644 --- a/applications/main/bad_kb/bad_kb_app_i.h +++ b/applications/main/bad_kb/bad_kb_app_i.h @@ -72,7 +72,6 @@ struct BadKbApp { Bt* bt; bool is_bt; - bool bt_remember; BadKbConfig config; // User options BadKbConfig id_config; // ID and BT_ID values diff --git a/applications/main/bad_kb/helpers/ducky_script.c b/applications/main/bad_kb/helpers/ducky_script.c index 4e7e48e22..70aec93af 100644 --- a/applications/main/bad_kb/helpers/ducky_script.c +++ b/applications/main/bad_kb/helpers/ducky_script.c @@ -344,6 +344,9 @@ static bool ducky_set_bt_id(BadKbScript* bad_kb, const char* line) { strlcpy(cfg->ble.name, line + mac_len, sizeof(cfg->ble.name)); FURI_LOG_D(WORKER_TAG, "set bt id: %s", line); + + // Can't set bonding via BT_ID, sync with user choice instead + cfg->ble.bonding = bad_kb->app->config.ble.bonding; return true; } diff --git a/applications/main/bad_kb/scenes/bad_kb_scene_config.c b/applications/main/bad_kb/scenes/bad_kb_scene_config.c index cc7cefdc0..d12863a7c 100644 --- a/applications/main/bad_kb/scenes/bad_kb_scene_config.c +++ b/applications/main/bad_kb/scenes/bad_kb_scene_config.c @@ -29,8 +29,14 @@ void bad_kb_scene_config_connection_callback(VariableItem* item) { void bad_kb_scene_config_bt_remember_callback(VariableItem* item) { BadKbApp* bad_kb = variable_item_get_context(item); - bad_kb->bt_remember = variable_item_get_current_value_index(item); - variable_item_set_current_value_text(item, bad_kb->bt_remember ? "ON" : "OFF"); + bool value = variable_item_get_current_value_index(item); + // Set user config and remember + bad_kb->config.ble.bonding = value; + // Apply to ID config so its temporarily overridden (currently can't set bonding with BT_ID anyway) + if(bad_kb->set_bt_id) { + bad_kb->id_config.ble.bonding = value; + } + variable_item_set_current_value_text(item, value ? "ON" : "OFF"); view_dispatcher_send_custom_event(bad_kb->view_dispatcher, VarItemListIndexBtRemember); } @@ -52,20 +58,22 @@ void bad_kb_scene_config_on_enter(void* context) { variable_item_set_current_value_text(item, bad_kb->is_bt ? "BT" : "USB"); if(bad_kb->is_bt) { + BadKbConfig* cfg = bad_kb->set_bt_id ? &bad_kb->id_config : &bad_kb->config; + item = variable_item_list_add( var_item_list, "BT Remember", 2, bad_kb_scene_config_bt_remember_callback, bad_kb); - variable_item_set_current_value_index(item, bad_kb->bt_remember); - variable_item_set_current_value_text(item, bad_kb->bt_remember ? "ON" : "OFF"); + variable_item_set_current_value_index(item, cfg->ble.bonding); + variable_item_set_current_value_text(item, cfg->ble.bonding ? "ON" : "OFF"); item = variable_item_list_add(var_item_list, "BT Device Name", 0, NULL, bad_kb); item = variable_item_list_add(var_item_list, "BT MAC Address", 0, NULL, bad_kb); - if(bad_kb->bt_remember) { + if(cfg->ble.bonding) { variable_item_set_locked(item, true, "Remember\nmust be Off!"); } item = variable_item_list_add(var_item_list, "Randomize BT MAC", 0, NULL, bad_kb); - if(bad_kb->bt_remember) { + if(cfg->ble.bonding) { variable_item_set_locked(item, true, "Remember\nmust be Off!"); } } else {