forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBLEDescriptor.cpp
48 lines (38 loc) · 1.18 KB
/
BLEDescriptor.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
#include "Arduino.h"
#include "BLEDescriptor.h"
BLEDescriptor::BLEDescriptor(const char* uuid, unsigned char valueSize) :
BLEAttribute(uuid, BLETypeDescriptor),
_valueSize(min(valueSize, BLE_ATTRIBUTE_MAX_VALUE_LENGTH)),
_valueLength(0)
{
_value = (unsigned char*)malloc(this->_valueSize);
}
BLEDescriptor::BLEDescriptor(const char* uuid, const char* value) :
BLEAttribute(uuid, BLETypeDescriptor),
_valueSize(min(strlen(value), BLE_ATTRIBUTE_MAX_VALUE_LENGTH)),
_valueLength(0)
{
_value = (unsigned char*)malloc(this->_valueSize);
this->setValue(value);
}
BLEDescriptor::~BLEDescriptor() {
if (this->_value) {
free(this->_value);
}
}
unsigned char BLEDescriptor::valueSize() const {
return this->_valueSize;
}
const unsigned char* BLEDescriptor::value() const {
return this->_value;
}
unsigned char BLEDescriptor::valueLength() const {
return this->_valueLength;
}
void BLEDescriptor::setValue(const unsigned char value[], unsigned char length) {
this->_valueLength = min(length, this->_valueSize);
memcpy(this->_value, value, this->_valueLength);
}
void BLEDescriptor::setValue(const char* value) {
this->setValue((const unsigned char *)value, strlen(value));
}