Skip to content

Commit

Permalink
Apply antenna gain just before tx instead of before enqueue.
Browse files Browse the repository at this point in the history
Closes #157.
  • Loading branch information
brocaar committed May 15, 2024
1 parent b267bef commit 5d1226e
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 62 deletions.
5 changes: 2 additions & 3 deletions chirpstack-concentratord-2g4/src/cmd/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ pub fn run(
// jit thread
threads.push(thread::spawn({
let queue = Arc::clone(&queue);
let antenna_gain_dbi = config.gateway.antenna_gain;
let stop_receive = signal_pool.new_receiver();
let stop_send = stop_send.clone();

move || {
if let Err(e) = handler::jit::jit_loop(queue, stop_receive) {
if let Err(e) = handler::jit::jit_loop(queue, antenna_gain_dbi, stop_receive) {
error!("JIT loop error: {}", e);
stop_send.send(Signal::Stop).unwrap();
}
Expand All @@ -106,14 +107,12 @@ pub fn run(
let stop_send = stop_send.clone();
let stop_send_err = stop_send.clone();
let lorawan_public = config.gateway.lorawan_public;
let antenna_gain = config.gateway.antenna_gain;

move || {
if let Err(e) = handler::command::handle_loop(
lorawan_public,
&vendor_config,
&gateway_id,
antenna_gain,
queue,
rep_sock,
stop_receive,
Expand Down
14 changes: 2 additions & 12 deletions chirpstack-concentratord-2g4/src/handler/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub fn handle_loop(
lorawan_public: bool,
vendor_config: &vendor::Configuration,
gateway_id: &[u8],
anteanna_gain: i8,
queue: Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
rep_sock: zmq::Socket,
stop_receive: Receiver<Signal>,
Expand All @@ -38,14 +37,7 @@ pub fn handle_loop(
continue;
}
commands::Command::Downlink(pl) => {
match handle_downlink(
lorawan_public,
vendor_config,
gateway_id,
&queue,
&pl,
anteanna_gain,
) {
match handle_downlink(lorawan_public, vendor_config, gateway_id, &queue, &pl) {
Ok(v) => v,
Err(_) => Vec::new(),
}
Expand Down Expand Up @@ -79,7 +71,6 @@ fn handle_downlink(
gateway_id: &[u8],
queue: &Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
pl: &chirpstack_api::gw::DownlinkFrame,
anteanna_gain: i8,
) -> Result<Vec<u8>> {
stats::inc_tx_packets_received();

Expand All @@ -93,7 +84,7 @@ fn handle_downlink(

for (i, item) in pl.items.iter().enumerate() {
// convert protobuf to hal struct
let mut tx_packet = match wrapper::downlink_from_proto(lorawan_public, item) {
let tx_packet = match wrapper::downlink_from_proto(lorawan_public, item) {
Ok(v) => v,
Err(err) => {
error!(
Expand All @@ -103,7 +94,6 @@ fn handle_downlink(
return Err(err);
}
};
tx_packet.rf_power -= anteanna_gain;

// validate frequency range
let freqs = vendor_config.min_max_tx_freq;
Expand Down
4 changes: 3 additions & 1 deletion chirpstack-concentratord-2g4/src/handler/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::super::wrapper;

pub fn jit_loop(
queue: Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
antenna_gain_dbi: i8,
stop_receive: Receiver<Signal>,
) -> Result<()> {
debug!("Start JIT queue loop");
Expand All @@ -31,7 +32,8 @@ pub fn jit_loop(
};

let downlink_id = tx_packet.get_id();
let tx_packet = tx_packet.tx_packet();
let mut tx_packet = tx_packet.tx_packet();
tx_packet.rf_power -= antenna_gain_dbi;

match hal::send(&tx_packet) {
Ok(_) => {
Expand Down
6 changes: 2 additions & 4 deletions chirpstack-concentratord-sx1301/src/cmd/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ pub fn run(
// jit thread
threads.push(thread::spawn({
let queue = Arc::clone(&queue);
let antenna_gain_dbi = config.gateway.antenna_gain;
let stop_receive = signal_pool.new_receiver();
let stop_send = stop_send.clone();

move || {
if let Err(e) = handler::jit::jit_loop(queue, stop_receive) {
if let Err(e) = handler::jit::jit_loop(queue, antenna_gain_dbi, stop_receive) {
error!("JIT loop error: {}", e);
stop_send.send(Signal::Stop).unwrap();
}
Expand All @@ -118,13 +119,10 @@ pub fn run(
let stop_send = stop_send.clone();
let stop_send_err = stop_send.clone();

let antenna_gain = config.gateway.antenna_gain;

move || {
if let Err(e) = handler::command::handle_loop(
&vendor_config,
&gateway_id,
antenna_gain,
queue,
rep_sock,
stop_receive,
Expand Down
7 changes: 2 additions & 5 deletions chirpstack-concentratord-sx1301/src/handler/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use super::timersync;
pub fn handle_loop(
vendor_config: &vendor::Configuration,
gateway_id: &[u8],
antenna_gain: i8,
queue: Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
rep_sock: zmq::Socket,
stop_receive: Receiver<Signal>,
Expand All @@ -36,7 +35,7 @@ pub fn handle_loop(
continue;
}
commands::Command::Downlink(pl) => {
match handle_downlink(vendor_config, gateway_id, &queue, &pl, antenna_gain) {
match handle_downlink(vendor_config, gateway_id, &queue, &pl) {
Ok(v) => v,
Err(e) => {
error!("Handle downlink error: {}", e);
Expand Down Expand Up @@ -75,7 +74,6 @@ fn handle_downlink(
gateway_id: &[u8],
queue: &Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
pl: &chirpstack_api::gw::DownlinkFrame,
antenna_gain: i8,
) -> Result<Vec<u8>> {
stats::inc_tx_packets_received();

Expand All @@ -89,7 +87,7 @@ fn handle_downlink(

for (i, item) in pl.items.iter().enumerate() {
// convert protobuf to hal struct
let mut tx_packet = match wrapper::downlink_from_proto(item) {
let tx_packet = match wrapper::downlink_from_proto(item) {
Ok(v) => v,
Err(err) => {
error!(
Expand All @@ -99,7 +97,6 @@ fn handle_downlink(
return Err(err);
}
};
tx_packet.rf_power -= antenna_gain;

// validate frequency range
let freqs = vendor_config.radio_min_max_tx_freq[tx_packet.rf_chain as usize];
Expand Down
4 changes: 3 additions & 1 deletion chirpstack-concentratord-sx1301/src/handler/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::timersync;

pub fn jit_loop(
queue: Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
antenna_gain_dbi: i8,
stop_receive: Receiver<Signal>,
) -> Result<()> {
debug!("Starting JIT queue loop");
Expand All @@ -32,7 +33,8 @@ pub fn jit_loop(
};

let downlink_id = tx_packet.get_id();
let tx_packet = tx_packet.tx_packet();
let mut tx_packet = tx_packet.tx_packet();
tx_packet.rf_power -= antenna_gain_dbi;

match hal::send(&tx_packet) {
Ok(_) => {
Expand Down
5 changes: 2 additions & 3 deletions chirpstack-concentratord-sx1302/src/cmd/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ pub fn run(
// jit thread
threads.push(thread::spawn({
let queue = Arc::clone(&queue);
let antenna_gain_dbi = config.gateway.antenna_gain;
let stop_receive = signal_pool.new_receiver();
let stop_send = stop_send.clone();

move || {
if let Err(e) = handler::jit::jit_loop(queue, stop_receive) {
if let Err(e) = handler::jit::jit_loop(queue, antenna_gain_dbi, stop_receive) {
error!("JIT loop error: {}", e);
stop_send.send(Signal::Stop).unwrap();
}
Expand All @@ -111,13 +112,11 @@ pub fn run(
let stop_receive = signal_pool.new_receiver();
let stop_send = stop_send.clone();
let stop_send_err = stop_send.clone();
let antenna_gain = config.gateway.antenna_gain;

move || {
if let Err(e) = handler::command::handle_loop(
&vendor_config,
&gateway_id,
antenna_gain,
queue,
rep_sock,
stop_receive,
Expand Down
7 changes: 2 additions & 5 deletions chirpstack-concentratord-sx1302/src/handler/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use super::super::wrapper;
pub fn handle_loop(
vendor_config: &vendor::Configuration,
gateway_id: &[u8],
antenna_gain: i8,
queue: Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
rep_sock: zmq::Socket,
stop_receive: Receiver<Signal>,
Expand All @@ -36,7 +35,7 @@ pub fn handle_loop(
continue;
}
commands::Command::Downlink(pl) => {
match handle_downlink(vendor_config, gateway_id, &queue, &pl, antenna_gain) {
match handle_downlink(vendor_config, gateway_id, &queue, &pl) {
Ok(v) => v,
Err(e) => {
error!("Handle downlink error: {}", e);
Expand Down Expand Up @@ -75,7 +74,6 @@ fn handle_downlink(
gateway_id: &[u8],
queue: &Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
pl: &chirpstack_api::gw::DownlinkFrame,
antenna_gain: i8,
) -> Result<Vec<u8>> {
stats::inc_tx_packets_received();

Expand All @@ -89,7 +87,7 @@ fn handle_downlink(

for (i, item) in pl.items.iter().enumerate() {
// convert protobuf to hal struct
let mut tx_packet = match wrapper::downlink_from_proto(item) {
let tx_packet = match wrapper::downlink_from_proto(item) {
Ok(v) => v,
Err(err) => {
error!(
Expand All @@ -99,7 +97,6 @@ fn handle_downlink(
return Err(err);
}
};
tx_packet.rf_power -= antenna_gain;

// validate frequency range
match vendor_config.radio_config.get(tx_packet.rf_chain as usize) {
Expand Down
4 changes: 3 additions & 1 deletion chirpstack-concentratord-sx1302/src/handler/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::super::wrapper;

pub fn jit_loop(
queue: Arc<Mutex<jitqueue::Queue<wrapper::TxPacket>>>,
antenna_gain_dbi: i8,
stop_receive: Receiver<Signal>,
) -> Result<()> {
debug!("Starting JIT queue loop");
Expand All @@ -31,7 +32,8 @@ pub fn jit_loop(
};

let downlink_id = tx_packet.get_id();
let tx_packet = tx_packet.tx_packet();
let mut tx_packet = tx_packet.tx_packet();
tx_packet.rf_power -= antenna_gain_dbi;

match hal::send(&tx_packet) {
Ok(_) => {
Expand Down
14 changes: 7 additions & 7 deletions libconcentratord/src/regulation/standard/etsi_en_300_220.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,49 @@ pub fn new() -> Configuration {
frequency_min: 863000000,
frequency_max: 865000000,
duty_cycle_permille_max: 1,
tx_power_max: 14,
tx_power_max_eirp: 14 + 2,
},
Band {
label: "L".into(),
frequency_min: 865000000,
frequency_max: 868000000,
duty_cycle_permille_max: 10,
tx_power_max: 14,
tx_power_max_eirp: 14 + 2,
},
Band {
label: "M".into(),
frequency_min: 868000000,
frequency_max: 868600000,
duty_cycle_permille_max: 10,
tx_power_max: 14,
tx_power_max_eirp: 14 + 2,
},
Band {
label: "N".into(),
frequency_min: 868700000,
frequency_max: 869200000,
duty_cycle_permille_max: 1,
tx_power_max: 14,
tx_power_max_eirp: 14 + 2,
},
Band {
label: "P".into(),
frequency_min: 869400000,
frequency_max: 869650000,
duty_cycle_permille_max: 100,
tx_power_max: 27,
tx_power_max_eirp: 27 + 2,
},
Band {
label: "P".into(),
frequency_min: 869700000,
frequency_max: 870000000,
duty_cycle_permille_max: 1000,
tx_power_max: 7,
tx_power_max_eirp: 7 + 2,
},
Band {
label: "Q".into(),
frequency_min: 869700000,
frequency_max: 870000000,
duty_cycle_permille_max: 10,
tx_power_max: 14,
tx_power_max_eirp: 14 + 2,
},
],
window_time: Duration::from_secs(60 * 60),
Expand Down
18 changes: 10 additions & 8 deletions libconcentratord/src/regulation/standard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Band {
pub frequency_min: u32,
pub frequency_max: u32,
pub duty_cycle_permille_max: u32,
pub tx_power_max: i8,
pub tx_power_max_eirp: i8,
}

impl fmt::Display for Band {
Expand Down Expand Up @@ -56,15 +56,17 @@ impl Configuration {
}
}

pub fn get_band(&self, tx_freq: u32, tx_power: i8) -> Result<Band, Error> {
pub fn get_band(&self, tx_freq: u32, tx_power_eirp: i8) -> Result<Band, Error> {
for b in &self.bands {
if b.frequency_min <= tx_freq && tx_freq < b.frequency_max && tx_power <= b.tx_power_max
if b.frequency_min <= tx_freq
&& tx_freq < b.frequency_max
&& tx_power_eirp <= b.tx_power_max_eirp
{
return Ok(b.clone());
}
}

Err(Error::BandNotFound(tx_freq, tx_power))
Err(Error::BandNotFound(tx_freq, tx_power_eirp))
}

pub fn get_regulation(&self) -> Regulation {
Expand All @@ -87,28 +89,28 @@ mod test {
struct Test {
name: String,
freq: u32,
tx_power: i8,
tx_power_eirp: i8,
expected_band: Option<Band>,
}

let tests = vec![Test {
name: "M band".into(),
freq: 868100000,
tx_power: 14,
tx_power_eirp: 16,
expected_band: Some(Band {
label: "M".into(),
frequency_min: 868000000,
frequency_max: 868600000,
duty_cycle_permille_max: 10,
tx_power_max: 14,
tx_power_max_eirp: 16,
}),
}];

let c = Configuration::new(Standard::ETSI_EN_300_220);
for tst in &tests {
println!("> {}", tst.name);

let res = c.get_band(tst.freq, tst.tx_power);
let res = c.get_band(tst.freq, tst.tx_power_eirp);
if tst.expected_band.is_none() {
assert!(res.is_err());
} else {
Expand Down
Loading

0 comments on commit 5d1226e

Please sign in to comment.