mirror of
https://github.com/G2-Games/minidisc-cli.git
synced 2025-04-19 11:42:53 -05:00
Ported to [yusb](https://github.com/fpagliughi/yusb)
This commit is contained in:
parent
c51d2b700a
commit
278ba2e8bd
3 changed files with 57 additions and 63 deletions
|
@ -20,10 +20,10 @@ once_cell = "1.18.0"
|
||||||
unicode-normalization = "0.1.22"
|
unicode-normalization = "0.1.22"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
des = "0.8.1"
|
des = "0.8.1"
|
||||||
webusb = "0.5.0"
|
|
||||||
unicode-jp = "0.4.0"
|
unicode-jp = "0.4.0"
|
||||||
regex = "1.10.2"
|
regex = "1.10.2"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
yusb = "0.1.2"
|
||||||
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
use nofmt;
|
use nofmt;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use webusb::{UsbDevice, UsbRecipient, UsbRequestType, UsbControlTransferParameters};
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use yusb::{Device, DeviceHandle, Direction, Recipient, RequestType};
|
||||||
|
|
||||||
const DEFAULT_TIMEOUT: std::time::Duration = std::time::Duration::new(9999999, 0);
|
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_WRITE_ENDPOINT: u8 = 0x02;
|
||||||
const BULK_READ_ENDPOINT: u8 = 0x81;
|
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(|| {
|
pub static DEVICE_IDS: Lazy<Box<[DeviceId]>> = Lazy::new(|| {
|
||||||
nofmt::pls! {Box::new(
|
nofmt::pls! {Box::new(
|
||||||
[
|
[
|
||||||
|
@ -65,6 +65,7 @@ pub static DEVICE_IDS: Lazy<Box<[DeviceId]>> = Lazy::new(|| {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/// The current status of the Minidisc device
|
||||||
pub enum Status {
|
pub enum Status {
|
||||||
Ready,
|
Ready,
|
||||||
Playing,
|
Playing,
|
||||||
|
@ -76,14 +77,16 @@ pub enum Status {
|
||||||
DiscBlank,
|
DiscBlank,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The ID of a device, including the name
|
||||||
pub struct DeviceId {
|
pub struct DeviceId {
|
||||||
vendor_id: u16,
|
vendor_id: u16,
|
||||||
product_id: u16,
|
product_id: u16,
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A connection to a NetMD device
|
||||||
pub struct NetMD {
|
pub struct NetMD {
|
||||||
device_connection: UsbDevice,
|
device: DeviceHandle,
|
||||||
model: DeviceId,
|
model: DeviceId,
|
||||||
status: Option<Status>,
|
status: Option<Status>,
|
||||||
}
|
}
|
||||||
|
@ -92,12 +95,12 @@ impl NetMD {
|
||||||
const READ_REPLY_RETRY_INTERVAL: u32 = 10;
|
const READ_REPLY_RETRY_INTERVAL: u32 = 10;
|
||||||
|
|
||||||
/// Creates a new `NetMD` struct
|
/// Creates a new `NetMD` struct
|
||||||
pub fn new(
|
pub fn new(device: Device) -> Result<Self, Box<dyn Error>> {
|
||||||
device: UsbDevice,
|
let descriptor = device.device_descriptor()?;
|
||||||
) -> Result<Self, Box<dyn Error>> {
|
|
||||||
let mut model = DeviceId {
|
let mut model = DeviceId {
|
||||||
vendor_id: device.vendor_id,
|
vendor_id: descriptor.vendor_id(),
|
||||||
product_id: device.product_id,
|
product_id: descriptor.product_id(),
|
||||||
name: None,
|
name: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -116,7 +119,7 @@ impl NetMD {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
device_connection: device,
|
device: device.open()?,
|
||||||
model,
|
model,
|
||||||
status: None,
|
status: None,
|
||||||
})
|
})
|
||||||
|
@ -140,18 +143,19 @@ impl NetMD {
|
||||||
/// Poll the device to get either the result
|
/// Poll the device to get either the result
|
||||||
/// of the previous command, or the status
|
/// of the previous command, or the status
|
||||||
pub fn poll(&mut self) -> Result<(u16, [u8; 4]), Box<dyn Error>> {
|
pub fn poll(&mut self) -> Result<(u16, [u8; 4]), Box<dyn Error>> {
|
||||||
let poll_result: [u8; 4] = match self.device_connection.control_transfer_in(
|
// Create an array to store the result of the poll
|
||||||
UsbControlTransferParameters {
|
let mut poll_result = [0u8; 4];
|
||||||
request_type: UsbRequestType::Vendor,
|
|
||||||
recipient: UsbRecipient::Interface,
|
let _status = match self.device.read_control(
|
||||||
request: 0x01,
|
STANDARD_RECV,
|
||||||
value: 0,
|
0x01,
|
||||||
index: 0,
|
0,
|
||||||
},
|
0,
|
||||||
4
|
&mut poll_result,
|
||||||
|
DEFAULT_TIMEOUT,
|
||||||
) {
|
) {
|
||||||
Ok(result) => result.try_into().unwrap(),
|
Ok(size) => size,
|
||||||
Err(error) => return Err(format!("USB error: {:?}", error).into()),
|
Err(error) => return Err(error.into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let length_bytes = [poll_result[2], poll_result[3]];
|
let length_bytes = [poll_result[2], poll_result[3]];
|
||||||
|
@ -172,7 +176,7 @@ impl NetMD {
|
||||||
command: Vec<u8>,
|
command: Vec<u8>,
|
||||||
use_factory_command: bool,
|
use_factory_command: bool,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
//First poll to ensure the device is ready
|
// First poll to ensure the device is ready
|
||||||
match self.poll() {
|
match self.poll() {
|
||||||
Ok(buffer) => match buffer.1[2] {
|
Ok(buffer) => match buffer.1[2] {
|
||||||
0 => 0,
|
0 => 0,
|
||||||
|
@ -186,18 +190,12 @@ impl NetMD {
|
||||||
true => 0xff,
|
true => 0xff,
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.device_connection.control_transfer_out(
|
match self
|
||||||
UsbControlTransferParameters {
|
.device
|
||||||
request_type: UsbRequestType::Vendor,
|
.write_control(STANDARD_SEND, request, 0, 0, &command, DEFAULT_TIMEOUT)
|
||||||
recipient: UsbRecipient::Interface,
|
{
|
||||||
request,
|
|
||||||
value: 0,
|
|
||||||
index: 0,
|
|
||||||
},
|
|
||||||
&command
|
|
||||||
) {
|
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(error) => Err(format!("USB error: {:?}", error).into()),
|
Err(error) => Err(error.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,7 +229,9 @@ impl NetMD {
|
||||||
current_attempt += 1;
|
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 {
|
let request = match use_factory_command {
|
||||||
false => 0x81,
|
false => 0x81,
|
||||||
|
@ -239,18 +239,15 @@ impl NetMD {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create a buffer to fill with the result
|
// Create a buffer to fill with the result
|
||||||
match self.device_connection.control_transfer_in(
|
let mut buf: Vec<u8> = vec![0; length as usize];
|
||||||
UsbControlTransferParameters {
|
|
||||||
request_type: UsbRequestType::Vendor,
|
// Create a buffer to fill with the result
|
||||||
recipient: UsbRecipient::Interface,
|
match self
|
||||||
request,
|
.device
|
||||||
value: 0,
|
.read_control(STANDARD_RECV, request, 0, 0, &mut buf, DEFAULT_TIMEOUT)
|
||||||
index: 0,
|
{
|
||||||
},
|
Ok(_) => Ok(buf),
|
||||||
length as usize
|
Err(error) => Err(error.into()),
|
||||||
) {
|
|
||||||
Ok(data) => Ok(data),
|
|
||||||
Err(error) => Err(format!("USB error: {:?}", error).into()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,13 +270,14 @@ impl NetMD {
|
||||||
while done < length {
|
while done < length {
|
||||||
let to_read = std::cmp::min(chunksize, length - done);
|
let to_read = std::cmp::min(chunksize, length - done);
|
||||||
done -= to_read;
|
done -= to_read;
|
||||||
|
let mut buffer: Vec<u8> = vec![0; to_read as usize];
|
||||||
|
|
||||||
let buffer = match self.device_connection.transfer_in(
|
match self
|
||||||
BULK_READ_ENDPOINT,
|
.device
|
||||||
to_read as usize,
|
.read_bulk(BULK_READ_ENDPOINT, &mut buffer, DEFAULT_TIMEOUT)
|
||||||
) {
|
{
|
||||||
Ok(result) => result,
|
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);
|
final_result.extend_from_slice(&buffer);
|
||||||
|
@ -289,13 +287,9 @@ impl NetMD {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_bulk(&mut self, data: &mut [u8]) -> Result<usize, Box<dyn Error>> {
|
pub fn write_bulk(&mut self, data: &mut [u8]) -> Result<usize, Box<dyn Error>> {
|
||||||
let written = match self.device_connection.transfer_out(
|
let written = self
|
||||||
BULK_WRITE_ENDPOINT,
|
.device
|
||||||
data
|
.write_bulk(BULK_WRITE_ENDPOINT, data, DEFAULT_TIMEOUT)?;
|
||||||
) {
|
|
||||||
Ok(output) => output,
|
|
||||||
Err(error) => return Err(format!("USB error: {:?}", error).into())
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(written)
|
Ok(written)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ use hex;
|
||||||
|
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use webusb;
|
use yusb;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
|
@ -204,7 +204,7 @@ impl NetMDInterface {
|
||||||
const INTERIM_RESPONSE_RETRY_INTERVAL: u32 = 100;
|
const INTERIM_RESPONSE_RETRY_INTERVAL: u32 = 100;
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
device: webusb::UsbDevice,
|
device: yusb::Device,
|
||||||
) -> Result<Self, Box<dyn Error>> {
|
) -> Result<Self, Box<dyn Error>> {
|
||||||
let net_md_device = base::NetMD::new(device).unwrap();
|
let net_md_device = base::NetMD::new(device).unwrap();
|
||||||
Ok(NetMDInterface { net_md_device })
|
Ok(NetMDInterface { net_md_device })
|
||||||
|
|
Loading…
Reference in a new issue