Skip to content

Commit d84bb1c

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 320b1b5 commit d84bb1c

File tree

10 files changed

+118
-57
lines changed

10 files changed

+118
-57
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: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
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), _chrc_local_imp(NULL),
13+
_bledev(), _internal(NULL), _chrc_local_imp(NULL), _broadcast(false),
1314
_properties(0), _value_size(0), _value(NULL),
1415
_event_handlers(NULL)
1516
{
@@ -19,7 +20,8 @@ BLECharacteristic::BLECharacteristic():
1920
BLECharacteristic::BLECharacteristic(const char* uuid,
2021
unsigned char properties,
2122
unsigned short valueSize):
22-
_bledev(), _internal(NULL), _chrc_local_imp(NULL), _properties(properties),
23+
_bledev(), _internal(NULL), _chrc_local_imp(NULL), _broadcast(false),
24+
_properties(properties),
2325
_value(NULL),
2426
_event_handlers(NULL)
2527
{
@@ -41,7 +43,7 @@ BLECharacteristic::BLECharacteristic(const char* uuid,
4143
BLECharacteristic::BLECharacteristic(BLECharacteristicImp *characteristicImp,
4244
const BLEDevice *bleDev):
4345
_bledev(bleDev), _internal(characteristicImp), _chrc_local_imp(NULL),
44-
_value(NULL),_event_handlers(NULL)
46+
_broadcast(false), _value(NULL),_event_handlers(NULL)
4547
{
4648
BLEUtils::uuidBT2String(characteristicImp->bt_uuid(), _uuid_cstr);
4749
_properties = characteristicImp->properties();
@@ -191,14 +193,6 @@ bool BLECharacteristic::setValue(const unsigned char value[], unsigned short len
191193

192194
bool BLECharacteristic::writeValue(const byte value[], int length)
193195
{
194-
bool retVar = false;
195-
BLECharacteristicImp *characteristicImp = getImplementation();
196-
197-
if (NULL != characteristicImp)
198-
{
199-
characteristicImp->writeValue(value, length);
200-
retVar = true;
201-
}
202196
return writeValue(value, length, 0);
203197
}
204198

@@ -211,6 +205,15 @@ bool BLECharacteristic::writeValue(const byte value[], int length, int offset)
211205
{
212206
characteristicImp->writeValue(value, length, offset);
213207
retVar = true;
208+
if (true == _broadcast &&
209+
true == BLEDeviceManager::instance()->advertising())
210+
{
211+
BLEDeviceManager::instance()->stopAdvertising();
212+
BLEDeviceManager::instance()->setAdvertisedServiceData(characteristicImp->bt_uuid(),
213+
characteristicImp->value(),
214+
characteristicImp->valueLength());
215+
BLEDeviceManager::instance()->startAdvertising();
216+
}
214217
}
215218
return retVar;
216219
}
@@ -222,8 +225,14 @@ bool BLECharacteristic::writeValue(const char* value)
222225

223226
bool BLECharacteristic::broadcast()
224227
{
225-
// TODO: Need more information
226-
return false;
228+
_broadcast = true;
229+
BLEDeviceManager::instance()->setConnectable(false);
230+
if (BLEDeviceManager::instance()->advertising())
231+
{
232+
BLEDeviceManager::instance()->stopAdvertising();
233+
BLEDeviceManager::instance()->startAdvertising();
234+
}
235+
return _broadcast;
227236
}
228237

229238
bool BLECharacteristic::written()
@@ -252,47 +261,46 @@ bool BLECharacteristic::subscribed()
252261

253262
bool BLECharacteristic::canNotify()
254263
{
255-
bool retVar = false;
256-
BLECharacteristicImp *characteristicImp = getImplementation();
257-
258-
if (NULL != characteristicImp)
259-
{
260-
retVar = characteristicImp->canNotify();
261-
}
262-
return retVar;
264+
return (_properties & BLENotify);
263265
}
264266

265267
bool BLECharacteristic::canIndicate()
266268
{
267-
bool retVar = false;
268-
BLECharacteristicImp *characteristicImp = getImplementation();
269-
270-
if (NULL != characteristicImp)
271-
{
272-
retVar = characteristicImp->canIndicate();
273-
}
274-
return retVar;
269+
return (_properties & BLEIndicate);
275270
}
276271

277272
bool BLECharacteristic::canRead()
278273
{
279-
// TODO: Need more confirmation
280-
return false;
274+
return (_properties & BLERead);
281275
}
276+
282277
bool BLECharacteristic::canWrite()
283278
{
284-
// TODO: Need more confirmation
285-
return false;
279+
return (_properties & BLEWrite);
286280
}
281+
287282
bool BLECharacteristic::canSubscribe()
288283
{
289-
// TODO: Need more confirmation
290-
return false;
284+
bool retVar = false;
285+
BLECharacteristicImp *characteristicImp = getImplementation();
286+
if (_properties & (BLENotify | BLEIndicate) &&
287+
(NULL != characteristicImp))
288+
{
289+
retVar = !characteristicImp->subscribed();
290+
}
291+
return retVar;
291292
}
293+
292294
bool BLECharacteristic::canUnsubscribe()
293295
{
294-
// TODO: Need more confirmation
295-
return false;
296+
bool retVar = false;
297+
BLECharacteristicImp *characteristicImp = getImplementation();
298+
299+
if (NULL != characteristicImp)
300+
{
301+
retVar = characteristicImp->subscribed();
302+
}
303+
return retVar;
296304
}
297305

298306
bool BLECharacteristic::read()

libraries/BLE/src/BLECharacteristic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ class BLECharacteristic: public BLEAttributeWithValue
514514
// None-NULL - GATT client
515515
BLECharacteristicImp *_internal; // The real implementation of characteristic.
516516
BLECharacteristicImp *_chrc_local_imp;
517+
bool _broadcast;
517518
protected:
518519
friend class BLECharacteristicImp;
519520
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
@@ -323,26 +323,39 @@ bool BLECharacteristicImp::valueUpdated()
323323
bool
324324
BLECharacteristicImp::subscribed()
325325
{
326-
return _subscribed;
326+
if (false == BLEUtils::isLocalBLE(_ble_device))
327+
{
328+
// GATT client
329+
return _subscribed;
330+
}
331+
else
332+
{
333+
// GATT server
334+
return (_ccc_value.value & (BT_GATT_CCC_NOTIFY | BT_GATT_CCC_INDICATE));
335+
}
327336
}
328337

329338
bool BLECharacteristicImp::canNotify()
330339
{
331340
if (false == BLEUtils::isLocalBLE(_ble_device))
332341
{
333-
// GATT server can't subscribe
342+
// GATT client can't subscribe
334343
return false;
335344
}
345+
346+
// GATT server
336347
return (_ccc_value.value & BT_GATT_CCC_NOTIFY);
337348
}
338349

339350
bool BLECharacteristicImp::canIndicate()
340351
{
341352
if (false == BLEUtils::isLocalBLE(_ble_device))
342353
{
343-
// GATT server can't subscribe
354+
// GATT client can't subscribe
344355
return false;
345356
}
357+
358+
// GATT server
346359
return (_ccc_value.value & BT_GATT_CCC_INDICATE);
347360
}
348361

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;
@@ -374,13 +392,12 @@ BLEDeviceManager::_advDataInit(void)
374392
lengthTotal += _manufacturer_data_length;
375393
}
376394

377-
#if 0
378-
if (_service_data)
395+
if (_service_data_length > 0)
379396
{
380397
/* Add Service Data (if it will fit) */
381398

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

401418
uint8_t *adv_tmp = _service_data_buf;
402419

403-
UINT16_TO_LESTREAM(adv_tmp, (((bt_uuid_16_t *)_service_data_uuid)->val));
420+
UINT16_TO_LESTREAM(adv_tmp, (((bt_uuid_16_t *)&_service_data_uuid)->val));
404421
memcpy(adv_tmp, _service_data, _service_data_length);
405422

406423
lengthTotal += block_len;
407424
pr_info(LOG_MODULE_BLE, "SVC Len -%d", block_len);
408425
}
409-
#endif
410426

411427
if (lengthTotal > BLE_MAX_ADV_SIZE)
412428
{
@@ -442,6 +458,11 @@ BLE_STATUS_T BLEDeviceManager::startAdvertising()
442458
return BLE_STATUS_SUCCESS;
443459
}
444460

461+
bool BLEDeviceManager::advertising()
462+
{
463+
return (BLE_PERIPH_STATE_ADVERTISING == _state);
464+
}
465+
445466
BLE_STATUS_T BLEDeviceManager::stopAdvertising()
446467
{
447468
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)