use super::*;
use crate::{try_d3xx, D3xxError};
#[cfg(windows)]
pub(crate) fn write_pipe(handle: FT_HANDLE, pipe: u8, buf: &[u8]) -> Result<usize> {
let mut bytes_written: ULONG = 0;
try_d3xx!(unsafe {
FT_WritePipe(
handle,
pipe,
buf.as_ptr().cast_mut(),
ULONG::try_from(buf.len()).expect("buffer length exceeds ULONG::MAX"),
std::ptr::addr_of_mut!(bytes_written),
std::ptr::null_mut(),
)
})?;
Ok(bytes_written as usize)
}
#[cfg(not(windows))]
pub(crate) fn write_pipe(handle: FT_HANDLE, pipe: u8, buf: &[u8]) -> Result<usize> {
let mut bytes_written: ULONG = 0;
try_d3xx!(unsafe {
FT_WritePipe(
handle,
pipe,
buf.as_ptr().cast_mut(),
ULONG::try_from(buf.len()).expect("buffer length exceeds ULONG::MAX"),
std::ptr::addr_of_mut!(bytes_written),
0,
)
})?;
Ok(bytes_written as usize)
}
#[cfg(windows)]
pub(crate) fn write_pipe_async(
handle: FT_HANDLE,
pipe: u8,
buf: &[u8],
overlapped: &mut _OVERLAPPED,
) -> Result<()> {
let mut bytes_written: ULONG = 0;
ignore_io_pending(try_d3xx!(unsafe {
FT_WritePipe(
handle,
pipe,
buf.as_ptr().cast_mut(),
ULONG::try_from(buf.len()).expect("buffer length exceeds ULONG::MAX"),
std::ptr::addr_of_mut!(bytes_written),
overlapped as *mut _OVERLAPPED,
)
}))
}
#[cfg(not(windows))]
pub fn write_pipe_async(
handle: FT_HANDLE,
pipe: u8,
buf: &[u8],
overlapped: &mut _OVERLAPPED,
) -> Result<()> {
let mut bytes_written: ULONG = 0;
ignore_io_pending(try_d3xx!(unsafe {
FT_WritePipeAsync(
handle,
pipe as u8,
buf.as_ptr().cast_mut(),
ULONG::try_from(buf.len()).expect("buffer length exceeds ULONG::MAX"),
std::ptr::addr_of_mut!(bytes_written),
overlapped as *mut _OVERLAPPED,
)
}))
}
#[cfg(windows)]
pub(crate) fn read_pipe(handle: FT_HANDLE, pipe: u8, buf: &mut [u8]) -> Result<usize> {
let mut bytes_read: ULONG = 0;
try_d3xx!(unsafe {
FT_ReadPipe(
handle,
pipe,
buf.as_mut_ptr().cast(),
ULONG::try_from(buf.len()).expect("buffer length exceeds ULONG::MAX"),
std::ptr::addr_of_mut!(bytes_read),
std::ptr::null_mut(),
)
})?;
Ok(bytes_read as usize)
}
#[cfg(not(windows))]
pub(crate) fn read_pipe(handle: FT_HANDLE, pipe: u8, buf: &mut [u8]) -> Result<usize> {
let mut bytes_read: ULONG = 0;
try_d3xx!(unsafe {
FT_ReadPipe(
handle,
pipe as u8,
buf.as_mut_ptr().cast(),
ULONG::try_from(buf.len()).expect("buffer length exceeds ULONG::MAX"),
std::ptr::addr_of_mut!(bytes_read),
0,
)
})?;
Ok(bytes_read as usize)
}
#[cfg(windows)]
pub(crate) fn read_pipe_async(
handle: FT_HANDLE,
pipe: u8,
buf: &mut [u8],
overlapped: &mut _OVERLAPPED,
) -> Result<()> {
let mut bytes_read: ULONG = 0;
ignore_io_pending(try_d3xx!(unsafe {
FT_ReadPipe(
handle,
pipe,
buf.as_mut_ptr().cast(),
ULONG::try_from(buf.len()).expect("buffer length exceeds ULONG::MAX"),
std::ptr::addr_of_mut!(bytes_read),
overlapped as *mut _OVERLAPPED,
)
}))
}
#[cfg(not(windows))]
pub fn read_pipe_async(
handle: FT_HANDLE,
pipe: u8,
buf: &mut [u8],
overlapped: &mut _OVERLAPPED,
) -> Result<()> {
let mut bytes_read: ULONG = 0;
ignore_io_pending(try_d3xx!(unsafe {
FT_ReadPipeAsync(
handle,
pipe,
buf.as_mut_ptr().cast(),
ULONG::try_from(buf.len()).expect("buffer length exceeds ULONG::MAX"),
std::ptr::addr_of_mut!(bytes_read),
overlapped as *mut _OVERLAPPED,
)
}))
}
#[inline]
fn ignore_io_pending(res: Result<()>) -> Result<()> {
match res {
Err(D3xxError::IoPending) => Ok(()),
x => x,
}
}