blob: 214c6c55edb969749e9b1b55764c3daa4eaa9450 [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,
506 BT_HDR* p_msg, uint16_t* p_len,
507 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800508 tGATT_STATUS status = GATT_NOT_FOUND;
509 uint8_t* p;
510 uint16_t len = *p_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800511 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800512
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700513 if (!el.p_db) return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800514
Myles Watson911d1ae2016-11-28 16:44:40 -0800515 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800516
Myles Watson911d1ae2016-11-28 16:44:40 -0800517 p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800518
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700519 for (auto& attr : el.p_db->attr_list) {
520 if (attr.handle > e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800521 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800522 }
523
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700524 if (attr.handle >= s_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800525 if (p_msg->offset == 0)
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700526 p_msg->offset = (attr.uuid.len == LEN_UUID_16)
Myles Watson911d1ae2016-11-28 16:44:40 -0800527 ? GATT_INFO_TYPE_PAIR_16
528 : GATT_INFO_TYPE_PAIR_128;
529
530 if (len >= info_pair_len[p_msg->offset - 1]) {
531 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700532 attr.uuid.len == LEN_UUID_16) {
533 UINT16_TO_STREAM(p, attr.handle);
534 UINT16_TO_STREAM(p, attr.uuid.uu.uuid16);
Myles Watson911d1ae2016-11-28 16:44:40 -0800535 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700536 attr.uuid.len == LEN_UUID_128) {
537 UINT16_TO_STREAM(p, attr.handle);
538 ARRAY_TO_STREAM(p, attr.uuid.uu.uuid128, LEN_UUID_128);
Myles Watson911d1ae2016-11-28 16:44:40 -0800539 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700540 attr.uuid.len == LEN_UUID_32) {
541 UINT16_TO_STREAM(p, attr.handle);
542 gatt_convert_uuid32_to_uuid128(p, attr.uuid.uu.uuid32);
Myles Watson911d1ae2016-11-28 16:44:40 -0800543 p += LEN_UUID_128;
544 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700545 LOG(ERROR) << "format mismatch";
Myles Watson911d1ae2016-11-28 16:44:40 -0800546 status = GATT_NO_RESOURCES;
547 break;
548 /* format mismatch */
549 }
550 p_msg->len += info_pair_len[p_msg->offset - 1];
551 len -= info_pair_len[p_msg->offset - 1];
552 status = GATT_SUCCESS;
553
554 } else {
555 status = GATT_NO_RESOURCES;
556 break;
557 }
558 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800559 }
560
561 *p_len = len;
562 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563}
564
565/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800566 *
567 * Function gatts_internal_read_by_type_req
568 *
Myles Watson9ca07092016-11-28 16:41:53 -0800569 * Description Check to see if the ReadByType request can be handled
570 * internally.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800571 *
572 * Returns void
573 *
574 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800575static tGATT_STATUS gatts_validate_packet_format(
576 uint8_t op_code, uint16_t* p_len, uint8_t** p_data, tBT_UUID* p_uuid_filter,
577 uint16_t* p_s_hdl, uint16_t* p_e_hdl) {
578 tGATT_STATUS reason = GATT_SUCCESS;
579 uint16_t uuid_len, s_hdl = 0, e_hdl = 0;
580 uint16_t len = *p_len;
581 uint8_t* p = *p_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800582
Myles Watson911d1ae2016-11-28 16:44:40 -0800583 if (len >= 4) {
584 /* obtain starting handle, and ending handle */
585 STREAM_TO_UINT16(s_hdl, p);
586 STREAM_TO_UINT16(e_hdl, p);
587 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800588
Myles Watson911d1ae2016-11-28 16:44:40 -0800589 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
590 !GATT_HANDLE_IS_VALID(e_hdl)) {
591 reason = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800592 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800593 /* for these PDUs, uuid filter must present */
594 else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
595 op_code == GATT_REQ_FIND_TYPE_VALUE ||
596 op_code == GATT_REQ_READ_BY_TYPE) {
597 if (len >= 2 && p_uuid_filter != NULL) {
598 uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
599
600 /* parse uuid now */
601 if (gatt_parse_uuid_from_cmd(p_uuid_filter, uuid_len, &p) == false ||
602 p_uuid_filter->len == 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700603 VLOG(1) << "UUID filter does not exsit";
Myles Watson911d1ae2016-11-28 16:44:40 -0800604 reason = GATT_INVALID_PDU;
605 } else
606 len -= p_uuid_filter->len;
607 } else
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700608 reason = GATT_INVALID_PDU;
Myles Watson911d1ae2016-11-28 16:44:40 -0800609 }
610 } else
611 reason = GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612
Myles Watson911d1ae2016-11-28 16:44:40 -0800613 *p_data = p;
614 *p_len = len;
615 *p_s_hdl = s_hdl;
616 *p_e_hdl = e_hdl;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800617
Myles Watson911d1ae2016-11-28 16:44:40 -0800618 return reason;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800619}
620
621/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800622 *
623 * Function gatts_process_primary_service_req
624 *
Myles Watson9ca07092016-11-28 16:41:53 -0800625 * Description Process ReadByGroupType/ReadByTypeValue request, for
626 * discovering all primary services or discover primary service
627 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800628 *
629 * Returns void
630 *
631 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700632void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800633 uint16_t len, uint8_t* p_data) {
634 uint8_t reason = GATT_INVALID_PDU;
635 uint16_t s_hdl = 0, e_hdl = 0;
636 tBT_UUID uuid, value,
637 primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
638 BT_HDR* p_msg = NULL;
639 uint16_t msg_len =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700640 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800641
Myles Watson911d1ae2016-11-28 16:44:40 -0800642 memset(&value, 0, sizeof(tBT_UUID));
643 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl,
644 &e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800645
Myles Watson911d1ae2016-11-28 16:44:40 -0800646 if (reason == GATT_SUCCESS) {
647 if (gatt_uuid_compare(uuid, primary_service)) {
648 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
649 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == false)
650 reason = GATT_INVALID_PDU;
651 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800652
Myles Watson911d1ae2016-11-28 16:44:40 -0800653 if (reason == GATT_SUCCESS) {
654 p_msg = (BT_HDR*)osi_calloc(msg_len);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700655 reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800656 e_hdl, p_data, value);
657 }
658 } else {
659 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
660 reason = GATT_UNSUPPORT_GRP_TYPE;
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700661 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: 0x%04x",
662 uuid.uu.uuid16);
Myles Watson911d1ae2016-11-28 16:44:40 -0800663 } else {
664 /* we do not support ReadByTypeValue with any non-primamry_service type
665 */
666 reason = GATT_NOT_FOUND;
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700667 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: 0x%04x",
668 uuid.uu.uuid16);
Myles Watson911d1ae2016-11-28 16:44:40 -0800669 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800670 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800671 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800672
Myles Watson911d1ae2016-11-28 16:44:40 -0800673 if (reason != GATT_SUCCESS) {
674 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700675 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800676 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700677 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800678}
679
680/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800681 *
682 * Function gatts_process_find_info
683 *
684 * Description process find information request, for discover character
685 * descriptors.
686 *
687 * Returns void
688 *
689 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700690static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800691 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700692 uint16_t s_hdl = 0, e_hdl = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800693
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700694 uint8_t reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL,
695 &s_hdl, &e_hdl);
696 if (reason != GATT_SUCCESS) {
697 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
698 return;
699 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800700
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700701 uint16_t buf_len =
702 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800703
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700704 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
705 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800706
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700707 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
708 *p++ = op_code + 1;
709 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800710
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700711 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800712
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700713 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
714 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
715 reason = gatt_build_find_info_rsp(el, p_msg, &buf_len, s_hdl, e_hdl);
716 if (reason == GATT_NO_RESOURCES) {
717 reason = GATT_SUCCESS;
718 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800719 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800720 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800721 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800722
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700723 *p = (uint8_t)p_msg->offset;
724
725 p_msg->offset = L2CAP_MIN_OFFSET;
726
Myles Watson911d1ae2016-11-28 16:44:40 -0800727 if (reason != GATT_SUCCESS) {
728 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700729 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800730 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700731 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800732}
733
734/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800735 *
736 * Function gatts_process_mtu_req
737 *
738 * Description This function is called to process excahnge MTU request.
739 * Only used on LE.
740 *
741 * Returns void
742 *
743 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700744static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800745 uint8_t* p_data) {
746 uint16_t mtu = 0;
747 uint8_t *p = p_data, i;
748 BT_HDR* p_buf;
749 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800750
Myles Watson911d1ae2016-11-28 16:44:40 -0800751 /* BR/EDR conenction, send error response */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700752 if (tcb.att_lcid != L2CAP_ATT_CID) {
753 gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800754 } else if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700755 LOG(ERROR) << "invalid MTU request PDU received.";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700756 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800757 } else {
758 STREAM_TO_UINT16(mtu, p);
759 /* mtu must be greater than default MTU which is 23/48 */
760 if (mtu < GATT_DEF_BLE_MTU_SIZE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700761 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800762 else if (mtu > GATT_MAX_MTU_SIZE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700763 tcb.payload_size = GATT_MAX_MTU_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800764 else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700765 tcb.payload_size = mtu;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800766
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700767 LOG(ERROR) << "MTU request PDU with MTU size " << +tcb.payload_size;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700768
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700769 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
770 tcb.payload_size);
Priti Aghera636d6712014-12-18 13:55:48 -0800771
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700772 p_buf =
773 attp_build_sr_msg(tcb, GATT_RSP_MTU, (tGATT_SR_MSG*)&tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800774 if (p_buf != NULL) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700775 attp_send_sr_msg(tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800776
Myles Watson911d1ae2016-11-28 16:44:40 -0800777 /* Notify all registered applicaiton with new MTU size. Us a transaction
778 * ID */
779 /* of 0, as no response is allowed from applcations */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800780
Myles Watson911d1ae2016-11-28 16:44:40 -0800781 for (i = 0; i < GATT_MAX_APPS; i++) {
782 if (gatt_cb.cl_rcb[i].in_use) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700783 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800784 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700785 (tGATTS_DATA*)&tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800786 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800787 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800788 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800789 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790}
791
792/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800793 *
794 * Function gatts_process_read_by_type_req
795 *
796 * Description process Read By type request.
797 * This PDU can be used to perform:
798 * - read characteristic value
799 * - read characteristic descriptor value
800 * - discover characteristic
801 * - discover characteristic by UUID
802 * - relationship discovery
803 *
804 * Returns void
805 *
806 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700807void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800808 uint16_t len, uint8_t* p_data) {
809 tBT_UUID uuid;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700810 size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800811 uint16_t buf_len, s_hdl, e_hdl, err_hdl = 0;
812 BT_HDR* p_msg = NULL;
813 tGATT_STATUS reason, ret;
814 uint8_t* p;
815 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800816
Myles Watson911d1ae2016-11-28 16:44:40 -0800817 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl,
818 &e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800819
Marie Janssend19e0782016-07-15 12:48:27 -0700820#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800821 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700822 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
823 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800824
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700825 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800826 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800827
Myles Watson911d1ae2016-11-28 16:44:40 -0800828 return;
829 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800830#endif
831
Myles Watson911d1ae2016-11-28 16:44:40 -0800832 if (reason == GATT_SUCCESS) {
833 p_msg = (BT_HDR*)osi_calloc(msg_len);
834 p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800835
Myles Watson911d1ae2016-11-28 16:44:40 -0800836 *p++ = op_code + 1;
837 /* reserve length byte */
838 p_msg->len = 2;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700839 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800840
Myles Watson911d1ae2016-11-28 16:44:40 -0800841 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800842
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700843 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
844 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700845 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800846
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700847 ret = gatts_db_read_attr_value_by_type(tcb, el.p_db, op_code, p_msg,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700848 s_hdl, e_hdl, uuid, &buf_len,
849 sec_flag, key_size, 0, &err_hdl);
Myles Watson911d1ae2016-11-28 16:44:40 -0800850 if (ret != GATT_NOT_FOUND) {
851 reason = ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800852
Myles Watson911d1ae2016-11-28 16:44:40 -0800853 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800854 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800855 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
856 s_hdl = err_hdl;
857 break;
858 }
859 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800860 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800861 *p = (uint8_t)p_msg->offset;
862 p_msg->offset = L2CAP_MIN_OFFSET;
863 }
864 if (reason != GATT_SUCCESS) {
865 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800866
Myles Watson911d1ae2016-11-28 16:44:40 -0800867 /* in theroy BUSY is not possible(should already been checked), protected
868 * check */
869 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700870 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800871 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700872 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800873}
874
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700875/**
876 * This function is called to process the write request from client.
877 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700878void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700879 uint16_t handle, uint8_t op_code, uint16_t len,
880 uint8_t* p_data,
Myles Watson911d1ae2016-11-28 16:44:40 -0800881 bt_gatt_db_attribute_type_t gatt_type) {
882 tGATTS_DATA sr_data;
883 uint32_t trans_id;
884 tGATT_STATUS status;
885 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800886 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800887
Myles Watson911d1ae2016-11-28 16:44:40 -0800888 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800889
Myles Watson911d1ae2016-11-28 16:44:40 -0800890 switch (op_code) {
891 case GATT_REQ_PREPARE_WRITE:
892 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700893 LOG(ERROR) << __func__
894 << ": Prepare write request was invalid - missing offset, "
895 "sending error response";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700896 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800897 return;
898 }
899 sr_data.write_req.is_prep = true;
900 STREAM_TO_UINT16(sr_data.write_req.offset, p);
901 len -= 2;
902 /* fall through */
903 case GATT_SIGN_CMD_WRITE:
904 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700905 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800906 len -= GATT_AUTH_SIGN_LEN;
907 }
908 /* fall through */
909 case GATT_CMD_WRITE:
910 case GATT_REQ_WRITE:
911 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
912 sr_data.write_req.need_rsp = true;
913 sr_data.write_req.handle = handle;
914 sr_data.write_req.len = len;
915 if (len != 0 && p != NULL) {
916 memcpy(sr_data.write_req.value, p, len);
917 }
918 break;
919 }
920
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700921 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800922
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700923 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
924 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800925 sec_flag, key_size);
926
927 if (status == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700928 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800929 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700930 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800931
932 uint8_t opcode = 0;
933 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
934 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
935 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
936 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
937 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700938 LOG(ERROR) << __func__
939 << "%s: Attempt to write attribute that's not tied with"
940 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800941 status = GATT_ERROR;
942 }
943
944 if (opcode) {
945 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
946 status = GATT_PENDING;
947 }
948 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700949 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -0800950 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800951 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800952 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800953
Myles Watson911d1ae2016-11-28 16:44:40 -0800954 /* in theroy BUSY is not possible(should already been checked), protected
955 * check */
956 if (status != GATT_PENDING && status != GATT_BUSY &&
957 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700958 gatt_send_error_rsp(tcb, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800959 }
960 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800961}
962
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700963/**
964 * This function is called to process the read request from client.
965 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700966static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Myles Watson911d1ae2016-11-28 16:44:40 -0800967 uint8_t op_code, uint16_t handle,
968 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700969 size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800970 tGATT_STATUS reason;
971 uint8_t sec_flag, key_size, *p;
972 uint16_t offset = 0, value_len = 0;
973 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800974
Myles Watson911d1ae2016-11-28 16:44:40 -0800975 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800976
Myles Watson911d1ae2016-11-28 16:44:40 -0800977 p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
978 *p++ = op_code + 1;
979 p_msg->len = 1;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700980 buf_len = tcb.payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800981
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700982 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800983
Myles Watson911d1ae2016-11-28 16:44:40 -0800984 reason = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700985 tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700986 sec_flag, key_size, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800987
Myles Watson911d1ae2016-11-28 16:44:40 -0800988 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800989
Myles Watson911d1ae2016-11-28 16:44:40 -0800990 if (reason != GATT_SUCCESS) {
991 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800992
Myles Watson911d1ae2016-11-28 16:44:40 -0800993 /* in theroy BUSY is not possible(should already been checked), protected
994 * check */
995 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700996 gatt_send_error_rsp(tcb, reason, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800997 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700998 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800999}
1000
1001/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001002 *
1003 * Function gatts_process_attribute_req
1004 *
Myles Watson9ca07092016-11-28 16:41:53 -08001005 * Description This function is called to process the per attribute handle
1006 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001007 *
1008 * Returns void
1009 *
1010 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001011void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
1012 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001013 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001014 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -08001015 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001016
Myles Watson911d1ae2016-11-28 16:44:40 -08001017 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001018 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -08001019 status = GATT_INVALID_PDU;
1020 } else {
1021 STREAM_TO_UINT16(handle, p);
1022 len -= 2;
1023 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001024
Marie Janssend19e0782016-07-15 12:48:27 -07001025#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001026 gatt_cb.handle = handle;
1027 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001028 VLOG(1) << "Conformance tst: forced err rsp: error status="
1029 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001030
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001031 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -08001032 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001033
Myles Watson911d1ae2016-11-28 16:44:40 -08001034 return;
1035 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001036#endif
1037
Myles Watson911d1ae2016-11-28 16:44:40 -08001038 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001039 for (auto& el : *gatt_cb.srv_list_info) {
1040 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1041 for (const auto& attr : el.p_db->attr_list) {
1042 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001043 switch (op_code) {
1044 case GATT_REQ_READ: /* read char/char descriptor value */
1045 case GATT_REQ_READ_BLOB:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001046 gatts_process_read_req(tcb, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001047 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001048
Myles Watson911d1ae2016-11-28 16:44:40 -08001049 case GATT_REQ_WRITE: /* write char/char descriptor value */
1050 case GATT_CMD_WRITE:
1051 case GATT_SIGN_CMD_WRITE:
1052 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001053 gatts_process_write_req(tcb, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001054 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001055 break;
1056 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001057 break;
1058 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001059 status = GATT_SUCCESS;
1060 break;
1061 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001062 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001063 break;
1064 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001065 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001066 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001067
Myles Watson911d1ae2016-11-28 16:44:40 -08001068 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1069 op_code != GATT_SIGN_CMD_WRITE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001070 gatt_send_error_rsp(tcb, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001071}
1072
1073/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001074 *
1075 * Function gatts_proc_srv_chg_ind_ack
1076 *
1077 * Description This function process the service changed indicaiton ACK
1078 *
1079 * Returns void
1080 *
1081 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001082static void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001083 tGATTS_SRV_CHG_REQ req;
1084 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001085
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001086 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001087
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001088 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001089 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001090 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001091 p_buf->srv_changed = false;
1092 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1093 if (gatt_cb.cb_info.p_srv_chg_callback)
1094 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1095 &req, NULL);
1096 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001097}
1098
1099/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001100 *
1101 * Function gatts_chk_pending_ind
1102 *
Myles Watson9ca07092016-11-28 16:41:53 -08001103 * Description This function check any pending indication needs to be sent
1104 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001105 *
1106 * Returns void
1107 *
1108 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001109static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001110 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001111
Myles Watson911d1ae2016-11-28 16:44:40 -08001112 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001113 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001114 if (p_buf != NULL) {
1115 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1116 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001117 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001118 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001119}
1120
1121/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001122 *
1123 * Function gatts_proc_ind_ack
1124 *
Myles Watson9ca07092016-11-28 16:41:53 -08001125 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001126 *
Myles Watson9ca07092016-11-28 16:41:53 -08001127 * Returns true continue to process the indication ack by the
1128 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001129 *
1130 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001131static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001132 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001133
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001134 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001135
Myles Watson911d1ae2016-11-28 16:44:40 -08001136 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001137 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001138 /* there is no need to inform the application since srv chg is handled
1139 * internally by GATT */
1140 continue_processing = false;
1141 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001142
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001143 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001144 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001145}
1146
1147/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001148 *
1149 * Function gatts_process_value_conf
1150 *
Myles Watson9ca07092016-11-28 16:41:53 -08001151 * Description This function is called to process the handle value
1152 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001153 *
1154 * Returns void
1155 *
1156 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001157void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) {
1158 uint16_t handle = tcb.indicate_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001159
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001160 alarm_cancel(tcb.conf_timer);
1161 if (!GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001162 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001163 return;
1164 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001165
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001166 tcb.indicate_handle = 0;
1167 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1168
1169 if (continue_processing) {
1170 for (auto& el : *gatt_cb.srv_list_info) {
1171 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1172 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
1173 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1174 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
1175 (tGATTS_DATA*)&handle);
Myles Watson911d1ae2016-11-28 16:44:40 -08001176 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001177 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001178 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001179}
1180
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001181/** This function is called to handle the client requests to server */
1182void gatt_server_handle_client_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -08001183 uint16_t len, uint8_t* p_data) {
1184 /* there is pending command, discard this one */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001185 if (!gatt_sr_cmd_empty(tcb) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001186
Myles Watson911d1ae2016-11-28 16:44:40 -08001187 /* the size of the message may not be bigger than the local max PDU size*/
1188 /* The message has to be smaller than the agreed MTU, len does not include op
1189 * code */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001190 if (len >= tcb.payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001191 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
1192 len + 1, tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001193 /* for invalid request expecting response, send it now */
1194 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1195 op_code != GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001196 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001198 /* otherwise, ignore the pkt */
1199 } else {
1200 switch (op_code) {
1201 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1202 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001203 gatts_process_primary_service_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001204 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001205
Myles Watson911d1ae2016-11-28 16:44:40 -08001206 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001207 gatts_process_find_info(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001208 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001209
Myles Watson911d1ae2016-11-28 16:44:40 -08001210 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1211 value */
1212 /* discover characteristic, discover char by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001213 gatts_process_read_by_type_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001214 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001215
Myles Watson911d1ae2016-11-28 16:44:40 -08001216 case GATT_REQ_READ: /* read char/char descriptor value */
1217 case GATT_REQ_READ_BLOB:
1218 case GATT_REQ_WRITE: /* write char/char descriptor value */
1219 case GATT_CMD_WRITE:
1220 case GATT_SIGN_CMD_WRITE:
1221 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001222 gatts_process_attribute_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001223 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001224
Myles Watson911d1ae2016-11-28 16:44:40 -08001225 case GATT_HANDLE_VALUE_CONF:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001226 gatts_process_value_conf(tcb, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001227 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001228
Myles Watson911d1ae2016-11-28 16:44:40 -08001229 case GATT_REQ_MTU:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001230 gatts_process_mtu_req(tcb, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001231 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001232
Myles Watson911d1ae2016-11-28 16:44:40 -08001233 case GATT_REQ_EXEC_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001234 gatt_process_exec_write_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001235 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001236
Myles Watson911d1ae2016-11-28 16:44:40 -08001237 case GATT_REQ_READ_MULTI:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001238 gatt_process_read_multi_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001239 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001240
Myles Watson911d1ae2016-11-28 16:44:40 -08001241 default:
1242 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001243 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001244 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001245}