]> granicus.if.org Git - esp-idf/commitdiff
Component/bt: add new cmd for blufi ,ESP32 close a gatt connection
authorzhiweijian <zhiweijian@espressif.com>
Fri, 21 Apr 2017 03:04:53 +0000 (11:04 +0800)
committerzhiweijian <zhiweijian@espressif.com>
Thu, 4 May 2017 12:31:58 +0000 (20:31 +0800)
components/bt/bluedroid/api/esp_blufi_api.c
components/bt/bluedroid/api/include/esp_blufi_api.h
components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c
components/bt/bluedroid/btc/profile/esp/blufi/blufi_protocol.c
components/bt/bluedroid/btc/profile/esp/blufi/include/blufi_int.h
docs/api-reference/bluetooth/esp_blufi.rst
examples/bluetooth/blufi/main/blufi_example_main.c

index 2fcbbab794822015e38c3c82a13c974c63516c56..b735ad549d141917cfa09f39bbab4eaa1c25015b 100644 (file)
@@ -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);
+}
index 6da6512163137c7e65762b4d227a8069ead5f516..8ab9ba350427c5e4477018709e04a258873264ad 100644 (file)
@@ -49,6 +49,7 @@ typedef enum {
     ESP_BLUFI_EVENT_RECV_SERVER_CERT,                    /*<! When Phone send Server certificate to ESP32, this event happen */
     ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY,                /*<! When Phone send Client Private key to ESP32, this event happen */
     ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY,                /*<! When Phone send Server Private key to ESP32, this event happen */
+    ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE,           /*<! When Phone send Disconnect key to ESP32, this event happen */
 } esp_blufi_cb_event_t;
 
 /// BLUFI config status
@@ -122,6 +123,8 @@ typedef union {
         */
     struct blufi_connect_evt_param {
         esp_bd_addr_t remote_bda;                   /*!< Blufi Remote bluetooth device address */
+        uint8_t    server_if;                       /*!< server interface */
+        uint16_t   conn_id;                         /*!< Connection id */
     } connect;                                                                     /*!< Blufi callback param of ESP_BLUFI_EVENT_CONNECT */
 
     /**
@@ -353,6 +356,18 @@ esp_err_t esp_blufi_send_wifi_conn_report(wifi_mode_t opmode, esp_blufi_sta_conn
  */
 uint16_t esp_blufi_get_version(void);
 
+/**
+ * @brief           Close a connection  a remote device.
+ *
+ * @param[in]       gatts_if: GATT server access interface
+ * @param[in]       conn_id: connection ID to be closed.
+ *
+ * @return
+ *                  - ESP_OK : success
+ *                  - other  : failed
+ *
+ */
+esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id);
 #ifdef __cplusplus
 }
 #endif
index 117ff0b03e61401951aedead2e6200e2a6a99fab..812ef2a2d2450c5b0d342ecb0bae81f35fed5cd1 100644 (file)
@@ -253,6 +253,8 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
         msg.pid = BTC_PID_BLUFI;
         msg.act = ESP_BLUFI_EVENT_BLE_CONNECT;
         memcpy(param.connect.remote_bda, p_data->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, &param, 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;
index c66649bf09819bf49ae2c77c1b74a2b9bdc4e202..ca2a847d0a84875d814b742cca27f28fa226c13a 100644 (file)
@@ -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;
index c8b002348db4315529468656a72e73e2e43e5f82..bfac5294229705883a9782642911e5d732ef23a9 100644 (file)
@@ -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
index 5a1b1595a9258571836355a69d0472daad289099..bc95abf9fcfb559069e973dc141ce05de43d11ec 100644 (file)
@@ -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
 
index 2a3f2ce62935c5275924a0fc7f6a0cf60d8a4a90..607c812a9d783165be92bc3f9633102972b20f76 100644 (file)
@@ -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;