blob: 9fe642cbaa18e213b2b1d607603b2c76f2f8a483 [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"
Hansong Zhangcd0d0912021-02-23 16:35:31 -080031#include "osi/include/log.h"
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020032#include "stack/eatt/eatt.h"
Chris Mantonc5557232021-05-06 12:10:11 -070033#include "stack/l2cap/l2c_int.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080034#define GATT_MTU_REQ_MIN_LEN 2
Hansong Zhangf451ded2021-09-08 10:15:34 -070035#define L2CAP_PKT_OVERHEAD 4
The Android Open Source Project5738f832012-12-12 16:00:35 -080036
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070037using base::StringPrintf;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070038using bluetooth::Uuid;
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020039using bluetooth::eatt::EattExtension;
40using bluetooth::eatt::EattChannel;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070041
The Android Open Source Project5738f832012-12-12 16:00:35 -080042/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080043 *
44 * Function gatt_sr_enqueue_cmd
45 *
46 * Description This function enqueue the request from client which needs a
47 * application response, and update the transaction ID.
48 *
49 * Returns void
50 *
51 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020052uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
53 uint16_t handle) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020054 tGATT_SR_CMD* p_cmd;
55
56 if (cid == tcb.att_lcid) {
57 p_cmd = &tcb.sr_cmd;
58 } else {
59 EattChannel* channel =
60 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
61 p_cmd = &channel->server_outstanding_cmd_;
62 }
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020063
Myles Watson911d1ae2016-11-28 16:44:40 -080064 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080065
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020066 p_cmd->cid = cid;
67
Myles Watson911d1ae2016-11-28 16:44:40 -080068 if ((p_cmd->op_code == 0) ||
69 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
70 {
71 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
72 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070073 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080074 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070075 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080076 p_cmd->op_code = op_code;
77 p_cmd->handle = handle;
78 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070079 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080080 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080081 }
Myles Watson911d1ae2016-11-28 16:44:40 -080082 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080083
Myles Watson911d1ae2016-11-28 16:44:40 -080084 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080085}
86
87/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080088 *
89 * Function gatt_sr_cmd_empty
90 *
Myles Watson9ca07092016-11-28 16:41:53 -080091 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080092 *
93 * Returns true if empty, false if there is pending command.
94 *
95 ******************************************************************************/
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020096bool gatt_sr_cmd_empty(tGATT_TCB& tcb, uint16_t cid) {
97 if (cid == tcb.att_lcid) return (tcb.sr_cmd.op_code == 0);
98
99 EattChannel* channel =
100 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
101
102 return (channel->server_outstanding_cmd_.op_code == 0);
103}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800104
105/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800106 *
107 * Function gatt_dequeue_sr_cmd
108 *
109 * Description This function dequeue the request from command queue.
110 *
111 * Returns void
112 *
113 ******************************************************************************/
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200114void gatt_dequeue_sr_cmd(tGATT_TCB& tcb, uint16_t cid) {
115 tGATT_SR_CMD* p_cmd;
116
117 if (cid == tcb.att_lcid) {
118 p_cmd = &tcb.sr_cmd;
119 } else {
120 EattChannel* channel =
121 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
122
123 p_cmd = &channel->server_outstanding_cmd_;
124 }
125
Myles Watson911d1ae2016-11-28 16:44:40 -0800126 /* Double check in case any buffers are queued */
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200127 VLOG(1) << "gatt_dequeue_sr_cmd cid: " << loghex(cid);
128 if (p_cmd->p_rsp_msg)
129 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = "
130 << p_cmd->p_rsp_msg;
131 osi_free_and_reset((void**)&p_cmd->p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800132
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200133 while (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
134 osi_free(fixed_queue_try_dequeue(p_cmd->multi_rsp_q));
135 fixed_queue_free(p_cmd->multi_rsp_q, NULL);
136 memset(p_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800137}
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200138
139static void build_read_multi_rsp(tGATT_SR_CMD* p_cmd, uint16_t mtu) {
140 uint16_t ii, total_len, len;
141 uint8_t* p;
142 bool is_overflow = false;
143
144 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
145 BT_HDR* p_buf = (BT_HDR*)osi_calloc(len);
146 p_buf->offset = L2CAP_MIN_OFFSET;
147 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
148
149 /* First byte in the response is the opcode */
150 if (p_cmd->multi_req.variable_len)
151 *p++ = GATT_RSP_READ_MULTI_VAR;
152 else
153 *p++ = GATT_RSP_READ_MULTI;
154
155 p_buf->len = 1;
156
157 /* Now walk through the buffers putting the data into the response in order
158 */
159 list_t* list = NULL;
160 const list_node_t* node = NULL;
161 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
162 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
163 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
164 tGATTS_RSP* p_rsp = NULL;
165
166 if (list != NULL) {
167 if (ii == 0)
168 node = list_begin(list);
169 else
170 node = list_next(node);
171 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
172 }
173
174 if (p_rsp != NULL) {
175 total_len = (p_buf->len + p_rsp->attr_value.len);
176
177 if (total_len > mtu) {
178 /* just send the partial response for the overflow case */
179 len = p_rsp->attr_value.len - (total_len - mtu);
180 is_overflow = true;
181 VLOG(1) << StringPrintf(
182 "multi read overflow available len=%d val_len=%d", len,
183 p_rsp->attr_value.len);
184 } else {
185 len = p_rsp->attr_value.len;
186 }
187
188 if (p_cmd->multi_req.variable_len) {
189 UINT16_TO_STREAM(p, len);
190 p_buf->len += 2;
191 }
192
193 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
194 memcpy(p, p_rsp->attr_value.value, len);
195 if (!is_overflow) p += len;
196 p_buf->len += len;
197 } else {
198 p_cmd->status = GATT_NOT_FOUND;
199 break;
200 }
201
202 if (is_overflow) break;
203
204 } else {
205 p_cmd->status = GATT_NOT_FOUND;
206 break;
207 }
208
209 } /* loop through all handles*/
210
211 /* Sanity check on the buffer length */
212 if (p_buf->len == 0) {
213 LOG(ERROR) << __func__ << " nothing found!!";
214 p_cmd->status = GATT_NOT_FOUND;
215 osi_free(p_buf);
216 VLOG(1) << __func__ << "osi_free(p_buf)";
217 } else if (p_cmd->p_rsp_msg != NULL) {
218 osi_free(p_buf);
219 } else {
220 p_cmd->p_rsp_msg = p_buf;
221 }
222}
223
The Android Open Source Project5738f832012-12-12 16:00:35 -0800224/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800225 *
226 * Function process_read_multi_rsp
227 *
228 * Description This function check the read multiple response.
229 *
230 * Returns bool if all replies have been received
231 *
232 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800233static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
234 tGATTS_RSP* p_msg, uint16_t mtu) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700235 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700236
Myles Watson911d1ae2016-11-28 16:44:40 -0800237 if (p_cmd->multi_rsp_q == NULL)
238 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800239
Myles Watson911d1ae2016-11-28 16:44:40 -0800240 /* Enqueue the response */
241 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
242 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
243 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800244
Myles Watson911d1ae2016-11-28 16:44:40 -0800245 p_cmd->status = status;
246 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700247 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200248 << " num_hdls=" << p_cmd->multi_req.num_handles
249 << " variable=" << p_cmd->multi_req.variable_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800250 /* Wait till we get all the responses */
251 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
252 p_cmd->multi_req.num_handles) {
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200253 build_read_multi_rsp(p_cmd, mtu);
Myles Watson911d1ae2016-11-28 16:44:40 -0800254 return (true);
255 }
256 } else /* any handle read exception occurs, return error */
257 {
258 return (true);
259 }
260
261 /* If here, still waiting */
262 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263}
264
265/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800266 *
267 * Function gatt_sr_process_app_rsp
268 *
Myles Watson9ca07092016-11-28 16:41:53 -0800269 * Description This function checks whether the response message from
270 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800271 *
272 * Returns void
273 *
274 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700275tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800276 UNUSED_ATTR uint32_t trans_id,
277 uint8_t op_code, tGATT_STATUS status,
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200278 tGATTS_RSP* p_msg,
279 tGATT_SR_CMD* sr_res_p) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800280 tGATT_STATUS ret_code = GATT_SUCCESS;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200281 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 -0800282
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700283 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800284
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200285 gatt_sr_update_cback_cnt(tcb, sr_res_p->cid, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800286
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200287 if ((op_code == GATT_REQ_READ_MULTI) ||
288 (op_code == GATT_REQ_READ_MULTI_VAR)) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800289 /* If no error and still waiting, just return */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200290 if (!process_read_multi_rsp(sr_res_p, status, p_msg, payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800291 return (GATT_SUCCESS);
292 } else {
293 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700294 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800295
296 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200297 gatt_sr_reset_cback_cnt(tcb, sr_res_p->cid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800298
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200299 sr_res_p->status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800300
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700301 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200302 if (sr_res_p->p_rsp_msg == NULL) {
303 sr_res_p->p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
304 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800305 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700306 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800307 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800309 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700310 if (gatt_sr_is_cback_cnt_zero(tcb)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200311 if ((sr_res_p->status == GATT_SUCCESS) && (sr_res_p->p_rsp_msg)) {
312 ret_code = attp_send_sr_msg(tcb, sr_res_p->cid, sr_res_p->p_rsp_msg);
313 sr_res_p->p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800314 } else {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200315 ret_code = gatt_send_error_rsp(tcb, sr_res_p->cid, status, op_code,
316 sr_res_p->handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800317 }
318
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200319 gatt_dequeue_sr_cmd(tcb, sr_res_p->cid);
Myles Watson911d1ae2016-11-28 16:44:40 -0800320 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800321
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700322 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800323
324 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800325}
326
327/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800328 *
329 * Function gatt_process_exec_write_req
330 *
331 * Description This function is called to process the execute write request
332 * from client.
333 *
334 * Returns void
335 *
336 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200337void gatt_process_exec_write_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
338 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800339 uint8_t *p = p_data, flag, i = 0;
340 uint32_t trans_id = 0;
341 tGATT_IF gatt_if;
342 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800343
Marie Janssend19e0782016-07-15 12:48:27 -0700344#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800345 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700346 VLOG(1)
347 << "Conformance tst: forced err rspv for Execute Write: error status="
348 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800349
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200350 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800351 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800352
Myles Watson911d1ae2016-11-28 16:44:40 -0800353 return;
354 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800355#endif
356
Stanley Tngcc9c7332018-04-05 09:54:13 -0700357 if (len < sizeof(flag)) {
358 android_errorWriteLog(0x534e4554, "73172115");
359 LOG(ERROR) << __func__ << "invalid length";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200360 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, GATT_REQ_EXEC_WRITE, 0,
361 false);
Stanley Tngcc9c7332018-04-05 09:54:13 -0700362 return;
363 }
364
Myles Watson911d1ae2016-11-28 16:44:40 -0800365 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800366
Myles Watson911d1ae2016-11-28 16:44:40 -0800367 /* mask the flag */
368 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369
Myles Watson911d1ae2016-11-28 16:44:40 -0800370 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700371 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200372 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, 0);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700373 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800374
Myles Watson911d1ae2016-11-28 16:44:40 -0800375 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700376 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800377 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700378 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700379 tGATTS_DATA gatts_data;
380 gatts_data.exec_write = flag;
Myles Watson911d1ae2016-11-28 16:44:40 -0800381 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
Myles Watson8d749042017-09-19 10:01:28 -0700382 &gatts_data);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700383 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800384 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800385 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800386 } else /* nothing needs to be executed , send response now */
387 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700388 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200389 gatt_send_error_rsp(tcb, cid, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800390 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800391}
392
393/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800394 *
395 * Function gatt_process_read_multi_req
396 *
397 * Description This function is called to process the read multiple request
398 * from client.
399 *
400 * Returns void
401 *
402 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200403void gatt_process_read_multi_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
404 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800405 uint32_t trans_id;
406 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700407 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800408 tGATT_STATUS err = GATT_SUCCESS;
409 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700411 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800412
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200413 tGATT_READ_MULTI* multi_req = gatt_sr_get_read_multi(tcb, cid);
414 multi_req->num_handles = 0;
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +0200415 multi_req->variable_len = (op_code == GATT_REQ_READ_MULTI_VAR);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700416 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800417
Marie Janssend19e0782016-07-15 12:48:27 -0700418#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800419 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700420 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
421 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422
Myles Watson911d1ae2016-11-28 16:44:40 -0800423 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800424
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200425 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
426 handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800427
Myles Watson911d1ae2016-11-28 16:44:40 -0800428 return;
429 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800430#endif
431
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200432 while (ll >= 2 && multi_req->num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800433 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700435 auto it = gatt_sr_find_i_rcb_by_handle(handle);
436 if (it != gatt_cb.srv_list_info->end()) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200437 multi_req->handles[multi_req->num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800438
439 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700440 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
441 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800442 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700443 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800444 break;
445 }
446 } else {
447 /* invalid handle */
448 err = GATT_INVALID_HANDLE;
449 break;
450 }
451 ll -= 2;
452 }
453
454 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700455 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800456 }
457
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200458 if (multi_req->num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800459
460 if (err == GATT_SUCCESS) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200461 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, multi_req->handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800462 if (trans_id != 0) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200463 tGATT_SR_CMD* sr_cmd_p = gatt_sr_get_cmd_by_cid(tcb, cid);
464
465 gatt_sr_reset_cback_cnt(tcb,
466 cid); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800467
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200468 for (ll = 0; ll < multi_req->num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800469 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200470 handle = multi_req->handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700471 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800472
Myles Watson911d1ae2016-11-28 16:44:40 -0800473 p_msg->attr_value.handle = handle;
474 err = gatts_read_attr_value_by_handle(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200475 tcb, cid, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700476 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
477 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800478
479 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700480 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200481 GATT_SUCCESS, p_msg, sr_cmd_p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800482 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800483 /* either not using or done using the buffer, release it now */
484 osi_free(p_msg);
485 }
486 } else
487 err = GATT_NO_RESOURCES;
488 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800489
Myles Watson911d1ae2016-11-28 16:44:40 -0800490 /* in theroy BUSY is not possible(should already been checked), protected
491 * check */
492 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200493 gatt_send_error_rsp(tcb, cid, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800494}
495
496/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800497 *
498 * Function gatt_build_primary_service_rsp
499 *
500 * Description Primamry service request processed internally. Theretically
501 * only deal with ReadByTypeVAlue and ReadByGroupType.
502 *
503 * Returns void
504 *
505 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800506static tGATT_STATUS gatt_build_primary_service_rsp(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200507 BT_HDR* p_msg, tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
508 uint16_t s_hdl, uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data,
509 const Uuid& value) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800510 tGATT_STATUS status = GATT_NOT_FOUND;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700511 uint8_t handle_len = 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800512
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700513 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800514
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200515 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
516
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700517 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700518 if (el.s_hdl < s_hdl || el.s_hdl > e_hdl ||
519 el.type != GATT_UUID_PRI_SERVICE) {
520 continue;
521 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700522
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700523 Uuid* p_uuid = gatts_get_service_uuid(el.p_db);
524 if (!p_uuid) continue;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800525
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700526 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Jakub Pawlowskif107e4f2018-04-12 05:42:31 -0700527 handle_len = 4 + gatt_build_uuid_to_stream_len(*p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800528
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700529 /* get the length byte in the repsonse */
530 if (p_msg->offset == 0) {
531 *p++ = op_code + 1;
532 p_msg->len++;
533 p_msg->offset = handle_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800534
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700535 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
536 *p++ = (uint8_t)p_msg->offset; /* length byte */
537 p_msg->len++;
Myles Watson911d1ae2016-11-28 16:44:40 -0800538 }
539 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700540
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200541 if (p_msg->len + p_msg->offset > payload_size ||
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700542 handle_len != p_msg->offset) {
543 break;
544 }
545
546 if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue;
547
548 UINT16_TO_STREAM(p, el.s_hdl);
549
Jakub Pawlowski4c6007c2018-04-16 07:55:06 -0700550 if (gatt_cb.last_service_handle &&
551 gatt_cb.last_service_handle == el.s_hdl) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700552 VLOG(1) << "Use 0xFFFF for the last primary attribute";
553 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
554 UINT16_TO_STREAM(p, 0xFFFF);
555 } else {
556 UINT16_TO_STREAM(p, el.e_hdl);
557 }
558
559 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
560 gatt_build_uuid_to_stream(&p, *p_uuid);
561
562 status = GATT_SUCCESS;
563 p_msg->len += p_msg->offset;
Myles Watson911d1ae2016-11-28 16:44:40 -0800564 }
565 p_msg->offset = L2CAP_MIN_OFFSET;
566
567 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800568}
569
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700570/**
571 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800572 *
573 * Returns true: if data filled sucessfully.
574 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700575 */
576static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700577 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700578 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800579 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800580
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700581 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800582
Myles Watson911d1ae2016-11-28 16:44:40 -0800583 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800584
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700585 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800586
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700587 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700588 if (attr.handle > e_hdl) break;
589
590 if (attr.handle < s_hdl) continue;
591
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700592 uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize();
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700593 if (p_msg->offset == 0)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700594 p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16
595 : GATT_INFO_TYPE_PAIR_128;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700596
597 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
598
599 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700600 uuid_len == Uuid::kNumBytes16) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700601 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700602 UINT16_TO_STREAM(p, attr.uuid.As16Bit());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700603 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700604 uuid_len == Uuid::kNumBytes128) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700605 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700606 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700607 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700608 uuid_len == Uuid::kNumBytes32) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700609 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700610 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700611 } else {
612 LOG(ERROR) << "format mismatch";
613 return GATT_NO_RESOURCES;
614 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800615 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700616 p_msg->len += info_pair_len[p_msg->offset - 1];
617 len -= info_pair_len[p_msg->offset - 1];
618 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800619 }
620
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700621 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800622}
623
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700624static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
625 uint16_t& e_hdl) {
626 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800627
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700628 /* obtain starting handle, and ending handle */
629 STREAM_TO_UINT16(s_hdl, p);
630 STREAM_TO_UINT16(e_hdl, p);
631 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800632
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700633 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
634 !GATT_HANDLE_IS_VALID(e_hdl)) {
Stanley Tngbe701122018-05-07 14:58:36 -0700635 return GATT_INVALID_HANDLE;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700636 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800637
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700638 return GATT_SUCCESS;
639}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700641static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700642 uint8_t*& p, Uuid* p_uuid,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700643 uint16_t& s_hdl,
644 uint16_t& e_hdl) {
645 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
646 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700648 if (len < 2) return GATT_INVALID_PDU;
649
650 /* parse uuid now */
651 CHECK(p_uuid);
652 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
653 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
654 VLOG(1) << "Bad UUID";
655 return GATT_INVALID_PDU;
656 }
657
658 len -= uuid_len;
659 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800660}
661
662/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800663 *
664 * Function gatts_process_primary_service_req
665 *
Myles Watson9ca07092016-11-28 16:41:53 -0800666 * Description Process ReadByGroupType/ReadByTypeValue request, for
667 * discovering all primary services or discover primary service
668 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800669 *
670 * Returns void
671 *
672 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200673void gatts_process_primary_service_req(tGATT_TCB& tcb, uint16_t cid,
674 uint8_t op_code, uint16_t len,
675 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800676 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700677 Uuid uuid = Uuid::kEmpty;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800678
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700679 uint8_t reason =
680 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
681 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200682 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700683 return;
684 }
685
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700686 if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700687 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200688 gatt_send_error_rsp(tcb, cid, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl,
689 false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700690 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s",
691 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700692 return;
693 }
694
695 // we do not support ReadByTypeValue with any non-primamry_service type
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200696 gatt_send_error_rsp(tcb, cid, GATT_NOT_FOUND, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700697 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s",
698 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700699 return;
700 }
701
702 // TODO: we assume theh value is UUID, there is no such requirement in spec
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700703 Uuid value = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700704 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
Myles Watson5d5fcf22017-10-06 16:51:21 -0700705 if (!gatt_parse_uuid_from_cmd(&value, len, &p_data)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200706 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800707 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800708 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800709
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200710 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
711
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700712 uint16_t msg_len =
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200713 (uint16_t)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700714 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200715 reason = gatt_build_primary_service_rsp(p_msg, tcb, cid, op_code, s_hdl,
716 e_hdl, p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800717 if (reason != GATT_SUCCESS) {
718 osi_free(p_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200719 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700720 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700721 }
722
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200723 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800724}
725
726/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800727 *
728 * Function gatts_process_find_info
729 *
730 * Description process find information request, for discover character
731 * descriptors.
732 *
733 * Returns void
734 *
735 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200736static void gatts_process_find_info(tGATT_TCB& tcb, uint16_t cid,
737 uint8_t op_code, uint16_t len,
738 uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700739 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700740 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700741 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200742 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700743 return;
744 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800745
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200746 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700747 uint16_t buf_len =
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200748 (uint16_t)(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800749
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700750 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
751 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800752
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700753 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
754 *p++ = op_code + 1;
755 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800756
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200757 buf_len = payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800758
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700759 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
760 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700761 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700762 if (reason == GATT_NO_RESOURCES) {
763 reason = GATT_SUCCESS;
764 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800765 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800766 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800767 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800768
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700769 *p = (uint8_t)p_msg->offset;
770
771 p_msg->offset = L2CAP_MIN_OFFSET;
772
Myles Watson911d1ae2016-11-28 16:44:40 -0800773 if (reason != GATT_SUCCESS) {
774 osi_free(p_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200775 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800776 } else
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200777 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800778}
779
780/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800781 *
782 * Function gatts_process_mtu_req
783 *
784 * Description This function is called to process excahnge MTU request.
785 * Only used on LE.
786 *
787 * Returns void
788 *
789 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200790static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t cid, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800791 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800792 /* BR/EDR conenction, send error response */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200793 if (cid != L2CAP_ATT_CID) {
794 gatt_send_error_rsp(tcb, cid, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0,
795 false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700796 return;
797 }
798
799 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700800 LOG(ERROR) << "invalid MTU request PDU received.";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200801 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700802 return;
803 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800804
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700805 uint16_t mtu = 0;
806 uint8_t* p = p_data;
807 STREAM_TO_UINT16(mtu, p);
808 /* mtu must be greater than default MTU which is 23/48 */
809 if (mtu < GATT_DEF_BLE_MTU_SIZE)
810 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
811 else if (mtu > GATT_MAX_MTU_SIZE)
812 tcb.payload_size = GATT_MAX_MTU_SIZE;
813 else
814 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700815
Jakub Pawlowskibb956ab2018-05-24 12:27:10 -0700816 LOG(INFO) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800817
Hansong Zhangf451ded2021-09-08 10:15:34 -0700818 BTM_SetBleDataLength(tcb.peer_bda, tcb.payload_size + L2CAP_PKT_OVERHEAD);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800819
Myles Watson8d749042017-09-19 10:01:28 -0700820 tGATT_SR_MSG gatt_sr_msg;
821 gatt_sr_msg.mtu = tcb.payload_size;
822 BT_HDR* p_buf = attp_build_sr_msg(tcb, GATT_RSP_MTU, &gatt_sr_msg);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200823 attp_send_sr_msg(tcb, cid, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800824
Myles Watson8d749042017-09-19 10:01:28 -0700825 tGATTS_DATA gatts_data;
826 gatts_data.mtu = tcb.payload_size;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700827 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
828 /* of 0, as no response is allowed from applcations */
829 for (int i = 0; i < GATT_MAX_APPS; i++) {
830 if (gatt_cb.cl_rcb[i].in_use) {
831 uint16_t conn_id =
832 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700833 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, &gatts_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800834 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800835 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800836}
837
838/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800839 *
840 * Function gatts_process_read_by_type_req
841 *
842 * Description process Read By type request.
843 * This PDU can be used to perform:
844 * - read characteristic value
845 * - read characteristic descriptor value
846 * - discover characteristic
847 * - discover characteristic by UUID
848 * - relationship discovery
849 *
850 * Returns void
851 *
852 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200853void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint16_t cid,
854 uint8_t op_code, uint16_t len,
855 uint8_t* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700856 Uuid uuid = Uuid::kEmpty;
Hansong Zhanga1603122018-04-12 11:45:03 -0700857 uint16_t s_hdl = 0, e_hdl = 0, err_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700858 tGATT_STATUS reason =
859 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800860
Marie Janssend19e0782016-07-15 12:48:27 -0700861#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800862 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700863 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
864 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800865
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200866 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, gatt_cb.req_op_code,
867 s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800868
Myles Watson911d1ae2016-11-28 16:44:40 -0800869 return;
870 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800871#endif
872
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700873 if (reason != GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200874 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700875 return;
876 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800877
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200878 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
879
880 size_t msg_len = sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700881 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
882 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800883
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700884 *p++ = op_code + 1;
885 /* reserve length byte */
886 p_msg->len = 2;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200887 uint16_t buf_len = payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800888
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700889 reason = GATT_NOT_FOUND;
890 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
891 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
892 uint8_t sec_flag, key_size;
893 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800894
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700895 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200896 tcb, cid, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len,
897 sec_flag, key_size, 0, &err_hdl);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700898 if (ret != GATT_NOT_FOUND) {
899 reason = ret;
900 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
901 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800902
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700903 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
904 s_hdl = err_hdl;
905 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800906 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800907 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800908 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700909 *p = (uint8_t)p_msg->offset;
910 p_msg->offset = L2CAP_MIN_OFFSET;
911
Myles Watson911d1ae2016-11-28 16:44:40 -0800912 if (reason != GATT_SUCCESS) {
913 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800914
Myles Watson911d1ae2016-11-28 16:44:40 -0800915 /* in theroy BUSY is not possible(should already been checked), protected
916 * check */
917 if (reason != GATT_PENDING && reason != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200918 gatt_send_error_rsp(tcb, cid, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700919
920 return;
921 }
922
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200923 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924}
925
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700926/**
927 * This function is called to process the write request from client.
928 */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200929static void gatts_process_write_req(tGATT_TCB& tcb, uint16_t cid,
930 tGATT_SRV_LIST_ELEM& el, uint16_t handle,
931 uint8_t op_code, uint16_t len,
932 uint8_t* p_data,
Chris Mantonefa23112020-03-04 11:26:31 -0800933 bt_gatt_db_attribute_type_t gatt_type) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800934 tGATTS_DATA sr_data;
935 uint32_t trans_id;
936 tGATT_STATUS status;
937 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800938 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939
Myles Watson911d1ae2016-11-28 16:44:40 -0800940 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941
Myles Watson911d1ae2016-11-28 16:44:40 -0800942 switch (op_code) {
943 case GATT_REQ_PREPARE_WRITE:
Chris Mantonefa23112020-03-04 11:26:31 -0800944 if (len < 2 || p == nullptr) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700945 LOG(ERROR) << __func__
946 << ": Prepare write request was invalid - missing offset, "
947 "sending error response";
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200948 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800949 return;
950 }
951 sr_data.write_req.is_prep = true;
952 STREAM_TO_UINT16(sr_data.write_req.offset, p);
953 len -= 2;
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700954 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800955 case GATT_SIGN_CMD_WRITE:
956 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700957 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800958 len -= GATT_AUTH_SIGN_LEN;
959 }
Chih-Hung Hsiehd4646582018-09-12 15:20:43 -0700960 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
Myles Watson911d1ae2016-11-28 16:44:40 -0800961 case GATT_CMD_WRITE:
962 case GATT_REQ_WRITE:
963 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
964 sr_data.write_req.need_rsp = true;
965 sr_data.write_req.handle = handle;
Chris Mantonefa23112020-03-04 11:26:31 -0800966 if (len > GATT_MAX_ATTR_LEN) len = GATT_MAX_ATTR_LEN;
Myles Watson911d1ae2016-11-28 16:44:40 -0800967 sr_data.write_req.len = len;
Chris Mantonefa23112020-03-04 11:26:31 -0800968 if (len != 0 && p != nullptr) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800969 memcpy(sr_data.write_req.value, p, len);
970 }
971 break;
972 }
973
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700974 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800975
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700976 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
977 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800978 sec_flag, key_size);
979
980 if (status == GATT_SUCCESS) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +0200981 trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800982 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700983 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800984
985 uint8_t opcode = 0;
986 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
987 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
988 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
989 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
990 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700991 LOG(ERROR) << __func__
992 << "%s: Attempt to write attribute that's not tied with"
993 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800994 status = GATT_ERROR;
995 }
996
997 if (opcode) {
998 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
999 status = GATT_PENDING;
1000 }
1001 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001002 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -08001003 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001004 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001005 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001006
Myles Watson911d1ae2016-11-28 16:44:40 -08001007 /* in theroy BUSY is not possible(should already been checked), protected
1008 * check */
1009 if (status != GATT_PENDING && status != GATT_BUSY &&
1010 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001011 gatt_send_error_rsp(tcb, cid, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -08001012 }
1013 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001014}
1015
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001016/**
1017 * This function is called to process the read request from client.
1018 */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001019static void gatts_process_read_req(tGATT_TCB& tcb, uint16_t cid,
1020 tGATT_SRV_LIST_ELEM& el, uint8_t op_code,
1021 uint16_t handle, uint16_t len,
1022 uint8_t* p_data) {
1023 uint16_t payload_size = gatt_tcb_get_payload_size_tx(tcb, cid);
1024
1025 size_t buf_len = sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001026 uint16_t offset = 0;
Stanley Tngcc9c7332018-04-05 09:54:13 -07001027
1028 if (op_code == GATT_REQ_READ_BLOB && len < sizeof(uint16_t)) {
1029 /* Error: packet length is too short */
1030 LOG(ERROR) << __func__ << ": packet length=" << len
1031 << " too short. min=" << sizeof(uint16_t);
1032 android_errorWriteWithInfoLog(0x534e4554, "73172115", -1, NULL, 0);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001033 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, 0, false);
Stanley Tngcc9c7332018-04-05 09:54:13 -07001034 return;
1035 }
1036
Myles Watson911d1ae2016-11-28 16:44:40 -08001037 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001038
Myles Watson911d1ae2016-11-28 16:44:40 -08001039 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001040
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001041 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -08001042 *p++ = op_code + 1;
1043 p_msg->len = 1;
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001044 buf_len = payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001046 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001047 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001048
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001049 uint16_t value_len = 0;
1050 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001051 tcb, cid, el.p_db, op_code, handle, offset, p, &value_len,
1052 (uint16_t)buf_len, sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -08001053 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001054
Myles Watson911d1ae2016-11-28 16:44:40 -08001055 if (reason != GATT_SUCCESS) {
1056 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001057
Stanley Tngcc9c7332018-04-05 09:54:13 -07001058 /* in theory BUSY is not possible(should already been checked), protected
Myles Watson911d1ae2016-11-28 16:44:40 -08001059 * check */
1060 if (reason != GATT_PENDING && reason != GATT_BUSY)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001061 gatt_send_error_rsp(tcb, cid, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -07001062
1063 return;
1064 }
1065
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001066 attp_send_sr_msg(tcb, cid, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001067}
1068
1069/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001070 *
1071 * Function gatts_process_attribute_req
1072 *
Myles Watson9ca07092016-11-28 16:41:53 -08001073 * Description This function is called to process the per attribute handle
1074 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001075 *
1076 * Returns void
1077 *
1078 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001079void gatts_process_attribute_req(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
1080 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001081 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001082 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -08001083 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001084
Myles Watson911d1ae2016-11-28 16:44:40 -08001085 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001086 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -08001087 status = GATT_INVALID_PDU;
1088 } else {
1089 STREAM_TO_UINT16(handle, p);
1090 len -= 2;
1091 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001092
Marie Janssend19e0782016-07-15 12:48:27 -07001093#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001094 gatt_cb.handle = handle;
1095 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001096 VLOG(1) << "Conformance tst: forced err rsp: error status="
1097 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001098
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001099 gatt_send_error_rsp(tcb, cid, gatt_cb.err_status, cid, gatt_cb.req_op_code,
1100 handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001101
Myles Watson911d1ae2016-11-28 16:44:40 -08001102 return;
1103 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001104#endif
1105
Myles Watson911d1ae2016-11-28 16:44:40 -08001106 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001107 for (auto& el : *gatt_cb.srv_list_info) {
1108 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1109 for (const auto& attr : el.p_db->attr_list) {
1110 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001111 switch (op_code) {
1112 case GATT_REQ_READ: /* read char/char descriptor value */
1113 case GATT_REQ_READ_BLOB:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001114 gatts_process_read_req(tcb, cid, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001115 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001116
Myles Watson911d1ae2016-11-28 16:44:40 -08001117 case GATT_REQ_WRITE: /* write char/char descriptor value */
1118 case GATT_CMD_WRITE:
1119 case GATT_SIGN_CMD_WRITE:
1120 case GATT_REQ_PREPARE_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001121 gatts_process_write_req(tcb, cid, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001122 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001123 break;
1124 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001125 break;
1126 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001127 status = GATT_SUCCESS;
1128 break;
1129 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001130 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001131 break;
1132 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001133 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001134 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001135
Myles Watson911d1ae2016-11-28 16:44:40 -08001136 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1137 op_code != GATT_SIGN_CMD_WRITE)
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001138 gatt_send_error_rsp(tcb, cid, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001139}
1140
1141/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001142 *
1143 * Function gatts_proc_srv_chg_ind_ack
1144 *
1145 * Description This function process the service changed indicaiton ACK
1146 *
1147 * Returns void
1148 *
1149 ******************************************************************************/
Jakub Pawlowski890c5012019-04-24 23:00:16 +02001150void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001151 tGATTS_SRV_CHG_REQ req;
1152 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001153
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001154 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001155
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001156 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001157 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001158 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001159 p_buf->srv_changed = false;
1160 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1161 if (gatt_cb.cb_info.p_srv_chg_callback)
1162 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1163 &req, NULL);
1164 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001165}
1166
1167/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001168 *
1169 * Function gatts_chk_pending_ind
1170 *
Myles Watson9ca07092016-11-28 16:41:53 -08001171 * Description This function check any pending indication needs to be sent
1172 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001173 *
1174 * Returns void
1175 *
1176 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001177static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001178 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001179
Myles Watson911d1ae2016-11-28 16:44:40 -08001180 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001181 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001182 if (p_buf != NULL) {
1183 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1184 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001185 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001186 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001187}
1188
1189/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001190 *
1191 * Function gatts_proc_ind_ack
1192 *
Myles Watson9ca07092016-11-28 16:41:53 -08001193 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001194 *
Myles Watson9ca07092016-11-28 16:41:53 -08001195 * Returns true continue to process the indication ack by the
1196 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001197 *
1198 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001199static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001200 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001201
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001202 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001203
Myles Watson911d1ae2016-11-28 16:44:40 -08001204 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001205 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001206 /* there is no need to inform the application since srv chg is handled
1207 * internally by GATT */
1208 continue_processing = false;
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001209
1210 // After receiving ack of svc_chg_ind, reset client status
1211 gatt_sr_update_cl_status(tcb, /* chg_aware= */ true);
Myles Watson911d1ae2016-11-28 16:44:40 -08001212 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001213
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001214 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001215 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001216}
1217
1218/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001219 *
1220 * Function gatts_process_value_conf
1221 *
Myles Watson9ca07092016-11-28 16:41:53 -08001222 * Description This function is called to process the handle value
1223 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001224 *
1225 * Returns void
1226 *
1227 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001228void gatts_process_value_conf(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code) {
1229 uint16_t handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001230
HsingYuan Lo95a96992020-09-08 11:27:40 +08001231 if (!gatt_tcb_find_indicate_handle(tcb, cid, &handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001232 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001233 return;
1234 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001235
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +02001236 gatt_stop_conf_timer(tcb, cid);
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001237
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001238 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1239
1240 if (continue_processing) {
Myles Watson8d749042017-09-19 10:01:28 -07001241 tGATTS_DATA gatts_data;
1242 gatts_data.handle = handle;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001243 for (auto& el : *gatt_cb.srv_list_info) {
1244 if (el.s_hdl <= handle && el.e_hdl >= handle) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001245 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, cid, op_code, handle);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001246 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1247 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
Myles Watson8d749042017-09-19 10:01:28 -07001248 &gatts_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001249 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001250 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001251 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001252}
1253
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001254static bool gatts_process_db_out_of_sync(tGATT_TCB& tcb, uint16_t cid,
1255 uint8_t op_code, uint16_t len,
1256 uint8_t* p_data) {
1257 if (gatt_sr_is_cl_change_aware(tcb)) return false;
1258
1259 // default value
1260 bool should_ignore = true;
1261 bool should_rsp = true;
1262
1263 switch (op_code) {
1264 case GATT_REQ_READ_BY_TYPE: {
1265 // Check if read database hash by UUID
1266 Uuid uuid = Uuid::kEmpty;
1267 uint16_t s_hdl = 0, e_hdl = 0;
1268 uint16_t db_hash_handle = gatt_cb.handle_of_database_hash;
1269 tGATT_STATUS reason = gatts_validate_packet_format(op_code, len, p_data,
1270 &uuid, s_hdl, e_hdl);
1271 if (reason == GATT_SUCCESS &&
1272 (s_hdl <= db_hash_handle && db_hash_handle <= e_hdl) &&
1273 (uuid == Uuid::From16Bit(GATT_UUID_DATABASE_HASH)))
1274 should_ignore = false;
1275
1276 } break;
1277 case GATT_REQ_READ: {
1278 // Check if read database hash by handle
1279 uint16_t handle = 0;
1280 uint8_t* p = p_data;
1281 tGATT_STATUS status = GATT_SUCCESS;
1282
1283 if (len < 2) {
1284 status = GATT_INVALID_PDU;
1285 } else {
1286 STREAM_TO_UINT16(handle, p);
1287 len -= 2;
1288 }
1289
1290 if (status == GATT_SUCCESS && handle == gatt_cb.handle_of_database_hash)
1291 should_ignore = false;
1292
1293 } break;
1294 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1295 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
1296 case GATT_REQ_FIND_INFO: /* discover char descrptor */
1297 case GATT_REQ_READ_BLOB: /* read long char */
1298 case GATT_REQ_READ_MULTI: /* read multi char*/
1299 case GATT_REQ_WRITE: /* write char/char descriptor value */
1300 case GATT_REQ_PREPARE_WRITE: /* write long char */
1301 // Use default value
1302 break;
1303 case GATT_CMD_WRITE: /* cmd */
1304 case GATT_SIGN_CMD_WRITE: /* sign cmd */
1305 should_rsp = false;
1306 break;
1307 case GATT_REQ_MTU: /* configure mtu */
1308 case GATT_REQ_EXEC_WRITE: /* execute write */
1309 case GATT_HANDLE_VALUE_CONF: /* confirm for indication */
1310 default:
1311 should_ignore = false;
1312 }
1313
1314 if (should_ignore) {
1315 if (should_rsp) {
1316 gatt_send_error_rsp(tcb, cid, GATT_DATABASE_OUT_OF_SYNC, op_code, 0x0000,
1317 false);
1318 }
1319 LOG(INFO) << __func__ << ": database out of sync, device=" << tcb.peer_bda
1320 << ", op_code=" << loghex((uint16_t)op_code)
1321 << ", should_rsp=" << should_rsp;
1322 gatt_sr_update_cl_status(tcb, /* chg_aware= */ should_rsp);
1323 }
1324
1325 return should_ignore;
1326}
1327
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001328/** This function is called to handle the client requests to server */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001329void gatt_server_handle_client_req(tGATT_TCB& tcb, uint16_t cid,
1330 uint8_t op_code, uint16_t len,
1331 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001332 /* there is pending command, discard this one */
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +02001333 if (!gatt_sr_cmd_empty(tcb, cid) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001334
Myles Watson911d1ae2016-11-28 16:44:40 -08001335 /* the size of the message may not be bigger than the local max PDU size*/
1336 /* The message has to be smaller than the agreed MTU, len does not include op
1337 * code */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001338
1339 uint16_t payload_size = gatt_tcb_get_payload_size_rx(tcb, cid);
1340 if (len >= payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001341 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001342 len + 1, payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001343 /* for invalid request expecting response, send it now */
1344 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1345 op_code != GATT_HANDLE_VALUE_CONF) {
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001346 gatt_send_error_rsp(tcb, cid, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001347 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001348 /* otherwise, ignore the pkt */
1349 } else {
HsingYuan Lo8f65e252020-12-17 18:16:16 +08001350 // handle database out of sync
1351 if (gatts_process_db_out_of_sync(tcb, cid, op_code, len, p_data)) return;
1352
Myles Watson911d1ae2016-11-28 16:44:40 -08001353 switch (op_code) {
1354 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1355 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001356 gatts_process_primary_service_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001357 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001358
Myles Watson911d1ae2016-11-28 16:44:40 -08001359 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001360 gatts_process_find_info(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001361 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001362
Myles Watson911d1ae2016-11-28 16:44:40 -08001363 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1364 value */
1365 /* discover characteristic, discover char by UUID */
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001366 gatts_process_read_by_type_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001367 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001368
Myles Watson911d1ae2016-11-28 16:44:40 -08001369 case GATT_REQ_READ: /* read char/char descriptor value */
1370 case GATT_REQ_READ_BLOB:
1371 case GATT_REQ_WRITE: /* write char/char descriptor value */
1372 case GATT_CMD_WRITE:
1373 case GATT_SIGN_CMD_WRITE:
1374 case GATT_REQ_PREPARE_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001375 gatts_process_attribute_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 case GATT_HANDLE_VALUE_CONF:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001379 gatts_process_value_conf(tcb, cid, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001380 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001381
Myles Watson911d1ae2016-11-28 16:44:40 -08001382 case GATT_REQ_MTU:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001383 gatts_process_mtu_req(tcb, cid, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001384 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001385
Myles Watson911d1ae2016-11-28 16:44:40 -08001386 case GATT_REQ_EXEC_WRITE:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001387 gatt_process_exec_write_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001388 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001389
Myles Watson911d1ae2016-11-28 16:44:40 -08001390 case GATT_REQ_READ_MULTI:
Łukasz Rymanowski8f4d5442020-06-18 12:18:16 +02001391 case GATT_REQ_READ_MULTI_VAR:
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +02001392 gatt_process_read_multi_req(tcb, cid, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001393 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001394
Myles Watson911d1ae2016-11-28 16:44:40 -08001395 default:
1396 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001397 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001398 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001399}