Skip to content

Commit 6a7fb8a

Browse files
committed
Implement CRC forwarding options.
By default, only uplinks with CRC OK are forwarded but these options make it possible to also (or only) forward uplinks with invalid CRC or CRC missing.
1 parent b1e131f commit 6a7fb8a

File tree

6 files changed

+58
-7
lines changed

6 files changed

+58
-7
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ edition = "2018"
99
publish = false
1010

1111
[dependencies]
12-
chirpstack_api = { version = "4.1.1", default-features = false }
12+
chirpstack_api = { version = "4.2.0-test.1", default-features = false }
1313
serde_json = "1.0"
1414
zmq = "0.10"
1515
clap = { version = "4.1", default-features = false, features = ["std", "help", "usage", "derive"] }

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ Configuration example:
5252
# the server address is a hostname.
5353
keepalive_max_failures=12
5454

55+
# Forward CRC OK.
56+
forward_crc_ok=true
57+
58+
# Forward CRC invalid.
59+
forward_crc_invalid=false
60+
61+
# Forward CRC missing.
62+
forward_crc_missing=false
63+
5564

5665
# Concentratord configuration.
5766
[concentratord]

src/config.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,27 @@ pub struct UdpForwarder {
1414
pub servers: Vec<Server>,
1515
}
1616

17-
#[derive(Deserialize, Default)]
17+
#[derive(Deserialize)]
1818
pub struct Server {
1919
pub server: String,
2020
pub keepalive_interval_secs: u64,
2121
pub keepalive_max_failures: u32,
22+
pub forward_crc_ok: bool,
23+
pub forward_crc_invalid: bool,
24+
pub forward_crc_missing: bool,
25+
}
26+
27+
impl Default for Server {
28+
fn default() -> Self {
29+
Server {
30+
server: "127.0.0.1:1700".into(),
31+
keepalive_interval_secs: 10,
32+
keepalive_max_failures: 12,
33+
forward_crc_ok: true,
34+
forward_crc_invalid: false,
35+
forward_crc_missing: false,
36+
}
37+
}
2238
}
2339

2440
#[derive(Deserialize, Default)]

src/forwarder.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::{Arc, Mutex};
44
use std::{thread, time};
55

66
use anyhow::Result;
7+
use chirpstack_api::gw;
78
use prost::Message;
89
use rand::Rng;
910

@@ -17,6 +18,9 @@ use super::structs;
1718
struct State {
1819
server: String,
1920
keepalive_interval: time::Duration,
21+
forward_crc_ok: bool,
22+
forward_crc_invalid: bool,
23+
forward_crc_missing: bool,
2024
keepalive_max_failures: u32,
2125
gateway_id: Vec<u8>,
2226
socket: UdpSocket,
@@ -121,6 +125,9 @@ pub fn start(conf: &Server, event_url: String, command_url: String, gateway_id:
121125
0 => time::Duration::from_secs(5),
122126
_ => time::Duration::from_secs(conf.keepalive_interval_secs),
123127
},
128+
forward_crc_ok: conf.forward_crc_ok,
129+
forward_crc_invalid: conf.forward_crc_invalid,
130+
forward_crc_missing: conf.forward_crc_missing,
124131
keepalive_max_failures: conf.keepalive_max_failures,
125132
gateway_id: gateway_id.clone(),
126133
push_data_token: Mutex::new(0),
@@ -374,6 +381,15 @@ fn events_stats(state: &Arc<State>, stats: chirpstack_api::gw::GatewayStats) {
374381
}
375382

376383
fn events_up(state: &Arc<State>, up: chirpstack_api::gw::UplinkFrame) {
384+
if let Some(rx_info) = &up.rx_info {
385+
if !((rx_info.crc_status() == gw::CrcStatus::CrcOk && state.forward_crc_ok)
386+
|| (rx_info.crc_status() == gw::CrcStatus::BadCrc && state.forward_crc_invalid)
387+
|| (rx_info.crc_status() == gw::CrcStatus::NoCrc && state.forward_crc_missing))
388+
{
389+
return;
390+
}
391+
}
392+
377393
let rxpk = match structs::RxPk::from_proto(&up) {
378394
Ok(v) => v,
379395
Err(err) => {

src/structs.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use chirpstack_api::gw;
1313
const PROTOCOL_VERSION: u8 = 0x02;
1414

1515
pub enum Crc {
16-
OK,
16+
Ok,
17+
Invalid,
18+
Missing,
1719
}
1820

1921
impl Serialize for Crc {
@@ -22,7 +24,9 @@ impl Serialize for Crc {
2224
S: Serializer,
2325
{
2426
match self {
25-
Crc::OK => serializer.serialize_i32(1),
27+
Crc::Ok => serializer.serialize_i32(1),
28+
Crc::Invalid => serializer.serialize_i32(-1),
29+
Crc::Missing => serializer.serialize_i32(0),
2630
}
2731
}
2832
}
@@ -249,7 +253,11 @@ impl RxPk {
249253
freq: tx_info.frequency as f64 / 1000000.0,
250254
chan: rx_info.channel,
251255
rfch: rx_info.rf_chain,
252-
stat: Crc::OK,
256+
stat: match rx_info.crc_status() {
257+
gw::CrcStatus::CrcOk => Crc::Ok,
258+
gw::CrcStatus::BadCrc => Crc::Invalid,
259+
gw::CrcStatus::NoCrc => Crc::Missing,
260+
},
253261
modu: match &tx_info.modulation {
254262
Some(v) => match &v.parameters {
255263
Some(v) => match &v {
@@ -704,6 +712,7 @@ mod tests {
704712
rf_chain: 1,
705713
antenna: 3,
706714
context: vec![1, 2, 3, 4],
715+
crc_status: gw::CrcStatus::CrcOk.into(),
707716
..Default::default()
708717
};
709718

@@ -763,6 +772,7 @@ mod tests {
763772
board: 3,
764773
antenna: 4,
765774
context: vec![1, 2, 3, 4],
775+
crc_status: gw::CrcStatus::CrcOk.into(),
766776
..Default::default()
767777
};
768778

0 commit comments

Comments
 (0)