User | Revision | Line number | New contents of line |
switches |
0:0e018d759a2a
|
1
|
/* mbed Microcontroller Library
|
switches |
0:0e018d759a2a
|
2
|
* Copyright (c) 2006-2013 ARM Limited
|
switches |
0:0e018d759a2a
|
3
|
*
|
switches |
0:0e018d759a2a
|
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
switches |
0:0e018d759a2a
|
5
|
* you may not use this file except in compliance with the License.
|
switches |
0:0e018d759a2a
|
6
|
* You may obtain a copy of the License at
|
switches |
0:0e018d759a2a
|
7
|
*
|
switches |
0:0e018d759a2a
|
8
|
* http://www.apache.org/licenses/LICENSE-2.0
|
switches |
0:0e018d759a2a
|
9
|
*
|
switches |
0:0e018d759a2a
|
10
|
* Unless required by applicable law or agreed to in writing, software
|
switches |
0:0e018d759a2a
|
11
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
switches |
0:0e018d759a2a
|
12
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
switches |
0:0e018d759a2a
|
13
|
* See the License for the specific language governing permissions and
|
switches |
0:0e018d759a2a
|
14
|
* limitations under the License.
|
switches |
0:0e018d759a2a
|
15
|
*/
|
switches |
0:0e018d759a2a
|
16
|
|
switches |
0:0e018d759a2a
|
17
|
#include "ble/DiscoveredCharacteristic.h"
|
switches |
0:0e018d759a2a
|
18
|
#include "ble/GattClient.h"
|
switches |
0:0e018d759a2a
|
19
|
|
switches |
0:0e018d759a2a
|
20
|
ble_error_t
|
switches |
0:0e018d759a2a
|
21
|
DiscoveredCharacteristic::read(uint16_t offset) const
|
switches |
0:0e018d759a2a
|
22
|
{
|
switches |
0:0e018d759a2a
|
23
|
if (!props.read()) {
|
switches |
0:0e018d759a2a
|
24
|
return BLE_ERROR_OPERATION_NOT_PERMITTED;
|
switches |
0:0e018d759a2a
|
25
|
}
|
switches |
0:0e018d759a2a
|
26
|
|
switches |
0:0e018d759a2a
|
27
|
if (!gattc) {
|
switches |
0:0e018d759a2a
|
28
|
return BLE_ERROR_INVALID_STATE;
|
switches |
0:0e018d759a2a
|
29
|
}
|
switches |
0:0e018d759a2a
|
30
|
|
switches |
0:0e018d759a2a
|
31
|
return gattc->read(connHandle, valueHandle, offset);
|
switches |
0:0e018d759a2a
|
32
|
}
|
switches |
0:0e018d759a2a
|
33
|
|
switches |
0:0e018d759a2a
|
34
|
struct OneShotReadCallback {
|
switches |
0:0e018d759a2a
|
35
|
static void launch(GattClient* client, Gap::Handle_t connHandle,
|
switches |
0:0e018d759a2a
|
36
|
GattAttribute::Handle_t handle, const GattClient::ReadCallback_t& cb) {
|
switches |
0:0e018d759a2a
|
37
|
OneShotReadCallback* oneShot = new OneShotReadCallback(client, connHandle, handle, cb);
|
switches |
0:0e018d759a2a
|
38
|
oneShot->attach();
|
switches |
0:0e018d759a2a
|
39
|
// delete will be made when this callback is called
|
switches |
0:0e018d759a2a
|
40
|
}
|
switches |
0:0e018d759a2a
|
41
|
|
switches |
0:0e018d759a2a
|
42
|
private:
|
switches |
0:0e018d759a2a
|
43
|
OneShotReadCallback(GattClient* client, Gap::Handle_t connHandle,
|
switches |
0:0e018d759a2a
|
44
|
GattAttribute::Handle_t handle, const GattClient::ReadCallback_t& cb) :
|
switches |
0:0e018d759a2a
|
45
|
_client(client),
|
switches |
0:0e018d759a2a
|
46
|
_connHandle(connHandle),
|
switches |
0:0e018d759a2a
|
47
|
_handle(handle),
|
switches |
0:0e018d759a2a
|
48
|
_callback(cb) { }
|
switches |
0:0e018d759a2a
|
49
|
|
switches |
0:0e018d759a2a
|
50
|
void attach() {
|
switches |
0:0e018d759a2a
|
51
|
_client->onDataRead(makeFunctionPointer(this, &OneShotReadCallback::call));
|
switches |
0:0e018d759a2a
|
52
|
}
|
switches |
0:0e018d759a2a
|
53
|
|
switches |
0:0e018d759a2a
|
54
|
void call(const GattReadCallbackParams* params) {
|
switches |
0:0e018d759a2a
|
55
|
// verifiy that it is the right characteristic on the right connection
|
switches |
0:0e018d759a2a
|
56
|
if (params->connHandle == _connHandle && params->handle == _handle) {
|
switches |
0:0e018d759a2a
|
57
|
_callback(params);
|
switches |
0:0e018d759a2a
|
58
|
_client->onDataRead().detach(makeFunctionPointer(this, &OneShotReadCallback::call));
|
switches |
0:0e018d759a2a
|
59
|
delete this;
|
switches |
0:0e018d759a2a
|
60
|
}
|
switches |
0:0e018d759a2a
|
61
|
}
|
switches |
0:0e018d759a2a
|
62
|
|
switches |
0:0e018d759a2a
|
63
|
GattClient* _client;
|
switches |
0:0e018d759a2a
|
64
|
Gap::Handle_t _connHandle;
|
switches |
0:0e018d759a2a
|
65
|
GattAttribute::Handle_t _handle;
|
switches |
0:0e018d759a2a
|
66
|
GattClient::ReadCallback_t _callback;
|
switches |
0:0e018d759a2a
|
67
|
};
|
switches |
0:0e018d759a2a
|
68
|
|
switches |
0:0e018d759a2a
|
69
|
ble_error_t DiscoveredCharacteristic::read(uint16_t offset, const GattClient::ReadCallback_t& onRead) const {
|
switches |
0:0e018d759a2a
|
70
|
ble_error_t error = read(offset);
|
switches |
0:0e018d759a2a
|
71
|
if (error) {
|
switches |
0:0e018d759a2a
|
72
|
return error;
|
switches |
0:0e018d759a2a
|
73
|
}
|
switches |
0:0e018d759a2a
|
74
|
|
switches |
0:0e018d759a2a
|
75
|
OneShotReadCallback::launch(gattc, connHandle, valueHandle, onRead);
|
switches |
0:0e018d759a2a
|
76
|
|
switches |
0:0e018d759a2a
|
77
|
return error;
|
switches |
0:0e018d759a2a
|
78
|
}
|
switches |
0:0e018d759a2a
|
79
|
|
switches |
0:0e018d759a2a
|
80
|
ble_error_t
|
switches |
0:0e018d759a2a
|
81
|
DiscoveredCharacteristic::write(uint16_t length, const uint8_t *value) const
|
switches |
0:0e018d759a2a
|
82
|
{
|
switches |
0:0e018d759a2a
|
83
|
if (!props.write()) {
|
switches |
0:0e018d759a2a
|
84
|
return BLE_ERROR_OPERATION_NOT_PERMITTED;
|
switches |
0:0e018d759a2a
|
85
|
}
|
switches |
0:0e018d759a2a
|
86
|
|
switches |
0:0e018d759a2a
|
87
|
if (!gattc) {
|
switches |
0:0e018d759a2a
|
88
|
return BLE_ERROR_INVALID_STATE;
|
switches |
0:0e018d759a2a
|
89
|
}
|
switches |
0:0e018d759a2a
|
90
|
|
switches |
0:0e018d759a2a
|
91
|
return gattc->write(GattClient::GATT_OP_WRITE_REQ, connHandle, valueHandle, length, value);
|
switches |
0:0e018d759a2a
|
92
|
}
|
switches |
0:0e018d759a2a
|
93
|
|
switches |
0:0e018d759a2a
|
94
|
ble_error_t
|
switches |
0:0e018d759a2a
|
95
|
DiscoveredCharacteristic::writeWoResponse(uint16_t length, const uint8_t *value) const
|
switches |
0:0e018d759a2a
|
96
|
{
|
switches |
0:0e018d759a2a
|
97
|
if (!props.writeWoResp()) {
|
switches |
0:0e018d759a2a
|
98
|
return BLE_ERROR_OPERATION_NOT_PERMITTED;
|
switches |
0:0e018d759a2a
|
99
|
}
|
switches |
0:0e018d759a2a
|
100
|
|
switches |
0:0e018d759a2a
|
101
|
if (!gattc) {
|
switches |
0:0e018d759a2a
|
102
|
return BLE_ERROR_INVALID_STATE;
|
switches |
0:0e018d759a2a
|
103
|
}
|
switches |
0:0e018d759a2a
|
104
|
|
switches |
0:0e018d759a2a
|
105
|
return gattc->write(GattClient::GATT_OP_WRITE_CMD, connHandle, valueHandle, length, value);
|
switches |
0:0e018d759a2a
|
106
|
}
|
switches |
0:0e018d759a2a
|
107
|
|
switches |
0:0e018d759a2a
|
108
|
struct OneShotWriteCallback {
|
switches |
0:0e018d759a2a
|
109
|
static void launch(GattClient* client, Gap::Handle_t connHandle,
|
switches |
0:0e018d759a2a
|
110
|
GattAttribute::Handle_t handle, const GattClient::WriteCallback_t& cb) {
|
switches |
0:0e018d759a2a
|
111
|
OneShotWriteCallback* oneShot = new OneShotWriteCallback(client, connHandle, handle, cb);
|
switches |
0:0e018d759a2a
|
112
|
oneShot->attach();
|
switches |
0:0e018d759a2a
|
113
|
// delete will be made when this callback is called
|
switches |
0:0e018d759a2a
|
114
|
}
|
switches |
0:0e018d759a2a
|
115
|
|
switches |
0:0e018d759a2a
|
116
|
private:
|
switches |
0:0e018d759a2a
|
117
|
OneShotWriteCallback(GattClient* client, Gap::Handle_t connHandle,
|
switches |
0:0e018d759a2a
|
118
|
GattAttribute::Handle_t handle, const GattClient::WriteCallback_t& cb) :
|
switches |
0:0e018d759a2a
|
119
|
_client(client),
|
switches |
0:0e018d759a2a
|
120
|
_connHandle(connHandle),
|
switches |
0:0e018d759a2a
|
121
|
_handle(handle),
|
switches |
0:0e018d759a2a
|
122
|
_callback(cb) { }
|
switches |
0:0e018d759a2a
|
123
|
|
switches |
0:0e018d759a2a
|
124
|
void attach() {
|
switches |
0:0e018d759a2a
|
125
|
_client->onDataWritten(makeFunctionPointer(this, &OneShotWriteCallback::call));
|
switches |
0:0e018d759a2a
|
126
|
}
|
switches |
0:0e018d759a2a
|
127
|
|
switches |
0:0e018d759a2a
|
128
|
void call(const GattWriteCallbackParams* params) {
|
switches |
0:0e018d759a2a
|
129
|
// verifiy that it is the right characteristic on the right connection
|
switches |
0:0e018d759a2a
|
130
|
if (params->connHandle == _connHandle && params->handle == _handle) {
|
switches |
0:0e018d759a2a
|
131
|
_callback(params);
|
switches |
0:0e018d759a2a
|
132
|
_client->onDataWritten().detach(makeFunctionPointer(this, &OneShotWriteCallback::call));
|
switches |
0:0e018d759a2a
|
133
|
delete this;
|
switches |
0:0e018d759a2a
|
134
|
}
|
switches |
0:0e018d759a2a
|
135
|
}
|
switches |
0:0e018d759a2a
|
136
|
|
switches |
0:0e018d759a2a
|
137
|
GattClient* _client;
|
switches |
0:0e018d759a2a
|
138
|
Gap::Handle_t _connHandle;
|
switches |
0:0e018d759a2a
|
139
|
GattAttribute::Handle_t _handle;
|
switches |
0:0e018d759a2a
|
140
|
GattClient::WriteCallback_t _callback;
|
switches |
0:0e018d759a2a
|
141
|
};
|
switches |
0:0e018d759a2a
|
142
|
|
switches |
0:0e018d759a2a
|
143
|
ble_error_t DiscoveredCharacteristic::write(uint16_t length, const uint8_t *value, const GattClient::WriteCallback_t& onRead) const {
|
switches |
0:0e018d759a2a
|
144
|
ble_error_t error = write(length, value);
|
switches |
0:0e018d759a2a
|
145
|
if (error) {
|
switches |
0:0e018d759a2a
|
146
|
return error;
|
switches |
0:0e018d759a2a
|
147
|
}
|
switches |
0:0e018d759a2a
|
148
|
|
switches |
0:0e018d759a2a
|
149
|
OneShotWriteCallback::launch(gattc, connHandle, valueHandle, onRead);
|
switches |
0:0e018d759a2a
|
150
|
|
switches |
0:0e018d759a2a
|
151
|
return error;
|
switches |
0:0e018d759a2a
|
152
|
}
|
switches |
0:0e018d759a2a
|
153
|
|
switches |
0:0e018d759a2a
|
154
|
ble_error_t DiscoveredCharacteristic::discoverDescriptors(
|
switches |
0:0e018d759a2a
|
155
|
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& onCharacteristicDiscovered,
|
switches |
0:0e018d759a2a
|
156
|
const CharacteristicDescriptorDiscovery::TerminationCallback_t& onTermination) const {
|
switches |
0:0e018d759a2a
|
157
|
|
switches |
0:0e018d759a2a
|
158
|
if(!gattc) {
|
switches |
0:0e018d759a2a
|
159
|
return BLE_ERROR_INVALID_STATE;
|
switches |
0:0e018d759a2a
|
160
|
}
|
switches |
0:0e018d759a2a
|
161
|
|
switches |
0:0e018d759a2a
|
162
|
ble_error_t err = gattc->discoverCharacteristicDescriptors(
|
switches |
0:0e018d759a2a
|
163
|
*this, onCharacteristicDiscovered, onTermination
|
switches |
0:0e018d759a2a
|
164
|
);
|
switches |
0:0e018d759a2a
|
165
|
|
switches |
0:0e018d759a2a
|
166
|
return err;
|
switches |
0:0e018d759a2a
|
167
|
}
|