From 121f73475ac012515870d1a7c7efa6656fce3310 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 25 Sep 2017 20:52:45 +0700 Subject: [PATCH] add hdl_range para to discoverDescriptor() fix ancs descritpor discover issue --- .../src/BLEClientCharacteristic.cpp | 4 +-- .../src/BLEClientCharacteristic.h | 2 +- libraries/Bluefruit52Lib/src/BLEDiscovery.cpp | 26 +++++++++---------- libraries/Bluefruit52Lib/src/BLEDiscovery.h | 2 +- .../Bluefruit52Lib/src/clients/BLEAncs.cpp | 23 +++++++++++----- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp index 16f997c8..e40bbb5b 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp @@ -118,7 +118,7 @@ BLEClientService& BLEClientCharacteristic::parentService (void) return *_service; } -bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle) +bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle, ble_gattc_handle_range_t hdl_range) { enum { MAX_DESCIRPTORS = 8 }; @@ -127,7 +127,7 @@ bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle) ble_gattc_desc_t descs[MAX_DESCIRPTORS]; }disc_rsp; - uint16_t count = Bluefruit.Discovery._discoverDescriptor(conn_handle, (ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, sizeof(disc_rsp)); + uint16_t count = Bluefruit.Discovery._discoverDescriptor(conn_handle, (ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, sizeof(disc_rsp), hdl_range); // only care CCCD for now for(uint16_t i=0; iassign(&disc_chr->chars[d]); - // Discovery All descriptors as well - _hdl_range.start_handle = disc_chr->chars[d].handle_value + 1; - if (_hdl_range.start_handle < _hdl_range.end_handle ) + ble_gattc_handle_range_t range = { disc_chr->chars[d].handle_value + 1, _hdl_range.end_handle }; + + // Discovery All descriptors if possible + if ( range.start_handle <= range.start_handle ) { - // skip discovery descriptor if it is last characteristic without descriptor - chr[i]->discoverDescriptor(conn_handle); + // skip discovery descriptor + chr[i]->discoverDescriptor(conn_handle, range); } found++; @@ -153,6 +154,10 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar } // increase handle range for next discovery + // should be last descriptor +1, but that will cause missing on the next Characteristic !!!!! + // Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 (Char declaration) in the result + // + // To be safe we use last chars + 1 _hdl_range.start_handle = disc_chr->chars[ disc_chr->count-1 ].handle_value + 1; } @@ -161,13 +166,13 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar return found; } -uint16_t BLEDiscovery::_discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t bufsize) +uint16_t BLEDiscovery::_discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t bufsize, ble_gattc_handle_range_t hdl_range) { - LOG_LV2(Discover, "[DESC] Handle start = %d, end = %d", _hdl_range.start_handle, _hdl_range.end_handle); + LOG_LV2(Discover, "[DESC] Handle start = %d, end = %d", hdl_range.start_handle, hdl_range.end_handle); _adamsg.prepare(disc_desc, bufsize); - VERIFY_STATUS( sd_ble_gattc_descriptors_discover(conn_handle, &_hdl_range), 0 ); + VERIFY_STATUS( sd_ble_gattc_descriptors_discover(conn_handle, &hdl_range), 0 ); // wait for discovery event int32_t bytecount = _adamsg.waitUntilComplete(BLE_DISCOVERY_TIMEOUT); @@ -180,11 +185,6 @@ uint16_t BLEDiscovery::_discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_d LOG_LV2(Discover, "[DESC] Descriptor %d: uuid = 0x%04X, handle = %d", i, disc_desc->descs[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 (Char declaration) in the result - _hdl_range.start_handle = disc_desc->descs[ disc_desc->count-1 ].handle; - return disc_desc->count; } diff --git a/libraries/Bluefruit52Lib/src/BLEDiscovery.h b/libraries/Bluefruit52Lib/src/BLEDiscovery.h index 108460f9..3268a61a 100644 --- a/libraries/Bluefruit52Lib/src/BLEDiscovery.h +++ b/libraries/Bluefruit52Lib/src/BLEDiscovery.h @@ -101,7 +101,7 @@ class BLEDiscovery * code. User should not call these directly *------------------------------------------------------------------*/ bool _discoverService(uint16_t conn_handle, BLEClientService& svc, uint16_t start_handle = 1); - uint16_t _discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t bufsize); + uint16_t _discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t bufsize, ble_gattc_handle_range_t hdl_range); friend class AdafruitBluefruit; }; diff --git a/libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp b/libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp index b9ed5796..96c241af 100644 --- a/libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp +++ b/libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp @@ -38,12 +38,14 @@ #define BLE_ANCS_TIMEOUT (5*BLE_GENERIC_TIMEOUT) +#define DEBUG_ANCS 0 + void bleancs_notification_cb(BLEClientCharacteristic& chr, uint8_t* data, uint16_t len); void bleancs_data_cb(BLEClientCharacteristic& chr, uint8_t* data, uint16_t len); /* ANCS Service : 7905F431-B5CE-4E99-A40F-4B1E122D00D0 - * Notification Source : 9FBF120D-6301-42D9-8C58-25E699A21DBD * Control Point : 69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9 + * Notification Source : 9FBF120D-6301-42D9-8C58-25E699A21DBD * Data Source : 22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB */ @@ -128,8 +130,8 @@ void BLEAncs::setNotificationCallback(notification_callback_t fp) bool BLEAncs::enableNotification(void) { // enable both Notification & Data Source - _data.enableNotify(); - _notification.enableNotify(); + VERIFY ( _data.enableNotify() ); + VERIFY ( _notification.enableNotify() ); return true; } @@ -179,7 +181,7 @@ uint16_t BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_ .cmd = ANCS_CMD_GET_NOTIFICATION_ATTR, .uid = uid, .attr = attr, - .len = bufsize + .len = bufsize }; uint8_t cmdlen = 6; @@ -190,7 +192,10 @@ uint16_t BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_ } // Write command using write response -// PRINT_BUFFER(&command, cmdlen); +#if DEBUG_ANCS + PRINT_BUFFER(&command, cmdlen); +#endif + _adamsg.prepare(buffer, bufsize); VERIFY( cmdlen == _control.write_resp(&command, cmdlen), 0); VERIFY( _adamsg.waitUntilComplete(BLE_ANCS_TIMEOUT) >= 0, 0); @@ -233,7 +238,9 @@ uint16_t BLEAncs::getAppAttribute(const char* appid, uint8_t attr, void* buffer, strcpy( (char*) command+1, appid); command[cmdlen-1] = attr; -// PRINT_BUFFER(command, cmdlen); +#if DEBUG_ANCS + PRINT_BUFFER(command, cmdlen); +#endif _adamsg.prepare(buffer, bufsize); // Write command using write response @@ -297,7 +304,9 @@ void BLEAncs::_handleNotification(uint8_t* data, uint16_t len) void BLEAncs::_handleData(uint8_t* data, uint16_t len) { - //PRINT_BUFFER(data, len); +#if DEBUG_ANCS + PRINT_BUFFER(data, len); +#endif _adamsg.feed(data, len); _adamsg.complete(); // mark as complete each time we received data