Skip to content

Commit 60527e6

Browse files
added getter methods to get information from an PdInfo
1 parent 2840552 commit 60527e6

File tree

1 file changed

+126
-11
lines changed

1 file changed

+126
-11
lines changed

libosdp/src/pdinfo.rs

Lines changed: 126 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,129 @@ use crate::{Channel, OsdpError, OsdpFlag, PdCapability, PdId};
1010
/// OSDP PD Information. This struct is used to describe a PD to LibOSDP
1111
#[derive(Debug, Default)]
1212
pub struct PdInfo {
13-
pub name: CString,
14-
pub address: i32,
15-
pub baud_rate: i32,
16-
pub flags: OsdpFlag,
17-
pub id: PdId,
18-
pub cap: Vec<libosdp_sys::osdp_pd_cap>,
19-
pub channel: Option<Box<dyn Channel>>,
20-
pub scbk: Option<[u8; 16]>,
13+
name: CString,
14+
address: i32,
15+
baud_rate: i32,
16+
flags: OsdpFlag,
17+
id: PdId,
18+
cap: Vec<libosdp_sys::osdp_pd_cap>,
19+
channel: Option<Box<dyn Channel>>,
20+
scbk: Option<[u8; 16]>,
21+
}
22+
impl PdInfo{
23+
/// Gets the PDs `name`
24+
/// A user provided `name` for this PD (log messages include this name defaults to `pd-<address>`)
25+
///
26+
/// # Example
27+
/// ```
28+
/// # use libosdp::PdInfoBuilder;
29+
/// let pd = PdInfoBuilder::new().name("").unwrap().build();
30+
/// assert_eq!(pd.name(), "door_42".to_string());
31+
/// ```
32+
#[must_use]
33+
pub fn name(&self)->String{
34+
self.name
35+
.clone()
36+
.into_string().expect("since this is configured with a &str, this must be valid String")
37+
}
38+
/// Gets the PDs 7 bit `address`
39+
/// The special address 0x7F is used for broadcast.
40+
/// So there can be 2^7-1 valid addresses on a bus.
41+
///
42+
/// # Example
43+
/// ```
44+
/// # use libosdp::PdInfoBuilder;
45+
/// let pd = PdInfoBuilder::new().address(42).unwrap().build();
46+
/// assert_eq!(pd.address(), 42);
47+
/// ```
48+
#[must_use]
49+
pub fn address(&self)->i32{
50+
self.address
51+
}
52+
53+
54+
/// Gets the PDs baud rate.
55+
/// Can be one of `9600`/`19200`/`38400`/`57600`/`115200`/`230400`
56+
///
57+
/// # Example
58+
/// ```
59+
/// # use libosdp::PdInfoBuilder;
60+
/// let pd = PdInfoBuilder::new().baud_rate(9600).unwrap().build();
61+
/// assert_eq!(pd.baud_rate(), 9600);
62+
/// ```
63+
pub fn baud_rate(&self) -> i32 {
64+
self.baud_rate
65+
}
66+
67+
/// Gets the PDs [`OsdpFlag`]
68+
/// Used to modify the way the context is set up
69+
///
70+
/// # Example
71+
/// ```
72+
/// # use libosdp::{OsdpFlag, PdInfoBuilder};
73+
/// let pd = PdInfoBuilder::new().flag(OsdpFlag::EnforceSecure).build();
74+
/// assert_eq!(pd.flag(), OsdpFlag::EnforceSecure);
75+
/// ```
76+
#[must_use]
77+
pub fn flag(&self) -> OsdpFlag {
78+
self.flags
79+
}
80+
81+
/// Gets the PDs' [`PdId`]
82+
/// Static information that the PD reports to the CP when it received a `CMD_ID`.
83+
/// For CP mode, this field is ignored, but PD mode must set it
84+
///
85+
/// # Example
86+
/// ```
87+
/// # use libosdp::{PdId, PdInfoBuilder};
88+
/// let pd = PdInfoBuilder::new().id(&PdId::from_number(42)).build();
89+
/// assert_eq!(pd.id(), PdId::from_number(42));
90+
/// ```
91+
#[must_use]
92+
pub fn id(&self) -> PdId {
93+
self.id
94+
}
95+
96+
/// Get a PDs [`PdCapability`]s
97+
///
98+
/// # Example
99+
/// ```
100+
/// # use libosdp::{PdCapability, PdInfoBuilder, PdCapEntity};
101+
/// let pd = PdInfoBuilder::new()
102+
/// .capability(PdCapability::CommunicationSecurity(PdCapEntity::new(1, 1)))
103+
/// .capability(PdCapability::AudibleOutput(PdCapEntity::new(1, 1)))
104+
/// .build();
105+
/// assert_eq!(
106+
/// pd.capabilities(),
107+
/// vec![PdCapability::CommunicationSecurity(PdCapEntity::new(1, 1)), PdCapability::AudibleOutput(PdCapEntity::new(1, 1))]
108+
/// );
109+
/// ```
110+
#[must_use]
111+
pub fn capabilities(&self) -> Vec<PdCapability> {
112+
self.cap.iter().cloned()
113+
.map(PdCapability::into)
114+
.collect()
115+
}
116+
117+
/// Get a PDs secure channel key.
118+
/// If the key is not set, the PD will be set to install mode.
119+
///
120+
/// # Example
121+
/// ```
122+
/// # use libosdp::PdInfoBuilder;
123+
/// # #[rustfmt::skip]
124+
/// # let pd_0_key = [
125+
/// # 0x94, 0x4b, 0x8e, 0xdd, 0xcb, 0xaa, 0x2b, 0x5f,
126+
/// # 0xe2, 0xb0, 0x14, 0x8d, 0x1b, 0x2f, 0x95, 0xc9
127+
/// # ];
128+
/// let pd = PdInfoBuilder::new().secure_channel_key(pd_0_key).build();
129+
/// assert_eq!(pd.secure_channel_key(), Some(pd_0_key));
130+
/// ```
131+
132+
#[must_use]
133+
pub fn secure_channel_key(&self) -> Option<[u8; 16]> {
134+
self.scbk
135+
}
21136
}
22137

23138
/// OSDP PD Info Builder
@@ -57,7 +172,7 @@ impl PdInfoBuilder {
57172
Ok(self)
58173
}
59174

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

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

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

0 commit comments

Comments
 (0)