Skip to content

Commit

Permalink
When deleting "all" settings, do not delete right-left link data
Browse files Browse the repository at this point in the history
  • Loading branch information
kareltucek committed Jan 9, 2025
1 parent 26039ca commit c275b61
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion device/src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#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"
Expand Down Expand Up @@ -56,8 +58,49 @@ void Settings_Reload(void) {
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) {
settings_delete(key);
if (shouldDeleteKey(key)) {
settings_delete(key);
}
return 0;
}

Expand Down

0 comments on commit c275b61

Please sign in to comment.