Skip to content

Commit

Permalink
Fix all clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
diondokter committed Feb 14, 2025
1 parent ad89067 commit 7f75be5
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 91 deletions.
28 changes: 20 additions & 8 deletions src/wire/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const ASSOCIATION_PERMIT: u8 = 0b1000_0000;
impl TryRead<'_> for SuperframeSpecification {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
check_len(&bytes, 2)?;
check_len(bytes, 2)?;
let byte: u8 = bytes.read(offset)?;
let beacon_order = BeaconOrder::from(byte & 0x0f);
let superframe_order = SuperframeOrder::from((byte >> 4) & 0x0f);
Expand All @@ -128,8 +128,8 @@ impl TryRead<'_> for SuperframeSpecification {
impl TryWrite for SuperframeSpecification {
fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result<usize> {
let offset = &mut 0;
let bo = u8::from(self.beacon_order.clone());
let so = u8::from(self.superframe_order.clone());
let bo = u8::from(self.beacon_order);
let so = u8::from(self.superframe_order);
bytes.write(offset, (bo & 0x0f) | (so << 4))?;
let ble = if self.battery_life_extension {
BATTERY_LIFE_EXTENSION
Expand Down Expand Up @@ -175,6 +175,12 @@ pub struct GuaranteedTimeSlotDescriptor {
pub direction: Direction,
}

impl Default for GuaranteedTimeSlotDescriptor {
fn default() -> Self {
Self::new()
}
}

impl GuaranteedTimeSlotDescriptor {
/// Create a new empty slot
pub fn new() -> Self {
Expand All @@ -190,7 +196,7 @@ impl GuaranteedTimeSlotDescriptor {
impl TryRead<'_> for GuaranteedTimeSlotDescriptor {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
check_len(&bytes, 3)?;
check_len(bytes, 3)?;
let short_address = bytes.read(offset)?;
let byte: u8 = bytes.read(offset)?;
let starting_slot = byte & 0x0f;
Expand All @@ -212,7 +218,7 @@ impl TryWrite for GuaranteedTimeSlotDescriptor {
fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result<usize> {
let offset = &mut 0;
bytes.write(offset, self.short_address)?;
bytes.write(offset, self.starting_slot | self.length << 4)?;
bytes.write(offset, self.starting_slot | (self.length << 4))?;
Ok(*offset)
}
}
Expand Down Expand Up @@ -241,6 +247,12 @@ pub struct GuaranteedTimeSlotInformation {
pub slots: heapless::Vec<GuaranteedTimeSlotDescriptor, 7>,
}

impl Default for GuaranteedTimeSlotInformation {
fn default() -> Self {
Self::new()
}
}

impl GuaranteedTimeSlotInformation {
/// Create a new empty GTS information
pub fn new() -> Self {
Expand Down Expand Up @@ -271,9 +283,9 @@ impl TryWrite for GuaranteedTimeSlotInformation {
let mut direction_mask = 0u8;
for slot in &self.slots {
if slot.direction_transmit() {
direction_mask = direction_mask | dir;
direction_mask |= dir;
}
dir = dir << 1;
dir <<= 1;
}
direction_mask
};
Expand Down Expand Up @@ -307,7 +319,7 @@ impl TryRead<'_> for GuaranteedTimeSlotInformation {
Direction::Receive
};
slot.set_direction(direction);
direction_mask = direction_mask >> 1;
direction_mask >>= 1;
slots
.push(slot)
.expect("slot_count can never be larger than Vec::capacity");
Expand Down
16 changes: 8 additions & 8 deletions src/wire/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ impl From<CapabilityInformation> for u8 {
fn from(ar: CapabilityInformation) -> Self {
let mut byte = 0u8;
if ar.full_function_device {
byte = byte | CAP_FFD;
byte |= CAP_FFD;
}
if ar.mains_power {
byte = byte | CAP_MAINS_POWER;
byte |= CAP_MAINS_POWER;
}
if ar.idle_receive {
byte = byte | CAP_IDLE_RECEIVE;
byte |= CAP_IDLE_RECEIVE;
}
if ar.frame_protection {
byte = byte | CAP_FRAME_PROTECTION;
byte |= CAP_FRAME_PROTECTION;
}
if ar.allocate_address {
byte = byte | CAP_ALLOCATE_ADDRESS;
byte |= CAP_ALLOCATE_ADDRESS;
}
byte
}
Expand Down Expand Up @@ -158,7 +158,7 @@ impl TryWrite for CoordinatorRealignmentData {
impl TryRead<'_> for CoordinatorRealignmentData {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
check_len(&bytes, 7)?;
check_len(bytes, 7)?;
let pan_id = bytes.read(offset)?;
let coordinator_address = bytes.read(offset)?;
let channel = bytes.read(offset)?;
Expand Down Expand Up @@ -214,10 +214,10 @@ impl From<GuaranteedTimeSlotCharacteristics> for u8 {
fn from(gtsc: GuaranteedTimeSlotCharacteristics) -> Self {
let mut byte = gtsc.count & 0x0f;
if gtsc.receive_only {
byte = byte | GTSC_RECEIVE_ONLY;
byte |= GTSC_RECEIVE_ONLY;
}
if gtsc.allocation {
byte = byte | GTSC_ALLOCATION;
byte |= GTSC_ALLOCATION;
}
byte
}
Expand Down
37 changes: 16 additions & 21 deletions src/wire/frame/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,13 @@ impl Header {
// Frame control + sequence number
let mut len = 3;

for i in [self.destination, self.source].iter() {
match i {
Some(addr) => {
// pan ID
len += 2;
// Address length
match addr {
Address::Short(..) => len += 2,
Address::Extended(..) => len += 8,
}
}
_ => {}
for addr in [self.destination, self.source].iter().flatten() {
// pan ID
len += 2;
// Address length
match addr {
Address::Short(..) => len += 2,
Address::Extended(..) => len += 8,
}
}
len
Expand All @@ -113,7 +108,7 @@ impl TryRead<'_> for Header {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
// Make sure we have enough buffer for the Frame Control field
check_len(&bytes, 3)?;
check_len(bytes, 3)?;

/* Decode Frame Control Field */
let bits: u16 = bytes.read_with(offset, LE)?;
Expand Down Expand Up @@ -230,14 +225,14 @@ where

let security = self.auxiliary_security_header.is_some();

let frame_control_raw = (self.frame_type as u16) << offset::FRAME_TYPE
| (security as u16) << offset::SECURITY
| (self.frame_pending as u16) << offset::PENDING
| (self.ack_request as u16) << offset::ACK
| (self.pan_id_compress as u16) << offset::PAN_ID_COMPRESS
| (dest_addr_mode as u16) << offset::DEST_ADDR_MODE
| (self.version as u16) << offset::VERSION
| (src_addr_mode as u16) << offset::SRC_ADDR_MODE;
let frame_control_raw = ((self.frame_type as u16) << offset::FRAME_TYPE)
| ((security as u16) << offset::SECURITY)
| ((self.frame_pending as u16) << offset::PENDING)
| ((self.ack_request as u16) << offset::ACK)
| ((self.pan_id_compress as u16) << offset::PAN_ID_COMPRESS)
| ((dest_addr_mode as u16) << offset::DEST_ADDR_MODE)
| ((self.version as u16) << offset::VERSION)
| ((src_addr_mode as u16) << offset::SRC_ADDR_MODE);

bytes.write_with(offset, frame_control_raw, LE)?;

Expand Down
23 changes: 10 additions & 13 deletions src/wire/frame/security/auxiliary_security_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl AuxiliarySecurityHeader {
/// Get the size of this security header, in octets
pub fn get_octet_size(&self) -> usize {
// SecurityControl length + FrameCounter length
let length = 1
+ 4

1 + 4
+ match self.key_identifier {
Some(key_id) => match key_id.key_source {
Some(source) => match source {
Expand All @@ -35,8 +35,7 @@ impl AuxiliarySecurityHeader {
None => 1,
},
None => 0,
};
length
}
}

/// Create a new Auxiliary Security Header with the specified control and key identifier
Expand All @@ -50,6 +49,8 @@ impl AuxiliarySecurityHeader {

/// Create a new Auxiliary Security Header with the specified control, key identifier, and frame counter.
///
/// # Safety
///
/// This function is unsafe because the frame_counter is almost always set when parsing a frame from a buffer,
/// or by the security context at the time of actually writing a secured frame.
pub unsafe fn new_unsafe(
Expand Down Expand Up @@ -137,11 +138,8 @@ where

bytes.write(offset, self.control)?;
bytes.write(offset, sec_ctx.frame_counter)?;
match self.key_identifier {
Some(key_identifier) => {
bytes.write(offset, key_identifier)?;
}
_ => {}
if let Some(key_identifier) = self.key_identifier {
bytes.write(offset, key_identifier)?;
}
Ok(*offset)
}
Expand All @@ -160,12 +158,11 @@ pub struct KeyIdentifier {
impl TryWrite for KeyIdentifier {
fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result<usize> {
let offset = &mut 0;
match self.key_source {
Some(source) => match source {
if let Some(source) = self.key_source {
match source {
KeySource::Short(src) => bytes.write(offset, src)?,
KeySource::Long(src) => bytes.write(offset, src)?,
},
_ => {}
}
}

bytes.write(offset, self.key_index)?;
Expand Down
Loading

0 comments on commit 7f75be5

Please sign in to comment.