blob: 37774d8d10d68f0eb8e78799cd942223bf5a447b [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"
Myles Watsond7ffd642016-10-27 10:27:36 -070027#include "osi/include/osi.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080028
The Android Open Source Project5738f832012-12-12 16:00:35 -080029#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"
Myles Watson911d1ae2016-11-28 16:44:40 -080033#define GATT_MTU_REQ_MIN_LEN 2
The Android Open Source Project5738f832012-12-12 16:00:35 -080034
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070035using base::StringPrintf;
The Android Open Source Project5738f832012-12-12 16:00:35 -080036/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080037 *
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 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070046uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint8_t op_code, uint16_t handle) {
47 tGATT_SR_CMD* p_cmd = &tcb.sr_cmd;
Myles Watson911d1ae2016-11-28 16:44:40 -080048 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080049
Myles Watson911d1ae2016-11-28 16:44:40 -080050 if ((p_cmd->op_code == 0) ||
51 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
52 {
53 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
54 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070055 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080056 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070057 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080058 p_cmd->op_code = op_code;
59 p_cmd->handle = handle;
60 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070061 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080062 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080063 }
Myles Watson911d1ae2016-11-28 16:44:40 -080064 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080065
Myles Watson911d1ae2016-11-28 16:44:40 -080066 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080067}
68
69/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080070 *
71 * Function gatt_sr_cmd_empty
72 *
Myles Watson9ca07092016-11-28 16:41:53 -080073 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080074 *
75 * Returns true if empty, false if there is pending command.
76 *
77 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070078bool gatt_sr_cmd_empty(tGATT_TCB& tcb) { return (tcb.sr_cmd.op_code == 0); }
The Android Open Source Project5738f832012-12-12 16:00:35 -080079
80/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080081 *
82 * Function gatt_dequeue_sr_cmd
83 *
84 * Description This function dequeue the request from command queue.
85 *
86 * Returns void
87 *
88 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070089void gatt_dequeue_sr_cmd(tGATT_TCB& tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -080090 /* Double check in case any buffers are queued */
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070091 VLOG(1) << "gatt_dequeue_sr_cmd";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070092 if (tcb.sr_cmd.p_rsp_msg)
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070093 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = " << tcb.sr_cmd.p_rsp_msg;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070094 osi_free_and_reset((void**)&tcb.sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -080095
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070096 while (!fixed_queue_is_empty(tcb.sr_cmd.multi_rsp_q))
97 osi_free(fixed_queue_try_dequeue(tcb.sr_cmd.multi_rsp_q));
98 fixed_queue_free(tcb.sr_cmd.multi_rsp_q, NULL);
99 memset(&tcb.sr_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800100}
101
102/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800103 *
104 * Function process_read_multi_rsp
105 *
106 * Description This function check the read multiple response.
107 *
108 * Returns bool if all replies have been received
109 *
110 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800111static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
112 tGATTS_RSP* p_msg, uint16_t mtu) {
113 uint16_t ii, total_len, len;
114 uint8_t* p;
115 bool is_overflow = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800116
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700117 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700118
Myles Watson911d1ae2016-11-28 16:44:40 -0800119 if (p_cmd->multi_rsp_q == NULL)
120 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121
Myles Watson911d1ae2016-11-28 16:44:40 -0800122 /* Enqueue the response */
123 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
124 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
125 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800126
Myles Watson911d1ae2016-11-28 16:44:40 -0800127 p_cmd->status = status;
128 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700129 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
130 << " num_hdls=" << p_cmd->multi_req.num_handles;
Myles Watson911d1ae2016-11-28 16:44:40 -0800131 /* Wait till we get all the responses */
132 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
133 p_cmd->multi_req.num_handles) {
134 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
135 p_buf = (BT_HDR*)osi_calloc(len);
136 p_buf->offset = L2CAP_MIN_OFFSET;
137 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800138
Myles Watson911d1ae2016-11-28 16:44:40 -0800139 /* First byte in the response is the opcode */
140 *p++ = GATT_RSP_READ_MULTI;
141 p_buf->len = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142
Myles Watson911d1ae2016-11-28 16:44:40 -0800143 /* Now walk through the buffers puting the data into the response in order
144 */
145 list_t* list = NULL;
146 const list_node_t* node = NULL;
147 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
148 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
149 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
150 tGATTS_RSP* p_rsp = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700151
Myles Watson911d1ae2016-11-28 16:44:40 -0800152 if (list != NULL) {
153 if (ii == 0)
154 node = list_begin(list);
155 else
156 node = list_next(node);
157 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800158 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800159
Myles Watson911d1ae2016-11-28 16:44:40 -0800160 if (p_rsp != NULL) {
161 total_len = (p_buf->len + p_rsp->attr_value.len);
162
163 if (total_len > mtu) {
164 /* just send the partial response for the overflow case */
165 len = p_rsp->attr_value.len - (total_len - mtu);
166 is_overflow = true;
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700167 VLOG(1) << StringPrintf(
168 "multi read overflow available len=%d val_len=%d", len,
169 p_rsp->attr_value.len);
Myles Watson911d1ae2016-11-28 16:44:40 -0800170 } else {
171 len = p_rsp->attr_value.len;
172 }
173
174 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
175 memcpy(p, p_rsp->attr_value.value, len);
176 if (!is_overflow) p += len;
177 p_buf->len += len;
178 } else {
179 p_cmd->status = GATT_NOT_FOUND;
180 break;
181 }
182
183 if (is_overflow) break;
184
185 } else {
186 p_cmd->status = GATT_NOT_FOUND;
187 break;
188 }
189
190 } /* loop through all handles*/
191
192 /* Sanity check on the buffer length */
193 if (p_buf->len == 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700194 LOG(ERROR) << __func__ << " nothing found!!";
Myles Watson911d1ae2016-11-28 16:44:40 -0800195 p_cmd->status = GATT_NOT_FOUND;
196 osi_free(p_buf);
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700197 VLOG(1) << __func__ << "osi_free(p_buf)";
Myles Watson911d1ae2016-11-28 16:44:40 -0800198 } else if (p_cmd->p_rsp_msg != NULL) {
199 osi_free(p_buf);
200 } else {
201 p_cmd->p_rsp_msg = p_buf;
202 }
203
204 return (true);
205 }
206 } else /* any handle read exception occurs, return error */
207 {
208 return (true);
209 }
210
211 /* If here, still waiting */
212 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800213}
214
215/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800216 *
217 * Function gatt_sr_process_app_rsp
218 *
Myles Watson9ca07092016-11-28 16:41:53 -0800219 * Description This function checks whether the response message from
220 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800221 *
222 * Returns void
223 *
224 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700225tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800226 UNUSED_ATTR uint32_t trans_id,
227 uint8_t op_code, tGATT_STATUS status,
228 tGATTS_RSP* p_msg) {
229 tGATT_STATUS ret_code = GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800230
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700231 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800232
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700233 gatt_sr_update_cback_cnt(tcb, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234
Myles Watson911d1ae2016-11-28 16:44:40 -0800235 if (op_code == GATT_REQ_READ_MULTI) {
236 /* If no error and still waiting, just return */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700237 if (!process_read_multi_rsp(&tcb.sr_cmd, status, p_msg, tcb.payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800238 return (GATT_SUCCESS);
239 } else {
240 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700241 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800242
243 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700244 gatt_sr_reset_cback_cnt(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800245
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700246 tcb.sr_cmd.status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800247
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700248 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
249 if (tcb.sr_cmd.p_rsp_msg == NULL) {
250 tcb.sr_cmd.p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
251 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800252 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700253 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800254 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800255 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800256 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700257 if (gatt_sr_is_cback_cnt_zero(tcb)) {
258 if ((tcb.sr_cmd.status == GATT_SUCCESS) && (tcb.sr_cmd.p_rsp_msg)) {
259 ret_code = attp_send_sr_msg(tcb, tcb.sr_cmd.p_rsp_msg);
260 tcb.sr_cmd.p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800261 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700262 ret_code =
263 gatt_send_error_rsp(tcb, status, op_code, tcb.sr_cmd.handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800264 }
265
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700266 gatt_dequeue_sr_cmd(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800267 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800268
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700269 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800270
271 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272}
273
274/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800275 *
276 * Function gatt_process_exec_write_req
277 *
278 * Description This function is called to process the execute write request
279 * from client.
280 *
281 * Returns void
282 *
283 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700284void gatt_process_exec_write_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800285 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
286 uint8_t *p = p_data, flag, i = 0;
287 uint32_t trans_id = 0;
288 tGATT_IF gatt_if;
289 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800290
Marie Janssend19e0782016-07-15 12:48:27 -0700291#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800292 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700293 VLOG(1)
294 << "Conformance tst: forced err rspv for Execute Write: error status="
295 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800296
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700297 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800298 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800299
Myles Watson911d1ae2016-11-28 16:44:40 -0800300 return;
301 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800302#endif
303
Myles Watson911d1ae2016-11-28 16:44:40 -0800304 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800305
Myles Watson911d1ae2016-11-28 16:44:40 -0800306 /* mask the flag */
307 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308
Myles Watson911d1ae2016-11-28 16:44:40 -0800309 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700310 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
311 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, 0);
312 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800313
Myles Watson911d1ae2016-11-28 16:44:40 -0800314 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700315 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800316 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700317 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800318 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
319 (tGATTS_DATA*)&flag);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700320 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800321 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800322 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800323 } else /* nothing needs to be executed , send response now */
324 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700325 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700326 gatt_send_error_rsp(tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800327 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800328}
329
330/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800331 *
332 * Function gatt_process_read_multi_req
333 *
334 * Description This function is called to process the read multiple request
335 * from client.
336 *
337 * Returns void
338 *
339 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700340void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
341 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800342 uint32_t trans_id;
343 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700344 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800345 tGATT_STATUS err = GATT_SUCCESS;
346 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800347
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700348 VLOG(1) << __func__;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700349 tcb.sr_cmd.multi_req.num_handles = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800350
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700351 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800352
Marie Janssend19e0782016-07-15 12:48:27 -0700353#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800354 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700355 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
356 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800357
Myles Watson911d1ae2016-11-28 16:44:40 -0800358 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700360 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -0800361 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800362
Myles Watson911d1ae2016-11-28 16:44:40 -0800363 return;
364 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800365#endif
366
Myles Watson911d1ae2016-11-28 16:44:40 -0800367 while (ll >= 2 &&
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700368 tcb.sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800369 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800370
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700371 auto it = gatt_sr_find_i_rcb_by_handle(handle);
372 if (it != gatt_cb.srv_list_info->end()) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700373 tcb.sr_cmd.multi_req.handles[tcb.sr_cmd.multi_req.num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800374
375 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700376 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
377 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800378 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700379 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800380 break;
381 }
382 } else {
383 /* invalid handle */
384 err = GATT_INVALID_HANDLE;
385 break;
386 }
387 ll -= 2;
388 }
389
390 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700391 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800392 }
393
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700394 if (tcb.sr_cmd.multi_req.num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800395
396 if (err == GATT_SUCCESS) {
397 trans_id =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700398 gatt_sr_enqueue_cmd(tcb, op_code, tcb.sr_cmd.multi_req.handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800399 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700400 gatt_sr_reset_cback_cnt(tcb); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800401
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700402 for (ll = 0; ll < tcb.sr_cmd.multi_req.num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800403 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700404 handle = tcb.sr_cmd.multi_req.handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700405 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800406
Myles Watson911d1ae2016-11-28 16:44:40 -0800407 p_msg->attr_value.handle = handle;
408 err = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700409 tcb, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700410 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
411 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800412
413 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700414 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700415 GATT_SUCCESS, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800417 /* either not using or done using the buffer, release it now */
418 osi_free(p_msg);
419 }
420 } else
421 err = GATT_NO_RESOURCES;
422 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800423
Myles Watson911d1ae2016-11-28 16:44:40 -0800424 /* in theroy BUSY is not possible(should already been checked), protected
425 * check */
426 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700427 gatt_send_error_rsp(tcb, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800428}
429
430/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800431 *
432 * Function gatt_build_primary_service_rsp
433 *
434 * Description Primamry service request processed internally. Theretically
435 * only deal with ReadByTypeVAlue and ReadByGroupType.
436 *
437 * Returns void
438 *
439 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800440static tGATT_STATUS gatt_build_primary_service_rsp(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700441 BT_HDR* p_msg, tGATT_TCB& tcb, uint8_t op_code, uint16_t s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800442 uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, tBT_UUID value) {
443 tGATT_STATUS status = GATT_NOT_FOUND;
444 uint8_t handle_len = 4, *p;
Myles Watson911d1ae2016-11-28 16:44:40 -0800445 tBT_UUID* p_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800446
Myles Watson911d1ae2016-11-28 16:44:40 -0800447 p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800448
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700449 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
450 if (el.s_hdl >= s_hdl && el.s_hdl <= e_hdl &&
451 el.type == GATT_UUID_PRI_SERVICE) {
452 p_uuid = gatts_get_service_uuid(el.p_db);
Myles Watson911d1ae2016-11-28 16:44:40 -0800453 if (p_uuid != NULL) {
454 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) handle_len = 4 + p_uuid->len;
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700455
Myles Watson911d1ae2016-11-28 16:44:40 -0800456 /* get the length byte in the repsonse */
457 if (p_msg->offset == 0) {
458 *p++ = op_code + 1;
459 p_msg->len++;
460 p_msg->offset = handle_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461
Myles Watson911d1ae2016-11-28 16:44:40 -0800462 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
463 *p++ = (uint8_t)p_msg->offset; /* length byte */
464 p_msg->len++;
465 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800466 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700468 if (p_msg->len + p_msg->offset <= tcb.payload_size &&
Myles Watson911d1ae2016-11-28 16:44:40 -0800469 handle_len == p_msg->offset) {
470 if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
471 gatt_uuid_compare(value, *p_uuid)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700472 UINT16_TO_STREAM(p, el.s_hdl);
Myles Watson911d1ae2016-11-28 16:44:40 -0800473
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700474 if (gatt_cb.last_primary_s_handle &&
475 gatt_cb.last_primary_s_handle == el.s_hdl) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700476 VLOG(1) << "Use 0xFFFF for the last primary attribute";
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700477 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
478 UINT16_TO_STREAM(p, 0xFFFF);
Myles Watson911d1ae2016-11-28 16:44:40 -0800479 } else {
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700480 UINT16_TO_STREAM(p, el.e_hdl);
Myles Watson911d1ae2016-11-28 16:44:40 -0800481 }
482
483 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
484 gatt_build_uuid_to_stream(&p, *p_uuid);
485
486 status = GATT_SUCCESS;
487 p_msg->len += p_msg->offset;
488 }
489 } else
490 break;
491 }
492 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800493 }
494 p_msg->offset = L2CAP_MIN_OFFSET;
495
496 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497}
498
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700499/**
500 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800501 *
502 * Returns true: if data filled sucessfully.
503 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700504 */
505static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700506 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700507 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800508 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800509
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700510 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800511
Myles Watson911d1ae2016-11-28 16:44:40 -0800512 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800513
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700514 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700516 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700517 if (attr.handle > e_hdl) break;
518
519 if (attr.handle < s_hdl) continue;
520
521 if (p_msg->offset == 0)
522 p_msg->offset = (attr.uuid.len == LEN_UUID_16) ? GATT_INFO_TYPE_PAIR_16
523 : GATT_INFO_TYPE_PAIR_128;
524
525 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
526
527 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
528 attr.uuid.len == LEN_UUID_16) {
529 UINT16_TO_STREAM(p, attr.handle);
530 UINT16_TO_STREAM(p, attr.uuid.uu.uuid16);
531 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
532 attr.uuid.len == LEN_UUID_128) {
533 UINT16_TO_STREAM(p, attr.handle);
534 ARRAY_TO_STREAM(p, attr.uuid.uu.uuid128, LEN_UUID_128);
535 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
536 attr.uuid.len == LEN_UUID_32) {
537 UINT16_TO_STREAM(p, attr.handle);
538 gatt_convert_uuid32_to_uuid128(p, attr.uuid.uu.uuid32);
539 p += LEN_UUID_128;
540 } else {
541 LOG(ERROR) << "format mismatch";
542 return GATT_NO_RESOURCES;
543 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800544 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700545 p_msg->len += info_pair_len[p_msg->offset - 1];
546 len -= info_pair_len[p_msg->offset - 1];
547 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800548 }
549
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700550 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800551}
552
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700553static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
554 uint16_t& e_hdl) {
555 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800556
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700557 /* obtain starting handle, and ending handle */
558 STREAM_TO_UINT16(s_hdl, p);
559 STREAM_TO_UINT16(e_hdl, p);
560 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700562 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
563 !GATT_HANDLE_IS_VALID(e_hdl)) {
564 return GATT_INVALID_PDU;
565 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800566
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700567 return GATT_SUCCESS;
568}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700570static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
571 uint8_t*& p, tBT_UUID* p_uuid,
572 uint16_t& s_hdl,
573 uint16_t& e_hdl) {
574 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
575 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700577 if (len < 2) return GATT_INVALID_PDU;
578
579 /* parse uuid now */
580 CHECK(p_uuid);
581 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
582 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
583 VLOG(1) << "Bad UUID";
584 return GATT_INVALID_PDU;
585 }
586
587 len -= uuid_len;
588 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800589}
590
591/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800592 *
593 * Function gatts_process_primary_service_req
594 *
Myles Watson9ca07092016-11-28 16:41:53 -0800595 * Description Process ReadByGroupType/ReadByTypeValue request, for
596 * discovering all primary services or discover primary service
597 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800598 *
599 * Returns void
600 *
601 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700602void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800603 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800604 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700605 tBT_UUID uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800606
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700607 uint8_t reason =
608 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
609 if (reason != GATT_SUCCESS) {
610 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
611 return;
612 }
613
614 tBT_UUID primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
615 if (!gatt_uuid_compare(uuid, primary_service)) {
616 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
617 gatt_send_error_rsp(tcb, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl, false);
618 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: 0x%04x",
619 uuid.uu.uuid16);
620 return;
621 }
622
623 // we do not support ReadByTypeValue with any non-primamry_service type
624 gatt_send_error_rsp(tcb, GATT_NOT_FOUND, op_code, s_hdl, false);
625 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: 0x%04x",
626 uuid.uu.uuid16);
627 return;
628 }
629
630 // TODO: we assume theh value is UUID, there is no such requirement in spec
631 tBT_UUID value;
Myles Watson911d1ae2016-11-28 16:44:40 -0800632 memset(&value, 0, sizeof(tBT_UUID));
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700633 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
634 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == false) {
635 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800636 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800637 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800638
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700639 uint16_t msg_len =
640 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
641 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
642 reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl, e_hdl,
643 p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800644 if (reason != GATT_SUCCESS) {
645 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700646 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700647 }
648
649 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800650}
651
652/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800653 *
654 * Function gatts_process_find_info
655 *
656 * Description process find information request, for discover character
657 * descriptors.
658 *
659 * Returns void
660 *
661 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700662static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800663 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700664 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700665 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700666 if (reason != GATT_SUCCESS) {
667 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
668 return;
669 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800670
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700671 uint16_t buf_len =
672 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800673
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700674 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
675 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800676
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700677 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
678 *p++ = op_code + 1;
679 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800680
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700681 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800682
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700683 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
684 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700685 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700686 if (reason == GATT_NO_RESOURCES) {
687 reason = GATT_SUCCESS;
688 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800689 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800690 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800691 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800692
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700693 *p = (uint8_t)p_msg->offset;
694
695 p_msg->offset = L2CAP_MIN_OFFSET;
696
Myles Watson911d1ae2016-11-28 16:44:40 -0800697 if (reason != GATT_SUCCESS) {
698 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700699 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800700 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700701 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800702}
703
704/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800705 *
706 * Function gatts_process_mtu_req
707 *
708 * Description This function is called to process excahnge MTU request.
709 * Only used on LE.
710 *
711 * Returns void
712 *
713 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700714static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800715 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800716 /* BR/EDR conenction, send error response */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700717 if (tcb.att_lcid != L2CAP_ATT_CID) {
718 gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700719 return;
720 }
721
722 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700723 LOG(ERROR) << "invalid MTU request PDU received.";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700724 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700725 return;
726 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800727
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700728 uint16_t mtu = 0;
729 uint8_t* p = p_data;
730 STREAM_TO_UINT16(mtu, p);
731 /* mtu must be greater than default MTU which is 23/48 */
732 if (mtu < GATT_DEF_BLE_MTU_SIZE)
733 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
734 else if (mtu > GATT_MAX_MTU_SIZE)
735 tcb.payload_size = GATT_MAX_MTU_SIZE;
736 else
737 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700738
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700739 LOG(ERROR) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800740
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700741 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
742 tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800743
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700744 BT_HDR* p_buf =
745 attp_build_sr_msg(tcb, GATT_RSP_MTU, (tGATT_SR_MSG*)&tcb.payload_size);
746 attp_send_sr_msg(tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800747
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700748 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
749 /* of 0, as no response is allowed from applcations */
750 for (int i = 0; i < GATT_MAX_APPS; i++) {
751 if (gatt_cb.cl_rcb[i].in_use) {
752 uint16_t conn_id =
753 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
754 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
755 (tGATTS_DATA*)&tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800756 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800757 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800758}
759
760/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800761 *
762 * Function gatts_process_read_by_type_req
763 *
764 * Description process Read By type request.
765 * This PDU can be used to perform:
766 * - read characteristic value
767 * - read characteristic descriptor value
768 * - discover characteristic
769 * - discover characteristic by UUID
770 * - relationship discovery
771 *
772 * Returns void
773 *
774 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700775void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800776 uint16_t len, uint8_t* p_data) {
777 tBT_UUID uuid;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700778 uint16_t s_hdl, e_hdl, err_hdl = 0;
779 tGATT_STATUS reason =
780 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800781
Marie Janssend19e0782016-07-15 12:48:27 -0700782#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800783 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700784 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
785 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800786
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700787 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800788 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800789
Myles Watson911d1ae2016-11-28 16:44:40 -0800790 return;
791 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800792#endif
793
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700794 if (reason != GATT_SUCCESS) {
795 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
796 return;
797 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800798
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700799 size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
800 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
801 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800802
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700803 *p++ = op_code + 1;
804 /* reserve length byte */
805 p_msg->len = 2;
806 uint16_t buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800807
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700808 reason = GATT_NOT_FOUND;
809 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
810 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
811 uint8_t sec_flag, key_size;
812 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800813
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700814 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
815 tcb, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len, sec_flag,
816 key_size, 0, &err_hdl);
817 if (ret != GATT_NOT_FOUND) {
818 reason = ret;
819 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
820 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800821
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700822 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
823 s_hdl = err_hdl;
824 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800825 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800826 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800827 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700828 *p = (uint8_t)p_msg->offset;
829 p_msg->offset = L2CAP_MIN_OFFSET;
830
Myles Watson911d1ae2016-11-28 16:44:40 -0800831 if (reason != GATT_SUCCESS) {
832 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800833
Myles Watson911d1ae2016-11-28 16:44:40 -0800834 /* in theroy BUSY is not possible(should already been checked), protected
835 * check */
836 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700837 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700838
839 return;
840 }
841
842 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800843}
844
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700845/**
846 * This function is called to process the write request from client.
847 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700848void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700849 uint16_t handle, uint8_t op_code, uint16_t len,
850 uint8_t* p_data,
Myles Watson911d1ae2016-11-28 16:44:40 -0800851 bt_gatt_db_attribute_type_t gatt_type) {
852 tGATTS_DATA sr_data;
853 uint32_t trans_id;
854 tGATT_STATUS status;
855 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800856 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800857
Myles Watson911d1ae2016-11-28 16:44:40 -0800858 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800859
Myles Watson911d1ae2016-11-28 16:44:40 -0800860 switch (op_code) {
861 case GATT_REQ_PREPARE_WRITE:
862 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700863 LOG(ERROR) << __func__
864 << ": Prepare write request was invalid - missing offset, "
865 "sending error response";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700866 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800867 return;
868 }
869 sr_data.write_req.is_prep = true;
870 STREAM_TO_UINT16(sr_data.write_req.offset, p);
871 len -= 2;
872 /* fall through */
873 case GATT_SIGN_CMD_WRITE:
874 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700875 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800876 len -= GATT_AUTH_SIGN_LEN;
877 }
878 /* fall through */
879 case GATT_CMD_WRITE:
880 case GATT_REQ_WRITE:
881 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
882 sr_data.write_req.need_rsp = true;
883 sr_data.write_req.handle = handle;
884 sr_data.write_req.len = len;
885 if (len != 0 && p != NULL) {
886 memcpy(sr_data.write_req.value, p, len);
887 }
888 break;
889 }
890
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700891 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800892
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700893 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
894 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800895 sec_flag, key_size);
896
897 if (status == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700898 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800899 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700900 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800901
902 uint8_t opcode = 0;
903 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
904 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
905 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
906 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
907 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700908 LOG(ERROR) << __func__
909 << "%s: Attempt to write attribute that's not tied with"
910 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800911 status = GATT_ERROR;
912 }
913
914 if (opcode) {
915 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
916 status = GATT_PENDING;
917 }
918 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700919 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -0800920 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800921 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800922 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923
Myles Watson911d1ae2016-11-28 16:44:40 -0800924 /* in theroy BUSY is not possible(should already been checked), protected
925 * check */
926 if (status != GATT_PENDING && status != GATT_BUSY &&
927 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700928 gatt_send_error_rsp(tcb, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800929 }
930 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931}
932
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700933/**
934 * This function is called to process the read request from client.
935 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700936static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Myles Watson911d1ae2016-11-28 16:44:40 -0800937 uint8_t op_code, uint16_t handle,
938 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700939 size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700940 uint16_t offset = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800941 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800942
Myles Watson911d1ae2016-11-28 16:44:40 -0800943 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800944
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700945 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800946 *p++ = op_code + 1;
947 p_msg->len = 1;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700948 buf_len = tcb.payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800949
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700950 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700951 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800952
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700953 uint16_t value_len = 0;
954 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700955 tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700956 sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -0800957 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800958
Myles Watson911d1ae2016-11-28 16:44:40 -0800959 if (reason != GATT_SUCCESS) {
960 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800961
Myles Watson911d1ae2016-11-28 16:44:40 -0800962 /* in theroy BUSY is not possible(should already been checked), protected
963 * check */
964 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700965 gatt_send_error_rsp(tcb, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700966
967 return;
968 }
969
970 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800971}
972
973/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800974 *
975 * Function gatts_process_attribute_req
976 *
Myles Watson9ca07092016-11-28 16:41:53 -0800977 * Description This function is called to process the per attribute handle
978 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800979 *
980 * Returns void
981 *
982 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700983void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
984 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800985 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700986 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800987 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800988
Myles Watson911d1ae2016-11-28 16:44:40 -0800989 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700990 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -0800991 status = GATT_INVALID_PDU;
992 } else {
993 STREAM_TO_UINT16(handle, p);
994 len -= 2;
995 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800996
Marie Janssend19e0782016-07-15 12:48:27 -0700997#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800998 gatt_cb.handle = handle;
999 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001000 VLOG(1) << "Conformance tst: forced err rsp: error status="
1001 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001003 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -08001004 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001005
Myles Watson911d1ae2016-11-28 16:44:40 -08001006 return;
1007 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001008#endif
1009
Myles Watson911d1ae2016-11-28 16:44:40 -08001010 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001011 for (auto& el : *gatt_cb.srv_list_info) {
1012 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1013 for (const auto& attr : el.p_db->attr_list) {
1014 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001015 switch (op_code) {
1016 case GATT_REQ_READ: /* read char/char descriptor value */
1017 case GATT_REQ_READ_BLOB:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001018 gatts_process_read_req(tcb, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001019 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001020
Myles Watson911d1ae2016-11-28 16:44:40 -08001021 case GATT_REQ_WRITE: /* write char/char descriptor value */
1022 case GATT_CMD_WRITE:
1023 case GATT_SIGN_CMD_WRITE:
1024 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001025 gatts_process_write_req(tcb, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001026 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001027 break;
1028 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001029 break;
1030 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001031 status = GATT_SUCCESS;
1032 break;
1033 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001034 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001035 break;
1036 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001037 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001038 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001039
Myles Watson911d1ae2016-11-28 16:44:40 -08001040 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1041 op_code != GATT_SIGN_CMD_WRITE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001042 gatt_send_error_rsp(tcb, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001043}
1044
1045/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001046 *
1047 * Function gatts_proc_srv_chg_ind_ack
1048 *
1049 * Description This function process the service changed indicaiton ACK
1050 *
1051 * Returns void
1052 *
1053 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001054static void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001055 tGATTS_SRV_CHG_REQ req;
1056 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001057
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001058 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001059
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001060 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001061 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001062 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001063 p_buf->srv_changed = false;
1064 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1065 if (gatt_cb.cb_info.p_srv_chg_callback)
1066 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1067 &req, NULL);
1068 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001069}
1070
1071/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001072 *
1073 * Function gatts_chk_pending_ind
1074 *
Myles Watson9ca07092016-11-28 16:41:53 -08001075 * Description This function check any pending indication needs to be sent
1076 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001077 *
1078 * Returns void
1079 *
1080 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001081static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001082 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001083
Myles Watson911d1ae2016-11-28 16:44:40 -08001084 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001085 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001086 if (p_buf != NULL) {
1087 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1088 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001089 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001090 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001091}
1092
1093/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001094 *
1095 * Function gatts_proc_ind_ack
1096 *
Myles Watson9ca07092016-11-28 16:41:53 -08001097 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001098 *
Myles Watson9ca07092016-11-28 16:41:53 -08001099 * Returns true continue to process the indication ack by the
1100 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001101 *
1102 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001103static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001104 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001105
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001106 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001107
Myles Watson911d1ae2016-11-28 16:44:40 -08001108 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001109 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001110 /* there is no need to inform the application since srv chg is handled
1111 * internally by GATT */
1112 continue_processing = false;
1113 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001115 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001116 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117}
1118
1119/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001120 *
1121 * Function gatts_process_value_conf
1122 *
Myles Watson9ca07092016-11-28 16:41:53 -08001123 * Description This function is called to process the handle value
1124 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001125 *
1126 * Returns void
1127 *
1128 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001129void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) {
1130 uint16_t handle = tcb.indicate_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001131
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001132 alarm_cancel(tcb.conf_timer);
1133 if (!GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001134 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001135 return;
1136 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001137
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001138 tcb.indicate_handle = 0;
1139 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1140
1141 if (continue_processing) {
1142 for (auto& el : *gatt_cb.srv_list_info) {
1143 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1144 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
1145 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1146 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
1147 (tGATTS_DATA*)&handle);
Myles Watson911d1ae2016-11-28 16:44:40 -08001148 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001149 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001150 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001151}
1152
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001153/** This function is called to handle the client requests to server */
1154void gatt_server_handle_client_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -08001155 uint16_t len, uint8_t* p_data) {
1156 /* there is pending command, discard this one */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001157 if (!gatt_sr_cmd_empty(tcb) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001158
Myles Watson911d1ae2016-11-28 16:44:40 -08001159 /* the size of the message may not be bigger than the local max PDU size*/
1160 /* The message has to be smaller than the agreed MTU, len does not include op
1161 * code */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001162 if (len >= tcb.payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001163 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
1164 len + 1, tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001165 /* for invalid request expecting response, send it now */
1166 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1167 op_code != GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001168 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001169 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001170 /* otherwise, ignore the pkt */
1171 } else {
1172 switch (op_code) {
1173 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1174 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001175 gatts_process_primary_service_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001176 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001177
Myles Watson911d1ae2016-11-28 16:44:40 -08001178 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001179 gatts_process_find_info(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001180 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001181
Myles Watson911d1ae2016-11-28 16:44:40 -08001182 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1183 value */
1184 /* discover characteristic, discover char by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001185 gatts_process_read_by_type_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001186 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001187
Myles Watson911d1ae2016-11-28 16:44:40 -08001188 case GATT_REQ_READ: /* read char/char descriptor value */
1189 case GATT_REQ_READ_BLOB:
1190 case GATT_REQ_WRITE: /* write char/char descriptor value */
1191 case GATT_CMD_WRITE:
1192 case GATT_SIGN_CMD_WRITE:
1193 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001194 gatts_process_attribute_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001195 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001196
Myles Watson911d1ae2016-11-28 16:44:40 -08001197 case GATT_HANDLE_VALUE_CONF:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001198 gatts_process_value_conf(tcb, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001199 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001200
Myles Watson911d1ae2016-11-28 16:44:40 -08001201 case GATT_REQ_MTU:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001202 gatts_process_mtu_req(tcb, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001203 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001204
Myles Watson911d1ae2016-11-28 16:44:40 -08001205 case GATT_REQ_EXEC_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001206 gatt_process_exec_write_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001207 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001208
Myles Watson911d1ae2016-11-28 16:44:40 -08001209 case GATT_REQ_READ_MULTI:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001210 gatt_process_read_multi_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001211 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001212
Myles Watson911d1ae2016-11-28 16:44:40 -08001213 default:
1214 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001215 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001216 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001217}