-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathBLERemoteDescriptor.cpp
134 lines (104 loc) · 2.75 KB
/
BLERemoteDescriptor.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
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2019 Arduino SA. 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
*/
#include "utility/ATT.h"
#include "BLERemoteDescriptor.h"
BLERemoteDescriptor::BLERemoteDescriptor(const uint8_t uuid[], uint8_t uuidLen, uint16_t connectionHandle, uint16_t handle) :
BLERemoteAttribute(uuid, uuidLen),
_connectionHandle(connectionHandle),
_handle(handle),
_value(NULL),
_valueLength(0)
{
}
BLERemoteDescriptor::~BLERemoteDescriptor()
{
if (_value) {
free(_value);
_value = NULL;
}
}
const uint8_t* BLERemoteDescriptor::value() const
{
return _value;
}
int BLERemoteDescriptor::valueLength() const
{
return _valueLength;
}
uint8_t BLERemoteDescriptor::operator[] (int offset) const
{
if (_value) {
return _value[offset];
}
return 0;
}
int BLERemoteDescriptor::writeValue(const uint8_t value[], int length)
{
if (!ATT.connected(_connectionHandle)) {
return 0;
}
uint16_t maxLength = ATT.mtu(_connectionHandle) - 3;
if (length > (int)maxLength) {
// cap to MTU max length
length = maxLength;
}
_value = (uint8_t*)realloc(_value, length);
if (_value == NULL) {
// realloc failed
return 0;
}
uint8_t resp[4];
int respLength = ATT.writeReq(_connectionHandle, _handle, value, length, resp);
if (!respLength) {
return 0;
}
if (resp[0] == 0x01) {
// error
return 0;
}
memcpy(_value, value, length);
_valueLength = length;
return 1;
}
bool BLERemoteDescriptor::read()
{
if (!ATT.connected(_connectionHandle)) {
return false;
}
uint8_t resp[256];
int respLength = ATT.readReq(_connectionHandle, _handle, resp);
if (!respLength) {
_valueLength = 0;
return false;
}
if (resp[0] == 0x01) {
// error
_valueLength = 0;
return false;
}
_valueLength = respLength - 1;
_value = (uint8_t*)realloc(_value, _valueLength);
if (_value == NULL) {
_valueLength = 0;
return false;
}
memcpy(_value, &resp[1], _valueLength);
return true;
}
uint16_t BLERemoteDescriptor::handle() const
{
return _handle;
}