From 00f54ab0c2584cfed7022c8bc776c8a5ccf66be3 Mon Sep 17 00:00:00 2001 From: user Date: Sat, 27 Jul 2024 23:17:33 +0500 Subject: [PATCH] formatting fix --- chirpstack/src/api/device.rs | 21 ++++++++++----- chirpstack/src/devaddr.rs | 2 +- chirpstack/src/storage/device_slot.rs | 4 +-- chirpstack/src/storage/helpers.rs | 38 +++++++++++++-------------- chirpstack/src/storage/mod.rs | 2 +- chirpstack/src/storage/schema.rs | 2 +- chirpstack/src/uplink/join.rs | 5 +++- lrwn/src/devaddr.rs | 9 +++---- lrwn/src/region/eu868.rs | 4 ++- 9 files changed, 49 insertions(+), 38 deletions(-) diff --git a/chirpstack/src/api/device.rs b/chirpstack/src/api/device.rs index 1b50bcea..8dca325d 100644 --- a/chirpstack/src/api/device.rs +++ b/chirpstack/src/api/device.rs @@ -474,8 +474,8 @@ impl DeviceService for Device { .await?; let dev_addr = DevAddr::from_str(&req_ds.dev_addr).map_err(|e| e.status())?; - let multicast_group_id = Uuid::from_str(&req_ds.multicast_group_id).map_err(|e| e.status())?; - + let multicast_group_id = + Uuid::from_str(&req_ds.multicast_group_id).map_err(|e| e.status())?; let ds = device_slot::DeviceSlot { dev_eui, @@ -509,12 +509,15 @@ impl DeviceService for Device { let slot: u32 = match ds.slot { Some(s) => s as u32, None => return Err(Status::invalid_argument("Slot is missing")), - }; + }; let dev_addr = match ds.dev_addr { Some(addr) => addr.to_string(), - None => return Err(Status::invalid_argument("DevAddr is missing or not specified")), + None => { + return Err(Status::invalid_argument( + "DevAddr is missing or not specified", + )) + } }; - let response = api::GetDeviceSlotResponse { dev_addr, @@ -553,7 +556,9 @@ impl DeviceService for Device { created_at: ds.created_at, }; - device_slot::update(updated_ds).await.map_err(|e| e.status())?; + device_slot::update(updated_ds) + .await + .map_err(|e| e.status())?; Ok(Response::new(())) } @@ -573,7 +578,9 @@ impl DeviceService for Device { ) .await?; - device_slot::delete(&dev_eui).await.map_err(|e| e.status())?; + device_slot::delete(&dev_eui) + .await + .map_err(|e| e.status())?; Ok(Response::new(())) } diff --git a/chirpstack/src/devaddr.rs b/chirpstack/src/devaddr.rs index 4065141a..8c28955c 100644 --- a/chirpstack/src/devaddr.rs +++ b/chirpstack/src/devaddr.rs @@ -135,7 +135,7 @@ fn regenerate_dev_addr_for_slot(slot: i32, max_slot_count: i32) -> DevAddr { for &byte in sha256_hash.iter().take(8) { big_int = (big_int << 8) | byte as u64; } - + let hash_int = (big_int % max_slot_count as u64) as i32; if hash_int % max_slot_count == slot { diff --git a/chirpstack/src/storage/device_slot.rs b/chirpstack/src/storage/device_slot.rs index 0f3909a1..672649a5 100644 --- a/chirpstack/src/storage/device_slot.rs +++ b/chirpstack/src/storage/device_slot.rs @@ -5,7 +5,7 @@ use diesel_async::RunQueryDsl; use tracing::info; use uuid::Uuid; -use lrwn::{EUI64, DevAddr}; +use lrwn::{DevAddr, EUI64}; use super::error::Error; use super::get_async_db_conn; @@ -82,4 +82,4 @@ pub async fn delete(dev_eui: &EUI64) -> Result<(), Error> { "Device slot deleted" ); Ok(()) -} \ No newline at end of file +} diff --git a/chirpstack/src/storage/helpers.rs b/chirpstack/src/storage/helpers.rs index cf6ca409..9862a262 100644 --- a/chirpstack/src/storage/helpers.rs +++ b/chirpstack/src/storage/helpers.rs @@ -1,14 +1,16 @@ use diesel::prelude::*; use diesel_async::RunQueryDsl; -use super::schema::{application, device, device_profile, tenant, multicast_group, multicast_group_device}; +use super::schema::{ + application, device, device_profile, multicast_group, multicast_group_device, tenant, +}; use super::{ - application::Application, device::Device, device_profile::DeviceProfile, tenant::Tenant + application::Application, device::Device, device_profile::DeviceProfile, tenant::Tenant, }; use super::{error::Error, get_async_db_conn}; use lrwn::EUI64; -use uuid::Uuid; use sha2::{Digest, Sha256}; +use uuid::Uuid; pub async fn get_all_device_data( dev_eui: EUI64, @@ -24,12 +26,9 @@ pub async fn get_all_device_data( Ok(res) } - static SECRET_KEY: &[u8; 32] = b"e2fc714cef004ad48c91d2c08b8c4f75"; // Custom function for ts-lora -pub async fn get_channel_index( - dev_eui: EUI64, -) -> Result { +pub async fn get_channel_index(dev_eui: EUI64) -> Result { let mut db_conn = get_async_db_conn().await?; // Query to find the multicast group ID for the given dev_eui @@ -38,14 +37,21 @@ pub async fn get_channel_index( .select(multicast_group_device::dsl::multicast_group_id) .first::(&mut db_conn) .await - .map_err(|e| Error::from_diesel(e, "Error while retrieving multicast group ID".to_string()))?; + .map_err(|e| { + Error::from_diesel(e, "Error while retrieving multicast group ID".to_string()) + })?; let counter = multicast_group::table .filter(multicast_group::dsl::id.eq(id)) .select(multicast_group::dsl::f_cnt) .first::(&mut db_conn) .await - .map_err(|e| Error::from_diesel(e, "Error while retrieving frame counter from Multicast Group".to_string()))?; + .map_err(|e| { + Error::from_diesel( + e, + "Error while retrieving frame counter from Multicast Group".to_string(), + ) + })?; let channel_index = generate_channel(counter, SECRET_KEY, id); @@ -63,11 +69,7 @@ fn generate_channel(asn: i64, key: &[u8], offset_uuid: Uuid) -> usize { let msb_3 = (hash_bytes[0] >> 5) as usize; // Calculate OFFSET_j' using the UUID bytes. - let sum_uuid_bytes: u64 = offset_uuid - .as_bytes() - .iter() - .map(|&b| b as u64) - .sum(); + let sum_uuid_bytes: u64 = offset_uuid.as_bytes().iter().map(|&b| b as u64).sum(); let offset_j_prime = ((sum_uuid_bytes as i64 + asn) % 8) as usize; // Convert OFFSET_j' to binary. @@ -80,8 +82,6 @@ fn generate_channel(asn: i64, key: &[u8], offset_uuid: Uuid) -> usize { let x_1 = (msb_3 >> 1) & 0b1; let x_2 = msb_3 & 0b1; - // Calculate the channel. - let ch_j = (x_0 ^ b_0) << 2 | (x_1 ^ b_1) << 1 | (x_2 ^ b_2); - - ch_j -} \ No newline at end of file + // Calculate and return the channel. + (x_0 ^ b_0) << 2 | (x_1 ^ b_1) << 1 | (x_2 ^ b_2) +} diff --git a/chirpstack/src/storage/mod.rs b/chirpstack/src/storage/mod.rs index 9e817477..10035b46 100644 --- a/chirpstack/src/storage/mod.rs +++ b/chirpstack/src/storage/mod.rs @@ -23,11 +23,11 @@ pub mod application; pub mod device; pub mod device_gateway; pub mod device_keys; -pub mod device_slot; pub mod device_profile; pub mod device_profile_template; pub mod device_queue; pub mod device_session; +pub mod device_slot; pub mod downlink_frame; pub mod error; pub mod fields; diff --git a/chirpstack/src/storage/schema.rs b/chirpstack/src/storage/schema.rs index 6e7bab9e..3dd4b71c 100644 --- a/chirpstack/src/storage/schema.rs +++ b/chirpstack/src/storage/schema.rs @@ -84,7 +84,7 @@ diesel::table! { device_slot (dev_eui) { dev_eui -> Bytea, dev_addr -> Nullable, - slot -> Nullable, + slot -> Nullable, multicast_group_id -> Uuid, created_at -> Timestamptz, } diff --git a/chirpstack/src/uplink/join.rs b/chirpstack/src/uplink/join.rs index 5cc376b4..f98e7391 100644 --- a/chirpstack/src/uplink/join.rs +++ b/chirpstack/src/uplink/join.rs @@ -801,7 +801,10 @@ impl JoinRequest { device_profile.reset_session_to_boot_params(&mut ds); - println!("in set_device_session: enabled channels for device {}", device.dev_eui); + println!( + "in set_device_session: enabled channels for device {}", + device.dev_eui + ); for ch in ds.enabled_uplink_channel_indices.clone().into_iter() { println!("{}", ch); } diff --git a/lrwn/src/devaddr.rs b/lrwn/src/devaddr.rs index 5358f2be..c1376460 100644 --- a/lrwn/src/devaddr.rs +++ b/lrwn/src/devaddr.rs @@ -199,18 +199,18 @@ impl DevAddr { // pub fn set_dev_addr_prefix_and_slot(&mut self, prefix: DevAddrPrefix, slot: i32) { // // convert devaddr to u32 // let mut devaddr = u32::from_be_bytes(self.0); - + // // Clean the prefix bits and the slot area (11 least significant bits) // let prefix_size = prefix.size(); // number of bits in prefix // let mask = (u32::MAX >> prefix_size) << 11; // clears bits for prefix and the 11 bit slot // devaddr &= !mask; - + // // Set the prefix // devaddr |= u32::from_be_bytes(prefix.prefix()); - + // // Set the slot in the 11 least significant bits // devaddr |= (slot as u32) & 0x07FF; // ensure slot is within 11 bits - + // self.0 = devaddr.to_be_bytes(); // } @@ -275,7 +275,6 @@ impl Iterator for DevAddrIntoIterator { } } - impl fmt::Display for DevAddr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", hex::encode(self.0)) diff --git a/lrwn/src/region/eu868.rs b/lrwn/src/region/eu868.rs index a0748640..7ed400e9 100644 --- a/lrwn/src/region/eu868.rs +++ b/lrwn/src/region/eu868.rs @@ -784,7 +784,9 @@ impl Region for Configuration { device_enabled_channels: &[usize], ) -> Vec { self.base - .custom_get_link_adr_req_payloads_for_enabled_uplink_channel_indices(device_enabled_channels) + .custom_get_link_adr_req_payloads_for_enabled_uplink_channel_indices( + device_enabled_channels, + ) } fn get_enabled_uplink_channel_indices_for_link_adr_payloads(