diff options
author | gornekich <n.gorbadey@gmail.com> | 2021-11-04 20:26:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-04 20:26:41 +0300 |
commit | 3225f408708fafde07d942645c6e739f1c399db4 (patch) | |
tree | af15859945e24549788ccc31cf1228f99e0c9a9e /applications/bt | |
parent | bb9c464a138a12283b6a34a9bdd5dadd7b034c8c (diff) |
[FL-1952] BLE bonding fix (#805)
* furi-hal-bt: add mutex guarding core2 state
* ble-glue: configure ble keys storage in SRAM2
* bt: add load and save ble keys in internal storage
* bt: improve work furi_hal_bt API
* bt: rework app_entry -> ble_glue
* bt: apply changes for f6 target
* desktop: remove furi check
* ble-glue: comment NVM in SRAM2 configuration
* FuriHal: fix flash controller state corruption, fix incorrect semaphore release, implement C1-C2 flash controller access according to spec. Gui: change logging level.
* Libs: better lfs integration with lfs_config.
* Ble: switch C2 NVM to RAM.
* FuriHalCrypto: ensure that core2 is alive before sending shci commands
* Ble: fix incorrect nvm buffer size
Co-authored-by: あく <alleteam@gmail.com>
Diffstat (limited to 'applications/bt')
-rwxr-xr-x | applications/bt/bt_service/bt.c | 20 | ||||
-rw-r--r-- | applications/bt/bt_service/bt_i.h | 3 | ||||
-rw-r--r-- | applications/bt/bt_service/bt_keys_storage.c | 41 | ||||
-rw-r--r-- | applications/bt/bt_service/bt_keys_storage.h | 7 |
4 files changed, 70 insertions, 1 deletions
diff --git a/applications/bt/bt_service/bt.c b/applications/bt/bt_service/bt.c index de4c58d4..aa999e36 100755 --- a/applications/bt/bt_service/bt.c +++ b/applications/bt/bt_service/bt.c @@ -1,5 +1,6 @@ #include "bt_i.h" #include "battery_service.h" +#include "bt_keys_storage.h" #define BT_SERVICE_TAG "BT" @@ -161,6 +162,14 @@ static void bt_on_gap_event_callback(BleEvent event, void* context) { } } +static void bt_on_key_storage_change_callback(uint8_t* addr, uint16_t size, void* context) { + furi_assert(context); + Bt* bt = context; + FURI_LOG_I(BT_SERVICE_TAG, "Changed addr start: %08lX, size changed: %d", addr, size); + BtMessage message = {.type = BtMessageTypeKeysStorageUpdated}; + furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK); +} + static void bt_statusbar_update(Bt* bt) { if(bt->status == BtStatusAdvertising) { view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_Bluetooth_5x8)); @@ -177,7 +186,12 @@ int32_t bt_srv() { Bt* bt = bt_alloc(); furi_record_create("bt", bt); - if(!furi_hal_bt_wait_startup()) { + // Read keys + if(!bt_load_key_storage(bt)) { + FURI_LOG_W(BT_SERVICE_TAG, "Failed to load saved bonding keys"); + } + // Start 2nd core + if(!furi_hal_bt_start_core2()) { FURI_LOG_E(BT_SERVICE_TAG, "Core2 startup failed"); } else { view_port_enabled_set(bt->statusbar_view_port, true); @@ -190,6 +204,8 @@ int32_t bt_srv() { FURI_LOG_E(BT_SERVICE_TAG, "BT App start failed"); } } + furi_hal_bt_set_key_storage_change_callback(bt_on_key_storage_change_callback, bt); + // Update statusbar bt_statusbar_update(bt); @@ -207,6 +223,8 @@ int32_t bt_srv() { } else if(message.type == BtMessageTypePinCodeShow) { // Display PIN code bt_pin_code_show_event_handler(bt, message.data.pin_code); + } else if(message.type == BtMessageTypeKeysStorageUpdated) { + bt_save_key_storage(bt); } } return 0; diff --git a/applications/bt/bt_service/bt_i.h b/applications/bt/bt_service/bt_i.h index 0588378e..3921a4c5 100644 --- a/applications/bt/bt_service/bt_i.h +++ b/applications/bt/bt_service/bt_i.h @@ -25,6 +25,7 @@ typedef enum { BtMessageTypeUpdateStatusbar, BtMessageTypeUpdateBatteryLevel, BtMessageTypePinCodeShow, + BtMessageTypeKeysStorageUpdated, } BtMessageType; typedef union { @@ -38,6 +39,8 @@ typedef struct { } BtMessage; struct Bt { + uint8_t* bt_keys_addr_start; + uint16_t bt_keys_size; BtSettings bt_settings; BtStatus status; osMessageQueueId_t message_queue; diff --git a/applications/bt/bt_service/bt_keys_storage.c b/applications/bt/bt_service/bt_keys_storage.c new file mode 100644 index 00000000..25c74882 --- /dev/null +++ b/applications/bt/bt_service/bt_keys_storage.c @@ -0,0 +1,41 @@ +#include "bt_keys_storage.h" +#include <furi.h> +#include <file-worker.h> + +#define BT_KEYS_STORAGE_TAG "bt keys storage" +#define BT_KEYS_STORAGE_PATH "/int/bt.keys" + +bool bt_load_key_storage(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); + return file_loaded; +} + +bool bt_save_key_storage(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); + return file_saved; +} diff --git a/applications/bt/bt_service/bt_keys_storage.h b/applications/bt/bt_service/bt_keys_storage.h new file mode 100644 index 00000000..4b09d7f2 --- /dev/null +++ b/applications/bt/bt_service/bt_keys_storage.h @@ -0,0 +1,7 @@ +#pragma once + +#include "bt_i.h" + +bool bt_load_key_storage(Bt* bt); + +bool bt_save_key_storage(Bt* bt); |