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