@@ -4,7 +4,6 @@ use anyhow::Result;
44use chirpstack_api:: { gw, prost_types} ;
55use libconcentratord:: jitqueue;
66use libloragw_2g4:: hal;
7- use rand:: Rng ;
87
98#[ derive( Copy , Clone ) ]
109pub struct TxPacket ( hal:: TxPacket , u32 ) ;
@@ -68,8 +67,6 @@ pub fn uplink_to_proto(
6867 packet : & hal:: RxPacket ,
6968 time_fallback : bool ,
7069) -> Result < gw:: UplinkFrame > {
71- let mut rng = rand:: rng ( ) ;
72-
7370 Ok ( gw:: UplinkFrame {
7471 phy_payload : packet. payload [ ..packet. size as usize ] . to_vec ( ) ,
7572 tx_info : Some ( gw:: UplinkTxInfo {
@@ -106,7 +103,7 @@ pub fn uplink_to_proto(
106103 } ) ,
107104 } ) ,
108105 rx_info : Some ( gw:: UplinkRxInfo {
109- uplink_id : rng . random ( ) ,
106+ uplink_id : getrandom :: u32 ( ) ? ,
110107 context : packet. count_us . to_be_bytes ( ) . to_vec ( ) ,
111108 gateway_id : hex:: encode ( gateway_id) ,
112109 rssi : packet. rssi as i32 ,
@@ -157,89 +154,91 @@ pub fn downlink_from_proto(
157154 } ;
158155
159156 if let Some ( timing) = & tx_info. timing
160- && let Some ( params) = & timing. parameters {
161- match params {
162- gw:: timing:: Parameters :: Immediately ( _) => {
163- packet. tx_mode = hal:: TxMode :: Immediate ;
157+ && let Some ( params) = & timing. parameters
158+ {
159+ match params {
160+ gw:: timing:: Parameters :: Immediately ( _) => {
161+ packet. tx_mode = hal:: TxMode :: Immediate ;
162+ }
163+ gw:: timing:: Parameters :: Delay ( v) => {
164+ packet. tx_mode = hal:: TxMode :: Timestamped ;
165+
166+ let ctx = & tx_info. context ;
167+ if ctx. len ( ) != 4 {
168+ return Err ( anyhow ! ( "context must be exactly 4 bytes" ) ) ;
164169 }
165- gw:: timing:: Parameters :: Delay ( v) => {
166- packet. tx_mode = hal:: TxMode :: Timestamped ;
167170
168- let ctx = & tx_info. context ;
169- if ctx. len ( ) != 4 {
170- return Err ( anyhow ! ( "context must be exactly 4 bytes" ) ) ;
171+ match & v. delay {
172+ Some ( v) => {
173+ let mut array = [ 0 ; 4 ] ;
174+ array. copy_from_slice ( ctx) ;
175+ packet. count_us = u32:: from_be_bytes ( array) . wrapping_add (
176+ ( Duration :: from_secs ( v. seconds as u64 )
177+ + Duration :: from_nanos ( v. nanos as u64 ) )
178+ . as_micros ( ) as u32 ,
179+ ) ;
171180 }
172-
173- match & v. delay {
174- Some ( v) => {
175- let mut array = [ 0 ; 4 ] ;
176- array. copy_from_slice ( ctx) ;
177- packet. count_us = u32:: from_be_bytes ( array) . wrapping_add (
178- ( Duration :: from_secs ( v. seconds as u64 )
179- + Duration :: from_nanos ( v. nanos as u64 ) )
180- . as_micros ( ) as u32 ,
181- ) ;
182- }
183- None => {
184- return Err ( anyhow ! ( "delay must not be null" ) ) ;
185- }
181+ None => {
182+ return Err ( anyhow ! ( "delay must not be null" ) ) ;
186183 }
187184 }
188- gw :: timing :: Parameters :: GpsEpoch ( _ ) => {
189- return Err ( anyhow ! ( "gps epoch timing is not implemented" ) ) ;
190- }
185+ }
186+ gw :: timing :: Parameters :: GpsEpoch ( _ ) => {
187+ return Err ( anyhow ! ( "gps epoch timing is not implemented" ) ) ;
191188 }
192189 }
190+ }
193191
194192 if let Some ( modulation) = & tx_info. modulation
195- && let Some ( params) = & modulation. parameters {
196- match params {
197- gw:: modulation:: Parameters :: Lora ( v) => {
198- packet. bandwidth = v. bandwidth ;
199- packet. datarate = match v. spreading_factor {
200- 5 => hal:: DataRate :: SF5 ,
201- 6 => hal:: DataRate :: SF6 ,
202- 7 => hal:: DataRate :: SF7 ,
203- 8 => hal:: DataRate :: SF8 ,
204- 9 => hal:: DataRate :: SF9 ,
205- 10 => hal:: DataRate :: SF10 ,
206- 11 => hal:: DataRate :: SF11 ,
207- 12 => hal:: DataRate :: SF12 ,
193+ && let Some ( params) = & modulation. parameters
194+ {
195+ match params {
196+ gw:: modulation:: Parameters :: Lora ( v) => {
197+ packet. bandwidth = v. bandwidth ;
198+ packet. datarate = match v. spreading_factor {
199+ 5 => hal:: DataRate :: SF5 ,
200+ 6 => hal:: DataRate :: SF6 ,
201+ 7 => hal:: DataRate :: SF7 ,
202+ 8 => hal:: DataRate :: SF8 ,
203+ 9 => hal:: DataRate :: SF9 ,
204+ 10 => hal:: DataRate :: SF10 ,
205+ 11 => hal:: DataRate :: SF11 ,
206+ 12 => hal:: DataRate :: SF12 ,
207+ _ => return Err ( anyhow ! ( "unexpected spreading-factor" ) ) ,
208+ } ;
209+ packet. coderate = match v. code_rate ( ) {
210+ gw:: CodeRate :: Cr45 => hal:: CodeRate :: LoRa4_5 ,
211+ gw:: CodeRate :: Cr46 => hal:: CodeRate :: LoRa4_6 ,
212+ gw:: CodeRate :: Cr47 => hal:: CodeRate :: LoRa4_7 ,
213+ gw:: CodeRate :: Cr48 => hal:: CodeRate :: LoRa4_8 ,
214+ gw:: CodeRate :: CrLi45 => hal:: CodeRate :: LoRaLi4_5 ,
215+ gw:: CodeRate :: CrLi46 => hal:: CodeRate :: LoRaLi4_6 ,
216+ gw:: CodeRate :: CrLi48 => hal:: CodeRate :: LoRaLi4_8 ,
217+ _ => return Err ( anyhow ! ( "unexpected coderate" ) ) ,
218+ } ;
219+ packet. preamble = if v. preamble > 0 {
220+ v. preamble as u16
221+ } else {
222+ match v. spreading_factor {
223+ 5 => 12 ,
224+ 6 => 12 ,
225+ 7 => 8 ,
226+ 8 => 8 ,
227+ 9 => 8 ,
228+ 10 => 8 ,
229+ 11 => 8 ,
230+ 12 => 8 ,
208231 _ => return Err ( anyhow ! ( "unexpected spreading-factor" ) ) ,
209- } ;
210- packet. coderate = match v. code_rate ( ) {
211- gw:: CodeRate :: Cr45 => hal:: CodeRate :: LoRa4_5 ,
212- gw:: CodeRate :: Cr46 => hal:: CodeRate :: LoRa4_6 ,
213- gw:: CodeRate :: Cr47 => hal:: CodeRate :: LoRa4_7 ,
214- gw:: CodeRate :: Cr48 => hal:: CodeRate :: LoRa4_8 ,
215- gw:: CodeRate :: CrLi45 => hal:: CodeRate :: LoRaLi4_5 ,
216- gw:: CodeRate :: CrLi46 => hal:: CodeRate :: LoRaLi4_6 ,
217- gw:: CodeRate :: CrLi48 => hal:: CodeRate :: LoRaLi4_8 ,
218- _ => return Err ( anyhow ! ( "unexpected coderate" ) ) ,
219- } ;
220- packet. preamble = if v. preamble > 0 {
221- v. preamble as u16
222- } else {
223- match v. spreading_factor {
224- 5 => 12 ,
225- 6 => 12 ,
226- 7 => 8 ,
227- 8 => 8 ,
228- 9 => 8 ,
229- 10 => 8 ,
230- 11 => 8 ,
231- 12 => 8 ,
232- _ => return Err ( anyhow ! ( "unexpected spreading-factor" ) ) ,
233- }
234- } ;
235- packet. no_crc = v. no_crc ;
236- packet. invert_pol = v. polarization_inversion ;
237- }
238- _ => {
239- return Err ( anyhow ! ( "only LORA modulation is implemented" ) ) ;
240- }
232+ }
233+ } ;
234+ packet. no_crc = v. no_crc ;
235+ packet. invert_pol = v. polarization_inversion ;
236+ }
237+ _ => {
238+ return Err ( anyhow ! ( "only LORA modulation is implemented" ) ) ;
241239 }
242240 }
241+ }
243242
244243 Ok ( packet)
245244}
0 commit comments