diff --git a/Cargo.lock b/Cargo.lock index 0b0da39fe408847709c63c8435d0e34f3b1989dc..cd0b5a95638f18022b89419b8e6f1bd8ecb776c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -306,7 +306,7 @@ dependencies = [ [[package]] name = "ham-cats" version = "0.1.0" -source = "git+https://gitlab.scd31.com/cats/ham-cats/#97ff918c4e3aaa6e15dadbaa1ab9c12b1fdb5655" +source = "git+https://gitlab.scd31.com/cats/ham-cats/#81e90ddc21a4aa0e56acd05a18edb658593cf434" dependencies = [ "arrayvec", "bitvec", @@ -370,9 +370,9 @@ checksum = "23c19ea22fc166b77441be6ea377e5aa20121490cdec0af18a14356d390f45b5" [[package]] name = "libm" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" [[package]] name = "lock_api" @@ -493,9 +493,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" dependencies = [ "unicode-ident", ] diff --git a/src/radio.rs b/src/radio.rs index f1a6a7fc8e74a94bdb6c5da9d8f303a9ecd4fe06..fcc66925e333dcc6b9268ee033e78d163eadf989 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -1,8 +1,4 @@ -use ham_cats::{ - buffer::Buffer, - packet::Packet, - whisker::{Route, RouteNode}, -}; +use ham_cats::{buffer::Buffer, packet::Packet}; use rf4463::{config::RADIO_CONFIG_CATS, error::TransferError, Rf4463}; use rtic::Mutex; use stm32f4xx_hal::{ @@ -119,8 +115,8 @@ impl<'a> RadioManager<'a> { callsign: &str, ssid: u8, ) { - if should_digipeat(callsign, ssid, &packet) { - if append_to_packet_route(callsign, ssid, &mut packet).is_none() { + if packet.should_digipeat(callsign, ssid).is_ok() { + if packet.append_to_route(callsign, ssid).is_err() { return; } @@ -132,103 +128,3 @@ impl<'a> RadioManager<'a> { } } } - -pub fn should_digipeat(callsign: &str, ssid: u8, packet: &Packet<MAX_PACKET_LEN>) -> bool { - let route = match packet.route() { - Some(x) => x, - None => { - return false; - } - }; - - if let Some(ident) = packet.identification() { - if &ident.callsign == callsign && ident.ssid == ssid { - // this node is the source of the packet - return false; - } - } - - let max_hops: usize = route.max_hops.into(); - let cur_hops = route - .iter() - .filter(|r| match r { - RouteNode::Internet => false, - RouteNode::Identity(_, _, is_future) => !is_future, - }) - .count(); - - if max_hops <= cur_hops { - // already hit our max hops - return false; - } - - let already_digipeated = route.iter().any(|r| match r { - RouteNode::Internet => false, - RouteNode::Identity(rc, rs, is_future) => rc == callsign && rs == ssid && !is_future, - }); - - if already_digipeated { - // this node has already digipeated this packet - return false; - } - - let next_node = route.iter().find_map(|r| match r { - RouteNode::Identity(c, s, is_future) if is_future => Some((c, s)), - _ => None, - }); - - match next_node { - Some((rc, rs)) => rc == callsign && rs == ssid, - None => true, - } -} - -#[must_use] -pub fn append_to_packet_route( - callsign: &str, - ssid: u8, - packet: &mut Packet<MAX_PACKET_LEN>, -) -> Option<()> { - let mut route = packet.route().unwrap_or(Route::new(0)); - append_to_route(callsign, ssid, &mut route)?; - packet.clear_route(); - packet.add_route(route).ok()?; - - Some(()) -} - -#[must_use] -fn append_to_route(callsign: &str, ssid: u8, r: &mut Route) -> Option<()> { - let replace_future = r.iter().any(|rn| match rn { - RouteNode::Identity(rc, rs, is_future) => rc == callsign && rs == ssid && is_future, - _ => false, - }); - - if replace_future { - let mut new_route = Route::new(r.max_hops); - let mut already_replaced = false; - - for rn in r.iter() { - match rn { - RouteNode::Identity(rc, rs, is_future) - if rc == callsign && rs == ssid && is_future && !already_replaced => - { - already_replaced = true; - new_route.push_callsign(callsign, ssid, false)?; - } - RouteNode::Identity(rc, rs, is_future) => { - new_route.push_callsign(rc, rs, is_future)?; - } - RouteNode::Internet => { - new_route.push_internet()?; - } - } - } - - *r = new_route; - } else { - r.push_callsign(callsign, ssid, false)?; - } - - Some(()) -}