blob: 998e46ed5ebe5a6f969497c6ffe35d3eba0f5554 [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)
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700103 GATT_TRACE_ERROR("free p_tcb->sr_cmd.p_rsp_msg = %d", p_tcb->sr_cmd.p_rsp_msg);
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800104 osi_free_and_reset((void **)&p_tcb->sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800105
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700106 while (!fixed_queue_is_empty(p_tcb->sr_cmd.multi_rsp_q))
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800107 osi_free(fixed_queue_try_dequeue(p_tcb->sr_cmd.multi_rsp_q));
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700108 fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800109 memset( &p_tcb->sr_cmd, 0, sizeof(tGATT_SR_CMD));
110}
111
112/*******************************************************************************
113**
114** Function process_read_multi_rsp
115**
116** Description This function check the read multiple response.
117**
118** Returns BOOLEAN if all replies have been received
119**
120*******************************************************************************/
121static BOOLEAN process_read_multi_rsp (tGATT_SR_CMD *p_cmd, tGATT_STATUS status,
122 tGATTS_RSP *p_msg, UINT16 mtu)
123{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124 UINT16 ii, total_len, len;
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800125 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(tGATTS_RSP));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800126 UINT8 *p;
127 BOOLEAN is_overflow = FALSE;
128
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700129 GATT_TRACE_DEBUG ("process_read_multi_rsp status=%d mtu=%d", status, mtu);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130
131 if (p_buf == NULL)
132 {
133 p_cmd->status = GATT_INSUF_RESOURCE;
134 return FALSE;
135 }
136
137 /* Enqueue the response */
138 memcpy((void *)p_buf, (const void *)p_msg, sizeof(tGATTS_RSP));
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700139 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800140
141 p_cmd->status = status;
142 if (status == GATT_SUCCESS)
143 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700144 GATT_TRACE_DEBUG("Multi read count=%d num_hdls=%d",
145 fixed_queue_length(&p_cmd->multi_rsp_q),
146 p_cmd->multi_req.num_handles);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800147 /* Wait till we get all the responses */
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700148 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 -0800149 {
150 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800151 if ((p_buf = (BT_HDR *)osi_malloc(len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800152 {
153 p_cmd->status = GATT_INSUF_RESOURCE;
154 return(TRUE);
155 }
156
157 memset(p_buf, 0, len);
158 p_buf->offset = L2CAP_MIN_OFFSET;
159 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
160
161 /* First byte in the response is the opcode */
162 *p++ = GATT_RSP_READ_MULTI;
163 p_buf->len = 1;
164
165 /* Now walk through the buffers puting the data into the response in order */
Pavlin Radoslavov577862e2015-10-07 18:07:48 -0700166 list_t *list = NULL;
167 const list_node_t *node = NULL;
168 if (! fixed_queue_is_empty(p_cmd->multi_rsp_q))
169 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800170 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++)
171 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700172 tGATTS_RSP *p_rsp = NULL;
173
Pavlin Radoslavov577862e2015-10-07 18:07:48 -0700174 if (list != NULL) {
175 if (ii == 0)
176 node = list_begin(list);
177 else
178 node = list_next(node);
179 if (node != list_end(list))
180 p_rsp = (tGATTS_RSP *)list_node(node);
181 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800182
183 if (p_rsp != NULL)
184 {
185
186 total_len = (p_buf->len + p_rsp->attr_value.len);
187
188 if (total_len > mtu)
189 {
190 /* just send the partial response for the overflow case */
191 len = p_rsp->attr_value.len - (total_len - mtu);
192 is_overflow = TRUE;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700193 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 -0800194 }
195 else
196 {
197 len = p_rsp->attr_value.len;
198 }
199
200 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii])
201 {
202 memcpy (p, p_rsp->attr_value.value, len);
203 if (!is_overflow)
204 p += len;
205 p_buf->len += len;
206 }
207 else
208 {
209 p_cmd->status = GATT_NOT_FOUND;
210 break;
211 }
212
213 if (is_overflow)
214 break;
215
216 }
217 else
218 {
219 p_cmd->status = GATT_NOT_FOUND;
220 break;
221 }
222
223 } /* loop through all handles*/
224
225
226 /* Sanity check on the buffer length */
227 if (p_buf->len == 0)
228 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700229 GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800230 p_cmd->status = GATT_NOT_FOUND;
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800231 osi_free(p_buf);
232 GATT_TRACE_DEBUG("osi_free(p_buf)");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800233 }
234 else if (p_cmd->p_rsp_msg != NULL)
235 {
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800236 osi_free(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237 }
238 else
239 {
240 p_cmd->p_rsp_msg = p_buf;
241 }
242
243 return(TRUE);
244 }
245 }
246 else /* any handle read exception occurs, return error */
247 {
248 return(TRUE);
249 }
250
251 /* If here, still waiting */
252 return(FALSE);
253}
254
255/*******************************************************************************
256**
257** Function gatt_sr_process_app_rsp
258**
259** Description This function checks whether the response message from application
260** match any pending request or not.
261**
262** Returns void
263**
264*******************************************************************************/
265tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if,
266 UINT32 trans_id, UINT8 op_code,
267 tGATT_STATUS status, tGATTS_RSP *p_msg)
268{
269 tGATT_STATUS ret_code = GATT_SUCCESS;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800270 UNUSED(trans_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800271
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700272 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp gatt_if=%d", gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800273
274 gatt_sr_update_cback_cnt(p_tcb, gatt_if, FALSE, FALSE);
275
276 if (op_code == GATT_REQ_READ_MULTI)
277 {
278 /* If no error and still waiting, just return */
279 if (!process_read_multi_rsp (&p_tcb->sr_cmd, status, p_msg, p_tcb->payload_size))
280 return(GATT_SUCCESS);
281 }
282 else
283 {
284 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
285 gatt_sr_update_prep_cnt(p_tcb, gatt_if, TRUE, FALSE);
286
287 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
288 gatt_sr_reset_cback_cnt(p_tcb);
289
290 p_tcb->sr_cmd.status = status;
291
292 if (gatt_sr_is_cback_cnt_zero(p_tcb)
293 && status == GATT_SUCCESS)
294 {
295 if (p_tcb->sr_cmd.p_rsp_msg == NULL)
296 {
297 p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg (p_tcb, (UINT8)(op_code + 1), (tGATT_SR_MSG *)p_msg);
298 }
299 else
300 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700301 GATT_TRACE_ERROR("Exception!!! already has respond message");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800302 }
303 }
304 }
305 if (gatt_sr_is_cback_cnt_zero(p_tcb))
306 {
307 if ( (p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg) )
308 {
309 ret_code = attp_send_sr_msg (p_tcb, p_tcb->sr_cmd.p_rsp_msg);
310 p_tcb->sr_cmd.p_rsp_msg = NULL;
311 }
312 else
313 {
314 ret_code = gatt_send_error_rsp (p_tcb, status, op_code, p_tcb->sr_cmd.handle, FALSE);
315 }
316
317 gatt_dequeue_sr_cmd(p_tcb);
318 }
319
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700320 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp ret_code=%d", ret_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800321
322 return ret_code;
323}
324
325/*******************************************************************************
326**
327** Function gatt_process_exec_write_req
328**
329** Description This function is called to process the execute write request
330** from client.
331**
332** Returns void
333**
334*******************************************************************************/
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700335void 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 -0800336{
337 UINT8 *p = p_data, flag, i = 0;
338 UINT32 trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800339 tGATT_IF gatt_if;
340 UINT16 conn_id;
341
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700342 UNUSED(len);
343
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800344#if GATT_CONFORMANCE_TESTING == TRUE
345 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
346 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700347 GATT_TRACE_DEBUG("Conformance tst: forced err rspv for Execute Write: error status=%d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700348 gatt_cb.err_status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800349
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700350 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 -0800351
352 return;
353 }
354#endif
355
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356 STREAM_TO_UINT8(flag, p);
357
358 /* mask the flag */
359 flag &= GATT_PREP_WRITE_EXEC;
360
361
362 /* no prep write is queued */
363 if (!gatt_sr_is_prep_cnt_zero(p_tcb))
364 {
365 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0);
366 gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb);
367
368 for (i=0; i<GATT_MAX_APPS; i++)
369 {
370 if (p_tcb->prep_cnt[i])
371 {
372 gatt_if = (tGATT_IF) (i+1);
373 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
374 gatt_sr_send_req_callback(conn_id,
375 trans_id,
376 GATTS_REQ_TYPE_WRITE_EXEC,
377 (tGATTS_DATA *)&flag);
378 p_tcb->prep_cnt[i]= 0;
379 }
380 }
381 }
382 else /* nothing needs to be executed , send response now */
383 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700384 GATT_TRACE_ERROR("gatt_process_exec_write_req: no prepare write pending");
Mudumba Ananthcf310332014-07-04 03:45:11 -0700385 gatt_send_error_rsp(p_tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, FALSE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800386 }
387}
388
389/*******************************************************************************
390**
391** Function gatt_process_read_multi_req
392**
393** Description This function is called to process the read multiple request
394** from client.
395**
396** Returns void
397**
398*******************************************************************************/
399void gatt_process_read_multi_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
400{
401 UINT32 trans_id;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700402 UINT16 handle = 0, ll = len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800403 UINT8 *p = p_data, i_rcb;
404 tGATT_STATUS err = GATT_SUCCESS;
405 UINT8 sec_flag, key_size;
406 tGATTS_RSP *p_msg;
407
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700408 GATT_TRACE_DEBUG("gatt_process_read_multi_req" );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800409 p_tcb->sr_cmd.multi_req.num_handles = 0;
410
411 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700412 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800413 &sec_flag,
414 &key_size);
415
416#if GATT_CONFORMANCE_TESTING == TRUE
417 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
418 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700419 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 -0800420
421 STREAM_TO_UINT16(handle, p);
422
423 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
424
425 return;
426 }
427#endif
428
429 while (ll >= 2 && p_tcb->sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES)
430 {
431 STREAM_TO_UINT16(handle, p);
432
433 if ((i_rcb = gatt_sr_find_i_rcb_by_handle(handle)) < GATT_MAX_SR_PROFILES)
434 {
435 p_tcb->sr_cmd.multi_req.handles[p_tcb->sr_cmd.multi_req.num_handles++] = handle;
436
437 /* check read permission */
438 if ((err = gatts_read_attr_perm_check( gatt_cb.sr_reg[i_rcb].p_db,
439 FALSE,
440 handle,
441 sec_flag,
442 key_size))
443 != GATT_SUCCESS)
444 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700445 GATT_TRACE_DEBUG("read permission denied : 0x%02x", err);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800446 break;
447 }
448 }
449 else
450 {
451 /* invalid handle */
452 err = GATT_INVALID_HANDLE;
453 break;
454 }
455 ll -= 2;
456 }
457
458 if (ll != 0)
459 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700460 GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461 }
462
463 if (p_tcb->sr_cmd.multi_req.num_handles == 0)
464 err = GATT_INVALID_HANDLE;
465
466 if (err == GATT_SUCCESS)
467 {
468 if ((trans_id = gatt_sr_enqueue_cmd (p_tcb, op_code, p_tcb->sr_cmd.multi_req.handles[0])) != 0)
469 {
470 gatt_sr_reset_cback_cnt(p_tcb); /* read multiple use multi_rsp_q's count*/
471
472 for (ll = 0; ll < p_tcb->sr_cmd.multi_req.num_handles; ll ++)
473 {
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800474 if ((p_msg = (tGATTS_RSP *)osi_malloc(sizeof(tGATTS_RSP))) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800475 {
476 memset(p_msg, 0, sizeof(tGATTS_RSP))
477 ;
478 handle = p_tcb->sr_cmd.multi_req.handles[ll];
479 i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
480
481 p_msg->attr_value.handle = handle;
482 err = gatts_read_attr_value_by_handle(p_tcb,
483 gatt_cb.sr_reg[i_rcb].p_db,
484 op_code,
485 handle,
486 0,
487 p_msg->attr_value.value,
488 &p_msg->attr_value.len,
489 GATT_MAX_ATTR_LEN,
490 sec_flag,
491 key_size,
492 trans_id);
493
494 if (err == GATT_SUCCESS)
495 {
496 gatt_sr_process_app_rsp(p_tcb, gatt_cb.sr_reg[i_rcb].gatt_if ,trans_id, op_code, GATT_SUCCESS, p_msg);
497 }
498 /* either not using or done using the buffer, release it now */
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800499 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800500 }
501 else
502 {
503 err = GATT_NO_RESOURCES;
504 gatt_dequeue_sr_cmd(p_tcb);
505 break;
506 }
507 }
508 }
509 else
510 err = GATT_NO_RESOURCES;
511 }
512
513 /* in theroy BUSY is not possible(should already been checked), protected check */
514 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
515 gatt_send_error_rsp(p_tcb, err, op_code, handle, FALSE);
516}
517
518/*******************************************************************************
519**
520** Function gatt_build_primary_service_rsp
521**
522** Description Primamry service request processed internally. Theretically
523** only deal with ReadByTypeVAlue and ReadByGroupType.
524**
525** Returns void
526**
527*******************************************************************************/
528static tGATT_STATUS gatt_build_primary_service_rsp (BT_HDR *p_msg, tGATT_TCB *p_tcb,
529 UINT8 op_code, UINT16 s_hdl,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700530 UINT16 e_hdl, UINT8 *p_data, tBT_UUID value)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800531{
532 tGATT_STATUS status = GATT_NOT_FOUND;
533 UINT8 handle_len =4, *p ;
534 tGATT_SR_REG *p_rcb;
535 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
536 tGATT_SRV_LIST_ELEM *p_srv=NULL;
537 tBT_UUID *p_uuid;
538
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700539 UNUSED(p_data);
540
The Android Open Source Project5738f832012-12-12 16:00:35 -0800541 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
542
543 p_srv = p_list->p_first;
544
545 while (p_srv)
546 {
547 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
548
549 if (p_rcb->in_use &&
550 p_rcb->s_hdl >= s_hdl &&
551 p_rcb->s_hdl <= e_hdl &&
552 p_rcb->type == GATT_UUID_PRI_SERVICE)
553 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700554 if ((p_uuid = gatts_get_service_uuid (p_rcb->p_db)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800555 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800556 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700557 handle_len = 4 + p_uuid->len;
558
559 /* get the length byte in the repsonse */
560 if (p_msg->offset ==0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700562 *p ++ = op_code + 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563 p_msg->len ++;
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700564 p_msg->offset = handle_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565
566 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700567 {
568 *p ++ = (UINT8)p_msg->offset; /* length byte */
569 p_msg->len ++;
570 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800571 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700572
573 if (p_msg->len + p_msg->offset <= p_tcb->payload_size &&
574 handle_len == p_msg->offset)
575 {
576 if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
577 gatt_uuid_compare(value, *p_uuid))
578 {
579 UINT16_TO_STREAM(p, p_rcb->s_hdl);
580
581 if (p_list->p_last_primary == p_srv &&
582 p_list->p_last_primary == p_list->p_last)
583 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700584 GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute");
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700585 UINT16_TO_STREAM(p, 0xFFFF); /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
586 }
587 else
588 {
589 UINT16_TO_STREAM(p, p_rcb->e_hdl);
590 }
591
592 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
593 gatt_build_uuid_to_stream(&p, *p_uuid);
594
595 status = GATT_SUCCESS;
596 p_msg->len += p_msg->offset;
597 }
598 }
599 else
600 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800601 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800602 }
603 p_srv = p_srv->p_next;
604 }
605 p_msg->offset = L2CAP_MIN_OFFSET;
606
607 return status;
608}
609
610/*******************************************************************************
611**
612** Function gatt_build_find_info_rsp
613**
614** Description fill the find information response information in the given
615** buffer.
616**
617** Returns TRUE: if data filled sucessfully.
618** FALSE: packet full, or format mismatch.
619**
620*******************************************************************************/
621static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SR_REG *p_rcb, BT_HDR *p_msg, UINT16 *p_len,
622 UINT16 s_hdl, UINT16 e_hdl)
623{
624 tGATT_STATUS status = GATT_NOT_FOUND;
625 UINT8 *p;
626 UINT16 len = *p_len;
627 tGATT_ATTR16 *p_attr = NULL;
628 UINT8 info_pair_len[2] = {4, 18};
629
630 if (!p_rcb->p_db || !p_rcb->p_db->p_attr_list)
631 return status;
632
633 /* check the attribute database */
634 p_attr = (tGATT_ATTR16 *) p_rcb->p_db->p_attr_list;
635
636 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
637
638 while (p_attr)
639 {
640 if (p_attr->handle > e_hdl)
641 {
642 break;
643 }
644
645 if (p_attr->handle >= s_hdl)
646 {
647 if (p_msg->offset == 0)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700648 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 -0800649
650 if (len >= info_pair_len[p_msg->offset - 1])
651 {
652 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16)
653 {
654 UINT16_TO_STREAM(p, p_attr->handle);
655 UINT16_TO_STREAM(p, p_attr->uuid);
656 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700657 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 -0800658 {
659 UINT16_TO_STREAM(p, p_attr->handle);
660 ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *) p_attr)->uuid, LEN_UUID_128);
661 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700662 else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32)
663 {
664 UINT16_TO_STREAM(p, p_attr->handle);
665 gatt_convert_uuid32_to_uuid128(p, ((tGATT_ATTR32 *) p_attr)->uuid);
666 p += LEN_UUID_128;
667 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800668 else
669 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700670 GATT_TRACE_ERROR("format mismatch");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800671 status = GATT_NO_RESOURCES;
672 break;
673 /* format mismatch */
674 }
675 p_msg->len += info_pair_len[p_msg->offset - 1];
676 len -= info_pair_len[p_msg->offset - 1];
677 status = GATT_SUCCESS;
678
679 }
680 else
681 {
682 status = GATT_NO_RESOURCES;
683 break;
684 }
685 }
686 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
687 }
688
689 *p_len = len;
690 return status;
691}
692
693/*******************************************************************************
694**
695** Function gatts_internal_read_by_type_req
696**
697** Description check to see if the ReadByType request can be handled internally.
698**
699** Returns void
700**
701*******************************************************************************/
702static tGATT_STATUS gatts_validate_packet_format(UINT8 op_code, UINT16 *p_len,
703 UINT8 **p_data, tBT_UUID *p_uuid_filter,
704 UINT16 *p_s_hdl, UINT16 *p_e_hdl)
705{
706 tGATT_STATUS reason = GATT_SUCCESS;
707 UINT16 uuid_len, s_hdl = 0, e_hdl = 0;
708 UINT16 len = *p_len;
709 UINT8 *p = *p_data;
710
711 if (len >= 4)
712 {
713 /* obtain starting handle, and ending handle */
714 STREAM_TO_UINT16(s_hdl, p);
715 STREAM_TO_UINT16(e_hdl, p);
716 len -= 4;
717
718 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || !GATT_HANDLE_IS_VALID(e_hdl))
719 {
720 reason = GATT_INVALID_HANDLE;
721 }
722 /* for these PDUs, uuid filter must present */
723 else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
724 op_code == GATT_REQ_FIND_TYPE_VALUE ||
725 op_code == GATT_REQ_READ_BY_TYPE)
726 {
727 if (len >= 2 && p_uuid_filter != NULL)
728 {
729 uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
730
731 /* parse uuid now */
732 if (gatt_parse_uuid_from_cmd (p_uuid_filter, uuid_len, &p) == FALSE ||
733 p_uuid_filter->len == 0)
734 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700735 GATT_TRACE_DEBUG("UUID filter does not exsit");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800736 reason = GATT_INVALID_PDU;
737 }
738 else
739 len -= p_uuid_filter->len;
740 }
741 else
742 reason = GATT_INVALID_PDU;
743 }
744 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700745 else
746 reason = GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800747
748 *p_data = p;
749 *p_len = len;
750 *p_s_hdl = s_hdl;
751 *p_e_hdl = e_hdl;
752
753 return reason;
754}
755
756/*******************************************************************************
757**
758** Function gatts_process_primary_service_req
759**
760** Description process ReadByGroupType/ReadByTypeValue request, for discover
761** all primary services or discover primary service by UUID request.
762**
763** Returns void
764**
765*******************************************************************************/
766void gatts_process_primary_service_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
767{
768 UINT8 reason = GATT_INVALID_PDU;
769 UINT16 s_hdl = 0, e_hdl = 0;
770 tBT_UUID uuid, value, primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
771 BT_HDR *p_msg = NULL;
772 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
773
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700774 memset (&value, 0, sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800775 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
776
777 if (reason == GATT_SUCCESS)
778 {
779 if (gatt_uuid_compare(uuid, primary_service))
780 {
781 if (op_code == GATT_REQ_FIND_TYPE_VALUE)
782 {
783 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == FALSE)
784 reason = GATT_INVALID_PDU;
785 }
786
787 if (reason == GATT_SUCCESS)
788 {
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800789 if ((p_msg = (BT_HDR *)osi_malloc(msg_len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700791 GATT_TRACE_ERROR("gatts_process_primary_service_req failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800792 reason = GATT_NO_RESOURCES;
793 }
794 else
795 {
796 memset(p_msg, 0, msg_len);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700797 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 -0800798 }
799 }
800 }
801 else
802 {
803 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
804 {
805 reason = GATT_UNSUPPORT_GRP_TYPE;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700806 GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x", uuid.uu.uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800807 }
808 else
809 {
810 /* we do not support ReadByTypeValue with any non-primamry_service type */
811 reason = GATT_NOT_FOUND;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700812 GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x", uuid.uu.uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800813 }
814 }
815 }
816
817 if (reason != GATT_SUCCESS)
818 {
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800819 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800820 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
821 }
822 else
823 attp_send_sr_msg(p_tcb, p_msg);
824
825}
826
827/*******************************************************************************
828**
829** Function gatts_process_find_info
830**
831** Description process find information request, for discover character
832** descriptors.
833**
834** Returns void
835**
836*******************************************************************************/
837static void gatts_process_find_info(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
838{
839 UINT8 reason = GATT_INVALID_PDU, *p;
840 UINT16 s_hdl = 0, e_hdl = 0, buf_len;
841 BT_HDR *p_msg = NULL;
842 tGATT_SR_REG *p_rcb;
843 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
844 tGATT_SRV_LIST_ELEM *p_srv=NULL;
845
846 reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, &e_hdl);
847
848 if (reason == GATT_SUCCESS)
849 {
850 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
851
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800852 if ((p_msg = (BT_HDR *)osi_malloc(buf_len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800853 {
854 reason = GATT_NO_RESOURCES;
855 }
856 else
857 {
858 reason = GATT_NOT_FOUND;
859
860 memset(p_msg, 0, buf_len);
861 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
862 *p ++ = op_code + 1;
863 p_msg->len = 2;
864
865 buf_len = p_tcb->payload_size - 2;
866
867 p_srv = p_list->p_first;
868
869 while (p_srv)
870 {
871 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
872
873 if (p_rcb->in_use &&
874 !(p_rcb->s_hdl > e_hdl ||
875 p_rcb->e_hdl < s_hdl))
876 {
877 reason = gatt_build_find_info_rsp(p_rcb, p_msg, &buf_len, s_hdl, e_hdl);
878 if (reason == GATT_NO_RESOURCES)
879 {
880 reason = GATT_SUCCESS;
881 break;
882 }
883 }
884 p_srv = p_srv->p_next;
885 }
886 *p = (UINT8)p_msg->offset;
887
888 p_msg->offset = L2CAP_MIN_OFFSET;
889 }
890 }
891
892 if (reason != GATT_SUCCESS)
893 {
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800894 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800895 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
896 }
897 else
898 attp_send_sr_msg(p_tcb, p_msg);
899
900}
901
902/*******************************************************************************
903**
904** Function gatts_process_mtu_req
905**
906** Description This function is called to process excahnge MTU request.
907** Only used on LE.
908**
909** Returns void
910**
911*******************************************************************************/
912static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data)
913{
914 UINT16 mtu = 0;
915 UINT8 *p = p_data, i;
916 BT_HDR *p_buf;
917 UINT16 conn_id;
918
The Android Open Source Project5738f832012-12-12 16:00:35 -0800919 /* BR/EDR conenction, send error response */
920 if (p_tcb->att_lcid != L2CAP_ATT_CID)
921 {
922 gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, FALSE);
923 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700924 else if (len < GATT_MTU_REQ_MIN_LEN)
925 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700926 GATT_TRACE_ERROR("invalid MTU request PDU received.");
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700927 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, FALSE);
928 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800929 else
930 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700931 STREAM_TO_UINT16 (mtu, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800932 /* mtu must be greater than default MTU which is 23/48 */
Andre Eisenbach4b638692013-06-14 13:22:25 -0700933 if (mtu < GATT_DEF_BLE_MTU_SIZE)
934 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
935 else if (mtu > GATT_MAX_MTU_SIZE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800936 p_tcb->payload_size = GATT_MAX_MTU_SIZE;
Andre Eisenbach4b638692013-06-14 13:22:25 -0700937 else
938 p_tcb->payload_size = mtu;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700940 GATT_TRACE_ERROR("MTU request PDU with MTU size %d", p_tcb->payload_size);
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700941
Priti Aghera636d6712014-12-18 13:55:48 -0800942 l2cble_set_fixed_channel_tx_data_length(p_tcb->peer_bda, L2CAP_ATT_CID, p_tcb->payload_size);
943
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;
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800984 size_t msg_len = sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET;
985 UINT16 buf_len, s_hdl, e_hdl, err_hdl = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800986 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 {
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001008 if ((p_msg = (BT_HDR *)osi_malloc(msg_len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001009 {
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 {
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001074 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001075
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{
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001190 size_t buf_len = sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001191 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);
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001197 if ((p_msg = (BT_HDR *)osi_malloc(buf_len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001198 {
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,
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001226 (uint16_t)buf_len,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001227 sec_flag,
1228 key_size,
1229 0);
1230
1231 p_msg->len += value_len;
1232 }
1233
1234 if (reason != GATT_SUCCESS)
1235 {
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001236 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001237
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);
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -08001378 osi_free(fixed_queue_try_remove_from_queue(p_tcb->pending_ind_q,
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001379 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
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08001428 alarm_cancel(p_tcb->conf_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001429 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 */