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