diff --git a/chirpstack/src/devaddr.rs b/chirpstack/src/devaddr.rs index da45d564..aa80befc 100644 --- a/chirpstack/src/devaddr.rs +++ b/chirpstack/src/devaddr.rs @@ -20,10 +20,10 @@ use crate::storage::schema::{ }; use crate::storage::{get_async_db_conn, AsyncPgPoolConnection}; -const NUMBER_OF_SLOTS: u32 = 64; -static mut CURRENT_SLOT: u32 = 0; pub async fn get_random_dev_addr_slot(dev_eui: EUI64) -> Result { + // The function get_async_db_conn() is defined in ./chirpstack/src/storage/mod.rs + // let mut conn = get_async_db_conn().await?; // Fetch multicast group ID and max slot count @@ -171,77 +171,3 @@ fn generate_dev_addr() -> DevAddr { dev_addr } - -// Old implementation -// project/chirpstack/src/devaddr.rs -pub fn get_random_dev_addr() -> DevAddr { - // check whether we still have any time slots left - unsafe { - // old implementation, panic if I run out of time slots - /* - if CURRENT_SLOT == NUMBER_OF_SLOTS { - panic!("no free time slots left!"); - }*/ - - // new implementation, counts slots from zero - if CURRENT_SLOT == NUMBER_OF_SLOTS { - CURRENT_SLOT = 0; - } - } - - let conf = config::get(); - let mut rng = rand::thread_rng(); - - // Get configured DevAddr prefixes. - let prefixes = if conf.network.dev_addr_prefixes.is_empty() { - vec![conf.network.net_id.dev_addr_prefix()] - } else { - conf.network.dev_addr_prefixes.clone() - }; - - // Pick a random one (in case multiple prefixes are configured). - let prefix = *prefixes.choose(&mut rng).unwrap(); - - // Generate random DevAddr. - let mut dev_addr: [u8; 4] = [0; 4]; - rng.fill_bytes(&mut dev_addr); - #[cfg(test)] - { - dev_addr = [1, 2, 3, 4]; - } - let mut dev_addr = DevAddr::from_be_bytes(dev_addr); - - // Set DevAddr prefix. - dev_addr.set_dev_addr_prefix(prefix); - - // print DevAddr as 4 integers - //dbg!(dev_addr.clone()); - // print the address prefix (AddrPrefix) - //dbg!(prefix); - - // find the slot of the DevAddr - - // hash function - let sum: u32 = dev_addr.clone().into_iter().map(|x| x as u32).sum(); - let generated_slot: u32 = sum % NUMBER_OF_SLOTS; - - // regenerate the dev_addr until the correct one is generated. - unsafe { - match CURRENT_SLOT == generated_slot { - true => { - // define the next time slot to generate - CURRENT_SLOT = CURRENT_SLOT + 1; - // print the generated time slot and devaddr value - println!( - "{:?} connected, time slot is: {}", - dev_addr.clone(), - generated_slot - ); - } - false => { - return get_random_dev_addr(); - } - }; - } - dev_addr -}