blob: d689acf16b97a0e0bd1ec259382535a6293e8513 [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 ******************************************************************************/
Hui Pengbcc29eb2023-07-27 04:09:04 +000024#include <algorithm>
The Android Open Source Project5738f832012-12-12 16:00:35 -080025#include "bt_target.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070026#include "osi/include/osi.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080027
The Android Open Source Project5738f832012-12-12 16:00:35 -080028#include <string.h>
Jakub Pawlowskie4f13782018-10-23 14:46:24 +020029
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include "gatt_int.h"
31#include "l2c_api.h"
Hansong Zhangcd0d0912021-02-23 16:35:31 -080032#include "osi/include/log.h"
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020033#include "stack/eatt/eatt.h"
Chris Mantonc5557232021-05-06 12:10:11 -070034#include "stack/l2cap/l2c_int.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080035#define GATT_MTU_REQ_MIN_LEN 2
Hansong Zhangf451ded2021-09-08 10:15:34 -070036#define L2CAP_PKT_OVERHEAD 4
The Android Open Source Project5738f832012-12-12 16:00:35 -080037
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070038using base::StringPrintf;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070039using bluetooth::Uuid;
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020040using bluetooth::eatt::EattExtension;
41using bluetooth::eatt::EattChannel;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070042
The Android Open Source Project5738f832012-12-12 16:00:35 -080043/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080044 *
45 * Function gatt_sr_enqueue_cmd
46 *
47 * Description This function enqueue the request from client which needs a
48 * application response, and update the transaction ID.
49 *
50 * Returns void
51 *
52 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020053uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
54 uint16_t handle) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020055 tGATT_SR_CMD* p_cmd;
56
57 if (cid == tcb.att_lcid) {
58 p_cmd = &tcb.sr_cmd;
59 } else {
60 EattChannel* channel =
61 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
62 p_cmd = &channel->server_outstanding_cmd_;
63 }
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020064
Myles Watson911d1ae2016-11-28 16:44:40 -080065 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080066
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020067 p_cmd->cid = cid;
68
Myles Watson911d1ae2016-11-28 16:44:40 -080069 if ((p_cmd->op_code == 0) ||
70 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
71 {
72 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
73 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070074 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080075 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070076 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080077 p_cmd->op_code = op_code;
78 p_cmd->handle = handle;
79 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070080 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080081 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080082 }
Myles Watson911d1ae2016-11-28 16:44:40 -080083 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080084
Myles Watson911d1ae2016-11-28 16:44:40 -080085 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080086}
87
88/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080089 *
90 * Function gatt_sr_cmd_empty
91 *
Myles Watson9ca07092016-11-28 16:41:53 -080092 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080093 *
94 * Returns true if empty, false if there is pending command.
95 *
96 ******************************************************************************/
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020097bool gatt_sr_cmd_empty(tGATT_TCB& tcb, uint16_t cid) {
98 if (cid == tcb.att_lcid) return (tcb.sr_cmd.op_code == 0);
99
100 EattChannel* channel =
101 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
102
103 return (channel->server_outstanding_cmd_.op_code == 0);
104}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800105
106/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800107 *
108 * Function gatt_dequeue_sr_cmd
109 *
110 * Description This function dequeue the request from command queue.
111 *
112 * Returns void
113 *
114 ******************************************************************************/
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200115void gatt_dequeue_sr_cmd(tGATT_TCB& tcb, uint16_t cid) {
116 tGATT_SR_CMD* p_cmd;
117
118 if (cid == tcb.att_lcid) {
119 p_cmd = &tcb.sr_cmd;
120 } else {
121 EattChannel* channel =
122 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
123
124 p_cmd = &channel->server_outstanding_cmd_;
125 }
126
Myles Watson911d1ae2016-11-28 16:44:40 -0800127 /* Double check in case any buffers are queued */
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200128 VLOG(1) << "gatt_dequeue_sr_cmd cid: " << loghex(cid);
129 if (p_cmd->p_rsp_msg)
130 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = "
131 << p_cmd->p_rsp_msg;
132 osi_free_and_reset((void**)&p_cmd->p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800133
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200134 while (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
135 osi_free(fixed_queue_try_dequeue(p_cmd->multi_rsp_q));
136 fixed_queue_free(p_cmd->multi_rsp_q, NULL);
137 memset(p_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800138}
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200139
140static void build_read_multi_rsp(tGATT_SR_CMD* p_cmd, uint16_t mtu) {
Brian Delwichea0672b22023-05-16 22:49:36 +0000141 uint16_t ii;
142 size_t total_len, len;
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200143 uint8_t* p;
144 bool is_overflow = false;
145
146 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
147 BT_HDR* p_buf = (BT_HDR*)osi_calloc(len);
148 p_buf->offset = L2CAP_MIN_OFFSET;
149 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
150
151 /* First byte in the response is the opcode */
152 if (p_cmd->multi_req.variable_len)
153 *p++ = GATT_RSP_READ_MULTI_VAR;
154 else
155 *p++ = GATT_RSP_READ_MULTI;
156
157 p_buf->len = 1;
158
159 /* Now walk through the buffers putting the data into the response in order
160 */
161 list_t* list = NULL;
162 const list_node_t* node = NULL;
163 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
164 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
165 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
166 tGATTS_RSP* p_rsp = NULL;
167
168 if (list != NULL) {
169 if (ii == 0)
170 node = list_begin(list);
171 else
172 node = list_next(node);
173 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
174 }
175
176 if (p_rsp != NULL) {
Hui Pengbcc29eb2023-07-27 04:09:04 +0000177 total_len = p_buf->len;
Jakub Pawlowskie82cd6e2022-04-04 21:49:42 +0200178 if (p_cmd->multi_req.variable_len) {
179 total_len += 2;
180 }
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200181
182 if (total_len > mtu) {
Hui Pengbcc29eb2023-07-27 04:09:04 +0000183 VLOG(1) << "Buffer space not enough for this data item, skipping";
184 break;
185 }
186
187 len = std::min((size_t) p_rsp->attr_value.len, mtu - total_len);
188
189 if (len == 0) {
190 VLOG(1) << "Buffer space not enough for this data item, skipping";
191 break;
192 }
193
194 if (len < p_rsp->attr_value.len) {
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200195 /* just send the partial response for the overflow case */
196 len = p_rsp->attr_value.len - (total_len - mtu);
197 is_overflow = true;
198 VLOG(1) << StringPrintf(
Brian Delwichea0672b22023-05-16 22:49:36 +0000199 "multi read overflow available len=%zu val_len=%d", len,
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200200 p_rsp->attr_value.len);
201 } else {
202 len = p_rsp->attr_value.len;
203 }
204
205 if (p_cmd->multi_req.variable_len) {
Hui Pengbcc29eb2023-07-27 04:09:04 +0000206 UINT16_TO_STREAM(p, (uint16_t) len);
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200207 p_buf->len += 2;
208 }
209
210 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
Hui Pengbcc29eb2023-07-27 04:09:04 +0000211 ARRAY_TO_STREAM(p, p_rsp->attr_value.value, (uint16_t) len);
212 p_buf->len += (uint16_t) len;
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200213 } else {
214 p_cmd->status = GATT_NOT_FOUND;
215 break;
216 }
217
218 if (is_overflow) break;
219
220 } else {
221 p_cmd->status = GATT_NOT_FOUND;
222 break;
223 }
224
225 } /* loop through all handles*/
226
227 /* Sanity check on the buffer length */
228 if (p_buf->len == 0) {
229 LOG(ERROR) << __func__ << " nothing found!!";
230 p_cmd->status = GATT_NOT_FOUND;
231 osi_free(p_buf);
232 VLOG(1) << __func__ << "osi_free(p_buf)";
233 } else if (p_cmd->p_rsp_msg != NULL) {
234 osi_free(p_buf);
235 } else {
236 p_cmd->p_rsp_msg = p_buf;
237 }
238}
239
The Android Open Source Project5738f832012-12-12 16:00:35 -0800240/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800241 *
242 * Function process_read_multi_rsp
243 *
244 * Description This function check the read multiple response.
245 *
246 * Returns bool if all replies have been received
247 *
248 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800249static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
250 tGATTS_RSP* p_msg, uint16_t mtu) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700251 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700252
Myles Watson911d1ae2016-11-28 16:44:40 -0800253 if (p_cmd->multi_rsp_q == NULL)
254 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800255
Myles Watson911d1ae2016-11-28 16:44:40 -0800256 /* Enqueue the response */
257 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
258 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
259 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800260
Myles Watson911d1ae2016-11-28 16:44:40 -0800261 p_cmd->status = status;
262 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700263 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200264 << " num_hdls=" << p_cmd->multi_req.num_handles
265 << " variable=" << p_cmd->multi_req.variable_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800266 /* Wait till we get all the responses */
267 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
268 p_cmd->multi_req.num_handles) {
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200269 build_read_multi_rsp(p_cmd, mtu);
Myles Watson911d1ae2016-11-28 16:44:40 -0800270 return (true);
271 }
272 } else /* any handle read exception occurs, return error */
273 {
274 return (true);
275 }
276
277 /* If here, still waiting */
278 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800279}
280
281/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800282 *
283 * Function gatt_sr_process_app_rsp
284 *
Myles Watson9ca07092016-11-28 16:41:53 -0800285 * Description This function checks whether the response message from
286 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800287 *
288 * Returns void
289 *
290 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700291tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800292 UNUSED_ATTR uint32_t trans_id,
293 uint8_t op_code, tGATT_STATUS status,
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200294 tGATTS_RSP* p_msg,
295 tGATT_SR_CMD* sr_res_p) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800296 tGATT_STATUS ret_code = GATT_SUCCESS;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200297 uint16_t payload_size = gatt_tcb_get_payload_size_rx(tcb, sr_res_p->cid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700299 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800300
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200301 gatt_sr_update_cback_cnt(tcb, sr_res_p->cid, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800302
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200303 if ((op_code == GATT_REQ_READ_MULTI) ||
304 (op_code == GATT_REQ_READ_MULTI_VAR)) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800305 /* If no error and still waiting, just return */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200306 if (!process_read_multi_rsp(sr_res_p, status, p_msg, payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800307 return (GATT_SUCCESS);
308 } else {
309 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700310 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800311
312 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200313 gatt_sr_reset_cback_cnt(tcb, sr_res_p->cid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800314
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200315 sr_res_p->status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800316
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700317 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200318 if (sr_res_p->p_rsp_msg == NULL) {
319 sr_res_p->p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
320 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800321 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700322 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800323 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800324 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800325 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700326 if (gatt_sr_is_cback_cnt_zero(tcb)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200327 if ((sr_res_p->status == GATT_SUCCESS) && (sr_res_p->p_rsp_msg)) {
328 ret_code = attp_send_sr_msg(tcb, sr_res_p->cid, sr_res_p->p_rsp_msg);
329 sr_res_p->p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800330 } else {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200331 ret_code = gatt_send_error_rsp(tcb, sr_res_p->cid, status, op_code,
332 sr_res_p->handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800333 }
334
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200335 gatt_dequeue_sr_cmd(tcb, sr_res_p->cid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800336 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800337
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700338 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800339
340 return ret_code;
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_exec_write_req
346 *
347 * Description This function is called to process the execute write request
348 * from client.
349 *
350 * Returns void
351 *
352 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200353void gatt_process_exec_write_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
354 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800355 uint8_t *p = p_data, flag, i = 0;
356 uint32_t trans_id = 0;
357 tGATT_IF gatt_if;
358 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359
Marie Janssend19e0782016-07-15 12:48:27 -0700360#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800361 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700362 VLOG(1)
363 << "Conformance tst: forced err rspv for Execute Write: error status="
364 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800365
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200366 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800367 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800368
Myles Watson911d1ae2016-11-28 16:44:40 -0800369 return;
370 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800371#endif
372
Stanley Tngcc9c7332018-04-05 09:54:13 -0700373 if (len < sizeof(flag)) {
374 android_errorWriteLog(0x534e4554, "73172115");
375 LOG(ERROR) << __func__ << "invalid length";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200376 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, GATT_REQ_EXEC_WRITE, 0,
377 false);
Stanley Tngcc9c7332018-04-05 09:54:13 -0700378 return;
379 }
380
Myles Watson911d1ae2016-11-28 16:44:40 -0800381 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800382
Myles Watson911d1ae2016-11-28 16:44:40 -0800383 /* mask the flag */
384 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800385
Myles Watson911d1ae2016-11-28 16:44:40 -0800386 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700387 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200388 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, 0);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700389 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800390
Myles Watson911d1ae2016-11-28 16:44:40 -0800391 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700392 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800393 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700394 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700395 tGATTS_DATA gatts_data;
396 gatts_data.exec_write = flag;
Myles Watson911d1ae2016-11-28 16:44:40 -0800397 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
Myles Watson8d749042017-09-19 10:01:28 -0700398 &gatts_data);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700399 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800400 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800401 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800402 } else /* nothing needs to be executed , send response now */
403 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700404 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200405 gatt_send_error_rsp(tcb, cid, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800406 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800407}
408
409/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800410 *
411 * Function gatt_process_read_multi_req
412 *
413 * Description This function is called to process the read multiple request
414 * from client.
415 *
416 * Returns void
417 *
418 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200419void gatt_process_read_multi_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
420 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800421 uint32_t trans_id;
422 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700423 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800424 tGATT_STATUS err = GATT_SUCCESS;
425 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800426
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700427 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800428
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200429 tGATT_READ_MULTI* multi_req = gatt_sr_get_read_multi(tcb, cid);
430 multi_req->num_handles = 0;
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200431 multi_req->variable_len = (op_code == GATT_REQ_READ_MULTI_VAR);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700432 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800433
Marie Janssend19e0782016-07-15 12:48:27 -0700434#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800435 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700436 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
437 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800438
Myles Watson911d1ae2016-11-28 16:44:40 -0800439 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800440
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200441 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
442 handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800443
Myles Watson911d1ae2016-11-28 16:44:40 -0800444 return;
445 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800446#endif
447
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200448 while (ll >= 2 && multi_req->num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800449 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800450
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700451 auto it = gatt_sr_find_i_rcb_by_handle(handle);
452 if (it != gatt_cb.srv_list_info->end()) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200453 multi_req->handles[multi_req->num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800454
455 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700456 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
457 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800458 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700459 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800460 break;
461 }
462 } else {
463 /* invalid handle */
464 err = GATT_INVALID_HANDLE;
465 break;
466 }
467 ll -= 2;
468 }
469
470 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700471 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800472 }
473
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200474 if (multi_req->num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800475
476 if (err == GATT_SUCCESS) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200477 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, multi_req->handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800478 if (trans_id != 0) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200479 tGATT_SR_CMD* sr_cmd_p = gatt_sr_get_cmd_by_cid(tcb, cid);
480
481 gatt_sr_reset_cback_cnt(tcb,
482 cid); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800483
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200484 for (ll = 0; ll < multi_req->num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800485 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200486 handle = multi_req->handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700487 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488
Myles Watson911d1ae2016-11-28 16:44:40 -0800489 p_msg->attr_value.handle = handle;
490 err = gatts_read_attr_value_by_handle(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200491 tcb, cid, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700492 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
493 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800494
495 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700496 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200497 GATT_SUCCESS, p_msg, sr_cmd_p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800498 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800499 /* either not using or done using the buffer, release it now */
500 osi_free(p_msg);
501 }
502 } else
503 err = GATT_NO_RESOURCES;
504 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800505
Myles Watson911d1ae2016-11-28 16:44:40 -0800506 /* in theroy BUSY is not possible(should already been checked), protected
507 * check */
508 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200509 gatt_send_error_rsp(tcb, cid, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800510}
511
512/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800513 *
514 * Function gatt_build_primary_service_rsp
515 *
516 * Description Primamry service request processed internally. Theretically
517 * only deal with ReadByTypeVAlue and ReadByGroupType.
518 *
519 * Returns void
520 *
521 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800522static tGATT_STATUS gatt_build_primary_service_rsp(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200523 BT_HDR* p_msg, tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
524 uint16_t s_hdl, uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data,
525 const Uuid& value) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800526 tGATT_STATUS status = GATT_NOT_FOUND;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700527 uint8_t handle_len = 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800528
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700529 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800530
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200531 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
532
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700533 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700534 if (el.s_hdl < s_hdl || el.s_hdl > e_hdl ||
535 el.type != GATT_UUID_PRI_SERVICE) {
536 continue;
537 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700538
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700539 Uuid* p_uuid = gatts_get_service_uuid(el.p_db);
540 if (!p_uuid) continue;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800541
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700542 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Jakub Pawlowskif107e4f2018-04-12 05:42:31 -0700543 handle_len = 4 + gatt_build_uuid_to_stream_len(*p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800544
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700545 /* get the length byte in the repsonse */
546 if (p_msg->offset == 0) {
547 *p++ = op_code + 1;
548 p_msg->len++;
549 p_msg->offset = handle_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800550
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700551 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
552 *p++ = (uint8_t)p_msg->offset; /* length byte */
553 p_msg->len++;
Myles Watson911d1ae2016-11-28 16:44:40 -0800554 }
555 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700556
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200557 if (p_msg->len + p_msg->offset > payload_size ||
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700558 handle_len != p_msg->offset) {
559 break;
560 }
561
562 if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue;
563
564 UINT16_TO_STREAM(p, el.s_hdl);
565
Jakub Pawlowski4c6007c2018-04-16 07:55:06 -0700566 if (gatt_cb.last_service_handle &&
567 gatt_cb.last_service_handle == el.s_hdl) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700568 VLOG(1) << "Use 0xFFFF for the last primary attribute";
569 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
570 UINT16_TO_STREAM(p, 0xFFFF);
571 } else {
572 UINT16_TO_STREAM(p, el.e_hdl);
573 }
574
575 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
576 gatt_build_uuid_to_stream(&p, *p_uuid);
577
578 status = GATT_SUCCESS;
579 p_msg->len += p_msg->offset;
Myles Watson911d1ae2016-11-28 16:44:40 -0800580 }
581 p_msg->offset = L2CAP_MIN_OFFSET;
582
583 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800584}
585
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700586/**
587 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800588 *
589 * Returns true: if data filled sucessfully.
590 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700591 */
592static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700593 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700594 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800595 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800596
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700597 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800598
Myles Watson911d1ae2016-11-28 16:44:40 -0800599 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700601 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800602
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700603 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700604 if (attr.handle > e_hdl) break;
605
606 if (attr.handle < s_hdl) continue;
607
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700608 uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize();
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700609 if (p_msg->offset == 0)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700610 p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16
611 : GATT_INFO_TYPE_PAIR_128;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700612
613 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
614
615 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700616 uuid_len == Uuid::kNumBytes16) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700617 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700618 UINT16_TO_STREAM(p, attr.uuid.As16Bit());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700619 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700620 uuid_len == Uuid::kNumBytes128) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700621 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700622 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700623 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700624 uuid_len == Uuid::kNumBytes32) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700625 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700626 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700627 } else {
628 LOG(ERROR) << "format mismatch";
629 return GATT_NO_RESOURCES;
630 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700632 p_msg->len += info_pair_len[p_msg->offset - 1];
633 len -= info_pair_len[p_msg->offset - 1];
634 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800635 }
636
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700637 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800638}
639
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700640static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
641 uint16_t& e_hdl) {
642 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800643
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700644 /* obtain starting handle, and ending handle */
645 STREAM_TO_UINT16(s_hdl, p);
646 STREAM_TO_UINT16(e_hdl, p);
647 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800648
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700649 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
650 !GATT_HANDLE_IS_VALID(e_hdl)) {
Stanley Tngbe701122018-05-07 14:58:36 -0700651 return GATT_INVALID_HANDLE;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700652 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800653
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700654 return GATT_SUCCESS;
655}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800656
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700657static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700658 uint8_t*& p, Uuid* p_uuid,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700659 uint16_t& s_hdl,
660 uint16_t& e_hdl) {
661 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
662 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800663
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700664 if (len < 2) return GATT_INVALID_PDU;
665
666 /* parse uuid now */
667 CHECK(p_uuid);
668 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
669 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
670 VLOG(1) << "Bad UUID";
671 return GATT_INVALID_PDU;
672 }
673
674 len -= uuid_len;
675 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800676}
677
678/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800679 *
680 * Function gatts_process_primary_service_req
681 *
Myles Watson9ca07092016-11-28 16:41:53 -0800682 * Description Process ReadByGroupType/ReadByTypeValue request, for
683 * discovering all primary services or discover primary service
684 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800685 *
686 * Returns void
687 *
688 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200689void gatts_process_primary_service_req(tGATT_TCB& tcb, uint16_t cid,
690 uint8_t op_code, uint16_t len,
691 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800692 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700693 Uuid uuid = Uuid::kEmpty;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800694
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700695 uint8_t reason =
696 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
697 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200698 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700699 return;
700 }
701
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700702 if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700703 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200704 gatt_send_error_rsp(tcb, cid, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl,
705 false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700706 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s",
707 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700708 return;
709 }
710
711 // we do not support ReadByTypeValue with any non-primamry_service type
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200712 gatt_send_error_rsp(tcb, cid, GATT_NOT_FOUND, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700713 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s",
714 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700715 return;
716 }
717
718 // TODO: we assume theh value is UUID, there is no such requirement in spec
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700719 Uuid value = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700720 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
Myles Watson5d5fcf22017-10-06 16:51:21 -0700721 if (!gatt_parse_uuid_from_cmd(&value, len, &p_data)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200722 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800723 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800724 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800725
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200726 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
727
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700728 uint16_t msg_len =
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200729 (uint16_t)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700730 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200731 reason = gatt_build_primary_service_rsp(p_msg, tcb, cid, op_code, s_hdl,
732 e_hdl, p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800733 if (reason != GATT_SUCCESS) {
734 osi_free(p_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200735 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700736 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700737 }
738
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200739 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800740}
741
742/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800743 *
744 * Function gatts_process_find_info
745 *
746 * Description process find information request, for discover character
747 * descriptors.
748 *
749 * Returns void
750 *
751 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200752static void gatts_process_find_info(tGATT_TCB& tcb, uint16_t cid,
753 uint8_t op_code, uint16_t len,
754 uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700755 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700756 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700757 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200758 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700759 return;
760 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800761
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200762 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700763 uint16_t buf_len =
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200764 (uint16_t)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800765
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700766 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
767 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800768
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700769 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
770 *p++ = op_code + 1;
771 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800772
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200773 buf_len = payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700775 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
776 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700777 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700778 if (reason == GATT_NO_RESOURCES) {
779 reason = GATT_SUCCESS;
780 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800781 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800782 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800783 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800784
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700785 *p = (uint8_t)p_msg->offset;
786
787 p_msg->offset = L2CAP_MIN_OFFSET;
788
Myles Watson911d1ae2016-11-28 16:44:40 -0800789 if (reason != GATT_SUCCESS) {
790 osi_free(p_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200791 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800792 } else
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200793 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800794}
795
796/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800797 *
798 * Function gatts_process_mtu_req
799 *
800 * Description This function is called to process excahnge MTU request.
801 * Only used on LE.
802 *
803 * Returns void
804 *
805 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200806static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t cid, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800807 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800808 /* BR/EDR conenction, send error response */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200809 if (cid != L2CAP_ATT_CID) {
810 gatt_send_error_rsp(tcb, cid, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0,
811 false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700812 return;
813 }
814
815 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700816 LOG(ERROR) << "invalid MTU request PDU received.";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200817 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700818 return;
819 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800820
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700821 uint16_t mtu = 0;
822 uint8_t* p = p_data;
823 STREAM_TO_UINT16(mtu, p);
824 /* mtu must be greater than default MTU which is 23/48 */
825 if (mtu < GATT_DEF_BLE_MTU_SIZE)
826 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
827 else if (mtu > GATT_MAX_MTU_SIZE)
828 tcb.payload_size = GATT_MAX_MTU_SIZE;
829 else
830 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700831
Jakub Pawlowskibb956ab2018-05-24 12:27:10 -0700832 LOG(INFO) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800833
Hansong Zhangf451ded2021-09-08 10:15:34 -0700834 BTM_SetBleDataLength(tcb.peer_bda, tcb.payload_size + L2CAP_PKT_OVERHEAD);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800835
Myles Watson8d749042017-09-19 10:01:28 -0700836 tGATT_SR_MSG gatt_sr_msg;
837 gatt_sr_msg.mtu = tcb.payload_size;
838 BT_HDR* p_buf = attp_build_sr_msg(tcb, GATT_RSP_MTU, &gatt_sr_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200839 attp_send_sr_msg(tcb, cid, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800840
Myles Watson8d749042017-09-19 10:01:28 -0700841 tGATTS_DATA gatts_data;
842 gatts_data.mtu = tcb.payload_size;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700843 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
844 /* of 0, as no response is allowed from applcations */
845 for (int i = 0; i < GATT_MAX_APPS; i++) {
846 if (gatt_cb.cl_rcb[i].in_use) {
847 uint16_t conn_id =
848 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700849 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, &gatts_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800850 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800851 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800852}
853
854/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800855 *
856 * Function gatts_process_read_by_type_req
857 *
858 * Description process Read By type request.
859 * This PDU can be used to perform:
860 * - read characteristic value
861 * - read characteristic descriptor value
862 * - discover characteristic
863 * - discover characteristic by UUID
864 * - relationship discovery
865 *
866 * Returns void
867 *
868 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200869void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint16_t cid,
870 uint8_t op_code, uint16_t len,
871 uint8_t* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700872 Uuid uuid = Uuid::kEmpty;
Hansong Zhanga1603122018-04-12 11:45:03 -0700873 uint16_t s_hdl = 0, e_hdl = 0, err_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700874 tGATT_STATUS reason =
875 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800876
Marie Janssend19e0782016-07-15 12:48:27 -0700877#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800878 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700879 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
880 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800881
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200882 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
883 s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800884
Myles Watson911d1ae2016-11-28 16:44:40 -0800885 return;
886 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800887#endif
888
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700889 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200890 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700891 return;
892 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800893
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200894 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
895
896 size_t msg_len = sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700897 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
898 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800899
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700900 *p++ = op_code + 1;
901 /* reserve length byte */
902 p_msg->len = 2;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200903 uint16_t buf_len = payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800904
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700905 reason = GATT_NOT_FOUND;
906 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
907 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
908 uint8_t sec_flag, key_size;
909 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800910
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700911 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200912 tcb, cid, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len,
913 sec_flag, key_size, 0, &err_hdl);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700914 if (ret != GATT_NOT_FOUND) {
915 reason = ret;
916 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
917 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800918
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700919 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
920 s_hdl = err_hdl;
921 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800922 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800924 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700925 *p = (uint8_t)p_msg->offset;
926 p_msg->offset = L2CAP_MIN_OFFSET;
927
Myles Watson911d1ae2016-11-28 16:44:40 -0800928 if (reason != GATT_SUCCESS) {
929 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800930
Myles Watson911d1ae2016-11-28 16:44:40 -0800931 /* in theroy BUSY is not possible(should already been checked), protected
932 * check */
933 if (reason != GATT_PENDING && reason != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200934 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700935
936 return;
937 }
938
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200939 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800940}
941
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700942/**
943 * This function is called to process the write request from client.
944 */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200945static void gatts_process_write_req(tGATT_TCB& tcb, uint16_t cid,
946 tGATT_SRV_LIST_ELEM& el, uint16_t handle,
947 uint8_t op_code, uint16_t len,
948 uint8_t* p_data,
Chris Mantonefa23112020-03-04 11:26:31 -0800949 bt_gatt_db_attribute_type_t gatt_type) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800950 tGATTS_DATA sr_data;
951 uint32_t trans_id;
952 tGATT_STATUS status;
953 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800954 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800955
Myles Watson911d1ae2016-11-28 16:44:40 -0800956 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800957
Myles Watson911d1ae2016-11-28 16:44:40 -0800958 switch (op_code) {
959 case GATT_REQ_PREPARE_WRITE:
Chris Mantonefa23112020-03-04 11:26:31 -0800960 if (len < 2 || p == nullptr) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700961 LOG(ERROR) << __func__
962 << ": Prepare write request was invalid - missing offset, "
963 "sending error response";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200964 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800965 return;
966 }
967 sr_data.write_req.is_prep = true;
968 STREAM_TO_UINT16(sr_data.write_req.offset, p);
969 len -= 2;
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700970 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800971 case GATT_SIGN_CMD_WRITE:
972 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700973 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800974 len -= GATT_AUTH_SIGN_LEN;
975 }
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700976 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800977 case GATT_CMD_WRITE:
978 case GATT_REQ_WRITE:
979 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
980 sr_data.write_req.need_rsp = true;
981 sr_data.write_req.handle = handle;
Chris Mantonefa23112020-03-04 11:26:31 -0800982 if (len > GATT_MAX_ATTR_LEN) len = GATT_MAX_ATTR_LEN;
Myles Watson911d1ae2016-11-28 16:44:40 -0800983 sr_data.write_req.len = len;
Chris Mantonefa23112020-03-04 11:26:31 -0800984 if (len != 0 && p != nullptr) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800985 memcpy(sr_data.write_req.value, p, len);
986 }
987 break;
988 }
989
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700990 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800991
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700992 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
993 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800994 sec_flag, key_size);
995
996 if (status == GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200997 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800998 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700999 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -08001000
1001 uint8_t opcode = 0;
1002 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
1003 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
1004 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
1005 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
1006 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001007 LOG(ERROR) << __func__
1008 << "%s: Attempt to write attribute that's not tied with"
1009 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -08001010 status = GATT_ERROR;
1011 }
1012
1013 if (opcode) {
1014 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
1015 status = GATT_PENDING;
1016 }
1017 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001018 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -08001019 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001020 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001021 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001022
Myles Watson911d1ae2016-11-28 16:44:40 -08001023 /* in theroy BUSY is not possible(should already been checked), protected
1024 * check */
1025 if (status != GATT_PENDING && status != GATT_BUSY &&
1026 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001027 gatt_send_error_rsp(tcb, cid, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -08001028 }
1029 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001030}
1031
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001032/**
1033 * This function is called to process the read request from client.
1034 */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001035static void gatts_process_read_req(tGATT_TCB& tcb, uint16_t cid,
1036 tGATT_SRV_LIST_ELEM& el, uint8_t op_code,
1037 uint16_t handle, uint16_t len,
1038 uint8_t* p_data) {
1039 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
1040
1041 size_t buf_len = sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001042 uint16_t offset = 0;
Stanley Tngcc9c7332018-04-05 09:54:13 -07001043
1044 if (op_code == GATT_REQ_READ_BLOB && len < sizeof(uint16_t)) {
1045 /* Error: packet length is too short */
1046 LOG(ERROR) << __func__ << ": packet length=" << len
1047 << " too short. min=" << sizeof(uint16_t);
1048 android_errorWriteWithInfoLog(0x534e4554, "73172115", -1, NULL, 0);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001049 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, 0, false);
Stanley Tngcc9c7332018-04-05 09:54:13 -07001050 return;
1051 }
1052
Myles Watson911d1ae2016-11-28 16:44:40 -08001053 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001054
Myles Watson911d1ae2016-11-28 16:44:40 -08001055 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001056
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001057 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -08001058 *p++ = op_code + 1;
1059 p_msg->len = 1;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001060 buf_len = payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001061
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001062 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001063 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001064
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001065 uint16_t value_len = 0;
1066 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001067 tcb, cid, el.p_db, op_code, handle, offset, p, &value_len,
1068 (uint16_t)buf_len, sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -08001069 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001070
Myles Watson911d1ae2016-11-28 16:44:40 -08001071 if (reason != GATT_SUCCESS) {
1072 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001073
Stanley Tngcc9c7332018-04-05 09:54:13 -07001074 /* in theory BUSY is not possible(should already been checked), protected
Myles Watson911d1ae2016-11-28 16:44:40 -08001075 * check */
1076 if (reason != GATT_PENDING && reason != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001077 gatt_send_error_rsp(tcb, cid, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001078
1079 return;
1080 }
1081
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001082 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001083}
1084
1085/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001086 *
1087 * Function gatts_process_attribute_req
1088 *
Myles Watson9ca07092016-11-28 16:41:53 -08001089 * Description This function is called to process the per attribute handle
1090 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001091 *
1092 * Returns void
1093 *
1094 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001095void gatts_process_attribute_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
1096 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001097 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001098 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -08001099 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100
Myles Watson911d1ae2016-11-28 16:44:40 -08001101 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001102 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -08001103 status = GATT_INVALID_PDU;
1104 } else {
1105 STREAM_TO_UINT16(handle, p);
1106 len -= 2;
1107 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001108
Marie Janssend19e0782016-07-15 12:48:27 -07001109#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001110 gatt_cb.handle = handle;
1111 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001112 VLOG(1) << "Conformance tst: forced err rsp: error status="
1113 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001115 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, cid, gatt_cb.req_op_code,
1116 handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117
Myles Watson911d1ae2016-11-28 16:44:40 -08001118 return;
1119 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001120#endif
1121
Myles Watson911d1ae2016-11-28 16:44:40 -08001122 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001123 for (auto& el : *gatt_cb.srv_list_info) {
1124 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1125 for (const auto& attr : el.p_db->attr_list) {
1126 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001127 switch (op_code) {
1128 case GATT_REQ_READ: /* read char/char descriptor value */
1129 case GATT_REQ_READ_BLOB:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001130 gatts_process_read_req(tcb, cid, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001131 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132
Myles Watson911d1ae2016-11-28 16:44:40 -08001133 case GATT_REQ_WRITE: /* write char/char descriptor value */
1134 case GATT_CMD_WRITE:
1135 case GATT_SIGN_CMD_WRITE:
1136 case GATT_REQ_PREPARE_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001137 gatts_process_write_req(tcb, cid, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001138 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001139 break;
1140 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001141 break;
1142 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001143 status = GATT_SUCCESS;
1144 break;
1145 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001146 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001147 break;
1148 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001149 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001150 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001151
Myles Watson911d1ae2016-11-28 16:44:40 -08001152 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1153 op_code != GATT_SIGN_CMD_WRITE)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001154 gatt_send_error_rsp(tcb, cid, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001155}
1156
1157/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001158 *
1159 * Function gatts_proc_srv_chg_ind_ack
1160 *
1161 * Description This function process the service changed indicaiton ACK
1162 *
1163 * Returns void
1164 *
1165 ******************************************************************************/
Jakub Pawlowski890c5012019-04-24 23:00:16 +02001166void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001167 tGATTS_SRV_CHG_REQ req;
1168 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001169
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001170 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001171
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001172 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001173 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001174 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001175 p_buf->srv_changed = false;
1176 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1177 if (gatt_cb.cb_info.p_srv_chg_callback)
1178 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1179 &req, NULL);
1180 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001181}
1182
1183/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001184 *
1185 * Function gatts_chk_pending_ind
1186 *
Myles Watson9ca07092016-11-28 16:41:53 -08001187 * Description This function check any pending indication needs to be sent
1188 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001189 *
1190 * Returns void
1191 *
1192 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001193static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001194 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001195
Myles Watson911d1ae2016-11-28 16:44:40 -08001196 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001197 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001198 if (p_buf != NULL) {
1199 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1200 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001201 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001202 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001203}
1204
1205/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001206 *
1207 * Function gatts_proc_ind_ack
1208 *
Myles Watson9ca07092016-11-28 16:41:53 -08001209 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001210 *
Myles Watson9ca07092016-11-28 16:41:53 -08001211 * Returns true continue to process the indication ack by the
1212 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001213 *
1214 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001215static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001216 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001217
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001218 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001219
Myles Watson911d1ae2016-11-28 16:44:40 -08001220 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001221 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001222 /* there is no need to inform the application since srv chg is handled
1223 * internally by GATT */
1224 continue_processing = false;
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001225
1226 // After receiving ack of svc_chg_ind, reset client status
1227 gatt_sr_update_cl_status(tcb, /* chg_aware= */ true);
Myles Watson911d1ae2016-11-28 16:44:40 -08001228 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001229
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001230 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001231 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001232}
1233
1234/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001235 *
1236 * Function gatts_process_value_conf
1237 *
Myles Watson9ca07092016-11-28 16:41:53 -08001238 * Description This function is called to process the handle value
1239 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001240 *
1241 * Returns void
1242 *
1243 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001244void gatts_process_value_conf(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code) {
1245 uint16_t handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001246
HsingYuan Lo95a96992020-09-08 11:27:40 +08001247 if (!gatt_tcb_find_indicate_handle(tcb, cid, &handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001248 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001249 return;
1250 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001251
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +02001252 gatt_stop_conf_timer(tcb, cid);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001253
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001254 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1255
1256 if (continue_processing) {
Myles Watson8d749042017-09-19 10:01:28 -07001257 tGATTS_DATA gatts_data;
1258 gatts_data.handle = handle;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001259 for (auto& el : *gatt_cb.srv_list_info) {
1260 if (el.s_hdl <= handle && el.e_hdl >= handle) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001261 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, handle);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001262 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1263 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
Myles Watson8d749042017-09-19 10:01:28 -07001264 &gatts_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001265 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001266 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001267 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001268}
1269
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001270static bool gatts_process_db_out_of_sync(tGATT_TCB& tcb, uint16_t cid,
1271 uint8_t op_code, uint16_t len,
1272 uint8_t* p_data) {
1273 if (gatt_sr_is_cl_change_aware(tcb)) return false;
1274
1275 // default value
1276 bool should_ignore = true;
1277 bool should_rsp = true;
1278
1279 switch (op_code) {
1280 case GATT_REQ_READ_BY_TYPE: {
1281 // Check if read database hash by UUID
1282 Uuid uuid = Uuid::kEmpty;
1283 uint16_t s_hdl = 0, e_hdl = 0;
1284 uint16_t db_hash_handle = gatt_cb.handle_of_database_hash;
1285 tGATT_STATUS reason = gatts_validate_packet_format(op_code, len, p_data,
1286 &uuid, s_hdl, e_hdl);
1287 if (reason == GATT_SUCCESS &&
1288 (s_hdl <= db_hash_handle && db_hash_handle <= e_hdl) &&
1289 (uuid == Uuid::From16Bit(GATT_UUID_DATABASE_HASH)))
1290 should_ignore = false;
1291
1292 } break;
1293 case GATT_REQ_READ: {
1294 // Check if read database hash by handle
1295 uint16_t handle = 0;
1296 uint8_t* p = p_data;
1297 tGATT_STATUS status = GATT_SUCCESS;
1298
1299 if (len < 2) {
1300 status = GATT_INVALID_PDU;
1301 } else {
1302 STREAM_TO_UINT16(handle, p);
1303 len -= 2;
1304 }
1305
1306 if (status == GATT_SUCCESS && handle == gatt_cb.handle_of_database_hash)
1307 should_ignore = false;
1308
1309 } break;
1310 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1311 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1312 case GATT_REQ_FIND_INFO: /* discover char descrptor */
1313 case GATT_REQ_READ_BLOB: /* read long char */
1314 case GATT_REQ_READ_MULTI: /* read multi char*/
1315 case GATT_REQ_WRITE: /* write char/char descriptor value */
1316 case GATT_REQ_PREPARE_WRITE: /* write long char */
1317 // Use default value
1318 break;
1319 case GATT_CMD_WRITE: /* cmd */
1320 case GATT_SIGN_CMD_WRITE: /* sign cmd */
1321 should_rsp = false;
1322 break;
1323 case GATT_REQ_MTU: /* configure mtu */
1324 case GATT_REQ_EXEC_WRITE: /* execute write */
1325 case GATT_HANDLE_VALUE_CONF: /* confirm for indication */
1326 default:
1327 should_ignore = false;
1328 }
1329
1330 if (should_ignore) {
1331 if (should_rsp) {
1332 gatt_send_error_rsp(tcb, cid, GATT_DATABASE_OUT_OF_SYNC, op_code, 0x0000,
1333 false);
1334 }
1335 LOG(INFO) << __func__ << ": database out of sync, device=" << tcb.peer_bda
1336 << ", op_code=" << loghex((uint16_t)op_code)
1337 << ", should_rsp=" << should_rsp;
1338 gatt_sr_update_cl_status(tcb, /* chg_aware= */ should_rsp);
1339 }
1340
1341 return should_ignore;
1342}
1343
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001344/** This function is called to handle the client requests to server */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001345void gatt_server_handle_client_req(tGATT_TCB& tcb, uint16_t cid,
1346 uint8_t op_code, uint16_t len,
1347 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001348 /* there is pending command, discard this one */
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +02001349 if (!gatt_sr_cmd_empty(tcb, cid) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001350
Myles Watson911d1ae2016-11-28 16:44:40 -08001351 /* the size of the message may not be bigger than the local max PDU size*/
1352 /* The message has to be smaller than the agreed MTU, len does not include op
1353 * code */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001354
1355 uint16_t payload_size = gatt_tcb_get_payload_size_rx(tcb, cid);
1356 if (len >= payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001357 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001358 len + 1, payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001359 /* for invalid request expecting response, send it now */
1360 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1361 op_code != GATT_HANDLE_VALUE_CONF) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001362 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001363 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001364 /* otherwise, ignore the pkt */
1365 } else {
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001366 // handle database out of sync
1367 if (gatts_process_db_out_of_sync(tcb, cid, op_code, len, p_data)) return;
1368
Myles Watson911d1ae2016-11-28 16:44:40 -08001369 switch (op_code) {
1370 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1371 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001372 gatts_process_primary_service_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001373 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001374
Myles Watson911d1ae2016-11-28 16:44:40 -08001375 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001376 gatts_process_find_info(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001377 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001378
Myles Watson911d1ae2016-11-28 16:44:40 -08001379 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1380 value */
1381 /* discover characteristic, discover char by UUID */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001382 gatts_process_read_by_type_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001383 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001384
Myles Watson911d1ae2016-11-28 16:44:40 -08001385 case GATT_REQ_READ: /* read char/char descriptor value */
1386 case GATT_REQ_READ_BLOB:
1387 case GATT_REQ_WRITE: /* write char/char descriptor value */
1388 case GATT_CMD_WRITE:
1389 case GATT_SIGN_CMD_WRITE:
1390 case GATT_REQ_PREPARE_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001391 gatts_process_attribute_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001392 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001393
Myles Watson911d1ae2016-11-28 16:44:40 -08001394 case GATT_HANDLE_VALUE_CONF:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001395 gatts_process_value_conf(tcb, cid, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001396 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001397
Myles Watson911d1ae2016-11-28 16:44:40 -08001398 case GATT_REQ_MTU:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001399 gatts_process_mtu_req(tcb, cid, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001400 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001401
Myles Watson911d1ae2016-11-28 16:44:40 -08001402 case GATT_REQ_EXEC_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001403 gatt_process_exec_write_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001404 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001405
Myles Watson911d1ae2016-11-28 16:44:40 -08001406 case GATT_REQ_READ_MULTI:
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +02001407 case GATT_REQ_READ_MULTI_VAR:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001408 gatt_process_read_multi_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001409 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001410
Myles Watson911d1ae2016-11-28 16:44:40 -08001411 default:
1412 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001413 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001414 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001415}