Skip to content

Commit 59d03ae

Browse files
sgbihuSidLeung
authored andcommitted
Jira 859. Support BLE descriptor processing.
Feature added: For Central mode, added the support for processing BLE descriptor from a connected Peripheral. Enable the user sketch to access to descriptor value, length info. Code modifications: 1. peripheral_explorer.ino: - Example sketch with added descriptor processing. 2. BLECharacteristic.cpp: - Bug fixed in handling multi-descriptors. 3. BLEDescriptor.cpp: - For constructors, added initialization of descriptor storage and info. - For copy constructor and assignment, take into the account of descripter value and info. - Added API's for readning and returning descriptor info, eg. value, size. 4. BLEDescriptor.h: - Prototyping. 5. BLEDevice.h: - Added descriptor storage declaration. 6. BLECallbacks.cpp: - Added call back event for decriptor arrival. 7. BLECallbacks.h: - Prototyping. 8. BLECharacteristicImp.cpp: - The processing of the descriptor. 9. BLECharacteristicImp.h: - Prototyping. 10. BLEServiceImp.cpp: - Added descriptor info to constructor. - Made copy constructor to be aware of descriptor info. 11. BLEServiceImp.h: - Prototyping.
1 parent 3ec8350 commit 59d03ae

15 files changed

+480
-121
lines changed

libraries/CurieBLE/examples/central/peripheral_explorer/peripheral_explorer.ino

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,30 @@ void exploreCharacteristic(BLECharacteristic characteristic) {
133133
printData(characteristic.value(), characteristic.valueLength());
134134
}
135135
}
136-
137136
Serial.println();
138137

138+
// loop the descriptors of the characteristic and explore each
139+
for (int i = 0; i < characteristic.descriptorCount(); i++) {
140+
BLEDescriptor descriptor = characteristic.descriptor(i);
141+
142+
exploreDescriptor(descriptor);
143+
}
144+
}
145+
146+
void exploreDescriptor(BLEDescriptor descriptor) {
147+
// print the UUID of the descriptor
148+
Serial.print("\t\tDescriptor ");
149+
Serial.print(descriptor.uuid());
150+
151+
// read the descriptor value
152+
descriptor.read();
153+
delay(1000);
154+
155+
// print out the value of the descriptor
156+
Serial.print(", value 0x");
157+
printData(descriptor.value(), descriptor.valueLength());
158+
159+
Serial.println();
139160
}
140161

141162
void printData(const unsigned char data[], int length) {

libraries/CurieBLE/src/BLECharacteristic.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,15 +444,20 @@ int BLECharacteristic::addDescriptor(BLEDescriptor& descriptor)
444444
else if (BLEUtils::isLocalBLE(_bledev) == true)
445445
{
446446
// Only support the GATT server that create the service in local device.
447-
_chrc_local_imp = new BLECharacteristicImp(*this, _bledev);
447+
// Consider to add multi-descriptor
448448
if (NULL == _chrc_local_imp)
449449
{
450-
retVar = BLE_STATUS_NO_MEMORY;
450+
_chrc_local_imp = new BLECharacteristicImp(*this, _bledev);
451451
}
452-
else
452+
453+
if (NULL != _chrc_local_imp)
453454
{
454455
retVar = _chrc_local_imp->addDescriptor(descriptor);
455456
}
457+
else
458+
{
459+
retVar = BLE_STATUS_NO_MEMORY;
460+
}
456461
}
457462
return retVar;
458463
}

libraries/CurieBLE/src/BLEDescriptor.cpp

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,19 @@ BLEDescriptor::BLEDescriptor(BLEDescriptorImp* descriptorImp,
3535
const BLEDevice *bleDev):
3636
_bledev(bleDev),
3737
_value_size(0),
38-
_value(NULL)
38+
_value(NULL),
39+
_internal(descriptorImp)
3940
{
4041
_properties = descriptorImp->properties();
4142
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
4243
BLEUtils::uuidBT2String(descriptorImp->bt_uuid(), _uuid_cstr);
43-
44-
_value_size = descriptorImp->valueSize();
45-
_value = (unsigned char*)malloc(_value_size);
46-
if (NULL == _value)
47-
{
48-
memcpy(_value, descriptorImp->value(), _value_size);
49-
}
50-
else
51-
{
52-
errno = ENOMEM;
53-
}
5444
}
5545

5646
BLEDescriptor::BLEDescriptor(const char* uuid,
5747
const unsigned char value[],
5848
unsigned short valueLength):
59-
_bledev()
49+
_bledev(),
50+
_internal(NULL)
6051
{
6152
bt_uuid_128_t uuid_tmp;
6253
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
@@ -65,15 +56,16 @@ BLEDescriptor::BLEDescriptor(const char* uuid,
6556

6657
_bledev.setAddress(*BLEUtils::bleGetLoalAddress());
6758

68-
_value_size = valueLength > BLE_MAX_ATTR_LONGDATA_LEN ? BLE_MAX_ATTR_LONGDATA_LEN : valueLength;
59+
_value_size = (valueLength > BLE_MAX_ATTR_LONGDATA_LEN) ? BLE_MAX_ATTR_LONGDATA_LEN : valueLength;
6960
_value = (unsigned char*)malloc(_value_size);
70-
if (NULL == _value)
61+
if (NULL != _value)
7162
{
7263
memcpy(_value, value, _value_size);
7364
}
7465
else
7566
{
7667
errno = ENOMEM;
68+
_value_size = 0;
7769
}
7870
}
7971

@@ -82,22 +74,27 @@ BLEDescriptor::BLEDescriptor(const char* uuid,
8274
BLEDescriptor(uuid, (const unsigned char*)value, strlen(value))
8375
{}
8476

85-
BLEDescriptor::BLEDescriptor(const BLEDescriptor& rhs)
77+
BLEDescriptor::BLEDescriptor(const BLEDescriptor& rhs):
78+
_bledev(&rhs._bledev),
79+
_properties(rhs._properties),
80+
_value_size(0),
81+
_value(NULL),
82+
_internal(rhs._internal)
8683
{
87-
_value = (unsigned char*)malloc(rhs._value_size); // Sid. KW: allocate memory for _value, not local
88-
if (_value)
89-
{
90-
memcpy(_value, rhs._value, rhs._value_size);
91-
_value_size = rhs._value_size;
92-
}
93-
else
84+
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
85+
if (NULL == _internal && rhs._value_size > 0)
9486
{
95-
_value_size = 0;
96-
errno = ENOMEM;
87+
_value = (unsigned char*)malloc(rhs._value_size); // Sid. KW: allocate memory for _value, not local
88+
if (_value)
89+
{
90+
memcpy(_value, rhs._value, rhs._value_size);
91+
_value_size = rhs._value_size;
92+
}
93+
else
94+
{
95+
errno = ENOMEM;
96+
}
9797
}
98-
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
99-
_properties = rhs._properties;
100-
_bledev = BLEDevice(&rhs._bledev);
10198
}
10299

103100
BLEDescriptor& BLEDescriptor::operator= (const BLEDescriptor& rhs)
@@ -107,23 +104,27 @@ BLEDescriptor& BLEDescriptor::operator= (const BLEDescriptor& rhs)
107104
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
108105
_properties = rhs._properties;
109106
_bledev = BLEDevice(&rhs._bledev);
110-
if (_value_size < rhs._value_size)
107+
_internal = rhs._internal;
108+
if (NULL == _internal && rhs._value_size > 0)
111109
{
112-
_value_size = rhs._value_size;
110+
if (_value_size < rhs._value_size)
111+
{
112+
_value_size = rhs._value_size;
113+
114+
if (NULL != _value)
115+
free(_value);
116+
_value = (unsigned char*)malloc(_value_size);
117+
}
113118

114119
if (NULL != _value)
115-
free(_value);
116-
_value = (unsigned char*)malloc(_value_size);
117-
}
118-
119-
if (NULL != _value)
120-
{
121-
memcpy(_value, rhs._value, rhs._value_size);
122-
}
123-
else
124-
{
125-
_value_size = 0;
126-
errno = ENOMEM;
120+
{
121+
memcpy(_value, rhs._value, rhs._value_size);
122+
}
123+
else
124+
{
125+
_value_size = 0;
126+
errno = ENOMEM;
127+
}
127128
}
128129
}
129130
return *this;
@@ -145,12 +146,22 @@ const char* BLEDescriptor::uuid() const
145146

146147
const byte* BLEDescriptor::value() const
147148
{
148-
return _value;
149+
const byte* ret = _value;
150+
if (NULL != _internal)
151+
{
152+
ret = _internal->value();
153+
}
154+
return ret;
149155
}
150156

151157
int BLEDescriptor::valueLength() const
152158
{
153-
return _value_size;
159+
int ret = _value_size;
160+
if (NULL != _internal)
161+
{
162+
ret = _internal->valueLength();
163+
}
164+
return ret;
154165
}
155166

156167
BLEDescriptor::operator bool() const
@@ -166,6 +177,22 @@ unsigned char BLEDescriptor::properties() const
166177

167178
int BLEDescriptor::valueSize() const
168179
{
169-
return _value_size;
180+
int ret = _value_size;
181+
if (NULL != _internal)
182+
{
183+
ret = _internal->valueSize();
184+
}
185+
return ret;
186+
}
187+
188+
bool BLEDescriptor::read()
189+
{
190+
bool retVar = false;
191+
192+
if (NULL != _internal)
193+
{
194+
retVar = _internal->read();
195+
}
196+
return retVar;
170197
}
171198

libraries/CurieBLE/src/BLEDescriptor.h

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,73 +37,92 @@ class BLEDescriptor
3737

3838
virtual ~BLEDescriptor();
3939

40+
/**
41+
* @brief Get the descriptor's UUID string
42+
*
43+
* @param none
44+
*
45+
* @return const char* The UUID string
46+
*
47+
* @note none
48+
*/
4049
const char* uuid() const;
4150

42-
virtual const byte* value() const; // returns the value buffer
43-
virtual int valueLength() const; // returns the current length of the value
44-
45-
virtual operator bool() const; // is the descriptor valid (discovered from peripheral)
46-
47-
unsigned char properties() const;
48-
int valueSize() const;
49-
private:
50-
char _uuid_cstr[37]; // The characteristic UUID
51-
BLEDevice _bledev;
52-
53-
unsigned char _properties; // The characteristic property
54-
55-
unsigned short _value_size; // The value size
56-
unsigned char* _value; // The value. Will delete after create the _internal
57-
58-
59-
// The API reserved for feature release
60-
// move here for temp
6151

6252
/**
63-
* @brief Write the value of the descriptor
53+
* @brief Get the value of descriptor
6454
*
65-
* @param value The value buffer that want to write to descriptor
55+
* @param none
6656
*
67-
* @param length The value buffer's length
57+
* @return const byte* The value buffer
6858
*
69-
* @return bool true - Success, false - Failed
59+
* @note none
60+
*/
61+
virtual const byte* value() const;
62+
63+
/**
64+
* @brief Get the current length of the value
65+
*
66+
* @param none
67+
*
68+
* @return int The current length of the value string
7069
*
7170
* @note none
7271
*/
73-
//virtual bool writeValue(const byte value[], int length);
72+
virtual int valueLength() const;
7473

7574
/**
76-
* @brief Write the value of the descriptor
75+
* @brief Is the descriptor valid
76+
*
77+
* @param none
7778
*
78-
* @param value The value buffer that want to write to descriptor
79+
* @return bool true/false
7980
*
80-
* @param length The value buffer's length
81+
* @note none
82+
*/
83+
virtual operator bool() const;
84+
85+
/**
86+
* @brief Read the descriptor value
8187
*
82-
* @param offset The offset in the descriptor's data
88+
* @param none
8389
*
8490
* @return bool true - Success, false - Failed
8591
*
92+
* @note Only for GATT client. Schedule read request to the GATT server
93+
*/
94+
bool read();
95+
96+
/**
97+
* @brief Get the property mask of the descriptor
98+
*
99+
* @param none
100+
*
101+
* @return unsigned char The property mask of the descriptor
102+
*
86103
* @note none
87104
*/
88-
//bool writeValue(const byte value[], int length, int offset);
105+
unsigned char properties() const;
89106

90107
/**
91-
* @brief Write the value of the descriptor
108+
* @brief Get the maximum size of the value
92109
*
93-
* @param value The value string that want to write to descriptor
110+
* @param none
94111
*
95-
* @return bool true - Success, false - Failed
112+
* @return int The maximum size of the value
96113
*
97114
* @note none
98115
*/
99-
//bool writeValue(const char* value);
100-
//virtual byte operator[] (int offset) const; // returns a byte of the value at the specified offset
101-
102-
// GATT client Write the value of the descriptor
103-
//virtual bool write(const byte value[], int length);
104-
//bool write(const byte value[], int length, int offset);
105-
//bool write(const char* value);
106-
//bool read();
116+
int valueSize() const;
117+
private:
118+
char _uuid_cstr[37]; // The characteristic UUID
119+
BLEDevice _bledev;
120+
121+
unsigned char _properties; // The characteristic property
122+
123+
unsigned short _value_size; // The value size
124+
unsigned char* _value; // The value. Will delete after create the _internal
125+
BLEDescriptorImp *_internal; // The real implementation of Descriptor
107126
};
108127

109128
#endif

libraries/CurieBLE/src/BLEDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ class BLEDevice
644644
void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler); // set an event handler (callback)
645645

646646
protected:
647+
friend class BLEDescriptorImp;
647648
friend class BLECharacteristicImp;
648649
friend class BLEServiceImp;
649650
friend class BLEDeviceManager;
@@ -659,6 +660,11 @@ class BLEDevice
659660
bt_gatt_read_params_t *params,
660661
const void *data,
661662
uint16_t length);
663+
friend uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
664+
int err,
665+
bt_gatt_read_params_t *params,
666+
const void *data,
667+
uint16_t length);
662668
const bt_addr_le_t* bt_le_address() const;
663669
const bt_le_conn_param* bt_conn_param() const;
664670
void setAddress(const bt_addr_le_t& addr);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy