]> granicus.if.org Git - esp-idf/commitdiff
component/bt: add open and close event for gatts
authorzhiweijian <zhiweijian@espressif.com>
Thu, 1 Jun 2017 06:16:24 +0000 (14:16 +0800)
committerzhiweijian <zhiweijian@espressif.com>
Thu, 1 Jun 2017 07:01:54 +0000 (15:01 +0800)
1.added open event and close event for gatts.
2.used esp_log_buffer_char/hex for gattc scan result.

components/bt/bluedroid/api/include/esp_gatts_api.h
components/bt/bluedroid/bta/gatt/bta_gatts_act.c
components/bt/bluedroid/bta/include/bta_gatt_api.h
components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c
components/log/log.c
examples/bluetooth/gatt_client/main/gattc_demo.c

index 777bbec3de1b09e8cad4d6a274dda3f9adcedb97..6bbc370a7b6a24c521246f4b46ab98ae74c62850 100644 (file)
@@ -208,12 +208,25 @@ typedef union {
     /**
      * @brief ESP_GATTS_OPEN_EVT
      */
+    struct gatts_open_evt_param {
+        esp_gatt_status_t status;       /*!< Operation status */
+    } open;                             /*!< Gatt server callback param of ESP_GATTS_OPEN_EVT */
+
     /**
      * @brief ESP_GATTS_CANCEL_OPEN_EVT
      */
+    struct gatts_cancel_open_evt_param {
+        esp_gatt_status_t status;       /*!< Operation status */
+    } cancel_open;                      /*!< Gatt server callback param of ESP_GATTS_CANCEL_OPEN_EVT */
+
     /**
      * @brief ESP_GATTS_CLOSE_EVT
      */
+    struct gatts_close_evt_param {
+        esp_gatt_status_t status;       /*!< Operation status */
+        uint16_t conn_id;               /*!< Connection id */
+    } close;                            /*!< Gatt server callback param of ESP_GATTS_CLOSE_EVT */
+
     /**
      * @brief ESP_GATTS_LISTEN_EVT
      */
index 6d75b864a0882ab664908220f1d3c404232d7404..d0e709b77174f0f9dedccb3311473fbd96b2a0f4 100644 (file)
@@ -710,6 +710,7 @@ void bta_gatts_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
     tBTA_GATTS_RCB      *p_rcb = NULL;
     tBTA_GATT_STATUS    status = BTA_GATT_ERROR;
     UINT16              conn_id;
+    tBTA_GATTS_OPEN    open;
     UNUSED(p_cb);
 
     if ((p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_open.server_if)) != NULL) {
@@ -728,7 +729,9 @@ void bta_gatts_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
     }
 
     if (p_rcb && p_rcb->p_cback) {
-        (*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT,  (tBTA_GATTS *)&status);
+        open.status = status;
+        open.server_if = p_msg->api_open.server_if;
+        (*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT,  (tBTA_GATTS *)&open);
     }
 
 }
@@ -745,6 +748,7 @@ void bta_gatts_cancel_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
 {
     tBTA_GATTS_RCB      *p_rcb;
     tBTA_GATT_STATUS    status = BTA_GATT_ERROR;
+    tBTA_GATTS_CANCEL_OPEN   cancel_open;
     UNUSED(p_cb);
 
     if ((p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_cancel_open.server_if)) != NULL) {
@@ -759,7 +763,10 @@ void bta_gatts_cancel_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
     }
 
     if (p_rcb && p_rcb->p_cback) {
-        (*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT,  (tBTA_GATTS *)&status);
+        cancel_open.status = status;
+        cancel_open.server_if = p_msg->api_cancel_open.server_if;
+        (*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT,  (tBTA_GATTS *)&cancel_open);
+
     }
 }
 /*******************************************************************************
@@ -778,7 +785,7 @@ void bta_gatts_close (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
     tGATT_IF            gatt_if;
     BD_ADDR             remote_bda;
     tBTA_GATT_TRANSPORT transport;
-
+    tBTA_GATTS_CLOSE    close;
     UNUSED(p_cb);
 
     if (GATT_GetConnectionInfor(p_msg->hdr.layer_specific, &gatt_if, remote_bda, &transport)) {
@@ -795,7 +802,9 @@ void bta_gatts_close (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
                 bta_sys_conn_close( BTA_ID_GATTS , BTA_ALL_APP_ID, remote_bda);
             }
 
-            (*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT,  (tBTA_GATTS *)&status);
+            close.status = status;
+            close.conn_id = p_msg->hdr.layer_specific;
+            (*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT,  (tBTA_GATTS *)&close);
         }
     } else {
         APPL_TRACE_ERROR("Unknown connection ID: %d", p_msg->hdr.layer_specific);
index 796a0171c25709d8641781557b25503cc39ccacc..8e845951676e3ff4fb8d377b903f6a098143bd25 100644 (file)
@@ -582,6 +582,21 @@ typedef struct {
     tBTA_GATT_STATUS status; /* notification/indication status */
 } tBTA_GATTS_CONF;
 
+typedef struct {
+    tBTA_GATT_STATUS    status;
+    UINT16              conn_id;    /* connection ID */
+} tBTA_GATTS_CLOSE;
+
+typedef struct {
+    tBTA_GATT_STATUS    status;
+    tBTA_GATTS_IF       server_if;
+} tBTA_GATTS_OPEN;
+
+typedef struct {
+    tBTA_GATT_STATUS    status;
+    tBTA_GATTS_IF       server_if;
+} tBTA_GATTS_CANCEL_OPEN;
+
 /* GATTS callback data */
 typedef union {
     tBTA_GATTS_REG_OPER         reg_oper;
@@ -596,6 +611,10 @@ typedef union {
     tBTA_GATTS_CONN             conn;           /* BTA_GATTS_CONN_EVT */
     tBTA_GATTS_CONGEST          congest;        /* BTA_GATTS_CONGEST_EVT callback data */
     tBTA_GATTS_CONF             confirm;        /* BTA_GATTS_CONF_EVT callback data */
+    tBTA_GATTS_CLOSE            close;          /* BTA_GATTS_CLOSE_EVT callback data */
+    tBTA_GATTS_OPEN             open;           /* BTA_GATTS_OPEN_EVT callback data */
+    tBTA_GATTS_CANCEL_OPEN      cancel_open;    /* tBTA_GATTS_CANCEL_OPEN callback data */
+
 } tBTA_GATTS;
 
 /* GATTS enable callback function */
index 7f63ad2cb6c6cf73bede958577b182737a154f52..9161ce8271c84f493fc3e7ccafd8d5db74eeec84 100644 (file)
@@ -765,11 +765,26 @@ void btc_gatts_cb_handler(btc_msg_t *msg)
         btc_gatts_cb_to_app(ESP_GATTS_DISCONNECT_EVT, gatts_if, &param);
         break;
     case BTA_GATTS_OPEN_EVT:
-        // do nothing
+        gatts_if = p_data->open.server_if;
+        param.open.status = p_data->open.status;
+
+        btc_gatts_cb_to_app(BTA_GATTS_OPEN_EVT, gatts_if, &param);
+        break;
     case BTA_GATTS_CANCEL_OPEN_EVT:
-        // do nothing
+        gatts_if = p_data->cancel_open.server_if;
+        param.cancel_open.status = p_data->cancel_open.status;
+
+        btc_gatts_cb_to_app(BTA_GATTS_CANCEL_OPEN_EVT, gatts_if, &param);
+        break;
+
     case BTA_GATTS_CLOSE_EVT:
-        // do nothing
+        gatts_if = BTC_GATT_GET_GATT_IF(p_data->close.conn_id);
+        param.close.status = p_data->close.status;
+        param.close.conn_id = BTC_GATT_GET_CONN_ID(p_data->close.conn_id);
+
+        btc_gatts_cb_to_app(BTA_GATTS_CLOSE_EVT, gatts_if, &param);
+        break;
+
     case BTA_GATTS_LISTEN_EVT:
         // do nothing
         break;
index 26d58b2284b3dd4b7ef48f00c7435285824ed62e..c5c1334d3cafb1eeebab34d1007508279f8befd2 100644 (file)
@@ -335,10 +335,10 @@ void esp_log_buffer_hex(const char *tag, const char *buffer, uint16_t buff_len)
 
 void esp_log_buffer_char(const char *tag, const char *buffer, uint16_t buff_len)
 {
-    char temp_buffer[2*BYTES_PER_LINE + 1] = {0};
+    char temp_buffer[BYTES_PER_LINE + 1] = {0};
     int line_len = 0;
     for (int i = 0; i < buff_len; i++) {
-        line_len += sprintf(temp_buffer+line_len, "%c ", buffer[i]);
+        line_len += sprintf(temp_buffer+line_len, "%c", buffer[i]);
         if (((i + 1) % BYTES_PER_LINE == 0) || (i == buff_len - 1)) {
             ESP_LOGI(tag, "%s", temp_buffer);
             line_len = 0;
index 7c16b29f1b66fd8924d7cf88d078f3b91ed61113..aa42a4131108f687f275c4fb017506ed743272d2 100644 (file)
@@ -119,11 +119,8 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
         memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
         ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
 
-        ESP_LOGI(GATTC_TAG, "REMOTE BDA  %02x:%02x:%02x:%02x:%02x:%02x",
-                            gl_profile_tab[PROFILE_A_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[1], 
-                            gl_profile_tab[PROFILE_A_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[3],
-                            gl_profile_tab[PROFILE_A_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[5]
-                         );
+        ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
+        esp_log_buffer_hex(GATTC_TAG, (char *)gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
 
         esp_ble_gattc_search_service(gattc_if, conn_id, NULL);
         break;
@@ -136,12 +133,8 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
         } else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
             ESP_LOGI(GATTC_TAG, "UUID32: %x", srvc_id->id.uuid.uuid.uuid32);
         } else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
-            ESP_LOGI(GATTC_TAG, "UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", srvc_id->id.uuid.uuid.uuid128[0],
-                     srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
-                     srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
-                     srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
-                     srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
-                     srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
+            ESP_LOGI(GATTC_TAG, "UUID128:");
+            esp_log_buffer_hex(GATTC_TAG, (char *)srvc_id->id.uuid.uuid.uuid128, ESP_UUID_LEN_128);
         } else {
             ESP_LOGE(GATTC_TAG, "UNKNOWN LEN %d", srvc_id->id.uuid.len);
         }
@@ -216,12 +209,8 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
         memcpy(gl_profile_tab[PROFILE_B_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
         ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
 
-        ESP_LOGI(GATTC_TAG, "REMOTE BDA  %02x:%02x:%02x:%02x:%02x:%02x",
-                            gl_profile_tab[PROFILE_B_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[1], 
-                            gl_profile_tab[PROFILE_B_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[3],
-                            gl_profile_tab[PROFILE_B_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[5]
-                         );
-
+        ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
+        esp_log_buffer_hex(GATTC_TAG, (char *)gl_profile_tab[PROFILE_B_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
         esp_ble_gattc_search_service(gattc_if, conn_id, NULL);
         break;
     case ESP_GATTC_SEARCH_RES_EVT: {
@@ -233,12 +222,8 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
         } else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
             ESP_LOGI(GATTC_TAG, "UUID32: %x", srvc_id->id.uuid.uuid.uuid32);
         } else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
-            ESP_LOGI(GATTC_TAG, "UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", srvc_id->id.uuid.uuid.uuid128[0],
-                     srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
-                     srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
-                     srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
-                     srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
-                     srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
+            ESP_LOGI(GATTC_TAG, "UUID128:");
+            esp_log_buffer_hex(GATTC_TAG, (char *)srvc_id->id.uuid.uuid.uuid128, ESP_UUID_LEN_128);
         } else {
             ESP_LOGE(GATTC_TAG, "UNKNOWN LEN %d", srvc_id->id.uuid.len);
         }
@@ -312,17 +297,13 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
         esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
         switch (scan_result->scan_rst.search_evt) {
         case ESP_GAP_SEARCH_INQ_RES_EVT:
-            for (int i = 0; i < 6; i++) {
-                ESP_LOGI(GATTC_TAG, "%x:", scan_result->scan_rst.bda[i]);
-            }
-            ESP_LOGI(GATTC_TAG, "Searched Adv Data Len %d, Scan Response Len %d\n", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
-            ESP_LOGI(GATTC_TAG, "\n");
+            esp_log_buffer_hex(GATTC_TAG, (char *)scan_result->scan_rst.bda, 6);
+            ESP_LOGI(GATTC_TAG, "Searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
             adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
                                                 ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
             ESP_LOGI(GATTC_TAG, "Searched Device Name Len %d", adv_name_len);
-            for (int j = 0; j < adv_name_len; j++) {
-                ESP_LOGI(GATTC_TAG, "%c", adv_name[j]);
-            }
+            esp_log_buffer_char(GATTC_TAG, (char *)adv_name, adv_name_len);
+            ESP_LOGI(GATTC_TAG, "\n");
             if (adv_name != NULL) {
                 if (strlen(device_name) == adv_name_len && strncmp((char *)adv_name, device_name, adv_name_len) == 0) {
                     ESP_LOGI(GATTC_TAG, "Searched device %s\n", device_name);