Skip to content

Commit

Permalink
moves Unix implemention to serial-unix crate
Browse files Browse the repository at this point in the history
  • Loading branch information
dcuddeback committed Jul 2, 2017
1 parent 7da8fd2 commit c2cfb4f
Show file tree
Hide file tree
Showing 10 changed files with 712 additions and 755 deletions.
63 changes: 3 additions & 60 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,8 @@ readme = "README.md"
keywords = ["serial", "hardware", "system", "RS232"]

[dependencies]
libc = "0.2.1"
libc = "0.2"
serial-core = { path = "./serial-core" }

[target.aarch64-unknown-linux-gnu.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.arm-unknown-linux-gnueabi.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.arm-unknown-linux-gnueabihf.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.armv7-unknown-linux-gnueabihf.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.i686-apple-darwin.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.i686-unknown-linux-gnu.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.mips-unknown-linux-gnu.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.mipsel-unknown-linux-gnu.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.powerpc-unknown-linux-gnu.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.x86_64-apple-darwin.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.x86_64-unknown-dragonfly.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.x86_64-unknown-freebsd.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.x86_64-unknown-linux-gnu.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.x86_64-unknown-openbsd.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"

[target.x86_64-unknown-linux-musl.dependencies]
termios = "0.2.2"
ioctl-rs = "0.1.5"
[target.'cfg(unix)'.dependencies]
serial-unix = { path = "./serial-unix" }
2 changes: 1 addition & 1 deletion examples/open_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {

serial::open("/dev/ttyUSB0").unwrap();
serial::open(Path::new("/dev/ttyUSB0")).unwrap();
serial::posix::TTYPort::open(Path::new("/dev/ttyUSB0")).unwrap();
serial::unix::TTYPort::open(Path::new("/dev/ttyUSB0")).unwrap();
}

#[cfg(windows)]
Expand Down
10 changes: 10 additions & 0 deletions serial-unix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "serial-unix"
version = "0.4.0"
authors = ["David Cuddeback <[email protected]>"]

[dependencies]
serial-core = { path = "../serial-core" }
libc = "0.2.1"
termios = "0.2.2"
ioctl-rs = "0.1.5"
36 changes: 16 additions & 20 deletions src/posix/error.rs → serial-unix/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
extern crate libc;
use core;

use std::error::Error;
use std::ffi::CStr;
use std::io;
use std::str;

use self::libc::{c_int,c_char,size_t};
use libc::{c_int, c_char, size_t};

pub fn last_os_error() -> ::core::Error {
pub fn last_os_error() -> core::Error {
from_raw_os_error(errno())
}

pub fn from_raw_os_error(errno: i32) -> ::core::Error {
use self::libc::{EBUSY,EISDIR,ELOOP,ENOTDIR,ENOENT,ENODEV,ENXIO,EACCES,EINVAL,ENAMETOOLONG,EINTR,EWOULDBLOCK};
pub fn from_raw_os_error(errno: i32) -> core::Error {
use libc::{EBUSY, EISDIR, ELOOP, ENOTDIR, ENOENT, ENODEV, ENXIO, EACCES, EINVAL, ENAMETOOLONG, EINTR, EWOULDBLOCK};

let kind = match errno {
EBUSY | EISDIR | ELOOP | ENOTDIR | ENOENT | ENODEV | ENXIO | EACCES => ::core::ErrorKind::NoDevice,
EINVAL | ENAMETOOLONG => ::core::ErrorKind::InvalidInput,
EBUSY | EISDIR | ELOOP | ENOTDIR | ENOENT | ENODEV | ENXIO | EACCES => core::ErrorKind::NoDevice,
EINVAL | ENAMETOOLONG => core::ErrorKind::InvalidInput,

EINTR => ::core::ErrorKind::Io(io::ErrorKind::Interrupted),
EWOULDBLOCK => ::core::ErrorKind::Io(io::ErrorKind::WouldBlock),
_ => ::core::ErrorKind::Io(io::ErrorKind::Other)
EINTR => core::ErrorKind::Io(io::ErrorKind::Interrupted),
EWOULDBLOCK => core::ErrorKind::Io(io::ErrorKind::WouldBlock),
_ => core::ErrorKind::Io(io::ErrorKind::Other),
};

::core::Error::new(kind, error_string(errno))
core::Error::new(kind, error_string(errno))
}

pub fn from_io_error(io_error: io::Error) -> ::core::Error {
pub fn from_io_error(io_error: io::Error) -> core::Error {
match io_error.raw_os_error() {
Some(errno) => from_raw_os_error(errno),
None => {
let description = io_error.description().to_string();

::core::Error::new(::core::ErrorKind::Io(io_error.kind()), description)
core::Error::new(core::ErrorKind::Io(io_error.kind()), description)
}
}
}
Expand All @@ -42,9 +42,7 @@ pub fn from_io_error(io_error: io::Error) -> ::core::Error {
const TMPBUF_SZ: usize = 128;

pub fn errno() -> i32 {
#[cfg(any(target_os = "macos",
target_os = "ios",
target_os = "freebsd"))]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))]
unsafe fn errno_location() -> *const c_int {
extern { fn __error() -> *const c_int; }
__error()
Expand Down Expand Up @@ -87,13 +85,11 @@ pub fn error_string(errno: i32) -> String {
#[cfg(target_os = "linux")]
extern {
#[link_name = "__xpg_strerror_r"]
fn strerror_r(errnum: c_int, buf: *mut c_char,
buflen: size_t) -> c_int;
fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int;
}
#[cfg(not(target_os = "linux"))]
extern {
fn strerror_r(errnum: c_int, buf: *mut c_char,
buflen: size_t) -> c_int;
fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int;
}

let mut buf = [0 as c_char; TMPBUF_SZ];
Expand Down
12 changes: 12 additions & 0 deletions serial-unix/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Serial port implementation for Unix operating systems.
extern crate serial_core as core;
extern crate libc;
extern crate termios;
extern crate ioctl_rs as ioctl;

pub use tty::*;

mod error;
mod poll;
mod tty;
28 changes: 14 additions & 14 deletions src/posix/poll.rs → serial-unix/src/poll.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![allow(non_camel_case_types,dead_code)]

extern crate libc;
use libc;

use std::io;
use std::time::Duration;

use self::libc::{c_int,c_short};
use libc::{c_int, c_short};

#[cfg(target_os = "linux")]
type nfds_t = libc::c_ulong;
Expand All @@ -15,10 +15,10 @@ type nfds_t = libc::c_uint;

#[derive(Debug)]
#[repr(C)]
struct PollFd {
struct pollfd {
fd: c_int,
events: c_short,
revents: c_short
revents: c_short,
}

const POLLIN: c_short = 0x0001;
Expand All @@ -38,9 +38,9 @@ pub fn wait_write_fd(fd: c_int, timeout: Duration) -> io::Result<()> {
}

fn wait_fd(fd: c_int, events: c_short, timeout: Duration) -> io::Result<()> {
use self::libc::{EINTR,EPIPE,EIO};
use libc::{EINTR, EPIPE, EIO};

let mut fds = vec!(PollFd { fd: fd, events: events, revents: 0 });
let mut fds = vec!(pollfd { fd: fd, events: events, revents: 0 });

let wait = do_poll(&mut fds, timeout);

Expand All @@ -49,7 +49,7 @@ fn wait_fd(fd: c_int, events: c_short, timeout: Duration) -> io::Result<()> {

let kind = match errno {
EINTR => io::ErrorKind::Interrupted,
_ => io::ErrorKind::Other
_ => io::ErrorKind::Other,
};

return Err(io::Error::new(kind, super::error::error_string(errno)));
Expand All @@ -72,21 +72,21 @@ fn wait_fd(fd: c_int, events: c_short, timeout: Duration) -> io::Result<()> {

#[cfg(target_os = "linux")]
#[inline]
fn do_poll(fds: &mut Vec<PollFd>, timeout: Duration) -> c_int {
fn do_poll(fds: &mut Vec<pollfd>, timeout: Duration) -> c_int {
use std::ptr;

use self::libc::{c_void};
use libc::c_void;

#[repr(C)]
struct sigset_t {
__private: c_void
__private: c_void,
}

extern "C" {
fn ppoll(fds: *mut PollFd, nfds: nfds_t, timeout_ts: *mut self::libc::timespec, sigmask: *const sigset_t) -> c_int;
fn ppoll(fds: *mut pollfd, nfds: nfds_t, timeout_ts: *mut libc::timespec, sigmask: *const sigset_t) -> c_int;
}

let mut timeout_ts = self::libc::timespec {
let mut timeout_ts = libc::timespec {
tv_sec: timeout.as_secs() as libc::time_t,
tv_nsec: timeout.subsec_nanos() as libc::c_long,
};
Expand All @@ -101,9 +101,9 @@ fn do_poll(fds: &mut Vec<PollFd>, timeout: Duration) -> c_int {

#[cfg(not(target_os = "linux"))]
#[inline]
fn do_poll(fds: &mut Vec<PollFd>, timeout: Duration) -> c_int {
fn do_poll(fds: &mut Vec<pollfd>, timeout: Duration) -> c_int {
extern "C" {
fn poll(fds: *mut PollFd, nfds: nfds_t, timeout: c_int) -> c_int;
fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int;
}

let milliseconds = timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000;
Expand Down
Loading

0 comments on commit c2cfb4f

Please sign in to comment.