Skip to content

Commit d2940d8

Browse files
committed
Added BaudRate enum for termios
Issue #514
1 parent c3905ee commit d2940d8

File tree

2 files changed

+155
-8
lines changed

2 files changed

+155
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
## [Unreleased]
77

88
### Added
9+
- Added `::nix::sys::termios::BaudRate` enum to provide portable baudrate
10+
values. ([#518](https://github.com/nix-rust/nix/pull/518))
911
- Added a new `WaitStatus::PtraceEvent` to support ptrace events on Linux
1012
and Android ([([#438](https://github.com/nix-rust/nix/pull/438))
1113
- Added support for POSIX AIO
@@ -41,6 +43,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4143
([#503](https://github.com/nix-rust/nix/pull/503))
4244

4345
### Changed
46+
- `::nix::sys::termios::{cfgetispeed, cfsetispeed, cfgetospeed, cfsetospeed}`
47+
switched to use `BaudRate` enum from `speed_t`.
48+
([#518](https://github.com/nix-rust/nix/pull/518))
4449
- `epoll_ctl` now could accept None as argument `event`
4550
when op is `EpollOp::EpollCtlDel`.
4651
([#480](https://github.com/nix-rust/nix/pull/480))

src/sys/termios.rs

Lines changed: 150 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,72 @@ mod ffi {
125125
pub c_ospeed: speed_t
126126
}
127127

128+
#[derive(Clone, Copy, Debug, PartialEq)]
129+
pub enum BaudRate {
130+
B0,
131+
B50,
132+
B75,
133+
B110,
134+
B134,
135+
B150,
136+
B200,
137+
B300,
138+
B600,
139+
B1200,
140+
B1800,
141+
B2400,
142+
B4800,
143+
B9600,
144+
B19200,
145+
B38400,
146+
B7200,
147+
B14400,
148+
B28800,
149+
B57600,
150+
B76800,
151+
B115200,
152+
B230400,
153+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
154+
B460800,
155+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
156+
B921600,
157+
}
158+
159+
impl From<speed_t> for BaudRate {
160+
fn from(s: speed_t) -> BaudRate {
161+
match s {
162+
0 => BaudRate::B0,
163+
50 => BaudRate::B50,
164+
75 => BaudRate::B75,
165+
110 => BaudRate::B110,
166+
134 => BaudRate::B134,
167+
150 => BaudRate::B150,
168+
200 => BaudRate::B200,
169+
300 => BaudRate::B300,
170+
600 => BaudRate::B600,
171+
1200 => BaudRate::B1200,
172+
1800 => BaudRate::B1800,
173+
2400 => BaudRate::B2400,
174+
4800 => BaudRate::B4800,
175+
9600 => BaudRate::B9600,
176+
19200 => BaudRate::B19200,
177+
38400 => BaudRate::B38400,
178+
7200 => BaudRate::B7200,
179+
14400 => BaudRate::B14400,
180+
28800 => BaudRate::B28800,
181+
57600 => BaudRate::B57600,
182+
76800 => BaudRate::B76800,
183+
115200 => BaudRate::B115200,
184+
230400 => BaudRate::B230400,
185+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
186+
460800 => BaudRate::B460800,
187+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
188+
921600 => BaudRate::B921600,
189+
b @ _ => unreachable!("Invalid baud constant: {}", b),
190+
}
191+
}
192+
}
193+
128194
pub const VEOF: usize = 0;
129195
pub const VEOL: usize = 1;
130196
pub const VEOL2: usize = 2;
@@ -293,6 +359,82 @@ mod ffi {
293359
pub c_ospeed: speed_t
294360
}
295361

362+
#[derive(Clone, Copy, Debug, PartialEq)]
363+
pub enum BaudRate {
364+
B0,
365+
B50,
366+
B75,
367+
B110,
368+
B130,
369+
B150,
370+
B200,
371+
B300,
372+
B600,
373+
B1200,
374+
B1800,
375+
B2400,
376+
B4800,
377+
B9600,
378+
B19200,
379+
B38400,
380+
BOTHER,
381+
B57600,
382+
B115200,
383+
B230400,
384+
B460800,
385+
B500000,
386+
B576000,
387+
B921600,
388+
B1000000,
389+
B1152000,
390+
B1500000,
391+
B2000000,
392+
B2500000,
393+
B3000000,
394+
B3500000,
395+
B4000000,
396+
}
397+
398+
impl From<speed_t> for BaudRate {
399+
fn from(s: speed_t) -> BaudRate {
400+
match s {
401+
0 => BaudRate::B0,
402+
1 => BaudRate::B50,
403+
2 => BaudRate::B75,
404+
3 => BaudRate::B110,
405+
4 => BaudRate::B130,
406+
5 => BaudRate::B150,
407+
6 => BaudRate::B200,
408+
7 => BaudRate::B300,
409+
10 => BaudRate::B600,
410+
11 => BaudRate::B1200,
411+
12 => BaudRate::B1800,
412+
13 => BaudRate::B2400,
413+
14 => BaudRate::B4800,
414+
15 => BaudRate::B9600,
415+
16 => BaudRate::B19200,
416+
17 => BaudRate::B38400,
417+
10000 => BaudRate::BOTHER,
418+
10001 => BaudRate::B57600,
419+
10002 => BaudRate::B115200,
420+
10003 => BaudRate::B230400,
421+
10004 => BaudRate::B460800,
422+
10005 => BaudRate::B500000,
423+
10006 => BaudRate::B576000,
424+
10007 => BaudRate::B921600,
425+
10010 => BaudRate::B1000000,
426+
10011 => BaudRate::B1152000,
427+
10012 => BaudRate::B1500000,
428+
10013 => BaudRate::B2000000,
429+
10014 => BaudRate::B2500000,
430+
10015 => BaudRate::B3000000,
431+
10016 => BaudRate::B3500000,
432+
10017 => BaudRate::B4000000,
433+
b @ _ => unreachable!("Invalid baud constant: {}", b),
434+
}
435+
}
436+
}
437+
296438
pub const VEOF: usize = 4;
297439
pub const VEOL: usize = 11;
298440
pub const VEOL2: usize = 16;
@@ -426,27 +568,27 @@ mod ffi {
426568
}
427569
}
428570

429-
pub fn cfgetispeed(termios: &Termios) -> speed_t {
571+
pub fn cfgetispeed(termios: &Termios) -> BaudRate {
430572
unsafe {
431-
ffi::cfgetispeed(termios)
573+
ffi::cfgetispeed(termios).into()
432574
}
433575
}
434576

435-
pub fn cfgetospeed(termios: &Termios) -> speed_t {
577+
pub fn cfgetospeed(termios: &Termios) -> BaudRate {
436578
unsafe {
437-
ffi::cfgetospeed(termios)
579+
ffi::cfgetospeed(termios).into()
438580
}
439581
}
440582

441-
pub fn cfsetispeed(termios: &mut Termios, speed: speed_t) -> Result<()> {
583+
pub fn cfsetispeed(termios: &mut Termios, baud: BaudRate) -> Result<()> {
442584
Errno::result(unsafe {
443-
ffi::cfsetispeed(termios, speed)
585+
ffi::cfsetispeed(termios, baud as speed_t)
444586
}).map(drop)
445587
}
446588

447-
pub fn cfsetospeed(termios: &mut Termios, speed: speed_t) -> Result<()> {
589+
pub fn cfsetospeed(termios: &mut Termios, baud: BaudRate) -> Result<()> {
448590
Errno::result(unsafe {
449-
ffi::cfsetospeed(termios, speed)
591+
ffi::cfsetospeed(termios, baud as speed_t)
450592
}).map(drop)
451593
}
452594

0 commit comments

Comments
 (0)