lrwn-filters: Expose is_match method on prefix types.

This commit is contained in:
Orne Brocaar 2024-10-17 15:41:20 +01:00
parent d69f18edad
commit d5929e0523

View File

@ -32,7 +32,7 @@ pub fn matches(phy_payload: &[u8], config: &Filters) -> bool {
let mhdr = phy_payload[0]; let mhdr = phy_payload[0];
let m_type = mhdr >> 5; let m_type = mhdr >> 5;
let dev_addr: Option<u32> = match m_type { let dev_addr: Option<[u8; 4]> = match m_type {
// DataUp // DataUp
0x02 | 0x04 => { 0x02 | 0x04 => {
// MHDR + DevAddr // MHDR + DevAddr
@ -40,7 +40,7 @@ pub fn matches(phy_payload: &[u8], config: &Filters) -> bool {
if phy_payload.len() >= 5 { if phy_payload.len() >= 5 {
let mut dev_addr: [u8; 4] = [0; 4]; let mut dev_addr: [u8; 4] = [0; 4];
dev_addr.clone_from_slice(&phy_payload[1..5]); dev_addr.clone_from_slice(&phy_payload[1..5]);
Some(u32::from_le_bytes(dev_addr)) Some(dev_addr)
} else { } else {
None None
} }
@ -48,7 +48,7 @@ pub fn matches(phy_payload: &[u8], config: &Filters) -> bool {
_ => None, _ => None,
}; };
let join_eui: Option<u64> = match m_type { let join_eui: Option<[u8; 8]> = match m_type {
// JoinRequest // JoinRequest
0x00 => { 0x00 => {
// MHDR + JoinEUI + DevEUI // MHDR + JoinEUI + DevEUI
@ -56,7 +56,7 @@ pub fn matches(phy_payload: &[u8], config: &Filters) -> bool {
if phy_payload.len() >= 17 { if phy_payload.len() >= 17 {
let mut join_eui: [u8; 8] = [0; 8]; let mut join_eui: [u8; 8] = [0; 8];
join_eui.clone_from_slice(&phy_payload[1..9]); join_eui.clone_from_slice(&phy_payload[1..9]);
Some(u64::from_le_bytes(join_eui)) Some(join_eui)
} else { } else {
None None
} }
@ -76,8 +76,7 @@ pub fn matches(phy_payload: &[u8], config: &Filters) -> bool {
} }
for p in &config.dev_addr_prefixes { for p in &config.dev_addr_prefixes {
let prefix = u32::from_be_bytes(p.prefix()); if p.is_match(dev_addr) {
if dev_addr >> (32 - p.size()) == prefix >> (32 - p.size()) {
return true; return true;
} }
} }
@ -89,8 +88,7 @@ pub fn matches(phy_payload: &[u8], config: &Filters) -> bool {
} }
for p in &config.join_eui_prefixes { for p in &config.join_eui_prefixes {
let prefix = u64::from_be_bytes(p.prefix()); if p.is_match(join_eui) {
if join_eui >> (64 - p.size()) == prefix >> (64 - p.size()) {
return true; return true;
} }
} }
@ -108,6 +106,12 @@ impl DevAddrPrefix {
DevAddrPrefix(prefix, size) DevAddrPrefix(prefix, size)
} }
pub fn is_match(&self, dev_addr_le: [u8; 4]) -> bool {
let dev_addr = u32::from_le_bytes(dev_addr_le);
let prefix = u32::from_be_bytes(self.prefix());
dev_addr >> (32 - self.size()) == prefix >> (32 - self.size())
}
fn prefix(&self) -> [u8; 4] { fn prefix(&self) -> [u8; 4] {
self.0 self.0
} }
@ -204,6 +208,14 @@ impl EuiPrefix {
EuiPrefix(prefix, size) EuiPrefix(prefix, size)
} }
pub fn is_match(&self, eui_le: [u8; 8]) -> bool {
let eui = u64::from_le_bytes(eui_le);
let prefix = u64::from_be_bytes(self.prefix());
println!("EUI: {}", eui >> (64 - self.size()));
println!("PREFIX: {}", prefix >> (64 - self.size()));
eui >> (64 - self.size()) == prefix >> (64 - self.size())
}
fn prefix(&self) -> [u8; 8] { fn prefix(&self) -> [u8; 8] {
self.0 self.0
} }