-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from budde25/udev-refactor
Udev refactor
- Loading branch information
Showing
34 changed files
with
855 additions
and
664 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,46 @@ | ||
use libusbk::{DeviceHandle, DeviceList}; | ||
use libusbk::{Device, DeviceHandle, DeviceList}; | ||
|
||
use super::{Device, SWITCH_PID, SWITCH_VID}; | ||
use super::{RCM_PID, RCM_VID}; | ||
use crate::Result; | ||
|
||
/// A connected and init switch device connection | ||
#[derive(Debug)] | ||
#[derive(Debug, Clone)] | ||
pub struct SwitchDevice { | ||
device: DeviceHandle, | ||
pub(crate) device: Device, | ||
} | ||
|
||
/// A connected and init switch device connection | ||
#[derive(Debug)] | ||
pub struct SwitchHandle { | ||
pub(crate) handle: DeviceHandle, | ||
} | ||
|
||
impl Device for SwitchDevice { | ||
impl super::Device for SwitchDevice { | ||
/// Tries to connect to the device and open and interface | ||
fn find_device() -> Result<Option<Self>> { | ||
fn find_device() -> Result<Self> { | ||
let devices = DeviceList::new()?; | ||
let device = devices.find_with_vid_and_pid(SWITCH_PID as i32, SWITCH_VID as i32); | ||
if let Ok(device) = device { | ||
let handle = device.open()?; | ||
return Ok(Some(Self::with_device_handle(handle))); | ||
} | ||
|
||
// We did not find the device | ||
Ok(None) | ||
let device = devices.find_with_vid_and_pid(RCM_PID as i32, RCM_VID as i32)?; | ||
Ok(Self { device }) | ||
} | ||
|
||
/// Init the device | ||
fn init(&mut self) -> Result<()> { | ||
// stub | ||
Ok(()) | ||
fn init(&mut self) -> Result<SwitchHandle> { | ||
Ok(SwitchHandle { | ||
handle: self.device.open()?, | ||
}) | ||
} | ||
} | ||
|
||
impl super::DeviceHandle for SwitchHandle { | ||
/// Read from the device into the buffer | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { | ||
let amount = self.device.read_pipe(0x81, buf)?; | ||
let amount = self.handle.read_pipe(0x81, buf)?; | ||
Ok(amount as usize) | ||
} | ||
|
||
/// Write to the device from the buffer | ||
fn write(&mut self, buf: &[u8]) -> Result<usize> { | ||
let amount = self.device.write_pipe(0x01, buf)?; | ||
let amount = self.handle.write_pipe(0x01, buf)?; | ||
Ok(amount as usize) | ||
} | ||
} | ||
|
||
impl SwitchDevice { | ||
pub fn with_device_handle(device: DeviceHandle) -> Self { | ||
Self { device } | ||
} | ||
|
||
pub fn device(&self) -> &DeviceHandle { | ||
&self.device | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,43 @@ | ||
use std::sync::mpsc::Sender; | ||
|
||
use thiserror::Error; | ||
|
||
use crate::error::Result; | ||
use crate::Switch; | ||
|
||
#[cfg(all(feature = "notify", target_os = "linux"))] | ||
mod notify; | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(any(target_os = "macos", target_os = "linux"))] { | ||
mod unix; | ||
pub use unix::create_hotplug; | ||
} else if #[cfg(target_os = "windows")] { | ||
#[cfg(target_os = "windows")] | ||
mod windows; | ||
pub use windows::create_hotplug; | ||
} else { | ||
compile_error!("Unsupported OS"); | ||
} | ||
} | ||
|
||
use crate::Switch; | ||
|
||
/// Defines the two actions for when a device is plugged in or removed | ||
pub trait Actions { | ||
/// A switch device has a arrived | ||
fn arrives(&mut self, switch: Switch); | ||
fn arrives(&mut self, switch: Result<Switch>); | ||
/// A switch device has left | ||
fn leaves(&mut self); | ||
} | ||
|
||
struct HotplugHandler { | ||
inner: Box<dyn Actions>, | ||
sender: Sender<Result<Switch>>, | ||
callback: Option<Box<dyn Fn() + Send + Sync>>, | ||
} | ||
unsafe impl Send for HotplugHandler {} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum HotplugError { | ||
#[error("The hotplug API is not supported on this platform")] | ||
NotSupported, | ||
|
||
#[error("A file watcher error occurred")] | ||
Watcher, | ||
} |
Oops, something went wrong.