@@ -31,16 +31,22 @@ ssize_t profile_read_process(struct bt_conn *conn,
31
31
{
32
32
const unsigned char *pvalue;
33
33
BLEAttribute *bleattr = (BLEAttribute *)attr->user_data ;
34
- BLECharacteristic* blecharacteritic;
35
34
BLEAttributeType type = bleattr->type ();
36
- if (BLETypeCharacteristic ! = type)
35
+ if (BLETypeCharacteristic = = type)
37
36
{
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 ());
39
42
}
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 ;
44
50
}
45
51
46
52
// Only for peripheral
@@ -256,7 +262,18 @@ BLEProfile::addAttribute (BLEAttribute& attribute)
256
262
start->read = bt_gatt_attr_read_service;
257
263
start->user_data = attribute.uuid ();
258
264
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);
260
277
start++;
261
278
_attr_index++;
262
279
}
@@ -297,6 +314,7 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
297
314
int err;
298
315
int i;
299
316
bool send_discover = false ;
317
+ const struct bt_gatt_attr *attr_rsp = attr;
300
318
301
319
for (i = 0 ; i < _num_attributes; i++)
302
320
{
@@ -311,37 +329,55 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
311
329
case BT_GATT_DISCOVER_CHARACTERISTIC:
312
330
{
313
331
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
+ }
316
337
break ;
317
338
}
318
339
case BT_GATT_DISCOVER_DESCRIPTOR:
319
340
{
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 ())
328
342
{
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
+ }
331
375
}
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 ())
343
377
{
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 ;
345
381
}
346
382
break ;
347
383
}
@@ -353,7 +389,7 @@ void BLEProfile::discover(const struct bt_gatt_attr *attr)
353
389
}
354
390
}
355
391
}
356
- attribute->discover (attr , &_discover_params);
392
+ attribute->discover (attr_rsp , &_discover_params);
357
393
break ;
358
394
}
359
395
}
0 commit comments