1 /****************************************************************************** 2 * 3 * Copyright 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 PORT API definitions 22 * 23 ******************************************************************************/ 24 #ifndef PORT_API_H 25 #define PORT_API_H 26 27 #include <cstdint> 28 29 #include "bt_target.h" 30 #include "types/raw_address.h" 31 32 /***************************************************************************** 33 * Constants and Types 34 ****************************************************************************/ 35 36 /* 37 * Define port settings structure send from the application in the 38 * set settings request, or to the application in the set settings indication. 39 */ 40 typedef struct { 41 #define PORT_BAUD_RATE_9600 0x03 42 43 uint8_t baud_rate; 44 45 #define PORT_8_BITS 0x03 46 47 uint8_t byte_size; 48 49 #define PORT_ONESTOPBIT 0x00 50 uint8_t stop_bits; 51 52 #define PORT_PARITY_NO 0x00 53 uint8_t parity; 54 55 #define PORT_ODD_PARITY 0x00 56 57 uint8_t parity_type; 58 59 #define PORT_FC_OFF 0x00 60 #define PORT_FC_CTS_ON_INPUT 0x04 61 #define PORT_FC_CTS_ON_OUTPUT 0x08 62 63 uint8_t fc_type; 64 65 uint8_t rx_char1; 66 67 #define PORT_XON_DC1 0x11 68 uint8_t xon_char; 69 70 #define PORT_XOFF_DC3 0x13 71 uint8_t xoff_char; 72 73 } tPORT_STATE; 74 75 /* 76 * Define the callback function prototypes. Parameters are specific 77 * to each event and are described bellow 78 */ 79 typedef int(tPORT_DATA_CALLBACK)(uint16_t port_handle, void* p_data, 80 uint16_t len); 81 82 #define DATA_CO_CALLBACK_TYPE_INCOMING 1 83 #define DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE 2 84 #define DATA_CO_CALLBACK_TYPE_OUTGOING 3 85 typedef int(tPORT_DATA_CO_CALLBACK)(uint16_t port_handle, uint8_t* p_buf, 86 uint16_t len, int type); 87 88 typedef void(tPORT_CALLBACK)(uint32_t code, uint16_t port_handle); 89 90 /* 91 * Define events that registered application can receive in the callback 92 */ 93 94 #define PORT_EV_RXCHAR 0x00000001 /* Any Character received */ 95 #define PORT_EV_RXFLAG 0x00000002 /* Received certain character */ 96 #define PORT_EV_TXEMPTY 0x00000004 /* Transmitt Queue Empty */ 97 #define PORT_EV_CTS 0x00000008 /* CTS changed state */ 98 #define PORT_EV_DSR 0x00000010 /* DSR changed state */ 99 #define PORT_EV_RLSD 0x00000020 /* RLSD changed state */ 100 #define PORT_EV_BREAK 0x00000040 /* BREAK received */ 101 #define PORT_EV_ERR 0x00000080 /* Line status error occurred */ 102 #define PORT_EV_RING 0x00000100 /* Ring signal detected */ 103 #define PORT_EV_CTSS 0x00000400 /* CTS state */ 104 #define PORT_EV_DSRS 0x00000800 /* DSR state */ 105 #define PORT_EV_RLSDS 0x00001000 /* RLSD state */ 106 #define PORT_EV_OVERRUN 0x00002000 /* receiver buffer overrun */ 107 #define PORT_EV_TXCHAR 0x00004000 /* Any character transmitted */ 108 109 /* RFCOMM connection established */ 110 #define PORT_EV_CONNECTED 0x00000200 111 /* Unable to establish connection or disconnected */ 112 #define PORT_EV_CONNECT_ERR 0x00008000 113 /* data flow enabled flag changed by remote */ 114 #define PORT_EV_FC 0x00010000 115 /* data flow enable status true = enabled */ 116 #define PORT_EV_FCS 0x00020000 117 118 /* 119 * Define port result codes 120 */ 121 #define PORT_SUCCESS 0 122 123 #define PORT_ERR_BASE 0 124 125 #define PORT_UNKNOWN_ERROR (PORT_ERR_BASE + 1) 126 #define PORT_ALREADY_OPENED (PORT_ERR_BASE + 2) 127 #define PORT_CMD_PENDING (PORT_ERR_BASE + 3) 128 #define PORT_APP_NOT_REGISTERED (PORT_ERR_BASE + 4) 129 #define PORT_NO_MEM (PORT_ERR_BASE + 5) 130 #define PORT_NO_RESOURCES (PORT_ERR_BASE + 6) 131 #define PORT_BAD_BD_ADDR (PORT_ERR_BASE + 7) 132 #define PORT_BAD_HANDLE (PORT_ERR_BASE + 9) 133 #define PORT_NOT_OPENED (PORT_ERR_BASE + 10) 134 #define PORT_LINE_ERR (PORT_ERR_BASE + 11) 135 #define PORT_START_FAILED (PORT_ERR_BASE + 12) 136 #define PORT_PAR_NEG_FAILED (PORT_ERR_BASE + 13) 137 #define PORT_PORT_NEG_FAILED (PORT_ERR_BASE + 14) 138 #define PORT_SEC_FAILED (PORT_ERR_BASE + 15) 139 #define PORT_PEER_CONNECTION_FAILED (PORT_ERR_BASE + 16) 140 #define PORT_PEER_FAILED (PORT_ERR_BASE + 17) 141 #define PORT_PEER_TIMEOUT (PORT_ERR_BASE + 18) 142 #define PORT_CLOSED (PORT_ERR_BASE + 19) 143 #define PORT_TX_FULL (PORT_ERR_BASE + 20) 144 #define PORT_LOCAL_CLOSED (PORT_ERR_BASE + 21) 145 #define PORT_LOCAL_TIMEOUT (PORT_ERR_BASE + 22) 146 #define PORT_TX_QUEUE_DISABLED (PORT_ERR_BASE + 23) 147 #define PORT_PAGE_TIMEOUT (PORT_ERR_BASE + 24) 148 #define PORT_INVALID_SCN (PORT_ERR_BASE + 25) 149 150 #define PORT_ERR_MAX (PORT_ERR_BASE + 26) 151 152 /***************************************************************************** 153 * External Function Declarations 154 ****************************************************************************/ 155 156 /******************************************************************************* 157 * 158 * Function RFCOMM_CreateConnection 159 * 160 * Description RFCOMM_CreateConnection is used from the application to 161 * establish a serial port connection to the peer device, 162 * or allow RFCOMM to accept a connection from the peer 163 * application. 164 * 165 * Parameters: scn - Service Channel Number as registered with 166 * the SDP (server) or obtained using SDP from 167 * the peer device (client). 168 * is_server - true if requesting application is a server 169 * mtu - Maximum frame size the application can accept 170 * bd_addr - address of the peer (client) 171 * mask - specifies events to be enabled. A value 172 * of zero disables all events. 173 * p_handle - OUT pointer to the handle. 174 * p_mgmt_cb - pointer to callback function to receive 175 * connection up/down events. 176 * Notes: 177 * 178 * Server can call this function with the same scn parameter multiple times if 179 * it is ready to accept multiple simulteneous connections. 180 * 181 * DLCI for the connection is (scn * 2 + 1) if client originates connection on 182 * existing none initiator multiplexer channel. Otherwise it is (scn * 2). 183 * For the server DLCI can be changed later if client will be calling it using 184 * (scn * 2 + 1) dlci. 185 * 186 ******************************************************************************/ 187 int RFCOMM_CreateConnectionWithSecurity(uint16_t uuid, uint8_t scn, 188 bool is_server, uint16_t mtu, 189 const RawAddress& bd_addr, 190 uint16_t* p_handle, 191 tPORT_CALLBACK* p_mgmt_cb, 192 uint16_t sec_mask); 193 194 /******************************************************************************* 195 * 196 * Function RFCOMM_ControlReqFromBTSOCK 197 * 198 * Description Send control parameters to the peer. 199 * So far only for qualification use. 200 * RFCOMM layer starts the control request only when it is the 201 * client. This API allows the host to start the control 202 * request while it works as a RFCOMM server. 203 * 204 * Parameters: dlci - the DLCI to send the MSC command 205 * bd_addr - bd_addr of the peer 206 * modem_signal - [DTR/DSR | RTS/CTS | RI | DCD] 207 * break_signal - 0-3 s in steps of 200 ms 208 * discard_buffers - 0 for do not discard, 1 for discard 209 * break_signal_seq - ASAP or in sequence 210 * fc - true when the device is unable to accept 211 * frames 212 * 213 ******************************************************************************/ 214 extern int RFCOMM_ControlReqFromBTSOCK(uint8_t dlci, const RawAddress& bd_addr, 215 uint8_t modem_signal, 216 uint8_t break_signal, 217 uint8_t discard_buffers, 218 uint8_t break_signal_seq, bool fc); 219 220 /******************************************************************************* 221 * 222 * Function RFCOMM_RemoveConnection 223 * 224 * Description This function is called to close the specified connection. 225 * 226 * Parameters: handle - Handle of the port returned in the Open 227 * 228 ******************************************************************************/ 229 int RFCOMM_RemoveConnection(uint16_t handle); 230 231 /******************************************************************************* 232 * 233 * Function RFCOMM_RemoveServer 234 * 235 * Description This function is called to close the server port. 236 * 237 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection 238 * 239 ******************************************************************************/ 240 int RFCOMM_RemoveServer(uint16_t handle); 241 242 /******************************************************************************* 243 * 244 * Function PORT_SetEventCallback 245 * 246 * Description Set event callback the specified connection. 247 * 248 * Parameters: handle - Handle of the port returned in the Open 249 * p_callback - address of the callback function which should 250 * be called from the RFCOMM when an event 251 * specified in the mask occurs. 252 * 253 ******************************************************************************/ 254 int PORT_SetEventCallback(uint16_t port_handle, tPORT_CALLBACK* p_port_cb); 255 256 /******************************************************************************* 257 * 258 * Function PORT_ClearKeepHandleFlag 259 * 260 * Description Called to clear the keep handle flag, which will cause 261 * not to keep the port handle open when closed 262 * 263 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection 264 * 265 ******************************************************************************/ 266 int PORT_ClearKeepHandleFlag(uint16_t port_handle); 267 268 int PORT_SetDataCOCallback(uint16_t port_handle, 269 tPORT_DATA_CO_CALLBACK* p_port_cb); 270 /******************************************************************************* 271 * 272 * Function PORT_SetEventMask 273 * 274 * Description This function is called to close the specified connection. 275 * 276 * Parameters: handle - Handle of the port returned in the Open 277 * mask - specifies events to be enabled. A value 278 * of zero disables all events. 279 * 280 ******************************************************************************/ 281 int PORT_SetEventMask(uint16_t port_handle, uint32_t mask); 282 283 /******************************************************************************* 284 * 285 * Function PORT_CheckConnection 286 * 287 * Description This function returns PORT_SUCCESS if connection referenced 288 * by handle is up and running 289 * 290 * Parameters: handle - Handle of the port returned in the Open 291 * bd_addr - OUT bd_addr of the peer 292 * p_lcid - OUT L2CAP's LCID 293 * 294 ******************************************************************************/ 295 int PORT_CheckConnection(uint16_t handle, RawAddress* bd_addr, 296 uint16_t* p_lcid); 297 298 /******************************************************************************* 299 * 300 * Function PORT_IsOpening 301 * 302 * Description This function returns true if there is any RFCOMM connection 303 * opening in process. 304 * 305 * Parameters: true if any connection opening is found 306 * bd_addr - bd_addr of the peer 307 * 308 ******************************************************************************/ 309 bool PORT_IsOpening(RawAddress* bd_addr); 310 311 /******************************************************************************* 312 * 313 * Function PORT_SetState 314 * 315 * Description This function configures connection according to the 316 * specifications in the tPORT_STATE structure. 317 * 318 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection 319 * p_settings - Pointer to a tPORT_STATE structure containing 320 * configuration information for the connection. 321 * 322 ******************************************************************************/ 323 int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings); 324 325 /******************************************************************************* 326 * 327 * Function PORT_GetState 328 * 329 * Description This function is called to fill tPORT_STATE structure 330 * with the current control settings for the port 331 * 332 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection 333 * p_settings - Pointer to a tPORT_STATE structure in which 334 * configuration information is returned. 335 * 336 ******************************************************************************/ 337 int PORT_GetState(uint16_t handle, tPORT_STATE* p_settings); 338 339 /******************************************************************************* 340 * 341 * Function PORT_FlowControl_MaxCredit 342 * 343 * Description This function directs a specified connection to pass 344 * flow control message to the peer device. Enable flag passed 345 * shows if port can accept more data. It also sends max credit 346 * when data flow enabled 347 * 348 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection 349 * enable - enables data flow 350 * 351 ******************************************************************************/ 352 int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable); 353 354 #define PORT_DTRDSR_ON 0x01 355 #define PORT_CTSRTS_ON 0x02 356 #define PORT_RING_ON 0x04 357 #define PORT_DCD_ON 0x08 358 359 /* 360 * Define default initial local modem signals state after connection established 361 */ 362 #define PORT_OBEX_DEFAULT_SIGNAL_STATE \ 363 (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON) 364 #define PORT_SPP_DEFAULT_SIGNAL_STATE \ 365 (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON) 366 #define PORT_PPP_DEFAULT_SIGNAL_STATE \ 367 (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON) 368 #define PORT_DUN_DEFAULT_SIGNAL_STATE (PORT_DTRDSR_ON | PORT_CTSRTS_ON) 369 370 #define PORT_ERR_BREAK 0x01 /* Break condition occured on the peer device */ 371 #define PORT_ERR_OVERRUN 0x02 /* Overrun is reported by peer device */ 372 #define PORT_ERR_FRAME 0x04 /* Framing error reported by peer device */ 373 #define PORT_ERR_RXOVER 0x08 /* Input queue overflow occured */ 374 #define PORT_ERR_TXFULL 0x10 /* Output queue overflow occured */ 375 376 /******************************************************************************* 377 * 378 * Function PORT_ReadData 379 * 380 * Description Normally application will call this function after receiving 381 * PORT_EVT_RXCHAR event. 382 * 383 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection 384 * callback. 385 * p_data - Data area 386 * max_len - Byte count requested 387 * p_len - Byte count received 388 * 389 ******************************************************************************/ 390 int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len, 391 uint16_t* p_len); 392 393 /******************************************************************************* 394 * 395 * Function PORT_WriteData 396 * 397 * Description This function is called from the legacy application to 398 * send data. 399 * 400 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection 401 * p_data - Data area 402 * max_len - Byte count to write 403 * p_len - Bytes written 404 * 405 ******************************************************************************/ 406 int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len, 407 uint16_t* p_len); 408 409 /******************************************************************************* 410 * 411 * Function PORT_WriteDataCO 412 * 413 * Description Normally not GKI aware application will call this function 414 * to send data to the port by callout functions. 415 * 416 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection 417 * 418 ******************************************************************************/ 419 int PORT_WriteDataCO(uint16_t handle, int* p_len); 420 421 /******************************************************************************* 422 * 423 * Function RFCOMM_Init 424 * 425 * Description This function is called to initialize RFCOMM layer 426 * 427 ******************************************************************************/ 428 void RFCOMM_Init(void); 429 430 /******************************************************************************* 431 * 432 * Function PORT_SetTraceLevel 433 * 434 * Description Set the trace level for RFCOMM. If called with 435 * a value of 0xFF, it simply reads the current trace level. 436 * 437 * Returns the new (current) trace level 438 * 439 ******************************************************************************/ 440 uint8_t PORT_SetTraceLevel(uint8_t new_level); 441 442 /******************************************************************************* 443 * 444 * Function PORT_GetResultString 445 * 446 * Description This function returns the human-readable string for a given 447 * result code. 448 * 449 * Returns a pointer to the human-readable string for the given 450 * result. Note that the string returned must not be freed. 451 * 452 ******************************************************************************/ 453 const char* PORT_GetResultString(const uint8_t result_code); 454 455 /******************************************************************************* 456 * 457 * Function PORT_GetSecurityMask 458 * 459 * Description This function returns the security bitmask for a port. 460 * 461 * Returns the security bitmask. 462 * 463 ******************************************************************************/ 464 int PORT_GetSecurityMask(uint16_t handle, uint16_t* sec_mask); 465 466 #endif /* PORT_API_H */ 467