Skip to content

Commit 0869120

Browse files
committed
Fix Jira 665 BLECentral Preview -- compile issue when instantiating BLE descriptor
Fix the build error. Note: 1. The current code only support one descriptor on characteristic. 2. The central discover logic need more twist. 3. Now is so long and can't process CCCD and another characteristic. 4. The library will support one other descriptor except CCCD.
1 parent 042b147 commit 0869120

File tree

3 files changed

+127
-34
lines changed

3 files changed

+127
-34
lines changed

libraries/CurieBLE/src/BLEDescriptor.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ BLEDescriptor::BLEDescriptor(const char* uuid, const char* value) :
4646
}
4747

4848
const unsigned char*
49-
BLEDescriptor::BLEDescriptor::value() const
49+
BLEDescriptor::value() const
5050
{
5151
return _value;
5252
}
@@ -63,3 +63,34 @@ BLEDescriptor::operator[] (int offset) const
6363
return _value[offset];
6464
}
6565

66+
void BLEDescriptor::discover(const struct bt_gatt_attr *attr,
67+
struct bt_gatt_discover_params *params)
68+
{
69+
if (!attr)
70+
{
71+
// Discovery complete
72+
_discoverying = false;
73+
return;
74+
}
75+
76+
// Chracteristic Char
77+
if (params->uuid == this->uuid())
78+
{
79+
// Set Discover CCCD parameter
80+
params->start_handle = attr->handle + 1;
81+
// Complete the discover
82+
_discoverying = false;
83+
}
84+
85+
}
86+
87+
88+
void BLEDescriptor::discover(struct bt_gatt_discover_params *params)
89+
{
90+
params->type = BT_GATT_DISCOVER_DESCRIPTOR;
91+
params->uuid = this->uuid();
92+
// Start discovering
93+
_discoverying = true;
94+
}
95+
96+

libraries/CurieBLE/src/BLEDescriptor.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,32 @@ class BLEDescriptor : public BLEAttribute {
6262
unsigned short valueLength(void) const;
6363

6464

65+
/**
66+
* @brief For central to discover the peripherial profile
67+
*
68+
* @param attr The discover response
69+
*
70+
* @param params The discover parameter that need to fill
71+
*
72+
* @return none
73+
*
74+
* @note Only for central
75+
*/
76+
void discover(const struct bt_gatt_attr *attr,
77+
struct bt_gatt_discover_params *params);
78+
79+
/**
80+
* @brief For central to discover the peripherial profile
81+
*
82+
* @param params The discover parameter that need to fill
83+
*
84+
* @return none
85+
*
86+
* @note Only for central
87+
*/
88+
void discover(struct bt_gatt_discover_params *params);
89+
90+
6591
unsigned char operator[] (int offset) const;
6692

6793
protected:

libraries/CurieBLE/src/BLEProfile.cpp

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,22 @@ ssize_t profile_read_process(struct bt_conn *conn,
3131
{
3232
const unsigned char *pvalue;
3333
BLEAttribute *bleattr = (BLEAttribute *)attr->user_data;
34-
BLECharacteristic* blecharacteritic;
3534
BLEAttributeType type = bleattr->type();
36-
if (BLETypeCharacteristic != type)
35+
if (BLETypeCharacteristic == type)
3736
{
38-
return 0;
37+
BLECharacteristic* blecharacteritic;
38+
blecharacteritic = (BLECharacteristic*)bleattr;
39+
pvalue = blecharacteritic->value();
40+
return bt_gatt_attr_read(conn, attr, buf, len, offset, pvalue,
41+
blecharacteritic->valueLength());
3942
}
40-
blecharacteritic = (BLECharacteristic*)bleattr;
41-
pvalue = blecharacteritic->value();
42-
return bt_gatt_attr_read(conn, attr, buf, len, offset, pvalue,
43-
blecharacteritic->valueLength());
43+
else if (BLETypeDescriptor == type)
44+
{
45+
BLEDescriptor *bledescriptor = (BLEDescriptor *)bleattr;
46+
pvalue = bledescriptor->value();
47+
return bt_gatt_attr_read(conn, attr, buf, len, offset, pvalue, bledescriptor->valueLength());
48+
}
49+
return 0;
4450
}
4551

4652
// Only for peripheral
@@ -256,7 +262,18 @@ BLEProfile::addAttribute (BLEAttribute& attribute)
256262
start->read = bt_gatt_attr_read_service;
257263
start->user_data = attribute.uuid();
258264

259-
pr_info(LOG_MODULE_BLE, "service-%p", start);
265+
pr_debug(LOG_MODULE_BLE, "service-%p", start);
266+
start++;
267+
_attr_index++;
268+
}
269+
else if (BLETypeDescriptor == type)
270+
{
271+
start->uuid = attribute.uuid();
272+
start->perm = BT_GATT_PERM_READ;
273+
start->read = profile_read_process;
274+
start->user_data = (void*)&attribute;
275+
276+
pr_debug(LOG_MODULE_BLE, "Descriptor-%p", start);
260277
start++;
261278
_attr_index++;
262279
}
@@ -297,6 +314,7 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
297314
int err;
298315
int i;
299316
bool send_discover = false;
317+
const struct bt_gatt_attr *attr_rsp = attr;
300318

301319
for (i = 0; i < _num_attributes; i++)
302320
{
@@ -311,37 +329,55 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
311329
case BT_GATT_DISCOVER_CHARACTERISTIC:
312330
{
313331
struct bt_gatt_attr *attr_dec = declarationAttr(attribute);
314-
attr_dec++;
315-
attr_dec->handle = attr->handle + 1;
332+
if (NULL != attr_dec)
333+
{
334+
attr_dec++;
335+
attr_dec->handle = attr->handle + 1;
336+
}
316337
break;
317338
}
318339
case BT_GATT_DISCOVER_DESCRIPTOR:
319340
{
320-
BLECharacteristic *chrc = (BLECharacteristic *)attribute;
321-
struct bt_gatt_attr *attr_dec = declarationAttr(attribute);
322-
struct bt_gatt_attr *attr_chrc = attr_dec + 1;
323-
struct bt_gatt_attr *attr_cccd = attr_dec + 2;
324-
struct bt_gatt_subscribe_params *sub_param_tmp = chrc->getSubscribeParams();
325-
struct bt_gatt_subscribe_params *sub_param = _sub_param + _sub_param_idx;
326-
struct bt_conn *conn = bt_conn_lookup_addr_le(_peripheral->bt_le_address());
327-
if (NULL == conn)
341+
if (BLETypeCharacteristic == attribute->type())
328342
{
329-
// Link lost
330-
return;
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+
}
331375
}
332-
333-
_sub_param_idx++;
334-
attr_cccd->handle = attr->handle;
335-
memcpy(sub_param, sub_param_tmp, sizeof(struct bt_gatt_subscribe_params));
336-
sub_param->ccc_handle = attr_cccd->handle;
337-
sub_param->value_handle = attr_chrc->handle;
338-
339-
// Enable CCCD to allow peripheral send Notification/Indication
340-
err = bt_gatt_subscribe(conn, sub_param);
341-
bt_conn_unref(conn);
342-
if (err && err != -EALREADY)
376+
else if (BLETypeDescriptor == attribute->type())
343377
{
344-
pr_debug(LOG_MODULE_APP, "Subscribe failed (err %d)\n", err);
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;
345381
}
346382
break;
347383
}
@@ -353,7 +389,7 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
353389
}
354390
}
355391
}
356-
attribute->discover(attr, &_discover_params);
392+
attribute->discover(attr_rsp, &_discover_params);
357393
break;
358394
}
359395
}

0 commit comments

Comments
 (0)