1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// This file is part of file-descriptors. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/file-descriptors/master/COPYRIGHT. No part of file-descriptors, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2019 The developers of file-descriptors. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/file-descriptors/master/COPYRIGHT.
/// Baud rate.
#[derive(EnumIter, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u32)]
pub enum BaudRate
{
/// This is used to terminate the connection.
///
/// If specified, the modem control lines shall no longer be asserted.
/// Normally, this will disconnect the line.
HangUpModem = B0,
/// Baud rate of 50 bits per second (bps).
B50 = B50,
/// Baud rate of 75 bits per second (bps).
B75 = B75,
/// Baud rate of 110 bits per second (bps).
B110 = B110,
/// Baud rate of 134 bits per second (bps).
B134 = B134,
/// Baud rate of 150 bits per second (bps).
B150 = B150,
/// Baud rate of 200 bits per second (bps).
B200 = B200,
/// Baud rate of 300 bits per second (bps).
B300 = B300,
/// Baud rate of 600 bits per second (bps).
B600 = B600,
/// Baud rate of 1,200 bits per second (bps).
B1200 = B1200,
/// Baud rate of 1,800 bits per second (bps).
B1800 = B1800,
/// Baud rate of 2,400 bits per second (bps).
B2400 = B2400,
/// Baud rate of 4,800 bits per second (bps).
B4800 = B4800,
/// Baud rate of 9,600 bits per second (bps).
B9600 = B9600,
/// Baud rate of 19,200 bits per second (bps).
B19200 = B19200,
/// Baud rate of 38,400 bits per second (bps).
///
/// Actually, this can be almost any bit rate on linux; see `man 8 setserial`.
B38400 = B38400,
/// Baud rate of 57,600 bits per second (bps).
B57600 = B57600,
/// Baud rate of 115,200 bits per second (bps).
///
/// This is the default.
B115200 = B115200,
/// Baud rate of 230,400 bits per second (bps).
B230400 = B230400,
/// Baud rate of 500,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B500000 = B500000,
/// Baud rate of 576,600 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B576000 = B576000,
/// Baud rate of 460,800 bits per second (bps).
///
/// Only supported on Android, FreeBSD, ?Fuschia and Linux.
#[cfg(any(target_os = "android", taget_os = "freebsd", target_os = "fuschia", target_os = "linux"))]
B460800 = B460800,
/// Baud rate of 921,600 bits per second (bps).
///
/// Only supported on Android, FreeBSD, ?Fuschia and Linux.
#[cfg(any(target_os = "android", taget_os = "freebsd", target_os = "fuschia", target_os = "linux"))]
B921600 = B921600,
/// Baud rate of 1,000,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B1000000 = B1000000,
/// Baud rate of 1,152,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B1152000 = B1152000,
/// Baud rate of 1,500,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B1500000 = B1500000,
/// Baud rate of 2,000,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B2000000 = B2000000,
/// Baud rate of 2,500,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B2500000 = B2500000,
/// Baud rate of 3,000,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B3000000 = B3000000,
/// Baud rate of 3,500,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B3500000 = B3500000,
/// Baud rate of 4,000,000 bits per second (bps).
///
/// Only supported on Android, ?Fuschia and Linux.
#[cfg(any(target_os = "android", target_os = "fuschia", target_os = "linux"))]
B4000000 = B4000000,
}
impl Default for BaudRate
{
#[inline(always)]
fn default() -> Self
{
BaudRate::B115200
}
}
impl BaudRate
{
/// `EXTA` baud rate; 19,200 bits per second.
pub const ExtendedA: Self = BaudRate::B19200;
/// `EXTB` baud rate; 38,400 bits per second.
pub const ExtendedB: Self = BaudRate::B38400;
/// On Linux, the terminal input speed and terminal output speed can not differ.
#[inline(always)]
pub(crate) fn set_terminal_input_and_output_speed(self, terminal_options: &mut termios)
{
let result = unsafe { cfsetspeed(terminal_options, self as u32) };
if likely!(result == 0)
{
}
else if likely!(result == -1)
{
match errno().0
{
EINVAL => panic!("Baud was not valid (`& !CBAUD`)"),
_ => unreachable_code(format_args!("")),
}
}
else
{
unreachable_code(format_args!(""))
}
}
}