26
26
#include "BLECharacteristicImp.h"
27
27
28
28
bt_uuid_16_t BLEServiceImp::_gatt_primary_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_PRIMARY_VAL};
29
+ bt_gatt_read_params_t BLEServiceImp::_read_params;
29
30
30
31
bt_uuid_t *BLEServiceImp::getPrimayUuid(void)
31
32
{
@@ -36,6 +37,7 @@ BLEServiceImp::BLEServiceImp(BLEService& service):
36
37
BLEAttribute(service.uuid(), BLETypeService),
37
38
_start_handle(0),
38
39
_end_handle(0xFFFF),
40
+ _reading(false),
39
41
_cur_discover_chrc(NULL)
40
42
{
41
43
memset(&_characteristics_header, 0, sizeof(_characteristics_header));
@@ -46,6 +48,7 @@ BLEServiceImp::BLEServiceImp(const bt_uuid_t* uuid):
46
48
BLEAttribute(uuid, BLETypeService),
47
49
_start_handle(0),
48
50
_end_handle(0xFFFF),
51
+ _reading(false),
49
52
_cur_discover_chrc(NULL)
50
53
{
51
54
memset(&_characteristics_header, 0, sizeof(_characteristics_header));
@@ -242,10 +245,17 @@ BLECharacteristicImp* BLEServiceImp::characteristic(const char* uuid)
242
245
243
246
bool BLEServiceImp::discovering()
244
247
{
245
- return (_cur_discover_chrc != NULL);
248
+ return (_cur_discover_chrc != NULL || _reading );
246
249
}
247
250
248
251
bool BLEServiceImp::discoverAttributes(BLEDevice* device)
252
+ {
253
+ return discoverAttributes(device, _start_handle, _end_handle);
254
+ }
255
+
256
+ bool BLEServiceImp::discoverAttributes(BLEDevice* device,
257
+ uint16_t start_handle,
258
+ uint16_t end_handle)
249
259
{
250
260
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
251
261
int err;
@@ -273,8 +283,8 @@ bool BLEServiceImp::discoverAttributes(BLEDevice* device)
273
283
return false;
274
284
}
275
285
temp = &_discover_params;
276
- temp->start_handle = _start_handle ;
277
- temp->end_handle = _end_handle ;
286
+ temp->start_handle = start_handle ;
287
+ temp->end_handle = end_handle ;
278
288
temp->uuid = NULL;
279
289
temp->type = BT_GATT_DISCOVER_CHARACTERISTIC;
280
290
temp->func = profile_discover_process;
@@ -309,21 +319,34 @@ uint8_t BLEServiceImp::discoverResponseProc(bt_conn_t *conn,
309
319
//const bt_uuid_t* chrc_uuid = attr->uuid;
310
320
uint16_t chrc_handle = attr->handle + 1;
311
321
struct bt_gatt_chrc* psttemp = (struct bt_gatt_chrc*)attr->user_data;
312
- int retval = (int)addCharacteristic(device,
313
- psttemp->uuid,
314
- chrc_handle,
315
- psttemp->properties);
322
+ const bt_uuid_t* chrc_uuid = psttemp->uuid;
316
323
317
- //pr_debug(LOG_MODULE_BLE, "%s-%d:handle-%d:%d", __FUNCTION__, __LINE__,attr->handle, chrc_handle);
318
- if (BLE_STATUS_SUCCESS != retval)
324
+ uint16_t le16;
325
+ memcpy(&le16, &BT_UUID_16(chrc_uuid)->val, sizeof(le16));
326
+ if (chrc_uuid->type == BT_UUID_TYPE_16 &&
327
+ le16 == 0)
319
328
{
320
- pr_error(LOG_MODULE_BLE, "%s-%d: Error-%d",
321
- __FUNCTION__, __LINE__, retval );
322
- errno = ENOMEM ;
329
+ // Read the UUID
330
+ readCharacteristic(device, chrc_handle );
331
+ retVal = BT_GATT_ITER_CONTINUE ;
323
332
}
324
333
else
325
334
{
326
- retVal = BT_GATT_ITER_CONTINUE;
335
+ int retval = (int)addCharacteristic(device,
336
+ psttemp->uuid,
337
+ chrc_handle,
338
+ psttemp->properties);
339
+
340
+ if (BLE_STATUS_SUCCESS != retval)
341
+ {
342
+ pr_error(LOG_MODULE_BLE, "%s-%d: Error-%d",
343
+ __FUNCTION__, __LINE__, retval);
344
+ errno = ENOMEM;
345
+ }
346
+ else
347
+ {
348
+ retVal = BT_GATT_ITER_CONTINUE;
349
+ }
327
350
}
328
351
}
329
352
break;
@@ -335,8 +358,8 @@ uint8_t BLEServiceImp::discoverResponseProc(bt_conn_t *conn,
335
358
if (NULL != _cur_discover_chrc)
336
359
{
337
360
retVal = _cur_discover_chrc->discoverResponseProc(conn,
338
- attr,
339
- params);
361
+ attr,
362
+ params);
340
363
}
341
364
break;
342
365
}
@@ -347,44 +370,143 @@ uint8_t BLEServiceImp::discoverResponseProc(bt_conn_t *conn,
347
370
}
348
371
}
349
372
350
- pr_debug(LOG_MODULE_BLE, "%s-%d:ret-%d",__FUNCTION__, __LINE__, retVal);
373
+ // pr_debug(LOG_MODULE_BLE, "%s-%d:ret-%d",__FUNCTION__, __LINE__, retVal);
351
374
if (retVal == BT_GATT_ITER_STOP)
352
375
{
353
376
if (errno == ENOMEM)
354
377
{
355
378
_cur_discover_chrc = NULL;
356
379
return retVal;
357
380
}
358
- const BLECharacteristicLinkNodeHeader* chrcHeader = &_characteristics_header;
359
- BLECharacteristicImp* chrcCurImp = NULL;
360
- BLECharacteristicNodePtr node = chrcHeader->next;
361
381
362
- pr_debug(LOG_MODULE_BLE, "%s-%d: node-%p",__FUNCTION__, __LINE__, node);
363
- // Discover next service
364
- while (node != NULL)
382
+ if (false == _reading)
365
383
{
366
- chrcCurImp = node->value;
367
-
368
- if (NULL == _cur_discover_chrc)
384
+ discoverNextCharacteristic(device);
385
+ }
386
+ }
387
+ return retVal;
388
+ }
389
+
390
+ void BLEServiceImp::discoverNextCharacteristic(BLEDevice &bledevice)
391
+ {
392
+ const BLECharacteristicLinkNodeHeader* chrcHeader = &_characteristics_header;
393
+ BLECharacteristicImp* chrcCurImp = NULL;
394
+ BLECharacteristicNodePtr node = chrcHeader->next;
395
+
396
+ //pr_debug(LOG_MODULE_BLE, "%s-%d: node-%p",__FUNCTION__, __LINE__, node);
397
+ // Discover next service
398
+ while (node != NULL)
399
+ {
400
+ chrcCurImp = node->value;
401
+
402
+ if (NULL == _cur_discover_chrc)
403
+ {
404
+ bool result = chrcCurImp->discoverAttributes(&bledevice);
405
+ pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
406
+ if (result == true)
369
407
{
370
- bool result = chrcCurImp->discoverAttributes(&device);
371
- pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
372
- if (result == true)
373
- {
374
- // Record the current discovering service
375
- _cur_discover_chrc = chrcCurImp;
376
- break;
377
- }
408
+ // Record the current discovering service
409
+ _cur_discover_chrc = chrcCurImp;
410
+ break;
378
411
}
379
- else if (_cur_discover_chrc == chrcCurImp)
412
+ }
413
+ else if (_cur_discover_chrc == chrcCurImp)
414
+ {
415
+ // Find next discoverable service
416
+ _cur_discover_chrc = NULL;
417
+ }
418
+ node = node->next;
419
+ }
420
+ }
421
+
422
+ bool BLEServiceImp::readCharacteristic(const BLEDevice &bledevice, uint16_t handle)
423
+ {
424
+ int retval = 0;
425
+ bt_conn_t* conn = NULL;
426
+
427
+ if (true == BLEUtils::isLocalBLE(bledevice))
428
+ {
429
+ // GATT server can't write
430
+ return false;
431
+ }
432
+
433
+ if (_reading)
434
+ {
435
+ return false;
436
+ }
437
+
438
+ _read_params.func = profile_characteristic_read_rsp_process;
439
+ _read_params.handle_count = 1;
440
+ _read_params.single.handle = handle - 1;
441
+ _read_params.single.offset = 0;
442
+
443
+ if (0 == _read_params.single.handle)
444
+ {
445
+ // Discover not complete
446
+ return false;
447
+ }
448
+
449
+ conn = bt_conn_lookup_addr_le(bledevice.bt_le_address());
450
+ if (NULL == conn)
451
+ {
452
+ return false;
453
+ }
454
+ // Send read request
455
+ retval = bt_gatt_read(conn, &_read_params);
456
+ bt_conn_unref(conn);
457
+ if (0 == retval)
458
+ {
459
+ _reading = true;
460
+ }
461
+ pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
462
+ return _reading;
463
+ }
464
+
465
+ uint8_t BLEServiceImp::characteristicReadRspProc(bt_conn_t *conn,
466
+ int err,
467
+ bt_gatt_read_params_t *params,
468
+ const void *data,
469
+ uint16_t length)
470
+ {
471
+ _reading = false;
472
+ if (NULL == data)
473
+ {
474
+ return BT_GATT_ITER_STOP;
475
+ }
476
+ BLEDevice bleDevice(bt_conn_get_dst(conn));
477
+
478
+ pr_debug(LOG_MODULE_BLE, "%s-%d:length-%d", __FUNCTION__, __LINE__, length);
479
+ if (length == UUID_SIZE_128 + 3)
480
+ {
481
+ const uint8_t* rspdata = (const uint8_t*) data;
482
+ bt_uuid_128_t uuid_tmp;
483
+ uint16_t chrc_handle = rspdata[1] | (rspdata[2] << 8);
484
+ uuid_tmp.uuid.type = BT_UUID_TYPE_128;
485
+ memcpy(uuid_tmp.val, &rspdata[3], UUID_SIZE_128);
486
+ int retval = (int)addCharacteristic(bleDevice,
487
+ (const bt_uuid_t*)&uuid_tmp,
488
+ chrc_handle,
489
+ rspdata[0]);
490
+
491
+ if (BLE_STATUS_SUCCESS != retval)
492
+ {
493
+ pr_error(LOG_MODULE_BLE, "%s-%d: Error-%d",
494
+ __FUNCTION__, __LINE__, retval);
495
+ errno = ENOMEM;
496
+ }
497
+ else
498
+ {
499
+ if (false == discovering())
380
500
{
381
- // Find next discoverable service
382
- _cur_discover_chrc = NULL;
501
+ if (false == discoverAttributes(&bleDevice, chrc_handle + 1, _end_handle))
502
+ {
503
+ discoverNextCharacteristic(bleDevice);
504
+ }
383
505
}
384
- node = node->next;
385
506
}
386
507
}
387
- return retVal;
508
+ pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
509
+
510
+ return BT_GATT_ITER_STOP;
388
511
}
389
512
390
-
0 commit comments