1
+ use std:: sync:: LazyLock ;
2
+
1
3
use aes:: cipher:: generic_array:: GenericArray ;
2
4
use aes:: cipher:: { BlockEncrypt , KeyInit } ;
3
5
use aes:: { Aes128 , Block } ;
@@ -7,14 +9,11 @@ use tracing::debug;
7
9
8
10
use lrwn:: DevAddr ;
9
11
10
- lazy_static ! {
11
- static ref BEACON_PERIOD : Duration = Duration :: try_seconds( 128 ) . unwrap( ) ;
12
- static ref BEACON_RESERVED : Duration = Duration :: try_milliseconds( 2120 ) . unwrap( ) ;
13
- static ref BEACON_GUARD : Duration = Duration :: try_seconds( 3 ) . unwrap( ) ;
14
- static ref BEACON_WINDOW : Duration = Duration :: try_milliseconds( 122880 ) . unwrap( ) ;
15
- static ref PING_PERIOD_BASE : usize = 1 << 12 ;
16
- static ref SLOT_LEN : Duration = Duration :: try_milliseconds( 30 ) . unwrap( ) ;
17
- }
12
+ static BEACON_PERIOD : LazyLock < Duration > = LazyLock :: new ( || Duration :: try_seconds ( 128 ) . unwrap ( ) ) ;
13
+ static BEACON_RESERVED : LazyLock < Duration > =
14
+ LazyLock :: new ( || Duration :: try_milliseconds ( 2120 ) . unwrap ( ) ) ;
15
+ static PING_PERIOD_BASE : usize = 1 << 12 ;
16
+ static SLOT_LEN : LazyLock < Duration > = LazyLock :: new ( || Duration :: try_milliseconds ( 30 ) . unwrap ( ) ) ;
18
17
19
18
pub fn get_beacon_start ( ts : Duration ) -> Duration {
20
19
Duration :: try_seconds ( ts. num_seconds ( ) - ( ts. num_seconds ( ) % BEACON_PERIOD . num_seconds ( ) ) )
@@ -26,7 +25,7 @@ pub fn get_ping_offset(beacon_ts: Duration, dev_addr: &DevAddr, ping_nb: usize)
26
25
return Err ( anyhow ! ( "ping_nb must be > 0" ) ) ;
27
26
}
28
27
29
- let ping_period = * PING_PERIOD_BASE / ping_nb;
28
+ let ping_period = PING_PERIOD_BASE / ping_nb;
30
29
let beacon_time = ( beacon_ts. num_seconds ( ) % ( 1 << 32 ) ) as u32 ;
31
30
32
31
let key_bytes: [ u8 ; 16 ] = [ 0x00 ; 16 ] ;
@@ -54,7 +53,7 @@ pub fn get_next_ping_slot_after(
54
53
}
55
54
56
55
let mut beacon_start_ts = get_beacon_start ( after_gps_epoch_ts) ;
57
- let ping_period = * PING_PERIOD_BASE / ping_nb;
56
+ let ping_period = PING_PERIOD_BASE / ping_nb;
58
57
59
58
loop {
60
59
let ping_offset = get_ping_offset ( beacon_start_ts, dev_addr, ping_nb) ?;
@@ -122,7 +121,7 @@ pub mod test {
122
121
for k in 0 ..8 {
123
122
let mut beacon_ts = Duration :: zero ( ) ;
124
123
let ping_nb: usize = 1 << k;
125
- let ping_period = * PING_PERIOD_BASE / ping_nb;
124
+ let ping_period = PING_PERIOD_BASE / ping_nb;
126
125
let dev_addr = DevAddr :: from_be_bytes ( [ 0 , 0 , 0 , 0 ] ) ;
127
126
128
127
for _ in 0 ..100000 {
0 commit comments