blob: 04eb8c587cde8b49fd59eb0d56ed54aed9143d5a [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2008-2012 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 * this file contains the GATT server functions
22 *
23 ******************************************************************************/
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 Watson8d749042017-09-19 10:01:28 -0700320 tGATTS_DATA gatts_data;
321 gatts_data.exec_write = flag;
Myles Watson911d1ae2016-11-28 16:44:40 -0800322 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
Myles Watson8d749042017-09-19 10:01:28 -0700323 &gatts_data);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700324 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800325 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800326 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800327 } else /* nothing needs to be executed , send response now */
328 {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700329 LOG(ERROR) << "gatt_process_exec_write_req: no prepare write pending";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700330 gatt_send_error_rsp(tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800331 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800332}
333
334/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800335 *
336 * Function gatt_process_read_multi_req
337 *
338 * Description This function is called to process the read multiple request
339 * from client.
340 *
341 * Returns void
342 *
343 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700344void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
345 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800346 uint32_t trans_id;
347 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700348 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800349 tGATT_STATUS err = GATT_SUCCESS;
350 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800351
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700352 VLOG(1) << __func__;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700353 tcb.sr_cmd.multi_req.num_handles = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800354
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700355 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356
Marie Janssend19e0782016-07-15 12:48:27 -0700357#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800358 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700359 VLOG(1) << "Conformance tst: forced err rspvofr ReadMultiple: error status="
360 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800361
Myles Watson911d1ae2016-11-28 16:44:40 -0800362 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800363
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700364 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -0800365 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800366
Myles Watson911d1ae2016-11-28 16:44:40 -0800367 return;
368 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369#endif
370
Myles Watson911d1ae2016-11-28 16:44:40 -0800371 while (ll >= 2 &&
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700372 tcb.sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800373 STREAM_TO_UINT16(handle, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800374
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700375 auto it = gatt_sr_find_i_rcb_by_handle(handle);
376 if (it != gatt_cb.srv_list_info->end()) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700377 tcb.sr_cmd.multi_req.handles[tcb.sr_cmd.multi_req.num_handles++] = handle;
Myles Watson911d1ae2016-11-28 16:44:40 -0800378
379 /* check read permission */
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700380 err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag,
381 key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800382 if (err != GATT_SUCCESS) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700383 VLOG(1) << StringPrintf("read permission denied : 0x%02x", err);
Myles Watson911d1ae2016-11-28 16:44:40 -0800384 break;
385 }
386 } else {
387 /* invalid handle */
388 err = GATT_INVALID_HANDLE;
389 break;
390 }
391 ll -= 2;
392 }
393
394 if (ll != 0) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700395 LOG(ERROR) << "max attribute handle reached in ReadMultiple Request.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800396 }
397
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700398 if (tcb.sr_cmd.multi_req.num_handles == 0) err = GATT_INVALID_HANDLE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800399
400 if (err == GATT_SUCCESS) {
401 trans_id =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700402 gatt_sr_enqueue_cmd(tcb, op_code, tcb.sr_cmd.multi_req.handles[0]);
Myles Watson911d1ae2016-11-28 16:44:40 -0800403 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700404 gatt_sr_reset_cback_cnt(tcb); /* read multiple use multi_rsp_q's count*/
Myles Watson911d1ae2016-11-28 16:44:40 -0800405
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700406 for (ll = 0; ll < tcb.sr_cmd.multi_req.num_handles; ll++) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800407 tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP));
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700408 handle = tcb.sr_cmd.multi_req.handles[ll];
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700409 auto it = gatt_sr_find_i_rcb_by_handle(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410
Myles Watson911d1ae2016-11-28 16:44:40 -0800411 p_msg->attr_value.handle = handle;
412 err = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700413 tcb, it->p_db, op_code, handle, 0, p_msg->attr_value.value,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700414 &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size,
415 trans_id);
Myles Watson911d1ae2016-11-28 16:44:40 -0800416
417 if (err == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700418 gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700419 GATT_SUCCESS, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800420 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800421 /* either not using or done using the buffer, release it now */
422 osi_free(p_msg);
423 }
424 } else
425 err = GATT_NO_RESOURCES;
426 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800427
Myles Watson911d1ae2016-11-28 16:44:40 -0800428 /* in theroy BUSY is not possible(should already been checked), protected
429 * check */
430 if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700431 gatt_send_error_rsp(tcb, err, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800432}
433
434/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800435 *
436 * Function gatt_build_primary_service_rsp
437 *
438 * Description Primamry service request processed internally. Theretically
439 * only deal with ReadByTypeVAlue and ReadByGroupType.
440 *
441 * Returns void
442 *
443 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800444static tGATT_STATUS gatt_build_primary_service_rsp(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700445 BT_HDR* p_msg, tGATT_TCB& tcb, uint8_t op_code, uint16_t s_hdl,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700446 uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, const Uuid& value) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800447 tGATT_STATUS status = GATT_NOT_FOUND;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700448 uint8_t handle_len = 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700450 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800451
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700452 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700453 if (el.s_hdl < s_hdl || el.s_hdl > e_hdl ||
454 el.type != GATT_UUID_PRI_SERVICE) {
455 continue;
456 }
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700457
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700458 Uuid* p_uuid = gatts_get_service_uuid(el.p_db);
459 if (!p_uuid) continue;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800460
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700461 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
Jakub Pawlowskif107e4f2018-04-12 05:42:31 -0700462 handle_len = 4 + gatt_build_uuid_to_stream_len(*p_uuid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800463
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700464 /* get the length byte in the repsonse */
465 if (p_msg->offset == 0) {
466 *p++ = op_code + 1;
467 p_msg->len++;
468 p_msg->offset = handle_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800469
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700470 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
471 *p++ = (uint8_t)p_msg->offset; /* length byte */
472 p_msg->len++;
Myles Watson911d1ae2016-11-28 16:44:40 -0800473 }
474 }
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700475
476 if (p_msg->len + p_msg->offset > tcb.payload_size ||
477 handle_len != p_msg->offset) {
478 break;
479 }
480
481 if (op_code == GATT_REQ_FIND_TYPE_VALUE && value != *p_uuid) continue;
482
483 UINT16_TO_STREAM(p, el.s_hdl);
484
Jakub Pawlowski4c6007c2018-04-16 07:55:06 -0700485 if (gatt_cb.last_service_handle &&
486 gatt_cb.last_service_handle == el.s_hdl) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700487 VLOG(1) << "Use 0xFFFF for the last primary attribute";
488 /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */
489 UINT16_TO_STREAM(p, 0xFFFF);
490 } else {
491 UINT16_TO_STREAM(p, el.e_hdl);
492 }
493
494 if (op_code == GATT_REQ_READ_BY_GRP_TYPE)
495 gatt_build_uuid_to_stream(&p, *p_uuid);
496
497 status = GATT_SUCCESS;
498 p_msg->len += p_msg->offset;
Myles Watson911d1ae2016-11-28 16:44:40 -0800499 }
500 p_msg->offset = L2CAP_MIN_OFFSET;
501
502 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800503}
504
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700505/**
506 * fill the find information response information in the given buffer.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800507 *
508 * Returns true: if data filled sucessfully.
509 * false: packet full, or format mismatch.
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700510 */
511static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700512 BT_HDR* p_msg, uint16_t& len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700513 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800514 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700516 if (!el.p_db) return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800517
Myles Watson911d1ae2016-11-28 16:44:40 -0800518 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800519
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700520 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800521
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700522 for (auto& attr : el.p_db->attr_list) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700523 if (attr.handle > e_hdl) break;
524
525 if (attr.handle < s_hdl) continue;
526
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700527 uint8_t uuid_len = attr.uuid.GetShortestRepresentationSize();
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700528 if (p_msg->offset == 0)
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700529 p_msg->offset = (uuid_len == Uuid::kNumBytes16) ? GATT_INFO_TYPE_PAIR_16
530 : GATT_INFO_TYPE_PAIR_128;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700531
532 if (len < info_pair_len[p_msg->offset - 1]) return GATT_NO_RESOURCES;
533
534 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700535 uuid_len == Uuid::kNumBytes16) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700536 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700537 UINT16_TO_STREAM(p, attr.uuid.As16Bit());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700538 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700539 uuid_len == Uuid::kNumBytes128) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700540 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700541 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700542 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700543 uuid_len == Uuid::kNumBytes32) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700544 UINT16_TO_STREAM(p, attr.handle);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700545 ARRAY_TO_STREAM(p, attr.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700546 } else {
547 LOG(ERROR) << "format mismatch";
548 return GATT_NO_RESOURCES;
549 /* format mismatch */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800550 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700551 p_msg->len += info_pair_len[p_msg->offset - 1];
552 len -= info_pair_len[p_msg->offset - 1];
553 return GATT_SUCCESS;
Myles Watson911d1ae2016-11-28 16:44:40 -0800554 }
555
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700556 return GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800557}
558
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700559static tGATT_STATUS read_handles(uint16_t& len, uint8_t*& p, uint16_t& s_hdl,
560 uint16_t& e_hdl) {
561 if (len < 4) return GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800562
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700563 /* obtain starting handle, and ending handle */
564 STREAM_TO_UINT16(s_hdl, p);
565 STREAM_TO_UINT16(e_hdl, p);
566 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700568 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
569 !GATT_HANDLE_IS_VALID(e_hdl)) {
Stanley Tngbe701122018-05-07 14:58:36 -0700570 return GATT_INVALID_HANDLE;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700571 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800572
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700573 return GATT_SUCCESS;
574}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800575
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700576static tGATT_STATUS gatts_validate_packet_format(uint8_t op_code, uint16_t& len,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700577 uint8_t*& p, Uuid* p_uuid,
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700578 uint16_t& s_hdl,
579 uint16_t& e_hdl) {
580 tGATT_STATUS ret = read_handles(len, p, s_hdl, e_hdl);
581 if (ret != GATT_SUCCESS) return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800582
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700583 if (len < 2) return GATT_INVALID_PDU;
584
585 /* parse uuid now */
586 CHECK(p_uuid);
587 uint16_t uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
588 if (!gatt_parse_uuid_from_cmd(p_uuid, uuid_len, &p)) {
589 VLOG(1) << "Bad UUID";
590 return GATT_INVALID_PDU;
591 }
592
593 len -= uuid_len;
594 return GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800595}
596
597/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800598 *
599 * Function gatts_process_primary_service_req
600 *
Myles Watson9ca07092016-11-28 16:41:53 -0800601 * Description Process ReadByGroupType/ReadByTypeValue request, for
602 * discovering all primary services or discover primary service
603 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800604 *
605 * Returns void
606 *
607 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700608void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800609 uint16_t len, uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800610 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700611 Uuid uuid = Uuid::kEmpty;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700613 uint8_t reason =
614 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
615 if (reason != GATT_SUCCESS) {
616 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
617 return;
618 }
619
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700620 if (uuid != Uuid::From16Bit(GATT_UUID_PRI_SERVICE)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700621 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
622 gatt_send_error_rsp(tcb, GATT_UNSUPPORT_GRP_TYPE, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700623 VLOG(1) << StringPrintf("unexpected ReadByGrpType Group: %s",
624 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700625 return;
626 }
627
628 // we do not support ReadByTypeValue with any non-primamry_service type
629 gatt_send_error_rsp(tcb, GATT_NOT_FOUND, op_code, s_hdl, false);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700630 VLOG(1) << StringPrintf("unexpected ReadByTypeValue type: %s",
631 uuid.ToString().c_str());
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700632 return;
633 }
634
635 // TODO: we assume theh value is UUID, there is no such requirement in spec
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700636 Uuid value = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700637 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
Myles Watson5d5fcf22017-10-06 16:51:21 -0700638 if (!gatt_parse_uuid_from_cmd(&value, len, &p_data)) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700639 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, s_hdl, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800641 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800642
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700643 uint16_t msg_len =
644 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
645 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
646 reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl, e_hdl,
647 p_data, value);
Myles Watson911d1ae2016-11-28 16:44:40 -0800648 if (reason != GATT_SUCCESS) {
649 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700650 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jack He882aec32017-08-14 23:02:16 -0700651 return;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700652 }
653
654 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800655}
656
657/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800658 *
659 * Function gatts_process_find_info
660 *
661 * Description process find information request, for discover character
662 * descriptors.
663 *
664 * Returns void
665 *
666 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700667static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800668 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700669 uint16_t s_hdl = 0, e_hdl = 0;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700670 uint8_t reason = read_handles(len, p_data, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700671 if (reason != GATT_SUCCESS) {
672 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
673 return;
674 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800675
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700676 uint16_t buf_len =
677 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800678
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700679 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
680 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800681
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700682 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
683 *p++ = op_code + 1;
684 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800685
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700686 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800687
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700688 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
689 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700690 reason = gatt_build_find_info_rsp(el, p_msg, buf_len, s_hdl, e_hdl);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700691 if (reason == GATT_NO_RESOURCES) {
692 reason = GATT_SUCCESS;
693 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800694 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800695 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800696 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800697
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700698 *p = (uint8_t)p_msg->offset;
699
700 p_msg->offset = L2CAP_MIN_OFFSET;
701
Myles Watson911d1ae2016-11-28 16:44:40 -0800702 if (reason != GATT_SUCCESS) {
703 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700704 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800705 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700706 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800707}
708
709/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800710 *
711 * Function gatts_process_mtu_req
712 *
713 * Description This function is called to process excahnge MTU request.
714 * Only used on LE.
715 *
716 * Returns void
717 *
718 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700719static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800720 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800721 /* BR/EDR conenction, send error response */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700722 if (tcb.att_lcid != L2CAP_ATT_CID) {
723 gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700724 return;
725 }
726
727 if (len < GATT_MTU_REQ_MIN_LEN) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700728 LOG(ERROR) << "invalid MTU request PDU received.";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700729 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700730 return;
731 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800732
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700733 uint16_t mtu = 0;
734 uint8_t* p = p_data;
735 STREAM_TO_UINT16(mtu, p);
736 /* mtu must be greater than default MTU which is 23/48 */
737 if (mtu < GATT_DEF_BLE_MTU_SIZE)
738 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
739 else if (mtu > GATT_MAX_MTU_SIZE)
740 tcb.payload_size = GATT_MAX_MTU_SIZE;
741 else
742 tcb.payload_size = mtu;
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700743
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700744 LOG(ERROR) << "MTU request PDU with MTU size " << +tcb.payload_size;
Priti Aghera636d6712014-12-18 13:55:48 -0800745
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700746 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
747 tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800748
Myles Watson8d749042017-09-19 10:01:28 -0700749 tGATT_SR_MSG gatt_sr_msg;
750 gatt_sr_msg.mtu = tcb.payload_size;
751 BT_HDR* p_buf = attp_build_sr_msg(tcb, GATT_RSP_MTU, &gatt_sr_msg);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700752 attp_send_sr_msg(tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800753
Myles Watson8d749042017-09-19 10:01:28 -0700754 tGATTS_DATA gatts_data;
755 gatts_data.mtu = tcb.payload_size;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700756 /* Notify all registered applicaiton with new MTU size. Us a transaction ID */
757 /* of 0, as no response is allowed from applcations */
758 for (int i = 0; i < GATT_MAX_APPS; i++) {
759 if (gatt_cb.cl_rcb[i].in_use) {
760 uint16_t conn_id =
761 GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson8d749042017-09-19 10:01:28 -0700762 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, &gatts_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800763 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800764 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800765}
766
767/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800768 *
769 * Function gatts_process_read_by_type_req
770 *
771 * Description process Read By type request.
772 * This PDU can be used to perform:
773 * - read characteristic value
774 * - read characteristic descriptor value
775 * - discover characteristic
776 * - discover characteristic by UUID
777 * - relationship discovery
778 *
779 * Returns void
780 *
781 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700782void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800783 uint16_t len, uint8_t* p_data) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700784 Uuid uuid = Uuid::kEmpty;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700785 uint16_t s_hdl, e_hdl, err_hdl = 0;
786 tGATT_STATUS reason =
787 gatts_validate_packet_format(op_code, len, p_data, &uuid, s_hdl, e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800788
Marie Janssend19e0782016-07-15 12:48:27 -0700789#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800790 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700791 VLOG(1) << "Conformance tst: forced err rsp for ReadByType: error status="
792 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800793
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700794 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800795 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800796
Myles Watson911d1ae2016-11-28 16:44:40 -0800797 return;
798 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800799#endif
800
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700801 if (reason != GATT_SUCCESS) {
802 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
803 return;
804 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800805
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700806 size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
807 BT_HDR* p_msg = (BT_HDR*)osi_calloc(msg_len);
808 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800809
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700810 *p++ = op_code + 1;
811 /* reserve length byte */
812 p_msg->len = 2;
813 uint16_t buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800814
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700815 reason = GATT_NOT_FOUND;
816 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
817 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
818 uint8_t sec_flag, key_size;
819 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800820
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700821 tGATT_STATUS ret = gatts_db_read_attr_value_by_type(
822 tcb, el.p_db, op_code, p_msg, s_hdl, e_hdl, uuid, &buf_len, sec_flag,
823 key_size, 0, &err_hdl);
824 if (ret != GATT_NOT_FOUND) {
825 reason = ret;
826 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
827 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800828
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700829 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
830 s_hdl = err_hdl;
831 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800832 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800833 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800834 }
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700835 *p = (uint8_t)p_msg->offset;
836 p_msg->offset = L2CAP_MIN_OFFSET;
837
Myles Watson911d1ae2016-11-28 16:44:40 -0800838 if (reason != GATT_SUCCESS) {
839 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800840
Myles Watson911d1ae2016-11-28 16:44:40 -0800841 /* in theroy BUSY is not possible(should already been checked), protected
842 * check */
843 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700844 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700845
846 return;
847 }
848
849 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800850}
851
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700852/**
853 * This function is called to process the write request from client.
854 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700855void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700856 uint16_t handle, uint8_t op_code, uint16_t len,
857 uint8_t* p_data,
Myles Watson911d1ae2016-11-28 16:44:40 -0800858 bt_gatt_db_attribute_type_t gatt_type) {
859 tGATTS_DATA sr_data;
860 uint32_t trans_id;
861 tGATT_STATUS status;
862 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800863 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800864
Myles Watson911d1ae2016-11-28 16:44:40 -0800865 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800866
Myles Watson911d1ae2016-11-28 16:44:40 -0800867 switch (op_code) {
868 case GATT_REQ_PREPARE_WRITE:
869 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700870 LOG(ERROR) << __func__
871 << ": Prepare write request was invalid - missing offset, "
872 "sending error response";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700873 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800874 return;
875 }
876 sr_data.write_req.is_prep = true;
877 STREAM_TO_UINT16(sr_data.write_req.offset, p);
878 len -= 2;
879 /* fall through */
880 case GATT_SIGN_CMD_WRITE:
881 if (op_code == GATT_SIGN_CMD_WRITE) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700882 VLOG(1) << "Write CMD with data sigining";
Myles Watson911d1ae2016-11-28 16:44:40 -0800883 len -= GATT_AUTH_SIGN_LEN;
884 }
885 /* fall through */
886 case GATT_CMD_WRITE:
887 case GATT_REQ_WRITE:
888 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
889 sr_data.write_req.need_rsp = true;
890 sr_data.write_req.handle = handle;
891 sr_data.write_req.len = len;
892 if (len != 0 && p != NULL) {
893 memcpy(sr_data.write_req.value, p, len);
894 }
895 break;
896 }
897
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700898 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800899
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700900 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
901 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800902 sec_flag, key_size);
903
904 if (status == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700905 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800906 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700907 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800908
909 uint8_t opcode = 0;
910 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
911 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
912 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
913 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
914 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700915 LOG(ERROR) << __func__
916 << "%s: Attempt to write attribute that's not tied with"
917 " characteristic or descriptor value.";
Myles Watson911d1ae2016-11-28 16:44:40 -0800918 status = GATT_ERROR;
919 }
920
921 if (opcode) {
922 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
923 status = GATT_PENDING;
924 }
925 } else {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700926 LOG(ERROR) << "max pending command, send error";
Myles Watson911d1ae2016-11-28 16:44:40 -0800927 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800928 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800929 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800930
Myles Watson911d1ae2016-11-28 16:44:40 -0800931 /* in theroy BUSY is not possible(should already been checked), protected
932 * check */
933 if (status != GATT_PENDING && status != GATT_BUSY &&
934 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700935 gatt_send_error_rsp(tcb, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800936 }
937 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938}
939
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700940/**
941 * This function is called to process the read request from client.
942 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700943static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Myles Watson911d1ae2016-11-28 16:44:40 -0800944 uint8_t op_code, uint16_t handle,
945 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700946 size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700947 uint16_t offset = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800948 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800949
Myles Watson911d1ae2016-11-28 16:44:40 -0800950 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800951
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700952 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800953 *p++ = op_code + 1;
954 p_msg->len = 1;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700955 buf_len = tcb.payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800956
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700957 uint8_t sec_flag, key_size;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700958 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800959
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700960 uint16_t value_len = 0;
961 tGATT_STATUS reason = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700962 tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700963 sec_flag, key_size, 0);
Myles Watson911d1ae2016-11-28 16:44:40 -0800964 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800965
Myles Watson911d1ae2016-11-28 16:44:40 -0800966 if (reason != GATT_SUCCESS) {
967 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800968
Myles Watson911d1ae2016-11-28 16:44:40 -0800969 /* in theroy BUSY is not possible(should already been checked), protected
970 * check */
971 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700972 gatt_send_error_rsp(tcb, reason, op_code, handle, false);
Jakub Pawlowskib4e47992017-07-11 15:36:48 -0700973
974 return;
975 }
976
977 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800978}
979
980/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800981 *
982 * Function gatts_process_attribute_req
983 *
Myles Watson9ca07092016-11-28 16:41:53 -0800984 * Description This function is called to process the per attribute handle
985 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800986 *
987 * Returns void
988 *
989 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700990void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
991 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800992 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700993 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800994 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800995
Myles Watson911d1ae2016-11-28 16:44:40 -0800996 if (len < 2) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -0700997 LOG(ERROR) << "Illegal PDU length, discard request";
Myles Watson911d1ae2016-11-28 16:44:40 -0800998 status = GATT_INVALID_PDU;
999 } else {
1000 STREAM_TO_UINT16(handle, p);
1001 len -= 2;
1002 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001003
Marie Janssend19e0782016-07-15 12:48:27 -07001004#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001005 gatt_cb.handle = handle;
1006 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001007 VLOG(1) << "Conformance tst: forced err rsp: error status="
1008 << +gatt_cb.err_status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001009
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001010 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -08001011 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001012
Myles Watson911d1ae2016-11-28 16:44:40 -08001013 return;
1014 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001015#endif
1016
Myles Watson911d1ae2016-11-28 16:44:40 -08001017 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001018 for (auto& el : *gatt_cb.srv_list_info) {
1019 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1020 for (const auto& attr : el.p_db->attr_list) {
1021 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001022 switch (op_code) {
1023 case GATT_REQ_READ: /* read char/char descriptor value */
1024 case GATT_REQ_READ_BLOB:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001025 gatts_process_read_req(tcb, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001026 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001027
Myles Watson911d1ae2016-11-28 16:44:40 -08001028 case GATT_REQ_WRITE: /* write char/char descriptor value */
1029 case GATT_CMD_WRITE:
1030 case GATT_SIGN_CMD_WRITE:
1031 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001032 gatts_process_write_req(tcb, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001033 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001034 break;
1035 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001036 break;
1037 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001038 status = GATT_SUCCESS;
1039 break;
1040 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001041 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001042 break;
1043 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001044 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001045 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001046
Myles Watson911d1ae2016-11-28 16:44:40 -08001047 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1048 op_code != GATT_SIGN_CMD_WRITE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001049 gatt_send_error_rsp(tcb, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001050}
1051
1052/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001053 *
1054 * Function gatts_proc_srv_chg_ind_ack
1055 *
1056 * Description This function process the service changed indicaiton ACK
1057 *
1058 * Returns void
1059 *
1060 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001061static void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001062 tGATTS_SRV_CHG_REQ req;
1063 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001064
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001065 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001066
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001067 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001068 if (p_buf != NULL) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001069 VLOG(1) << "NV update set srv chg = false";
Myles Watson911d1ae2016-11-28 16:44:40 -08001070 p_buf->srv_changed = false;
1071 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1072 if (gatt_cb.cb_info.p_srv_chg_callback)
1073 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1074 &req, NULL);
1075 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001076}
1077
1078/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001079 *
1080 * Function gatts_chk_pending_ind
1081 *
Myles Watson9ca07092016-11-28 16:41:53 -08001082 * Description This function check any pending indication needs to be sent
1083 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001084 *
1085 * Returns void
1086 *
1087 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001088static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001089 VLOG(1) << __func__;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001090
Myles Watson911d1ae2016-11-28 16:44:40 -08001091 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001092 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001093 if (p_buf != NULL) {
1094 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1095 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001096 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001097 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001098}
1099
1100/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001101 *
1102 * Function gatts_proc_ind_ack
1103 *
Myles Watson9ca07092016-11-28 16:41:53 -08001104 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001105 *
Myles Watson9ca07092016-11-28 16:41:53 -08001106 * Returns true continue to process the indication ack by the
1107 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001108 *
1109 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001110static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001111 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001112
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001113 VLOG(1) << __func__ << " ack handle=%d" << ack_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114
Myles Watson911d1ae2016-11-28 16:44:40 -08001115 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001116 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001117 /* there is no need to inform the application since srv chg is handled
1118 * internally by GATT */
1119 continue_processing = false;
1120 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001121
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001122 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001123 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001124}
1125
1126/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001127 *
1128 * Function gatts_process_value_conf
1129 *
Myles Watson9ca07092016-11-28 16:41:53 -08001130 * Description This function is called to process the handle value
1131 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001132 *
1133 * Returns void
1134 *
1135 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001136void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) {
1137 uint16_t handle = tcb.indicate_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001138
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001139 alarm_cancel(tcb.conf_timer);
1140 if (!GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001141 LOG(ERROR) << "unexpected handle value confirmation";
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001142 return;
1143 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001144
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001145 tcb.indicate_handle = 0;
1146 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1147
1148 if (continue_processing) {
Myles Watson8d749042017-09-19 10:01:28 -07001149 tGATTS_DATA gatts_data;
1150 gatts_data.handle = handle;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001151 for (auto& el : *gatt_cb.srv_list_info) {
1152 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1153 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
1154 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1155 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
Myles Watson8d749042017-09-19 10:01:28 -07001156 &gatts_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001157 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001158 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001159 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001160}
1161
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001162/** This function is called to handle the client requests to server */
1163void gatt_server_handle_client_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -08001164 uint16_t len, uint8_t* p_data) {
1165 /* there is pending command, discard this one */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001166 if (!gatt_sr_cmd_empty(tcb) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001167
Myles Watson911d1ae2016-11-28 16:44:40 -08001168 /* the size of the message may not be bigger than the local max PDU size*/
1169 /* The message has to be smaller than the agreed MTU, len does not include op
1170 * code */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001171 if (len >= tcb.payload_size) {
Jakub Pawlowskid8be0e52017-06-08 17:04:47 -07001172 LOG(ERROR) << StringPrintf("server receive invalid PDU size:%d pdu size:%d",
1173 len + 1, tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001174 /* for invalid request expecting response, send it now */
1175 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1176 op_code != GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001177 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001178 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001179 /* otherwise, ignore the pkt */
1180 } else {
1181 switch (op_code) {
1182 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1183 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001184 gatts_process_primary_service_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001185 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001186
Myles Watson911d1ae2016-11-28 16:44:40 -08001187 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001188 gatts_process_find_info(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_BY_TYPE: /* read characteristic value, char descriptor
1192 value */
1193 /* discover characteristic, discover char by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001194 gatts_process_read_by_type_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001195 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001196
Myles Watson911d1ae2016-11-28 16:44:40 -08001197 case GATT_REQ_READ: /* read char/char descriptor value */
1198 case GATT_REQ_READ_BLOB:
1199 case GATT_REQ_WRITE: /* write char/char descriptor value */
1200 case GATT_CMD_WRITE:
1201 case GATT_SIGN_CMD_WRITE:
1202 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001203 gatts_process_attribute_req(tcb, op_code, 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_HANDLE_VALUE_CONF:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001207 gatts_process_value_conf(tcb, op_code);
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_MTU:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001211 gatts_process_mtu_req(tcb, 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 case GATT_REQ_EXEC_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001215 gatt_process_exec_write_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001216 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001217
Myles Watson911d1ae2016-11-28 16:44:40 -08001218 case GATT_REQ_READ_MULTI:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001219 gatt_process_read_multi_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001220 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001221
Myles Watson911d1ae2016-11-28 16:44:40 -08001222 default:
1223 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001224 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001225 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001226}