Skip to content

Commit

Permalink
Merge pull request #28 from Sympatron/bit-count
Browse files Browse the repository at this point in the history
Fix card data length for "raw bit array"
  • Loading branch information
sidcha authored Nov 11, 2024
2 parents 2c6a271 + 93285ff commit bb0fe6e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion libosdp-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ homepage = "https://libosdp.sidcha.dev/"
readme = "README.md"
repository = "https://github.com/goToMain/libosdp-rs"
license = "Apache-2.0"
keywords = ["osdp", "libosdp", "acs", "sia", "weigand"]
keywords = ["osdp", "libosdp", "acs", "sia", "wiegand"]
categories = ["development-tools", "embedded"]

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion libosdp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ homepage = "https://libosdp.sidcha.dev/"
readme = "README.md"
repository = "https://github.com/goToMain/libosdp-rs"
license = "Apache-2.0"
keywords = ["osdp", "libosdp", "acs", "sia", "weigand"]
keywords = ["osdp", "libosdp", "acs", "sia", "wiegand"]
categories = ["development-tools", "embedded"]

[dependencies]
Expand Down
18 changes: 9 additions & 9 deletions libosdp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum OsdpCardFormats {
/// Card format is not specified
Unspecified,

/// Weigand format
/// Wiegand format
Wiegand,

/// Ascii format
Expand Down Expand Up @@ -87,7 +87,7 @@ pub struct OsdpEventCardRead {
pub direction: bool,

/// Number of valid data bits in [`OsdpEventCardRead::data`] when the card
/// format is [`OsdpCardFormats::Weigand`]. For all other formats, this
/// format is not [`OsdpCardFormats::Ascii`]. For [`OsdpCardFormats::Ascii`], this
/// field is set to zero.
pub nr_bits: usize,

Expand All @@ -107,8 +107,8 @@ impl OsdpEventCardRead {
}
}

/// 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> {
/// Create a Wiegand card read event for self and direction set to forward
pub fn new_wiegand(nr_bits: usize, data: Vec<u8>) -> Result<Self> {
if nr_bits > data.len() * 8 {
return Err(OsdpError::Command);
}
Expand All @@ -128,8 +128,8 @@ impl From<libosdp_sys::osdp_event_cardread> for OsdpEventCardRead {
let format = value.format.into();
let len = value.length as usize;
let (nr_bits, nr_bytes) = match format {
OsdpCardFormats::Wiegand => (len, (len + 7) / 8),
_ => (0, len),
OsdpCardFormats::Ascii => (0, len),
_ => (len, len.div_ceil(8)),
};
let data = value.data[0..nr_bytes].to_vec();
OsdpEventCardRead {
Expand All @@ -146,8 +146,8 @@ impl From<OsdpEventCardRead> for libosdp_sys::osdp_event_cardread {
fn from(value: OsdpEventCardRead) -> Self {
let mut data = [0; libosdp_sys::OSDP_EVENT_CARDREAD_MAX_DATALEN as usize];
let length = match value.format {
OsdpCardFormats::Wiegand => value.nr_bits as i32,
_ => value.data.len() as i32,
OsdpCardFormats::Ascii => value.data.len() as i32,
_ => value.nr_bits as i32,
};
data[..value.data.len()].copy_from_slice(&value.data[..]);
libosdp_sys::osdp_event_cardread {
Expand Down Expand Up @@ -449,7 +449,7 @@ mod tests {

assert_eq!(event, event_struct.into());

let event = OsdpEventCardRead::new_weigand(15, vec![0x55, 0xAA]).unwrap();
let event = OsdpEventCardRead::new_wiegand(15, vec![0x55, 0xAA]).unwrap();
let event_struct: osdp_event_cardread = event.clone().into();

assert_eq!(event_struct.length, 15);
Expand Down

0 comments on commit bb0fe6e

Please sign in to comment.