Skip to content

Commit d58fdfc

Browse files
committed
Fix Jira and update code based on code review
1. Fix Jira 736 BLE in Central mode Peripheral object returns true even if Peripheral does not exist 2. Dismiss the order of add characteristic and service
1 parent 2db92b6 commit d58fdfc

File tree

7 files changed

+93
-23
lines changed

7 files changed

+93
-23
lines changed

libraries/BLE/src/BLECharacteristic.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class BLECharacteristic: public BLEAttributeWithValue
166166
* @note none
167167
*/
168168
virtual byte operator[] (int offset) const;
169+
BLECharacteristic& operator= (const BLECharacteristic& chrc);
169170

170171
/**
171172
* Set the current value of the Characteristic
@@ -467,6 +468,7 @@ class BLECharacteristic: public BLEAttributeWithValue
467468
protected:
468469
friend class BLEDevice;
469470
friend class BLEService;
471+
friend class BLEServiceImp;
470472
/**
471473
* @brief Create a characteristic with specified value size
472474
*
@@ -504,7 +506,7 @@ class BLECharacteristic: public BLEAttributeWithValue
504506

505507
// For GATT
506508
void setBLECharacteristicImp(BLECharacteristicImp *characteristicImp);
507-
509+
BLECharacteristicImp* fetchCharacteristicImp();
508510
private:
509511
void _setValue(const uint8_t value[], uint16_t length);
510512
BLECharacteristicImp *getImplementation() const;
@@ -515,6 +517,7 @@ class BLECharacteristic: public BLEAttributeWithValue
515517
// NULL - GATT server
516518
// None-NULL - GATT client
517519
BLECharacteristicImp *_internal; // The real implementation of characteristic.
520+
BLECharacteristicImp *_chrc_local_imp;
518521
protected:
519522
friend class BLECharacteristicImp;
520523
unsigned char _properties; // The characteristic property

libraries/BLE/src/BLEService.cpp

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@
2323

2424
#include "./internal/BLEUtils.h"
2525

26-
BLEService::BLEService():_bledevice(),_service_imp(NULL)
26+
BLEService::BLEService():_bledevice(),
27+
_service_imp(NULL),
28+
_service_local_imp(NULL)
2729
{
2830
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
2931
}
3032

31-
BLEService::BLEService(const char* uuid):_bledevice(),_service_imp(NULL)
33+
BLEService::BLEService(const char* uuid):_bledevice(),
34+
_service_imp(NULL),
35+
_service_local_imp(NULL)
3236
{
3337
bt_uuid_128_t uuid_tmp;
3438
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
@@ -38,42 +42,82 @@ BLEService::BLEService(const char* uuid):_bledevice(),_service_imp(NULL)
3842
_bledevice.setAddress(*BLEUtils::bleGetLoalAddress());
3943
}
4044

41-
BLEService::BLEService(const bt_uuid_t* uuid):_bledevice(),_service_imp(NULL)
45+
BLEService::BLEService(const bt_uuid_t* uuid):_bledevice(),
46+
_service_imp(NULL),
47+
_service_local_imp(NULL)
4248
{
4349
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
4450
BLEUtils::uuidBT2String(uuid, _uuid_cstr);
45-
_bledevice.setAddress(*BLEUtils::bleGetLoalAddress());
4651
}
4752

4853
BLEService::BLEService(BLEServiceImp* serviceImp, const BLEDevice* bledev):
49-
_bledevice(bledev),_service_imp(serviceImp)
54+
_bledevice(bledev),_service_imp(serviceImp),
55+
_service_local_imp(NULL)
5056
{
5157
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
5258
BLEUtils::uuidBT2String(serviceImp->bt_uuid(), _uuid_cstr);
5359
}
5460

5561
BLEService::~BLEService()
5662
{
63+
if (NULL != _service_local_imp)
64+
{
65+
delete _service_local_imp;
66+
_service_local_imp = NULL;
67+
}
5768
}
5869

5970
BLEService::operator bool() const
6071
{
6172
return (strlen(_uuid_cstr) > 3);
6273
}
6374

75+
BLEService& BLEService::operator= (const BLEService& service)
76+
{
77+
memcpy(_uuid_cstr, service._uuid_cstr, sizeof(_uuid_cstr));
78+
_bledevice.setAddress(*service._bledevice.bt_le_address());
79+
_service_imp = service._service_imp;
80+
_service_local_imp = NULL; // Not copy
81+
return *this;
82+
}
83+
6484
const char* BLEService::uuid() const
6585
{
6686
return _uuid_cstr;
6787
}
6888

69-
void BLEService::addCharacteristic(BLECharacteristic& characteristic)
89+
int BLEService::addCharacteristic(BLECharacteristic& characteristic)
7090
{
7191
BLEServiceImp* serviceImp = getServiceImp();
92+
int retVar = BLE_STATUS_ERROR;
7293

7394
if (NULL != serviceImp)
7495
{
75-
serviceImp->addCharacteristic(_bledevice, characteristic);
96+
retVar = serviceImp->addCharacteristic(_bledevice, characteristic);
97+
}
98+
else if (BLEUtils::isLocalBLE(_bledevice) == true)
99+
{
100+
// Only support the GATT server that create the service in local device.
101+
_service_local_imp = new BLEServiceImp(*this);
102+
if (NULL == _service_local_imp)
103+
{
104+
return BLE_STATUS_NO_MEMORY;
105+
}
106+
retVar = _service_local_imp->addCharacteristic(_bledevice, characteristic);
76107
}
108+
return retVar;
109+
}
110+
111+
BLEServiceImp* BLEService::getLocalServiceImp()
112+
{
113+
return _service_local_imp;
114+
}
115+
116+
BLEServiceImp* BLEService::fetchOutLocalServiceImp()
117+
{
118+
BLEServiceImp* temp = _service_local_imp;
119+
_service_local_imp = NULL;
120+
return temp;
77121
}
78122

79123
int BLEService::characteristicCount() const

libraries/BLE/src/BLEService.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class BLEService
3434
virtual ~BLEService();
3535

3636
virtual operator bool() const; // is the service valid
37+
BLEService& operator= (const BLEService& service);
3738

3839
const char* uuid() const;
3940

@@ -42,11 +43,11 @@ class BLEService
4243
*
4344
* @param characteristic The characteristic want to be added to service
4445
*
45-
* @return none
46+
* @return int 0 - Success. Others - Error codes
4647
*
4748
* @note none
4849
*/
49-
void addCharacteristic(BLECharacteristic& characteristic);
50+
int addCharacteristic(BLECharacteristic& characteristic);
5051

5152
/**
5253
* @brief Get the number of characteristics the service has
@@ -132,6 +133,9 @@ class BLEService
132133
BLEService(BLEServiceImp* serviceImp, const BLEDevice* bledev);
133134
BLEService(const bt_uuid_t* uuid);
134135
void setServiceImp(BLEServiceImp* serviceImp);
136+
137+
BLEServiceImp* getLocalServiceImp();
138+
BLEServiceImp* fetchOutLocalServiceImp();
135139
private:
136140
BLEServiceImp* getServiceImp();
137141
BLEServiceImp* getServiceImp() const;
@@ -140,6 +144,8 @@ class BLEService
140144
BLEDevice _bledevice;
141145
BLEServiceImp* _service_imp;
142146
char _uuid_cstr[37];
147+
148+
BLEServiceImp* _service_local_imp; // This not allow copy
143149
};
144150

145151
#endif

libraries/BLE/src/internal/BLECharacteristicImp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class BLECharacteristicImp: public BLEAttribute{
182182
protected:
183183
friend class BLEProfileManager;
184184
friend class BLEServiceImp;
185+
friend class BLECharacteristic;
185186
/**
186187
* Constructor for BLE Characteristic
187188
*

libraries/BLE/src/internal/BLEProfileManager.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,21 @@ BLEProfileManager::addService (BLEDevice &bledevice, BLEService& service)
102102
// The service alreay exist
103103
return BLE_STATUS_SUCCESS;
104104
}
105-
serviceImp = new BLEServiceImp(service);
105+
106106
if (NULL == serviceImp)
107107
{
108-
return BLE_STATUS_NO_MEMORY;
108+
serviceImp = service.fetchOutLocalServiceImp();
109109
}
110+
111+
if (NULL == serviceImp)
112+
{
113+
serviceImp = new BLEServiceImp(service);
114+
if (NULL == serviceImp)
115+
{
116+
return BLE_STATUS_NO_MEMORY;
117+
}
118+
}
119+
110120
BLEServiceNodePtr node = link_node_create(serviceImp);
111121
if (NULL == node)
112122
{

libraries/BLE/src/internal/BLEServiceImp.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ int BLEServiceImp::addCharacteristic(BLEDevice& bledevice, BLECharacteristic& ch
6464
pr_info(LOG_MODULE_BLE, "%s-%d: Already exist",__FUNCTION__, __LINE__);
6565
return BLE_STATUS_SUCCESS;
6666
}
67-
characteristicImp = new BLECharacteristicImp(characteristic, bledevice);
68-
pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
67+
68+
characteristicImp = characteristic.fetchCharacteristicImp();
6969
if (NULL == characteristicImp)
7070
{
71-
return BLE_STATUS_NO_MEMORY;
71+
characteristicImp = new BLECharacteristicImp(characteristic, bledevice);
72+
pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
73+
if (NULL == characteristicImp)
74+
{
75+
return BLE_STATUS_NO_MEMORY;
76+
}
7277
}
7378

7479
BLECharacteristicNodePtr node = link_node_create(characteristicImp);

libraries/BLE/src/internal/BLEUtils.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,22 @@ pr_info(LOG_MODULE_BLE,"%s",BLEUtils::macAddressBT2String(bd_addr2).c_str());
6363

6464
bool BLEUtils::macAddressValid(const bt_addr_le_t &bd_addr)
6565
{
66+
bool temp = false;
67+
#if 0
6668
static const bt_addr_le_t zero = {0,{0,0,0,0,0,0}};
67-
bool temp = (memcmp(bd_addr.val, zero.val, 6) != 0);//false;//
68-
#if 0
69+
temp = (memcmp(bd_addr.val, zero.val, 6) != 0);
70+
#else
6971
for (int i = 0; i < 6; i++)
7072
{
71-
if (bd_addr.val[i] != zero.val[i])
72-
{
73-
74-
pr_info(LOG_MODULE_BLE, "%s-idx %d-%.2x:%.2x", __FUNCTION__, i ,bd_addr.val[i], zero.val[i]);
75-
pr_info(LOG_MODULE_BLE,"%s",BLEUtils::macAddressBT2String(zero).c_str());
73+
if (bd_addr.val[i] != 0)
74+
{
75+
//pr_info(LOG_MODULE_BLE, "%s-idx %d-%.2x:%.2x", __FUNCTION__, i ,bd_addr.val[i], zero.val[i]);
76+
//pr_info(LOG_MODULE_BLE,"%s",BLEUtils::macAddressBT2String(zero).c_str());
7677
temp = true;
7778
break;
7879
}
7980
}
80-
#endif
81+
#endif
8182
return temp;
8283
}
8384

0 commit comments

Comments
 (0)