blob: 681189ae7c4117c4fe3c162372d522db5f7bc7e7 [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 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080024#include "bt_target.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070025#include "osi/include/osi.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080026
Jakub Pawlowskie4f13782018-10-23 14:46:24 +020027#include <log/log.h>
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"
Priti Aghera636d6712014-12-18 13:55:48 -080032#include "l2c_int.h"
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020033#include "stack/eatt/eatt.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080034#define GATT_MTU_REQ_MIN_LEN 2
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070036using base::StringPrintf;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070037using bluetooth::Uuid;
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020038using bluetooth::eatt::EattExtension;
39using bluetooth::eatt::EattChannel;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070040
The Android Open Source Project5738f832012-12-12 16:00:35 -080041/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080042 *
43 * Function gatt_sr_enqueue_cmd
44 *
45 * Description This function enqueue the request from client which needs a
46 * application response, and update the transaction ID.
47 *
48 * Returns void
49 *
50 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020051uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
52 uint16_t handle) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020053 tGATT_SR_CMD* p_cmd;
54
55 if (cid == tcb.att_lcid) {
56 p_cmd = &tcb.sr_cmd;
57 } else {
58 EattChannel* channel =
59 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
60 p_cmd = &channel->server_outstanding_cmd_;
61 }
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020062
Myles Watson911d1ae2016-11-28 16:44:40 -080063 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080064
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020065 p_cmd->cid = cid;
66
Myles Watson911d1ae2016-11-28 16:44:40 -080067 if ((p_cmd->op_code == 0) ||
68 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
69 {
70 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
71 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070072 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080073 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070074 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080075 p_cmd->op_code = op_code;
76 p_cmd->handle = handle;
77 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070078 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080079 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080080 }
Myles Watson911d1ae2016-11-28 16:44:40 -080081 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080082
Myles Watson911d1ae2016-11-28 16:44:40 -080083 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080084}
85
86/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080087 *
88 * Function gatt_sr_cmd_empty
89 *
Myles Watson9ca07092016-11-28 16:41:53 -080090 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080091 *
92 * Returns true if empty, false if there is pending command.
93 *
94 ******************************************************************************/
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020095bool gatt_sr_cmd_empty(tGATT_TCB& tcb, uint16_t cid) {
96 if (cid == tcb.att_lcid) return (tcb.sr_cmd.op_code == 0);
97
98 EattChannel* channel =
99 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
100
101 return (channel->server_outstanding_cmd_.op_code == 0);
102}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800103
104/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800105 *
106 * Function gatt_dequeue_sr_cmd
107 *
108 * Description This function dequeue the request from command queue.
109 *
110 * Returns void
111 *
112 ******************************************************************************/
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200113void gatt_dequeue_sr_cmd(tGATT_TCB& tcb, uint16_t cid) {
114 tGATT_SR_CMD* p_cmd;
115
116 if (cid == tcb.att_lcid) {
117 p_cmd = &tcb.sr_cmd;
118 } else {
119 EattChannel* channel =
120 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
121
122 p_cmd = &channel->server_outstanding_cmd_;
123 }
124
Myles Watson911d1ae2016-11-28 16:44:40 -0800125 /* Double check in case any buffers are queued */
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200126 VLOG(1) << "gatt_dequeue_sr_cmd cid: " << loghex(cid);
127 if (p_cmd->p_rsp_msg)
128 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = "
129 << p_cmd->p_rsp_msg;
130 osi_free_and_reset((void**)&p_cmd->p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800131
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200132 while (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
133 osi_free(fixed_queue_try_dequeue(p_cmd->multi_rsp_q));
134 fixed_queue_free(p_cmd->multi_rsp_q, NULL);
135 memset(p_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800137/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800138 *
139 * Function process_read_multi_rsp
140 *
141 * Description This function check the read multiple response.
142 *
143 * Returns bool if all replies have been received
144 *
145 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800146static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
147 tGATTS_RSP* p_msg, uint16_t mtu) {
148 uint16_t ii, total_len, len;
149 uint8_t* p;
150 bool is_overflow = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800151
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700152 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700153
Myles Watson911d1ae2016-11-28 16:44:40 -0800154 if (p_cmd->multi_rsp_q == NULL)
155 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156
Myles Watson911d1ae2016-11-28 16:44:40 -0800157 /* Enqueue the response */
158 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
159 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
160 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800161
Myles Watson911d1ae2016-11-28 16:44:40 -0800162 p_cmd->status = status;
163 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700164 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
165 << " num_hdls=" << p_cmd->multi_req.num_handles;
Myles Watson911d1ae2016-11-28 16:44:40 -0800166 /* Wait till we get all the responses */
167 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
168 p_cmd->multi_req.num_handles) {
169 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
170 p_buf = (BT_HDR*)osi_calloc(len);
171 p_buf->offset = L2CAP_MIN_OFFSET;
172 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800173
Myles Watson911d1ae2016-11-28 16:44:40 -0800174 /* First byte in the response is the opcode */
175 *p++ = GATT_RSP_READ_MULTI;
176 p_buf->len = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800177
Myles Watson911d1ae2016-11-28 16:44:40 -0800178 /* Now walk through the buffers puting the data into the response in order
179 */
180 list_t* list = NULL;
181 const list_node_t* node = NULL;
182 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
183 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
184 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
185 tGATTS_RSP* p_rsp = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700186
Myles Watson911d1ae2016-11-28 16:44:40 -0800187 if (list != NULL) {
188 if (ii == 0)
189 node = list_begin(list);
190 else
191 node = list_next(node);
192 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800194
Myles Watson911d1ae2016-11-28 16:44:40 -0800195 if (p_rsp != NULL) {
196 total_len = (p_buf->len + p_rsp->attr_value.len);
197
198 if (total_len > mtu) {
199 /* just send the partial response for the overflow case */
200 len = p_rsp->attr_value.len - (total_len - mtu);
201 is_overflow = true;
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700202 VLOG(1) << StringPrintf(
203 "multi read overflow available len=%d val_len=%d", len,
204 p_rsp->attr_value.len);
Myles Watson911d1ae2016-11-28 16:44:40 -0800205 } else {
206 len = p_rsp->attr_value.len;
207 }
208
209 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
210 memcpy(p, p_rsp->attr_value.value, len);
211 if (!is_overflow) p += len;
212 p_buf->len += len;
213 } 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) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700229 LOG(ERROR) << __func__ << " nothing found!!";
Myles Watson911d1ae2016-11-28 16:44:40 -0800230 p_cmd->status = GATT_NOT_FOUND;
231 osi_free(p_buf);
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700232 VLOG(1) << __func__ << "osi_free(p_buf)";
Myles Watson911d1ae2016-11-28 16:44:40 -0800233 } 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 return (true);
240 }
241 } else /* any handle read exception occurs, return error */
242 {
243 return (true);
244 }
245
246 /* If here, still waiting */
247 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800248}
249
250/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800251 *
252 * Function gatt_sr_process_app_rsp
253 *
Myles Watson9ca07092016-11-28 16:41:53 -0800254 * Description This function checks whether the response message from
255 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800256 *
257 * Returns void
258 *
259 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700260tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800261 UNUSED_ATTR uint32_t trans_id,
262 uint8_t op_code, tGATT_STATUS status,
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200263 tGATTS_RSP* p_msg,
264 tGATT_SR_CMD* sr_res_p) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800265 tGATT_STATUS ret_code = GATT_SUCCESS;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200266 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 -0800267
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700268 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800269
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200270 gatt_sr_update_cback_cnt(tcb, sr_res_p->cid, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800271
Myles Watson911d1ae2016-11-28 16:44:40 -0800272 if (op_code == GATT_REQ_READ_MULTI) {
273 /* If no error and still waiting, just return */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200274 if (!process_read_multi_rsp(sr_res_p, status, p_msg, payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800275 return (GATT_SUCCESS);
276 } else {
277 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700278 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800279
280 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200281 gatt_sr_reset_cback_cnt(tcb, sr_res_p->cid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800282
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200283 sr_res_p->status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800284
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700285 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200286 if (sr_res_p->p_rsp_msg == NULL) {
287 sr_res_p->p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
288 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800289 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700290 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800291 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800292 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800293 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700294 if (gatt_sr_is_cback_cnt_zero(tcb)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200295 if ((sr_res_p->status == GATT_SUCCESS) && (sr_res_p->p_rsp_msg)) {
296 ret_code = attp_send_sr_msg(tcb, sr_res_p->cid, sr_res_p->p_rsp_msg);
297 sr_res_p->p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800298 } else {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200299 ret_code = gatt_send_error_rsp(tcb, sr_res_p->cid, status, op_code,
300 sr_res_p->handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800301 }
302
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200303 gatt_dequeue_sr_cmd(tcb, sr_res_p->cid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800304 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800305
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700306 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800307
308 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800309}
310
311/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800312 *
313 * Function gatt_process_exec_write_req
314 *
315 * Description This function is called to process the execute write request
316 * from client.
317 *
318 * Returns void
319 *
320 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200321void gatt_process_exec_write_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
322 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800323 uint8_t *p = p_data, flag, i = 0;
324 uint32_t trans_id = 0;
325 tGATT_IF gatt_if;
326 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800327
Marie Janssend19e0782016-07-15 12:48:27 -0700328#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800329 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700330 VLOG(1)
331 << "Conformance tst: forced err rspv for Execute Write: error status="
332 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800333
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200334 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800335 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800336
Myles Watson911d1ae2016-11-28 16:44:40 -0800337 return;
338 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800339#endif
340
Stanley Tngcc9c7332018-04-05 09:54:13 -0700341 if (len < sizeof(flag)) {
342 android_errorWriteLog(0x534e4554, "73172115");
343 LOG(ERROR) << __func__ << "invalid length";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200344 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, GATT_REQ_EXEC_WRITE, 0,
345 false);
Stanley Tngcc9c7332018-04-05 09:54:13 -0700346 return;
347 }
348
Myles Watson911d1ae2016-11-28 16:44:40 -0800349 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800350
Myles Watson911d1ae2016-11-28 16:44:40 -0800351 /* mask the flag */
352 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800353
Myles Watson911d1ae2016-11-28 16:44:40 -0800354 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700355 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200356 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, 0);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700357 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800358
Myles Watson911d1ae2016-11-28 16:44:40 -0800359 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700360 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800361 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700362 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700363 tGATTS_DATA gatts_data;
364 gatts_data.exec_write = flag;
Myles Watson911d1ae2016-11-28 16:44:40 -0800365 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
Myles Watson8d749042017-09-19 10:01:28 -0700366 &gatts_data);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700367 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800368 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800370 } else /* nothing needs to be executed , send response now */
371 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700372 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200373 gatt_send_error_rsp(tcb, cid, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800374 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800375}
376
377/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800378 *
379 * Function gatt_process_read_multi_req
380 *
381 * Description This function is called to process the read multiple request
382 * from client.
383 *
384 * Returns void
385 *
386 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200387void gatt_process_read_multi_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
388 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800389 uint32_t trans_id;
390 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700391 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800392 tGATT_STATUS err = GATT_SUCCESS;
393 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800394
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700395 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800396
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200397 tGATT_READ_MULTI* multi_req = gatt_sr_get_read_multi(tcb, cid);
398 multi_req->num_handles = 0;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700399 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800400
Marie Janssend19e0782016-07-15 12:48:27 -0700401#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800402 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700403 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
404 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800405
Myles Watson911d1ae2016-11-28 16:44:40 -0800406 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800407
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200408 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
409 handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410
Myles Watson911d1ae2016-11-28 16:44:40 -0800411 return;
412 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800413#endif
414
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200415 while (ll >= 2 && multi_req->num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800416 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800417
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700418 auto it = gatt_sr_find_i_rcb_by_handle(handle);
419 if (it != gatt_cb.srv_list_info->end()) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200420 multi_req->handles[multi_req->num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800421
422 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700423 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
424 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800425 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700426 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800427 break;
428 }
429 } else {
430 /* invalid handle */
431 err = GATT_INVALID_HANDLE;
432 break;
433 }
434 ll -= 2;
435 }
436
437 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700438 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800439 }
440
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200441 if (multi_req->num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800442
443 if (err == GATT_SUCCESS) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200444 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, multi_req->handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800445 if (trans_id != 0) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200446 tGATT_SR_CMD* sr_cmd_p = gatt_sr_get_cmd_by_cid(tcb, cid);
447
448 gatt_sr_reset_cback_cnt(tcb,
449 cid); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800450
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200451 for (ll = 0; ll < multi_req->num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800452 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200453 handle = multi_req->handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700454 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800455
Myles Watson911d1ae2016-11-28 16:44:40 -0800456 p_msg->attr_value.handle = handle;
457 err = gatts_read_attr_value_by_handle(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200458 tcb, cid, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700459 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
460 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800461
462 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700463 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200464 GATT_SUCCESS, p_msg, sr_cmd_p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800465 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800466 /* either not using or done using the buffer, release it now */
467 osi_free(p_msg);
468 }
469 } else
470 err = GATT_NO_RESOURCES;
471 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800472
Myles Watson911d1ae2016-11-28 16:44:40 -0800473 /* in theroy BUSY is not possible(should already been checked), protected
474 * check */
475 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200476 gatt_send_error_rsp(tcb, cid, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800477}
478
479/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800480 *
481 * Function gatt_build_primary_service_rsp
482 *
483 * Description Primamry service request processed internally. Theretically
484 * only deal with ReadByTypeVAlue and ReadByGroupType.
485 *
486 * Returns void
487 *
488 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800489static tGATT_STATUS gatt_build_primary_service_rsp(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200490 BT_HDR* p_msg, tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
491 uint16_t s_hdl, uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data,
492 const Uuid& value) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800493 tGATT_STATUS status = GATT_NOT_FOUND;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700494 uint8_t handle_len = 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800495
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700496 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200498 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
499
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700500 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700501 if (el.s_hdl < s_hdl || el.s_hdl > e_hdl ||
502 el.type != GATT_UUID_PRI_SERVICE) {
503 continue;
504 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700505
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700506 Uuid* p_uuid = gatts_get_service_uuid(el.p_db);
507 if (!p_uuid) continue;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800508
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700509 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Jakub Pawlowskif107e4f2018-04-12 05:42:31 -0700510 handle_len = 4 + gatt_build_uuid_to_stream_len(*p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800511
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700512 /* get the length byte in the repsonse */
513 if (p_msg->offset == 0) {
514 *p++ = op_code + 1;
515 p_msg->len++;
516 p_msg->offset = handle_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800517
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700518 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
519 *p++ = (uint8_t)p_msg->offset; /* length byte */
520 p_msg->len++;
Myles Watson911d1ae2016-11-28 16:44:40 -0800521 }
522 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700523
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200524 if (p_msg->len + p_msg->offset > payload_size ||
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700525 handle_len != p_msg->offset) {
526 break;
527 }
528
529 if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue;
530
531 UINT16_TO_STREAM(p, el.s_hdl);
532
Jakub Pawlowski4c6007c2018-04-16 07:55:06 -0700533 if (gatt_cb.last_service_handle &&
534 gatt_cb.last_service_handle == el.s_hdl) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700535 VLOG(1) << "Use 0xFFFF for the last primary attribute";
536 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
537 UINT16_TO_STREAM(p, 0xFFFF);
538 } else {
539 UINT16_TO_STREAM(p, el.e_hdl);
540 }
541
542 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
543 gatt_build_uuid_to_stream(&p, *p_uuid);
544
545 status = GATT_SUCCESS;
546 p_msg->len += p_msg->offset;
Myles Watson911d1ae2016-11-28 16:44:40 -0800547 }
548 p_msg->offset = L2CAP_MIN_OFFSET;
549
550 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800551}
552
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700553/**
554 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800555 *
556 * Returns true: if data filled sucessfully.
557 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700558 */
559static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700560 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700561 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800562 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700564 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565
Myles Watson911d1ae2016-11-28 16:44:40 -0800566 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700568 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700570 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700571 if (attr.handle > e_hdl) break;
572
573 if (attr.handle < s_hdl) continue;
574
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700575 uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize();
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700576 if (p_msg->offset == 0)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700577 p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16
578 : GATT_INFO_TYPE_PAIR_128;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700579
580 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
581
582 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700583 uuid_len == Uuid::kNumBytes16) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700584 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700585 UINT16_TO_STREAM(p, attr.uuid.As16Bit());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700586 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700587 uuid_len == Uuid::kNumBytes128) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700588 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700589 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700590 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700591 uuid_len == Uuid::kNumBytes32) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700592 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700593 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700594 } else {
595 LOG(ERROR) << "format mismatch";
596 return GATT_NO_RESOURCES;
597 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800598 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700599 p_msg->len += info_pair_len[p_msg->offset - 1];
600 len -= info_pair_len[p_msg->offset - 1];
601 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800602 }
603
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700604 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800605}
606
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700607static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
608 uint16_t& e_hdl) {
609 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800610
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700611 /* obtain starting handle, and ending handle */
612 STREAM_TO_UINT16(s_hdl, p);
613 STREAM_TO_UINT16(e_hdl, p);
614 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800615
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700616 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
617 !GATT_HANDLE_IS_VALID(e_hdl)) {
Stanley Tngbe701122018-05-07 14:58:36 -0700618 return GATT_INVALID_HANDLE;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700619 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800620
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700621 return GATT_SUCCESS;
622}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800623
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700624static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700625 uint8_t*& p, Uuid* p_uuid,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700626 uint16_t& s_hdl,
627 uint16_t& e_hdl) {
628 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
629 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800630
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700631 if (len < 2) return GATT_INVALID_PDU;
632
633 /* parse uuid now */
634 CHECK(p_uuid);
635 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
636 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
637 VLOG(1) << "Bad UUID";
638 return GATT_INVALID_PDU;
639 }
640
641 len -= uuid_len;
642 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800643}
644
645/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800646 *
647 * Function gatts_process_primary_service_req
648 *
Myles Watson9ca07092016-11-28 16:41:53 -0800649 * Description Process ReadByGroupType/ReadByTypeValue request, for
650 * discovering all primary services or discover primary service
651 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800652 *
653 * Returns void
654 *
655 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200656void gatts_process_primary_service_req(tGATT_TCB& tcb, uint16_t cid,
657 uint8_t op_code, uint16_t len,
658 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800659 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700660 Uuid uuid = Uuid::kEmpty;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800661
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700662 uint8_t reason =
663 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
664 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200665 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700666 return;
667 }
668
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700669 if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700670 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200671 gatt_send_error_rsp(tcb, cid, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl,
672 false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700673 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s",
674 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700675 return;
676 }
677
678 // we do not support ReadByTypeValue with any non-primamry_service type
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200679 gatt_send_error_rsp(tcb, cid, GATT_NOT_FOUND, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700680 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s",
681 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700682 return;
683 }
684
685 // TODO: we assume theh value is UUID, there is no such requirement in spec
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700686 Uuid value = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700687 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
Myles Watson5d5fcf22017-10-06 16:51:21 -0700688 if (!gatt_parse_uuid_from_cmd(&value, len, &p_data)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200689 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800690 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800691 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800692
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200693 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
694
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700695 uint16_t msg_len =
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200696 (uint16_t)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700697 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200698 reason = gatt_build_primary_service_rsp(p_msg, tcb, cid, op_code, s_hdl,
699 e_hdl, p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800700 if (reason != GATT_SUCCESS) {
701 osi_free(p_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200702 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700703 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700704 }
705
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200706 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800707}
708
709/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800710 *
711 * Function gatts_process_find_info
712 *
713 * Description process find information request, for discover character
714 * descriptors.
715 *
716 * Returns void
717 *
718 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200719static void gatts_process_find_info(tGATT_TCB& tcb, uint16_t cid,
720 uint8_t op_code, uint16_t len,
721 uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700722 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700723 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700724 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200725 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700726 return;
727 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800728
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200729 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700730 uint16_t buf_len =
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200731 (uint16_t)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800732
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700733 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
734 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800735
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700736 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
737 *p++ = op_code + 1;
738 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800739
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200740 buf_len = payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800741
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700742 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
743 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700744 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700745 if (reason == GATT_NO_RESOURCES) {
746 reason = GATT_SUCCESS;
747 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800748 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800749 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800750 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800751
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700752 *p = (uint8_t)p_msg->offset;
753
754 p_msg->offset = L2CAP_MIN_OFFSET;
755
Myles Watson911d1ae2016-11-28 16:44:40 -0800756 if (reason != GATT_SUCCESS) {
757 osi_free(p_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200758 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800759 } else
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200760 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800761}
762
763/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800764 *
765 * Function gatts_process_mtu_req
766 *
767 * Description This function is called to process excahnge MTU request.
768 * Only used on LE.
769 *
770 * Returns void
771 *
772 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200773static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t cid, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800774 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800775 /* BR/EDR conenction, send error response */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200776 if (cid != L2CAP_ATT_CID) {
777 gatt_send_error_rsp(tcb, cid, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0,
778 false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700779 return;
780 }
781
782 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700783 LOG(ERROR) << "invalid MTU request PDU received.";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200784 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700785 return;
786 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800787
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700788 uint16_t mtu = 0;
789 uint8_t* p = p_data;
790 STREAM_TO_UINT16(mtu, p);
791 /* mtu must be greater than default MTU which is 23/48 */
792 if (mtu < GATT_DEF_BLE_MTU_SIZE)
793 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
794 else if (mtu > GATT_MAX_MTU_SIZE)
795 tcb.payload_size = GATT_MAX_MTU_SIZE;
796 else
797 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700798
Jakub Pawlowskibb956ab2018-05-24 12:27:10 -0700799 LOG(INFO) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800800
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700801 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
802 tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800803
Myles Watson8d749042017-09-19 10:01:28 -0700804 tGATT_SR_MSG gatt_sr_msg;
805 gatt_sr_msg.mtu = tcb.payload_size;
806 BT_HDR* p_buf = attp_build_sr_msg(tcb, GATT_RSP_MTU, &gatt_sr_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200807 attp_send_sr_msg(tcb, cid, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808
Myles Watson8d749042017-09-19 10:01:28 -0700809 tGATTS_DATA gatts_data;
810 gatts_data.mtu = tcb.payload_size;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700811 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
812 /* of 0, as no response is allowed from applcations */
813 for (int i = 0; i < GATT_MAX_APPS; i++) {
814 if (gatt_cb.cl_rcb[i].in_use) {
815 uint16_t conn_id =
816 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700817 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, &gatts_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800818 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800819 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800820}
821
822/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800823 *
824 * Function gatts_process_read_by_type_req
825 *
826 * Description process Read By type request.
827 * This PDU can be used to perform:
828 * - read characteristic value
829 * - read characteristic descriptor value
830 * - discover characteristic
831 * - discover characteristic by UUID
832 * - relationship discovery
833 *
834 * Returns void
835 *
836 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200837void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint16_t cid,
838 uint8_t op_code, uint16_t len,
839 uint8_t* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700840 Uuid uuid = Uuid::kEmpty;
Hansong Zhanga1603122018-04-12 11:45:03 -0700841 uint16_t s_hdl = 0, e_hdl = 0, err_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700842 tGATT_STATUS reason =
843 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800844
Marie Janssend19e0782016-07-15 12:48:27 -0700845#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800846 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700847 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
848 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800849
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200850 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
851 s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800852
Myles Watson911d1ae2016-11-28 16:44:40 -0800853 return;
854 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800855#endif
856
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700857 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200858 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700859 return;
860 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800861
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200862 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
863
864 size_t msg_len = sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700865 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
866 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800867
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700868 *p++ = op_code + 1;
869 /* reserve length byte */
870 p_msg->len = 2;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200871 uint16_t buf_len = payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800872
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700873 reason = GATT_NOT_FOUND;
874 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
875 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
876 uint8_t sec_flag, key_size;
877 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800878
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700879 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200880 tcb, cid, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len,
881 sec_flag, key_size, 0, &err_hdl);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700882 if (ret != GATT_NOT_FOUND) {
883 reason = ret;
884 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
885 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700887 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
888 s_hdl = err_hdl;
889 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800890 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800891 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800892 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700893 *p = (uint8_t)p_msg->offset;
894 p_msg->offset = L2CAP_MIN_OFFSET;
895
Myles Watson911d1ae2016-11-28 16:44:40 -0800896 if (reason != GATT_SUCCESS) {
897 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800898
Myles Watson911d1ae2016-11-28 16:44:40 -0800899 /* in theroy BUSY is not possible(should already been checked), protected
900 * check */
901 if (reason != GATT_PENDING && reason != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200902 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700903
904 return;
905 }
906
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200907 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800908}
909
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700910/**
911 * This function is called to process the write request from client.
912 */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200913static void gatts_process_write_req(tGATT_TCB& tcb, uint16_t cid,
914 tGATT_SRV_LIST_ELEM& el, uint16_t handle,
915 uint8_t op_code, uint16_t len,
916 uint8_t* p_data,
Chris Mantonefa23112020-03-04 11:26:31 -0800917 bt_gatt_db_attribute_type_t gatt_type) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800918 tGATTS_DATA sr_data;
919 uint32_t trans_id;
920 tGATT_STATUS status;
921 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800922 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923
Myles Watson911d1ae2016-11-28 16:44:40 -0800924 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800925
Myles Watson911d1ae2016-11-28 16:44:40 -0800926 switch (op_code) {
927 case GATT_REQ_PREPARE_WRITE:
Chris Mantonefa23112020-03-04 11:26:31 -0800928 if (len < 2 || p == nullptr) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700929 LOG(ERROR) << __func__
930 << ": Prepare write request was invalid - missing offset, "
931 "sending error response";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200932 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800933 return;
934 }
935 sr_data.write_req.is_prep = true;
936 STREAM_TO_UINT16(sr_data.write_req.offset, p);
937 len -= 2;
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700938 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800939 case GATT_SIGN_CMD_WRITE:
940 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700941 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800942 len -= GATT_AUTH_SIGN_LEN;
943 }
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700944 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800945 case GATT_CMD_WRITE:
946 case GATT_REQ_WRITE:
947 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
948 sr_data.write_req.need_rsp = true;
949 sr_data.write_req.handle = handle;
Chris Mantonefa23112020-03-04 11:26:31 -0800950 if (len > GATT_MAX_ATTR_LEN) len = GATT_MAX_ATTR_LEN;
Myles Watson911d1ae2016-11-28 16:44:40 -0800951 sr_data.write_req.len = len;
Chris Mantonefa23112020-03-04 11:26:31 -0800952 if (len != 0 && p != nullptr) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800953 memcpy(sr_data.write_req.value, p, len);
954 }
955 break;
956 }
957
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700958 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800959
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700960 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
961 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800962 sec_flag, key_size);
963
964 if (status == GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200965 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800966 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700967 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800968
969 uint8_t opcode = 0;
970 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
971 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
972 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
973 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
974 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700975 LOG(ERROR) << __func__
976 << "%s: Attempt to write attribute that's not tied with"
977 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800978 status = GATT_ERROR;
979 }
980
981 if (opcode) {
982 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
983 status = GATT_PENDING;
984 }
985 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700986 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -0800987 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800988 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800989 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800990
Myles Watson911d1ae2016-11-28 16:44:40 -0800991 /* in theroy BUSY is not possible(should already been checked), protected
992 * check */
993 if (status != GATT_PENDING && status != GATT_BUSY &&
994 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200995 gatt_send_error_rsp(tcb, cid, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800996 }
997 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800998}
999
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001000/**
1001 * This function is called to process the read request from client.
1002 */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001003static void gatts_process_read_req(tGATT_TCB& tcb, uint16_t cid,
1004 tGATT_SRV_LIST_ELEM& el, uint8_t op_code,
1005 uint16_t handle, uint16_t len,
1006 uint8_t* p_data) {
1007 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
1008
1009 size_t buf_len = sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001010 uint16_t offset = 0;
Stanley Tngcc9c7332018-04-05 09:54:13 -07001011
1012 if (op_code == GATT_REQ_READ_BLOB && len < sizeof(uint16_t)) {
1013 /* Error: packet length is too short */
1014 LOG(ERROR) << __func__ << ": packet length=" << len
1015 << " too short. min=" << sizeof(uint16_t);
1016 android_errorWriteWithInfoLog(0x534e4554, "73172115", -1, NULL, 0);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001017 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, 0, false);
Stanley Tngcc9c7332018-04-05 09:54:13 -07001018 return;
1019 }
1020
Myles Watson911d1ae2016-11-28 16:44:40 -08001021 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001022
Myles Watson911d1ae2016-11-28 16:44:40 -08001023 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001024
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001025 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -08001026 *p++ = op_code + 1;
1027 p_msg->len = 1;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001028 buf_len = payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001029
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001030 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001031 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001032
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001033 uint16_t value_len = 0;
1034 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001035 tcb, cid, el.p_db, op_code, handle, offset, p, &value_len,
1036 (uint16_t)buf_len, sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -08001037 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001038
Myles Watson911d1ae2016-11-28 16:44:40 -08001039 if (reason != GATT_SUCCESS) {
1040 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001041
Stanley Tngcc9c7332018-04-05 09:54:13 -07001042 /* in theory BUSY is not possible(should already been checked), protected
Myles Watson911d1ae2016-11-28 16:44:40 -08001043 * check */
1044 if (reason != GATT_PENDING && reason != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001045 gatt_send_error_rsp(tcb, cid, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001046
1047 return;
1048 }
1049
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001050 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001051}
1052
1053/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001054 *
1055 * Function gatts_process_attribute_req
1056 *
Myles Watson9ca07092016-11-28 16:41:53 -08001057 * Description This function is called to process the per attribute handle
1058 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001059 *
1060 * Returns void
1061 *
1062 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001063void gatts_process_attribute_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
1064 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001065 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001066 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -08001067 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068
Myles Watson911d1ae2016-11-28 16:44:40 -08001069 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001070 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -08001071 status = GATT_INVALID_PDU;
1072 } else {
1073 STREAM_TO_UINT16(handle, p);
1074 len -= 2;
1075 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001076
Marie Janssend19e0782016-07-15 12:48:27 -07001077#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001078 gatt_cb.handle = handle;
1079 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001080 VLOG(1) << "Conformance tst: forced err rsp: error status="
1081 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001082
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001083 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, cid, gatt_cb.req_op_code,
1084 handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001085
Myles Watson911d1ae2016-11-28 16:44:40 -08001086 return;
1087 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001088#endif
1089
Myles Watson911d1ae2016-11-28 16:44:40 -08001090 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001091 for (auto& el : *gatt_cb.srv_list_info) {
1092 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1093 for (const auto& attr : el.p_db->attr_list) {
1094 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001095 switch (op_code) {
1096 case GATT_REQ_READ: /* read char/char descriptor value */
1097 case GATT_REQ_READ_BLOB:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001098 gatts_process_read_req(tcb, cid, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001099 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100
Myles Watson911d1ae2016-11-28 16:44:40 -08001101 case GATT_REQ_WRITE: /* write char/char descriptor value */
1102 case GATT_CMD_WRITE:
1103 case GATT_SIGN_CMD_WRITE:
1104 case GATT_REQ_PREPARE_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001105 gatts_process_write_req(tcb, cid, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001106 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001107 break;
1108 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001109 break;
1110 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001111 status = GATT_SUCCESS;
1112 break;
1113 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001115 break;
1116 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001118 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001119
Myles Watson911d1ae2016-11-28 16:44:40 -08001120 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1121 op_code != GATT_SIGN_CMD_WRITE)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001122 gatt_send_error_rsp(tcb, cid, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001123}
1124
1125/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001126 *
1127 * Function gatts_proc_srv_chg_ind_ack
1128 *
1129 * Description This function process the service changed indicaiton ACK
1130 *
1131 * Returns void
1132 *
1133 ******************************************************************************/
Jakub Pawlowski890c5012019-04-24 23:00:16 +02001134void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001135 tGATTS_SRV_CHG_REQ req;
1136 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001137
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001138 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001139
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001140 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001141 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001142 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001143 p_buf->srv_changed = false;
1144 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1145 if (gatt_cb.cb_info.p_srv_chg_callback)
1146 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1147 &req, NULL);
1148 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001149}
1150
1151/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001152 *
1153 * Function gatts_chk_pending_ind
1154 *
Myles Watson9ca07092016-11-28 16:41:53 -08001155 * Description This function check any pending indication needs to be sent
1156 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001157 *
1158 * Returns void
1159 *
1160 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001161static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001162 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001163
Myles Watson911d1ae2016-11-28 16:44:40 -08001164 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001165 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001166 if (p_buf != NULL) {
1167 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1168 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001169 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001170 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001171}
1172
1173/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001174 *
1175 * Function gatts_proc_ind_ack
1176 *
Myles Watson9ca07092016-11-28 16:41:53 -08001177 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001178 *
Myles Watson9ca07092016-11-28 16:41:53 -08001179 * Returns true continue to process the indication ack by the
1180 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001181 *
1182 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001183static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001184 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001185
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001186 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001187
Myles Watson911d1ae2016-11-28 16:44:40 -08001188 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001189 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001190 /* there is no need to inform the application since srv chg is handled
1191 * internally by GATT */
1192 continue_processing = false;
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001193
1194 // After receiving ack of svc_chg_ind, reset client status
1195 gatt_sr_update_cl_status(tcb, /* chg_aware= */ true);
Myles Watson911d1ae2016-11-28 16:44:40 -08001196 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001198 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001199 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001200}
1201
1202/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001203 *
1204 * Function gatts_process_value_conf
1205 *
Myles Watson9ca07092016-11-28 16:41:53 -08001206 * Description This function is called to process the handle value
1207 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001208 *
1209 * Returns void
1210 *
1211 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001212void gatts_process_value_conf(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code) {
1213 uint16_t handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001214
HsingYuan Lo95a96992020-09-08 11:27:40 +08001215 if (!gatt_tcb_find_indicate_handle(tcb, cid, &handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001216 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001217 return;
1218 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001219
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +02001220 gatt_stop_conf_timer(tcb, cid);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001221
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001222 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1223
1224 if (continue_processing) {
Myles Watson8d749042017-09-19 10:01:28 -07001225 tGATTS_DATA gatts_data;
1226 gatts_data.handle = handle;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001227 for (auto& el : *gatt_cb.srv_list_info) {
1228 if (el.s_hdl <= handle && el.e_hdl >= handle) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001229 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, handle);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001230 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1231 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
Myles Watson8d749042017-09-19 10:01:28 -07001232 &gatts_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001233 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001234 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001235 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001236}
1237
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001238static bool gatts_process_db_out_of_sync(tGATT_TCB& tcb, uint16_t cid,
1239 uint8_t op_code, uint16_t len,
1240 uint8_t* p_data) {
1241 if (gatt_sr_is_cl_change_aware(tcb)) return false;
1242
1243 // default value
1244 bool should_ignore = true;
1245 bool should_rsp = true;
1246
1247 switch (op_code) {
1248 case GATT_REQ_READ_BY_TYPE: {
1249 // Check if read database hash by UUID
1250 Uuid uuid = Uuid::kEmpty;
1251 uint16_t s_hdl = 0, e_hdl = 0;
1252 uint16_t db_hash_handle = gatt_cb.handle_of_database_hash;
1253 tGATT_STATUS reason = gatts_validate_packet_format(op_code, len, p_data,
1254 &uuid, s_hdl, e_hdl);
1255 if (reason == GATT_SUCCESS &&
1256 (s_hdl <= db_hash_handle && db_hash_handle <= e_hdl) &&
1257 (uuid == Uuid::From16Bit(GATT_UUID_DATABASE_HASH)))
1258 should_ignore = false;
1259
1260 } break;
1261 case GATT_REQ_READ: {
1262 // Check if read database hash by handle
1263 uint16_t handle = 0;
1264 uint8_t* p = p_data;
1265 tGATT_STATUS status = GATT_SUCCESS;
1266
1267 if (len < 2) {
1268 status = GATT_INVALID_PDU;
1269 } else {
1270 STREAM_TO_UINT16(handle, p);
1271 len -= 2;
1272 }
1273
1274 if (status == GATT_SUCCESS && handle == gatt_cb.handle_of_database_hash)
1275 should_ignore = false;
1276
1277 } break;
1278 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1279 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1280 case GATT_REQ_FIND_INFO: /* discover char descrptor */
1281 case GATT_REQ_READ_BLOB: /* read long char */
1282 case GATT_REQ_READ_MULTI: /* read multi char*/
1283 case GATT_REQ_WRITE: /* write char/char descriptor value */
1284 case GATT_REQ_PREPARE_WRITE: /* write long char */
1285 // Use default value
1286 break;
1287 case GATT_CMD_WRITE: /* cmd */
1288 case GATT_SIGN_CMD_WRITE: /* sign cmd */
1289 should_rsp = false;
1290 break;
1291 case GATT_REQ_MTU: /* configure mtu */
1292 case GATT_REQ_EXEC_WRITE: /* execute write */
1293 case GATT_HANDLE_VALUE_CONF: /* confirm for indication */
1294 default:
1295 should_ignore = false;
1296 }
1297
1298 if (should_ignore) {
1299 if (should_rsp) {
1300 gatt_send_error_rsp(tcb, cid, GATT_DATABASE_OUT_OF_SYNC, op_code, 0x0000,
1301 false);
1302 }
1303 LOG(INFO) << __func__ << ": database out of sync, device=" << tcb.peer_bda
1304 << ", op_code=" << loghex((uint16_t)op_code)
1305 << ", should_rsp=" << should_rsp;
1306 gatt_sr_update_cl_status(tcb, /* chg_aware= */ should_rsp);
1307 }
1308
1309 return should_ignore;
1310}
1311
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001312/** This function is called to handle the client requests to server */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001313void gatt_server_handle_client_req(tGATT_TCB& tcb, uint16_t cid,
1314 uint8_t op_code, uint16_t len,
1315 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001316 /* there is pending command, discard this one */
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +02001317 if (!gatt_sr_cmd_empty(tcb, cid) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001318
Myles Watson911d1ae2016-11-28 16:44:40 -08001319 /* the size of the message may not be bigger than the local max PDU size*/
1320 /* The message has to be smaller than the agreed MTU, len does not include op
1321 * code */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001322
1323 uint16_t payload_size = gatt_tcb_get_payload_size_rx(tcb, cid);
1324 if (len >= payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001325 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001326 len + 1, payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001327 /* for invalid request expecting response, send it now */
1328 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1329 op_code != GATT_HANDLE_VALUE_CONF) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001330 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001331 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001332 /* otherwise, ignore the pkt */
1333 } else {
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001334 // handle database out of sync
1335 if (gatts_process_db_out_of_sync(tcb, cid, op_code, len, p_data)) return;
1336
Myles Watson911d1ae2016-11-28 16:44:40 -08001337 switch (op_code) {
1338 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1339 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001340 gatts_process_primary_service_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001341 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001342
Myles Watson911d1ae2016-11-28 16:44:40 -08001343 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001344 gatts_process_find_info(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001345 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001346
Myles Watson911d1ae2016-11-28 16:44:40 -08001347 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1348 value */
1349 /* discover characteristic, discover char by UUID */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001350 gatts_process_read_by_type_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001351 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001352
Myles Watson911d1ae2016-11-28 16:44:40 -08001353 case GATT_REQ_READ: /* read char/char descriptor value */
1354 case GATT_REQ_READ_BLOB:
1355 case GATT_REQ_WRITE: /* write char/char descriptor value */
1356 case GATT_CMD_WRITE:
1357 case GATT_SIGN_CMD_WRITE:
1358 case GATT_REQ_PREPARE_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001359 gatts_process_attribute_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001360 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001361
Myles Watson911d1ae2016-11-28 16:44:40 -08001362 case GATT_HANDLE_VALUE_CONF:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001363 gatts_process_value_conf(tcb, cid, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001364 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001365
Myles Watson911d1ae2016-11-28 16:44:40 -08001366 case GATT_REQ_MTU:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001367 gatts_process_mtu_req(tcb, cid, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001368 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001369
Myles Watson911d1ae2016-11-28 16:44:40 -08001370 case GATT_REQ_EXEC_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001371 gatt_process_exec_write_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001372 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001373
Myles Watson911d1ae2016-11-28 16:44:40 -08001374 case GATT_REQ_READ_MULTI:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001375 gatt_process_read_multi_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001376 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001377
Myles Watson911d1ae2016-11-28 16:44:40 -08001378 default:
1379 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001380 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001381 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001382}