blob: 5c8494130b1c186917af8da28a8c2bdd4a3fa2ac [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"
Priti Aghera636d6712014-12-18 13:55:48 -080032#include "l2c_int.h"
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
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700109 while (!fixed_queue_is_empty(p_tcb->sr_cmd.multi_rsp_q))
110 GKI_freebuf(fixed_queue_try_dequeue(p_tcb->sr_cmd.multi_rsp_q));
111 fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800112 memset( &p_tcb->sr_cmd, 0, sizeof(tGATT_SR_CMD));
113}
114
115/*******************************************************************************
116**
117** Function process_read_multi_rsp
118**
119** Description This function check the read multiple response.
120**
121** Returns BOOLEAN if all replies have been received
122**
123*******************************************************************************/
124static BOOLEAN process_read_multi_rsp (tGATT_SR_CMD *p_cmd, tGATT_STATUS status,
125 tGATTS_RSP *p_msg, UINT16 mtu)
126{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800127 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));
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700142 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800143
144 p_cmd->status = status;
145 if (status == GATT_SUCCESS)
146 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700147 GATT_TRACE_DEBUG("Multi read count=%d num_hdls=%d",
148 fixed_queue_length(&p_cmd->multi_rsp_q),
149 p_cmd->multi_req.num_handles);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800150 /* Wait till we get all the responses */
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700151 if (fixed_queue_length(p_cmd->multi_rsp_q) == p_cmd->multi_req.num_handles)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800152 {
153 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
154 if ((p_buf = (BT_HDR *)GKI_getbuf(len)) == NULL)
155 {
156 p_cmd->status = GATT_INSUF_RESOURCE;
157 return(TRUE);
158 }
159
160 memset(p_buf, 0, len);
161 p_buf->offset = L2CAP_MIN_OFFSET;
162 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
163
164 /* First byte in the response is the opcode */
165 *p++ = GATT_RSP_READ_MULTI;
166 p_buf->len = 1;
167
168 /* Now walk through the buffers puting the data into the response in order */
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700169 list_t *list = fixed_queue_get_list(p_cmd->multi_rsp_q);
170 const list_node_t *node;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800171 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++)
172 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700173 tGATTS_RSP *p_rsp = NULL;
174
175 if (ii == 0)
176 node = list_begin(list);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800177 else
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700178 node = list_next(node);
179 if (node != list_end(list))
180 p_rsp = (tGATTS_RSP *)list_node(node);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800181
182 if (p_rsp != NULL)
183 {
184
185 total_len = (p_buf->len + p_rsp->attr_value.len);
186
187 if (total_len > mtu)
188 {
189 /* just send the partial response for the overflow case */
190 len = p_rsp->attr_value.len - (total_len - mtu);
191 is_overflow = TRUE;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700192 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 -0800193 }
194 else
195 {
196 len = p_rsp->attr_value.len;
197 }
198
199 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii])
200 {
201 memcpy (p, p_rsp->attr_value.value, len);
202 if (!is_overflow)
203 p += len;
204 p_buf->len += len;
205 }
206 else
207 {
208 p_cmd->status = GATT_NOT_FOUND;
209 break;
210 }
211
212 if (is_overflow)
213 break;
214
215 }
216 else
217 {
218 p_cmd->status = GATT_NOT_FOUND;
219 break;
220 }
221
222 } /* loop through all handles*/
223
224
225 /* Sanity check on the buffer length */
226 if (p_buf->len == 0)
227 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700228 GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229 p_cmd->status = GATT_NOT_FOUND;
230 GKI_freebuf (p_buf);
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700231 GATT_TRACE_DEBUG(" GKI_freebuf (p_buf)");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800232 }
233 else if (p_cmd->p_rsp_msg != NULL)
234 {
235 GKI_freebuf (p_buf);
236 }
237 else
238 {
239 p_cmd->p_rsp_msg = p_buf;
240 }
241
242 return(TRUE);
243 }
244 }
245 else /* any handle read exception occurs, return error */
246 {
247 return(TRUE);
248 }
249
250 /* If here, still waiting */
251 return(FALSE);
252}
253
254/*******************************************************************************
255**
256** Function gatt_sr_process_app_rsp
257**
258** Description This function checks whether the response message from application
259** match any pending request or not.
260**
261** Returns void
262**
263*******************************************************************************/
264tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if,
265 UINT32 trans_id, UINT8 op_code,
266 tGATT_STATUS status, tGATTS_RSP *p_msg)
267{
268 tGATT_STATUS ret_code = GATT_SUCCESS;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800269 UNUSED(trans_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800270
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700271 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp gatt_if=%d", gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272
273 gatt_sr_update_cback_cnt(p_tcb, gatt_if, FALSE, FALSE);
274
275 if (op_code == GATT_REQ_READ_MULTI)
276 {
277 /* If no error and still waiting, just return */
278 if (!process_read_multi_rsp (&p_tcb->sr_cmd, status, p_msg, p_tcb->payload_size))
279 return(GATT_SUCCESS);
280 }
281 else
282 {
283 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
284 gatt_sr_update_prep_cnt(p_tcb, gatt_if, TRUE, FALSE);
285
286 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
287 gatt_sr_reset_cback_cnt(p_tcb);
288
289 p_tcb->sr_cmd.status = status;
290
291 if (gatt_sr_is_cback_cnt_zero(p_tcb)
292 && status == GATT_SUCCESS)
293 {
294 if (p_tcb->sr_cmd.p_rsp_msg == NULL)
295 {
296 p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg (p_tcb, (UINT8)(op_code + 1), (tGATT_SR_MSG *)p_msg);
297 }
298 else
299 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700300 GATT_TRACE_ERROR("Exception!!! already has respond message");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800301 }
302 }
303 }
304 if (gatt_sr_is_cback_cnt_zero(p_tcb))
305 {
306 if ( (p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg) )
307 {
308 ret_code = attp_send_sr_msg (p_tcb, p_tcb->sr_cmd.p_rsp_msg);
309 p_tcb->sr_cmd.p_rsp_msg = NULL;
310 }
311 else
312 {
313 ret_code = gatt_send_error_rsp (p_tcb, status, op_code, p_tcb->sr_cmd.handle, FALSE);
314 }
315
316 gatt_dequeue_sr_cmd(p_tcb);
317 }
318
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700319 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp ret_code=%d", ret_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800320
321 return ret_code;
322}
323
324/*******************************************************************************
325**
326** Function gatt_process_exec_write_req
327**
328** Description This function is called to process the execute write request
329** from client.
330**
331** Returns void
332**
333*******************************************************************************/
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700334void 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 -0800335{
336 UINT8 *p = p_data, flag, i = 0;
337 UINT32 trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800338 tGATT_IF gatt_if;
339 UINT16 conn_id;
340
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700341 UNUSED(len);
342
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800343#if GATT_CONFORMANCE_TESTING == TRUE
344 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
345 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700346 GATT_TRACE_DEBUG("Conformance tst: forced err rspv for Execute Write: error status=%d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700347 gatt_cb.err_status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800348
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700349 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 -0800350
351 return;
352 }
353#endif
354
The Android Open Source Project5738f832012-12-12 16:00:35 -0800355 STREAM_TO_UINT8(flag, p);
356
357 /* mask the flag */
358 flag &= GATT_PREP_WRITE_EXEC;
359
360
361 /* no prep write is queued */
362 if (!gatt_sr_is_prep_cnt_zero(p_tcb))
363 {
364 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0);
365 gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb);
366
367 for (i=0; i<GATT_MAX_APPS; i++)
368 {
369 if (p_tcb->prep_cnt[i])
370 {
371 gatt_if = (tGATT_IF) (i+1);
372 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
373 gatt_sr_send_req_callback(conn_id,
374 trans_id,
375 GATTS_REQ_TYPE_WRITE_EXEC,
376 (tGATTS_DATA *)&flag);
377 p_tcb->prep_cnt[i]= 0;
378 }
379 }
380 }
381 else /* nothing needs to be executed , send response now */
382 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700383 GATT_TRACE_ERROR("gatt_process_exec_write_req: no prepare write pending");
Mudumba Ananthcf310332014-07-04 03:45:11 -0700384 gatt_send_error_rsp(p_tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, FALSE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800385 }
386}
387
388/*******************************************************************************
389**
390** Function gatt_process_read_multi_req
391**
392** Description This function is called to process the read multiple request
393** from client.
394**
395** Returns void
396**
397*******************************************************************************/
398void gatt_process_read_multi_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
399{
400 UINT32 trans_id;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700401 UINT16 handle = 0, ll = len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800402 UINT8 *p = p_data, i_rcb;
403 tGATT_STATUS err = GATT_SUCCESS;
404 UINT8 sec_flag, key_size;
405 tGATTS_RSP *p_msg;
406
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700407 GATT_TRACE_DEBUG("gatt_process_read_multi_req" );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800408 p_tcb->sr_cmd.multi_req.num_handles = 0;
409
410 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700411 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800412 &sec_flag,
413 &key_size);
414
415#if GATT_CONFORMANCE_TESTING == TRUE
416 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
417 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700418 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 -0800419
420 STREAM_TO_UINT16(handle, p);
421
422 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
423
424 return;
425 }
426#endif
427
428 while (ll >= 2 && p_tcb->sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES)
429 {
430 STREAM_TO_UINT16(handle, p);
431
432 if ((i_rcb = gatt_sr_find_i_rcb_by_handle(handle)) < GATT_MAX_SR_PROFILES)
433 {
434 p_tcb->sr_cmd.multi_req.handles[p_tcb->sr_cmd.multi_req.num_handles++] = handle;
435
436 /* check read permission */
437 if ((err = gatts_read_attr_perm_check( gatt_cb.sr_reg[i_rcb].p_db,
438 FALSE,
439 handle,
440 sec_flag,
441 key_size))
442 != GATT_SUCCESS)
443 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700444 GATT_TRACE_DEBUG("read permission denied : 0x%02x", err);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800445 break;
446 }
447 }
448 else
449 {
450 /* invalid handle */
451 err = GATT_INVALID_HANDLE;
452 break;
453 }
454 ll -= 2;
455 }
456
457 if (ll != 0)
458 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700459 GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800460 }
461
462 if (p_tcb->sr_cmd.multi_req.num_handles == 0)
463 err = GATT_INVALID_HANDLE;
464
465 if (err == GATT_SUCCESS)
466 {
467 if ((trans_id = gatt_sr_enqueue_cmd (p_tcb, op_code, p_tcb->sr_cmd.multi_req.handles[0])) != 0)
468 {
469 gatt_sr_reset_cback_cnt(p_tcb); /* read multiple use multi_rsp_q's count*/
470
471 for (ll = 0; ll < p_tcb->sr_cmd.multi_req.num_handles; ll ++)
472 {
473 if ((p_msg = (tGATTS_RSP *)GKI_getbuf(sizeof(tGATTS_RSP))) != NULL)
474 {
475 memset(p_msg, 0, sizeof(tGATTS_RSP))
476 ;
477 handle = p_tcb->sr_cmd.multi_req.handles[ll];
478 i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
479
480 p_msg->attr_value.handle = handle;
481 err = gatts_read_attr_value_by_handle(p_tcb,
482 gatt_cb.sr_reg[i_rcb].p_db,
483 op_code,
484 handle,
485 0,
486 p_msg->attr_value.value,
487 &p_msg->attr_value.len,
488 GATT_MAX_ATTR_LEN,
489 sec_flag,
490 key_size,
491 trans_id);
492
493 if (err == GATT_SUCCESS)
494 {
495 gatt_sr_process_app_rsp(p_tcb, gatt_cb.sr_reg[i_rcb].gatt_if ,trans_id, op_code, GATT_SUCCESS, p_msg);
496 }
497 /* either not using or done using the buffer, release it now */
498 GKI_freebuf(p_msg);
499 }
500 else
501 {
502 err = GATT_NO_RESOURCES;
503 gatt_dequeue_sr_cmd(p_tcb);
504 break;
505 }
506 }
507 }
508 else
509 err = GATT_NO_RESOURCES;
510 }
511
512 /* in theroy BUSY is not possible(should already been checked), protected check */
513 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
514 gatt_send_error_rsp(p_tcb, err, op_code, handle, FALSE);
515}
516
517/*******************************************************************************
518**
519** Function gatt_build_primary_service_rsp
520**
521** Description Primamry service request processed internally. Theretically
522** only deal with ReadByTypeVAlue and ReadByGroupType.
523**
524** Returns void
525**
526*******************************************************************************/
527static tGATT_STATUS gatt_build_primary_service_rsp (BT_HDR *p_msg, tGATT_TCB *p_tcb,
528 UINT8 op_code, UINT16 s_hdl,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700529 UINT16 e_hdl, UINT8 *p_data, tBT_UUID value)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800530{
531 tGATT_STATUS status = GATT_NOT_FOUND;
532 UINT8 handle_len =4, *p ;
533 tGATT_SR_REG *p_rcb;
534 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
535 tGATT_SRV_LIST_ELEM *p_srv=NULL;
536 tBT_UUID *p_uuid;
537
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700538 UNUSED(p_data);
539
The Android Open Source Project5738f832012-12-12 16:00:35 -0800540 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
541
542 p_srv = p_list->p_first;
543
544 while (p_srv)
545 {
546 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
547
548 if (p_rcb->in_use &&
549 p_rcb->s_hdl >= s_hdl &&
550 p_rcb->s_hdl <= e_hdl &&
551 p_rcb->type == GATT_UUID_PRI_SERVICE)
552 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700553 if ((p_uuid = gatts_get_service_uuid (p_rcb->p_db)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800554 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800555 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700556 handle_len = 4 + p_uuid->len;
557
558 /* get the length byte in the repsonse */
559 if (p_msg->offset ==0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700561 *p ++ = op_code + 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800562 p_msg->len ++;
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700563 p_msg->offset = handle_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800564
565 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700566 {
567 *p ++ = (UINT8)p_msg->offset; /* length byte */
568 p_msg->len ++;
569 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800570 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700571
572 if (p_msg->len + p_msg->offset <= p_tcb->payload_size &&
573 handle_len == p_msg->offset)
574 {
575 if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
576 gatt_uuid_compare(value, *p_uuid))
577 {
578 UINT16_TO_STREAM(p, p_rcb->s_hdl);
579
580 if (p_list->p_last_primary == p_srv &&
581 p_list->p_last_primary == p_list->p_last)
582 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700583 GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute");
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700584 UINT16_TO_STREAM(p, 0xFFFF); /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
585 }
586 else
587 {
588 UINT16_TO_STREAM(p, p_rcb->e_hdl);
589 }
590
591 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
592 gatt_build_uuid_to_stream(&p, *p_uuid);
593
594 status = GATT_SUCCESS;
595 p_msg->len += p_msg->offset;
596 }
597 }
598 else
599 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800601 }
602 p_srv = p_srv->p_next;
603 }
604 p_msg->offset = L2CAP_MIN_OFFSET;
605
606 return status;
607}
608
609/*******************************************************************************
610**
611** Function gatt_build_find_info_rsp
612**
613** Description fill the find information response information in the given
614** buffer.
615**
616** Returns TRUE: if data filled sucessfully.
617** FALSE: packet full, or format mismatch.
618**
619*******************************************************************************/
620static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SR_REG *p_rcb, BT_HDR *p_msg, UINT16 *p_len,
621 UINT16 s_hdl, UINT16 e_hdl)
622{
623 tGATT_STATUS status = GATT_NOT_FOUND;
624 UINT8 *p;
625 UINT16 len = *p_len;
626 tGATT_ATTR16 *p_attr = NULL;
627 UINT8 info_pair_len[2] = {4, 18};
628
629 if (!p_rcb->p_db || !p_rcb->p_db->p_attr_list)
630 return status;
631
632 /* check the attribute database */
633 p_attr = (tGATT_ATTR16 *) p_rcb->p_db->p_attr_list;
634
635 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
636
637 while (p_attr)
638 {
639 if (p_attr->handle > e_hdl)
640 {
641 break;
642 }
643
644 if (p_attr->handle >= s_hdl)
645 {
646 if (p_msg->offset == 0)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700647 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 -0800648
649 if (len >= info_pair_len[p_msg->offset - 1])
650 {
651 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16)
652 {
653 UINT16_TO_STREAM(p, p_attr->handle);
654 UINT16_TO_STREAM(p, p_attr->uuid);
655 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700656 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 -0800657 {
658 UINT16_TO_STREAM(p, p_attr->handle);
659 ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *) p_attr)->uuid, LEN_UUID_128);
660 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700661 else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32)
662 {
663 UINT16_TO_STREAM(p, p_attr->handle);
664 gatt_convert_uuid32_to_uuid128(p, ((tGATT_ATTR32 *) p_attr)->uuid);
665 p += LEN_UUID_128;
666 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800667 else
668 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700669 GATT_TRACE_ERROR("format mismatch");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800670 status = GATT_NO_RESOURCES;
671 break;
672 /* format mismatch */
673 }
674 p_msg->len += info_pair_len[p_msg->offset - 1];
675 len -= info_pair_len[p_msg->offset - 1];
676 status = GATT_SUCCESS;
677
678 }
679 else
680 {
681 status = GATT_NO_RESOURCES;
682 break;
683 }
684 }
685 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
686 }
687
688 *p_len = len;
689 return status;
690}
691
692/*******************************************************************************
693**
694** Function gatts_internal_read_by_type_req
695**
696** Description check to see if the ReadByType request can be handled internally.
697**
698** Returns void
699**
700*******************************************************************************/
701static tGATT_STATUS gatts_validate_packet_format(UINT8 op_code, UINT16 *p_len,
702 UINT8 **p_data, tBT_UUID *p_uuid_filter,
703 UINT16 *p_s_hdl, UINT16 *p_e_hdl)
704{
705 tGATT_STATUS reason = GATT_SUCCESS;
706 UINT16 uuid_len, s_hdl = 0, e_hdl = 0;
707 UINT16 len = *p_len;
708 UINT8 *p = *p_data;
709
710 if (len >= 4)
711 {
712 /* obtain starting handle, and ending handle */
713 STREAM_TO_UINT16(s_hdl, p);
714 STREAM_TO_UINT16(e_hdl, p);
715 len -= 4;
716
717 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || !GATT_HANDLE_IS_VALID(e_hdl))
718 {
719 reason = GATT_INVALID_HANDLE;
720 }
721 /* for these PDUs, uuid filter must present */
722 else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
723 op_code == GATT_REQ_FIND_TYPE_VALUE ||
724 op_code == GATT_REQ_READ_BY_TYPE)
725 {
726 if (len >= 2 && p_uuid_filter != NULL)
727 {
728 uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
729
730 /* parse uuid now */
731 if (gatt_parse_uuid_from_cmd (p_uuid_filter, uuid_len, &p) == FALSE ||
732 p_uuid_filter->len == 0)
733 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700734 GATT_TRACE_DEBUG("UUID filter does not exsit");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800735 reason = GATT_INVALID_PDU;
736 }
737 else
738 len -= p_uuid_filter->len;
739 }
740 else
741 reason = GATT_INVALID_PDU;
742 }
743 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700744 else
745 reason = GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800746
747 *p_data = p;
748 *p_len = len;
749 *p_s_hdl = s_hdl;
750 *p_e_hdl = e_hdl;
751
752 return reason;
753}
754
755/*******************************************************************************
756**
757** Function gatts_process_primary_service_req
758**
759** Description process ReadByGroupType/ReadByTypeValue request, for discover
760** all primary services or discover primary service by UUID request.
761**
762** Returns void
763**
764*******************************************************************************/
765void gatts_process_primary_service_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
766{
767 UINT8 reason = GATT_INVALID_PDU;
768 UINT16 s_hdl = 0, e_hdl = 0;
769 tBT_UUID uuid, value, primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
770 BT_HDR *p_msg = NULL;
771 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
772
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700773 memset (&value, 0, sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
775
776 if (reason == GATT_SUCCESS)
777 {
778 if (gatt_uuid_compare(uuid, primary_service))
779 {
780 if (op_code == GATT_REQ_FIND_TYPE_VALUE)
781 {
782 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == FALSE)
783 reason = GATT_INVALID_PDU;
784 }
785
786 if (reason == GATT_SUCCESS)
787 {
788 if ((p_msg = (BT_HDR *)GKI_getbuf(msg_len)) == NULL)
789 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700790 GATT_TRACE_ERROR("gatts_process_primary_service_req failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800791 reason = GATT_NO_RESOURCES;
792 }
793 else
794 {
795 memset(p_msg, 0, msg_len);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700796 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 -0800797 }
798 }
799 }
800 else
801 {
802 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
803 {
804 reason = GATT_UNSUPPORT_GRP_TYPE;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700805 GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x", uuid.uu.uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800806 }
807 else
808 {
809 /* we do not support ReadByTypeValue with any non-primamry_service type */
810 reason = GATT_NOT_FOUND;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700811 GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x", uuid.uu.uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800812 }
813 }
814 }
815
816 if (reason != GATT_SUCCESS)
817 {
818 if (p_msg) GKI_freebuf(p_msg);
819 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
820 }
821 else
822 attp_send_sr_msg(p_tcb, p_msg);
823
824}
825
826/*******************************************************************************
827**
828** Function gatts_process_find_info
829**
830** Description process find information request, for discover character
831** descriptors.
832**
833** Returns void
834**
835*******************************************************************************/
836static void gatts_process_find_info(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
837{
838 UINT8 reason = GATT_INVALID_PDU, *p;
839 UINT16 s_hdl = 0, e_hdl = 0, buf_len;
840 BT_HDR *p_msg = NULL;
841 tGATT_SR_REG *p_rcb;
842 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
843 tGATT_SRV_LIST_ELEM *p_srv=NULL;
844
845 reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, &e_hdl);
846
847 if (reason == GATT_SUCCESS)
848 {
849 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
850
851 if ((p_msg = (BT_HDR *)GKI_getbuf(buf_len)) == NULL)
852 {
853 reason = GATT_NO_RESOURCES;
854 }
855 else
856 {
857 reason = GATT_NOT_FOUND;
858
859 memset(p_msg, 0, buf_len);
860 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
861 *p ++ = op_code + 1;
862 p_msg->len = 2;
863
864 buf_len = p_tcb->payload_size - 2;
865
866 p_srv = p_list->p_first;
867
868 while (p_srv)
869 {
870 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
871
872 if (p_rcb->in_use &&
873 !(p_rcb->s_hdl > e_hdl ||
874 p_rcb->e_hdl < s_hdl))
875 {
876 reason = gatt_build_find_info_rsp(p_rcb, p_msg, &buf_len, s_hdl, e_hdl);
877 if (reason == GATT_NO_RESOURCES)
878 {
879 reason = GATT_SUCCESS;
880 break;
881 }
882 }
883 p_srv = p_srv->p_next;
884 }
885 *p = (UINT8)p_msg->offset;
886
887 p_msg->offset = L2CAP_MIN_OFFSET;
888 }
889 }
890
891 if (reason != GATT_SUCCESS)
892 {
893 if (p_msg) GKI_freebuf(p_msg);
894 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
895 }
896 else
897 attp_send_sr_msg(p_tcb, p_msg);
898
899}
900
901/*******************************************************************************
902**
903** Function gatts_process_mtu_req
904**
905** Description This function is called to process excahnge MTU request.
906** Only used on LE.
907**
908** Returns void
909**
910*******************************************************************************/
911static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data)
912{
913 UINT16 mtu = 0;
914 UINT8 *p = p_data, i;
915 BT_HDR *p_buf;
916 UINT16 conn_id;
917
The Android Open Source Project5738f832012-12-12 16:00:35 -0800918 /* BR/EDR conenction, send error response */
919 if (p_tcb->att_lcid != L2CAP_ATT_CID)
920 {
921 gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, FALSE);
922 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700923 else if (len < GATT_MTU_REQ_MIN_LEN)
924 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700925 GATT_TRACE_ERROR("invalid MTU request PDU received.");
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700926 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, FALSE);
927 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800928 else
929 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700930 STREAM_TO_UINT16 (mtu, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931 /* mtu must be greater than default MTU which is 23/48 */
Andre Eisenbach4b638692013-06-14 13:22:25 -0700932 if (mtu < GATT_DEF_BLE_MTU_SIZE)
933 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
934 else if (mtu > GATT_MAX_MTU_SIZE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800935 p_tcb->payload_size = GATT_MAX_MTU_SIZE;
Andre Eisenbach4b638692013-06-14 13:22:25 -0700936 else
937 p_tcb->payload_size = mtu;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700939 GATT_TRACE_ERROR("MTU request PDU with MTU size %d", p_tcb->payload_size);
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700940
Priti Aghera636d6712014-12-18 13:55:48 -0800941 l2cble_set_fixed_channel_tx_data_length(p_tcb->peer_bda, L2CAP_ATT_CID, p_tcb->payload_size);
942
The Android Open Source Project5738f832012-12-12 16:00:35 -0800943 if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL)
944 {
945 attp_send_sr_msg (p_tcb, p_buf);
946
947 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
948 /* of 0, as no response is allowed from applcations */
949
950 for (i = 0; i < GATT_MAX_APPS; i ++)
951 {
952 if (gatt_cb.cl_rcb[i].in_use )
953 {
954 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
955 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
956 (tGATTS_DATA *)&p_tcb->payload_size);
957 }
958 }
959
960 }
961 }
962}
963
964/*******************************************************************************
965**
966** Function gatts_process_read_by_type_req
967**
968** Description process Read By type request.
969** This PDU can be used to perform:
970** - read characteristic value
971** - read characteristic descriptor value
972** - discover characteristic
973** - discover characteristic by UUID
974** - relationship discovery
975**
976** Returns void
977**
978*******************************************************************************/
979void gatts_process_read_by_type_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
980{
981 tBT_UUID uuid;
982 tGATT_SR_REG *p_rcb;
983 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET),
984 buf_len,
985 s_hdl, e_hdl, err_hdl = 0;
986 BT_HDR *p_msg = NULL;
987 tGATT_STATUS reason, ret;
988 UINT8 *p;
989 UINT8 sec_flag, key_size;
990 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
991 tGATT_SRV_LIST_ELEM *p_srv=NULL;
992
993 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
994
995#if GATT_CONFORMANCE_TESTING == TRUE
996 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
997 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700998 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 -0800999
1000 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, FALSE);
1001
1002 return;
1003 }
1004#endif
1005
1006 if (reason == GATT_SUCCESS)
1007 {
1008 if ((p_msg = (BT_HDR *)GKI_getbuf(msg_len)) == NULL)
1009 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001010 GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001011
1012 reason = GATT_NO_RESOURCES;
1013 }
1014 else
1015 {
1016 memset(p_msg, 0, msg_len);
1017 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1018
1019 *p ++ = op_code + 1;
1020 /* reserve length byte */
1021 p_msg->len = 2;
1022 buf_len = p_tcb->payload_size - 2;
1023
1024 reason = GATT_NOT_FOUND;
1025
1026 p_srv = p_list->p_first;
1027
1028 while (p_srv)
1029 {
1030 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
1031
1032 if (p_rcb->in_use &&
1033 !(p_rcb->s_hdl > e_hdl ||
1034 p_rcb->e_hdl < s_hdl))
1035 {
1036 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001037 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001038 &sec_flag,
1039 &key_size);
1040
1041 ret = gatts_db_read_attr_value_by_type(p_tcb,
1042 p_rcb->p_db,
1043 op_code,
1044 p_msg,
1045 s_hdl,
1046 e_hdl,
1047 uuid,
1048 &buf_len,
1049 sec_flag,
1050 key_size,
1051 0,
1052 &err_hdl);
1053 if (ret != GATT_NOT_FOUND)
1054 {
1055 reason = ret;
1056
1057 if (ret == GATT_NO_RESOURCES)
1058 reason = GATT_SUCCESS;
1059 }
1060 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND)
1061 {
1062 s_hdl = err_hdl;
1063 break;
1064 }
1065 }
1066 p_srv = p_srv->p_next;
1067 }
1068 *p = (UINT8)p_msg->offset;
1069 p_msg->offset = L2CAP_MIN_OFFSET;
1070 }
1071 }
1072 if (reason != GATT_SUCCESS)
1073 {
1074 if (p_msg) GKI_freebuf(p_msg);
1075
1076 /* in theroy BUSY is not possible(should already been checked), protected check */
1077 if (reason != GATT_PENDING && reason != GATT_BUSY)
1078 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
1079 }
1080 else
1081 attp_send_sr_msg(p_tcb, p_msg);
1082
1083}
1084
1085/*******************************************************************************
1086**
1087** Function gatts_process_write_req
1088**
1089** Description This function is called to process the write request
1090** from client.
1091**
1092** Returns void
1093**
1094*******************************************************************************/
1095void gatts_process_write_req (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
1096 UINT8 op_code, UINT16 len, UINT8 *p_data)
1097{
1098 tGATTS_DATA sr_data;
1099 UINT32 trans_id;
1100 tGATT_STATUS status;
1101 UINT8 sec_flag, key_size, *p = p_data;
1102 tGATT_SR_REG *p_sreg;
1103 UINT16 conn_id;
1104
1105 memset(&sr_data, 0, sizeof(tGATTS_DATA));
1106
1107 switch (op_code)
1108 {
1109 case GATT_REQ_PREPARE_WRITE:
1110 sr_data.write_req.is_prep = TRUE;
1111 STREAM_TO_UINT16(sr_data.write_req.offset, p);
1112 len -= 2;
1113 /* fall through */
1114 case GATT_SIGN_CMD_WRITE:
1115 if (op_code == GATT_SIGN_CMD_WRITE)
1116 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001117 GATT_TRACE_DEBUG("Write CMD with data sigining" );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001118 len -= GATT_AUTH_SIGN_LEN;
1119 }
1120 /* fall through */
1121 case GATT_CMD_WRITE:
1122 case GATT_REQ_WRITE:
1123 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
1124 sr_data.write_req.need_rsp = TRUE;
1125 sr_data.write_req.handle = handle;
1126 sr_data.write_req.len = len;
Satya Calloji444a8da2015-03-06 10:38:22 -08001127 if (len != 0 && p != NULL)
1128 {
1129 memcpy (sr_data.write_req.value, p, len);
1130 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001131 break;
1132 }
1133
1134 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001135 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001136 &sec_flag,
1137 &key_size);
1138
1139 status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
1140 op_code,
1141 handle,
1142 sr_data.write_req.offset,
1143 p,
1144 len,
1145 sec_flag,
1146 key_size);
1147
1148 if (status == GATT_SUCCESS)
1149 {
1150 if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0)
1151 {
1152 p_sreg = &gatt_cb.sr_reg[i_rcb];
1153 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
1154 gatt_sr_send_req_callback(conn_id,
1155 trans_id,
1156 GATTS_REQ_TYPE_WRITE,
1157 &sr_data);
1158
1159 status = GATT_PENDING;
1160 }
1161 else
1162 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001163 GATT_TRACE_ERROR("max pending command, send error");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001164 status = GATT_BUSY; /* max pending command, application error */
1165 }
1166 }
1167
1168 /* in theroy BUSY is not possible(should already been checked), protected check */
1169 if (status != GATT_PENDING && status != GATT_BUSY &&
1170 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE))
1171 {
1172 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1173 }
1174 return;
1175}
1176
1177/*******************************************************************************
1178**
1179** Function gatts_process_read_req
1180**
1181** Description This function is called to process the read request
1182** from client.
1183**
1184** Returns void
1185**
1186*******************************************************************************/
1187static 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 -07001188 UINT16 handle, UINT16 len, UINT8 *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001189{
1190 UINT16 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
1191 tGATT_STATUS reason;
1192 BT_HDR *p_msg = NULL;
1193 UINT8 sec_flag, key_size, *p;
1194 UINT16 offset = 0, value_len = 0;
1195
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001196 UNUSED (len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197 if ((p_msg = (BT_HDR *)GKI_getbuf(buf_len)) == NULL)
1198 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001199 GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001200
1201 reason = GATT_NO_RESOURCES;
1202 }
1203 else
1204 {
1205 if (op_code == GATT_REQ_READ_BLOB)
1206 STREAM_TO_UINT16(offset, p_data);
1207
1208 memset(p_msg, 0, buf_len);
1209 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1210 *p ++ = op_code + 1;
1211 p_msg->len = 1;
1212 buf_len = p_tcb->payload_size - 1;
1213
1214 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001215 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001216 &sec_flag,
1217 &key_size);
1218
1219 reason = gatts_read_attr_value_by_handle(p_tcb,
1220 p_rcb->p_db,
1221 op_code,
1222 handle,
1223 offset,
1224 p,
1225 &value_len,
1226 buf_len,
1227 sec_flag,
1228 key_size,
1229 0);
1230
1231 p_msg->len += value_len;
1232 }
1233
1234 if (reason != GATT_SUCCESS)
1235 {
1236 if (p_msg) GKI_freebuf(p_msg);
1237
1238 /* in theroy BUSY is not possible(should already been checked), protected check */
1239 if (reason != GATT_PENDING && reason != GATT_BUSY)
1240 gatt_send_error_rsp (p_tcb, reason, op_code, handle, FALSE);
1241 }
1242 else
1243 attp_send_sr_msg(p_tcb, p_msg);
1244
1245}
1246
1247/*******************************************************************************
1248**
1249** Function gatts_process_attribute_req
1250**
1251** Description This function is called to process the per attribute handle request
1252** from client.
1253**
1254** Returns void
1255**
1256*******************************************************************************/
1257void gatts_process_attribute_req (tGATT_TCB *p_tcb, UINT8 op_code,
1258 UINT16 len, UINT8 *p_data)
1259{
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001260 UINT16 handle = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001261 UINT8 *p = p_data, i;
1262 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1263 tGATT_STATUS status = GATT_INVALID_HANDLE;
1264 tGATT_ATTR16 *p_attr;
1265
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001266 if (len < 2)
1267 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001268 GATT_TRACE_ERROR("Illegal PDU length, discard request");
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001269 status = GATT_INVALID_PDU;
1270 }
1271 else
1272 {
1273 STREAM_TO_UINT16(handle, p);
1274 len -= 2;
1275 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001276
1277#if GATT_CONFORMANCE_TESTING == TRUE
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001278 gatt_cb.handle = handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001279 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
1280 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001281 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 -08001282
1283 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
1284
1285 return;
1286 }
1287#endif
1288
1289 if (GATT_HANDLE_IS_VALID(handle))
1290 {
1291 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1292 {
1293 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1294 {
1295 p_attr = (tGATT_ATTR16 *)p_rcb->p_db->p_attr_list;
1296
1297 while (p_attr)
1298 {
1299 if (p_attr->handle == handle)
1300 {
1301 switch (op_code)
1302 {
1303 case GATT_REQ_READ: /* read char/char descriptor value */
1304 case GATT_REQ_READ_BLOB:
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001305 gatts_process_read_req(p_tcb, p_rcb, op_code, handle, len, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001306 break;
1307
1308 case GATT_REQ_WRITE: /* write char/char descriptor value */
1309 case GATT_CMD_WRITE:
1310 case GATT_SIGN_CMD_WRITE:
1311 case GATT_REQ_PREPARE_WRITE:
1312 gatts_process_write_req(p_tcb, i, handle, op_code, len, p);
1313 break;
1314 default:
1315 break;
1316 }
1317 status = GATT_SUCCESS;
1318 break;
1319 }
1320 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
1321 }
1322 break;
1323 }
1324 }
1325 }
1326
1327 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE)
1328 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1329}
1330
1331/*******************************************************************************
1332**
1333** Function gatts_proc_srv_chg_ind_ack
1334**
1335** Description This function process the service changed indicaiton ACK
1336**
1337** Returns void
1338**
1339*******************************************************************************/
1340static void gatts_proc_srv_chg_ind_ack(tGATT_TCB *p_tcb )
1341{
1342 tGATTS_SRV_CHG_REQ req;
1343 tGATTS_SRV_CHG *p_buf = NULL;
1344
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001345 GATT_TRACE_DEBUG("gatts_proc_srv_chg_ind_ack");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001346
1347 if ((p_buf = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
1348 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001349 GATT_TRACE_DEBUG("NV update set srv chg = FALSE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001350 p_buf->srv_changed = FALSE;
1351 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1352 if (gatt_cb.cb_info.p_srv_chg_callback)
1353 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,&req, NULL);
1354 }
1355}
1356
1357/*******************************************************************************
1358**
1359** Function gatts_chk_pending_ind
1360**
1361** Description This function check any pending indication needs to be sent if
1362** there is a pending indication then sent the indication
1363**
1364** Returns void
1365**
1366*******************************************************************************/
1367static void gatts_chk_pending_ind(tGATT_TCB *p_tcb )
1368{
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001369 GATT_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001370
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001371 tGATT_VALUE *p_buf = (tGATT_VALUE *)fixed_queue_try_peek_first(p_tcb->pending_ind_q);
1372 if (p_buf != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001373 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001374 GATTS_HandleValueIndication(p_buf->conn_id,
1375 p_buf->handle,
1376 p_buf->len,
1377 p_buf->value);
1378 GKI_freebuf(fixed_queue_try_remove_from_queue(p_tcb->pending_ind_q,
1379 p_buf));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001380 }
1381}
1382
1383/*******************************************************************************
1384**
1385** Function gatts_proc_ind_ack
1386**
1387** Description This function process the Indication ack
1388**
1389** Returns TRUE continue to process the indication ack by the aaplication
1390** if the ACk is not a Service Changed Indication Ack
1391**
1392*******************************************************************************/
1393static BOOLEAN gatts_proc_ind_ack(tGATT_TCB *p_tcb, UINT16 ack_handle)
1394{
1395 BOOLEAN continue_processing = TRUE;
1396
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001397 GATT_TRACE_DEBUG ("gatts_proc_ind_ack ack handle=%d", ack_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001398
1399 if (ack_handle == gatt_cb.handle_of_h_r)
1400 {
1401 gatts_proc_srv_chg_ind_ack(p_tcb);
1402 /* there is no need to inform the application since srv chg is handled internally by GATT */
1403 continue_processing = FALSE;
1404 }
1405
1406 gatts_chk_pending_ind(p_tcb);
1407 return continue_processing;
1408}
1409
1410/*******************************************************************************
1411**
1412** Function gatts_process_value_conf
1413**
1414** Description This function is called to process the handle value confirmation.
1415**
1416** Returns void
1417**
1418*******************************************************************************/
1419void gatts_process_value_conf(tGATT_TCB *p_tcb, UINT8 op_code)
1420{
1421 UINT16 handle = p_tcb->indicate_handle;
1422 UINT32 trans_id;
1423 UINT8 i;
1424 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1425 BOOLEAN continue_processing;
1426 UINT16 conn_id;
1427
1428 btu_stop_timer (&p_tcb->conf_timer_ent);
1429 if (GATT_HANDLE_IS_VALID(handle))
1430 {
1431 p_tcb->indicate_handle = 0;
1432 continue_processing = gatts_proc_ind_ack(p_tcb, handle);
1433
1434 if (continue_processing)
1435 {
1436 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1437 {
1438 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1439 {
1440 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
1441 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_rcb->gatt_if);
1442 gatt_sr_send_req_callback(conn_id,
1443 trans_id, GATTS_REQ_TYPE_CONF, (tGATTS_DATA *)&handle);
1444 }
1445 }
1446 }
1447 }
1448 else
1449 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001450 GATT_TRACE_ERROR("unexpected handle value confirmation");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001451 }
1452}
1453
1454/*******************************************************************************
1455**
1456** Function gatt_server_handle_client_req
1457**
1458** Description This function is called to handle the client requests to
1459** server.
1460**
1461**
1462** Returns void
1463**
1464*******************************************************************************/
1465void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
1466 UINT16 len, UINT8 *p_data)
1467{
1468 /* there is pending command, discard this one */
1469 if (!gatt_sr_cmd_empty(p_tcb) && op_code != GATT_HANDLE_VALUE_CONF)
1470 return;
1471
1472 /* the size of the message may not be bigger than the local max PDU size*/
1473 /* The message has to be smaller than the agreed MTU, len does not include op code */
1474 if (len >= p_tcb->payload_size)
1475 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001476 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 -08001477 /* for invalid request expecting response, send it now */
1478 if (op_code != GATT_CMD_WRITE &&
1479 op_code != GATT_SIGN_CMD_WRITE &&
1480 op_code != GATT_HANDLE_VALUE_CONF)
1481 {
1482 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, op_code, 0, FALSE);
1483 }
1484 /* otherwise, ignore the pkt */
1485 }
1486 else
1487 {
1488 switch (op_code)
1489 {
1490 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1491 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1492 gatts_process_primary_service_req (p_tcb, op_code, len, p_data);
1493 break;
1494
1495 case GATT_REQ_FIND_INFO:/* discover char descrptor */
1496 gatts_process_find_info(p_tcb, op_code, len, p_data);
1497 break;
1498
1499 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor value */
1500 /* discover characteristic, discover char by UUID */
1501 gatts_process_read_by_type_req(p_tcb, op_code, len, p_data);
1502 break;
1503
1504
1505 case GATT_REQ_READ: /* read char/char descriptor value */
1506 case GATT_REQ_READ_BLOB:
1507 case GATT_REQ_WRITE: /* write char/char descriptor value */
1508 case GATT_CMD_WRITE:
1509 case GATT_SIGN_CMD_WRITE:
1510 case GATT_REQ_PREPARE_WRITE:
1511 gatts_process_attribute_req (p_tcb, op_code, len, p_data);
1512 break;
1513
1514 case GATT_HANDLE_VALUE_CONF:
1515 gatts_process_value_conf (p_tcb, op_code);
1516 break;
1517
1518 case GATT_REQ_MTU:
1519 gatts_process_mtu_req (p_tcb, len, p_data);
1520 break;
1521
1522 case GATT_REQ_EXEC_WRITE:
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001523 gatt_process_exec_write_req (p_tcb, op_code, len, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001524 break;
1525
1526 case GATT_REQ_READ_MULTI:
1527 gatt_process_read_multi_req (p_tcb, op_code, len, p_data);
1528 break;
1529
1530 default:
1531 break;
1532 }
1533 }
1534}
1535
1536#endif /* BLE_INCLUDED */