Skip to content

Commit 8f3702a

Browse files
committed
Improve the discover logic and support another descriptor
1. Improve the discover logic 2. Support another the descriptor and CCCD at same time.
1 parent 0869120 commit 8f3702a

File tree

4 files changed

+188
-93
lines changed

4 files changed

+188
-93
lines changed

libraries/CurieBLE/src/BLEPeripheralHelper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ BLEAttribute *BLEPeripheralHelper::attribute(struct bt_gatt_subscribe_params *pa
2929
return _profile.attribute(params);
3030
}
3131

32-
void BLEPeripheralHelper::discover(const struct bt_gatt_attr *attr)
32+
uint8_t BLEPeripheralHelper::discover(const struct bt_gatt_attr *attr)
3333
{
3434
// Not allow to call the discover
3535
if (NULL == _central)
3636
{
37-
return;
37+
return BT_GATT_ITER_STOP;
3838
}
39-
_profile.discover(attr);
39+
return _profile.discover(attr);
4040
}
4141

4242
void BLEPeripheralHelper::discover()

libraries/CurieBLE/src/BLEPeripheralHelper.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,19 @@ class BLEPeripheralHelper : public BLEHelper {
6262
* For central to discover the profile
6363
*/
6464
void discover();
65-
void discover(const struct bt_gatt_attr *attr);
65+
66+
/**
67+
* @brief Process the discover response and
68+
* discover the BLE peripheral profile
69+
*
70+
* @param const struct bt_gatt_attr * The gatt attribute response
71+
*
72+
* @return uint8_t BT_GATT_ITER_STOP Stop discover the profile
73+
* BT_GATT_ITER_CONTINUE Continue to send the discover request
74+
*
75+
* @note This function only for the central device.
76+
*/
77+
uint8_t discover(const struct bt_gatt_attr *attr);
6678

6779
// For peripheral to register the tree
6880
int registerProfile();

libraries/CurieBLE/src/BLEProfile.cpp

Lines changed: 143 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ uint8_t profile_discover_process(struct bt_conn *conn,
9393
struct bt_gatt_discover_params *params)
9494
{
9595
BLEPeripheralHelper* peripheral = BLECentralRole::instance()->peripheral(conn);// Find peripheral by bt_conn
96-
peripheral->discover(attr);
97-
return BT_GATT_ITER_STOP;
96+
return peripheral->discover(attr);
9897
}
9998

10099
// Only for central
@@ -303,133 +302,190 @@ int BLEProfile::registerProfile()
303302

304303
ret = bt_gatt_register(_attr_base,
305304
_attr_index);
306-
pr_info(LOG_MODULE_APP, "%s: ret, %d", __FUNCTION__, ret);
305+
pr_debug(LOG_MODULE_APP, "%s: ret, %d", __FUNCTION__, ret);
307306

308307
return ret;
309308
}
310309

311-
void BLEProfile::discover(const struct bt_gatt_attr *attr)
310+
void BLEProfile::characteristicDiscoverRsp(const struct bt_gatt_attr *attr, BLEAttribute* bleattr)
311+
{
312+
struct bt_gatt_attr *attr_dec = declarationAttr(bleattr);
313+
if ((NULL != attr) && (NULL != attr_dec))
314+
{
315+
if (bt_uuid_cmp (attr_dec->uuid, attr->uuid) == 0)
316+
{
317+
attr_dec++;
318+
attr_dec->handle = attr->handle + 1;
319+
}
320+
}
321+
bleattr->discover(attr, &_discover_params);
322+
}
323+
324+
void BLEProfile::descriptorDiscoverRsp(const struct bt_gatt_attr *attr, BLEAttribute* bleattr)
312325
{
313-
BLEAttribute* attribute = NULL;
314326
int err;
327+
struct bt_gatt_attr *attr_dec = declarationAttr(bleattr);
328+
if (BLETypeCharacteristic == bleattr->type())
329+
{
330+
BLECharacteristic *chrc = (BLECharacteristic *)bleattr;
331+
if (bt_uuid_cmp (chrc->getClientCharacteristicConfigUuid(), attr->uuid) == 0)
332+
{
333+
//CCCD
334+
struct bt_gatt_attr *attr_chrc = attr_dec + 1;
335+
struct bt_gatt_attr *attr_cccd = attr_dec + 2;
336+
struct bt_gatt_subscribe_params *sub_param_tmp = chrc->getSubscribeParams();
337+
struct bt_gatt_subscribe_params *sub_param = _sub_param + _sub_param_idx;
338+
struct bt_conn *conn = bt_conn_lookup_addr_le(_peripheral->bt_le_address());
339+
if (NULL == conn)
340+
{
341+
// Link lost
342+
return;
343+
}
344+
345+
_sub_param_idx++;
346+
attr_cccd->handle = attr->handle;
347+
memcpy(sub_param, sub_param_tmp, sizeof(struct bt_gatt_subscribe_params));
348+
sub_param->ccc_handle = attr_cccd->handle;
349+
sub_param->value_handle = attr_chrc->handle;
350+
351+
// Enable CCCD to allow peripheral send Notification/Indication
352+
err = bt_gatt_subscribe(conn, sub_param);
353+
bt_conn_unref(conn);
354+
if (err && err != -EALREADY)
355+
{
356+
pr_debug(LOG_MODULE_APP, "Subscribe failed (err %d)\n", err);
357+
}
358+
bleattr->discover(attr, &_discover_params);
359+
}
360+
else
361+
{
362+
// Not CCCD
363+
// If want to support more descriptor,
364+
// change the offset 3 as a loop to search the ATTR
365+
struct bt_gatt_attr *attr_descriptor = attr_dec + 3;
366+
if (attr_descriptor->uuid != NULL &&
367+
bt_uuid_cmp (attr_descriptor->uuid, attr->uuid) == 0)
368+
{
369+
attr_descriptor->handle = attr->handle;
370+
}
371+
}
372+
}
373+
else if (BLETypeDescriptor == bleattr->type())
374+
{
375+
struct bt_gatt_attr *attr_descriptor = attr_dec++; // The descriptor is separate
376+
if (bt_uuid_cmp (attr_dec->uuid, attr->uuid) == 0)
377+
{
378+
attr_descriptor->handle = attr->handle;
379+
}
380+
bleattr->discover(attr, &_discover_params);
381+
}
382+
}
383+
384+
uint8_t BLEProfile::discover(const struct bt_gatt_attr *attr)
385+
{
386+
BLEAttribute* attribute_tmp = NULL;
315387
int i;
388+
int err;
389+
uint8_t ret = BT_GATT_ITER_STOP;
316390
bool send_discover = false;
317-
const struct bt_gatt_attr *attr_rsp = attr;
318391

319392
for (i = 0; i < _num_attributes; i++)
320393
{
321-
attribute = _attributes[i];
322-
if (attribute->discovering())
394+
// Find the discovering attribute
395+
attribute_tmp = _attributes[i];
396+
if (attribute_tmp->discovering())
323397
{
324-
if (NULL != attr)
398+
if (NULL == attr)
399+
{
400+
attribute_tmp->discover(attr, &_discover_params);
401+
break;
402+
}
403+
// Discover success
404+
switch (_discover_params.type)
325405
{
326-
// Discover success
327-
switch (_discover_params.type)
406+
case BT_GATT_DISCOVER_CHARACTERISTIC:
328407
{
329-
case BT_GATT_DISCOVER_CHARACTERISTIC:
330-
{
331-
struct bt_gatt_attr *attr_dec = declarationAttr(attribute);
332-
if (NULL != attr_dec)
333-
{
334-
attr_dec++;
335-
attr_dec->handle = attr->handle + 1;
336-
}
337-
break;
338-
}
339-
case BT_GATT_DISCOVER_DESCRIPTOR:
340-
{
341-
if (BLETypeCharacteristic == attribute->type())
342-
{
343-
BLECharacteristic *chrc = (BLECharacteristic *)attribute;
344-
if (bt_uuid_cmp (chrc->getClientCharacteristicConfigUuid(), attr->uuid) != 0)
345-
{
346-
// Failed other descriptor. Not CCCD
347-
attr_rsp = NULL;
348-
break;
349-
}
350-
struct bt_gatt_attr *attr_dec = declarationAttr(attribute);
351-
struct bt_gatt_attr *attr_chrc = attr_dec + 1;
352-
struct bt_gatt_attr *attr_cccd = attr_dec + 2;
353-
struct bt_gatt_subscribe_params *sub_param_tmp = chrc->getSubscribeParams();
354-
struct bt_gatt_subscribe_params *sub_param = _sub_param + _sub_param_idx;
355-
struct bt_conn *conn = bt_conn_lookup_addr_le(_peripheral->bt_le_address());
356-
if (NULL == conn)
357-
{
358-
// Link lost
359-
return;
360-
}
361-
362-
_sub_param_idx++;
363-
attr_cccd->handle = attr->handle;
364-
memcpy(sub_param, sub_param_tmp, sizeof(struct bt_gatt_subscribe_params));
365-
sub_param->ccc_handle = attr_cccd->handle;
366-
sub_param->value_handle = attr_chrc->handle;
367-
368-
// Enable CCCD to allow peripheral send Notification/Indication
369-
err = bt_gatt_subscribe(conn, sub_param);
370-
bt_conn_unref(conn);
371-
if (err && err != -EALREADY)
372-
{
373-
pr_debug(LOG_MODULE_APP, "Subscribe failed (err %d)\n", err);
374-
}
375-
}
376-
else if (BLETypeDescriptor == attribute->type())
377-
{
378-
struct bt_gatt_attr *attr_dec = declarationAttr(attribute);
379-
struct bt_gatt_attr *attr_descriptor = attr_dec++; // The descriptor is separate
380-
attr_descriptor->handle = attr->handle;
381-
}
382-
break;
383-
}
384-
case BT_GATT_DISCOVER_PRIMARY:
385-
default:
386-
{
387-
// Do nothing
388-
break;
389-
}
408+
characteristicDiscoverRsp(attr, attribute_tmp);
409+
send_discover = true;
410+
break;
411+
}
412+
case BT_GATT_DISCOVER_DESCRIPTOR:
413+
{
414+
descriptorDiscoverRsp(attr, attribute_tmp);
415+
break;
416+
}
417+
case BT_GATT_DISCOVER_PRIMARY:
418+
send_discover = true;
419+
default:
420+
{
421+
attribute_tmp->discover(attr, &_discover_params);
422+
break;
390423
}
391424
}
392-
attribute->discover(attr_rsp, &_discover_params);
393425
break;
394426
}
395427
}
396428

397-
// Send discover
398-
if (attribute->discovering())
399-
{
400-
send_discover = true;
401-
}
402-
else
429+
// Find next attribute to discover
430+
if (attribute_tmp->discovering() == false)
403431
{
404432
// Current attribute complete discovery
405-
// Find next attribute to discover
406433
i++;
407-
if (i < _num_attributes)
434+
while (i < _num_attributes)
408435
{
409-
attribute = _attributes[i];
410-
attribute->discover(&_discover_params);
411-
send_discover = true;
436+
attribute_tmp = _attributes[i];
437+
if (attribute_tmp->type() == BLETypeDescriptor)
438+
{
439+
// The descriptor may have been discovered by previous descriptor
440+
struct bt_gatt_attr *attr_gatt = NULL;
441+
for (int j = 0; j < _attr_index; j++)
442+
{
443+
attr_gatt = _attr_base + i;
444+
if (attribute_tmp->uuid() == attr_gatt->uuid)
445+
{
446+
break;
447+
}
448+
}
449+
450+
if (attr_gatt->handle != 0)
451+
{
452+
// Skip discovered descriptor
453+
i++;
454+
continue;
455+
}
456+
}
457+
458+
attribute_tmp->discover(&_discover_params);
459+
ret = BT_GATT_ITER_CONTINUE;
460+
break;
412461
}
413462
}
463+
else
464+
{
465+
ret = BT_GATT_ITER_CONTINUE;
466+
}
414467

415-
if (send_discover)
468+
// Send the discover request if necessary
469+
if (send_discover && attribute_tmp->discovering())
416470
{
417471
struct bt_conn *conn = bt_conn_lookup_addr_le(_peripheral->bt_le_address());
418472

473+
ret = BT_GATT_ITER_STOP;
419474
if (NULL == conn)
420475
{
421476
// Link lost
422477
pr_debug(LOG_MODULE_APP, "Can't find connection\n");
423-
return;
478+
return ret;
424479
}
425480
err = bt_gatt_discover(conn, &_discover_params);
426481
bt_conn_unref(conn);
427482
if (err)
428483
{
429484
pr_debug(LOG_MODULE_APP, "Discover failed(err %d)\n", err);
430-
return;
485+
return ret;
431486
}
432487
}
488+
return ret;
433489
}
434490

435491

libraries/CurieBLE/src/BLEProfile.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ class BLEProfile{
8383
*
8484
* @param const struct bt_gatt_attr * The gatt attribute response
8585
*
86-
* @return none
86+
* @return uint8_t BT_GATT_ITER_STOP Stop discover the profile
87+
* BT_GATT_ITER_CONTINUE Continue to send the discover request
8788
*
8889
* @note This function only for the central device.
8990
*/
90-
void discover(const struct bt_gatt_attr *attr);
91+
uint8_t discover(const struct bt_gatt_attr *attr);
9192

9293
/**
9394
* @brief Discover the BLE peripheral profile
@@ -171,6 +172,32 @@ class BLEProfile{
171172
*/
172173
struct bt_gatt_attr* declarationAttr(BLEAttribute *attr);
173174

175+
/**
176+
* @brief Process the descriptor discover response
177+
*
178+
* @param const struct bt_gatt_attr * The discover response
179+
*
180+
* @param BLEAttribute * The BLEAttribute object in discovering
181+
*
182+
* @return none
183+
*
184+
* @note none
185+
*/
186+
void descriptorDiscoverRsp(const struct bt_gatt_attr *attr, BLEAttribute* bleattr);
187+
188+
/**
189+
* @brief Process the characteristic discover response
190+
*
191+
* @param const struct bt_gatt_attr * The discover response
192+
*
193+
* @param BLEAttribute * The BLEAttribute object in discovering
194+
*
195+
* @return none
196+
*
197+
* @note none
198+
*/
199+
void characteristicDiscoverRsp(const struct bt_gatt_attr *attr, BLEAttribute* bleattr);
200+
174201
private:
175202
BLEPeripheralHelper *_peripheral;
176203
struct bt_gatt_attr *_attr_base;

0 commit comments

Comments
 (0)