2018-11-02 07:27:48 +00:00
|
|
|
// Copyright 2018 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
use std;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use bindings;
|
|
|
|
|
|
|
|
/// Error type for libusb.
|
|
|
|
pub enum Error {
|
|
|
|
Success(i32),
|
|
|
|
IO,
|
|
|
|
InvalidParam,
|
|
|
|
Access,
|
|
|
|
NoDevice,
|
|
|
|
NotFound,
|
|
|
|
Busy,
|
|
|
|
Timeout,
|
|
|
|
Overflow,
|
|
|
|
Pipe,
|
|
|
|
Interrupted,
|
|
|
|
NoMem,
|
|
|
|
NotSupported,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
lint: Resolve the easier clippy lints
Hopefully the changes are self-explanatory and uncontroversial. This
eliminates much of the noise from `cargo clippy` and, for my purposes,
gives me a reasonable way to use it as a tool when writing and reviewing
code.
Here is the Clippy invocation I was using:
cargo +nightly clippy -- -W clippy::correctness -A renamed_and_removed_lints -Aclippy::{blacklisted_name,borrowed_box,cast_lossless,cast_ptr_alignment,enum_variant_names,identity_op,if_same_then_else,mut_from_ref,needless_pass_by_value,new_without_default,new_without_default_derive,or_fun_call,ptr_arg,should_implement_trait,single_match,too_many_arguments,trivially_copy_pass_by_ref,unreadable_literal,unsafe_vector_initialization,useless_transmute}
TEST=cargo check --features wl-dmabuf,gpu,usb-emulation
TEST=boot linux
Change-Id: I55eb1b4a72beb2f762480e3333a921909314a0a2
Reviewed-on: https://chromium-review.googlesource.com/1356911
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2018-12-02 01:49:30 +00:00
|
|
|
match *self {
|
|
|
|
Error::Success(_v) => write!(f, "Success (no error)"),
|
|
|
|
Error::IO => write!(f, "Input/output error"),
|
|
|
|
Error::InvalidParam => write!(f, "Invalid parameter"),
|
|
|
|
Error::Access => write!(f, "Access denied (insufficient permissions)"),
|
|
|
|
Error::NoDevice => write!(f, "No such device (it may have been disconnected)"),
|
|
|
|
Error::NotFound => write!(f, "Entity not found"),
|
|
|
|
Error::Busy => write!(f, "Resource busy"),
|
|
|
|
Error::Timeout => write!(f, "Operation timed out"),
|
|
|
|
Error::Overflow => write!(f, "Overflow"),
|
|
|
|
Error::Pipe => write!(f, "Pipe error"),
|
|
|
|
Error::Interrupted => write!(f, "System call interrupted (perhaps due to signal)"),
|
|
|
|
Error::NoMem => write!(f, "Insufficient memory"),
|
|
|
|
Error::NotSupported => write!(
|
2018-11-02 07:27:48 +00:00
|
|
|
f,
|
|
|
|
"Operation not supported or unimplemented on this platform"
|
|
|
|
),
|
lint: Resolve the easier clippy lints
Hopefully the changes are self-explanatory and uncontroversial. This
eliminates much of the noise from `cargo clippy` and, for my purposes,
gives me a reasonable way to use it as a tool when writing and reviewing
code.
Here is the Clippy invocation I was using:
cargo +nightly clippy -- -W clippy::correctness -A renamed_and_removed_lints -Aclippy::{blacklisted_name,borrowed_box,cast_lossless,cast_ptr_alignment,enum_variant_names,identity_op,if_same_then_else,mut_from_ref,needless_pass_by_value,new_without_default,new_without_default_derive,or_fun_call,ptr_arg,should_implement_trait,single_match,too_many_arguments,trivially_copy_pass_by_ref,unreadable_literal,unsafe_vector_initialization,useless_transmute}
TEST=cargo check --features wl-dmabuf,gpu,usb-emulation
TEST=boot linux
Change-Id: I55eb1b4a72beb2f762480e3333a921909314a0a2
Reviewed-on: https://chromium-review.googlesource.com/1356911
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2018-12-02 01:49:30 +00:00
|
|
|
Error::Other => write!(f, "Other error"),
|
2018-11-02 07:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bindings::libusb_error> for Error {
|
|
|
|
fn from(e: bindings::libusb_error) -> Self {
|
|
|
|
match e {
|
|
|
|
bindings::LIBUSB_ERROR_IO => Error::IO,
|
|
|
|
bindings::LIBUSB_ERROR_INVALID_PARAM => Error::InvalidParam,
|
|
|
|
bindings::LIBUSB_ERROR_ACCESS => Error::Access,
|
|
|
|
bindings::LIBUSB_ERROR_NO_DEVICE => Error::NoDevice,
|
|
|
|
bindings::LIBUSB_ERROR_NOT_FOUND => Error::NotFound,
|
|
|
|
bindings::LIBUSB_ERROR_BUSY => Error::Busy,
|
|
|
|
bindings::LIBUSB_ERROR_TIMEOUT => Error::Timeout,
|
|
|
|
bindings::LIBUSB_ERROR_OVERFLOW => Error::Overflow,
|
|
|
|
bindings::LIBUSB_ERROR_PIPE => Error::Pipe,
|
|
|
|
bindings::LIBUSB_ERROR_INTERRUPTED => Error::Interrupted,
|
|
|
|
bindings::LIBUSB_ERROR_NO_MEM => Error::NoMem,
|
|
|
|
bindings::LIBUSB_ERROR_NOT_SUPPORTED => Error::NotSupported,
|
|
|
|
bindings::LIBUSB_ERROR_OTHER => Error::Other,
|
|
|
|
// All possible errors are defined above, other values mean success,
|
|
|
|
// see libusb_get_device_list for example.
|
|
|
|
_ => Error::Success(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! try_libusb {
|
|
|
|
($x:expr) => {
|
|
|
|
match Error::from($x as i32) {
|
|
|
|
Error::Success(e) => e,
|
|
|
|
err => return Err(err),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|