blob: 9be12aa9deb17f3403e68e749bb3b7b5d08a9501 [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
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700106 osi_freebuf (p_tcb->sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800107 }
108
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700109 while (!fixed_queue_is_empty(p_tcb->sr_cmd.multi_rsp_q))
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700110 osi_freebuf(fixed_queue_try_dequeue(p_tcb->sr_cmd.multi_rsp_q));
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700111 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;
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700128 BT_HDR *p_buf = (BT_HDR *)osi_getbuf((UINT16)sizeof(tGATTS_RSP));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129 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;
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700154 if ((p_buf = (BT_HDR *)osi_getbuf(len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800155 {
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 Radoslavov577862e2015-10-07 18:07:48 -0700169 list_t *list = NULL;
170 const list_node_t *node = NULL;
171 if (! fixed_queue_is_empty(p_cmd->multi_rsp_q))
172 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800173 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++)
174 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700175 tGATTS_RSP *p_rsp = NULL;
176
Pavlin Radoslavov577862e2015-10-07 18:07:48 -0700177 if (list != NULL) {
178 if (ii == 0)
179 node = list_begin(list);
180 else
181 node = list_next(node);
182 if (node != list_end(list))
183 p_rsp = (tGATTS_RSP *)list_node(node);
184 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800185
186 if (p_rsp != NULL)
187 {
188
189 total_len = (p_buf->len + p_rsp->attr_value.len);
190
191 if (total_len > mtu)
192 {
193 /* just send the partial response for the overflow case */
194 len = p_rsp->attr_value.len - (total_len - mtu);
195 is_overflow = TRUE;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700196 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 -0800197 }
198 else
199 {
200 len = p_rsp->attr_value.len;
201 }
202
203 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii])
204 {
205 memcpy (p, p_rsp->attr_value.value, len);
206 if (!is_overflow)
207 p += len;
208 p_buf->len += len;
209 }
210 else
211 {
212 p_cmd->status = GATT_NOT_FOUND;
213 break;
214 }
215
216 if (is_overflow)
217 break;
218
219 }
220 else
221 {
222 p_cmd->status = GATT_NOT_FOUND;
223 break;
224 }
225
226 } /* loop through all handles*/
227
228
229 /* Sanity check on the buffer length */
230 if (p_buf->len == 0)
231 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700232 GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800233 p_cmd->status = GATT_NOT_FOUND;
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700234 osi_freebuf (p_buf);
235 GATT_TRACE_DEBUG(" osi_freebuf (p_buf)");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800236 }
237 else if (p_cmd->p_rsp_msg != NULL)
238 {
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700239 osi_freebuf (p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800240 }
241 else
242 {
243 p_cmd->p_rsp_msg = p_buf;
244 }
245
246 return(TRUE);
247 }
248 }
249 else /* any handle read exception occurs, return error */
250 {
251 return(TRUE);
252 }
253
254 /* If here, still waiting */
255 return(FALSE);
256}
257
258/*******************************************************************************
259**
260** Function gatt_sr_process_app_rsp
261**
262** Description This function checks whether the response message from application
263** match any pending request or not.
264**
265** Returns void
266**
267*******************************************************************************/
268tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if,
269 UINT32 trans_id, UINT8 op_code,
270 tGATT_STATUS status, tGATTS_RSP *p_msg)
271{
272 tGATT_STATUS ret_code = GATT_SUCCESS;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800273 UNUSED(trans_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800274
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700275 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp gatt_if=%d", gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800276
277 gatt_sr_update_cback_cnt(p_tcb, gatt_if, FALSE, FALSE);
278
279 if (op_code == GATT_REQ_READ_MULTI)
280 {
281 /* If no error and still waiting, just return */
282 if (!process_read_multi_rsp (&p_tcb->sr_cmd, status, p_msg, p_tcb->payload_size))
283 return(GATT_SUCCESS);
284 }
285 else
286 {
287 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
288 gatt_sr_update_prep_cnt(p_tcb, gatt_if, TRUE, FALSE);
289
290 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
291 gatt_sr_reset_cback_cnt(p_tcb);
292
293 p_tcb->sr_cmd.status = status;
294
295 if (gatt_sr_is_cback_cnt_zero(p_tcb)
296 && status == GATT_SUCCESS)
297 {
298 if (p_tcb->sr_cmd.p_rsp_msg == NULL)
299 {
300 p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg (p_tcb, (UINT8)(op_code + 1), (tGATT_SR_MSG *)p_msg);
301 }
302 else
303 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700304 GATT_TRACE_ERROR("Exception!!! already has respond message");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800305 }
306 }
307 }
308 if (gatt_sr_is_cback_cnt_zero(p_tcb))
309 {
310 if ( (p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg) )
311 {
312 ret_code = attp_send_sr_msg (p_tcb, p_tcb->sr_cmd.p_rsp_msg);
313 p_tcb->sr_cmd.p_rsp_msg = NULL;
314 }
315 else
316 {
317 ret_code = gatt_send_error_rsp (p_tcb, status, op_code, p_tcb->sr_cmd.handle, FALSE);
318 }
319
320 gatt_dequeue_sr_cmd(p_tcb);
321 }
322
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700323 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp ret_code=%d", ret_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800324
325 return ret_code;
326}
327
328/*******************************************************************************
329**
330** Function gatt_process_exec_write_req
331**
332** Description This function is called to process the execute write request
333** from client.
334**
335** Returns void
336**
337*******************************************************************************/
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700338void 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 -0800339{
340 UINT8 *p = p_data, flag, i = 0;
341 UINT32 trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800342 tGATT_IF gatt_if;
343 UINT16 conn_id;
344
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700345 UNUSED(len);
346
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800347#if GATT_CONFORMANCE_TESTING == TRUE
348 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
349 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700350 GATT_TRACE_DEBUG("Conformance tst: forced err rspv for Execute Write: error status=%d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700351 gatt_cb.err_status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800352
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700353 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 -0800354
355 return;
356 }
357#endif
358
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359 STREAM_TO_UINT8(flag, p);
360
361 /* mask the flag */
362 flag &= GATT_PREP_WRITE_EXEC;
363
364
365 /* no prep write is queued */
366 if (!gatt_sr_is_prep_cnt_zero(p_tcb))
367 {
368 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0);
369 gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb);
370
371 for (i=0; i<GATT_MAX_APPS; i++)
372 {
373 if (p_tcb->prep_cnt[i])
374 {
375 gatt_if = (tGATT_IF) (i+1);
376 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
377 gatt_sr_send_req_callback(conn_id,
378 trans_id,
379 GATTS_REQ_TYPE_WRITE_EXEC,
380 (tGATTS_DATA *)&flag);
381 p_tcb->prep_cnt[i]= 0;
382 }
383 }
384 }
385 else /* nothing needs to be executed , send response now */
386 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700387 GATT_TRACE_ERROR("gatt_process_exec_write_req: no prepare write pending");
Mudumba Ananthcf310332014-07-04 03:45:11 -0700388 gatt_send_error_rsp(p_tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, FALSE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800389 }
390}
391
392/*******************************************************************************
393**
394** Function gatt_process_read_multi_req
395**
396** Description This function is called to process the read multiple request
397** from client.
398**
399** Returns void
400**
401*******************************************************************************/
402void gatt_process_read_multi_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
403{
404 UINT32 trans_id;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700405 UINT16 handle = 0, ll = len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800406 UINT8 *p = p_data, i_rcb;
407 tGATT_STATUS err = GATT_SUCCESS;
408 UINT8 sec_flag, key_size;
409 tGATTS_RSP *p_msg;
410
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700411 GATT_TRACE_DEBUG("gatt_process_read_multi_req" );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800412 p_tcb->sr_cmd.multi_req.num_handles = 0;
413
414 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700415 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416 &sec_flag,
417 &key_size);
418
419#if GATT_CONFORMANCE_TESTING == TRUE
420 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
421 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700422 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 -0800423
424 STREAM_TO_UINT16(handle, p);
425
426 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
427
428 return;
429 }
430#endif
431
432 while (ll >= 2 && p_tcb->sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES)
433 {
434 STREAM_TO_UINT16(handle, p);
435
436 if ((i_rcb = gatt_sr_find_i_rcb_by_handle(handle)) < GATT_MAX_SR_PROFILES)
437 {
438 p_tcb->sr_cmd.multi_req.handles[p_tcb->sr_cmd.multi_req.num_handles++] = handle;
439
440 /* check read permission */
441 if ((err = gatts_read_attr_perm_check( gatt_cb.sr_reg[i_rcb].p_db,
442 FALSE,
443 handle,
444 sec_flag,
445 key_size))
446 != GATT_SUCCESS)
447 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700448 GATT_TRACE_DEBUG("read permission denied : 0x%02x", err);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449 break;
450 }
451 }
452 else
453 {
454 /* invalid handle */
455 err = GATT_INVALID_HANDLE;
456 break;
457 }
458 ll -= 2;
459 }
460
461 if (ll != 0)
462 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700463 GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800464 }
465
466 if (p_tcb->sr_cmd.multi_req.num_handles == 0)
467 err = GATT_INVALID_HANDLE;
468
469 if (err == GATT_SUCCESS)
470 {
471 if ((trans_id = gatt_sr_enqueue_cmd (p_tcb, op_code, p_tcb->sr_cmd.multi_req.handles[0])) != 0)
472 {
473 gatt_sr_reset_cback_cnt(p_tcb); /* read multiple use multi_rsp_q's count*/
474
475 for (ll = 0; ll < p_tcb->sr_cmd.multi_req.num_handles; ll ++)
476 {
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700477 if ((p_msg = (tGATTS_RSP *)osi_getbuf(sizeof(tGATTS_RSP))) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800478 {
479 memset(p_msg, 0, sizeof(tGATTS_RSP))
480 ;
481 handle = p_tcb->sr_cmd.multi_req.handles[ll];
482 i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
483
484 p_msg->attr_value.handle = handle;
485 err = gatts_read_attr_value_by_handle(p_tcb,
486 gatt_cb.sr_reg[i_rcb].p_db,
487 op_code,
488 handle,
489 0,
490 p_msg->attr_value.value,
491 &p_msg->attr_value.len,
492 GATT_MAX_ATTR_LEN,
493 sec_flag,
494 key_size,
495 trans_id);
496
497 if (err == GATT_SUCCESS)
498 {
499 gatt_sr_process_app_rsp(p_tcb, gatt_cb.sr_reg[i_rcb].gatt_if ,trans_id, op_code, GATT_SUCCESS, p_msg);
500 }
501 /* either not using or done using the buffer, release it now */
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700502 osi_freebuf(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800503 }
504 else
505 {
506 err = GATT_NO_RESOURCES;
507 gatt_dequeue_sr_cmd(p_tcb);
508 break;
509 }
510 }
511 }
512 else
513 err = GATT_NO_RESOURCES;
514 }
515
516 /* in theroy BUSY is not possible(should already been checked), protected check */
517 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
518 gatt_send_error_rsp(p_tcb, err, op_code, handle, FALSE);
519}
520
521/*******************************************************************************
522**
523** Function gatt_build_primary_service_rsp
524**
525** Description Primamry service request processed internally. Theretically
526** only deal with ReadByTypeVAlue and ReadByGroupType.
527**
528** Returns void
529**
530*******************************************************************************/
531static tGATT_STATUS gatt_build_primary_service_rsp (BT_HDR *p_msg, tGATT_TCB *p_tcb,
532 UINT8 op_code, UINT16 s_hdl,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700533 UINT16 e_hdl, UINT8 *p_data, tBT_UUID value)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800534{
535 tGATT_STATUS status = GATT_NOT_FOUND;
536 UINT8 handle_len =4, *p ;
537 tGATT_SR_REG *p_rcb;
538 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
539 tGATT_SRV_LIST_ELEM *p_srv=NULL;
540 tBT_UUID *p_uuid;
541
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700542 UNUSED(p_data);
543
The Android Open Source Project5738f832012-12-12 16:00:35 -0800544 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
545
546 p_srv = p_list->p_first;
547
548 while (p_srv)
549 {
550 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
551
552 if (p_rcb->in_use &&
553 p_rcb->s_hdl >= s_hdl &&
554 p_rcb->s_hdl <= e_hdl &&
555 p_rcb->type == GATT_UUID_PRI_SERVICE)
556 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700557 if ((p_uuid = gatts_get_service_uuid (p_rcb->p_db)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800558 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700560 handle_len = 4 + p_uuid->len;
561
562 /* get the length byte in the repsonse */
563 if (p_msg->offset ==0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800564 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700565 *p ++ = op_code + 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800566 p_msg->len ++;
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700567 p_msg->offset = handle_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800568
569 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700570 {
571 *p ++ = (UINT8)p_msg->offset; /* length byte */
572 p_msg->len ++;
573 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800574 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700575
576 if (p_msg->len + p_msg->offset <= p_tcb->payload_size &&
577 handle_len == p_msg->offset)
578 {
579 if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
580 gatt_uuid_compare(value, *p_uuid))
581 {
582 UINT16_TO_STREAM(p, p_rcb->s_hdl);
583
584 if (p_list->p_last_primary == p_srv &&
585 p_list->p_last_primary == p_list->p_last)
586 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700587 GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute");
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700588 UINT16_TO_STREAM(p, 0xFFFF); /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
589 }
590 else
591 {
592 UINT16_TO_STREAM(p, p_rcb->e_hdl);
593 }
594
595 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
596 gatt_build_uuid_to_stream(&p, *p_uuid);
597
598 status = GATT_SUCCESS;
599 p_msg->len += p_msg->offset;
600 }
601 }
602 else
603 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800604 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800605 }
606 p_srv = p_srv->p_next;
607 }
608 p_msg->offset = L2CAP_MIN_OFFSET;
609
610 return status;
611}
612
613/*******************************************************************************
614**
615** Function gatt_build_find_info_rsp
616**
617** Description fill the find information response information in the given
618** buffer.
619**
620** Returns TRUE: if data filled sucessfully.
621** FALSE: packet full, or format mismatch.
622**
623*******************************************************************************/
624static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SR_REG *p_rcb, BT_HDR *p_msg, UINT16 *p_len,
625 UINT16 s_hdl, UINT16 e_hdl)
626{
627 tGATT_STATUS status = GATT_NOT_FOUND;
628 UINT8 *p;
629 UINT16 len = *p_len;
630 tGATT_ATTR16 *p_attr = NULL;
631 UINT8 info_pair_len[2] = {4, 18};
632
633 if (!p_rcb->p_db || !p_rcb->p_db->p_attr_list)
634 return status;
635
636 /* check the attribute database */
637 p_attr = (tGATT_ATTR16 *) p_rcb->p_db->p_attr_list;
638
639 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
640
641 while (p_attr)
642 {
643 if (p_attr->handle > e_hdl)
644 {
645 break;
646 }
647
648 if (p_attr->handle >= s_hdl)
649 {
650 if (p_msg->offset == 0)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700651 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 -0800652
653 if (len >= info_pair_len[p_msg->offset - 1])
654 {
655 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16)
656 {
657 UINT16_TO_STREAM(p, p_attr->handle);
658 UINT16_TO_STREAM(p, p_attr->uuid);
659 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700660 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 -0800661 {
662 UINT16_TO_STREAM(p, p_attr->handle);
663 ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *) p_attr)->uuid, LEN_UUID_128);
664 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700665 else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32)
666 {
667 UINT16_TO_STREAM(p, p_attr->handle);
668 gatt_convert_uuid32_to_uuid128(p, ((tGATT_ATTR32 *) p_attr)->uuid);
669 p += LEN_UUID_128;
670 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800671 else
672 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700673 GATT_TRACE_ERROR("format mismatch");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800674 status = GATT_NO_RESOURCES;
675 break;
676 /* format mismatch */
677 }
678 p_msg->len += info_pair_len[p_msg->offset - 1];
679 len -= info_pair_len[p_msg->offset - 1];
680 status = GATT_SUCCESS;
681
682 }
683 else
684 {
685 status = GATT_NO_RESOURCES;
686 break;
687 }
688 }
689 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
690 }
691
692 *p_len = len;
693 return status;
694}
695
696/*******************************************************************************
697**
698** Function gatts_internal_read_by_type_req
699**
700** Description check to see if the ReadByType request can be handled internally.
701**
702** Returns void
703**
704*******************************************************************************/
705static tGATT_STATUS gatts_validate_packet_format(UINT8 op_code, UINT16 *p_len,
706 UINT8 **p_data, tBT_UUID *p_uuid_filter,
707 UINT16 *p_s_hdl, UINT16 *p_e_hdl)
708{
709 tGATT_STATUS reason = GATT_SUCCESS;
710 UINT16 uuid_len, s_hdl = 0, e_hdl = 0;
711 UINT16 len = *p_len;
712 UINT8 *p = *p_data;
713
714 if (len >= 4)
715 {
716 /* obtain starting handle, and ending handle */
717 STREAM_TO_UINT16(s_hdl, p);
718 STREAM_TO_UINT16(e_hdl, p);
719 len -= 4;
720
721 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || !GATT_HANDLE_IS_VALID(e_hdl))
722 {
723 reason = GATT_INVALID_HANDLE;
724 }
725 /* for these PDUs, uuid filter must present */
726 else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
727 op_code == GATT_REQ_FIND_TYPE_VALUE ||
728 op_code == GATT_REQ_READ_BY_TYPE)
729 {
730 if (len >= 2 && p_uuid_filter != NULL)
731 {
732 uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
733
734 /* parse uuid now */
735 if (gatt_parse_uuid_from_cmd (p_uuid_filter, uuid_len, &p) == FALSE ||
736 p_uuid_filter->len == 0)
737 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700738 GATT_TRACE_DEBUG("UUID filter does not exsit");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800739 reason = GATT_INVALID_PDU;
740 }
741 else
742 len -= p_uuid_filter->len;
743 }
744 else
745 reason = GATT_INVALID_PDU;
746 }
747 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700748 else
749 reason = GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800750
751 *p_data = p;
752 *p_len = len;
753 *p_s_hdl = s_hdl;
754 *p_e_hdl = e_hdl;
755
756 return reason;
757}
758
759/*******************************************************************************
760**
761** Function gatts_process_primary_service_req
762**
763** Description process ReadByGroupType/ReadByTypeValue request, for discover
764** all primary services or discover primary service by UUID request.
765**
766** Returns void
767**
768*******************************************************************************/
769void gatts_process_primary_service_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
770{
771 UINT8 reason = GATT_INVALID_PDU;
772 UINT16 s_hdl = 0, e_hdl = 0;
773 tBT_UUID uuid, value, primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
774 BT_HDR *p_msg = NULL;
775 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
776
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700777 memset (&value, 0, sizeof(tBT_UUID));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800778 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
779
780 if (reason == GATT_SUCCESS)
781 {
782 if (gatt_uuid_compare(uuid, primary_service))
783 {
784 if (op_code == GATT_REQ_FIND_TYPE_VALUE)
785 {
786 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == FALSE)
787 reason = GATT_INVALID_PDU;
788 }
789
790 if (reason == GATT_SUCCESS)
791 {
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700792 if ((p_msg = (BT_HDR *)osi_getbuf(msg_len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800793 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700794 GATT_TRACE_ERROR("gatts_process_primary_service_req failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800795 reason = GATT_NO_RESOURCES;
796 }
797 else
798 {
799 memset(p_msg, 0, msg_len);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700800 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 -0800801 }
802 }
803 }
804 else
805 {
806 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
807 {
808 reason = GATT_UNSUPPORT_GRP_TYPE;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700809 GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x", uuid.uu.uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800810 }
811 else
812 {
813 /* we do not support ReadByTypeValue with any non-primamry_service type */
814 reason = GATT_NOT_FOUND;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700815 GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x", uuid.uu.uuid16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800816 }
817 }
818 }
819
820 if (reason != GATT_SUCCESS)
821 {
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700822 if (p_msg) osi_freebuf(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800823 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
824 }
825 else
826 attp_send_sr_msg(p_tcb, p_msg);
827
828}
829
830/*******************************************************************************
831**
832** Function gatts_process_find_info
833**
834** Description process find information request, for discover character
835** descriptors.
836**
837** Returns void
838**
839*******************************************************************************/
840static void gatts_process_find_info(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
841{
842 UINT8 reason = GATT_INVALID_PDU, *p;
843 UINT16 s_hdl = 0, e_hdl = 0, buf_len;
844 BT_HDR *p_msg = NULL;
845 tGATT_SR_REG *p_rcb;
846 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
847 tGATT_SRV_LIST_ELEM *p_srv=NULL;
848
849 reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, &e_hdl);
850
851 if (reason == GATT_SUCCESS)
852 {
853 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
854
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700855 if ((p_msg = (BT_HDR *)osi_getbuf(buf_len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800856 {
857 reason = GATT_NO_RESOURCES;
858 }
859 else
860 {
861 reason = GATT_NOT_FOUND;
862
863 memset(p_msg, 0, buf_len);
864 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
865 *p ++ = op_code + 1;
866 p_msg->len = 2;
867
868 buf_len = p_tcb->payload_size - 2;
869
870 p_srv = p_list->p_first;
871
872 while (p_srv)
873 {
874 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
875
876 if (p_rcb->in_use &&
877 !(p_rcb->s_hdl > e_hdl ||
878 p_rcb->e_hdl < s_hdl))
879 {
880 reason = gatt_build_find_info_rsp(p_rcb, p_msg, &buf_len, s_hdl, e_hdl);
881 if (reason == GATT_NO_RESOURCES)
882 {
883 reason = GATT_SUCCESS;
884 break;
885 }
886 }
887 p_srv = p_srv->p_next;
888 }
889 *p = (UINT8)p_msg->offset;
890
891 p_msg->offset = L2CAP_MIN_OFFSET;
892 }
893 }
894
895 if (reason != GATT_SUCCESS)
896 {
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700897 if (p_msg) osi_freebuf(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800898 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
899 }
900 else
901 attp_send_sr_msg(p_tcb, p_msg);
902
903}
904
905/*******************************************************************************
906**
907** Function gatts_process_mtu_req
908**
909** Description This function is called to process excahnge MTU request.
910** Only used on LE.
911**
912** Returns void
913**
914*******************************************************************************/
915static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data)
916{
917 UINT16 mtu = 0;
918 UINT8 *p = p_data, i;
919 BT_HDR *p_buf;
920 UINT16 conn_id;
921
The Android Open Source Project5738f832012-12-12 16:00:35 -0800922 /* BR/EDR conenction, send error response */
923 if (p_tcb->att_lcid != L2CAP_ATT_CID)
924 {
925 gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, FALSE);
926 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700927 else if (len < GATT_MTU_REQ_MIN_LEN)
928 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700929 GATT_TRACE_ERROR("invalid MTU request PDU received.");
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700930 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, FALSE);
931 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800932 else
933 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700934 STREAM_TO_UINT16 (mtu, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800935 /* mtu must be greater than default MTU which is 23/48 */
Andre Eisenbach4b638692013-06-14 13:22:25 -0700936 if (mtu < GATT_DEF_BLE_MTU_SIZE)
937 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
938 else if (mtu > GATT_MAX_MTU_SIZE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939 p_tcb->payload_size = GATT_MAX_MTU_SIZE;
Andre Eisenbach4b638692013-06-14 13:22:25 -0700940 else
941 p_tcb->payload_size = mtu;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800942
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700943 GATT_TRACE_ERROR("MTU request PDU with MTU size %d", p_tcb->payload_size);
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700944
Priti Aghera636d6712014-12-18 13:55:48 -0800945 l2cble_set_fixed_channel_tx_data_length(p_tcb->peer_bda, L2CAP_ATT_CID, p_tcb->payload_size);
946
The Android Open Source Project5738f832012-12-12 16:00:35 -0800947 if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL)
948 {
949 attp_send_sr_msg (p_tcb, p_buf);
950
951 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
952 /* of 0, as no response is allowed from applcations */
953
954 for (i = 0; i < GATT_MAX_APPS; i ++)
955 {
956 if (gatt_cb.cl_rcb[i].in_use )
957 {
958 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
959 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
960 (tGATTS_DATA *)&p_tcb->payload_size);
961 }
962 }
963
964 }
965 }
966}
967
968/*******************************************************************************
969**
970** Function gatts_process_read_by_type_req
971**
972** Description process Read By type request.
973** This PDU can be used to perform:
974** - read characteristic value
975** - read characteristic descriptor value
976** - discover characteristic
977** - discover characteristic by UUID
978** - relationship discovery
979**
980** Returns void
981**
982*******************************************************************************/
983void gatts_process_read_by_type_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
984{
985 tBT_UUID uuid;
986 tGATT_SR_REG *p_rcb;
987 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET),
988 buf_len,
989 s_hdl, e_hdl, err_hdl = 0;
990 BT_HDR *p_msg = NULL;
991 tGATT_STATUS reason, ret;
992 UINT8 *p;
993 UINT8 sec_flag, key_size;
994 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
995 tGATT_SRV_LIST_ELEM *p_srv=NULL;
996
997 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
998
999#if GATT_CONFORMANCE_TESTING == TRUE
1000 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
1001 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001002 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 -08001003
1004 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, FALSE);
1005
1006 return;
1007 }
1008#endif
1009
1010 if (reason == GATT_SUCCESS)
1011 {
Pavlin Radoslavov258c2532015-09-27 20:59:05 -07001012 if ((p_msg = (BT_HDR *)osi_getbuf(msg_len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001013 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001014 GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001015
1016 reason = GATT_NO_RESOURCES;
1017 }
1018 else
1019 {
1020 memset(p_msg, 0, msg_len);
1021 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1022
1023 *p ++ = op_code + 1;
1024 /* reserve length byte */
1025 p_msg->len = 2;
1026 buf_len = p_tcb->payload_size - 2;
1027
1028 reason = GATT_NOT_FOUND;
1029
1030 p_srv = p_list->p_first;
1031
1032 while (p_srv)
1033 {
1034 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
1035
1036 if (p_rcb->in_use &&
1037 !(p_rcb->s_hdl > e_hdl ||
1038 p_rcb->e_hdl < s_hdl))
1039 {
1040 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001041 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001042 &sec_flag,
1043 &key_size);
1044
1045 ret = gatts_db_read_attr_value_by_type(p_tcb,
1046 p_rcb->p_db,
1047 op_code,
1048 p_msg,
1049 s_hdl,
1050 e_hdl,
1051 uuid,
1052 &buf_len,
1053 sec_flag,
1054 key_size,
1055 0,
1056 &err_hdl);
1057 if (ret != GATT_NOT_FOUND)
1058 {
1059 reason = ret;
1060
1061 if (ret == GATT_NO_RESOURCES)
1062 reason = GATT_SUCCESS;
1063 }
1064 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND)
1065 {
1066 s_hdl = err_hdl;
1067 break;
1068 }
1069 }
1070 p_srv = p_srv->p_next;
1071 }
1072 *p = (UINT8)p_msg->offset;
1073 p_msg->offset = L2CAP_MIN_OFFSET;
1074 }
1075 }
1076 if (reason != GATT_SUCCESS)
1077 {
Pavlin Radoslavov258c2532015-09-27 20:59:05 -07001078 if (p_msg) osi_freebuf(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001079
1080 /* in theroy BUSY is not possible(should already been checked), protected check */
1081 if (reason != GATT_PENDING && reason != GATT_BUSY)
1082 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
1083 }
1084 else
1085 attp_send_sr_msg(p_tcb, p_msg);
1086
1087}
1088
1089/*******************************************************************************
1090**
1091** Function gatts_process_write_req
1092**
1093** Description This function is called to process the write request
1094** from client.
1095**
1096** Returns void
1097**
1098*******************************************************************************/
1099void gatts_process_write_req (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
1100 UINT8 op_code, UINT16 len, UINT8 *p_data)
1101{
1102 tGATTS_DATA sr_data;
1103 UINT32 trans_id;
1104 tGATT_STATUS status;
1105 UINT8 sec_flag, key_size, *p = p_data;
1106 tGATT_SR_REG *p_sreg;
1107 UINT16 conn_id;
1108
1109 memset(&sr_data, 0, sizeof(tGATTS_DATA));
1110
1111 switch (op_code)
1112 {
1113 case GATT_REQ_PREPARE_WRITE:
1114 sr_data.write_req.is_prep = TRUE;
1115 STREAM_TO_UINT16(sr_data.write_req.offset, p);
1116 len -= 2;
1117 /* fall through */
1118 case GATT_SIGN_CMD_WRITE:
1119 if (op_code == GATT_SIGN_CMD_WRITE)
1120 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001121 GATT_TRACE_DEBUG("Write CMD with data sigining" );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001122 len -= GATT_AUTH_SIGN_LEN;
1123 }
1124 /* fall through */
1125 case GATT_CMD_WRITE:
1126 case GATT_REQ_WRITE:
1127 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
1128 sr_data.write_req.need_rsp = TRUE;
1129 sr_data.write_req.handle = handle;
1130 sr_data.write_req.len = len;
Satya Calloji444a8da2015-03-06 10:38:22 -08001131 if (len != 0 && p != NULL)
1132 {
1133 memcpy (sr_data.write_req.value, p, len);
1134 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001135 break;
1136 }
1137
1138 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001139 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001140 &sec_flag,
1141 &key_size);
1142
1143 status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
1144 op_code,
1145 handle,
1146 sr_data.write_req.offset,
1147 p,
1148 len,
1149 sec_flag,
1150 key_size);
1151
1152 if (status == GATT_SUCCESS)
1153 {
1154 if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0)
1155 {
1156 p_sreg = &gatt_cb.sr_reg[i_rcb];
1157 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
1158 gatt_sr_send_req_callback(conn_id,
1159 trans_id,
1160 GATTS_REQ_TYPE_WRITE,
1161 &sr_data);
1162
1163 status = GATT_PENDING;
1164 }
1165 else
1166 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001167 GATT_TRACE_ERROR("max pending command, send error");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001168 status = GATT_BUSY; /* max pending command, application error */
1169 }
1170 }
1171
1172 /* in theroy BUSY is not possible(should already been checked), protected check */
1173 if (status != GATT_PENDING && status != GATT_BUSY &&
1174 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE))
1175 {
1176 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1177 }
1178 return;
1179}
1180
1181/*******************************************************************************
1182**
1183** Function gatts_process_read_req
1184**
1185** Description This function is called to process the read request
1186** from client.
1187**
1188** Returns void
1189**
1190*******************************************************************************/
1191static 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 -07001192 UINT16 handle, UINT16 len, UINT8 *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001193{
1194 UINT16 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
1195 tGATT_STATUS reason;
1196 BT_HDR *p_msg = NULL;
1197 UINT8 sec_flag, key_size, *p;
1198 UINT16 offset = 0, value_len = 0;
1199
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001200 UNUSED (len);
Pavlin Radoslavov258c2532015-09-27 20:59:05 -07001201 if ((p_msg = (BT_HDR *)osi_getbuf(buf_len)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001202 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001203 GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001204
1205 reason = GATT_NO_RESOURCES;
1206 }
1207 else
1208 {
1209 if (op_code == GATT_REQ_READ_BLOB)
1210 STREAM_TO_UINT16(offset, p_data);
1211
1212 memset(p_msg, 0, buf_len);
1213 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1214 *p ++ = op_code + 1;
1215 p_msg->len = 1;
1216 buf_len = p_tcb->payload_size - 1;
1217
1218 gatt_sr_get_sec_info(p_tcb->peer_bda,
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001219 p_tcb->transport,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001220 &sec_flag,
1221 &key_size);
1222
1223 reason = gatts_read_attr_value_by_handle(p_tcb,
1224 p_rcb->p_db,
1225 op_code,
1226 handle,
1227 offset,
1228 p,
1229 &value_len,
1230 buf_len,
1231 sec_flag,
1232 key_size,
1233 0);
1234
1235 p_msg->len += value_len;
1236 }
1237
1238 if (reason != GATT_SUCCESS)
1239 {
Pavlin Radoslavov258c2532015-09-27 20:59:05 -07001240 if (p_msg) osi_freebuf(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001241
1242 /* in theroy BUSY is not possible(should already been checked), protected check */
1243 if (reason != GATT_PENDING && reason != GATT_BUSY)
1244 gatt_send_error_rsp (p_tcb, reason, op_code, handle, FALSE);
1245 }
1246 else
1247 attp_send_sr_msg(p_tcb, p_msg);
1248
1249}
1250
1251/*******************************************************************************
1252**
1253** Function gatts_process_attribute_req
1254**
1255** Description This function is called to process the per attribute handle request
1256** from client.
1257**
1258** Returns void
1259**
1260*******************************************************************************/
1261void gatts_process_attribute_req (tGATT_TCB *p_tcb, UINT8 op_code,
1262 UINT16 len, UINT8 *p_data)
1263{
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001264 UINT16 handle = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001265 UINT8 *p = p_data, i;
1266 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1267 tGATT_STATUS status = GATT_INVALID_HANDLE;
1268 tGATT_ATTR16 *p_attr;
1269
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001270 if (len < 2)
1271 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001272 GATT_TRACE_ERROR("Illegal PDU length, discard request");
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001273 status = GATT_INVALID_PDU;
1274 }
1275 else
1276 {
1277 STREAM_TO_UINT16(handle, p);
1278 len -= 2;
1279 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001280
1281#if GATT_CONFORMANCE_TESTING == TRUE
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001282 gatt_cb.handle = handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001283 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
1284 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001285 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 -08001286
1287 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
1288
1289 return;
1290 }
1291#endif
1292
1293 if (GATT_HANDLE_IS_VALID(handle))
1294 {
1295 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1296 {
1297 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1298 {
1299 p_attr = (tGATT_ATTR16 *)p_rcb->p_db->p_attr_list;
1300
1301 while (p_attr)
1302 {
1303 if (p_attr->handle == handle)
1304 {
1305 switch (op_code)
1306 {
1307 case GATT_REQ_READ: /* read char/char descriptor value */
1308 case GATT_REQ_READ_BLOB:
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001309 gatts_process_read_req(p_tcb, p_rcb, op_code, handle, len, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001310 break;
1311
1312 case GATT_REQ_WRITE: /* write char/char descriptor value */
1313 case GATT_CMD_WRITE:
1314 case GATT_SIGN_CMD_WRITE:
1315 case GATT_REQ_PREPARE_WRITE:
1316 gatts_process_write_req(p_tcb, i, handle, op_code, len, p);
1317 break;
1318 default:
1319 break;
1320 }
1321 status = GATT_SUCCESS;
1322 break;
1323 }
1324 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
1325 }
1326 break;
1327 }
1328 }
1329 }
1330
1331 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE)
1332 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1333}
1334
1335/*******************************************************************************
1336**
1337** Function gatts_proc_srv_chg_ind_ack
1338**
1339** Description This function process the service changed indicaiton ACK
1340**
1341** Returns void
1342**
1343*******************************************************************************/
1344static void gatts_proc_srv_chg_ind_ack(tGATT_TCB *p_tcb )
1345{
1346 tGATTS_SRV_CHG_REQ req;
1347 tGATTS_SRV_CHG *p_buf = NULL;
1348
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001349 GATT_TRACE_DEBUG("gatts_proc_srv_chg_ind_ack");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001350
1351 if ((p_buf = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
1352 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001353 GATT_TRACE_DEBUG("NV update set srv chg = FALSE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001354 p_buf->srv_changed = FALSE;
1355 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1356 if (gatt_cb.cb_info.p_srv_chg_callback)
1357 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,&req, NULL);
1358 }
1359}
1360
1361/*******************************************************************************
1362**
1363** Function gatts_chk_pending_ind
1364**
1365** Description This function check any pending indication needs to be sent if
1366** there is a pending indication then sent the indication
1367**
1368** Returns void
1369**
1370*******************************************************************************/
1371static void gatts_chk_pending_ind(tGATT_TCB *p_tcb )
1372{
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001373 GATT_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001374
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001375 tGATT_VALUE *p_buf = (tGATT_VALUE *)fixed_queue_try_peek_first(p_tcb->pending_ind_q);
1376 if (p_buf != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001377 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001378 GATTS_HandleValueIndication(p_buf->conn_id,
1379 p_buf->handle,
1380 p_buf->len,
1381 p_buf->value);
Pavlin Radoslavov258c2532015-09-27 20:59:05 -07001382 osi_freebuf(fixed_queue_try_remove_from_queue(p_tcb->pending_ind_q,
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -07001383 p_buf));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001384 }
1385}
1386
1387/*******************************************************************************
1388**
1389** Function gatts_proc_ind_ack
1390**
1391** Description This function process the Indication ack
1392**
1393** Returns TRUE continue to process the indication ack by the aaplication
1394** if the ACk is not a Service Changed Indication Ack
1395**
1396*******************************************************************************/
1397static BOOLEAN gatts_proc_ind_ack(tGATT_TCB *p_tcb, UINT16 ack_handle)
1398{
1399 BOOLEAN continue_processing = TRUE;
1400
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001401 GATT_TRACE_DEBUG ("gatts_proc_ind_ack ack handle=%d", ack_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001402
1403 if (ack_handle == gatt_cb.handle_of_h_r)
1404 {
1405 gatts_proc_srv_chg_ind_ack(p_tcb);
1406 /* there is no need to inform the application since srv chg is handled internally by GATT */
1407 continue_processing = FALSE;
1408 }
1409
1410 gatts_chk_pending_ind(p_tcb);
1411 return continue_processing;
1412}
1413
1414/*******************************************************************************
1415**
1416** Function gatts_process_value_conf
1417**
1418** Description This function is called to process the handle value confirmation.
1419**
1420** Returns void
1421**
1422*******************************************************************************/
1423void gatts_process_value_conf(tGATT_TCB *p_tcb, UINT8 op_code)
1424{
1425 UINT16 handle = p_tcb->indicate_handle;
1426 UINT32 trans_id;
1427 UINT8 i;
1428 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1429 BOOLEAN continue_processing;
1430 UINT16 conn_id;
1431
1432 btu_stop_timer (&p_tcb->conf_timer_ent);
1433 if (GATT_HANDLE_IS_VALID(handle))
1434 {
1435 p_tcb->indicate_handle = 0;
1436 continue_processing = gatts_proc_ind_ack(p_tcb, handle);
1437
1438 if (continue_processing)
1439 {
1440 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1441 {
1442 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1443 {
1444 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
1445 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_rcb->gatt_if);
1446 gatt_sr_send_req_callback(conn_id,
1447 trans_id, GATTS_REQ_TYPE_CONF, (tGATTS_DATA *)&handle);
1448 }
1449 }
1450 }
1451 }
1452 else
1453 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001454 GATT_TRACE_ERROR("unexpected handle value confirmation");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001455 }
1456}
1457
1458/*******************************************************************************
1459**
1460** Function gatt_server_handle_client_req
1461**
1462** Description This function is called to handle the client requests to
1463** server.
1464**
1465**
1466** Returns void
1467**
1468*******************************************************************************/
1469void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
1470 UINT16 len, UINT8 *p_data)
1471{
1472 /* there is pending command, discard this one */
1473 if (!gatt_sr_cmd_empty(p_tcb) && op_code != GATT_HANDLE_VALUE_CONF)
1474 return;
1475
1476 /* the size of the message may not be bigger than the local max PDU size*/
1477 /* The message has to be smaller than the agreed MTU, len does not include op code */
1478 if (len >= p_tcb->payload_size)
1479 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001480 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 -08001481 /* for invalid request expecting response, send it now */
1482 if (op_code != GATT_CMD_WRITE &&
1483 op_code != GATT_SIGN_CMD_WRITE &&
1484 op_code != GATT_HANDLE_VALUE_CONF)
1485 {
1486 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, op_code, 0, FALSE);
1487 }
1488 /* otherwise, ignore the pkt */
1489 }
1490 else
1491 {
1492 switch (op_code)
1493 {
1494 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1495 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1496 gatts_process_primary_service_req (p_tcb, op_code, len, p_data);
1497 break;
1498
1499 case GATT_REQ_FIND_INFO:/* discover char descrptor */
1500 gatts_process_find_info(p_tcb, op_code, len, p_data);
1501 break;
1502
1503 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor value */
1504 /* discover characteristic, discover char by UUID */
1505 gatts_process_read_by_type_req(p_tcb, op_code, len, p_data);
1506 break;
1507
1508
1509 case GATT_REQ_READ: /* read char/char descriptor value */
1510 case GATT_REQ_READ_BLOB:
1511 case GATT_REQ_WRITE: /* write char/char descriptor value */
1512 case GATT_CMD_WRITE:
1513 case GATT_SIGN_CMD_WRITE:
1514 case GATT_REQ_PREPARE_WRITE:
1515 gatts_process_attribute_req (p_tcb, op_code, len, p_data);
1516 break;
1517
1518 case GATT_HANDLE_VALUE_CONF:
1519 gatts_process_value_conf (p_tcb, op_code);
1520 break;
1521
1522 case GATT_REQ_MTU:
1523 gatts_process_mtu_req (p_tcb, len, p_data);
1524 break;
1525
1526 case GATT_REQ_EXEC_WRITE:
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001527 gatt_process_exec_write_req (p_tcb, op_code, len, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001528 break;
1529
1530 case GATT_REQ_READ_MULTI:
1531 gatt_process_read_multi_req (p_tcb, op_code, len, p_data);
1532 break;
1533
1534 default:
1535 break;
1536 }
1537 }
1538}
1539
1540#endif /* BLE_INCLUDED */