Skip to content

Commit

Permalink
rust: reorg some structs into dedicated modules
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 509dab7 commit af47df7
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 113 deletions.
4 changes: 3 additions & 1 deletion osdpctl/src/config.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions rust/examples/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
4 changes: 3 additions & 1 deletion rust/examples/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
10 changes: 6 additions & 4 deletions rust/src/cp.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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}"),
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
// #![warn(missing_docs)]

pub mod osdp_sys;
pub mod common;
pub mod pdinfo;
pub mod cp;
pub mod pd;
pub mod commands;
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()
}
10 changes: 5 additions & 5 deletions rust/src/pd.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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}"),
Expand Down Expand Up @@ -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 {
Expand Down
101 changes: 2 additions & 99 deletions rust/src/common.rs → rust/src/pdcap.rs
Original file line number Diff line number Diff line change
@@ -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 <osdp_sys::osdp_pd_id> 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<PdId> 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 {
Expand Down Expand Up @@ -248,67 +215,3 @@ impl From<PdCapability> 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<Self, Self::Err> {
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<osdp_sys::osdp_pd_cap>,
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<PdCapability>, 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()
}
35 changes: 35 additions & 0 deletions rust/src/pdid.rs
Original file line number Diff line number Diff line change
@@ -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 <osdp_sys::osdp_pd_id> 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<PdId> 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,
}
}
}
65 changes: 65 additions & 0 deletions rust/src/pdinfo.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
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<osdp_sys::osdp_pd_cap>,
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<PdCapability>, 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(),
}
}
}

0 comments on commit af47df7

Please sign in to comment.