adding flasher

This commit is contained in:
Brent Perteet
2025-10-12 13:40:31 -05:00
parent a89fdc6843
commit 04d2c71d01
18 changed files with 1034 additions and 194 deletions

View File

@@ -109,24 +109,11 @@ static void bt_app_av_sm_hdlr(uint16_t event, void *param);
/* utils for transfer BLuetooth Deveice Address into string form */
static char *bda2str(esp_bd_addr_t bda, char *str, size_t size);
/* NVS storage functions for paired devices */
typedef struct {
esp_bd_addr_t bda;
char name[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
uint32_t last_connected;
} paired_device_t;
static esp_err_t nvs_save_paired_device(const paired_device_t *device);
static esp_err_t nvs_load_paired_devices(paired_device_t *devices, size_t *count);
static esp_err_t nvs_remove_paired_device(esp_bd_addr_t bda);
static bool nvs_is_device_known(esp_bd_addr_t bda);
static esp_err_t nvs_get_known_device_count(size_t *count);
static esp_err_t nvs_try_connect_known_devices(void);
static void nvs_debug_print_known_devices(void);
static esp_err_t nvs_add_discovered_device(esp_bd_addr_t bda, const char *name);
static esp_err_t nvs_update_connection_timestamp(esp_bd_addr_t bda);
static esp_err_t nvs_try_connect_all_known_devices(void);
static esp_err_t nvs_try_next_known_device(void);
static esp_err_t bt_try_connect_known_devices(void);
static void bt_debug_print_known_devices(void);
static esp_err_t bt_add_discovered_device(esp_bd_addr_t bda, const char *name);
static esp_err_t bt_try_connect_all_known_devices(void);
static esp_err_t bt_try_next_known_device(void);
/* A2DP application state machine handler for each state */
static void bt_app_av_state_unconnected_hdlr(uint16_t event, void *param);
@@ -167,145 +154,18 @@ static bool s_volume_control_available = false; /* Whether AVRC vo
* NVS STORAGE FUNCTION DEFINITIONS
********************************/
static esp_err_t nvs_save_paired_device(const paired_device_t *device)
{
nvs_handle_t nvs_handle;
esp_err_t ret;
size_t count = 0;
char key[32];
if (!device) {
return ESP_ERR_INVALID_ARG;
}
ret = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs_handle);
if (ret != ESP_OK) {
ESP_LOGE(BT_AV_TAG, "Error opening NVS handle: %s", esp_err_to_name(ret));
return ret;
}
// Get current device count
size_t required_size = sizeof(size_t);
nvs_get_blob(nvs_handle, NVS_KEY_COUNT, &count, &required_size);
// Check if device already exists
paired_device_t existing_devices[MAX_PAIRED_DEVICES];
size_t existing_count = MAX_PAIRED_DEVICES;
nvs_load_paired_devices(existing_devices, &existing_count);
int device_index = -1;
for (int i = 0; i < existing_count; i++) {
if (memcmp(existing_devices[i].bda, device->bda, ESP_BD_ADDR_LEN) == 0) {
device_index = i;
break;
}
}
// If device not found and we have space, add it
if (device_index == -1) {
if (count >= MAX_PAIRED_DEVICES) {
ESP_LOGW(BT_AV_TAG, "Maximum paired devices reached");
nvs_close(nvs_handle);
return ESP_ERR_NO_MEM;
}
device_index = count;
count++;
}
// Save device data
snprintf(key, sizeof(key), "%s%d", NVS_KEY_PREFIX, device_index);
ret = nvs_set_blob(nvs_handle, key, device, sizeof(paired_device_t));
if (ret != ESP_OK) {
ESP_LOGE(BT_AV_TAG, "Error saving device: %s", esp_err_to_name(ret));
nvs_close(nvs_handle);
return ret;
}
// Save device count
ret = nvs_set_blob(nvs_handle, NVS_KEY_COUNT, &count, sizeof(size_t));
if (ret != ESP_OK) {
ESP_LOGE(BT_AV_TAG, "Error saving device count: %s", esp_err_to_name(ret));
nvs_close(nvs_handle);
return ret;
}
ret = nvs_commit(nvs_handle);
nvs_close(nvs_handle);
char bda_str[18];
ESP_LOGI(BT_AV_TAG, "Saved paired device: %s (%s)",
device->name, bda2str(device->bda, bda_str, sizeof(bda_str)));
return ret;
}
// This function is no longer used - replaced by system_savePairedDevice
static esp_err_t nvs_load_paired_devices(paired_device_t *devices, size_t *count)
{
nvs_handle_t nvs_handle;
esp_err_t ret;
size_t device_count = 0;
char key[32];
if (!devices || !count || *count == 0) {
return ESP_ERR_INVALID_ARG;
}
ret = nvs_open(NVS_NAMESPACE, NVS_READONLY, &nvs_handle);
if (ret != ESP_OK) {
ESP_LOGD(BT_AV_TAG, "NVS namespace not found (first run): %s", esp_err_to_name(ret));
*count = 0;
return ESP_OK;
}
// Get device count
size_t required_size = sizeof(size_t);
ret = nvs_get_blob(nvs_handle, NVS_KEY_COUNT, &device_count, &required_size);
if (ret != ESP_OK) {
ESP_LOGD(BT_AV_TAG, "No paired devices found");
nvs_close(nvs_handle);
*count = 0;
return ESP_OK;
}
// Load each device
size_t loaded = 0;
for (int i = 0; i < device_count && loaded < *count; i++) {
snprintf(key, sizeof(key), "%s%d", NVS_KEY_PREFIX, i);
required_size = sizeof(paired_device_t);
ret = nvs_get_blob(nvs_handle, key, &devices[loaded], &required_size);
if (ret == ESP_OK) {
loaded++;
}
}
nvs_close(nvs_handle);
*count = loaded;
return ESP_OK;
}
// This function is no longer used - replaced by system_loadPairedDevices
static bool nvs_is_device_known(esp_bd_addr_t bda)
// This function is no longer used - replaced by system_isDeviceKnown
static esp_err_t bt_try_connect_known_devices(void)
{
paired_device_t devices[MAX_PAIRED_DEVICES];
size_t count = MAX_PAIRED_DEVICES;
if (nvs_load_paired_devices(devices, &count) != ESP_OK) {
return false;
}
for (int i = 0; i < count; i++) {
if (memcmp(devices[i].bda, bda, ESP_BD_ADDR_LEN) == 0) {
return true;
}
}
return false;
}
static esp_err_t nvs_try_connect_known_devices(void)
{
paired_device_t devices[MAX_PAIRED_DEVICES];
size_t count = MAX_PAIRED_DEVICES;
esp_err_t ret = nvs_load_paired_devices(devices, &count);
esp_err_t ret = system_loadPairedDevices(devices, &count);
if (ret != ESP_OK || count == 0) {
ESP_LOGI(BT_AV_TAG, "No known devices to connect to");
return ESP_ERR_NOT_FOUND;
@@ -355,14 +215,14 @@ static esp_err_t nvs_try_connect_known_devices(void)
return ret;
}
static void nvs_debug_print_known_devices(void)
static void bt_debug_print_known_devices(void)
{
paired_device_t devices[MAX_PAIRED_DEVICES];
size_t count = MAX_PAIRED_DEVICES;
const paired_device_t* devices;
size_t count = 0;
esp_err_t ret = nvs_load_paired_devices(devices, &count);
if (ret != ESP_OK) {
ESP_LOGE(BT_AV_TAG, "Failed to load devices for debug: %s", esp_err_to_name(ret));
devices = system_getPairedDevices(&count);
if (!devices) {
ESP_LOGE(BT_AV_TAG, "Failed to load devices for debug");
return;
}
@@ -377,12 +237,12 @@ static void nvs_debug_print_known_devices(void)
ESP_LOGI(BT_AV_TAG, "=== End Device List ===");
}
static esp_err_t nvs_remove_paired_device(esp_bd_addr_t bda)
static esp_err_t __attribute__((unused)) nvs_remove_paired_device(esp_bd_addr_t bda)
{
paired_device_t devices[MAX_PAIRED_DEVICES];
size_t count = MAX_PAIRED_DEVICES;
esp_err_t ret = nvs_load_paired_devices(devices, &count);
esp_err_t ret = system_loadPairedDevices(devices, &count);
if (ret != ESP_OK || count == 0) {
return ESP_ERR_NOT_FOUND;
}
@@ -438,7 +298,7 @@ static esp_err_t nvs_remove_paired_device(esp_bd_addr_t bda)
return ret;
}
static esp_err_t nvs_get_known_device_count(size_t *count)
static esp_err_t __attribute__((unused)) nvs_get_known_device_count(size_t *count)
{
if (!count) {
return ESP_ERR_INVALID_ARG;
@@ -462,14 +322,14 @@ static esp_err_t nvs_get_known_device_count(size_t *count)
return ret;
}
static esp_err_t nvs_add_discovered_device(esp_bd_addr_t bda, const char *name)
static esp_err_t bt_add_discovered_device(esp_bd_addr_t bda, const char *name)
{
if (!bda || !name) {
return ESP_ERR_INVALID_ARG;
}
// Check if device is already known
if (nvs_is_device_known(bda)) {
if (system_isDeviceKnown(bda)) {
char bda_str[18];
ESP_LOGD(BT_AV_TAG, "Device %s (%s) already known, skipping",
name, bda2str(bda, bda_str, sizeof(bda_str)));
@@ -479,12 +339,12 @@ static esp_err_t nvs_add_discovered_device(esp_bd_addr_t bda, const char *name)
// Create paired_device_t structure for discovered device
paired_device_t device;
memcpy(device.bda, bda, ESP_BD_ADDR_LEN);
strncpy(device.name, name, ESP_BT_GAP_MAX_BDNAME_LEN);
device.name[ESP_BT_GAP_MAX_BDNAME_LEN] = '\0';
strncpy(device.name, name, DEVICE_NAME_MAX_LEN - 1);
device.name[DEVICE_NAME_MAX_LEN - 1] = '\0';
device.last_connected = 0; // Never connected, set to 0
// Save to NVS
esp_err_t ret = nvs_save_paired_device(&device);
esp_err_t ret = system_savePairedDevice(&device);
if (ret == ESP_OK) {
char bda_str[18];
ESP_LOGI(BT_AV_TAG, "Added discovered device to NVS: %s (%s)",
@@ -496,7 +356,7 @@ static esp_err_t nvs_add_discovered_device(esp_bd_addr_t bda, const char *name)
return ret;
}
static esp_err_t nvs_update_connection_timestamp(esp_bd_addr_t bda)
static esp_err_t __attribute__((unused)) nvs_update_connection_timestamp(esp_bd_addr_t bda)
{
if (!bda) {
return ESP_ERR_INVALID_ARG;
@@ -505,7 +365,7 @@ static esp_err_t nvs_update_connection_timestamp(esp_bd_addr_t bda)
paired_device_t devices[MAX_PAIRED_DEVICES];
size_t count = MAX_PAIRED_DEVICES;
esp_err_t ret = nvs_load_paired_devices(devices, &count);
esp_err_t ret = system_loadPairedDevices(devices, &count);
if (ret != ESP_OK || count == 0) {
ESP_LOGW(BT_AV_TAG, "No devices found to update timestamp");
return ESP_ERR_NOT_FOUND;
@@ -529,7 +389,7 @@ static esp_err_t nvs_update_connection_timestamp(esp_bd_addr_t bda)
devices[device_index].last_connected = (uint32_t)(esp_timer_get_time() / 1000000); // Convert to seconds
// Save updated device
ret = nvs_save_paired_device(&devices[device_index]);
ret = system_savePairedDevice(&devices[device_index]);
if (ret == ESP_OK) {
char bda_str[18];
ESP_LOGI(BT_AV_TAG, "Updated connection timestamp for device: %s (%s)",
@@ -541,11 +401,11 @@ static esp_err_t nvs_update_connection_timestamp(esp_bd_addr_t bda)
return ret;
}
static esp_err_t nvs_try_connect_all_known_devices(void)
static esp_err_t bt_try_connect_all_known_devices(void)
{
// Load all known devices into cache
s_known_device_count = MAX_PAIRED_DEVICES;
esp_err_t ret = nvs_load_paired_devices(s_known_devices, &s_known_device_count);
esp_err_t ret = system_loadPairedDevices(s_known_devices, &s_known_device_count);
if (ret != ESP_OK || s_known_device_count == 0) {
ESP_LOGI(BT_AV_TAG, "No known devices to connect to");
s_current_device_index = -1;
@@ -587,7 +447,7 @@ static esp_err_t nvs_try_connect_all_known_devices(void)
return ret;
}
static esp_err_t nvs_try_next_known_device(void)
static esp_err_t bt_try_next_known_device(void)
{
if (s_current_device_index < 0 || s_known_device_count == 0) {
ESP_LOGI(BT_AV_TAG, "No more devices to try");
@@ -714,7 +574,7 @@ static void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param)
get_name_from_eir(eir, s_peer_bdname, NULL);
// Save discovered audio device to NVS (but don't connect to it)
nvs_add_discovered_device(param->disc_res.bda, (char *)s_peer_bdname);
bt_add_discovered_device(param->disc_res.bda, (char *)s_peer_bdname);
// Add to device list for GUI
add_device_to_list(param->disc_res.bda, (char *)s_peer_bdname, false, rssi);
@@ -754,7 +614,8 @@ static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
} else {
/* not discovered, continue to discover */
ESP_LOGI(BT_AV_TAG, "Device discovery failed, continue to discover...");
esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
s_a2d_state = APP_AV_STATE_UNCONNECTED;
//esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
}
} else if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STARTED) {
ESP_LOGI(BT_AV_TAG, "Discovery started.");
@@ -854,10 +715,10 @@ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
esp_bt_gap_get_device_name();
// Print list of saved devices from NVS
nvs_debug_print_known_devices();
bt_debug_print_known_devices();
// Try to connect to all known devices sequentially
esp_err_t connect_ret = nvs_try_connect_all_known_devices();
esp_err_t connect_ret = bt_try_connect_all_known_devices();
if (connect_ret != ESP_OK) {
// No known devices found, start discovery to find new devices
ESP_LOGI(BT_AV_TAG, "No known devices found, starting discovery...");
@@ -1222,7 +1083,8 @@ static void bt_app_av_state_unconnected_hdlr(uint16_t event, void *param)
break;
case BT_APP_HEART_BEAT_EVT: {
// Try to connect to known devices, or start discovery if none available
esp_err_t connect_ret = nvs_try_connect_all_known_devices();
esp_err_t connect_ret = bt_try_connect_all_known_devices();
if (connect_ret != ESP_OK) {
// No known devices, start discovery
ESP_LOGI(BT_AV_TAG, "No known devices available, starting discovery...");
@@ -1257,14 +1119,17 @@ static void bt_app_av_state_connecting_hdlr(uint16_t event, void *param)
s_media_state = APP_AV_MEDIA_STATE_IDLE;
// Update connection timestamp for this device
nvs_update_connection_timestamp(s_peer_bda);
system_updateConnectionTimestamp(s_peer_bda);
} else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
ESP_LOGI(BT_AV_TAG, "Connection failed, trying next device...");
// Try next known device before giving up
esp_err_t next_ret = nvs_try_next_known_device();
esp_err_t next_ret = bt_try_next_known_device();
if (next_ret != ESP_OK) {
// No more devices to try, go to unconnected state
s_a2d_state = APP_AV_STATE_UNCONNECTED;
//s_a2d_state = APP_AV_STATE_UNCONNECTED;
ESP_LOGI(BT_AV_TAG, "No known devices available, starting discovery...");
s_a2d_state = APP_AV_STATE_DISCOVERING;
esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
}
}
break;
@@ -1281,7 +1146,7 @@ static void bt_app_av_state_connecting_hdlr(uint16_t event, void *param)
if (++s_connecting_intv >= 2) {
ESP_LOGI(BT_AV_TAG, "Connection timeout, trying next device...");
// Try next known device before giving up
esp_err_t next_ret = nvs_try_next_known_device();
esp_err_t next_ret = bt_try_next_known_device();
if (next_ret != ESP_OK) {
// No more devices to try, go to unconnected state
s_a2d_state = APP_AV_STATE_UNCONNECTED;
@@ -1834,7 +1699,7 @@ static void load_paired_devices_to_list(void) {
size_t count = MAX_PAIRED_DEVICES;
ESP_LOGI(BT_AV_TAG, "Attempting to load paired devices from NVS");
esp_err_t err = nvs_load_paired_devices(paired_devices, &count);
esp_err_t err = system_loadPairedDevices(paired_devices, &count);
if (err == ESP_OK) {
ESP_LOGI(BT_AV_TAG, "Successfully loaded %d paired devices from NVS", (int)count);
for (size_t i = 0; i < count; i++) {