(
phy: &mut lrwn::PhyPayload,
tx_dr: u8,
tx_ch: u8,
)
| 503 | } |
| 504 | |
| 505 | pub async fn get_for_phypayload( |
| 506 | phy: &mut lrwn::PhyPayload, |
| 507 | tx_dr: u8, |
| 508 | tx_ch: u8, |
| 509 | ) -> Result<Device, Error> { |
| 510 | // Get the dev_addr and original f_cnt. |
| 511 | let (dev_addr, f_cnt_orig) = if let lrwn::Payload::MACPayload(pl) = &phy.payload { |
| 512 | (pl.fhdr.devaddr, pl.fhdr.f_cnt) |
| 513 | } else { |
| 514 | return Err(Error::InvalidPayload("MacPayload".to_string())); |
| 515 | }; |
| 516 | |
| 517 | let mut devices: Vec<Device> = device::dsl::device |
| 518 | .filter( |
| 519 | device::dsl::dev_addr |
| 520 | .eq(&dev_addr) |
| 521 | .or(device::dsl::secondary_dev_addr.eq(&dev_addr)), |
| 522 | ) |
| 523 | .filter(device::dsl::is_disabled.eq(false)) |
| 524 | .load(&mut get_async_db_conn().await?) |
| 525 | .await?; |
| 526 | |
| 527 | if devices.is_empty() { |
| 528 | return Err(Error::NotFound(dev_addr.to_string())); |
| 529 | } |
| 530 | |
| 531 | for d in &mut devices { |
| 532 | let mut sessions = vec![]; |
| 533 | |
| 534 | if let Some(ds) = &d.device_session { |
| 535 | sessions.push(ds.clone()); |
| 536 | if let Some(ds) = &ds.pending_rejoin_device_session { |
| 537 | sessions.push(ds.as_ref().into()); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | for ds in &mut sessions { |
| 542 | // Set the d.f_cnt_up if it is 0. |
| 543 | if d.f_cnt_up == 0 { |
| 544 | d.f_cnt_up = ds.deprecated_f_cnt_up.into(); |
| 545 | } |
| 546 | |
| 547 | // TODO: Also validate region_config_id? |
| 548 | if ds.dev_addr != dev_addr.to_vec() { |
| 549 | continue; |
| 550 | } |
| 551 | |
| 552 | // Get the full 32bit frame-counter. |
| 553 | let full_f_cnt = get_full_f_cnt_up(d.f_cnt_up as u32, f_cnt_orig); |
| 554 | let f_nwk_s_int_key = lrwn::AES128Key::from_slice(&ds.f_nwk_s_int_key)?; |
| 555 | let s_nwk_s_int_key = lrwn::AES128Key::from_slice(&ds.s_nwk_s_int_key)?; |
| 556 | |
| 557 | // Set the full f_cnt |
| 558 | if let lrwn::Payload::MACPayload(pl) = &mut phy.payload { |
| 559 | pl.fhdr.f_cnt = full_f_cnt; |
| 560 | } |
| 561 | |
| 562 | let mic_ok = phy |
no test coverage detected