Merge remote-tracking branch 'ofw/dev' into mntm-dev

This commit is contained in:
Willy-JL
2024-08-05 01:50:20 +02:00
109 changed files with 1115 additions and 1860 deletions

View File

@@ -61,6 +61,21 @@ static void bt_pin_code_view_port_input_callback(InputEvent* event, void* contex
}
}
static void bt_storage_callback(const void* message, void* context) {
furi_assert(context);
Bt* bt = context;
const StorageEvent* event = message;
if(event->type == StorageEventTypeCardMount) {
const BtMessage msg = {
.type = BtMessageTypeReloadKeysSettings,
};
furi_check(
furi_message_queue_put(bt->message_queue, &msg, FuriWaitForever) == FuriStatusOk);
}
}
static ViewPort* bt_pin_code_view_port_alloc(Bt* bt) {
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, bt_pin_code_view_port_draw_callback, bt);
@@ -143,8 +158,6 @@ Bt* bt_alloc(void) {
// Init default maximum packet size
bt->max_packet_size = BLE_PROFILE_SERIAL_PACKET_SIZE_MAX;
bt->current_profile = NULL;
// Load settings
bt_settings_load(&bt->bt_settings);
// Keys storage
bt->keys_storage = bt_keys_storage_alloc(BT_KEYS_STORAGE_PATH);
// Alloc queue
@@ -396,6 +409,8 @@ void bt_close_rpc_connection(Bt* bt) {
static void bt_change_profile(Bt* bt, BtMessage* message) {
if(furi_hal_bt_is_gatt_gap_supported()) {
bt_settings_load(&bt->bt_settings);
bt_close_rpc_connection(bt);
bt_keys_storage_load(bt->keys_storage);
@@ -439,6 +454,87 @@ static void bt_close_connection(Bt* bt, BtMessage* message) {
if(message->lock) api_lock_unlock(message->lock);
}
static void bt_apply_settings(Bt* bt) {
if(bt->bt_settings.enabled) {
furi_hal_bt_start_advertising();
} else {
furi_hal_bt_stop_advertising();
}
}
static void bt_load_keys(Bt* bt) {
if(!furi_hal_bt_is_gatt_gap_supported()) {
bt_show_warning(bt, "Unsupported radio stack");
bt->status = BtStatusUnavailable;
return;
} else if(bt_keys_storage_is_changed(bt->keys_storage)) {
FURI_LOG_I(TAG, "Loading new keys");
bt_close_rpc_connection(bt);
bt_keys_storage_load(bt->keys_storage);
bt->current_profile = NULL;
} else {
FURI_LOG_I(TAG, "Keys unchanged");
}
}
static void bt_start_application(Bt* bt) {
if(!bt->current_profile) {
bt->current_profile =
furi_hal_bt_change_app(ble_profile_serial, NULL, bt_on_gap_event_callback, bt);
if(!bt->current_profile) {
FURI_LOG_E(TAG, "BLE App start failed");
bt->status = BtStatusUnavailable;
}
}
}
static void bt_load_settings(Bt* bt) {
bt_settings_load(&bt->bt_settings);
bt_apply_settings(bt);
}
static void bt_handle_get_settings(Bt* bt, BtMessage* message) {
furi_assert(message->lock);
*message->data.settings = bt->bt_settings;
api_lock_unlock(message->lock);
}
static void bt_handle_set_settings(Bt* bt, BtMessage* message) {
furi_assert(message->lock);
bt->bt_settings = *message->data.csettings;
bt_apply_settings(bt);
bt_settings_save(&bt->bt_settings);
api_lock_unlock(message->lock);
}
static void bt_handle_reload_keys_settings(Bt* bt) {
bt_load_keys(bt);
bt_start_application(bt);
bt_load_settings(bt);
}
static void bt_init_keys_settings(Bt* bt) {
Storage* storage = furi_record_open(RECORD_STORAGE);
furi_pubsub_subscribe(storage_get_pubsub(storage), bt_storage_callback, bt);
if(storage_sd_status(storage) != FSE_OK) {
FURI_LOG_D(TAG, "SD Card not ready, skipping settings");
// Just start the BLE serial application without loading the keys or settings
bt_start_application(bt);
return;
}
bt_handle_reload_keys_settings(bt);
}
bool bt_remote_rssi(Bt* bt, uint8_t* rssi) {
furi_assert(bt);
@@ -465,35 +561,18 @@ int32_t bt_srv(void* p) {
return 0;
}
// Load keys
if(!bt_keys_storage_load(bt->keys_storage)) {
FURI_LOG_W(TAG, "Failed to load bonding keys");
}
if(furi_hal_bt_start_radio_stack()) {
bt_init_keys_settings(bt);
furi_hal_bt_set_key_storage_change_callback(bt_on_key_storage_change_callback, bt);
// Start radio stack
if(!furi_hal_bt_start_radio_stack()) {
FURI_LOG_E(TAG, "Radio stack start failed");
}
if(furi_hal_bt_is_gatt_gap_supported()) {
bt->current_profile =
furi_hal_bt_start_app(ble_profile_serial, NULL, bt_on_gap_event_callback, bt);
if(!bt->current_profile) {
FURI_LOG_E(TAG, "BLE App start failed");
} else {
if(bt->bt_settings.enabled) {
furi_hal_bt_start_advertising();
}
furi_hal_bt_set_key_storage_change_callback(bt_on_key_storage_change_callback, bt);
}
} else {
bt_show_warning(bt, "Unsupported radio stack");
bt->status = BtStatusUnavailable;
FURI_LOG_E(TAG, "Radio stack start failed");
}
furi_record_create(RECORD_BT, bt);
BtMessage message;
while(1) {
furi_check(
furi_message_queue_get(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
@@ -523,7 +602,14 @@ int32_t bt_srv(void* p) {
bt_close_connection(bt, &message);
} else if(message.type == BtMessageTypeForgetBondedDevices) {
bt_keys_storage_delete(bt->keys_storage);
} else if(message.type == BtMessageTypeGetSettings) {
bt_handle_get_settings(bt, &message);
} else if(message.type == BtMessageTypeSetSettings) {
bt_handle_set_settings(bt, &message);
} else if(message.type == BtMessageTypeReloadKeysSettings) {
bt_handle_reload_keys_settings(bt);
}
}
return 0;
}

View File

@@ -77,3 +77,39 @@ void bt_keys_storage_set_default_path(Bt* bt) {
bt_keys_storage_set_file_path(bt->keys_storage, BT_KEYS_STORAGE_PATH);
}
/*
* Private API for the Settings app
*/
void bt_get_settings(Bt* bt, BtSettings* settings) {
furi_assert(bt);
furi_assert(settings);
BtMessage message = {
.lock = api_lock_alloc_locked(),
.type = BtMessageTypeGetSettings,
.data.settings = settings,
};
furi_check(
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
api_lock_wait_unlock_and_free(message.lock);
}
void bt_set_settings(Bt* bt, const BtSettings* settings) {
furi_assert(bt);
furi_assert(settings);
BtMessage message = {
.lock = api_lock_alloc_locked(),
.type = BtMessageTypeSetSettings,
.data.csettings = settings,
};
furi_check(
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
api_lock_wait_unlock_and_free(message.lock);
}

View File

@@ -30,6 +30,9 @@ typedef enum {
BtMessageTypeSetProfile,
BtMessageTypeDisconnect,
BtMessageTypeForgetBondedDevices,
BtMessageTypeGetSettings,
BtMessageTypeSetSettings,
BtMessageTypeReloadKeysSettings,
} BtMessageType;
typedef struct {
@@ -47,6 +50,8 @@ typedef union {
} profile;
FuriHalBleProfileParams profile_params;
BtKeyStorageUpdateData key_storage_data;
BtSettings* settings;
const BtSettings* csettings;
} BtMessageData;
typedef struct {

View File

@@ -13,6 +13,7 @@
struct BtKeysStorage {
uint8_t* nvm_sram_buff;
uint16_t nvm_sram_buff_size;
uint16_t current_size;
FuriString* file_path;
};
@@ -66,44 +67,114 @@ void bt_keys_storage_set_ram_params(BtKeysStorage* instance, uint8_t* buff, uint
instance->nvm_sram_buff_size = size;
}
static bool bt_keys_storage_file_exists(const char* file_path) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FileInfo file_info;
const bool ret = storage_common_stat(storage, file_path, &file_info) == FSE_OK &&
file_info.size != 0;
furi_record_close(RECORD_STORAGE);
return ret;
}
static bool bt_keys_storage_validate_file(const char* file_path, size_t* payload_size) {
uint8_t magic, version;
size_t size;
if(!saved_struct_get_metadata(file_path, &magic, &version, &size)) {
FURI_LOG_E(TAG, "Failed to get metadata");
return false;
} else if(magic != BT_KEYS_STORAGE_MAGIC || version != BT_KEYS_STORAGE_VERSION) {
FURI_LOG_E(TAG, "File version mismatch");
return false;
}
*payload_size = size;
return true;
}
bool bt_keys_storage_is_changed(BtKeysStorage* instance) {
furi_assert(instance);
bool is_changed = false;
uint8_t* data_buffer = NULL;
do {
const char* file_path = furi_string_get_cstr(instance->file_path);
size_t payload_size;
if(!bt_keys_storage_file_exists(file_path)) {
FURI_LOG_W(TAG, "Missing or empty file");
break;
} else if(!bt_keys_storage_validate_file(file_path, &payload_size)) {
FURI_LOG_E(TAG, "Invalid or corrupted file");
break;
}
data_buffer = malloc(payload_size);
const bool data_loaded = saved_struct_load(
file_path, data_buffer, payload_size, BT_KEYS_STORAGE_MAGIC, BT_KEYS_STORAGE_VERSION);
if(!data_loaded) {
FURI_LOG_E(TAG, "Failed to load file");
break;
} else if(payload_size == instance->current_size) {
furi_hal_bt_nvm_sram_sem_acquire();
is_changed = memcmp(data_buffer, instance->nvm_sram_buff, payload_size);
furi_hal_bt_nvm_sram_sem_release();
} else {
FURI_LOG_D(TAG, "Size mismatch");
is_changed = true;
}
} while(false);
if(data_buffer) {
free(data_buffer);
}
return is_changed;
}
bool bt_keys_storage_load(BtKeysStorage* instance) {
furi_assert(instance);
bool loaded = false;
do {
const char* file_path = furi_string_get_cstr(instance->file_path);
// Get payload size
uint8_t magic = 0, version = 0;
size_t payload_size = 0;
if(!saved_struct_get_metadata(
furi_string_get_cstr(instance->file_path), &magic, &version, &payload_size)) {
FURI_LOG_E(TAG, "Failed to read payload size");
size_t payload_size;
if(!bt_keys_storage_validate_file(file_path, &payload_size)) {
FURI_LOG_E(TAG, "Invalid or corrupted file");
break;
}
if(magic != BT_KEYS_STORAGE_MAGIC || version != BT_KEYS_STORAGE_VERSION) {
FURI_LOG_E(TAG, "Saved data version is mismatched");
break;
}
if(payload_size > instance->nvm_sram_buff_size) {
FURI_LOG_E(TAG, "Saved data doesn't fit ram buffer");
} else if(payload_size > instance->nvm_sram_buff_size) {
FURI_LOG_E(TAG, "NVM RAM buffer overflow");
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),
const bool data_loaded = saved_struct_load(
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");
FURI_LOG_E(TAG, "Failed to load file");
break;
}
instance->current_size = payload_size;
loaded = true;
} while(false);
@@ -130,6 +201,8 @@ bool bt_keys_storage_update(BtKeysStorage* instance, uint8_t* start_addr, uint32
break;
}
instance->current_size = new_size;
furi_hal_bt_nvm_sram_sem_acquire();
bool data_updated = saved_struct_save(
furi_string_get_cstr(instance->file_path),
@@ -138,10 +211,12 @@ bool bt_keys_storage_update(BtKeysStorage* instance, uint8_t* start_addr, uint32
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);

View File

@@ -17,6 +17,8 @@ void bt_keys_storage_set_file_path(BtKeysStorage* instance, const char* path);
void bt_keys_storage_set_ram_params(BtKeysStorage* instance, uint8_t* buff, uint16_t size);
bool bt_keys_storage_is_changed(BtKeysStorage* instance);
bool bt_keys_storage_load(BtKeysStorage* instance);
bool bt_keys_storage_update(BtKeysStorage* instance, uint8_t* start_addr, uint32_t size);

View File

@@ -0,0 +1,8 @@
#pragma once
#include "bt.h"
#include "../bt_settings.h"
void bt_get_settings(Bt* bt, BtSettings* settings);
void bt_set_settings(Bt* bt, const BtSettings* settings);

View File

@@ -1,22 +1,35 @@
#include "bt_settings.h"
#include "bt_settings_filename.h"
#include <furi.h>
#include <lib/toolbox/saved_struct.h>
#include <storage/storage.h>
#include <toolbox/saved_struct.h>
#define TAG "BtSettings"
#define BT_SETTINGS_VERSION (0)
#define BT_SETTINGS_MAGIC (0x19)
bool bt_settings_load(BtSettings* bt_settings) {
void bt_settings_load(BtSettings* bt_settings) {
furi_assert(bt_settings);
return saved_struct_load(
const bool success = saved_struct_load(
BT_SETTINGS_PATH, bt_settings, sizeof(BtSettings), BT_SETTINGS_MAGIC, BT_SETTINGS_VERSION);
if(!success) {
FURI_LOG_W(TAG, "Failed to load settings, using defaults");
memset(bt_settings, 0, sizeof(BtSettings));
bt_settings_save(bt_settings);
}
}
bool bt_settings_save(const BtSettings* bt_settings) {
void bt_settings_save(const BtSettings* bt_settings) {
furi_assert(bt_settings);
return saved_struct_save(
const bool success = saved_struct_save(
BT_SETTINGS_PATH, bt_settings, sizeof(BtSettings), BT_SETTINGS_MAGIC, BT_SETTINGS_VERSION);
if(!success) {
FURI_LOG_E(TAG, "Failed to save settings");
}
}

View File

@@ -1,6 +1,5 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
@@ -13,9 +12,9 @@ typedef struct {
bool enabled;
} BtSettings;
bool bt_settings_load(BtSettings* bt_settings);
void bt_settings_load(BtSettings* bt_settings);
bool bt_settings_save(const BtSettings* bt_settings);
void bt_settings_save(const BtSettings* bt_settings);
#ifdef __cplusplus
}