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
use crate::{error, UsbContext};
use libusb1_sys::{constants::*, libusb_set_option};

/// A `libusb` runtime option that can be enabled for a context.
pub struct UsbOption {
    inner: OptionInner,
}

impl UsbOption {
    /// Use the [UsbDk] backend if available.
    ///
    /// **Note**: This method is available on **Windows** only!
    ///
    /// [UsbDk]: https://github.com/daynix/UsbDk
    #[cfg(windows)]
    pub fn use_usbdk() -> Self {
        Self {
            inner: OptionInner::UseUsbdk,
        }
    }

    pub(crate) fn apply<T: UsbContext>(&self, ctx: &mut T) -> crate::Result<()> {
        match self.inner {
            OptionInner::UseUsbdk => {
                let err = unsafe { libusb_set_option(ctx.as_raw(), LIBUSB_OPTION_USE_USBDK) };
                if err == LIBUSB_SUCCESS {
                    Ok(())
                } else {
                    Err(error::from_libusb(err))
                }
            }
        }
    }
}

enum OptionInner {
    #[cfg_attr(not(windows), allow(dead_code))] // only constructed on Windows
    UseUsbdk,
}

/// Disable device scanning in `libusb` init.
///
/// Hotplug functionality will also be deactivated.
///
/// This is a Linux only option and it must be set before any [`Context`]
/// creation.
///
/// The option is useful in combination with [`Context::open_device_with_fd()`],
/// which can access a device directly without prior device scanning.
#[cfg(unix)]
pub fn disable_device_discovery() -> crate::Result<()> {
    try_unsafe!(libusb1_sys::libusb_set_option(
        std::ptr::null_mut(),
        LIBUSB_OPTION_NO_DEVICE_DISCOVERY
    ));
    Ok(())
}