Skip to content
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
58 changes: 36 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
[package]
name = "radio"
description = "Generic traits for embedded packet radio devices"
repository = "https://github.com/rust-iot/radio-hal"
authors = ["Ryan Kurte <[email protected]>"]
repository = "https://github.com/annie444/radio-hal"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably don't need to change this

authors = [
"Ryan Kurte <[email protected]>",
"Annie Ehler <[email protected]",
]
license = "MIT"
edition = "2018"
edition = "2024"
version = "0.12.1"

[package.metadata.docs.rs]
features = [ "std", "nonblocking", "mock", "helpers" ]
features = ["std", "nonblocking", "mock", "helpers", "log", "clap"]

[features]
std = [ ]
nonblocking = [ ]
mock = [ "embedded-hal-mock" ]
helpers = [ "clap", "humantime", "std", "pcap-file", "libc", "byteorder", "rolling-stats" ]
default = [ ]
std = ["dep:humantime"]
nonblocking = []
mock = ["dep:embedded-hal-mock", "std", "log"]
helpers = [
"clap",
"dep:humantime",
"std",
"dep:pcap-file",
"dep:libc",
"dep:byteorder",
"dep:rolling-stats",
"log",
]
default = []
log = ["dep:log"]
clap = ["dep:clap", "std"]

[dependencies]
embedded-hal = "1.0.0"
embedded-hal-mock = { version = "0.10.0", optional = true }
nb = "1.0.0"
embedded-hal-mock = { version = "0.11.1", optional = true }
nb = "1.1.0"

log = { version = "0.4.14", default_features = false }
defmt = { version = "0.3.0", optional = true }
log = { version = "0.4.27", default-features = false, optional = true }
defmt = { version = "1.0.1", optional = true }

chrono = { version = "0.4.19", default_features = false }
humantime = { version = "2.0.1", optional = true }
pcap-file = { version = "1.1.1", optional = true }
async-std = { version = "1.4.0", optional = true }
libc = { version = "0.2.71", optional = true }
byteorder = { version = "1.3.4", optional = true }
chrono = { version = "0.4.41", default-features = false }
humantime = { version = "2.2.0", optional = true }
pcap-file = { version = "2.0.0", optional = true }
async-std = { version = "1.13.1", optional = true }
libc = { version = "0.2.172", optional = true }
byteorder = { version = "1.5.0", optional = true }
rolling-stats = { version = "0.7.0", optional = true }
thiserror = { version = "1.0.30", optional = true }
clap = { version = "4.4.7", optional = true, features = [ "derive" ] }
thiserror = { version = "2.0.12", optional = true }
clap = { version = "4.5.38", optional = true, features = ["derive"] }

[dev-dependencies]
anyhow = "1.0.44"
anyhow = "1.0.98"
25 changes: 25 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
check:
#!/usr/bin/env python3
import subprocess
import itertools
esc_start = "\033["
esc_end = "m"
esc_join = ";"
bold = "1"
bold_reset = "22"
cyan_fg = "36"
fg_color_reset = "39"
tq_out = subprocess.Popen(["tq", ".features", "--file=Cargo.toml", "--output=json"], stdout=subprocess.PIPE)
jq_out = subprocess.Popen(["jq", "--raw-output", "--monochrome-output", ". | keys | .[]", "-"], stdin=tq_out.stdout, stdout=subprocess.PIPE)
tq_out.stdout.close()
features, errors = jq_out.communicate()
features = list(filter(lambda x : x != '', features.decode().split("\n")))
print(features)
for i in range(len(features) + 1):
for subset in itertools.combinations(features, i):
feats = ",".join(list(subset))
print()
print(esc_start + esc_join.join([bold, cyan_fg]) + esc_end + feats + esc_start + esc_join.join([bold_reset, fg_color_reset]) + esc_end)
print()
check = subprocess.Popen(["cargo", "check", "-F", feats], shell=False)
check.communicate()
10 changes: 5 additions & 5 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use embedded_hal::delay::DelayNs;
#[cfg(feature = "defmt")]
use defmt::debug;

#[cfg(all(feature = "log", not(feature = "defmt")))]
use log::debug;

#[cfg(feature = "clap")]
use clap::Parser;

#[cfg(feature = "std")]
use std::string::ToString;

use crate::{Receive, State, Transmit};

/// BlockingOptions for blocking radio functions
Expand Down Expand Up @@ -70,7 +70,7 @@ impl<E> From<E> for BlockingError<E> {
```
# use radio::*;
# use radio::mock::*;
use radio::{BlockingTransmit, BlockingOptions};
use radio::blocking::{BlockingTransmit, BlockingOptions};

# let mut radio = MockRadio::new(&[
# Transaction::start_transmit(vec![0xaa, 0xbb], None),
Expand Down Expand Up @@ -144,7 +144,7 @@ where
```
# use radio::*;
# use radio::mock::*;
use radio::{BlockingReceive, BlockingOptions};
use radio::blocking::{BlockingReceive, BlockingOptions};

let data = [0xaa, 0xbb];
let info = BasicInfo::new(-81, 0);
Expand Down
25 changes: 13 additions & 12 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::time::SystemTime;

use libc::{self};

#[cfg(not(feature = "defmt"))]
#[cfg(all(not(feature = "defmt"), feature = "log"))]
use log::{debug, info};

#[cfg(feature = "defmt")]
Expand All @@ -22,13 +22,15 @@ use embedded_hal::delay::DelayNs;
use humantime::Duration as HumanDuration;

use byteorder::{ByteOrder, NetworkEndian};
use pcap_file::{pcap::PcapHeader, DataLink, PcapWriter};
use pcap_file::{
DataLink,
pcap::{PcapHeader, PcapPacket, PcapWriter},
};
use rolling_stats::Stats;

use crate::*;
use crate::{
blocking::{BlockingError, BlockingOptions, BlockingReceive, BlockingTransmit},
Power, Receive, ReceiveInfo, Rssi, Transmit,
blocking::{BlockingError, BlockingOptions, BlockingReceive, BlockingTransmit},
};

/// Basic operations supported by the helpers package
Expand Down Expand Up @@ -202,7 +204,7 @@ impl PcapOptions {
h.datalink = DataLink::IEEE802_15_4;

// Write header
let w = PcapWriter::with_header(h, f).expect("Error writing to PCAP file");
let w = PcapWriter::with_header(f, h).expect("Error writing to PCAP file");
Some(w)
}
};
Expand Down Expand Up @@ -248,13 +250,8 @@ where
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();

p.write(
t.as_secs() as u32,
t.as_nanos() as u32 % 1_000_000,
&buff[0..n],
n as u32,
)
.expect("Error writing pcap file");
p.write_packet(&PcapPacket::new(t, n as u32, &buff[0..n]))
.expect("Error writing pcap file");
}

if !options.continuous {
Expand Down Expand Up @@ -444,6 +441,7 @@ where
NetworkEndian::write_u32(&mut buff[0..], i as u32);
let n = 4;

#[cfg(any(feature = "log", feature = "defmt"))]
debug!("Sending message {}", i);

// Send message
Expand All @@ -453,6 +451,7 @@ where
let (n, info) = match radio.do_receive(&mut buff, options.blocking_options.clone()) {
Ok(r) => r,
Err(BlockingError::Timeout) => {
#[cfg(any(feature = "log", feature = "defmt"))]
debug!("Timeout awaiting response {}", i);
continue;
}
Expand All @@ -461,6 +460,7 @@ where

let receive_index = NetworkEndian::read_u32(&buff[0..n]);
if receive_index != i {
#[cfg(any(feature = "log", feature = "defmt"))]
debug!("Invalid receive index");
continue;
}
Expand All @@ -471,6 +471,7 @@ where
false => None,
};

#[cfg(any(feature = "log", feature = "defmt"))]
debug!(
"Received response {} with local rssi: {} and remote rssi: {:?}",
receive_index,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
impl Default for BasicInfo {
fn default() -> Self {
Self {
rssi: core::i16::MIN,

Check warning on line 99 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

usage of a legacy numeric constant
lqi: core::u16::MIN,

Check warning on line 100 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

usage of a legacy numeric constant
}
}
}
Expand Down Expand Up @@ -254,7 +254,7 @@
use std::str::FromStr;

#[cfg(feature = "std")]
fn duration_from_str(s: &str) -> Result<core::time::Duration, humantime::DurationError> {
pub fn duration_from_str(s: &str) -> Result<core::time::Duration, humantime::DurationError> {
let d = humantime::Duration::from_str(s)?;
Ok(*d)
}
Expand Down
15 changes: 11 additions & 4 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//! ## <https://github.com/rust-iot/radio-hal>
//! ## Copyright 2020-2022 Ryan Kurte

extern crate std;
use std::convert::Infallible;
use std::fmt::Debug;
use std::vec::Vec;

Expand Down Expand Up @@ -52,8 +50,8 @@ where
Self { inner }
}

pub fn expect(&mut self, expectations: &[Transaction<St, Reg, Ch, Inf, Irq, E>]) {
self.inner.expect(expectations);
pub fn update_expectations(&mut self, expectations: &[Transaction<St, Reg, Ch, Inf, Irq, E>]) {
self.inner.update_expectations(expectations);
}

pub fn next(&mut self) -> Option<Transaction<St, Reg, Ch, Inf, Irq, E>> {
Expand Down Expand Up @@ -223,6 +221,15 @@ impl<St, Reg, Ch, Inf, Irq, E> Transaction<St, Reg, Ch, Inf, Irq, E> {
response: Response::Ok,
}
}

/// Delay for a certain time
pub fn delay_us(us: u32) -> Self {
let ns = us * 1000;
Self {
request: Request::DelayNs(ns),
response: Response::Ok,
}
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
Loading