blob: 69e7b0d235da803ef7eb1f8b1e026138652337cf [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2008-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 * this file contains the GATT server functions
22 *
23 ******************************************************************************/
24
25#include "bt_target.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080026#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080027
28#if BLE_INCLUDED == TRUE
29#include <string.h>
30#include "gatt_int.h"
31#include "l2c_api.h"
32
Andre Eisenbachccf9c152013-10-02 15:37:21 -070033#define GATT_MTU_REQ_MIN_LEN 2
34
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
36/*******************************************************************************
37**
38** Function gatt_sr_enqueue_cmd
39**
40** Description This function enqueue the request from client which needs a
41** application response, and update the transaction ID.
42**
43** Returns void
44**
45*******************************************************************************/
46UINT32 gatt_sr_enqueue_cmd (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 handle)
47{
48 tGATT_SR_CMD *p_cmd = &p_tcb->sr_cmd;
49 UINT32 trans_id = 0;
50
51 if ( (p_cmd->op_code == 0) ||
52 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
53 {
54 if (op_code == GATT_CMD_WRITE ||
55 op_code == GATT_SIGN_CMD_WRITE ||
56 op_code == GATT_REQ_MTU ||
57 op_code == GATT_HANDLE_VALUE_CONF)
58 {
59 trans_id = ++p_tcb->trans_id;
60 }
61 else
62 {
63 p_cmd->trans_id = ++p_tcb->trans_id;
64 p_cmd->op_code = op_code;
65 p_cmd->handle = handle;
66 p_cmd->status = GATT_NOT_FOUND;
67 p_tcb->trans_id %= GATT_TRANS_ID_MAX;
68 trans_id = p_cmd->trans_id;
69 }
70 }
71
72 return trans_id;
73}
74
75/*******************************************************************************
76**
77** Function gatt_sr_cmd_empty
78**
79** Description This function check the server command queue is empty or not.
80**
81** Returns TRUE if empty, FALSE if there is pending command.
82**
83*******************************************************************************/
84BOOLEAN gatt_sr_cmd_empty (tGATT_TCB *p_tcb)
85{
86 return(p_tcb->sr_cmd.op_code == 0);
87}
88
89/*******************************************************************************
90**
91** Function gatt_dequeue_sr_cmd
92**
93** Description This function dequeue the request from command queue.
94**
95** Returns void
96**
97*******************************************************************************/
98void gatt_dequeue_sr_cmd (tGATT_TCB *p_tcb)
99{
100 /* Double check in case any buffers are queued */
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700101 GATT_TRACE_DEBUG("gatt_dequeue_sr_cmd" );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800102 if (p_tcb->sr_cmd.p_rsp_msg)
103 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700104 GATT_TRACE_ERROR("free p_tcb->sr_cmd.p_rsp_msg = %d", p_tcb->sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800105
106 GKI_freebuf (p_tcb->sr_cmd.p_rsp_msg);
107 }
108
109 while (p_tcb->sr_cmd.multi_rsp_q.p_first)
110 GKI_freebuf (GKI_dequeue (&p_tcb->sr_cmd.multi_rsp_q));
111 memset( &p_tcb->sr_cmd, 0, sizeof(tGATT_SR_CMD));
112}
113
114/*******************************************************************************
115**
116** Function process_read_multi_rsp
117**
118** Description This function check the read multiple response.
119**
120** Returns BOOLEAN if all replies have been received
121**
122*******************************************************************************/
123static BOOLEAN process_read_multi_rsp (tGATT_SR_CMD *p_cmd, tGATT_STATUS status,
124 tGATTS_RSP *p_msg, UINT16 mtu)
125{
126 tGATTS_RSP *p_rsp;
127 UINT16 ii, total_len, len;
128 BT_HDR *p_buf = (BT_HDR *)GKI_getbuf((UINT16)sizeof(tGATTS_RSP));
129 UINT8 *p;
130 BOOLEAN is_overflow = FALSE;
131
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700132 GATT_TRACE_DEBUG ("process_read_multi_rsp status=%d mtu=%d", status, mtu);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800133
134 if (p_buf == NULL)
135 {
136 p_cmd->status = GATT_INSUF_RESOURCE;
137 return FALSE;
138 }
139
140 /* Enqueue the response */
141 memcpy((void *)p_buf, (const void *)p_msg, sizeof(tGATTS_RSP));
142 GKI_enqueue (&p_cmd->multi_rsp_q, p_buf);
143
144 p_cmd->status = status;
145 if (status == GATT_SUCCESS)
146 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700147 GATT_TRACE_DEBUG ("Multi read count=%d num_hdls=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800148 p_cmd->multi_rsp_q.count, p_cmd->multi_req.num_handles);
149 /* Wait till we get all the responses */
150 if (p_cmd->multi_rsp_q.count == p_cmd->multi_req.num_handles)
151 {
152 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
153 if ((p_buf = (BT_HDR *)GKI_getbuf(len)) == NULL)
154 {
155 p_cmd->status = GATT_INSUF_RESOURCE;
156 return(TRUE);
157 }
158
159 memset(p_buf, 0, len);
160 p_buf->offset = L2CAP_MIN_OFFSET;
161 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
162
163 /* First byte in the response is the opcode */
164 *p++ = GATT_RSP_READ_MULTI;
165 p_buf->len = 1;
166
167 /* Now walk through the buffers puting the data into the response in order */
168 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++)
169 {
170 if (ii==0)
171 {
172 p_rsp = (tGATTS_RSP *)GKI_getfirst (&p_cmd->multi_rsp_q);
173 }
174 else
175 {
176 p_rsp = (tGATTS_RSP *)GKI_getnext (p_rsp);
177 }
178
179 if (p_rsp != NULL)
180 {
181
182 total_len = (p_buf->len + p_rsp->attr_value.len);
183
184 if (total_len > mtu)
185 {
186 /* just send the partial response for the overflow case */
187 len = p_rsp->attr_value.len - (total_len - mtu);
188 is_overflow = TRUE;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700189 GATT_TRACE_DEBUG ("multi read overflow available len=%d val_len=%d", len, p_rsp->attr_value.len );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800190 }
191 else
192 {
193 len = p_rsp->attr_value.len;
194 }
195
196 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii])
197 {
198 memcpy (p, p_rsp->attr_value.value, len);
199 if (!is_overflow)
200 p += len;
201 p_buf->len += len;
202 }
203 else
204 {
205 p_cmd->status = GATT_NOT_FOUND;
206 break;
207 }
208
209 if (is_overflow)
210 break;
211
212 }
213 else
214 {
215 p_cmd->status = GATT_NOT_FOUND;
216 break;
217 }
218
219 } /* loop through all handles*/
220
221
222 /* Sanity check on the buffer length */
223 if (p_buf->len == 0)
224 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700225 GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800226 p_cmd->status = GATT_NOT_FOUND;
227 GKI_freebuf (p_buf);
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700228 GATT_TRACE_DEBUG(" GKI_freebuf (p_buf)");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229 }
230 else if (p_cmd->p_rsp_msg != NULL)
231 {
232 GKI_freebuf (p_buf);
233 }
234 else
235 {
236 p_cmd->p_rsp_msg = p_buf;
237 }
238
239 return(TRUE);
240 }
241 }
242 else /* any handle read exception occurs, return error */
243 {
244 return(TRUE);
245 }
246
247 /* If here, still waiting */
248 return(FALSE);
249}
250
251/*******************************************************************************
252**
253** Function gatt_sr_process_app_rsp
254**
255** Description This function checks whether the response message from application
256** match any pending request or not.
257**
258** Returns void
259**
260*******************************************************************************/
261tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if,
262 UINT32 trans_id, UINT8 op_code,
263 tGATT_STATUS status, tGATTS_RSP *p_msg)
264{
265 tGATT_STATUS ret_code = GATT_SUCCESS;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800266 UNUSED(trans_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800267
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700268 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp gatt_if=%d", gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800269
270 gatt_sr_update_cback_cnt(p_tcb, gatt_if, FALSE, FALSE);
271
272 if (op_code == GATT_REQ_READ_MULTI)
273 {
274 /* If no error and still waiting, just return */
275 if (!process_read_multi_rsp (&p_tcb->sr_cmd, status, p_msg, p_tcb->payload_size))
276 return(GATT_SUCCESS);
277 }
278 else
279 {
280 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
281 gatt_sr_update_prep_cnt(p_tcb, gatt_if, TRUE, FALSE);
282
283 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
284 gatt_sr_reset_cback_cnt(p_tcb);
285
286 p_tcb->sr_cmd.status = status;
287
288 if (gatt_sr_is_cback_cnt_zero(p_tcb)
289 && status == GATT_SUCCESS)
290 {
291 if (p_tcb->sr_cmd.p_rsp_msg == NULL)
292 {
293 p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg (p_tcb, (UINT8)(op_code + 1), (tGATT_SR_MSG *)p_msg);
294 }
295 else
296 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700297 GATT_TRACE_ERROR("Exception!!! already has respond message");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298 }
299 }
300 }
301 if (gatt_sr_is_cback_cnt_zero(p_tcb))
302 {
303 if ( (p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg) )
304 {
305 ret_code = attp_send_sr_msg (p_tcb, p_tcb->sr_cmd.p_rsp_msg);
306 p_tcb->sr_cmd.p_rsp_msg = NULL;
307 }
308 else
309 {
310 ret_code = gatt_send_error_rsp (p_tcb, status, op_code, p_tcb->sr_cmd.handle, FALSE);
311 }
312
313 gatt_dequeue_sr_cmd(p_tcb);
314 }
315
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700316 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp ret_code=%d", ret_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800317
318 return ret_code;
319}
320
321/*******************************************************************************
322**
323** Function gatt_process_exec_write_req
324**
325** Description This function is called to process the execute write request
326** from client.
327**
328** Returns void
329**
330*******************************************************************************/
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700331void gatt_process_exec_write_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800332{
333 UINT8 *p = p_data, flag, i = 0;
334 UINT32 trans_id = 0;
335 BT_HDR *p_buf;
336 tGATT_IF gatt_if;
337 UINT16 conn_id;
338
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700339 UNUSED(len);
340
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800341#if GATT_CONFORMANCE_TESTING == TRUE
342 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
343 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700344 GATT_TRACE_DEBUG("Conformance tst: forced err rspv for Execute Write: error status=%d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700345 gatt_cb.err_status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800346
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700347 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, gatt_cb.handle, FALSE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800348
349 return;
350 }
351#endif
352
The Android Open Source Project5738f832012-12-12 16:00:35 -0800353 STREAM_TO_UINT8(flag, p);
354
355 /* mask the flag */
356 flag &= GATT_PREP_WRITE_EXEC;
357
358
359 /* no prep write is queued */
360 if (!gatt_sr_is_prep_cnt_zero(p_tcb))
361 {
362 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0);
363 gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb);
364
365 for (i=0; i<GATT_MAX_APPS; i++)
366 {
367 if (p_tcb->prep_cnt[i])
368 {
369 gatt_if = (tGATT_IF) (i+1);
370 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
371 gatt_sr_send_req_callback(conn_id,
372 trans_id,
373 GATTS_REQ_TYPE_WRITE_EXEC,
374 (tGATTS_DATA *)&flag);
375 p_tcb->prep_cnt[i]= 0;
376 }
377 }
378 }
379 else /* nothing needs to be executed , send response now */
380 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700381 GATT_TRACE_ERROR("gatt_process_exec_write_req: no prepare write pending");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800382
383 if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_EXEC_WRITE, NULL)) != NULL)
384 {
385 attp_send_sr_msg (p_tcb, p_buf);
386 }
387
388 }
389}
390
391/*******************************************************************************
392**
393** Function gatt_process_read_multi_req
394**
395** Description This function is called to process the read multiple request
396** from client.
397**
398** Returns void
399**
400*******************************************************************************/
401void gatt_process_read_multi_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
402{
403 UINT32 trans_id;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700404 UINT16 handle = 0, ll = len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800405 UINT8 *p = p_data, i_rcb;
406 tGATT_STATUS err = GATT_SUCCESS;
407 UINT8 sec_flag, key_size;
408 tGATTS_RSP *p_msg;
409
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700410 GATT_TRACE_DEBUG("gatt_process_read_multi_req" );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800411 p_tcb->sr_cmd.multi_req.num_handles = 0;
412
413 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700414 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800415 &sec_flag,
416 &key_size);
417
418#if GATT_CONFORMANCE_TESTING == TRUE
419 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
420 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700421 GATT_TRACE_DEBUG("Conformance tst: forced err rspvofr ReadMultiple: error status=%d", gatt_cb.err_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422
423 STREAM_TO_UINT16(handle, p);
424
425 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
426
427 return;
428 }
429#endif
430
431 while (ll >= 2 && p_tcb->sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES)
432 {
433 STREAM_TO_UINT16(handle, p);
434
435 if ((i_rcb = gatt_sr_find_i_rcb_by_handle(handle)) < GATT_MAX_SR_PROFILES)
436 {
437 p_tcb->sr_cmd.multi_req.handles[p_tcb->sr_cmd.multi_req.num_handles++] = handle;
438
439 /* check read permission */
440 if ((err = gatts_read_attr_perm_check( gatt_cb.sr_reg[i_rcb].p_db,
441 FALSE,
442 handle,
443 sec_flag,
444 key_size))
445 != GATT_SUCCESS)
446 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700447 GATT_TRACE_DEBUG("read permission denied : 0x%02x", err);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800448 break;
449 }
450 }
451 else
452 {
453 /* invalid handle */
454 err = GATT_INVALID_HANDLE;
455 break;
456 }
457 ll -= 2;
458 }
459
460 if (ll != 0)
461 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700462 GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800463 }
464
465 if (p_tcb->sr_cmd.multi_req.num_handles == 0)
466 err = GATT_INVALID_HANDLE;
467
468 if (err == GATT_SUCCESS)
469 {
470 if ((trans_id = gatt_sr_enqueue_cmd (p_tcb, op_code, p_tcb->sr_cmd.multi_req.handles[0])) != 0)
471 {
472 gatt_sr_reset_cback_cnt(p_tcb); /* read multiple use multi_rsp_q's count*/
473
474 for (ll = 0; ll < p_tcb->sr_cmd.multi_req.num_handles; ll ++)
475 {
476 if ((p_msg = (tGATTS_RSP *)GKI_getbuf(sizeof(tGATTS_RSP))) != NULL)
477 {
478 memset(p_msg, 0, sizeof(tGATTS_RSP))
479 ;
480 handle = p_tcb->sr_cmd.multi_req.handles[ll];
481 i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
482
483 p_msg->attr_value.handle = handle;
484 err = gatts_read_attr_value_by_handle(p_tcb,
485 gatt_cb.sr_reg[i_rcb].p_db,
486 op_code,
487 handle,
488 0,
489 p_msg->attr_value.value,
490 &p_msg->attr_value.len,
491 GATT_MAX_ATTR_LEN,
492 sec_flag,
493 key_size,
494 trans_id);
495
496 if (err == GATT_SUCCESS)
497 {
498 gatt_sr_process_app_rsp(p_tcb, gatt_cb.sr_reg[i_rcb].gatt_if ,trans_id, op_code, GATT_SUCCESS, p_msg);
499 }
500 /* either not using or done using the buffer, release it now */
501 GKI_freebuf(p_msg);
502 }
503 else
504 {
505 err = GATT_NO_RESOURCES;
506 gatt_dequeue_sr_cmd(p_tcb);
507 break;
508 }
509 }
510 }
511 else
512 err = GATT_NO_RESOURCES;
513 }
514
515 /* in theroy BUSY is not possible(should already been checked), protected check */
516 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
517 gatt_send_error_rsp(p_tcb, err, op_code, handle, FALSE);
518}
519
520/*******************************************************************************
521**
522** Function gatt_build_primary_service_rsp
523**
524** Description Primamry service request processed internally. Theretically
525** only deal with ReadByTypeVAlue and ReadByGroupType.
526**
527** Returns void
528**
529*******************************************************************************/
530static tGATT_STATUS gatt_build_primary_service_rsp (BT_HDR *p_msg, tGATT_TCB *p_tcb,
531 UINT8 op_code, UINT16 s_hdl,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700532 UINT16 e_hdl, UINT8 *p_data, tBT_UUID value)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800533{
534 tGATT_STATUS status = GATT_NOT_FOUND;
535 UINT8 handle_len =4, *p ;
536 tGATT_SR_REG *p_rcb;
537 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
538 tGATT_SRV_LIST_ELEM *p_srv=NULL;
539 tBT_UUID *p_uuid;
540
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700541 UNUSED(p_data);
542
The Android Open Source Project5738f832012-12-12 16:00:35 -0800543 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
544
545 p_srv = p_list->p_first;
546
547 while (p_srv)
548 {
549 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
550
551 if (p_rcb->in_use &&
552 p_rcb->s_hdl >= s_hdl &&
553 p_rcb->s_hdl <= e_hdl &&
554 p_rcb->type == GATT_UUID_PRI_SERVICE)
555 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700556 if ((p_uuid = gatts_get_service_uuid (p_rcb->p_db)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800557 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800558 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700559 handle_len = 4 + p_uuid->len;
560
561 /* get the length byte in the repsonse */
562 if (p_msg->offset ==0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700564 *p ++ = op_code + 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565 p_msg->len ++;
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700566 p_msg->offset = handle_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567
568 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700569 {
570 *p ++ = (UINT8)p_msg->offset; /* length byte */
571 p_msg->len ++;
572 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800573 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700574
575 if (p_msg->len + p_msg->offset <= p_tcb->payload_size &&
576 handle_len == p_msg->offset)
577 {
578 if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
579 gatt_uuid_compare(value, *p_uuid))
580 {
581 UINT16_TO_STREAM(p, p_rcb->s_hdl);
582
583 if (p_list->p_last_primary == p_srv &&
584 p_list->p_last_primary == p_list->p_last)
585 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700586 GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute");
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700587 UINT16_TO_STREAM(p, 0xFFFF); /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
588 }
589 else
590 {
591 UINT16_TO_STREAM(p, p_rcb->e_hdl);
592 }
593
594 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
595 gatt_build_uuid_to_stream(&p, *p_uuid);
596
597 status = GATT_SUCCESS;
598 p_msg->len += p_msg->offset;
599 }
600 }
601 else
602 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800603 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800604 }
605 p_srv = p_srv->p_next;
606 }
607 p_msg->offset = L2CAP_MIN_OFFSET;
608
609 return status;
610}
611
612/*******************************************************************************
613**
614** Function gatt_build_find_info_rsp
615**
616** Description fill the find information response information in the given
617** buffer.
618**
619** Returns TRUE: if data filled sucessfully.
620** FALSE: packet full, or format mismatch.
621**
622*******************************************************************************/
623static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SR_REG *p_rcb, BT_HDR *p_msg, UINT16 *p_len,
624 UINT16 s_hdl, UINT16 e_hdl)
625{
626 tGATT_STATUS status = GATT_NOT_FOUND;
627 UINT8 *p;
628 UINT16 len = *p_len;
629 tGATT_ATTR16 *p_attr = NULL;
630 UINT8 info_pair_len[2] = {4, 18};
631
632 if (!p_rcb->p_db || !p_rcb->p_db->p_attr_list)
633 return status;
634
635 /* check the attribute database */
636 p_attr = (tGATT_ATTR16 *) p_rcb->p_db->p_attr_list;
637
638 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
639
640 while (p_attr)
641 {
642 if (p_attr->handle > e_hdl)
643 {
644 break;
645 }
646
647 if (p_attr->handle >= s_hdl)
648 {
649 if (p_msg->offset == 0)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700650 p_msg->offset = (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) ? GATT_INFO_TYPE_PAIR_16 : GATT_INFO_TYPE_PAIR_128;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800651
652 if (len >= info_pair_len[p_msg->offset - 1])
653 {
654 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16)
655 {
656 UINT16_TO_STREAM(p, p_attr->handle);
657 UINT16_TO_STREAM(p, p_attr->uuid);
658 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700659 else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_128 )
The Android Open Source Project5738f832012-12-12 16:00:35 -0800660 {
661 UINT16_TO_STREAM(p, p_attr->handle);
662 ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *) p_attr)->uuid, LEN_UUID_128);
663 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700664 else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32)
665 {
666 UINT16_TO_STREAM(p, p_attr->handle);
667 gatt_convert_uuid32_to_uuid128(p, ((tGATT_ATTR32 *) p_attr)->uuid);
668 p += LEN_UUID_128;
669 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800670 else
671 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700672 GATT_TRACE_ERROR("format mismatch");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800673 status = GATT_NO_RESOURCES;
674 break;
675 /* format mismatch */
676 }
677 p_msg->len += info_pair_len[p_msg->offset - 1];
678 len -= info_pair_len[p_msg->offset - 1];
679 status = GATT_SUCCESS;
680
681 }
682 else
683 {
684 status = GATT_NO_RESOURCES;
685 break;
686 }
687 }
688 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
689 }
690
691 *p_len = len;
692 return status;
693}
694
695/*******************************************************************************
696**
697** Function gatts_internal_read_by_type_req
698**
699** Description check to see if the ReadByType request can be handled internally.
700**
701** Returns void
702**
703*******************************************************************************/
704static tGATT_STATUS gatts_validate_packet_format(UINT8 op_code, UINT16 *p_len,
705 UINT8 **p_data, tBT_UUID *p_uuid_filter,
706 UINT16 *p_s_hdl, UINT16 *p_e_hdl)
707{
708 tGATT_STATUS reason = GATT_SUCCESS;
709 UINT16 uuid_len, s_hdl = 0, e_hdl = 0;
710 UINT16 len = *p_len;
711 UINT8 *p = *p_data;
712
713 if (len >= 4)
714 {
715 /* obtain starting handle, and ending handle */
716 STREAM_TO_UINT16(s_hdl, p);
717 STREAM_TO_UINT16(e_hdl, p);
718 len -= 4;
719
720 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || !GATT_HANDLE_IS_VALID(e_hdl))
721 {
722 reason = GATT_INVALID_HANDLE;
723 }
724 /* for these PDUs, uuid filter must present */
725 else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
726 op_code == GATT_REQ_FIND_TYPE_VALUE ||
727 op_code == GATT_REQ_READ_BY_TYPE)
728 {
729 if (len >= 2 && p_uuid_filter != NULL)
730 {
731 uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
732
733 /* parse uuid now */
734 if (gatt_parse_uuid_from_cmd (p_uuid_filter, uuid_len, &p) == FALSE ||
735 p_uuid_filter->len == 0)
736 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700737 GATT_TRACE_DEBUG("UUID filter does not exsit");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800738 reason = GATT_INVALID_PDU;
739 }
740 else
741 len -= p_uuid_filter->len;
742 }
743 else
744 reason = GATT_INVALID_PDU;
745 }
746 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700747 else
748 reason = GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800749
750 *p_data = p;
751 *p_len = len;
752 *p_s_hdl = s_hdl;
753 *p_e_hdl = e_hdl;
754
755 return reason;
756}
757
758/*******************************************************************************
759**
760** Function gatts_process_primary_service_req
761**
762** Description process ReadByGroupType/ReadByTypeValue request, for discover
763** all primary services or discover primary service by UUID request.
764**
765** Returns void
766**
767*******************************************************************************/
768void gatts_process_primary_service_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
769{
770 UINT8 reason = GATT_INVALID_PDU;
771 UINT16 s_hdl = 0, e_hdl = 0;
772 tBT_UUID uuid, value, primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
773 BT_HDR *p_msg = NULL;
774 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
775
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700776 memset (&value, 0, sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800777 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
778
779 if (reason == GATT_SUCCESS)
780 {
781 if (gatt_uuid_compare(uuid, primary_service))
782 {
783 if (op_code == GATT_REQ_FIND_TYPE_VALUE)
784 {
785 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == FALSE)
786 reason = GATT_INVALID_PDU;
787 }
788
789 if (reason == GATT_SUCCESS)
790 {
791 if ((p_msg = (BT_HDR *)GKI_getbuf(msg_len)) == NULL)
792 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700793 GATT_TRACE_ERROR("gatts_process_primary_service_req failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800794 reason = GATT_NO_RESOURCES;
795 }
796 else
797 {
798 memset(p_msg, 0, msg_len);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700799 reason = gatt_build_primary_service_rsp (p_msg, p_tcb, op_code, s_hdl, e_hdl, p_data, value);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800800 }
801 }
802 }
803 else
804 {
805 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
806 {
807 reason = GATT_UNSUPPORT_GRP_TYPE;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700808 GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x", uuid.uu.uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800809 }
810 else
811 {
812 /* we do not support ReadByTypeValue with any non-primamry_service type */
813 reason = GATT_NOT_FOUND;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700814 GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x", uuid.uu.uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800815 }
816 }
817 }
818
819 if (reason != GATT_SUCCESS)
820 {
821 if (p_msg) GKI_freebuf(p_msg);
822 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
823 }
824 else
825 attp_send_sr_msg(p_tcb, p_msg);
826
827}
828
829/*******************************************************************************
830**
831** Function gatts_process_find_info
832**
833** Description process find information request, for discover character
834** descriptors.
835**
836** Returns void
837**
838*******************************************************************************/
839static void gatts_process_find_info(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
840{
841 UINT8 reason = GATT_INVALID_PDU, *p;
842 UINT16 s_hdl = 0, e_hdl = 0, buf_len;
843 BT_HDR *p_msg = NULL;
844 tGATT_SR_REG *p_rcb;
845 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
846 tGATT_SRV_LIST_ELEM *p_srv=NULL;
847
848 reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, &e_hdl);
849
850 if (reason == GATT_SUCCESS)
851 {
852 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
853
854 if ((p_msg = (BT_HDR *)GKI_getbuf(buf_len)) == NULL)
855 {
856 reason = GATT_NO_RESOURCES;
857 }
858 else
859 {
860 reason = GATT_NOT_FOUND;
861
862 memset(p_msg, 0, buf_len);
863 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
864 *p ++ = op_code + 1;
865 p_msg->len = 2;
866
867 buf_len = p_tcb->payload_size - 2;
868
869 p_srv = p_list->p_first;
870
871 while (p_srv)
872 {
873 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
874
875 if (p_rcb->in_use &&
876 !(p_rcb->s_hdl > e_hdl ||
877 p_rcb->e_hdl < s_hdl))
878 {
879 reason = gatt_build_find_info_rsp(p_rcb, p_msg, &buf_len, s_hdl, e_hdl);
880 if (reason == GATT_NO_RESOURCES)
881 {
882 reason = GATT_SUCCESS;
883 break;
884 }
885 }
886 p_srv = p_srv->p_next;
887 }
888 *p = (UINT8)p_msg->offset;
889
890 p_msg->offset = L2CAP_MIN_OFFSET;
891 }
892 }
893
894 if (reason != GATT_SUCCESS)
895 {
896 if (p_msg) GKI_freebuf(p_msg);
897 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
898 }
899 else
900 attp_send_sr_msg(p_tcb, p_msg);
901
902}
903
904/*******************************************************************************
905**
906** Function gatts_process_mtu_req
907**
908** Description This function is called to process excahnge MTU request.
909** Only used on LE.
910**
911** Returns void
912**
913*******************************************************************************/
914static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data)
915{
916 UINT16 mtu = 0;
917 UINT8 *p = p_data, i;
918 BT_HDR *p_buf;
919 UINT16 conn_id;
920
The Android Open Source Project5738f832012-12-12 16:00:35 -0800921 /* BR/EDR conenction, send error response */
922 if (p_tcb->att_lcid != L2CAP_ATT_CID)
923 {
924 gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, FALSE);
925 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700926 else if (len < GATT_MTU_REQ_MIN_LEN)
927 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700928 GATT_TRACE_ERROR("invalid MTU request PDU received.");
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700929 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, FALSE);
930 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931 else
932 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700933 STREAM_TO_UINT16 (mtu, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800934 /* mtu must be greater than default MTU which is 23/48 */
Andre Eisenbach4b638692013-06-14 13:22:25 -0700935 if (mtu < GATT_DEF_BLE_MTU_SIZE)
936 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
937 else if (mtu > GATT_MAX_MTU_SIZE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938 p_tcb->payload_size = GATT_MAX_MTU_SIZE;
Andre Eisenbach4b638692013-06-14 13:22:25 -0700939 else
940 p_tcb->payload_size = mtu;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700942 GATT_TRACE_ERROR("MTU request PDU with MTU size %d", p_tcb->payload_size);
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700943
The Android Open Source Project5738f832012-12-12 16:00:35 -0800944 if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL)
945 {
946 attp_send_sr_msg (p_tcb, p_buf);
947
948 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
949 /* of 0, as no response is allowed from applcations */
950
951 for (i = 0; i < GATT_MAX_APPS; i ++)
952 {
953 if (gatt_cb.cl_rcb[i].in_use )
954 {
955 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
956 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
957 (tGATTS_DATA *)&p_tcb->payload_size);
958 }
959 }
960
961 }
962 }
963}
964
965/*******************************************************************************
966**
967** Function gatts_process_read_by_type_req
968**
969** Description process Read By type request.
970** This PDU can be used to perform:
971** - read characteristic value
972** - read characteristic descriptor value
973** - discover characteristic
974** - discover characteristic by UUID
975** - relationship discovery
976**
977** Returns void
978**
979*******************************************************************************/
980void gatts_process_read_by_type_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
981{
982 tBT_UUID uuid;
983 tGATT_SR_REG *p_rcb;
984 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET),
985 buf_len,
986 s_hdl, e_hdl, err_hdl = 0;
987 BT_HDR *p_msg = NULL;
988 tGATT_STATUS reason, ret;
989 UINT8 *p;
990 UINT8 sec_flag, key_size;
991 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
992 tGATT_SRV_LIST_ELEM *p_srv=NULL;
993
994 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
995
996#if GATT_CONFORMANCE_TESTING == TRUE
997 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
998 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700999 GATT_TRACE_DEBUG("Conformance tst: forced err rsp for ReadByType: error status=%d", gatt_cb.err_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001000
1001 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, FALSE);
1002
1003 return;
1004 }
1005#endif
1006
1007 if (reason == GATT_SUCCESS)
1008 {
1009 if ((p_msg = (BT_HDR *)GKI_getbuf(msg_len)) == NULL)
1010 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001011 GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001012
1013 reason = GATT_NO_RESOURCES;
1014 }
1015 else
1016 {
1017 memset(p_msg, 0, msg_len);
1018 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1019
1020 *p ++ = op_code + 1;
1021 /* reserve length byte */
1022 p_msg->len = 2;
1023 buf_len = p_tcb->payload_size - 2;
1024
1025 reason = GATT_NOT_FOUND;
1026
1027 p_srv = p_list->p_first;
1028
1029 while (p_srv)
1030 {
1031 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
1032
1033 if (p_rcb->in_use &&
1034 !(p_rcb->s_hdl > e_hdl ||
1035 p_rcb->e_hdl < s_hdl))
1036 {
1037 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001038 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001039 &sec_flag,
1040 &key_size);
1041
1042 ret = gatts_db_read_attr_value_by_type(p_tcb,
1043 p_rcb->p_db,
1044 op_code,
1045 p_msg,
1046 s_hdl,
1047 e_hdl,
1048 uuid,
1049 &buf_len,
1050 sec_flag,
1051 key_size,
1052 0,
1053 &err_hdl);
1054 if (ret != GATT_NOT_FOUND)
1055 {
1056 reason = ret;
1057
1058 if (ret == GATT_NO_RESOURCES)
1059 reason = GATT_SUCCESS;
1060 }
1061 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND)
1062 {
1063 s_hdl = err_hdl;
1064 break;
1065 }
1066 }
1067 p_srv = p_srv->p_next;
1068 }
1069 *p = (UINT8)p_msg->offset;
1070 p_msg->offset = L2CAP_MIN_OFFSET;
1071 }
1072 }
1073 if (reason != GATT_SUCCESS)
1074 {
1075 if (p_msg) GKI_freebuf(p_msg);
1076
1077 /* in theroy BUSY is not possible(should already been checked), protected check */
1078 if (reason != GATT_PENDING && reason != GATT_BUSY)
1079 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
1080 }
1081 else
1082 attp_send_sr_msg(p_tcb, p_msg);
1083
1084}
1085
1086/*******************************************************************************
1087**
1088** Function gatts_process_write_req
1089**
1090** Description This function is called to process the write request
1091** from client.
1092**
1093** Returns void
1094**
1095*******************************************************************************/
1096void gatts_process_write_req (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
1097 UINT8 op_code, UINT16 len, UINT8 *p_data)
1098{
1099 tGATTS_DATA sr_data;
1100 UINT32 trans_id;
1101 tGATT_STATUS status;
1102 UINT8 sec_flag, key_size, *p = p_data;
1103 tGATT_SR_REG *p_sreg;
1104 UINT16 conn_id;
1105
1106 memset(&sr_data, 0, sizeof(tGATTS_DATA));
1107
1108 switch (op_code)
1109 {
1110 case GATT_REQ_PREPARE_WRITE:
1111 sr_data.write_req.is_prep = TRUE;
1112 STREAM_TO_UINT16(sr_data.write_req.offset, p);
1113 len -= 2;
1114 /* fall through */
1115 case GATT_SIGN_CMD_WRITE:
1116 if (op_code == GATT_SIGN_CMD_WRITE)
1117 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001118 GATT_TRACE_DEBUG("Write CMD with data sigining" );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001119 len -= GATT_AUTH_SIGN_LEN;
1120 }
1121 /* fall through */
1122 case GATT_CMD_WRITE:
1123 case GATT_REQ_WRITE:
1124 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
1125 sr_data.write_req.need_rsp = TRUE;
1126 sr_data.write_req.handle = handle;
1127 sr_data.write_req.len = len;
1128 memcpy (sr_data.write_req.value, p, len);
1129 break;
1130 }
1131
1132 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001133 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001134 &sec_flag,
1135 &key_size);
1136
1137 status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
1138 op_code,
1139 handle,
1140 sr_data.write_req.offset,
1141 p,
1142 len,
1143 sec_flag,
1144 key_size);
1145
1146 if (status == GATT_SUCCESS)
1147 {
1148 if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0)
1149 {
1150 p_sreg = &gatt_cb.sr_reg[i_rcb];
1151 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
1152 gatt_sr_send_req_callback(conn_id,
1153 trans_id,
1154 GATTS_REQ_TYPE_WRITE,
1155 &sr_data);
1156
1157 status = GATT_PENDING;
1158 }
1159 else
1160 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001161 GATT_TRACE_ERROR("max pending command, send error");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001162 status = GATT_BUSY; /* max pending command, application error */
1163 }
1164 }
1165
1166 /* in theroy BUSY is not possible(should already been checked), protected check */
1167 if (status != GATT_PENDING && status != GATT_BUSY &&
1168 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE))
1169 {
1170 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1171 }
1172 return;
1173}
1174
1175/*******************************************************************************
1176**
1177** Function gatts_process_read_req
1178**
1179** Description This function is called to process the read request
1180** from client.
1181**
1182** Returns void
1183**
1184*******************************************************************************/
1185static void gatts_process_read_req(tGATT_TCB *p_tcb, tGATT_SR_REG *p_rcb, UINT8 op_code,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001186 UINT16 handle, UINT16 len, UINT8 *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001187{
1188 UINT16 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
1189 tGATT_STATUS reason;
1190 BT_HDR *p_msg = NULL;
1191 UINT8 sec_flag, key_size, *p;
1192 UINT16 offset = 0, value_len = 0;
1193
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001194 UNUSED (len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001195 if ((p_msg = (BT_HDR *)GKI_getbuf(buf_len)) == NULL)
1196 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001197 GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001198
1199 reason = GATT_NO_RESOURCES;
1200 }
1201 else
1202 {
1203 if (op_code == GATT_REQ_READ_BLOB)
1204 STREAM_TO_UINT16(offset, p_data);
1205
1206 memset(p_msg, 0, buf_len);
1207 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1208 *p ++ = op_code + 1;
1209 p_msg->len = 1;
1210 buf_len = p_tcb->payload_size - 1;
1211
1212 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001213 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001214 &sec_flag,
1215 &key_size);
1216
1217 reason = gatts_read_attr_value_by_handle(p_tcb,
1218 p_rcb->p_db,
1219 op_code,
1220 handle,
1221 offset,
1222 p,
1223 &value_len,
1224 buf_len,
1225 sec_flag,
1226 key_size,
1227 0);
1228
1229 p_msg->len += value_len;
1230 }
1231
1232 if (reason != GATT_SUCCESS)
1233 {
1234 if (p_msg) GKI_freebuf(p_msg);
1235
1236 /* in theroy BUSY is not possible(should already been checked), protected check */
1237 if (reason != GATT_PENDING && reason != GATT_BUSY)
1238 gatt_send_error_rsp (p_tcb, reason, op_code, handle, FALSE);
1239 }
1240 else
1241 attp_send_sr_msg(p_tcb, p_msg);
1242
1243}
1244
1245/*******************************************************************************
1246**
1247** Function gatts_process_attribute_req
1248**
1249** Description This function is called to process the per attribute handle request
1250** from client.
1251**
1252** Returns void
1253**
1254*******************************************************************************/
1255void gatts_process_attribute_req (tGATT_TCB *p_tcb, UINT8 op_code,
1256 UINT16 len, UINT8 *p_data)
1257{
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001258 UINT16 handle = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001259 UINT8 *p = p_data, i;
1260 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1261 tGATT_STATUS status = GATT_INVALID_HANDLE;
1262 tGATT_ATTR16 *p_attr;
1263
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001264 if (len < 2)
1265 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001266 GATT_TRACE_ERROR("Illegal PDU length, discard request");
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001267 status = GATT_INVALID_PDU;
1268 }
1269 else
1270 {
1271 STREAM_TO_UINT16(handle, p);
1272 len -= 2;
1273 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001274
1275#if GATT_CONFORMANCE_TESTING == TRUE
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001276 gatt_cb.handle = handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001277 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
1278 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001279 GATT_TRACE_DEBUG("Conformance tst: forced err rsp: error status=%d", gatt_cb.err_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001280
1281 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
1282
1283 return;
1284 }
1285#endif
1286
1287 if (GATT_HANDLE_IS_VALID(handle))
1288 {
1289 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1290 {
1291 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1292 {
1293 p_attr = (tGATT_ATTR16 *)p_rcb->p_db->p_attr_list;
1294
1295 while (p_attr)
1296 {
1297 if (p_attr->handle == handle)
1298 {
1299 switch (op_code)
1300 {
1301 case GATT_REQ_READ: /* read char/char descriptor value */
1302 case GATT_REQ_READ_BLOB:
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001303 gatts_process_read_req(p_tcb, p_rcb, op_code, handle, len, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001304 break;
1305
1306 case GATT_REQ_WRITE: /* write char/char descriptor value */
1307 case GATT_CMD_WRITE:
1308 case GATT_SIGN_CMD_WRITE:
1309 case GATT_REQ_PREPARE_WRITE:
1310 gatts_process_write_req(p_tcb, i, handle, op_code, len, p);
1311 break;
1312 default:
1313 break;
1314 }
1315 status = GATT_SUCCESS;
1316 break;
1317 }
1318 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
1319 }
1320 break;
1321 }
1322 }
1323 }
1324
1325 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE)
1326 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1327}
1328
1329/*******************************************************************************
1330**
1331** Function gatts_proc_srv_chg_ind_ack
1332**
1333** Description This function process the service changed indicaiton ACK
1334**
1335** Returns void
1336**
1337*******************************************************************************/
1338static void gatts_proc_srv_chg_ind_ack(tGATT_TCB *p_tcb )
1339{
1340 tGATTS_SRV_CHG_REQ req;
1341 tGATTS_SRV_CHG *p_buf = NULL;
1342
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001343 GATT_TRACE_DEBUG("gatts_proc_srv_chg_ind_ack");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001344
1345 if ((p_buf = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
1346 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001347 GATT_TRACE_DEBUG("NV update set srv chg = FALSE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001348 p_buf->srv_changed = FALSE;
1349 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1350 if (gatt_cb.cb_info.p_srv_chg_callback)
1351 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,&req, NULL);
1352 }
1353}
1354
1355/*******************************************************************************
1356**
1357** Function gatts_chk_pending_ind
1358**
1359** Description This function check any pending indication needs to be sent if
1360** there is a pending indication then sent the indication
1361**
1362** Returns void
1363**
1364*******************************************************************************/
1365static void gatts_chk_pending_ind(tGATT_TCB *p_tcb )
1366{
1367 tGATT_VALUE *p_buf = (tGATT_VALUE *)GKI_getfirst(&p_tcb->pending_ind_q);
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001368 GATT_TRACE_DEBUG("gatts_chk_pending_ind");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001369
1370 if (p_buf )
1371 {
1372 GATTS_HandleValueIndication (p_buf->conn_id,
1373 p_buf->handle,
1374 p_buf->len,
1375 p_buf->value);
1376 GKI_freebuf(GKI_remove_from_queue (&p_tcb->pending_ind_q, p_buf));
1377 }
1378}
1379
1380/*******************************************************************************
1381**
1382** Function gatts_proc_ind_ack
1383**
1384** Description This function process the Indication ack
1385**
1386** Returns TRUE continue to process the indication ack by the aaplication
1387** if the ACk is not a Service Changed Indication Ack
1388**
1389*******************************************************************************/
1390static BOOLEAN gatts_proc_ind_ack(tGATT_TCB *p_tcb, UINT16 ack_handle)
1391{
1392 BOOLEAN continue_processing = TRUE;
1393
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001394 GATT_TRACE_DEBUG ("gatts_proc_ind_ack ack handle=%d", ack_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001395
1396 if (ack_handle == gatt_cb.handle_of_h_r)
1397 {
1398 gatts_proc_srv_chg_ind_ack(p_tcb);
1399 /* there is no need to inform the application since srv chg is handled internally by GATT */
1400 continue_processing = FALSE;
1401 }
1402
1403 gatts_chk_pending_ind(p_tcb);
1404 return continue_processing;
1405}
1406
1407/*******************************************************************************
1408**
1409** Function gatts_process_value_conf
1410**
1411** Description This function is called to process the handle value confirmation.
1412**
1413** Returns void
1414**
1415*******************************************************************************/
1416void gatts_process_value_conf(tGATT_TCB *p_tcb, UINT8 op_code)
1417{
1418 UINT16 handle = p_tcb->indicate_handle;
1419 UINT32 trans_id;
1420 UINT8 i;
1421 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1422 BOOLEAN continue_processing;
1423 UINT16 conn_id;
1424
1425 btu_stop_timer (&p_tcb->conf_timer_ent);
1426 if (GATT_HANDLE_IS_VALID(handle))
1427 {
1428 p_tcb->indicate_handle = 0;
1429 continue_processing = gatts_proc_ind_ack(p_tcb, handle);
1430
1431 if (continue_processing)
1432 {
1433 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1434 {
1435 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1436 {
1437 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
1438 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_rcb->gatt_if);
1439 gatt_sr_send_req_callback(conn_id,
1440 trans_id, GATTS_REQ_TYPE_CONF, (tGATTS_DATA *)&handle);
1441 }
1442 }
1443 }
1444 }
1445 else
1446 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001447 GATT_TRACE_ERROR("unexpected handle value confirmation");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001448 }
1449}
1450
1451/*******************************************************************************
1452**
1453** Function gatt_server_handle_client_req
1454**
1455** Description This function is called to handle the client requests to
1456** server.
1457**
1458**
1459** Returns void
1460**
1461*******************************************************************************/
1462void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
1463 UINT16 len, UINT8 *p_data)
1464{
1465 /* there is pending command, discard this one */
1466 if (!gatt_sr_cmd_empty(p_tcb) && op_code != GATT_HANDLE_VALUE_CONF)
1467 return;
1468
1469 /* the size of the message may not be bigger than the local max PDU size*/
1470 /* The message has to be smaller than the agreed MTU, len does not include op code */
1471 if (len >= p_tcb->payload_size)
1472 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001473 GATT_TRACE_ERROR("server receive invalid PDU size:%d pdu size:%d", len + 1, p_tcb->payload_size );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001474 /* for invalid request expecting response, send it now */
1475 if (op_code != GATT_CMD_WRITE &&
1476 op_code != GATT_SIGN_CMD_WRITE &&
1477 op_code != GATT_HANDLE_VALUE_CONF)
1478 {
1479 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, op_code, 0, FALSE);
1480 }
1481 /* otherwise, ignore the pkt */
1482 }
1483 else
1484 {
1485 switch (op_code)
1486 {
1487 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1488 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1489 gatts_process_primary_service_req (p_tcb, op_code, len, p_data);
1490 break;
1491
1492 case GATT_REQ_FIND_INFO:/* discover char descrptor */
1493 gatts_process_find_info(p_tcb, op_code, len, p_data);
1494 break;
1495
1496 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor value */
1497 /* discover characteristic, discover char by UUID */
1498 gatts_process_read_by_type_req(p_tcb, op_code, len, p_data);
1499 break;
1500
1501
1502 case GATT_REQ_READ: /* read char/char descriptor value */
1503 case GATT_REQ_READ_BLOB:
1504 case GATT_REQ_WRITE: /* write char/char descriptor value */
1505 case GATT_CMD_WRITE:
1506 case GATT_SIGN_CMD_WRITE:
1507 case GATT_REQ_PREPARE_WRITE:
1508 gatts_process_attribute_req (p_tcb, op_code, len, p_data);
1509 break;
1510
1511 case GATT_HANDLE_VALUE_CONF:
1512 gatts_process_value_conf (p_tcb, op_code);
1513 break;
1514
1515 case GATT_REQ_MTU:
1516 gatts_process_mtu_req (p_tcb, len, p_data);
1517 break;
1518
1519 case GATT_REQ_EXEC_WRITE:
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001520 gatt_process_exec_write_req (p_tcb, op_code, len, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001521 break;
1522
1523 case GATT_REQ_READ_MULTI:
1524 gatt_process_read_multi_req (p_tcb, op_code, len, p_data);
1525 break;
1526
1527 default:
1528 break;
1529 }
1530 }
1531}
1532
1533#endif /* BLE_INCLUDED */