blob: c006dd263b8bff2e0be265e4112ff8841e52e64a [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2008-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 * this file contains the GATT server functions
22 *
23 ******************************************************************************/
24
25#include "bt_target.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080026#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080027
28#if BLE_INCLUDED == TRUE
29#include <string.h>
30#include "gatt_int.h"
31#include "l2c_api.h"
32
Andre Eisenbachccf9c152013-10-02 15:37:21 -070033#define GATT_MTU_REQ_MIN_LEN 2
34
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
36/*******************************************************************************
37**
38** Function gatt_sr_enqueue_cmd
39**
40** Description This function enqueue the request from client which needs a
41** application response, and update the transaction ID.
42**
43** Returns void
44**
45*******************************************************************************/
46UINT32 gatt_sr_enqueue_cmd (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 handle)
47{
48 tGATT_SR_CMD *p_cmd = &p_tcb->sr_cmd;
49 UINT32 trans_id = 0;
50
51 if ( (p_cmd->op_code == 0) ||
52 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
53 {
54 if (op_code == GATT_CMD_WRITE ||
55 op_code == GATT_SIGN_CMD_WRITE ||
56 op_code == GATT_REQ_MTU ||
57 op_code == GATT_HANDLE_VALUE_CONF)
58 {
59 trans_id = ++p_tcb->trans_id;
60 }
61 else
62 {
63 p_cmd->trans_id = ++p_tcb->trans_id;
64 p_cmd->op_code = op_code;
65 p_cmd->handle = handle;
66 p_cmd->status = GATT_NOT_FOUND;
67 p_tcb->trans_id %= GATT_TRANS_ID_MAX;
68 trans_id = p_cmd->trans_id;
69 }
70 }
71
72 return trans_id;
73}
74
75/*******************************************************************************
76**
77** Function gatt_sr_cmd_empty
78**
79** Description This function check the server command queue is empty or not.
80**
81** Returns TRUE if empty, FALSE if there is pending command.
82**
83*******************************************************************************/
84BOOLEAN gatt_sr_cmd_empty (tGATT_TCB *p_tcb)
85{
86 return(p_tcb->sr_cmd.op_code == 0);
87}
88
89/*******************************************************************************
90**
91** Function gatt_dequeue_sr_cmd
92**
93** Description This function dequeue the request from command queue.
94**
95** Returns void
96**
97*******************************************************************************/
98void gatt_dequeue_sr_cmd (tGATT_TCB *p_tcb)
99{
100 /* Double check in case any buffers are queued */
101 GATT_TRACE_DEBUG0("gatt_dequeue_sr_cmd" );
102 if (p_tcb->sr_cmd.p_rsp_msg)
103 {
104 GATT_TRACE_ERROR1("free p_tcb->sr_cmd.p_rsp_msg = %d", p_tcb->sr_cmd.p_rsp_msg);
105
106 GKI_freebuf (p_tcb->sr_cmd.p_rsp_msg);
107 }
108
109 while (p_tcb->sr_cmd.multi_rsp_q.p_first)
110 GKI_freebuf (GKI_dequeue (&p_tcb->sr_cmd.multi_rsp_q));
111 memset( &p_tcb->sr_cmd, 0, sizeof(tGATT_SR_CMD));
112}
113
114/*******************************************************************************
115**
116** Function process_read_multi_rsp
117**
118** Description This function check the read multiple response.
119**
120** Returns BOOLEAN if all replies have been received
121**
122*******************************************************************************/
123static BOOLEAN process_read_multi_rsp (tGATT_SR_CMD *p_cmd, tGATT_STATUS status,
124 tGATTS_RSP *p_msg, UINT16 mtu)
125{
126 tGATTS_RSP *p_rsp;
127 UINT16 ii, total_len, len;
128 BT_HDR *p_buf = (BT_HDR *)GKI_getbuf((UINT16)sizeof(tGATTS_RSP));
129 UINT8 *p;
130 BOOLEAN is_overflow = FALSE;
131
132 GATT_TRACE_DEBUG2 ("process_read_multi_rsp status=%d mtu=%d", status, mtu);
133
134 if (p_buf == NULL)
135 {
136 p_cmd->status = GATT_INSUF_RESOURCE;
137 return FALSE;
138 }
139
140 /* Enqueue the response */
141 memcpy((void *)p_buf, (const void *)p_msg, sizeof(tGATTS_RSP));
142 GKI_enqueue (&p_cmd->multi_rsp_q, p_buf);
143
144 p_cmd->status = status;
145 if (status == GATT_SUCCESS)
146 {
147 GATT_TRACE_DEBUG2 ("Multi read count=%d num_hdls=%d",
148 p_cmd->multi_rsp_q.count, p_cmd->multi_req.num_handles);
149 /* Wait till we get all the responses */
150 if (p_cmd->multi_rsp_q.count == p_cmd->multi_req.num_handles)
151 {
152 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
153 if ((p_buf = (BT_HDR *)GKI_getbuf(len)) == NULL)
154 {
155 p_cmd->status = GATT_INSUF_RESOURCE;
156 return(TRUE);
157 }
158
159 memset(p_buf, 0, len);
160 p_buf->offset = L2CAP_MIN_OFFSET;
161 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
162
163 /* First byte in the response is the opcode */
164 *p++ = GATT_RSP_READ_MULTI;
165 p_buf->len = 1;
166
167 /* Now walk through the buffers puting the data into the response in order */
168 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++)
169 {
170 if (ii==0)
171 {
172 p_rsp = (tGATTS_RSP *)GKI_getfirst (&p_cmd->multi_rsp_q);
173 }
174 else
175 {
176 p_rsp = (tGATTS_RSP *)GKI_getnext (p_rsp);
177 }
178
179 if (p_rsp != NULL)
180 {
181
182 total_len = (p_buf->len + p_rsp->attr_value.len);
183
184 if (total_len > mtu)
185 {
186 /* just send the partial response for the overflow case */
187 len = p_rsp->attr_value.len - (total_len - mtu);
188 is_overflow = TRUE;
189 GATT_TRACE_DEBUG2 ("multi read overflow available len=%d val_len=%d", len, p_rsp->attr_value.len );
190 }
191 else
192 {
193 len = p_rsp->attr_value.len;
194 }
195
196 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii])
197 {
198 memcpy (p, p_rsp->attr_value.value, len);
199 if (!is_overflow)
200 p += len;
201 p_buf->len += len;
202 }
203 else
204 {
205 p_cmd->status = GATT_NOT_FOUND;
206 break;
207 }
208
209 if (is_overflow)
210 break;
211
212 }
213 else
214 {
215 p_cmd->status = GATT_NOT_FOUND;
216 break;
217 }
218
219 } /* loop through all handles*/
220
221
222 /* Sanity check on the buffer length */
223 if (p_buf->len == 0)
224 {
225 GATT_TRACE_ERROR0("process_read_multi_rsp - nothing found!!");
226 p_cmd->status = GATT_NOT_FOUND;
227 GKI_freebuf (p_buf);
228 GATT_TRACE_DEBUG0(" GKI_freebuf (p_buf)");
229 }
230 else if (p_cmd->p_rsp_msg != NULL)
231 {
232 GKI_freebuf (p_buf);
233 }
234 else
235 {
236 p_cmd->p_rsp_msg = p_buf;
237 }
238
239 return(TRUE);
240 }
241 }
242 else /* any handle read exception occurs, return error */
243 {
244 return(TRUE);
245 }
246
247 /* If here, still waiting */
248 return(FALSE);
249}
250
251/*******************************************************************************
252**
253** Function gatt_sr_process_app_rsp
254**
255** Description This function checks whether the response message from application
256** match any pending request or not.
257**
258** Returns void
259**
260*******************************************************************************/
261tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if,
262 UINT32 trans_id, UINT8 op_code,
263 tGATT_STATUS status, tGATTS_RSP *p_msg)
264{
265 tGATT_STATUS ret_code = GATT_SUCCESS;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800266 UNUSED(trans_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800267
268 GATT_TRACE_DEBUG1("gatt_sr_process_app_rsp gatt_if=%d", gatt_if);
269
270 gatt_sr_update_cback_cnt(p_tcb, gatt_if, FALSE, FALSE);
271
272 if (op_code == GATT_REQ_READ_MULTI)
273 {
274 /* If no error and still waiting, just return */
275 if (!process_read_multi_rsp (&p_tcb->sr_cmd, status, p_msg, p_tcb->payload_size))
276 return(GATT_SUCCESS);
277 }
278 else
279 {
280 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
281 gatt_sr_update_prep_cnt(p_tcb, gatt_if, TRUE, FALSE);
282
283 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
284 gatt_sr_reset_cback_cnt(p_tcb);
285
286 p_tcb->sr_cmd.status = status;
287
288 if (gatt_sr_is_cback_cnt_zero(p_tcb)
289 && status == GATT_SUCCESS)
290 {
291 if (p_tcb->sr_cmd.p_rsp_msg == NULL)
292 {
293 p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg (p_tcb, (UINT8)(op_code + 1), (tGATT_SR_MSG *)p_msg);
294 }
295 else
296 {
297 GATT_TRACE_ERROR0("Exception!!! already has respond message");
298 }
299 }
300 }
301 if (gatt_sr_is_cback_cnt_zero(p_tcb))
302 {
303 if ( (p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg) )
304 {
305 ret_code = attp_send_sr_msg (p_tcb, p_tcb->sr_cmd.p_rsp_msg);
306 p_tcb->sr_cmd.p_rsp_msg = NULL;
307 }
308 else
309 {
310 ret_code = gatt_send_error_rsp (p_tcb, status, op_code, p_tcb->sr_cmd.handle, FALSE);
311 }
312
313 gatt_dequeue_sr_cmd(p_tcb);
314 }
315
316 GATT_TRACE_DEBUG1("gatt_sr_process_app_rsp ret_code=%d", ret_code);
317
318 return ret_code;
319}
320
321/*******************************************************************************
322**
323** Function gatt_process_exec_write_req
324**
325** Description This function is called to process the execute write request
326** from client.
327**
328** Returns void
329**
330*******************************************************************************/
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800331static void gatt_process_exec_write_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT8 *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800332{
333 UINT8 *p = p_data, flag, i = 0;
334 UINT32 trans_id = 0;
335 BT_HDR *p_buf;
336 tGATT_IF gatt_if;
337 UINT16 conn_id;
338
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800339#if GATT_CONFORMANCE_TESTING == TRUE
340 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
341 {
342 GATT_TRACE_DEBUG2("conf test forced err rsp for %s error status=%d",
343 __FUNCTION__,gatt_cb.err_status);
344
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700345 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 -0800346
347 return;
348 }
349#endif
350
The Android Open Source Project5738f832012-12-12 16:00:35 -0800351 STREAM_TO_UINT8(flag, p);
352
353 /* mask the flag */
354 flag &= GATT_PREP_WRITE_EXEC;
355
356
357 /* no prep write is queued */
358 if (!gatt_sr_is_prep_cnt_zero(p_tcb))
359 {
360 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0);
361 gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb);
362
363 for (i=0; i<GATT_MAX_APPS; i++)
364 {
365 if (p_tcb->prep_cnt[i])
366 {
367 gatt_if = (tGATT_IF) (i+1);
368 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
369 gatt_sr_send_req_callback(conn_id,
370 trans_id,
371 GATTS_REQ_TYPE_WRITE_EXEC,
372 (tGATTS_DATA *)&flag);
373 p_tcb->prep_cnt[i]= 0;
374 }
375 }
376 }
377 else /* nothing needs to be executed , send response now */
378 {
379 GATT_TRACE_ERROR0("gatt_process_exec_write_req: no prepare write pending");
380
381 if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_EXEC_WRITE, NULL)) != NULL)
382 {
383 attp_send_sr_msg (p_tcb, p_buf);
384 }
385
386 }
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
408 GATT_TRACE_DEBUG0("gatt_process_read_multi_req" );
409 p_tcb->sr_cmd.multi_req.num_handles = 0;
410
411 gatt_sr_get_sec_info(p_tcb->peer_bda,
412 (BOOLEAN)(p_tcb->att_lcid == L2CAP_ATT_CID),
413 &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 {
419 GATT_TRACE_DEBUG1("Conformance tst: forced err rspvofr ReadMultiple: error status=%d", gatt_cb.err_status);
420
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 {
445 GATT_TRACE_DEBUG1("read permission denied : 0x%02x", err);
446 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 {
460 GATT_TRACE_ERROR0("max attribute handle reached in ReadMultiple Request.");
461 }
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 {
474 if ((p_msg = (tGATTS_RSP *)GKI_getbuf(sizeof(tGATTS_RSP))) != NULL)
475 {
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 */
499 GKI_freebuf(p_msg);
500 }
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,
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800530 UINT16 e_hdl, 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
539 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
540
541 p_srv = p_list->p_first;
542
543 while (p_srv)
544 {
545 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
546
547 if (p_rcb->in_use &&
548 p_rcb->s_hdl >= s_hdl &&
549 p_rcb->s_hdl <= e_hdl &&
550 p_rcb->type == GATT_UUID_PRI_SERVICE)
551 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700552 if ((p_uuid = gatts_get_service_uuid (p_rcb->p_db)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800553 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800554 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700555 handle_len = 4 + p_uuid->len;
556
557 /* get the length byte in the repsonse */
558 if (p_msg->offset ==0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700560 *p ++ = op_code + 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561 p_msg->len ++;
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700562 p_msg->offset = handle_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563
564 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700565 {
566 *p ++ = (UINT8)p_msg->offset; /* length byte */
567 p_msg->len ++;
568 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700570
571 if (p_msg->len + p_msg->offset <= p_tcb->payload_size &&
572 handle_len == p_msg->offset)
573 {
574 if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
575 gatt_uuid_compare(value, *p_uuid))
576 {
577 UINT16_TO_STREAM(p, p_rcb->s_hdl);
578
579 if (p_list->p_last_primary == p_srv &&
580 p_list->p_last_primary == p_list->p_last)
581 {
582 GATT_TRACE_DEBUG0("Use 0xFFFF for the last primary attribute");
583 UINT16_TO_STREAM(p, 0xFFFF); /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
584 }
585 else
586 {
587 UINT16_TO_STREAM(p, p_rcb->e_hdl);
588 }
589
590 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
591 gatt_build_uuid_to_stream(&p, *p_uuid);
592
593 status = GATT_SUCCESS;
594 p_msg->len += p_msg->offset;
595 }
596 }
597 else
598 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800599 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600 }
601 p_srv = p_srv->p_next;
602 }
603 p_msg->offset = L2CAP_MIN_OFFSET;
604
605 return status;
606}
607
608/*******************************************************************************
609**
610** Function gatt_build_find_info_rsp
611**
612** Description fill the find information response information in the given
613** buffer.
614**
615** Returns TRUE: if data filled sucessfully.
616** FALSE: packet full, or format mismatch.
617**
618*******************************************************************************/
619static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SR_REG *p_rcb, BT_HDR *p_msg, UINT16 *p_len,
620 UINT16 s_hdl, UINT16 e_hdl)
621{
622 tGATT_STATUS status = GATT_NOT_FOUND;
623 UINT8 *p;
624 UINT16 len = *p_len;
625 tGATT_ATTR16 *p_attr = NULL;
626 UINT8 info_pair_len[2] = {4, 18};
627
628 if (!p_rcb->p_db || !p_rcb->p_db->p_attr_list)
629 return status;
630
631 /* check the attribute database */
632 p_attr = (tGATT_ATTR16 *) p_rcb->p_db->p_attr_list;
633
634 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
635
636 while (p_attr)
637 {
638 if (p_attr->handle > e_hdl)
639 {
640 break;
641 }
642
643 if (p_attr->handle >= s_hdl)
644 {
645 if (p_msg->offset == 0)
646 p_msg->offset = (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_128) ? GATT_INFO_TYPE_PAIR_128 : GATT_INFO_TYPE_PAIR_16;
647
648 if (len >= info_pair_len[p_msg->offset - 1])
649 {
650 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16)
651 {
652 UINT16_TO_STREAM(p, p_attr->handle);
653 UINT16_TO_STREAM(p, p_attr->uuid);
654 }
655 else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
656 p_attr->uuid_type == GATT_ATTR_UUID_TYPE_128 )
657 {
658 UINT16_TO_STREAM(p, p_attr->handle);
659 ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *) p_attr)->uuid, LEN_UUID_128);
660 }
661 else
662 {
663 GATT_TRACE_ERROR0("format mismatch");
664 status = GATT_NO_RESOURCES;
665 break;
666 /* format mismatch */
667 }
668 p_msg->len += info_pair_len[p_msg->offset - 1];
669 len -= info_pair_len[p_msg->offset - 1];
670 status = GATT_SUCCESS;
671
672 }
673 else
674 {
675 status = GATT_NO_RESOURCES;
676 break;
677 }
678 }
679 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
680 }
681
682 *p_len = len;
683 return status;
684}
685
686/*******************************************************************************
687**
688** Function gatts_internal_read_by_type_req
689**
690** Description check to see if the ReadByType request can be handled internally.
691**
692** Returns void
693**
694*******************************************************************************/
695static tGATT_STATUS gatts_validate_packet_format(UINT8 op_code, UINT16 *p_len,
696 UINT8 **p_data, tBT_UUID *p_uuid_filter,
697 UINT16 *p_s_hdl, UINT16 *p_e_hdl)
698{
699 tGATT_STATUS reason = GATT_SUCCESS;
700 UINT16 uuid_len, s_hdl = 0, e_hdl = 0;
701 UINT16 len = *p_len;
702 UINT8 *p = *p_data;
703
704 if (len >= 4)
705 {
706 /* obtain starting handle, and ending handle */
707 STREAM_TO_UINT16(s_hdl, p);
708 STREAM_TO_UINT16(e_hdl, p);
709 len -= 4;
710
711 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || !GATT_HANDLE_IS_VALID(e_hdl))
712 {
713 reason = GATT_INVALID_HANDLE;
714 }
715 /* for these PDUs, uuid filter must present */
716 else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
717 op_code == GATT_REQ_FIND_TYPE_VALUE ||
718 op_code == GATT_REQ_READ_BY_TYPE)
719 {
720 if (len >= 2 && p_uuid_filter != NULL)
721 {
722 uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
723
724 /* parse uuid now */
725 if (gatt_parse_uuid_from_cmd (p_uuid_filter, uuid_len, &p) == FALSE ||
726 p_uuid_filter->len == 0)
727 {
728 GATT_TRACE_DEBUG0("UUID filter does not exsit");
729 reason = GATT_INVALID_PDU;
730 }
731 else
732 len -= p_uuid_filter->len;
733 }
734 else
735 reason = GATT_INVALID_PDU;
736 }
737 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700738 else
739 reason = GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800740
741 *p_data = p;
742 *p_len = len;
743 *p_s_hdl = s_hdl;
744 *p_e_hdl = e_hdl;
745
746 return reason;
747}
748
749/*******************************************************************************
750**
751** Function gatts_process_primary_service_req
752**
753** Description process ReadByGroupType/ReadByTypeValue request, for discover
754** all primary services or discover primary service by UUID request.
755**
756** Returns void
757**
758*******************************************************************************/
759void gatts_process_primary_service_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
760{
761 UINT8 reason = GATT_INVALID_PDU;
762 UINT16 s_hdl = 0, e_hdl = 0;
763 tBT_UUID uuid, value, primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
764 BT_HDR *p_msg = NULL;
765 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
766
767 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
768
769 if (reason == GATT_SUCCESS)
770 {
771 if (gatt_uuid_compare(uuid, primary_service))
772 {
773 if (op_code == GATT_REQ_FIND_TYPE_VALUE)
774 {
775 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == FALSE)
776 reason = GATT_INVALID_PDU;
777 }
778
779 if (reason == GATT_SUCCESS)
780 {
781 if ((p_msg = (BT_HDR *)GKI_getbuf(msg_len)) == NULL)
782 {
783 GATT_TRACE_ERROR0("gatts_process_primary_service_req failed. no resources.");
784 reason = GATT_NO_RESOURCES;
785 }
786 else
787 {
788 memset(p_msg, 0, msg_len);
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800789 reason = gatt_build_primary_service_rsp (p_msg, p_tcb, op_code, s_hdl, e_hdl, value);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 }
791 }
792 }
793 else
794 {
795 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
796 {
797 reason = GATT_UNSUPPORT_GRP_TYPE;
798 GATT_TRACE_DEBUG1("unexpected ReadByGrpType Group: 0x%04x", uuid.uu.uuid16);
799 }
800 else
801 {
802 /* we do not support ReadByTypeValue with any non-primamry_service type */
803 reason = GATT_NOT_FOUND;
804 GATT_TRACE_DEBUG1("unexpected ReadByTypeValue type: 0x%04x", uuid.uu.uuid16);
805 }
806 }
807 }
808
809 if (reason != GATT_SUCCESS)
810 {
811 if (p_msg) GKI_freebuf(p_msg);
812 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
813 }
814 else
815 attp_send_sr_msg(p_tcb, p_msg);
816
817}
818
819/*******************************************************************************
820**
821** Function gatts_process_find_info
822**
823** Description process find information request, for discover character
824** descriptors.
825**
826** Returns void
827**
828*******************************************************************************/
829static void gatts_process_find_info(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
830{
831 UINT8 reason = GATT_INVALID_PDU, *p;
832 UINT16 s_hdl = 0, e_hdl = 0, buf_len;
833 BT_HDR *p_msg = NULL;
834 tGATT_SR_REG *p_rcb;
835 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
836 tGATT_SRV_LIST_ELEM *p_srv=NULL;
837
838 reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, &e_hdl);
839
840 if (reason == GATT_SUCCESS)
841 {
842 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
843
844 if ((p_msg = (BT_HDR *)GKI_getbuf(buf_len)) == NULL)
845 {
846 reason = GATT_NO_RESOURCES;
847 }
848 else
849 {
850 reason = GATT_NOT_FOUND;
851
852 memset(p_msg, 0, buf_len);
853 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
854 *p ++ = op_code + 1;
855 p_msg->len = 2;
856
857 buf_len = p_tcb->payload_size - 2;
858
859 p_srv = p_list->p_first;
860
861 while (p_srv)
862 {
863 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
864
865 if (p_rcb->in_use &&
866 !(p_rcb->s_hdl > e_hdl ||
867 p_rcb->e_hdl < s_hdl))
868 {
869 reason = gatt_build_find_info_rsp(p_rcb, p_msg, &buf_len, s_hdl, e_hdl);
870 if (reason == GATT_NO_RESOURCES)
871 {
872 reason = GATT_SUCCESS;
873 break;
874 }
875 }
876 p_srv = p_srv->p_next;
877 }
878 *p = (UINT8)p_msg->offset;
879
880 p_msg->offset = L2CAP_MIN_OFFSET;
881 }
882 }
883
884 if (reason != GATT_SUCCESS)
885 {
886 if (p_msg) GKI_freebuf(p_msg);
887 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
888 }
889 else
890 attp_send_sr_msg(p_tcb, p_msg);
891
892}
893
894/*******************************************************************************
895**
896** Function gatts_process_mtu_req
897**
898** Description This function is called to process excahnge MTU request.
899** Only used on LE.
900**
901** Returns void
902**
903*******************************************************************************/
904static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data)
905{
906 UINT16 mtu = 0;
907 UINT8 *p = p_data, i;
908 BT_HDR *p_buf;
909 UINT16 conn_id;
910
The Android Open Source Project5738f832012-12-12 16:00:35 -0800911 /* BR/EDR conenction, send error response */
912 if (p_tcb->att_lcid != L2CAP_ATT_CID)
913 {
914 gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, FALSE);
915 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700916 else if (len < GATT_MTU_REQ_MIN_LEN)
917 {
918 GATT_TRACE_ERROR0("invalid MTU request PDU received.");
919 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, FALSE);
920 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800921 else
922 {
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700923 STREAM_TO_UINT16 (mtu, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924 /* mtu must be greater than default MTU which is 23/48 */
Andre Eisenbach4b638692013-06-14 13:22:25 -0700925 if (mtu < GATT_DEF_BLE_MTU_SIZE)
926 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
927 else if (mtu > GATT_MAX_MTU_SIZE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800928 p_tcb->payload_size = GATT_MAX_MTU_SIZE;
Andre Eisenbach4b638692013-06-14 13:22:25 -0700929 else
930 p_tcb->payload_size = mtu;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700932 GATT_TRACE_ERROR1("MTU request PDU with MTU size %d", p_tcb->payload_size);
933
The Android Open Source Project5738f832012-12-12 16:00:35 -0800934 if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL)
935 {
936 attp_send_sr_msg (p_tcb, p_buf);
937
938 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
939 /* of 0, as no response is allowed from applcations */
940
941 for (i = 0; i < GATT_MAX_APPS; i ++)
942 {
943 if (gatt_cb.cl_rcb[i].in_use )
944 {
945 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
946 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
947 (tGATTS_DATA *)&p_tcb->payload_size);
948 }
949 }
950
951 }
952 }
953}
954
955/*******************************************************************************
956**
957** Function gatts_process_read_by_type_req
958**
959** Description process Read By type request.
960** This PDU can be used to perform:
961** - read characteristic value
962** - read characteristic descriptor value
963** - discover characteristic
964** - discover characteristic by UUID
965** - relationship discovery
966**
967** Returns void
968**
969*******************************************************************************/
970void gatts_process_read_by_type_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
971{
972 tBT_UUID uuid;
973 tGATT_SR_REG *p_rcb;
974 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET),
975 buf_len,
976 s_hdl, e_hdl, err_hdl = 0;
977 BT_HDR *p_msg = NULL;
978 tGATT_STATUS reason, ret;
979 UINT8 *p;
980 UINT8 sec_flag, key_size;
981 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
982 tGATT_SRV_LIST_ELEM *p_srv=NULL;
983
984 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
985
986#if GATT_CONFORMANCE_TESTING == TRUE
987 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
988 {
989 GATT_TRACE_DEBUG1("Conformance tst: forced err rsp for ReadByType: error status=%d", gatt_cb.err_status);
990
991 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, FALSE);
992
993 return;
994 }
995#endif
996
997 if (reason == GATT_SUCCESS)
998 {
999 if ((p_msg = (BT_HDR *)GKI_getbuf(msg_len)) == NULL)
1000 {
1001 GATT_TRACE_ERROR0("gatts_process_find_info failed. no resources.");
1002
1003 reason = GATT_NO_RESOURCES;
1004 }
1005 else
1006 {
1007 memset(p_msg, 0, msg_len);
1008 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1009
1010 *p ++ = op_code + 1;
1011 /* reserve length byte */
1012 p_msg->len = 2;
1013 buf_len = p_tcb->payload_size - 2;
1014
1015 reason = GATT_NOT_FOUND;
1016
1017 p_srv = p_list->p_first;
1018
1019 while (p_srv)
1020 {
1021 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
1022
1023 if (p_rcb->in_use &&
1024 !(p_rcb->s_hdl > e_hdl ||
1025 p_rcb->e_hdl < s_hdl))
1026 {
1027 gatt_sr_get_sec_info(p_tcb->peer_bda,
1028 (BOOLEAN)(p_tcb->att_lcid == L2CAP_ATT_CID),
1029 &sec_flag,
1030 &key_size);
1031
1032 ret = gatts_db_read_attr_value_by_type(p_tcb,
1033 p_rcb->p_db,
1034 op_code,
1035 p_msg,
1036 s_hdl,
1037 e_hdl,
1038 uuid,
1039 &buf_len,
1040 sec_flag,
1041 key_size,
1042 0,
1043 &err_hdl);
1044 if (ret != GATT_NOT_FOUND)
1045 {
1046 reason = ret;
1047
1048 if (ret == GATT_NO_RESOURCES)
1049 reason = GATT_SUCCESS;
1050 }
1051 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND)
1052 {
1053 s_hdl = err_hdl;
1054 break;
1055 }
1056 }
1057 p_srv = p_srv->p_next;
1058 }
1059 *p = (UINT8)p_msg->offset;
1060 p_msg->offset = L2CAP_MIN_OFFSET;
1061 }
1062 }
1063 if (reason != GATT_SUCCESS)
1064 {
1065 if (p_msg) GKI_freebuf(p_msg);
1066
1067 /* in theroy BUSY is not possible(should already been checked), protected check */
1068 if (reason != GATT_PENDING && reason != GATT_BUSY)
1069 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
1070 }
1071 else
1072 attp_send_sr_msg(p_tcb, p_msg);
1073
1074}
1075
1076/*******************************************************************************
1077**
1078** Function gatts_process_write_req
1079**
1080** Description This function is called to process the write request
1081** from client.
1082**
1083** Returns void
1084**
1085*******************************************************************************/
1086void gatts_process_write_req (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
1087 UINT8 op_code, UINT16 len, UINT8 *p_data)
1088{
1089 tGATTS_DATA sr_data;
1090 UINT32 trans_id;
1091 tGATT_STATUS status;
1092 UINT8 sec_flag, key_size, *p = p_data;
1093 tGATT_SR_REG *p_sreg;
1094 UINT16 conn_id;
1095
1096 memset(&sr_data, 0, sizeof(tGATTS_DATA));
1097
1098 switch (op_code)
1099 {
1100 case GATT_REQ_PREPARE_WRITE:
1101 sr_data.write_req.is_prep = TRUE;
1102 STREAM_TO_UINT16(sr_data.write_req.offset, p);
1103 len -= 2;
1104 /* fall through */
1105 case GATT_SIGN_CMD_WRITE:
1106 if (op_code == GATT_SIGN_CMD_WRITE)
1107 {
1108 GATT_TRACE_DEBUG0("Write CMD with data sigining" );
1109 len -= GATT_AUTH_SIGN_LEN;
1110 }
1111 /* fall through */
1112 case GATT_CMD_WRITE:
1113 case GATT_REQ_WRITE:
1114 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
1115 sr_data.write_req.need_rsp = TRUE;
1116 sr_data.write_req.handle = handle;
1117 sr_data.write_req.len = len;
1118 memcpy (sr_data.write_req.value, p, len);
1119 break;
1120 }
1121
1122 gatt_sr_get_sec_info(p_tcb->peer_bda,
1123 (BOOLEAN)(p_tcb->att_lcid == L2CAP_ATT_CID),
1124 &sec_flag,
1125 &key_size);
1126
1127 status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
1128 op_code,
1129 handle,
1130 sr_data.write_req.offset,
1131 p,
1132 len,
1133 sec_flag,
1134 key_size);
1135
1136 if (status == GATT_SUCCESS)
1137 {
1138 if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0)
1139 {
1140 p_sreg = &gatt_cb.sr_reg[i_rcb];
1141 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
1142 gatt_sr_send_req_callback(conn_id,
1143 trans_id,
1144 GATTS_REQ_TYPE_WRITE,
1145 &sr_data);
1146
1147 status = GATT_PENDING;
1148 }
1149 else
1150 {
1151 GATT_TRACE_ERROR0("max pending command, send error");
1152 status = GATT_BUSY; /* max pending command, application error */
1153 }
1154 }
1155
1156 /* in theroy BUSY is not possible(should already been checked), protected check */
1157 if (status != GATT_PENDING && status != GATT_BUSY &&
1158 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE))
1159 {
1160 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1161 }
1162 return;
1163}
1164
1165/*******************************************************************************
1166**
1167** Function gatts_process_read_req
1168**
1169** Description This function is called to process the read request
1170** from client.
1171**
1172** Returns void
1173**
1174*******************************************************************************/
1175static void gatts_process_read_req(tGATT_TCB *p_tcb, tGATT_SR_REG *p_rcb, UINT8 op_code,
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08001176 UINT16 handle, UINT8 *p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001177{
1178 UINT16 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
1179 tGATT_STATUS reason;
1180 BT_HDR *p_msg = NULL;
1181 UINT8 sec_flag, key_size, *p;
1182 UINT16 offset = 0, value_len = 0;
1183
1184 if ((p_msg = (BT_HDR *)GKI_getbuf(buf_len)) == NULL)
1185 {
1186 GATT_TRACE_ERROR0("gatts_process_find_info failed. no resources.");
1187
1188 reason = GATT_NO_RESOURCES;
1189 }
1190 else
1191 {
1192 if (op_code == GATT_REQ_READ_BLOB)
1193 STREAM_TO_UINT16(offset, p_data);
1194
1195 memset(p_msg, 0, buf_len);
1196 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1197 *p ++ = op_code + 1;
1198 p_msg->len = 1;
1199 buf_len = p_tcb->payload_size - 1;
1200
1201 gatt_sr_get_sec_info(p_tcb->peer_bda,
1202 (BOOLEAN)(p_tcb->att_lcid == L2CAP_ATT_CID),
1203 &sec_flag,
1204 &key_size);
1205
1206 reason = gatts_read_attr_value_by_handle(p_tcb,
1207 p_rcb->p_db,
1208 op_code,
1209 handle,
1210 offset,
1211 p,
1212 &value_len,
1213 buf_len,
1214 sec_flag,
1215 key_size,
1216 0);
1217
1218 p_msg->len += value_len;
1219 }
1220
1221 if (reason != GATT_SUCCESS)
1222 {
1223 if (p_msg) GKI_freebuf(p_msg);
1224
1225 /* in theroy BUSY is not possible(should already been checked), protected check */
1226 if (reason != GATT_PENDING && reason != GATT_BUSY)
1227 gatt_send_error_rsp (p_tcb, reason, op_code, handle, FALSE);
1228 }
1229 else
1230 attp_send_sr_msg(p_tcb, p_msg);
1231
1232}
1233
1234/*******************************************************************************
1235**
1236** Function gatts_process_attribute_req
1237**
1238** Description This function is called to process the per attribute handle request
1239** from client.
1240**
1241** Returns void
1242**
1243*******************************************************************************/
1244void gatts_process_attribute_req (tGATT_TCB *p_tcb, UINT8 op_code,
1245 UINT16 len, UINT8 *p_data)
1246{
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001247 UINT16 handle = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001248 UINT8 *p = p_data, i;
1249 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1250 tGATT_STATUS status = GATT_INVALID_HANDLE;
1251 tGATT_ATTR16 *p_attr;
1252
Andre Eisenbachccf9c152013-10-02 15:37:21 -07001253 if (len < 2)
1254 {
1255 GATT_TRACE_ERROR0("Illegal PDU length, discard request");
1256 status = GATT_INVALID_PDU;
1257 }
1258 else
1259 {
1260 STREAM_TO_UINT16(handle, p);
1261 len -= 2;
1262 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001263
1264#if GATT_CONFORMANCE_TESTING == TRUE
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001265 gatt_cb.handle = handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001266 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
1267 {
1268 GATT_TRACE_DEBUG1("Conformance tst: forced err rsp: error status=%d", gatt_cb.err_status);
1269
1270 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
1271
1272 return;
1273 }
1274#endif
1275
1276 if (GATT_HANDLE_IS_VALID(handle))
1277 {
1278 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1279 {
1280 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1281 {
1282 p_attr = (tGATT_ATTR16 *)p_rcb->p_db->p_attr_list;
1283
1284 while (p_attr)
1285 {
1286 if (p_attr->handle == handle)
1287 {
1288 switch (op_code)
1289 {
1290 case GATT_REQ_READ: /* read char/char descriptor value */
1291 case GATT_REQ_READ_BLOB:
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08001292 gatts_process_read_req(p_tcb, p_rcb, op_code, handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001293 break;
1294
1295 case GATT_REQ_WRITE: /* write char/char descriptor value */
1296 case GATT_CMD_WRITE:
1297 case GATT_SIGN_CMD_WRITE:
1298 case GATT_REQ_PREPARE_WRITE:
1299 gatts_process_write_req(p_tcb, i, handle, op_code, len, p);
1300 break;
1301 default:
1302 break;
1303 }
1304 status = GATT_SUCCESS;
1305 break;
1306 }
1307 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
1308 }
1309 break;
1310 }
1311 }
1312 }
1313
1314 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE)
1315 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1316}
1317
1318/*******************************************************************************
1319**
1320** Function gatts_proc_srv_chg_ind_ack
1321**
1322** Description This function process the service changed indicaiton ACK
1323**
1324** Returns void
1325**
1326*******************************************************************************/
1327static void gatts_proc_srv_chg_ind_ack(tGATT_TCB *p_tcb )
1328{
1329 tGATTS_SRV_CHG_REQ req;
1330 tGATTS_SRV_CHG *p_buf = NULL;
1331
1332 GATT_TRACE_DEBUG0("gatts_proc_srv_chg_ind_ack");
1333
1334 if ((p_buf = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
1335 {
1336 GATT_TRACE_DEBUG0("NV update set srv chg = FALSE");
1337 p_buf->srv_changed = FALSE;
1338 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1339 if (gatt_cb.cb_info.p_srv_chg_callback)
1340 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,&req, NULL);
1341 }
1342}
1343
1344/*******************************************************************************
1345**
1346** Function gatts_chk_pending_ind
1347**
1348** Description This function check any pending indication needs to be sent if
1349** there is a pending indication then sent the indication
1350**
1351** Returns void
1352**
1353*******************************************************************************/
1354static void gatts_chk_pending_ind(tGATT_TCB *p_tcb )
1355{
1356 tGATT_VALUE *p_buf = (tGATT_VALUE *)GKI_getfirst(&p_tcb->pending_ind_q);
1357 GATT_TRACE_DEBUG0("gatts_chk_pending_ind");
1358
1359 if (p_buf )
1360 {
1361 GATTS_HandleValueIndication (p_buf->conn_id,
1362 p_buf->handle,
1363 p_buf->len,
1364 p_buf->value);
1365 GKI_freebuf(GKI_remove_from_queue (&p_tcb->pending_ind_q, p_buf));
1366 }
1367}
1368
1369/*******************************************************************************
1370**
1371** Function gatts_proc_ind_ack
1372**
1373** Description This function process the Indication ack
1374**
1375** Returns TRUE continue to process the indication ack by the aaplication
1376** if the ACk is not a Service Changed Indication Ack
1377**
1378*******************************************************************************/
1379static BOOLEAN gatts_proc_ind_ack(tGATT_TCB *p_tcb, UINT16 ack_handle)
1380{
1381 BOOLEAN continue_processing = TRUE;
1382
1383 GATT_TRACE_DEBUG1 ("gatts_proc_ind_ack ack handle=%d", ack_handle);
1384
1385 if (ack_handle == gatt_cb.handle_of_h_r)
1386 {
1387 gatts_proc_srv_chg_ind_ack(p_tcb);
1388 /* there is no need to inform the application since srv chg is handled internally by GATT */
1389 continue_processing = FALSE;
1390 }
1391
1392 gatts_chk_pending_ind(p_tcb);
1393 return continue_processing;
1394}
1395
1396/*******************************************************************************
1397**
1398** Function gatts_process_value_conf
1399**
1400** Description This function is called to process the handle value confirmation.
1401**
1402** Returns void
1403**
1404*******************************************************************************/
1405void gatts_process_value_conf(tGATT_TCB *p_tcb, UINT8 op_code)
1406{
1407 UINT16 handle = p_tcb->indicate_handle;
1408 UINT32 trans_id;
1409 UINT8 i;
1410 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1411 BOOLEAN continue_processing;
1412 UINT16 conn_id;
1413
1414 btu_stop_timer (&p_tcb->conf_timer_ent);
1415 if (GATT_HANDLE_IS_VALID(handle))
1416 {
1417 p_tcb->indicate_handle = 0;
1418 continue_processing = gatts_proc_ind_ack(p_tcb, handle);
1419
1420 if (continue_processing)
1421 {
1422 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1423 {
1424 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1425 {
1426 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
1427 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_rcb->gatt_if);
1428 gatt_sr_send_req_callback(conn_id,
1429 trans_id, GATTS_REQ_TYPE_CONF, (tGATTS_DATA *)&handle);
1430 }
1431 }
1432 }
1433 }
1434 else
1435 {
1436 GATT_TRACE_ERROR0("unexpected handle value confirmation");
1437 }
1438}
1439
1440/*******************************************************************************
1441**
1442** Function gatt_server_handle_client_req
1443**
1444** Description This function is called to handle the client requests to
1445** server.
1446**
1447**
1448** Returns void
1449**
1450*******************************************************************************/
1451void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
1452 UINT16 len, UINT8 *p_data)
1453{
1454 /* there is pending command, discard this one */
1455 if (!gatt_sr_cmd_empty(p_tcb) && op_code != GATT_HANDLE_VALUE_CONF)
1456 return;
1457
1458 /* the size of the message may not be bigger than the local max PDU size*/
1459 /* The message has to be smaller than the agreed MTU, len does not include op code */
1460 if (len >= p_tcb->payload_size)
1461 {
1462 GATT_TRACE_ERROR2("server receive invalid PDU size:%d pdu size:%d", len + 1, p_tcb->payload_size );
1463 /* for invalid request expecting response, send it now */
1464 if (op_code != GATT_CMD_WRITE &&
1465 op_code != GATT_SIGN_CMD_WRITE &&
1466 op_code != GATT_HANDLE_VALUE_CONF)
1467 {
1468 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, op_code, 0, FALSE);
1469 }
1470 /* otherwise, ignore the pkt */
1471 }
1472 else
1473 {
1474 switch (op_code)
1475 {
1476 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1477 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1478 gatts_process_primary_service_req (p_tcb, op_code, len, p_data);
1479 break;
1480
1481 case GATT_REQ_FIND_INFO:/* discover char descrptor */
1482 gatts_process_find_info(p_tcb, op_code, len, p_data);
1483 break;
1484
1485 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor value */
1486 /* discover characteristic, discover char by UUID */
1487 gatts_process_read_by_type_req(p_tcb, op_code, len, p_data);
1488 break;
1489
1490
1491 case GATT_REQ_READ: /* read char/char descriptor value */
1492 case GATT_REQ_READ_BLOB:
1493 case GATT_REQ_WRITE: /* write char/char descriptor value */
1494 case GATT_CMD_WRITE:
1495 case GATT_SIGN_CMD_WRITE:
1496 case GATT_REQ_PREPARE_WRITE:
1497 gatts_process_attribute_req (p_tcb, op_code, len, p_data);
1498 break;
1499
1500 case GATT_HANDLE_VALUE_CONF:
1501 gatts_process_value_conf (p_tcb, op_code);
1502 break;
1503
1504 case GATT_REQ_MTU:
1505 gatts_process_mtu_req (p_tcb, len, p_data);
1506 break;
1507
1508 case GATT_REQ_EXEC_WRITE:
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08001509 gatt_process_exec_write_req (p_tcb, op_code, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001510 break;
1511
1512 case GATT_REQ_READ_MULTI:
1513 gatt_process_read_multi_req (p_tcb, op_code, len, p_data);
1514 break;
1515
1516 default:
1517 break;
1518 }
1519 }
1520}
1521
1522#endif /* BLE_INCLUDED */