The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 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. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 26 | #include "bt_utils.h" |
Myles Watson | d7ffd64 | 2016-10-27 10:27:36 -0700 | [diff] [blame] | 27 | #include "osi/include/osi.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 28 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include "gatt_int.h" |
| 31 | #include "l2c_api.h" |
Priti Aghera | 636d671 | 2014-12-18 13:55:48 -0800 | [diff] [blame] | 32 | #include "l2c_int.h" |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 33 | #define GATT_MTU_REQ_MIN_LEN 2 |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 34 | |
| 35 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 36 | * |
| 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 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 45 | uint32_t gatt_sr_enqueue_cmd(tGATT_TCB* p_tcb, uint8_t op_code, |
| 46 | uint16_t handle) { |
| 47 | tGATT_SR_CMD* p_cmd = &p_tcb->sr_cmd; |
| 48 | uint32_t trans_id = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 49 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 50 | if ((p_cmd->op_code == 0) || |
| 51 | (op_code == GATT_HANDLE_VALUE_CONF)) /* no pending request */ |
| 52 | { |
| 53 | if (op_code == GATT_CMD_WRITE || op_code == GATT_SIGN_CMD_WRITE || |
| 54 | op_code == GATT_REQ_MTU || op_code == GATT_HANDLE_VALUE_CONF) { |
| 55 | trans_id = ++p_tcb->trans_id; |
| 56 | } else { |
| 57 | p_cmd->trans_id = ++p_tcb->trans_id; |
| 58 | p_cmd->op_code = op_code; |
| 59 | p_cmd->handle = handle; |
| 60 | p_cmd->status = GATT_NOT_FOUND; |
| 61 | p_tcb->trans_id %= GATT_TRANS_ID_MAX; |
| 62 | trans_id = p_cmd->trans_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 63 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 64 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 65 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 66 | return trans_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 70 | * |
| 71 | * Function gatt_sr_cmd_empty |
| 72 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 73 | * Description This function checks if the server command queue is empty. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 74 | * |
| 75 | * Returns true if empty, false if there is pending command. |
| 76 | * |
| 77 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 78 | bool gatt_sr_cmd_empty(tGATT_TCB* p_tcb) { |
| 79 | return (p_tcb->sr_cmd.op_code == 0); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 83 | * |
| 84 | * Function gatt_dequeue_sr_cmd |
| 85 | * |
| 86 | * Description This function dequeue the request from command queue. |
| 87 | * |
| 88 | * Returns void |
| 89 | * |
| 90 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 91 | void gatt_dequeue_sr_cmd(tGATT_TCB* p_tcb) { |
| 92 | /* Double check in case any buffers are queued */ |
| 93 | GATT_TRACE_DEBUG("gatt_dequeue_sr_cmd"); |
| 94 | if (p_tcb->sr_cmd.p_rsp_msg) |
| 95 | GATT_TRACE_ERROR("free p_tcb->sr_cmd.p_rsp_msg = %d", |
| 96 | p_tcb->sr_cmd.p_rsp_msg); |
| 97 | osi_free_and_reset((void**)&p_tcb->sr_cmd.p_rsp_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 98 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 99 | while (!fixed_queue_is_empty(p_tcb->sr_cmd.multi_rsp_q)) |
| 100 | osi_free(fixed_queue_try_dequeue(p_tcb->sr_cmd.multi_rsp_q)); |
| 101 | fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, NULL); |
| 102 | memset(&p_tcb->sr_cmd, 0, sizeof(tGATT_SR_CMD)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 106 | * |
| 107 | * Function process_read_multi_rsp |
| 108 | * |
| 109 | * Description This function check the read multiple response. |
| 110 | * |
| 111 | * Returns bool if all replies have been received |
| 112 | * |
| 113 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 114 | static bool process_read_multi_rsp(tGATT_SR_CMD* p_cmd, tGATT_STATUS status, |
| 115 | tGATTS_RSP* p_msg, uint16_t mtu) { |
| 116 | uint16_t ii, total_len, len; |
| 117 | uint8_t* p; |
| 118 | bool is_overflow = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 119 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 120 | GATT_TRACE_DEBUG("%s status=%d mtu=%d", __func__, status, mtu); |
Subramanian Srinivasan | 089cd11 | 2016-05-16 11:14:03 -0700 | [diff] [blame] | 121 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 122 | if (p_cmd->multi_rsp_q == NULL) |
| 123 | p_cmd->multi_rsp_q = fixed_queue_new(SIZE_MAX); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 124 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 125 | /* Enqueue the response */ |
| 126 | BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(tGATTS_RSP)); |
| 127 | memcpy((void*)p_buf, (const void*)p_msg, sizeof(tGATTS_RSP)); |
| 128 | fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 129 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 130 | p_cmd->status = status; |
| 131 | if (status == GATT_SUCCESS) { |
| 132 | GATT_TRACE_DEBUG("Multi read count=%d num_hdls=%d", |
| 133 | fixed_queue_length(p_cmd->multi_rsp_q), |
| 134 | p_cmd->multi_req.num_handles); |
| 135 | /* Wait till we get all the responses */ |
| 136 | if (fixed_queue_length(p_cmd->multi_rsp_q) == |
| 137 | p_cmd->multi_req.num_handles) { |
| 138 | len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu; |
| 139 | p_buf = (BT_HDR*)osi_calloc(len); |
| 140 | p_buf->offset = L2CAP_MIN_OFFSET; |
| 141 | p = (uint8_t*)(p_buf + 1) + p_buf->offset; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 142 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 143 | /* First byte in the response is the opcode */ |
| 144 | *p++ = GATT_RSP_READ_MULTI; |
| 145 | p_buf->len = 1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 146 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 147 | /* Now walk through the buffers puting the data into the response in order |
| 148 | */ |
| 149 | list_t* list = NULL; |
| 150 | const list_node_t* node = NULL; |
| 151 | if (!fixed_queue_is_empty(p_cmd->multi_rsp_q)) |
| 152 | list = fixed_queue_get_list(p_cmd->multi_rsp_q); |
| 153 | for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) { |
| 154 | tGATTS_RSP* p_rsp = NULL; |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 155 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 156 | if (list != NULL) { |
| 157 | if (ii == 0) |
| 158 | node = list_begin(list); |
| 159 | else |
| 160 | node = list_next(node); |
| 161 | if (node != list_end(list)) p_rsp = (tGATTS_RSP*)list_node(node); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 162 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 163 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 164 | if (p_rsp != NULL) { |
| 165 | total_len = (p_buf->len + p_rsp->attr_value.len); |
| 166 | |
| 167 | if (total_len > mtu) { |
| 168 | /* just send the partial response for the overflow case */ |
| 169 | len = p_rsp->attr_value.len - (total_len - mtu); |
| 170 | is_overflow = true; |
| 171 | GATT_TRACE_DEBUG("multi read overflow available len=%d val_len=%d", |
| 172 | len, p_rsp->attr_value.len); |
| 173 | } else { |
| 174 | len = p_rsp->attr_value.len; |
| 175 | } |
| 176 | |
| 177 | if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) { |
| 178 | memcpy(p, p_rsp->attr_value.value, len); |
| 179 | if (!is_overflow) p += len; |
| 180 | p_buf->len += len; |
| 181 | } else { |
| 182 | p_cmd->status = GATT_NOT_FOUND; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | if (is_overflow) break; |
| 187 | |
| 188 | } else { |
| 189 | p_cmd->status = GATT_NOT_FOUND; |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | } /* loop through all handles*/ |
| 194 | |
| 195 | /* Sanity check on the buffer length */ |
| 196 | if (p_buf->len == 0) { |
| 197 | GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!"); |
| 198 | p_cmd->status = GATT_NOT_FOUND; |
| 199 | osi_free(p_buf); |
| 200 | GATT_TRACE_DEBUG("osi_free(p_buf)"); |
| 201 | } else if (p_cmd->p_rsp_msg != NULL) { |
| 202 | osi_free(p_buf); |
| 203 | } else { |
| 204 | p_cmd->p_rsp_msg = p_buf; |
| 205 | } |
| 206 | |
| 207 | return (true); |
| 208 | } |
| 209 | } else /* any handle read exception occurs, return error */ |
| 210 | { |
| 211 | return (true); |
| 212 | } |
| 213 | |
| 214 | /* If here, still waiting */ |
| 215 | return (false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 219 | * |
| 220 | * Function gatt_sr_process_app_rsp |
| 221 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 222 | * Description This function checks whether the response message from |
| 223 | * application matches any pending request. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 224 | * |
| 225 | * Returns void |
| 226 | * |
| 227 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 228 | tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB* p_tcb, tGATT_IF gatt_if, |
| 229 | UNUSED_ATTR uint32_t trans_id, |
| 230 | uint8_t op_code, tGATT_STATUS status, |
| 231 | tGATTS_RSP* p_msg) { |
| 232 | tGATT_STATUS ret_code = GATT_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 233 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 234 | GATT_TRACE_DEBUG("gatt_sr_process_app_rsp gatt_if=%d", gatt_if); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 235 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 236 | gatt_sr_update_cback_cnt(p_tcb, gatt_if, false, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 237 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 238 | if (op_code == GATT_REQ_READ_MULTI) { |
| 239 | /* If no error and still waiting, just return */ |
| 240 | if (!process_read_multi_rsp(&p_tcb->sr_cmd, status, p_msg, |
| 241 | p_tcb->payload_size)) |
| 242 | return (GATT_SUCCESS); |
| 243 | } else { |
| 244 | if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS) |
| 245 | gatt_sr_update_prep_cnt(p_tcb, gatt_if, true, false); |
| 246 | |
| 247 | if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS) |
| 248 | gatt_sr_reset_cback_cnt(p_tcb); |
| 249 | |
| 250 | p_tcb->sr_cmd.status = status; |
| 251 | |
| 252 | if (gatt_sr_is_cback_cnt_zero(p_tcb) && status == GATT_SUCCESS) { |
| 253 | if (p_tcb->sr_cmd.p_rsp_msg == NULL) { |
| 254 | p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg( |
| 255 | p_tcb, (uint8_t)(op_code + 1), (tGATT_SR_MSG*)p_msg); |
| 256 | } else { |
| 257 | GATT_TRACE_ERROR("Exception!!! already has respond message"); |
| 258 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 259 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 260 | } |
| 261 | if (gatt_sr_is_cback_cnt_zero(p_tcb)) { |
| 262 | if ((p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg)) { |
| 263 | ret_code = attp_send_sr_msg(p_tcb, p_tcb->sr_cmd.p_rsp_msg); |
| 264 | p_tcb->sr_cmd.p_rsp_msg = NULL; |
| 265 | } else { |
| 266 | ret_code = gatt_send_error_rsp(p_tcb, status, op_code, |
| 267 | p_tcb->sr_cmd.handle, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 270 | gatt_dequeue_sr_cmd(p_tcb); |
| 271 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 272 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 273 | GATT_TRACE_DEBUG("gatt_sr_process_app_rsp ret_code=%d", ret_code); |
| 274 | |
| 275 | return ret_code; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 279 | * |
| 280 | * Function gatt_process_exec_write_req |
| 281 | * |
| 282 | * Description This function is called to process the execute write request |
| 283 | * from client. |
| 284 | * |
| 285 | * Returns void |
| 286 | * |
| 287 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 288 | void gatt_process_exec_write_req(tGATT_TCB* p_tcb, uint8_t op_code, |
| 289 | UNUSED_ATTR uint16_t len, uint8_t* p_data) { |
| 290 | uint8_t *p = p_data, flag, i = 0; |
| 291 | uint32_t trans_id = 0; |
| 292 | tGATT_IF gatt_if; |
| 293 | uint16_t conn_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 294 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 295 | #if (GATT_CONFORMANCE_TESTING == TRUE) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 296 | if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) { |
| 297 | GATT_TRACE_DEBUG( |
| 298 | "Conformance tst: forced err rspv for Execute Write: error status=%d", |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 299 | gatt_cb.err_status); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 300 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 301 | gatt_send_error_rsp(p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, |
| 302 | gatt_cb.handle, false); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 303 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 304 | return; |
| 305 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 306 | #endif |
| 307 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 308 | STREAM_TO_UINT8(flag, p); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 309 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 310 | /* mask the flag */ |
| 311 | flag &= GATT_PREP_WRITE_EXEC; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 312 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 313 | /* no prep write is queued */ |
| 314 | if (!gatt_sr_is_prep_cnt_zero(p_tcb)) { |
| 315 | trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0); |
| 316 | gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 317 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 318 | for (i = 0; i < GATT_MAX_APPS; i++) { |
| 319 | if (p_tcb->prep_cnt[i]) { |
| 320 | gatt_if = (tGATT_IF)(i + 1); |
| 321 | conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if); |
| 322 | gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC, |
| 323 | (tGATTS_DATA*)&flag); |
| 324 | p_tcb->prep_cnt[i] = 0; |
| 325 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 326 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 327 | } else /* nothing needs to be executed , send response now */ |
| 328 | { |
| 329 | GATT_TRACE_ERROR("gatt_process_exec_write_req: no prepare write pending"); |
| 330 | gatt_send_error_rsp(p_tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false); |
| 331 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 335 | * |
| 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 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 344 | void gatt_process_read_multi_req(tGATT_TCB* p_tcb, uint8_t op_code, |
| 345 | uint16_t len, uint8_t* p_data) { |
| 346 | uint32_t trans_id; |
| 347 | uint16_t handle = 0, ll = len; |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 348 | uint8_t* p = p_data; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 349 | tGATT_STATUS err = GATT_SUCCESS; |
| 350 | uint8_t sec_flag, key_size; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 351 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 352 | GATT_TRACE_DEBUG("gatt_process_read_multi_req"); |
| 353 | p_tcb->sr_cmd.multi_req.num_handles = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 354 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 355 | gatt_sr_get_sec_info(p_tcb->peer_bda, p_tcb->transport, &sec_flag, &key_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 356 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 357 | #if (GATT_CONFORMANCE_TESTING == TRUE) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 358 | if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) { |
| 359 | GATT_TRACE_DEBUG( |
| 360 | "Conformance tst: forced err rspvofr ReadMultiple: error status=%d", |
| 361 | gatt_cb.err_status); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 362 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 363 | STREAM_TO_UINT16(handle, p); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 364 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 365 | gatt_send_error_rsp(p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, |
| 366 | false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 367 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 368 | return; |
| 369 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 370 | #endif |
| 371 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 372 | while (ll >= 2 && |
| 373 | p_tcb->sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) { |
| 374 | STREAM_TO_UINT16(handle, p); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 375 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 376 | auto it = gatt_sr_find_i_rcb_by_handle(handle); |
| 377 | if (it != gatt_cb.srv_list_info->end()) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 378 | p_tcb->sr_cmd.multi_req.handles[p_tcb->sr_cmd.multi_req.num_handles++] = |
| 379 | handle; |
| 380 | |
| 381 | /* check read permission */ |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 382 | err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag, |
| 383 | key_size); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 384 | if (err != GATT_SUCCESS) { |
| 385 | GATT_TRACE_DEBUG("read permission denied : 0x%02x", err); |
| 386 | break; |
| 387 | } |
| 388 | } else { |
| 389 | /* invalid handle */ |
| 390 | err = GATT_INVALID_HANDLE; |
| 391 | break; |
| 392 | } |
| 393 | ll -= 2; |
| 394 | } |
| 395 | |
| 396 | if (ll != 0) { |
| 397 | GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request."); |
| 398 | } |
| 399 | |
| 400 | if (p_tcb->sr_cmd.multi_req.num_handles == 0) err = GATT_INVALID_HANDLE; |
| 401 | |
| 402 | if (err == GATT_SUCCESS) { |
| 403 | trans_id = |
| 404 | gatt_sr_enqueue_cmd(p_tcb, op_code, p_tcb->sr_cmd.multi_req.handles[0]); |
| 405 | if (trans_id != 0) { |
| 406 | gatt_sr_reset_cback_cnt(p_tcb); /* read multiple use multi_rsp_q's count*/ |
| 407 | |
| 408 | for (ll = 0; ll < p_tcb->sr_cmd.multi_req.num_handles; ll++) { |
| 409 | tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP)); |
| 410 | handle = p_tcb->sr_cmd.multi_req.handles[ll]; |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 411 | auto it = gatt_sr_find_i_rcb_by_handle(handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 412 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 413 | p_msg->attr_value.handle = handle; |
| 414 | err = gatts_read_attr_value_by_handle( |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 415 | p_tcb, it->p_db, op_code, handle, 0, p_msg->attr_value.value, |
| 416 | &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size, |
| 417 | trans_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 418 | |
| 419 | if (err == GATT_SUCCESS) { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 420 | gatt_sr_process_app_rsp(p_tcb, it->gatt_if, trans_id, op_code, |
| 421 | GATT_SUCCESS, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 422 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 423 | /* either not using or done using the buffer, release it now */ |
| 424 | osi_free(p_msg); |
| 425 | } |
| 426 | } else |
| 427 | err = GATT_NO_RESOURCES; |
| 428 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 429 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 430 | /* in theroy BUSY is not possible(should already been checked), protected |
| 431 | * check */ |
| 432 | if (err != GATT_SUCCESS && err != GATT_PENDING && err != GATT_BUSY) |
| 433 | gatt_send_error_rsp(p_tcb, err, op_code, handle, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 437 | * |
| 438 | * Function gatt_build_primary_service_rsp |
| 439 | * |
| 440 | * Description Primamry service request processed internally. Theretically |
| 441 | * only deal with ReadByTypeVAlue and ReadByGroupType. |
| 442 | * |
| 443 | * Returns void |
| 444 | * |
| 445 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 446 | static tGATT_STATUS gatt_build_primary_service_rsp( |
| 447 | BT_HDR* p_msg, tGATT_TCB* p_tcb, uint8_t op_code, uint16_t s_hdl, |
| 448 | uint16_t e_hdl, UNUSED_ATTR uint8_t* p_data, tBT_UUID value) { |
| 449 | tGATT_STATUS status = GATT_NOT_FOUND; |
| 450 | uint8_t handle_len = 4, *p; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 451 | tBT_UUID* p_uuid; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 452 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 453 | p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 454 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 455 | for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) { |
| 456 | if (el.s_hdl >= s_hdl && el.s_hdl <= e_hdl && |
| 457 | el.type == GATT_UUID_PRI_SERVICE) { |
| 458 | p_uuid = gatts_get_service_uuid(el.p_db); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 459 | if (p_uuid != NULL) { |
| 460 | if (op_code == GATT_REQ_READ_BY_GRP_TYPE) handle_len = 4 + p_uuid->len; |
Andre Eisenbach | ccf9c15 | 2013-10-02 15:37:21 -0700 | [diff] [blame] | 461 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 462 | /* get the length byte in the repsonse */ |
| 463 | if (p_msg->offset == 0) { |
| 464 | *p++ = op_code + 1; |
| 465 | p_msg->len++; |
| 466 | p_msg->offset = handle_len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 467 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 468 | if (op_code == GATT_REQ_READ_BY_GRP_TYPE) { |
| 469 | *p++ = (uint8_t)p_msg->offset; /* length byte */ |
| 470 | p_msg->len++; |
| 471 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 472 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 473 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 474 | if (p_msg->len + p_msg->offset <= p_tcb->payload_size && |
| 475 | handle_len == p_msg->offset) { |
| 476 | if (op_code != GATT_REQ_FIND_TYPE_VALUE || |
| 477 | gatt_uuid_compare(value, *p_uuid)) { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 478 | UINT16_TO_STREAM(p, el.s_hdl); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 479 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 480 | if (gatt_cb.last_primary_s_handle && |
| 481 | gatt_cb.last_primary_s_handle == el.s_hdl) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 482 | GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute"); |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 483 | /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */ |
| 484 | UINT16_TO_STREAM(p, 0xFFFF); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 485 | } else { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 486 | UINT16_TO_STREAM(p, el.e_hdl); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | if (op_code == GATT_REQ_READ_BY_GRP_TYPE) |
| 490 | gatt_build_uuid_to_stream(&p, *p_uuid); |
| 491 | |
| 492 | status = GATT_SUCCESS; |
| 493 | p_msg->len += p_msg->offset; |
| 494 | } |
| 495 | } else |
| 496 | break; |
| 497 | } |
| 498 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 499 | } |
| 500 | p_msg->offset = L2CAP_MIN_OFFSET; |
| 501 | |
| 502 | return status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 505 | /** |
| 506 | * fill the find information response information in the given buffer. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 507 | * |
| 508 | * Returns true: if data filled sucessfully. |
| 509 | * false: packet full, or format mismatch. |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 510 | */ |
| 511 | static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SRV_LIST_ELEM& el, |
| 512 | BT_HDR* p_msg, uint16_t* p_len, |
| 513 | uint16_t s_hdl, uint16_t e_hdl) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 514 | tGATT_STATUS status = GATT_NOT_FOUND; |
| 515 | uint8_t* p; |
| 516 | uint16_t len = *p_len; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 517 | uint8_t info_pair_len[2] = {4, 18}; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 518 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 519 | if (!el.p_db) return status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 520 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 521 | /* check the attribute database */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 522 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 523 | p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 524 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 525 | for (auto& attr : el.p_db->attr_list) { |
| 526 | if (attr.handle > e_hdl) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 527 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 530 | if (attr.handle >= s_hdl) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 531 | if (p_msg->offset == 0) |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 532 | p_msg->offset = (attr.uuid.len == LEN_UUID_16) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 533 | ? GATT_INFO_TYPE_PAIR_16 |
| 534 | : GATT_INFO_TYPE_PAIR_128; |
| 535 | |
| 536 | if (len >= info_pair_len[p_msg->offset - 1]) { |
| 537 | if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 538 | attr.uuid.len == LEN_UUID_16) { |
| 539 | UINT16_TO_STREAM(p, attr.handle); |
| 540 | UINT16_TO_STREAM(p, attr.uuid.uu.uuid16); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 541 | } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 542 | attr.uuid.len == LEN_UUID_128) { |
| 543 | UINT16_TO_STREAM(p, attr.handle); |
| 544 | ARRAY_TO_STREAM(p, attr.uuid.uu.uuid128, LEN_UUID_128); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 545 | } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 546 | attr.uuid.len == LEN_UUID_32) { |
| 547 | UINT16_TO_STREAM(p, attr.handle); |
| 548 | gatt_convert_uuid32_to_uuid128(p, attr.uuid.uu.uuid32); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 549 | p += LEN_UUID_128; |
| 550 | } else { |
| 551 | GATT_TRACE_ERROR("format mismatch"); |
| 552 | status = GATT_NO_RESOURCES; |
| 553 | break; |
| 554 | /* format mismatch */ |
| 555 | } |
| 556 | p_msg->len += info_pair_len[p_msg->offset - 1]; |
| 557 | len -= info_pair_len[p_msg->offset - 1]; |
| 558 | status = GATT_SUCCESS; |
| 559 | |
| 560 | } else { |
| 561 | status = GATT_NO_RESOURCES; |
| 562 | break; |
| 563 | } |
| 564 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | *p_len = len; |
| 568 | return status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 572 | * |
| 573 | * Function gatts_internal_read_by_type_req |
| 574 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 575 | * Description Check to see if the ReadByType request can be handled |
| 576 | * internally. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 577 | * |
| 578 | * Returns void |
| 579 | * |
| 580 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 581 | static tGATT_STATUS gatts_validate_packet_format( |
| 582 | uint8_t op_code, uint16_t* p_len, uint8_t** p_data, tBT_UUID* p_uuid_filter, |
| 583 | uint16_t* p_s_hdl, uint16_t* p_e_hdl) { |
| 584 | tGATT_STATUS reason = GATT_SUCCESS; |
| 585 | uint16_t uuid_len, s_hdl = 0, e_hdl = 0; |
| 586 | uint16_t len = *p_len; |
| 587 | uint8_t* p = *p_data; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 588 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 589 | if (len >= 4) { |
| 590 | /* obtain starting handle, and ending handle */ |
| 591 | STREAM_TO_UINT16(s_hdl, p); |
| 592 | STREAM_TO_UINT16(e_hdl, p); |
| 593 | len -= 4; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 594 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 595 | if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || |
| 596 | !GATT_HANDLE_IS_VALID(e_hdl)) { |
| 597 | reason = GATT_INVALID_HANDLE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 598 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 599 | /* for these PDUs, uuid filter must present */ |
| 600 | else if (op_code == GATT_REQ_READ_BY_GRP_TYPE || |
| 601 | op_code == GATT_REQ_FIND_TYPE_VALUE || |
| 602 | op_code == GATT_REQ_READ_BY_TYPE) { |
| 603 | if (len >= 2 && p_uuid_filter != NULL) { |
| 604 | uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len; |
| 605 | |
| 606 | /* parse uuid now */ |
| 607 | if (gatt_parse_uuid_from_cmd(p_uuid_filter, uuid_len, &p) == false || |
| 608 | p_uuid_filter->len == 0) { |
| 609 | GATT_TRACE_DEBUG("UUID filter does not exsit"); |
| 610 | reason = GATT_INVALID_PDU; |
| 611 | } else |
| 612 | len -= p_uuid_filter->len; |
| 613 | } else |
Andre Eisenbach | ccf9c15 | 2013-10-02 15:37:21 -0700 | [diff] [blame] | 614 | reason = GATT_INVALID_PDU; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 615 | } |
| 616 | } else |
| 617 | reason = GATT_INVALID_PDU; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 618 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 619 | *p_data = p; |
| 620 | *p_len = len; |
| 621 | *p_s_hdl = s_hdl; |
| 622 | *p_e_hdl = e_hdl; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 623 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 624 | return reason; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 628 | * |
| 629 | * Function gatts_process_primary_service_req |
| 630 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 631 | * Description Process ReadByGroupType/ReadByTypeValue request, for |
| 632 | * discovering all primary services or discover primary service |
| 633 | * by UUID request. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 634 | * |
| 635 | * Returns void |
| 636 | * |
| 637 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 638 | void gatts_process_primary_service_req(tGATT_TCB* p_tcb, uint8_t op_code, |
| 639 | uint16_t len, uint8_t* p_data) { |
| 640 | uint8_t reason = GATT_INVALID_PDU; |
| 641 | uint16_t s_hdl = 0, e_hdl = 0; |
| 642 | tBT_UUID uuid, value, |
| 643 | primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}}; |
| 644 | BT_HDR* p_msg = NULL; |
| 645 | uint16_t msg_len = |
| 646 | (uint16_t)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 647 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 648 | memset(&value, 0, sizeof(tBT_UUID)); |
| 649 | reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, |
| 650 | &e_hdl); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 651 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 652 | if (reason == GATT_SUCCESS) { |
| 653 | if (gatt_uuid_compare(uuid, primary_service)) { |
| 654 | if (op_code == GATT_REQ_FIND_TYPE_VALUE) { |
| 655 | if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == false) |
| 656 | reason = GATT_INVALID_PDU; |
| 657 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 658 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 659 | if (reason == GATT_SUCCESS) { |
| 660 | p_msg = (BT_HDR*)osi_calloc(msg_len); |
| 661 | reason = gatt_build_primary_service_rsp(p_msg, p_tcb, op_code, s_hdl, |
| 662 | e_hdl, p_data, value); |
| 663 | } |
| 664 | } else { |
| 665 | if (op_code == GATT_REQ_READ_BY_GRP_TYPE) { |
| 666 | reason = GATT_UNSUPPORT_GRP_TYPE; |
| 667 | GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x", |
| 668 | uuid.uu.uuid16); |
| 669 | } else { |
| 670 | /* we do not support ReadByTypeValue with any non-primamry_service type |
| 671 | */ |
| 672 | reason = GATT_NOT_FOUND; |
| 673 | GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x", |
| 674 | uuid.uu.uuid16); |
| 675 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 676 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 677 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 678 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 679 | if (reason != GATT_SUCCESS) { |
| 680 | osi_free(p_msg); |
| 681 | gatt_send_error_rsp(p_tcb, reason, op_code, s_hdl, false); |
| 682 | } else |
| 683 | attp_send_sr_msg(p_tcb, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 687 | * |
| 688 | * Function gatts_process_find_info |
| 689 | * |
| 690 | * Description process find information request, for discover character |
| 691 | * descriptors. |
| 692 | * |
| 693 | * Returns void |
| 694 | * |
| 695 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 696 | static void gatts_process_find_info(tGATT_TCB* p_tcb, uint8_t op_code, |
| 697 | uint16_t len, uint8_t* p_data) { |
| 698 | uint8_t reason = GATT_INVALID_PDU, *p; |
| 699 | uint16_t s_hdl = 0, e_hdl = 0, buf_len; |
| 700 | BT_HDR* p_msg = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 701 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 702 | reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, |
| 703 | &e_hdl); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 704 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 705 | if (reason == GATT_SUCCESS) { |
| 706 | buf_len = |
| 707 | (uint16_t)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 708 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 709 | p_msg = (BT_HDR*)osi_calloc(buf_len); |
| 710 | reason = GATT_NOT_FOUND; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 711 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 712 | p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET; |
| 713 | *p++ = op_code + 1; |
| 714 | p_msg->len = 2; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 715 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 716 | buf_len = p_tcb->payload_size - 2; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 717 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 718 | for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) { |
| 719 | if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) { |
| 720 | reason = gatt_build_find_info_rsp(el, p_msg, &buf_len, s_hdl, e_hdl); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 721 | if (reason == GATT_NO_RESOURCES) { |
| 722 | reason = GATT_SUCCESS; |
| 723 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 724 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 725 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 726 | } |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 727 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 728 | *p = (uint8_t)p_msg->offset; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 729 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 730 | p_msg->offset = L2CAP_MIN_OFFSET; |
| 731 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 732 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 733 | if (reason != GATT_SUCCESS) { |
| 734 | osi_free(p_msg); |
| 735 | gatt_send_error_rsp(p_tcb, reason, op_code, s_hdl, false); |
| 736 | } else |
| 737 | attp_send_sr_msg(p_tcb, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 741 | * |
| 742 | * Function gatts_process_mtu_req |
| 743 | * |
| 744 | * Description This function is called to process excahnge MTU request. |
| 745 | * Only used on LE. |
| 746 | * |
| 747 | * Returns void |
| 748 | * |
| 749 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 750 | static void gatts_process_mtu_req(tGATT_TCB* p_tcb, uint16_t len, |
| 751 | uint8_t* p_data) { |
| 752 | uint16_t mtu = 0; |
| 753 | uint8_t *p = p_data, i; |
| 754 | BT_HDR* p_buf; |
| 755 | uint16_t conn_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 756 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 757 | /* BR/EDR conenction, send error response */ |
| 758 | if (p_tcb->att_lcid != L2CAP_ATT_CID) { |
| 759 | gatt_send_error_rsp(p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false); |
| 760 | } else if (len < GATT_MTU_REQ_MIN_LEN) { |
| 761 | GATT_TRACE_ERROR("invalid MTU request PDU received."); |
| 762 | gatt_send_error_rsp(p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false); |
| 763 | } else { |
| 764 | STREAM_TO_UINT16(mtu, p); |
| 765 | /* mtu must be greater than default MTU which is 23/48 */ |
| 766 | if (mtu < GATT_DEF_BLE_MTU_SIZE) |
| 767 | p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE; |
| 768 | else if (mtu > GATT_MAX_MTU_SIZE) |
| 769 | p_tcb->payload_size = GATT_MAX_MTU_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 770 | else |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 771 | p_tcb->payload_size = mtu; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 772 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 773 | GATT_TRACE_ERROR("MTU request PDU with MTU size %d", p_tcb->payload_size); |
Zhihai Xu | 52c0ccb | 2014-03-20 17:50:12 -0700 | [diff] [blame] | 774 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 775 | l2cble_set_fixed_channel_tx_data_length(p_tcb->peer_bda, L2CAP_ATT_CID, |
| 776 | p_tcb->payload_size); |
Priti Aghera | 636d671 | 2014-12-18 13:55:48 -0800 | [diff] [blame] | 777 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 778 | p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, |
| 779 | (tGATT_SR_MSG*)&p_tcb->payload_size); |
| 780 | if (p_buf != NULL) { |
| 781 | attp_send_sr_msg(p_tcb, p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 782 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 783 | /* Notify all registered applicaiton with new MTU size. Us a transaction |
| 784 | * ID */ |
| 785 | /* of 0, as no response is allowed from applcations */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 786 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 787 | for (i = 0; i < GATT_MAX_APPS; i++) { |
| 788 | if (gatt_cb.cl_rcb[i].in_use) { |
| 789 | conn_id = |
| 790 | GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_cb.cl_rcb[i].gatt_if); |
| 791 | gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, |
| 792 | (tGATTS_DATA*)&p_tcb->payload_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 793 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 794 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 795 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 796 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 800 | * |
| 801 | * Function gatts_process_read_by_type_req |
| 802 | * |
| 803 | * Description process Read By type request. |
| 804 | * This PDU can be used to perform: |
| 805 | * - read characteristic value |
| 806 | * - read characteristic descriptor value |
| 807 | * - discover characteristic |
| 808 | * - discover characteristic by UUID |
| 809 | * - relationship discovery |
| 810 | * |
| 811 | * Returns void |
| 812 | * |
| 813 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 814 | void gatts_process_read_by_type_req(tGATT_TCB* p_tcb, uint8_t op_code, |
| 815 | uint16_t len, uint8_t* p_data) { |
| 816 | tBT_UUID uuid; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 817 | size_t msg_len = sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET; |
| 818 | uint16_t buf_len, s_hdl, e_hdl, err_hdl = 0; |
| 819 | BT_HDR* p_msg = NULL; |
| 820 | tGATT_STATUS reason, ret; |
| 821 | uint8_t* p; |
| 822 | uint8_t sec_flag, key_size; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 823 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 824 | reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, |
| 825 | &e_hdl); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 826 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 827 | #if (GATT_CONFORMANCE_TESTING == TRUE) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 828 | if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) { |
| 829 | GATT_TRACE_DEBUG( |
| 830 | "Conformance tst: forced err rsp for ReadByType: error status=%d", |
| 831 | gatt_cb.err_status); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 832 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 833 | gatt_send_error_rsp(p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, |
| 834 | false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 835 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 836 | return; |
| 837 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 838 | #endif |
| 839 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 840 | if (reason == GATT_SUCCESS) { |
| 841 | p_msg = (BT_HDR*)osi_calloc(msg_len); |
| 842 | p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 843 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 844 | *p++ = op_code + 1; |
| 845 | /* reserve length byte */ |
| 846 | p_msg->len = 2; |
| 847 | buf_len = p_tcb->payload_size - 2; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 848 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 849 | reason = GATT_NOT_FOUND; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 850 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 851 | for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) { |
| 852 | if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 853 | gatt_sr_get_sec_info(p_tcb->peer_bda, p_tcb->transport, &sec_flag, |
| 854 | &key_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 855 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 856 | ret = gatts_db_read_attr_value_by_type(p_tcb, el.p_db, op_code, p_msg, |
| 857 | s_hdl, e_hdl, uuid, &buf_len, |
| 858 | sec_flag, key_size, 0, &err_hdl); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 859 | if (ret != GATT_NOT_FOUND) { |
| 860 | reason = ret; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 861 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 862 | if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 863 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 864 | if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) { |
| 865 | s_hdl = err_hdl; |
| 866 | break; |
| 867 | } |
| 868 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 869 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 870 | *p = (uint8_t)p_msg->offset; |
| 871 | p_msg->offset = L2CAP_MIN_OFFSET; |
| 872 | } |
| 873 | if (reason != GATT_SUCCESS) { |
| 874 | osi_free(p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 875 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 876 | /* in theroy BUSY is not possible(should already been checked), protected |
| 877 | * check */ |
| 878 | if (reason != GATT_PENDING && reason != GATT_BUSY) |
| 879 | gatt_send_error_rsp(p_tcb, reason, op_code, s_hdl, false); |
| 880 | } else |
| 881 | attp_send_sr_msg(p_tcb, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 884 | /** |
| 885 | * This function is called to process the write request from client. |
| 886 | */ |
| 887 | void gatts_process_write_req(tGATT_TCB* p_tcb, tGATT_SRV_LIST_ELEM& el, |
| 888 | uint16_t handle, uint8_t op_code, uint16_t len, |
| 889 | uint8_t* p_data, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 890 | bt_gatt_db_attribute_type_t gatt_type) { |
| 891 | tGATTS_DATA sr_data; |
| 892 | uint32_t trans_id; |
| 893 | tGATT_STATUS status; |
| 894 | uint8_t sec_flag, key_size, *p = p_data; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 895 | uint16_t conn_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 896 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 897 | memset(&sr_data, 0, sizeof(tGATTS_DATA)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 898 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 899 | switch (op_code) { |
| 900 | case GATT_REQ_PREPARE_WRITE: |
| 901 | if (len < 2) { |
| 902 | GATT_TRACE_ERROR( |
| 903 | "%s: Prepare write request was invalid - missing offset, sending " |
| 904 | "error response", |
| 905 | __func__); |
| 906 | gatt_send_error_rsp(p_tcb, GATT_INVALID_PDU, op_code, handle, false); |
| 907 | return; |
| 908 | } |
| 909 | sr_data.write_req.is_prep = true; |
| 910 | STREAM_TO_UINT16(sr_data.write_req.offset, p); |
| 911 | len -= 2; |
| 912 | /* fall through */ |
| 913 | case GATT_SIGN_CMD_WRITE: |
| 914 | if (op_code == GATT_SIGN_CMD_WRITE) { |
| 915 | GATT_TRACE_DEBUG("Write CMD with data sigining"); |
| 916 | len -= GATT_AUTH_SIGN_LEN; |
| 917 | } |
| 918 | /* fall through */ |
| 919 | case GATT_CMD_WRITE: |
| 920 | case GATT_REQ_WRITE: |
| 921 | if (op_code == GATT_REQ_WRITE || op_code == GATT_REQ_PREPARE_WRITE) |
| 922 | sr_data.write_req.need_rsp = true; |
| 923 | sr_data.write_req.handle = handle; |
| 924 | sr_data.write_req.len = len; |
| 925 | if (len != 0 && p != NULL) { |
| 926 | memcpy(sr_data.write_req.value, p, len); |
| 927 | } |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | gatt_sr_get_sec_info(p_tcb->peer_bda, p_tcb->transport, &sec_flag, &key_size); |
| 932 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 933 | status = gatts_write_attr_perm_check(el.p_db, op_code, handle, |
| 934 | sr_data.write_req.offset, p, len, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 935 | sec_flag, key_size); |
| 936 | |
| 937 | if (status == GATT_SUCCESS) { |
| 938 | trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle); |
| 939 | if (trans_id != 0) { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 940 | conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, el.gatt_if); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 941 | |
| 942 | uint8_t opcode = 0; |
| 943 | if (gatt_type == BTGATT_DB_DESCRIPTOR) { |
| 944 | opcode = GATTS_REQ_TYPE_WRITE_DESCRIPTOR; |
| 945 | } else if (gatt_type == BTGATT_DB_CHARACTERISTIC) { |
| 946 | opcode = GATTS_REQ_TYPE_WRITE_CHARACTERISTIC; |
| 947 | } else { |
| 948 | GATT_TRACE_ERROR( |
| 949 | "%s: Attempt to write attribute that's not tied with" |
| 950 | " characteristic or descriptor value.", |
| 951 | __func__); |
| 952 | status = GATT_ERROR; |
| 953 | } |
| 954 | |
| 955 | if (opcode) { |
| 956 | gatt_sr_send_req_callback(conn_id, trans_id, opcode, &sr_data); |
| 957 | status = GATT_PENDING; |
| 958 | } |
| 959 | } else { |
| 960 | GATT_TRACE_ERROR("max pending command, send error"); |
| 961 | status = GATT_BUSY; /* max pending command, application error */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 962 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 963 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 964 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 965 | /* in theroy BUSY is not possible(should already been checked), protected |
| 966 | * check */ |
| 967 | if (status != GATT_PENDING && status != GATT_BUSY && |
| 968 | (op_code == GATT_REQ_PREPARE_WRITE || op_code == GATT_REQ_WRITE)) { |
| 969 | gatt_send_error_rsp(p_tcb, status, op_code, handle, false); |
| 970 | } |
| 971 | return; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 974 | /** |
| 975 | * This function is called to process the read request from client. |
| 976 | */ |
| 977 | static void gatts_process_read_req(tGATT_TCB* p_tcb, tGATT_SRV_LIST_ELEM& el, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 978 | uint8_t op_code, uint16_t handle, |
| 979 | UNUSED_ATTR uint16_t len, uint8_t* p_data) { |
| 980 | size_t buf_len = sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET; |
| 981 | tGATT_STATUS reason; |
| 982 | uint8_t sec_flag, key_size, *p; |
| 983 | uint16_t offset = 0, value_len = 0; |
| 984 | BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 985 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 986 | if (op_code == GATT_REQ_READ_BLOB) STREAM_TO_UINT16(offset, p_data); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 987 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 988 | p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET; |
| 989 | *p++ = op_code + 1; |
| 990 | p_msg->len = 1; |
| 991 | buf_len = p_tcb->payload_size - 1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 992 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 993 | gatt_sr_get_sec_info(p_tcb->peer_bda, p_tcb->transport, &sec_flag, &key_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 994 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 995 | reason = gatts_read_attr_value_by_handle( |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 996 | p_tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len, |
| 997 | sec_flag, key_size, 0); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 998 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 999 | p_msg->len += value_len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1000 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1001 | if (reason != GATT_SUCCESS) { |
| 1002 | osi_free(p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1003 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1004 | /* in theroy BUSY is not possible(should already been checked), protected |
| 1005 | * check */ |
| 1006 | if (reason != GATT_PENDING && reason != GATT_BUSY) |
| 1007 | gatt_send_error_rsp(p_tcb, reason, op_code, handle, false); |
| 1008 | } else |
| 1009 | attp_send_sr_msg(p_tcb, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1013 | * |
| 1014 | * Function gatts_process_attribute_req |
| 1015 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1016 | * Description This function is called to process the per attribute handle |
| 1017 | * request from client. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1018 | * |
| 1019 | * Returns void |
| 1020 | * |
| 1021 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1022 | void gatts_process_attribute_req(tGATT_TCB* p_tcb, uint8_t op_code, |
| 1023 | uint16_t len, uint8_t* p_data) { |
| 1024 | uint16_t handle = 0; |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 1025 | uint8_t* p = p_data; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1026 | tGATT_STATUS status = GATT_INVALID_HANDLE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1027 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1028 | if (len < 2) { |
| 1029 | GATT_TRACE_ERROR("Illegal PDU length, discard request"); |
| 1030 | status = GATT_INVALID_PDU; |
| 1031 | } else { |
| 1032 | STREAM_TO_UINT16(handle, p); |
| 1033 | len -= 2; |
| 1034 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1035 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1036 | #if (GATT_CONFORMANCE_TESTING == TRUE) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1037 | gatt_cb.handle = handle; |
| 1038 | if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) { |
| 1039 | GATT_TRACE_DEBUG("Conformance tst: forced err rsp: error status=%d", |
| 1040 | gatt_cb.err_status); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1041 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1042 | gatt_send_error_rsp(p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, |
| 1043 | false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1044 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1045 | return; |
| 1046 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1047 | #endif |
| 1048 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1049 | if (GATT_HANDLE_IS_VALID(handle)) { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 1050 | for (auto& el : *gatt_cb.srv_list_info) { |
| 1051 | if (el.s_hdl <= handle && el.e_hdl >= handle) { |
| 1052 | for (const auto& attr : el.p_db->attr_list) { |
| 1053 | if (attr.handle == handle) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1054 | switch (op_code) { |
| 1055 | case GATT_REQ_READ: /* read char/char descriptor value */ |
| 1056 | case GATT_REQ_READ_BLOB: |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 1057 | gatts_process_read_req(p_tcb, el, op_code, handle, len, p); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1058 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1059 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1060 | case GATT_REQ_WRITE: /* write char/char descriptor value */ |
| 1061 | case GATT_CMD_WRITE: |
| 1062 | case GATT_SIGN_CMD_WRITE: |
| 1063 | case GATT_REQ_PREPARE_WRITE: |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 1064 | gatts_process_write_req(p_tcb, el, handle, op_code, len, p, |
| 1065 | attr.gatt_type); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1066 | break; |
| 1067 | default: |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1068 | break; |
| 1069 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1070 | status = GATT_SUCCESS; |
| 1071 | break; |
| 1072 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1073 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1074 | break; |
| 1075 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1076 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1077 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1078 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1079 | if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && |
| 1080 | op_code != GATT_SIGN_CMD_WRITE) |
| 1081 | gatt_send_error_rsp(p_tcb, status, op_code, handle, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1085 | * |
| 1086 | * Function gatts_proc_srv_chg_ind_ack |
| 1087 | * |
| 1088 | * Description This function process the service changed indicaiton ACK |
| 1089 | * |
| 1090 | * Returns void |
| 1091 | * |
| 1092 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1093 | static void gatts_proc_srv_chg_ind_ack(tGATT_TCB* p_tcb) { |
| 1094 | tGATTS_SRV_CHG_REQ req; |
| 1095 | tGATTS_SRV_CHG* p_buf = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1096 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1097 | GATT_TRACE_DEBUG("gatts_proc_srv_chg_ind_ack"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1098 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1099 | p_buf = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda); |
| 1100 | if (p_buf != NULL) { |
| 1101 | GATT_TRACE_DEBUG("NV update set srv chg = false"); |
| 1102 | p_buf->srv_changed = false; |
| 1103 | memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG)); |
| 1104 | if (gatt_cb.cb_info.p_srv_chg_callback) |
| 1105 | (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT, |
| 1106 | &req, NULL); |
| 1107 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1111 | * |
| 1112 | * Function gatts_chk_pending_ind |
| 1113 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1114 | * Description This function check any pending indication needs to be sent |
| 1115 | * if there is a pending indication then sent the indication |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1116 | * |
| 1117 | * Returns void |
| 1118 | * |
| 1119 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1120 | static void gatts_chk_pending_ind(tGATT_TCB* p_tcb) { |
| 1121 | GATT_TRACE_DEBUG("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1122 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1123 | tGATT_VALUE* p_buf = |
| 1124 | (tGATT_VALUE*)fixed_queue_try_peek_first(p_tcb->pending_ind_q); |
| 1125 | if (p_buf != NULL) { |
| 1126 | GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len, |
| 1127 | p_buf->value); |
| 1128 | osi_free(fixed_queue_try_remove_from_queue(p_tcb->pending_ind_q, p_buf)); |
| 1129 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1133 | * |
| 1134 | * Function gatts_proc_ind_ack |
| 1135 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1136 | * Description This function processes the Indication ack |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1137 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1138 | * Returns true continue to process the indication ack by the |
| 1139 | * application if the ACK is not a Service Changed Indication |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1140 | * |
| 1141 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1142 | static bool gatts_proc_ind_ack(tGATT_TCB* p_tcb, uint16_t ack_handle) { |
| 1143 | bool continue_processing = true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1144 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1145 | GATT_TRACE_DEBUG("gatts_proc_ind_ack ack handle=%d", ack_handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1146 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1147 | if (ack_handle == gatt_cb.handle_of_h_r) { |
| 1148 | gatts_proc_srv_chg_ind_ack(p_tcb); |
| 1149 | /* there is no need to inform the application since srv chg is handled |
| 1150 | * internally by GATT */ |
| 1151 | continue_processing = false; |
| 1152 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1153 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1154 | gatts_chk_pending_ind(p_tcb); |
| 1155 | return continue_processing; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1159 | * |
| 1160 | * Function gatts_process_value_conf |
| 1161 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1162 | * Description This function is called to process the handle value |
| 1163 | * confirmation. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1164 | * |
| 1165 | * Returns void |
| 1166 | * |
| 1167 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1168 | void gatts_process_value_conf(tGATT_TCB* p_tcb, uint8_t op_code) { |
| 1169 | uint16_t handle = p_tcb->indicate_handle; |
| 1170 | uint32_t trans_id; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1171 | bool continue_processing; |
| 1172 | uint16_t conn_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1173 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1174 | alarm_cancel(p_tcb->conf_timer); |
| 1175 | if (GATT_HANDLE_IS_VALID(handle)) { |
| 1176 | p_tcb->indicate_handle = 0; |
| 1177 | continue_processing = gatts_proc_ind_ack(p_tcb, handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1178 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1179 | if (continue_processing) { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 1180 | for (auto& el : *gatt_cb.srv_list_info) { |
| 1181 | if (el.s_hdl <= handle && el.e_hdl >= handle) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1182 | trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle); |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame^] | 1183 | conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, el.gatt_if); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1184 | gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_CONF, |
| 1185 | (tGATTS_DATA*)&handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1186 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1187 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1188 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1189 | } else { |
| 1190 | GATT_TRACE_ERROR("unexpected handle value confirmation"); |
| 1191 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1195 | * |
| 1196 | * Function gatt_server_handle_client_req |
| 1197 | * |
| 1198 | * Description This function is called to handle the client requests to |
| 1199 | * server. |
| 1200 | * |
| 1201 | * |
| 1202 | * Returns void |
| 1203 | * |
| 1204 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1205 | void gatt_server_handle_client_req(tGATT_TCB* p_tcb, uint8_t op_code, |
| 1206 | uint16_t len, uint8_t* p_data) { |
| 1207 | /* there is pending command, discard this one */ |
| 1208 | if (!gatt_sr_cmd_empty(p_tcb) && op_code != GATT_HANDLE_VALUE_CONF) return; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1209 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1210 | /* the size of the message may not be bigger than the local max PDU size*/ |
| 1211 | /* The message has to be smaller than the agreed MTU, len does not include op |
| 1212 | * code */ |
| 1213 | if (len >= p_tcb->payload_size) { |
| 1214 | GATT_TRACE_ERROR("server receive invalid PDU size:%d pdu size:%d", len + 1, |
| 1215 | p_tcb->payload_size); |
| 1216 | /* for invalid request expecting response, send it now */ |
| 1217 | if (op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE && |
| 1218 | op_code != GATT_HANDLE_VALUE_CONF) { |
| 1219 | gatt_send_error_rsp(p_tcb, GATT_INVALID_PDU, op_code, 0, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1220 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1221 | /* otherwise, ignore the pkt */ |
| 1222 | } else { |
| 1223 | switch (op_code) { |
| 1224 | case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */ |
| 1225 | case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */ |
| 1226 | gatts_process_primary_service_req(p_tcb, op_code, len, p_data); |
| 1227 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1228 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1229 | case GATT_REQ_FIND_INFO: /* discover char descrptor */ |
| 1230 | gatts_process_find_info(p_tcb, op_code, len, p_data); |
| 1231 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1232 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1233 | case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor |
| 1234 | value */ |
| 1235 | /* discover characteristic, discover char by UUID */ |
| 1236 | gatts_process_read_by_type_req(p_tcb, op_code, len, p_data); |
| 1237 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1238 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1239 | case GATT_REQ_READ: /* read char/char descriptor value */ |
| 1240 | case GATT_REQ_READ_BLOB: |
| 1241 | case GATT_REQ_WRITE: /* write char/char descriptor value */ |
| 1242 | case GATT_CMD_WRITE: |
| 1243 | case GATT_SIGN_CMD_WRITE: |
| 1244 | case GATT_REQ_PREPARE_WRITE: |
| 1245 | gatts_process_attribute_req(p_tcb, op_code, len, p_data); |
| 1246 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1247 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1248 | case GATT_HANDLE_VALUE_CONF: |
| 1249 | gatts_process_value_conf(p_tcb, op_code); |
| 1250 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1251 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1252 | case GATT_REQ_MTU: |
| 1253 | gatts_process_mtu_req(p_tcb, len, p_data); |
| 1254 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1255 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1256 | case GATT_REQ_EXEC_WRITE: |
| 1257 | gatt_process_exec_write_req(p_tcb, op_code, len, p_data); |
| 1258 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1259 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1260 | case GATT_REQ_READ_MULTI: |
| 1261 | gatt_process_read_multi_req(p_tcb, op_code, len, p_data); |
| 1262 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1263 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1264 | default: |
| 1265 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1266 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1267 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1268 | } |