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