Skip to content

Commit c1882ee

Browse files
committed
Add special handling for 0x2902 and 0x2904 descriptors
1 parent 8ee6a02 commit c1882ee

File tree

5 files changed

+70
-15
lines changed

5 files changed

+70
-15
lines changed

libraries/CurieBle/src/BleCharacteristic.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ BleCharacteristic::BleCharacteristic(const char* uuid,
3232
_written(false),
3333
_cccd_value(0),
3434
_value_handle(0),
35-
_cccd_handle(0)
35+
_cccd_handle(0),
36+
_user_description(NULL),
37+
_presentation_format(NULL)
3638
{
3739
_max_len = maxLength > BLE_MAX_ATTR_DATA_LEN ? BLE_MAX_ATTR_DATA_LEN : maxLength;
3840

@@ -148,6 +150,8 @@ BleCharacteristic::add(uint16_t serviceHandle)
148150

149151
struct ble_gatts_characteristic char_data;
150152
struct ble_gatts_char_handles handles;
153+
struct ble_gatt_char_user_desc user_desc;
154+
struct ble_gatt_pf_desc pf_desc;
151155

152156
memset(&char_data, 0, sizeof(char_data));
153157

@@ -170,6 +174,19 @@ BleCharacteristic::add(uint16_t serviceHandle)
170174
char_data.max_len = _max_len;
171175
char_data.p_value = _data;
172176

177+
if (_user_description) {
178+
user_desc.buffer = (uint8_t*)_user_description->value();
179+
user_desc.len = _user_description->valueLength();
180+
181+
char_data.p_user_desc = &user_desc;
182+
}
183+
184+
if (_presentation_format) {
185+
memcpy(&pf_desc, _presentation_format->value(), sizeof(pf_desc));
186+
187+
char_data.p_char_pf_desc = &pf_desc;
188+
}
189+
173190
BleStatus status = ble_client_gatts_add_characteristic(serviceHandle, &char_data, &handles);
174191
if (BLE_STATUS_SUCCESS == status) {
175192
_value_handle = handles.value_handle;
@@ -209,6 +226,17 @@ BleCharacteristic::setCccdValue(BleCentral& central, uint16_t value)
209226
}
210227
}
211228

229+
void
230+
BleCharacteristic::setUserDescription(BleDescriptor *descriptor)
231+
{
232+
_user_description = descriptor;
233+
}
234+
235+
void BleCharacteristic::setPresentationFormat(BleDescriptor *descriptor)
236+
{
237+
_presentation_format = descriptor;
238+
}
239+
212240
void
213241
BleCharacteristic::_setValue(const uint8_t value[], uint16_t length)
214242
{

libraries/CurieBle/src/BleCharacteristic.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "BleAttribute.h"
2424
#include "BleCentral.h"
25+
#include "BleDescriptor.h"
2526

2627
/**
2728
* BLE Characteristic Events
@@ -153,6 +154,9 @@ class BleCharacteristic : public BleAttribute {
153154
void setValue(BleCentral& central, const uint8_t value[], uint16_t length);
154155
void setCccdValue(BleCentral& central, uint16_t value);
155156

157+
void setUserDescription(BleDescriptor *descriptor);
158+
void setPresentationFormat(BleDescriptor *descriptor);
159+
156160
friend class BlePeripheral;
157161

158162
private:
@@ -167,6 +171,9 @@ class BleCharacteristic : public BleAttribute {
167171
uint16_t _cccd_value;
168172
uint16_t _value_handle;
169173
uint16_t _cccd_handle;
174+
175+
BleDescriptor* _user_description;
176+
BleDescriptor* _presentation_format;
170177

171178
BleCharacteristicEventHandler _event_handlers[BleCharacteristicEventLast];
172179
};

libraries/CurieBle/src/BleDescriptor.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,12 @@ BleDescriptor::add(uint16_t serviceHandle)
6666

6767
desc.p_uuid = &uuid;
6868

69+
desc.p_value = _data;
70+
desc.length = _data_len;
71+
6972
// this class only supports read-only descriptors
7073
desc.perms.rd = GAP_SEC_MODE_1 | GAP_SEC_LEVEL_1;
7174
desc.perms.wr = GAP_SEC_NO_PERMISSION;
7275

73-
BleStatus status = ble_client_gatts_add_descriptor(serviceHandle, &desc, &handle);
74-
75-
if (BLE_STATUS_SUCCESS == status) {
76-
setHandle(handle);
77-
status = ble_client_gatts_set_attribute_value(handle, _data_len, _data, 0);
78-
}
79-
80-
return status;
76+
return ble_client_gatts_add_descriptor(serviceHandle, &desc, &handle);
8177
}

libraries/CurieBle/src/BlePeripheral.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ blePeripheralGattsEventHandler(ble_client_gatts_event_t event, struct ble_gatts_
4444
}
4545

4646
BlePeripheral::BlePeripheral(void) :
47+
_state(BLE_PERIPH_STATE_NOT_READY),
48+
_appearance(0),
4749
_central(this),
4850
_attributes(NULL),
49-
_num_attributes(0)
51+
_num_attributes(0),
52+
_last_added_characteritic(NULL)
5053
{
51-
_appearance = 0;
52-
_state = BLE_PERIPH_STATE_NOT_READY;
53-
5454
memset(_event_handlers, 0x00, sizeof(_event_handlers));
5555

5656
ble_client_get_factory_config(&_local_bda, _local_name);
@@ -89,10 +89,15 @@ BleStatus BlePeripheral::begin()
8989

9090
characteristic->add(lastServiceHandle);
9191
} else if (BleTypeDescriptor == type) {
92-
// TODO: handle UUID 0x2901 and 0x2904 in a special way
93-
9492
BleDescriptor *descriptor = (BleDescriptor*)attribute;
9593

94+
if (strcmp(descriptor->uuid(), "2901") == 0 ||
95+
strcmp(descriptor->uuid(), "2902") == 0 ||
96+
strcmp(descriptor->uuid(), "2903") == 0 ||
97+
strcmp(descriptor->uuid(), "2904") == 0) {
98+
continue; // skip
99+
}
100+
96101
status = descriptor->add(lastServiceHandle);
97102
}
98103

@@ -189,6 +194,22 @@ BlePeripheral::addAttribute(BleAttribute& attribute)
189194
_attributes[_num_attributes] = &attribute;
190195
_num_attributes++;
191196

197+
BleAttributeType type = attribute.type();
198+
199+
if (BleTypeCharacteristic == type) {
200+
_last_added_characteritic = (BleCharacteristic*)&attribute;
201+
} else if (BleTypeDescriptor == type) {
202+
if (_last_added_characteritic) {
203+
BleDescriptor* descriptor = (BleDescriptor*)&attribute;
204+
205+
if (strcmp("2901", descriptor->uuid()) == 0) {
206+
_last_added_characteritic->setUserDescription(descriptor);
207+
} else if (strcmp("2904", descriptor->uuid()) == 0) {
208+
_last_added_characteritic->setPresentationFormat(descriptor);
209+
}
210+
}
211+
}
212+
192213
return BLE_STATUS_SUCCESS;
193214
}
194215

libraries/CurieBle/src/BlePeripheral.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "BleAttribute.h"
2626
#include "BleCentral.h"
27+
#include "BleCharacteristic.h"
2728
#include "BleCommon.h"
2829

2930
/**
@@ -185,6 +186,8 @@ class BlePeripheral {
185186

186187
BleAttribute** _attributes;
187188
uint16_t _num_attributes;
189+
190+
BleCharacteristic* _last_added_characteritic;
188191
};
189192

190193
#endif // _BLE_DEVICE_H_INCLUDED

0 commit comments

Comments
 (0)