diff --git a/components/protocomm/include/transports/protocomm_ble.h b/components/protocomm/include/transports/protocomm_ble.h index cbc7c81bd5..04af25f9fa 100644 --- a/components/protocomm/include/transports/protocomm_ble.h +++ b/components/protocomm/include/transports/protocomm_ble.h @@ -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; /** diff --git a/components/protocomm/src/simple_ble/simple_ble.c b/components/protocomm/src/simple_ble/simple_ble.c index ac33cf2409..f892245170 100644 --- a/components/protocomm/src/simple_ble/simple_ble.c +++ b/components/protocomm/src/simple_ble/simple_ble.c @@ -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; diff --git a/components/protocomm/src/simple_ble/simple_ble.h b/components/protocomm/src/simple_ble/simple_ble.h index 259225de83..dd3858096f 100644 --- a/components/protocomm/src/simple_ble/simple_ble.h +++ b/components/protocomm/src/simple_ble/simple_ble.h @@ -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 diff --git a/components/protocomm/src/transports/protocomm_ble.c b/components/protocomm/src/transports/protocomm_ble.c index 6fedd85abb..a780319dde 100644 --- a/components/protocomm/src/transports/protocomm_ble.c +++ b/components/protocomm/src/transports/protocomm_ble.c @@ -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; } @@ -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; diff --git a/components/protocomm/src/transports/protocomm_nimble.c b/components/protocomm/src/transports/protocomm_nimble.c index 8aa2e4ec6d..c95b518622 100644 --- a/components/protocomm/src/transports/protocomm_nimble.c +++ b/components/protocomm/src/transports/protocomm_nimble.c @@ -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; @@ -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()); @@ -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; diff --git a/components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h b/components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h index c30a3a86b8..c4872d732d 100644 --- a/components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h +++ b/components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h @@ -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 * diff --git a/components/wifi_provisioning/src/manager.c b/components/wifi_provisioning/src/manager.c index c3b0538306..a4e4d35b63 100644 --- a/components/wifi_provisioning/src/manager.c +++ b/components/wifi_provisioning/src/manager.c @@ -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 diff --git a/components/wifi_provisioning/src/scheme_ble.c b/components/wifi_provisioning/src/scheme_ble.c index f7035cb48b..22782762a6 100644 --- a/components/wifi_provisioning/src/scheme_ble.c +++ b/components/wifi_provisioning/src/scheme_ble.c @@ -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) { @@ -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