Skip to content

Commit d89471d

Browse files
committed
Change code based on code review
1. Modify the code based on code review 2. Fix build issues 3. Update the charateristic related issue
1 parent d58fdfc commit d89471d

File tree

10 files changed

+123
-59
lines changed

10 files changed

+123
-59
lines changed

libraries/BLE/examples/peripheral/led/led.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
BLEService ledService("19b10000e8f2537e4f6cd104768a1214");
2727

2828
// create switch characteristic
29-
BLEByteCharacteristic switchCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
29+
BLECharCharacteristic switchCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
3030

3131
BLEDescriptor switchDescriptor("2901", "switch");
3232

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
#include "BLECharacteristic.h"
77
#include "./internal/BLEProfileManager.h"
8+
#include "./internal/BLEDeviceManager.h"
89

910
#include "./internal/BLECharacteristicImp.h"
1011

1112
BLECharacteristic::BLECharacteristic():
12-
_bledev(), _internal(NULL), _properties(0),
13-
_value_size(0), _value(NULL)
13+
_bledev(), _internal(NULL), _chrc_local_imp(NULL), _broadcast(false),
14+
_properties(0), _value_size(0), _value(NULL),
15+
_event_handlers(NULL)
1416
{
1517
memset(_uuid_cstr, 0, sizeof(_uuid_cstr));
1618
memset(_event_handlers, 0, sizeof(_event_handlers));
@@ -19,7 +21,10 @@ BLECharacteristic::BLECharacteristic():
1921
BLECharacteristic::BLECharacteristic(const char* uuid,
2022
unsigned char properties,
2123
unsigned short valueSize):
22-
_bledev(), _internal(NULL), _properties(properties), _value(NULL)
24+
_bledev(), _internal(NULL), _chrc_local_imp(NULL), _broadcast(false),
25+
_properties(properties),
26+
_value(NULL),
27+
_event_handlers(NULL)
2328
{
2429
bt_uuid_128 bt_uuid_tmp;
2530
_value_size = valueSize > BLE_MAX_ATTR_LONGDATA_LEN ? BLE_MAX_ATTR_LONGDATA_LEN : valueSize;
@@ -39,8 +44,8 @@ BLECharacteristic::BLECharacteristic(const char* uuid,
3944

4045
BLECharacteristic::BLECharacteristic(BLECharacteristicImp *characteristicImp,
4146
const BLEDevice *bleDev):
42-
_bledev(bleDev), _internal(characteristicImp),
43-
_value(NULL)
47+
_bledev(bleDev), _internal(characteristicImp), _chrc_local_imp(NULL),
48+
_broadcast(false), _value(NULL),_event_handlers(NULL)
4449
{
4550
BLEUtils::uuidBT2String(characteristicImp->bt_uuid(), _uuid_cstr);
4651
_properties = characteristicImp->properties();
@@ -171,14 +176,6 @@ bool BLECharacteristic::setValue(const unsigned char value[], unsigned short len
171176

172177
bool BLECharacteristic::writeValue(const byte value[], int length)
173178
{
174-
bool retVar = false;
175-
BLECharacteristicImp *characteristicImp = getImplementation();
176-
177-
if (NULL != characteristicImp)
178-
{
179-
characteristicImp->writeValue(value, length);
180-
retVar = true;
181-
}
182179
return writeValue(value, length, 0);
183180
}
184181

@@ -191,6 +188,15 @@ bool BLECharacteristic::writeValue(const byte value[], int length, int offset)
191188
{
192189
characteristicImp->writeValue(value, length, offset);
193190
retVar = true;
191+
if (true == _broadcast &&
192+
true == BLEDeviceManager::instance()->advertising())
193+
{
194+
BLEDeviceManager::instance()->stopAdvertising();
195+
BLEDeviceManager::instance()->setAdvertisedServiceData(characteristicImp->bt_uuid(),
196+
characteristicImp->value(),
197+
characteristicImp->valueLength());
198+
BLEDeviceManager::instance()->startAdvertising();
199+
}
194200
}
195201
return retVar;
196202
}
@@ -202,8 +208,14 @@ bool BLECharacteristic::writeValue(const char* value)
202208

203209
bool BLECharacteristic::broadcast()
204210
{
205-
// TODO: Need more information
206-
return false;
211+
_broadcast = true;
212+
BLEDeviceManager::instance()->setConnectable(false);
213+
if (BLEDeviceManager::instance()->advertising())
214+
{
215+
BLEDeviceManager::instance()->stopAdvertising();
216+
BLEDeviceManager::instance()->startAdvertising();
217+
}
218+
return _broadcast;
207219
}
208220

209221
bool BLECharacteristic::written()
@@ -232,47 +244,46 @@ bool BLECharacteristic::subscribed()
232244

233245
bool BLECharacteristic::canNotify()
234246
{
235-
bool retVar = false;
236-
BLECharacteristicImp *characteristicImp = getImplementation();
237-
238-
if (NULL != characteristicImp)
239-
{
240-
retVar = characteristicImp->canNotify();
241-
}
242-
return retVar;
247+
return (_properties & BLENotify);
243248
}
244249

245250
bool BLECharacteristic::canIndicate()
246251
{
247-
bool retVar = false;
248-
BLECharacteristicImp *characteristicImp = getImplementation();
249-
250-
if (NULL != characteristicImp)
251-
{
252-
retVar = characteristicImp->canIndicate();
253-
}
254-
return retVar;
252+
return (_properties & BLEIndicate);
255253
}
256254

257255
bool BLECharacteristic::canRead()
258256
{
259-
// TODO: Need more confirmation
260-
return false;
257+
return (_properties & BLERead);
261258
}
259+
262260
bool BLECharacteristic::canWrite()
263261
{
264-
// TODO: Need more confirmation
265-
return false;
262+
return (_properties & BLEWrite);
266263
}
264+
267265
bool BLECharacteristic::canSubscribe()
268266
{
269-
// TODO: Need more confirmation
270-
return false;
267+
bool retVar = false;
268+
BLECharacteristicImp *characteristicImp = getImplementation();
269+
if (_properties & (BLENotify | BLEIndicate) &&
270+
(NULL != characteristicImp))
271+
{
272+
retVar = !characteristicImp->subscribed();
273+
}
274+
return retVar;
271275
}
276+
272277
bool BLECharacteristic::canUnsubscribe()
273278
{
274-
// TODO: Need more confirmation
275-
return false;
279+
bool retVar = false;
280+
BLECharacteristicImp *characteristicImp = getImplementation();
281+
282+
if (NULL != characteristicImp)
283+
{
284+
retVar = characteristicImp->subscribed();
285+
}
286+
return retVar;
276287
}
277288

278289
bool BLECharacteristic::read()

libraries/BLE/src/BLECharacteristic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ class BLECharacteristic: public BLEAttributeWithValue
518518
// None-NULL - GATT client
519519
BLECharacteristicImp *_internal; // The real implementation of characteristic.
520520
BLECharacteristicImp *_chrc_local_imp;
521+
bool _broadcast;
521522
protected:
522523
friend class BLECharacteristicImp;
523524
unsigned char _properties; // The characteristic property

libraries/BLE/src/BLEDescriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
19-
#include "BLEAttribute.h"
19+
#include "./internal/BLEAttribute.h"
2020
#include "BLEDescriptor.h"
2121
#include "./internal/BLEUtils.h"
2222
#include "./internal/BLEDescriptorImp.h"

libraries/BLE/src/BLEDevice.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ class BLEDevice
4848
*/
4949
BLEDevice();
5050

51-
/**
52-
* @brief The BLE device constructure
53-
*
54-
* @param[in] bleaddress BLE device address
55-
*
56-
* @return none
57-
*
58-
* @note none
59-
*/
60-
BLEDevice(const bt_addr_le_t* bleaddress);
6151
/**
6252
* @brief The BLE device constructure
6353
*
@@ -615,10 +605,29 @@ class BLEDevice
615605
friend class BLECharacteristic;
616606
friend class BLEDescriptor;
617607
friend class BLEService;
608+
friend uint8_t profile_notify_process (bt_conn_t *conn,
609+
bt_gatt_subscribe_params_t *params,
610+
const void *data, uint16_t length);
611+
friend uint8_t profile_read_rsp_process(bt_conn_t *conn,
612+
int err,
613+
bt_gatt_read_params_t *params,
614+
const void *data,
615+
uint16_t length);
618616
const bt_addr_le_t* bt_le_address() const;
619617
const bt_le_conn_param* bt_conn_param() const;
620618
void setAddress(const bt_addr_le_t& addr);
619+
621620
void setAdvertiseData(const uint8_t* adv_data, uint8_t len);
621+
/**
622+
* @brief The BLE device constructure
623+
*
624+
* @param[in] bleaddress BLE device address
625+
*
626+
* @return none
627+
*
628+
* @note none
629+
*/
630+
BLEDevice(const bt_addr_le_t* bleaddress);
622631
private:
623632
void preCheckProfile();
624633

libraries/BLE/src/internal/BLECharacteristicImp.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,26 +320,39 @@ bool BLECharacteristicImp::valueUpdated()
320320
bool
321321
BLECharacteristicImp::subscribed()
322322
{
323-
return _subscribed;
323+
if (false == BLEUtils::isLocalBLE(_ble_device))
324+
{
325+
// GATT client
326+
return _subscribed;
327+
}
328+
else
329+
{
330+
// GATT server
331+
return (_ccc_value.value & (BT_GATT_CCC_NOTIFY | BT_GATT_CCC_INDICATE));
332+
}
324333
}
325334

326335
bool BLECharacteristicImp::canNotify()
327336
{
328337
if (false == BLEUtils::isLocalBLE(_ble_device))
329338
{
330-
// GATT server can't subscribe
339+
// GATT client can't subscribe
331340
return false;
332341
}
342+
343+
// GATT server
333344
return (_ccc_value.value & BT_GATT_CCC_NOTIFY);
334345
}
335346

336347
bool BLECharacteristicImp::canIndicate()
337348
{
338349
if (false == BLEUtils::isLocalBLE(_ble_device))
339350
{
340-
// GATT server can't subscribe
351+
// GATT client can't subscribe
341352
return false;
342353
}
354+
355+
// GATT server
343356
return (_ccc_value.value & BT_GATT_CCC_INDICATE);
344357
}
345358

libraries/BLE/src/internal/BLEDeviceManager.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ BLEDeviceManager::BLEDeviceManager():
4040
_has_service_solicit_uuid(false),
4141
_appearance(0),
4242
_manufacturer_data_length(0),
43+
_service_data_length(0),
4344
_adv_type(0),
4445
_adv_data_idx(0),
4546
_local_name(""),
@@ -51,6 +52,9 @@ BLEDeviceManager::BLEDeviceManager():
5152

5253
memset(&_service_uuid, 0, sizeof(_service_uuid));
5354
memset(&_service_solicit_uuid, 0, sizeof(_service_solicit_uuid));
55+
memset(&_service_data_uuid, 0, sizeof(_service_data_uuid));
56+
memset(_service_data, 0, sizeof(_service_data));
57+
memset(_service_data_buf, 0, sizeof(_service_data_buf));
5458
memset(_adv_data, 0, sizeof(_adv_data));
5559

5660
memset(&_peer_central, 0, sizeof (bt_addr_le_t));
@@ -189,6 +193,20 @@ void BLEDeviceManager::setAdvertisedServiceUuid(const char* advertisedServiceUui
189193
BLEUtils::uuidString2BT(advertisedServiceUuid, (bt_uuid_t *)&_service_uuid);
190194
}
191195

196+
void BLEDeviceManager::setAdvertisedServiceData(const bt_uuid_t* serviceDataUuid,
197+
const uint8_t* serviceData,
198+
uint8_t serviceDataLength)
199+
{
200+
memcpy(&_service_data_uuid, serviceDataUuid, sizeof(_service_data_uuid));
201+
if (serviceDataLength > BLE_MAX_ADV_SIZE)
202+
{
203+
serviceDataLength = BLE_MAX_ADV_SIZE;
204+
}
205+
206+
memcpy(_service_data, serviceData, serviceDataLength);
207+
_service_data_length = serviceDataLength;
208+
}
209+
192210
void BLEDeviceManager::setServiceSolicitationUuid(const char* serviceSolicitationUuid)
193211
{
194212
_has_service_solicit_uuid = true;
@@ -375,13 +393,12 @@ BLEDeviceManager::_advDataInit(void)
375393
lengthTotal += _manufacturer_data_length;
376394
}
377395

378-
#if 0
379-
if (_service_data)
396+
if (_service_data_length > 0)
380397
{
381398
/* Add Service Data (if it will fit) */
382399

383400
/* A 128-bit Service Data UUID won't fit in an Advertising packet */
384-
if (BT_UUID_TYPE_16 != _service_data_uuid->type)
401+
if (BT_UUID_TYPE_16 != _service_data_uuid.uuid.type)
385402
{
386403
/* We support service data only for 16-bit service UUID */
387404
return BLE_STATUS_NOT_SUPPORTED;
@@ -401,13 +418,12 @@ BLEDeviceManager::_advDataInit(void)
401418

402419
uint8_t *adv_tmp = _service_data_buf;
403420

404-
UINT16_TO_LESTREAM(adv_tmp, (((bt_uuid_16_t *)_service_data_uuid)->val));
421+
UINT16_TO_LESTREAM(adv_tmp, (((bt_uuid_16_t *)&_service_data_uuid)->val));
405422
memcpy(adv_tmp, _service_data, _service_data_length);
406423

407424
lengthTotal += block_len;
408425
pr_info(LOG_MODULE_BLE, "SVC Len -%d", block_len);
409426
}
410-
#endif
411427

412428
if (lengthTotal > BLE_MAX_ADV_SIZE)
413429
{
@@ -443,6 +459,11 @@ BLE_STATUS_T BLEDeviceManager::startAdvertising()
443459
return BLE_STATUS_SUCCESS;
444460
}
445461

462+
bool BLEDeviceManager::advertising()
463+
{
464+
return (BLE_PERIPH_STATE_ADVERTISING == _state);
465+
}
466+
446467
BLE_STATUS_T BLEDeviceManager::stopAdvertising()
447468
{
448469
int err_code = 0;

libraries/BLE/src/internal/BLEDeviceManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ class BLEDeviceManager
124124
* Only for peripheral mode.
125125
*/
126126
void setServiceSolicitationUuid(const char* serviceSolicitationUuid);
127+
void setAdvertisedServiceData(const bt_uuid_t* serviceDataUuid,
128+
const uint8_t* serviceData,
129+
uint8_t serviceDataLength);
127130

128131
/**
129132
* @brief Set the manufacturer data in the BLE Peripheral Device advertises
@@ -262,6 +265,8 @@ class BLEDeviceManager
262265
* @note none
263266
*/
264267
BLE_STATUS_T startAdvertising();
268+
269+
bool advertising();
265270

266271
/**
267272
* @brief Stop send advertisement
@@ -392,6 +397,10 @@ class BLEDeviceManager
392397
uint16_t _appearance;
393398
uint8_t _manufacturer_data[BLE_MAX_ADV_SIZE];
394399
uint8_t _manufacturer_data_length;
400+
bt_uuid_128_t _service_data_uuid;
401+
uint8_t _service_data[BLE_MAX_ADV_SIZE];
402+
uint8_t _service_data_buf[BLE_MAX_ADV_SIZE];
403+
uint8_t _service_data_length;
395404

396405
// ADV data for peripheral
397406
uint8_t _adv_type;

0 commit comments

Comments
 (0)