Mstdlib-1.24.0
m_io_serial.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_IO_SERIAL_H__
25#define __M_IO_SERIAL_H__
26
27#include <mstdlib/base/m_defs.h>
28#include <mstdlib/base/m_types.h>
29#include <mstdlib/io/m_io.h>
30#include <mstdlib/io/m_event.h>
31
32__BEGIN_DECLS
33
34/*! \addtogroup m_io_serial Serial Port I/O functions
35 * \ingroup m_eventio_base
36 *
37 * Serial Port I/O functions
38 *
39 * @{
40 */
41
42/*! Baud rate. */
44 M_IO_SERIAL_BAUD_0 = 0, /*!< Used to terminate the connection (drop DTR) */
57 M_IO_SERIAL_BAUD_7200 = 7200, /* Not POSIX */
59 M_IO_SERIAL_BAUD_14400 = 14400, /* Not POSIX */
61 M_IO_SERIAL_BAUD_28800 = 28800, /* Not POSIX */
63 /* Bauds below are not technically POSIX.1 and may not exist on all systems */
81};
83
84
85/*! Types of flow control. */
90};
92
93#define M_IO_SERIAL_MODE_MASK_BITS 0x000F
94#define M_IO_SERIAL_MODE_MASK_PARITY 0x00F0
95#define M_IO_SERIAL_MODE_MASK_STOPBITS 0x0F00
96#define M_IO_SERIAL_MODE_BITS_8 0x0000 /* CS8 */
97#define M_IO_SERIAL_MODE_BITS_7 0x0001 /* CS7 */
98#define M_IO_SERIAL_MODE_PARITY_NONE 0x0000 /* &= ~(PARENB | PARODD | CMSPAR) */
99#define M_IO_SERIAL_MODE_PARITY_EVEN 0x0010 /* PARENB */
100#define M_IO_SERIAL_MODE_PARITY_ODD 0x0020 /* PARENB | PARODD */
101#define M_IO_SERIAL_MODE_PARITY_MARK 0x0030 /* PARENB | CMSPAR | PARODD -- CMSPAR may be undefined */
102#define M_IO_SERIAL_MODE_PARITY_SPACE 0x0040 /* PARENB | CMSPAR - &= ~PARODD -- CMSPAR may be undefined */
103#define M_IO_SERIAL_MODE_STOPBITS_1 0x0000 /* &= ~(CSTOPB) */
104#define M_IO_SERIAL_MODE_STOPBITS_2 0x0100 /* CSTOPB */
105
106/*! Mode. */
108 /* Mode is split up into 3 4-bit sections */
112};
114
115/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
116
117/*! Flags to control behavior.
118 *
119 * These flags provide work around for broken system.
120 */
122 M_IO_SERIAL_FLAG_NONE = 0, /*!< Default, assume strict and proper behavior */
123 M_IO_SERIAL_FLAG_IGNORE_TERMIOS_FAILURE = 1 << 0, /*!< Ignore any termios (baud, mode, flow) setting failures.
124 Some serial port emulators may intentionally fail. */
125 M_IO_SERIAL_FLAG_NO_FLUSH_ON_CLOSE = 1 << 1, /*!< Do not flush any pending data on close. This may confuse
126 or lock up some serial port emulators. */
127 M_IO_SERIAL_FLAG_NO_RESTORE_ON_CLOSE = 1 << 2, /*!< Do not restore termios (baud, mode, flow) settings on
128 close. It is a best practice but often does not provide
129 any real benefit. */
130 M_IO_SERIAL_FLAG_ASYNC_TIMEOUT = 1 << 3, /*!< Windows Only. For Asynchronous reads use a timeout value
131 rather than infinite as some drivers may not allow
132 canceling of async reads (such as Citrix serial
133 forwarding). Not used if BUSY_POLLING is used */
134 M_IO_SERIAL_FLAG_BUSY_POLLING = 1 << 4 /*!< Windows Only. Perform busy-polling in a separate thread
135 rather than using asynchronous reads. This may work
136 around driver issues that do not properly support
137 Overlapped IO. */
140
141struct M_io_serial_enum;
143
144
145/*! Create a serial connection.
146 *
147 * \param[out] io_out io object for communication.
148 * \param[in] path Path to serial device.
149 * \param[in] baud Baud rate.
150 * \param[in] flowcontrol Flow control method.
151 * \param[in] mode Mode.
152 * \param[in] flags M_io_serial_flags_t mapping. M_IO_SERIAL_FLAG_IGNORE_TERMIOS_FAILURE may need to
153 * be enabled for some "virtual" serial ports, but the device will still open and be usable.
154 *
155 * \return Result.
156 */
157M_API M_io_error_t M_io_serial_create(M_io_t **io_out, const char *path, M_io_serial_baud_t baud, M_io_serial_flowcontrol_t flowcontrol, M_io_serial_mode_t mode, M_uint32 flags /* enum M_io_serial_flags */);
158
159
160/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161
162/*! Set the baud rate on a serial io object.
163 *
164 * \param[in] io io object.
165 * \param[in] baud Baud rate.
166 *
167 * \return Result.
168 */
170
171
172/*! Set the flow control on a serial io object.
173 *
174 * \param[in] io io object.
175 * \param[in] flowcontrol Flow control method.
176 *
177 * \return Result.
178 */
180
181
182/*! Set the mode on a serial io object.
183 *
184 * \param[in] io io object.
185 * \param[in] mode Mode.
186 *
187 * \return Result.
188 */
190
191
192/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
193
194/*! Get the flow control of an serial io object.
195 *
196 * \param[in] io io object.
197 * \param[out] flowcontrol Flow control method to return.
198 *
199 * \return Result.
200 */
202
203
204/*! Get the mode of an serial io object.
205 *
206 * \param[in] io io object.
207 * \param[out] mode Mode to return.
208 *
209 * \return Result.
210 */
212
213
214/*! Get the baud rate of an serial io object.
215 *
216 * \param[in] io io object.
217 * \param[out] baud Baud to return.
218 *
219 * \return Result.
220 */
222
223
224/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
225
226/*! Create a serial enumeration object.
227 *
228 * Use to determine what serial devices are connected. On some OS's this may
229 * be a list of device ports and not necessarily what's connected.
230 *
231 * \return Serial enumeration object.
232 */
233M_API M_io_serial_enum_t *M_io_serial_enum(M_bool include_modems);
234
235
236/*! Destroy a serial enumeration object.
237 *
238 * \param[in] serenum Serial enumeration object.
239 */
241
242
243/*! Number of serial objects in the enumeration.
244 *
245 * \param[in] serenum Serial enumeration object.
246 *
247 * \return Count of serial devices.
248 */
249M_API size_t M_io_serial_enum_count(const M_io_serial_enum_t *serenum);
250
251
252/*! Path of serial device as reported by the device.
253 *
254 * \param[in] serenum Serial enumeration object.
255 * \param[in] idx Index in serial enumeration.
256 *
257 * \return String.
258 */
259M_API const char *M_io_serial_enum_path(const M_io_serial_enum_t *serenum, size_t idx);
260
261/*! Name of serial device.
262 *
263 * \param[in] serenum Serial enumeration object.
264 * \param[in] idx Index in serial enumeration.
265 *
266 * \return String.
267 */
268M_API const char *M_io_serial_enum_name(const M_io_serial_enum_t *serenum, size_t idx);
269
270/*! @} */
271
272__END_DECLS
273
274#endif /* __M_IO_SERIAL_H__ */
enum M_io_serial_baud M_io_serial_baud_t
Definition: m_io_serial.h:82
M_io_serial_mode
Definition: m_io_serial.h:107
#define M_IO_SERIAL_MODE_STOPBITS_1
Definition: m_io_serial.h:103
void M_io_serial_enum_destroy(M_io_serial_enum_t *serenum)
M_io_error_t M_io_serial_set_baud(M_io_t *io, M_io_serial_baud_t baud)
#define M_IO_SERIAL_MODE_PARITY_EVEN
Definition: m_io_serial.h:99
size_t M_io_serial_enum_count(const M_io_serial_enum_t *serenum)
M_io_error_t M_io_serial_get_baud(M_io_t *io, M_io_serial_baud_t *baud)
const char * M_io_serial_enum_path(const M_io_serial_enum_t *serenum, size_t idx)
struct M_io_serial_enum M_io_serial_enum_t
Definition: m_io_serial.h:142
M_io_error_t M_io_serial_get_mode(M_io_t *io, M_io_serial_mode_t *mode)
M_io_error_t M_io_serial_create(M_io_t **io_out, const char *path, M_io_serial_baud_t baud, M_io_serial_flowcontrol_t flowcontrol, M_io_serial_mode_t mode, M_uint32 flags)
enum M_io_serial_flags M_io_serial_flags_t
Definition: m_io_serial.h:139
M_io_serial_flowcontrol
Definition: m_io_serial.h:86
enum M_io_serial_flowcontrol M_io_serial_flowcontrol_t
Definition: m_io_serial.h:91
#define M_IO_SERIAL_MODE_BITS_8
Definition: m_io_serial.h:96
#define M_IO_SERIAL_MODE_PARITY_ODD
Definition: m_io_serial.h:100
M_io_serial_enum_t * M_io_serial_enum(M_bool include_modems)
M_io_error_t M_io_serial_get_flowcontrol(M_io_t *io, M_io_serial_flowcontrol_t *flowcontrol)
#define M_IO_SERIAL_MODE_PARITY_NONE
Definition: m_io_serial.h:98
enum M_io_serial_mode M_io_serial_mode_t
Definition: m_io_serial.h:113
M_io_error_t M_io_serial_set_mode(M_io_t *io, M_io_serial_mode_t mode)
M_io_serial_flags
Definition: m_io_serial.h:121
M_io_error_t M_io_serial_set_flowcontrol(M_io_t *io, M_io_serial_flowcontrol_t flowcontrol)
M_io_serial_baud
Definition: m_io_serial.h:43
#define M_IO_SERIAL_MODE_BITS_7
Definition: m_io_serial.h:97
const char * M_io_serial_enum_name(const M_io_serial_enum_t *serenum, size_t idx)
@ M_IO_SERIAL_MODE_7E1
Definition: m_io_serial.h:110
@ M_IO_SERIAL_MODE_7O1
Definition: m_io_serial.h:111
@ M_IO_SERIAL_MODE_8N1
Definition: m_io_serial.h:109
@ M_IO_SERIAL_FLOWCONTROL_NONE
Definition: m_io_serial.h:87
@ M_IO_SERIAL_FLOWCONTROL_HARDWARE
Definition: m_io_serial.h:88
@ M_IO_SERIAL_FLOWCONTROL_SOFTWARE
Definition: m_io_serial.h:89
@ M_IO_SERIAL_FLAG_NONE
Definition: m_io_serial.h:122
@ M_IO_SERIAL_FLAG_ASYNC_TIMEOUT
Definition: m_io_serial.h:130
@ M_IO_SERIAL_FLAG_NO_RESTORE_ON_CLOSE
Definition: m_io_serial.h:127
@ M_IO_SERIAL_FLAG_NO_FLUSH_ON_CLOSE
Definition: m_io_serial.h:125
@ M_IO_SERIAL_FLAG_IGNORE_TERMIOS_FAILURE
Definition: m_io_serial.h:123
@ M_IO_SERIAL_FLAG_BUSY_POLLING
Definition: m_io_serial.h:134
@ M_IO_SERIAL_BAUD_1000000
Definition: m_io_serial.h:73
@ M_IO_SERIAL_BAUD_128000
Definition: m_io_serial.h:66
@ M_IO_SERIAL_BAUD_134
Definition: m_io_serial.h:48
@ M_IO_SERIAL_BAUD_7200
Definition: m_io_serial.h:57
@ M_IO_SERIAL_BAUD_75
Definition: m_io_serial.h:46
@ M_IO_SERIAL_BAUD_1200
Definition: m_io_serial.h:53
@ M_IO_SERIAL_BAUD_200
Definition: m_io_serial.h:50
@ M_IO_SERIAL_BAUD_19200
Definition: m_io_serial.h:60
@ M_IO_SERIAL_BAUD_2500000
Definition: m_io_serial.h:77
@ M_IO_SERIAL_BAUD_2400
Definition: m_io_serial.h:55
@ M_IO_SERIAL_BAUD_4800
Definition: m_io_serial.h:56
@ M_IO_SERIAL_BAUD_1800
Definition: m_io_serial.h:54
@ M_IO_SERIAL_BAUD_50
Definition: m_io_serial.h:45
@ M_IO_SERIAL_BAUD_38400
Definition: m_io_serial.h:62
@ M_IO_SERIAL_BAUD_256000
Definition: m_io_serial.h:68
@ M_IO_SERIAL_BAUD_1500000
Definition: m_io_serial.h:75
@ M_IO_SERIAL_BAUD_460800
Definition: m_io_serial.h:69
@ M_IO_SERIAL_BAUD_28800
Definition: m_io_serial.h:61
@ M_IO_SERIAL_BAUD_14400
Definition: m_io_serial.h:59
@ M_IO_SERIAL_BAUD_3500000
Definition: m_io_serial.h:79
@ M_IO_SERIAL_BAUD_500000
Definition: m_io_serial.h:70
@ M_IO_SERIAL_BAUD_110
Definition: m_io_serial.h:47
@ M_IO_SERIAL_BAUD_576000
Definition: m_io_serial.h:71
@ M_IO_SERIAL_BAUD_57600
Definition: m_io_serial.h:64
@ M_IO_SERIAL_BAUD_2000000
Definition: m_io_serial.h:76
@ M_IO_SERIAL_BAUD_115200
Definition: m_io_serial.h:65
@ M_IO_SERIAL_BAUD_230400
Definition: m_io_serial.h:67
@ M_IO_SERIAL_BAUD_1152000
Definition: m_io_serial.h:74
@ M_IO_SERIAL_BAUD_921600
Definition: m_io_serial.h:72
@ M_IO_SERIAL_BAUD_4000000
Definition: m_io_serial.h:80
@ M_IO_SERIAL_BAUD_600
Definition: m_io_serial.h:52
@ M_IO_SERIAL_BAUD_9600
Definition: m_io_serial.h:58
@ M_IO_SERIAL_BAUD_300
Definition: m_io_serial.h:51
@ M_IO_SERIAL_BAUD_3000000
Definition: m_io_serial.h:78
@ M_IO_SERIAL_BAUD_0
Definition: m_io_serial.h:44
@ M_IO_SERIAL_BAUD_150
Definition: m_io_serial.h:49
enum M_io_error M_io_error_t
Definition: m_io.h:93
struct M_io M_io_t
Definition: m_io.h:59