forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLECharacteristic.cpp
147 lines (111 loc) · 3.62 KB
/
BLECharacteristic.cpp
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
#include "Arduino.h"
#include "BLEDeviceLimits.h"
#include "BLECharacteristic.h"
BLECharacteristic::BLECharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize) :
BLELocalAttribute(uuid, BLETypeCharacteristic),
_valueSize(min(valueSize, BLE_ATTRIBUTE_MAX_VALUE_LENGTH)),
_value(NULL),
_valueLength(0),
_properties(properties),
_written(false),
_subscribed(false),
_listener(NULL)
{
memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers));
if (valueSize) {
this->_value = (unsigned char*)malloc(this->_valueSize);
}
}
BLECharacteristic::BLECharacteristic(const char* uuid, unsigned char properties, const char* value) :
BLELocalAttribute(uuid, BLETypeCharacteristic),
_valueSize(min(strlen(value), BLE_ATTRIBUTE_MAX_VALUE_LENGTH)),
_value(NULL),
_valueLength(0),
_properties(properties),
_written(false),
_subscribed(false),
_listener(NULL)
{
memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers));
this->_value = (unsigned char*)malloc(this->_valueSize);
this->setValue(value);
}
BLECharacteristic::~BLECharacteristic() {
if (this->_value) {
free(this->_value);
}
}
unsigned char BLECharacteristic::properties() const {
return this->_properties;
}
unsigned char BLECharacteristic::valueSize() const {
return this->_valueSize;
}
unsigned const char* BLECharacteristic::value() const {
return this->_value;
}
unsigned char BLECharacteristic::valueLength() const {
return this->_valueLength;
}
bool BLECharacteristic::fixedLength() const {
return false;
}
unsigned char BLECharacteristic::operator[] (int offset) const {
return this->_value[offset];
}
bool BLECharacteristic::setValue(const unsigned char value[], unsigned char length) {
bool success = true;
this->_valueLength = min(length, this->_valueSize);
memcpy(this->_value, value, this->_valueLength);
if (this->_listener) {
success = this->_listener->characteristicValueChanged(*this);
}
return success;
}
bool BLECharacteristic::setValue(const char* value) {
return this->setValue((const unsigned char *)value, strlen(value));
}
bool BLECharacteristic::broadcast() {
bool success = false;
if (this->_listener) {
success = this->_listener->broadcastCharacteristic(*this);
}
return success;
}
bool BLECharacteristic::written() {
bool written = this->_written;
this->_written = false;
return written;
}
void BLECharacteristic::setValue(BLECentral& central, const unsigned char value[], unsigned char length) {
this->setValue(value, length);
this->_written = true;
BLECharacteristicEventHandler eventHandler = this->_eventHandlers[BLEWritten];
if (eventHandler) {
eventHandler(central, *this);
}
}
bool BLECharacteristic::subscribed() {
return this->_subscribed;
}
void BLECharacteristic::setSubscribed(BLECentral& central, bool subscribed) {
this->_subscribed = subscribed;
BLECharacteristicEventHandler eventHandler = this->_eventHandlers[subscribed ? BLESubscribed : BLEUnsubscribed];
if (eventHandler) {
eventHandler(central, *this);
}
}
bool BLECharacteristic::canNotify() {
return (this->_listener) ? this->_listener->canNotifyCharacteristic(*this) : false;
}
bool BLECharacteristic::canIndicate() {
return (this->_listener) ? this->_listener->canIndicateCharacteristic(*this) : false;
}
void BLECharacteristic::setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler eventHandler) {
if (event < sizeof(this->_eventHandlers)) {
this->_eventHandlers[event] = eventHandler;
}
}
void BLECharacteristic::setValueChangeListener(BLECharacteristicValueChangeListener& listener) {
this->_listener = &listener;
}