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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/******************************************************************************
*
* Copyright 2008-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* this file contains the main GATT server attributes access request
* handling functions.
*
******************************************************************************/
#include "bt_target.h"
#include "bt_utils.h"
#include "gatt_api.h"
#include "gatt_int.h"
#include "osi/include/osi.h"
using base::StringPrintf;
using bluetooth::Uuid;
#define GATTP_MAX_NUM_INC_SVR 0
#define GATTP_MAX_CHAR_NUM 2
#define GATTP_MAX_ATTR_NUM (GATTP_MAX_CHAR_NUM * 2 + GATTP_MAX_NUM_INC_SVR + 1)
#define GATTP_MAX_CHAR_VALUE_SIZE 50
#ifndef GATTP_ATTR_DB_SIZE
#define GATTP_ATTR_DB_SIZE \
GATT_DB_MEM_SIZE(GATTP_MAX_NUM_INC_SVR, GATTP_MAX_CHAR_NUM, \
GATTP_MAX_CHAR_VALUE_SIZE)
#endif
static void gatt_request_cback(uint16_t conn_id, uint32_t trans_id,
uint8_t op_code, tGATTS_DATA* p_data);
static void gatt_connect_cback(UNUSED_ATTR tGATT_IF gatt_if,
const RawAddress& bda, uint16_t conn_id,
bool connected, tGATT_DISCONN_REASON reason,
tBT_TRANSPORT transport);
static void gatt_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
tGATT_DISC_RES* p_data);
static void gatt_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
tGATT_STATUS status);
static void gatt_cl_op_cmpl_cback(UNUSED_ATTR uint16_t conn_id,
UNUSED_ATTR tGATTC_OPTYPE op,
UNUSED_ATTR tGATT_STATUS status,
UNUSED_ATTR tGATT_CL_COMPLETE* p_data);
static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB* p_clcb);
static tGATT_CBACK gatt_profile_cback = {gatt_connect_cback,
gatt_cl_op_cmpl_cback,
gatt_disc_res_cback,
gatt_disc_cmpl_cback,
gatt_request_cback,
NULL,
NULL,
NULL,
NULL};
/*******************************************************************************
*
* Function gatt_profile_find_conn_id_by_bd_addr
*
* Description Find the connection ID by remote address
*
* Returns Connection ID
*
******************************************************************************/
uint16_t gatt_profile_find_conn_id_by_bd_addr(const RawAddress& remote_bda) {
uint16_t conn_id = GATT_INVALID_CONN_ID;
GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &conn_id,
BT_TRANSPORT_LE);
if (conn_id == GATT_INVALID_CONN_ID)
GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &conn_id,
BT_TRANSPORT_BR_EDR);
return conn_id;
}
/*******************************************************************************
*
* Function gatt_profile_find_clcb_by_conn_id
*
* Description find clcb by Connection ID
*
* Returns Pointer to the found link conenction control block.
*
******************************************************************************/
static tGATT_PROFILE_CLCB* gatt_profile_find_clcb_by_conn_id(uint16_t conn_id) {
uint8_t i_clcb;
tGATT_PROFILE_CLCB* p_clcb = NULL;
for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS;
i_clcb++, p_clcb++) {
if (p_clcb->in_use && p_clcb->conn_id == conn_id) return p_clcb;
}
return NULL;
}
/*******************************************************************************
*
* Function gatt_profile_find_clcb_by_bd_addr
*
* Description The function searches all LCBs with macthing bd address.
*
* Returns Pointer to the found link conenction control block.
*
******************************************************************************/
static tGATT_PROFILE_CLCB* gatt_profile_find_clcb_by_bd_addr(
const RawAddress& bda, tBT_TRANSPORT transport) {
uint8_t i_clcb;
tGATT_PROFILE_CLCB* p_clcb = NULL;
for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS;
i_clcb++, p_clcb++) {
if (p_clcb->in_use && p_clcb->transport == transport && p_clcb->connected &&
p_clcb->bda == bda)
return p_clcb;
}
return NULL;
}
/*******************************************************************************
*
* Function gatt_profile_clcb_alloc
*
* Description The function allocates a GATT profile connection link
* control block
*
* Returns NULL if not found. Otherwise pointer to the connection link
* block.
*
******************************************************************************/
tGATT_PROFILE_CLCB* gatt_profile_clcb_alloc(uint16_t conn_id,
const RawAddress& bda,
tBT_TRANSPORT tranport) {
uint8_t i_clcb = 0;
tGATT_PROFILE_CLCB* p_clcb = NULL;
for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS;
i_clcb++, p_clcb++) {
if (!p_clcb->in_use) {
p_clcb->in_use = true;
p_clcb->conn_id = conn_id;
p_clcb->connected = true;
p_clcb->transport = tranport;
p_clcb->bda = bda;
break;
}
}
if (i_clcb < GATT_MAX_APPS) return p_clcb;
return NULL;
}
/*******************************************************************************
*
* Function gatt_profile_clcb_dealloc
*
* Description The function deallocates a GATT profile connection link
* control block
*
* Returns void
*
******************************************************************************/
void gatt_profile_clcb_dealloc(tGATT_PROFILE_CLCB* p_clcb) {
memset(p_clcb, 0, sizeof(tGATT_PROFILE_CLCB));
}
/*******************************************************************************
*
* Function gatt_request_cback
*
* Description GATT profile attribute access request callback.
*
* Returns void.
*
******************************************************************************/
static void gatt_request_cback(uint16_t conn_id, uint32_t trans_id,
tGATTS_REQ_TYPE type, tGATTS_DATA* p_data) {
uint8_t status = GATT_INVALID_PDU;
tGATTS_RSP rsp_msg;
bool ignore = false;
memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
switch (type) {
case GATTS_REQ_TYPE_READ_CHARACTERISTIC:
case GATTS_REQ_TYPE_READ_DESCRIPTOR:
status = GATT_READ_NOT_PERMIT;
break;
case GATTS_REQ_TYPE_WRITE_CHARACTERISTIC:
case GATTS_REQ_TYPE_WRITE_DESCRIPTOR:
status = GATT_WRITE_NOT_PERMIT;
break;
case GATTS_REQ_TYPE_WRITE_EXEC:
case GATT_CMD_WRITE:
ignore = true;
VLOG(1) << "Ignore GATT_REQ_EXEC_WRITE/WRITE_CMD";
break;
case GATTS_REQ_TYPE_MTU:
VLOG(1) << "Get MTU exchange new mtu size: " << +p_data->mtu;
ignore = true;
break;
default:
VLOG(1) << "Unknown/unexpected LE GAP ATT request: " << loghex(type);
break;
}
if (!ignore) GATTS_SendRsp(conn_id, trans_id, status, &rsp_msg);
}
/*******************************************************************************
*
* Function gatt_connect_cback
*
* Description Gatt profile connection callback.
*
* Returns void
*
******************************************************************************/
static void gatt_connect_cback(UNUSED_ATTR tGATT_IF gatt_if,
const RawAddress& bda, uint16_t conn_id,
bool connected, tGATT_DISCONN_REASON reason,
tBT_TRANSPORT transport) {
VLOG(1) << __func__ << ": from " << bda << " connected: " << connected
<< ", conn_id: " << loghex(conn_id) << "reason: " << loghex(reason);
tGATT_PROFILE_CLCB* p_clcb =
gatt_profile_find_clcb_by_bd_addr(bda, transport);
if (p_clcb == NULL) return;
if (connected) {
p_clcb->conn_id = conn_id;
p_clcb->connected = true;
if (p_clcb->ccc_stage == GATT_SVC_CHANGED_CONNECTING) {
p_clcb->ccc_stage++;
gatt_cl_start_config_ccc(p_clcb);
}
} else {
gatt_profile_clcb_dealloc(p_clcb);
}
}
/*******************************************************************************
*
* Function gatt_profile_db_init
*
* Description Initializa the GATT profile attribute database.
*
******************************************************************************/
void gatt_profile_db_init(void) {
uint16_t service_handle = 0;
/* Fill our internal UUID with a fixed pattern 0x81 */
std::array<uint8_t, Uuid::kNumBytes128> tmp;
tmp.fill(0x81);
/* Create a GATT profile service */
gatt_cb.gatt_if = GATT_Register(Uuid::From128BitBE(tmp), &gatt_profile_cback);
GATT_StartIf(gatt_cb.gatt_if);
Uuid service_uuid = Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER);
Uuid char_uuid = Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD);
btgatt_db_element_t service[] = {
{.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = service_uuid},
{.type = BTGATT_DB_CHARACTERISTIC,
.uuid = char_uuid,
.properties = GATT_CHAR_PROP_BIT_INDICATE,
.permissions = 0}};
GATTS_AddService(gatt_cb.gatt_if, service,
sizeof(service) / sizeof(btgatt_db_element_t));
service_handle = service[0].attribute_handle;
gatt_cb.handle_of_h_r = service[1].attribute_handle;
VLOG(1) << __func__ << ": gatt_if=" << +gatt_cb.gatt_if;
}
/*******************************************************************************
*
* Function gatt_disc_res_cback
*
* Description Gatt profile discovery result callback
*
* Returns void
*
******************************************************************************/
static void gatt_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
tGATT_DISC_RES* p_data) {
tGATT_PROFILE_CLCB* p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
if (p_clcb == NULL) return;
switch (disc_type) {
case GATT_DISC_SRVC_BY_UUID: /* stage 1 */
p_clcb->e_handle = p_data->value.group_value.e_handle;
p_clcb->ccc_result++;
break;
case GATT_DISC_CHAR: /* stage 2 */
p_clcb->s_handle = p_data->value.dclr_value.val_handle;
p_clcb->ccc_result++;
break;
case GATT_DISC_CHAR_DSCPT: /* stage 3 */
if (p_data->type == Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG)) {
p_clcb->s_handle = p_data->handle;
p_clcb->ccc_result++;
}
break;
}
}
/*******************************************************************************
*
* Function gatt_disc_cmpl_cback
*
* Description Gatt profile discovery complete callback
*
* Returns void
*
******************************************************************************/
static void gatt_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
tGATT_STATUS status) {
tGATT_PROFILE_CLCB* p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
if (p_clcb == NULL) return;
if (status != GATT_SUCCESS || p_clcb->ccc_result == 0) {
LOG(WARNING) << __func__
<< ": Unable to register for service changed indication";
return;
}
p_clcb->ccc_result = 0;
p_clcb->ccc_stage++;
gatt_cl_start_config_ccc(p_clcb);
}
/*******************************************************************************
*
* Function gatt_cl_op_cmpl_cback
*
* Description Gatt profile client operation complete callback
*
* Returns void
*
******************************************************************************/
static void gatt_cl_op_cmpl_cback(UNUSED_ATTR uint16_t conn_id,
UNUSED_ATTR tGATTC_OPTYPE op,
UNUSED_ATTR tGATT_STATUS status,
UNUSED_ATTR tGATT_CL_COMPLETE* p_data) {}
/*******************************************************************************
*
* Function gatt_cl_start_config_ccc
*
* Description Gatt profile start configure service change CCC
*
* Returns void
*
******************************************************************************/
static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB* p_clcb) {
VLOG(1) << __func__ << ": stage: " << +p_clcb->ccc_stage;
switch (p_clcb->ccc_stage) {
case GATT_SVC_CHANGED_SERVICE: /* discover GATT service */
GATTC_Discover(p_clcb->conn_id, GATT_DISC_SRVC_BY_UUID, 0x0001, 0xffff,
Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER));
break;
case GATT_SVC_CHANGED_CHARACTERISTIC: /* discover service change char */
GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR, 0x0001, p_clcb->e_handle,
Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD));
break;
case GATT_SVC_CHANGED_DESCRIPTOR: /* discover service change ccc */
GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR_DSCPT, p_clcb->s_handle,
p_clcb->e_handle);
break;
case GATT_SVC_CHANGED_CONFIGURE_CCCD: /* write ccc */
{
tGATT_VALUE ccc_value;
memset(&ccc_value, 0, sizeof(tGATT_VALUE));
ccc_value.handle = p_clcb->s_handle;
ccc_value.len = 2;
ccc_value.value[0] = GATT_CLT_CONFIG_INDICATION;
GATTC_Write(p_clcb->conn_id, GATT_WRITE, &ccc_value);
break;
}
}
}
/*******************************************************************************
*
* Function GATT_ConfigServiceChangeCCC
*
* Description Configure service change indication on remote device
*
* Returns none
*
******************************************************************************/
void GATT_ConfigServiceChangeCCC(const RawAddress& remote_bda, bool enable,
tBT_TRANSPORT transport) {
tGATT_PROFILE_CLCB* p_clcb =
gatt_profile_find_clcb_by_bd_addr(remote_bda, transport);
if (p_clcb == NULL)
p_clcb = gatt_profile_clcb_alloc(0, remote_bda, transport);
if (p_clcb == NULL) return;
if (GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &p_clcb->conn_id,
transport)) {
p_clcb->connected = true;
}
/* hold the link here */
GATT_Connect(gatt_cb.gatt_if, remote_bda, true, transport, true);
p_clcb->ccc_stage = GATT_SVC_CHANGED_CONNECTING;
if (!p_clcb->connected) {
/* wait for connection */
return;
}
p_clcb->ccc_stage++;
gatt_cl_start_config_ccc(p_clcb);
}
|