blob: 264bc82b74a4215fbff89c008cbd3c0d03663f46 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2008-2012 Broadcom Corporation
4 *
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 ******************************************************************************/
24
25#include "bt_target.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080026#include "bt_utils.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070027#include "osi/include/osi.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080028
The Android Open Source Project5738f832012-12-12 16:00:35 -080029#include <string.h>
30#include "gatt_int.h"
31#include "l2c_api.h"
Priti Aghera636d6712014-12-18 13:55:48 -080032#include "l2c_int.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080033#define GATT_MTU_REQ_MIN_LEN 2
The Android Open Source Project5738f832012-12-12 16:00:35 -080034
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070035using base::StringPrintf;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070036using bluetooth::Uuid;
37
The Android Open Source Project5738f832012-12-12 16:00:35 -080038/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080039 *
40 * Function gatt_sr_enqueue_cmd
41 *
42 * Description This function enqueue the request from client which needs a
43 * application response, and update the transaction ID.
44 *
45 * Returns void
46 *
47 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070048uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint8_t op_code, uint16_t handle) {
49 tGATT_SR_CMD* p_cmd = &tcb.sr_cmd;
Myles Watson911d1ae2016-11-28 16:44:40 -080050 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080051
Myles Watson911d1ae2016-11-28 16:44:40 -080052 if ((p_cmd->op_code == 0) ||
53 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
54 {
55 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
56 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070057 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080058 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070059 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080060 p_cmd->op_code = op_code;
61 p_cmd->handle = handle;
62 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070063 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080064 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080065 }
Myles Watson911d1ae2016-11-28 16:44:40 -080066 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080067
Myles Watson911d1ae2016-11-28 16:44:40 -080068 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080069}
70
71/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080072 *
73 * Function gatt_sr_cmd_empty
74 *
Myles Watson9ca07092016-11-28 16:41:53 -080075 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080076 *
77 * Returns true if empty, false if there is pending command.
78 *
79 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070080bool gatt_sr_cmd_empty(tGATT_TCB& tcb) { return (tcb.sr_cmd.op_code == 0); }
The Android Open Source Project5738f832012-12-12 16:00:35 -080081
82/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080083 *
84 * Function gatt_dequeue_sr_cmd
85 *
86 * Description This function dequeue the request from command queue.
87 *
88 * Returns void
89 *
90 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070091void gatt_dequeue_sr_cmd(tGATT_TCB& tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -080092 /* Double check in case any buffers are queued */
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070093 VLOG(1) << "gatt_dequeue_sr_cmd";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070094 if (tcb.sr_cmd.p_rsp_msg)
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070095 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = " << tcb.sr_cmd.p_rsp_msg;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070096 osi_free_and_reset((void**)&tcb.sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -080097
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070098 while (!fixed_queue_is_empty(tcb.sr_cmd.multi_rsp_q))
99 osi_free(fixed_queue_try_dequeue(tcb.sr_cmd.multi_rsp_q));
100 fixed_queue_free(tcb.sr_cmd.multi_rsp_q, NULL);
101 memset(&tcb.sr_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800102}
103
104/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800105 *
106 * Function process_read_multi_rsp
107 *
108 * Description This function check the read multiple response.
109 *
110 * Returns bool if all replies have been received
111 *
112 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800113static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
114 tGATTS_RSP* p_msg, uint16_t mtu) {
115 uint16_t ii, total_len, len;
116 uint8_t* p;
117 bool is_overflow = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800118
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700119 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700120
Myles Watson911d1ae2016-11-28 16:44:40 -0800121 if (p_cmd->multi_rsp_q == NULL)
122 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800123
Myles Watson911d1ae2016-11-28 16:44:40 -0800124 /* Enqueue the response */
125 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
126 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
127 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800128
Myles Watson911d1ae2016-11-28 16:44:40 -0800129 p_cmd->status = status;
130 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700131 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
132 << " num_hdls=" << p_cmd->multi_req.num_handles;
Myles Watson911d1ae2016-11-28 16:44:40 -0800133 /* Wait till we get all the responses */
134 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
135 p_cmd->multi_req.num_handles) {
136 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
137 p_buf = (BT_HDR*)osi_calloc(len);
138 p_buf->offset = L2CAP_MIN_OFFSET;
139 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800140
Myles Watson911d1ae2016-11-28 16:44:40 -0800141 /* First byte in the response is the opcode */
142 *p++ = GATT_RSP_READ_MULTI;
143 p_buf->len = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800144
Myles Watson911d1ae2016-11-28 16:44:40 -0800145 /* Now walk through the buffers puting the data into the response in order
146 */
147 list_t* list = NULL;
148 const list_node_t* node = NULL;
149 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
150 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
151 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
152 tGATTS_RSP* p_rsp = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700153
Myles Watson911d1ae2016-11-28 16:44:40 -0800154 if (list != NULL) {
155 if (ii == 0)
156 node = list_begin(list);
157 else
158 node = list_next(node);
159 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800160 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800161
Myles Watson911d1ae2016-11-28 16:44:40 -0800162 if (p_rsp != NULL) {
163 total_len = (p_buf->len + p_rsp->attr_value.len);
164
165 if (total_len > mtu) {
166 /* just send the partial response for the overflow case */
167 len = p_rsp->attr_value.len - (total_len - mtu);
168 is_overflow = true;
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700169 VLOG(1) << StringPrintf(
170 "multi read overflow available len=%d val_len=%d", len,
171 p_rsp->attr_value.len);
Myles Watson911d1ae2016-11-28 16:44:40 -0800172 } else {
173 len = p_rsp->attr_value.len;
174 }
175
176 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
177 memcpy(p, p_rsp->attr_value.value, len);
178 if (!is_overflow) p += len;
179 p_buf->len += len;
180 } else {
181 p_cmd->status = GATT_NOT_FOUND;
182 break;
183 }
184
185 if (is_overflow) break;
186
187 } else {
188 p_cmd->status = GATT_NOT_FOUND;
189 break;
190 }
191
192 } /* loop through all handles*/
193
194 /* Sanity check on the buffer length */
195 if (p_buf->len == 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700196 LOG(ERROR) << __func__ << " nothing found!!";
Myles Watson911d1ae2016-11-28 16:44:40 -0800197 p_cmd->status = GATT_NOT_FOUND;
198 osi_free(p_buf);
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700199 VLOG(1) << __func__ << "osi_free(p_buf)";
Myles Watson911d1ae2016-11-28 16:44:40 -0800200 } else if (p_cmd->p_rsp_msg != NULL) {
201 osi_free(p_buf);
202 } else {
203 p_cmd->p_rsp_msg = p_buf;
204 }
205
206 return (true);
207 }
208 } else /* any handle read exception occurs, return error */
209 {
210 return (true);
211 }
212
213 /* If here, still waiting */
214 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800215}
216
217/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800218 *
219 * Function gatt_sr_process_app_rsp
220 *
Myles Watson9ca07092016-11-28 16:41:53 -0800221 * Description This function checks whether the response message from
222 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800223 *
224 * Returns void
225 *
226 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700227tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800228 UNUSED_ATTR uint32_t trans_id,
229 uint8_t op_code, tGATT_STATUS status,
230 tGATTS_RSP* p_msg) {
231 tGATT_STATUS ret_code = GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800232
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700233 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700235 gatt_sr_update_cback_cnt(tcb, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800236
Myles Watson911d1ae2016-11-28 16:44:40 -0800237 if (op_code == GATT_REQ_READ_MULTI) {
238 /* If no error and still waiting, just return */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700239 if (!process_read_multi_rsp(&tcb.sr_cmd, status, p_msg, tcb.payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800240 return (GATT_SUCCESS);
241 } else {
242 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700243 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800244
245 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700246 gatt_sr_reset_cback_cnt(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800247
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700248 tcb.sr_cmd.status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800249
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700250 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
251 if (tcb.sr_cmd.p_rsp_msg == NULL) {
252 tcb.sr_cmd.p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
253 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800254 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700255 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800256 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800257 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800258 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700259 if (gatt_sr_is_cback_cnt_zero(tcb)) {
260 if ((tcb.sr_cmd.status == GATT_SUCCESS) && (tcb.sr_cmd.p_rsp_msg)) {
261 ret_code = attp_send_sr_msg(tcb, tcb.sr_cmd.p_rsp_msg);
262 tcb.sr_cmd.p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800263 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700264 ret_code =
265 gatt_send_error_rsp(tcb, status, op_code, tcb.sr_cmd.handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800266 }
267
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700268 gatt_dequeue_sr_cmd(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800269 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800270
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700271 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800272
273 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800274}
275
276/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800277 *
278 * Function gatt_process_exec_write_req
279 *
280 * Description This function is called to process the execute write request
281 * from client.
282 *
283 * Returns void
284 *
285 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700286void gatt_process_exec_write_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800287 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
288 uint8_t *p = p_data, flag, i = 0;
289 uint32_t trans_id = 0;
290 tGATT_IF gatt_if;
291 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800292
Marie Janssend19e0782016-07-15 12:48:27 -0700293#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800294 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700295 VLOG(1)
296 << "Conformance tst: forced err rspv for Execute Write: error status="
297 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800298
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700299 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800300 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800301
Myles Watson911d1ae2016-11-28 16:44:40 -0800302 return;
303 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800304#endif
305
Myles Watson911d1ae2016-11-28 16:44:40 -0800306 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800307
Myles Watson911d1ae2016-11-28 16:44:40 -0800308 /* mask the flag */
309 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800310
Myles Watson911d1ae2016-11-28 16:44:40 -0800311 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700312 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
313 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, 0);
314 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800315
Myles Watson911d1ae2016-11-28 16:44:40 -0800316 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700317 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800318 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700319 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800320 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
321 (tGATTS_DATA*)&flag);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700322 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800323 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800324 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800325 } else /* nothing needs to be executed , send response now */
326 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700327 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700328 gatt_send_error_rsp(tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800329 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800330}
331
332/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800333 *
334 * Function gatt_process_read_multi_req
335 *
336 * Description This function is called to process the read multiple request
337 * from client.
338 *
339 * Returns void
340 *
341 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700342void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
343 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800344 uint32_t trans_id;
345 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700346 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800347 tGATT_STATUS err = GATT_SUCCESS;
348 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800349
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700350 VLOG(1) << __func__;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700351 tcb.sr_cmd.multi_req.num_handles = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800352
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700353 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800354
Marie Janssend19e0782016-07-15 12:48:27 -0700355#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800356 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700357 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
358 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359
Myles Watson911d1ae2016-11-28 16:44:40 -0800360 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800361
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700362 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -0800363 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800364
Myles Watson911d1ae2016-11-28 16:44:40 -0800365 return;
366 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800367#endif
368
Myles Watson911d1ae2016-11-28 16:44:40 -0800369 while (ll >= 2 &&
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700370 tcb.sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800371 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800372
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700373 auto it = gatt_sr_find_i_rcb_by_handle(handle);
374 if (it != gatt_cb.srv_list_info->end()) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700375 tcb.sr_cmd.multi_req.handles[tcb.sr_cmd.multi_req.num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800376
377 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700378 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
379 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800380 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700381 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800382 break;
383 }
384 } else {
385 /* invalid handle */
386 err = GATT_INVALID_HANDLE;
387 break;
388 }
389 ll -= 2;
390 }
391
392 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700393 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800394 }
395
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700396 if (tcb.sr_cmd.multi_req.num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800397
398 if (err == GATT_SUCCESS) {
399 trans_id =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700400 gatt_sr_enqueue_cmd(tcb, op_code, tcb.sr_cmd.multi_req.handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800401 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700402 gatt_sr_reset_cback_cnt(tcb); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800403
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700404 for (ll = 0; ll < tcb.sr_cmd.multi_req.num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800405 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700406 handle = tcb.sr_cmd.multi_req.handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700407 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800408
Myles Watson911d1ae2016-11-28 16:44:40 -0800409 p_msg->attr_value.handle = handle;
410 err = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700411 tcb, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700412 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
413 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800414
415 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700416 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700417 GATT_SUCCESS, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800418 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800419 /* either not using or done using the buffer, release it now */
420 osi_free(p_msg);
421 }
422 } else
423 err = GATT_NO_RESOURCES;
424 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800425
Myles Watson911d1ae2016-11-28 16:44:40 -0800426 /* in theroy BUSY is not possible(should already been checked), protected
427 * check */
428 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700429 gatt_send_error_rsp(tcb, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800430}
431
432/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800433 *
434 * Function gatt_build_primary_service_rsp
435 *
436 * Description Primamry service request processed internally. Theretically
437 * only deal with ReadByTypeVAlue and ReadByGroupType.
438 *
439 * Returns void
440 *
441 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800442static tGATT_STATUS gatt_build_primary_service_rsp(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700443 BT_HDR* p_msg, tGATT_TCB& tcb, uint8_t op_code, uint16_t s_hdl,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700444 uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, const Uuid& value) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800445 tGATT_STATUS status = GATT_NOT_FOUND;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700446 uint8_t handle_len = 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800447
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700448 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700450 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700451 if (el.s_hdl < s_hdl || el.s_hdl > e_hdl ||
452 el.type != GATT_UUID_PRI_SERVICE) {
453 continue;
454 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700455
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700456 Uuid* p_uuid = gatts_get_service_uuid(el.p_db);
457 if (!p_uuid) continue;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800458
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700459 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
460 handle_len = 4 + p_uuid->GetShortestRepresentationSize();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700462 /* get the length byte in the repsonse */
463 if (p_msg->offset == 0) {
464 *p++ = op_code + 1;
465 p_msg->len++;
466 p_msg->offset = handle_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800467
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700468 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
469 *p++ = (uint8_t)p_msg->offset; /* length byte */
470 p_msg->len++;
Myles Watson911d1ae2016-11-28 16:44:40 -0800471 }
472 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700473
474 if (p_msg->len + p_msg->offset > tcb.payload_size ||
475 handle_len != p_msg->offset) {
476 break;
477 }
478
479 if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue;
480
481 UINT16_TO_STREAM(p, el.s_hdl);
482
483 if (gatt_cb.last_primary_s_handle &&
484 gatt_cb.last_primary_s_handle == el.s_hdl) {
485 VLOG(1) << "Use 0xFFFF for the last primary attribute";
486 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
487 UINT16_TO_STREAM(p, 0xFFFF);
488 } else {
489 UINT16_TO_STREAM(p, el.e_hdl);
490 }
491
492 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
493 gatt_build_uuid_to_stream(&p, *p_uuid);
494
495 status = GATT_SUCCESS;
496 p_msg->len += p_msg->offset;
Myles Watson911d1ae2016-11-28 16:44:40 -0800497 }
498 p_msg->offset = L2CAP_MIN_OFFSET;
499
500 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800501}
502
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700503/**
504 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800505 *
506 * Returns true: if data filled sucessfully.
507 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700508 */
509static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700510 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700511 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800512 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800513
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700514 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515
Myles Watson911d1ae2016-11-28 16:44:40 -0800516 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800517
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700518 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800519
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700520 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700521 if (attr.handle > e_hdl) break;
522
523 if (attr.handle < s_hdl) continue;
524
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700525 uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize();
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700526 if (p_msg->offset == 0)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700527 p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16
528 : GATT_INFO_TYPE_PAIR_128;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700529
530 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
531
532 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700533 uuid_len == Uuid::kNumBytes16) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700534 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700535 UINT16_TO_STREAM(p, attr.uuid.As16Bit());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700536 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700537 uuid_len == Uuid::kNumBytes128) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700538 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700539 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700540 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700541 uuid_len == Uuid::kNumBytes32) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700542 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700543 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700544 } else {
545 LOG(ERROR) << "format mismatch";
546 return GATT_NO_RESOURCES;
547 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800548 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700549 p_msg->len += info_pair_len[p_msg->offset - 1];
550 len -= info_pair_len[p_msg->offset - 1];
551 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800552 }
553
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700554 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800555}
556
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700557static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
558 uint16_t& e_hdl) {
559 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700561 /* obtain starting handle, and ending handle */
562 STREAM_TO_UINT16(s_hdl, p);
563 STREAM_TO_UINT16(e_hdl, p);
564 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700566 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
567 !GATT_HANDLE_IS_VALID(e_hdl)) {
568 return GATT_INVALID_PDU;
569 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800570
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700571 return GATT_SUCCESS;
572}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800573
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700574static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700575 uint8_t*& p, Uuid* p_uuid,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700576 uint16_t& s_hdl,
577 uint16_t& e_hdl) {
578 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
579 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800580
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700581 if (len < 2) return GATT_INVALID_PDU;
582
583 /* parse uuid now */
584 CHECK(p_uuid);
585 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
586 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
587 VLOG(1) << "Bad UUID";
588 return GATT_INVALID_PDU;
589 }
590
591 len -= uuid_len;
592 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800593}
594
595/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800596 *
597 * Function gatts_process_primary_service_req
598 *
Myles Watson9ca07092016-11-28 16:41:53 -0800599 * Description Process ReadByGroupType/ReadByTypeValue request, for
600 * discovering all primary services or discover primary service
601 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800602 *
603 * Returns void
604 *
605 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700606void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800607 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800608 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700609 Uuid uuid = Uuid::kEmpty;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800610
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700611 uint8_t reason =
612 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
613 if (reason != GATT_SUCCESS) {
614 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
615 return;
616 }
617
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700618 if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700619 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
620 gatt_send_error_rsp(tcb, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700621 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s",
622 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700623 return;
624 }
625
626 // we do not support ReadByTypeValue with any non-primamry_service type
627 gatt_send_error_rsp(tcb, GATT_NOT_FOUND, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700628 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s",
629 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700630 return;
631 }
632
633 // TODO: we assume theh value is UUID, there is no such requirement in spec
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700634 Uuid value = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700635 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
636 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == false) {
637 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800638 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800639 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700641 uint16_t msg_len =
642 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
643 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
644 reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl, e_hdl,
645 p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800646 if (reason != GATT_SUCCESS) {
647 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700648 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700649 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700650 }
651
652 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800653}
654
655/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800656 *
657 * Function gatts_process_find_info
658 *
659 * Description process find information request, for discover character
660 * descriptors.
661 *
662 * Returns void
663 *
664 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700665static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800666 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700667 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700668 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700669 if (reason != GATT_SUCCESS) {
670 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
671 return;
672 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800673
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700674 uint16_t buf_len =
675 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800676
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700677 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
678 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800679
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700680 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
681 *p++ = op_code + 1;
682 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800683
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700684 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800685
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700686 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
687 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700688 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700689 if (reason == GATT_NO_RESOURCES) {
690 reason = GATT_SUCCESS;
691 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800692 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800693 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800694 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800695
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700696 *p = (uint8_t)p_msg->offset;
697
698 p_msg->offset = L2CAP_MIN_OFFSET;
699
Myles Watson911d1ae2016-11-28 16:44:40 -0800700 if (reason != GATT_SUCCESS) {
701 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700702 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800703 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700704 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800705}
706
707/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800708 *
709 * Function gatts_process_mtu_req
710 *
711 * Description This function is called to process excahnge MTU request.
712 * Only used on LE.
713 *
714 * Returns void
715 *
716 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700717static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800718 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800719 /* BR/EDR conenction, send error response */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700720 if (tcb.att_lcid != L2CAP_ATT_CID) {
721 gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700722 return;
723 }
724
725 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700726 LOG(ERROR) << "invalid MTU request PDU received.";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700727 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700728 return;
729 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800730
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700731 uint16_t mtu = 0;
732 uint8_t* p = p_data;
733 STREAM_TO_UINT16(mtu, p);
734 /* mtu must be greater than default MTU which is 23/48 */
735 if (mtu < GATT_DEF_BLE_MTU_SIZE)
736 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
737 else if (mtu > GATT_MAX_MTU_SIZE)
738 tcb.payload_size = GATT_MAX_MTU_SIZE;
739 else
740 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700741
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700742 LOG(ERROR) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800743
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700744 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
745 tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800746
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700747 BT_HDR* p_buf =
748 attp_build_sr_msg(tcb, GATT_RSP_MTU, (tGATT_SR_MSG*)&tcb.payload_size);
749 attp_send_sr_msg(tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800750
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700751 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
752 /* of 0, as no response is allowed from applcations */
753 for (int i = 0; i < GATT_MAX_APPS; i++) {
754 if (gatt_cb.cl_rcb[i].in_use) {
755 uint16_t conn_id =
756 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
757 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
758 (tGATTS_DATA*)&tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800759 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800760 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800761}
762
763/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800764 *
765 * Function gatts_process_read_by_type_req
766 *
767 * Description process Read By type request.
768 * This PDU can be used to perform:
769 * - read characteristic value
770 * - read characteristic descriptor value
771 * - discover characteristic
772 * - discover characteristic by UUID
773 * - relationship discovery
774 *
775 * Returns void
776 *
777 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700778void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800779 uint16_t len, uint8_t* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700780 Uuid uuid = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700781 uint16_t s_hdl, e_hdl, err_hdl = 0;
782 tGATT_STATUS reason =
783 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800784
Marie Janssend19e0782016-07-15 12:48:27 -0700785#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800786 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700787 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
788 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800789
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700790 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800791 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800792
Myles Watson911d1ae2016-11-28 16:44:40 -0800793 return;
794 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800795#endif
796
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700797 if (reason != GATT_SUCCESS) {
798 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
799 return;
800 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800801
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700802 size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
803 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
804 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800805
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700806 *p++ = op_code + 1;
807 /* reserve length byte */
808 p_msg->len = 2;
809 uint16_t buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800810
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700811 reason = GATT_NOT_FOUND;
812 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
813 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
814 uint8_t sec_flag, key_size;
815 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800816
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700817 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
818 tcb, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len, sec_flag,
819 key_size, 0, &err_hdl);
820 if (ret != GATT_NOT_FOUND) {
821 reason = ret;
822 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
823 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800824
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700825 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
826 s_hdl = err_hdl;
827 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800828 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800829 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800830 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700831 *p = (uint8_t)p_msg->offset;
832 p_msg->offset = L2CAP_MIN_OFFSET;
833
Myles Watson911d1ae2016-11-28 16:44:40 -0800834 if (reason != GATT_SUCCESS) {
835 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800836
Myles Watson911d1ae2016-11-28 16:44:40 -0800837 /* in theroy BUSY is not possible(should already been checked), protected
838 * check */
839 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700840 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700841
842 return;
843 }
844
845 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800846}
847
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700848/**
849 * This function is called to process the write request from client.
850 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700851void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700852 uint16_t handle, uint8_t op_code, uint16_t len,
853 uint8_t* p_data,
Myles Watson911d1ae2016-11-28 16:44:40 -0800854 bt_gatt_db_attribute_type_t gatt_type) {
855 tGATTS_DATA sr_data;
856 uint32_t trans_id;
857 tGATT_STATUS status;
858 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800859 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800860
Myles Watson911d1ae2016-11-28 16:44:40 -0800861 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800862
Myles Watson911d1ae2016-11-28 16:44:40 -0800863 switch (op_code) {
864 case GATT_REQ_PREPARE_WRITE:
865 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700866 LOG(ERROR) << __func__
867 << ": Prepare write request was invalid - missing offset, "
868 "sending error response";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700869 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800870 return;
871 }
872 sr_data.write_req.is_prep = true;
873 STREAM_TO_UINT16(sr_data.write_req.offset, p);
874 len -= 2;
875 /* fall through */
876 case GATT_SIGN_CMD_WRITE:
877 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700878 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800879 len -= GATT_AUTH_SIGN_LEN;
880 }
881 /* fall through */
882 case GATT_CMD_WRITE:
883 case GATT_REQ_WRITE:
884 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
885 sr_data.write_req.need_rsp = true;
886 sr_data.write_req.handle = handle;
887 sr_data.write_req.len = len;
888 if (len != 0 && p != NULL) {
889 memcpy(sr_data.write_req.value, p, len);
890 }
891 break;
892 }
893
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700894 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800895
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700896 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
897 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800898 sec_flag, key_size);
899
900 if (status == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700901 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800902 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700903 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800904
905 uint8_t opcode = 0;
906 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
907 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
908 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
909 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
910 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700911 LOG(ERROR) << __func__
912 << "%s: Attempt to write attribute that's not tied with"
913 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800914 status = GATT_ERROR;
915 }
916
917 if (opcode) {
918 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
919 status = GATT_PENDING;
920 }
921 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700922 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -0800923 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800925 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926
Myles Watson911d1ae2016-11-28 16:44:40 -0800927 /* in theroy BUSY is not possible(should already been checked), protected
928 * check */
929 if (status != GATT_PENDING && status != GATT_BUSY &&
930 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700931 gatt_send_error_rsp(tcb, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800932 }
933 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800934}
935
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700936/**
937 * This function is called to process the read request from client.
938 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700939static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Myles Watson911d1ae2016-11-28 16:44:40 -0800940 uint8_t op_code, uint16_t handle,
941 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700942 size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700943 uint16_t offset = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800944 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800945
Myles Watson911d1ae2016-11-28 16:44:40 -0800946 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800947
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700948 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800949 *p++ = op_code + 1;
950 p_msg->len = 1;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700951 buf_len = tcb.payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800952
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700953 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700954 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800955
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700956 uint16_t value_len = 0;
957 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700958 tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700959 sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -0800960 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800961
Myles Watson911d1ae2016-11-28 16:44:40 -0800962 if (reason != GATT_SUCCESS) {
963 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800964
Myles Watson911d1ae2016-11-28 16:44:40 -0800965 /* in theroy BUSY is not possible(should already been checked), protected
966 * check */
967 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700968 gatt_send_error_rsp(tcb, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700969
970 return;
971 }
972
973 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800974}
975
976/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800977 *
978 * Function gatts_process_attribute_req
979 *
Myles Watson9ca07092016-11-28 16:41:53 -0800980 * Description This function is called to process the per attribute handle
981 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800982 *
983 * Returns void
984 *
985 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700986void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
987 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800988 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700989 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800990 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800991
Myles Watson911d1ae2016-11-28 16:44:40 -0800992 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700993 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -0800994 status = GATT_INVALID_PDU;
995 } else {
996 STREAM_TO_UINT16(handle, p);
997 len -= 2;
998 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800999
Marie Janssend19e0782016-07-15 12:48:27 -07001000#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001001 gatt_cb.handle = handle;
1002 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001003 VLOG(1) << "Conformance tst: forced err rsp: error status="
1004 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001005
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001006 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -08001007 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001008
Myles Watson911d1ae2016-11-28 16:44:40 -08001009 return;
1010 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001011#endif
1012
Myles Watson911d1ae2016-11-28 16:44:40 -08001013 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001014 for (auto& el : *gatt_cb.srv_list_info) {
1015 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1016 for (const auto& attr : el.p_db->attr_list) {
1017 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001018 switch (op_code) {
1019 case GATT_REQ_READ: /* read char/char descriptor value */
1020 case GATT_REQ_READ_BLOB:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001021 gatts_process_read_req(tcb, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001022 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001023
Myles Watson911d1ae2016-11-28 16:44:40 -08001024 case GATT_REQ_WRITE: /* write char/char descriptor value */
1025 case GATT_CMD_WRITE:
1026 case GATT_SIGN_CMD_WRITE:
1027 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001028 gatts_process_write_req(tcb, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001029 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001030 break;
1031 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001032 break;
1033 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001034 status = GATT_SUCCESS;
1035 break;
1036 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001037 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001038 break;
1039 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001040 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001041 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001042
Myles Watson911d1ae2016-11-28 16:44:40 -08001043 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1044 op_code != GATT_SIGN_CMD_WRITE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001045 gatt_send_error_rsp(tcb, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001046}
1047
1048/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001049 *
1050 * Function gatts_proc_srv_chg_ind_ack
1051 *
1052 * Description This function process the service changed indicaiton ACK
1053 *
1054 * Returns void
1055 *
1056 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001057static void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001058 tGATTS_SRV_CHG_REQ req;
1059 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001061 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001062
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001063 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001064 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001065 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001066 p_buf->srv_changed = false;
1067 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1068 if (gatt_cb.cb_info.p_srv_chg_callback)
1069 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1070 &req, NULL);
1071 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001072}
1073
1074/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001075 *
1076 * Function gatts_chk_pending_ind
1077 *
Myles Watson9ca07092016-11-28 16:41:53 -08001078 * Description This function check any pending indication needs to be sent
1079 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001080 *
1081 * Returns void
1082 *
1083 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001084static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001085 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001086
Myles Watson911d1ae2016-11-28 16:44:40 -08001087 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001088 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001089 if (p_buf != NULL) {
1090 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1091 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001092 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001093 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001094}
1095
1096/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001097 *
1098 * Function gatts_proc_ind_ack
1099 *
Myles Watson9ca07092016-11-28 16:41:53 -08001100 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001101 *
Myles Watson9ca07092016-11-28 16:41:53 -08001102 * Returns true continue to process the indication ack by the
1103 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001104 *
1105 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001106static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001107 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001108
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001109 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001110
Myles Watson911d1ae2016-11-28 16:44:40 -08001111 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001112 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001113 /* there is no need to inform the application since srv chg is handled
1114 * internally by GATT */
1115 continue_processing = false;
1116 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001118 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001119 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001120}
1121
1122/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001123 *
1124 * Function gatts_process_value_conf
1125 *
Myles Watson9ca07092016-11-28 16:41:53 -08001126 * Description This function is called to process the handle value
1127 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001128 *
1129 * Returns void
1130 *
1131 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001132void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) {
1133 uint16_t handle = tcb.indicate_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001134
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001135 alarm_cancel(tcb.conf_timer);
1136 if (!GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001137 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001138 return;
1139 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001140
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001141 tcb.indicate_handle = 0;
1142 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1143
1144 if (continue_processing) {
1145 for (auto& el : *gatt_cb.srv_list_info) {
1146 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1147 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
1148 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1149 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
1150 (tGATTS_DATA*)&handle);
Myles Watson911d1ae2016-11-28 16:44:40 -08001151 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001152 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001153 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001154}
1155
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001156/** This function is called to handle the client requests to server */
1157void gatt_server_handle_client_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -08001158 uint16_t len, uint8_t* p_data) {
1159 /* there is pending command, discard this one */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001160 if (!gatt_sr_cmd_empty(tcb) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001161
Myles Watson911d1ae2016-11-28 16:44:40 -08001162 /* the size of the message may not be bigger than the local max PDU size*/
1163 /* The message has to be smaller than the agreed MTU, len does not include op
1164 * code */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001165 if (len >= tcb.payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001166 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
1167 len + 1, tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001168 /* for invalid request expecting response, send it now */
1169 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1170 op_code != GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001171 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001172 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001173 /* otherwise, ignore the pkt */
1174 } else {
1175 switch (op_code) {
1176 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1177 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001178 gatts_process_primary_service_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001179 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001180
Myles Watson911d1ae2016-11-28 16:44:40 -08001181 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001182 gatts_process_find_info(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001183 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001184
Myles Watson911d1ae2016-11-28 16:44:40 -08001185 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1186 value */
1187 /* discover characteristic, discover char by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001188 gatts_process_read_by_type_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001189 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001190
Myles Watson911d1ae2016-11-28 16:44:40 -08001191 case GATT_REQ_READ: /* read char/char descriptor value */
1192 case GATT_REQ_READ_BLOB:
1193 case GATT_REQ_WRITE: /* write char/char descriptor value */
1194 case GATT_CMD_WRITE:
1195 case GATT_SIGN_CMD_WRITE:
1196 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001197 gatts_process_attribute_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001198 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001199
Myles Watson911d1ae2016-11-28 16:44:40 -08001200 case GATT_HANDLE_VALUE_CONF:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001201 gatts_process_value_conf(tcb, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001202 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001203
Myles Watson911d1ae2016-11-28 16:44:40 -08001204 case GATT_REQ_MTU:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001205 gatts_process_mtu_req(tcb, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001206 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001207
Myles Watson911d1ae2016-11-28 16:44:40 -08001208 case GATT_REQ_EXEC_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001209 gatt_process_exec_write_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001210 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001211
Myles Watson911d1ae2016-11-28 16:44:40 -08001212 case GATT_REQ_READ_MULTI:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001213 gatt_process_read_multi_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001214 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001215
Myles Watson911d1ae2016-11-28 16:44:40 -08001216 default:
1217 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001218 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001219 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001220}