From fb45ee7a4cbcb0793d092c5f191db9a62f206fbd Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Fri, 21 Apr 2017 11:04:53 +0800 Subject: [PATCH] Component/bt: add new cmd for blufi ,ESP32 close a gatt connection --- components/bt/bluedroid/api/esp_blufi_api.c | 15 +++++++++++++++ .../bt/bluedroid/api/include/esp_blufi_api.h | 15 +++++++++++++++ .../bluedroid/btc/profile/esp/blufi/blufi_prf.c | 5 +++++ .../btc/profile/esp/blufi/blufi_protocol.c | 6 ++++++ .../btc/profile/esp/blufi/include/blufi_int.h | 1 + docs/api-reference/bluetooth/esp_blufi.rst | 1 + .../bluetooth/blufi/main/blufi_example_main.c | 9 +++++++++ 7 files changed, 52 insertions(+) diff --git a/components/bt/bluedroid/api/esp_blufi_api.c b/components/bt/bluedroid/api/esp_blufi_api.c index 2fcbbab794..b735ad549d 100644 --- a/components/bt/bluedroid/api/esp_blufi_api.c +++ b/components/bt/bluedroid/api/esp_blufi_api.c @@ -21,6 +21,7 @@ #include "btc_manage.h" #include "btc_main.h" #include "future.h" +#include "btc_gatts.h" esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks) { @@ -92,3 +93,17 @@ uint16_t esp_blufi_get_version(void) return btc_blufi_get_version(); } +esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id) +{ + btc_msg_t msg; + btc_ble_gatts_args_t arg; + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GATTS; + msg.act = BTC_GATTS_ACT_CLOSE; + arg.close.conn_id = conn_id; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/bluedroid/api/include/esp_blufi_api.h b/components/bt/bluedroid/api/include/esp_blufi_api.h index 6da6512163..8ab9ba3504 100644 --- a/components/bt/bluedroid/api/include/esp_blufi_api.h +++ b/components/bt/bluedroid/api/include/esp_blufi_api.h @@ -49,6 +49,7 @@ typedef enum { ESP_BLUFI_EVENT_RECV_SERVER_CERT, /*conn.remote_bda, sizeof(esp_bd_addr_t)); + param.connect.conn_id=p_data->conn.conn_id; + param.connect.server_if=p_data->conn.server_if; btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); break; } @@ -767,6 +769,9 @@ void btc_blufi_cb_handler(btc_msg_t *msg) case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY: btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY, param); break; + case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE: + btc_blufi_cb_to_app(ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE, param); + break; default: LOG_ERROR("%s UNKNOWN %d\n", __func__, msg->act); break; diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c index c66649bf09..ca2a847d0a 100644 --- a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c +++ b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c @@ -100,6 +100,12 @@ void btc_blufi_protocol_handler(uint8_t type, uint8_t *data, int len) btc_blufi_send_encap(type, &data[0], sizeof(data)); break; } + case BLUFI_TYPE_CTRL_SUBTYPE_DISCONNECT_BLE: + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE; + btc_transfer_context(&msg, NULL, 0, NULL); + break; default: LOG_ERROR("%s Unkown Ctrl pkt %02x\n", __func__, type); break; diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h b/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h index c8b002348d..bfac529422 100644 --- a/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h +++ b/components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h @@ -92,6 +92,7 @@ typedef struct blufi_frag_hdr blufi_frag_hdr_t; #define BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_STATUS 0x05 #define BLUFI_TYPE_CTRL_SUBTYPE_DEAUTHENTICATE_STA 0x06 #define BLUFI_TYPE_CTRL_SUBTYPE_GET_VERSION 0x07 +#define BLUFI_TYPE_CTRL_SUBTYPE_DISCONNECT_BLE 0x08 #define BLUFI_TYPE_DATA 0x1 #define BLUFI_TYPE_DATA_SUBTYPE_NEG 0x00 diff --git a/docs/api-reference/bluetooth/esp_blufi.rst b/docs/api-reference/bluetooth/esp_blufi.rst index 5a1b1595a9..bc95abf9fc 100644 --- a/docs/api-reference/bluetooth/esp_blufi.rst +++ b/docs/api-reference/bluetooth/esp_blufi.rst @@ -125,4 +125,5 @@ Functions .. doxygenfunction:: esp_blufi_profile_deinit .. doxygenfunction:: esp_blufi_send_wifi_conn_report .. doxygenfunction:: esp_blufi_get_version +.. doxygenfunction:: esp_blufi_close diff --git a/examples/bluetooth/blufi/main/blufi_example_main.c b/examples/bluetooth/blufi/main/blufi_example_main.c index 2a3f2ce629..607c812a9d 100644 --- a/examples/bluetooth/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/blufi/main/blufi_example_main.c @@ -88,6 +88,9 @@ static uint8_t gl_sta_bssid[6]; static uint8_t gl_sta_ssid[32]; static int gl_sta_ssid_len; +/* connect infor*/ +static uint8_t server_if; +static uint16_t conn_id; static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) { wifi_mode_t mode; @@ -178,6 +181,8 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para break; case ESP_BLUFI_EVENT_BLE_CONNECT: BLUFI_INFO("BLUFI ble connect\n"); + server_if=param->connect.server_if; + conn_id=param->connect.conn_id; esp_ble_gap_stop_advertising(); blufi_security_deinit(); blufi_security_init(); @@ -218,6 +223,10 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para break; } + case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE: + BLUFI_INFO("blufi close a gatt connection"); + esp_blufi_close(server_if,conn_id); + break; case ESP_BLUFI_EVENT_DEAUTHENTICATE_STA: /* TODO */ break; -- 2.40.0