2626#include " BLECharacteristicImp.h"
2727
2828bt_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;
2930
3031bt_uuid_t *BLEServiceImp::getPrimayUuid (void )
3132{
@@ -36,6 +37,7 @@ BLEServiceImp::BLEServiceImp(BLEService& service):
3637 BLEAttribute(service.uuid(), BLETypeService),
3738 _start_handle(0 ),
3839 _end_handle(0xFFFF ),
40+ _reading(false ),
3941 _cur_discover_chrc(NULL )
4042{
4143 memset (&_characteristics_header, 0 , sizeof (_characteristics_header));
@@ -46,6 +48,7 @@ BLEServiceImp::BLEServiceImp(const bt_uuid_t* uuid):
4648 BLEAttribute(uuid, BLETypeService),
4749 _start_handle(0 ),
4850 _end_handle(0xFFFF ),
51+ _reading(false ),
4952 _cur_discover_chrc(NULL )
5053{
5154 memset (&_characteristics_header, 0 , sizeof (_characteristics_header));
@@ -242,10 +245,17 @@ BLECharacteristicImp* BLEServiceImp::characteristic(const char* uuid)
242245
243246bool BLEServiceImp::discovering ()
244247{
245- return (_cur_discover_chrc != NULL );
248+ return (_cur_discover_chrc != NULL || _reading );
246249}
247250
248251bool 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)
249259{
250260 pr_debug (LOG_MODULE_BLE, " %s-%d" , __FUNCTION__, __LINE__);
251261 int err;
@@ -273,8 +283,8 @@ bool BLEServiceImp::discoverAttributes(BLEDevice* device)
273283 return false ;
274284 }
275285 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 ;
278288 temp->uuid = NULL ;
279289 temp->type = BT_GATT_DISCOVER_CHARACTERISTIC;
280290 temp->func = profile_discover_process;
@@ -309,21 +319,34 @@ uint8_t BLEServiceImp::discoverResponseProc(bt_conn_t *conn,
309319 // const bt_uuid_t* chrc_uuid = attr->uuid;
310320 uint16_t chrc_handle = attr->handle + 1 ;
311321 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 ;
316323
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 )
319328 {
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 ;
323332 }
324333 else
325334 {
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+ }
327350 }
328351 }
329352 break ;
@@ -335,8 +358,8 @@ uint8_t BLEServiceImp::discoverResponseProc(bt_conn_t *conn,
335358 if (NULL != _cur_discover_chrc)
336359 {
337360 retVal = _cur_discover_chrc->discoverResponseProc (conn,
338- attr,
339- params);
361+ attr,
362+ params);
340363 }
341364 break ;
342365 }
@@ -347,44 +370,143 @@ uint8_t BLEServiceImp::discoverResponseProc(bt_conn_t *conn,
347370 }
348371 }
349372
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);
351374 if (retVal == BT_GATT_ITER_STOP)
352375 {
353376 if (errno == ENOMEM)
354377 {
355378 _cur_discover_chrc = NULL ;
356379 return retVal;
357380 }
358- const BLECharacteristicLinkNodeHeader* chrcHeader = &_characteristics_header;
359- BLECharacteristicImp* chrcCurImp = NULL ;
360- BLECharacteristicNodePtr node = chrcHeader->next ;
361381
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)
365383 {
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 )
369407 {
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 ;
378411 }
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 ())
380500 {
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+ }
383505 }
384- node = node->next ;
385506 }
386507 }
387- return retVal;
508+ pr_debug (LOG_MODULE_BLE, " %s-%d" , __FUNCTION__, __LINE__);
509+
510+ return BT_GATT_ITER_STOP;
388511}
389512
390-
0 commit comments