Skip to content

Commit 121f734

Browse files
committed
add hdl_range para to discoverDescriptor()
fix ancs descritpor discover issue
1 parent f7f4daa commit 121f734

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ BLEClientService& BLEClientCharacteristic::parentService (void)
118118
return *_service;
119119
}
120120

121-
bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle)
121+
bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle, ble_gattc_handle_range_t hdl_range)
122122
{
123123
enum { MAX_DESCIRPTORS = 8 };
124124

@@ -127,7 +127,7 @@ bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle)
127127
ble_gattc_desc_t descs[MAX_DESCIRPTORS];
128128
}disc_rsp;
129129

130-
uint16_t count = Bluefruit.Discovery._discoverDescriptor(conn_handle, (ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, sizeof(disc_rsp));
130+
uint16_t count = Bluefruit.Discovery._discoverDescriptor(conn_handle, (ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, sizeof(disc_rsp), hdl_range);
131131

132132
// only care CCCD for now
133133
for(uint16_t i=0; i<count; i++)

libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class BLEClientCharacteristic
5757
virtual ~BLEClientCharacteristic();
5858

5959
void assign(ble_gattc_char_t* gattc_chr);
60-
bool discoverDescriptor(uint16_t conn_handle);
60+
bool discoverDescriptor(uint16_t conn_handle, ble_gattc_handle_range_t hdl_range);
6161

6262
void begin(BLEClientService* parent_svc = NULL);
6363

libraries/Bluefruit52Lib/src/BLEDiscovery.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
137137
// characteristic assign overload
138138
chr[i]->assign(&disc_chr->chars[d]);
139139

140-
// Discovery All descriptors as well
141-
_hdl_range.start_handle = disc_chr->chars[d].handle_value + 1;
142-
if (_hdl_range.start_handle < _hdl_range.end_handle )
140+
ble_gattc_handle_range_t range = { disc_chr->chars[d].handle_value + 1, _hdl_range.end_handle };
141+
142+
// Discovery All descriptors if possible
143+
if ( range.start_handle <= range.start_handle )
143144
{
144-
// skip discovery descriptor if it is last characteristic without descriptor
145-
chr[i]->discoverDescriptor(conn_handle);
145+
// skip discovery descriptor
146+
chr[i]->discoverDescriptor(conn_handle, range);
146147
}
147148

148149
found++;
@@ -153,6 +154,10 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
153154
}
154155

155156
// increase handle range for next discovery
157+
// should be last descriptor +1, but that will cause missing on the next Characteristic !!!!!
158+
// Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 (Char declaration) in the result
159+
//
160+
// To be safe we use last chars + 1
156161
_hdl_range.start_handle = disc_chr->chars[ disc_chr->count-1 ].handle_value + 1;
157162
}
158163

@@ -161,13 +166,13 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
161166
return found;
162167
}
163168

164-
uint16_t BLEDiscovery::_discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t bufsize)
169+
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)
165170
{
166-
LOG_LV2(Discover, "[DESC] Handle start = %d, end = %d", _hdl_range.start_handle, _hdl_range.end_handle);
171+
LOG_LV2(Discover, "[DESC] Handle start = %d, end = %d", hdl_range.start_handle, hdl_range.end_handle);
167172

168173
_adamsg.prepare(disc_desc, bufsize);
169174

170-
VERIFY_STATUS( sd_ble_gattc_descriptors_discover(conn_handle, &_hdl_range), 0 );
175+
VERIFY_STATUS( sd_ble_gattc_descriptors_discover(conn_handle, &hdl_range), 0 );
171176

172177
// wait for discovery event
173178
int32_t bytecount = _adamsg.waitUntilComplete(BLE_DISCOVERY_TIMEOUT);
@@ -180,11 +185,6 @@ uint16_t BLEDiscovery::_discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_d
180185
LOG_LV2(Discover, "[DESC] Descriptor %d: uuid = 0x%04X, handle = %d", i, disc_desc->descs[i].uuid.uuid, disc_desc->descs[i].handle);
181186
}
182187

183-
// increase handle range for next discovery
184-
// should be +1 more, but that will cause missing on the next Characteristic !!!!!
185-
// Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 (Char declaration) in the result
186-
_hdl_range.start_handle = disc_desc->descs[ disc_desc->count-1 ].handle;
187-
188188
return disc_desc->count;
189189
}
190190

libraries/Bluefruit52Lib/src/BLEDiscovery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class BLEDiscovery
101101
* code. User should not call these directly
102102
*------------------------------------------------------------------*/
103103
bool _discoverService(uint16_t conn_handle, BLEClientService& svc, uint16_t start_handle = 1);
104-
uint16_t _discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t bufsize);
104+
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);
105105

106106
friend class AdafruitBluefruit;
107107
};

libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838

3939
#define BLE_ANCS_TIMEOUT (5*BLE_GENERIC_TIMEOUT)
4040

41+
#define DEBUG_ANCS 0
42+
4143
void bleancs_notification_cb(BLEClientCharacteristic& chr, uint8_t* data, uint16_t len);
4244
void bleancs_data_cb(BLEClientCharacteristic& chr, uint8_t* data, uint16_t len);
4345

4446
/* ANCS Service : 7905F431-B5CE-4E99-A40F-4B1E122D00D0
45-
* Notification Source : 9FBF120D-6301-42D9-8C58-25E699A21DBD
4647
* Control Point : 69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9
48+
* Notification Source : 9FBF120D-6301-42D9-8C58-25E699A21DBD
4749
* Data Source : 22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB
4850
*/
4951

@@ -128,8 +130,8 @@ void BLEAncs::setNotificationCallback(notification_callback_t fp)
128130
bool BLEAncs::enableNotification(void)
129131
{
130132
// enable both Notification & Data Source
131-
_data.enableNotify();
132-
_notification.enableNotify();
133+
VERIFY ( _data.enableNotify() );
134+
VERIFY ( _notification.enableNotify() );
133135

134136
return true;
135137
}
@@ -179,7 +181,7 @@ uint16_t BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_
179181
.cmd = ANCS_CMD_GET_NOTIFICATION_ATTR,
180182
.uid = uid,
181183
.attr = attr,
182-
.len = bufsize
184+
.len = bufsize
183185
};
184186
uint8_t cmdlen = 6;
185187

@@ -190,7 +192,10 @@ uint16_t BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_
190192
}
191193

192194
// Write command using write response
193-
// PRINT_BUFFER(&command, cmdlen);
195+
#if DEBUG_ANCS
196+
PRINT_BUFFER(&command, cmdlen);
197+
#endif
198+
194199
_adamsg.prepare(buffer, bufsize);
195200
VERIFY( cmdlen == _control.write_resp(&command, cmdlen), 0);
196201
VERIFY( _adamsg.waitUntilComplete(BLE_ANCS_TIMEOUT) >= 0, 0);
@@ -233,7 +238,9 @@ uint16_t BLEAncs::getAppAttribute(const char* appid, uint8_t attr, void* buffer,
233238
strcpy( (char*) command+1, appid);
234239
command[cmdlen-1] = attr;
235240

236-
// PRINT_BUFFER(command, cmdlen);
241+
#if DEBUG_ANCS
242+
PRINT_BUFFER(command, cmdlen);
243+
#endif
237244
_adamsg.prepare(buffer, bufsize);
238245

239246
// Write command using write response
@@ -297,7 +304,9 @@ void BLEAncs::_handleNotification(uint8_t* data, uint16_t len)
297304

298305
void BLEAncs::_handleData(uint8_t* data, uint16_t len)
299306
{
300-
//PRINT_BUFFER(data, len);
307+
#if DEBUG_ANCS
308+
PRINT_BUFFER(data, len);
309+
#endif
301310

302311
_adamsg.feed(data, len);
303312
_adamsg.complete(); // mark as complete each time we received data

0 commit comments

Comments
 (0)