Skip to content

Commit

Permalink
Update hop_count implementation.
Browse files Browse the repository at this point in the history
This makes the min / max value 1 - 8 (000 = 1, 111 = 8). While this is
not an official feature yet, this makes sure that in the future we can
use up to 8 hops. This also changes the initial hop_count to 1. Before
it was set to 0, and then in the metadata we would use hop_count + 1.
  • Loading branch information
brocaar committed Mar 20, 2024
1 parent 704be03 commit 89148dd
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Default for Relay {
filters: Filters::default(),
border_gateway: false,
border_gateway_ignore_direct_uplinks: false,
max_hop_count: 0,
max_hop_count: 1,
}
}
}
Expand Down
51 changes: 32 additions & 19 deletions src/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl fmt::Display for RelayPacket {
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct MHDR {
pub payload_type: PayloadType,
pub hop_count: u8,
pub hop_count: u8, // 000 = 1, ... 111 = 8
}

impl MHDR {
Expand All @@ -100,16 +100,20 @@ impl MHDR {

Ok(MHDR {
payload_type: PayloadType::from_byte((b >> 3) & 0x03)?,
hop_count: b & 0x07,
hop_count: (b & 0x07) + 1,
})
}

pub fn to_byte(&self) -> Result<u8> {
if self.hop_count > 7 {
return Err(anyhow!("Max hop_count is 7"));
if self.hop_count == 0 {
return Err(anyhow!("Min hop_count is 1"));
}

Ok(0x07 << 5 | self.payload_type.to_byte() << 3 | self.hop_count)
if self.hop_count > 8 {
return Err(anyhow!("Max hop_count is 8"));
}

Ok(0x07 << 5 | self.payload_type.to_byte() << 3 | (self.hop_count - 1))
}
}

Expand Down Expand Up @@ -385,19 +389,19 @@ mod test {
let tests = vec![
Test {
name: "uplink + hop count 3".to_string(),
byte: 0xe3,
byte: 0xe2,
expected_mhdr: Some(MHDR {
payload_type: PayloadType::Uplink,
hop_count: 3,
}),
expected_error: None,
},
Test {
name: "downlink + hop count 7".to_string(),
name: "downlink + hop count 8".to_string(),
byte: 0xef,
expected_mhdr: Some(MHDR {
payload_type: PayloadType::Downlink,
hop_count: 7,
hop_count: 8,
}),
expected_error: None,
},
Expand Down Expand Up @@ -437,14 +441,14 @@ mod test {
payload_type: PayloadType::Uplink,
hop_count: 3,
},
expected_byte: Some(0xe3),
expected_byte: Some(0xe2),
expected_error: None,
},
Test {
name: "downlink + hop count 7".to_string(),
name: "downlink + hop count 8".to_string(),
mhdr: MHDR {
payload_type: PayloadType::Downlink,
hop_count: 7,
hop_count: 8,
},
expected_byte: Some(0xef),
expected_error: None,
Expand All @@ -453,10 +457,19 @@ mod test {
name: "hop count exceeds max value".to_string(),
mhdr: MHDR {
payload_type: PayloadType::Uplink,
hop_count: 8,
hop_count: 9,
},
expected_byte: None,
expected_error: Some("Max hop_count is 8".into()),
},
Test {
name: "hop count is 0".to_string(),
mhdr: MHDR {
payload_type: PayloadType::Uplink,
hop_count: 0,
},
expected_byte: None,
expected_error: Some("Max hop_count is 7".into()),
expected_error: Some("Min hop_count is 1".into()),
},
];

Expand Down Expand Up @@ -825,7 +838,7 @@ mod test {
Test {
name: "uplink".into(),
bytes: vec![
0xe3, 0x40, 0x03, 0x78, 0x34, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05,
0xe2, 0x40, 0x03, 0x78, 0x34, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05,
],
expected_relay_packet: RelayPacket {
mhdr: MHDR {
Expand Down Expand Up @@ -853,7 +866,7 @@ mod test {
expected_relay_packet: RelayPacket {
mhdr: MHDR {
payload_type: PayloadType::Downlink,
hop_count: 7,
hop_count: 8,
},
payload: Payload::Downlink(DownlinkPayload {
metadata: DownlinkMetadata {
Expand Down Expand Up @@ -889,7 +902,7 @@ mod test {
Test {
name: "uplink".into(),
expected_bytes: vec![
0xe3, 0x40, 0x03, 0x78, 0x34, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05,
0xe2, 0x40, 0x03, 0x78, 0x34, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05,
],
relay_packet: RelayPacket {
mhdr: MHDR {
Expand Down Expand Up @@ -917,7 +930,7 @@ mod test {
relay_packet: RelayPacket {
mhdr: MHDR {
payload_type: PayloadType::Downlink,
hop_count: 7,
hop_count: 8,
},
payload: Payload::Downlink(DownlinkPayload {
metadata: DownlinkMetadata {
Expand Down Expand Up @@ -953,7 +966,7 @@ mod test {
Test {
name: "relay packet".into(),
bytes: vec![
0xe3, 0x40, 0x03, 0x78, 0x34, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05,
0xe2, 0x40, 0x03, 0x78, 0x34, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05,
],
expected_packet: Packet::Relay(RelayPacket {
mhdr: MHDR {
Expand Down Expand Up @@ -999,7 +1012,7 @@ mod test {
Test {
name: "relay packet".into(),
expected_bytes: vec![
0xe3, 0x40, 0x03, 0x78, 0x34, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05,
0xe2, 0x40, 0x03, 0x78, 0x34, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05,
],
packet: Packet::Relay(RelayPacket {
mhdr: MHDR {
Expand Down
11 changes: 5 additions & 6 deletions src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ async fn proxy_uplink_relay_packet(pl: &gw::UplinkFrame, packet: RelayPacket) ->
rx_info.gateway_id = hex::encode(backend::get_gateway_id().await?);

// Set metadata.
rx_info.metadata.insert(
"hop_count".to_string(),
(packet.mhdr.hop_count + 1).to_string(),
);
rx_info
.metadata
.insert("hop_count".to_string(), (packet.mhdr.hop_count).to_string());
rx_info
.metadata
.insert("relay_id".to_string(), hex::encode(relay_pl.relay_id));
Expand Down Expand Up @@ -248,7 +247,7 @@ async fn relay_uplink_lora_packet(pl: &gw::UplinkFrame) -> Result<()> {
let packet = RelayPacket {
mhdr: MHDR {
payload_type: PayloadType::Uplink,
hop_count: 0,
hop_count: 1,
},
payload: Payload::Uplink(UplinkPayload {
metadata: UplinkMetadata {
Expand Down Expand Up @@ -337,7 +336,7 @@ async fn relay_downlink_lora_packet(pl: &gw::DownlinkFrame) -> Result<gw::Downli
let packet = packets::RelayPacket {
mhdr: packets::MHDR {
payload_type: packets::PayloadType::Downlink,
hop_count: 0,
hop_count: 1,
},
payload: packets::Payload::Downlink(packets::DownlinkPayload {
phy_payload: downlink_item.phy_payload.clone(),
Expand Down
2 changes: 1 addition & 1 deletion tests/border_gateway_downlink_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn test_border_gateway_downlink_relay() {
packets::RelayPacket {
mhdr: packets::MHDR {
payload_type: packets::PayloadType::Downlink,
hop_count: 0,
hop_count: 1,
},
payload: packets::Payload::Downlink(packets::DownlinkPayload {
metadata: packets::DownlinkMetadata {
Expand Down
2 changes: 1 addition & 1 deletion tests/border_gateway_uplink_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn test_border_gateway_uplink_relay() {
let packet = packets::RelayPacket {
mhdr: packets::MHDR {
payload_type: packets::PayloadType::Uplink,
hop_count: 0,
hop_count: 1,
},
payload: packets::Payload::Uplink(packets::UplinkPayload {
metadata: packets::UplinkMetadata {
Expand Down
4 changes: 2 additions & 2 deletions tests/relay_gateway_downlink_lora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ async fn test_relay_gateway_downlink_lora() {
let down_packet = packets::RelayPacket {
mhdr: packets::MHDR {
payload_type: packets::PayloadType::Downlink,
hop_count: 0,
hop_count: 1,
},
payload: packets::Payload::Downlink(packets::DownlinkPayload {
metadata: packets::DownlinkMetadata {
uplink_id: uplink_id,
uplink_id,
dr: 0,
frequency: 867100000,
tx_power: 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/relay_gateway_uplink_lora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async fn test_relay_gateway_uplink_lora() {
packets::RelayPacket {
mhdr: packets::MHDR {
payload_type: packets::PayloadType::Uplink,
hop_count: 0
hop_count: 1,
},
payload: packets::Payload::Uplink(packets::UplinkPayload {
metadata: packets::UplinkMetadata {
Expand Down

0 comments on commit 89148dd

Please sign in to comment.