forked from arduino/ArduinoCore-arc32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLECharacteristic.h
184 lines (151 loc) · 5.57 KB
/
BLECharacteristic.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
/*
* Copyright (c) 2015 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_CHARACTERISTIC_H_INCLUDED
#define _BLE_CHARACTERISTIC_H_INCLUDED
#include "BLEAttribute.h"
#include "BLECentral.h"
#include "BLEDescriptor.h"
/**
* BLE Characteristic Events
*/
enum BLECharacteristicEvent {
BLEWritten = 0,
BLESubscribed = 1,
BLEUnsubscribed = 2,
BLECharacteristicEventLast = 3
};
/* Forward declaration needed for callback function prototype below */
class BLECharacteristic;
class BLEPeripheral;
/** Function prototype for BLE Characteristic event callback */
typedef void (*BLECharacteristicEventHandler)(BLECentral ¢ral, BLECharacteristic &characteristic);
/**
* BLE Characteristic Property types
*/
enum BLEProperty {
// broadcast (0x01) not supported
BLERead = 0x02,
BLEWriteWithoutResponse = 0x04,
BLEWrite = 0x08,
BLENotify = 0x10,
BLEIndicate = 0x20
};
/**
* BLE GATT Characteristic
*/
class BLECharacteristic : public BLEAttribute {
public:
/**
* Constructor for BLE Characteristic
*
* @param uuid 16-bit or 128-bit UUID (in string form) defined by BLE standard
* @param properties Characteristic property mask
* @param maxLength Maximum data length required for characteristic value (<= BLE_MAX_ATTR_DATA_LEN)
*/
BLECharacteristic(const char* uuid,
const unsigned char properties,
const unsigned short maxLength);
/**
* Constructor for BLE Characteristic
*
* @param uuid 16-bit or 128-bit UUID (in string form) defined by BLE standard
* @param properties Characteristic property mask
* @param value String value for characteristic (string length (<= BLE_MAX_ATTR_DATA_LEN))
*/
BLECharacteristic(const char* uuid,
const unsigned char properties,
const char* value);
virtual ~BLECharacteristic();
/**
* Set the current value of the Characteristic
*
* @param value New value to set, as a byte array. Data is stored in internal copy.
* @param 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);
/**
* Is a central listening for notifications or indications of the Characteristic
*
* @return bool true is central is subscribed, otherwise false
*/
bool subscribed(void);
/**
* Provide a function to be called when events related to this Characteristic are raised
*
* @param event Event type to set event handler for
* @param callback Pointer to callback function to invoke when the event occurs.
*/
void setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler callback);
protected:
bool add(uint16_t serviceHandle);
uint16_t valueHandle(void);
uint16_t cccdHandle(void);
void setValue(BLECentral& central, const uint8_t value[], uint16_t length);
void setCccdValue(BLECentral& central, uint16_t value);
void setUserDescription(BLEDescriptor *descriptor);
void setPresentationFormat(BLEDescriptor *descriptor);
friend class BLEPeripheral;
private:
void _setValue(const uint8_t value[], uint16_t length);
private:
unsigned char _properties;
unsigned short _value_size;
unsigned short _value_length;
unsigned char* _value;
bool _written;
uint16_t _cccd_value;
uint16_t _value_handle;
uint16_t _cccd_handle;
BLEDescriptor* _user_description;
BLEDescriptor* _presentation_format;
BLECharacteristicEventHandler _event_handlers[BLECharacteristicEventLast];
};
#endif // _BLE_CHARACTERISTIC_H_INCLUDED