blob: 211e57c9b675ebf5c4a93594facef631ec520765 [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;
The Android Open Source Project5738f832012-12-12 16:00:35 -080036/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080037 *
38 * Function gatt_sr_enqueue_cmd
39 *
40 * Description This function enqueue the request from client which needs a
41 * application response, and update the transaction ID.
42 *
43 * Returns void
44 *
45 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070046uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint8_t op_code, uint16_t handle) {
47 tGATT_SR_CMD* p_cmd = &tcb.sr_cmd;
Myles Watson911d1ae2016-11-28 16:44:40 -080048 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080049
Myles Watson911d1ae2016-11-28 16:44:40 -080050 if ((p_cmd->op_code == 0) ||
51 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
52 {
53 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
54 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070055 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080056 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070057 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080058 p_cmd->op_code = op_code;
59 p_cmd->handle = handle;
60 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070061 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080062 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080063 }
Myles Watson911d1ae2016-11-28 16:44:40 -080064 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080065
Myles Watson911d1ae2016-11-28 16:44:40 -080066 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080067}
68
69/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080070 *
71 * Function gatt_sr_cmd_empty
72 *
Myles Watson9ca07092016-11-28 16:41:53 -080073 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080074 *
75 * Returns true if empty, false if there is pending command.
76 *
77 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070078bool gatt_sr_cmd_empty(tGATT_TCB& tcb) { return (tcb.sr_cmd.op_code == 0); }
The Android Open Source Project5738f832012-12-12 16:00:35 -080079
80/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080081 *
82 * Function gatt_dequeue_sr_cmd
83 *
84 * Description This function dequeue the request from command queue.
85 *
86 * Returns void
87 *
88 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070089void gatt_dequeue_sr_cmd(tGATT_TCB& tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -080090 /* Double check in case any buffers are queued */
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070091 VLOG(1) << "gatt_dequeue_sr_cmd";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070092 if (tcb.sr_cmd.p_rsp_msg)
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -070093 LOG(ERROR) << "free tcb.sr_cmd.p_rsp_msg = " << tcb.sr_cmd.p_rsp_msg;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070094 osi_free_and_reset((void**)&tcb.sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -080095
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070096 while (!fixed_queue_is_empty(tcb.sr_cmd.multi_rsp_q))
97 osi_free(fixed_queue_try_dequeue(tcb.sr_cmd.multi_rsp_q));
98 fixed_queue_free(tcb.sr_cmd.multi_rsp_q, NULL);
99 memset(&tcb.sr_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800100}
101
102/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800103 *
104 * Function process_read_multi_rsp
105 *
106 * Description This function check the read multiple response.
107 *
108 * Returns bool if all replies have been received
109 *
110 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800111static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
112 tGATTS_RSP* p_msg, uint16_t mtu) {
113 uint16_t ii, total_len, len;
114 uint8_t* p;
115 bool is_overflow = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800116
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700117 VLOG(1) << StringPrintf("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700118
Myles Watson911d1ae2016-11-28 16:44:40 -0800119 if (p_cmd->multi_rsp_q == NULL)
120 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121
Myles Watson911d1ae2016-11-28 16:44:40 -0800122 /* Enqueue the response */
123 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
124 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
125 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800126
Myles Watson911d1ae2016-11-28 16:44:40 -0800127 p_cmd->status = status;
128 if (status == GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700129 VLOG(1) << "Multi read count=" << fixed_queue_length(p_cmd->multi_rsp_q)
130 << " num_hdls=" << p_cmd->multi_req.num_handles;
Myles Watson911d1ae2016-11-28 16:44:40 -0800131 /* Wait till we get all the responses */
132 if (fixed_queue_length(p_cmd->multi_rsp_q) ==
133 p_cmd->multi_req.num_handles) {
134 len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
135 p_buf = (BT_HDR*)osi_calloc(len);
136 p_buf->offset = L2CAP_MIN_OFFSET;
137 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800138
Myles Watson911d1ae2016-11-28 16:44:40 -0800139 /* First byte in the response is the opcode */
140 *p++ = GATT_RSP_READ_MULTI;
141 p_buf->len = 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142
Myles Watson911d1ae2016-11-28 16:44:40 -0800143 /* Now walk through the buffers puting the data into the response in order
144 */
145 list_t* list = NULL;
146 const list_node_t* node = NULL;
147 if (!fixed_queue_is_empty(p_cmd->multi_rsp_q))
148 list = fixed_queue_get_list(p_cmd->multi_rsp_q);
149 for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
150 tGATTS_RSP* p_rsp = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700151
Myles Watson911d1ae2016-11-28 16:44:40 -0800152 if (list != NULL) {
153 if (ii == 0)
154 node = list_begin(list);
155 else
156 node = list_next(node);
157 if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800158 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800159
Myles Watson911d1ae2016-11-28 16:44:40 -0800160 if (p_rsp != NULL) {
161 total_len = (p_buf->len + p_rsp->attr_value.len);
162
163 if (total_len > mtu) {
164 /* just send the partial response for the overflow case */
165 len = p_rsp->attr_value.len - (total_len - mtu);
166 is_overflow = true;
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700167 VLOG(1) << StringPrintf(
168 "multi read overflow available len=%d val_len=%d", len,
169 p_rsp->attr_value.len);
Myles Watson911d1ae2016-11-28 16:44:40 -0800170 } else {
171 len = p_rsp->attr_value.len;
172 }
173
174 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
175 memcpy(p, p_rsp->attr_value.value, len);
176 if (!is_overflow) p += len;
177 p_buf->len += len;
178 } else {
179 p_cmd->status = GATT_NOT_FOUND;
180 break;
181 }
182
183 if (is_overflow) break;
184
185 } else {
186 p_cmd->status = GATT_NOT_FOUND;
187 break;
188 }
189
190 } /* loop through all handles*/
191
192 /* Sanity check on the buffer length */
193 if (p_buf->len == 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700194 LOG(ERROR) << __func__ << " nothing found!!";
Myles Watson911d1ae2016-11-28 16:44:40 -0800195 p_cmd->status = GATT_NOT_FOUND;
196 osi_free(p_buf);
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700197 VLOG(1) << __func__ << "osi_free(p_buf)";
Myles Watson911d1ae2016-11-28 16:44:40 -0800198 } else if (p_cmd->p_rsp_msg != NULL) {
199 osi_free(p_buf);
200 } else {
201 p_cmd->p_rsp_msg = p_buf;
202 }
203
204 return (true);
205 }
206 } else /* any handle read exception occurs, return error */
207 {
208 return (true);
209 }
210
211 /* If here, still waiting */
212 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800213}
214
215/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800216 *
217 * Function gatt_sr_process_app_rsp
218 *
Myles Watson9ca07092016-11-28 16:41:53 -0800219 * Description This function checks whether the response message from
220 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800221 *
222 * Returns void
223 *
224 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700225tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800226 UNUSED_ATTR uint32_t trans_id,
227 uint8_t op_code, tGATT_STATUS status,
228 tGATTS_RSP* p_msg) {
229 tGATT_STATUS ret_code = GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800230
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700231 VLOG(1) << __func__ << " gatt_if=" << +gatt_if;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800232
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700233 gatt_sr_update_cback_cnt(tcb, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234
Myles Watson911d1ae2016-11-28 16:44:40 -0800235 if (op_code == GATT_REQ_READ_MULTI) {
236 /* If no error and still waiting, just return */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700237 if (!process_read_multi_rsp(&tcb.sr_cmd, status, p_msg, tcb.payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800238 return (GATT_SUCCESS);
239 } else {
240 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700241 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800242
243 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700244 gatt_sr_reset_cback_cnt(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800245
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700246 tcb.sr_cmd.status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800247
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700248 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
249 if (tcb.sr_cmd.p_rsp_msg == NULL) {
250 tcb.sr_cmd.p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
251 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800252 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700253 LOG(ERROR) << "Exception!!! already has respond message";
Myles Watson911d1ae2016-11-28 16:44:40 -0800254 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800255 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800256 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700257 if (gatt_sr_is_cback_cnt_zero(tcb)) {
258 if ((tcb.sr_cmd.status == GATT_SUCCESS) && (tcb.sr_cmd.p_rsp_msg)) {
259 ret_code = attp_send_sr_msg(tcb, tcb.sr_cmd.p_rsp_msg);
260 tcb.sr_cmd.p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800261 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700262 ret_code =
263 gatt_send_error_rsp(tcb, status, op_code, tcb.sr_cmd.handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800264 }
265
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700266 gatt_dequeue_sr_cmd(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800267 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800268
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700269 VLOG(1) << __func__ << " ret_code=" << +ret_code;
Myles Watson911d1ae2016-11-28 16:44:40 -0800270
271 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272}
273
274/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800275 *
276 * Function gatt_process_exec_write_req
277 *
278 * Description This function is called to process the execute write request
279 * from client.
280 *
281 * Returns void
282 *
283 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700284void gatt_process_exec_write_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800285 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
286 uint8_t *p = p_data, flag, i = 0;
287 uint32_t trans_id = 0;
288 tGATT_IF gatt_if;
289 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800290
Marie Janssend19e0782016-07-15 12:48:27 -0700291#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800292 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700293 VLOG(1)
294 << "Conformance tst: forced err rspv for Execute Write: error status="
295 << +gatt_cb.err_status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800296
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700297 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800298 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800299
Myles Watson911d1ae2016-11-28 16:44:40 -0800300 return;
301 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800302#endif
303
Myles Watson911d1ae2016-11-28 16:44:40 -0800304 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800305
Myles Watson911d1ae2016-11-28 16:44:40 -0800306 /* mask the flag */
307 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308
Myles Watson911d1ae2016-11-28 16:44:40 -0800309 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700310 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
311 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, 0);
312 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800313
Myles Watson911d1ae2016-11-28 16:44:40 -0800314 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700315 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800316 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700317 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800318 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
319 (tGATTS_DATA*)&flag);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700320 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800321 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800322 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800323 } else /* nothing needs to be executed , send response now */
324 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700325 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700326 gatt_send_error_rsp(tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800327 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800328}
329
330/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800331 *
332 * Function gatt_process_read_multi_req
333 *
334 * Description This function is called to process the read multiple request
335 * from client.
336 *
337 * Returns void
338 *
339 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700340void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
341 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800342 uint32_t trans_id;
343 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700344 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800345 tGATT_STATUS err = GATT_SUCCESS;
346 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800347
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700348 VLOG(1) << __func__;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700349 tcb.sr_cmd.multi_req.num_handles = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800350
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700351 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800352
Marie Janssend19e0782016-07-15 12:48:27 -0700353#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800354 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700355 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
356 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800357
Myles Watson911d1ae2016-11-28 16:44:40 -0800358 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800359
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700360 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -0800361 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800362
Myles Watson911d1ae2016-11-28 16:44:40 -0800363 return;
364 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800365#endif
366
Myles Watson911d1ae2016-11-28 16:44:40 -0800367 while (ll >= 2 &&
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700368 tcb.sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800369 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800370
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700371 auto it = gatt_sr_find_i_rcb_by_handle(handle);
372 if (it != gatt_cb.srv_list_info->end()) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700373 tcb.sr_cmd.multi_req.handles[tcb.sr_cmd.multi_req.num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800374
375 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700376 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
377 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800378 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700379 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800380 break;
381 }
382 } else {
383 /* invalid handle */
384 err = GATT_INVALID_HANDLE;
385 break;
386 }
387 ll -= 2;
388 }
389
390 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700391 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800392 }
393
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700394 if (tcb.sr_cmd.multi_req.num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800395
396 if (err == GATT_SUCCESS) {
397 trans_id =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700398 gatt_sr_enqueue_cmd(tcb, op_code, tcb.sr_cmd.multi_req.handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800399 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700400 gatt_sr_reset_cback_cnt(tcb); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800401
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700402 for (ll = 0; ll < tcb.sr_cmd.multi_req.num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800403 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700404 handle = tcb.sr_cmd.multi_req.handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700405 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800406
Myles Watson911d1ae2016-11-28 16:44:40 -0800407 p_msg->attr_value.handle = handle;
408 err = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700409 tcb, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700410 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
411 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800412
413 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700414 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700415 GATT_SUCCESS, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800417 /* either not using or done using the buffer, release it now */
418 osi_free(p_msg);
419 }
420 } else
421 err = GATT_NO_RESOURCES;
422 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800423
Myles Watson911d1ae2016-11-28 16:44:40 -0800424 /* in theroy BUSY is not possible(should already been checked), protected
425 * check */
426 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700427 gatt_send_error_rsp(tcb, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800428}
429
430/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800431 *
432 * Function gatt_build_primary_service_rsp
433 *
434 * Description Primamry service request processed internally. Theretically
435 * only deal with ReadByTypeVAlue and ReadByGroupType.
436 *
437 * Returns void
438 *
439 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800440static tGATT_STATUS gatt_build_primary_service_rsp(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700441 BT_HDR* p_msg, tGATT_TCB& tcb, uint8_t op_code, uint16_t s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800442 uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, tBT_UUID value) {
443 tGATT_STATUS status = GATT_NOT_FOUND;
444 uint8_t handle_len = 4, *p;
Myles Watson911d1ae2016-11-28 16:44:40 -0800445 tBT_UUID* p_uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800446
Myles Watson911d1ae2016-11-28 16:44:40 -0800447 p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800448
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700449 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
450 if (el.s_hdl >= s_hdl && el.s_hdl <= e_hdl &&
451 el.type == GATT_UUID_PRI_SERVICE) {
452 p_uuid = gatts_get_service_uuid(el.p_db);
Myles Watson911d1ae2016-11-28 16:44:40 -0800453 if (p_uuid != NULL) {
454 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) handle_len = 4 + p_uuid->len;
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700455
Myles Watson911d1ae2016-11-28 16:44:40 -0800456 /* get the length byte in the repsonse */
457 if (p_msg->offset == 0) {
458 *p++ = op_code + 1;
459 p_msg->len++;
460 p_msg->offset = handle_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461
Myles Watson911d1ae2016-11-28 16:44:40 -0800462 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
463 *p++ = (uint8_t)p_msg->offset; /* length byte */
464 p_msg->len++;
465 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800466 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700468 if (p_msg->len + p_msg->offset <= tcb.payload_size &&
Myles Watson911d1ae2016-11-28 16:44:40 -0800469 handle_len == p_msg->offset) {
470 if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
471 gatt_uuid_compare(value, *p_uuid)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700472 UINT16_TO_STREAM(p, el.s_hdl);
Myles Watson911d1ae2016-11-28 16:44:40 -0800473
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700474 if (gatt_cb.last_primary_s_handle &&
475 gatt_cb.last_primary_s_handle == el.s_hdl) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700476 VLOG(1) << "Use 0xFFFF for the last primary attribute";
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700477 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
478 UINT16_TO_STREAM(p, 0xFFFF);
Myles Watson911d1ae2016-11-28 16:44:40 -0800479 } else {
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700480 UINT16_TO_STREAM(p, el.e_hdl);
Myles Watson911d1ae2016-11-28 16:44:40 -0800481 }
482
483 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
484 gatt_build_uuid_to_stream(&p, *p_uuid);
485
486 status = GATT_SUCCESS;
487 p_msg->len += p_msg->offset;
488 }
489 } else
490 break;
491 }
492 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800493 }
494 p_msg->offset = L2CAP_MIN_OFFSET;
495
496 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497}
498
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700499/**
500 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800501 *
502 * Returns true: if data filled sucessfully.
503 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700504 */
505static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700506 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700507 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800508 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800509
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700510 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800511
Myles Watson911d1ae2016-11-28 16:44:40 -0800512 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800513
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700514 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700516 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700517 if (attr.handle > e_hdl) break;
518
519 if (attr.handle < s_hdl) continue;
520
521 if (p_msg->offset == 0)
522 p_msg->offset = (attr.uuid.len == LEN_UUID_16) ? GATT_INFO_TYPE_PAIR_16
523 : GATT_INFO_TYPE_PAIR_128;
524
525 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
526
527 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
528 attr.uuid.len == LEN_UUID_16) {
529 UINT16_TO_STREAM(p, attr.handle);
530 UINT16_TO_STREAM(p, attr.uuid.uu.uuid16);
531 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
532 attr.uuid.len == LEN_UUID_128) {
533 UINT16_TO_STREAM(p, attr.handle);
534 ARRAY_TO_STREAM(p, attr.uuid.uu.uuid128, LEN_UUID_128);
535 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
536 attr.uuid.len == LEN_UUID_32) {
537 UINT16_TO_STREAM(p, attr.handle);
538 gatt_convert_uuid32_to_uuid128(p, attr.uuid.uu.uuid32);
539 p += LEN_UUID_128;
540 } else {
541 LOG(ERROR) << "format mismatch";
542 return GATT_NO_RESOURCES;
543 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800544 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700545 p_msg->len += info_pair_len[p_msg->offset - 1];
546 len -= info_pair_len[p_msg->offset - 1];
547 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800548 }
549
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700550 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800551}
552
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700553static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
554 uint16_t& e_hdl) {
555 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800556
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700557 /* obtain starting handle, and ending handle */
558 STREAM_TO_UINT16(s_hdl, p);
559 STREAM_TO_UINT16(e_hdl, p);
560 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700562 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
563 !GATT_HANDLE_IS_VALID(e_hdl)) {
564 return GATT_INVALID_PDU;
565 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800566
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700567 return GATT_SUCCESS;
568}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700570static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
571 uint8_t*& p, tBT_UUID* p_uuid,
572 uint16_t& s_hdl,
573 uint16_t& e_hdl) {
574 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
575 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700577 if (len < 2) return GATT_INVALID_PDU;
578
579 /* parse uuid now */
580 CHECK(p_uuid);
581 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
582 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
583 VLOG(1) << "Bad UUID";
584 return GATT_INVALID_PDU;
585 }
586
587 len -= uuid_len;
588 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800589}
590
591/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800592 *
593 * Function gatts_process_primary_service_req
594 *
Myles Watson9ca07092016-11-28 16:41:53 -0800595 * Description Process ReadByGroupType/ReadByTypeValue request, for
596 * discovering all primary services or discover primary service
597 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800598 *
599 * Returns void
600 *
601 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700602void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800603 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800604 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700605 tBT_UUID uuid;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800606
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700607 uint8_t reason =
608 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
609 if (reason != GATT_SUCCESS) {
610 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
611 return;
612 }
613
614 tBT_UUID primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
615 if (!gatt_uuid_compare(uuid, primary_service)) {
616 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
617 gatt_send_error_rsp(tcb, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl, false);
618 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: 0x%04x",
619 uuid.uu.uuid16);
620 return;
621 }
622
623 // we do not support ReadByTypeValue with any non-primamry_service type
624 gatt_send_error_rsp(tcb, GATT_NOT_FOUND, op_code, s_hdl, false);
625 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: 0x%04x",
626 uuid.uu.uuid16);
627 return;
628 }
629
630 // TODO: we assume theh value is UUID, there is no such requirement in spec
631 tBT_UUID value;
Myles Watson911d1ae2016-11-28 16:44:40 -0800632 memset(&value, 0, sizeof(tBT_UUID));
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700633 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
634 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == false) {
635 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800636 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800637 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800638
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700639 uint16_t msg_len =
640 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
641 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
642 reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl, e_hdl,
643 p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800644 if (reason != GATT_SUCCESS) {
645 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700646 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700647 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700648 }
649
650 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800651}
652
653/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800654 *
655 * Function gatts_process_find_info
656 *
657 * Description process find information request, for discover character
658 * descriptors.
659 *
660 * Returns void
661 *
662 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700663static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800664 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700665 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700666 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700667 if (reason != GATT_SUCCESS) {
668 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
669 return;
670 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800671
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700672 uint16_t buf_len =
673 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800674
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700675 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
676 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800677
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700678 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
679 *p++ = op_code + 1;
680 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800681
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700682 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800683
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700684 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
685 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700686 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700687 if (reason == GATT_NO_RESOURCES) {
688 reason = GATT_SUCCESS;
689 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800690 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800691 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800692 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800693
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700694 *p = (uint8_t)p_msg->offset;
695
696 p_msg->offset = L2CAP_MIN_OFFSET;
697
Myles Watson911d1ae2016-11-28 16:44:40 -0800698 if (reason != GATT_SUCCESS) {
699 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700700 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800701 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700702 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800703}
704
705/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800706 *
707 * Function gatts_process_mtu_req
708 *
709 * Description This function is called to process excahnge MTU request.
710 * Only used on LE.
711 *
712 * Returns void
713 *
714 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700715static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800716 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800717 /* BR/EDR conenction, send error response */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700718 if (tcb.att_lcid != L2CAP_ATT_CID) {
719 gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700720 return;
721 }
722
723 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700724 LOG(ERROR) << "invalid MTU request PDU received.";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700725 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700726 return;
727 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800728
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700729 uint16_t mtu = 0;
730 uint8_t* p = p_data;
731 STREAM_TO_UINT16(mtu, p);
732 /* mtu must be greater than default MTU which is 23/48 */
733 if (mtu < GATT_DEF_BLE_MTU_SIZE)
734 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
735 else if (mtu > GATT_MAX_MTU_SIZE)
736 tcb.payload_size = GATT_MAX_MTU_SIZE;
737 else
738 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700739
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700740 LOG(ERROR) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800741
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700742 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
743 tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800744
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700745 BT_HDR* p_buf =
746 attp_build_sr_msg(tcb, GATT_RSP_MTU, (tGATT_SR_MSG*)&tcb.payload_size);
747 attp_send_sr_msg(tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800748
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700749 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
750 /* of 0, as no response is allowed from applcations */
751 for (int i = 0; i < GATT_MAX_APPS; i++) {
752 if (gatt_cb.cl_rcb[i].in_use) {
753 uint16_t conn_id =
754 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
755 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
756 (tGATTS_DATA*)&tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800757 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800758 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800759}
760
761/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800762 *
763 * Function gatts_process_read_by_type_req
764 *
765 * Description process Read By type request.
766 * This PDU can be used to perform:
767 * - read characteristic value
768 * - read characteristic descriptor value
769 * - discover characteristic
770 * - discover characteristic by UUID
771 * - relationship discovery
772 *
773 * Returns void
774 *
775 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700776void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800777 uint16_t len, uint8_t* p_data) {
778 tBT_UUID uuid;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700779 uint16_t s_hdl, e_hdl, err_hdl = 0;
780 tGATT_STATUS reason =
781 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800782
Marie Janssend19e0782016-07-15 12:48:27 -0700783#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800784 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700785 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
786 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800787
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700788 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800789 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790
Myles Watson911d1ae2016-11-28 16:44:40 -0800791 return;
792 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800793#endif
794
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700795 if (reason != GATT_SUCCESS) {
796 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
797 return;
798 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800799
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700800 size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
801 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
802 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800803
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700804 *p++ = op_code + 1;
805 /* reserve length byte */
806 p_msg->len = 2;
807 uint16_t buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700809 reason = GATT_NOT_FOUND;
810 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
811 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
812 uint8_t sec_flag, key_size;
813 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800814
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700815 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
816 tcb, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len, sec_flag,
817 key_size, 0, &err_hdl);
818 if (ret != GATT_NOT_FOUND) {
819 reason = ret;
820 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
821 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800822
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700823 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
824 s_hdl = err_hdl;
825 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800826 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800827 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800828 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700829 *p = (uint8_t)p_msg->offset;
830 p_msg->offset = L2CAP_MIN_OFFSET;
831
Myles Watson911d1ae2016-11-28 16:44:40 -0800832 if (reason != GATT_SUCCESS) {
833 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800834
Myles Watson911d1ae2016-11-28 16:44:40 -0800835 /* in theroy BUSY is not possible(should already been checked), protected
836 * check */
837 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700838 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700839
840 return;
841 }
842
843 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800844}
845
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700846/**
847 * This function is called to process the write request from client.
848 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700849void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700850 uint16_t handle, uint8_t op_code, uint16_t len,
851 uint8_t* p_data,
Myles Watson911d1ae2016-11-28 16:44:40 -0800852 bt_gatt_db_attribute_type_t gatt_type) {
853 tGATTS_DATA sr_data;
854 uint32_t trans_id;
855 tGATT_STATUS status;
856 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800857 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800858
Myles Watson911d1ae2016-11-28 16:44:40 -0800859 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800860
Myles Watson911d1ae2016-11-28 16:44:40 -0800861 switch (op_code) {
862 case GATT_REQ_PREPARE_WRITE:
863 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700864 LOG(ERROR) << __func__
865 << ": Prepare write request was invalid - missing offset, "
866 "sending error response";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700867 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800868 return;
869 }
870 sr_data.write_req.is_prep = true;
871 STREAM_TO_UINT16(sr_data.write_req.offset, p);
872 len -= 2;
873 /* fall through */
874 case GATT_SIGN_CMD_WRITE:
875 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700876 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800877 len -= GATT_AUTH_SIGN_LEN;
878 }
879 /* fall through */
880 case GATT_CMD_WRITE:
881 case GATT_REQ_WRITE:
882 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
883 sr_data.write_req.need_rsp = true;
884 sr_data.write_req.handle = handle;
885 sr_data.write_req.len = len;
886 if (len != 0 && p != NULL) {
887 memcpy(sr_data.write_req.value, p, len);
888 }
889 break;
890 }
891
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700892 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800893
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700894 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
895 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800896 sec_flag, key_size);
897
898 if (status == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700899 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800900 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700901 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800902
903 uint8_t opcode = 0;
904 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
905 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
906 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
907 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
908 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700909 LOG(ERROR) << __func__
910 << "%s: Attempt to write attribute that's not tied with"
911 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800912 status = GATT_ERROR;
913 }
914
915 if (opcode) {
916 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
917 status = GATT_PENDING;
918 }
919 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700920 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -0800921 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800922 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800923 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924
Myles Watson911d1ae2016-11-28 16:44:40 -0800925 /* in theroy BUSY is not possible(should already been checked), protected
926 * check */
927 if (status != GATT_PENDING && status != GATT_BUSY &&
928 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700929 gatt_send_error_rsp(tcb, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800930 }
931 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800932}
933
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700934/**
935 * This function is called to process the read request from client.
936 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700937static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Myles Watson911d1ae2016-11-28 16:44:40 -0800938 uint8_t op_code, uint16_t handle,
939 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700940 size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700941 uint16_t offset = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800942 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800943
Myles Watson911d1ae2016-11-28 16:44:40 -0800944 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800945
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700946 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800947 *p++ = op_code + 1;
948 p_msg->len = 1;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700949 buf_len = tcb.payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800950
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700951 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700952 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800953
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700954 uint16_t value_len = 0;
955 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700956 tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700957 sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -0800958 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800959
Myles Watson911d1ae2016-11-28 16:44:40 -0800960 if (reason != GATT_SUCCESS) {
961 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800962
Myles Watson911d1ae2016-11-28 16:44:40 -0800963 /* in theroy BUSY is not possible(should already been checked), protected
964 * check */
965 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700966 gatt_send_error_rsp(tcb, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700967
968 return;
969 }
970
971 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800972}
973
974/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800975 *
976 * Function gatts_process_attribute_req
977 *
Myles Watson9ca07092016-11-28 16:41:53 -0800978 * Description This function is called to process the per attribute handle
979 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800980 *
981 * Returns void
982 *
983 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700984void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
985 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800986 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700987 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800988 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800989
Myles Watson911d1ae2016-11-28 16:44:40 -0800990 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700991 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -0800992 status = GATT_INVALID_PDU;
993 } else {
994 STREAM_TO_UINT16(handle, p);
995 len -= 2;
996 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800997
Marie Janssend19e0782016-07-15 12:48:27 -0700998#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800999 gatt_cb.handle = handle;
1000 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001001 VLOG(1) << "Conformance tst: forced err rsp: error status="
1002 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001003
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001004 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -08001005 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001006
Myles Watson911d1ae2016-11-28 16:44:40 -08001007 return;
1008 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001009#endif
1010
Myles Watson911d1ae2016-11-28 16:44:40 -08001011 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001012 for (auto& el : *gatt_cb.srv_list_info) {
1013 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1014 for (const auto& attr : el.p_db->attr_list) {
1015 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001016 switch (op_code) {
1017 case GATT_REQ_READ: /* read char/char descriptor value */
1018 case GATT_REQ_READ_BLOB:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001019 gatts_process_read_req(tcb, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001020 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001021
Myles Watson911d1ae2016-11-28 16:44:40 -08001022 case GATT_REQ_WRITE: /* write char/char descriptor value */
1023 case GATT_CMD_WRITE:
1024 case GATT_SIGN_CMD_WRITE:
1025 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001026 gatts_process_write_req(tcb, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001027 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001028 break;
1029 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001030 break;
1031 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001032 status = GATT_SUCCESS;
1033 break;
1034 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001035 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001036 break;
1037 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001038 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001039 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001040
Myles Watson911d1ae2016-11-28 16:44:40 -08001041 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1042 op_code != GATT_SIGN_CMD_WRITE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001043 gatt_send_error_rsp(tcb, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001044}
1045
1046/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001047 *
1048 * Function gatts_proc_srv_chg_ind_ack
1049 *
1050 * Description This function process the service changed indicaiton ACK
1051 *
1052 * Returns void
1053 *
1054 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001055static void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001056 tGATTS_SRV_CHG_REQ req;
1057 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001058
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001059 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001061 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001062 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001063 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001064 p_buf->srv_changed = false;
1065 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1066 if (gatt_cb.cb_info.p_srv_chg_callback)
1067 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1068 &req, NULL);
1069 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001070}
1071
1072/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001073 *
1074 * Function gatts_chk_pending_ind
1075 *
Myles Watson9ca07092016-11-28 16:41:53 -08001076 * Description This function check any pending indication needs to be sent
1077 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001078 *
1079 * Returns void
1080 *
1081 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001082static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001083 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001084
Myles Watson911d1ae2016-11-28 16:44:40 -08001085 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001086 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001087 if (p_buf != NULL) {
1088 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1089 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001090 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001091 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001092}
1093
1094/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001095 *
1096 * Function gatts_proc_ind_ack
1097 *
Myles Watson9ca07092016-11-28 16:41:53 -08001098 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001099 *
Myles Watson9ca07092016-11-28 16:41:53 -08001100 * Returns true continue to process the indication ack by the
1101 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001102 *
1103 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001104static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001105 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001106
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001107 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001108
Myles Watson911d1ae2016-11-28 16:44:40 -08001109 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001110 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001111 /* there is no need to inform the application since srv chg is handled
1112 * internally by GATT */
1113 continue_processing = false;
1114 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001115
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001116 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001117 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001118}
1119
1120/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001121 *
1122 * Function gatts_process_value_conf
1123 *
Myles Watson9ca07092016-11-28 16:41:53 -08001124 * Description This function is called to process the handle value
1125 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001126 *
1127 * Returns void
1128 *
1129 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001130void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) {
1131 uint16_t handle = tcb.indicate_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001133 alarm_cancel(tcb.conf_timer);
1134 if (!GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001135 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001136 return;
1137 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001138
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001139 tcb.indicate_handle = 0;
1140 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1141
1142 if (continue_processing) {
1143 for (auto& el : *gatt_cb.srv_list_info) {
1144 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1145 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
1146 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1147 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
1148 (tGATTS_DATA*)&handle);
Myles Watson911d1ae2016-11-28 16:44:40 -08001149 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001150 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001151 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001152}
1153
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001154/** This function is called to handle the client requests to server */
1155void gatt_server_handle_client_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -08001156 uint16_t len, uint8_t* p_data) {
1157 /* there is pending command, discard this one */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001158 if (!gatt_sr_cmd_empty(tcb) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001159
Myles Watson911d1ae2016-11-28 16:44:40 -08001160 /* the size of the message may not be bigger than the local max PDU size*/
1161 /* The message has to be smaller than the agreed MTU, len does not include op
1162 * code */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001163 if (len >= tcb.payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001164 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
1165 len + 1, tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001166 /* for invalid request expecting response, send it now */
1167 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1168 op_code != GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001169 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001170 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001171 /* otherwise, ignore the pkt */
1172 } else {
1173 switch (op_code) {
1174 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1175 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001176 gatts_process_primary_service_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001177 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001178
Myles Watson911d1ae2016-11-28 16:44:40 -08001179 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001180 gatts_process_find_info(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001181 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001182
Myles Watson911d1ae2016-11-28 16:44:40 -08001183 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1184 value */
1185 /* discover characteristic, discover char by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001186 gatts_process_read_by_type_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001187 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001188
Myles Watson911d1ae2016-11-28 16:44:40 -08001189 case GATT_REQ_READ: /* read char/char descriptor value */
1190 case GATT_REQ_READ_BLOB:
1191 case GATT_REQ_WRITE: /* write char/char descriptor value */
1192 case GATT_CMD_WRITE:
1193 case GATT_SIGN_CMD_WRITE:
1194 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001195 gatts_process_attribute_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001196 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197
Myles Watson911d1ae2016-11-28 16:44:40 -08001198 case GATT_HANDLE_VALUE_CONF:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001199 gatts_process_value_conf(tcb, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001200 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001201
Myles Watson911d1ae2016-11-28 16:44:40 -08001202 case GATT_REQ_MTU:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001203 gatts_process_mtu_req(tcb, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001204 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001205
Myles Watson911d1ae2016-11-28 16:44:40 -08001206 case GATT_REQ_EXEC_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001207 gatt_process_exec_write_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001208 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001209
Myles Watson911d1ae2016-11-28 16:44:40 -08001210 case GATT_REQ_READ_MULTI:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001211 gatt_process_read_multi_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001212 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001213
Myles Watson911d1ae2016-11-28 16:44:40 -08001214 default:
1215 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001216 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001217 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001218}