The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 1999-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 Serial Port API code |
| 22 | * |
| 23 | ******************************************************************************/ |
| 24 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 25 | #define LOG_TAG "bt_port_api" |
| 26 | |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 27 | #include <base/logging.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 28 | #include <string.h> |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 29 | |
Andre Eisenbach | d3842ed | 2015-10-12 18:32:46 -0700 | [diff] [blame] | 30 | #include "osi/include/log.h" |
| 31 | #include "osi/include/mutex.h" |
| 32 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 33 | #include "bt_common.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 34 | #include "btm_api.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 35 | #include "btm_int.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 36 | #include "l2c_api.h" |
Andre Eisenbach | d3842ed | 2015-10-12 18:32:46 -0700 | [diff] [blame] | 37 | #include "port_api.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 38 | #include "port_int.h" |
| 39 | #include "rfc_int.h" |
| 40 | #include "rfcdefs.h" |
| 41 | #include "sdp_api.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 42 | |
| 43 | /* duration of break in 200ms units */ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 44 | #define PORT_BREAK_DURATION 1 |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 45 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 46 | #define info(fmt, ...) LOG_INFO(LOG_TAG, "%s: " fmt, __func__, ##__VA_ARGS__) |
| 47 | #define debug(fmt, ...) LOG_DEBUG(LOG_TAG, "%s: " fmt, __func__, ##__VA_ARGS__) |
| 48 | #define error(fmt, ...) \ |
| 49 | LOG_ERROR(LOG_TAG, "## ERROR : %s: " fmt "##", __func__, ##__VA_ARGS__) |
| 50 | #define asrt(s) \ |
| 51 | if (!(s)) \ |
| 52 | LOG_ERROR(LOG_TAG, "## %s assert %s failed at line:%d ##", __func__, #s, \ |
| 53 | __LINE__) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 54 | |
June R. Tate-Gans | 1e8cb56 | 2014-12-02 12:48:03 -0800 | [diff] [blame] | 55 | /* Mapping from PORT_* result codes to human readable strings. */ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 56 | static const char* result_code_strings[] = {"Success", |
| 57 | "Unknown error", |
| 58 | "Already opened", |
| 59 | "Command pending", |
| 60 | "App not registered", |
| 61 | "No memory", |
| 62 | "No resources", |
| 63 | "Bad BD address", |
| 64 | "Unspecified error", |
| 65 | "Bad handle", |
| 66 | "Not opened", |
| 67 | "Line error", |
| 68 | "Start failed", |
| 69 | "Parameter negotiation failed", |
| 70 | "Port negotiation failed", |
| 71 | "Sec failed", |
| 72 | "Peer connection failed", |
| 73 | "Peer failed", |
| 74 | "Peer timeout", |
| 75 | "Closed", |
| 76 | "TX full", |
| 77 | "Local closed", |
| 78 | "Local timeout", |
| 79 | "TX queue disabled", |
| 80 | "Page timeout", |
| 81 | "Invalid SCN", |
| 82 | "Unknown result code"}; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 83 | |
| 84 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 85 | * |
| 86 | * Function RFCOMM_CreateConnection |
| 87 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 88 | * Description RFCOMM_CreateConnection function is used from the |
| 89 | * application to establish serial port connection to the peer |
| 90 | * device, or allow RFCOMM to accept a connection from the peer |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 91 | * application. |
| 92 | * |
| 93 | * Parameters: scn - Service Channel Number as registered with |
| 94 | * the SDP (server) or obtained using SDP from |
| 95 | * the peer device (client). |
| 96 | * is_server - true if requesting application is a server |
| 97 | * mtu - Maximum frame size the application can accept |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 98 | * bd_addr - address of the peer (client) |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 99 | * mask - specifies events to be enabled. A value |
| 100 | * of zero disables all events. |
| 101 | * p_handle - OUT pointer to the handle. |
| 102 | * p_mgmt_cb - pointer to callback function to receive |
| 103 | * connection up/down events. |
| 104 | * Notes: |
| 105 | * |
| 106 | * Server can call this function with the same scn parameter multiple times if |
| 107 | * it is ready to accept multiple simulteneous connections. |
| 108 | * |
| 109 | * DLCI for the connection is (scn * 2 + 1) if client originates connection on |
| 110 | * existing none initiator multiplexer channel. Otherwise it is (scn * 2). |
| 111 | * For the server DLCI can be changed later if client will be calling it using |
| 112 | * (scn * 2 + 1) dlci. |
| 113 | * |
| 114 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 115 | int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server, |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 116 | uint16_t mtu, const RawAddress& bd_addr, |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 117 | uint16_t* p_handle, tPORT_CALLBACK* p_mgmt_cb) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 118 | tPORT* p_port; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 119 | uint8_t dlci; |
| 120 | tRFC_MCB* p_mcb = port_find_mcb(bd_addr); |
| 121 | uint16_t rfcomm_mtu; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 122 | |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 123 | VLOG(0) << __func__ << " BDA: " << bd_addr; |
Chris Manton | cccf02f | 2014-10-21 13:55:24 -0700 | [diff] [blame] | 124 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 125 | *p_handle = 0; |
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 | if ((scn == 0) || (scn >= PORT_MAX_RFC_PORTS)) { |
| 128 | /* Server Channel Number(SCN) should be in range 1...30 */ |
| 129 | RFCOMM_TRACE_ERROR("RFCOMM_CreateConnection - invalid SCN"); |
| 130 | return (PORT_INVALID_SCN); |
| 131 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 132 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 133 | /* For client that originate connection on the existing none initiator */ |
| 134 | /* multiplexer channel DLCI should be odd */ |
| 135 | if (p_mcb && !p_mcb->is_initiator && !is_server) |
| 136 | dlci = (scn << 1) + 1; |
| 137 | else |
| 138 | dlci = (scn << 1); |
| 139 | RFCOMM_TRACE_API( |
| 140 | "RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, " |
| 141 | "p_mcb:%p", |
| 142 | scn, dlci, is_server, mtu, p_mcb); |
| 143 | |
| 144 | /* For the server side always allocate a new port. On the client side */ |
| 145 | /* do not allow the same (dlci, bd_addr) to be opened twice by application */ |
Marie Janssen | f9c5752 | 2017-02-22 08:35:29 -0800 | [diff] [blame] | 146 | if (!is_server) { |
| 147 | p_port = port_find_port(dlci, bd_addr); |
| 148 | if (p_port != NULL) { |
| 149 | /* if existing port is also a client port */ |
| 150 | if (p_port->is_server == false) { |
| 151 | RFCOMM_TRACE_ERROR( |
| 152 | "RFCOMM_CreateConnection - already opened state:%d, RFC state:%d, " |
| 153 | "MCB state:%d", |
| 154 | p_port->state, p_port->rfc.state, |
| 155 | p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0); |
| 156 | *p_handle = p_port->inx; |
| 157 | return (PORT_ALREADY_OPENED); |
| 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 | } |
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_port = port_allocate_port(dlci, bd_addr); |
| 163 | if (p_port == NULL) { |
| 164 | RFCOMM_TRACE_WARNING("RFCOMM_CreateConnection - no resources"); |
| 165 | return (PORT_NO_RESOURCES); |
| 166 | } |
| 167 | RFCOMM_TRACE_API( |
| 168 | "RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, " |
| 169 | "p_mcb:%p, p_port:%p", |
| 170 | scn, dlci, is_server, mtu, p_mcb, p_port); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 171 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 172 | p_port->default_signal_state = |
| 173 | (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 174 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 175 | switch (uuid) { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 176 | case UUID_PROTOCOL_OBEX: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 177 | p_port->default_signal_state = PORT_OBEX_DEFAULT_SIGNAL_STATE; |
| 178 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 179 | case UUID_SERVCLASS_SERIAL_PORT: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 180 | p_port->default_signal_state = PORT_SPP_DEFAULT_SIGNAL_STATE; |
| 181 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 182 | case UUID_SERVCLASS_LAN_ACCESS_USING_PPP: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 183 | p_port->default_signal_state = PORT_PPP_DEFAULT_SIGNAL_STATE; |
| 184 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 185 | case UUID_SERVCLASS_DIALUP_NETWORKING: |
| 186 | case UUID_SERVCLASS_FAX: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 187 | p_port->default_signal_state = PORT_DUN_DEFAULT_SIGNAL_STATE; |
| 188 | break; |
| 189 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 190 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 191 | RFCOMM_TRACE_EVENT("RFCOMM_CreateConnection dlci:%d signal state:0x%x", dlci, |
| 192 | p_port->default_signal_state); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 193 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 194 | *p_handle = p_port->inx; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 195 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 196 | p_port->state = PORT_STATE_OPENING; |
| 197 | p_port->uuid = uuid; |
| 198 | p_port->is_server = is_server; |
| 199 | p_port->scn = scn; |
| 200 | p_port->ev_mask = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 201 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 202 | /* If the MTU is not specified (0), keep MTU decision until the |
| 203 | * PN frame has to be send |
| 204 | * at that time connection should be established and we |
| 205 | * will know for sure our prefered MTU |
| 206 | */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 207 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 208 | rfcomm_mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 209 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 210 | if (mtu) |
| 211 | p_port->mtu = (mtu < rfcomm_mtu) ? mtu : rfcomm_mtu; |
| 212 | else |
| 213 | p_port->mtu = rfcomm_mtu; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 214 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 215 | /* server doesn't need to release port when closing */ |
| 216 | if (is_server) { |
| 217 | p_port->keep_port_handle = true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 218 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 219 | /* keep mtu that user asked, p_port->mtu could be updated during param |
| 220 | * negotiation */ |
| 221 | p_port->keep_mtu = p_port->mtu; |
| 222 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 223 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 224 | p_port->local_ctrl.modem_signal = p_port->default_signal_state; |
| 225 | p_port->local_ctrl.fc = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 226 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 227 | p_port->p_mgmt_callback = p_mgmt_cb; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 228 | |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 229 | p_port->bd_addr = bd_addr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 230 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 231 | /* If this is not initiator of the connection need to just wait */ |
| 232 | if (p_port->is_server) { |
| 233 | return (PORT_SUCCESS); |
| 234 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 235 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 236 | /* Open will be continued after security checks are passed */ |
| 237 | return port_open_continue(p_port); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 238 | } |
| 239 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 240 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 241 | * |
| 242 | * Function RFCOMM_RemoveConnection |
| 243 | * |
| 244 | * Description This function is called to close the specified connection. |
| 245 | * |
| 246 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 247 | * |
| 248 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 249 | int RFCOMM_RemoveConnection(uint16_t handle) { |
| 250 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 251 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 252 | RFCOMM_TRACE_API("RFCOMM_RemoveConnection() handle:%d", handle); |
Chris Manton | cccf02f | 2014-10-21 13:55:24 -0700 | [diff] [blame] | 253 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 254 | /* Check if handle is valid to avoid crashing */ |
| 255 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 256 | RFCOMM_TRACE_ERROR("RFCOMM_RemoveConnection() BAD handle:%d", handle); |
| 257 | return (PORT_BAD_HANDLE); |
| 258 | } |
| 259 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 260 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 261 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 262 | RFCOMM_TRACE_EVENT("RFCOMM_RemoveConnection() Not opened:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 263 | return (PORT_SUCCESS); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | p_port->state = PORT_STATE_CLOSING; |
| 267 | |
| 268 | port_start_close(p_port); |
| 269 | |
| 270 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 274 | * |
| 275 | * Function RFCOMM_RemoveServer |
| 276 | * |
| 277 | * Description This function is called to close the server port. |
| 278 | * |
| 279 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 280 | * |
| 281 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 282 | int RFCOMM_RemoveServer(uint16_t handle) { |
| 283 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 284 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 285 | RFCOMM_TRACE_API("RFCOMM_RemoveServer() handle:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 286 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 287 | /* Check if handle is valid to avoid crashing */ |
| 288 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 289 | RFCOMM_TRACE_ERROR("RFCOMM_RemoveServer() BAD handle:%d", handle); |
| 290 | return (PORT_BAD_HANDLE); |
| 291 | } |
| 292 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 293 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 294 | /* Do not report any events to the client any more. */ |
| 295 | p_port->p_mgmt_callback = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 296 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 297 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 298 | RFCOMM_TRACE_EVENT("RFCOMM_RemoveServer() Not opened:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 299 | return (PORT_SUCCESS); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* this port will be deallocated after closing */ |
| 303 | p_port->keep_port_handle = false; |
| 304 | p_port->state = PORT_STATE_CLOSING; |
| 305 | |
| 306 | port_start_close(p_port); |
| 307 | |
| 308 | return (PORT_SUCCESS); |
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 PORT_SetEventCallback |
| 314 | * |
| 315 | * Description This function is called to provide an address of the |
| 316 | * function which will be called when one of the events |
| 317 | * specified in the mask occures. |
| 318 | * |
| 319 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 320 | * p_callback - address of the callback function which should |
| 321 | * be called from the RFCOMM when an event |
| 322 | * specified in the mask occures. |
| 323 | * |
| 324 | * |
| 325 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 326 | int PORT_SetEventCallback(uint16_t port_handle, tPORT_CALLBACK* p_port_cb) { |
| 327 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 328 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 329 | /* Check if handle is valid to avoid crashing */ |
| 330 | if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) { |
| 331 | return (PORT_BAD_HANDLE); |
| 332 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 333 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 334 | p_port = &rfc_cb.port.port[port_handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 335 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 336 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 337 | return (PORT_NOT_OPENED); |
| 338 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 339 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 340 | RFCOMM_TRACE_API("PORT_SetEventCallback() handle:%d", port_handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 341 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 342 | p_port->p_callback = p_port_cb; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 343 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 344 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 345 | } |
Matthew Xie | 9ac641d | 2013-01-15 15:54:03 -0800 | [diff] [blame] | 346 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 347 | * |
| 348 | * Function PORT_ClearKeepHandleFlag |
| 349 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 350 | * Description Clear the keep handle flag, which will cause not to keep the |
| 351 | * port handle open when closed |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 352 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 353 | * |
| 354 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 355 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 356 | int PORT_ClearKeepHandleFlag(uint16_t port_handle) { |
| 357 | tPORT* p_port; |
Matthew Xie | 9ac641d | 2013-01-15 15:54:03 -0800 | [diff] [blame] | 358 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 359 | /* Check if handle is valid to avoid crashing */ |
| 360 | if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) { |
| 361 | return (PORT_BAD_HANDLE); |
| 362 | } |
Matthew Xie | 9ac641d | 2013-01-15 15:54:03 -0800 | [diff] [blame] | 363 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 364 | p_port = &rfc_cb.port.port[port_handle - 1]; |
| 365 | p_port->keep_port_handle = 0; |
| 366 | return (PORT_SUCCESS); |
Matthew Xie | 9ac641d | 2013-01-15 15:54:03 -0800 | [diff] [blame] | 367 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 368 | |
| 369 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 370 | * |
| 371 | * Function PORT_SetDataCallback |
| 372 | * |
| 373 | * Description This function is when a data packet is received |
| 374 | * |
| 375 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 376 | * p_callback - address of the callback function which should |
| 377 | * be called from the RFCOMM when data packet |
| 378 | * is received. |
| 379 | * |
| 380 | * |
| 381 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 382 | int PORT_SetDataCallback(uint16_t port_handle, tPORT_DATA_CALLBACK* p_port_cb) { |
| 383 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 384 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 385 | RFCOMM_TRACE_API("PORT_SetDataCallback() handle:%d cb 0x%x", port_handle, |
| 386 | p_port_cb); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 387 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 388 | /* Check if handle is valid to avoid crashing */ |
| 389 | if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) { |
| 390 | return (PORT_BAD_HANDLE); |
| 391 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 392 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 393 | p_port = &rfc_cb.port.port[port_handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 394 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 395 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 396 | return (PORT_NOT_OPENED); |
| 397 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 398 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 399 | p_port->p_data_callback = p_port_cb; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 400 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 401 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 402 | } |
| 403 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 404 | * |
| 405 | * Function PORT_SetCODataCallback |
| 406 | * |
| 407 | * Description This function is when a data packet is received |
| 408 | * |
| 409 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 410 | * p_callback - address of the callback function which should |
| 411 | * be called from the RFCOMM when data packet |
| 412 | * is received. |
| 413 | * |
| 414 | * |
| 415 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 416 | int PORT_SetDataCOCallback(uint16_t port_handle, |
| 417 | tPORT_DATA_CO_CALLBACK* p_port_cb) { |
| 418 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 419 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 420 | RFCOMM_TRACE_API("PORT_SetDataCOCallback() handle:%d cb 0x%x", port_handle, |
| 421 | p_port_cb); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 422 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 423 | /* Check if handle is valid to avoid crashing */ |
| 424 | if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) { |
| 425 | return (PORT_BAD_HANDLE); |
| 426 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 427 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 428 | p_port = &rfc_cb.port.port[port_handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 429 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 430 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 431 | return (PORT_NOT_OPENED); |
| 432 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 433 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 434 | p_port->p_data_co_callback = p_port_cb; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 435 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 436 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 437 | } |
| 438 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 439 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 440 | * |
| 441 | * Function PORT_SetEventMask |
| 442 | * |
| 443 | * Description This function is called to close the specified connection. |
| 444 | * |
| 445 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 446 | * mask - Bitmask of the events the host is interested in |
| 447 | * |
| 448 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 449 | int PORT_SetEventMask(uint16_t port_handle, uint32_t mask) { |
| 450 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 451 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 452 | RFCOMM_TRACE_API("PORT_SetEventMask() handle:%d mask:0x%x", port_handle, |
| 453 | mask); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 454 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 455 | /* Check if handle is valid to avoid crashing */ |
| 456 | if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) { |
| 457 | return (PORT_BAD_HANDLE); |
| 458 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 459 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 460 | p_port = &rfc_cb.port.port[port_handle - 1]; |
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 (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 463 | return (PORT_NOT_OPENED); |
| 464 | } |
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 | p_port->ev_mask = mask; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 467 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 468 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 469 | } |
| 470 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 471 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 472 | * |
| 473 | * Function PORT_CheckConnection |
| 474 | * |
| 475 | * Description This function returns PORT_SUCCESS if connection referenced |
| 476 | * by handle is up and running |
| 477 | * |
| 478 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 479 | * bd_addr - OUT bd_addr of the peer |
| 480 | * p_lcid - OUT L2CAP's LCID |
| 481 | * |
| 482 | ******************************************************************************/ |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 483 | int PORT_CheckConnection(uint16_t handle, RawAddress& bd_addr, |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 484 | uint16_t* p_lcid) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 485 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 486 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 487 | RFCOMM_TRACE_API("PORT_CheckConnection() handle:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 488 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 489 | /* Check if handle is valid to avoid crashing */ |
| 490 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 491 | return (PORT_BAD_HANDLE); |
| 492 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 493 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 494 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 495 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 496 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 497 | return (PORT_NOT_OPENED); |
| 498 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 499 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 500 | if (!p_port->rfc.p_mcb || !p_port->rfc.p_mcb->peer_ready || |
| 501 | (p_port->rfc.state != RFC_STATE_OPENED)) { |
| 502 | return (PORT_LINE_ERR); |
| 503 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 504 | |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 505 | bd_addr = p_port->rfc.p_mcb->bd_addr; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 506 | if (p_lcid) *p_lcid = p_port->rfc.p_mcb->lcid; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 507 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 508 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 512 | * |
| 513 | * Function PORT_IsOpening |
| 514 | * |
| 515 | * Description This function returns true if there is any RFCOMM connection |
| 516 | * opening in process. |
| 517 | * |
| 518 | * Parameters: true if any connection opening is found |
| 519 | * bd_addr - bd_addr of the peer |
| 520 | * |
| 521 | ******************************************************************************/ |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 522 | bool PORT_IsOpening(RawAddress& bd_addr) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 523 | uint8_t xx, yy; |
| 524 | tRFC_MCB* p_mcb = NULL; |
| 525 | tPORT* p_port; |
| 526 | bool found_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 527 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 528 | /* Check for any rfc_mcb which is in the middle of opening. */ |
| 529 | for (xx = 0; xx < MAX_BD_CONNECTIONS; xx++) { |
| 530 | if ((rfc_cb.port.rfc_mcb[xx].state > RFC_MX_STATE_IDLE) && |
| 531 | (rfc_cb.port.rfc_mcb[xx].state < RFC_MX_STATE_CONNECTED)) { |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 532 | bd_addr = rfc_cb.port.rfc_mcb[xx].bd_addr; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 533 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 536 | if (rfc_cb.port.rfc_mcb[xx].state == RFC_MX_STATE_CONNECTED) { |
| 537 | found_port = false; |
| 538 | p_mcb = &rfc_cb.port.rfc_mcb[xx]; |
| 539 | p_port = &rfc_cb.port.port[0]; |
| 540 | |
| 541 | for (yy = 0; yy < MAX_RFC_PORTS; yy++, p_port++) { |
| 542 | if (p_port->rfc.p_mcb == p_mcb) { |
| 543 | found_port = true; |
| 544 | break; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | if ((!found_port) || |
| 549 | (found_port && (p_port->rfc.state < RFC_STATE_OPENED))) { |
| 550 | /* Port is not established yet. */ |
Jakub Pawlowski | 1979fa3 | 2017-06-16 10:46:47 -0700 | [diff] [blame] | 551 | bd_addr = rfc_cb.port.rfc_mcb[xx].bd_addr; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 552 | return true; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 561 | * |
| 562 | * Function PORT_SetState |
| 563 | * |
| 564 | * Description This function configures connection according to the |
| 565 | * specifications in the tPORT_STATE structure. |
| 566 | * |
| 567 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 568 | * p_settings - Pointer to a tPORT_STATE structure containing |
| 569 | * configuration information for the connection. |
| 570 | * |
| 571 | * |
| 572 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 573 | int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings) { |
| 574 | tPORT* p_port; |
| 575 | uint8_t baud_rate; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 576 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 577 | RFCOMM_TRACE_API("PORT_SetState() handle:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 578 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 579 | /* Check if handle is valid to avoid crashing */ |
| 580 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 581 | return (PORT_BAD_HANDLE); |
| 582 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 583 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 584 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 585 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 586 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 587 | return (PORT_NOT_OPENED); |
| 588 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 589 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 590 | if (p_port->line_status) { |
| 591 | return (PORT_LINE_ERR); |
| 592 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 593 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 594 | RFCOMM_TRACE_API("PORT_SetState() handle:%d FC_TYPE:0x%x", handle, |
| 595 | p_settings->fc_type); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 596 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 597 | baud_rate = p_port->user_port_pars.baud_rate; |
| 598 | p_port->user_port_pars = *p_settings; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 599 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 600 | /* for now we've been asked to pass only baud rate */ |
| 601 | if (baud_rate != p_settings->baud_rate) { |
| 602 | port_start_par_neg(p_port); |
| 603 | } |
| 604 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 608 | * |
| 609 | * Function PORT_GetRxQueueCnt |
| 610 | * |
| 611 | * Description This function return number of buffers on the rx queue. |
| 612 | * |
| 613 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 614 | * p_rx_queue_count - Pointer to return queue count in. |
| 615 | * |
| 616 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 617 | int PORT_GetRxQueueCnt(uint16_t handle, uint16_t* p_rx_queue_count) { |
| 618 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 619 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 620 | RFCOMM_TRACE_API("PORT_GetRxQueueCnt() handle:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 621 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 622 | /* Check if handle is valid to avoid crashing */ |
| 623 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 624 | return (PORT_BAD_HANDLE); |
| 625 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 626 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 627 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 628 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 629 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 630 | return (PORT_NOT_OPENED); |
| 631 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 632 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 633 | if (p_port->line_status) { |
| 634 | return (PORT_LINE_ERR); |
| 635 | } |
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 | *p_rx_queue_count = p_port->rx.queue_size; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 638 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 639 | RFCOMM_TRACE_API( |
| 640 | "PORT_GetRxQueueCnt() p_rx_queue_count:%d, p_port->rx.queue.count = %d", |
| 641 | *p_rx_queue_count, p_port->rx.queue_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 642 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 643 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 647 | * |
| 648 | * Function PORT_GetState |
| 649 | * |
| 650 | * Description This function is called to fill tPORT_STATE structure |
| 651 | * with the curremt control settings for the port |
| 652 | * |
| 653 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 654 | * p_settings - Pointer to a tPORT_STATE structure in which |
| 655 | * configuration information is returned. |
| 656 | * |
| 657 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 658 | int PORT_GetState(uint16_t handle, tPORT_STATE* p_settings) { |
| 659 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 660 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 661 | RFCOMM_TRACE_API("PORT_GetState() handle:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 662 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 663 | /* Check if handle is valid to avoid crashing */ |
| 664 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 665 | return (PORT_BAD_HANDLE); |
| 666 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 667 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 668 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 669 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 670 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 671 | return (PORT_NOT_OPENED); |
| 672 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 673 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 674 | if (p_port->line_status) { |
| 675 | return (PORT_LINE_ERR); |
| 676 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 677 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 678 | *p_settings = p_port->user_port_pars; |
| 679 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 680 | } |
| 681 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 682 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 683 | * |
| 684 | * Function PORT_Control |
| 685 | * |
| 686 | * Description This function directs a specified connection to pass control |
| 687 | * control information to the peer device. |
| 688 | * |
| 689 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 690 | * signal = specify the function to be passed |
| 691 | * |
| 692 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 693 | int PORT_Control(uint16_t handle, uint8_t signal) { |
| 694 | tPORT* p_port; |
| 695 | uint8_t old_modem_signal; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 696 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 697 | RFCOMM_TRACE_API("PORT_Control() handle:%d signal:0x%x", handle, signal); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 698 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 699 | /* Check if handle is valid to avoid crashing */ |
| 700 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 701 | return (PORT_BAD_HANDLE); |
| 702 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 703 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 704 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 705 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 706 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 707 | return (PORT_NOT_OPENED); |
| 708 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 709 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 710 | old_modem_signal = p_port->local_ctrl.modem_signal; |
| 711 | p_port->local_ctrl.break_signal = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 712 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 713 | switch (signal) { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 714 | case PORT_SET_CTSRTS: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 715 | p_port->local_ctrl.modem_signal |= PORT_CTSRTS_ON; |
| 716 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 717 | |
| 718 | case PORT_CLR_CTSRTS: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 719 | p_port->local_ctrl.modem_signal &= ~PORT_CTSRTS_ON; |
| 720 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 721 | |
| 722 | case PORT_SET_DTRDSR: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 723 | p_port->local_ctrl.modem_signal |= PORT_DTRDSR_ON; |
| 724 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 725 | |
| 726 | case PORT_CLR_DTRDSR: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 727 | p_port->local_ctrl.modem_signal &= ~PORT_DTRDSR_ON; |
| 728 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 729 | |
| 730 | case PORT_SET_RI: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 731 | p_port->local_ctrl.modem_signal |= PORT_RING_ON; |
| 732 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 733 | |
| 734 | case PORT_CLR_RI: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 735 | p_port->local_ctrl.modem_signal &= ~PORT_RING_ON; |
| 736 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 737 | |
| 738 | case PORT_SET_DCD: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 739 | p_port->local_ctrl.modem_signal |= PORT_DCD_ON; |
| 740 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 741 | |
| 742 | case PORT_CLR_DCD: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 743 | p_port->local_ctrl.modem_signal &= ~PORT_DCD_ON; |
| 744 | break; |
| 745 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 746 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 747 | if (signal == PORT_BREAK) |
| 748 | p_port->local_ctrl.break_signal = PORT_BREAK_DURATION; |
| 749 | else if (p_port->local_ctrl.modem_signal == old_modem_signal) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 750 | return (PORT_SUCCESS); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 751 | |
| 752 | port_start_control(p_port); |
| 753 | |
| 754 | RFCOMM_TRACE_EVENT( |
| 755 | "PORT_Control DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d", |
| 756 | ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0), |
| 757 | ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0), |
| 758 | ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0), |
| 759 | ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0)); |
| 760 | |
| 761 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 762 | } |
| 763 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 764 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 765 | * |
| 766 | * Function PORT_FlowControl |
| 767 | * |
| 768 | * Description This function directs a specified connection to pass |
| 769 | * flow control message to the peer device. Enable flag passed |
| 770 | * shows if port can accept more data. |
| 771 | * |
| 772 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 773 | * enable - enables data flow |
| 774 | * |
| 775 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 776 | int PORT_FlowControl(uint16_t handle, bool enable) { |
| 777 | tPORT* p_port; |
| 778 | bool old_fc; |
| 779 | uint32_t events; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 780 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 781 | RFCOMM_TRACE_API("PORT_FlowControl() handle:%d enable: %d", handle, enable); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 782 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 783 | /* Check if handle is valid to avoid crashing */ |
| 784 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 785 | return (PORT_BAD_HANDLE); |
| 786 | } |
| 787 | |
| 788 | p_port = &rfc_cb.port.port[handle - 1]; |
| 789 | |
| 790 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 791 | return (PORT_NOT_OPENED); |
| 792 | } |
| 793 | |
| 794 | if (!p_port->rfc.p_mcb) { |
| 795 | return (PORT_NOT_OPENED); |
| 796 | } |
| 797 | |
| 798 | p_port->rx.user_fc = !enable; |
| 799 | |
| 800 | if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) { |
| 801 | if (!p_port->rx.user_fc) { |
| 802 | port_flow_control_peer(p_port, true, 0); |
| 803 | } |
| 804 | } else { |
| 805 | old_fc = p_port->local_ctrl.fc; |
| 806 | |
| 807 | /* FC is set if user is set or peer is set */ |
| 808 | p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc); |
| 809 | |
| 810 | if (p_port->local_ctrl.fc != old_fc) port_start_control(p_port); |
| 811 | } |
| 812 | |
| 813 | /* Need to take care of the case when we could not deliver events */ |
| 814 | /* to the application because we were flow controlled */ |
| 815 | if (enable && (p_port->rx.queue_size != 0)) { |
| 816 | events = PORT_EV_RXCHAR; |
| 817 | if (p_port->rx_flag_ev_pending) { |
| 818 | p_port->rx_flag_ev_pending = false; |
| 819 | events |= PORT_EV_RXFLAG; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 822 | events &= p_port->ev_mask; |
| 823 | if (p_port->p_callback && events) { |
| 824 | p_port->p_callback(events, p_port->inx); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 825 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 826 | } |
| 827 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 828 | } |
zzy | 181adbe | 2013-08-09 17:52:52 -0700 | [diff] [blame] | 829 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 830 | * |
| 831 | * Function PORT_FlowControl_MaxCredit |
| 832 | * |
| 833 | * Description This function directs a specified connection to pass |
| 834 | * flow control message to the peer device. Enable flag passed |
| 835 | * shows if port can accept more data. It also sends max credit |
| 836 | * when data flow enabled |
| 837 | * |
| 838 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 839 | * enable - enables data flow |
| 840 | * |
| 841 | ******************************************************************************/ |
zzy | 181adbe | 2013-08-09 17:52:52 -0700 | [diff] [blame] | 842 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 843 | int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable) { |
| 844 | tPORT* p_port; |
| 845 | bool old_fc; |
| 846 | uint32_t events; |
zzy | 181adbe | 2013-08-09 17:52:52 -0700 | [diff] [blame] | 847 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 848 | RFCOMM_TRACE_API("PORT_FlowControl() handle:%d enable: %d", handle, enable); |
zzy | 181adbe | 2013-08-09 17:52:52 -0700 | [diff] [blame] | 849 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 850 | /* Check if handle is valid to avoid crashing */ |
| 851 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 852 | return (PORT_BAD_HANDLE); |
| 853 | } |
| 854 | |
| 855 | p_port = &rfc_cb.port.port[handle - 1]; |
| 856 | |
| 857 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 858 | return (PORT_NOT_OPENED); |
| 859 | } |
| 860 | |
| 861 | if (!p_port->rfc.p_mcb) { |
| 862 | return (PORT_NOT_OPENED); |
| 863 | } |
| 864 | |
| 865 | p_port->rx.user_fc = !enable; |
| 866 | |
| 867 | if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) { |
| 868 | if (!p_port->rx.user_fc) { |
| 869 | port_flow_control_peer(p_port, true, p_port->credit_rx); |
| 870 | } |
| 871 | } else { |
| 872 | old_fc = p_port->local_ctrl.fc; |
| 873 | |
| 874 | /* FC is set if user is set or peer is set */ |
| 875 | p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc); |
| 876 | |
| 877 | if (p_port->local_ctrl.fc != old_fc) port_start_control(p_port); |
| 878 | } |
| 879 | |
| 880 | /* Need to take care of the case when we could not deliver events */ |
| 881 | /* to the application because we were flow controlled */ |
| 882 | if (enable && (p_port->rx.queue_size != 0)) { |
| 883 | events = PORT_EV_RXCHAR; |
| 884 | if (p_port->rx_flag_ev_pending) { |
| 885 | p_port->rx_flag_ev_pending = false; |
| 886 | events |= PORT_EV_RXFLAG; |
zzy | 181adbe | 2013-08-09 17:52:52 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 889 | events &= p_port->ev_mask; |
| 890 | if (p_port->p_callback && events) { |
| 891 | p_port->p_callback(events, p_port->inx); |
zzy | 181adbe | 2013-08-09 17:52:52 -0700 | [diff] [blame] | 892 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 893 | } |
| 894 | return (PORT_SUCCESS); |
zzy | 181adbe | 2013-08-09 17:52:52 -0700 | [diff] [blame] | 895 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 896 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 897 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 898 | * |
| 899 | * Function PORT_GetModemStatus |
| 900 | * |
| 901 | * Description This function retrieves modem control signals. Normally |
| 902 | * application will call this function after a callback |
| 903 | * function is called with notification that one of signals |
| 904 | * has been changed. |
| 905 | * |
| 906 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 907 | * p_signal - specify the pointer to control signals info |
| 908 | * |
| 909 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 910 | int PORT_GetModemStatus(uint16_t handle, uint8_t* p_signal) { |
| 911 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 912 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 913 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 914 | return (PORT_BAD_HANDLE); |
| 915 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 916 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 917 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 918 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 919 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 920 | return (PORT_NOT_OPENED); |
| 921 | } |
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 | *p_signal = p_port->peer_ctrl.modem_signal; |
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 | RFCOMM_TRACE_API("PORT_GetModemStatus() handle:%d signal:%x", handle, |
| 926 | *p_signal); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 927 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 928 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 929 | } |
| 930 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 931 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 932 | * |
| 933 | * Function PORT_ClearError |
| 934 | * |
| 935 | * Description This function retreives information about a communications |
| 936 | * error and reports current status of a connection. The |
| 937 | * function should be called when an error occures to clear |
| 938 | * the connection error flag and to enable additional read |
| 939 | * and write operations. |
| 940 | * |
| 941 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 942 | * p_errors - pointer of the variable to receive error codes |
| 943 | * p_status - pointer to the tPORT_STATUS structur to receive |
| 944 | * connection status |
| 945 | * |
| 946 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 947 | int PORT_ClearError(uint16_t handle, uint16_t* p_errors, |
| 948 | tPORT_STATUS* p_status) { |
| 949 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 950 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 951 | RFCOMM_TRACE_API("PORT_ClearError() handle:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 952 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 953 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 954 | return (PORT_BAD_HANDLE); |
| 955 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 956 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 957 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 958 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 959 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 960 | return (PORT_NOT_OPENED); |
| 961 | } |
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 | *p_errors = p_port->line_status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 964 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 965 | /* This is the only call to clear error status. We can not clear */ |
| 966 | /* connection failed status. To clean it port should be closed and reopened |
| 967 | */ |
| 968 | p_port->line_status = (p_port->line_status & LINE_STATUS_FAILED); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 969 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 970 | PORT_GetQueueStatus(handle, p_status); |
| 971 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 972 | } |
| 973 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 974 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 975 | * |
| 976 | * Function PORT_SendError |
| 977 | * |
| 978 | * Description This function send a communications error to the peer device |
| 979 | * |
| 980 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 981 | * errors - receive error codes |
| 982 | * |
| 983 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 984 | int PORT_SendError(uint16_t handle, uint8_t errors) { |
| 985 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 986 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 987 | RFCOMM_TRACE_API("PORT_SendError() handle:%d errors:0x%x", handle, errors); |
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 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 990 | return (PORT_BAD_HANDLE); |
| 991 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 992 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 993 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 994 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 995 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 996 | return (PORT_NOT_OPENED); |
| 997 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 998 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 999 | if (!p_port->rfc.p_mcb) { |
| 1000 | return (PORT_NOT_OPENED); |
| 1001 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1002 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1003 | RFCOMM_LineStatusReq(p_port->rfc.p_mcb, p_port->dlci, errors); |
| 1004 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1007 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1008 | * |
| 1009 | * Function PORT_GetQueueStatus |
| 1010 | * |
| 1011 | * Description This function reports current status of a connection. |
| 1012 | * |
| 1013 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 1014 | * p_status - pointer to the tPORT_STATUS structur to receive |
| 1015 | * connection status |
| 1016 | * |
| 1017 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1018 | int PORT_GetQueueStatus(uint16_t handle, tPORT_STATUS* p_status) { |
| 1019 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1020 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1021 | /* RFCOMM_TRACE_API ("PORT_GetQueueStatus() handle:%d", handle); */ |
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 ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 1024 | return (PORT_BAD_HANDLE); |
| 1025 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1026 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1027 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1028 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1029 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 1030 | return (PORT_NOT_OPENED); |
| 1031 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1032 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1033 | p_status->in_queue_size = (uint16_t)p_port->rx.queue_size; |
| 1034 | p_status->out_queue_size = (uint16_t)p_port->tx.queue_size; |
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 | p_status->mtu_size = (uint16_t)p_port->peer_mtu; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1037 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1038 | p_status->flags = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1039 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1040 | if (!(p_port->peer_ctrl.modem_signal & PORT_CTSRTS_ON)) |
| 1041 | p_status->flags |= PORT_FLAG_CTS_HOLD; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1042 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1043 | if (!(p_port->peer_ctrl.modem_signal & PORT_DTRDSR_ON)) |
| 1044 | p_status->flags |= PORT_FLAG_DSR_HOLD; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1045 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1046 | if (!(p_port->peer_ctrl.modem_signal & PORT_DCD_ON)) |
| 1047 | p_status->flags |= PORT_FLAG_RLSD_HOLD; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1048 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1049 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1052 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1053 | * |
| 1054 | * Function PORT_Purge |
| 1055 | * |
| 1056 | * Description This function discards all the data from the output or |
| 1057 | * input queues of the specified connection. |
| 1058 | * |
| 1059 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 1060 | * purge_flags - specify the action to take. |
| 1061 | * |
| 1062 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1063 | int PORT_Purge(uint16_t handle, uint8_t purge_flags) { |
| 1064 | tPORT* p_port; |
| 1065 | BT_HDR* p_buf; |
| 1066 | uint16_t count; |
| 1067 | uint32_t events; |
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 | RFCOMM_TRACE_API("PORT_Purge() handle:%d flags:0x%x", handle, purge_flags); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1070 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1071 | /* Check if handle is valid to avoid crashing */ |
| 1072 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 1073 | return (PORT_BAD_HANDLE); |
| 1074 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1075 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1076 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1077 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1078 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 1079 | return (PORT_NOT_OPENED); |
| 1080 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1081 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1082 | if (purge_flags & PORT_PURGE_RXCLEAR) { |
| 1083 | mutex_global_lock(); /* to prevent missing credit */ |
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 | count = fixed_queue_length(p_port->rx.queue); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1086 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1087 | while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->rx.queue)) != NULL) |
| 1088 | osi_free(p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1089 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1090 | p_port->rx.queue_size = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1091 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1092 | mutex_global_unlock(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1093 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1094 | /* If we flowed controlled peer based on rx_queue size enable data again */ |
| 1095 | if (count) port_flow_control_peer(p_port, true, count); |
| 1096 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1097 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1098 | if (purge_flags & PORT_PURGE_TXCLEAR) { |
| 1099 | mutex_global_lock(); /* to prevent tx.queue_size from being negative */ |
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 | while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->tx.queue)) != NULL) |
| 1102 | osi_free(p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1103 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1104 | p_port->tx.queue_size = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1105 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1106 | mutex_global_unlock(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1107 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1108 | events = PORT_EV_TXEMPTY; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1109 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1110 | events |= port_flow_control_user(p_port); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1111 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1112 | events &= p_port->ev_mask; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1113 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1114 | if ((p_port->p_callback != NULL) && events) |
| 1115 | (p_port->p_callback)(events, p_port->inx); |
| 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 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1119 | } |
| 1120 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1121 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1122 | * |
| 1123 | * Function PORT_ReadData |
| 1124 | * |
| 1125 | * Description Normally not GKI aware application will call this function |
| 1126 | * after receiving PORT_EV_RXCHAR event. |
| 1127 | * |
| 1128 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 1129 | * p_data - Data area |
| 1130 | * max_len - Byte count requested |
| 1131 | * p_len - Byte count received |
| 1132 | * |
| 1133 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1134 | int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len, |
| 1135 | uint16_t* p_len) { |
| 1136 | tPORT* p_port; |
| 1137 | BT_HDR* p_buf; |
| 1138 | uint16_t count; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1139 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1140 | RFCOMM_TRACE_API("PORT_ReadData() handle:%d max_len:%d", handle, max_len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1141 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1142 | /* Initialize this in case of an error */ |
| 1143 | *p_len = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1144 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1145 | /* Check if handle is valid to avoid crashing */ |
| 1146 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 1147 | return (PORT_BAD_HANDLE); |
| 1148 | } |
| 1149 | |
| 1150 | p_port = &rfc_cb.port.port[handle - 1]; |
| 1151 | |
| 1152 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 1153 | return (PORT_NOT_OPENED); |
| 1154 | } |
| 1155 | |
| 1156 | if (p_port->line_status) { |
| 1157 | return (PORT_LINE_ERR); |
| 1158 | } |
| 1159 | |
| 1160 | if (fixed_queue_is_empty(p_port->rx.queue)) return (PORT_SUCCESS); |
| 1161 | |
| 1162 | count = 0; |
| 1163 | |
| 1164 | while (max_len) { |
| 1165 | p_buf = (BT_HDR*)fixed_queue_try_peek_first(p_port->rx.queue); |
| 1166 | if (p_buf == NULL) break; |
| 1167 | |
| 1168 | if (p_buf->len > max_len) { |
| 1169 | memcpy(p_data, (uint8_t*)(p_buf + 1) + p_buf->offset, max_len); |
| 1170 | p_buf->offset += max_len; |
| 1171 | p_buf->len -= max_len; |
| 1172 | |
| 1173 | *p_len += max_len; |
| 1174 | |
| 1175 | mutex_global_lock(); |
| 1176 | |
| 1177 | p_port->rx.queue_size -= max_len; |
| 1178 | |
| 1179 | mutex_global_unlock(); |
| 1180 | |
| 1181 | break; |
| 1182 | } else { |
| 1183 | memcpy(p_data, (uint8_t*)(p_buf + 1) + p_buf->offset, p_buf->len); |
| 1184 | |
| 1185 | *p_len += p_buf->len; |
| 1186 | max_len -= p_buf->len; |
| 1187 | |
| 1188 | mutex_global_lock(); |
| 1189 | |
| 1190 | p_port->rx.queue_size -= p_buf->len; |
| 1191 | |
| 1192 | if (max_len) { |
| 1193 | p_data += p_buf->len; |
| 1194 | } |
| 1195 | |
| 1196 | osi_free(fixed_queue_try_dequeue(p_port->rx.queue)); |
| 1197 | |
| 1198 | mutex_global_unlock(); |
| 1199 | |
| 1200 | count++; |
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 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1203 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1204 | if (*p_len == 1) { |
| 1205 | RFCOMM_TRACE_EVENT("PORT_ReadData queue:%d returned:%d %x", |
| 1206 | p_port->rx.queue_size, *p_len, (p_data[0])); |
| 1207 | } else { |
| 1208 | RFCOMM_TRACE_EVENT("PORT_ReadData queue:%d returned:%d", |
| 1209 | p_port->rx.queue_size, *p_len); |
| 1210 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1211 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1212 | /* If rfcomm suspended traffic from the peer based on the rx_queue_size */ |
| 1213 | /* check if it can be resumed now */ |
| 1214 | port_flow_control_peer(p_port, true, count); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1215 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1216 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1219 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1220 | * |
| 1221 | * Function PORT_Read |
| 1222 | * |
| 1223 | * Description Normally application will call this function after receiving |
| 1224 | * PORT_EV_RXCHAR event. |
| 1225 | * |
| 1226 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 1227 | * pp_buf - pointer to address of buffer with data, |
| 1228 | * |
| 1229 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1230 | int PORT_Read(uint16_t handle, BT_HDR** pp_buf) { |
| 1231 | tPORT* p_port; |
| 1232 | BT_HDR* p_buf; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1233 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1234 | RFCOMM_TRACE_API("PORT_Read() handle:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1235 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1236 | /* Check if handle is valid to avoid crashing */ |
| 1237 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 1238 | return (PORT_BAD_HANDLE); |
| 1239 | } |
| 1240 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1241 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1242 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 1243 | return (PORT_NOT_OPENED); |
| 1244 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1245 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1246 | if (p_port->line_status) { |
| 1247 | return (PORT_LINE_ERR); |
| 1248 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1249 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1250 | mutex_global_lock(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1251 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1252 | p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->rx.queue); |
| 1253 | if (p_buf) { |
| 1254 | p_port->rx.queue_size -= p_buf->len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1255 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1256 | mutex_global_unlock(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1257 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1258 | /* If rfcomm suspended traffic from the peer based on the rx_queue_size */ |
| 1259 | /* check if it can be resumed now */ |
| 1260 | port_flow_control_peer(p_port, true, 1); |
| 1261 | } else { |
| 1262 | mutex_global_unlock(); |
| 1263 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1264 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1265 | *pp_buf = p_buf; |
| 1266 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1269 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1270 | * |
| 1271 | * Function port_write |
| 1272 | * |
| 1273 | * Description This function when a data packet is received from the apper |
| 1274 | * layer task. |
| 1275 | * |
| 1276 | * Parameters: p_port - pointer to address of port control block |
| 1277 | * p_buf - pointer to address of buffer with data, |
| 1278 | * |
| 1279 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1280 | static int port_write(tPORT* p_port, BT_HDR* p_buf) { |
| 1281 | /* We should not allow to write data in to server port when connection is not |
| 1282 | * opened */ |
| 1283 | if (p_port->is_server && (p_port->rfc.state != RFC_STATE_OPENED)) { |
| 1284 | osi_free(p_buf); |
| 1285 | return (PORT_CLOSED); |
| 1286 | } |
| 1287 | |
| 1288 | /* Keep the data in pending queue if peer does not allow data, or */ |
| 1289 | /* Peer is not ready or Port is not yet opened or initial port control */ |
| 1290 | /* command has not been sent */ |
| 1291 | if (p_port->tx.peer_fc || !p_port->rfc.p_mcb || |
| 1292 | !p_port->rfc.p_mcb->peer_ready || |
| 1293 | (p_port->rfc.state != RFC_STATE_OPENED) || |
| 1294 | ((p_port->port_ctrl & (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)) != |
| 1295 | (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED))) { |
| 1296 | if ((p_port->tx.queue_size > PORT_TX_CRITICAL_WM) || |
| 1297 | (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_CRITICAL_WM)) { |
| 1298 | RFCOMM_TRACE_WARNING("PORT_Write: Queue size: %d", p_port->tx.queue_size); |
| 1299 | |
| 1300 | osi_free(p_buf); |
| 1301 | |
| 1302 | if ((p_port->p_callback != NULL) && (p_port->ev_mask & PORT_EV_ERR)) |
| 1303 | p_port->p_callback(PORT_EV_ERR, p_port->inx); |
| 1304 | |
| 1305 | return (PORT_TX_FULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1306 | } |
| 1307 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1308 | RFCOMM_TRACE_EVENT( |
| 1309 | "PORT_Write : Data is enqued. flow disabled %d peer_ready %d state %d " |
| 1310 | "ctrl_state %x", |
| 1311 | p_port->tx.peer_fc, |
| 1312 | (p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready), p_port->rfc.state, |
| 1313 | p_port->port_ctrl); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1314 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1315 | fixed_queue_enqueue(p_port->tx.queue, p_buf); |
| 1316 | p_port->tx.queue_size += p_buf->len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1317 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1318 | return (PORT_CMD_PENDING); |
| 1319 | } else { |
| 1320 | RFCOMM_TRACE_EVENT("PORT_Write : Data is being sent"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1321 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1322 | RFCOMM_DataReq(p_port->rfc.p_mcb, p_port->dlci, p_buf); |
| 1323 | return (PORT_SUCCESS); |
| 1324 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1328 | * |
| 1329 | * Function PORT_Write |
| 1330 | * |
| 1331 | * Description This function when a data packet is received from the apper |
| 1332 | * layer task. |
| 1333 | * |
| 1334 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 1335 | * pp_buf - pointer to address of buffer with data, |
| 1336 | * |
| 1337 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1338 | int PORT_Write(uint16_t handle, BT_HDR* p_buf) { |
| 1339 | tPORT* p_port; |
| 1340 | uint32_t event = 0; |
| 1341 | int rc; |
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 | RFCOMM_TRACE_API("PORT_Write() handle:%d", handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1344 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1345 | /* Check if handle is valid to avoid crashing */ |
| 1346 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 1347 | osi_free(p_buf); |
| 1348 | return (PORT_BAD_HANDLE); |
| 1349 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1350 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1351 | p_port = &rfc_cb.port.port[handle - 1]; |
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 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 1354 | osi_free(p_buf); |
| 1355 | return (PORT_NOT_OPENED); |
| 1356 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1357 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1358 | if (p_port->line_status) { |
| 1359 | RFCOMM_TRACE_WARNING("PORT_Write: Data dropped line_status:0x%x", |
| 1360 | p_port->line_status); |
| 1361 | osi_free(p_buf); |
| 1362 | return (PORT_LINE_ERR); |
| 1363 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1364 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1365 | rc = port_write(p_port, p_buf); |
| 1366 | event |= port_flow_control_user(p_port); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1367 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1368 | switch (rc) { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1369 | case PORT_TX_FULL: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1370 | event |= PORT_EV_ERR; |
| 1371 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1372 | |
| 1373 | case PORT_SUCCESS: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1374 | event |= (PORT_EV_TXCHAR | PORT_EV_TXEMPTY); |
| 1375 | break; |
| 1376 | } |
| 1377 | /* Mask out all events that are not of interest to user */ |
| 1378 | event &= p_port->ev_mask; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1379 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1380 | /* Send event to the application */ |
| 1381 | if (p_port->p_callback && event) (p_port->p_callback)(event, p_port->inx); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1382 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1383 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1384 | } |
| 1385 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1386 | * |
| 1387 | * Function PORT_WriteDataCO |
| 1388 | * |
| 1389 | * Description Normally not GKI aware application will call this function |
| 1390 | * to send data to the port by callout functions |
| 1391 | * |
| 1392 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 1393 | * fd - socket fd |
| 1394 | * p_len - Byte count returned |
| 1395 | * |
| 1396 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1397 | int PORT_WriteDataCO(uint16_t handle, int* p_len) { |
| 1398 | tPORT* p_port; |
| 1399 | BT_HDR* p_buf; |
| 1400 | uint32_t event = 0; |
| 1401 | int rc = 0; |
| 1402 | uint16_t length; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1403 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1404 | RFCOMM_TRACE_API("PORT_WriteDataCO() handle:%d", handle); |
| 1405 | *p_len = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1406 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1407 | /* Check if handle is valid to avoid crashing */ |
| 1408 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 1409 | return (PORT_BAD_HANDLE); |
| 1410 | } |
| 1411 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1412 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1413 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 1414 | RFCOMM_TRACE_WARNING("PORT_WriteDataByFd() no port state:%d", |
| 1415 | p_port->state); |
| 1416 | return (PORT_NOT_OPENED); |
| 1417 | } |
| 1418 | |
| 1419 | if (!p_port->peer_mtu) { |
| 1420 | RFCOMM_TRACE_ERROR("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu); |
| 1421 | return (PORT_UNKNOWN_ERROR); |
| 1422 | } |
| 1423 | int available = 0; |
| 1424 | // if(ioctl(fd, FIONREAD, &available) < 0) |
| 1425 | if (p_port->p_data_co_callback( |
| 1426 | handle, (uint8_t*)&available, sizeof(available), |
| 1427 | DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE) == false) { |
| 1428 | RFCOMM_TRACE_ERROR( |
| 1429 | "p_data_co_callback DATA_CO_CALLBACK_TYPE_INCOMING_SIZE failed, " |
| 1430 | "available:%d", |
| 1431 | available); |
| 1432 | return (PORT_UNKNOWN_ERROR); |
| 1433 | } |
| 1434 | if (available == 0) return PORT_SUCCESS; |
| 1435 | /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len |
| 1436 | */ |
| 1437 | length = RFCOMM_DATA_BUF_SIZE - |
| 1438 | (uint16_t)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD); |
| 1439 | |
| 1440 | /* If there are buffers scheduled for transmission check if requested */ |
| 1441 | /* data fits into the end of the queue */ |
| 1442 | mutex_global_lock(); |
| 1443 | |
Marie Janssen | f9c5752 | 2017-02-22 08:35:29 -0800 | [diff] [blame] | 1444 | p_buf = (BT_HDR*)fixed_queue_try_peek_last(p_port->tx.queue); |
| 1445 | if ((p_buf != NULL) && |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1446 | (((int)p_buf->len + available) <= (int)p_port->peer_mtu) && |
| 1447 | (((int)p_buf->len + available) <= (int)length)) { |
| 1448 | // if(recv(fd, (uint8_t *)(p_buf + 1) + p_buf->offset + p_buf->len, |
| 1449 | // available, 0) != available) |
| 1450 | if (p_port->p_data_co_callback( |
| 1451 | handle, (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len, |
| 1452 | available, DATA_CO_CALLBACK_TYPE_OUTGOING) == false) |
| 1453 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1454 | { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1455 | error( |
| 1456 | "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, " |
| 1457 | "available:%d", |
| 1458 | available); |
| 1459 | mutex_global_unlock(); |
| 1460 | return (PORT_UNKNOWN_ERROR); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1461 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1462 | // memcpy ((uint8_t *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, |
| 1463 | // max_len); |
| 1464 | p_port->tx.queue_size += (uint16_t)available; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1465 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1466 | *p_len = available; |
| 1467 | p_buf->len += (uint16_t)available; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1468 | |
Pavlin Radoslavov | e29c343 | 2015-09-27 23:27:10 -0700 | [diff] [blame] | 1469 | mutex_global_unlock(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1470 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1471 | return (PORT_SUCCESS); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | mutex_global_unlock(); |
| 1475 | |
| 1476 | // int max_read = length < p_port->peer_mtu ? length : p_port->peer_mtu; |
| 1477 | |
| 1478 | // max_read = available < max_read ? available : max_read; |
| 1479 | |
| 1480 | while (available) { |
| 1481 | /* if we're over buffer high water mark, we're done */ |
| 1482 | if ((p_port->tx.queue_size > PORT_TX_HIGH_WM) || |
| 1483 | (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM)) { |
| 1484 | port_flow_control_user(p_port); |
| 1485 | event |= PORT_EV_FC; |
| 1486 | RFCOMM_TRACE_EVENT( |
| 1487 | "tx queue is full,tx.queue_size:%d,tx.queue.count:%d,available:%d", |
| 1488 | p_port->tx.queue_size, fixed_queue_length(p_port->tx.queue), |
| 1489 | available); |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | /* continue with rfcomm data write */ |
| 1494 | p_buf = (BT_HDR*)osi_malloc(RFCOMM_DATA_BUF_SIZE); |
| 1495 | p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET; |
| 1496 | p_buf->layer_specific = handle; |
| 1497 | |
| 1498 | if (p_port->peer_mtu < length) length = p_port->peer_mtu; |
| 1499 | if (available < (int)length) length = (uint16_t)available; |
| 1500 | p_buf->len = length; |
| 1501 | p_buf->event = BT_EVT_TO_BTU_SP_DATA; |
| 1502 | |
| 1503 | // memcpy ((uint8_t *)(p_buf + 1) + p_buf->offset, p_data, length); |
| 1504 | // if(recv(fd, (uint8_t *)(p_buf + 1) + p_buf->offset, (int)length, 0) != |
| 1505 | // (int)length) |
| 1506 | if (p_port->p_data_co_callback( |
| 1507 | handle, (uint8_t*)(p_buf + 1) + p_buf->offset, length, |
| 1508 | DATA_CO_CALLBACK_TYPE_OUTGOING) == false) { |
| 1509 | error( |
| 1510 | "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:%d", |
| 1511 | length); |
| 1512 | return (PORT_UNKNOWN_ERROR); |
| 1513 | } |
| 1514 | |
| 1515 | RFCOMM_TRACE_EVENT("PORT_WriteData %d bytes", length); |
| 1516 | |
| 1517 | rc = port_write(p_port, p_buf); |
| 1518 | |
| 1519 | /* If queue went below the threashold need to send flow control */ |
| 1520 | event |= port_flow_control_user(p_port); |
| 1521 | |
| 1522 | if (rc == PORT_SUCCESS) event |= PORT_EV_TXCHAR; |
| 1523 | |
| 1524 | if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) break; |
| 1525 | |
| 1526 | *p_len += length; |
| 1527 | available -= (int)length; |
| 1528 | } |
| 1529 | if (!available && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED)) |
| 1530 | event |= PORT_EV_TXEMPTY; |
| 1531 | |
| 1532 | /* Mask out all events that are not of interest to user */ |
| 1533 | event &= p_port->ev_mask; |
| 1534 | |
| 1535 | /* Send event to the application */ |
| 1536 | if (p_port->p_callback && event) (p_port->p_callback)(event, p_port->inx); |
| 1537 | |
| 1538 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1539 | } |
| 1540 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1541 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1542 | * |
| 1543 | * Function PORT_WriteData |
| 1544 | * |
| 1545 | * Description Normally not GKI aware application will call this function |
| 1546 | * to send data to the port. |
| 1547 | * |
| 1548 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 1549 | * p_data - Data area |
| 1550 | * max_len - Byte count requested |
| 1551 | * p_len - Byte count received |
| 1552 | * |
| 1553 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1554 | int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len, |
| 1555 | uint16_t* p_len) { |
| 1556 | tPORT* p_port; |
| 1557 | BT_HDR* p_buf; |
| 1558 | uint32_t event = 0; |
| 1559 | int rc = 0; |
| 1560 | uint16_t length; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1561 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1562 | RFCOMM_TRACE_API("PORT_WriteData() max_len:%d", max_len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1563 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1564 | *p_len = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1565 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1566 | /* Check if handle is valid to avoid crashing */ |
| 1567 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 1568 | return (PORT_BAD_HANDLE); |
| 1569 | } |
| 1570 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1571 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1572 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 1573 | RFCOMM_TRACE_WARNING("PORT_WriteData() no port state:%d", p_port->state); |
| 1574 | return (PORT_NOT_OPENED); |
| 1575 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1576 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1577 | if (!max_len || !p_port->peer_mtu) { |
| 1578 | RFCOMM_TRACE_ERROR("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu); |
| 1579 | return (PORT_UNKNOWN_ERROR); |
| 1580 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1581 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1582 | /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len |
| 1583 | */ |
| 1584 | length = RFCOMM_DATA_BUF_SIZE - |
| 1585 | (uint16_t)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1586 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1587 | /* If there are buffers scheduled for transmission check if requested */ |
| 1588 | /* data fits into the end of the queue */ |
| 1589 | mutex_global_lock(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1590 | |
Marie Janssen | f9c5752 | 2017-02-22 08:35:29 -0800 | [diff] [blame] | 1591 | p_buf = (BT_HDR*)fixed_queue_try_peek_last(p_port->tx.queue); |
| 1592 | if ((p_buf != NULL) && ((p_buf->len + max_len) <= p_port->peer_mtu) && |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1593 | ((p_buf->len + max_len) <= length)) { |
| 1594 | memcpy((uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len); |
| 1595 | p_port->tx.queue_size += max_len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1596 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1597 | *p_len = max_len; |
| 1598 | p_buf->len += max_len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1599 | |
Pavlin Radoslavov | e29c343 | 2015-09-27 23:27:10 -0700 | [diff] [blame] | 1600 | mutex_global_unlock(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1601 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1602 | return (PORT_SUCCESS); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | mutex_global_unlock(); |
| 1606 | |
| 1607 | while (max_len) { |
| 1608 | /* if we're over buffer high water mark, we're done */ |
| 1609 | if ((p_port->tx.queue_size > PORT_TX_HIGH_WM) || |
| 1610 | (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM)) |
| 1611 | break; |
| 1612 | |
| 1613 | /* continue with rfcomm data write */ |
| 1614 | p_buf = (BT_HDR*)osi_malloc(RFCOMM_DATA_BUF_SIZE); |
| 1615 | p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET; |
| 1616 | p_buf->layer_specific = handle; |
| 1617 | |
| 1618 | if (p_port->peer_mtu < length) length = p_port->peer_mtu; |
| 1619 | if (max_len < length) length = max_len; |
| 1620 | p_buf->len = length; |
| 1621 | p_buf->event = BT_EVT_TO_BTU_SP_DATA; |
| 1622 | |
| 1623 | memcpy((uint8_t*)(p_buf + 1) + p_buf->offset, p_data, length); |
| 1624 | |
| 1625 | RFCOMM_TRACE_EVENT("PORT_WriteData %d bytes", length); |
| 1626 | |
| 1627 | rc = port_write(p_port, p_buf); |
| 1628 | |
| 1629 | /* If queue went below the threashold need to send flow control */ |
| 1630 | event |= port_flow_control_user(p_port); |
| 1631 | |
| 1632 | if (rc == PORT_SUCCESS) event |= PORT_EV_TXCHAR; |
| 1633 | |
| 1634 | if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) break; |
| 1635 | |
| 1636 | *p_len += length; |
| 1637 | max_len -= length; |
| 1638 | p_data += length; |
| 1639 | } |
| 1640 | if (!max_len && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED)) |
| 1641 | event |= PORT_EV_TXEMPTY; |
| 1642 | |
| 1643 | /* Mask out all events that are not of interest to user */ |
| 1644 | event &= p_port->ev_mask; |
| 1645 | |
| 1646 | /* Send event to the application */ |
| 1647 | if (p_port->p_callback && event) (p_port->p_callback)(event, p_port->inx); |
| 1648 | |
| 1649 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1650 | } |
| 1651 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1652 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1653 | * |
| 1654 | * Function PORT_Test |
| 1655 | * |
| 1656 | * Description Application can call this function to send RFCOMM Test frame |
| 1657 | * |
| 1658 | * Parameters: handle - Handle returned in the RFCOMM_CreateConnection |
| 1659 | * p_data - Data area |
| 1660 | * max_len - Byte count requested |
| 1661 | * |
| 1662 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1663 | int PORT_Test(uint16_t handle, uint8_t* p_data, uint16_t len) { |
| 1664 | tPORT* p_port; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1665 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1666 | RFCOMM_TRACE_API("PORT_Test() len:%d", len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1667 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1668 | if ((handle == 0) || (handle > MAX_RFC_PORTS)) { |
| 1669 | return (PORT_BAD_HANDLE); |
| 1670 | } |
| 1671 | p_port = &rfc_cb.port.port[handle - 1]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1672 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1673 | if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) { |
| 1674 | return (PORT_NOT_OPENED); |
| 1675 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1676 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1677 | if (len > ((p_port->mtu == 0) ? RFCOMM_DEFAULT_MTU : p_port->mtu)) { |
| 1678 | return (PORT_UNKNOWN_ERROR); |
| 1679 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1680 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1681 | BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE); |
| 1682 | p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2; |
| 1683 | p_buf->len = len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1684 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1685 | memcpy((uint8_t*)(p_buf + 1) + p_buf->offset, p_data, p_buf->len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1686 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1687 | rfc_send_test(p_port->rfc.p_mcb, true, p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1688 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1689 | return (PORT_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1693 | * |
| 1694 | * Function RFCOMM_Init |
| 1695 | * |
| 1696 | * Description This function is called to initialize RFCOMM layer |
| 1697 | * |
| 1698 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1699 | void RFCOMM_Init(void) { |
| 1700 | memset(&rfc_cb, 0, sizeof(tRFC_CB)); /* Init RFCOMM control block */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1701 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1702 | rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1703 | |
| 1704 | #if defined(RFCOMM_INITIAL_TRACE_LEVEL) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1705 | rfc_cb.trace_level = RFCOMM_INITIAL_TRACE_LEVEL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1706 | #else |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1707 | rfc_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1708 | #endif |
| 1709 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1710 | rfcomm_l2cap_if_init(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1714 | * |
| 1715 | * Function PORT_SetTraceLevel |
| 1716 | * |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 1717 | * Description Set the trace level for RFCOMM. If called with 0xFF, it |
| 1718 | * simply reads the current trace level. |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1719 | * |
| 1720 | * Returns the new (current) trace level |
| 1721 | * |
| 1722 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1723 | uint8_t PORT_SetTraceLevel(uint8_t new_level) { |
| 1724 | if (new_level != 0xFF) rfc_cb.trace_level = new_level; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1725 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1726 | return (rfc_cb.trace_level); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1727 | } |
| 1728 | |
June R. Tate-Gans | 1e8cb56 | 2014-12-02 12:48:03 -0800 | [diff] [blame] | 1729 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 1730 | * |
| 1731 | * Function PORT_GetResultString |
| 1732 | * |
| 1733 | * Description This function returns the human-readable string for a given |
| 1734 | * result code. |
| 1735 | * |
| 1736 | * Returns a pointer to the human-readable string for the given result. |
| 1737 | * |
| 1738 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1739 | const char* PORT_GetResultString(const uint8_t result_code) { |
June R. Tate-Gans | 1e8cb56 | 2014-12-02 12:48:03 -0800 | [diff] [blame] | 1740 | if (result_code > PORT_ERR_MAX) { |
| 1741 | return result_code_strings[PORT_ERR_MAX]; |
| 1742 | } |
| 1743 | |
| 1744 | return result_code_strings[result_code]; |
| 1745 | } |