blob: 2b332d9c6fa17639a9ed59ef0ec5b79c50529f98 [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
35/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080036 *
37 * Function gatt_sr_enqueue_cmd
38 *
39 * Description This function enqueue the request from client which needs a
40 * application response, and update the transaction ID.
41 *
42 * Returns void
43 *
44 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070045uint32_t gatt_sr_enqueue_cmd(tGATT_TCB& tcb, uint8_t op_code, uint16_t handle) {
46 tGATT_SR_CMD* p_cmd = &tcb.sr_cmd;
Myles Watson911d1ae2016-11-28 16:44:40 -080047 uint32_t trans_id = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080048
Myles Watson911d1ae2016-11-28 16:44:40 -080049 if ((p_cmd->op_code == 0) ||
50 (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */
51 {
52 if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE ||
53 op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070054 trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080055 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070056 p_cmd->trans_id = ++tcb.trans_id;
Myles Watson911d1ae2016-11-28 16:44:40 -080057 p_cmd->op_code = op_code;
58 p_cmd->handle = handle;
59 p_cmd->status = GATT_NOT_FOUND;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070060 tcb.trans_id %= GATT_TRANS_ID_MAX;
Myles Watson911d1ae2016-11-28 16:44:40 -080061 trans_id = p_cmd->trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080062 }
Myles Watson911d1ae2016-11-28 16:44:40 -080063 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080064
Myles Watson911d1ae2016-11-28 16:44:40 -080065 return trans_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -080066}
67
68/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080069 *
70 * Function gatt_sr_cmd_empty
71 *
Myles Watson9ca07092016-11-28 16:41:53 -080072 * Description This function checks if the server command queue is empty.
Myles Watsonee96a3c2016-11-23 14:49:54 -080073 *
74 * Returns true if empty, false if there is pending command.
75 *
76 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070077bool gatt_sr_cmd_empty(tGATT_TCB& tcb) { return (tcb.sr_cmd.op_code == 0); }
The Android Open Source Project5738f832012-12-12 16:00:35 -080078
79/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080080 *
81 * Function gatt_dequeue_sr_cmd
82 *
83 * Description This function dequeue the request from command queue.
84 *
85 * Returns void
86 *
87 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070088void gatt_dequeue_sr_cmd(tGATT_TCB& tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -080089 /* Double check in case any buffers are queued */
90 GATT_TRACE_DEBUG("gatt_dequeue_sr_cmd");
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070091 if (tcb.sr_cmd.p_rsp_msg)
92 GATT_TRACE_ERROR("free tcb.sr_cmd.p_rsp_msg = %d", tcb.sr_cmd.p_rsp_msg);
93 osi_free_and_reset((void**)&tcb.sr_cmd.p_rsp_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -080094
Jakub Pawlowskif4c02922017-05-30 11:21:04 -070095 while (!fixed_queue_is_empty(tcb.sr_cmd.multi_rsp_q))
96 osi_free(fixed_queue_try_dequeue(tcb.sr_cmd.multi_rsp_q));
97 fixed_queue_free(tcb.sr_cmd.multi_rsp_q, NULL);
98 memset(&tcb.sr_cmd, 0, sizeof(tGATT_SR_CMD));
The Android Open Source Project5738f832012-12-12 16:00:35 -080099}
100
101/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800102 *
103 * Function process_read_multi_rsp
104 *
105 * Description This function check the read multiple response.
106 *
107 * Returns bool if all replies have been received
108 *
109 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800110static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status,
111 tGATTS_RSP* p_msg, uint16_t mtu) {
112 uint16_t ii, total_len, len;
113 uint8_t* p;
114 bool is_overflow = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800115
Myles Watson911d1ae2016-11-28 16:44:40 -0800116 GATT_TRACE_DEBUG("%s status=%d mtu=%d", __func__, status, mtu);
Subramanian Srinivasan089cd112016-05-16 11:14:03 -0700117
Myles Watson911d1ae2016-11-28 16:44:40 -0800118 if (p_cmd->multi_rsp_q == NULL)
119 p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800120
Myles Watson911d1ae2016-11-28 16:44:40 -0800121 /* Enqueue the response */
122 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP));
123 memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP));
124 fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800125
Myles Watson911d1ae2016-11-28 16:44:40 -0800126 p_cmd->status = status;
127 if (status == GATT_SUCCESS) {
128 GATT_TRACE_DEBUG("Multi read count=%d num_hdls=%d",
129 fixed_queue_length(p_cmd->multi_rsp_q),
130 p_cmd->multi_req.num_handles);
131 /* 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;
167 GATT_TRACE_DEBUG("multi read overflow available len=%d val_len=%d",
168 len, p_rsp->attr_value.len);
169 } else {
170 len = p_rsp->attr_value.len;
171 }
172
173 if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
174 memcpy(p, p_rsp->attr_value.value, len);
175 if (!is_overflow) p += len;
176 p_buf->len += len;
177 } else {
178 p_cmd->status = GATT_NOT_FOUND;
179 break;
180 }
181
182 if (is_overflow) break;
183
184 } else {
185 p_cmd->status = GATT_NOT_FOUND;
186 break;
187 }
188
189 } /* loop through all handles*/
190
191 /* Sanity check on the buffer length */
192 if (p_buf->len == 0) {
193 GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!");
194 p_cmd->status = GATT_NOT_FOUND;
195 osi_free(p_buf);
196 GATT_TRACE_DEBUG("osi_free(p_buf)");
197 } else if (p_cmd->p_rsp_msg != NULL) {
198 osi_free(p_buf);
199 } else {
200 p_cmd->p_rsp_msg = p_buf;
201 }
202
203 return (true);
204 }
205 } else /* any handle read exception occurs, return error */
206 {
207 return (true);
208 }
209
210 /* If here, still waiting */
211 return (false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800212}
213
214/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800215 *
216 * Function gatt_sr_process_app_rsp
217 *
Myles Watson9ca07092016-11-28 16:41:53 -0800218 * Description This function checks whether the response message from
219 * application matches any pending request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800220 *
221 * Returns void
222 *
223 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700224tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if,
Myles Watson911d1ae2016-11-28 16:44:40 -0800225 UNUSED_ATTR uint32_t trans_id,
226 uint8_t op_code, tGATT_STATUS status,
227 tGATTS_RSP* p_msg) {
228 tGATT_STATUS ret_code = GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700230 GATT_TRACE_DEBUG("%s gatt_if=%d", __func__, gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800231
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700232 gatt_sr_update_cback_cnt(tcb, gatt_if, false, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800233
Myles Watson911d1ae2016-11-28 16:44:40 -0800234 if (op_code == GATT_REQ_READ_MULTI) {
235 /* If no error and still waiting, just return */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700236 if (!process_read_multi_rsp(&tcb.sr_cmd, status, p_msg, tcb.payload_size))
Myles Watson911d1ae2016-11-28 16:44:40 -0800237 return (GATT_SUCCESS);
238 } else {
239 if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700240 gatt_sr_update_prep_cnt(tcb, gatt_if, true, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800241
242 if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700243 gatt_sr_reset_cback_cnt(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800244
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700245 tcb.sr_cmd.status = status;
Myles Watson911d1ae2016-11-28 16:44:40 -0800246
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700247 if (gatt_sr_is_cback_cnt_zero(tcb) && status == GATT_SUCCESS) {
248 if (tcb.sr_cmd.p_rsp_msg == NULL) {
249 tcb.sr_cmd.p_rsp_msg = attp_build_sr_msg(tcb, (uint8_t)(op_code + 1),
250 (tGATT_SR_MSG*)p_msg);
Myles Watson911d1ae2016-11-28 16:44:40 -0800251 } else {
252 GATT_TRACE_ERROR("Exception!!! already has respond message");
253 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800254 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800255 }
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700256 if (gatt_sr_is_cback_cnt_zero(tcb)) {
257 if ((tcb.sr_cmd.status == GATT_SUCCESS) && (tcb.sr_cmd.p_rsp_msg)) {
258 ret_code = attp_send_sr_msg(tcb, tcb.sr_cmd.p_rsp_msg);
259 tcb.sr_cmd.p_rsp_msg = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -0800260 } else {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700261 ret_code =
262 gatt_send_error_rsp(tcb, status, op_code, tcb.sr_cmd.handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263 }
264
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700265 gatt_dequeue_sr_cmd(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -0800266 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800267
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700268 GATT_TRACE_DEBUG("%s ret_code=%d", __func__, ret_code);
Myles Watson911d1ae2016-11-28 16:44:40 -0800269
270 return ret_code;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800271}
272
273/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800274 *
275 * Function gatt_process_exec_write_req
276 *
277 * Description This function is called to process the execute write request
278 * from client.
279 *
280 * Returns void
281 *
282 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700283void gatt_process_exec_write_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800284 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
285 uint8_t *p = p_data, flag, i = 0;
286 uint32_t trans_id = 0;
287 tGATT_IF gatt_if;
288 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800289
Marie Janssend19e0782016-07-15 12:48:27 -0700290#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800291 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
292 GATT_TRACE_DEBUG(
293 "Conformance tst: forced err rspv for Execute Write: error status=%d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700294 gatt_cb.err_status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800295
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700296 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800297 gatt_cb.handle, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800298
Myles Watson911d1ae2016-11-28 16:44:40 -0800299 return;
300 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800301#endif
302
Myles Watson911d1ae2016-11-28 16:44:40 -0800303 STREAM_TO_UINT8(flag, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800304
Myles Watson911d1ae2016-11-28 16:44:40 -0800305 /* mask the flag */
306 flag &= GATT_PREP_WRITE_EXEC;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800307
Myles Watson911d1ae2016-11-28 16:44:40 -0800308 /* no prep write is queued */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700309 if (!gatt_sr_is_prep_cnt_zero(tcb)) {
310 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, 0);
311 gatt_sr_copy_prep_cnt_to_cback_cnt(tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800312
Myles Watson911d1ae2016-11-28 16:44:40 -0800313 for (i = 0; i < GATT_MAX_APPS; i++) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700314 if (tcb.prep_cnt[i]) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800315 gatt_if = (tGATT_IF)(i + 1);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700316 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800317 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC,
318 (tGATTS_DATA*)&flag);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700319 tcb.prep_cnt[i] = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -0800320 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800321 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800322 } else /* nothing needs to be executed , send response now */
323 {
324 GATT_TRACE_ERROR("gatt_process_exec_write_req: no prepare write pending");
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700325 gatt_send_error_rsp(tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800326 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800327}
328
329/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800330 *
331 * Function gatt_process_read_multi_req
332 *
333 * Description This function is called to process the read multiple request
334 * from client.
335 *
336 * Returns void
337 *
338 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700339void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
340 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800341 uint32_t trans_id;
342 uint16_t handle = 0, ll = len;
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700343 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800344 tGATT_STATUS err = GATT_SUCCESS;
345 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800346
Myles Watson911d1ae2016-11-28 16:44:40 -0800347 GATT_TRACE_DEBUG("gatt_process_read_multi_req");
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700348 tcb.sr_cmd.multi_req.num_handles = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800349
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700350 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800351
Marie Janssend19e0782016-07-15 12:48:27 -0700352#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800353 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
354 GATT_TRACE_DEBUG(
355 "Conformance tst: forced err rspvofr ReadMultiple: error status=%d",
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) {
379 GATT_TRACE_DEBUG("read permission denied : 0x%02x", err);
380 break;
381 }
382 } else {
383 /* invalid handle */
384 err = GATT_INVALID_HANDLE;
385 break;
386 }
387 ll -= 2;
388 }
389
390 if (ll != 0) {
391 GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request.");
392 }
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) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800476 GATT_TRACE_DEBUG("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,
506 BT_HDR* p_msg, uint16_t* p_len,
507 uint16_t s_hdl, uint16_t e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800508 tGATT_STATUS status = GATT_NOT_FOUND;
509 uint8_t* p;
510 uint16_t len = *p_len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800511 uint8_t info_pair_len[2] = {4, 18};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800512
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700513 if (!el.p_db) return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800514
Myles Watson911d1ae2016-11-28 16:44:40 -0800515 /* check the attribute database */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800516
Myles Watson911d1ae2016-11-28 16:44:40 -0800517 p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800518
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700519 for (auto& attr : el.p_db->attr_list) {
520 if (attr.handle > e_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800521 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800522 }
523
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700524 if (attr.handle >= s_hdl) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800525 if (p_msg->offset == 0)
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700526 p_msg->offset = (attr.uuid.len == LEN_UUID_16)
Myles Watson911d1ae2016-11-28 16:44:40 -0800527 ? GATT_INFO_TYPE_PAIR_16
528 : GATT_INFO_TYPE_PAIR_128;
529
530 if (len >= info_pair_len[p_msg->offset - 1]) {
531 if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 &&
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700532 attr.uuid.len == LEN_UUID_16) {
533 UINT16_TO_STREAM(p, attr.handle);
534 UINT16_TO_STREAM(p, attr.uuid.uu.uuid16);
Myles Watson911d1ae2016-11-28 16:44:40 -0800535 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700536 attr.uuid.len == LEN_UUID_128) {
537 UINT16_TO_STREAM(p, attr.handle);
538 ARRAY_TO_STREAM(p, attr.uuid.uu.uuid128, LEN_UUID_128);
Myles Watson911d1ae2016-11-28 16:44:40 -0800539 } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 &&
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700540 attr.uuid.len == LEN_UUID_32) {
541 UINT16_TO_STREAM(p, attr.handle);
542 gatt_convert_uuid32_to_uuid128(p, attr.uuid.uu.uuid32);
Myles Watson911d1ae2016-11-28 16:44:40 -0800543 p += LEN_UUID_128;
544 } else {
545 GATT_TRACE_ERROR("format mismatch");
546 status = GATT_NO_RESOURCES;
547 break;
548 /* format mismatch */
549 }
550 p_msg->len += info_pair_len[p_msg->offset - 1];
551 len -= info_pair_len[p_msg->offset - 1];
552 status = GATT_SUCCESS;
553
554 } else {
555 status = GATT_NO_RESOURCES;
556 break;
557 }
558 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800559 }
560
561 *p_len = len;
562 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563}
564
565/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800566 *
567 * Function gatts_internal_read_by_type_req
568 *
Myles Watson9ca07092016-11-28 16:41:53 -0800569 * Description Check to see if the ReadByType request can be handled
570 * internally.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800571 *
572 * Returns void
573 *
574 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800575static tGATT_STATUS gatts_validate_packet_format(
576 uint8_t op_code, uint16_t* p_len, uint8_t** p_data, tBT_UUID* p_uuid_filter,
577 uint16_t* p_s_hdl, uint16_t* p_e_hdl) {
578 tGATT_STATUS reason = GATT_SUCCESS;
579 uint16_t uuid_len, s_hdl = 0, e_hdl = 0;
580 uint16_t len = *p_len;
581 uint8_t* p = *p_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800582
Myles Watson911d1ae2016-11-28 16:44:40 -0800583 if (len >= 4) {
584 /* obtain starting handle, and ending handle */
585 STREAM_TO_UINT16(s_hdl, p);
586 STREAM_TO_UINT16(e_hdl, p);
587 len -= 4;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800588
Myles Watson911d1ae2016-11-28 16:44:40 -0800589 if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) ||
590 !GATT_HANDLE_IS_VALID(e_hdl)) {
591 reason = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800592 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800593 /* for these PDUs, uuid filter must present */
594 else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
595 op_code == GATT_REQ_FIND_TYPE_VALUE ||
596 op_code == GATT_REQ_READ_BY_TYPE) {
597 if (len >= 2 && p_uuid_filter != NULL) {
598 uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
599
600 /* parse uuid now */
601 if (gatt_parse_uuid_from_cmd(p_uuid_filter, uuid_len, &p) == false ||
602 p_uuid_filter->len == 0) {
603 GATT_TRACE_DEBUG("UUID filter does not exsit");
604 reason = GATT_INVALID_PDU;
605 } else
606 len -= p_uuid_filter->len;
607 } else
Andre Eisenbachccf9c152013-10-02 15:37:21 -0700608 reason = GATT_INVALID_PDU;
Myles Watson911d1ae2016-11-28 16:44:40 -0800609 }
610 } else
611 reason = GATT_INVALID_PDU;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612
Myles Watson911d1ae2016-11-28 16:44:40 -0800613 *p_data = p;
614 *p_len = len;
615 *p_s_hdl = s_hdl;
616 *p_e_hdl = e_hdl;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800617
Myles Watson911d1ae2016-11-28 16:44:40 -0800618 return reason;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800619}
620
621/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800622 *
623 * Function gatts_process_primary_service_req
624 *
Myles Watson9ca07092016-11-28 16:41:53 -0800625 * Description Process ReadByGroupType/ReadByTypeValue request, for
626 * discovering all primary services or discover primary service
627 * by UUID request.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800628 *
629 * Returns void
630 *
631 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700632void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800633 uint16_t len, uint8_t* p_data) {
634 uint8_t reason = GATT_INVALID_PDU;
635 uint16_t s_hdl = 0, e_hdl = 0;
636 tBT_UUID uuid, value,
637 primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
638 BT_HDR* p_msg = NULL;
639 uint16_t msg_len =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700640 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800641
Myles Watson911d1ae2016-11-28 16:44:40 -0800642 memset(&value, 0, sizeof(tBT_UUID));
643 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl,
644 &e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800645
Myles Watson911d1ae2016-11-28 16:44:40 -0800646 if (reason == GATT_SUCCESS) {
647 if (gatt_uuid_compare(uuid, primary_service)) {
648 if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
649 if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == false)
650 reason = GATT_INVALID_PDU;
651 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800652
Myles Watson911d1ae2016-11-28 16:44:40 -0800653 if (reason == GATT_SUCCESS) {
654 p_msg = (BT_HDR*)osi_calloc(msg_len);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700655 reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800656 e_hdl, p_data, value);
657 }
658 } else {
659 if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
660 reason = GATT_UNSUPPORT_GRP_TYPE;
661 GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x",
662 uuid.uu.uuid16);
663 } else {
664 /* we do not support ReadByTypeValue with any non-primamry_service type
665 */
666 reason = GATT_NOT_FOUND;
667 GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x",
668 uuid.uu.uuid16);
669 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800670 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800671 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800672
Myles Watson911d1ae2016-11-28 16:44:40 -0800673 if (reason != GATT_SUCCESS) {
674 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700675 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800676 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700677 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800678}
679
680/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800681 *
682 * Function gatts_process_find_info
683 *
684 * Description process find information request, for discover character
685 * descriptors.
686 *
687 * Returns void
688 *
689 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700690static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800691 uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700692 uint16_t s_hdl = 0, e_hdl = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800693
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700694 uint8_t reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL,
695 &s_hdl, &e_hdl);
696 if (reason != GATT_SUCCESS) {
697 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
698 return;
699 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800700
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700701 uint16_t buf_len =
702 (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800703
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700704 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
705 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800706
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700707 uint8_t* p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
708 *p++ = op_code + 1;
709 p_msg->len = 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800710
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700711 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800712
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700713 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
714 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
715 reason = gatt_build_find_info_rsp(el, p_msg, &buf_len, s_hdl, e_hdl);
716 if (reason == GATT_NO_RESOURCES) {
717 reason = GATT_SUCCESS;
718 break;
Myles Watson911d1ae2016-11-28 16:44:40 -0800719 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800720 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800721 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800722
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700723 *p = (uint8_t)p_msg->offset;
724
725 p_msg->offset = L2CAP_MIN_OFFSET;
726
Myles Watson911d1ae2016-11-28 16:44:40 -0800727 if (reason != GATT_SUCCESS) {
728 osi_free(p_msg);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700729 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800730 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700731 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800732}
733
734/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800735 *
736 * Function gatts_process_mtu_req
737 *
738 * Description This function is called to process excahnge MTU request.
739 * Only used on LE.
740 *
741 * Returns void
742 *
743 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700744static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800745 uint8_t* p_data) {
746 uint16_t mtu = 0;
747 uint8_t *p = p_data, i;
748 BT_HDR* p_buf;
749 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800750
Myles Watson911d1ae2016-11-28 16:44:40 -0800751 /* BR/EDR conenction, send error response */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700752 if (tcb.att_lcid != L2CAP_ATT_CID) {
753 gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800754 } else if (len < GATT_MTU_REQ_MIN_LEN) {
755 GATT_TRACE_ERROR("invalid MTU request PDU received.");
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700756 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800757 } else {
758 STREAM_TO_UINT16(mtu, p);
759 /* mtu must be greater than default MTU which is 23/48 */
760 if (mtu < GATT_DEF_BLE_MTU_SIZE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700761 tcb.payload_size = GATT_DEF_BLE_MTU_SIZE;
Myles Watson911d1ae2016-11-28 16:44:40 -0800762 else if (mtu > GATT_MAX_MTU_SIZE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700763 tcb.payload_size = GATT_MAX_MTU_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800764 else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700765 tcb.payload_size = mtu;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800766
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700767 GATT_TRACE_ERROR("MTU request PDU with MTU size %d", tcb.payload_size);
Zhihai Xu52c0ccb2014-03-20 17:50:12 -0700768
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700769 l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID,
770 tcb.payload_size);
Priti Aghera636d6712014-12-18 13:55:48 -0800771
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700772 p_buf =
773 attp_build_sr_msg(tcb, GATT_RSP_MTU, (tGATT_SR_MSG*)&tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800774 if (p_buf != NULL) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700775 attp_send_sr_msg(tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800776
Myles Watson911d1ae2016-11-28 16:44:40 -0800777 /* Notify all registered applicaiton with new MTU size. Us a transaction
778 * ID */
779 /* of 0, as no response is allowed from applcations */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800780
Myles Watson911d1ae2016-11-28 16:44:40 -0800781 for (i = 0; i < GATT_MAX_APPS; i++) {
782 if (gatt_cb.cl_rcb[i].in_use) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700783 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800784 gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700785 (tGATTS_DATA*)&tcb.payload_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800786 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800787 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800788 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800789 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790}
791
792/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800793 *
794 * Function gatts_process_read_by_type_req
795 *
796 * Description process Read By type request.
797 * This PDU can be used to perform:
798 * - read characteristic value
799 * - read characteristic descriptor value
800 * - discover characteristic
801 * - discover characteristic by UUID
802 * - relationship discovery
803 *
804 * Returns void
805 *
806 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700807void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -0800808 uint16_t len, uint8_t* p_data) {
809 tBT_UUID uuid;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700810 size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800811 uint16_t buf_len, s_hdl, e_hdl, err_hdl = 0;
812 BT_HDR* p_msg = NULL;
813 tGATT_STATUS reason, ret;
814 uint8_t* p;
815 uint8_t sec_flag, key_size;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800816
Myles Watson911d1ae2016-11-28 16:44:40 -0800817 reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl,
818 &e_hdl);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800819
Marie Janssend19e0782016-07-15 12:48:27 -0700820#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800821 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
822 GATT_TRACE_DEBUG(
823 "Conformance tst: forced err rsp for ReadByType: error status=%d",
824 gatt_cb.err_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800825
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700826 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl,
Myles Watson911d1ae2016-11-28 16:44:40 -0800827 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800828
Myles Watson911d1ae2016-11-28 16:44:40 -0800829 return;
830 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800831#endif
832
Myles Watson911d1ae2016-11-28 16:44:40 -0800833 if (reason == GATT_SUCCESS) {
834 p_msg = (BT_HDR*)osi_calloc(msg_len);
835 p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800836
Myles Watson911d1ae2016-11-28 16:44:40 -0800837 *p++ = op_code + 1;
838 /* reserve length byte */
839 p_msg->len = 2;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700840 buf_len = tcb.payload_size - 2;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800841
Myles Watson911d1ae2016-11-28 16:44:40 -0800842 reason = GATT_NOT_FOUND;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800843
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700844 for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) {
845 if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700846 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800847
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700848 ret = gatts_db_read_attr_value_by_type(tcb, el.p_db, op_code, p_msg,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700849 s_hdl, e_hdl, uuid, &buf_len,
850 sec_flag, key_size, 0, &err_hdl);
Myles Watson911d1ae2016-11-28 16:44:40 -0800851 if (ret != GATT_NOT_FOUND) {
852 reason = ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800853
Myles Watson911d1ae2016-11-28 16:44:40 -0800854 if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800855 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800856 if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
857 s_hdl = err_hdl;
858 break;
859 }
860 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800861 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800862 *p = (uint8_t)p_msg->offset;
863 p_msg->offset = L2CAP_MIN_OFFSET;
864 }
865 if (reason != GATT_SUCCESS) {
866 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800867
Myles Watson911d1ae2016-11-28 16:44:40 -0800868 /* in theroy BUSY is not possible(should already been checked), protected
869 * check */
870 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700871 gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800872 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700873 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800874}
875
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700876/**
877 * This function is called to process the write request from client.
878 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700879void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700880 uint16_t handle, uint8_t op_code, uint16_t len,
881 uint8_t* p_data,
Myles Watson911d1ae2016-11-28 16:44:40 -0800882 bt_gatt_db_attribute_type_t gatt_type) {
883 tGATTS_DATA sr_data;
884 uint32_t trans_id;
885 tGATT_STATUS status;
886 uint8_t sec_flag, key_size, *p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -0800887 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800888
Myles Watson911d1ae2016-11-28 16:44:40 -0800889 memset(&sr_data, 0, sizeof(tGATTS_DATA));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800890
Myles Watson911d1ae2016-11-28 16:44:40 -0800891 switch (op_code) {
892 case GATT_REQ_PREPARE_WRITE:
893 if (len < 2) {
894 GATT_TRACE_ERROR(
895 "%s: Prepare write request was invalid - missing offset, sending "
896 "error response",
897 __func__);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700898 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800899 return;
900 }
901 sr_data.write_req.is_prep = true;
902 STREAM_TO_UINT16(sr_data.write_req.offset, p);
903 len -= 2;
904 /* fall through */
905 case GATT_SIGN_CMD_WRITE:
906 if (op_code == GATT_SIGN_CMD_WRITE) {
907 GATT_TRACE_DEBUG("Write CMD with data sigining");
908 len -= GATT_AUTH_SIGN_LEN;
909 }
910 /* fall through */
911 case GATT_CMD_WRITE:
912 case GATT_REQ_WRITE:
913 if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE)
914 sr_data.write_req.need_rsp = true;
915 sr_data.write_req.handle = handle;
916 sr_data.write_req.len = len;
917 if (len != 0 && p != NULL) {
918 memcpy(sr_data.write_req.value, p, len);
919 }
920 break;
921 }
922
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700923 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
Myles Watson911d1ae2016-11-28 16:44:40 -0800924
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700925 status = gatts_write_attr_perm_check(el.p_db, op_code, handle,
926 sr_data.write_req.offset, p, len,
Myles Watson911d1ae2016-11-28 16:44:40 -0800927 sec_flag, key_size);
928
929 if (status == GATT_SUCCESS) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700930 trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
Myles Watson911d1ae2016-11-28 16:44:40 -0800931 if (trans_id != 0) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700932 conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
Myles Watson911d1ae2016-11-28 16:44:40 -0800933
934 uint8_t opcode = 0;
935 if (gatt_type == BTGATT_DB_DESCRIPTOR) {
936 opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR;
937 } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) {
938 opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC;
939 } else {
940 GATT_TRACE_ERROR(
941 "%s: Attempt to write attribute that's not tied with"
942 " characteristic or descriptor value.",
943 __func__);
944 status = GATT_ERROR;
945 }
946
947 if (opcode) {
948 gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data);
949 status = GATT_PENDING;
950 }
951 } else {
952 GATT_TRACE_ERROR("max pending command, send error");
953 status = GATT_BUSY; /* max pending command, application error */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800954 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800955 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800956
Myles Watson911d1ae2016-11-28 16:44:40 -0800957 /* in theroy BUSY is not possible(should already been checked), protected
958 * check */
959 if (status != GATT_PENDING && status != GATT_BUSY &&
960 (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700961 gatt_send_error_rsp(tcb, status, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -0800962 }
963 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800964}
965
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700966/**
967 * This function is called to process the read request from client.
968 */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700969static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el,
Myles Watson911d1ae2016-11-28 16:44:40 -0800970 uint8_t op_code, uint16_t handle,
971 UNUSED_ATTR uint16_t len, uint8_t* p_data) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700972 size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET;
Myles Watson911d1ae2016-11-28 16:44:40 -0800973 tGATT_STATUS reason;
974 uint8_t sec_flag, key_size, *p;
975 uint16_t offset = 0, value_len = 0;
976 BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800977
Myles Watson911d1ae2016-11-28 16:44:40 -0800978 if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800979
Myles Watson911d1ae2016-11-28 16:44:40 -0800980 p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
981 *p++ = op_code + 1;
982 p_msg->len = 1;
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700983 buf_len = tcb.payload_size - 1;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800984
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700985 gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800986
Myles Watson911d1ae2016-11-28 16:44:40 -0800987 reason = gatts_read_attr_value_by_handle(
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700988 tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len,
Jakub Pawlowski6395f152017-05-09 05:02:38 -0700989 sec_flag, key_size, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800990
Myles Watson911d1ae2016-11-28 16:44:40 -0800991 p_msg->len += value_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800992
Myles Watson911d1ae2016-11-28 16:44:40 -0800993 if (reason != GATT_SUCCESS) {
994 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800995
Myles Watson911d1ae2016-11-28 16:44:40 -0800996 /* in theroy BUSY is not possible(should already been checked), protected
997 * check */
998 if (reason != GATT_PENDING && reason != GATT_BUSY)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -0700999 gatt_send_error_rsp(tcb, reason, op_code, handle, false);
Myles Watson911d1ae2016-11-28 16:44:40 -08001000 } else
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001001 attp_send_sr_msg(tcb, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002}
1003
1004/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001005 *
1006 * Function gatts_process_attribute_req
1007 *
Myles Watson9ca07092016-11-28 16:41:53 -08001008 * Description This function is called to process the per attribute handle
1009 * request from client.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001010 *
1011 * Returns void
1012 *
1013 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001014void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len,
1015 uint8_t* p_data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001016 uint16_t handle = 0;
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001017 uint8_t* p = p_data;
Myles Watson911d1ae2016-11-28 16:44:40 -08001018 tGATT_STATUS status = GATT_INVALID_HANDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001019
Myles Watson911d1ae2016-11-28 16:44:40 -08001020 if (len < 2) {
1021 GATT_TRACE_ERROR("Illegal PDU length, discard request");
1022 status = GATT_INVALID_PDU;
1023 } else {
1024 STREAM_TO_UINT16(handle, p);
1025 len -= 2;
1026 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001027
Marie Janssend19e0782016-07-15 12:48:27 -07001028#if (GATT_CONFORMANCE_TESTING == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001029 gatt_cb.handle = handle;
1030 if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
1031 GATT_TRACE_DEBUG("Conformance tst: forced err rsp: error status=%d",
1032 gatt_cb.err_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001033
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001034 gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle,
Myles Watson911d1ae2016-11-28 16:44:40 -08001035 false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001036
Myles Watson911d1ae2016-11-28 16:44:40 -08001037 return;
1038 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001039#endif
1040
Myles Watson911d1ae2016-11-28 16:44:40 -08001041 if (GATT_HANDLE_IS_VALID(handle)) {
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001042 for (auto& el : *gatt_cb.srv_list_info) {
1043 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1044 for (const auto& attr : el.p_db->attr_list) {
1045 if (attr.handle == handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001046 switch (op_code) {
1047 case GATT_REQ_READ: /* read char/char descriptor value */
1048 case GATT_REQ_READ_BLOB:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001049 gatts_process_read_req(tcb, el, op_code, handle, len, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001050 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001051
Myles Watson911d1ae2016-11-28 16:44:40 -08001052 case GATT_REQ_WRITE: /* write char/char descriptor value */
1053 case GATT_CMD_WRITE:
1054 case GATT_SIGN_CMD_WRITE:
1055 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001056 gatts_process_write_req(tcb, el, handle, op_code, len, p,
Jakub Pawlowski6395f152017-05-09 05:02:38 -07001057 attr.gatt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001058 break;
1059 default:
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060 break;
1061 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001062 status = GATT_SUCCESS;
1063 break;
1064 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001065 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001066 break;
1067 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001069 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001070
Myles Watson911d1ae2016-11-28 16:44:40 -08001071 if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE &&
1072 op_code != GATT_SIGN_CMD_WRITE)
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001073 gatt_send_error_rsp(tcb, status, op_code, handle, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001074}
1075
1076/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001077 *
1078 * Function gatts_proc_srv_chg_ind_ack
1079 *
1080 * Description This function process the service changed indicaiton ACK
1081 *
1082 * Returns void
1083 *
1084 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001085static void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001086 tGATTS_SRV_CHG_REQ req;
1087 tGATTS_SRV_CHG* p_buf = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001088
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001089 GATT_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001090
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001091 p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08001092 if (p_buf != NULL) {
1093 GATT_TRACE_DEBUG("NV update set srv chg = false");
1094 p_buf->srv_changed = false;
1095 memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
1096 if (gatt_cb.cb_info.p_srv_chg_callback)
1097 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,
1098 &req, NULL);
1099 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100}
1101
1102/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001103 *
1104 * Function gatts_chk_pending_ind
1105 *
Myles Watson9ca07092016-11-28 16:41:53 -08001106 * Description This function check any pending indication needs to be sent
1107 * if there is a pending indication then sent the indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001108 *
1109 * Returns void
1110 *
1111 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001112static void gatts_chk_pending_ind(tGATT_TCB& tcb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001113 GATT_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114
Myles Watson911d1ae2016-11-28 16:44:40 -08001115 tGATT_VALUE* p_buf =
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001116 (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q);
Myles Watson911d1ae2016-11-28 16:44:40 -08001117 if (p_buf != NULL) {
1118 GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len,
1119 p_buf->value);
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001120 osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf));
Myles Watson911d1ae2016-11-28 16:44:40 -08001121 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001122}
1123
1124/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001125 *
1126 * Function gatts_proc_ind_ack
1127 *
Myles Watson9ca07092016-11-28 16:41:53 -08001128 * Description This function processes the Indication ack
Myles Watsonee96a3c2016-11-23 14:49:54 -08001129 *
Myles Watson9ca07092016-11-28 16:41:53 -08001130 * Returns true continue to process the indication ack by the
1131 * application if the ACK is not a Service Changed Indication
Myles Watsonee96a3c2016-11-23 14:49:54 -08001132 *
1133 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001134static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001135 bool continue_processing = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001136
Myles Watson911d1ae2016-11-28 16:44:40 -08001137 GATT_TRACE_DEBUG("gatts_proc_ind_ack ack handle=%d", ack_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001138
Myles Watson911d1ae2016-11-28 16:44:40 -08001139 if (ack_handle == gatt_cb.handle_of_h_r) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001140 gatts_proc_srv_chg_ind_ack(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001141 /* there is no need to inform the application since srv chg is handled
1142 * internally by GATT */
1143 continue_processing = false;
1144 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001145
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001146 gatts_chk_pending_ind(tcb);
Myles Watson911d1ae2016-11-28 16:44:40 -08001147 return continue_processing;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001148}
1149
1150/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001151 *
1152 * Function gatts_process_value_conf
1153 *
Myles Watson9ca07092016-11-28 16:41:53 -08001154 * Description This function is called to process the handle value
1155 * confirmation.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001156 *
1157 * Returns void
1158 *
1159 ******************************************************************************/
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001160void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) {
1161 uint16_t handle = tcb.indicate_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001162
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001163 alarm_cancel(tcb.conf_timer);
1164 if (!GATT_HANDLE_IS_VALID(handle)) {
1165 GATT_TRACE_ERROR("unexpected handle value confirmation");
1166 return;
1167 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001168
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001169 tcb.indicate_handle = 0;
1170 bool continue_processing = gatts_proc_ind_ack(tcb, handle);
1171
1172 if (continue_processing) {
1173 for (auto& el : *gatt_cb.srv_list_info) {
1174 if (el.s_hdl <= handle && el.e_hdl >= handle) {
1175 uint32_t trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle);
1176 uint16_t conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if);
1177 gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF,
1178 (tGATTS_DATA*)&handle);
Myles Watson911d1ae2016-11-28 16:44:40 -08001179 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001180 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001181 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001182}
1183
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001184/** This function is called to handle the client requests to server */
1185void gatt_server_handle_client_req(tGATT_TCB& tcb, uint8_t op_code,
Myles Watson911d1ae2016-11-28 16:44:40 -08001186 uint16_t len, uint8_t* p_data) {
1187 /* there is pending command, discard this one */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001188 if (!gatt_sr_cmd_empty(tcb) && op_code != GATT_HANDLE_VALUE_CONF) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001189
Myles Watson911d1ae2016-11-28 16:44:40 -08001190 /* the size of the message may not be bigger than the local max PDU size*/
1191 /* The message has to be smaller than the agreed MTU, len does not include op
1192 * code */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001193 if (len >= tcb.payload_size) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001194 GATT_TRACE_ERROR("server receive invalid PDU size:%d pdu size:%d", len + 1,
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001195 tcb.payload_size);
Myles Watson911d1ae2016-11-28 16:44:40 -08001196 /* for invalid request expecting response, send it now */
1197 if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE &&
1198 op_code != GATT_HANDLE_VALUE_CONF) {
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001199 gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001200 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001201 /* otherwise, ignore the pkt */
1202 } else {
1203 switch (op_code) {
1204 case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
1205 case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001206 gatts_process_primary_service_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001207 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001208
Myles Watson911d1ae2016-11-28 16:44:40 -08001209 case GATT_REQ_FIND_INFO: /* discover char descrptor */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001210 gatts_process_find_info(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001211 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001212
Myles Watson911d1ae2016-11-28 16:44:40 -08001213 case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor
1214 value */
1215 /* discover characteristic, discover char by UUID */
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001216 gatts_process_read_by_type_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001217 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001218
Myles Watson911d1ae2016-11-28 16:44:40 -08001219 case GATT_REQ_READ: /* read char/char descriptor value */
1220 case GATT_REQ_READ_BLOB:
1221 case GATT_REQ_WRITE: /* write char/char descriptor value */
1222 case GATT_CMD_WRITE:
1223 case GATT_SIGN_CMD_WRITE:
1224 case GATT_REQ_PREPARE_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001225 gatts_process_attribute_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001226 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001227
Myles Watson911d1ae2016-11-28 16:44:40 -08001228 case GATT_HANDLE_VALUE_CONF:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001229 gatts_process_value_conf(tcb, op_code);
Myles Watson911d1ae2016-11-28 16:44:40 -08001230 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001231
Myles Watson911d1ae2016-11-28 16:44:40 -08001232 case GATT_REQ_MTU:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001233 gatts_process_mtu_req(tcb, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001234 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001235
Myles Watson911d1ae2016-11-28 16:44:40 -08001236 case GATT_REQ_EXEC_WRITE:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001237 gatt_process_exec_write_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001238 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001239
Myles Watson911d1ae2016-11-28 16:44:40 -08001240 case GATT_REQ_READ_MULTI:
Jakub Pawlowskif4c02922017-05-30 11:21:04 -07001241 gatt_process_read_multi_req(tcb, op_code, len, p_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001242 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001243
Myles Watson911d1ae2016-11-28 16:44:40 -08001244 default:
1245 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001246 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001247 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001248}