From d2940d85917e56dbcdb310ebee4205ddd24ff01e Mon Sep 17 00:00:00 2001 From: Zac Berkowitz Date: Thu, 16 Feb 2017 16:30:47 +0000 Subject: Added BaudRate enum for termios Issue #514 --- CHANGELOG.md | 5 ++ src/sys/termios.rs | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 155 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41c6f255..bb3aeedb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added +- Added `::nix::sys::termios::BaudRate` enum to provide portable baudrate + values. ([#518](https://github.com/nix-rust/nix/pull/518)) - Added a new `WaitStatus::PtraceEvent` to support ptrace events on Linux and Android ([([#438](https://github.com/nix-rust/nix/pull/438)) - Added support for POSIX AIO @@ -41,6 +43,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#503](https://github.com/nix-rust/nix/pull/503)) ### Changed +- `::nix::sys::termios::{cfgetispeed, cfsetispeed, cfgetospeed, cfsetospeed}` + switched to use `BaudRate` enum from `speed_t`. + ([#518](https://github.com/nix-rust/nix/pull/518)) - `epoll_ctl` now could accept None as argument `event` when op is `EpollOp::EpollCtlDel`. ([#480](https://github.com/nix-rust/nix/pull/480)) diff --git a/src/sys/termios.rs b/src/sys/termios.rs index 9622f392..c45627b3 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -125,6 +125,72 @@ mod ffi { pub c_ospeed: speed_t } + #[derive(Clone, Copy, Debug, PartialEq)] + pub enum BaudRate { + B0, + B50, + B75, + B110, + B134, + B150, + B200, + B300, + B600, + B1200, + B1800, + B2400, + B4800, + B9600, + B19200, + B38400, + B7200, + B14400, + B28800, + B57600, + B76800, + B115200, + B230400, + #[cfg(any(target_os = "netbsd", target_os = "freebsd"))] + B460800, + #[cfg(any(target_os = "netbsd", target_os = "freebsd"))] + B921600, + } + + impl From for BaudRate { + fn from(s: speed_t) -> BaudRate { + match s { + 0 => BaudRate::B0, + 50 => BaudRate::B50, + 75 => BaudRate::B75, + 110 => BaudRate::B110, + 134 => BaudRate::B134, + 150 => BaudRate::B150, + 200 => BaudRate::B200, + 300 => BaudRate::B300, + 600 => BaudRate::B600, + 1200 => BaudRate::B1200, + 1800 => BaudRate::B1800, + 2400 => BaudRate::B2400, + 4800 => BaudRate::B4800, + 9600 => BaudRate::B9600, + 19200 => BaudRate::B19200, + 38400 => BaudRate::B38400, + 7200 => BaudRate::B7200, + 14400 => BaudRate::B14400, + 28800 => BaudRate::B28800, + 57600 => BaudRate::B57600, + 76800 => BaudRate::B76800, + 115200 => BaudRate::B115200, + 230400 => BaudRate::B230400, + #[cfg(any(target_os = "netbsd", target_os = "freebsd"))] + 460800 => BaudRate::B460800, + #[cfg(any(target_os = "netbsd", target_os = "freebsd"))] + 921600 => BaudRate::B921600, + b @ _ => unreachable!("Invalid baud constant: {}", b), + } + } + } + pub const VEOF: usize = 0; pub const VEOL: usize = 1; pub const VEOL2: usize = 2; @@ -293,6 +359,82 @@ mod ffi { pub c_ospeed: speed_t } + #[derive(Clone, Copy, Debug, PartialEq)] + pub enum BaudRate { + B0, + B50, + B75, + B110, + B130, + B150, + B200, + B300, + B600, + B1200, + B1800, + B2400, + B4800, + B9600, + B19200, + B38400, + BOTHER, + B57600, + B115200, + B230400, + B460800, + B500000, + B576000, + B921600, + B1000000, + B1152000, + B1500000, + B2000000, + B2500000, + B3000000, + B3500000, + B4000000, + } + + impl From for BaudRate { + fn from(s: speed_t) -> BaudRate { + match s { + 0 => BaudRate::B0, + 1 => BaudRate::B50, + 2 => BaudRate::B75, + 3 => BaudRate::B110, + 4 => BaudRate::B130, + 5 => BaudRate::B150, + 6 => BaudRate::B200, + 7 => BaudRate::B300, + 10 => BaudRate::B600, + 11 => BaudRate::B1200, + 12 => BaudRate::B1800, + 13 => BaudRate::B2400, + 14 => BaudRate::B4800, + 15 => BaudRate::B9600, + 16 => BaudRate::B19200, + 17 => BaudRate::B38400, + 10000 => BaudRate::BOTHER, + 10001 => BaudRate::B57600, + 10002 => BaudRate::B115200, + 10003 => BaudRate::B230400, + 10004 => BaudRate::B460800, + 10005 => BaudRate::B500000, + 10006 => BaudRate::B576000, + 10007 => BaudRate::B921600, + 10010 => BaudRate::B1000000, + 10011 => BaudRate::B1152000, + 10012 => BaudRate::B1500000, + 10013 => BaudRate::B2000000, + 10014 => BaudRate::B2500000, + 10015 => BaudRate::B3000000, + 10016 => BaudRate::B3500000, + 10017 => BaudRate::B4000000, + b @ _ => unreachable!("Invalid baud constant: {}", b), + } + } + } + pub const VEOF: usize = 4; pub const VEOL: usize = 11; pub const VEOL2: usize = 16; @@ -426,27 +568,27 @@ mod ffi { } } -pub fn cfgetispeed(termios: &Termios) -> speed_t { +pub fn cfgetispeed(termios: &Termios) -> BaudRate { unsafe { - ffi::cfgetispeed(termios) + ffi::cfgetispeed(termios).into() } } -pub fn cfgetospeed(termios: &Termios) -> speed_t { +pub fn cfgetospeed(termios: &Termios) -> BaudRate { unsafe { - ffi::cfgetospeed(termios) + ffi::cfgetospeed(termios).into() } } -pub fn cfsetispeed(termios: &mut Termios, speed: speed_t) -> Result<()> { +pub fn cfsetispeed(termios: &mut Termios, baud: BaudRate) -> Result<()> { Errno::result(unsafe { - ffi::cfsetispeed(termios, speed) + ffi::cfsetispeed(termios, baud as speed_t) }).map(drop) } -pub fn cfsetospeed(termios: &mut Termios, speed: speed_t) -> Result<()> { +pub fn cfsetospeed(termios: &mut Termios, baud: BaudRate) -> Result<()> { Errno::result(unsafe { - ffi::cfsetospeed(termios, speed) + ffi::cfsetospeed(termios, baud as speed_t) }).map(drop) } -- cgit v1.2.3 From a2172120ff378a62e335ababcd32904cb1fcdaef Mon Sep 17 00:00:00 2001 From: Zac Berkowitz Date: Sun, 19 Feb 2017 12:10:01 +0000 Subject: Corrected linux baud constants to octals --- src/sys/termios.rs | 64 +++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/sys/termios.rs b/src/sys/termios.rs index c45627b3..29ed513c 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -398,38 +398,38 @@ mod ffi { impl From for BaudRate { fn from(s: speed_t) -> BaudRate { match s { - 0 => BaudRate::B0, - 1 => BaudRate::B50, - 2 => BaudRate::B75, - 3 => BaudRate::B110, - 4 => BaudRate::B130, - 5 => BaudRate::B150, - 6 => BaudRate::B200, - 7 => BaudRate::B300, - 10 => BaudRate::B600, - 11 => BaudRate::B1200, - 12 => BaudRate::B1800, - 13 => BaudRate::B2400, - 14 => BaudRate::B4800, - 15 => BaudRate::B9600, - 16 => BaudRate::B19200, - 17 => BaudRate::B38400, - 10000 => BaudRate::BOTHER, - 10001 => BaudRate::B57600, - 10002 => BaudRate::B115200, - 10003 => BaudRate::B230400, - 10004 => BaudRate::B460800, - 10005 => BaudRate::B500000, - 10006 => BaudRate::B576000, - 10007 => BaudRate::B921600, - 10010 => BaudRate::B1000000, - 10011 => BaudRate::B1152000, - 10012 => BaudRate::B1500000, - 10013 => BaudRate::B2000000, - 10014 => BaudRate::B2500000, - 10015 => BaudRate::B3000000, - 10016 => BaudRate::B3500000, - 10017 => BaudRate::B4000000, + 0o0 => BaudRate::B0, + 0o1 => BaudRate::B50, + 0o2 => BaudRate::B75, + 0o3 => BaudRate::B110, + 0o4 => BaudRate::B130, + 0o5 => BaudRate::B150, + 0o6 => BaudRate::B200, + 0o7 => BaudRate::B300, + 0o10 => BaudRate::B600, + 0o11 => BaudRate::B1200, + 0o12 => BaudRate::B1800, + 0o13 => BaudRate::B2400, + 0o14 => BaudRate::B4800, + 0o15 => BaudRate::B9600, + 0o16 => BaudRate::B19200, + 0o17 => BaudRate::B38400, + 0o10000 => BaudRate::BOTHER, + 0o10001 => BaudRate::B57600, + 0o10002 => BaudRate::B115200, + 0o10003 => BaudRate::B230400, + 0o10004 => BaudRate::B460800, + 0o10005 => BaudRate::B500000, + 0o10006 => BaudRate::B576000, + 0o10007 => BaudRate::B921600, + 0o10010 => BaudRate::B1000000, + 0o10011 => BaudRate::B1152000, + 0o10012 => BaudRate::B1500000, + 0o10013 => BaudRate::B2000000, + 0o10014 => BaudRate::B2500000, + 0o10015 => BaudRate::B3000000, + 0o10016 => BaudRate::B3500000, + 0o10017 => BaudRate::B4000000, b @ _ => unreachable!("Invalid baud constant: {}", b), } } -- cgit v1.2.3 From 8b6242eb0d83d3902b11cea395536509f44f6559 Mon Sep 17 00:00:00 2001 From: Zac Berkowitz Date: Sat, 25 Feb 2017 01:33:02 +0000 Subject: Using baud constants from libc New baud constants landed in rust-lang/libc#530, we'll use them. --- src/sys/termios.rs | 137 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 59 deletions(-) diff --git a/src/sys/termios.rs b/src/sys/termios.rs index 29ed513c..e8df1ed9 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -158,34 +158,45 @@ mod ffi { impl From for BaudRate { fn from(s: speed_t) -> BaudRate { + + use libc::{ + B0, B50, B75, B110, B134, B150, + B200, B300, B600, B1200, B1800, B2400, + B4800, B9600, B19200, B38400, + B7200, B14400, B28800, B57600, + B76800, B115200, B230400}; + + #[cfg(any(target_os = "netbsd", target_os = "freebsd"))] + use libc::{B460800, B921600}; + match s { - 0 => BaudRate::B0, - 50 => BaudRate::B50, - 75 => BaudRate::B75, - 110 => BaudRate::B110, - 134 => BaudRate::B134, - 150 => BaudRate::B150, - 200 => BaudRate::B200, - 300 => BaudRate::B300, - 600 => BaudRate::B600, - 1200 => BaudRate::B1200, - 1800 => BaudRate::B1800, - 2400 => BaudRate::B2400, - 4800 => BaudRate::B4800, - 9600 => BaudRate::B9600, - 19200 => BaudRate::B19200, - 38400 => BaudRate::B38400, - 7200 => BaudRate::B7200, - 14400 => BaudRate::B14400, - 28800 => BaudRate::B28800, - 57600 => BaudRate::B57600, - 76800 => BaudRate::B76800, - 115200 => BaudRate::B115200, - 230400 => BaudRate::B230400, + B0 => BaudRate::B0, + B50 => BaudRate::B50, + B75 => BaudRate::B75, + B110 => BaudRate::B110, + B134 => BaudRate::B134, + B150 => BaudRate::B150, + B200 => BaudRate::B200, + B300 => BaudRate::B300, + B600 => BaudRate::B600, + B1200 => BaudRate::B1200, + B1800 => BaudRate::B1800, + B2400 => BaudRate::B2400, + B4800 => BaudRate::B4800, + B9600 => BaudRate::B9600, + B19200 => BaudRate::B19200, + B38400 => BaudRate::B38400, + B7200 => BaudRate::B7200, + B14400 => BaudRate::B14400, + B28800 => BaudRate::B28800, + B57600 => BaudRate::B57600, + B76800 => BaudRate::B76800, + B115200 => BaudRate::B115200, + B230400 => BaudRate::B230400, #[cfg(any(target_os = "netbsd", target_os = "freebsd"))] - 460800 => BaudRate::B460800, + B460800 => BaudRate::B460800, #[cfg(any(target_os = "netbsd", target_os = "freebsd"))] - 921600 => BaudRate::B921600, + B921600 => BaudRate::B921600, b @ _ => unreachable!("Invalid baud constant: {}", b), } } @@ -365,7 +376,7 @@ mod ffi { B50, B75, B110, - B130, + B134, B150, B200, B300, @@ -377,7 +388,6 @@ mod ffi { B9600, B19200, B38400, - BOTHER, B57600, B115200, B230400, @@ -397,39 +407,48 @@ mod ffi { impl From for BaudRate { fn from(s: speed_t) -> BaudRate { + + use libc::{ + B0, B50, B75, B110, B134, B150, + B200, B300, B600, B1200, B1800, B2400, + B4800, B9600, B19200, B38400, B57600, + B115200, B230400, B460800, B500000, + B576000, B921600, B1000000, B1152000, + B1500000, B2000000, B2500000, B3000000, + B3500000, B4000000}; + match s { - 0o0 => BaudRate::B0, - 0o1 => BaudRate::B50, - 0o2 => BaudRate::B75, - 0o3 => BaudRate::B110, - 0o4 => BaudRate::B130, - 0o5 => BaudRate::B150, - 0o6 => BaudRate::B200, - 0o7 => BaudRate::B300, - 0o10 => BaudRate::B600, - 0o11 => BaudRate::B1200, - 0o12 => BaudRate::B1800, - 0o13 => BaudRate::B2400, - 0o14 => BaudRate::B4800, - 0o15 => BaudRate::B9600, - 0o16 => BaudRate::B19200, - 0o17 => BaudRate::B38400, - 0o10000 => BaudRate::BOTHER, - 0o10001 => BaudRate::B57600, - 0o10002 => BaudRate::B115200, - 0o10003 => BaudRate::B230400, - 0o10004 => BaudRate::B460800, - 0o10005 => BaudRate::B500000, - 0o10006 => BaudRate::B576000, - 0o10007 => BaudRate::B921600, - 0o10010 => BaudRate::B1000000, - 0o10011 => BaudRate::B1152000, - 0o10012 => BaudRate::B1500000, - 0o10013 => BaudRate::B2000000, - 0o10014 => BaudRate::B2500000, - 0o10015 => BaudRate::B3000000, - 0o10016 => BaudRate::B3500000, - 0o10017 => BaudRate::B4000000, + B0 => BaudRate::B0, + B50 => BaudRate::B50, + B75 => BaudRate::B75, + B110 => BaudRate::B110, + B134 => BaudRate::B134, + B150 => BaudRate::B150, + B200 => BaudRate::B200, + B300 => BaudRate::B300, + B600 => BaudRate::B600, + B1200 => BaudRate::B1200, + B1800 => BaudRate::B1800, + B2400 => BaudRate::B2400, + B4800 => BaudRate::B4800, + B9600 => BaudRate::B9600, + B19200 => BaudRate::B19200, + B38400 => BaudRate::B38400, + B57600 => BaudRate::B57600, + B115200 => BaudRate::B115200, + B230400 => BaudRate::B230400, + B460800 => BaudRate::B460800, + B500000 => BaudRate::B500000, + B576000 => BaudRate::B576000, + B921600 => BaudRate::B921600, + B1000000 => BaudRate::B1000000, + B1152000 => BaudRate::B1152000, + B1500000 => BaudRate::B1500000, + B2000000 => BaudRate::B2000000, + B2500000 => BaudRate::B2500000, + B3000000 => BaudRate::B3000000, + B3500000 => BaudRate::B3500000, + B4000000 => BaudRate::B4000000, b @ _ => unreachable!("Invalid baud constant: {}", b), } } -- cgit v1.2.3