forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 7
/
BLECharacteristic.cpp
131 lines (100 loc) · 3.37 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
#include "Arduino.h"
#include "BLECharacteristic.h"
BLECharacteristic::BLECharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize, bool fixedLength) :
BLEAttribute(uuid, BLETypeCharacteristic),
_properties(properties),
_valueSize(min(valueSize, BLE_ATTRIBUTE_MAX_VALUE_LENGTH)),
_valueLength(0),
_fixedLength(fixedLength),
_written(false),
_subscribed(false),
_listener(NULL)
{
memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers));
_value = (unsigned char*)malloc(this->_valueSize);
}
BLECharacteristic::BLECharacteristic(const char* uuid, unsigned char properties, const char* value, bool fixedLength) :
BLEAttribute(uuid, BLETypeCharacteristic),
_properties(properties),
_valueSize(min(strlen(value), BLE_ATTRIBUTE_MAX_VALUE_LENGTH)),
_valueLength(0),
_fixedLength(fixedLength),
_written(false),
_subscribed(false),
_listener(NULL)
{
memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers));
_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 this->_fixedLength;
}
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::written() {
bool written = this->_written;
if (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;
}