VirtualBox

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

最後變更 在這個檔案從69892是 69892,由 vboxsync 提交於 7 年 前

Runtime: Start on a platform independent serial port access API which will be more advanced and cleaner than the code in DrvHostSerial. DrvHostSerial will be converted to use this API later on when it is mature. Additionally it will be used by a serial port test utility for automatic serial port testing in the validation kit

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 12.9 KB
 
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.alldomusa.eu.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
32RT_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. */
49typedef struct RTSERIALPORTINTERNAL *RTSERIALPORT;
50/** Pointer to a Serial Port handle. */
51typedef RTSERIALPORT *PRTSERIALPORT;
52/** NIL Serial Port handle value. */
53#define NIL_RTSERIALPORT ((RTSERIALPORT)0)
54
55
56/**
57 * Serial port event.
58 */
59typedef enum RTSERIALPORTEVT
60{
61 /** Invalid event. */
62 RTSERIALPORTEVT_INVALID = 0,
63 /** Data was received and can be read. */
64 RTSERIALPORTEVT_DATA_RX,
65 /** All data was transmitted and there is room again in the transmit buffer. */
66 RTSERIALPORTEVT_DATA_TX,
67 /** A BREAK condition was detected on the communication channel. */
68 RTSERIALPORTEVT_BREAK_DETECTED,
69 /** One of the monitored status lines changed, check with RTSerialPortQueryStatusLines(). */
70 RTSERIALPORTEVT_STATUS_LINE_CHANGED,
71 /** Status line monitor failed with an error and status line monitoring is disabled. */
72 RTSERIALPORTEVT_STATUS_LINE_MONITOR_FAILED,
73 /** 32bit hack. */
74 RTSERIALPORTEVT_32BIT_HACK = 0x7fffffff
75} RTSERIALPORTEVT;
76/** Pointer to a serial port event enum. */
77typedef RTSERIALPORTEVT *PRTSERIALPORTEVT;
78
79/**
80 * Supported parity settings.
81 */
82typedef enum RTSERIALPORTPARITY
83{
84 /** Invalid parity setting. */
85 RTSERIALPORTPARITY_INVALID = 0,
86 /** No parity used. */
87 RTSERIALPORTPARITY_NONE,
88 /** Even parity used. */
89 RTSERIALPORTPARITY_EVEN,
90 /** Odd parity used. */
91 RTSERIALPORTPARITY_ODD,
92 /** Mark parity (parity bit always 1) used. */
93 RTSERIALPORTPARITY_MARK,
94 /** Space parity (parity bit always 0) used. */
95 RTSERIALPORTPARITY_SPACE,
96 /** 32bit hack. */
97 RTSERIALPORTPARITY_32BIT_HACK = 0x7fffffff
98} RTSERIALPORTPARITY;
99
100
101/**
102 * Supported data bit count setting.
103 */
104typedef enum RTSERIALPORTDATABITS
105{
106 /** Invalid bitcount setting. */
107 RTSERIALPORTDATABITS_INVALID = 0,
108 /** 5 data bits. */
109 RTSERIALPORTDATABITS_5BITS,
110 /** 6 data bits. */
111 RTSERIALPORTDATABITS_6BITS,
112 /** 7 data bits. */
113 RTSERIALPORTDATABITS_7BITS,
114 /** 8 data bits. */
115 RTSERIALPORTDATABITS_8BITS,
116 /** 32bit hack. */
117 RTSERIALPORTDATABITS_32BIT_HACK = 0x7fffffff
118} RTSERIALPORTDATABITS;
119
120
121/**
122 * Supported stop bit setting.
123 */
124typedef enum RTSERIALPORTSTOPBITS
125{
126 /** Invalid stop bit setting. */
127 RTSERIALPORTSTOPBITS_INVALID = 0,
128 /** One stop bit is used. */
129 RTSERIALPORTSTOPBITS_ONE,
130 /** 1.5 stop bits are used. */
131 RTSERIALPORTSTOPBITS_ONEPOINTFIVE,
132 /** 2 stop bits are used. */
133 RTSERIALPORTSTOPBITS_TWO,
134 /** 32bit hack. */
135 RTSERIALPORTSTOPBITS_32BIT_HACK = 0x7fffffff
136} RTSERIALPORTSTOPBITS;
137
138
139/**
140 * Serial port config structure.
141 */
142typedef struct RTSERIALPORTCFG
143{
144 /** Baud rate. */
145 uint32_t uBaudRate;
146 /** Used parity. */
147 RTSERIALPORTPARITY enmParity;
148 /** Number of data bits. */
149 RTSERIALPORTDATABITS enmDataBitCount;
150 /** Number of stop bits. */
151 RTSERIALPORTSTOPBITS enmStopBitCount;
152} RTSERIALPORTCFG;
153/** Pointer to a serial port config. */
154typedef RTSERIALPORTCFG *PRTSERIALPORTCFG;
155/** Pointer to a const serial port config. */
156typedef const RTSERIALPORTCFG *PCRTSERIALPORTCFG;
157
158
159/** @name RTSerialPortOpen flags
160 * @{ */
161/** Open the serial port with the receiver enabled to receive data. */
162#define RT_SERIALPORT_OPEN_F_READ RT_BIT(0)
163/** Open the serial port with the transmitter enabled to transmit data. */
164#define RT_SERIALPORT_OPEN_F_WRITE RT_BIT(1)
165/** Open the serial port with status line monitoring enabled to get notified about status line changes. */
166#define RT_SERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING RT_BIT(2)
167/** Open the serial port with BREAK condition detection enabled (Requires extra work on some hosts). */
168#define RT_SERIALPORT_OPEN_F_DETECT_BREAK_CONDITION RT_BIT(3)
169/** Open the serial port with loopback mode enabled. */
170#define RT_SERIALPORT_OPEN_F_ENABLE_LOOPBACK RT_BIT(4)
171/** Bitmask of valid flags. */
172#define RT_SERIALPORT_OPEN_F_VALID_MASK UINT32_C(0x0000001f)
173/** @} */
174
175
176/** @name RTSerialPortChgModemLines flags
177 * @{ */
178/** Change the RTS (Ready To Send) line signal. */
179#define RT_SERIALPORT_CHG_STS_LINES_F_RTS RT_BIT(0)
180/** Change the DTR (Data Terminal Ready) line signal. */
181#define RT_SERIALPORT_CHG_STS_LINES_F_DTR RT_BIT(1)
182/** Bitmask of valid flags. */
183#define RT_SERIALPORT_CHG_STS_LINES_F_VALID_MASK UINT32_C(0x00000003)
184/** @} */
185
186
187/** @name RTSerialPortQueryStatusLines flags
188 * @{ */
189/** The DCD (Data Carrier Detect) signal is active. */
190#define RT_SERIALPORT_STS_LINE_DCD RT_BIT(0)
191/** The RI (Ring Indicator) signal is active. */
192#define RT_SERIALPORT_STS_LINE_RI RT_BIT(1)
193/** The DSR (Data Set Ready) signal is active. */
194#define RT_SERIALPORT_STS_LINE_DSR RT_BIT(2)
195/** The CTS (Clear To Send) signal is active. */
196#define RT_SERIALPORT_STS_LINE_CTS RT_BIT(3)
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 RT_SERIALPORT_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 penmEvt Where to store the event on success.
311 * @param msTimeout Number of milliseconds to wait for an event.
312 */
313RTDECL(int) RTSerialPortEvtPoll(RTSERIALPORT hSerialPort, RTSERIALPORTEVT *penmEvt, 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 * @thread Any thread.
323 */
324RTDECL(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 */
334RTDECL(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 RT_SERIALPORT_CHG_STS_LINES_F_*.
343 * @param fSet Combination of status lines to set, see RT_SERIALPORT_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 */
347RTDECL(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 RT_SERIALPORT_STS_LINE_*.
357 */
358RTDECL(int) RTSerialPortQueryStatusLines(RTSERIALPORT hSerialPort, uint32_t *pfStsLines);
359
360/** @} */
361
362RT_C_DECLS_END
363
364#endif
365
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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