Skip to content

Commit

Permalink
Merge branch 'bugfix/changes_in_wifi_prov_mgr' into 'master'
Browse files Browse the repository at this point in the history
fix(wifi_prov_mgr): Api to keep BLE on after provisioning

Closes IDF-8295

See merge request espressif/esp-idf!32764
  • Loading branch information
rahult-github committed Aug 21, 2024
2 parents ad3a257 + 6e6fd2f commit 5b56ff6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 19 deletions.
6 changes: 6 additions & 0 deletions components/protocomm/include/transports/protocomm_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ typedef struct protocomm_ble_config {
* BLE address
*/
uint8_t *ble_addr;

/**
* Flag to keep BLE on
*/
unsigned keep_ble_on:1;

} protocomm_ble_config_t;

/**
Expand Down
5 changes: 5 additions & 0 deletions components/protocomm/src/simple_ble/simple_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ static esp_bd_addr_t s_cached_remote_bda = {0x0,};
#define adv_config_flag (1 << 0)
#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 *uuid128_ptr;
Expand Down
12 changes: 12 additions & 0 deletions components/protocomm/src/simple_ble/simple_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,22 @@ typedef struct {
unsigned ble_sm_sc:1;
/** BLE Address */
uint8_t *ble_addr;
/** Flag to keep BLE on */
unsigned keep_ble_on:1;

} 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
*
* This function allocates memory and returns a pointer to the
Expand Down
22 changes: 14 additions & 8 deletions components/protocomm/src/transports/protocomm_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_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) {
ble_config->ble_addr = protocomm_ble_addr;
}
Expand Down Expand Up @@ -630,23 +633,26 @@ esp_err_t protocomm_ble_stop(protocomm_t *pc)
(pc == protoble_internal->pc_ble)) {
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
/* Keep BT stack on, but terminate the connection after provisioning */
ret = simple_ble_disconnect();
if (ret) {
ESP_LOGE(TAG, "BLE disconnect failed");
}
simple_ble_deinit();
ret = simple_ble_disconnect();
if (ret) {
ESP_LOGE(TAG, "BLE disconnect failed");
}
simple_ble_deinit();
#endif // CONFIG_ESP_PROTOCOMM_DISCONNECT_AFTER_BLE_STOP
#else
}
else {

/* If flag is not enabled, stop the stack. */
ret = simple_ble_stop();
if (ret) {
ESP_LOGE(TAG, "BLE stop failed");
}
simple_ble_deinit();
#endif // CONFIG_ESP_PROTOCOMM_KEEP_BLE_ON_AFTER_BLE_STOP
}

protocomm_ble_cleanup();
return ret;
Expand Down
25 changes: 15 additions & 10 deletions components/protocomm/src/transports/protocomm_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ typedef struct {
unsigned ble_link_encryption:1;
/** BLE address */
uint8_t *ble_addr;
/** Flag to keep BLE on */
unsigned keep_ble_on:1;
} simple_ble_cfg_t;

static simple_ble_cfg_t *ble_cfg_p;
Expand Down Expand Up @@ -1003,6 +1005,8 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con
return ESP_ERR_NO_MEM;
}

ble_config->keep_ble_on = config->keep_ble_on;

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());

Expand Down Expand Up @@ -1031,23 +1035,24 @@ esp_err_t protocomm_ble_stop(protocomm_t *pc)
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
/* Keep BT stack on, but terminate the connection after provisioning */
rc = ble_gap_terminate(s_cached_conn_handle, BLE_ERR_REM_USER_CONN_TERM);
if (rc) {
ESP_LOGI(TAG, "Error in terminating connection rc = %d",rc);
}
free_gatt_ble_misc_memory(ble_cfg_p);
/* Keep BT stack on, but terminate the connection after provisioning */
rc = ble_gap_terminate(s_cached_conn_handle, BLE_ERR_REM_USER_CONN_TERM);
if (rc) {
ESP_LOGI(TAG, "Error in terminating connection rc = %d",rc);
}
free_gatt_ble_misc_memory(ble_cfg_p);
#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();
if (ret == 0) {
nimble_port_deinit();
}
free_gatt_ble_misc_memory(ble_cfg_p);
#endif // CONFIG_ESP_PROTOCOMM_KEEP_BLE_ON_AFTER_BLE_STOP
}

protocomm_ble_cleanup();
return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
* @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
*
Expand Down
2 changes: 1 addition & 1 deletion components/wifi_provisioning/src/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static struct wifi_prov_mgr_ctx *prov_ctx;

/* 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()
* either inside the event callbacks or from another thread. Therefore
* post execution of execute_event_cb(), the validity of prov_ctx must
Expand Down
14 changes: 14 additions & 0 deletions components/wifi_provisioning/src/scheme_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ extern const wifi_prov_scheme_t wifi_prov_scheme_ble;

static uint8_t *custom_service_uuid;
static uint8_t *custom_ble_addr;
static uint8_t custom_keep_ble_on;

static uint8_t *custom_manufacturer_data;
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)
{
if (!pc) {
Expand All @@ -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;

ble_config->keep_ble_on = custom_keep_ble_on;

#if defined(CONFIG_WIFI_PROV_BLE_BONDING)
ble_config->ble_bonding = 1;
#endif
Expand Down

0 comments on commit 5b56ff6

Please sign in to comment.