Skip to content

Commit 30b88ef

Browse files
committed
Fix the callback issue and the Klock scan issues
1. Fix the callback doesn't work issue 2. Fix the Klock scan issue
1 parent 521d1db commit 30b88ef

13 files changed

+153
-40
lines changed

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99

1010
BLECharacteristic::BLECharacteristic():
1111
_bledev(), _internal(NULL), _properties(0),
12-
_value_size(0), _value(NULL)
12+
_value_size(0), _value(NULL),_event_handlers(NULL)
1313
{
1414
memset(_uuid_cstr, 0, sizeof(_uuid_cstr));
1515
}
1616

1717
BLECharacteristic::BLECharacteristic(const char* uuid,
1818
unsigned char properties,
19-
unsigned char valueSize):
20-
_bledev(), _internal(NULL), _properties(properties), _value(NULL)
19+
unsigned short valueSize):
20+
_bledev(), _internal(NULL), _properties(properties), _value(NULL),
21+
_event_handlers(NULL)
2122
{
2223
bt_uuid_128 bt_uuid_tmp;
2324
_value_size = valueSize > BLE_MAX_ATTR_LONGDATA_LEN ? BLE_MAX_ATTR_LONGDATA_LEN : valueSize;
@@ -37,7 +38,7 @@ BLECharacteristic::BLECharacteristic(const char* uuid,
3738
BLECharacteristic::BLECharacteristic(BLECharacteristicImp *characteristicImp,
3839
const BLEDevice *bleDev):
3940
_bledev(bleDev), _internal(characteristicImp),
40-
_value(NULL)
41+
_value(NULL),_event_handlers(NULL)
4142
{
4243
BLEUtils::uuidBT2String(characteristicImp->bt_uuid(), _uuid_cstr);
4344
_properties = characteristicImp->properties();
@@ -51,6 +52,12 @@ BLECharacteristic::~BLECharacteristic()
5152
bfree(_value);
5253
_value = NULL;
5354
}
55+
56+
if (_event_handlers != NULL)
57+
{
58+
bfree(_event_handlers);
59+
_event_handlers = NULL;
60+
}
5461
}
5562

5663
const char* BLECharacteristic::uuid() const
@@ -329,9 +336,33 @@ BLEDescriptor BLECharacteristic::descriptor(const char * uuid, int index) const
329336
// TODO: Not support now
330337
return BLEDescriptor();
331338
}
339+
332340
void BLECharacteristic::setEventHandler(BLECharacteristicEvent event,
333341
BLECharacteristicEventHandler eventHandler)
334-
{}
342+
{
343+
BLECharacteristicImp *characteristicImp = getImplementation();
344+
if (event >= BLECharacteristicEventLast)
345+
{
346+
return;
347+
}
348+
349+
if (NULL != characteristicImp)
350+
{
351+
characteristicImp->setEventHandler(event, eventHandler);
352+
}
353+
else
354+
{
355+
if (_event_handlers == NULL)
356+
{
357+
_event_handlers = (BLECharacteristicEventHandler*)balloc(sizeof(BLECharacteristicEventHandler) * BLECharacteristicEventLast, NULL);
358+
}
359+
360+
if (_event_handlers != NULL)
361+
{
362+
_event_handlers[event] = eventHandler;
363+
}
364+
}
365+
}
335366

336367

337368
void

libraries/BLE/src/BLECharacteristic.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class BLECharacteristic //: public BLEAttributeWithValue
6969
*/
7070
BLECharacteristic(const char* uuid,
7171
unsigned char properties,
72-
unsigned char valueSize);
72+
unsigned short valueSize);
7373

7474
/**
7575
* @brief Create a characteristic with string value
@@ -498,8 +498,10 @@ class BLECharacteristic //: public BLEAttributeWithValue
498498
friend class BLECharacteristicImp;
499499
unsigned char _properties; // The characteristic property
500500

501-
unsigned char _value_size; // The value size
501+
unsigned short _value_size; // The value size
502502
unsigned char* _value; // The value. Will delete after create the _internal
503+
504+
BLECharacteristicEventHandler* _event_handlers;
503505
};
504506

505507
#endif

libraries/BLE/src/BLECharacteristicImp.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,16 @@ BLECharacteristicImp::BLECharacteristicImp(BLECharacteristic& characteristic,
143143
_sub_params.value |= BT_GATT_CCC_INDICATE;
144144
}
145145
_gatt_chrc.uuid = (bt_uuid_t*)this->bt_uuid();//&_characteristic_uuid;//this->uuid();
146-
memset(_event_handlers, 0, sizeof(_event_handlers));
146+
if (NULL != characteristic._event_handlers)
147+
{
148+
memcpy(_event_handlers,
149+
characteristic._event_handlers,
150+
sizeof(_event_handlers));
151+
}
152+
else
153+
{
154+
memset(_event_handlers, 0, sizeof(_event_handlers));
155+
}
147156

148157
_sub_params.notify = profile_notify_process;
149158

@@ -182,6 +191,7 @@ BLECharacteristicImp::properties() const
182191
bool BLECharacteristicImp::writeValue(const byte value[], int length)
183192
{
184193
int status;
194+
bool retVal = false;
185195

186196
_setValue(value, length);
187197

@@ -192,16 +202,16 @@ bool BLECharacteristicImp::writeValue(const byte value[], int length)
192202
{
193203
// Notify for peripheral.
194204
status = bt_gatt_notify(NULL, _attr_chrc_value, value, length, NULL);
195-
if (0 != status)
205+
if (0 == status)
196206
{
197-
return false;
207+
retVal = true;
198208
}
199209
}
200210

201211
//Not schedule write request for central
202212
// The write request may failed.
203213
// If user want to get latest set value. Call read and get the real value
204-
return true;
214+
return retVal;
205215
}
206216

207217
bool
@@ -389,7 +399,7 @@ void
389399
BLECharacteristicImp::setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler callback)
390400
{
391401
noInterrupts();
392-
if (event < sizeof(_event_handlers)) {
402+
if (event < BLECharacteristicEventLast) {
393403
_event_handlers[event] = callback;
394404
}
395405
interrupts();
@@ -666,7 +676,7 @@ int BLECharacteristicImp::addDescriptor(BLEDescriptor& descriptor)
666676
BLEDescriptorNodePtr node = link_node_create(descriptorImp);
667677
if (NULL == node)
668678
{
669-
delete[] descriptorImp;
679+
delete descriptorImp;
670680
return BLE_STATUS_NO_MEMORY;
671681
}
672682
link_node_insert_last(&_descriptors_header, node);
@@ -693,7 +703,7 @@ int BLECharacteristicImp::addDescriptor(const bt_uuid_t* uuid,
693703
BLEDescriptorNodePtr node = link_node_create(descriptorImp);
694704
if (NULL == node)
695705
{
696-
delete[] descriptorImp;
706+
delete descriptorImp;
697707
return BLE_STATUS_NO_MEMORY;
698708
}
699709
link_node_insert_last(&_descriptors_header, node);
@@ -736,7 +746,7 @@ void BLECharacteristicImp::releaseDescriptors()
736746
while (NULL != node)
737747
{
738748
BLEDescriptorImp* descriptorImp = node->value;
739-
delete[] descriptorImp;
749+
delete descriptorImp;
740750
link_node_remove_first(&_descriptors_header);
741751
node = link_node_get_first(&_descriptors_header);
742752
}

libraries/BLE/src/BLEDescriptor.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
#include "BLEDescriptor.h"
2020
#include "BLEUtils.h"
2121

22-
BLEDescriptor::BLEDescriptor()
23-
{}
22+
BLEDescriptor::BLEDescriptor():
23+
_properties(0),
24+
_value_size(0),
25+
_value(NULL)
26+
{
27+
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
28+
}
2429

2530
BLEDescriptor::BLEDescriptor(const char* uuid,
2631
const unsigned char value[],
27-
unsigned char valueLength):
32+
unsigned short valueLength):
2833
_bledev()
2934
{
3035
bt_uuid_128_t uuid_tmp;

libraries/BLE/src/BLEDescriptor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BLEDescriptor
2828
{
2929
public:
3030
BLEDescriptor();
31-
BLEDescriptor(const char* uuid, const unsigned char value[], unsigned char valueLength); // create a descriptor the specified uuid and value
31+
BLEDescriptor(const char* uuid, const unsigned char value[], unsigned short valueLength); // create a descriptor the specified uuid and value
3232
BLEDescriptor(const char* uuid, const char* value); // create a descriptor the specified uuid and string value
3333

3434
virtual ~BLEDescriptor();
@@ -91,7 +91,7 @@ class BLEDescriptor
9191

9292
unsigned char _properties; // The characteristic property
9393

94-
unsigned char _value_size; // The value size
94+
unsigned short _value_size; // The value size
9595
unsigned char* _value; // The value. Will delete after create the _internal
9696
};
9797

libraries/BLE/src/BLEDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ BLEDevice::BLEDevice(const bt_addr_le_t* bleaddress):
5656
BLEDevice::BLEDevice(const BLEDevice* bleaddress)
5757
{
5858
memcpy(&_bt_addr, bleaddress->bt_le_address(), sizeof(bt_addr_le_t));
59-
memcpy(&_conn_param, bleaddress->bt_conn_param(), sizeof (ble_conn_param_t));
59+
memcpy(&_conn_param, &bleaddress->_conn_param, sizeof (ble_conn_param_t));
6060
}
6161

6262
BLEDevice::~BLEDevice()
@@ -409,7 +409,7 @@ BLECharacteristic BLEDevice::characteristic(const char * uuid, int index) const
409409
void BLEDevice::setEventHandler(BLEDeviceEvent event,
410410
BLEDeviceEventHandler eventHandler)
411411
{
412-
// TODO:
412+
BLEDeviceManager::instance()->setEventHandler(event, eventHandler);
413413
}
414414

415415
const bt_addr_le_t* BLEDevice::bt_le_address() const

libraries/BLE/src/BLEDevice.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
//class BLEDevice;
2929

3030
enum BLEDeviceEvent {
31-
BLEDiscovered = 0, // Discover profile completed
32-
BLEConnected = 1, // BLE device connected
33-
BLEDisconnected = 2, // BLE device disconnected
34-
BLEConParamUpdate = 3, // Update the connection parameter
31+
BLEConnected = 0, // BLE device connected
32+
BLEDisconnected = 1, // BLE device disconnected
33+
BLEConParamUpdate = 2, // Update the connection parameter
3534
// Connection update request in central
3635
// Connection parameter updated in peripheral
36+
BLEDeviceLastEvent
3737
};
3838

3939
typedef void (*BLEDeviceEventHandler)(BLEDevice& device);

libraries/BLE/src/BLEDeviceManager.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ BLEDeviceManager::BLEDeviceManager():
6868
memset(&_adv_accept_critical, 0, sizeof(_adv_accept_critical));
6969

7070
memset(_peer_peripheral, 0, sizeof(_peer_peripheral));
71+
memset(_device_events, 0, sizeof(_device_events));
7172
}
7273

7374
BLEDeviceManager::~BLEDeviceManager()
@@ -383,7 +384,9 @@ BLEDevice BLEDeviceManager::peripheral()
383384
}
384385

385386
void BLEDeviceManager::linkLost()
386-
{}
387+
{
388+
389+
}
387390

388391
bool BLEDeviceManager::startScanning()
389392
{
@@ -405,7 +408,7 @@ bool BLEDeviceManager::startScanningWithDuplicates()
405408
bool BLEDeviceManager::stopScanning()
406409
{
407410
int err = bt_le_scan_stop();
408-
if (err)
411+
if (0 != err)
409412
{
410413
pr_info(LOG_MODULE_BLE, "Stop LE scan failed (err %d)\n", err);
411414
return false;
@@ -531,6 +534,7 @@ bool BLEDeviceManager::connectToDevice(BLEDevice &device)
531534
bool retval = false;
532535

533536
pr_debug(LOG_MODULE_BLE, "%s-%d-1", __FUNCTION__, __LINE__);
537+
534538
// Find free peripheral Items
535539
for (int i = 0; i < BLE_MAX_CONN_CFG; i++)
536540
{
@@ -555,7 +559,7 @@ bool BLEDeviceManager::connectToDevice(BLEDevice &device)
555559
}
556560
pr_debug(LOG_MODULE_BLE, "%s-%d:link_existed-%d unused-%p", __FUNCTION__, __LINE__, link_existed, unused);
557561

558-
if (!link_existed)
562+
if (!link_existed && NULL != unused)
559563
{
560564
pr_debug(LOG_MODULE_BLE, "%s-%d-Device:%s", __FUNCTION__, __LINE__, device.address().c_str());
561565
// Send connect request
@@ -589,6 +593,13 @@ BLEDeviceManager* BLEDeviceManager::instance()
589593
return _instance;
590594
}
591595

596+
void BLEDeviceManager::setEventHandler(BLEDeviceEvent event,
597+
BLEDeviceEventHandler eventHandler)
598+
{
599+
if (event < BLEDeviceLastEvent)
600+
_device_events[event] = eventHandler;
601+
}
602+
592603
void BLEDeviceManager::handleConnectEvent(bt_conn_t *conn, uint8_t err)
593604
{
594605
struct bt_conn_info role_info;
@@ -605,6 +616,12 @@ void BLEDeviceManager::handleConnectEvent(bt_conn_t *conn, uint8_t err)
605616
// Peripheral has established the connection with this Central device
606617
BLEProfileManager::instance()->handleConnectedEvent(bt_conn_get_dst(conn));
607618
}
619+
620+
if (NULL != _device_events[BLEConnected])
621+
{
622+
BLEDevice tempdev(bt_conn_get_dst(conn));
623+
_device_events[BLEConnected](tempdev);
624+
}
608625
}
609626

610627
void BLEDeviceManager::handleDisconnectEvent(bt_conn_t *conn, uint8_t reason)
@@ -619,7 +636,25 @@ void BLEDeviceManager::handleDisconnectEvent(bt_conn_t *conn, uint8_t reason)
619636
}
620637
else
621638
{
639+
bt_addr_le_t* temp = NULL;
640+
const bt_addr_le_t* disConnAddr = bt_conn_get_dst(conn);
641+
for (int i = 0; i < BLE_MAX_CONN_CFG; i++)
642+
{
643+
temp = &_peer_peripheral[i];
644+
if (bt_addr_le_cmp(temp, disConnAddr) == 0)
645+
{
646+
memset(temp, 0, sizeof(bt_addr_le_t));
647+
break;
648+
}
649+
}
622650
// Peripheral has established the connection with this Central device
651+
BLEProfileManager::instance()->handleDisconnectedEvent(bt_conn_get_dst(conn));
652+
}
653+
654+
if (NULL != _device_events[BLEDisconnected])
655+
{
656+
BLEDevice tempdev(bt_conn_get_dst(conn));
657+
_device_events[BLEDisconnected](tempdev);
623658
}
624659
}
625660

@@ -628,7 +663,11 @@ void BLEDeviceManager::handleParamUpdated (bt_conn_t *conn,
628663
uint16_t latency,
629664
uint16_t timeout)
630665
{
631-
666+
if (NULL != _device_events[BLEConParamUpdate])
667+
{
668+
BLEDevice tempdev(bt_conn_get_dst(conn));
669+
_device_events[BLEConParamUpdate](tempdev);
670+
}
632671
}
633672

634673
bool BLEDeviceManager::advertiseDataProc(uint8_t type,

libraries/BLE/src/BLEDeviceManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class BLEDeviceManager
100100
*/
101101
bool disconnect(BLEDevice *device);
102102

103+
void setEventHandler(BLEDeviceEvent event,
104+
BLEDeviceEventHandler eventHandler);
103105
/**
104106
* @brief Set the service UUID that the BLE Peripheral Device advertises
105107
*
@@ -412,6 +414,8 @@ class BLEDeviceManager
412414
// Connected device object
413415
bt_addr_le_t _peer_central;
414416
bt_addr_le_t _peer_peripheral[BLE_MAX_CONN_CFG];
417+
418+
BLEDeviceEventHandler _device_events[BLEDeviceLastEvent];
415419
};
416420

417421
#endif

0 commit comments

Comments
 (0)