Skip to content

Commit

Permalink
rust: Run cargo fmt for uniform style
Browse files Browse the repository at this point in the history
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Dec 12, 2023
1 parent 9b525d1 commit 6223def
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 164 deletions.
35 changes: 15 additions & 20 deletions rust/examples/cp.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use std::{
time::Duration,
thread,
result::Result,
};
use libosdp::{
cp::ControlPanel,
PdInfo, OsdpFlag, OsdpError,
channel::{OsdpChannel, UnixChannel},
cp::ControlPanel,
OsdpError, OsdpFlag, PdInfo,
};
use std::{result::Result, thread, time::Duration};

fn main() -> Result<(), OsdpError> {
env_logger::builder()
Expand All @@ -16,21 +12,20 @@ fn main() -> Result<(), OsdpError> {
.format_timestamp(None)
.init();
let stream = UnixChannel::connect("conn-1")?;
let pd_info = vec![
PdInfo::for_cp(
"PD 101", 101,
115200,
OsdpFlag::EnforceSecure,
OsdpChannel::new::<UnixChannel>(Box::new(stream)),
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
]
),
];
let pd_info = vec![PdInfo::for_cp(
"PD 101",
101,
115200,
OsdpFlag::EnforceSecure,
OsdpChannel::new::<UnixChannel>(Box::new(stream)),
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f,
],
)];
let mut cp = ControlPanel::new(pd_info)?;
loop {
cp.refresh();
thread::sleep(Duration::from_millis(50));
}
}
}
24 changes: 9 additions & 15 deletions rust/examples/pd.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use std::{
time::Duration,
thread,
result::Result,
};
use libosdp::{
pd::PeripheralDevice,
PdInfo, OsdpFlag, OsdpError, PdId, PdCapability, PdCapEntity,
channel::{OsdpChannel, UnixChannel},
pd::PeripheralDevice,
OsdpError, OsdpFlag, PdCapEntity, PdCapability, PdId, PdInfo,
};
use std::{result::Result, thread, time::Duration};

fn main() -> Result<(), OsdpError> {
env_logger::builder()
Expand All @@ -16,20 +12,18 @@ fn main() -> Result<(), OsdpError> {
.format_timestamp(None)
.init();
let stream = UnixChannel::new("conn-1")?;
let pd_info = PdInfo::for_pd(
let pd_info = PdInfo::for_pd(
"PD 101",
101,
115200,
OsdpFlag::EnforceSecure,
PdId::from_number(101),
vec![
PdCapability::CommunicationSecurity(PdCapEntity::new(1, 1)),
],
vec![PdCapability::CommunicationSecurity(PdCapEntity::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,
]
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f,
],
);
let mut pd = PeripheralDevice::new(pd_info)?;
pd.set_command_callback(|_| {
Expand All @@ -40,4 +34,4 @@ fn main() -> Result<(), OsdpError> {
pd.refresh();
thread::sleep(Duration::from_millis(50));
}
}
}
1 change: 1 addition & 0 deletions rust/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use unix_channel::UnixChannel;
use alloc::collections::BTreeMap as HashMap;
use alloc::{boxed::Box, format, sync::Arc, vec};
use core::ffi::c_void;
use lazy_static::lazy_static;
#[cfg(feature = "std")]
use std::{
collections::{hash_map::DefaultHasher, HashMap},
Expand Down
36 changes: 22 additions & 14 deletions rust/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub enum OsdpLedColor {
/// No Color
#[default] None,
#[default]
None,

/// Red Color
Red,
Expand Down Expand Up @@ -107,7 +108,7 @@ impl From<libosdp_sys::osdp_cmd_led_params> for OsdpLedParams {
}
}

impl From<OsdpLedParams> for libosdp_sys::osdp_cmd_led_params{
impl From<OsdpLedParams> for libosdp_sys::osdp_cmd_led_params {
fn from(value: OsdpLedParams) -> Self {
libosdp_sys::osdp_cmd_led_params {
control_code: value.control_code,
Expand Down Expand Up @@ -502,7 +503,8 @@ impl From<OsdpCommandFileTx> for libosdp_sys::osdp_cmd_file_tx {
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub enum OsdpCommandStatus {
/// Query local status (tamper and power) from PD
#[default] Local,
#[default]
Local,
/// Query input status from PD
Input,
/// Query output status from PD
Expand All @@ -512,9 +514,15 @@ pub enum OsdpCommandStatus {
impl From<libosdp_sys::osdp_cmd_status> for OsdpCommandStatus {
fn from(value: libosdp_sys::osdp_cmd_status) -> Self {
match value.type_ {
libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_INPUT => OsdpCommandStatus::Input,
libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_OUTPUT => OsdpCommandStatus::Output,
libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_LOCAL => OsdpCommandStatus::Local,
libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_INPUT => {
OsdpCommandStatus::Input
}
libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_OUTPUT => {
OsdpCommandStatus::Output
}
libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_LOCAL => {
OsdpCommandStatus::Local
}
_ => panic!("Unknown status command type"),
}
}
Expand All @@ -524,13 +532,13 @@ impl From<OsdpCommandStatus> for libosdp_sys::osdp_cmd_status {
fn from(value: OsdpCommandStatus) -> Self {
match value {
OsdpCommandStatus::Input => libosdp_sys::osdp_cmd_status {
type_: libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_INPUT
type_: libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_INPUT,
},
OsdpCommandStatus::Output => libosdp_sys::osdp_cmd_status {
type_: libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_OUTPUT
type_: libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_OUTPUT,
},
OsdpCommandStatus::Local => libosdp_sys::osdp_cmd_status {
type_: libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_LOCAL
type_: libosdp_sys::osdp_command_status_query_e_OSDP_CMD_STATUS_QUERY_LOCAL,
},
}
}
Expand Down Expand Up @@ -577,7 +585,7 @@ impl From<OsdpCommand> for libosdp_sys::osdp_cmd {
OsdpCommand::Led(c) => libosdp_sys::osdp_cmd {
id: libosdp_sys::osdp_cmd_e_OSDP_CMD_LED,
__bindgen_anon_1: libosdp_sys::osdp_cmd__bindgen_ty_1 {
led: c.clone().into()
led: c.clone().into(),
},
},
OsdpCommand::Buzzer(c) => libosdp_sys::osdp_cmd {
Expand Down Expand Up @@ -613,7 +621,7 @@ impl From<OsdpCommand> for libosdp_sys::osdp_cmd {
OsdpCommand::Mfg(c) => libosdp_sys::osdp_cmd {
id: libosdp_sys::osdp_cmd_e_OSDP_CMD_MFG,
__bindgen_anon_1: libosdp_sys::osdp_cmd__bindgen_ty_1 {
mfg: c.clone().into()
mfg: c.clone().into(),
},
},
OsdpCommand::FileTx(c) => libosdp_sys::osdp_cmd {
Expand All @@ -625,7 +633,7 @@ impl From<OsdpCommand> for libosdp_sys::osdp_cmd {
OsdpCommand::Status(c) => libosdp_sys::osdp_cmd {
id: libosdp_sys::osdp_cmd_e_OSDP_CMD_STATUS,
__bindgen_anon_1: libosdp_sys::osdp_cmd__bindgen_ty_1 {
status: c.clone().into()
status: c.clone().into(),
},
},
}
Expand Down Expand Up @@ -677,7 +685,7 @@ mod tests {
let cmd = OsdpCommandMfg {
vendor_code: (0x05, 0x07, 0x09),
command: 0x47,
data: vec![0x55, 0xAA]
data: vec![0x55, 0xAA],
};
let cmd_struct: osdp_cmd_mfg = cmd.clone().into();

Expand All @@ -689,4 +697,4 @@ mod tests {

assert_eq!(cmd, cmd_struct.into());
}
}
}
21 changes: 9 additions & 12 deletions rust/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#[cfg(feature = "std")]
use crate::file::{impl_osdp_file_ops_for, OsdpFile, OsdpFileOps};
use crate::{
commands::OsdpCommand, events::OsdpEvent, OsdpError, OsdpFlag, PdCapability, PdId,
PdInfo,
commands::OsdpCommand, events::OsdpEvent, OsdpError, OsdpFlag, PdCapability, PdId, PdInfo,
};
use alloc::vec::Vec;
use core::ffi::c_void;
Expand Down Expand Up @@ -57,9 +56,7 @@ where
}

fn cp_setup(info: Vec<libosdp_sys::osdp_pd_info_t>) -> Result<*mut c_void> {
let ctx = unsafe {
libosdp_sys::osdp_cp_setup(info.len() as i32, info.as_ptr())
};
let ctx = unsafe { libosdp_sys::osdp_cp_setup(info.len() as i32, info.as_ptr()) };
if ctx.is_null() {
Err(OsdpError::Setup)
} else {
Expand Down Expand Up @@ -100,14 +97,14 @@ impl ControlPanel {
/// ```
pub fn new(pd_info: Vec<PdInfo>) -> Result<Self> {
if pd_info.len() > 126 {
return Err(OsdpError::PdInfo("max PD count exceeded"))
return Err(OsdpError::PdInfo("max PD count exceeded"));
}
let info: Vec<libosdp_sys::osdp_pd_info_t> = pd_info
.iter()
.map(|i| { i.as_struct() })
.collect();
let info: Vec<libosdp_sys::osdp_pd_info_t> =
pd_info.iter().map(|i| i.as_struct()).collect();
unsafe { libosdp_sys::osdp_set_log_callback(Some(log_handler)) };
Ok(Self { ctx: cp_setup(info)? })
Ok(Self {
ctx: cp_setup(info)?,
})
}

/// The application must call this method periodically to refresh the
Expand Down Expand Up @@ -205,4 +202,4 @@ impl Drop for ControlPanel {
}

#[cfg(feature = "std")]
impl_osdp_file_ops_for!(ControlPanel);
impl_osdp_file_ops_for!(ControlPanel);
41 changes: 25 additions & 16 deletions rust/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ impl Into<u32> for OsdpCardFormats {
}
OsdpCardFormats::Weigand => {
libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_WIEGAND
},
OsdpCardFormats::Ascii => {
libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII
},
}
OsdpCardFormats::Ascii => libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII,
}
}
}
Expand Down Expand Up @@ -93,21 +91,21 @@ impl OsdpEventCardRead {
format: OsdpCardFormats::Ascii,
direction: false,
nr_bits: 0,
data
data,
}
}

/// Create a Weigand card read event for self and direction set to forward
pub fn new_weigand(nr_bits: usize, data: Vec<u8>) -> Result<Self> {
if nr_bits > data.len() * 8 {
return Err(OsdpError::Command)
return Err(OsdpError::Command);
}
Ok(Self {
reader_no: 0,
format: OsdpCardFormats::Weigand,
direction: false,
nr_bits,
data
data,
})
}
}
Expand Down Expand Up @@ -258,12 +256,18 @@ pub struct OsdpEventIO {
impl OsdpEventIO {
/// Create an input event with given bit mask
pub fn new_input(mask: u32) -> Self {
Self { type_: 0, status: mask }
Self {
type_: 0,
status: mask,
}
}

/// Create an output event with given bit mask
pub fn new_output(mask: u32) -> Self {
Self { type_: 0, status: mask }
Self {
type_: 0,
status: mask,
}
}
}

Expand Down Expand Up @@ -365,7 +369,7 @@ impl From<OsdpEvent> for libosdp_sys::osdp_event {
OsdpEvent::IO(e) => libosdp_sys::osdp_event {
type_: libosdp_sys::osdp_event_type_OSDP_EVENT_IO,
__bindgen_anon_1: libosdp_sys::osdp_event__bindgen_ty_1 {
io: e.clone().into()
io: e.clone().into(),
},
},
OsdpEvent::Status(e) => libosdp_sys::osdp_event {
Expand Down Expand Up @@ -403,12 +407,11 @@ impl From<libosdp_sys::osdp_event> for OsdpEvent {

#[cfg(test)]
mod tests {
use super::OsdpEventCardRead;
use libosdp_sys::{
osdp_event_cardread,
osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII,
osdp_event_cardread, osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII,
osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_WIEGAND,
};
use super::OsdpEventCardRead;

#[test]
fn test_event_cardread() {
Expand All @@ -417,7 +420,10 @@ mod tests {

assert_eq!(event_struct.length, 2);
assert_eq!(event_struct.direction, 0);
assert_eq!(event_struct.format, osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII);
assert_eq!(
event_struct.format,
osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII
);
assert_eq!(event_struct.data[0], 0x55);
assert_eq!(event_struct.data[1], 0xAA);

Expand All @@ -428,10 +434,13 @@ mod tests {

assert_eq!(event_struct.length, 15);
assert_eq!(event_struct.direction, 0);
assert_eq!(event_struct.format, osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_WIEGAND);
assert_eq!(
event_struct.format,
osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_WIEGAND
);
assert_eq!(event_struct.data[0], 0x55);
assert_eq!(event_struct.data[1], 0xAA);

assert_eq!(event, event_struct.into());
}
}
}
9 changes: 7 additions & 2 deletions rust/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ impl OsdpFile {
/// CP and PD.
/// * `path` - Path to file to read from (CP) or write to (PD).
pub fn new(id: i32, path: PathBuf) -> Self {
Self { id, path, file: None, size: 0, }
Self {
id,
path,
file: None,
size: 0,
}
}

/// For internal use by {cp,pd}.register_file() methods.
Expand Down Expand Up @@ -154,4 +159,4 @@ macro_rules! impl_osdp_file_ops_for {
}
)+)
}
pub(crate) use impl_osdp_file_ops_for;
pub(crate) use impl_osdp_file_ops_for;
Loading

0 comments on commit 6223def

Please sign in to comment.