Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
G2-Games committed Dec 17, 2023
1 parent c51d2b7 commit 278ba2e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 63 deletions.
2 changes: 1 addition & 1 deletion minidisc-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ once_cell = "1.18.0"
unicode-normalization = "0.1.22"
hex = "0.4.3"
des = "0.8.1"
webusb = "0.5.0"
unicode-jp = "0.4.0"
regex = "1.10.2"
lazy_static = "1.4.0"
yusb = "0.1.2"


[lib]
Expand Down
114 changes: 54 additions & 60 deletions minidisc-rs/src/netmd/base.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use nofmt;
use once_cell::sync::Lazy;
use webusb::{UsbDevice, UsbRecipient, UsbRequestType, UsbControlTransferParameters};
use std::error::Error;
use yusb::{Device, DeviceHandle, Direction, Recipient, RequestType};

const DEFAULT_TIMEOUT: std::time::Duration = std::time::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;

pub const CHUNKSIZE: u32 = 0x10000;

// TODO: I think this sucks, figure out a better way
pub static DEVICE_IDS: Lazy<Box<[DeviceId]>> = Lazy::new(|| {
nofmt::pls! {Box::new(
[
Expand Down Expand Up @@ -65,6 +65,7 @@ pub static DEVICE_IDS: Lazy<Box<[DeviceId]>> = Lazy::new(|| {
}
});

/// The current status of the Minidisc device
pub enum Status {
Ready,
Playing,
Expand All @@ -76,14 +77,16 @@ pub enum Status {
DiscBlank,
}

/// The ID of a device, including the name
pub struct DeviceId {
vendor_id: u16,
product_id: u16,
name: Option<String>,
}

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

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

let mut model = DeviceId {
vendor_id: device.vendor_id,
product_id: device.product_id,
vendor_id: descriptor.vendor_id(),
product_id: descriptor.product_id(),
name: None,
};

Expand All @@ -116,7 +119,7 @@ impl NetMD {
}

Ok(Self {
device_connection: device,
device: device.open()?,
model,
status: None,
})
Expand All @@ -140,18 +143,19 @@ impl NetMD {
/// Poll the device to get either the result
/// of the previous command, or the status
pub fn poll(&mut self) -> Result<(u16, [u8; 4]), Box<dyn Error>> {
let poll_result: [u8; 4] = match self.device_connection.control_transfer_in(
UsbControlTransferParameters {
request_type: UsbRequestType::Vendor,
recipient: UsbRecipient::Interface,
request: 0x01,
value: 0,
index: 0,
},
4
// 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,
&mut poll_result,
DEFAULT_TIMEOUT,
) {
Ok(result) => result.try_into().unwrap(),
Err(error) => return Err(format!("USB error: {:?}", error).into()),
Ok(size) => size,
Err(error) => return Err(error.into()),
};

let length_bytes = [poll_result[2], poll_result[3]];
Expand All @@ -172,7 +176,7 @@ impl NetMD {
command: Vec<u8>,
use_factory_command: bool,
) -> Result<(), Box<dyn Error>> {
//First poll to ensure the device is ready
// First poll to ensure the device is ready
match self.poll() {
Ok(buffer) => match buffer.1[2] {
0 => 0,
Expand All @@ -186,18 +190,12 @@ impl NetMD {
true => 0xff,
};

match self.device_connection.control_transfer_out(
UsbControlTransferParameters {
request_type: UsbRequestType::Vendor,
recipient: UsbRecipient::Interface,
request,
value: 0,
index: 0,
},
&command
) {
match self
.device
.write_control(STANDARD_SEND, request, 0, 0, &command, DEFAULT_TIMEOUT)
{
Ok(_) => Ok(()),
Err(error) => Err(format!("USB error: {:?}", error).into()),
Err(error) => Err(error.into()),
}
}

Expand Down Expand Up @@ -231,26 +229,25 @@ impl NetMD {
current_attempt += 1;
}

if let Some(value) = override_length { length = value as u16 }
if let Some(value) = override_length {
length = value as u16
}

let request = match use_factory_command {
false => 0x81,
true => 0xff,
};

// Create a buffer to fill with the result
match self.device_connection.control_transfer_in(
UsbControlTransferParameters {
request_type: UsbRequestType::Vendor,
recipient: UsbRecipient::Interface,
request,
value: 0,
index: 0,
},
length as usize
) {
Ok(data) => Ok(data),
Err(error) => Err(format!("USB error: {:?}", error).into()),
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)
{
Ok(_) => Ok(buf),
Err(error) => Err(error.into()),
}
}

Expand All @@ -273,13 +270,14 @@ impl NetMD {
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 = match self.device_connection.transfer_in(
BULK_READ_ENDPOINT,
to_read as usize,
) {
match self
.device
.read_bulk(BULK_READ_ENDPOINT, &mut buffer, DEFAULT_TIMEOUT)
{
Ok(result) => result,
Err(error) => return Err(format!("USB error: {:?}", error).into())
Err(error) => return Err(format!("USB error: {:?}", error).into()),
};

final_result.extend_from_slice(&buffer);
Expand All @@ -289,13 +287,9 @@ impl NetMD {
}

pub fn write_bulk(&mut self, data: &mut [u8]) -> Result<usize, Box<dyn Error>> {
let written = match self.device_connection.transfer_out(
BULK_WRITE_ENDPOINT,
data
) {
Ok(output) => output,
Err(error) => return Err(format!("USB error: {:?}", error).into())
};
let written = self
.device
.write_bulk(BULK_WRITE_ENDPOINT, data, DEFAULT_TIMEOUT)?;

Ok(written)
}
Expand Down
4 changes: 2 additions & 2 deletions minidisc-rs/src/netmd/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use hex;

use std::thread::sleep;
use std::time::Duration;
use webusb;
use yusb;
use lazy_static::lazy_static;

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -204,7 +204,7 @@ impl NetMDInterface {
const INTERIM_RESPONSE_RETRY_INTERVAL: u32 = 100;

pub fn new(
device: webusb::UsbDevice,
device: yusb::Device,
) -> Result<Self, Box<dyn Error>> {
let net_md_device = base::NetMD::new(device).unwrap();
Ok(NetMDInterface { net_md_device })
Expand Down

0 comments on commit 278ba2e

Please sign in to comment.