Skip to content

Commit

Permalink
Merge pull request #2 from G2-Games/nusb
Browse files Browse the repository at this point in the history
Moved code to `nusb`, ran `rustfmt`
  • Loading branch information
G2-Games committed Jan 29, 2024
2 parents 5e22607 + 3030695 commit 1f74050
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 189 deletions.
3 changes: 2 additions & 1 deletion minidisc-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ des = "0.8.1"
unicode-jp = "0.4.0"
regex = "1.10.2"
lazy_static = "1.4.0"
yusb = "0.1.2"
nusb = "0.1.4"
futures-lite = "2.2.0"


[lib]
Expand Down
105 changes: 65 additions & 40 deletions minidisc-rs/src/netmd/base.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use nofmt;
use once_cell::sync::Lazy;
use std::error::Error;
use yusb::{Device, DeviceHandle, Direction, Recipient, RequestType};
use std::time::Duration;

const DEFAULT_TIMEOUT: std::time::Duration = std::time::Duration::new(9999999, 0);
// USB stuff
use nusb::transfer::{Control, ControlType, Recipient, RequestBuffer};
use nusb::{Device, DeviceInfo, Interface};

use futures_lite::future::block_on;

const DEFAULT_TIMEOUT: Duration = Duration::new(9999999, 0);

const STANDARD_SEND: u8 =
yusb::request_type(Direction::Out, RequestType::Vendor, Recipient::Interface);
const STANDARD_RECV: u8 =
yusb::request_type(Direction::In, RequestType::Vendor, Recipient::Interface);
const BULK_WRITE_ENDPOINT: u8 = 0x02;
const BULK_READ_ENDPOINT: u8 = 0x81;

Expand Down Expand Up @@ -86,7 +88,8 @@ pub struct DeviceId {

/// A connection to a NetMD device
pub struct NetMD {
device: DeviceHandle,
usb_device: Device,
usb_interface: Interface,
model: DeviceId,
status: Option<Status>,
}
Expand All @@ -95,12 +98,10 @@ impl NetMD {
const READ_REPLY_RETRY_INTERVAL: u32 = 10;

/// Creates a new interface to a NetMD device
pub fn new(device: Device) -> Result<Self, Box<dyn Error>> {
let descriptor = device.device_descriptor()?;

pub fn new(device_info: DeviceInfo) -> Result<Self, Box<dyn Error>> {
let mut model = DeviceId {
vendor_id: descriptor.vendor_id(),
product_id: descriptor.product_id(),
vendor_id: device_info.vendor_id(),
product_id: device_info.product_id(),
name: None,
};

Expand All @@ -118,8 +119,12 @@ impl NetMD {
Some(_) => (),
}

let usb_device = device_info.open()?;
let usb_interface = usb_device.claim_interface(0)?;

Ok(Self {
device: device.open()?,
usb_device,
usb_interface,
model,
status: None,
})
Expand All @@ -146,11 +151,14 @@ impl NetMD {
// Create an array to store the result of the poll
let mut poll_result = [0u8; 4];

let _status = match self.device.read_control(
STANDARD_RECV,
0x01,
0,
0,
let _status = match self.usb_interface.control_in_blocking(
Control {
control_type: ControlType::Vendor,
recipient: Recipient::Interface,
request: 0x01,
value: 0,
index: 0,
},
&mut poll_result,
DEFAULT_TIMEOUT,
) {
Expand Down Expand Up @@ -190,10 +198,17 @@ impl NetMD {
true => 0xff,
};

match self
.device
.write_control(STANDARD_SEND, request, 0, 0, &command, DEFAULT_TIMEOUT)
{
match self.usb_interface.control_out_blocking(
Control {
control_type: ControlType::Vendor,
recipient: Recipient::Interface,
request,
value: 0,
index: 0,
},
&command,
DEFAULT_TIMEOUT,
) {
Ok(_) => Ok(()),
Err(error) => Err(error.into()),
}
Expand Down Expand Up @@ -242,55 +257,65 @@ impl NetMD {
let mut buf: Vec<u8> = vec![0; length as usize];

// Create a buffer to fill with the result
match self
.device
.read_control(STANDARD_RECV, request, 0, 0, &mut buf, DEFAULT_TIMEOUT)
{
match self.usb_interface.control_in_blocking(
Control {
control_type: ControlType::Vendor,
recipient: Recipient::Interface,
request,
value: 0,
index: 0,
},
&mut buf,
DEFAULT_TIMEOUT,
) {
Ok(_) => Ok(buf),
Err(error) => Err(error.into()),
}
}

// Default chunksize should be 0x10000
// TODO: Make these Async eventually
pub fn read_bulk(&mut self, length: u32, chunksize: u32) -> Result<Vec<u8>, Box<dyn Error>> {
pub fn read_bulk(
&mut self,
length: usize,
chunksize: usize,
) -> Result<Vec<u8>, Box<dyn Error>> {
let result = self.read_bulk_to_array(length, chunksize)?;

Ok(result)
}

pub fn read_bulk_to_array(
&mut self,
length: u32,
chunksize: u32,
length: usize,
chunksize: usize,
) -> Result<Vec<u8>, Box<dyn Error>> {
let mut final_result: Vec<u8> = Vec::new();
let mut done = 0;

while done < length {
let to_read = std::cmp::min(chunksize, length - done);
done -= to_read;
let mut buffer: Vec<u8> = vec![0; to_read as usize];
let buffer = RequestBuffer::new(to_read);

match self
.device
.read_bulk(BULK_READ_ENDPOINT, &mut buffer, DEFAULT_TIMEOUT)
let res = match block_on(self.usb_interface.bulk_in(BULK_READ_ENDPOINT, buffer))
.into_result()
{
Ok(result) => result,
Err(error) => return Err(format!("USB error: {:?}", error).into()),
};

final_result.extend_from_slice(&buffer);
final_result.extend_from_slice(&res);
}

Ok(final_result)
}

pub fn write_bulk(&mut self, data: &mut [u8]) -> Result<usize, Box<dyn Error>> {
let written = self
.device
.write_bulk(BULK_WRITE_ENDPOINT, data, DEFAULT_TIMEOUT)?;

Ok(written)
pub fn write_bulk(&mut self, data: Vec<u8>) -> Result<usize, Box<dyn Error>> {
Ok(
block_on(self.usb_interface.bulk_out(BULK_WRITE_ENDPOINT, data))
.into_result()?
.actual_length(),
)
}
}
Loading

0 comments on commit 1f74050

Please sign in to comment.