Skip to content

Commit

Permalink
upd badkb
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Apr 18, 2024
1 parent 88a32ff commit a80208f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
40 changes: 25 additions & 15 deletions base_pack/bad_kb/bad_kb_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void bad_kb_load_settings(BadKbApp* app) {
FlipperFormat* file = flipper_format_file_alloc(storage);
if(flipper_format_file_open_existing(file, BAD_KB_SETTINGS_PATH)) {
FuriString* tmp_str = furi_string_alloc();
uint32_t tmp_uint = 0;

if(!flipper_format_read_string(file, "Keyboard_Layout", app->keyboard_layout)) {
furi_string_reset(app->keyboard_layout);
Expand All @@ -47,11 +48,17 @@ 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);
}

if(!flipper_format_read_uint32(file, "Bt_Pairing", &tmp_uint, 1)) {
tmp_uint = GapPairingNone;
flipper_format_rewind(file);
}
cfg->ble.pairing = tmp_uint;

if(flipper_format_read_string(file, "Bt_Name", tmp_str)) {
strlcpy(cfg->ble.name, furi_string_get_cstr(tmp_str), sizeof(cfg->ble.name));
} else {
Expand Down Expand Up @@ -115,9 +122,12 @@ static void bad_kb_save_settings(BadKbApp* app) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
if(flipper_format_file_open_always(file, BAD_KB_SETTINGS_PATH)) {
uint32_t tmp_uint = 0;
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);
tmp_uint = cfg->ble.pairing;
flipper_format_write_uint32(file, "Bt_Pairing", &tmp_uint, 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,22 +153,21 @@ 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;
if(app->bt_remember) {
app->cur_ble_cfg.pairing = GapPairingPinCodeVerifyYesNo;
} else {
app->cur_ble_cfg.pairing = GapPairingNone;
if(app->cur_ble_cfg.bonding) {
// 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));
}

// 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 @@ -246,7 +255,8 @@ 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 || cfg->ble.pairing != cur->pairing;
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 base_pack/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
4 changes: 4 additions & 0 deletions base_pack/bad_kb/helpers/ducky_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ 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 and pairing via BT_ID, sync with user choice instead
cfg->ble.bonding = bad_kb->app->config.ble.bonding;
cfg->ble.pairing = bad_kb->app->config.ble.pairing;
return true;
}

Expand Down
51 changes: 45 additions & 6 deletions base_pack/bad_kb/scenes/bad_kb_scene_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum VarItemListIndex {

enum VarItemListIndexBt {
VarItemListIndexBtRemember = VarItemListIndexConnection + 1,
VarItemListIndexBtPairing,
VarItemListIndexBtDeviceName,
VarItemListIndexBtMacAddress,
VarItemListIndexBtRandomizeMac,
Expand All @@ -29,11 +30,35 @@ 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);
}

const char* const bt_pairing_names[GapPairingCount] = {
"YesNo",
"PIN Type",
"PIN Y/N",
};
void bad_kb_scene_config_bt_pairing_callback(VariableItem* item) {
BadKbApp* bad_kb = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
// Set user config and remember
bad_kb->config.ble.pairing = index;
// Apply to ID config so its temporarily overridden (currently can't set pairing with BT_ID anyway)
if(bad_kb->set_bt_id) {
bad_kb->id_config.ble.pairing = index;
}
variable_item_set_current_value_text(item, bt_pairing_names[index]);
view_dispatcher_send_custom_event(bad_kb->view_dispatcher, VarItemListIndexBtPairing);
}

void bad_kb_scene_config_var_item_list_callback(void* context, uint32_t index) {
BadKbApp* bad_kb = context;
view_dispatcher_send_custom_event(bad_kb->view_dispatcher, index);
Expand All @@ -52,20 +77,31 @@ 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 Pairing",
GapPairingCount,
bad_kb_scene_config_bt_pairing_callback,
bad_kb);
variable_item_set_current_value_index(item, cfg->ble.pairing);
variable_item_set_current_value_text(item, bt_pairing_names[cfg->ble.pairing]);

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 Expand Up @@ -109,6 +145,9 @@ bool bad_kb_scene_config_on_event(void* context, SceneManagerEvent event) {
case VarItemListIndexBtRemember:
bad_kb_config_refresh(bad_kb);
break;
case VarItemListIndexBtPairing:
bad_kb_config_refresh(bad_kb);
break;
case VarItemListIndexBtDeviceName:
scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigBtName);
break;
Expand Down

0 comments on commit a80208f

Please sign in to comment.