Skip to content

Commit

Permalink
rust: Adapt to const changes in osdp.h
Browse files Browse the repository at this point in the history
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Nov 20, 2023
1 parent 87fadea commit 8dfc75e
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 41 deletions.
4 changes: 2 additions & 2 deletions osdpctl/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct CpDaemon {

impl CpDaemon {
pub fn new(dev: &CpConfig) -> Result<Self> {
let mut pd_info = dev.pd_info()?;
let mut cp = ControlPanel::new(&mut pd_info)?;
let pd_info = dev.pd_info()?;
let mut cp = ControlPanel::new(pd_info)?;
cp.set_event_callback(|pd, event| {
match event {
OsdpEvent::CardRead(e) => {
Expand Down
4 changes: 2 additions & 2 deletions osdpctl/src/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct PdDaemon {

impl PdDaemon {
pub fn new(dev: &PdConfig) -> Result<Self> {
let mut pd_info = dev.pd_info()?;
let mut pd = PeripheralDevice::new(&mut pd_info)?;
let pd_info = dev.pd_info()?;
let mut pd = PeripheralDevice::new(pd_info)?;
pd.set_command_callback(|command| {
match command {
OsdpCommand::Led(c) => {
Expand Down
4 changes: 2 additions & 2 deletions rust/examples/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use libosdp::{
fn main() -> Result<(), OsdpError> {
env_logger::init();
let stream = UnixChannel::connect("conn-1")?;
let mut pd_info = vec![
let pd_info = vec![
PdInfo::new(
"PD 101", 101,
115200,
Expand All @@ -33,7 +33,7 @@ fn main() -> Result<(), OsdpError> {
]
),
];
let mut cp = ControlPanel::new(&mut pd_info)?;
let mut cp = ControlPanel::new(pd_info)?;
loop {
cp.refresh();
thread::sleep(Duration::from_millis(50));
Expand Down
6 changes: 3 additions & 3 deletions rust/examples/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use libosdp::{
fn main() -> Result<(), OsdpError> {
env_logger::init();
let stream = UnixChannel::new("conn-1")?;
let mut pd_info = PdInfo::new(
let pd_info = PdInfo::new(
"PD 101",
101,
115200,
Expand All @@ -26,15 +26,15 @@ fn main() -> Result<(), OsdpError> {
firmware_version: 0x05,
},
vec![
PdCapability::CommunicationSecurity(PdCapEntry { compliance: 1, num_items: 1 }),
PdCapability::CommunicationSecurity(PdCapEntry::new(1, 1)),
],
OsdpChannel::new::<UnixChannel>(Box::new(stream)),
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
]
);
let mut pd = PeripheralDevice::new(&mut pd_info)?;
let mut pd = PeripheralDevice::new(pd_info)?;
pd.set_command_callback(|_| {
println!("Received command!");
0
Expand Down
7 changes: 4 additions & 3 deletions rust/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ use std::{
ffi::c_void,
hash::{Hash, Hasher},
io::{Read, Write},
sync::Mutex,
sync::{Mutex, Arc},
};

pub trait Channel: Read + Write {
fn get_id(&self) -> i32;
}

#[derive(Clone)]
pub struct OsdpChannel {
stream: Mutex<Box<dyn Channel>>,
stream: Arc<Mutex<Box<dyn Channel>>>,
}

impl std::fmt::Debug for OsdpChannel {
Expand Down Expand Up @@ -62,7 +63,7 @@ unsafe extern "C" fn raw_flush(data: *mut c_void) {
impl OsdpChannel {
pub fn new<T: Channel>(stream: Box<dyn Channel>) -> OsdpChannel {
Self {
stream: Mutex::new(stream),
stream: Arc::new(Mutex::new(stream)),
}
}

Expand Down
18 changes: 12 additions & 6 deletions rust/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ impl From<PdId> for osdp_sys::osdp_pd_id {

#[derive(Clone, Debug, Default)]
pub struct PdCapEntry {
pub compliance: u8,
pub num_items: u8,
compliance: u8,
num_items: u8,
}

impl PdCapEntry {
pub fn new(compliance: u8, num_items: u8) -> Self {
Self { compliance, num_items }
}
}

// From "Compliance:10,NumItems:20" to PdCapEntry { compliance: 10, num_items: 20 }
Expand Down Expand Up @@ -286,16 +292,16 @@ impl PdInfo {
Self { name, address, baud_rate, flags, id, cap, channel, scbk }
}

pub fn as_struct(&mut self) -> osdp_sys::osdp_pd_info_t {
pub fn as_struct(&self) -> osdp_sys::osdp_pd_info_t {
osdp_sys::osdp_pd_info_t {
name: self.name.as_ptr(),
baud_rate: self.baud_rate,
address: self.address,
flags: self.flags.bits() as i32,
id: self.id.clone().into(),
cap: self.cap.as_mut_ptr(),
channel: self.channel.as_struct(),
scbk: self.scbk.as_mut_ptr(),
cap: self.cap.as_ptr(),
channel: self.channel.clone().as_struct(),
scbk: self.scbk.as_ptr(),
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions rust/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ where
trampoline::<F>
}

fn cp_setup(mut info: Vec<osdp_sys::osdp_pd_info_t>) -> Result<*mut c_void> {
fn cp_setup(info: Vec<osdp_sys::osdp_pd_info_t>) -> Result<*mut c_void> {
let ctx = unsafe {
osdp_sys::osdp_cp_setup(info.len() as i32, info.as_mut_ptr())
osdp_sys::osdp_cp_setup(info.len() as i32, info.as_ptr())
};
if ctx.is_null() {
Err(OsdpError::Setup)
Expand All @@ -71,12 +71,12 @@ pub struct ControlPanel {
}

impl ControlPanel {
pub fn new(pd_info: &mut Vec<PdInfo>) -> Result<Self> {
pub fn new(pd_info: Vec<PdInfo>) -> Result<Self> {
if pd_info.len() > 126 {
return Err(OsdpError::PdInfo("max PD count exceeded"))
}
let info: Vec<osdp_sys::osdp_pd_info_t> = pd_info
.iter_mut()
.iter()
.map(|i| { i.as_struct() })
.collect();
unsafe { osdp_sys::osdp_set_log_callback(Some(log_handler)) };
Expand All @@ -88,8 +88,7 @@ impl ControlPanel {
}

pub fn send_command(&mut self, pd: i32, cmd: OsdpCommand) -> Result<()> {
let mut cmd = cmd.into();
let rc = unsafe { osdp_sys::osdp_cp_send_command(self.ctx, pd, std::ptr::addr_of_mut!(cmd)) };
let rc = unsafe { osdp_sys::osdp_cp_send_command(self.ctx, pd, &cmd.into()) };
if rc < 0 {
Err(OsdpError::Command)
} else {
Expand Down
7 changes: 1 addition & 6 deletions rust/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ unsafe extern "C" fn raw_file_close(data: *mut c_void) -> i32 {

impl OsdpFile {
pub fn new(id: i32, path: PathBuf) -> Self {
Self {
id,
path,
file: None,
size: 0,
}
Self { id, path, file: None, size: 0, }
}

pub fn get_ops_struct(&mut self) -> osdp_sys::osdp_file_ops {
Expand Down
22 changes: 11 additions & 11 deletions rust/src/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ where
trampoline::<F>
}

fn pd_setup(info: &mut PdInfo) -> Result<*mut c_void> {
fn pd_setup(info: PdInfo) -> Result<*mut c_void> {
let info = info.as_struct();
let ctx = unsafe {
osdp_sys::osdp_pd_setup(&mut info.as_struct())
osdp_sys::osdp_pd_setup(&info)
};
if ctx.is_null() {
Err(OsdpError::Setup)
Expand All @@ -67,7 +68,7 @@ pub struct PeripheralDevice {
}

impl PeripheralDevice {
pub fn new(info: &mut PdInfo) -> Result<Self> {
pub fn new(info: PdInfo) -> Result<Self> {
unsafe { osdp_sys::osdp_set_log_callback(Some(log_handler)) };
Ok(Self { ctx: pd_setup(info)? })
}
Expand All @@ -76,20 +77,19 @@ impl PeripheralDevice {
unsafe { osdp_sys::osdp_pd_refresh(self.ctx) }
}

pub fn set_capabilities(&self, cap: &mut Vec<PdCapability>) {
let mut cap: Vec<osdp_sys::osdp_pd_cap> = cap.iter_mut()
pub fn set_capabilities(&self, cap: &Vec<PdCapability>) {
let cap: Vec<osdp_sys::osdp_pd_cap> = cap.iter()
.map(|c| -> osdp_sys::osdp_pd_cap { c.clone().into() })
.collect();
unsafe { osdp_sys::osdp_pd_set_capabilities(self.ctx, cap.as_mut_ptr()) }
unsafe { osdp_sys::osdp_pd_set_capabilities(self.ctx, cap.as_ptr()) }
}

pub fn flush_events(&mut self) {
let _ = unsafe { osdp_sys::osdp_pd_flush_events(self.ctx) };
}

pub fn notify_event(&mut self, event: OsdpEvent) -> Result<()> {
let mut event = event.into();
let rc = unsafe { osdp_sys::osdp_pd_notify_event(self.ctx, &mut event) };
let rc = unsafe { osdp_sys::osdp_pd_notify_event(self.ctx, &event.into()) };
if rc < 0 {
Err(OsdpError::Event)
} else {
Expand Down Expand Up @@ -120,7 +120,7 @@ impl PeripheralDevice {
}
}

pub fn get_file_transfer_status(&mut self) -> Result<(i32, i32)> {
pub fn get_file_transfer_status(&self) -> Result<(i32, i32)> {
let mut size: i32 = 0;
let mut offset: i32 = 0;
let rc = unsafe {
Expand Down Expand Up @@ -148,13 +148,13 @@ impl PeripheralDevice {
crate::common::cstr_to_string(s)
}

pub fn is_online(&mut self) -> bool {
pub fn is_online(&self) -> bool {
let mut buf: u8 = 0;
unsafe { osdp_sys::osdp_get_status_mask(self.ctx, &mut buf as *mut u8) };
buf != 0
}

pub fn is_sc_active(&mut self) -> bool {
pub fn is_sc_active(&self) -> bool {
let mut buf: u8 = 0;
unsafe { osdp_sys::osdp_get_sc_status_mask(self.ctx, &mut buf as *mut u8) };
buf != 0
Expand Down

0 comments on commit 8dfc75e

Please sign in to comment.