Skip to content

Commit

Permalink
BadKB: Improve BT Remember handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Apr 17, 2024
1 parent 053040e commit a19598c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
27 changes: 14 additions & 13 deletions applications/main/bad_kb/bad_kb_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}
Expand Down
1 change: 0 additions & 1 deletion applications/main/bad_kb/bad_kb_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions applications/main/bad_kb/helpers/ducky_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
20 changes: 14 additions & 6 deletions applications/main/bad_kb/scenes/bad_kb_scene_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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 {
Expand Down

0 comments on commit a19598c

Please sign in to comment.