VirtualBox

source: vbox/trunk/include/iprt/serialport.h@ 77946

Last change on this file since 77946 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette