| 5 | use chirpstack_api::internal; |
| 6 | |
| 7 | pub fn handle( |
| 8 | dev: &mut device::Device, |
| 9 | block: &lrwn::MACCommandSet, |
| 10 | pending: Option<&lrwn::MACCommandSet>, |
| 11 | ) -> Result<Option<lrwn::MACCommandSet>> { |
| 12 | let dev_eui = dev.dev_eui; |
| 13 | let ds = dev.get_device_session_mut()?; |
| 14 | |
| 15 | if pending.is_none() { |
| 16 | return Err(anyhow!("Expected pending EndDeviceConfReq mac-command")); |
| 17 | } |
| 18 | |
| 19 | let req_mac = (**pending.unwrap()) |
| 20 | .first() |
| 21 | .ok_or_else(|| anyhow!("MACCommandSet is empty"))?; |
| 22 | let req_pl = if let lrwn::MACCommand::EndDeviceConfReq(pl) = req_mac { |
| 23 | pl |
| 24 | } else { |
| 25 | return Err(anyhow!("EndDeviceConfReq is expected")); |
| 26 | }; |
| 27 | |
| 28 | let ans_mac = (**block) |
| 29 | .first() |
| 30 | .ok_or_else(|| anyhow!("MACCommandSet is empty"))?; |
| 31 | let ans_pl = if let lrwn::MACCommand::EndDeviceConfAns(pl) = ans_mac { |
| 32 | pl |
| 33 | } else { |
| 34 | return Err(anyhow!("EndDeviceConfAns is expected")); |
| 35 | }; |
| 36 | |
| 37 | if ds.relay.is_none() { |
| 38 | ds.relay = Some(internal::Relay::default()); |
| 39 | } |
| 40 | |
| 41 | if ans_pl.second_ch_freq_ack |
| 42 | && ans_pl.second_ch_dr_ack |
| 43 | && ans_pl.second_ch_idx_ack |
| 44 | && ans_pl.backoff_ack |
| 45 | { |
| 46 | info!(dev_eui = %dev_eui, "EndDeviceConfReq acknowledged"); |
| 47 | |
| 48 | if let Some(relay) = &mut ds.relay { |
| 49 | relay.ed_activation_mode = |
| 50 | req_pl.activation_relay_mode.relay_mode_activation.to_u8() as u32; |
| 51 | relay.ed_smart_enable_level = req_pl.activation_relay_mode.smart_enable_level as u32; |
| 52 | |
| 53 | relay.second_channel_ack_offset = |
| 54 | req_pl.channel_settings_ed.second_ch_ack_offset as u32; |
| 55 | relay.second_channel_dr = req_pl.channel_settings_ed.second_ch_dr as u32; |
| 56 | relay.ed_back_off = req_pl.channel_settings_ed.backoff as u32; |
| 57 | |
| 58 | relay.second_channel_freq = req_pl.second_ch_freq; |
| 59 | } |
| 60 | } else { |
| 61 | warn!( |
| 62 | dev_eui = %dev_eui, |
| 63 | second_ch_freq_ack = ans_pl.second_ch_freq_ack, |
| 64 | second_ch_dr_ack = ans_pl.second_ch_dr_ack, |