fix(wifi_prov_mgr): Api to keep BLE on after provisioning

This commit is contained in:
shreeyash 2024-07-29 14:27:45 +05:30
parent 1ddd2f42b5
commit 6e6fd2f6a3
8 changed files with 90 additions and 19 deletions

View File

@ -142,6 +142,12 @@ typedef struct protocomm_ble_config {
* BLE address * BLE address
*/ */
uint8_t *ble_addr; uint8_t *ble_addr;
/**
* Flag to keep BLE on
*/
unsigned keep_ble_on:1;
} protocomm_ble_config_t; } protocomm_ble_config_t;
/** /**

View File

@ -31,6 +31,11 @@ static esp_bd_addr_t s_cached_remote_bda = {0x0,};
#define adv_config_flag (1 << 0) #define adv_config_flag (1 << 0)
#define scan_rsp_config_flag (1 << 1) #define scan_rsp_config_flag (1 << 1)
uint8_t get_keep_ble_on()
{
return g_ble_cfg_p->keep_ble_on;
}
const uint8_t *simple_ble_get_uuid128(uint16_t handle) const uint8_t *simple_ble_get_uuid128(uint16_t handle)
{ {
const uint8_t *uuid128_ptr; const uint8_t *uuid128_ptr;

View File

@ -53,10 +53,22 @@ typedef struct {
unsigned ble_sm_sc:1; unsigned ble_sm_sc:1;
/** BLE Address */ /** BLE Address */
uint8_t *ble_addr; uint8_t *ble_addr;
/** Flag to keep BLE on */
unsigned keep_ble_on:1;
} simple_ble_cfg_t; } simple_ble_cfg_t;
/**
* @brief Get the current BLE keep-on status
*
* This function returns the current value of the `keep_ble_on` flag
* from the global BLE configuration structure.
*
* @return uint8_t Current status of the `keep_ble_on` flag
*/
uint8_t get_keep_ble_on(void);
/** Initialize a simple ble connection /** Initialize a simple ble connection
* *
* This function allocates memory and returns a pointer to the * This function allocates memory and returns a pointer to the

View File

@ -599,6 +599,9 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con
ble_config->ble_bonding = config->ble_bonding; ble_config->ble_bonding = config->ble_bonding;
ble_config->ble_sm_sc = config->ble_sm_sc; ble_config->ble_sm_sc = config->ble_sm_sc;
/* Set parameter to keep BLE on */
ble_config->keep_ble_on = config->keep_ble_on;
if (config->ble_addr != NULL) { if (config->ble_addr != NULL) {
ble_config->ble_addr = protocomm_ble_addr; ble_config->ble_addr = protocomm_ble_addr;
} }
@ -630,23 +633,26 @@ esp_err_t protocomm_ble_stop(protocomm_t *pc)
(pc == protoble_internal->pc_ble)) { (pc == protoble_internal->pc_ble)) {
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
#ifdef CONFIG_ESP_PROTOCOMM_KEEP_BLE_ON_AFTER_BLE_STOP uint8_t protocomm_keep_ble_on = get_keep_ble_on();
if (protocomm_keep_ble_on) {
#ifdef CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP #ifdef CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP
/* Keep BT stack on, but terminate the connection after provisioning */ /* Keep BT stack on, but terminate the connection after provisioning */
ret = simple_ble_disconnect(); ret = simple_ble_disconnect();
if (ret) { if (ret) {
ESP_LOGE(TAG, "BLE disconnect failed"); ESP_LOGE(TAG, "BLE disconnect failed");
} }
simple_ble_deinit(); simple_ble_deinit();
#endif // CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP #endif // CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP
#else }
else {
/* If flag is not enabled, stop the stack. */ /* If flag is not enabled, stop the stack. */
ret = simple_ble_stop(); ret = simple_ble_stop();
if (ret) { if (ret) {
ESP_LOGE(TAG, "BLE stop failed"); ESP_LOGE(TAG, "BLE stop failed");
} }
simple_ble_deinit(); simple_ble_deinit();
#endif // CONFIG_ESP_PROTOCOMM_KEEP_BLE_ON_AFTER_BLE_STOP }
protocomm_ble_cleanup(); protocomm_ble_cleanup();
return ret; return ret;

View File

@ -133,6 +133,8 @@ typedef struct {
unsigned ble_link_encryption:1; unsigned ble_link_encryption:1;
/** BLE address */ /** BLE address */
uint8_t *ble_addr; uint8_t *ble_addr;
/** Flag to keep BLE on */
unsigned keep_ble_on:1;
} simple_ble_cfg_t; } simple_ble_cfg_t;
static simple_ble_cfg_t *ble_cfg_p; static simple_ble_cfg_t *ble_cfg_p;
@ -1003,6 +1005,8 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con
return ESP_ERR_NO_MEM; return ESP_ERR_NO_MEM;
} }
ble_config->keep_ble_on = config->keep_ble_on;
esp_err_t err = simple_ble_start(ble_config); esp_err_t err = simple_ble_start(ble_config);
ESP_LOGD(TAG, "Free Heap size after simple_ble_start= %" PRIu32, esp_get_free_heap_size()); ESP_LOGD(TAG, "Free Heap size after simple_ble_start= %" PRIu32, esp_get_free_heap_size());
@ -1031,23 +1035,24 @@ esp_err_t protocomm_ble_stop(protocomm_t *pc)
rc); rc);
} }
#ifdef CONFIG_ESP_PROTOCOMM_KEEP_BLE_ON_AFTER_BLE_STOP if (ble_cfg_p->keep_ble_on) {
#ifdef CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP #ifdef CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP
/* Keep BT stack on, but terminate the connection after provisioning */ /* Keep BT stack on, but terminate the connection after provisioning */
rc = ble_gap_terminate(s_cached_conn_handle, BLE_ERR_REM_USER_CONN_TERM); rc = ble_gap_terminate(s_cached_conn_handle, BLE_ERR_REM_USER_CONN_TERM);
if (rc) { if (rc) {
ESP_LOGI(TAG, "Error in terminating connection rc = %d",rc); ESP_LOGI(TAG, "Error in terminating connection rc = %d",rc);
} }
free_gatt_ble_misc_memory(ble_cfg_p); free_gatt_ble_misc_memory(ble_cfg_p);
#endif // CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP #endif // CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP
#else }
/* If flag is enabled, don't stop the stack. User application can start a new advertising to perform its BT activities */ else {
/* If flag is enabled, don't stop the stack. User application can start a new advertising to perform its BT activities */
ret = nimble_port_stop(); ret = nimble_port_stop();
if (ret == 0) { if (ret == 0) {
nimble_port_deinit(); nimble_port_deinit();
} }
free_gatt_ble_misc_memory(ble_cfg_p); free_gatt_ble_misc_memory(ble_cfg_p);
#endif // CONFIG_ESP_PROTOCOMM_KEEP_BLE_ON_AFTER_BLE_STOP }
protocomm_ble_cleanup(); protocomm_ble_cleanup();
return ret; return ret;

View File

@ -69,6 +69,29 @@ void wifi_prov_scheme_ble_event_cb_free_bt (void *user_data, wifi_prov_cb_event
*/ */
esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128); esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128);
/**
* @brief Keep the BLE on after provisioning
*
* This API is used to specify whether the BLE should remain on
* after the provisioning process has stopped.
*
* This must be called before starting provisioning, i.e. before
* making a call to wifi_prov_mgr_start_provisioning(), otherwise
* the default behavior will be used.
*
* @note The value being pointed to by the argument must be valid
* at least until provisioning is started. Upon start, the
* manager will store an internal copy of this value, and
* this data can be freed or invalidated afterwards.
*
* @param[in] is_on_after_ble_stop A boolean indicating if BLE should remain on after provisioning
*
* @return
* - ESP_OK : Success
* - ESP_ERR_INVALID_ARG : Null argument
*/
esp_err_t wifi_prov_mgr_keep_ble_on(uint8_t is_on_after_ble_stop);
/** /**
* @brief Set manufacturer specific data in scan response * @brief Set manufacturer specific data in scan response
* *

View File

@ -156,7 +156,7 @@ static struct wifi_prov_mgr_ctx *prov_ctx;
/* This executes registered app_event_callback for a particular event /* This executes registered app_event_callback for a particular event
* *
* NOTE : By the time this fucntion returns, it is possible that * NOTE : By the time this function returns, it is possible that
* the manager got de-initialized due to a call to wifi_prov_mgr_deinit() * the manager got de-initialized due to a call to wifi_prov_mgr_deinit()
* either inside the event callbacks or from another thread. Therefore * either inside the event callbacks or from another thread. Therefore
* post execution of execute_event_cb(), the validity of prov_ctx must * post execution of execute_event_cb(), the validity of prov_ctx must

View File

@ -23,9 +23,21 @@ extern const wifi_prov_scheme_t wifi_prov_scheme_ble;
static uint8_t *custom_service_uuid; static uint8_t *custom_service_uuid;
static uint8_t *custom_ble_addr; static uint8_t *custom_ble_addr;
static uint8_t custom_keep_ble_on;
static uint8_t *custom_manufacturer_data; static uint8_t *custom_manufacturer_data;
static size_t custom_manufacturer_data_len; static size_t custom_manufacturer_data_len;
esp_err_t wifi_prov_mgr_keep_ble_on(uint8_t is_on_after_ble_stop)
{
if (!is_on_after_ble_stop) {
return ESP_ERR_INVALID_ARG;
}
custom_keep_ble_on = is_on_after_ble_stop;
return ESP_OK;
}
static esp_err_t prov_start(protocomm_t *pc, void *config) static esp_err_t prov_start(protocomm_t *pc, void *config)
{ {
if (!pc) { if (!pc) {
@ -40,6 +52,8 @@ static esp_err_t prov_start(protocomm_t *pc, void *config)
protocomm_ble_config_t *ble_config = (protocomm_ble_config_t *) config; protocomm_ble_config_t *ble_config = (protocomm_ble_config_t *) config;
ble_config->keep_ble_on = custom_keep_ble_on;
#if defined(CONFIG_WIFI_PROV_BLE_BONDING) #if defined(CONFIG_WIFI_PROV_BLE_BONDING)
ble_config->ble_bonding = 1; ble_config->ble_bonding = 1;
#endif #endif