@@ -93,8 +93,7 @@ uint8_t profile_discover_process(struct bt_conn *conn,
93
93
struct bt_gatt_discover_params *params)
94
94
{
95
95
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);
98
97
}
99
98
100
99
// Only for central
@@ -303,133 +302,190 @@ int BLEProfile::registerProfile()
303
302
304
303
ret = bt_gatt_register (_attr_base,
305
304
_attr_index);
306
- pr_info (LOG_MODULE_APP, " %s: ret, %d" , __FUNCTION__, ret);
305
+ pr_debug (LOG_MODULE_APP, " %s: ret, %d" , __FUNCTION__, ret);
307
306
308
307
return ret;
309
308
}
310
309
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)
312
325
{
313
- BLEAttribute* attribute = NULL ;
314
326
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 ;
315
387
int i;
388
+ int err;
389
+ uint8_t ret = BT_GATT_ITER_STOP;
316
390
bool send_discover = false ;
317
- const struct bt_gatt_attr *attr_rsp = attr;
318
391
319
392
for (i = 0 ; i < _num_attributes; i++)
320
393
{
321
- attribute = _attributes[i];
322
- if (attribute->discovering ())
394
+ // Find the discovering attribute
395
+ attribute_tmp = _attributes[i];
396
+ if (attribute_tmp->discovering ())
323
397
{
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 )
325
405
{
326
- // Discover success
327
- switch (_discover_params.type )
406
+ case BT_GATT_DISCOVER_CHARACTERISTIC:
328
407
{
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 ;
390
423
}
391
424
}
392
- attribute->discover (attr_rsp, &_discover_params);
393
425
break ;
394
426
}
395
427
}
396
428
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 )
403
431
{
404
432
// Current attribute complete discovery
405
- // Find next attribute to discover
406
433
i++;
407
- if (i < _num_attributes)
434
+ while (i < _num_attributes)
408
435
{
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 ;
412
461
}
413
462
}
463
+ else
464
+ {
465
+ ret = BT_GATT_ITER_CONTINUE;
466
+ }
414
467
415
- if (send_discover)
468
+ // Send the discover request if necessary
469
+ if (send_discover && attribute_tmp->discovering ())
416
470
{
417
471
struct bt_conn *conn = bt_conn_lookup_addr_le (_peripheral->bt_le_address ());
418
472
473
+ ret = BT_GATT_ITER_STOP;
419
474
if (NULL == conn)
420
475
{
421
476
// Link lost
422
477
pr_debug (LOG_MODULE_APP, " Can't find connection\n " );
423
- return ;
478
+ return ret ;
424
479
}
425
480
err = bt_gatt_discover (conn, &_discover_params);
426
481
bt_conn_unref (conn);
427
482
if (err)
428
483
{
429
484
pr_debug (LOG_MODULE_APP, " Discover failed(err %d)\n " , err);
430
- return ;
485
+ return ret ;
431
486
}
432
487
}
488
+ return ret;
433
489
}
434
490
435
491
0 commit comments