| 17 | } |
| 18 | |
| 19 | pub fn handle( |
| 20 | dev: &mut device::Device, |
| 21 | block: &lrwn::MACCommandSet, |
| 22 | pending: Option<&lrwn::MACCommandSet>, |
| 23 | ) -> Result<Option<lrwn::MACCommandSet>> { |
| 24 | let ds = dev.get_device_session_mut()?; |
| 25 | |
| 26 | if pending.is_none() { |
| 27 | return Err(anyhow!("Expected pending RxParamSetupReq")); |
| 28 | } |
| 29 | |
| 30 | let req_mac = (**pending.unwrap()) |
| 31 | .first() |
| 32 | .ok_or_else(|| anyhow!("MACCommandSet is empty"))?; |
| 33 | let ans_mac = (**block) |
| 34 | .first() |
| 35 | .ok_or_else(|| anyhow!("MACCommandSet is empty"))?; |
| 36 | |
| 37 | let req_pl = if let lrwn::MACCommand::RxParamSetupReq(pl) = req_mac { |
| 38 | pl |
| 39 | } else { |
| 40 | return Err(anyhow!("RxParamSetupReq expected")); |
| 41 | }; |
| 42 | let ans_pl = if let lrwn::MACCommand::RxParamSetupAns(pl) = ans_mac { |
| 43 | pl |
| 44 | } else { |
| 45 | return Err(anyhow!("RxParamSetupAns expected")); |
| 46 | }; |
| 47 | |
| 48 | if ans_pl.channel_ack && ans_pl.rx1_dr_offset_ack && ans_pl.rx2_dr_ack { |
| 49 | // Reset the error-counter. |
| 50 | ds.mac_command_error_count |
| 51 | .remove(&(lrwn::CID::RxParamSetupReq.to_u8() as u32)); |
| 52 | |
| 53 | ds.rx2_frequency = req_pl.frequency; |
| 54 | ds.rx2_dr = req_pl.dl_settings.rx2_dr as u32; |
| 55 | ds.rx1_dr_offset = req_pl.dl_settings.rx1_dr_offset as u32; |
| 56 | |
| 57 | info!(dev_eui = %dev.dev_eui, rx2_freq = req_pl.frequency, rx2_dr = req_pl.dl_settings.rx2_dr, rx1_dr_offset = req_pl.dl_settings.rx1_dr_offset, "RxParamSetupReq acknowledged"); |
| 58 | } else { |
| 59 | let count = ds |
| 60 | .mac_command_error_count |
| 61 | .entry(lrwn::CID::RxParamSetupReq.to_u8() as u32) |
| 62 | .or_insert(0); |
| 63 | *count += 1; |
| 64 | warn!(dev_eui = %dev.dev_eui, rx2_freq = req_pl.frequency, rx2_dr = req_pl.dl_settings.rx2_dr, rx1_dr_offset = req_pl.dl_settings.rx1_dr_offset, "RxParamSetupReq not acknowledged"); |
| 65 | } |
| 66 | |
| 67 | Ok(None) |
| 68 | } |
| 69 | |
| 70 | #[cfg(test)] |
| 71 | pub mod test { |