blob: 94d81efa28df9f28f52e19a5b1eda8a4018d4390 [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
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
Jakub Pawlowskie4f13782018-10-23 14:46:24 +020029#include <log/log.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include <string.h>
Jakub Pawlowskie4f13782018-10-23 14:46:24 +020031
The Android Open Source Project5738f832012-12-12 16:00:35 -080032#include "gatt_int.h"
33#include "l2c_api.h"
Priti Aghera636d6712014-12-18 13:55:48 -080034#include "l2c_int.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080035#define GATT_MTU_REQ_MIN_LEN 2
The Android Open Source Project5738f832012-12-12 16:00:35 -080036
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070037using base::StringPrintf;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070038using bluetooth::Uuid;
39
The Android Open Source Project5738f832012-12-12 16:00:35 -080040/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080041 *
42 * Function gatt_sr_enqueue_cmd
43 *
44 * Description This function enqueue the request from client which needs a
45 * application response, and update the transaction ID.
46 *
47 * Returns void
48 *
49 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070050uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint8_t op_code, uint16_t handle) {
51 tGATT_SR_CMD* p_cmd = &tcb.sr_cmd;
Myles Watson911d1ae2016-11-28 16:44:40 -080052 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080053
Myles Watson911d1ae2016-11-28 16:44:40 -080054 if ((p_cmd->op_code == 0) ||
55 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
56 {
57 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
58 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070059 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080060 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070061 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080062 p_cmd->op_code = op_code;
63 p_cmd->handle = handle;
64 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070065 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080066 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080067 }
Myles Watson911d1ae2016-11-28 16:44:40 -080068 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080069
Myles Watson911d1ae2016-11-28 16:44:40 -080070 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080071}
72
73/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080074 *
75 * Function gatt_sr_cmd_empty
76 *
Myles Watson9ca07092016-11-28 16:41:53 -080077 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080078 *
79 * Returns true if empty, false if there is pending command.
80 *
81 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070082bool gatt_sr_cmd_empty(tGATT_TCB& tcb) { return (tcb.sr_cmd.op_code == 0); }
The Android Open Source Project5738f832012-12-12 16:00:35 -080083
84/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080085 *
86 * Function gatt_dequeue_sr_cmd
87 *
88 * Description This function dequeue the request from command queue.
89 *
90 * Returns void
91 *
92 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070093void gatt_dequeue_sr_cmd(tGATT_TCB& tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -080094 /* Double check in case any buffers are queued */
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070095 VLOG(1) << "gatt_dequeue_sr_cmd";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070096 if (tcb.sr_cmd.p_rsp_msg)
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070097 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = " << tcb.sr_cmd.p_rsp_msg;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070098 osi_free_and_reset((void**)&tcb.sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -080099
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700100 while (!fixed_queue_is_empty(tcb.sr_cmd.multi_rsp_q))
101 osi_free(fixed_queue_try_dequeue(tcb.sr_cmd.multi_rsp_q));
102 fixed_queue_free(tcb.sr_cmd.multi_rsp_q, NULL);
103 memset(&tcb.sr_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800104}
105
106/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800107 *
108 * Function process_read_multi_rsp
109 *
110 * Description This function check the read multiple response.
111 *
112 * Returns bool if all replies have been received
113 *
114 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800115static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
116 tGATTS_RSP* p_msg, uint16_t mtu) {
117 uint16_t ii, total_len, len;
118 uint8_t* p;
119 bool is_overflow = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800120
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700121 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700122
Myles Watson911d1ae2016-11-28 16:44:40 -0800123 if (p_cmd->multi_rsp_q == NULL)
124 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800125
Myles Watson911d1ae2016-11-28 16:44:40 -0800126 /* Enqueue the response */
127 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
128 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
129 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130
Myles Watson911d1ae2016-11-28 16:44:40 -0800131 p_cmd->status = status;
132 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700133 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
134 << " num_hdls=" << p_cmd->multi_req.num_handles;
Myles Watson911d1ae2016-11-28 16:44:40 -0800135 /* Wait till we get all the responses */
136 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
137 p_cmd->multi_req.num_handles) {
138 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
139 p_buf = (BT_HDR*)osi_calloc(len);
140 p_buf->offset = L2CAP_MIN_OFFSET;
141 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142
Myles Watson911d1ae2016-11-28 16:44:40 -0800143 /* First byte in the response is the opcode */
144 *p++ = GATT_RSP_READ_MULTI;
145 p_buf->len = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800146
Myles Watson911d1ae2016-11-28 16:44:40 -0800147 /* Now walk through the buffers puting the data into the response in order
148 */
149 list_t* list = NULL;
150 const list_node_t* node = NULL;
151 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
152 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
153 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
154 tGATTS_RSP* p_rsp = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700155
Myles Watson911d1ae2016-11-28 16:44:40 -0800156 if (list != NULL) {
157 if (ii == 0)
158 node = list_begin(list);
159 else
160 node = list_next(node);
161 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800162 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800163
Myles Watson911d1ae2016-11-28 16:44:40 -0800164 if (p_rsp != NULL) {
165 total_len = (p_buf->len + p_rsp->attr_value.len);
166
167 if (total_len > mtu) {
168 /* just send the partial response for the overflow case */
169 len = p_rsp->attr_value.len - (total_len - mtu);
170 is_overflow = true;
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700171 VLOG(1) << StringPrintf(
172 "multi read overflow available len=%d val_len=%d", len,
173 p_rsp->attr_value.len);
Myles Watson911d1ae2016-11-28 16:44:40 -0800174 } else {
175 len = p_rsp->attr_value.len;
176 }
177
178 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
179 memcpy(p, p_rsp->attr_value.value, len);
180 if (!is_overflow) p += len;
181 p_buf->len += len;
182 } else {
183 p_cmd->status = GATT_NOT_FOUND;
184 break;
185 }
186
187 if (is_overflow) break;
188
189 } else {
190 p_cmd->status = GATT_NOT_FOUND;
191 break;
192 }
193
194 } /* loop through all handles*/
195
196 /* Sanity check on the buffer length */
197 if (p_buf->len == 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700198 LOG(ERROR) << __func__ << " nothing found!!";
Myles Watson911d1ae2016-11-28 16:44:40 -0800199 p_cmd->status = GATT_NOT_FOUND;
200 osi_free(p_buf);
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700201 VLOG(1) << __func__ << "osi_free(p_buf)";
Myles Watson911d1ae2016-11-28 16:44:40 -0800202 } else if (p_cmd->p_rsp_msg != NULL) {
203 osi_free(p_buf);
204 } else {
205 p_cmd->p_rsp_msg = p_buf;
206 }
207
208 return (true);
209 }
210 } else /* any handle read exception occurs, return error */
211 {
212 return (true);
213 }
214
215 /* If here, still waiting */
216 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800217}
218
219/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800220 *
221 * Function gatt_sr_process_app_rsp
222 *
Myles Watson9ca07092016-11-28 16:41:53 -0800223 * Description This function checks whether the response message from
224 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800225 *
226 * Returns void
227 *
228 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700229tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800230 UNUSED_ATTR uint32_t trans_id,
231 uint8_t op_code, tGATT_STATUS status,
232 tGATTS_RSP* p_msg) {
233 tGATT_STATUS ret_code = GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700235 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800236
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700237 gatt_sr_update_cback_cnt(tcb, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800238
Myles Watson911d1ae2016-11-28 16:44:40 -0800239 if (op_code == GATT_REQ_READ_MULTI) {
240 /* If no error and still waiting, just return */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700241 if (!process_read_multi_rsp(&tcb.sr_cmd, status, p_msg, tcb.payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800242 return (GATT_SUCCESS);
243 } else {
244 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700245 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800246
247 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700248 gatt_sr_reset_cback_cnt(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800249
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700250 tcb.sr_cmd.status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800251
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700252 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
253 if (tcb.sr_cmd.p_rsp_msg == NULL) {
254 tcb.sr_cmd.p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
255 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800256 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700257 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800258 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800259 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800260 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700261 if (gatt_sr_is_cback_cnt_zero(tcb)) {
262 if ((tcb.sr_cmd.status == GATT_SUCCESS) && (tcb.sr_cmd.p_rsp_msg)) {
263 ret_code = attp_send_sr_msg(tcb, tcb.sr_cmd.p_rsp_msg);
264 tcb.sr_cmd.p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800265 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700266 ret_code =
267 gatt_send_error_rsp(tcb, status, op_code, tcb.sr_cmd.handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800268 }
269
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700270 gatt_dequeue_sr_cmd(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800271 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700273 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800274
275 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800276}
277
278/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800279 *
280 * Function gatt_process_exec_write_req
281 *
282 * Description This function is called to process the execute write request
283 * from client.
284 *
285 * Returns void
286 *
287 ******************************************************************************/
Stanley Tngcc9c7332018-04-05 09:54:13 -0700288void gatt_process_exec_write_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
289 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800290 uint8_t *p = p_data, flag, i = 0;
291 uint32_t trans_id = 0;
292 tGATT_IF gatt_if;
293 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800294
Marie Janssend19e0782016-07-15 12:48:27 -0700295#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800296 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700297 VLOG(1)
298 << "Conformance tst: forced err rspv for Execute Write: error status="
299 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800300
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700301 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800302 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800303
Myles Watson911d1ae2016-11-28 16:44:40 -0800304 return;
305 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800306#endif
307
Stanley Tngcc9c7332018-04-05 09:54:13 -0700308 if (len < sizeof(flag)) {
309 android_errorWriteLog(0x534e4554, "73172115");
310 LOG(ERROR) << __func__ << "invalid length";
311 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_EXEC_WRITE, 0, false);
312 return;
313 }
314
Myles Watson911d1ae2016-11-28 16:44:40 -0800315 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800316
Myles Watson911d1ae2016-11-28 16:44:40 -0800317 /* mask the flag */
318 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800319
Myles Watson911d1ae2016-11-28 16:44:40 -0800320 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700321 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
322 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, 0);
323 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800324
Myles Watson911d1ae2016-11-28 16:44:40 -0800325 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700326 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800327 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700328 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700329 tGATTS_DATA gatts_data;
330 gatts_data.exec_write = flag;
Myles Watson911d1ae2016-11-28 16:44:40 -0800331 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
Myles Watson8d749042017-09-19 10:01:28 -0700332 &gatts_data);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700333 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800334 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800335 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800336 } else /* nothing needs to be executed , send response now */
337 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700338 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Jakub Pawlowski4be97502018-05-24 21:14:18 +0000339 gatt_send_error_rsp(tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800340 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800341}
342
343/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800344 *
345 * Function gatt_process_read_multi_req
346 *
347 * Description This function is called to process the read multiple request
348 * from client.
349 *
350 * Returns void
351 *
352 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700353void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
354 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800355 uint32_t trans_id;
356 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700357 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800358 tGATT_STATUS err = GATT_SUCCESS;
359 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800360
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700361 VLOG(1) << __func__;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700362 tcb.sr_cmd.multi_req.num_handles = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800363
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700364 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800365
Marie Janssend19e0782016-07-15 12:48:27 -0700366#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800367 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700368 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
369 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800370
Myles Watson911d1ae2016-11-28 16:44:40 -0800371 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800372
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700373 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -0800374 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800375
Myles Watson911d1ae2016-11-28 16:44:40 -0800376 return;
377 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800378#endif
379
Myles Watson911d1ae2016-11-28 16:44:40 -0800380 while (ll >= 2 &&
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700381 tcb.sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800382 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800383
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700384 auto it = gatt_sr_find_i_rcb_by_handle(handle);
385 if (it != gatt_cb.srv_list_info->end()) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700386 tcb.sr_cmd.multi_req.handles[tcb.sr_cmd.multi_req.num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800387
388 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700389 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
390 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800391 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700392 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800393 break;
394 }
395 } else {
396 /* invalid handle */
397 err = GATT_INVALID_HANDLE;
398 break;
399 }
400 ll -= 2;
401 }
402
403 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700404 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800405 }
406
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700407 if (tcb.sr_cmd.multi_req.num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800408
409 if (err == GATT_SUCCESS) {
410 trans_id =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700411 gatt_sr_enqueue_cmd(tcb, op_code, tcb.sr_cmd.multi_req.handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800412 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700413 gatt_sr_reset_cback_cnt(tcb); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800414
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700415 for (ll = 0; ll < tcb.sr_cmd.multi_req.num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800416 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700417 handle = tcb.sr_cmd.multi_req.handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700418 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419
Myles Watson911d1ae2016-11-28 16:44:40 -0800420 p_msg->attr_value.handle = handle;
421 err = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700422 tcb, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700423 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
424 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800425
426 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700427 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700428 GATT_SUCCESS, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800429 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800430 /* either not using or done using the buffer, release it now */
431 osi_free(p_msg);
432 }
433 } else
434 err = GATT_NO_RESOURCES;
435 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800436
Myles Watson911d1ae2016-11-28 16:44:40 -0800437 /* in theroy BUSY is not possible(should already been checked), protected
438 * check */
439 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700440 gatt_send_error_rsp(tcb, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800441}
442
443/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800444 *
445 * Function gatt_build_primary_service_rsp
446 *
447 * Description Primamry service request processed internally. Theretically
448 * only deal with ReadByTypeVAlue and ReadByGroupType.
449 *
450 * Returns void
451 *
452 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800453static tGATT_STATUS gatt_build_primary_service_rsp(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700454 BT_HDR* p_msg, tGATT_TCB& tcb, uint8_t op_code, uint16_t s_hdl,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700455 uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, const Uuid& value) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800456 tGATT_STATUS status = GATT_NOT_FOUND;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700457 uint8_t handle_len = 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800458
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700459 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800460
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700461 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700462 if (el.s_hdl < s_hdl || el.s_hdl > e_hdl ||
463 el.type != GATT_UUID_PRI_SERVICE) {
464 continue;
465 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700466
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700467 Uuid* p_uuid = gatts_get_service_uuid(el.p_db);
468 if (!p_uuid) continue;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800469
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700470 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Jakub Pawlowskif107e4f2018-04-12 05:42:31 -0700471 handle_len = 4 + gatt_build_uuid_to_stream_len(*p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800472
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700473 /* get the length byte in the repsonse */
474 if (p_msg->offset == 0) {
475 *p++ = op_code + 1;
476 p_msg->len++;
477 p_msg->offset = handle_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800478
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700479 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
480 *p++ = (uint8_t)p_msg->offset; /* length byte */
481 p_msg->len++;
Myles Watson911d1ae2016-11-28 16:44:40 -0800482 }
483 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700484
485 if (p_msg->len + p_msg->offset > tcb.payload_size ||
486 handle_len != p_msg->offset) {
487 break;
488 }
489
490 if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue;
491
492 UINT16_TO_STREAM(p, el.s_hdl);
493
Jakub Pawlowski4c6007c2018-04-16 07:55:06 -0700494 if (gatt_cb.last_service_handle &&
495 gatt_cb.last_service_handle == el.s_hdl) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700496 VLOG(1) << "Use 0xFFFF for the last primary attribute";
497 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
498 UINT16_TO_STREAM(p, 0xFFFF);
499 } else {
500 UINT16_TO_STREAM(p, el.e_hdl);
501 }
502
503 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
504 gatt_build_uuid_to_stream(&p, *p_uuid);
505
506 status = GATT_SUCCESS;
507 p_msg->len += p_msg->offset;
Myles Watson911d1ae2016-11-28 16:44:40 -0800508 }
509 p_msg->offset = L2CAP_MIN_OFFSET;
510
511 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800512}
513
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700514/**
515 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800516 *
517 * Returns true: if data filled sucessfully.
518 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700519 */
520static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700521 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700522 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800523 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800524
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700525 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800526
Myles Watson911d1ae2016-11-28 16:44:40 -0800527 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800528
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700529 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800530
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700531 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700532 if (attr.handle > e_hdl) break;
533
534 if (attr.handle < s_hdl) continue;
535
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700536 uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize();
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700537 if (p_msg->offset == 0)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700538 p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16
539 : GATT_INFO_TYPE_PAIR_128;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700540
541 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
542
543 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700544 uuid_len == Uuid::kNumBytes16) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700545 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700546 UINT16_TO_STREAM(p, attr.uuid.As16Bit());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700547 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700548 uuid_len == Uuid::kNumBytes128) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700549 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700550 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700551 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700552 uuid_len == Uuid::kNumBytes32) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700553 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700554 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700555 } else {
556 LOG(ERROR) << "format mismatch";
557 return GATT_NO_RESOURCES;
558 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700560 p_msg->len += info_pair_len[p_msg->offset - 1];
561 len -= info_pair_len[p_msg->offset - 1];
562 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800563 }
564
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700565 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800566}
567
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700568static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
569 uint16_t& e_hdl) {
570 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800571
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700572 /* obtain starting handle, and ending handle */
573 STREAM_TO_UINT16(s_hdl, p);
574 STREAM_TO_UINT16(e_hdl, p);
575 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700577 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
578 !GATT_HANDLE_IS_VALID(e_hdl)) {
Stanley Tngbe701122018-05-07 14:58:36 -0700579 return GATT_INVALID_HANDLE;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700580 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800581
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700582 return GATT_SUCCESS;
583}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800584
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700585static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700586 uint8_t*& p, Uuid* p_uuid,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700587 uint16_t& s_hdl,
588 uint16_t& e_hdl) {
589 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
590 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800591
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700592 if (len < 2) return GATT_INVALID_PDU;
593
594 /* parse uuid now */
595 CHECK(p_uuid);
596 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
597 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
598 VLOG(1) << "Bad UUID";
599 return GATT_INVALID_PDU;
600 }
601
602 len -= uuid_len;
603 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800604}
605
606/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800607 *
608 * Function gatts_process_primary_service_req
609 *
Myles Watson9ca07092016-11-28 16:41:53 -0800610 * Description Process ReadByGroupType/ReadByTypeValue request, for
611 * discovering all primary services or discover primary service
612 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800613 *
614 * Returns void
615 *
616 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700617void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800618 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800619 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700620 Uuid uuid = Uuid::kEmpty;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800621
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700622 uint8_t reason =
623 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
624 if (reason != GATT_SUCCESS) {
625 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
626 return;
627 }
628
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700629 if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700630 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
631 gatt_send_error_rsp(tcb, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700632 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s",
633 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700634 return;
635 }
636
637 // we do not support ReadByTypeValue with any non-primamry_service type
638 gatt_send_error_rsp(tcb, GATT_NOT_FOUND, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700639 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s",
640 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700641 return;
642 }
643
644 // TODO: we assume theh value is UUID, there is no such requirement in spec
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700645 Uuid value = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700646 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
Myles Watson5d5fcf22017-10-06 16:51:21 -0700647 if (!gatt_parse_uuid_from_cmd(&value, len, &p_data)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700648 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800649 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800650 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800651
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700652 uint16_t msg_len =
653 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
654 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
655 reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl, e_hdl,
656 p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800657 if (reason != GATT_SUCCESS) {
658 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700659 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700660 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700661 }
662
663 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800664}
665
666/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800667 *
668 * Function gatts_process_find_info
669 *
670 * Description process find information request, for discover character
671 * descriptors.
672 *
673 * Returns void
674 *
675 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700676static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800677 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700678 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700679 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700680 if (reason != GATT_SUCCESS) {
681 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
682 return;
683 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800684
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700685 uint16_t buf_len =
686 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800687
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700688 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
689 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800690
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700691 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
692 *p++ = op_code + 1;
693 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800694
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700695 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800696
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700697 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
698 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700699 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700700 if (reason == GATT_NO_RESOURCES) {
701 reason = GATT_SUCCESS;
702 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800703 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800704 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800705 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800706
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700707 *p = (uint8_t)p_msg->offset;
708
709 p_msg->offset = L2CAP_MIN_OFFSET;
710
Myles Watson911d1ae2016-11-28 16:44:40 -0800711 if (reason != GATT_SUCCESS) {
712 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700713 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800714 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700715 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800716}
717
718/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800719 *
720 * Function gatts_process_mtu_req
721 *
722 * Description This function is called to process excahnge MTU request.
723 * Only used on LE.
724 *
725 * Returns void
726 *
727 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700728static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800729 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800730 /* BR/EDR conenction, send error response */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700731 if (tcb.att_lcid != L2CAP_ATT_CID) {
732 gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700733 return;
734 }
735
736 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700737 LOG(ERROR) << "invalid MTU request PDU received.";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700738 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700739 return;
740 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800741
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700742 uint16_t mtu = 0;
743 uint8_t* p = p_data;
744 STREAM_TO_UINT16(mtu, p);
745 /* mtu must be greater than default MTU which is 23/48 */
746 if (mtu < GATT_DEF_BLE_MTU_SIZE)
747 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
748 else if (mtu > GATT_MAX_MTU_SIZE)
749 tcb.payload_size = GATT_MAX_MTU_SIZE;
750 else
751 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700752
Jakub Pawlowskibb956ab2018-05-24 12:27:10 -0700753 LOG(INFO) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800754
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700755 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
756 tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800757
Myles Watson8d749042017-09-19 10:01:28 -0700758 tGATT_SR_MSG gatt_sr_msg;
759 gatt_sr_msg.mtu = tcb.payload_size;
760 BT_HDR* p_buf = attp_build_sr_msg(tcb, GATT_RSP_MTU, &gatt_sr_msg);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700761 attp_send_sr_msg(tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800762
Myles Watson8d749042017-09-19 10:01:28 -0700763 tGATTS_DATA gatts_data;
764 gatts_data.mtu = tcb.payload_size;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700765 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
766 /* of 0, as no response is allowed from applcations */
767 for (int i = 0; i < GATT_MAX_APPS; i++) {
768 if (gatt_cb.cl_rcb[i].in_use) {
769 uint16_t conn_id =
770 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700771 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, &gatts_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800772 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800773 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774}
775
776/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800777 *
778 * Function gatts_process_read_by_type_req
779 *
780 * Description process Read By type request.
781 * This PDU can be used to perform:
782 * - read characteristic value
783 * - read characteristic descriptor value
784 * - discover characteristic
785 * - discover characteristic by UUID
786 * - relationship discovery
787 *
788 * Returns void
789 *
790 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700791void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800792 uint16_t len, uint8_t* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700793 Uuid uuid = Uuid::kEmpty;
Hansong Zhanga1603122018-04-12 11:45:03 -0700794 uint16_t s_hdl = 0, e_hdl = 0, err_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700795 tGATT_STATUS reason =
796 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800797
Marie Janssend19e0782016-07-15 12:48:27 -0700798#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800799 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700800 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
801 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800802
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700803 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800804 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800805
Myles Watson911d1ae2016-11-28 16:44:40 -0800806 return;
807 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808#endif
809
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700810 if (reason != GATT_SUCCESS) {
811 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
812 return;
813 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800814
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700815 size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
816 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
817 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800818
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700819 *p++ = op_code + 1;
820 /* reserve length byte */
821 p_msg->len = 2;
822 uint16_t buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800823
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700824 reason = GATT_NOT_FOUND;
825 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
826 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
827 uint8_t sec_flag, key_size;
828 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800829
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700830 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
831 tcb, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len, sec_flag,
832 key_size, 0, &err_hdl);
833 if (ret != GATT_NOT_FOUND) {
834 reason = ret;
835 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
836 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800837
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700838 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
839 s_hdl = err_hdl;
840 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800841 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800842 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800843 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700844 *p = (uint8_t)p_msg->offset;
845 p_msg->offset = L2CAP_MIN_OFFSET;
846
Myles Watson911d1ae2016-11-28 16:44:40 -0800847 if (reason != GATT_SUCCESS) {
848 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800849
Myles Watson911d1ae2016-11-28 16:44:40 -0800850 /* in theroy BUSY is not possible(should already been checked), protected
851 * check */
852 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700853 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700854
855 return;
856 }
857
858 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800859}
860
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700861/**
862 * This function is called to process the write request from client.
863 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700864void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700865 uint16_t handle, uint8_t op_code, uint16_t len,
866 uint8_t* p_data,
Myles Watson911d1ae2016-11-28 16:44:40 -0800867 bt_gatt_db_attribute_type_t gatt_type) {
868 tGATTS_DATA sr_data;
869 uint32_t trans_id;
870 tGATT_STATUS status;
871 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800872 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800873
Myles Watson911d1ae2016-11-28 16:44:40 -0800874 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800875
Myles Watson911d1ae2016-11-28 16:44:40 -0800876 switch (op_code) {
877 case GATT_REQ_PREPARE_WRITE:
878 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700879 LOG(ERROR) << __func__
880 << ": Prepare write request was invalid - missing offset, "
881 "sending error response";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700882 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800883 return;
884 }
885 sr_data.write_req.is_prep = true;
886 STREAM_TO_UINT16(sr_data.write_req.offset, p);
887 len -= 2;
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700888 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800889 case GATT_SIGN_CMD_WRITE:
890 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700891 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800892 len -= GATT_AUTH_SIGN_LEN;
893 }
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700894 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800895 case GATT_CMD_WRITE:
896 case GATT_REQ_WRITE:
897 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
898 sr_data.write_req.need_rsp = true;
899 sr_data.write_req.handle = handle;
900 sr_data.write_req.len = len;
901 if (len != 0 && p != NULL) {
902 memcpy(sr_data.write_req.value, p, len);
903 }
904 break;
905 }
906
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700907 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800908
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700909 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
910 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800911 sec_flag, key_size);
912
913 if (status == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700914 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800915 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700916 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800917
918 uint8_t opcode = 0;
919 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
920 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
921 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
922 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
923 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700924 LOG(ERROR) << __func__
925 << "%s: Attempt to write attribute that's not tied with"
926 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800927 status = GATT_ERROR;
928 }
929
930 if (opcode) {
931 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
932 status = GATT_PENDING;
933 }
934 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700935 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -0800936 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800937 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800938 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939
Myles Watson911d1ae2016-11-28 16:44:40 -0800940 /* in theroy BUSY is not possible(should already been checked), protected
941 * check */
942 if (status != GATT_PENDING && status != GATT_BUSY &&
943 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700944 gatt_send_error_rsp(tcb, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800945 }
946 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800947}
948
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700949/**
950 * This function is called to process the read request from client.
951 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700952static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Myles Watson911d1ae2016-11-28 16:44:40 -0800953 uint8_t op_code, uint16_t handle,
Stanley Tngcc9c7332018-04-05 09:54:13 -0700954 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700955 size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700956 uint16_t offset = 0;
Stanley Tngcc9c7332018-04-05 09:54:13 -0700957
958 if (op_code == GATT_REQ_READ_BLOB && len < sizeof(uint16_t)) {
959 /* Error: packet length is too short */
960 LOG(ERROR) << __func__ << ": packet length=" << len
961 << " too short. min=" << sizeof(uint16_t);
962 android_errorWriteWithInfoLog(0x534e4554, "73172115", -1, NULL, 0);
963 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
964 return;
965 }
966
Myles Watson911d1ae2016-11-28 16:44:40 -0800967 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800968
Myles Watson911d1ae2016-11-28 16:44:40 -0800969 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800970
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700971 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800972 *p++ = op_code + 1;
973 p_msg->len = 1;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700974 buf_len = tcb.payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800975
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700976 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700977 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800978
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700979 uint16_t value_len = 0;
980 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700981 tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700982 sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -0800983 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800984
Myles Watson911d1ae2016-11-28 16:44:40 -0800985 if (reason != GATT_SUCCESS) {
986 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800987
Stanley Tngcc9c7332018-04-05 09:54:13 -0700988 /* in theory BUSY is not possible(should already been checked), protected
Myles Watson911d1ae2016-11-28 16:44:40 -0800989 * check */
990 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700991 gatt_send_error_rsp(tcb, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700992
993 return;
994 }
995
996 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800997}
998
999/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001000 *
1001 * Function gatts_process_attribute_req
1002 *
Myles Watson9ca07092016-11-28 16:41:53 -08001003 * Description This function is called to process the per attribute handle
1004 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001005 *
1006 * Returns void
1007 *
1008 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001009void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
1010 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001011 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001012 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -08001013 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001014
Myles Watson911d1ae2016-11-28 16:44:40 -08001015 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001016 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -08001017 status = GATT_INVALID_PDU;
1018 } else {
1019 STREAM_TO_UINT16(handle, p);
1020 len -= 2;
1021 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001022
Marie Janssend19e0782016-07-15 12:48:27 -07001023#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001024 gatt_cb.handle = handle;
1025 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001026 VLOG(1) << "Conformance tst: forced err rsp: error status="
1027 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001028
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001029 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -08001030 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001031
Myles Watson911d1ae2016-11-28 16:44:40 -08001032 return;
1033 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001034#endif
1035
Myles Watson911d1ae2016-11-28 16:44:40 -08001036 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001037 for (auto& el : *gatt_cb.srv_list_info) {
1038 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1039 for (const auto& attr : el.p_db->attr_list) {
1040 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001041 switch (op_code) {
1042 case GATT_REQ_READ: /* read char/char descriptor value */
1043 case GATT_REQ_READ_BLOB:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001044 gatts_process_read_req(tcb, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001045 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001046
Myles Watson911d1ae2016-11-28 16:44:40 -08001047 case GATT_REQ_WRITE: /* write char/char descriptor value */
1048 case GATT_CMD_WRITE:
1049 case GATT_SIGN_CMD_WRITE:
1050 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001051 gatts_process_write_req(tcb, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001052 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001053 break;
1054 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001055 break;
1056 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001057 status = GATT_SUCCESS;
1058 break;
1059 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001061 break;
1062 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001063 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001064 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001065
Myles Watson911d1ae2016-11-28 16:44:40 -08001066 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1067 op_code != GATT_SIGN_CMD_WRITE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001068 gatt_send_error_rsp(tcb, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001069}
1070
1071/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001072 *
1073 * Function gatts_proc_srv_chg_ind_ack
1074 *
1075 * Description This function process the service changed indicaiton ACK
1076 *
1077 * Returns void
1078 *
1079 ******************************************************************************/
Jakub Pawlowski890c5012019-04-24 23:00:16 +02001080void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001081 tGATTS_SRV_CHG_REQ req;
1082 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001083
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001084 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001085
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001086 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001087 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001088 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001089 p_buf->srv_changed = false;
1090 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1091 if (gatt_cb.cb_info.p_srv_chg_callback)
1092 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1093 &req, NULL);
1094 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001095}
1096
1097/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001098 *
1099 * Function gatts_chk_pending_ind
1100 *
Myles Watson9ca07092016-11-28 16:41:53 -08001101 * Description This function check any pending indication needs to be sent
1102 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001103 *
1104 * Returns void
1105 *
1106 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001107static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001108 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001109
Myles Watson911d1ae2016-11-28 16:44:40 -08001110 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001111 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001112 if (p_buf != NULL) {
1113 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1114 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001115 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001116 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117}
1118
1119/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001120 *
1121 * Function gatts_proc_ind_ack
1122 *
Myles Watson9ca07092016-11-28 16:41:53 -08001123 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001124 *
Myles Watson9ca07092016-11-28 16:41:53 -08001125 * Returns true continue to process the indication ack by the
1126 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001127 *
1128 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001129static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001130 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001131
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001132 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001133
Myles Watson911d1ae2016-11-28 16:44:40 -08001134 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001135 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001136 /* there is no need to inform the application since srv chg is handled
1137 * internally by GATT */
1138 continue_processing = false;
1139 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001140
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001141 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001142 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001143}
1144
1145/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001146 *
1147 * Function gatts_process_value_conf
1148 *
Myles Watson9ca07092016-11-28 16:41:53 -08001149 * Description This function is called to process the handle value
1150 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001151 *
1152 * Returns void
1153 *
1154 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001155void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) {
1156 uint16_t handle = tcb.indicate_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001157
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001158 alarm_cancel(tcb.conf_timer);
1159 if (!GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001160 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001161 return;
1162 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001163
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001164 tcb.indicate_handle = 0;
1165 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1166
1167 if (continue_processing) {
Myles Watson8d749042017-09-19 10:01:28 -07001168 tGATTS_DATA gatts_data;
1169 gatts_data.handle = handle;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001170 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,
Myles Watson8d749042017-09-19 10:01:28 -07001175 &gatts_data);
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}