1 /**
2  * @brief "Bottom" of native tty uart driver
3  *
4  * When built with the native_simulator this will be built in the runner context,
5  * that is, with the host C library, and with the host include paths.
6  *
7  * Copyright (c) 2023 Marko Sagadin
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #ifndef DRIVERS_SERIAL_UART_NATIVE_TTY_BOTTOM_H
12 #define DRIVERS_SERIAL_UART_NATIVE_TTY_BOTTOM_H
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #include <stdint.h>
19 
20 /* Below enums are just differently namespaced copies of uart_config_* enums. Options that are not
21  * supported on the host are not listed.
22  */
23 enum native_tty_bottom_parity {
24 	NTB_PARITY_NONE,
25 	NTB_PARITY_ODD,
26 	NTB_PARITY_EVEN,
27 };
28 
29 enum native_tty_bottom_stop_bits {
30 	NTB_STOP_BITS_1,
31 	NTB_STOP_BITS_2,
32 };
33 
34 enum native_tty_bottom_data_bits {
35 	NTB_DATA_BITS_5,
36 	NTB_DATA_BITS_6,
37 	NTB_DATA_BITS_7,
38 	NTB_DATA_BITS_8,
39 };
40 
41 enum native_tty_bottom_flow_control {
42 	NTB_FLOW_CTRL_NONE,
43 };
44 
45 struct native_tty_bottom_cfg {
46 	uint32_t baudrate;
47 	enum native_tty_bottom_parity parity;
48 	enum native_tty_bottom_stop_bits stop_bits;
49 	enum native_tty_bottom_data_bits data_bits;
50 	enum native_tty_bottom_flow_control flow_ctrl;
51 };
52 
53 /* Note: None of these functions are public interfaces. They are internal to the native tty driver.
54  */
55 
56 /**
57  * @brief Check for available input on tty file descriptor
58  *
59  * @param fd
60  *
61  * @retval 1 if data is available
62  * @retval 0 if data is not available
63  * @retval <0 on error
64  */
65 int native_tty_poll_bottom(int fd);
66 
67 /**
68  * @brief Opens tty port on the given pathname
69  *
70  * Returned file descriptor can be then passed to native_tty_configure_bottom to configure it.
71  *
72  * @param pathname
73  *
74  * @return file descriptor
75  */
76 int native_tty_open_tty_bottom(const char *pathname);
77 
78 /**
79  * @brief Configure tty port
80  *
81  * @param fd	File descriptor of the tty port.
82  * @param cfg	Configuration struct.
83  *
84  * @retval 0	if successful,
85  * @retval -1	otherwise.
86  */
87 int native_tty_configure_bottom(int fd, struct native_tty_bottom_cfg *cfg);
88 
89 #ifdef __cplusplus
90 }
91 #endif
92 
93 #endif /* DRIVERS_SERIAL_UART_NATIVE_TTY_BOTTOM_H */
94