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 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 45 | uint32_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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 47 | uint32_t trans_id = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 48 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 49 | 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 54 | trans_id = ++tcb.trans_id; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 55 | } else { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 56 | p_cmd->trans_id = ++tcb.trans_id; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 57 | p_cmd->op_code = op_code; |
| 58 | p_cmd->handle = handle; |
| 59 | p_cmd->status = GATT_NOT_FOUND; |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 60 | tcb.trans_id %= GATT_TRANS_ID_MAX; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 61 | trans_id = p_cmd->trans_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 62 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 63 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 64 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 65 | return trans_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 69 | * |
| 70 | * Function gatt_sr_cmd_empty |
| 71 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 72 | * Description This function checks if the server command queue is empty. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 73 | * |
| 74 | * Returns true if empty, false if there is pending command. |
| 75 | * |
| 76 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 77 | bool gatt_sr_cmd_empty(tGATT_TCB& tcb) { return (tcb.sr_cmd.op_code == 0); } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 78 | |
| 79 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 80 | * |
| 81 | * Function gatt_dequeue_sr_cmd |
| 82 | * |
| 83 | * Description This function dequeue the request from command queue. |
| 84 | * |
| 85 | * Returns void |
| 86 | * |
| 87 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 88 | void gatt_dequeue_sr_cmd(tGATT_TCB& tcb) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 89 | /* Double check in case any buffers are queued */ |
| 90 | GATT_TRACE_DEBUG("gatt_dequeue_sr_cmd"); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 91 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 94 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 95 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 102 | * |
| 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 110 | static 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 115 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 116 | GATT_TRACE_DEBUG("%s status=%d mtu=%d", __func__, status, mtu); |
Subramanian Srinivasan | 089cd11 | 2016-05-16 11:14:03 -0700 | [diff] [blame] | 117 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 118 | if (p_cmd->multi_rsp_q == NULL) |
| 119 | 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] | 120 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 121 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 125 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 126 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 138 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 139 | /* First byte in the response is the opcode */ |
| 140 | *p++ = GATT_RSP_READ_MULTI; |
| 141 | p_buf->len = 1; |
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 | /* 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 Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 151 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 152 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 158 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 159 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 160 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 215 | * |
| 216 | * Function gatt_sr_process_app_rsp |
| 217 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 218 | * Description This function checks whether the response message from |
| 219 | * application matches any pending request. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 220 | * |
| 221 | * Returns void |
| 222 | * |
| 223 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 224 | tGATT_STATUS gatt_sr_process_app_rsp(tGATT_TCB& tcb, tGATT_IF gatt_if, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 225 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 229 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 230 | GATT_TRACE_DEBUG("%s gatt_if=%d", __func__, gatt_if); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 231 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 232 | gatt_sr_update_cback_cnt(tcb, gatt_if, false, false); |
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 | if (op_code == GATT_REQ_READ_MULTI) { |
| 235 | /* If no error and still waiting, just return */ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 236 | if (!process_read_multi_rsp(&tcb.sr_cmd, status, p_msg, tcb.payload_size)) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 237 | return (GATT_SUCCESS); |
| 238 | } else { |
| 239 | if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS) |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 240 | gatt_sr_update_prep_cnt(tcb, gatt_if, true, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 241 | |
| 242 | if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS) |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 243 | gatt_sr_reset_cback_cnt(tcb); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 244 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 245 | tcb.sr_cmd.status = status; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 246 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 247 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 251 | } else { |
| 252 | GATT_TRACE_ERROR("Exception!!! already has respond message"); |
| 253 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 254 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 255 | } |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 256 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 260 | } else { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 261 | ret_code = |
| 262 | gatt_send_error_rsp(tcb, status, op_code, tcb.sr_cmd.handle, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 265 | gatt_dequeue_sr_cmd(tcb); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 266 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 267 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 268 | GATT_TRACE_DEBUG("%s ret_code=%d", __func__, ret_code); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 269 | |
| 270 | return ret_code; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 274 | * |
| 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 283 | void gatt_process_exec_write_req(tGATT_TCB& tcb, uint8_t op_code, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 284 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 289 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 290 | #if (GATT_CONFORMANCE_TESTING == TRUE) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 291 | 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 Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 294 | gatt_cb.err_status); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 295 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 296 | gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 297 | gatt_cb.handle, false); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 298 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 299 | return; |
| 300 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 301 | #endif |
| 302 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 303 | STREAM_TO_UINT8(flag, p); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 304 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 305 | /* mask the flag */ |
| 306 | flag &= GATT_PREP_WRITE_EXEC; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 307 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 308 | /* no prep write is queued */ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 309 | 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 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 | for (i = 0; i < GATT_MAX_APPS; i++) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 314 | if (tcb.prep_cnt[i]) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 315 | gatt_if = (tGATT_IF)(i + 1); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 316 | conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_if); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 317 | gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE_EXEC, |
| 318 | (tGATTS_DATA*)&flag); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 319 | tcb.prep_cnt[i] = 0; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 320 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 321 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 322 | } else /* nothing needs to be executed , send response now */ |
| 323 | { |
| 324 | GATT_TRACE_ERROR("gatt_process_exec_write_req: no prepare write pending"); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 325 | gatt_send_error_rsp(tcb, GATT_ERROR, GATT_REQ_EXEC_WRITE, 0, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 326 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 330 | * |
| 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 339 | void gatt_process_read_multi_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len, |
| 340 | uint8_t* p_data) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 341 | uint32_t trans_id; |
| 342 | uint16_t handle = 0, ll = len; |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 343 | uint8_t* p = p_data; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 344 | tGATT_STATUS err = GATT_SUCCESS; |
| 345 | uint8_t sec_flag, key_size; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 346 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 347 | GATT_TRACE_DEBUG("gatt_process_read_multi_req"); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 348 | tcb.sr_cmd.multi_req.num_handles = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 349 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 350 | gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 351 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 352 | #if (GATT_CONFORMANCE_TESTING == TRUE) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 353 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 357 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 358 | STREAM_TO_UINT16(handle, p); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 359 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 360 | gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 361 | false); |
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 | return; |
| 364 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 365 | #endif |
| 366 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 367 | while (ll >= 2 && |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 368 | tcb.sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 369 | STREAM_TO_UINT16(handle, p); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 370 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 371 | auto it = gatt_sr_find_i_rcb_by_handle(handle); |
| 372 | if (it != gatt_cb.srv_list_info->end()) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 373 | tcb.sr_cmd.multi_req.handles[tcb.sr_cmd.multi_req.num_handles++] = handle; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 374 | |
| 375 | /* check read permission */ |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 376 | err = gatts_read_attr_perm_check(it->p_db, false, handle, sec_flag, |
| 377 | key_size); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 378 | 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 394 | if (tcb.sr_cmd.multi_req.num_handles == 0) err = GATT_INVALID_HANDLE; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 395 | |
| 396 | if (err == GATT_SUCCESS) { |
| 397 | trans_id = |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 398 | gatt_sr_enqueue_cmd(tcb, op_code, tcb.sr_cmd.multi_req.handles[0]); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 399 | if (trans_id != 0) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 400 | gatt_sr_reset_cback_cnt(tcb); /* read multiple use multi_rsp_q's count*/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 401 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 402 | for (ll = 0; ll < tcb.sr_cmd.multi_req.num_handles; ll++) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 403 | tGATTS_RSP* p_msg = (tGATTS_RSP*)osi_calloc(sizeof(tGATTS_RSP)); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 404 | handle = tcb.sr_cmd.multi_req.handles[ll]; |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 405 | 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] | 406 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 407 | p_msg->attr_value.handle = handle; |
| 408 | err = gatts_read_attr_value_by_handle( |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 409 | tcb, it->p_db, op_code, handle, 0, p_msg->attr_value.value, |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 410 | &p_msg->attr_value.len, GATT_MAX_ATTR_LEN, sec_flag, key_size, |
| 411 | trans_id); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 412 | |
| 413 | if (err == GATT_SUCCESS) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 414 | gatt_sr_process_app_rsp(tcb, it->gatt_if, trans_id, op_code, |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 415 | GATT_SUCCESS, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 416 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 417 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 423 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 424 | /* 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 427 | gatt_send_error_rsp(tcb, err, op_code, handle, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 431 | * |
| 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 440 | static tGATT_STATUS gatt_build_primary_service_rsp( |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 441 | BT_HDR* p_msg, tGATT_TCB& tcb, uint8_t op_code, uint16_t s_hdl, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 442 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 445 | tBT_UUID* p_uuid; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 446 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 447 | p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 448 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 449 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 453 | if (p_uuid != NULL) { |
| 454 | 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] | 455 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 456 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 461 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 462 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 466 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 467 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 468 | if (p_msg->len + p_msg->offset <= tcb.payload_size && |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 469 | handle_len == p_msg->offset) { |
| 470 | if (op_code != GATT_REQ_FIND_TYPE_VALUE || |
| 471 | gatt_uuid_compare(value, *p_uuid)) { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 472 | UINT16_TO_STREAM(p, el.s_hdl); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 473 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 474 | if (gatt_cb.last_primary_s_handle && |
| 475 | gatt_cb.last_primary_s_handle == el.s_hdl) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 476 | GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute"); |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 477 | /* see GATT ERRATA 4065, 4063, ATT ERRATA 4062 */ |
| 478 | UINT16_TO_STREAM(p, 0xFFFF); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 479 | } else { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 480 | UINT16_TO_STREAM(p, el.e_hdl); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 481 | } |
| 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 493 | } |
| 494 | p_msg->offset = L2CAP_MIN_OFFSET; |
| 495 | |
| 496 | return status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 499 | /** |
| 500 | * fill the find information response information in the given buffer. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 501 | * |
| 502 | * Returns true: if data filled sucessfully. |
| 503 | * false: packet full, or format mismatch. |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 504 | */ |
| 505 | static 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 508 | tGATT_STATUS status = GATT_NOT_FOUND; |
| 509 | uint8_t* p; |
| 510 | uint16_t len = *p_len; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 511 | uint8_t info_pair_len[2] = {4, 18}; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 512 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 513 | if (!el.p_db) return status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 514 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 515 | /* check the attribute database */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 516 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 517 | 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] | 518 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 519 | for (auto& attr : el.p_db->attr_list) { |
| 520 | if (attr.handle > e_hdl) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 521 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 524 | if (attr.handle >= s_hdl) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 525 | if (p_msg->offset == 0) |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 526 | p_msg->offset = (attr.uuid.len == LEN_UUID_16) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 527 | ? 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 Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 532 | attr.uuid.len == LEN_UUID_16) { |
| 533 | UINT16_TO_STREAM(p, attr.handle); |
| 534 | UINT16_TO_STREAM(p, attr.uuid.uu.uuid16); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 535 | } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 536 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 539 | } else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 540 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 543 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | *p_len = len; |
| 562 | return status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 566 | * |
| 567 | * Function gatts_internal_read_by_type_req |
| 568 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 569 | * Description Check to see if the ReadByType request can be handled |
| 570 | * internally. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 571 | * |
| 572 | * Returns void |
| 573 | * |
| 574 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 575 | static 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 582 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 583 | 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 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 (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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 592 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 593 | /* 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 Eisenbach | ccf9c15 | 2013-10-02 15:37:21 -0700 | [diff] [blame] | 608 | reason = GATT_INVALID_PDU; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 609 | } |
| 610 | } else |
| 611 | reason = GATT_INVALID_PDU; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 612 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 613 | *p_data = p; |
| 614 | *p_len = len; |
| 615 | *p_s_hdl = s_hdl; |
| 616 | *p_e_hdl = e_hdl; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 617 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 618 | return reason; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 622 | * |
| 623 | * Function gatts_process_primary_service_req |
| 624 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 625 | * Description Process ReadByGroupType/ReadByTypeValue request, for |
| 626 | * discovering all primary services or discover primary service |
| 627 | * by UUID request. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 628 | * |
| 629 | * Returns void |
| 630 | * |
| 631 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 632 | void gatts_process_primary_service_req(tGATT_TCB& tcb, uint8_t op_code, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 633 | 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 640 | (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 641 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 642 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 645 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 646 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 652 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 653 | if (reason == GATT_SUCCESS) { |
| 654 | p_msg = (BT_HDR*)osi_calloc(msg_len); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 655 | reason = gatt_build_primary_service_rsp(p_msg, tcb, op_code, s_hdl, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 656 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 670 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 671 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 672 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 673 | if (reason != GATT_SUCCESS) { |
| 674 | osi_free(p_msg); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 675 | gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 676 | } else |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 677 | attp_send_sr_msg(tcb, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 681 | * |
| 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 690 | static void gatts_process_find_info(tGATT_TCB& tcb, uint8_t op_code, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 691 | uint16_t len, uint8_t* p_data) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 692 | uint16_t s_hdl = 0, e_hdl = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 693 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 694 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 700 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 701 | uint16_t buf_len = |
| 702 | (uint16_t)(sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 703 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 704 | BT_HDR* p_msg = (BT_HDR*)osi_calloc(buf_len); |
| 705 | reason = GATT_NOT_FOUND; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 706 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 707 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 710 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 711 | buf_len = tcb.payload_size - 2; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 712 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 713 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 719 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 720 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 721 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 722 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 723 | *p = (uint8_t)p_msg->offset; |
| 724 | |
| 725 | p_msg->offset = L2CAP_MIN_OFFSET; |
| 726 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 727 | if (reason != GATT_SUCCESS) { |
| 728 | osi_free(p_msg); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 729 | gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 730 | } else |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 731 | attp_send_sr_msg(tcb, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 735 | * |
| 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 744 | static void gatts_process_mtu_req(tGATT_TCB& tcb, uint16_t len, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 745 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 750 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 751 | /* BR/EDR conenction, send error response */ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 752 | if (tcb.att_lcid != L2CAP_ATT_CID) { |
| 753 | gatt_send_error_rsp(tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 754 | } else if (len < GATT_MTU_REQ_MIN_LEN) { |
| 755 | GATT_TRACE_ERROR("invalid MTU request PDU received."); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 756 | gatt_send_error_rsp(tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 757 | } 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 761 | tcb.payload_size = GATT_DEF_BLE_MTU_SIZE; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 762 | else if (mtu > GATT_MAX_MTU_SIZE) |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 763 | tcb.payload_size = GATT_MAX_MTU_SIZE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 764 | else |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 765 | tcb.payload_size = mtu; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 766 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 767 | GATT_TRACE_ERROR("MTU request PDU with MTU size %d", tcb.payload_size); |
Zhihai Xu | 52c0ccb | 2014-03-20 17:50:12 -0700 | [diff] [blame] | 768 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 769 | l2cble_set_fixed_channel_tx_data_length(tcb.peer_bda, L2CAP_ATT_CID, |
| 770 | tcb.payload_size); |
Priti Aghera | 636d671 | 2014-12-18 13:55:48 -0800 | [diff] [blame] | 771 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 772 | p_buf = |
| 773 | attp_build_sr_msg(tcb, GATT_RSP_MTU, (tGATT_SR_MSG*)&tcb.payload_size); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 774 | if (p_buf != NULL) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 775 | attp_send_sr_msg(tcb, p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 776 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 777 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 780 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 781 | for (i = 0; i < GATT_MAX_APPS; i++) { |
| 782 | if (gatt_cb.cl_rcb[i].in_use) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 783 | conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, gatt_cb.cl_rcb[i].gatt_if); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 784 | gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU, |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 785 | (tGATTS_DATA*)&tcb.payload_size); |
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 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 788 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 789 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 793 | * |
| 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 807 | void gatts_process_read_by_type_req(tGATT_TCB& tcb, uint8_t op_code, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 808 | uint16_t len, uint8_t* p_data) { |
| 809 | tBT_UUID uuid; |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 810 | size_t msg_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 811 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 816 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 817 | reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, |
| 818 | &e_hdl); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 819 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 820 | #if (GATT_CONFORMANCE_TESTING == TRUE) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 821 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 825 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 826 | gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 827 | false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 828 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 829 | return; |
| 830 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 831 | #endif |
| 832 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 833 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 836 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 837 | *p++ = op_code + 1; |
| 838 | /* reserve length byte */ |
| 839 | p_msg->len = 2; |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 840 | buf_len = tcb.payload_size - 2; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 841 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 842 | reason = GATT_NOT_FOUND; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 843 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 844 | for (tGATT_SRV_LIST_ELEM& el : *gatt_cb.srv_list_info) { |
| 845 | if (el.s_hdl <= e_hdl && el.e_hdl >= s_hdl) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 846 | gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 847 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 848 | ret = gatts_db_read_attr_value_by_type(tcb, el.p_db, op_code, p_msg, |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 849 | s_hdl, e_hdl, uuid, &buf_len, |
| 850 | sec_flag, key_size, 0, &err_hdl); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 851 | if (ret != GATT_NOT_FOUND) { |
| 852 | reason = ret; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 853 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 854 | if (ret == GATT_NO_RESOURCES) reason = GATT_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 855 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 856 | if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) { |
| 857 | s_hdl = err_hdl; |
| 858 | break; |
| 859 | } |
| 860 | } |
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 | *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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 867 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 868 | /* in theroy BUSY is not possible(should already been checked), protected |
| 869 | * check */ |
| 870 | if (reason != GATT_PENDING && reason != GATT_BUSY) |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 871 | gatt_send_error_rsp(tcb, reason, op_code, s_hdl, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 872 | } else |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 873 | attp_send_sr_msg(tcb, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 876 | /** |
| 877 | * This function is called to process the write request from client. |
| 878 | */ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 879 | void gatts_process_write_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el, |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 880 | uint16_t handle, uint8_t op_code, uint16_t len, |
| 881 | uint8_t* p_data, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 882 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 887 | uint16_t conn_id; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 888 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 889 | memset(&sr_data, 0, sizeof(tGATTS_DATA)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 890 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 891 | 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 898 | gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, handle, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 899 | 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 923 | gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 924 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 925 | status = gatts_write_attr_perm_check(el.p_db, op_code, handle, |
| 926 | sr_data.write_req.offset, p, len, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 927 | sec_flag, key_size); |
| 928 | |
| 929 | if (status == GATT_SUCCESS) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 930 | trans_id = gatt_sr_enqueue_cmd(tcb, op_code, handle); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 931 | if (trans_id != 0) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 932 | conn_id = GATT_CREATE_CONN_ID(tcb.tcb_idx, el.gatt_if); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 933 | |
| 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 954 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 955 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 956 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 957 | /* 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 961 | gatt_send_error_rsp(tcb, status, op_code, handle, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 962 | } |
| 963 | return; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 966 | /** |
| 967 | * This function is called to process the read request from client. |
| 968 | */ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 969 | static void gatts_process_read_req(tGATT_TCB& tcb, tGATT_SRV_LIST_ELEM& el, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 970 | uint8_t op_code, uint16_t handle, |
| 971 | UNUSED_ATTR uint16_t len, uint8_t* p_data) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 972 | size_t buf_len = sizeof(BT_HDR) + tcb.payload_size + L2CAP_MIN_OFFSET; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 973 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 977 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 978 | 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] | 979 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 980 | p = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET; |
| 981 | *p++ = op_code + 1; |
| 982 | p_msg->len = 1; |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 983 | buf_len = tcb.payload_size - 1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 984 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 985 | gatt_sr_get_sec_info(tcb.peer_bda, tcb.transport, &sec_flag, &key_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 986 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 987 | reason = gatts_read_attr_value_by_handle( |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 988 | tcb, el.p_db, op_code, handle, offset, p, &value_len, (uint16_t)buf_len, |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 989 | sec_flag, key_size, 0); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 990 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 991 | p_msg->len += value_len; |
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 | if (reason != GATT_SUCCESS) { |
| 994 | osi_free(p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 995 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 996 | /* in theroy BUSY is not possible(should already been checked), protected |
| 997 | * check */ |
| 998 | if (reason != GATT_PENDING && reason != GATT_BUSY) |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 999 | gatt_send_error_rsp(tcb, reason, op_code, handle, false); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1000 | } else |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1001 | attp_send_sr_msg(tcb, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1005 | * |
| 1006 | * Function gatts_process_attribute_req |
| 1007 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1008 | * Description This function is called to process the per attribute handle |
| 1009 | * request from client. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1010 | * |
| 1011 | * Returns void |
| 1012 | * |
| 1013 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1014 | void gatts_process_attribute_req(tGATT_TCB& tcb, uint8_t op_code, uint16_t len, |
| 1015 | uint8_t* p_data) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1016 | uint16_t handle = 0; |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 1017 | uint8_t* p = p_data; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1018 | tGATT_STATUS status = GATT_INVALID_HANDLE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1019 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1020 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1027 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 1028 | #if (GATT_CONFORMANCE_TESTING == TRUE) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1029 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1033 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1034 | gatt_send_error_rsp(tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1035 | false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1036 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1037 | return; |
| 1038 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1039 | #endif |
| 1040 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1041 | if (GATT_HANDLE_IS_VALID(handle)) { |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 1042 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1046 | switch (op_code) { |
| 1047 | case GATT_REQ_READ: /* read char/char descriptor value */ |
| 1048 | case GATT_REQ_READ_BLOB: |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1049 | gatts_process_read_req(tcb, el, op_code, handle, len, p); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1050 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1051 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1052 | 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1056 | gatts_process_write_req(tcb, el, handle, op_code, len, p, |
Jakub Pawlowski | 6395f15 | 2017-05-09 05:02:38 -0700 | [diff] [blame] | 1057 | attr.gatt_type); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1058 | break; |
| 1059 | default: |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1060 | break; |
| 1061 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1062 | status = GATT_SUCCESS; |
| 1063 | break; |
| 1064 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1065 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1066 | break; |
| 1067 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1068 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1069 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1070 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1071 | if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && |
| 1072 | op_code != GATT_SIGN_CMD_WRITE) |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1073 | gatt_send_error_rsp(tcb, status, op_code, handle, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1077 | * |
| 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1085 | static void gatts_proc_srv_chg_ind_ack(tGATT_TCB tcb) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1086 | tGATTS_SRV_CHG_REQ req; |
| 1087 | tGATTS_SRV_CHG* p_buf = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1088 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1089 | GATT_TRACE_DEBUG("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1090 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1091 | p_buf = gatt_is_bda_in_the_srv_chg_clt_list(tcb.peer_bda); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1092 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1103 | * |
| 1104 | * Function gatts_chk_pending_ind |
| 1105 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1106 | * Description This function check any pending indication needs to be sent |
| 1107 | * if there is a pending indication then sent the indication |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1108 | * |
| 1109 | * Returns void |
| 1110 | * |
| 1111 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1112 | static void gatts_chk_pending_ind(tGATT_TCB& tcb) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1113 | GATT_TRACE_DEBUG("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1114 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1115 | tGATT_VALUE* p_buf = |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1116 | (tGATT_VALUE*)fixed_queue_try_peek_first(tcb.pending_ind_q); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1117 | if (p_buf != NULL) { |
| 1118 | GATTS_HandleValueIndication(p_buf->conn_id, p_buf->handle, p_buf->len, |
| 1119 | p_buf->value); |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1120 | osi_free(fixed_queue_try_remove_from_queue(tcb.pending_ind_q, p_buf)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1121 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1125 | * |
| 1126 | * Function gatts_proc_ind_ack |
| 1127 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1128 | * Description This function processes the Indication ack |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1129 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1130 | * Returns true continue to process the indication ack by the |
| 1131 | * application if the ACK is not a Service Changed Indication |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1132 | * |
| 1133 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1134 | static bool gatts_proc_ind_ack(tGATT_TCB& tcb, uint16_t ack_handle) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1135 | bool continue_processing = true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1136 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1137 | 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] | 1138 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1139 | if (ack_handle == gatt_cb.handle_of_h_r) { |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1140 | gatts_proc_srv_chg_ind_ack(tcb); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1141 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1145 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1146 | gatts_chk_pending_ind(tcb); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1147 | return continue_processing; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1151 | * |
| 1152 | * Function gatts_process_value_conf |
| 1153 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1154 | * Description This function is called to process the handle value |
| 1155 | * confirmation. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1156 | * |
| 1157 | * Returns void |
| 1158 | * |
| 1159 | ******************************************************************************/ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1160 | void gatts_process_value_conf(tGATT_TCB& tcb, uint8_t op_code) { |
| 1161 | uint16_t handle = tcb.indicate_handle; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1162 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1163 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1168 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1169 | 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1179 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1180 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1181 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1184 | /** This function is called to handle the client requests to server */ |
| 1185 | void gatt_server_handle_client_req(tGATT_TCB& tcb, uint8_t op_code, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1186 | uint16_t len, uint8_t* p_data) { |
| 1187 | /* there is pending command, discard this one */ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1188 | if (!gatt_sr_cmd_empty(tcb) && op_code != GATT_HANDLE_VALUE_CONF) return; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1189 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1190 | /* 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1193 | if (len >= tcb.payload_size) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1194 | GATT_TRACE_ERROR("server receive invalid PDU size:%d pdu size:%d", len + 1, |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1195 | tcb.payload_size); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1196 | /* 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1199 | gatt_send_error_rsp(tcb, GATT_INVALID_PDU, op_code, 0, false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1200 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1201 | /* 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1206 | gatts_process_primary_service_req(tcb, op_code, len, p_data); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1207 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1208 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1209 | case GATT_REQ_FIND_INFO: /* discover char descrptor */ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1210 | gatts_process_find_info(tcb, op_code, len, p_data); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1211 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1212 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1213 | case GATT_REQ_READ_BY_TYPE: /* read characteristic value, char descriptor |
| 1214 | value */ |
| 1215 | /* discover characteristic, discover char by UUID */ |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1216 | gatts_process_read_by_type_req(tcb, op_code, len, p_data); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1217 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1218 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1219 | 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 Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1225 | gatts_process_attribute_req(tcb, op_code, len, p_data); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1226 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1227 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1228 | case GATT_HANDLE_VALUE_CONF: |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1229 | gatts_process_value_conf(tcb, op_code); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1230 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1231 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1232 | case GATT_REQ_MTU: |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1233 | gatts_process_mtu_req(tcb, len, p_data); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1234 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1235 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1236 | case GATT_REQ_EXEC_WRITE: |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1237 | gatt_process_exec_write_req(tcb, op_code, len, p_data); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1238 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1239 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1240 | case GATT_REQ_READ_MULTI: |
Jakub Pawlowski | f4c0292 | 2017-05-30 11:21:04 -0700 | [diff] [blame] | 1241 | gatt_process_read_multi_req(tcb, op_code, len, p_data); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1242 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1243 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1244 | default: |
| 1245 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1246 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1247 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1248 | } |