wpan: use builtin conversion methods

This commit is contained in:
xoviat 2023-07-16 13:59:15 -05:00
parent e95a7dc555
commit 7c465465c1
5 changed files with 15 additions and 25 deletions

View File

@ -1,4 +1,3 @@
use super::helpers::to_u16;
use super::indications::{
AssociateIndication, BeaconNotifyIndication, CommStatusIndication, DataIndication, DisassociateIndication,
DpsIndication, GtsIndication, OrphanIndication, PollIndication, SyncLossIndication,
@ -58,7 +57,8 @@ impl TryFrom<&[u8]> for MacEvent {
type Error = ();
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let opcode = to_u16(&value[0..2]);
let opcode = u16::from_le_bytes(value[0..2].try_into().unwrap());
let opcode = OpcodeM0ToM4::try_from(opcode)?;
let buf = &value[2..];

View File

@ -1,7 +0,0 @@
pub fn to_u16(buf: &[u8]) -> u16 {
((buf[1] as u16) << 8) | buf[0] as u16
}
pub fn to_u32(buf: &[u8]) -> u32 {
((buf[0] as u32) << 0) + ((buf[1] as u32) << 8) + ((buf[2] as u32) << 16) + ((buf[3] as u32) << 24)
}

View File

@ -1,6 +1,5 @@
use super::consts::MAX_PENDING_ADDRESS;
use super::event::ParseableMacEvent;
use super::helpers::to_u32;
use super::typedefs::{
AddressMode, Capabilities, DisassociationReason, KeyIdMode, MacAddress, MacChannel, MacStatus, PanDescriptor,
PanId, SecurityLevel,
@ -114,7 +113,7 @@ impl ParseableMacEvent for BeaconNotifyIndication {
];
Ok(Self {
sdu_ptr: to_u32(&buf[0..4]) as *const u8,
sdu_ptr: u32::from_le_bytes(buf[0..4].try_into().unwrap()) as *const u8,
pan_descriptor: PanDescriptor::try_from(&buf[4..26])?,
addr_list,
bsn: buf[82],
@ -405,7 +404,7 @@ impl ParseableMacEvent for DataIndication {
};
Ok(Self {
msdu_ptr: to_u32(&buf[0..4]) as *const u8,
msdu_ptr: u32::from_le_bytes(buf[0..4].try_into().unwrap()) as *const u8,
src_addr_mode,
src_pan_id: PanId([buf[5], buf[6]]),
src_address,
@ -424,10 +423,10 @@ impl ParseableMacEvent for DataIndication {
uwn_preamble_symbol_repetitions: buf[45],
datrate: buf[46],
ranging_received: buf[47],
ranging_counter_start: to_u32(&buf[48..52]),
ranging_counter_stop: to_u32(&buf[52..56]),
ranging_tracking_interval: to_u32(&buf[56..60]),
ranging_offset: to_u32(&buf[60..64]),
ranging_counter_start: u32::from_le_bytes(buf[48..52].try_into().unwrap()),
ranging_counter_stop: u32::from_le_bytes(buf[52..56].try_into().unwrap()),
ranging_tracking_interval: u32::from_le_bytes(buf[56..60].try_into().unwrap()),
ranging_offset: u32::from_le_bytes(buf[60..64].try_into().unwrap()),
ranging_fom: buf[65],
rssi: buf[66],
})

View File

@ -1,7 +1,6 @@
pub mod commands;
mod consts;
pub mod event;
mod helpers;
pub mod indications;
mod macros;
mod opcodes;

View File

@ -1,6 +1,5 @@
use super::consts::{MAX_ED_SCAN_RESULTS_SUPPORTED, MAX_PAN_DESC_SUPPORTED, MAX_SOUNDING_LIST_SUPPORTED};
use super::event::ParseableMacEvent;
use super::helpers::to_u32;
use super::typedefs::{
AddressMode, AssociationStatus, KeyIdMode, MacAddress, MacStatus, PanDescriptor, PanId, PibId, ScanType,
SecurityLevel,
@ -100,7 +99,7 @@ impl ParseableMacEvent for GetConfirm {
fn try_parse(buf: &[u8]) -> Result<Self, ()> {
Self::validate(buf)?;
let address = to_u32(&buf[0..4]);
let address = u32::from_le_bytes(buf[0..4].try_into().unwrap());
Ok(Self {
pib_attribute_value_ptr: address as *const u8,
@ -357,8 +356,8 @@ impl ParseableMacEvent for CalibrateConfirm {
Ok(Self {
status: MacStatus::try_from(buf[0])?,
// 3 byte stuffing
cal_tx_rmaker_offset: to_u32(&buf[4..8]),
cal_rx_rmaker_offset: to_u32(&buf[8..12]),
cal_tx_rmaker_offset: u32::from_le_bytes(buf[4..8].try_into().unwrap()),
cal_rx_rmaker_offset: u32::from_le_bytes(buf[8..12].try_into().unwrap()),
})
}
}
@ -400,10 +399,10 @@ impl ParseableMacEvent for DataConfirm {
time_stamp: [buf[1], buf[2], buf[3], buf[4]],
ranging_received: buf[5],
status: MacStatus::try_from(buf[6])?,
ranging_counter_start: to_u32(&buf[7..11]),
ranging_counter_stop: to_u32(&buf[11..15]),
ranging_tracking_interval: to_u32(&buf[15..19]),
ranging_offset: to_u32(&buf[19..23]),
ranging_counter_start: u32::from_le_bytes(buf[7..11].try_into().unwrap()),
ranging_counter_stop: u32::from_le_bytes(buf[11..15].try_into().unwrap()),
ranging_tracking_interval: u32::from_le_bytes(buf[15..19].try_into().unwrap()),
ranging_offset: u32::from_le_bytes(buf[19..23].try_into().unwrap()),
ranging_fom: buf[24],
})
}