Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix all clippy warnings #61

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/mac/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 Down Expand Up @@ -130,8 +130,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 @@ -189,10 +189,16 @@ impl GuaranteedTimeSlotDescriptor {
}
}

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

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 Down Expand Up @@ -259,6 +265,12 @@ impl GuaranteedTimeSlotInformation {
}
}

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

impl TryWrite for GuaranteedTimeSlotInformation {
fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result<usize> {
let offset = &mut 0;
Expand All @@ -275,9 +287,9 @@ impl TryWrite for GuaranteedTimeSlotInformation {
for n in 0..self.slot_count {
let slot = self.slots[n];
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 {
if slot_count > 0 {
check_len(&bytes[*offset..], 2 + (3 * slot_count))?;
let mut direction_mask: u8 = bytes.read(offset)?;
for n in 0..slot_count {
for item in slots.iter_mut().take(slot_count) {
let mut slot: GuaranteedTimeSlotDescriptor =
bytes.read(offset)?;
let direction = if direction_mask & 0b1 == 0b1 {
Expand All @@ -316,8 +328,8 @@ impl TryRead<'_> for GuaranteedTimeSlotInformation {
Direction::Receive
};
slot.set_direction(direction);
direction_mask = direction_mask >> 1;
slots[n] = slot;
direction_mask >>= 1;
*item = slot;
}
}
Ok((
Expand Down Expand Up @@ -383,6 +395,12 @@ impl PendingAddress {
}
}

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

impl TryRead<'_> for PendingAddress {
fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> {
let offset = &mut 0;
Expand All @@ -393,12 +411,12 @@ impl TryRead<'_> for PendingAddress {
let el = ((byte & EXTENDED_MASK) >> 4) as usize;
check_len(&bytes[*offset..], (sl * ss) + (el * es))?;
let mut short_addresses = [ShortAddress::broadcast(); 7];
for n in 0..sl {
short_addresses[n] = bytes.read(offset)?;
for item in short_addresses.iter_mut().take(sl) {
*item = bytes.read(offset)?;
}
let mut extended_addresses = [ExtendedAddress::broadcast(); 7];
for n in 0..el {
extended_addresses[n] = bytes.read(offset)?;
for item in extended_addresses.iter_mut().take(el) {
*item = bytes.read(offset)?;
}
Ok((
Self {
Expand Down
16 changes: 8 additions & 8 deletions src/mac/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,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 @@ -157,7 +157,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 @@ -213,10 +213,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
21 changes: 8 additions & 13 deletions src/mac/frame/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,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 @@ -115,7 +110,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
22 changes: 9 additions & 13 deletions src/mac/frame/security/auxiliary_security_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ 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 @@ -36,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 @@ -54,6 +52,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 @@ -143,11 +143,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 @@ -166,12 +163,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
30 changes: 15 additions & 15 deletions src/mac/frame/security/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ fn calculate_nonce(
sec_level: SecurityLevel,
) -> [u8; 13] {
let mut output = [0u8; 13];
for i in 0..8 {
output[i] = (source_addr >> (8 * i) & 0xFF) as u8;
for (i, item) in output.iter_mut().enumerate().take(8) {
*item = (source_addr >> (8 * i) & 0xFF) as u8;
}

for i in 0..4 {
Expand All @@ -326,7 +326,7 @@ fn calculate_nonce(
///
/// # Panics
/// if footer_mode is not None due to currently absent implementation of explicit footers
pub(crate) fn secure_frame<'a, AEADBLKCIPH, KEYDESCLO>(
pub(crate) fn secure_frame<AEADBLKCIPH, KEYDESCLO>(
frame: Frame<'_>,
context: &mut SecurityContext<AEADBLKCIPH, KEYDESCLO>,
footer_mode: FooterMode,
Expand All @@ -347,7 +347,7 @@ where
}
}

let mut offset = 0 as usize;
let mut offset = 0;
let header = frame.header;

if header.has_security() {
Expand All @@ -368,12 +368,12 @@ where

// If frame size plus AuthLen plus AuxLen plus FCS is bigger than aMaxPHYPacketSize
// 7.2.1b4
if !(frame.payload.len()
if frame.payload.len()
+ frame.header.get_octet_size()
+ aux_len
+ auth_len
+ 2
<= 127)
> 127
{
return Err(SecurityError::FrameTooLong);
}
Expand Down Expand Up @@ -432,7 +432,7 @@ where
),
$encmic => aead.encrypt_in_place_detached(
&GenericArray::from_slice(&nonce),
&mut [],
&[],
auth_enc_part,
),
_ => {
Expand Down Expand Up @@ -482,15 +482,15 @@ where
#[allow(unreachable_patterns)]
_ => {}
};
return Ok(offset);
Ok(offset)
} else {
return Err(SecurityError::UnavailableKey);
Err(SecurityError::UnavailableKey)
}
} else {
return Err(SecurityError::AuxSecHeaderAbsent);
Err(SecurityError::AuxSecHeaderAbsent)
}
} else {
return Err(SecurityError::SecurityNotEnabled);
Err(SecurityError::SecurityNotEnabled)
}
}

Expand All @@ -510,7 +510,7 @@ where
///
/// # Panics
/// if footer_mode is not None due to currently absent implementation of explicit footers
pub(crate) fn unsecure_frame<'a, AEADBLKCIPH, KEYDESCLO, DEVDESCLO>(
pub(crate) fn unsecure_frame<AEADBLKCIPH, KEYDESCLO, DEVDESCLO>(
header: &Header,
buffer: &mut [u8],
context: &mut SecurityContext<AEADBLKCIPH, KEYDESCLO>,
Expand Down Expand Up @@ -618,7 +618,7 @@ where
),
$encmic => aead.decrypt_in_place_detached(
&GenericArray::from_slice(&nonce),
&mut [],
&[],
auth_enc_part,
&tag,
),
Expand Down Expand Up @@ -670,9 +670,9 @@ where
} else {
return Err(SecurityError::UnavailableKey);
}
return Ok(taglen);
Ok(taglen)
} else {
return Err(SecurityError::SecurityNotEnabled);
Err(SecurityError::SecurityNotEnabled)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/mac/frame/security/security_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl SecurityLevel {
}
}

pub(crate) fn to_bits(&self) -> u8 {
pub(crate) fn to_bits(self) -> u8 {
match self {
SecurityLevel::None => 0b000,
SecurityLevel::MIC32 => 0b001,
Expand Down Expand Up @@ -150,7 +150,7 @@ impl KeyIdentifierMode {
_ => None,
}
}
fn to_bits(&self) -> u8 {
fn to_bits(self) -> u8 {
match self {
KeyIdentifierMode::None => 0b00,
KeyIdentifierMode::KeyIndex => 0b01,
Expand Down
6 changes: 0 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ macro_rules! extended_enum {
$( $name::$var => *self == $val, )*
}
}

fn ne(&self, other: &$name) -> bool {
match *other {
$( $name::$var => *self != $val, )*
}
}
}
);
}