Monado OpenXR Runtime
Loading...
Searching...
No Matches
os_serial.h
Go to the documentation of this file.
1// Copyright 2019, Collabora, Ltd.
2// Copyright 2026, Beyley Cardellio
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Wrapper around OS native serial functions.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
9 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
10 *
11 * @ingroup aux_os
12 */
13
14#pragma once
15
16#include "xrt/xrt_config_os.h"
17#include "xrt/xrt_compiler.h"
18
19#include <stdint.h>
20#include <stddef.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26
27/*!
28 * @interface os_serial_device
29 *
30 * Representing a single serial interface on a device.
31 */
33{
34 ssize_t (*read)(struct os_serial_device *serial_dev, uint8_t *data, size_t size, int milliseconds);
35
36 ssize_t (*write)(struct os_serial_device *serial_dev, const uint8_t *data, size_t size);
37
38 int (*set_line_control)(struct os_serial_device *serial_dev, bool dtr, bool rts);
39
40 void (*destroy)(struct os_serial_device *serial_dev);
41};
42
43enum os_serial_parity_mode
44{
45 OS_SERIAL_PARITY_NONE,
46 OS_SERIAL_PARITY_EVEN,
47 OS_SERIAL_PARITY_ODD,
48};
49
50/*!
51 * Represents the parameters for opening a serial device, such as baud rate, parity, etc.
52 */
54{
55 uint32_t baud_rate;
56 uint8_t data_bits; // must be 5, 6, 7, or 8 bits
57 uint8_t stop_bits; // must be 1 or 2
58 enum os_serial_parity_mode parity;
59};
60
61/*!
62 * Read the next input report, if any, from the given serial device.
63 *
64 * If milliseconds are negative, this call blocks indefinitely, 0 polls,
65 * and positive will block for that amount of milliseconds.
66 *
67 * @public @memberof os_serial_device
68 */
69static inline ssize_t
70os_serial_read(struct os_serial_device *serial_dev, uint8_t *data, size_t size, int milliseconds)
71{
72 return serial_dev->read(serial_dev, data, size, milliseconds);
73}
74
75/*!
76 * Write an output report to the given device.
77 *
78 * @public @memberof os_serial_device
79 */
80static inline ssize_t
81os_serial_write(struct os_serial_device *serial_dev, const uint8_t *data, size_t size)
82{
83 return serial_dev->write(serial_dev, data, size);
84}
85
86/*!
87 * Set the line control signals (DTR and RTS) for the given serial device.
88 *
89 * @public @memberof os_serial_device
90 */
91static inline int
92os_serial_set_line_control(struct os_serial_device *serial_dev, bool dtr, bool rts)
93{
94 return serial_dev->set_line_control(serial_dev, dtr, rts);
95}
96
97/*!
98 * Close and free the given device.
99 *
100 * @public @memberof os_serial_device
101 */
102static inline void
104{
105 serial_dev->destroy(serial_dev);
106}
107
108int
109os_serial_open(const char *path, const struct os_serial_parameters *parameters, struct os_serial_device **out_serial);
110
111#ifdef __cplusplus
112} // extern "C"
113#endif
Representing a single serial interface on a device.
Definition os_serial.h:33
static ssize_t os_serial_read(struct os_serial_device *serial_dev, uint8_t *data, size_t size, int milliseconds)
Read the next input report, if any, from the given serial device.
Definition os_serial.h:70
static int os_serial_set_line_control(struct os_serial_device *serial_dev, bool dtr, bool rts)
Set the line control signals (DTR and RTS) for the given serial device.
Definition os_serial.h:92
static void os_serial_destroy(struct os_serial_device *serial_dev)
Close and free the given device.
Definition os_serial.h:103
static ssize_t os_serial_write(struct os_serial_device *serial_dev, const uint8_t *data, size_t size)
Write an output report to the given device.
Definition os_serial.h:81
Represents the parameters for opening a serial device, such as baud rate, parity, etc.
Definition os_serial.h:54
Header holding common defines.
Auto detect OS and certain features.