]> granicus.if.org Git - esp-idf/commitdiff
component/bt: bug fix of lack of checking bluetooth stack status inside API functions
authorwangmengyang <wangmengyang@espressif.com>
Wed, 11 Jan 2017 03:04:41 +0000 (11:04 +0800)
committerwangmengyang <wangmengyang@espressif.com>
Wed, 11 Jan 2017 03:04:41 +0000 (11:04 +0800)
12 files changed:
components/bt/bluedroid/api/esp_blufi_api.c
components/bt/bluedroid/api/esp_bt_device.c
components/bt/bluedroid/api/esp_bt_main.c
components/bt/bluedroid/api/esp_gap_ble_api.c
components/bt/bluedroid/api/esp_gattc_api.c
components/bt/bluedroid/api/esp_gatts_api.c
components/bt/bluedroid/api/include/esp_bt_device.h
components/bt/bluedroid/api/include/esp_bt_main.h
components/bt/bluedroid/btc/core/btc_main.c
docs/api/bt.rst
docs/api/esp_blufi.rst
docs/api/esp_bt_main.rst

index 094fbfae5fd9c6038a94dc036f6467606c19aec6..ac84ee3fc2413f4e4a5ad19e557b4314c6307b17 100644 (file)
 
 esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks)
 {
+    if (ESP_BLUEDROID_STATUS_UNINITIALIZED == esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     if (callbacks == NULL) {
         return ESP_FAIL;
     }
@@ -37,6 +41,10 @@ esp_err_t esp_blufi_send_wifi_conn_report(wifi_mode_t opmode, esp_blufi_sta_conn
     btc_msg_t msg;
     btc_blufi_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_BLUFI;
     msg.act = BTC_BLUFI_ACT_SEND_CFG_REPORT;
@@ -53,6 +61,10 @@ esp_err_t esp_blufi_profile_init(void)
 {
     btc_msg_t msg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_BLUFI;
     msg.act = BTC_BLUFI_ACT_INIT;
@@ -64,6 +76,10 @@ esp_err_t esp_blufi_profile_deinit(void)
 {
     btc_msg_t msg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_BLUFI;
     msg.act = BTC_BLUFI_ACT_DEINIT;
index 745e446c232a3f09473f27e93fb4882f38dac1ba..1eb48c98acb78d276610ffbb6875e3f2fa015ed8 100644 (file)
 
 
 #include "esp_bt_device.h"
+#include "esp_bt_main.h"
 #include "controller.h"
 
 const uint8_t *esp_bt_dev_get_address(void)
 {
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return NULL;
+    }
     return controller_get_interface()->get_address()->address;
 }
index 2dd9166d2298933875bf44d04041418f156698bb..3093b56403da7831448fac165ee6a6d0c174f445 100644 (file)
 static bool esp_already_enable = false;
 static bool esp_already_init = false;
 
+esp_bluedroid_status_t esp_bluedroid_get_status(void)
+{
+    if (esp_already_init) {
+       if (esp_already_enable) {
+           return ESP_BLUEDROID_STATUS_ENABLED;
+       } else {
+           return ESP_BLUEDROID_STATUS_INITIALIZED;
+       }
+    } else {
+       return ESP_BLUEDROID_STATUS_UNINITIALIZED;
+    }
+}
+
 esp_err_t esp_bluedroid_enable(void)
 {
     btc_msg_t msg;
@@ -114,7 +127,7 @@ esp_err_t esp_bluedroid_init(void)
         return ESP_FAIL;
     }
 
-    esp_already_init = true;;
+    esp_already_init = true;
 
     return ESP_OK;
 }
index 6e18f65822b5aab71576adeef1819c134d548ec5..34914f8b4dc0eaca2ed3f2a7fe5307ea2166d18d 100644 (file)
@@ -14,6 +14,7 @@
 
 #include <string.h>
 
+#include "esp_bt_main.h"
 #include "esp_gap_ble_api.h"
 #include "bta_api.h"
 #include "bt_trace.h"
@@ -23,6 +24,9 @@
 
 esp_err_t esp_ble_gap_register_callback(esp_gap_ble_cb_t callback)
 {
+    if (ESP_BLUEDROID_STATUS_UNINITIALIZED == esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
     return (btc_profile_cb_set(BTC_PID_GAP_BLE, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
 
@@ -31,7 +35,11 @@ esp_err_t esp_ble_gap_config_adv_data(esp_ble_adv_data_t *adv_data)
 {
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
-
+    
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     if (adv_data == NULL) {
         return ESP_ERR_INVALID_ARG;
     }
@@ -54,7 +62,11 @@ esp_err_t esp_ble_gap_set_scan_params(esp_ble_scan_params_t *scan_params)
 {
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
-
+    
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     if (scan_params == NULL) {
         return ESP_ERR_INVALID_ARG;
     }
@@ -72,6 +84,10 @@ esp_err_t esp_ble_gap_start_scanning(uint32_t duration)
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GAP_BLE;
     msg.act = BTC_GAP_BLE_ACT_START_SCAN;
@@ -84,7 +100,11 @@ esp_err_t esp_ble_gap_start_scanning(uint32_t duration)
 esp_err_t esp_ble_gap_stop_scanning(void)
 {
     btc_msg_t msg;
-
+    
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GAP_BLE;
     msg.act = BTC_GAP_BLE_ACT_STOP_SCAN;
@@ -96,6 +116,10 @@ esp_err_t esp_ble_gap_start_advertising(esp_ble_adv_params_t *adv_params)
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GAP_BLE;
     msg.act = BTC_GAP_BLE_ACT_START_ADV;
@@ -108,6 +132,10 @@ esp_err_t esp_ble_gap_stop_advertising(void)
 {
     btc_msg_t msg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GAP_BLE;
     msg.act = BTC_GAP_BLE_ACT_STOP_ADV;
@@ -121,6 +149,10 @@ esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params)
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GAP_BLE;
     msg.act = BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM;
@@ -133,7 +165,11 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_
 {
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
-
+    
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GAP_BLE;
     msg.act = BTC_GAP_BLE_ACT_SET_PKT_DATA_LEN;
@@ -148,7 +184,11 @@ esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr)
 {
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
-
+    
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GAP_BLE;
     msg.act = BTC_GAP_BLE_ACT_SET_RAND_ADDRESS;
@@ -163,6 +203,10 @@ esp_err_t esp_ble_gap_config_local_privacy (bool privacy_enable)
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GAP_BLE;
     msg.act = BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY;
@@ -176,6 +220,10 @@ esp_err_t esp_ble_gap_set_device_name(const char *name)
     btc_msg_t msg;
     btc_ble_gap_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     if (strlen(name) > ESP_GAP_DEVICE_NAME_MAX) {
         return ESP_ERR_INVALID_ARG;
     }
index 28fc429575835fcd271bad3174797b527643e193..4baa0a77fc32221163ff40fefd2971f1b088aa92 100644 (file)
 #include <string.h>
 
 #include "esp_gattc_api.h"
+#include "esp_bt_main.h"
 #include "btc_manage.h"
 #include "btc_gattc.h"
 #include "btc_gatt_util.h"
 
 esp_err_t esp_ble_gattc_register_callback(esp_gattc_cb_t callback)
 {
+    if (ESP_BLUEDROID_STATUS_UNINITIALIZED == esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     if (callback == NULL) {
         return ESP_FAIL;
     }
@@ -34,6 +39,10 @@ esp_err_t esp_ble_gattc_app_register(uint16_t app_id)
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     if (app_id > ESP_APP_ID_MAX) {
         return ESP_ERR_INVALID_ARG;
     }
@@ -51,6 +60,10 @@ esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if)
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_APP_UNREGISTER;
@@ -64,6 +77,10 @@ esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, b
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_OPEN;
@@ -79,6 +96,10 @@ esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id)
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_CLOSE;
@@ -92,6 +113,10 @@ esp_err_t esp_ble_gattc_config_mtu (esp_gatt_if_t gattc_if, uint16_t conn_id, ui
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     if ((mtu < ESP_GATT_DEF_BLE_MTU_SIZE) || (mtu > ESP_GATT_MAX_MTU_SIZE)) {
         return ESP_GATT_ILLEGAL_PARAMETER;
     }
@@ -110,6 +135,10 @@ esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_SEARCH_SERVICE;
@@ -133,6 +162,10 @@ esp_err_t esp_ble_gattc_get_characteristic(esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     if (start_char_id) {
@@ -158,6 +191,10 @@ esp_err_t esp_ble_gattc_get_descriptor(esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
 
@@ -185,6 +222,10 @@ esp_err_t esp_ble_gattc_get_included_service(esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
 
@@ -211,6 +252,10 @@ esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_READ_CHAR;
@@ -232,6 +277,10 @@ esp_err_t esp_ble_gattc_read_char_descr (esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_READ_CHAR_DESCR;
@@ -256,6 +305,10 @@ esp_err_t esp_ble_gattc_write_char( esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_WRITE_CHAR;
@@ -283,6 +336,10 @@ esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_WRITE_CHAR_DESCR;
@@ -311,6 +368,10 @@ esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_PREPARE_WRITE;
@@ -330,6 +391,10 @@ esp_err_t esp_ble_gattc_execute_write (esp_gatt_if_t gattc_if, uint16_t conn_id,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_EXECUTE_WRITE;
@@ -347,6 +412,10 @@ esp_gatt_status_t esp_ble_gattc_register_for_notify (esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_REG_FOR_NOTIFY;
@@ -366,6 +435,10 @@ esp_gatt_status_t esp_ble_gattc_unregister_for_notify (esp_gatt_if_t gattc_if,
     btc_msg_t msg;
     btc_ble_gattc_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTC;
     msg.act = BTC_GATTC_ACT_UNREG_FOR_NOTIFY;
index 2504e58f8f3a03ea36b3cae7e1872bcb1c2f3a80..1f0d668e5a96f9ceb02c0d9e1687aebaaaf35394 100644 (file)
@@ -15,6 +15,7 @@
 #include "string.h"
 #include "esp_gatt_defs.h"
 #include "esp_gatts_api.h"
+#include "esp_bt_main.h"
 #include "btc_manage.h"
 #include "btc_gatts.h"
 #include "btc_gatt_util.h"
@@ -23,6 +24,9 @@
 
 esp_err_t esp_ble_gatts_register_callback(esp_gatts_cb_t callback)
 {
+    if (ESP_BLUEDROID_STATUS_UNINITIALIZED == esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
     return (btc_profile_cb_set(BTC_PID_GATTS, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
 
@@ -31,6 +35,10 @@ esp_err_t esp_ble_gatts_app_register(uint16_t app_id)
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     //if (app_id < ESP_APP_ID_MIN || app_id > ESP_APP_ID_MAX) {
     if (app_id > ESP_APP_ID_MAX) {
         return ESP_ERR_INVALID_ARG;
@@ -50,6 +58,10 @@ esp_err_t esp_ble_gatts_app_unregister(esp_gatt_if_t gatts_if)
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_APP_UNREGISTER;
@@ -64,6 +76,10 @@ esp_err_t esp_ble_gatts_create_service(esp_gatt_if_t gatts_if,
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_CREATE_SERVICE;
@@ -80,6 +96,10 @@ esp_err_t esp_ble_gatts_add_included_service(uint16_t service_handle, uint16_t i
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_ADD_INCLUDE_SERVICE;
@@ -96,6 +116,10 @@ esp_err_t esp_ble_gatts_add_char(uint16_t service_handle,  esp_bt_uuid_t  *char_
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_ADD_CHAR;
@@ -115,6 +139,10 @@ esp_err_t esp_ble_gatts_add_char_descr (uint16_t service_handle,
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_ADD_CHAR_DESCR;
@@ -130,6 +158,10 @@ esp_err_t esp_ble_gatts_delete_service(uint16_t service_handle)
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_DELETE_SERVICE;
@@ -143,6 +175,10 @@ esp_err_t esp_ble_gatts_start_service(uint16_t service_handle)
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_START_SERVICE;
@@ -156,6 +192,10 @@ esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle)
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_STOP_SERVICE;
@@ -171,6 +211,10 @@ esp_err_t esp_ble_gatts_send_indicate(esp_gatt_if_t gatts_if, uint16_t conn_id,
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_SEND_INDICATE;
@@ -189,6 +233,10 @@ esp_err_t esp_ble_gatts_send_response(esp_gatt_if_t gatts_if, uint16_t conn_id,
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_SEND_RESPONSE;
@@ -205,6 +253,10 @@ esp_err_t esp_ble_gatts_open(esp_gatt_if_t gatts_if, esp_bd_addr_t remote_bda, b
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+    
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_OPEN;
@@ -220,6 +272,10 @@ esp_err_t esp_ble_gatts_close(esp_gatt_if_t gatts_if, uint16_t conn_id)
     btc_msg_t msg;
     btc_ble_gatts_args_t arg;
 
+    if (ESP_BLUEDROID_STATUS_ENABLED != esp_bluedroid_get_status()) {
+       return ESP_ERR_INVALID_STATE;
+    }
+       
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GATTS;
     msg.act = BTC_GATTS_ACT_CLOSE;
index 51c24f28812f97353d3a7c51424942cd021f7e4b..c84d042d66468341d7da99870ec5f1fb30db3f44 100644 (file)
@@ -26,7 +26,7 @@ extern "C" {
  *
  * @brief      Get bluetooth device address.  Must use after "esp_bluedroid_enable".
  * 
- * @return     bluetooth device address (six bytes)
+ * @return     bluetooth device address (six bytes), or NULL if bluetooth stack is not enabled
  */
 const uint8_t *esp_bt_dev_get_address(void);
 
index b24daf33cd4930673fce89f524319d119818d101..fad010d2c2bf4b005802bc7783c4534de5e004b6 100644 (file)
 extern "C" {
 #endif
 
+/**
+ * @brief Bluetooth stack status type, to indicate whether the bluetooth stack is ready
+ */
+typedef enum {
+    ESP_BLUEDROID_STATUS_UNINITIALIZED   = 0,        /*!< Bluetooth not initialized */
+    ESP_BLUEDROID_STATUS_INITIALIZED,                /*!< Bluetooth initialized but not enabled */
+    ESP_BLUEDROID_STATUS_ENABLED                     /*!< Bluetooth initialized and enabled */
+} esp_bluedroid_status_t;
+
+/**
+ * @brief     Get bluetooth stack status
+ *
+ * @return    Bluetooth stack status
+ *
+ */
+esp_bluedroid_status_t esp_bluedroid_get_status(void);
+    
 /**
  * @brief     Enable bluetooth, must after esp_bluedroid_init()
  *
index 6fae3af43570229b99d24c9f7764b8df814c530a..323a81f75ac98ff755dc22a86cea3318d875f4fe 100644 (file)
@@ -42,14 +42,14 @@ static void btc_sec_callback(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
 static void btc_enable_bluetooth(void)
 {
     if (BTA_EnableBluetooth(btc_sec_callback) != BTA_SUCCESS) {
-        future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_SUCCESS);
+        future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_FAIL);
        }
 }
 
 static void btc_disable_bluetooth(void)
 {
     if (BTA_DisableBluetooth() != BTA_SUCCESS) {
-        future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_SUCCESS);
+        future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_FAIL);
        }
 }
 
index 6a14d40684acfc7fc245d53081ce8cd36bfb36d8..2eae5dd4cf85fcebe7155578991354eba1ed5085 100644 (file)
@@ -6,5 +6,4 @@ Bluetooth
 
    Bluetooth Controller && VHCI <controller_vhci>
    Bluetooth Common <bt_common>
-   Bluetooth Classic
    Bluetooth LE <bt_le>
index f620edeb83ed1f67710f0ca1d2cc09388a94c98d..9dd80851665d02ed2813e1abb78b3b0148e7763d 100644 (file)
@@ -5,9 +5,9 @@ Overview
 --------
 BLUFI is a profile based GATT to config ESP32 WIFI to connect/disconnect AP or setup a softap and etc.
 Use should concern these things: 
-    1. The event sent from profile. Then you need to do something as the event indicate.
-    2. Security reference. You can write your own Security functions such as symmetrical encryption/decryption and checksum functions.
-       Even you can define the "Key Exchange/Negotiation" procedure.
+1. The event sent from profile. Then you need to do something as the event indicate.
+2. Security reference. You can write your own Security functions such as symmetrical encryption/decryption and checksum functions.
+Even you can define the "Key Exchange/Negotiation" procedure.
 
 Application Example
 -------------------
index cc6e80b2e51125b862d79643d20d2858c065b9d5..48bb0c9cc0e89624778c9d2a67ff16b5eb19950c 100644 (file)
@@ -34,6 +34,8 @@ Type Definitions
 Enumerations
 ^^^^^^^^^^^^
 
+.. doxygenenum:: esp_bluedroid_status_t
+
 
 Structures
 ^^^^^^^^^^
@@ -42,6 +44,7 @@ Structures
 Functions
 ^^^^^^^^^
 
+.. doxygenfunction:: esp_bluedroid_get_status
 .. doxygenfunction:: esp_bluedroid_enable
 .. doxygenfunction:: esp_bluedroid_disable
 .. doxygenfunction:: esp_bluedroid_init