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