Skip to content

Commit e916136

Browse files
committed
Fix discover potential bugs
1. The discover can support mutiple services in GATT server. 2. The attributes doesn't include the CCCD and make regiter service failed
1 parent 5b69b5a commit e916136

File tree

9 files changed

+174
-145
lines changed

9 files changed

+174
-145
lines changed

libraries/BLE/src/BLECharacteristicImp.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include "BLEAttribute.h"
21+
#include "BLEServiceImp.h"
2122
#include "BLECharacteristicImp.h"
2223

2324
#include "BLECallbacks.h"
@@ -811,6 +812,11 @@ void BLECharacteristicImp::releaseDescriptors()
811812
int BLECharacteristicImp::getAttributeCount()
812813
{
813814
int counter = link_list_size(&_descriptors_header) + 2; // Declaration and descriptor
815+
// Notification/Indecation
816+
if (_gatt_chrc.properties & (BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_INDICATE))
817+
{
818+
counter++;
819+
}
814820
return counter;
815821
}
816822

@@ -893,13 +899,19 @@ uint8_t BLECharacteristicImp::discoverResponseProc(bt_conn_t *conn,
893899
{
894900
if (NULL != attr)
895901
{
902+
retVal = BT_GATT_ITER_CONTINUE;
896903
const bt_uuid_t* desc_uuid = attr->uuid;
897904
uint16_t desc_handle = attr->handle;
898905
pr_debug(LOG_MODULE_BLE, "%s-%d:handle-%d:%d", __FUNCTION__, __LINE__,attr->handle, desc_handle);
899906
if (isClientCharacteristicConfigurationDescriptor(desc_uuid))
900907
{
901908
setCCCDHandle(desc_handle);
902909
}
910+
else if (bt_uuid_cmp(BLEServiceImp::getPrimayUuid(), desc_uuid) == 0 ||
911+
bt_uuid_cmp(getCharacteristicAttributeUuid(), desc_uuid) == 0 )
912+
{
913+
retVal = BT_GATT_ITER_STOP;
914+
}
903915
else
904916
{
905917
int retval = (int)addDescriptor(desc_uuid,
@@ -913,7 +925,6 @@ uint8_t BLECharacteristicImp::discoverResponseProc(bt_conn_t *conn,
913925
}
914926

915927
}
916-
retVal = BT_GATT_ITER_CONTINUE;
917928
}
918929
break;
919930
}

libraries/BLE/src/BLECommon.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ typedef uint16_t ble_status_t; /**< Response and event BLE service status type @
9999

100100
typedef ble_status_t BleStatus;
101101

102-
#define BLE_MAX_CONN_CFG 2
102+
#define BLE_LIB_ASSERT(cond) ((cond) ? (void)0 : __assert_fail())
103+
104+
#define BLE_MAX_CONN_CFG 2
105+
#define BLE_MAX_ADV_BUFFER_CFG 3
103106

104107
typedef bool (*ble_advertise_handle_cb_t)(uint8_t type, const uint8_t *dataPtr,
105108
uint8_t data_len, const bt_addr_le_t *addrPtr);

libraries/BLE/src/BLEDevice.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,20 @@ void BLEDevice::setAdvertisingInterval(float advertisingInterval)
124124
BLEDeviceManager::instance()->setAdvertisingInterval(advertisingInterval);
125125
}
126126

127-
void BLEDevice::setConnectionInterval(float minimumConnectionInterval,
128-
float maximumConnectionInterval,
127+
void BLEDevice::setConnectionInterval(int minimumConnectionInterval,
128+
int maximumConnectionInterval,
129129
uint16_t latency,
130130
uint16_t timeout)
131-
{}
131+
{
132+
// TODO: Update the connection interval need more discussion
133+
}
132134

133-
void BLEDevice::setConnectionInterval(float minimumConnectionInterval,
134-
float maximumConnectionInterval)
135-
{}
135+
void BLEDevice::setConnectionInterval(int minimumConnectionInterval,
136+
int maximumConnectionInterval)
137+
{
138+
// TODO: Update the connection interval need more discussion
139+
140+
}
136141

137142
bool BLEDevice::setTxPower(int txPower)
138143
{
@@ -177,14 +182,11 @@ BLEDevice BLEDevice::central()
177182

178183
BLEDevice BLEDevice::peripheral()
179184
{
180-
// TODO
185+
// TODO: How to get the target devices
181186
BLEDevice temp;
182187
return temp;
183188
}
184189

185-
void BLEDevice::linkLost()
186-
{}
187-
188190
BLEDevice::operator bool() const
189191
{
190192
return BLEUtils::macAddressValid(_bt_addr);
@@ -225,7 +227,9 @@ void BLEDevice::startScanning(BLEService& service)
225227
}
226228

227229
void BLEDevice::startScanningWithDuplicates()
228-
{}
230+
{
231+
// TODO
232+
}
229233

230234
void BLEDevice::stopScanning()
231235
{

libraries/BLE/src/BLEDevice.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ class BLEDevice
226226
*
227227
* @note none
228228
*/
229-
void setConnectionInterval(float minimumConnectionInterval,
230-
float maximumConnectionInterval,
229+
void setConnectionInterval(int minimumConnectionInterval,
230+
int maximumConnectionInterval,
231231
uint16_t latency,
232232
uint16_t timeout);
233233

@@ -243,8 +243,8 @@ class BLEDevice
243243
*
244244
* @note none
245245
*/
246-
void setConnectionInterval(float minimumConnectionInterval,
247-
float maximumConnectionInterval);
246+
void setConnectionInterval(int minimumConnectionInterval,
247+
int maximumConnectionInterval);
248248

249249
/**
250250
* @brief Set TX power of the radio in dBM
@@ -346,17 +346,6 @@ class BLEDevice
346346
*/
347347
BLEDevice peripheral();
348348

349-
/**
350-
* @brief Release the resources when link lost
351-
*
352-
* @param none
353-
*
354-
* @return none
355-
*
356-
* @note Peer devices only. Do nothing if local BLE device called.
357-
*/
358-
void linkLost();
359-
360349
operator bool() const;
361350
bool operator==(const BLEDevice& device) const;
362351
bool operator!=(const BLEDevice& device) const;

libraries/BLE/src/BLEDeviceManager.cpp

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ BLEDeviceManager::BLEDeviceManager():
3737
_has_service_uuid(false),
3838
_has_service_solicit_uuid(false),
3939
_appearance(0),
40+
_manufacturer_data_length(0),
4041
_adv_type(0),
4142
_adv_data_idx(0),
4243
_local_name(""),
@@ -71,6 +72,9 @@ BLEDeviceManager::BLEDeviceManager():
7172

7273
memset(_peer_peripheral, 0, sizeof(_peer_peripheral));
7374
memset(_device_events, 0, sizeof(_device_events));
75+
memset(_manufacturer_data, 0, sizeof(_manufacturer_data));
76+
memset(_peer_adv_data, 0, sizeof(_peer_adv_data));
77+
memset(_peer_adv_data_len, 0, sizeof(_peer_adv_data_len));
7478
}
7579

7680
BLEDeviceManager::~BLEDeviceManager()
@@ -153,7 +157,12 @@ void BLEDeviceManager::setServiceSolicitationUuid(const char* serviceSolicitatio
153157
void BLEDeviceManager::setManufacturerData(const unsigned char manufacturerData[],
154158
unsigned char manufacturerDataLength)
155159
{
156-
// TODO: Add late
160+
if (manufacturerDataLength > BLE_MAX_ADV_SIZE)
161+
{
162+
manufacturerDataLength = BLE_MAX_ADV_SIZE;
163+
}
164+
_manufacturer_data_length = manufacturerDataLength;
165+
memcpy(_manufacturer_data, manufacturerData, manufacturerDataLength);
157166
}
158167

159168
void BLEDeviceManager::setLocalName(const char *localName)
@@ -313,8 +322,18 @@ BLEDeviceManager::_advDataInit(void)
313322
pr_info(LOG_MODULE_BLE, "Local Name Len -%d", _local_name.length());
314323
}
315324

325+
if (_manufacturer_data_length > 0)
326+
{
327+
// Add manufacturer data
328+
_adv_data[_adv_data_idx].type = BT_DATA_MANUFACTURER_DATA;
329+
_adv_data[_adv_data_idx].data = _manufacturer_data;
330+
_adv_data[_adv_data_idx].data_len = _manufacturer_data_length;
331+
_adv_data_idx++;
332+
333+
lengthTotal += _manufacturer_data_length;
334+
}
335+
316336
#if 0
317-
318337
if (_service_data)
319338
{
320339
/* Add Service Data (if it will fit) */
@@ -347,6 +366,7 @@ BLEDeviceManager::_advDataInit(void)
347366
pr_info(LOG_MODULE_BLE, "SVC Len -%d", block_len);
348367
}
349368
#endif
369+
350370
if (lengthTotal > BLE_MAX_ADV_SIZE)
351371
{
352372
pr_error(LOG_MODULE_BLE, "ADV Total length-%d", lengthTotal);
@@ -412,11 +432,6 @@ BLEDevice BLEDeviceManager::peripheral()
412432
return temp;
413433
}
414434

415-
void BLEDeviceManager::linkLost()
416-
{
417-
418-
}
419-
420435
bool BLEDeviceManager::startScanning()
421436
{
422437
int err = bt_le_scan_start(&_scan_param, ble_central_device_found);
@@ -718,43 +733,6 @@ bool BLEDeviceManager::advertiseDataProc(uint8_t type,
718733
// Now Only support 1 critical. Change those code if want support multi-criticals
719734
return true;
720735
}
721-
// Please see https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
722-
// To decode the data the central device cares.
723-
// This example use UUID as identity.
724-
#if 0
725-
switch (type)
726-
{
727-
case BT_DATA_UUID128_SOME:
728-
case BT_DATA_UUID128_ALL:
729-
{
730-
if (data_len % UUID_SIZE_128 != 0)
731-
{
732-
Serial.println("AD malformed");
733-
return true;
734-
}
735-
for (i = 0; i < data_len; i += UUID_SIZE_128)
736-
{
737-
if (bleImuService.uuidCompare(dataPtr + i, UUID_SIZE_128) == false)
738-
{
739-
continue;
740-
}
741-
742-
// Accept the advertisement
743-
if (!bleCentral.stopScan())
744-
{
745-
Serial.println("Stop LE scan failed");
746-
continue;
747-
}
748-
Serial.println("Connecting");
749-
return false;
750-
}
751-
}
752-
case BT_DATA_NAME_COMPLETE:
753-
{
754-
break;
755-
}
756-
}
757-
#endif
758736

759737
return false;
760738
}
@@ -764,11 +742,11 @@ BLEDevice BLEDeviceManager::available()
764742
BLEDevice tempdevice;
765743
bt_addr_le_t* temp = NULL;
766744
uint64_t timestamp = millis();
767-
uint8_t index = 3;
745+
uint8_t index = BLE_MAX_ADV_BUFFER_CFG;
768746
uint8_t i = 0;
769747
uint64_t max_delta = 0;
770748

771-
for (i = 0; i < 3; i++)
749+
for (i = 0; i < BLE_MAX_ADV_BUFFER_CFG; i++)
772750
{
773751
uint64_t timestamp_delta = timestamp - _peer_adv_mill[i];
774752
temp = &_peer_adv_buffer[i];
@@ -780,7 +758,7 @@ BLEDevice BLEDeviceManager::available()
780758
}
781759
//pr_debug(LOG_MODULE_BLE, "%s-%d:index %d, i-%d", __FUNCTION__, __LINE__, index, i);
782760

783-
if (index < 3)
761+
if (index < BLE_MAX_ADV_BUFFER_CFG)
784762
{
785763
temp = &_peer_adv_buffer[index];
786764
if (true == BLEUtils::macAddressValid(*temp))
@@ -793,16 +771,18 @@ BLEDevice BLEDeviceManager::available()
793771
return tempdevice;
794772
}
795773

796-
bool BLEDeviceManager::setAdvertiseBuffer(const bt_addr_le_t* bt_addr)
774+
bool BLEDeviceManager::setAdvertiseBuffer(const bt_addr_le_t* bt_addr,
775+
const uint8_t *ad,
776+
uint8_t data_len)
797777
{
798778
bt_addr_le_t* temp = NULL;
799779
uint64_t timestamp = millis();
800-
uint8_t index = 3;
780+
uint8_t index = BLE_MAX_ADV_BUFFER_CFG;
801781
uint8_t i = 0;
802782
uint64_t max_delta = 0;
803783
bool retval = false;
804784
//pr_debug(LOG_MODULE_BLE, "%s-%d-1", __FUNCTION__, __LINE__);
805-
for (i = 0; i < 3; i++)
785+
for (i = 0; i < BLE_MAX_ADV_BUFFER_CFG; i++)
806786
{
807787
uint64_t timestamp_delta = timestamp - _peer_adv_mill[i];
808788
temp = &_peer_adv_buffer[i];
@@ -824,13 +804,19 @@ bool BLEDeviceManager::setAdvertiseBuffer(const bt_addr_le_t* bt_addr)
824804
//pr_debug(LOG_MODULE_BLE, "%s-%d:index %d, i-%d", __FUNCTION__, __LINE__, index, i);
825805

826806
//pr_debug(LOG_MODULE_BLE, "%s-%d-2", __FUNCTION__, __LINE__);
827-
if (index < 3)
807+
if (index < BLE_MAX_ADV_BUFFER_CFG)
828808
{
829809
temp = &_peer_adv_buffer[index];
830-
if (i >= 3)
810+
if (i >= BLE_MAX_ADV_BUFFER_CFG)
831811
{
832812
memcpy(temp, bt_addr, sizeof (bt_addr_le_t));
833813
}
814+
if (data_len > BLE_MAX_ADV_SIZE)
815+
{
816+
data_len = BLE_MAX_ADV_SIZE;
817+
}
818+
memcpy(_peer_adv_data[index], ad, data_len);
819+
_peer_adv_data_len[index] = data_len;
834820
// Update the timestamp
835821
_peer_adv_mill[index] = timestamp;
836822
retval = true;
@@ -882,7 +868,7 @@ void BLEDeviceManager::handleDeviceFound(const bt_addr_le_t *addr,
882868
{
883869
// The critical is accepted
884870
// Find the oldest and expired buffer
885-
if(false == setAdvertiseBuffer(addr))
871+
if(false == setAdvertiseBuffer(addr, ad, data_len))
886872
{
887873
pr_info(LOG_MODULE_BLE, "No buffer to store the ADV\n");
888874
}

libraries/BLE/src/BLEDeviceManager.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,6 @@ class BLEDeviceManager
294294
*/
295295
BLEDevice peripheral();
296296

297-
/**
298-
* @brief Release the resources when link lost
299-
*
300-
* @param none
301-
*
302-
* @return none
303-
*
304-
* @note Peer devices only. Do nothing if local BLE device called.
305-
*/
306-
void linkLost();
307-
308297
operator bool() const;
309298

310299
// central mode
@@ -358,7 +347,9 @@ class BLEDeviceManager
358347
bool advertiseDataProc(uint8_t type,
359348
const uint8_t *dataPtr,
360349
uint8_t data_len);
361-
bool setAdvertiseBuffer(const bt_addr_le_t* bt_addr);
350+
bool setAdvertiseBuffer(const bt_addr_le_t* bt_addr,
351+
const uint8_t *ad,
352+
uint8_t data_len);
362353

363354
private:
364355
uint16_t _min_conn_interval;
@@ -368,8 +359,10 @@ class BLEDeviceManager
368359

369360
// For Central
370361
bt_le_scan_param_t _scan_param; // Scan parameter
371-
bt_addr_le_t _peer_adv_buffer[3]; // Accepted peer device adress
372-
uint64_t _peer_adv_mill[3]; // The ADV found time stamp
362+
bt_addr_le_t _peer_adv_buffer[BLE_MAX_ADV_BUFFER_CFG]; // Accepted peer device adress
363+
uint64_t _peer_adv_mill[BLE_MAX_ADV_BUFFER_CFG]; // The ADV found time stamp
364+
uint8_t _peer_adv_data[BLE_MAX_ADV_BUFFER_CFG][BLE_MAX_ADV_SIZE];
365+
uint8_t _peer_adv_data_len[BLE_MAX_ADV_BUFFER_CFG];
373366
bt_data_t _adv_accept_critical; // The filters for central device
374367
String _adv_critical_local_name;
375368
bt_uuid_128_t _adv_critical_service_uuid;
@@ -382,6 +375,8 @@ class BLEDeviceManager
382375
bool _has_service_solicit_uuid;
383376
bt_uuid_128_t _service_solicit_uuid;
384377
uint16_t _appearance;
378+
uint8_t _manufacturer_data[BLE_MAX_ADV_SIZE];
379+
uint8_t _manufacturer_data_length;
385380

386381
// ADV data for peripheral
387382
uint8_t _adv_type;

0 commit comments

Comments
 (0)