use std::ptr::NonNull;
use crate::error::{Result, SpanDspError};
use crate::t30::T30ModemSupport;
use crate::t38_core::T38Core;
pub struct T38Gateway {
inner: NonNull<spandsp_sys::t38_gateway_state_t>,
}
impl T38Gateway {
pub unsafe fn new_raw(
tx_packet_handler: spandsp_sys::t38_tx_packet_handler_t,
tx_packet_user_data: *mut std::ffi::c_void,
) -> Result<Self> {
unsafe {
let ptr = spandsp_sys::t38_gateway_init(
std::ptr::null_mut(),
tx_packet_handler,
tx_packet_user_data,
);
let inner = NonNull::new(ptr).ok_or(SpanDspError::InitFailed)?;
Ok(Self { inner })
}
}
pub fn as_ptr(&self) -> *mut spandsp_sys::t38_gateway_state_t {
self.inner.as_ptr()
}
pub fn get_t38_core_state(&self) -> Result<T38Core> {
let ptr = unsafe { spandsp_sys::t38_gateway_get_t38_core_state(self.inner.as_ptr()) };
unsafe { T38Core::from_raw(ptr) }
}
pub fn rx(&self, samples: &mut [i16]) -> usize {
unsafe {
spandsp_sys::t38_gateway_rx(
self.inner.as_ptr(),
samples.as_mut_ptr(),
samples.len() as i32,
) as usize
}
}
pub fn tx(&self, buf: &mut [i16]) -> usize {
unsafe {
spandsp_sys::t38_gateway_tx(self.inner.as_ptr(), buf.as_mut_ptr(), buf.len() as i32)
as usize
}
}
pub fn set_ecm_capability(&self, allowed: bool) {
unsafe {
spandsp_sys::t38_gateway_set_ecm_capability(self.inner.as_ptr(), allowed);
}
}
pub fn set_transmit_on_idle(&self, on: bool) {
unsafe {
spandsp_sys::t38_gateway_set_transmit_on_idle(self.inner.as_ptr(), on);
}
}
pub fn set_supported_modems(&self, modems: T30ModemSupport) {
unsafe {
spandsp_sys::t38_gateway_set_supported_modems(self.inner.as_ptr(), modems.bits());
}
}
pub fn set_tep_mode(&self, use_tep: bool) {
unsafe {
spandsp_sys::t38_gateway_set_tep_mode(self.inner.as_ptr(), use_tep);
}
}
pub fn get_transfer_statistics(&self) -> spandsp_sys::t38_stats_t {
let mut stats = unsafe { std::mem::zeroed::<spandsp_sys::t38_stats_t>() };
unsafe {
spandsp_sys::t38_gateway_get_transfer_statistics(self.inner.as_ptr(), &mut stats);
}
stats
}
}
impl Drop for T38Gateway {
fn drop(&mut self) {
unsafe {
spandsp_sys::t38_gateway_free(self.inner.as_ptr());
}
}
}