use std::ffi::c_ulong;
use crate::{try_d3xx, D3xxError};
use super::{FT_ReadPipe, FT_WritePipe, Result, FT_HANDLE, _OVERLAPPED};
pub(crate) fn write_pipe(handle: FT_HANDLE, pipe: u8, buf: &[u8]) -> Result<usize> {
let mut bytes_written: c_ulong = 0;
try_d3xx!(unsafe {
FT_WritePipe(
handle,
pipe,
buf.as_ptr().cast_mut(),
c_ulong::try_from(buf.len()).expect("buffer length exceeds c_ulong::MAX"),
std::ptr::addr_of_mut!(bytes_written),
std::ptr::null_mut(),
)
})?;
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: c_ulong = 0;
ignore_io_pending(try_d3xx!(unsafe {
FT_WritePipe(
handle,
pipe,
buf.as_ptr().cast_mut(),
c_ulong::try_from(buf.len()).expect("buffer length exceeds c_ulong::MAX"),
std::ptr::addr_of_mut!(bytes_written),
overlapped as *mut _OVERLAPPED,
)
}))
}
#[cfg(not(windows))]
pub fn write_pipe_async(
device: &Device,
pipe: u8,
buf: &[u8],
overlapped: &mut _OVERLAPPED,
) -> Result<()> {
let mut bytes_written: c_ulong = 0;
ignore_io_pending(try_d3xx!(unsafe {
FT_WritePipeAsync(
handle,
pipe as u8,
buf.as_ptr().cast_mut(),
c_ulong::try_from(buf.len()).expect("buffer length exceeds c_ulong::MAX"),
std::ptr::addr_of_mut!(bytes_written),
overlapped as *mut _OVERLAPPED,
)
}))
}
pub(crate) fn read_pipe(handle: FT_HANDLE, pipe: u8, buf: &mut [u8]) -> Result<usize> {
let mut bytes_read: c_ulong = 0;
try_d3xx!(unsafe {
FT_ReadPipe(
handle,
pipe,
buf.as_mut_ptr().cast(),
c_ulong::try_from(buf.len()).expect("buffer length exceeds c_ulong::MAX"),
std::ptr::addr_of_mut!(bytes_read),
std::ptr::null_mut(),
)
})?;
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: c_ulong = 0;
ignore_io_pending(try_d3xx!(unsafe {
FT_ReadPipe(
handle,
pipe,
buf.as_mut_ptr().cast(),
c_ulong::try_from(buf.len()).expect("buffer length exceeds c_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: c_ulong = 0;
ignore_io_pending(try_d3xx!(unsafe {
FT_ReadPipeAsync(
handle,
pipe,
buf.as_mut_ptr().cast(),
c_ulong::try_from(buf.len()).expect("buffer length exceeds c_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,
}
}