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