From af47df7d46f081df17bd41f729dd3bc887a375f1 Mon Sep 17 00:00:00 2001 From: Siddharth Chandrasekaran Date: Tue, 21 Nov 2023 00:27:47 +0100 Subject: [PATCH] rust: reorg some structs into dedicated modules Signed-off-by: Siddharth Chandrasekaran --- osdpctl/src/config.rs | 4 +- rust/examples/cp.rs | 4 +- rust/examples/pd.rs | 4 +- rust/src/cp.rs | 10 +-- rust/src/lib.rs | 11 +++- rust/src/pd.rs | 10 +-- rust/src/{common.rs => pdcap.rs} | 101 +------------------------------ rust/src/pdid.rs | 35 +++++++++++ rust/src/pdinfo.rs | 65 ++++++++++++++++++++ 9 files changed, 131 insertions(+), 113 deletions(-) rename rust/src/{common.rs => pdcap.rs} (78%) create mode 100644 rust/src/pdid.rs create mode 100644 rust/src/pdinfo.rs diff --git a/osdpctl/src/config.rs b/osdpctl/src/config.rs index 3ab4ba77..7d6874a0 100644 --- a/osdpctl/src/config.rs +++ b/osdpctl/src/config.rs @@ -1,7 +1,9 @@ use configparser::ini::Ini; use libosdp::{ channel::{unix_channel::UnixChannel, OsdpChannel}, - common::{OsdpFlag, PdCapability, PdId, PdInfo}, + pdinfo::{OsdpFlag, PdInfo}, + pdid::PdId, + pdcap::PdCapability, }; use std::{ fmt::Write, diff --git a/rust/examples/cp.rs b/rust/examples/cp.rs index a045b1d3..111739b0 100644 --- a/rust/examples/cp.rs +++ b/rust/examples/cp.rs @@ -5,9 +5,9 @@ use std::{ }; use libosdp::{ cp::ControlPanel, - common::{PdInfo, OsdpFlag, PdId}, + pdinfo::{PdInfo, OsdpFlag}, channel::{OsdpChannel, unix_channel::UnixChannel}, - error::OsdpError, + error::OsdpError, pdid::PdId, }; fn main() -> Result<(), OsdpError> { diff --git a/rust/examples/pd.rs b/rust/examples/pd.rs index 6094dac8..05793e26 100644 --- a/rust/examples/pd.rs +++ b/rust/examples/pd.rs @@ -5,9 +5,11 @@ use std::{ }; use libosdp::{ pd::PeripheralDevice, - common::{PdInfo, OsdpFlag, PdId, PdCapability, PdCapEntry}, + pdinfo::{PdInfo, OsdpFlag}, channel::{OsdpChannel, unix_channel::UnixChannel}, error::OsdpError, + pdid::PdId, + pdcap::{PdCapability, PdCapEntry}, }; fn main() -> Result<(), OsdpError> { diff --git a/rust/src/cp.rs b/rust/src/cp.rs index 5ebcc035..a06f5695 100644 --- a/rust/src/cp.rs +++ b/rust/src/cp.rs @@ -1,10 +1,12 @@ use crate::{ commands::OsdpCommand, - common::{OsdpFlag, PdCapability, PdId, PdInfo}, events::OsdpEvent, file::OsdpFile, osdp_sys, error::OsdpError, + pdinfo::{PdInfo, OsdpFlag}, + pdcap::PdCapability, + pdid::PdId, }; use log::{debug, error, info, warn}; use std::ffi::c_void; @@ -19,7 +21,7 @@ unsafe extern "C" fn log_handler( _line: ::std::os::raw::c_ulong, msg: *const ::std::os::raw::c_char, ) { - let msg = crate::common::cstr_to_string(msg); + let msg = crate::cstr_to_string(msg); let msg = msg.trim(); match log_level as u32 { osdp_sys::osdp_log_level_e_OSDP_LOG_EMERG => error!("{msg}"), @@ -169,12 +171,12 @@ impl ControlPanel { pub fn get_version(&self) -> String { let s = unsafe { osdp_sys::osdp_get_version() }; - crate::common::cstr_to_string(s) + crate::cstr_to_string(s) } pub fn get_source_info(&self) -> String { let s = unsafe { osdp_sys::osdp_get_source_info() }; - crate::common::cstr_to_string(s) + crate::cstr_to_string(s) } pub fn is_online(&self, pd: i32) -> bool { diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 8ca48c9b..df0dedb0 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -4,7 +4,7 @@ // #![warn(missing_docs)] pub mod osdp_sys; -pub mod common; +pub mod pdinfo; pub mod cp; pub mod pd; pub mod commands; @@ -12,3 +12,12 @@ pub mod events; pub mod channel; pub mod file; pub mod error; +pub mod pdcap; +pub mod pdid; + +pub fn cstr_to_string(s: *const ::std::os::raw::c_char) -> String { + let s = unsafe { + std::ffi::CStr::from_ptr(s) + }; + s.to_str().unwrap().to_owned() +} \ No newline at end of file diff --git a/rust/src/pd.rs b/rust/src/pd.rs index d91e8a7e..56c63e5d 100644 --- a/rust/src/pd.rs +++ b/rust/src/pd.rs @@ -1,10 +1,10 @@ use crate::{ commands::OsdpCommand, - common::{PdCapability, PdInfo}, + pdinfo::PdInfo, events::OsdpEvent, file::OsdpFile, osdp_sys, - error::OsdpError, + error::OsdpError, pdcap::PdCapability, }; use log::{debug, error, info, warn}; use std::ffi::c_void; @@ -19,7 +19,7 @@ unsafe extern "C" fn log_handler( _line: ::std::os::raw::c_ulong, msg: *const ::std::os::raw::c_char, ) { - let msg = crate::common::cstr_to_string(msg); + let msg = crate::cstr_to_string(msg); let msg = msg.trim(); match log_level as u32 { osdp_sys::osdp_log_level_e_OSDP_LOG_EMERG => error!("{msg}"), @@ -140,12 +140,12 @@ impl PeripheralDevice { pub fn get_version(&self) -> String { let s = unsafe { osdp_sys::osdp_get_version() }; - crate::common::cstr_to_string(s) + crate::cstr_to_string(s) } pub fn get_source_info(&self) -> String { let s = unsafe { osdp_sys::osdp_get_source_info() }; - crate::common::cstr_to_string(s) + crate::cstr_to_string(s) } pub fn is_online(&self) -> bool { diff --git a/rust/src/common.rs b/rust/src/pdcap.rs similarity index 78% rename from rust/src/common.rs rename to rust/src/pdcap.rs index 5c9ce8b3..e95f6f4b 100644 --- a/rust/src/common.rs +++ b/rust/src/pdcap.rs @@ -1,38 +1,5 @@ -use crate::{osdp_sys, channel::OsdpChannel, error::OsdpError}; -use std::{ffi::{CString, CStr}, str::FromStr}; - -#[derive(Clone, Debug, Default)] -pub struct PdId { - pub version: i32, - pub model: i32, - pub vendor_code: u32, - pub serial_number: u32, - pub firmware_version: u32, -} - -impl From for PdId { - fn from(value: osdp_sys::osdp_pd_id) -> Self { - Self { - version: value.version, - model: value.model, - vendor_code: value.vendor_code, - serial_number: value.serial_number, - firmware_version: value.firmware_version, - } - } -} - -impl From for osdp_sys::osdp_pd_id { - fn from(value: PdId) -> Self { - osdp_sys::osdp_pd_id { - version: value.version, - model: value.model, - vendor_code: value.vendor_code, - serial_number: value.serial_number, - firmware_version: value.firmware_version, - } - } -} +use std::str::FromStr; +use crate::{error::OsdpError, osdp_sys}; #[derive(Clone, Debug, Default)] pub struct PdCapEntry { @@ -248,67 +215,3 @@ impl From for osdp_sys::osdp_pd_cap { } } } - -bitflags::bitflags! { - #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] - pub struct OsdpFlag: u32 { - const EnforceSecure = osdp_sys::OSDP_FLAG_ENFORCE_SECURE; - const InstallMode = osdp_sys::OSDP_FLAG_INSTALL_MODE; - const IgnoreUnsolicited = osdp_sys::OSDP_FLAG_IGN_UNSOLICITED; - } -} - -impl FromStr for OsdpFlag { - type Err = OsdpError; - - fn from_str(s: &str) -> Result { - match s { - "EnforceSecure" => Ok(OsdpFlag::EnforceSecure), - "InstallMode" => Ok(OsdpFlag::InstallMode), - "IgnoreUnsolicited" => Ok(OsdpFlag::IgnoreUnsolicited), - _ => Err(OsdpError::Parse(format!("OsdpFlag: {s}"))) - } - } -} - -#[derive(Debug)] -pub struct PdInfo { - pub name: CString, - pub address: i32, - pub baud_rate: i32, - pub flags: OsdpFlag, - pub id: PdId, - pub cap: Vec, - pub channel: OsdpChannel, - pub scbk: [u8; 16], -} - -impl PdInfo { - pub fn new(name: &str, address: i32, baud_rate: i32, flags: OsdpFlag, id: PdId, cap: Vec, channel: OsdpChannel, scbk: [u8; 16]) -> Self { - let name = CString::new(name).unwrap(); - let cap = cap.into_iter() - .map(|c| { c.into() }) - .collect(); - Self { name, address, baud_rate, flags, id, cap, channel, scbk } - } - - 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_ptr(), - channel: self.channel.as_struct(), - scbk: self.scbk.as_ptr(), - } - } -} - -pub fn cstr_to_string(s: *const ::std::os::raw::c_char) -> String { - let s = unsafe { - CStr::from_ptr(s) - }; - s.to_str().unwrap().to_owned() -} diff --git a/rust/src/pdid.rs b/rust/src/pdid.rs new file mode 100644 index 00000000..41390b2b --- /dev/null +++ b/rust/src/pdid.rs @@ -0,0 +1,35 @@ +use crate::osdp_sys; + + +#[derive(Clone, Debug, Default)] +pub struct PdId { + pub version: i32, + pub model: i32, + pub vendor_code: u32, + pub serial_number: u32, + pub firmware_version: u32, +} + +impl From for PdId { + fn from(value: osdp_sys::osdp_pd_id) -> Self { + Self { + version: value.version, + model: value.model, + vendor_code: value.vendor_code, + serial_number: value.serial_number, + firmware_version: value.firmware_version, + } + } +} + +impl From for osdp_sys::osdp_pd_id { + fn from(value: PdId) -> Self { + osdp_sys::osdp_pd_id { + version: value.version, + model: value.model, + vendor_code: value.vendor_code, + serial_number: value.serial_number, + firmware_version: value.firmware_version, + } + } +} diff --git a/rust/src/pdinfo.rs b/rust/src/pdinfo.rs new file mode 100644 index 00000000..6b87f730 --- /dev/null +++ b/rust/src/pdinfo.rs @@ -0,0 +1,65 @@ +use crate::{ + osdp_sys, + channel::OsdpChannel, + error::OsdpError, + pdid::PdId, + pdcap::PdCapability +}; +use std::{ffi::CString, str::FromStr}; + +bitflags::bitflags! { + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] + pub struct OsdpFlag: u32 { + const EnforceSecure = osdp_sys::OSDP_FLAG_ENFORCE_SECURE; + const InstallMode = osdp_sys::OSDP_FLAG_INSTALL_MODE; + const IgnoreUnsolicited = osdp_sys::OSDP_FLAG_IGN_UNSOLICITED; + } +} + +impl FromStr for OsdpFlag { + type Err = OsdpError; + + fn from_str(s: &str) -> Result { + match s { + "EnforceSecure" => Ok(OsdpFlag::EnforceSecure), + "InstallMode" => Ok(OsdpFlag::InstallMode), + "IgnoreUnsolicited" => Ok(OsdpFlag::IgnoreUnsolicited), + _ => Err(OsdpError::Parse(format!("OsdpFlag: {s}"))) + } + } +} + +#[derive(Debug)] +pub struct PdInfo { + pub name: CString, + pub address: i32, + pub baud_rate: i32, + pub flags: OsdpFlag, + pub id: PdId, + pub cap: Vec, + pub channel: OsdpChannel, + pub scbk: [u8; 16], +} + +impl PdInfo { + pub fn new(name: &str, address: i32, baud_rate: i32, flags: OsdpFlag, id: PdId, cap: Vec, channel: OsdpChannel, scbk: [u8; 16]) -> Self { + let name = CString::new(name).unwrap(); + let cap = cap.into_iter() + .map(|c| { c.into() }) + .collect(); + Self { name, address, baud_rate, flags, id, cap, channel, scbk } + } + + 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_ptr(), + channel: self.channel.as_struct(), + scbk: self.scbk.as_ptr(), + } + } +}