1 | /** @file
|
---|
2 | * IPRT Serial Port API.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2017 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_serialport_h
|
---|
27 | #define ___iprt_serialport_h
|
---|
28 |
|
---|
29 | #include <iprt/err.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 |
|
---|
32 | RT_C_DECLS_BEGIN
|
---|
33 |
|
---|
34 | /** @defgroup grp_rt_serial IPRT Serial Port API
|
---|
35 | * @ingroup grp_rt
|
---|
36 | *
|
---|
37 | * The IPRT serial port API provides a platform independent API to control a
|
---|
38 | * serial port of the host. It supports receiving/transmitting data as well as
|
---|
39 | * controlling and monitoring the status lines of a standard serial port.
|
---|
40 | *
|
---|
41 | * The user of the API is currently resposible for serializing calls to it.
|
---|
42 | * The only exception is RTSerialPortEvtPollInterrupt() which can be called on
|
---|
43 | * any thread to interrupt another thread waiting in RTSerialPortEvtPoll().
|
---|
44 | *
|
---|
45 | * @{
|
---|
46 | */
|
---|
47 |
|
---|
48 | /** Serial Port handle. */
|
---|
49 | typedef struct RTSERIALPORTINTERNAL *RTSERIALPORT;
|
---|
50 | /** Pointer to a Serial Port handle. */
|
---|
51 | typedef RTSERIALPORT *PRTSERIALPORT;
|
---|
52 | /** NIL Serial Port handle value. */
|
---|
53 | #define NIL_RTSERIALPORT ((RTSERIALPORT)0)
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Supported parity settings.
|
---|
58 | */
|
---|
59 | typedef enum RTSERIALPORTPARITY
|
---|
60 | {
|
---|
61 | /** Invalid parity setting. */
|
---|
62 | RTSERIALPORTPARITY_INVALID = 0,
|
---|
63 | /** No parity used. */
|
---|
64 | RTSERIALPORTPARITY_NONE,
|
---|
65 | /** Even parity used. */
|
---|
66 | RTSERIALPORTPARITY_EVEN,
|
---|
67 | /** Odd parity used. */
|
---|
68 | RTSERIALPORTPARITY_ODD,
|
---|
69 | /** Mark parity (parity bit always 1) used. */
|
---|
70 | RTSERIALPORTPARITY_MARK,
|
---|
71 | /** Space parity (parity bit always 0) used. */
|
---|
72 | RTSERIALPORTPARITY_SPACE,
|
---|
73 | /** 32bit hack. */
|
---|
74 | RTSERIALPORTPARITY_32BIT_HACK = 0x7fffffff
|
---|
75 | } RTSERIALPORTPARITY;
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Supported data bit count setting.
|
---|
80 | */
|
---|
81 | typedef enum RTSERIALPORTDATABITS
|
---|
82 | {
|
---|
83 | /** Invalid bitcount setting. */
|
---|
84 | RTSERIALPORTDATABITS_INVALID = 0,
|
---|
85 | /** 5 data bits. */
|
---|
86 | RTSERIALPORTDATABITS_5BITS,
|
---|
87 | /** 6 data bits. */
|
---|
88 | RTSERIALPORTDATABITS_6BITS,
|
---|
89 | /** 7 data bits. */
|
---|
90 | RTSERIALPORTDATABITS_7BITS,
|
---|
91 | /** 8 data bits. */
|
---|
92 | RTSERIALPORTDATABITS_8BITS,
|
---|
93 | /** 32bit hack. */
|
---|
94 | RTSERIALPORTDATABITS_32BIT_HACK = 0x7fffffff
|
---|
95 | } RTSERIALPORTDATABITS;
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Supported stop bit setting.
|
---|
100 | */
|
---|
101 | typedef enum RTSERIALPORTSTOPBITS
|
---|
102 | {
|
---|
103 | /** Invalid stop bit setting. */
|
---|
104 | RTSERIALPORTSTOPBITS_INVALID = 0,
|
---|
105 | /** One stop bit is used. */
|
---|
106 | RTSERIALPORTSTOPBITS_ONE,
|
---|
107 | /** 1.5 stop bits are used. */
|
---|
108 | RTSERIALPORTSTOPBITS_ONEPOINTFIVE,
|
---|
109 | /** 2 stop bits are used. */
|
---|
110 | RTSERIALPORTSTOPBITS_TWO,
|
---|
111 | /** 32bit hack. */
|
---|
112 | RTSERIALPORTSTOPBITS_32BIT_HACK = 0x7fffffff
|
---|
113 | } RTSERIALPORTSTOPBITS;
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Serial port config structure.
|
---|
118 | */
|
---|
119 | typedef struct RTSERIALPORTCFG
|
---|
120 | {
|
---|
121 | /** Baud rate. */
|
---|
122 | uint32_t uBaudRate;
|
---|
123 | /** Used parity. */
|
---|
124 | RTSERIALPORTPARITY enmParity;
|
---|
125 | /** Number of data bits. */
|
---|
126 | RTSERIALPORTDATABITS enmDataBitCount;
|
---|
127 | /** Number of stop bits. */
|
---|
128 | RTSERIALPORTSTOPBITS enmStopBitCount;
|
---|
129 | } RTSERIALPORTCFG;
|
---|
130 | /** Pointer to a serial port config. */
|
---|
131 | typedef RTSERIALPORTCFG *PRTSERIALPORTCFG;
|
---|
132 | /** Pointer to a const serial port config. */
|
---|
133 | typedef const RTSERIALPORTCFG *PCRTSERIALPORTCFG;
|
---|
134 |
|
---|
135 |
|
---|
136 | /** @name RTSerialPortOpen flags
|
---|
137 | * @{ */
|
---|
138 | /** Open the serial port with the receiver enabled to receive data. */
|
---|
139 | #define RTSERIALPORT_OPEN_F_READ RT_BIT(0)
|
---|
140 | /** Open the serial port with the transmitter enabled to transmit data. */
|
---|
141 | #define RTSERIALPORT_OPEN_F_WRITE RT_BIT(1)
|
---|
142 | /** Open the serial port with status line monitoring enabled to get notified about status line changes. */
|
---|
143 | #define RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING RT_BIT(2)
|
---|
144 | /** Open the serial port with BREAK condition detection enabled (Requires extra work on some hosts). */
|
---|
145 | #define RTSERIALPORT_OPEN_F_DETECT_BREAK_CONDITION RT_BIT(3)
|
---|
146 | /** Open the serial port with loopback mode enabled. */
|
---|
147 | #define RTSERIALPORT_OPEN_F_ENABLE_LOOPBACK RT_BIT(4)
|
---|
148 | /** Bitmask of valid flags. */
|
---|
149 | #define RTSERIALPORT_OPEN_F_VALID_MASK UINT32_C(0x0000001f)
|
---|
150 | /** @} */
|
---|
151 |
|
---|
152 |
|
---|
153 | /** @name RTSerialPortChgModemLines flags
|
---|
154 | * @{ */
|
---|
155 | /** Change the RTS (Ready To Send) line signal. */
|
---|
156 | #define RTSERIALPORT_CHG_STS_LINES_F_RTS RT_BIT(0)
|
---|
157 | /** Change the DTR (Data Terminal Ready) line signal. */
|
---|
158 | #define RTSERIALPORT_CHG_STS_LINES_F_DTR RT_BIT(1)
|
---|
159 | /** Bitmask of valid flags. */
|
---|
160 | #define RTSERIALPORT_CHG_STS_LINES_F_VALID_MASK UINT32_C(0x00000003)
|
---|
161 | /** @} */
|
---|
162 |
|
---|
163 |
|
---|
164 | /** @name RTSerialPortQueryStatusLines flags
|
---|
165 | * @{ */
|
---|
166 | /** The DCD (Data Carrier Detect) signal is active. */
|
---|
167 | #define RTSERIALPORT_STS_LINE_DCD RT_BIT(0)
|
---|
168 | /** The RI (Ring Indicator) signal is active. */
|
---|
169 | #define RTSERIALPORT_STS_LINE_RI RT_BIT(1)
|
---|
170 | /** The DSR (Data Set Ready) signal is active. */
|
---|
171 | #define RTSERIALPORT_STS_LINE_DSR RT_BIT(2)
|
---|
172 | /** The CTS (Clear To Send) signal is active. */
|
---|
173 | #define RTSERIALPORT_STS_LINE_CTS RT_BIT(3)
|
---|
174 | /** @} */
|
---|
175 |
|
---|
176 |
|
---|
177 | /** @name RTSerialPortEvtPoll flags
|
---|
178 | * @{ */
|
---|
179 | /** Data was received and can be read. */
|
---|
180 | #define RTSERIALPORT_EVT_F_DATA_RX RT_BIT(0)
|
---|
181 | /** All data was transmitted and there is room again in the transmit buffer. */
|
---|
182 | #define RTSERIALPORT_EVT_F_DATA_TX RT_BIT(1)
|
---|
183 | /** A BREAK condition was detected on the communication channel.
|
---|
184 | * Only available when BREAK condition detection was enabled when opening the serial port .*/
|
---|
185 | #define RTSERIALPORT_EVT_F_BREAK_DETECTED RT_BIT(2)
|
---|
186 | /** One of the monitored status lines changed, check with RTSerialPortQueryStatusLines().
|
---|
187 | * Only available if status line monitoring was enabled when opening the serial port. */
|
---|
188 | #define RTSERIALPORT_EVT_F_STATUS_LINE_CHANGED RT_BIT(3)
|
---|
189 | /** Status line monitor failed with an error and status line monitoring is disabled,
|
---|
190 | * this cannot be given in the event mask but will be set if status line
|
---|
191 | * monitoring is enabled and the monitor failed. */
|
---|
192 | #define RTSERIALPORT_EVT_F_STATUS_LINE_MONITOR_FAILED RT_BIT(4)
|
---|
193 | /** Bitmask of valid flags. */
|
---|
194 | #define RTSERIALPORT_EVT_F_VALID_MASK UINT32_C(0x0000001f)
|
---|
195 | /** @} */
|
---|
196 |
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Opens a serial port with the specified flags.
|
---|
200 | *
|
---|
201 | * @returns IPRT status code.
|
---|
202 | * @param phSerialPort Where to store the IPRT serial port handle on success.
|
---|
203 | * @param pszPortAddress The address of the serial port (host dependent).
|
---|
204 | * @param fFlags Flags to open the serial port with, see RTSERIALPORT_OPEN_F_*.
|
---|
205 | */
|
---|
206 | RTDECL(int) RTSerialPortOpen(PRTSERIALPORT phSerialPort, const char *pszPortAddress, uint32_t fFlags);
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Closes the given serial port handle.
|
---|
211 | *
|
---|
212 | * @returns IPRT status code.
|
---|
213 | * @param hSerialPort The IPRT serial port handle.
|
---|
214 | */
|
---|
215 | RTDECL(int) RTSerialPortClose(RTSERIALPORT hSerialPort);
|
---|
216 |
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Gets the native handle for an IPRT serial port handle.
|
---|
220 | *
|
---|
221 | * @returns The native handle. -1 on failure.
|
---|
222 | * @param hSerialPort The IPRT serial port handle.
|
---|
223 | */
|
---|
224 | RTDECL(RTHCINTPTR) RTSerialPortToNative(RTSERIALPORT hSerialPort);
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Tries to read the given number of bytes from the serial port, blocking version.
|
---|
229 | *
|
---|
230 | * @returns IPRT status code.
|
---|
231 | * @retval VERR_SERIALPORT_BREAK_DETECTED if a break was detected before the requested number of bytes was received.
|
---|
232 | * @param hSerialPort The IPRT serial port handle.
|
---|
233 | * @param pvBuf Where to store the read data.
|
---|
234 | * @param cbToRead How much to read from the serial port.
|
---|
235 | * @param pcbRead Where to store the number of bytes received until an error condition occurred, optional.
|
---|
236 | */
|
---|
237 | RTDECL(int) RTSerialPortRead(RTSERIALPORT hSerialPort, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Tries to read the given number of bytes from the serial port, non-blocking version.
|
---|
242 | *
|
---|
243 | * @returns IPRT status code.
|
---|
244 | * @retval VERR_SERIALPORT_BREAK_DETECTED if a break was detected before anything could be received.
|
---|
245 | * @retval VINF_TRY_AGAIN if nothing could be read.
|
---|
246 | * @param hSerialPort The IPRT serial port handle.
|
---|
247 | * @param pvBuf Where to store the read data.
|
---|
248 | * @param cbToRead How much to read from the serial port.
|
---|
249 | * @param pcbRead Where to store the number of bytes received.
|
---|
250 | */
|
---|
251 | RTDECL(int) RTSerialPortReadNB(RTSERIALPORT hSerialPort, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
252 |
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Writes the given data to the serial port, blocking version.
|
---|
256 | *
|
---|
257 | * @returns IPRT status code.
|
---|
258 | * @param hSerialPort The IPRT serial port handle.
|
---|
259 | * @param pvBuf The data to write.
|
---|
260 | * @param cbToWrite How much to write.
|
---|
261 | * @param pcbWritten Where to store the number of bytes written until an error condition occurred, optional.
|
---|
262 | */
|
---|
263 | RTDECL(int) RTSerialPortWrite(RTSERIALPORT hSerialPort, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Writes the given data to the serial port, non-blocking version.
|
---|
268 | *
|
---|
269 | * @returns IPRT status code.
|
---|
270 | * @retval VINF_TRY_AGAIN if nothing could be written.
|
---|
271 | * @param hSerialPort The IPRT serial port handle.
|
---|
272 | * @param pvBuf The data to write.
|
---|
273 | * @param cbToWrite How much to write.
|
---|
274 | * @param pcbWritten Where to store the number of bytes written.
|
---|
275 | */
|
---|
276 | RTDECL(int) RTSerialPortWriteNB(RTSERIALPORT hSerialPort, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
|
---|
277 |
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Queries the current active serial port config.
|
---|
281 | *
|
---|
282 | * @returns IPRT status code.
|
---|
283 | * @param hSerialPort The IPRT serial port handle.
|
---|
284 | * @param pCfg Where to store the current active config.
|
---|
285 | */
|
---|
286 | RTDECL(int) RTSerialPortCfgQueryCurrent(RTSERIALPORT hSerialPort, PRTSERIALPORTCFG pCfg);
|
---|
287 |
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Change the serial port to the given config.
|
---|
291 | *
|
---|
292 | * @returns IPRT status code.
|
---|
293 | * @retval VERR_SERIALPORT_INVALID_BAUDRATE if the baud rate is not supported on the serial port.
|
---|
294 | * @param hSerialPort The IPRT serial port handle.
|
---|
295 | * @param pCfg The config to write.
|
---|
296 | * @param pErrInfo Where to store additional information on error, optional.
|
---|
297 | */
|
---|
298 | RTDECL(int) RTSerialPortCfgSet(RTSERIALPORT hSerialPort, PCRTSERIALPORTCFG pCfg, PRTERRINFO pErrInfo);
|
---|
299 |
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Poll for an event on the given serial port.
|
---|
303 | *
|
---|
304 | * @returns IPRT status code.
|
---|
305 | * @retval VERR_TIMEOUT if the timeout was reached before an event happened.
|
---|
306 | * @retval VERR_INTERRUPTED if another thread interrupted the polling through RTSerialPortEvtPollInterrupt().
|
---|
307 | * @param hSerialPort The IPRT serial port handle.
|
---|
308 | * @param fEvtMask The mask of events to receive, see RTSERIALPORT_EVT_F_*
|
---|
309 | * @param pfEvtsRecv Where to store the bitmask of events received.
|
---|
310 | * @param msTimeout Number of milliseconds to wait for an event.
|
---|
311 | */
|
---|
312 | RTDECL(int) RTSerialPortEvtPoll(RTSERIALPORT hSerialPort, uint32_t fEvtMask, uint32_t *pfEvtsRecv,
|
---|
313 | RTMSINTERVAL msTimeout);
|
---|
314 |
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Interrupt another thread currently polling for an event.
|
---|
318 | *
|
---|
319 | * @returns IPRT status code.
|
---|
320 | * @param hSerialPort The IPRT serial port handle.
|
---|
321 | *
|
---|
322 | * @note Any thread.
|
---|
323 | */
|
---|
324 | RTDECL(int) RTSerialPortEvtPollInterrupt(RTSERIALPORT hSerialPort);
|
---|
325 |
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Sets or clears a BREAK condition on the given serial port.
|
---|
329 | *
|
---|
330 | * @returns IPRT status code.
|
---|
331 | * @param hSerialPort The IPRT serial port handle.
|
---|
332 | * @param fSet Flag whether to set the BREAK condition or clear it.
|
---|
333 | */
|
---|
334 | RTDECL(int) RTSerialPortChgBreakCondition(RTSERIALPORT hSerialPort, bool fSet);
|
---|
335 |
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Modify the status lines of the given serial port.
|
---|
339 | *
|
---|
340 | * @returns IPRT status code.
|
---|
341 | * @param hSerialPort The IPRT serial port handle.
|
---|
342 | * @param fClear Combination of status lines to clear, see RTSERIALPORT_CHG_STS_LINES_F_*.
|
---|
343 | * @param fSet Combination of status lines to set, see RTSERIALPORT_CHG_STS_LINES_F_*.
|
---|
344 | *
|
---|
345 | * @note fClear takes precedence over fSet in case the same status line bit is set in both arguments.
|
---|
346 | */
|
---|
347 | RTDECL(int) RTSerialPortChgStatusLines(RTSERIALPORT hSerialPort, uint32_t fClear, uint32_t fSet);
|
---|
348 |
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Query the status of the status lines on the given serial port.
|
---|
352 | *
|
---|
353 | * @returns IPRT status code.
|
---|
354 | * @param hSerialPort The IPRT serial port handle.
|
---|
355 | * @param pfStsLines Where to store the bitmask of active status lines on success,
|
---|
356 | * see RTSERIALPORT_STS_LINE_*.
|
---|
357 | */
|
---|
358 | RTDECL(int) RTSerialPortQueryStatusLines(RTSERIALPORT hSerialPort, uint32_t *pfStsLines);
|
---|
359 |
|
---|
360 | /** @} */
|
---|
361 |
|
---|
362 | RT_C_DECLS_END
|
---|
363 |
|
---|
364 | #endif
|
---|
365 |
|
---|