Skip to content

Commit

Permalink
formatting fix
Browse files Browse the repository at this point in the history
  • Loading branch information
user committed Jul 27, 2024
1 parent 275d9f9 commit 00f54ab
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 38 deletions.
21 changes: 14 additions & 7 deletions chirpstack/src/api/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(()))
}
Expand All @@ -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(()))
}
Expand Down
2 changes: 1 addition & 1 deletion chirpstack/src/devaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions chirpstack/src/storage/device_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,4 +82,4 @@ pub async fn delete(dev_eui: &EUI64) -> Result<(), Error> {
"Device slot deleted"
);
Ok(())
}
}
38 changes: 19 additions & 19 deletions chirpstack/src/storage/helpers.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<usize, Error> {
pub async fn get_channel_index(dev_eui: EUI64) -> Result<usize, Error> {
let mut db_conn = get_async_db_conn().await?;

// Query to find the multicast group ID for the given dev_eui
Expand All @@ -38,14 +37,21 @@ pub async fn get_channel_index(
.select(multicast_group_device::dsl::multicast_group_id)
.first::<Uuid>(&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::<i64>(&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);

Expand All @@ -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.
Expand All @@ -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
}
// Calculate and return the channel.
(x_0 ^ b_0) << 2 | (x_1 ^ b_1) << 1 | (x_2 ^ b_2)
}
2 changes: 1 addition & 1 deletion chirpstack/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion chirpstack/src/storage/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ diesel::table! {
device_slot (dev_eui) {
dev_eui -> Bytea,
dev_addr -> Nullable<Bytea>,
slot -> Nullable<Int4>,
slot -> Nullable<Int4>,
multicast_group_id -> Uuid,
created_at -> Timestamptz,
}
Expand Down
5 changes: 4 additions & 1 deletion chirpstack/src/uplink/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 4 additions & 5 deletions lrwn/src/devaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
// }

Expand Down Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion lrwn/src/region/eu868.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,9 @@ impl Region for Configuration {
device_enabled_channels: &[usize],
) -> Vec<LinkADRReqPayload> {
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(
Expand Down

0 comments on commit 00f54ab

Please sign in to comment.