Skip to content

Commit

Permalink
Merge pull request #1082 from UltimateHackingKeyboard/add_delete_all_…
Browse files Browse the repository at this point in the history
…data_command

Delete all settings
  • Loading branch information
kareltucek authored Jan 11, 2025
2 parents a099b0b + eda4ffe commit 28bd585
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
72 changes: 64 additions & 8 deletions device/src/settings.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#include <zephyr/bluetooth/addr.h>
#include <zephyr/settings/settings.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/device.h>
#include <string.h>
#include <stdio.h>
#include "bt_conn.h"
#include "dongle_leds.h"
#include "stubs.h"

bool RightAddressIsSet = false;

static void setRightAddressIsSet(bool isSet)
{
static void setRightAddressIsSet(bool isSet) {
if (RightAddressIsSet != isSet) {
RightAddressIsSet = isSet;
DongleLeds_Update();
}
}

// (This isn't getting called at all when there is no "uhk/addr" settings present.)
static int peerAddressSet(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg)
{
static int peerAddressSet(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) {
static char foo_val[BT_ADDR_SIZE];
read_cb(cb_arg, &foo_val, len);

Expand Down Expand Up @@ -45,16 +47,70 @@ struct settings_handler settingsHandler = {
.h_set = peerAddressSet,
};

void InitSettings(void)
{
void InitSettings(void) {
DongleLeds_Update();
settings_subsys_init();
settings_register(&settingsHandler);
settings_load();
}

void Settings_Reload(void)
{
void Settings_Reload(void) {
setRightAddressIsSet(false);
settings_load();
}

static bool shouldDeleteKey(const char *key) {
const char *protectedKey = NULL;
const bt_addr_le_t *protectedAddr = NULL;
#if DEVICE_IS_UHK80_LEFT
protectedKey = "uhk/addr/right";
protectedAddr = &Peers[PeerIdRight].addr;
#elif DEVICE_IS_UHK80_RIGHT
protectedKey = "uhk/addr/left";
protectedAddr = &Peers[PeerIdLeft].addr;
#endif

if (protectedAddr == NULL || protectedKey == NULL) {
return true;
}

if (strcmp(key, protectedKey) == 0) {
return false;
}

// 2*6 bytes + type flag + null terminator
const uint8_t addrStrLen = 14;
char addrString[addrStrLen];

// print addr to string without colons in hex using sprintf
for (uint8_t i = 0; i < BLE_ADDR_LEN; i++) {
sprintf(&addrString[i*2], "%02x", protectedAddr->a.val[BLE_ADDR_LEN-1-i]);
}
addrString[addrStrLen-2] = protectedAddr->type ? '1' : '0';
addrString[addrStrLen-1] = '\0';

const char* keyAddr = key + strlen(key) + 1 - addrStrLen;

bool keyIsProtected = strncmp(keyAddr, addrString, addrStrLen) == 0;

printk("Matched key: %s, addrString: %s ? %d\n", keyAddr, addrString, keyIsProtected);

return !keyIsProtected;
}

static int delete_handler(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg, void *param) {
if (shouldDeleteKey(key)) {
settings_delete(key);
}
return 0;
}

void Settings_Erase(void) {
settings_load();
settings_load_subtree_direct(NULL, delete_handler, NULL);
settings_save();

printk("Settings: Erased all settings\n");

Settings_Reload();
}
1 change: 1 addition & 0 deletions device/src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

void InitSettings(void);
void Settings_Reload(void);
void Settings_Erase(void);

// Variables:

Expand Down
1 change: 1 addition & 0 deletions right/src/usb_commands/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
target_sources(${PROJECT_NAME} PRIVATE
usb_command_apply_config.c
usb_command_draw_oled.c
usb_command_erase_ble_settings.c
usb_command_exec_macro_command.c
#usb_command_get_adc_value.c
usb_command_get_debug_buffer.c
Expand Down
11 changes: 11 additions & 0 deletions right/src/usb_commands/usb_command_erase_ble_settings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifdef __ZEPHYR__

#include "settings.h"
#include "usb_command_erase_ble_settings.h"

void UsbCommand_EraseAllSettings(void) {
Settings_Erase();
}

#endif

14 changes: 14 additions & 0 deletions right/src/usb_commands/usb_command_erase_ble_settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __USB_COMMAND_ERASE_BLE_SETTINGS_H__
#define __USB_COMMAND_ERASE_BLE_SETTINGS_H__

// Includes:

// Includes:

// Typedefs:

// Functions:

void UsbCommand_EraseAllSettings(void);

#endif
10 changes: 10 additions & 0 deletions right/src/usb_protocol_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifdef __ZEPHYR__
#include "usb_commands/usb_command_draw_oled.h"
#include "usb_commands/usb_command_pairing.h"
#include "usb_commands/usb_command_erase_ble_settings.h"
#include "bt_conn.h"
#else
#include "usb_commands/usb_command_set_test_led.h"
Expand Down Expand Up @@ -106,6 +107,9 @@ void UsbProtocolHandler(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidI
case UsbCommandId_EnterPairingMode:
UsbCommand_EnterPairingMode(GenericHidOutBuffer, GenericHidInBuffer);
break;
case UsbCommandId_EraseBleSettings:
UsbCommand_EraseAllSettings();
break;
#else
case UsbCommandId_JumpToModuleBootloader:
UsbCommand_JumpToModuleBootloader(GenericHidOutBuffer, GenericHidInBuffer);
Expand Down Expand Up @@ -153,4 +157,10 @@ void SetBufferBleAddress(uint8_t *GenericHidInBuffer, uint32_t offset, const bt_
GenericHidInBuffer[offset + i] = addr->a.val[i];
}
}


// void UsbCommand_EraseBleSettings(void) {
// Settings_Erase();
// }

#endif
1 change: 1 addition & 0 deletions right/src/usb_protocol_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
UsbCommandId_UnpairAll = 0x1a,
UsbCommandId_IsPaired = 0x1b,
UsbCommandId_EnterPairingMode = 0x1c,
UsbCommandId_EraseBleSettings = 0x1d,
} usb_command_id_t;

typedef enum {
Expand Down

0 comments on commit 28bd585

Please sign in to comment.