Skip to content

Commit 19b230e

Browse files
committed
Discover Services 1800 and 1801 by pass parameter
1. Fix Jira 769/git #378. Discover GAP service by parameter. 2. Fix Jira 770/git #379 to show the device name in central when discover GAP service. Changed service BLEDevice.cpp - Modify the API BLEDevice.h - Modify the API BLEDeviceManager.cpp - Add get device name logic BLEProfileManager.cpp - Implement get devicename and dicovery logic BLEProfileManager.h - Add API to get devicename from characteristic BLEServiceImp.cpp - Modify the filter logic BLEServiceImp.h - Modify the API
1 parent 4bb88a2 commit 19b230e

File tree

7 files changed

+76
-23
lines changed

7 files changed

+76
-23
lines changed

Diff for: libraries/CurieBLE/src/BLEDevice.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,10 @@ bool BLEDevice::connect()
376376
return BLEDeviceManager::instance()->connect(*this);
377377
}
378378

379-
bool BLEDevice::discoverAttributes()
379+
bool BLEDevice::discoverAttributes(bool discoverGapGatt)
380380
{
381-
return BLEProfileManager::instance()->discoverAllAttributes(this);
381+
return BLEProfileManager::instance()->discoverAllAttributes(this,
382+
discoverGapGatt);
382383
}
383384

384385
bool BLEDevice::discoverAttributesByService(const char* svc_uuid)

Diff for: libraries/CurieBLE/src/BLEDevice.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ class BLEDevice
481481
int rssi() const; // returns the RSSI of the peripheral at discovery
482482

483483
bool connect(); // connect to the peripheral
484-
bool discoverAttributes(); // discover the peripheral's attributes
484+
bool discoverAttributes(bool discoverGapGatt = false); // discover the peripheral's attributes
485485
bool discoverAttributesByService(const char* svc_uuid);
486486

487487
String deviceName(); // read the device name attribute of the peripheral, and return String value

Diff for: libraries/CurieBLE/src/internal/BLEDeviceManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ String BLEDeviceManager::deviceName(const BLEDevice* device)
12931293
{
12941294
return _device_name;
12951295
}
1296-
return String("");
1296+
return BLEProfileManager::instance()->getDeviceName(device);
12971297
}
12981298

12991299
int BLEDeviceManager::appearance()

Diff for: libraries/CurieBLE/src/internal/BLEProfileManager.cpp

+45-10
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,10 @@ void BLEProfileManager::handleDisconnectedPutOffEvent()
593593
}
594594
}
595595

596-
bool BLEProfileManager::discoverAllAttributes(BLEDevice* device)
596+
bool BLEProfileManager::discoverAllAttributes(BLEDevice* device,
597+
bool discoverGapGatt)
597598
{
599+
_discover_gap_gatt = discoverGapGatt;
598600
return discoverAttributes(device, NULL);
599601
}
600602

@@ -692,7 +694,7 @@ void BLEProfileManager::setDiscovering(bool discover)
692694
void BLEProfileManager::singleServiceDiscoverResponseProc(BLEDevice &device,
693695
BLEServiceImp* service)
694696
{
695-
bool result = service->discoverAttributes(&device);
697+
bool result = service->discoverAttributes(&device, true);
696698
if (result == true)
697699
{
698700
// Record the current discovering service
@@ -786,7 +788,8 @@ bool BLEProfileManager::discoverNextService(BLEDevice &device)
786788

787789
if (NULL == _cur_discover_service)
788790
{
789-
bool result = serviceCurImp->discoverAttributes(&device);
791+
bool result = serviceCurImp->discoverAttributes(&device,
792+
_discover_gap_gatt);
790793
if (result == true)
791794
{
792795
// Record the current discovering service
@@ -890,17 +893,19 @@ void BLEProfileManager::serviceDiscoverComplete(const BLEDevice &bledevice)
890893
serviceCurImp = node->value;
891894
if (NULL != serviceCurImp)
892895
{
893-
if (servicePrevImp) // KW issue: Chk for NULL.
896+
if (servicePrevImp)
897+
{
894898
servicePrevImp->setEndHandle(serviceCurImp->startHandle() - 1);
899+
pr_debug(LOG_MODULE_BLE, "Pre: start-%d, end-%d",
900+
servicePrevImp->startHandle(),
901+
servicePrevImp->endHandle());
902+
pr_debug(LOG_MODULE_BLE, "Curr: start-%d, end-%d",
903+
serviceCurImp->startHandle(),
904+
serviceCurImp->endHandle());
905+
}
895906
}
896907

897-
if (servicePrevImp)
898-
{
899-
pr_debug(LOG_MODULE_BLE, "Curr: start-%d, end-%d", servicePrevImp->startHandle(), servicePrevImp->endHandle());
900-
}
901908
servicePrevImp = serviceCurImp;
902-
if (servicePrevImp) // KW issue: Chk for NULL.
903-
pr_debug(LOG_MODULE_BLE, "Curr: start-%d, end-%d", servicePrevImp->startHandle(), servicePrevImp->endHandle());
904909
node = node->next;
905910
}
906911
return;
@@ -1059,3 +1064,33 @@ unsigned short BLEProfileManager::getAppearance()
10591064
return appearenceChrc.value();
10601065
}
10611066

1067+
String BLEProfileManager::getDeviceName(const BLEDevice* device)
1068+
{
1069+
String temp("");
1070+
char device_name_buff[BLE_MAX_DEVICE_NAME];
1071+
unsigned short device_name_len;
1072+
1073+
BLEServiceImp* gap_service = service(*device, "1800");
1074+
if (NULL == gap_service)
1075+
{
1076+
return temp;
1077+
}
1078+
BLECharacteristicImp* devicename_chrc = gap_service->characteristic("2a00");
1079+
if (NULL == devicename_chrc)
1080+
{
1081+
return temp;
1082+
}
1083+
1084+
// Read device name
1085+
if (false == devicename_chrc->read(true))
1086+
{
1087+
return temp;
1088+
}
1089+
device_name_len = devicename_chrc->valueLength();
1090+
memcpy(device_name_buff, devicename_chrc->value(), device_name_len);
1091+
device_name_buff[device_name_len] = '\0';
1092+
temp = device_name_buff;
1093+
1094+
return temp;
1095+
}
1096+

Diff for: libraries/CurieBLE/src/internal/BLEProfileManager.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ class BLEProfileManager{
107107
const bt_gatt_attr_t *attr,
108108
bt_gatt_discover_params_t *params);
109109

110-
bool discoverAllAttributes(BLEDevice* device);
110+
// Default not discover the GAP/GATT service
111+
bool discoverAllAttributes(BLEDevice* device,
112+
bool discoverGapGatt = false);
111113
bool discoverAttributesByService(BLEDevice* device, const bt_uuid_t* svc_uuid);
112114
void handleConnectedEvent(const bt_addr_le_t* deviceAddr);
113115
void handleDisconnectedEvent(const bt_addr_le_t* deviceAddr);
@@ -131,6 +133,8 @@ class BLEProfileManager{
131133
*/
132134
void setAppearance(unsigned short appearance);
133135
unsigned short getAppearance();
136+
137+
String getDeviceName(const BLEDevice* device);
134138
protected:
135139
friend ssize_t profile_write_process(bt_conn_t *conn,
136140
const bt_gatt_attr_t *attr,
@@ -223,6 +227,7 @@ class BLEProfileManager{
223227
bt_addr_le_t _discovering_ble_addresses;
224228

225229
bool _start_discover;
230+
bool _discover_gap_gatt;
226231

227232
bool _discovering;
228233
uint64_t _discover_rsp_timestamp;

Diff for: libraries/CurieBLE/src/internal/BLEServiceImp.cpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,19 @@ void BLEServiceImp::setDiscovering(bool enable)
284284
_discovering = enable;
285285
}
286286

287-
bool BLEServiceImp::discoverAttributes(BLEDevice* device)
287+
bool BLEServiceImp::discoverAttributes(BLEDevice* device,
288+
bool discoverGapGatt)
288289
{
289-
return discoverAttributes(device, _start_handle, _end_handle);
290+
return discoverAttributes(device,
291+
_start_handle,
292+
_end_handle,
293+
discoverGapGatt);
290294
}
291295

292296
bool BLEServiceImp::discoverAttributes(BLEDevice* device,
293297
uint16_t start_handle,
294-
uint16_t end_handle)
298+
uint16_t end_handle,
299+
bool discoverGapGatt)
295300
{
296301
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
297302
int err;
@@ -300,9 +305,10 @@ bool BLEServiceImp::discoverAttributes(BLEDevice* device,
300305
const bt_uuid_t* service_uuid = bt_uuid();
301306

302307
// Filt out GAP/GATT service
303-
if (service_uuid->type == BT_UUID_TYPE_16)
308+
if (discoverGapGatt == false && // Don't discover GAP and GATT service
309+
service_uuid->type == BT_UUID_TYPE_16)
304310
{
305-
uint16_t uuid_tmp;// = ((bt_uuid_16_t*)service_uuid)->val;
311+
uint16_t uuid_tmp;
306312

307313
memcpy(&uuid_tmp, &((bt_uuid_16_t*)service_uuid)->val, sizeof(uuid_tmp));
308314
if (BT_UUID_GAP_VAL == uuid_tmp ||
@@ -319,6 +325,7 @@ bool BLEServiceImp::discoverAttributes(BLEDevice* device,
319325
pr_debug(LOG_MODULE_BLE, "Can't find connection\n");
320326
return false;
321327
}
328+
322329
temp = &_discover_params;
323330
temp->start_handle = start_handle;
324331
temp->end_handle = end_handle;
@@ -533,7 +540,10 @@ uint8_t BLEServiceImp::characteristicReadRspProc(bt_conn_t *conn,
533540
{
534541
if (false == discovering())
535542
{
536-
if (false == discoverAttributes(&bleDevice, chrc_handle + 1, _end_handle))
543+
if (false == discoverAttributes(&bleDevice,
544+
chrc_handle + 1,
545+
_end_handle,
546+
true))
537547
{
538548
discoverNextCharacteristic(bleDevice);
539549
}

Diff for: libraries/CurieBLE/src/internal/BLEServiceImp.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class BLEServiceImp: public BLEAttribute{
6969
inline uint16_t endHandle(){return _end_handle;}
7070
inline uint16_t startHandle(){return _start_handle;}
7171

72-
bool discoverAttributes(BLEDevice* device);
72+
bool discoverAttributes(BLEDevice* device,
73+
bool discoverGapGatt);
7374
uint8_t discoverResponseProc(bt_conn_t *conn,
7475
const bt_gatt_attr_t *attr,
7576
bt_gatt_discover_params_t *params);
@@ -95,7 +96,8 @@ class BLEServiceImp: public BLEAttribute{
9596
bool readCharacteristic(const BLEDevice &bledevice, uint16_t handle);
9697
bool discoverAttributes(BLEDevice* device,
9798
uint16_t start_handle,
98-
uint16_t end_handle);
99+
uint16_t end_handle,
100+
bool discoverGapGatt);
99101
void setDiscovering(bool enable);
100102
private:
101103
typedef LinkNode<BLECharacteristicImp *> BLECharacteristicLinkNodeHeader;

0 commit comments

Comments
 (0)