2021-11-04 20:26:41 +03:00
|
|
|
#include "bt_keys_storage.h"
|
2022-04-21 18:36:53 +03:00
|
|
|
|
2021-11-04 20:26:41 +03:00
|
|
|
#include <furi.h>
|
2022-12-20 15:32:24 +03:00
|
|
|
#include <furi_hal_bt.h>
|
2022-04-21 18:36:53 +03:00
|
|
|
#include <lib/toolbox/saved_struct.h>
|
2022-07-26 15:21:51 +03:00
|
|
|
#include <storage/storage.h>
|
2021-11-04 20:26:41 +03:00
|
|
|
|
2022-04-21 18:36:53 +03:00
|
|
|
#define BT_KEYS_STORAGE_VERSION (0)
|
|
|
|
#define BT_KEYS_STORAGE_MAGIC (0x18)
|
2021-11-04 20:26:41 +03:00
|
|
|
|
2022-12-20 15:32:24 +03:00
|
|
|
#define TAG "BtKeyStorage"
|
2021-11-04 20:26:41 +03:00
|
|
|
|
2022-12-20 15:32:24 +03:00
|
|
|
struct BtKeysStorage {
|
|
|
|
uint8_t* nvm_sram_buff;
|
|
|
|
uint16_t nvm_sram_buff_size;
|
|
|
|
FuriString* file_path;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool bt_keys_storage_delete(BtKeysStorage* instance) {
|
|
|
|
furi_assert(instance);
|
2022-01-21 20:32:03 +03:00
|
|
|
|
|
|
|
bool delete_succeed = false;
|
2022-02-07 16:37:56 +03:00
|
|
|
bool bt_is_active = furi_hal_bt_is_active();
|
2022-01-21 20:32:03 +03:00
|
|
|
|
|
|
|
furi_hal_bt_stop_advertising();
|
|
|
|
delete_succeed = furi_hal_bt_clear_white_list();
|
2022-02-07 16:37:56 +03:00
|
|
|
if(bt_is_active) {
|
2022-01-21 20:32:03 +03:00
|
|
|
furi_hal_bt_start_advertising();
|
|
|
|
}
|
|
|
|
|
|
|
|
return delete_succeed;
|
|
|
|
}
|
2022-12-20 15:32:24 +03:00
|
|
|
|
|
|
|
BtKeysStorage* bt_keys_storage_alloc(const char* keys_storage_path) {
|
|
|
|
furi_assert(keys_storage_path);
|
|
|
|
|
|
|
|
BtKeysStorage* instance = malloc(sizeof(BtKeysStorage));
|
|
|
|
// Set default nvm ram parameters
|
|
|
|
furi_hal_bt_get_key_storage_buff(&instance->nvm_sram_buff, &instance->nvm_sram_buff_size);
|
|
|
|
// Set key storage file
|
|
|
|
instance->file_path = furi_string_alloc();
|
|
|
|
furi_string_set_str(instance->file_path, keys_storage_path);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_keys_storage_free(BtKeysStorage* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
furi_string_free(instance->file_path);
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_keys_storage_set_file_path(BtKeysStorage* instance, const char* path) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(path);
|
|
|
|
|
|
|
|
furi_string_set_str(instance->file_path, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_keys_storage_set_ram_params(BtKeysStorage* instance, uint8_t* buff, uint16_t size) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(buff);
|
|
|
|
|
|
|
|
instance->nvm_sram_buff = buff;
|
|
|
|
instance->nvm_sram_buff_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_keys_storage_load(BtKeysStorage* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
bool loaded = false;
|
|
|
|
do {
|
|
|
|
// Get payload size
|
|
|
|
size_t payload_size = 0;
|
|
|
|
if(!saved_struct_get_payload_size(
|
|
|
|
furi_string_get_cstr(instance->file_path),
|
|
|
|
BT_KEYS_STORAGE_MAGIC,
|
|
|
|
BT_KEYS_STORAGE_VERSION,
|
|
|
|
&payload_size)) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to read payload size");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(payload_size > instance->nvm_sram_buff_size) {
|
|
|
|
FURI_LOG_E(TAG, "Saved data doesn't fit ram buffer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load saved data to ram
|
|
|
|
furi_hal_bt_nvm_sram_sem_acquire();
|
|
|
|
bool data_loaded = saved_struct_load(
|
|
|
|
furi_string_get_cstr(instance->file_path),
|
|
|
|
instance->nvm_sram_buff,
|
|
|
|
payload_size,
|
|
|
|
BT_KEYS_STORAGE_MAGIC,
|
|
|
|
BT_KEYS_STORAGE_VERSION);
|
|
|
|
furi_hal_bt_nvm_sram_sem_release();
|
|
|
|
if(!data_loaded) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to load struct");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
loaded = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_keys_storage_update(BtKeysStorage* instance, uint8_t* start_addr, uint32_t size) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(start_addr);
|
|
|
|
|
|
|
|
bool updated = false;
|
|
|
|
|
|
|
|
FURI_LOG_I(
|
|
|
|
TAG,
|
2022-12-26 15:13:30 +03:00
|
|
|
"Base address: %p. Start update address: %p. Size changed: %lu",
|
2022-12-20 15:32:24 +03:00
|
|
|
(void*)instance->nvm_sram_buff,
|
|
|
|
start_addr,
|
|
|
|
size);
|
|
|
|
|
|
|
|
do {
|
|
|
|
size_t new_size = start_addr - instance->nvm_sram_buff + size;
|
|
|
|
if(new_size > instance->nvm_sram_buff_size) {
|
|
|
|
FURI_LOG_E(TAG, "NVM RAM buffer overflow");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_bt_nvm_sram_sem_acquire();
|
|
|
|
bool data_updated = saved_struct_save(
|
|
|
|
furi_string_get_cstr(instance->file_path),
|
|
|
|
instance->nvm_sram_buff,
|
|
|
|
new_size,
|
|
|
|
BT_KEYS_STORAGE_MAGIC,
|
|
|
|
BT_KEYS_STORAGE_VERSION);
|
|
|
|
furi_hal_bt_nvm_sram_sem_release();
|
|
|
|
if(!data_updated) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to update key storage");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
updated = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
}
|