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
//! This crate provides a safe wrapper around the native `libusb` library.

pub use libusb1_sys as ffi;
pub use libusb1_sys::constants;

pub use crate::{
    config_descriptor::{ConfigDescriptor, Interfaces},
    context::{Context, GlobalContext, LogLevel, UsbContext},
    device::Device,
    device_descriptor::DeviceDescriptor,
    device_handle::DeviceHandle,
    device_list::{DeviceList, Devices},
    endpoint_descriptor::EndpointDescriptor,
    error::{Error, Result},
    fields::{
        request_type, Direction, Recipient, RequestType, Speed, SyncType, TransferType, UsageType,
        Version,
    },
    hotplug::{Hotplug, HotplugBuilder, Registration},
    interface_descriptor::{
        EndpointDescriptors, Interface, InterfaceDescriptor, InterfaceDescriptors,
    },
    language::{Language, PrimaryLanguage, SubLanguage},
    options::UsbOption,
    version::{version, LibraryVersion},
};

#[cfg(test)]
#[macro_use]
mod test_helpers;

#[macro_use]
mod error;
mod version;

mod context;
mod device;
mod device_handle;
mod device_list;

mod config_descriptor;
mod device_descriptor;
mod endpoint_descriptor;
mod fields;
mod hotplug;
mod interface_descriptor;
mod language;
mod options;

/// Tests whether the running `libusb` library supports capability API.
pub fn has_capability() -> bool {
    GlobalContext::default().as_raw();
    unsafe { libusb1_sys::libusb_has_capability(constants::LIBUSB_CAP_HAS_CAPABILITY) != 0 }
}

/// Tests whether the running `libusb` library supports hotplug.
pub fn has_hotplug() -> bool {
    GlobalContext::default().as_raw();
    unsafe { libusb1_sys::libusb_has_capability(constants::LIBUSB_CAP_HAS_HOTPLUG) != 0 }
}

/// Tests whether the running `libusb` library has HID access.
pub fn has_hid_access() -> bool {
    GlobalContext::default().as_raw();
    unsafe { libusb1_sys::libusb_has_capability(constants::LIBUSB_CAP_HAS_HID_ACCESS) != 0 }
}

/// Tests whether the running `libusb` library supports detaching the kernel driver.
pub fn supports_detach_kernel_driver() -> bool {
    GlobalContext::default().as_raw();
    unsafe {
        libusb1_sys::libusb_has_capability(constants::LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER) != 0
    }
}

/// Returns a list of the current USB devices. Using global context
pub fn devices() -> crate::Result<DeviceList<GlobalContext>> {
    GlobalContext::default().devices()
}

/// Sets the log level of a `libusb` global context.
pub fn set_log_level(level: LogLevel) {
    unsafe {
        libusb1_sys::libusb_set_debug(GlobalContext::default().as_raw(), level.as_c_int());
    }
}

/// Convenience function to open a device by its vendor ID and product ID.
/// Using global context
///
/// This function is provided as a convenience for building prototypes without having to
/// iterate a [`DeviceList`](struct.DeviceList.html). It is not meant for production
/// applications.
///
/// Returns a device handle for the first device found matching `vendor_id` and `product_id`.
/// On error, or if the device could not be found, it returns `None`.
pub fn open_device_with_vid_pid(
    vendor_id: u16,
    product_id: u16,
) -> Option<DeviceHandle<GlobalContext>> {
    let handle = unsafe {
        libusb1_sys::libusb_open_device_with_vid_pid(
            GlobalContext::default().as_raw(),
            vendor_id,
            product_id,
        )
    };

    if handle.is_null() {
        None
    } else {
        Some(unsafe {
            DeviceHandle::from_libusb(
                GlobalContext::default(),
                std::ptr::NonNull::new_unchecked(handle),
            )
        })
    }
}