| 64 | } |
| 65 | |
| 66 | pub fn handle( |
| 67 | dev: &mut device::Device, |
| 68 | block: &lrwn::MACCommandSet, |
| 69 | pending: Option<&lrwn::MACCommandSet>, |
| 70 | region_conf: Arc<Box<dyn lrwn::region::Region + Send + Sync>>, |
| 71 | ) -> Result<Option<lrwn::MACCommandSet>> { |
| 72 | let dev_eui = dev.dev_eui; |
| 73 | let ds = dev.get_device_session_mut()?; |
| 74 | |
| 75 | if pending.is_none() { |
| 76 | return Err(anyhow!("Expected pending NewChannelReq")); |
| 77 | } |
| 78 | |
| 79 | let block_macs = &**block; |
| 80 | let pending_macs = &**pending.unwrap(); |
| 81 | |
| 82 | if block_macs.len() != pending_macs.len() { |
| 83 | return Err(anyhow!( |
| 84 | "Requested number of NewChannelReq items does not match NewChannelAns items" |
| 85 | )); |
| 86 | } |
| 87 | |
| 88 | for (i, ans_mac) in block_macs.iter().enumerate() { |
| 89 | let ans_pl = if let lrwn::MACCommand::NewChannelAns(ans_pl) = &ans_mac { |
| 90 | ans_pl |
| 91 | } else { |
| 92 | return Err(anyhow!("Expected NewChannelAns")); |
| 93 | }; |
| 94 | |
| 95 | let req_pl = if let lrwn::MACCommand::NewChannelReq(req_pl) = &pending_macs[i] { |
| 96 | req_pl |
| 97 | } else { |
| 98 | return Err(anyhow!("Expected NewChannelReq")); |
| 99 | }; |
| 100 | |
| 101 | if ans_pl.channel_freq_ok && ans_pl.dr_range_ok { |
| 102 | // Reset the error-counter. |
| 103 | ds.mac_command_error_count |
| 104 | .remove(&(lrwn::CID::NewChannelReq.to_u8() as u32)); |
| 105 | |
| 106 | let data_rates = region_conf |
| 107 | .get_data_rates_for_new_channel_req_dr_range(req_pl.min_dr, req_pl.max_dr)?; |
| 108 | |
| 109 | ds.extra_uplink_channels.insert( |
| 110 | req_pl.ch_index as u32, |
| 111 | internal::DeviceSessionChannel { |
| 112 | frequency: req_pl.freq, |
| 113 | data_rates: data_rates.into_iter().map(|v| v as u32).collect(), |
| 114 | ..Default::default() |
| 115 | }, |
| 116 | ); |
| 117 | |
| 118 | if !ds |
| 119 | .enabled_uplink_channel_indices |
| 120 | .contains(&(req_pl.ch_index as u32)) |
| 121 | { |
| 122 | ds.enabled_uplink_channel_indices |
| 123 | .push(req_pl.ch_index as u32); |