From 65f86005c38dfa23ccd8eb4e2a9891a2bc15787b Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 22 Mar 2017 19:30:17 +0700 Subject: [PATCH] add Central discoverDescriptor() --- libraries/Bluefruit52Lib/src/BLECentral.cpp | 93 +++++++++++++++---- libraries/Bluefruit52Lib/src/BLECentral.h | 2 + .../src/BLECentralCharacteristic.h | 2 +- .../src/central_services/BLECentralUart.cpp | 6 +- 4 files changed, 81 insertions(+), 22 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index 6f0c04cc..633e9c54 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -253,13 +253,48 @@ bool BLECentral::discoverCharacteristic(BLECentralCharacteristic& chr) { chr = BLECentralCharacteristic(&disc_chr.chars[0]); - // increase handle range for next characteristic + // increase handle range for next discovery _disc_hdl_range.start_handle = disc_chr.chars[0].handle_value + 1; } return true; } +uint16_t BLECentral::discoverDescriptor(ble_gattc_desc_t desc_arr[], uint16_t max_count) +{ + _evt_bufsize = sizeof(ble_gattc_evt_desc_disc_rsp_t) + (max_count-1)*sizeof(ble_gattc_desc_t); + + ble_gattc_evt_desc_disc_rsp_t* disc_desc = (ble_gattc_evt_desc_disc_rsp_t*) rtos_malloc( _evt_bufsize ); + + _evt_buf = disc_desc; + + LOG_LV1(Discover, "Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); + + uint16_t result = 0; + if( ERROR_NONE == sd_ble_gattc_descriptors_discover(_conn_hdl, &_disc_hdl_range) ) + { + // wait for discovery event: timeout or has no data + if ( xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) && (_evt_bufsize > 0) ) + { + result = min16(disc_desc->count, max_count); + if (result) + { + for(uint16_t i=0; idescs[i].uuid, disc_desc->descs[i].handle); + desc_arr[i] = disc_desc->descs[i]; + } + + // increase handle range for next discovery + _disc_hdl_range.start_handle = disc_desc->descs[result-1].handle + 1; + } + } + } + + rtos_free(_evt_buf); + return result; +} + /** * Event is forwarded from Bluefruit Poll() method * @param event @@ -315,29 +350,21 @@ void BLECentral::_event_handler(ble_evt_t* evt) case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP: { ble_gattc_evt_t* gattc = &evt->evt.gattc_evt; + ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp; if ( _conn_hdl == gattc->conn_handle ) { - if ( _evt_buf ) + LOG_LV1(Discover, "Service Count: %d", svc_rsp->count); + + if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS && svc_rsp->count && _evt_buf) { - if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) - { - ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp; + // Only support 1 service - LOG_LV1(Discover, "Service Count: %d", svc_rsp->count); - - COMMENT_OUT ( - uint16_t len = sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t) + (svc_rsp->count-1)*sizeof(ble_gattc_service_t); - _evt_bufsize = min16(_evt_bufsize, len); - ) - - // Only support 1 service - _evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t)); - memcpy(_evt_buf, svc_rsp, _evt_bufsize); - }else - { - _evt_bufsize = 0; // no data - } + _evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t)); + memcpy(_evt_buf, svc_rsp, _evt_bufsize); + }else + { + _evt_bufsize = 0; // no data } xSemaphoreGive(_evt_sem); @@ -353,7 +380,7 @@ void BLECentral::_event_handler(ble_evt_t* evt) if ( _conn_hdl == gattc->conn_handle ) { LOG_LV1(Discover, "Characteristic Count: %d", chr_rsp->count); - if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && (chr_rsp->count > 0) ) + if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && chr_rsp->count && _evt_buf ) { // TODO support only 1 discovered char now _evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_char_disc_rsp_t)); @@ -369,6 +396,32 @@ void BLECentral::_event_handler(ble_evt_t* evt) } break; + case BLE_GATTC_EVT_DESC_DISC_RSP: + { + ble_gattc_evt_t* gattc = &evt->evt.gattc_evt; + ble_gattc_evt_desc_disc_rsp_t* desc_rsp = &gattc->params.desc_disc_rsp; + + if ( _conn_hdl == gattc->conn_handle ) + { + LOG_LV1(Discover, "Descriptor Count: %d", desc_rsp->count); + + if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && desc_rsp->count && _evt_buf ) + { + // Copy up to bufsize + uint16_t len = sizeof(ble_gattc_evt_desc_disc_rsp_t) + (desc_rsp->count-1)*sizeof(ble_gattc_desc_t); + _evt_bufsize = min16(_evt_bufsize, len); + + memcpy(_evt_buf, desc_rsp, _evt_bufsize); + }else + { + _evt_bufsize = 0; // no data + } + } + + xSemaphoreGive(_evt_sem); + } + break; + default: break; } } diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h index ac58f5d6..c91977c3 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -90,6 +90,8 @@ class BLECentral bool discoverCharacteristic(BLECentralCharacteristic& chr); + uint16_t discoverDescriptor(ble_gattc_desc_t desc_arr[], uint16_t max_count); + /*------------------------------------------------------------------*/ /* CALLBACKS *------------------------------------------------------------------*/ diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h index e5af2662..55331b3d 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h @@ -48,7 +48,7 @@ class BLECentralCharacteristic BLECentralCharacteristic(void); BLECentralCharacteristic(ble_gattc_char_t* gattc_char); - uint16_t valueHandle();; + uint16_t valueHandle(); // uint16_t cccdHandle(); private: diff --git a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp index b15e9ae8..8aa07d57 100644 --- a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp +++ b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp @@ -64,12 +64,16 @@ bool BLECentralUart::discover(uint16_t start_handle) if (uuid16 == 0x0002) { + LOG_LV1(BLECentralUart, "Found RXD"); _rxd = chr_temp; }else if (uuid16 == 0x0003) { + LOG_LV1(BLECentralUart, "Found TXD"); _txd = chr_temp; - // TXD has CCCD + // TXD has CCCD, discovery it + ble_gattc_desc_t desc_arr[8]; + Bluefruit.Central.discoverDescriptor(desc_arr, arrcount(desc_arr)); } }