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_CreateConnectionWithSecurity(uint16_t uuid, uint8_t scn,
185                                                bool is_server, uint16_t mtu,
186                                                const RawAddress& bd_addr,
187                                                uint16_t* p_handle,
188                                                tPORT_CALLBACK* p_mgmt_cb,
189                                                uint16_t sec_mask);
190 
191 extern void RFCOMM_ClearSecurityRecord(uint32_t scn);
192 
193 extern int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server,
194                                    uint16_t mtu, const RawAddress& bd_addr,
195                                    uint16_t* p_handle,
196                                    tPORT_CALLBACK* p_mgmt_cb);
197 
198 /*******************************************************************************
199  *
200  * Function         RFCOMM_RemoveConnection
201  *
202  * Description      This function is called to close the specified connection.
203  *
204  * Parameters:      handle     - Handle of the port returned in the Open
205  *
206  ******************************************************************************/
207 extern int RFCOMM_RemoveConnection(uint16_t handle);
208 
209 /*******************************************************************************
210  *
211  * Function         RFCOMM_RemoveServer
212  *
213  * Description      This function is called to close the server port.
214  *
215  * Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
216  *
217  ******************************************************************************/
218 extern int RFCOMM_RemoveServer(uint16_t handle);
219 
220 /*******************************************************************************
221  *
222  * Function         PORT_SetEventCallback
223  *
224  * Description      Set event callback the specified connection.
225  *
226  * Parameters:      handle       - Handle of the port returned in the Open
227  *                  p_callback   - address of the callback function which should
228  *                                 be called from the RFCOMM when an event
229  *                                 specified in the mask occurs.
230  *
231  ******************************************************************************/
232 extern int PORT_SetEventCallback(uint16_t port_handle,
233                                  tPORT_CALLBACK* p_port_cb);
234 
235 /*******************************************************************************
236  *
237  * Function         PORT_ClearKeepHandleFlag
238  *
239  * Description      Called to clear the keep handle flag, which will cause
240  *                  not to keep the port handle open when closed
241  *
242  * Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
243  *
244  ******************************************************************************/
245 int PORT_ClearKeepHandleFlag(uint16_t port_handle);
246 
247 extern int PORT_SetDataCOCallback(uint16_t port_handle,
248                                   tPORT_DATA_CO_CALLBACK* p_port_cb);
249 /*******************************************************************************
250  *
251  * Function         PORT_SetEventMask
252  *
253  * Description      This function is called to close the specified connection.
254  *
255  * Parameters:      handle - Handle of the port returned in the Open
256  *                  mask   - specifies events to be enabled.  A value
257  *                           of zero disables all events.
258  *
259  ******************************************************************************/
260 extern int PORT_SetEventMask(uint16_t port_handle, uint32_t mask);
261 
262 /*******************************************************************************
263  *
264  * Function         PORT_CheckConnection
265  *
266  * Description      This function returns PORT_SUCCESS if connection referenced
267  *                  by handle is up and running
268  *
269  * Parameters:      handle     - Handle of the port returned in the Open
270  *                  bd_addr    - OUT bd_addr of the peer
271  *                  p_lcid     - OUT L2CAP's LCID
272  *
273  ******************************************************************************/
274 extern int PORT_CheckConnection(uint16_t handle, RawAddress* bd_addr,
275                                 uint16_t* p_lcid);
276 
277 /*******************************************************************************
278  *
279  * Function         PORT_IsOpening
280  *
281  * Description      This function returns true if there is any RFCOMM connection
282  *                  opening in process.
283  *
284  * Parameters:      true if any connection opening is found
285  *                  bd_addr    - bd_addr of the peer
286  *
287  ******************************************************************************/
288 extern bool PORT_IsOpening(RawAddress* bd_addr);
289 
290 /*******************************************************************************
291  *
292  * Function         PORT_SetState
293  *
294  * Description      This function configures connection according to the
295  *                  specifications in the tPORT_STATE structure.
296  *
297  * Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
298  *                  p_settings - Pointer to a tPORT_STATE structure containing
299  *                               configuration information for the connection.
300  *
301  ******************************************************************************/
302 extern int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings);
303 
304 /*******************************************************************************
305  *
306  * Function         PORT_GetState
307  *
308  * Description      This function is called to fill tPORT_STATE structure
309  *                  with the current control settings for the port
310  *
311  * Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
312  *                  p_settings - Pointer to a tPORT_STATE structure in which
313  *                               configuration information is returned.
314  *
315  ******************************************************************************/
316 extern int PORT_GetState(uint16_t handle, tPORT_STATE* p_settings);
317 
318 /*******************************************************************************
319  *
320  * Function         PORT_FlowControl_MaxCredit
321  *
322  * Description      This function directs a specified connection to pass
323  *                  flow control message to the peer device.  Enable flag passed
324  *                  shows if port can accept more data. It also sends max credit
325  *                  when data flow enabled
326  *
327  * Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
328  *                  enable     - enables data flow
329  *
330  ******************************************************************************/
331 extern int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable);
332 
333 #define PORT_DTRDSR_ON 0x01
334 #define PORT_CTSRTS_ON 0x02
335 #define PORT_RING_ON 0x04
336 #define PORT_DCD_ON 0x08
337 
338 /*
339  * Define default initial local modem signals state after connection established
340 */
341 #define PORT_OBEX_DEFAULT_SIGNAL_STATE \
342   (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON)
343 #define PORT_SPP_DEFAULT_SIGNAL_STATE \
344   (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON)
345 #define PORT_PPP_DEFAULT_SIGNAL_STATE \
346   (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON)
347 #define PORT_DUN_DEFAULT_SIGNAL_STATE (PORT_DTRDSR_ON | PORT_CTSRTS_ON)
348 
349 #define PORT_ERR_BREAK 0x01   /* Break condition occured on the peer device */
350 #define PORT_ERR_OVERRUN 0x02 /* Overrun is reported by peer device */
351 #define PORT_ERR_FRAME 0x04   /* Framing error reported by peer device */
352 #define PORT_ERR_RXOVER 0x08  /* Input queue overflow occured */
353 #define PORT_ERR_TXFULL 0x10  /* Output queue overflow occured */
354 
355 /*******************************************************************************
356  *
357  * Function         PORT_ReadData
358  *
359  * Description      Normally application will call this function after receiving
360  *                  PORT_EVT_RXCHAR event.
361  *
362  * Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
363  *                                callback.
364  *                  p_data      - Data area
365  *                  max_len     - Byte count requested
366  *                  p_len       - Byte count received
367  *
368  ******************************************************************************/
369 extern int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len,
370                          uint16_t* p_len);
371 
372 /*******************************************************************************
373  *
374  * Function         PORT_WriteData
375  *
376  * Description      This function is called from the legacy application to
377  *                  send data.
378  *
379  * Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
380  *                  p_data      - Data area
381  *                  max_len     - Byte count to write
382  *                  p_len       - Bytes written
383  *
384  ******************************************************************************/
385 extern int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len,
386                           uint16_t* p_len);
387 
388 /*******************************************************************************
389  *
390  * Function         PORT_WriteDataCO
391  *
392  * Description      Normally not GKI aware application will call this function
393  *                  to send data to the port by callout functions.
394  *
395  * Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
396  *
397  ******************************************************************************/
398 extern int PORT_WriteDataCO(uint16_t handle, int* p_len);
399 
400 /*******************************************************************************
401  *
402  * Function         RFCOMM_Init
403  *
404  * Description      This function is called to initialize RFCOMM layer
405  *
406  ******************************************************************************/
407 extern void RFCOMM_Init(void);
408 
409 /*******************************************************************************
410  *
411  * Function         PORT_SetTraceLevel
412  *
413  * Description      Set the trace level for RFCOMM. If called with
414  *                  a value of 0xFF, it simply reads the current trace level.
415  *
416  * Returns          the new (current) trace level
417  *
418  ******************************************************************************/
419 extern uint8_t PORT_SetTraceLevel(uint8_t new_level);
420 
421 /*******************************************************************************
422  *
423  * Function         PORT_GetResultString
424  *
425  * Description      This function returns the human-readable string for a given
426  *                  result code.
427  *
428  * Returns          a pointer to the human-readable string for the given
429  *                  result. Note that the string returned must not be freed.
430  *
431  ******************************************************************************/
432 extern const char* PORT_GetResultString(const uint8_t result_code);
433 
434 #endif /* PORT_API_H */
435