blob: 4a37d9bfaa4598a7f7e5c9fec16953b050769499 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2008-2012 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 * this file contains the GATT server functions
22 *
23 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080024#include "bt_target.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070025#include "osi/include/osi.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080026
Jakub Pawlowskie4f13782018-10-23 14:46:24 +020027#include <log/log.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080028#include <string.h>
Jakub Pawlowskie4f13782018-10-23 14:46:24 +020029
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include "gatt_int.h"
31#include "l2c_api.h"
Priti Aghera636d6712014-12-18 13:55:48 -080032#include "l2c_int.h"
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020033#include "stack/eatt/eatt.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080034#define GATT_MTU_REQ_MIN_LEN 2
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070036using base::StringPrintf;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070037using bluetooth::Uuid;
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020038using bluetooth::eatt::EattExtension;
39using bluetooth::eatt::EattChannel;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070040
The Android Open Source Project5738f832012-12-12 16:00:35 -080041/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080042 *
43 * Function gatt_sr_enqueue_cmd
44 *
45 * Description This function enqueue the request from client which needs a
46 * application response, and update the transaction ID.
47 *
48 * Returns void
49 *
50 ******************************************************************************/
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020051uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint16_t cid, uint8_t op_code,
52 uint16_t handle) {
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020053 tGATT_SR_CMD* p_cmd;
54
55 if (cid == tcb.att_lcid) {
56 p_cmd = &tcb.sr_cmd;
57 } else {
58 EattChannel* channel =
59 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
60 p_cmd = &channel->server_outstanding_cmd_;
61 }
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020062
Myles Watson911d1ae2016-11-28 16:44:40 -080063 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080064
Łukasz Rymanowski7a496c82020-05-01 02:40:58 +020065 p_cmd->cid = cid;
66
Myles Watson911d1ae2016-11-28 16:44:40 -080067 if ((p_cmd->op_code == 0) ||
68 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
69 {
70 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
71 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070072 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080073 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070074 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080075 p_cmd->op_code = op_code;
76 p_cmd->handle = handle;
77 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070078 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080079 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080080 }
Myles Watson911d1ae2016-11-28 16:44:40 -080081 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080082
Myles Watson911d1ae2016-11-28 16:44:40 -080083 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080084}
85
86/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080087 *
88 * Function gatt_sr_cmd_empty
89 *
Myles Watson9ca07092016-11-28 16:41:53 -080090 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080091 *
92 * Returns true if empty, false if there is pending command.
93 *
94 ******************************************************************************/
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +020095bool gatt_sr_cmd_empty(tGATT_TCB& tcb, uint16_t cid) {
96 if (cid == tcb.att_lcid) return (tcb.sr_cmd.op_code == 0);
97
98 EattChannel* channel =
99 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
100
101 return (channel->server_outstanding_cmd_.op_code == 0);
102}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800103
104/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800105 *
106 * Function gatt_dequeue_sr_cmd
107 *
108 * Description This function dequeue the request from command queue.
109 *
110 * Returns void
111 *
112 ******************************************************************************/
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200113void gatt_dequeue_sr_cmd(tGATT_TCB& tcb, uint16_t cid) {
114 tGATT_SR_CMD* p_cmd;
115
116 if (cid == tcb.att_lcid) {
117 p_cmd = &tcb.sr_cmd;
118 } else {
119 EattChannel* channel =
120 EattExtension::GetInstance()->FindEattChannelByCid(tcb.peer_bda, cid);
121
122 p_cmd = &channel->server_outstanding_cmd_;
123 }
124
Myles Watson911d1ae2016-11-28 16:44:40 -0800125 /* Double check in case any buffers are queued */
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200126 VLOG(1) << "gatt_dequeue_sr_cmd cid: " << loghex(cid);
127 if (p_cmd->p_rsp_msg)
128 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = "
129 << p_cmd->p_rsp_msg;
130 osi_free_and_reset((void**)&p_cmd->p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800131
Łukasz Rymanowskib6317be2020-05-09 01:26:11 +0200132 while (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
133 osi_free(fixed_queue_try_dequeue(p_cmd->multi_rsp_q));
134 fixed_queue_free(p_cmd->multi_rsp_q, NULL);
135 memset(p_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136}
Ł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
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700817 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
818 tcb.payload_size);
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}