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