From 83fe22cdf08fd7d1c53d9b59fa92883719294901 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 22 Mar 2017 21:55:09 +0700 Subject: [PATCH] remvoe malloc in Central discoverDescriptor() and clean up --- libraries/Bluefruit52Lib/src/BLECentral.cpp | 53 ++++++++----------- libraries/Bluefruit52Lib/src/BLECentral.h | 3 +- .../src/BLECentralCharacteristic.cpp | 16 +++--- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index bc36caee..f9afdaf8 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -223,7 +223,7 @@ bool BLECentral::discoverService(BLEUuid uuid, uint16_t start_handle) if ( (disc_svc.count == 1) && (uuid == disc_svc.services[0].uuid) ) { _disc_hdl_range = disc_svc.services[0].handle_range; - LOG_LV1(Discover SVC, "Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); + LOG_LV1(Discover, "[SVC] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); _disc_hdl_range.start_handle++; // increase for next discovery return true; @@ -244,7 +244,7 @@ bool BLECentral::discoverCharacteristic(BLECentralCharacteristic& chr) _evt_buf = &disc_chr; _evt_bufsize = sizeof(disc_chr); - LOG_LV1(Discover CHR, "Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); + LOG_LV1(Discover, "[CHR] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &_disc_hdl_range), false ); @@ -265,40 +265,33 @@ bool BLECentral::discoverCharacteristic(BLECentralCharacteristic& chr) return true; } -uint16_t BLECentral::discoverDescriptor(ble_gattc_desc_t desc_arr[], uint16_t max_count) +uint16_t BLECentral::_discoverDescriptor(ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count) { + _evt_buf = disc_desc; _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 DSC, "Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); + LOG_LV1(Discover, "[DESC] 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.uuid, disc_desc->descs[i].handle); - desc_arr[i] = disc_desc->descs[i]; - } + VERIFY_STATUS( sd_ble_gattc_descriptors_discover(_conn_hdl, &_disc_hdl_range), 0 ); - // increase handle range for next discovery - // should be +1 more, but that will cause missing on the next Characteristic !!!!! - // Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 in the result - _disc_hdl_range.start_handle = disc_desc->descs[result-1].handle; - } + // wait for discovery event: timeout or has no data + if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return 0; + + result = min16(disc_desc->count, max_count); + if (result) + { + for(uint16_t i=0; idescs[i].uuid.uuid, disc_desc->descs[i].handle); } + + // increase handle range for next discovery + // should be +1 more, but that will cause missing on the next Characteristic !!!!! + // Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 in the result + _disc_hdl_range.start_handle = disc_desc->descs[result-1].handle; } - rtos_free(_evt_buf); return result; } @@ -361,7 +354,7 @@ void BLECentral::_event_handler(ble_evt_t* evt) if ( _conn_hdl == gattc->conn_handle ) { - LOG_LV1(Discover SVC, "Service Count: %d", svc_rsp->count); + LOG_LV1(Discover, "[SVC] Service Count: %d", svc_rsp->count); if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS && svc_rsp->count && _evt_buf) { @@ -386,7 +379,7 @@ void BLECentral::_event_handler(ble_evt_t* evt) if ( _conn_hdl == gattc->conn_handle ) { - LOG_LV1(Discover CHR, "Characteristic Count: %d", chr_rsp->count); + LOG_LV1(Discover, "[CHR] Characteristic Count: %d", chr_rsp->count); if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && chr_rsp->count && _evt_buf ) { // TODO support only 1 discovered char now @@ -410,7 +403,7 @@ void BLECentral::_event_handler(ble_evt_t* evt) if ( _conn_hdl == gattc->conn_handle ) { - LOG_LV1(Discover DSC, "Descriptor Count: %d", desc_rsp->count); + LOG_LV1(Discover, "[DESC] Descriptor Count: %d", desc_rsp->count); if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && desc_rsp->count && _evt_buf ) { diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h index c91977c3..f3868725 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -90,7 +90,8 @@ class BLECentral bool discoverCharacteristic(BLECentralCharacteristic& chr); - uint16_t discoverDescriptor(ble_gattc_desc_t desc_arr[], uint16_t max_count); + // Called by discoverCharacteristic(), user should not call this directly + uint16_t _discoverDescriptor(ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count); /*------------------------------------------------------------------*/ /* CALLBACKS diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp index 081652d1..51928586 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp @@ -59,17 +59,21 @@ uint16_t BLECentralCharacteristic::valueHandle() bool BLECentralCharacteristic::discoverDescriptor(void) { - ble_gattc_desc_t desc_arr[MAX_DESCIRPTORS]; - uint16_t count = Bluefruit.Central.discoverDescriptor(desc_arr, arrcount(desc_arr)); + struct { + uint16_t count; + ble_gattc_desc_t descs[MAX_DESCIRPTORS]; + }disc_rsp; + + uint16_t count = Bluefruit.Central._discoverDescriptor((ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, MAX_DESCIRPTORS); // only care CCCD for now for(uint16_t i=0; i