From 0b59de7109ba55f7449594a83ada60311aa565c4 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Sat, 23 Sep 2023 11:57:23 -0500 Subject: [PATCH] Implemented all functions in `base` --- src/netmd/base.rs | 36 +++++++++++++++++++++++++++++++++--- src/netmd/interface.rs | 4 ++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/netmd/base.rs b/src/netmd/base.rs index 9e42393..7eb1e79 100644 --- a/src/netmd/base.rs +++ b/src/netmd/base.rs @@ -10,6 +10,9 @@ const STANDARD_SEND: u8 = const STANDARD_RECV: u8 = rusb::request_type(Direction::In, RequestType::Vendor, Recipient::Interface); +const BULK_WRITE_ENDPOINT: u8 = 0x02; +const BULK_READ_ENDPOINT: u8 = 0x01; + pub const CHUNKSIZE: u32 = 0x10000; // TODO: I think this sucks, figure out a better way @@ -170,8 +173,16 @@ impl NetMD { Ok((u16::from_le_bytes(length_bytes), poll_result)) } + pub fn send_command(&self, command: Vec) -> Result<(), Box> { + self._send_command(command, false) + } + + pub fn send_factory_command(&self, command: Vec) -> Result<(), Box> { + self._send_command(command, false) + } + /// Send a control message to the device - pub fn send_command( + fn _send_command( &self, command: Vec, use_factory_command: bool, @@ -203,9 +214,17 @@ impl NetMD { } } + pub fn read_reply(&self) -> Result, Box> { + self._read_reply(false) + } + + pub fn read_factory_reply(&self) -> Result, Box> { + self._read_reply(true) + } + /// Poll to see if a message is ready, /// and if so, recieve it - pub fn read_reply(&self, use_factory_command: bool) -> Result, Box> { + fn _read_reply(&self, use_factory_command: bool) -> Result, Box> { let poll_result = match self.poll(30) { Ok(buffer) => buffer, Err(error) => return Err(error), @@ -233,6 +252,7 @@ impl NetMD { } // Default chunksize should be 0x10000 + // TODO: Make these Async eventually pub fn read_bulk(&self, length: u32, chunksize: u32) -> Result, Box> { let result = self.read_bulk_to_array(length, chunksize)?; @@ -248,7 +268,7 @@ impl NetMD { let mut buffer: Vec = vec![0; to_read as usize]; done += self.device_connection.read_bulk( - 1, + BULK_READ_ENDPOINT, &mut buffer, DEFAULT_TIMEOUT )? as u32; @@ -257,4 +277,14 @@ impl NetMD { Ok(final_result) } + + pub fn write_bulk(&self, data: &mut Vec) -> Result> { + let written = self.device_connection.read_bulk( + BULK_WRITE_ENDPOINT, + data, + DEFAULT_TIMEOUT + )?; + + Ok(written) + } } diff --git a/src/netmd/interface.rs b/src/netmd/interface.rs index bc2c944..498f24d 100644 --- a/src/netmd/interface.rs +++ b/src/netmd/interface.rs @@ -309,7 +309,7 @@ impl NetMDInterface { new_query.push(status_byte as u8); new_query.append(query); - self.net_md_device.send_command(new_query, false)?; + self.net_md_device.send_command(new_query)?; Ok(()) } @@ -319,7 +319,7 @@ impl NetMDInterface { let mut data; while current_attempt < Self::MAX_INTERIM_READ_ATTEMPTS { - data = match self.net_md_device.read_reply(false) { + data = match self.net_md_device.read_reply() { Ok(reply) => reply, Err(error) => return Err(error.into()), };