blob: e99c72b04f35e44736951899f04200f442db8633 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2008-2012 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
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
Stanley Tngcc9c7332018-04-05 09:54:13 -070025#include <log/log.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080026#include "bt_target.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080027#include "bt_utils.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070028#include "osi/include/osi.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080029
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include <string.h>
31#include "gatt_int.h"
32#include "l2c_api.h"
Priti Aghera636d6712014-12-18 13:55:48 -080033#include "l2c_int.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080034#define GATT_MTU_REQ_MIN_LEN 2
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070036using base::StringPrintf;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070037using bluetooth::Uuid;
38
The Android Open Source Project5738f832012-12-12 16:00:35 -080039/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080040 *
41 * Function gatt_sr_enqueue_cmd
42 *
43 * Description This function enqueue the request from client which needs a
44 * application response, and update the transaction ID.
45 *
46 * Returns void
47 *
48 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070049uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint8_t op_code, uint16_t handle) {
50 tGATT_SR_CMD* p_cmd = &tcb.sr_cmd;
Myles Watson911d1ae2016-11-28 16:44:40 -080051 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080052
Myles Watson911d1ae2016-11-28 16:44:40 -080053 if ((p_cmd->op_code == 0) ||
54 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
55 {
56 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
57 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070058 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080059 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070060 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080061 p_cmd->op_code = op_code;
62 p_cmd->handle = handle;
63 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070064 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080065 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080066 }
Myles Watson911d1ae2016-11-28 16:44:40 -080067 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080068
Myles Watson911d1ae2016-11-28 16:44:40 -080069 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080070}
71
72/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080073 *
74 * Function gatt_sr_cmd_empty
75 *
Myles Watson9ca07092016-11-28 16:41:53 -080076 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080077 *
78 * Returns true if empty, false if there is pending command.
79 *
80 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070081bool gatt_sr_cmd_empty(tGATT_TCB& tcb) { return (tcb.sr_cmd.op_code == 0); }
The Android Open Source Project5738f832012-12-12 16:00:35 -080082
83/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080084 *
85 * Function gatt_dequeue_sr_cmd
86 *
87 * Description This function dequeue the request from command queue.
88 *
89 * Returns void
90 *
91 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070092void gatt_dequeue_sr_cmd(tGATT_TCB& tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -080093 /* Double check in case any buffers are queued */
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070094 VLOG(1) << "gatt_dequeue_sr_cmd";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070095 if (tcb.sr_cmd.p_rsp_msg)
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070096 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = " << tcb.sr_cmd.p_rsp_msg;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070097 osi_free_and_reset((void**)&tcb.sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -080098
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070099 while (!fixed_queue_is_empty(tcb.sr_cmd.multi_rsp_q))
100 osi_free(fixed_queue_try_dequeue(tcb.sr_cmd.multi_rsp_q));
101 fixed_queue_free(tcb.sr_cmd.multi_rsp_q, NULL);
102 memset(&tcb.sr_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800103}
104
105/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800106 *
107 * Function process_read_multi_rsp
108 *
109 * Description This function check the read multiple response.
110 *
111 * Returns bool if all replies have been received
112 *
113 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800114static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
115 tGATTS_RSP* p_msg, uint16_t mtu) {
116 uint16_t ii, total_len, len;
117 uint8_t* p;
118 bool is_overflow = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700120 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700121
Myles Watson911d1ae2016-11-28 16:44:40 -0800122 if (p_cmd->multi_rsp_q == NULL)
123 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124
Myles Watson911d1ae2016-11-28 16:44:40 -0800125 /* Enqueue the response */
126 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
127 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
128 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129
Myles Watson911d1ae2016-11-28 16:44:40 -0800130 p_cmd->status = status;
131 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700132 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
133 << " num_hdls=" << p_cmd->multi_req.num_handles;
Myles Watson911d1ae2016-11-28 16:44:40 -0800134 /* Wait till we get all the responses */
135 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
136 p_cmd->multi_req.num_handles) {
137 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
138 p_buf = (BT_HDR*)osi_calloc(len);
139 p_buf->offset = L2CAP_MIN_OFFSET;
140 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800141
Myles Watson911d1ae2016-11-28 16:44:40 -0800142 /* First byte in the response is the opcode */
143 *p++ = GATT_RSP_READ_MULTI;
144 p_buf->len = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800145
Myles Watson911d1ae2016-11-28 16:44:40 -0800146 /* Now walk through the buffers puting the data into the response in order
147 */
148 list_t* list = NULL;
149 const list_node_t* node = NULL;
150 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
151 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
152 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
153 tGATTS_RSP* p_rsp = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700154
Myles Watson911d1ae2016-11-28 16:44:40 -0800155 if (list != NULL) {
156 if (ii == 0)
157 node = list_begin(list);
158 else
159 node = list_next(node);
160 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800161 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800162
Myles Watson911d1ae2016-11-28 16:44:40 -0800163 if (p_rsp != NULL) {
164 total_len = (p_buf->len + p_rsp->attr_value.len);
165
166 if (total_len > mtu) {
167 /* just send the partial response for the overflow case */
168 len = p_rsp->attr_value.len - (total_len - mtu);
169 is_overflow = true;
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700170 VLOG(1) << StringPrintf(
171 "multi read overflow available len=%d val_len=%d", len,
172 p_rsp->attr_value.len);
Myles Watson911d1ae2016-11-28 16:44:40 -0800173 } else {
174 len = p_rsp->attr_value.len;
175 }
176
177 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
178 memcpy(p, p_rsp->attr_value.value, len);
179 if (!is_overflow) p += len;
180 p_buf->len += len;
181 } else {
182 p_cmd->status = GATT_NOT_FOUND;
183 break;
184 }
185
186 if (is_overflow) break;
187
188 } else {
189 p_cmd->status = GATT_NOT_FOUND;
190 break;
191 }
192
193 } /* loop through all handles*/
194
195 /* Sanity check on the buffer length */
196 if (p_buf->len == 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700197 LOG(ERROR) << __func__ << " nothing found!!";
Myles Watson911d1ae2016-11-28 16:44:40 -0800198 p_cmd->status = GATT_NOT_FOUND;
199 osi_free(p_buf);
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700200 VLOG(1) << __func__ << "osi_free(p_buf)";
Myles Watson911d1ae2016-11-28 16:44:40 -0800201 } else if (p_cmd->p_rsp_msg != NULL) {
202 osi_free(p_buf);
203 } else {
204 p_cmd->p_rsp_msg = p_buf;
205 }
206
207 return (true);
208 }
209 } else /* any handle read exception occurs, return error */
210 {
211 return (true);
212 }
213
214 /* If here, still waiting */
215 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800216}
217
218/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800219 *
220 * Function gatt_sr_process_app_rsp
221 *
Myles Watson9ca07092016-11-28 16:41:53 -0800222 * Description This function checks whether the response message from
223 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800224 *
225 * Returns void
226 *
227 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700228tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800229 UNUSED_ATTR uint32_t trans_id,
230 uint8_t op_code, tGATT_STATUS status,
231 tGATTS_RSP* p_msg) {
232 tGATT_STATUS ret_code = GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800233
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700234 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800235
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700236 gatt_sr_update_cback_cnt(tcb, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237
Myles Watson911d1ae2016-11-28 16:44:40 -0800238 if (op_code == GATT_REQ_READ_MULTI) {
239 /* If no error and still waiting, just return */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700240 if (!process_read_multi_rsp(&tcb.sr_cmd, status, p_msg, tcb.payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800241 return (GATT_SUCCESS);
242 } else {
243 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700244 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800245
246 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700247 gatt_sr_reset_cback_cnt(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800248
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700249 tcb.sr_cmd.status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800250
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700251 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
252 if (tcb.sr_cmd.p_rsp_msg == NULL) {
253 tcb.sr_cmd.p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
254 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800255 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700256 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800257 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800258 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800259 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700260 if (gatt_sr_is_cback_cnt_zero(tcb)) {
261 if ((tcb.sr_cmd.status == GATT_SUCCESS) && (tcb.sr_cmd.p_rsp_msg)) {
262 ret_code = attp_send_sr_msg(tcb, tcb.sr_cmd.p_rsp_msg);
263 tcb.sr_cmd.p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800264 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700265 ret_code =
266 gatt_send_error_rsp(tcb, status, op_code, tcb.sr_cmd.handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800267 }
268
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700269 gatt_dequeue_sr_cmd(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800270 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800271
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700272 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800273
274 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800275}
276
277/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800278 *
279 * Function gatt_process_exec_write_req
280 *
281 * Description This function is called to process the execute write request
282 * from client.
283 *
284 * Returns void
285 *
286 ******************************************************************************/
Stanley Tngcc9c7332018-04-05 09:54:13 -0700287void gatt_process_exec_write_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
288 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800289 uint8_t *p = p_data, flag, i = 0;
290 uint32_t trans_id = 0;
291 tGATT_IF gatt_if;
292 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800293
Marie Janssend19e0782016-07-15 12:48:27 -0700294#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800295 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700296 VLOG(1)
297 << "Conformance tst: forced err rspv for Execute Write: error status="
298 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800299
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700300 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800301 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800302
Myles Watson911d1ae2016-11-28 16:44:40 -0800303 return;
304 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800305#endif
306
Stanley Tngcc9c7332018-04-05 09:54:13 -0700307 if (len < sizeof(flag)) {
308 android_errorWriteLog(0x534e4554, "73172115");
309 LOG(ERROR) << __func__ << "invalid length";
310 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_EXEC_WRITE, 0, false);
311 return;
312 }
313
Myles Watson911d1ae2016-11-28 16:44:40 -0800314 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800315
Myles Watson911d1ae2016-11-28 16:44:40 -0800316 /* mask the flag */
317 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800318
Myles Watson911d1ae2016-11-28 16:44:40 -0800319 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700320 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
321 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, 0);
322 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800323
Myles Watson911d1ae2016-11-28 16:44:40 -0800324 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700325 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800326 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700327 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700328 tGATTS_DATA gatts_data;
329 gatts_data.exec_write = flag;
Myles Watson911d1ae2016-11-28 16:44:40 -0800330 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
Myles Watson8d749042017-09-19 10:01:28 -0700331 &gatts_data);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700332 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800333 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800334 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800335 } else /* nothing needs to be executed , send response now */
336 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700337 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700338 gatt_send_error_rsp(tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800339 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800340}
341
342/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800343 *
344 * Function gatt_process_read_multi_req
345 *
346 * Description This function is called to process the read multiple request
347 * from client.
348 *
349 * Returns void
350 *
351 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700352void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
353 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800354 uint32_t trans_id;
355 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700356 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800357 tGATT_STATUS err = GATT_SUCCESS;
358 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700360 VLOG(1) << __func__;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700361 tcb.sr_cmd.multi_req.num_handles = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800362
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700363 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800364
Marie Janssend19e0782016-07-15 12:48:27 -0700365#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800366 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700367 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
368 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369
Myles Watson911d1ae2016-11-28 16:44:40 -0800370 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800371
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700372 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -0800373 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800374
Myles Watson911d1ae2016-11-28 16:44:40 -0800375 return;
376 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800377#endif
378
Myles Watson911d1ae2016-11-28 16:44:40 -0800379 while (ll >= 2 &&
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700380 tcb.sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800381 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800382
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700383 auto it = gatt_sr_find_i_rcb_by_handle(handle);
384 if (it != gatt_cb.srv_list_info->end()) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700385 tcb.sr_cmd.multi_req.handles[tcb.sr_cmd.multi_req.num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800386
387 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700388 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
389 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800390 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700391 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800392 break;
393 }
394 } else {
395 /* invalid handle */
396 err = GATT_INVALID_HANDLE;
397 break;
398 }
399 ll -= 2;
400 }
401
402 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700403 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800404 }
405
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700406 if (tcb.sr_cmd.multi_req.num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800407
408 if (err == GATT_SUCCESS) {
409 trans_id =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700410 gatt_sr_enqueue_cmd(tcb, op_code, tcb.sr_cmd.multi_req.handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800411 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700412 gatt_sr_reset_cback_cnt(tcb); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800413
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700414 for (ll = 0; ll < tcb.sr_cmd.multi_req.num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800415 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700416 handle = tcb.sr_cmd.multi_req.handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700417 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800418
Myles Watson911d1ae2016-11-28 16:44:40 -0800419 p_msg->attr_value.handle = handle;
420 err = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700421 tcb, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700422 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
423 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800424
425 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700426 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700427 GATT_SUCCESS, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800428 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800429 /* either not using or done using the buffer, release it now */
430 osi_free(p_msg);
431 }
432 } else
433 err = GATT_NO_RESOURCES;
434 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800435
Myles Watson911d1ae2016-11-28 16:44:40 -0800436 /* in theroy BUSY is not possible(should already been checked), protected
437 * check */
438 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700439 gatt_send_error_rsp(tcb, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800440}
441
442/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800443 *
444 * Function gatt_build_primary_service_rsp
445 *
446 * Description Primamry service request processed internally. Theretically
447 * only deal with ReadByTypeVAlue and ReadByGroupType.
448 *
449 * Returns void
450 *
451 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800452static tGATT_STATUS gatt_build_primary_service_rsp(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700453 BT_HDR* p_msg, tGATT_TCB& tcb, uint8_t op_code, uint16_t s_hdl,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700454 uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, const Uuid& value) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800455 tGATT_STATUS status = GATT_NOT_FOUND;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700456 uint8_t handle_len = 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800457
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700458 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800459
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700460 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700461 if (el.s_hdl < s_hdl || el.s_hdl > e_hdl ||
462 el.type != GATT_UUID_PRI_SERVICE) {
463 continue;
464 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700465
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700466 Uuid* p_uuid = gatts_get_service_uuid(el.p_db);
467 if (!p_uuid) continue;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800468
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700469 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
470 handle_len = 4 + p_uuid->GetShortestRepresentationSize();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800471
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700472 /* get the length byte in the repsonse */
473 if (p_msg->offset == 0) {
474 *p++ = op_code + 1;
475 p_msg->len++;
476 p_msg->offset = handle_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800477
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700478 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
479 *p++ = (uint8_t)p_msg->offset; /* length byte */
480 p_msg->len++;
Myles Watson911d1ae2016-11-28 16:44:40 -0800481 }
482 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700483
484 if (p_msg->len + p_msg->offset > tcb.payload_size ||
485 handle_len != p_msg->offset) {
486 break;
487 }
488
489 if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue;
490
491 UINT16_TO_STREAM(p, el.s_hdl);
492
493 if (gatt_cb.last_primary_s_handle &&
494 gatt_cb.last_primary_s_handle == el.s_hdl) {
495 VLOG(1) << "Use 0xFFFF for the last primary attribute";
496 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
497 UINT16_TO_STREAM(p, 0xFFFF);
498 } else {
499 UINT16_TO_STREAM(p, el.e_hdl);
500 }
501
502 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
503 gatt_build_uuid_to_stream(&p, *p_uuid);
504
505 status = GATT_SUCCESS;
506 p_msg->len += p_msg->offset;
Myles Watson911d1ae2016-11-28 16:44:40 -0800507 }
508 p_msg->offset = L2CAP_MIN_OFFSET;
509
510 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800511}
512
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700513/**
514 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800515 *
516 * Returns true: if data filled sucessfully.
517 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700518 */
519static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700520 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700521 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800522 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800523
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700524 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800525
Myles Watson911d1ae2016-11-28 16:44:40 -0800526 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800527
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700528 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800529
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700530 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700531 if (attr.handle > e_hdl) break;
532
533 if (attr.handle < s_hdl) continue;
534
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700535 uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize();
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700536 if (p_msg->offset == 0)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700537 p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16
538 : GATT_INFO_TYPE_PAIR_128;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700539
540 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
541
542 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700543 uuid_len == Uuid::kNumBytes16) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700544 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700545 UINT16_TO_STREAM(p, attr.uuid.As16Bit());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700546 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700547 uuid_len == Uuid::kNumBytes128) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700548 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700549 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700550 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700551 uuid_len == Uuid::kNumBytes32) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700552 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700553 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700554 } else {
555 LOG(ERROR) << "format mismatch";
556 return GATT_NO_RESOURCES;
557 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800558 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700559 p_msg->len += info_pair_len[p_msg->offset - 1];
560 len -= info_pair_len[p_msg->offset - 1];
561 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800562 }
563
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700564 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565}
566
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700567static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
568 uint16_t& e_hdl) {
569 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800570
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700571 /* obtain starting handle, and ending handle */
572 STREAM_TO_UINT16(s_hdl, p);
573 STREAM_TO_UINT16(e_hdl, p);
574 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800575
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700576 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
577 !GATT_HANDLE_IS_VALID(e_hdl)) {
578 return GATT_INVALID_PDU;
579 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800580
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700581 return GATT_SUCCESS;
582}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800583
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700584static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700585 uint8_t*& p, Uuid* p_uuid,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700586 uint16_t& s_hdl,
587 uint16_t& e_hdl) {
588 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
589 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800590
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700591 if (len < 2) return GATT_INVALID_PDU;
592
593 /* parse uuid now */
594 CHECK(p_uuid);
595 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
596 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
597 VLOG(1) << "Bad UUID";
598 return GATT_INVALID_PDU;
599 }
600
601 len -= uuid_len;
602 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800603}
604
605/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800606 *
607 * Function gatts_process_primary_service_req
608 *
Myles Watson9ca07092016-11-28 16:41:53 -0800609 * Description Process ReadByGroupType/ReadByTypeValue request, for
610 * discovering all primary services or discover primary service
611 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800612 *
613 * Returns void
614 *
615 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700616void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800617 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800618 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700619 Uuid uuid = Uuid::kEmpty;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800620
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700621 uint8_t reason =
622 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
623 if (reason != GATT_SUCCESS) {
624 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
625 return;
626 }
627
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700628 if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700629 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
630 gatt_send_error_rsp(tcb, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700631 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s",
632 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700633 return;
634 }
635
636 // we do not support ReadByTypeValue with any non-primamry_service type
637 gatt_send_error_rsp(tcb, GATT_NOT_FOUND, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700638 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s",
639 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700640 return;
641 }
642
643 // TODO: we assume theh value is UUID, there is no such requirement in spec
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700644 Uuid value = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700645 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
Myles Watson5d5fcf22017-10-06 16:51:21 -0700646 if (!gatt_parse_uuid_from_cmd(&value, len, &p_data)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700647 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800648 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800649 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800650
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700651 uint16_t msg_len =
652 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
653 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
654 reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl, e_hdl,
655 p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800656 if (reason != GATT_SUCCESS) {
657 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700658 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700659 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700660 }
661
662 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800663}
664
665/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800666 *
667 * Function gatts_process_find_info
668 *
669 * Description process find information request, for discover character
670 * descriptors.
671 *
672 * Returns void
673 *
674 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700675static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800676 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700677 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700678 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700679 if (reason != GATT_SUCCESS) {
680 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
681 return;
682 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800683
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700684 uint16_t buf_len =
685 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800686
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700687 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
688 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800689
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700690 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
691 *p++ = op_code + 1;
692 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800693
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700694 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800695
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700696 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
697 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700698 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700699 if (reason == GATT_NO_RESOURCES) {
700 reason = GATT_SUCCESS;
701 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800702 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800703 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800704 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800705
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700706 *p = (uint8_t)p_msg->offset;
707
708 p_msg->offset = L2CAP_MIN_OFFSET;
709
Myles Watson911d1ae2016-11-28 16:44:40 -0800710 if (reason != GATT_SUCCESS) {
711 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700712 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800713 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700714 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800715}
716
717/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800718 *
719 * Function gatts_process_mtu_req
720 *
721 * Description This function is called to process excahnge MTU request.
722 * Only used on LE.
723 *
724 * Returns void
725 *
726 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700727static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800728 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800729 /* BR/EDR conenction, send error response */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700730 if (tcb.att_lcid != L2CAP_ATT_CID) {
731 gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700732 return;
733 }
734
735 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700736 LOG(ERROR) << "invalid MTU request PDU received.";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700737 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700738 return;
739 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800740
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700741 uint16_t mtu = 0;
742 uint8_t* p = p_data;
743 STREAM_TO_UINT16(mtu, p);
744 /* mtu must be greater than default MTU which is 23/48 */
745 if (mtu < GATT_DEF_BLE_MTU_SIZE)
746 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
747 else if (mtu > GATT_MAX_MTU_SIZE)
748 tcb.payload_size = GATT_MAX_MTU_SIZE;
749 else
750 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700751
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700752 LOG(ERROR) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800753
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700754 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
755 tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800756
Myles Watson8d749042017-09-19 10:01:28 -0700757 tGATT_SR_MSG gatt_sr_msg;
758 gatt_sr_msg.mtu = tcb.payload_size;
759 BT_HDR* p_buf = attp_build_sr_msg(tcb, GATT_RSP_MTU, &gatt_sr_msg);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700760 attp_send_sr_msg(tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800761
Myles Watson8d749042017-09-19 10:01:28 -0700762 tGATTS_DATA gatts_data;
763 gatts_data.mtu = tcb.payload_size;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700764 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
765 /* of 0, as no response is allowed from applcations */
766 for (int i = 0; i < GATT_MAX_APPS; i++) {
767 if (gatt_cb.cl_rcb[i].in_use) {
768 uint16_t conn_id =
769 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700770 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, &gatts_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800771 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800772 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800773}
774
775/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800776 *
777 * Function gatts_process_read_by_type_req
778 *
779 * Description process Read By type request.
780 * This PDU can be used to perform:
781 * - read characteristic value
782 * - read characteristic descriptor value
783 * - discover characteristic
784 * - discover characteristic by UUID
785 * - relationship discovery
786 *
787 * Returns void
788 *
789 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700790void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800791 uint16_t len, uint8_t* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700792 Uuid uuid = Uuid::kEmpty;
Hansong Zhanga1603122018-04-12 11:45:03 -0700793 uint16_t s_hdl = 0, e_hdl = 0, err_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700794 tGATT_STATUS reason =
795 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800796
Marie Janssend19e0782016-07-15 12:48:27 -0700797#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800798 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700799 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
800 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800801
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700802 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800803 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800804
Myles Watson911d1ae2016-11-28 16:44:40 -0800805 return;
806 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800807#endif
808
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700809 if (reason != GATT_SUCCESS) {
810 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
811 return;
812 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800813
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700814 size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
815 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
816 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800817
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700818 *p++ = op_code + 1;
819 /* reserve length byte */
820 p_msg->len = 2;
821 uint16_t buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800822
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700823 reason = GATT_NOT_FOUND;
824 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
825 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
826 uint8_t sec_flag, key_size;
827 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800828
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700829 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
830 tcb, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len, sec_flag,
831 key_size, 0, &err_hdl);
832 if (ret != GATT_NOT_FOUND) {
833 reason = ret;
834 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
835 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800836
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700837 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
838 s_hdl = err_hdl;
839 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800840 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800841 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800842 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700843 *p = (uint8_t)p_msg->offset;
844 p_msg->offset = L2CAP_MIN_OFFSET;
845
Myles Watson911d1ae2016-11-28 16:44:40 -0800846 if (reason != GATT_SUCCESS) {
847 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800848
Myles Watson911d1ae2016-11-28 16:44:40 -0800849 /* in theroy BUSY is not possible(should already been checked), protected
850 * check */
851 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700852 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700853
854 return;
855 }
856
857 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800858}
859
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700860/**
861 * This function is called to process the write request from client.
862 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700863void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700864 uint16_t handle, uint8_t op_code, uint16_t len,
865 uint8_t* p_data,
Myles Watson911d1ae2016-11-28 16:44:40 -0800866 bt_gatt_db_attribute_type_t gatt_type) {
867 tGATTS_DATA sr_data;
868 uint32_t trans_id;
869 tGATT_STATUS status;
870 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800871 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800872
Myles Watson911d1ae2016-11-28 16:44:40 -0800873 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800874
Myles Watson911d1ae2016-11-28 16:44:40 -0800875 switch (op_code) {
876 case GATT_REQ_PREPARE_WRITE:
877 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700878 LOG(ERROR) << __func__
879 << ": Prepare write request was invalid - missing offset, "
880 "sending error response";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700881 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800882 return;
883 }
884 sr_data.write_req.is_prep = true;
885 STREAM_TO_UINT16(sr_data.write_req.offset, p);
886 len -= 2;
887 /* fall through */
888 case GATT_SIGN_CMD_WRITE:
889 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700890 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800891 len -= GATT_AUTH_SIGN_LEN;
892 }
893 /* fall through */
894 case GATT_CMD_WRITE:
895 case GATT_REQ_WRITE:
896 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
897 sr_data.write_req.need_rsp = true;
898 sr_data.write_req.handle = handle;
899 sr_data.write_req.len = len;
900 if (len != 0 && p != NULL) {
901 memcpy(sr_data.write_req.value, p, len);
902 }
903 break;
904 }
905
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700906 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800907
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700908 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
909 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800910 sec_flag, key_size);
911
912 if (status == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700913 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800914 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700915 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800916
917 uint8_t opcode = 0;
918 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
919 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
920 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
921 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
922 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700923 LOG(ERROR) << __func__
924 << "%s: Attempt to write attribute that's not tied with"
925 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800926 status = GATT_ERROR;
927 }
928
929 if (opcode) {
930 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
931 status = GATT_PENDING;
932 }
933 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700934 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -0800935 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800936 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800937 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938
Myles Watson911d1ae2016-11-28 16:44:40 -0800939 /* in theroy BUSY is not possible(should already been checked), protected
940 * check */
941 if (status != GATT_PENDING && status != GATT_BUSY &&
942 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700943 gatt_send_error_rsp(tcb, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800944 }
945 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800946}
947
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700948/**
949 * This function is called to process the read request from client.
950 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700951static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Myles Watson911d1ae2016-11-28 16:44:40 -0800952 uint8_t op_code, uint16_t handle,
Stanley Tngcc9c7332018-04-05 09:54:13 -0700953 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700954 size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700955 uint16_t offset = 0;
Stanley Tngcc9c7332018-04-05 09:54:13 -0700956
957 if (op_code == GATT_REQ_READ_BLOB && len < sizeof(uint16_t)) {
958 /* Error: packet length is too short */
959 LOG(ERROR) << __func__ << ": packet length=" << len
960 << " too short. min=" << sizeof(uint16_t);
961 android_errorWriteWithInfoLog(0x534e4554, "73172115", -1, NULL, 0);
962 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
963 return;
964 }
965
Myles Watson911d1ae2016-11-28 16:44:40 -0800966 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800967
Myles Watson911d1ae2016-11-28 16:44:40 -0800968 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800969
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700970 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800971 *p++ = op_code + 1;
972 p_msg->len = 1;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700973 buf_len = tcb.payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800974
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700975 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700976 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800977
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700978 uint16_t value_len = 0;
979 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700980 tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700981 sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -0800982 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800983
Myles Watson911d1ae2016-11-28 16:44:40 -0800984 if (reason != GATT_SUCCESS) {
985 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800986
Stanley Tngcc9c7332018-04-05 09:54:13 -0700987 /* in theory BUSY is not possible(should already been checked), protected
Myles Watson911d1ae2016-11-28 16:44:40 -0800988 * check */
989 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700990 gatt_send_error_rsp(tcb, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700991
992 return;
993 }
994
995 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800996}
997
998/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800999 *
1000 * Function gatts_process_attribute_req
1001 *
Myles Watson9ca07092016-11-28 16:41:53 -08001002 * Description This function is called to process the per attribute handle
1003 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001004 *
1005 * Returns void
1006 *
1007 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001008void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
1009 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001010 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001011 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -08001012 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001013
Myles Watson911d1ae2016-11-28 16:44:40 -08001014 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001015 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -08001016 status = GATT_INVALID_PDU;
1017 } else {
1018 STREAM_TO_UINT16(handle, p);
1019 len -= 2;
1020 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001021
Marie Janssend19e0782016-07-15 12:48:27 -07001022#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001023 gatt_cb.handle = handle;
1024 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001025 VLOG(1) << "Conformance tst: forced err rsp: error status="
1026 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001027
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001028 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -08001029 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001030
Myles Watson911d1ae2016-11-28 16:44:40 -08001031 return;
1032 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001033#endif
1034
Myles Watson911d1ae2016-11-28 16:44:40 -08001035 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001036 for (auto& el : *gatt_cb.srv_list_info) {
1037 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1038 for (const auto& attr : el.p_db->attr_list) {
1039 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001040 switch (op_code) {
1041 case GATT_REQ_READ: /* read char/char descriptor value */
1042 case GATT_REQ_READ_BLOB:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001043 gatts_process_read_req(tcb, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001044 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045
Myles Watson911d1ae2016-11-28 16:44:40 -08001046 case GATT_REQ_WRITE: /* write char/char descriptor value */
1047 case GATT_CMD_WRITE:
1048 case GATT_SIGN_CMD_WRITE:
1049 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001050 gatts_process_write_req(tcb, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001051 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001052 break;
1053 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001054 break;
1055 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001056 status = GATT_SUCCESS;
1057 break;
1058 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001059 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001060 break;
1061 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001062 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001063 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001064
Myles Watson911d1ae2016-11-28 16:44:40 -08001065 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1066 op_code != GATT_SIGN_CMD_WRITE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001067 gatt_send_error_rsp(tcb, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068}
1069
1070/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001071 *
1072 * Function gatts_proc_srv_chg_ind_ack
1073 *
1074 * Description This function process the service changed indicaiton ACK
1075 *
1076 * Returns void
1077 *
1078 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001079static void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001080 tGATTS_SRV_CHG_REQ req;
1081 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001082
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001083 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001084
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001085 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001086 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001087 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001088 p_buf->srv_changed = false;
1089 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1090 if (gatt_cb.cb_info.p_srv_chg_callback)
1091 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1092 &req, NULL);
1093 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001094}
1095
1096/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001097 *
1098 * Function gatts_chk_pending_ind
1099 *
Myles Watson9ca07092016-11-28 16:41:53 -08001100 * Description This function check any pending indication needs to be sent
1101 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001102 *
1103 * Returns void
1104 *
1105 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001106static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001107 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001108
Myles Watson911d1ae2016-11-28 16:44:40 -08001109 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001110 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001111 if (p_buf != NULL) {
1112 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1113 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001114 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001115 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001116}
1117
1118/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001119 *
1120 * Function gatts_proc_ind_ack
1121 *
Myles Watson9ca07092016-11-28 16:41:53 -08001122 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001123 *
Myles Watson9ca07092016-11-28 16:41:53 -08001124 * Returns true continue to process the indication ack by the
1125 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001126 *
1127 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001128static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001129 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001130
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001131 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132
Myles Watson911d1ae2016-11-28 16:44:40 -08001133 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001134 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001135 /* there is no need to inform the application since srv chg is handled
1136 * internally by GATT */
1137 continue_processing = false;
1138 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001139
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001140 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001141 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001142}
1143
1144/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001145 *
1146 * Function gatts_process_value_conf
1147 *
Myles Watson9ca07092016-11-28 16:41:53 -08001148 * Description This function is called to process the handle value
1149 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001150 *
1151 * Returns void
1152 *
1153 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001154void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) {
1155 uint16_t handle = tcb.indicate_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001156
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001157 alarm_cancel(tcb.conf_timer);
1158 if (!GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001159 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001160 return;
1161 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001162
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001163 tcb.indicate_handle = 0;
1164 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1165
1166 if (continue_processing) {
Myles Watson8d749042017-09-19 10:01:28 -07001167 tGATTS_DATA gatts_data;
1168 gatts_data.handle = handle;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001169 for (auto& el : *gatt_cb.srv_list_info) {
1170 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1171 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
1172 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1173 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
Myles Watson8d749042017-09-19 10:01:28 -07001174 &gatts_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001175 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001176 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001177 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001178}
1179
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001180/** This function is called to handle the client requests to server */
1181void gatt_server_handle_client_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -08001182 uint16_t len, uint8_t* p_data) {
1183 /* there is pending command, discard this one */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001184 if (!gatt_sr_cmd_empty(tcb) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001185
Myles Watson911d1ae2016-11-28 16:44:40 -08001186 /* the size of the message may not be bigger than the local max PDU size*/
1187 /* The message has to be smaller than the agreed MTU, len does not include op
1188 * code */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001189 if (len >= tcb.payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001190 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
1191 len + 1, tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001192 /* for invalid request expecting response, send it now */
1193 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1194 op_code != GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001195 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001196 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001197 /* otherwise, ignore the pkt */
1198 } else {
1199 switch (op_code) {
1200 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1201 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001202 gatts_process_primary_service_req(tcb, op_code, 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_FIND_INFO: /* discover char descrptor */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001206 gatts_process_find_info(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_BY_TYPE: /* read characteristic value, char descriptor
1210 value */
1211 /* discover characteristic, discover char by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001212 gatts_process_read_by_type_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001213 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001214
Myles Watson911d1ae2016-11-28 16:44:40 -08001215 case GATT_REQ_READ: /* read char/char descriptor value */
1216 case GATT_REQ_READ_BLOB:
1217 case GATT_REQ_WRITE: /* write char/char descriptor value */
1218 case GATT_CMD_WRITE:
1219 case GATT_SIGN_CMD_WRITE:
1220 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001221 gatts_process_attribute_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001222 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001223
Myles Watson911d1ae2016-11-28 16:44:40 -08001224 case GATT_HANDLE_VALUE_CONF:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001225 gatts_process_value_conf(tcb, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001226 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001227
Myles Watson911d1ae2016-11-28 16:44:40 -08001228 case GATT_REQ_MTU:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001229 gatts_process_mtu_req(tcb, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001230 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001231
Myles Watson911d1ae2016-11-28 16:44:40 -08001232 case GATT_REQ_EXEC_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001233 gatt_process_exec_write_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001234 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001235
Myles Watson911d1ae2016-11-28 16:44:40 -08001236 case GATT_REQ_READ_MULTI:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001237 gatt_process_read_multi_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001238 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001239
Myles Watson911d1ae2016-11-28 16:44:40 -08001240 default:
1241 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001242 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001243 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001244}