-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathBLECallbacks.cpp
281 lines (238 loc) · 9.82 KB
/
BLECallbacks.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) 2016 Intel Corporation. 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 <errno.h>
#include "CurieBLE.h"
#include "BLEAttribute.h"
#include "BLECharacteristicImp.h"
#include "BLEDeviceManager.h"
#include "BLEProfileManager.h"
// GATT Server Only
ssize_t profile_read_process(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
void *buf, uint16_t len,
uint16_t offset)
{
const unsigned char *pvalue;
BLEAttribute *bleattr = (BLEAttribute *)attr->user_data;
BLEAttributeType type = bleattr->type();
if (BLETypeCharacteristic == type)
{
BLECharacteristicImp* blecharacteritic = (BLECharacteristicImp*)bleattr;
pvalue = blecharacteritic->value();
return bt_gatt_attr_read(conn, attr, buf, len, offset, pvalue,
blecharacteritic->valueLength());
}
else if (BLETypeDescriptor == type)
{
BLEDescriptorImp* bledescriptor = (BLEDescriptorImp*)bleattr;
pvalue = bledescriptor->value();
return bt_gatt_attr_read(conn, attr, buf, len, offset, pvalue, bledescriptor->valueLength());
}
return 0;
}
// GATT server only
ssize_t profile_write_process(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
const void *buf, uint16_t len,
uint16_t offset)
{
pr_info(LOG_MODULE_BLE, "%s1", __FUNCTION__);
BLEAttribute *bleattr = (BLEAttribute *)attr->user_data;
BLECharacteristicImp* blecharacteritic;
BLEAttributeType type = bleattr->type();
if ((BLETypeCharacteristic != type) || 0 != offset)
{
return 0;
}
blecharacteritic = (BLECharacteristicImp*)bleattr;
blecharacteritic->setValue((const uint8_t *) buf, len);
return len;
}
ssize_t profile_longwrite_process(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf, uint16_t len,
uint16_t offset)
{
BLEAttribute *bleattr = (BLEAttribute *)attr->user_data;
BLEAttributeType type = bleattr->type();
if (BLETypeCharacteristic != type)
{
return 0;
}
BLECharacteristicImp *blecharacteritic = (BLECharacteristicImp*)bleattr;
blecharacteritic->setBuffer((const uint8_t *) buf, len, offset);
return len;
}
int profile_longflush_process(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
uint8_t flags)
{
BLEAttribute *bleattr = (BLEAttribute *)attr->user_data;
BLEAttributeType type = bleattr->type();
if (BLETypeCharacteristic != type)
{
return 0;
}
BLECharacteristicImp *blecharacteritic = (BLECharacteristicImp*)bleattr;
switch (flags)
{
case BT_GATT_FLUSH_DISCARD:
/* Discard buffer reseting it back with data */
blecharacteritic->discardBuffer();
return 0;
case BT_GATT_FLUSH_SYNC:
/* Sync buffer to data */
blecharacteritic->syncupBuffer2Value();
return 0;
}
return -EINVAL;
}
// GATT client only
uint8_t profile_notify_process (bt_conn_t *conn,
bt_gatt_subscribe_params_t *params,
const void *data, uint16_t length)
{
//BLEPeripheralHelper* peripheral = BLECentralRole::instance()->peripheral(conn);// Find peripheral by bt_conn
//BLEAttribute* notifyatt = peripheral->attribute(params); // Find attribute by params
BLECharacteristicImp* chrc = NULL;
BLEDevice bleDevice(bt_conn_get_dst(conn));
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->value_handle);
//assert(notifyatt->type() == BLETypeCharacteristic);
pr_debug(LOG_MODULE_APP, "%s1", __FUNCTION__);
if (NULL != chrc)
{
chrc->setValue((const unsigned char *)data, length);
}
return BT_GATT_ITER_CONTINUE;
}
// GATT client only
uint8_t profile_discover_process(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
bt_gatt_discover_params_t *params)
{
uint8_t ret = BT_GATT_ITER_STOP;
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
ret = BLEProfileManager::instance()->discoverResponseProc(conn, attr, params);
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
return ret;
}
// GATT Client only
uint8_t profile_read_rsp_process(bt_conn_t *conn,
int err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length)
{
BLECharacteristicImp *chrc = NULL;
BLEDevice bleDevice(bt_conn_get_dst(conn));
// Get characteristic by handle params->single.handle
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->single.handle);
if (chrc) // KW issue: may be NULL and will be dereferenced
chrc->setValue((const unsigned char *)data, length);
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
return BT_GATT_ITER_STOP;
}
uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
int err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length)
{
if (NULL == data)
{
return BT_GATT_ITER_STOP;
}
BLEDescriptorImp *descriptor = NULL;
BLEDevice bleDevice(bt_conn_get_dst(conn));
// Get characteristic by handle params->single.handle
descriptor = BLEProfileManager::instance()->descriptor(bleDevice, params->single.handle);
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
if (descriptor)
{
descriptor->writeValue((const unsigned char *)data, length, params->single.offset);
}
//pr_debug(LOG_MODULE_BLE, "%s-%d: desc len-%d", __FUNCTION__, __LINE__, descriptor->valueLength());
return BT_GATT_ITER_STOP;
}
uint8_t profile_service_read_rsp_process(bt_conn_t *conn,
int err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length)
{
uint8_t ret = BLEProfileManager::instance()->serviceReadRspProc(conn, err, params, data, length);
pr_debug(LOG_MODULE_BLE, "%s-%d:ret-%d", __FUNCTION__, __LINE__, ret);
return ret;
}
uint8_t profile_characteristic_read_rsp_process(bt_conn_t *conn,
int err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length)
{
BLEDevice bleDevice(bt_conn_get_dst(conn));
BLEServiceImp* service_imp = BLEProfileManager::instance()->getServiceBySubHandle(bleDevice, params->single.handle);
uint8_t ret = service_imp->characteristicReadRspProc(conn,
err,
params,
data,
length);
pr_debug(LOG_MODULE_BLE, "%s-%d:ret-%d", __FUNCTION__, __LINE__, ret);
return ret;
}
void bleConnectEventHandler(bt_conn_t *conn,
uint8_t err,
void *param)
{
BLEDeviceManager* p = (BLEDeviceManager*)param;
p->handleConnectEvent(conn, err);
}
void bleDisconnectEventHandler(bt_conn_t *conn,
uint8_t reason,
void *param)
{
BLEDeviceManager* p = (BLEDeviceManager*)param;
pr_info(LOG_MODULE_BLE, "Connect lost. Reason: %d", reason);
p->handleDisconnectEvent(conn, reason);
}
void bleParamUpdatedEventHandler(bt_conn_t *conn,
uint16_t interval,
uint16_t latency,
uint16_t timeout,
void *param)
{
BLEDeviceManager* p = (BLEDeviceManager*)param;
p->handleParamUpdated(conn, interval, latency, timeout);
}
void ble_central_device_found(const bt_addr_le_t *addr,
int8_t rssi,
uint8_t type,
const uint8_t *ad,
uint8_t len)
{
//char dev[BT_ADDR_LE_STR_LEN];
//bt_addr_le_to_str(addr, dev, sizeof(dev));
//pr_debug(LOG_MODULE_BLE, "[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i\n",
// dev, type, len, rssi);
BLEDeviceManager::instance()->handleDeviceFound(addr, rssi, type,
ad, len);
}
void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
const void *data)
{
BLECharacteristicImp::writeResponseReceived(conn, err, data);
}