1 /******************************************************************************
3 * Copyright (C) 2008-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * this file contains the GATT server functions
23 ******************************************************************************/
25 #include "bt_target.h"
28 #if BLE_INCLUDED == TRUE
33 #define GATT_MTU_REQ_MIN_LEN 2
36 /*******************************************************************************
38 ** Function gatt_sr_enqueue_cmd
40 ** Description This function enqueue the request from client which needs a
41 ** application response, and update the transaction ID.
45 *******************************************************************************/
46 UINT32 gatt_sr_enqueue_cmd (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 handle)
48 tGATT_SR_CMD *p_cmd = &p_tcb->sr_cmd;
51 if ( (p_cmd->op_code == 0) ||
52 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
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)
59 trans_id = ++p_tcb->trans_id;
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;
75 /*******************************************************************************
77 ** Function gatt_sr_cmd_empty
79 ** Description This function check the server command queue is empty or not.
81 ** Returns TRUE if empty, FALSE if there is pending command.
83 *******************************************************************************/
84 BOOLEAN gatt_sr_cmd_empty (tGATT_TCB *p_tcb)
86 return(p_tcb->sr_cmd.op_code == 0);
89 /*******************************************************************************
91 ** Function gatt_dequeue_sr_cmd
93 ** Description This function dequeue the request from command queue.
97 *******************************************************************************/
98 void gatt_dequeue_sr_cmd (tGATT_TCB *p_tcb)
100 /* Double check in case any buffers are queued */
101 GATT_TRACE_DEBUG("gatt_dequeue_sr_cmd" );
102 if (p_tcb->sr_cmd.p_rsp_msg)
104 GATT_TRACE_ERROR("free p_tcb->sr_cmd.p_rsp_msg = %d", p_tcb->sr_cmd.p_rsp_msg);
106 GKI_freebuf (p_tcb->sr_cmd.p_rsp_msg);
109 while (GKI_getfirst(&p_tcb->sr_cmd.multi_rsp_q))
110 GKI_freebuf (GKI_dequeue (&p_tcb->sr_cmd.multi_rsp_q));
111 memset( &p_tcb->sr_cmd, 0, sizeof(tGATT_SR_CMD));
114 /*******************************************************************************
116 ** Function process_read_multi_rsp
118 ** Description This function check the read multiple response.
120 ** Returns BOOLEAN if all replies have been received
122 *******************************************************************************/
123 static BOOLEAN process_read_multi_rsp (tGATT_SR_CMD *p_cmd, tGATT_STATUS status,
124 tGATTS_RSP *p_msg, UINT16 mtu)
126 tGATTS_RSP *p_rsp = NULL;
127 UINT16 ii, total_len, len;
128 BT_HDR *p_buf = (BT_HDR *)GKI_getbuf((UINT16)sizeof(tGATTS_RSP));
130 BOOLEAN is_overflow = FALSE;
132 GATT_TRACE_DEBUG ("process_read_multi_rsp status=%d mtu=%d", status, mtu);
136 p_cmd->status = GATT_INSUF_RESOURCE;
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);
144 p_cmd->status = status;
145 if (status == GATT_SUCCESS)
147 GATT_TRACE_DEBUG ("Multi read count=%d num_hdls=%d",
148 GKI_queue_length(&p_cmd->multi_rsp_q), p_cmd->multi_req.num_handles);
149 /* Wait till we get all the responses */
150 if (GKI_queue_length(&p_cmd->multi_rsp_q) == p_cmd->multi_req.num_handles)
152 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
153 if ((p_buf = (BT_HDR *)GKI_getbuf(len)) == NULL)
155 p_cmd->status = GATT_INSUF_RESOURCE;
159 memset(p_buf, 0, len);
160 p_buf->offset = L2CAP_MIN_OFFSET;
161 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
163 /* First byte in the response is the opcode */
164 *p++ = GATT_RSP_READ_MULTI;
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++)
172 p_rsp = (tGATTS_RSP *)GKI_getfirst (&p_cmd->multi_rsp_q);
176 p_rsp = (tGATTS_RSP *)GKI_getnext (p_rsp);
182 total_len = (p_buf->len + p_rsp->attr_value.len);
186 /* just send the partial response for the overflow case */
187 len = p_rsp->attr_value.len - (total_len - mtu);
189 GATT_TRACE_DEBUG ("multi read overflow available len=%d val_len=%d", len, p_rsp->attr_value.len );
193 len = p_rsp->attr_value.len;
196 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii])
198 memcpy (p, p_rsp->attr_value.value, len);
205 p_cmd->status = GATT_NOT_FOUND;
215 p_cmd->status = GATT_NOT_FOUND;
219 } /* loop through all handles*/
222 /* Sanity check on the buffer length */
225 GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!");
226 p_cmd->status = GATT_NOT_FOUND;
228 GATT_TRACE_DEBUG(" GKI_freebuf (p_buf)");
230 else if (p_cmd->p_rsp_msg != NULL)
236 p_cmd->p_rsp_msg = p_buf;
242 else /* any handle read exception occurs, return error */
247 /* If here, still waiting */
251 /*******************************************************************************
253 ** Function gatt_sr_process_app_rsp
255 ** Description This function checks whether the response message from application
256 ** match any pending request or not.
260 *******************************************************************************/
261 tGATT_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)
265 tGATT_STATUS ret_code = GATT_SUCCESS;
268 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp gatt_if=%d", gatt_if);
270 gatt_sr_update_cback_cnt(p_tcb, gatt_if, FALSE, FALSE);
272 if (op_code == GATT_REQ_READ_MULTI)
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);
280 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
281 gatt_sr_update_prep_cnt(p_tcb, gatt_if, TRUE, FALSE);
283 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
284 gatt_sr_reset_cback_cnt(p_tcb);
286 p_tcb->sr_cmd.status = status;
288 if (gatt_sr_is_cback_cnt_zero(p_tcb)
289 && status == GATT_SUCCESS)
291 if (p_tcb->sr_cmd.p_rsp_msg == NULL)
293 p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg (p_tcb, (UINT8)(op_code + 1), (tGATT_SR_MSG *)p_msg);
297 GATT_TRACE_ERROR("Exception!!! already has respond message");
301 if (gatt_sr_is_cback_cnt_zero(p_tcb))
303 if ( (p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg) )
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;
310 ret_code = gatt_send_error_rsp (p_tcb, status, op_code, p_tcb->sr_cmd.handle, FALSE);
313 gatt_dequeue_sr_cmd(p_tcb);
316 GATT_TRACE_DEBUG("gatt_sr_process_app_rsp ret_code=%d", ret_code);
321 /*******************************************************************************
323 ** Function gatt_process_exec_write_req
325 ** Description This function is called to process the execute write request
330 *******************************************************************************/
331 void gatt_process_exec_write_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
333 UINT8 *p = p_data, flag, i = 0;
340 #if GATT_CONFORMANCE_TESTING == TRUE
341 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
343 GATT_TRACE_DEBUG("Conformance tst: forced err rspv for Execute Write: error status=%d",
346 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, gatt_cb.handle, FALSE);
352 STREAM_TO_UINT8(flag, p);
355 flag &= GATT_PREP_WRITE_EXEC;
358 /* no prep write is queued */
359 if (!gatt_sr_is_prep_cnt_zero(p_tcb))
361 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0);
362 gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb);
364 for (i=0; i<GATT_MAX_APPS; i++)
366 if (p_tcb->prep_cnt[i])
368 gatt_if = (tGATT_IF) (i+1);
369 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
370 gatt_sr_send_req_callback(conn_id,
372 GATTS_REQ_TYPE_WRITE_EXEC,
373 (tGATTS_DATA *)&flag);
374 p_tcb->prep_cnt[i]= 0;
378 else /* nothing needs to be executed , send response now */
380 GATT_TRACE_ERROR("gatt_process_exec_write_req: no prepare write pending");
381 gatt_send_error_rsp(p_tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, FALSE);
385 /*******************************************************************************
387 ** Function gatt_process_read_multi_req
389 ** Description This function is called to process the read multiple request
394 *******************************************************************************/
395 void gatt_process_read_multi_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
398 UINT16 handle = 0, ll = len;
399 UINT8 *p = p_data, i_rcb;
400 tGATT_STATUS err = GATT_SUCCESS;
401 UINT8 sec_flag, key_size;
404 GATT_TRACE_DEBUG("gatt_process_read_multi_req" );
405 p_tcb->sr_cmd.multi_req.num_handles = 0;
407 gatt_sr_get_sec_info(p_tcb->peer_bda,
412 #if GATT_CONFORMANCE_TESTING == TRUE
413 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
415 GATT_TRACE_DEBUG("Conformance tst: forced err rspvofr ReadMultiple: error status=%d", gatt_cb.err_status);
417 STREAM_TO_UINT16(handle, p);
419 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
425 while (ll >= 2 && p_tcb->sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES)
427 STREAM_TO_UINT16(handle, p);
429 if ((i_rcb = gatt_sr_find_i_rcb_by_handle(handle)) < GATT_MAX_SR_PROFILES)
431 p_tcb->sr_cmd.multi_req.handles[p_tcb->sr_cmd.multi_req.num_handles++] = handle;
433 /* check read permission */
434 if ((err = gatts_read_attr_perm_check( gatt_cb.sr_reg[i_rcb].p_db,
441 GATT_TRACE_DEBUG("read permission denied : 0x%02x", err);
448 err = GATT_INVALID_HANDLE;
456 GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request.");
459 if (p_tcb->sr_cmd.multi_req.num_handles == 0)
460 err = GATT_INVALID_HANDLE;
462 if (err == GATT_SUCCESS)
464 if ((trans_id = gatt_sr_enqueue_cmd (p_tcb, op_code, p_tcb->sr_cmd.multi_req.handles[0])) != 0)
466 gatt_sr_reset_cback_cnt(p_tcb); /* read multiple use multi_rsp_q's count*/
468 for (ll = 0; ll < p_tcb->sr_cmd.multi_req.num_handles; ll ++)
470 if ((p_msg = (tGATTS_RSP *)GKI_getbuf(sizeof(tGATTS_RSP))) != NULL)
472 memset(p_msg, 0, sizeof(tGATTS_RSP))
474 handle = p_tcb->sr_cmd.multi_req.handles[ll];
475 i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
477 p_msg->attr_value.handle = handle;
478 err = gatts_read_attr_value_by_handle(p_tcb,
479 gatt_cb.sr_reg[i_rcb].p_db,
483 p_msg->attr_value.value,
484 &p_msg->attr_value.len,
490 if (err == GATT_SUCCESS)
492 gatt_sr_process_app_rsp(p_tcb, gatt_cb.sr_reg[i_rcb].gatt_if ,trans_id, op_code, GATT_SUCCESS, p_msg);
494 /* either not using or done using the buffer, release it now */
499 err = GATT_NO_RESOURCES;
500 gatt_dequeue_sr_cmd(p_tcb);
506 err = GATT_NO_RESOURCES;
509 /* in theroy BUSY is not possible(should already been checked), protected check */
510 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
511 gatt_send_error_rsp(p_tcb, err, op_code, handle, FALSE);
514 /*******************************************************************************
516 ** Function gatt_build_primary_service_rsp
518 ** Description Primamry service request processed internally. Theretically
519 ** only deal with ReadByTypeVAlue and ReadByGroupType.
523 *******************************************************************************/
524 static tGATT_STATUS gatt_build_primary_service_rsp (BT_HDR *p_msg, tGATT_TCB *p_tcb,
525 UINT8 op_code, UINT16 s_hdl,
526 UINT16 e_hdl, UINT8 *p_data, tBT_UUID value)
528 tGATT_STATUS status = GATT_NOT_FOUND;
529 UINT8 handle_len =4, *p ;
531 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
532 tGATT_SRV_LIST_ELEM *p_srv=NULL;
537 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
539 p_srv = p_list->p_first;
543 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
546 p_rcb->s_hdl >= s_hdl &&
547 p_rcb->s_hdl <= e_hdl &&
548 p_rcb->type == GATT_UUID_PRI_SERVICE)
550 if ((p_uuid = gatts_get_service_uuid (p_rcb->p_db)) != NULL)
552 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
553 handle_len = 4 + p_uuid->len;
555 /* get the length byte in the repsonse */
556 if (p_msg->offset ==0)
560 p_msg->offset = handle_len;
562 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
564 *p ++ = (UINT8)p_msg->offset; /* length byte */
569 if (p_msg->len + p_msg->offset <= p_tcb->payload_size &&
570 handle_len == p_msg->offset)
572 if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
573 gatt_uuid_compare(value, *p_uuid))
575 UINT16_TO_STREAM(p, p_rcb->s_hdl);
577 if (p_list->p_last_primary == p_srv &&
578 p_list->p_last_primary == p_list->p_last)
580 GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute");
581 UINT16_TO_STREAM(p, 0xFFFF); /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
585 UINT16_TO_STREAM(p, p_rcb->e_hdl);
588 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
589 gatt_build_uuid_to_stream(&p, *p_uuid);
591 status = GATT_SUCCESS;
592 p_msg->len += p_msg->offset;
599 p_srv = p_srv->p_next;
601 p_msg->offset = L2CAP_MIN_OFFSET;
606 /*******************************************************************************
608 ** Function gatt_build_find_info_rsp
610 ** Description fill the find information response information in the given
613 ** Returns TRUE: if data filled sucessfully.
614 ** FALSE: packet full, or format mismatch.
616 *******************************************************************************/
617 static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SR_REG *p_rcb, BT_HDR *p_msg, UINT16 *p_len,
618 UINT16 s_hdl, UINT16 e_hdl)
620 tGATT_STATUS status = GATT_NOT_FOUND;
623 tGATT_ATTR16 *p_attr = NULL;
624 UINT8 info_pair_len[2] = {4, 18};
626 if (!p_rcb->p_db || !p_rcb->p_db->p_attr_list)
629 /* check the attribute database */
630 p_attr = (tGATT_ATTR16 *) p_rcb->p_db->p_attr_list;
632 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
636 if (p_attr->handle > e_hdl)
641 if (p_attr->handle >= s_hdl)
643 if (p_msg->offset == 0)
644 p_msg->offset = (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) ? GATT_INFO_TYPE_PAIR_16 : GATT_INFO_TYPE_PAIR_128;
646 if (len >= info_pair_len[p_msg->offset - 1])
648 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16)
650 UINT16_TO_STREAM(p, p_attr->handle);
651 UINT16_TO_STREAM(p, p_attr->uuid);
653 else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_128 )
655 UINT16_TO_STREAM(p, p_attr->handle);
656 ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *) p_attr)->uuid, LEN_UUID_128);
658 else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32)
660 UINT16_TO_STREAM(p, p_attr->handle);
661 gatt_convert_uuid32_to_uuid128(p, ((tGATT_ATTR32 *) p_attr)->uuid);
666 GATT_TRACE_ERROR("format mismatch");
667 status = GATT_NO_RESOURCES;
669 /* format mismatch */
671 p_msg->len += info_pair_len[p_msg->offset - 1];
672 len -= info_pair_len[p_msg->offset - 1];
673 status = GATT_SUCCESS;
678 status = GATT_NO_RESOURCES;
682 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
689 /*******************************************************************************
691 ** Function gatts_internal_read_by_type_req
693 ** Description check to see if the ReadByType request can be handled internally.
697 *******************************************************************************/
698 static tGATT_STATUS gatts_validate_packet_format(UINT8 op_code, UINT16 *p_len,
699 UINT8 **p_data, tBT_UUID *p_uuid_filter,
700 UINT16 *p_s_hdl, UINT16 *p_e_hdl)
702 tGATT_STATUS reason = GATT_SUCCESS;
703 UINT16 uuid_len, s_hdl = 0, e_hdl = 0;
709 /* obtain starting handle, and ending handle */
710 STREAM_TO_UINT16(s_hdl, p);
711 STREAM_TO_UINT16(e_hdl, p);
714 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || !GATT_HANDLE_IS_VALID(e_hdl))
716 reason = GATT_INVALID_HANDLE;
718 /* for these PDUs, uuid filter must present */
719 else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
720 op_code == GATT_REQ_FIND_TYPE_VALUE ||
721 op_code == GATT_REQ_READ_BY_TYPE)
723 if (len >= 2 && p_uuid_filter != NULL)
725 uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
728 if (gatt_parse_uuid_from_cmd (p_uuid_filter, uuid_len, &p) == FALSE ||
729 p_uuid_filter->len == 0)
731 GATT_TRACE_DEBUG("UUID filter does not exsit");
732 reason = GATT_INVALID_PDU;
735 len -= p_uuid_filter->len;
738 reason = GATT_INVALID_PDU;
742 reason = GATT_INVALID_PDU;
752 /*******************************************************************************
754 ** Function gatts_process_primary_service_req
756 ** Description process ReadByGroupType/ReadByTypeValue request, for discover
757 ** all primary services or discover primary service by UUID request.
761 *******************************************************************************/
762 void gatts_process_primary_service_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
764 UINT8 reason = GATT_INVALID_PDU;
765 UINT16 s_hdl = 0, e_hdl = 0;
766 tBT_UUID uuid, value, primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
767 BT_HDR *p_msg = NULL;
768 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
770 memset (&value, 0, sizeof(tBT_UUID));
771 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
773 if (reason == GATT_SUCCESS)
775 if (gatt_uuid_compare(uuid, primary_service))
777 if (op_code == GATT_REQ_FIND_TYPE_VALUE)
779 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == FALSE)
780 reason = GATT_INVALID_PDU;
783 if (reason == GATT_SUCCESS)
785 if ((p_msg = (BT_HDR *)GKI_getbuf(msg_len)) == NULL)
787 GATT_TRACE_ERROR("gatts_process_primary_service_req failed. no resources.");
788 reason = GATT_NO_RESOURCES;
792 memset(p_msg, 0, msg_len);
793 reason = gatt_build_primary_service_rsp (p_msg, p_tcb, op_code, s_hdl, e_hdl, p_data, value);
799 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
801 reason = GATT_UNSUPPORT_GRP_TYPE;
802 GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x", uuid.uu.uuid16);
806 /* we do not support ReadByTypeValue with any non-primamry_service type */
807 reason = GATT_NOT_FOUND;
808 GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x", uuid.uu.uuid16);
813 if (reason != GATT_SUCCESS)
815 if (p_msg) GKI_freebuf(p_msg);
816 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
819 attp_send_sr_msg(p_tcb, p_msg);
823 /*******************************************************************************
825 ** Function gatts_process_find_info
827 ** Description process find information request, for discover character
832 *******************************************************************************/
833 static void gatts_process_find_info(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
835 UINT8 reason = GATT_INVALID_PDU, *p;
836 UINT16 s_hdl = 0, e_hdl = 0, buf_len;
837 BT_HDR *p_msg = NULL;
839 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
840 tGATT_SRV_LIST_ELEM *p_srv=NULL;
842 reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, &e_hdl);
844 if (reason == GATT_SUCCESS)
846 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
848 if ((p_msg = (BT_HDR *)GKI_getbuf(buf_len)) == NULL)
850 reason = GATT_NO_RESOURCES;
854 reason = GATT_NOT_FOUND;
856 memset(p_msg, 0, buf_len);
857 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
861 buf_len = p_tcb->payload_size - 2;
863 p_srv = p_list->p_first;
867 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
870 !(p_rcb->s_hdl > e_hdl ||
871 p_rcb->e_hdl < s_hdl))
873 reason = gatt_build_find_info_rsp(p_rcb, p_msg, &buf_len, s_hdl, e_hdl);
874 if (reason == GATT_NO_RESOURCES)
876 reason = GATT_SUCCESS;
880 p_srv = p_srv->p_next;
882 *p = (UINT8)p_msg->offset;
884 p_msg->offset = L2CAP_MIN_OFFSET;
888 if (reason != GATT_SUCCESS)
890 if (p_msg) GKI_freebuf(p_msg);
891 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
894 attp_send_sr_msg(p_tcb, p_msg);
898 /*******************************************************************************
900 ** Function gatts_process_mtu_req
902 ** Description This function is called to process excahnge MTU request.
907 *******************************************************************************/
908 static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data)
911 UINT8 *p = p_data, i;
915 /* BR/EDR conenction, send error response */
916 if (p_tcb->att_lcid != L2CAP_ATT_CID)
918 gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, FALSE);
920 else if (len < GATT_MTU_REQ_MIN_LEN)
922 GATT_TRACE_ERROR("invalid MTU request PDU received.");
923 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, FALSE);
927 STREAM_TO_UINT16 (mtu, p);
928 /* mtu must be greater than default MTU which is 23/48 */
929 if (mtu < GATT_DEF_BLE_MTU_SIZE)
930 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
931 else if (mtu > GATT_MAX_MTU_SIZE)
932 p_tcb->payload_size = GATT_MAX_MTU_SIZE;
934 p_tcb->payload_size = mtu;
936 GATT_TRACE_ERROR("MTU request PDU with MTU size %d", p_tcb->payload_size);
938 if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL)
940 attp_send_sr_msg (p_tcb, p_buf);
942 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
943 /* of 0, as no response is allowed from applcations */
945 for (i = 0; i < GATT_MAX_APPS; i ++)
947 if (gatt_cb.cl_rcb[i].in_use )
949 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
950 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
951 (tGATTS_DATA *)&p_tcb->payload_size);
959 /*******************************************************************************
961 ** Function gatts_process_read_by_type_req
963 ** Description process Read By type request.
964 ** This PDU can be used to perform:
965 ** - read characteristic value
966 ** - read characteristic descriptor value
967 ** - discover characteristic
968 ** - discover characteristic by UUID
969 ** - relationship discovery
973 *******************************************************************************/
974 void gatts_process_read_by_type_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
978 UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET),
980 s_hdl, e_hdl, err_hdl = 0;
981 BT_HDR *p_msg = NULL;
982 tGATT_STATUS reason, ret;
984 UINT8 sec_flag, key_size;
985 tGATT_SRV_LIST_INFO *p_list= &gatt_cb.srv_list_info;
986 tGATT_SRV_LIST_ELEM *p_srv=NULL;
988 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
990 #if GATT_CONFORMANCE_TESTING == TRUE
991 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
993 GATT_TRACE_DEBUG("Conformance tst: forced err rsp for ReadByType: error status=%d", gatt_cb.err_status);
995 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, FALSE);
1001 if (reason == GATT_SUCCESS)
1003 if ((p_msg = (BT_HDR *)GKI_getbuf(msg_len)) == NULL)
1005 GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.");
1007 reason = GATT_NO_RESOURCES;
1011 memset(p_msg, 0, msg_len);
1012 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1014 *p ++ = op_code + 1;
1015 /* reserve length byte */
1017 buf_len = p_tcb->payload_size - 2;
1019 reason = GATT_NOT_FOUND;
1021 p_srv = p_list->p_first;
1025 p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
1027 if (p_rcb->in_use &&
1028 !(p_rcb->s_hdl > e_hdl ||
1029 p_rcb->e_hdl < s_hdl))
1031 gatt_sr_get_sec_info(p_tcb->peer_bda,
1036 ret = gatts_db_read_attr_value_by_type(p_tcb,
1048 if (ret != GATT_NOT_FOUND)
1052 if (ret == GATT_NO_RESOURCES)
1053 reason = GATT_SUCCESS;
1055 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND)
1061 p_srv = p_srv->p_next;
1063 *p = (UINT8)p_msg->offset;
1064 p_msg->offset = L2CAP_MIN_OFFSET;
1067 if (reason != GATT_SUCCESS)
1069 if (p_msg) GKI_freebuf(p_msg);
1071 /* in theroy BUSY is not possible(should already been checked), protected check */
1072 if (reason != GATT_PENDING && reason != GATT_BUSY)
1073 gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
1076 attp_send_sr_msg(p_tcb, p_msg);
1080 /*******************************************************************************
1082 ** Function gatts_process_write_req
1084 ** Description This function is called to process the write request
1089 *******************************************************************************/
1090 void gatts_process_write_req (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
1091 UINT8 op_code, UINT16 len, UINT8 *p_data)
1093 tGATTS_DATA sr_data;
1095 tGATT_STATUS status;
1096 UINT8 sec_flag, key_size, *p = p_data;
1097 tGATT_SR_REG *p_sreg;
1100 memset(&sr_data, 0, sizeof(tGATTS_DATA));
1104 case GATT_REQ_PREPARE_WRITE:
1105 sr_data.write_req.is_prep = TRUE;
1106 STREAM_TO_UINT16(sr_data.write_req.offset, p);
1109 case GATT_SIGN_CMD_WRITE:
1110 if (op_code == GATT_SIGN_CMD_WRITE)
1112 GATT_TRACE_DEBUG("Write CMD with data sigining" );
1113 len -= GATT_AUTH_SIGN_LEN;
1116 case GATT_CMD_WRITE:
1117 case GATT_REQ_WRITE:
1118 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
1119 sr_data.write_req.need_rsp = TRUE;
1120 sr_data.write_req.handle = handle;
1121 sr_data.write_req.len = len;
1122 if (len != 0 && p != NULL)
1124 memcpy (sr_data.write_req.value, p, len);
1129 gatt_sr_get_sec_info(p_tcb->peer_bda,
1134 status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
1137 sr_data.write_req.offset,
1143 if (status == GATT_SUCCESS)
1145 if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0)
1147 p_sreg = &gatt_cb.sr_reg[i_rcb];
1148 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
1149 gatt_sr_send_req_callback(conn_id,
1151 GATTS_REQ_TYPE_WRITE,
1154 status = GATT_PENDING;
1158 GATT_TRACE_ERROR("max pending command, send error");
1159 status = GATT_BUSY; /* max pending command, application error */
1163 /* in theroy BUSY is not possible(should already been checked), protected check */
1164 if (status != GATT_PENDING && status != GATT_BUSY &&
1165 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE))
1167 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1172 /*******************************************************************************
1174 ** Function gatts_process_read_req
1176 ** Description This function is called to process the read request
1181 *******************************************************************************/
1182 static void gatts_process_read_req(tGATT_TCB *p_tcb, tGATT_SR_REG *p_rcb, UINT8 op_code,
1183 UINT16 handle, UINT16 len, UINT8 *p_data)
1185 UINT16 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
1186 tGATT_STATUS reason;
1187 BT_HDR *p_msg = NULL;
1188 UINT8 sec_flag, key_size, *p;
1189 UINT16 offset = 0, value_len = 0;
1192 if ((p_msg = (BT_HDR *)GKI_getbuf(buf_len)) == NULL)
1194 GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.");
1196 reason = GATT_NO_RESOURCES;
1200 if (op_code == GATT_REQ_READ_BLOB)
1201 STREAM_TO_UINT16(offset, p_data);
1203 memset(p_msg, 0, buf_len);
1204 p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
1205 *p ++ = op_code + 1;
1207 buf_len = p_tcb->payload_size - 1;
1209 gatt_sr_get_sec_info(p_tcb->peer_bda,
1214 reason = gatts_read_attr_value_by_handle(p_tcb,
1226 p_msg->len += value_len;
1229 if (reason != GATT_SUCCESS)
1231 if (p_msg) GKI_freebuf(p_msg);
1233 /* in theroy BUSY is not possible(should already been checked), protected check */
1234 if (reason != GATT_PENDING && reason != GATT_BUSY)
1235 gatt_send_error_rsp (p_tcb, reason, op_code, handle, FALSE);
1238 attp_send_sr_msg(p_tcb, p_msg);
1242 /*******************************************************************************
1244 ** Function gatts_process_attribute_req
1246 ** Description This function is called to process the per attribute handle request
1251 *******************************************************************************/
1252 void gatts_process_attribute_req (tGATT_TCB *p_tcb, UINT8 op_code,
1253 UINT16 len, UINT8 *p_data)
1256 UINT8 *p = p_data, i;
1257 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1258 tGATT_STATUS status = GATT_INVALID_HANDLE;
1259 tGATT_ATTR16 *p_attr;
1263 GATT_TRACE_ERROR("Illegal PDU length, discard request");
1264 status = GATT_INVALID_PDU;
1268 STREAM_TO_UINT16(handle, p);
1272 #if GATT_CONFORMANCE_TESTING == TRUE
1273 gatt_cb.handle = handle;
1274 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code)
1276 GATT_TRACE_DEBUG("Conformance tst: forced err rsp: error status=%d", gatt_cb.err_status);
1278 gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
1284 if (GATT_HANDLE_IS_VALID(handle))
1286 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1288 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1290 p_attr = (tGATT_ATTR16 *)p_rcb->p_db->p_attr_list;
1294 if (p_attr->handle == handle)
1298 case GATT_REQ_READ: /* read char/char descriptor value */
1299 case GATT_REQ_READ_BLOB:
1300 gatts_process_read_req(p_tcb, p_rcb, op_code, handle, len, p);
1303 case GATT_REQ_WRITE: /* write char/char descriptor value */
1304 case GATT_CMD_WRITE:
1305 case GATT_SIGN_CMD_WRITE:
1306 case GATT_REQ_PREPARE_WRITE:
1307 gatts_process_write_req(p_tcb, i, handle, op_code, len, p);
1312 status = GATT_SUCCESS;
1315 p_attr = (tGATT_ATTR16 *)p_attr->p_next;
1322 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE)
1323 gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
1326 /*******************************************************************************
1328 ** Function gatts_proc_srv_chg_ind_ack
1330 ** Description This function process the service changed indicaiton ACK
1334 *******************************************************************************/
1335 static void gatts_proc_srv_chg_ind_ack(tGATT_TCB *p_tcb )
1337 tGATTS_SRV_CHG_REQ req;
1338 tGATTS_SRV_CHG *p_buf = NULL;
1340 GATT_TRACE_DEBUG("gatts_proc_srv_chg_ind_ack");
1342 if ((p_buf = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
1344 GATT_TRACE_DEBUG("NV update set srv chg = FALSE");
1345 p_buf->srv_changed = FALSE;
1346 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1347 if (gatt_cb.cb_info.p_srv_chg_callback)
1348 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,&req, NULL);
1352 /*******************************************************************************
1354 ** Function gatts_chk_pending_ind
1356 ** Description This function check any pending indication needs to be sent if
1357 ** there is a pending indication then sent the indication
1361 *******************************************************************************/
1362 static void gatts_chk_pending_ind(tGATT_TCB *p_tcb )
1364 tGATT_VALUE *p_buf = (tGATT_VALUE *)GKI_getfirst(&p_tcb->pending_ind_q);
1365 GATT_TRACE_DEBUG("gatts_chk_pending_ind");
1369 GATTS_HandleValueIndication (p_buf->conn_id,
1373 GKI_freebuf(GKI_remove_from_queue (&p_tcb->pending_ind_q, p_buf));
1377 /*******************************************************************************
1379 ** Function gatts_proc_ind_ack
1381 ** Description This function process the Indication ack
1383 ** Returns TRUE continue to process the indication ack by the aaplication
1384 ** if the ACk is not a Service Changed Indication Ack
1386 *******************************************************************************/
1387 static BOOLEAN gatts_proc_ind_ack(tGATT_TCB *p_tcb, UINT16 ack_handle)
1389 BOOLEAN continue_processing = TRUE;
1391 GATT_TRACE_DEBUG ("gatts_proc_ind_ack ack handle=%d", ack_handle);
1393 if (ack_handle == gatt_cb.handle_of_h_r)
1395 gatts_proc_srv_chg_ind_ack(p_tcb);
1396 /* there is no need to inform the application since srv chg is handled internally by GATT */
1397 continue_processing = FALSE;
1400 gatts_chk_pending_ind(p_tcb);
1401 return continue_processing;
1404 /*******************************************************************************
1406 ** Function gatts_process_value_conf
1408 ** Description This function is called to process the handle value confirmation.
1412 *******************************************************************************/
1413 void gatts_process_value_conf(tGATT_TCB *p_tcb, UINT8 op_code)
1415 UINT16 handle = p_tcb->indicate_handle;
1418 tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
1419 BOOLEAN continue_processing;
1422 btu_stop_timer (&p_tcb->conf_timer_ent);
1423 if (GATT_HANDLE_IS_VALID(handle))
1425 p_tcb->indicate_handle = 0;
1426 continue_processing = gatts_proc_ind_ack(p_tcb, handle);
1428 if (continue_processing)
1430 for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++)
1432 if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle)
1434 trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
1435 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_rcb->gatt_if);
1436 gatt_sr_send_req_callback(conn_id,
1437 trans_id, GATTS_REQ_TYPE_CONF, (tGATTS_DATA *)&handle);
1444 GATT_TRACE_ERROR("unexpected handle value confirmation");
1448 /*******************************************************************************
1450 ** Function gatt_server_handle_client_req
1452 ** Description This function is called to handle the client requests to
1458 *******************************************************************************/
1459 void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
1460 UINT16 len, UINT8 *p_data)
1462 /* there is pending command, discard this one */
1463 if (!gatt_sr_cmd_empty(p_tcb) && op_code != GATT_HANDLE_VALUE_CONF)
1466 /* the size of the message may not be bigger than the local max PDU size*/
1467 /* The message has to be smaller than the agreed MTU, len does not include op code */
1468 if (len >= p_tcb->payload_size)
1470 GATT_TRACE_ERROR("server receive invalid PDU size:%d pdu size:%d", len + 1, p_tcb->payload_size );
1471 /* for invalid request expecting response, send it now */
1472 if (op_code != GATT_CMD_WRITE &&
1473 op_code != GATT_SIGN_CMD_WRITE &&
1474 op_code != GATT_HANDLE_VALUE_CONF)
1476 gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, op_code, 0, FALSE);
1478 /* otherwise, ignore the pkt */
1484 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1485 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1486 gatts_process_primary_service_req (p_tcb, op_code, len, p_data);
1489 case GATT_REQ_FIND_INFO:/* discover char descrptor */
1490 gatts_process_find_info(p_tcb, op_code, len, p_data);
1493 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor value */
1494 /* discover characteristic, discover char by UUID */
1495 gatts_process_read_by_type_req(p_tcb, op_code, len, p_data);
1499 case GATT_REQ_READ: /* read char/char descriptor value */
1500 case GATT_REQ_READ_BLOB:
1501 case GATT_REQ_WRITE: /* write char/char descriptor value */
1502 case GATT_CMD_WRITE:
1503 case GATT_SIGN_CMD_WRITE:
1504 case GATT_REQ_PREPARE_WRITE:
1505 gatts_process_attribute_req (p_tcb, op_code, len, p_data);
1508 case GATT_HANDLE_VALUE_CONF:
1509 gatts_process_value_conf (p_tcb, op_code);
1513 gatts_process_mtu_req (p_tcb, len, p_data);
1516 case GATT_REQ_EXEC_WRITE:
1517 gatt_process_exec_write_req (p_tcb, op_code, len, p_data);
1520 case GATT_REQ_READ_MULTI:
1521 gatt_process_read_multi_req (p_tcb, op_code, len, p_data);
1530 #endif /* BLE_INCLUDED */