Skip to content

Commit

Permalink
Add defmt::Format to some types (esp-rs#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani authored and bjoernQ committed May 23, 2024
1 parent 0d79d7a commit 43c93c6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
2 changes: 2 additions & 0 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
defmt = { workspace = true, optional = true }
embedded-hal.workspace = true
esp32c3-hal = { workspace = true, optional = true }
esp32c2-hal = { workspace = true, optional = true }
Expand Down Expand Up @@ -74,3 +75,4 @@ ps-min-modem = []
esp-now = [ "wifi" ]
big-heap = []
ipv6 = ["smoltcp?/proto-ipv6"]
defmt = ["dep:defmt", "smoltcp?/defmt"]
1 change: 1 addition & 0 deletions esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static BT_RECEIVE_QUEUE: Mutex<RefCell<SimpleQueue<ReceivedPacket, 10>>> =
Mutex::new(RefCell::new(SimpleQueue::new()));

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ReceivedPacket {
pub len: u8,
pub data: [u8; 256],
Expand Down
1 change: 1 addition & 0 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static BT_RECEIVE_QUEUE: Mutex<RefCell<SimpleQueue<ReceivedPacket, 10>>> =
type OsMembufT = u32;

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ReceivedPacket {
pub len: u8,
pub data: [u8; 256],
Expand Down
47 changes: 29 additions & 18 deletions esp-wifi/src/esp_now/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use core::{cell::RefCell, fmt::Debug};

use atomic_polyfill::{AtomicBool, Ordering};
use critical_section::Mutex;
use atomic_polyfill::{Ordering, AtomicBool};
use esp_hal_common::peripheral::{Peripheral, PeripheralRef};

use crate::compat::queue::SimpleQueue;
Expand All @@ -27,7 +27,7 @@ static RECEIVE_QUEUE: Mutex<RefCell<SimpleQueue<ReceivedData, 10>>> =
Mutex::new(RefCell::new(SimpleQueue::new()));
/// This atomic behaves like a guard, so we need strict memory ordering when
/// operating it.
///
///
/// This flag indicates whether the send callback has been called after a sending.
static ESP_NOW_SEND_CB_INVOKED: AtomicBool = AtomicBool::new(false);
/// Status of esp now send, true for success, false for failure
Expand All @@ -37,13 +37,14 @@ macro_rules! check_error {
($block:block) => {
match unsafe { $block } {
0 => Ok(()),
res => Err(EspNowError::Error(Error::from_code(res as u32)))
res => Err(EspNowError::Error(Error::from_code(res as u32))),
}
};
}

#[repr(u32)]
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
NotInitialized = 12389,
InvalidArgument = 12390,
Expand Down Expand Up @@ -73,17 +74,20 @@ impl Error {
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum EspNowError {
Error(Error),
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PeerCount {
pub total_count: i32,
pub encrypted_count: i32,
}

#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WifiPhyRate {
/// < 1 Mbps with long preamble
Rate1mL = 0,
Expand Down Expand Up @@ -156,6 +160,7 @@ pub enum WifiPhyRate {
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PeerInfo {
pub peer_address: [u8; 6],
pub lmk: Option<[u8; 16]>,
Expand All @@ -166,6 +171,7 @@ pub struct PeerInfo {

#[cfg(not(any(esp32c6)))]
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RxControlInfo {
pub rssi: i32,
pub rate: u32,
Expand All @@ -190,6 +196,7 @@ pub struct RxControlInfo {

#[cfg(any(esp32c6))]
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RxControlInfo {
pub rssi: i32,
pub rate: u32,
Expand All @@ -214,13 +221,15 @@ pub struct RxControlInfo {
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ReceiveInfo {
pub src_address: [u8; 6],
pub dst_address: [u8; 6],
pub rx_control: RxControlInfo,
}

#[derive(Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ReceivedData {
pub len: u8,
pub data: [u8; 256],
Expand Down Expand Up @@ -448,11 +457,11 @@ impl<'d> EspNow<'d> {
/// Send data to peer
///
/// The peer needs to be added to the peer list first.
///
/// This method returns a `SendWaiter` on success. ESP-NOW protocol provides guaranteed
/// delivery on MAC layer. If you need this guatantee, call `wait` method of the returned
/// `SendWaiter` and make sure it returns `SendStatus::Success`.
/// However, this method will block current task for milliseconds.
///
/// This method returns a `SendWaiter` on success. ESP-NOW protocol provides guaranteed
/// delivery on MAC layer. If you need this guarantee, call `wait` method of the returned
/// `SendWaiter` and make sure it returns `SendStatus::Success`.
/// However, this method will block current task for milliseconds.
/// So you can just drop the waiter if you want high frequency sending.
pub fn send(&self, dst_addr: &[u8; 6], data: &[u8]) -> Result<SendWaiter, EspNowError> {
let mut addr = [0u8; 6];
Expand Down Expand Up @@ -483,20 +492,21 @@ impl Drop for EspNow<'_> {

/// This is essentially [esp_now_send_status_t](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html#_CPPv421esp_now_send_status_t)
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SendStatus {
Success,
Failed
Failed,
}

/// This struct is returned by a sync esp now send. Invoking `wait` method of this
/// struct will block current task until the callback function of esp now send is called
/// and return the status of previous sending.
/// and return the status of previous sending.
pub struct SendWaiter(());

impl SendWaiter {
/// Wait for the previous sending to complete, i.e. the send callback is invoked with
/// Wait for the previous sending to complete, i.e. the send callback is invoked with
/// status of the sending.
///
///
/// Note: if you firstly dropped waiter of a sending and then wait for a following sending,
/// you probably get unreliable status because we cannot determine which sending the waited status
/// belongs to.
Expand All @@ -511,14 +521,11 @@ impl SendWaiter {
}
}

unsafe extern "C" fn send_cb(
_mac_addr: *const u8,
status: esp_now_send_status_t
) {
unsafe extern "C" fn send_cb(_mac_addr: *const u8, status: esp_now_send_status_t) {
critical_section::with(|_| {
let is_success = status == esp_now_send_status_t_ESP_NOW_SEND_SUCCESS;
ESP_NOW_SEND_STATUS.store(is_success, Ordering::Relaxed);

ESP_NOW_SEND_CB_INVOKED.store(true, Ordering::Release);

#[cfg(feature = "async")]
Expand Down Expand Up @@ -642,7 +649,11 @@ mod asynch {
ReceiveFuture.await
}

pub fn send_async(&self, dst_addr: &[u8; 6], data: &[u8]) -> Result<SendFuture, EspNowError> {
pub fn send_async(
&self,
dst_addr: &[u8; 6],
data: &[u8],
) -> Result<SendFuture, EspNowError> {
let mut addr = [0u8; 6];
addr.copy_from_slice(dst_addr);
ESP_NOW_SEND_CB_INVOKED.store(false, Ordering::Release);
Expand Down
4 changes: 4 additions & 0 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ pub type EspWifiTimer = Alarm<Target, 0>;
pub type EspWifiTimer = hal::timer::Timer<hal::timer::Timer0<hal::peripherals::TIMG1>>;

#[derive(Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub struct EspWifiInitializationInternal;

#[derive(Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum EspWifiInitialization {
#[cfg(feature = "wifi")]
Wifi(EspWifiInitializationInternal),
Expand Down Expand Up @@ -202,6 +204,7 @@ impl EspWifiInitialization {
}

#[derive(Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum EspWifiInitFor {
#[cfg(feature = "wifi")]
Wifi,
Expand Down Expand Up @@ -336,6 +339,7 @@ pub fn initialize(
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum InitializationError {
General(i32),
#[cfg(feature = "wifi")]
Expand Down
4 changes: 4 additions & 0 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ use crate::{
use log::debug;

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WifiMode {
Sta,
Ap,
Expand Down Expand Up @@ -177,6 +178,7 @@ pub(crate) static DATA_QUEUE_TX: Mutex<RefCell<SimpleQueue<DataFrame, TX_QUEUE_S
Mutex::new(RefCell::new(SimpleQueue::new()));

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WifiError {
NotInitialized,
InternalError(InternalWifiError),
Expand All @@ -186,6 +188,7 @@ pub enum WifiError {
}
#[repr(i32)]
#[derive(Debug, FromPrimitive, EnumSetType)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WifiEvent {
WifiReady = 0,
ScanDone,
Expand Down Expand Up @@ -213,6 +216,7 @@ pub enum WifiEvent {

#[repr(i32)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum InternalWifiError {
///Out of memory
EspErrNoMem = 0x101,
Expand Down
2 changes: 2 additions & 0 deletions esp-wifi/src/wifi_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ impl<'a> WifiStack<'a> {
}

#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WifiStackError {
Unknown(i32),
InitializationError(crate::InitializationError),
Expand Down Expand Up @@ -605,6 +606,7 @@ impl<'s, 'n: 's> Drop for Socket<'s, 'n> {
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IoError {
SocketClosed,
MultiCastError(smoltcp::iface::MulticastError),
Expand Down

0 comments on commit 43c93c6

Please sign in to comment.