[FL-2432], [FL-2487] Rework bt keys load and save (#1139)
* bt keys: rework load and save with saved_struct * bt: rename bt keys storage functions * furi_hal_nfc: allow context switch during emilation * bt settings: rework with saved struct * infrared: replace file worker with dialogs and storage * Core, Loader: fix thread allocation tracking, much better, so wow. Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -320,7 +320,7 @@ int32_t bt_srv() {
|
||||
Bt* bt = bt_alloc();
|
||||
|
||||
// Read keys
|
||||
if(!bt_load_key_storage(bt)) {
|
||||
if(!bt_keys_storage_load(bt)) {
|
||||
FURI_LOG_W(TAG, "Failed to load bonding keys");
|
||||
}
|
||||
|
||||
@@ -365,11 +365,11 @@ int32_t bt_srv() {
|
||||
// Display PIN code
|
||||
bt_pin_code_show(bt, message.data.pin_code);
|
||||
} else if(message.type == BtMessageTypeKeysStorageUpdated) {
|
||||
bt_save_key_storage(bt);
|
||||
bt_keys_storage_save(bt);
|
||||
} else if(message.type == BtMessageTypeSetProfile) {
|
||||
bt_change_profile(bt, &message);
|
||||
} else if(message.type == BtMessageTypeForgetBondedDevices) {
|
||||
bt_delete_key_storage(bt);
|
||||
bt_keys_storage_delete(bt);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
@@ -1,46 +1,47 @@
|
||||
#include "bt_keys_storage.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <file_worker.h>
|
||||
#include <lib/toolbox/saved_struct.h>
|
||||
|
||||
#define BT_KEYS_STORAGE_TAG "bt keys storage"
|
||||
#define BT_KEYS_STORAGE_PATH "/int/bt.keys"
|
||||
#define BT_KEYS_STORAGE_VERSION (0)
|
||||
#define BT_KEYS_STORAGE_MAGIC (0x18)
|
||||
|
||||
bool bt_load_key_storage(Bt* bt) {
|
||||
bool bt_keys_storage_load(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
|
||||
bool file_loaded = false;
|
||||
furi_hal_bt_get_key_storage_buff(&bt->bt_keys_addr_start, &bt->bt_keys_size);
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
if(file_worker_open(file_worker, BT_KEYS_STORAGE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
furi_hal_bt_nvm_sram_sem_acquire();
|
||||
if(file_worker_read(file_worker, bt->bt_keys_addr_start, bt->bt_keys_size)) {
|
||||
file_loaded = true;
|
||||
}
|
||||
furi_hal_bt_nvm_sram_sem_release();
|
||||
}
|
||||
file_worker_free(file_worker);
|
||||
furi_hal_bt_get_key_storage_buff(&bt->bt_keys_addr_start, &bt->bt_keys_size);
|
||||
furi_hal_bt_nvm_sram_sem_acquire();
|
||||
file_loaded = saved_struct_load(
|
||||
BT_KEYS_STORAGE_PATH,
|
||||
bt->bt_keys_addr_start,
|
||||
bt->bt_keys_size,
|
||||
BT_KEYS_STORAGE_MAGIC,
|
||||
BT_KEYS_STORAGE_VERSION);
|
||||
furi_hal_bt_nvm_sram_sem_release();
|
||||
|
||||
return file_loaded;
|
||||
}
|
||||
|
||||
bool bt_save_key_storage(Bt* bt) {
|
||||
bool bt_keys_storage_save(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
furi_assert(bt->bt_keys_addr_start);
|
||||
|
||||
bool file_saved = false;
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
if(file_worker_open(file_worker, BT_KEYS_STORAGE_PATH, FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
|
||||
furi_hal_bt_nvm_sram_sem_acquire();
|
||||
if(file_worker_write(file_worker, bt->bt_keys_addr_start, bt->bt_keys_size)) {
|
||||
file_saved = true;
|
||||
}
|
||||
furi_hal_bt_nvm_sram_sem_release();
|
||||
}
|
||||
file_worker_free(file_worker);
|
||||
|
||||
furi_hal_bt_nvm_sram_sem_acquire();
|
||||
file_saved = saved_struct_save(
|
||||
BT_KEYS_STORAGE_PATH,
|
||||
bt->bt_keys_addr_start,
|
||||
bt->bt_keys_size,
|
||||
BT_KEYS_STORAGE_MAGIC,
|
||||
BT_KEYS_STORAGE_VERSION);
|
||||
furi_hal_bt_nvm_sram_sem_release();
|
||||
|
||||
return file_saved;
|
||||
}
|
||||
|
||||
bool bt_delete_key_storage(Bt* bt) {
|
||||
bool bt_keys_storage_delete(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
bool delete_succeed = false;
|
||||
bool bt_is_active = furi_hal_bt_is_active();
|
||||
|
@@ -2,8 +2,8 @@
|
||||
|
||||
#include "bt_i.h"
|
||||
|
||||
bool bt_load_key_storage(Bt* bt);
|
||||
bool bt_keys_storage_load(Bt* bt);
|
||||
|
||||
bool bt_save_key_storage(Bt* bt);
|
||||
bool bt_keys_storage_save(Bt* bt);
|
||||
|
||||
bool bt_delete_key_storage(Bt* bt);
|
||||
bool bt_keys_storage_delete(Bt* bt);
|
||||
|
@@ -1,50 +1,22 @@
|
||||
#include "bt_settings.h"
|
||||
#include <furi.h>
|
||||
#include <file_worker.h>
|
||||
|
||||
#define TAG "BtSettings"
|
||||
#include <furi.h>
|
||||
#include <lib/toolbox/saved_struct.h>
|
||||
|
||||
#define BT_SETTINGS_PATH "/int/bt.settings"
|
||||
#define BT_SETTINGS_VERSION (0)
|
||||
#define BT_SETTINGS_MAGIC (0x19)
|
||||
|
||||
bool bt_settings_load(BtSettings* bt_settings) {
|
||||
furi_assert(bt_settings);
|
||||
bool file_loaded = false;
|
||||
BtSettings settings = {};
|
||||
|
||||
FURI_LOG_I(TAG, "Loading settings from \"%s\"", BT_SETTINGS_PATH);
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
if(file_worker_open(file_worker, BT_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
if(file_worker_read(file_worker, &settings, sizeof(settings))) {
|
||||
file_loaded = true;
|
||||
}
|
||||
}
|
||||
file_worker_free(file_worker);
|
||||
|
||||
if(file_loaded) {
|
||||
FURI_LOG_I(TAG, "Settings load success");
|
||||
if(settings.version != BT_SETTINGS_VERSION) {
|
||||
FURI_LOG_E(TAG, "Settings version mismatch");
|
||||
} else {
|
||||
osKernelLock();
|
||||
*bt_settings = settings;
|
||||
osKernelUnlock();
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Settings load failed");
|
||||
}
|
||||
return file_loaded;
|
||||
return saved_struct_load(
|
||||
BT_SETTINGS_PATH, bt_settings, sizeof(BtSettings), BT_SETTINGS_MAGIC, BT_SETTINGS_VERSION);
|
||||
}
|
||||
|
||||
bool bt_settings_save(BtSettings* bt_settings) {
|
||||
furi_assert(bt_settings);
|
||||
bool result = false;
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
if(file_worker_open(file_worker, BT_SETTINGS_PATH, FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
|
||||
if(file_worker_write(file_worker, bt_settings, sizeof(BtSettings))) {
|
||||
FURI_LOG_I(TAG, "Settings saved to \"%s\"", BT_SETTINGS_PATH);
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
file_worker_free(file_worker);
|
||||
return result;
|
||||
return saved_struct_save(
|
||||
BT_SETTINGS_PATH, bt_settings, sizeof(BtSettings), BT_SETTINGS_MAGIC, BT_SETTINGS_VERSION);
|
||||
}
|
||||
|
@@ -3,10 +3,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define BT_SETTINGS_VERSION (0)
|
||||
|
||||
typedef struct {
|
||||
uint8_t version;
|
||||
bool enabled;
|
||||
} BtSettings;
|
||||
|
||||
|
Reference in New Issue
Block a user