Skip to content

Commit

Permalink
added getter methods to get information from an PdInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Aug 18, 2024
1 parent 2840552 commit 60527e6
Showing 1 changed file with 126 additions and 11 deletions.
137 changes: 126 additions & 11 deletions libosdp/src/pdinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,129 @@ use crate::{Channel, OsdpError, OsdpFlag, PdCapability, PdId};
/// OSDP PD Information. This struct is used to describe a PD to LibOSDP
#[derive(Debug, Default)]
pub struct PdInfo {
pub name: CString,
pub address: i32,
pub baud_rate: i32,
pub flags: OsdpFlag,
pub id: PdId,
pub cap: Vec<libosdp_sys::osdp_pd_cap>,
pub channel: Option<Box<dyn Channel>>,
pub scbk: Option<[u8; 16]>,
name: CString,
address: i32,
baud_rate: i32,
flags: OsdpFlag,
id: PdId,
cap: Vec<libosdp_sys::osdp_pd_cap>,
channel: Option<Box<dyn Channel>>,
scbk: Option<[u8; 16]>,
}
impl PdInfo{
/// Gets the PDs `name`
/// A user provided `name` for this PD (log messages include this name defaults to `pd-<address>`)
///
/// # Example
/// ```
/// # use libosdp::PdInfoBuilder;
/// let pd = PdInfoBuilder::new().name("").unwrap().build();
/// assert_eq!(pd.name(), "door_42".to_string());
/// ```
#[must_use]
pub fn name(&self)->String{
self.name
.clone()
.into_string().expect("since this is configured with a &str, this must be valid String")
}
/// Gets the PDs 7 bit `address`
/// The special address 0x7F is used for broadcast.
/// So there can be 2^7-1 valid addresses on a bus.
///
/// # Example
/// ```
/// # use libosdp::PdInfoBuilder;
/// let pd = PdInfoBuilder::new().address(42).unwrap().build();
/// assert_eq!(pd.address(), 42);
/// ```
#[must_use]
pub fn address(&self)->i32{
self.address
}


/// Gets the PDs baud rate.
/// Can be one of `9600`/`19200`/`38400`/`57600`/`115200`/`230400`
///
/// # Example
/// ```
/// # use libosdp::PdInfoBuilder;
/// let pd = PdInfoBuilder::new().baud_rate(9600).unwrap().build();
/// assert_eq!(pd.baud_rate(), 9600);
/// ```
pub fn baud_rate(&self) -> i32 {
self.baud_rate
}

/// Gets the PDs [`OsdpFlag`]
/// Used to modify the way the context is set up
///
/// # Example
/// ```
/// # use libosdp::{OsdpFlag, PdInfoBuilder};
/// let pd = PdInfoBuilder::new().flag(OsdpFlag::EnforceSecure).build();
/// assert_eq!(pd.flag(), OsdpFlag::EnforceSecure);
/// ```
#[must_use]
pub fn flag(&self) -> OsdpFlag {
self.flags
}

/// Gets the PDs' [`PdId`]
/// Static information that the PD reports to the CP when it received a `CMD_ID`.
/// For CP mode, this field is ignored, but PD mode must set it
///
/// # Example
/// ```
/// # use libosdp::{PdId, PdInfoBuilder};
/// let pd = PdInfoBuilder::new().id(&PdId::from_number(42)).build();
/// assert_eq!(pd.id(), PdId::from_number(42));
/// ```
#[must_use]
pub fn id(&self) -> PdId {
self.id
}

/// Get a PDs [`PdCapability`]s
///
/// # Example
/// ```
/// # use libosdp::{PdCapability, PdInfoBuilder, PdCapEntity};
/// let pd = PdInfoBuilder::new()
/// .capability(PdCapability::CommunicationSecurity(PdCapEntity::new(1, 1)))
/// .capability(PdCapability::AudibleOutput(PdCapEntity::new(1, 1)))
/// .build();
/// assert_eq!(
/// pd.capabilities(),
/// vec![PdCapability::CommunicationSecurity(PdCapEntity::new(1, 1)), PdCapability::AudibleOutput(PdCapEntity::new(1, 1))]
/// );
/// ```
#[must_use]
pub fn capabilities(&self) -> Vec<PdCapability> {
self.cap.iter().cloned()
.map(PdCapability::into)

Check failure on line 113 in libosdp/src/pdinfo.rs

View workflow job for this annotation

GitHub Actions / test

type mismatch in function arguments

Check failure on line 113 in libosdp/src/pdinfo.rs

View workflow job for this annotation

GitHub Actions / lint

type mismatch in function arguments
.collect()

Check failure on line 114 in libosdp/src/pdinfo.rs

View workflow job for this annotation

GitHub Actions / test

the method `collect` exists for struct `Map<Cloned<Iter<'_, osdp_pd_cap>>, fn(PdCapability) -> _ {<PdCapability as Into<_>>::into}>`, but its trait bounds were not satisfied

Check failure on line 114 in libosdp/src/pdinfo.rs

View workflow job for this annotation

GitHub Actions / lint

the method `collect` exists for struct `Map<Cloned<Iter<'_, osdp_pd_cap>>, fn(PdCapability) -> _ {<PdCapability as Into<_>>::into}>`, but its trait bounds were not satisfied
}

/// Get a PDs secure channel key.
/// If the key is not set, the PD will be set to install mode.
///
/// # Example
/// ```
/// # use libosdp::PdInfoBuilder;
/// # #[rustfmt::skip]
/// # let pd_0_key = [
/// # 0x94, 0x4b, 0x8e, 0xdd, 0xcb, 0xaa, 0x2b, 0x5f,
/// # 0xe2, 0xb0, 0x14, 0x8d, 0x1b, 0x2f, 0x95, 0xc9
/// # ];
/// let pd = PdInfoBuilder::new().secure_channel_key(pd_0_key).build();
/// assert_eq!(pd.secure_channel_key(), Some(pd_0_key));
/// ```
#[must_use]
pub fn secure_channel_key(&self) -> Option<[u8; 16]> {
self.scbk
}
}

/// OSDP PD Info Builder
Expand Down Expand Up @@ -57,7 +172,7 @@ impl PdInfoBuilder {
Ok(self)
}

/// Set baud rate; can be one of 9600/19200/38400/57600/115200/230400
/// Set baud rate; can be one of `9600`/`19200`/`38400`/`57600`/`115200`/`230400`
pub fn baud_rate(mut self, baud_rate: i32) -> Result<PdInfoBuilder, OsdpError> {
if baud_rate != 9600
&& baud_rate != 19200
Expand All @@ -80,7 +195,7 @@ impl PdInfoBuilder {

/// Set PD ID; Static information that the PD reports to the CP when it
/// received a `CMD_ID`. For CP mode, this field is ignored, but PD mode
/// must set
/// must set it
pub fn id(mut self, id: &PdId) -> PdInfoBuilder {
self.id = *id;
self
Expand Down Expand Up @@ -108,7 +223,7 @@ impl PdInfoBuilder {
self
}

/// Set secure channel key. If the key is not set, the PD will be be set to
/// Set secure channel key. If the key is not set, the PD will be set to
/// install mode.
pub fn secure_channel_key(mut self, key: [u8; 16]) -> PdInfoBuilder {
self.scbk = Some(key);
Expand Down

0 comments on commit 60527e6

Please sign in to comment.