blob: e637eb243d5fe5235909b55e0671b7f715375eb5 [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
The Android Open Source Project5738f832012-12-12 16:00:35 -080027#include <string.h>
Jakub Pawlowskie4f13782018-10-23 14:46:24 +020028
The Android Open Source Project5738f832012-12-12 16:00:35 -080029#include "gatt_int.h"
30#include "l2c_api.h"
Priti Aghera636d6712014-12-18 13:55:48 -080031#include "l2c_int.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"
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}
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200137
138static void build_read_multi_rsp(tGATT_SR_CMD* p_cmd, uint16_t mtu) {
139 uint16_t ii, total_len, len;
140 uint8_t* p;
141 bool is_overflow = false;
142
143 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
144 BT_HDR* p_buf = (BT_HDR*)osi_calloc(len);
145 p_buf->offset = L2CAP_MIN_OFFSET;
146 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
147
148 /* First byte in the response is the opcode */
149 if (p_cmd->multi_req.variable_len)
150 *p++ = GATT_RSP_READ_MULTI_VAR;
151 else
152 *p++ = GATT_RSP_READ_MULTI;
153
154 p_buf->len = 1;
155
156 /* Now walk through the buffers putting the data into the response in order
157 */
158 list_t* list = NULL;
159 const list_node_t* node = NULL;
160 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
161 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
162 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
163 tGATTS_RSP* p_rsp = NULL;
164
165 if (list != NULL) {
166 if (ii == 0)
167 node = list_begin(list);
168 else
169 node = list_next(node);
170 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
171 }
172
173 if (p_rsp != NULL) {
174 total_len = (p_buf->len + p_rsp->attr_value.len);
175
176 if (total_len > mtu) {
177 /* just send the partial response for the overflow case */
178 len = p_rsp->attr_value.len - (total_len - mtu);
179 is_overflow = true;
180 VLOG(1) << StringPrintf(
181 "multi read overflow available len=%d val_len=%d", len,
182 p_rsp->attr_value.len);
183 } else {
184 len = p_rsp->attr_value.len;
185 }
186
187 if (p_cmd->multi_req.variable_len) {
188 UINT16_TO_STREAM(p, len);
189 p_buf->len += 2;
190 }
191
192 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
193 memcpy(p, p_rsp->attr_value.value, len);
194 if (!is_overflow) p += len;
195 p_buf->len += len;
196 } else {
197 p_cmd->status = GATT_NOT_FOUND;
198 break;
199 }
200
201 if (is_overflow) break;
202
203 } else {
204 p_cmd->status = GATT_NOT_FOUND;
205 break;
206 }
207
208 } /* loop through all handles*/
209
210 /* Sanity check on the buffer length */
211 if (p_buf->len == 0) {
212 LOG(ERROR) << __func__ << " nothing found!!";
213 p_cmd->status = GATT_NOT_FOUND;
214 osi_free(p_buf);
215 VLOG(1) << __func__ << "osi_free(p_buf)";
216 } else if (p_cmd->p_rsp_msg != NULL) {
217 osi_free(p_buf);
218 } else {
219 p_cmd->p_rsp_msg = p_buf;
220 }
221}
222
The Android Open Source Project5738f832012-12-12 16:00:35 -0800223/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800224 *
225 * Function process_read_multi_rsp
226 *
227 * Description This function check the read multiple response.
228 *
229 * Returns bool if all replies have been received
230 *
231 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800232static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
233 tGATTS_RSP* p_msg, uint16_t mtu) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700234 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700235
Myles Watson911d1ae2016-11-28 16:44:40 -0800236 if (p_cmd->multi_rsp_q == NULL)
237 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800238
Myles Watson911d1ae2016-11-28 16:44:40 -0800239 /* Enqueue the response */
240 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
241 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
242 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800243
Myles Watson911d1ae2016-11-28 16:44:40 -0800244 p_cmd->status = status;
245 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700246 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200247 << " num_hdls=" << p_cmd->multi_req.num_handles
248 << " variable=" << p_cmd->multi_req.variable_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800249 /* Wait till we get all the responses */
250 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
251 p_cmd->multi_req.num_handles) {
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200252 build_read_multi_rsp(p_cmd, mtu);
Myles Watson911d1ae2016-11-28 16:44:40 -0800253 return (true);
254 }
255 } else /* any handle read exception occurs, return error */
256 {
257 return (true);
258 }
259
260 /* If here, still waiting */
261 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800262}
263
264/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800265 *
266 * Function gatt_sr_process_app_rsp
267 *
Myles Watson9ca07092016-11-28 16:41:53 -0800268 * Description This function checks whether the response message from
269 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800270 *
271 * Returns void
272 *
273 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700274tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800275 UNUSED_ATTR uint32_t trans_id,
276 uint8_t op_code, tGATT_STATUS status,
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200277 tGATTS_RSP* p_msg,
278 tGATT_SR_CMD* sr_res_p) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800279 tGATT_STATUS ret_code = GATT_SUCCESS;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200280 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 -0800281
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700282 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200284 gatt_sr_update_cback_cnt(tcb, sr_res_p->cid, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200286 if ((op_code == GATT_REQ_READ_MULTI) ||
287 (op_code == GATT_REQ_READ_MULTI_VAR)) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800288 /* If no error and still waiting, just return */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200289 if (!process_read_multi_rsp(sr_res_p, status, p_msg, payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800290 return (GATT_SUCCESS);
291 } else {
292 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700293 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800294
295 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200296 gatt_sr_reset_cback_cnt(tcb, sr_res_p->cid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800297
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200298 sr_res_p->status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800299
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700300 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200301 if (sr_res_p->p_rsp_msg == NULL) {
302 sr_res_p->p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
303 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800304 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700305 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800306 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800307 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800308 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700309 if (gatt_sr_is_cback_cnt_zero(tcb)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200310 if ((sr_res_p->status == GATT_SUCCESS) && (sr_res_p->p_rsp_msg)) {
311 ret_code = attp_send_sr_msg(tcb, sr_res_p->cid, sr_res_p->p_rsp_msg);
312 sr_res_p->p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800313 } else {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200314 ret_code = gatt_send_error_rsp(tcb, sr_res_p->cid, status, op_code,
315 sr_res_p->handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800316 }
317
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200318 gatt_dequeue_sr_cmd(tcb, sr_res_p->cid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800319 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800320
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700321 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800322
323 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800324}
325
326/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800327 *
328 * Function gatt_process_exec_write_req
329 *
330 * Description This function is called to process the execute write request
331 * from client.
332 *
333 * Returns void
334 *
335 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200336void gatt_process_exec_write_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
337 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800338 uint8_t *p = p_data, flag, i = 0;
339 uint32_t trans_id = 0;
340 tGATT_IF gatt_if;
341 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800342
Marie Janssend19e0782016-07-15 12:48:27 -0700343#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800344 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700345 VLOG(1)
346 << "Conformance tst: forced err rspv for Execute Write: error status="
347 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800348
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200349 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800350 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800351
Myles Watson911d1ae2016-11-28 16:44:40 -0800352 return;
353 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800354#endif
355
Stanley Tngcc9c7332018-04-05 09:54:13 -0700356 if (len < sizeof(flag)) {
357 android_errorWriteLog(0x534e4554, "73172115");
358 LOG(ERROR) << __func__ << "invalid length";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200359 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, GATT_REQ_EXEC_WRITE, 0,
360 false);
Stanley Tngcc9c7332018-04-05 09:54:13 -0700361 return;
362 }
363
Myles Watson911d1ae2016-11-28 16:44:40 -0800364 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800365
Myles Watson911d1ae2016-11-28 16:44:40 -0800366 /* mask the flag */
367 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800368
Myles Watson911d1ae2016-11-28 16:44:40 -0800369 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700370 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200371 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, 0);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700372 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800373
Myles Watson911d1ae2016-11-28 16:44:40 -0800374 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700375 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800376 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700377 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700378 tGATTS_DATA gatts_data;
379 gatts_data.exec_write = flag;
Myles Watson911d1ae2016-11-28 16:44:40 -0800380 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
Myles Watson8d749042017-09-19 10:01:28 -0700381 &gatts_data);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700382 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800383 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800384 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800385 } else /* nothing needs to be executed , send response now */
386 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700387 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200388 gatt_send_error_rsp(tcb, cid, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800389 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800390}
391
392/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800393 *
394 * Function gatt_process_read_multi_req
395 *
396 * Description This function is called to process the read multiple request
397 * from client.
398 *
399 * Returns void
400 *
401 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200402void gatt_process_read_multi_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
403 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800404 uint32_t trans_id;
405 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700406 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800407 tGATT_STATUS err = GATT_SUCCESS;
408 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800409
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700410 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800411
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200412 tGATT_READ_MULTI* multi_req = gatt_sr_get_read_multi(tcb, cid);
413 multi_req->num_handles = 0;
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200414 multi_req->variable_len = (op_code == GATT_REQ_READ_MULTI_VAR);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700415 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416
Marie Janssend19e0782016-07-15 12:48:27 -0700417#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800418 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700419 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
420 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800421
Myles Watson911d1ae2016-11-28 16:44:40 -0800422 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800423
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200424 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
425 handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800426
Myles Watson911d1ae2016-11-28 16:44:40 -0800427 return;
428 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800429#endif
430
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200431 while (ll >= 2 && multi_req->num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800432 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800433
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700434 auto it = gatt_sr_find_i_rcb_by_handle(handle);
435 if (it != gatt_cb.srv_list_info->end()) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200436 multi_req->handles[multi_req->num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800437
438 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700439 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
440 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800441 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700442 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800443 break;
444 }
445 } else {
446 /* invalid handle */
447 err = GATT_INVALID_HANDLE;
448 break;
449 }
450 ll -= 2;
451 }
452
453 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700454 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800455 }
456
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200457 if (multi_req->num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800458
459 if (err == GATT_SUCCESS) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200460 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, multi_req->handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800461 if (trans_id != 0) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200462 tGATT_SR_CMD* sr_cmd_p = gatt_sr_get_cmd_by_cid(tcb, cid);
463
464 gatt_sr_reset_cback_cnt(tcb,
465 cid); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800466
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200467 for (ll = 0; ll < multi_req->num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800468 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200469 handle = multi_req->handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700470 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800471
Myles Watson911d1ae2016-11-28 16:44:40 -0800472 p_msg->attr_value.handle = handle;
473 err = gatts_read_attr_value_by_handle(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200474 tcb, cid, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700475 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
476 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800477
478 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700479 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200480 GATT_SUCCESS, p_msg, sr_cmd_p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800481 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800482 /* either not using or done using the buffer, release it now */
483 osi_free(p_msg);
484 }
485 } else
486 err = GATT_NO_RESOURCES;
487 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488
Myles Watson911d1ae2016-11-28 16:44:40 -0800489 /* in theroy BUSY is not possible(should already been checked), protected
490 * check */
491 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200492 gatt_send_error_rsp(tcb, cid, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800493}
494
495/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800496 *
497 * Function gatt_build_primary_service_rsp
498 *
499 * Description Primamry service request processed internally. Theretically
500 * only deal with ReadByTypeVAlue and ReadByGroupType.
501 *
502 * Returns void
503 *
504 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800505static tGATT_STATUS gatt_build_primary_service_rsp(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200506 BT_HDR* p_msg, tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
507 uint16_t s_hdl, uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data,
508 const Uuid& value) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800509 tGATT_STATUS status = GATT_NOT_FOUND;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700510 uint8_t handle_len = 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800511
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700512 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800513
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200514 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
515
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700516 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700517 if (el.s_hdl < s_hdl || el.s_hdl > e_hdl ||
518 el.type != GATT_UUID_PRI_SERVICE) {
519 continue;
520 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700521
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700522 Uuid* p_uuid = gatts_get_service_uuid(el.p_db);
523 if (!p_uuid) continue;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800524
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700525 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Jakub Pawlowskif107e4f2018-04-12 05:42:31 -0700526 handle_len = 4 + gatt_build_uuid_to_stream_len(*p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800527
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700528 /* get the length byte in the repsonse */
529 if (p_msg->offset == 0) {
530 *p++ = op_code + 1;
531 p_msg->len++;
532 p_msg->offset = handle_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800533
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700534 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
535 *p++ = (uint8_t)p_msg->offset; /* length byte */
536 p_msg->len++;
Myles Watson911d1ae2016-11-28 16:44:40 -0800537 }
538 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700539
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200540 if (p_msg->len + p_msg->offset > payload_size ||
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700541 handle_len != p_msg->offset) {
542 break;
543 }
544
545 if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue;
546
547 UINT16_TO_STREAM(p, el.s_hdl);
548
Jakub Pawlowski4c6007c2018-04-16 07:55:06 -0700549 if (gatt_cb.last_service_handle &&
550 gatt_cb.last_service_handle == el.s_hdl) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700551 VLOG(1) << "Use 0xFFFF for the last primary attribute";
552 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
553 UINT16_TO_STREAM(p, 0xFFFF);
554 } else {
555 UINT16_TO_STREAM(p, el.e_hdl);
556 }
557
558 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
559 gatt_build_uuid_to_stream(&p, *p_uuid);
560
561 status = GATT_SUCCESS;
562 p_msg->len += p_msg->offset;
Myles Watson911d1ae2016-11-28 16:44:40 -0800563 }
564 p_msg->offset = L2CAP_MIN_OFFSET;
565
566 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567}
568
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700569/**
570 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800571 *
572 * Returns true: if data filled sucessfully.
573 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700574 */
575static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700576 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700577 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800578 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800579
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700580 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800581
Myles Watson911d1ae2016-11-28 16:44:40 -0800582 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800583
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700584 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800585
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700586 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700587 if (attr.handle > e_hdl) break;
588
589 if (attr.handle < s_hdl) continue;
590
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700591 uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize();
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700592 if (p_msg->offset == 0)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700593 p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16
594 : GATT_INFO_TYPE_PAIR_128;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700595
596 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
597
598 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700599 uuid_len == Uuid::kNumBytes16) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700600 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700601 UINT16_TO_STREAM(p, attr.uuid.As16Bit());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700602 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700603 uuid_len == Uuid::kNumBytes128) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700604 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700605 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700606 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700607 uuid_len == Uuid::kNumBytes32) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700608 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700609 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700610 } else {
611 LOG(ERROR) << "format mismatch";
612 return GATT_NO_RESOURCES;
613 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800614 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700615 p_msg->len += info_pair_len[p_msg->offset - 1];
616 len -= info_pair_len[p_msg->offset - 1];
617 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800618 }
619
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700620 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800621}
622
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700623static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
624 uint16_t& e_hdl) {
625 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800626
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700627 /* obtain starting handle, and ending handle */
628 STREAM_TO_UINT16(s_hdl, p);
629 STREAM_TO_UINT16(e_hdl, p);
630 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700632 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
633 !GATT_HANDLE_IS_VALID(e_hdl)) {
Stanley Tngbe701122018-05-07 14:58:36 -0700634 return GATT_INVALID_HANDLE;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700635 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800636
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700637 return GATT_SUCCESS;
638}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800639
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700640static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700641 uint8_t*& p, Uuid* p_uuid,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700642 uint16_t& s_hdl,
643 uint16_t& e_hdl) {
644 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
645 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800646
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700647 if (len < 2) return GATT_INVALID_PDU;
648
649 /* parse uuid now */
650 CHECK(p_uuid);
651 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
652 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
653 VLOG(1) << "Bad UUID";
654 return GATT_INVALID_PDU;
655 }
656
657 len -= uuid_len;
658 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800659}
660
661/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800662 *
663 * Function gatts_process_primary_service_req
664 *
Myles Watson9ca07092016-11-28 16:41:53 -0800665 * Description Process ReadByGroupType/ReadByTypeValue request, for
666 * discovering all primary services or discover primary service
667 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800668 *
669 * Returns void
670 *
671 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200672void gatts_process_primary_service_req(tGATT_TCB& tcb, uint16_t cid,
673 uint8_t op_code, uint16_t len,
674 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800675 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700676 Uuid uuid = Uuid::kEmpty;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800677
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700678 uint8_t reason =
679 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
680 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200681 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700682 return;
683 }
684
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700685 if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700686 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200687 gatt_send_error_rsp(tcb, cid, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl,
688 false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700689 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s",
690 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700691 return;
692 }
693
694 // we do not support ReadByTypeValue with any non-primamry_service type
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200695 gatt_send_error_rsp(tcb, cid, GATT_NOT_FOUND, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700696 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s",
697 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700698 return;
699 }
700
701 // TODO: we assume theh value is UUID, there is no such requirement in spec
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700702 Uuid value = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700703 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
Myles Watson5d5fcf22017-10-06 16:51:21 -0700704 if (!gatt_parse_uuid_from_cmd(&value, len, &p_data)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200705 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800706 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800707 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800708
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200709 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
710
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700711 uint16_t msg_len =
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200712 (uint16_t)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700713 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200714 reason = gatt_build_primary_service_rsp(p_msg, tcb, cid, op_code, s_hdl,
715 e_hdl, p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800716 if (reason != GATT_SUCCESS) {
717 osi_free(p_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200718 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700719 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700720 }
721
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200722 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800723}
724
725/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800726 *
727 * Function gatts_process_find_info
728 *
729 * Description process find information request, for discover character
730 * descriptors.
731 *
732 * Returns void
733 *
734 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200735static void gatts_process_find_info(tGATT_TCB& tcb, uint16_t cid,
736 uint8_t op_code, uint16_t len,
737 uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700738 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700739 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700740 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200741 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700742 return;
743 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800744
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200745 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700746 uint16_t buf_len =
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200747 (uint16_t)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800748
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700749 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
750 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800751
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700752 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
753 *p++ = op_code + 1;
754 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800755
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200756 buf_len = payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800757
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700758 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
759 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700760 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700761 if (reason == GATT_NO_RESOURCES) {
762 reason = GATT_SUCCESS;
763 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800764 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800765 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800766 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800767
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700768 *p = (uint8_t)p_msg->offset;
769
770 p_msg->offset = L2CAP_MIN_OFFSET;
771
Myles Watson911d1ae2016-11-28 16:44:40 -0800772 if (reason != GATT_SUCCESS) {
773 osi_free(p_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200774 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800775 } else
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200776 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800777}
778
779/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800780 *
781 * Function gatts_process_mtu_req
782 *
783 * Description This function is called to process excahnge MTU request.
784 * Only used on LE.
785 *
786 * Returns void
787 *
788 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200789static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t cid, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800790 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800791 /* BR/EDR conenction, send error response */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200792 if (cid != L2CAP_ATT_CID) {
793 gatt_send_error_rsp(tcb, cid, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0,
794 false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700795 return;
796 }
797
798 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700799 LOG(ERROR) << "invalid MTU request PDU received.";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200800 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700801 return;
802 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800803
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700804 uint16_t mtu = 0;
805 uint8_t* p = p_data;
806 STREAM_TO_UINT16(mtu, p);
807 /* mtu must be greater than default MTU which is 23/48 */
808 if (mtu < GATT_DEF_BLE_MTU_SIZE)
809 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
810 else if (mtu > GATT_MAX_MTU_SIZE)
811 tcb.payload_size = GATT_MAX_MTU_SIZE;
812 else
813 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700814
Jakub Pawlowskibb956ab2018-05-24 12:27:10 -0700815 LOG(INFO) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800816
Hansong Zhang3cb55142021-01-12 16:17:27 -0800817 BTM_SetBleDataLength(tcb.peer_bda, tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800818
Myles Watson8d749042017-09-19 10:01:28 -0700819 tGATT_SR_MSG gatt_sr_msg;
820 gatt_sr_msg.mtu = tcb.payload_size;
821 BT_HDR* p_buf = attp_build_sr_msg(tcb, GATT_RSP_MTU, &gatt_sr_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200822 attp_send_sr_msg(tcb, cid, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800823
Myles Watson8d749042017-09-19 10:01:28 -0700824 tGATTS_DATA gatts_data;
825 gatts_data.mtu = tcb.payload_size;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700826 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
827 /* of 0, as no response is allowed from applcations */
828 for (int i = 0; i < GATT_MAX_APPS; i++) {
829 if (gatt_cb.cl_rcb[i].in_use) {
830 uint16_t conn_id =
831 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700832 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, &gatts_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800833 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800834 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800835}
836
837/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800838 *
839 * Function gatts_process_read_by_type_req
840 *
841 * Description process Read By type request.
842 * This PDU can be used to perform:
843 * - read characteristic value
844 * - read characteristic descriptor value
845 * - discover characteristic
846 * - discover characteristic by UUID
847 * - relationship discovery
848 *
849 * Returns void
850 *
851 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200852void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint16_t cid,
853 uint8_t op_code, uint16_t len,
854 uint8_t* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700855 Uuid uuid = Uuid::kEmpty;
Hansong Zhanga1603122018-04-12 11:45:03 -0700856 uint16_t s_hdl = 0, e_hdl = 0, err_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700857 tGATT_STATUS reason =
858 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800859
Marie Janssend19e0782016-07-15 12:48:27 -0700860#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800861 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700862 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
863 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800864
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200865 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
866 s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800867
Myles Watson911d1ae2016-11-28 16:44:40 -0800868 return;
869 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800870#endif
871
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700872 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200873 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700874 return;
875 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800876
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200877 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
878
879 size_t msg_len = sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700880 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
881 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800882
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700883 *p++ = op_code + 1;
884 /* reserve length byte */
885 p_msg->len = 2;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200886 uint16_t buf_len = payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800887
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700888 reason = GATT_NOT_FOUND;
889 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
890 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
891 uint8_t sec_flag, key_size;
892 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800893
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700894 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200895 tcb, cid, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len,
896 sec_flag, key_size, 0, &err_hdl);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700897 if (ret != GATT_NOT_FOUND) {
898 reason = ret;
899 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
900 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800901
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700902 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
903 s_hdl = err_hdl;
904 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800905 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800906 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800907 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700908 *p = (uint8_t)p_msg->offset;
909 p_msg->offset = L2CAP_MIN_OFFSET;
910
Myles Watson911d1ae2016-11-28 16:44:40 -0800911 if (reason != GATT_SUCCESS) {
912 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800913
Myles Watson911d1ae2016-11-28 16:44:40 -0800914 /* in theroy BUSY is not possible(should already been checked), protected
915 * check */
916 if (reason != GATT_PENDING && reason != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200917 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700918
919 return;
920 }
921
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200922 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923}
924
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700925/**
926 * This function is called to process the write request from client.
927 */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200928static void gatts_process_write_req(tGATT_TCB& tcb, uint16_t cid,
929 tGATT_SRV_LIST_ELEM& el, uint16_t handle,
930 uint8_t op_code, uint16_t len,
931 uint8_t* p_data,
Chris Mantonefa23112020-03-04 11:26:31 -0800932 bt_gatt_db_attribute_type_t gatt_type) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800933 tGATTS_DATA sr_data;
934 uint32_t trans_id;
935 tGATT_STATUS status;
936 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800937 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938
Myles Watson911d1ae2016-11-28 16:44:40 -0800939 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800940
Myles Watson911d1ae2016-11-28 16:44:40 -0800941 switch (op_code) {
942 case GATT_REQ_PREPARE_WRITE:
Chris Mantonefa23112020-03-04 11:26:31 -0800943 if (len < 2 || p == nullptr) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700944 LOG(ERROR) << __func__
945 << ": Prepare write request was invalid - missing offset, "
946 "sending error response";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200947 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800948 return;
949 }
950 sr_data.write_req.is_prep = true;
951 STREAM_TO_UINT16(sr_data.write_req.offset, p);
952 len -= 2;
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700953 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800954 case GATT_SIGN_CMD_WRITE:
955 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700956 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800957 len -= GATT_AUTH_SIGN_LEN;
958 }
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700959 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800960 case GATT_CMD_WRITE:
961 case GATT_REQ_WRITE:
962 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
963 sr_data.write_req.need_rsp = true;
964 sr_data.write_req.handle = handle;
Chris Mantonefa23112020-03-04 11:26:31 -0800965 if (len > GATT_MAX_ATTR_LEN) len = GATT_MAX_ATTR_LEN;
Myles Watson911d1ae2016-11-28 16:44:40 -0800966 sr_data.write_req.len = len;
Chris Mantonefa23112020-03-04 11:26:31 -0800967 if (len != 0 && p != nullptr) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800968 memcpy(sr_data.write_req.value, p, len);
969 }
970 break;
971 }
972
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700973 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800974
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700975 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
976 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800977 sec_flag, key_size);
978
979 if (status == GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200980 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800981 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700982 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800983
984 uint8_t opcode = 0;
985 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
986 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
987 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
988 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
989 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700990 LOG(ERROR) << __func__
991 << "%s: Attempt to write attribute that's not tied with"
992 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800993 status = GATT_ERROR;
994 }
995
996 if (opcode) {
997 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
998 status = GATT_PENDING;
999 }
1000 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001001 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -08001002 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001003 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001004 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001005
Myles Watson911d1ae2016-11-28 16:44:40 -08001006 /* in theroy BUSY is not possible(should already been checked), protected
1007 * check */
1008 if (status != GATT_PENDING && status != GATT_BUSY &&
1009 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001010 gatt_send_error_rsp(tcb, cid, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -08001011 }
1012 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001013}
1014
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001015/**
1016 * This function is called to process the read request from client.
1017 */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001018static void gatts_process_read_req(tGATT_TCB& tcb, uint16_t cid,
1019 tGATT_SRV_LIST_ELEM& el, uint8_t op_code,
1020 uint16_t handle, uint16_t len,
1021 uint8_t* p_data) {
1022 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
1023
1024 size_t buf_len = sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001025 uint16_t offset = 0;
Stanley Tngcc9c7332018-04-05 09:54:13 -07001026
1027 if (op_code == GATT_REQ_READ_BLOB && len < sizeof(uint16_t)) {
1028 /* Error: packet length is too short */
1029 LOG(ERROR) << __func__ << ": packet length=" << len
1030 << " too short. min=" << sizeof(uint16_t);
1031 android_errorWriteWithInfoLog(0x534e4554, "73172115", -1, NULL, 0);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001032 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, 0, false);
Stanley Tngcc9c7332018-04-05 09:54:13 -07001033 return;
1034 }
1035
Myles Watson911d1ae2016-11-28 16:44:40 -08001036 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001037
Myles Watson911d1ae2016-11-28 16:44:40 -08001038 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001039
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001040 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -08001041 *p++ = op_code + 1;
1042 p_msg->len = 1;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001043 buf_len = payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001044
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001045 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001046 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001047
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001048 uint16_t value_len = 0;
1049 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001050 tcb, cid, el.p_db, op_code, handle, offset, p, &value_len,
1051 (uint16_t)buf_len, sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -08001052 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001053
Myles Watson911d1ae2016-11-28 16:44:40 -08001054 if (reason != GATT_SUCCESS) {
1055 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001056
Stanley Tngcc9c7332018-04-05 09:54:13 -07001057 /* in theory BUSY is not possible(should already been checked), protected
Myles Watson911d1ae2016-11-28 16:44:40 -08001058 * check */
1059 if (reason != GATT_PENDING && reason != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001060 gatt_send_error_rsp(tcb, cid, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001061
1062 return;
1063 }
1064
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001065 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001066}
1067
1068/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001069 *
1070 * Function gatts_process_attribute_req
1071 *
Myles Watson9ca07092016-11-28 16:41:53 -08001072 * Description This function is called to process the per attribute handle
1073 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001074 *
1075 * Returns void
1076 *
1077 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001078void gatts_process_attribute_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
1079 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001080 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001081 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -08001082 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001083
Myles Watson911d1ae2016-11-28 16:44:40 -08001084 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001085 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -08001086 status = GATT_INVALID_PDU;
1087 } else {
1088 STREAM_TO_UINT16(handle, p);
1089 len -= 2;
1090 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001091
Marie Janssend19e0782016-07-15 12:48:27 -07001092#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001093 gatt_cb.handle = handle;
1094 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001095 VLOG(1) << "Conformance tst: forced err rsp: error status="
1096 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001097
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001098 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, cid, gatt_cb.req_op_code,
1099 handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100
Myles Watson911d1ae2016-11-28 16:44:40 -08001101 return;
1102 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001103#endif
1104
Myles Watson911d1ae2016-11-28 16:44:40 -08001105 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001106 for (auto& el : *gatt_cb.srv_list_info) {
1107 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1108 for (const auto& attr : el.p_db->attr_list) {
1109 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001110 switch (op_code) {
1111 case GATT_REQ_READ: /* read char/char descriptor value */
1112 case GATT_REQ_READ_BLOB:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001113 gatts_process_read_req(tcb, cid, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001114 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001115
Myles Watson911d1ae2016-11-28 16:44:40 -08001116 case GATT_REQ_WRITE: /* write char/char descriptor value */
1117 case GATT_CMD_WRITE:
1118 case GATT_SIGN_CMD_WRITE:
1119 case GATT_REQ_PREPARE_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001120 gatts_process_write_req(tcb, cid, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001121 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001122 break;
1123 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001124 break;
1125 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001126 status = GATT_SUCCESS;
1127 break;
1128 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001129 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001130 break;
1131 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001133 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001134
Myles Watson911d1ae2016-11-28 16:44:40 -08001135 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1136 op_code != GATT_SIGN_CMD_WRITE)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001137 gatt_send_error_rsp(tcb, cid, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001138}
1139
1140/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001141 *
1142 * Function gatts_proc_srv_chg_ind_ack
1143 *
1144 * Description This function process the service changed indicaiton ACK
1145 *
1146 * Returns void
1147 *
1148 ******************************************************************************/
Jakub Pawlowski890c5012019-04-24 23:00:16 +02001149void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001150 tGATTS_SRV_CHG_REQ req;
1151 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001152
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001153 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001154
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001155 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001156 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001157 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001158 p_buf->srv_changed = false;
1159 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1160 if (gatt_cb.cb_info.p_srv_chg_callback)
1161 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1162 &req, NULL);
1163 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001164}
1165
1166/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001167 *
1168 * Function gatts_chk_pending_ind
1169 *
Myles Watson9ca07092016-11-28 16:41:53 -08001170 * Description This function check any pending indication needs to be sent
1171 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001172 *
1173 * Returns void
1174 *
1175 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001176static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001177 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001178
Myles Watson911d1ae2016-11-28 16:44:40 -08001179 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001180 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001181 if (p_buf != NULL) {
1182 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1183 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001184 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001185 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001186}
1187
1188/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001189 *
1190 * Function gatts_proc_ind_ack
1191 *
Myles Watson9ca07092016-11-28 16:41:53 -08001192 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001193 *
Myles Watson9ca07092016-11-28 16:41:53 -08001194 * Returns true continue to process the indication ack by the
1195 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001196 *
1197 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001198static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001199 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001200
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001201 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001202
Myles Watson911d1ae2016-11-28 16:44:40 -08001203 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001204 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001205 /* there is no need to inform the application since srv chg is handled
1206 * internally by GATT */
1207 continue_processing = false;
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001208
1209 // After receiving ack of svc_chg_ind, reset client status
1210 gatt_sr_update_cl_status(tcb, /* chg_aware= */ true);
Myles Watson911d1ae2016-11-28 16:44:40 -08001211 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001212
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001213 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001214 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001215}
1216
1217/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001218 *
1219 * Function gatts_process_value_conf
1220 *
Myles Watson9ca07092016-11-28 16:41:53 -08001221 * Description This function is called to process the handle value
1222 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001223 *
1224 * Returns void
1225 *
1226 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001227void gatts_process_value_conf(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code) {
1228 uint16_t handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001229
HsingYuan Lo95a96992020-09-08 11:27:40 +08001230 if (!gatt_tcb_find_indicate_handle(tcb, cid, &handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001231 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001232 return;
1233 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001234
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +02001235 gatt_stop_conf_timer(tcb, cid);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001236
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001237 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1238
1239 if (continue_processing) {
Myles Watson8d749042017-09-19 10:01:28 -07001240 tGATTS_DATA gatts_data;
1241 gatts_data.handle = handle;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001242 for (auto& el : *gatt_cb.srv_list_info) {
1243 if (el.s_hdl <= handle && el.e_hdl >= handle) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001244 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, handle);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001245 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1246 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
Myles Watson8d749042017-09-19 10:01:28 -07001247 &gatts_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001248 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001249 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001250 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001251}
1252
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001253static bool gatts_process_db_out_of_sync(tGATT_TCB& tcb, uint16_t cid,
1254 uint8_t op_code, uint16_t len,
1255 uint8_t* p_data) {
1256 if (gatt_sr_is_cl_change_aware(tcb)) return false;
1257
1258 // default value
1259 bool should_ignore = true;
1260 bool should_rsp = true;
1261
1262 switch (op_code) {
1263 case GATT_REQ_READ_BY_TYPE: {
1264 // Check if read database hash by UUID
1265 Uuid uuid = Uuid::kEmpty;
1266 uint16_t s_hdl = 0, e_hdl = 0;
1267 uint16_t db_hash_handle = gatt_cb.handle_of_database_hash;
1268 tGATT_STATUS reason = gatts_validate_packet_format(op_code, len, p_data,
1269 &uuid, s_hdl, e_hdl);
1270 if (reason == GATT_SUCCESS &&
1271 (s_hdl <= db_hash_handle && db_hash_handle <= e_hdl) &&
1272 (uuid == Uuid::From16Bit(GATT_UUID_DATABASE_HASH)))
1273 should_ignore = false;
1274
1275 } break;
1276 case GATT_REQ_READ: {
1277 // Check if read database hash by handle
1278 uint16_t handle = 0;
1279 uint8_t* p = p_data;
1280 tGATT_STATUS status = GATT_SUCCESS;
1281
1282 if (len < 2) {
1283 status = GATT_INVALID_PDU;
1284 } else {
1285 STREAM_TO_UINT16(handle, p);
1286 len -= 2;
1287 }
1288
1289 if (status == GATT_SUCCESS && handle == gatt_cb.handle_of_database_hash)
1290 should_ignore = false;
1291
1292 } break;
1293 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1294 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1295 case GATT_REQ_FIND_INFO: /* discover char descrptor */
1296 case GATT_REQ_READ_BLOB: /* read long char */
1297 case GATT_REQ_READ_MULTI: /* read multi char*/
1298 case GATT_REQ_WRITE: /* write char/char descriptor value */
1299 case GATT_REQ_PREPARE_WRITE: /* write long char */
1300 // Use default value
1301 break;
1302 case GATT_CMD_WRITE: /* cmd */
1303 case GATT_SIGN_CMD_WRITE: /* sign cmd */
1304 should_rsp = false;
1305 break;
1306 case GATT_REQ_MTU: /* configure mtu */
1307 case GATT_REQ_EXEC_WRITE: /* execute write */
1308 case GATT_HANDLE_VALUE_CONF: /* confirm for indication */
1309 default:
1310 should_ignore = false;
1311 }
1312
1313 if (should_ignore) {
1314 if (should_rsp) {
1315 gatt_send_error_rsp(tcb, cid, GATT_DATABASE_OUT_OF_SYNC, op_code, 0x0000,
1316 false);
1317 }
1318 LOG(INFO) << __func__ << ": database out of sync, device=" << tcb.peer_bda
1319 << ", op_code=" << loghex((uint16_t)op_code)
1320 << ", should_rsp=" << should_rsp;
1321 gatt_sr_update_cl_status(tcb, /* chg_aware= */ should_rsp);
1322 }
1323
1324 return should_ignore;
1325}
1326
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001327/** This function is called to handle the client requests to server */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001328void gatt_server_handle_client_req(tGATT_TCB& tcb, uint16_t cid,
1329 uint8_t op_code, uint16_t len,
1330 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001331 /* there is pending command, discard this one */
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +02001332 if (!gatt_sr_cmd_empty(tcb, cid) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001333
Myles Watson911d1ae2016-11-28 16:44:40 -08001334 /* the size of the message may not be bigger than the local max PDU size*/
1335 /* The message has to be smaller than the agreed MTU, len does not include op
1336 * code */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001337
1338 uint16_t payload_size = gatt_tcb_get_payload_size_rx(tcb, cid);
1339 if (len >= payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001340 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001341 len + 1, payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001342 /* for invalid request expecting response, send it now */
1343 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1344 op_code != GATT_HANDLE_VALUE_CONF) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001345 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001346 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001347 /* otherwise, ignore the pkt */
1348 } else {
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001349 // handle database out of sync
1350 if (gatts_process_db_out_of_sync(tcb, cid, op_code, len, p_data)) return;
1351
Myles Watson911d1ae2016-11-28 16:44:40 -08001352 switch (op_code) {
1353 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1354 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001355 gatts_process_primary_service_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001356 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001357
Myles Watson911d1ae2016-11-28 16:44:40 -08001358 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001359 gatts_process_find_info(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_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1363 value */
1364 /* discover characteristic, discover char by UUID */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001365 gatts_process_read_by_type_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001366 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367
Myles Watson911d1ae2016-11-28 16:44:40 -08001368 case GATT_REQ_READ: /* read char/char descriptor value */
1369 case GATT_REQ_READ_BLOB:
1370 case GATT_REQ_WRITE: /* write char/char descriptor value */
1371 case GATT_CMD_WRITE:
1372 case GATT_SIGN_CMD_WRITE:
1373 case GATT_REQ_PREPARE_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001374 gatts_process_attribute_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001375 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001376
Myles Watson911d1ae2016-11-28 16:44:40 -08001377 case GATT_HANDLE_VALUE_CONF:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001378 gatts_process_value_conf(tcb, cid, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001379 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001380
Myles Watson911d1ae2016-11-28 16:44:40 -08001381 case GATT_REQ_MTU:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001382 gatts_process_mtu_req(tcb, cid, 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_EXEC_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001386 gatt_process_exec_write_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001387 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001388
Myles Watson911d1ae2016-11-28 16:44:40 -08001389 case GATT_REQ_READ_MULTI:
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +02001390 case GATT_REQ_READ_MULTI_VAR:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001391 gatt_process_read_multi_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 default:
1395 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001396 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001397 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001398}