-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathBLECharacteristicImp.h
350 lines (294 loc) · 10.9 KB
/
BLECharacteristicImp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef _BLE_CHARACTERISTICIMP_H_INCLUDED
#define _BLE_CHARACTERISTICIMP_H_INCLUDED
//#include "BLECommon.h"
//#include "BLEDevice.h"
//#include "BLEDescriptor.h"
#include "CurieBLE.h"
#include "BLEDescriptorImp.h"
#include "BLEDevice.h"
#include "LinkList.h"
class BLEDescriptorImp;
/**
* BLE GATT Characteristic : public BLEAttribute
*/
class BLECharacteristicImp: public BLEAttribute{
public:
virtual ~BLECharacteristicImp();
/**
* @brief Add the characteristic's descriptor
*
* @param descriptor The descriptor for characteristic
*
* @return none
*
* @note none
*/
int addDescriptor(BLEDescriptor& descriptor);
int addDescriptor(const bt_uuid_t* uuid,
unsigned char property,
uint16_t handle);
void releaseDescriptors();
/**
* @brief Write the value of the characteristic
*
* @param value The value buffer that want to write to characteristic
*
* @param length The value buffer's length
*
* @param offset The offset in the characteristic's data
*
* @return bool true - Success, false - Failed
*
* @note none
*/
bool writeValue(const byte value[], int length);
bool writeValue(const byte value[], int length, int offset);
/**
* Set the current value of the Characteristic
*
* @param[in] value New value to set, as a byte array. Data is stored in internal copy.
* @param[in] length Length, in bytes, of valid data in the array to write.
* Must not exceed maxLength set for this characteristic.
*
* @return bool true set value success, false on error
*/
bool setValue(const unsigned char value[], unsigned short length);
/**
* Get the property mask of the Characteristic
*
* @return unsigned char property mask of the Characteristic
*/
unsigned char properties(void) const;
/**
* Get the (maximum) size of the Characteristic
*
* @return unsigned size of characateristic in bytes
*/
unsigned short valueSize(void) const;
/**
* Get data pointer to the value of the Characteristic
*
* @return const unsigned char* pointer to the value of the Characteristic
*/
const unsigned char* value(void) const;
/**
* Get the current length of the value of the Characteristic
*
* @return unsigned short size of characateristic value in bytes
*/
unsigned short valueLength() const;
unsigned char operator[] (int offset) const;
/**
* Has the value of the Characteristic been written by a central
*
* @return bool true is central has updated characteristic value, otherwise false
*/
bool written(void);
bool valueUpdated();
/**
* Is a central listening for notifications or indications of the Characteristic
*
* @return bool true is central is subscribed, otherwise false
*/
bool subscribed(void);
bool canNotify();
bool canIndicate();
bool subscribe(void);
bool unsubscribe(void);
/**
* Provide a function to be called when events related to this Characteristic are raised
*
* @param[in] event Event type to set event handler for
* @param[in] callback Pointer to callback function to invoke when the event occurs.
*/
void setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler callback);
void setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandlerOld callback);
/**
* @brief Schedule the read request to read the characteristic in peripheral
*
* @param[in] blocked Flag the call is blocked or un-blocked
*
* @return bool Indicate the success or error
*
* @note Only for GATT client
* Default it is block call as per Arduino request
*/
bool read(bool blocked = true);
/**
* @brief Schedule the write request to update the characteristic in peripheral
*
* @param[in] value New value to set, as a byte array. Data is stored in internal copy.
* @param[in] length Length, in bytes, of valid data in the array to write.
* Must not exceed maxLength set for this characteristic.
*
* @return bool true set value success, false on error
*
* @note none
*/
bool write(const unsigned char value[],
uint16_t length);
static void writeResponseReceived(struct bt_conn *conn,
uint8_t err,
const void *data);
int descriptorCount() const;
uint8_t discoverResponseProc(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
bt_gatt_discover_params_t *params);
bool discoverAttributes(BLEDevice* device);
BLEDescriptorImp* descrptor(const bt_uuid_t* uuid);
BLEDescriptorImp* descrptor(const char* uuid);
BLEDescriptorImp* descrptor(int index);
protected:
friend class BLEProfileManager;
friend class BLEServiceImp;
friend class BLECharacteristic;
BLEDescriptorImp* descriptor(uint16_t handle);
/**
* Constructor for BLE Characteristic
*
* @param[in] characteristic The characteristic
* @param[in] bledevice The device that has this characteristic
*/
BLECharacteristicImp(BLECharacteristic& characteristic, const BLEDevice& bledevice);
BLECharacteristicImp(const bt_uuid_t* uuid,
unsigned char properties,
uint16_t handle,
const BLEDevice& bledevice);
friend int profile_longflush_process(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
uint8_t flags);
friend ssize_t profile_longwrite_process(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf, uint16_t len,
uint16_t offset);
int updateProfile(bt_gatt_attr_t *attr_start, int& index);
int getAttributeCount();
bool longCharacteristic();
void setBuffer(const uint8_t value[],
uint16_t length,
uint16_t offset);
void discardBuffer();
void syncupBuffer2Value();
/**
* @brief Get the characteristic value handle
*
* @param none
*
* @return none
*
* @note Only for peripheral
*/
uint16_t valueHandle(void);
/**
* @brief Get characteristic configuration descriptor value handle
*
* @param none
*
* @return uint16_t The value handle
* 0 is invalid handle
*
* @note Only for peripheral
*/
uint16_t cccdHandle(void);
inline _bt_gatt_ccc_t* getCccCfg(void);
inline bt_gatt_chrc_t* getCharacteristicAttValue(void);
static bt_uuid_t* getCharacteristicAttributeUuid(void);
static bt_uuid_t* getClientCharacteristicConfigUuid(void);
/**
* @brief Get the characteristic permission
*
* @param none
*
* @return uint8_t The characteristic permission
*
* @note none
*/
uint8_t getPermission(void);
/**
* @brief For central to discover the peripherial profile
*
* @param[in] attr The discover response
*
* @param[in] params The discover parameter that need to fill
*
* @return none
*
* @note Only for central
*/
void discover(const bt_gatt_attr_t *attr,
bt_gatt_discover_params_t *params);
/**
* @brief For central to discover the peripherial profile
*
* @param[in] params The discover parameter that need to fill
*
* @return none
*
* @note Only for central
*/
void discover(bt_gatt_discover_params_t *params);
/**
* @brief Get the subscribe parameter
*
* @param none
*
* @return bt_gatt_subscribe_params_t * the subscribe parameter
*
* @note Only for central
*/
bt_gatt_subscribe_params_t* getSubscribeParams();
private:
void setCCCDHandle(uint16_t handle);
void setHandle(uint16_t handle);
void _setValue(const uint8_t value[], uint16_t length, uint16_t offset);
bool isClientCharacteristicConfigurationDescriptor(const bt_uuid_t* uuid);
private:
// Those 2 UUIDs are used for define the characteristic.
static bt_uuid_16_t _gatt_chrc_uuid; // Characteristic UUID
static bt_uuid_16_t _gatt_ccc_uuid; // CCCD UUID
unsigned short _value_size;
unsigned short _value_length;
unsigned char* _value;
unsigned char* _value_buffer;
bool _value_updated;
uint16_t _value_handle; // GATT client only
uint16_t _cccd_handle; // GATT client only
bt_gatt_discover_params_t _discover_params;// GATT client only
bt_gatt_ccc_cfg_t _ccc_cfg;
_bt_gatt_ccc_t _ccc_value;
bt_gatt_chrc_t _gatt_chrc;
bt_gatt_attr_t *_attr_chrc_value; // GATT server only
bt_gatt_attr_t *_attr_cccd; // GATT server only
// For GATT Client to subscribe the Notification/Indication
bt_gatt_subscribe_params_t _sub_params;
bool _subscribed;
volatile bool _reading;
volatile bool _gattc_read_result;
static volatile bool _gattc_writing;
static volatile bool _gattc_write_result;
bt_gatt_read_params_t _read_params; // GATT read parameter
typedef LinkNode<BLEDescriptorImp *> BLEDescriptorLinkNodeHeader;
typedef LinkNode<BLEDescriptorImp *>* BLEDescriptorNodePtr;
typedef LinkNode<BLEDescriptorImp *> BLEDescriptorNode;
BLECharacteristicEventHandler _event_handlers[BLECharacteristicEventLast];
BLECharacteristicEventHandlerOld _oldevent_handlers[BLECharacteristicEventLast];
BLEDescriptorLinkNodeHeader _descriptors_header;
BLEDevice _ble_device;
};
#endif // _BLE_CHARACTERISTIC_H_INCLUDED