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