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

Add serde support to Error #103

Merged
merged 3 commits into from
Sep 1, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ An EtherCAT master written in Rust.
- [#91] Add support for "cross" topologies, e.g. with EK1122.
- [#102] PDU retry behaviour is now configurable between no retries, a limited count, or retrying
forever with the `RetryBehaviour` struct and associated `ClientConfig.retry_behaviour` option.
- [#103] Added optional `serde` feature to enable ser/de of some EtherCrab items.

### Changed

Expand All @@ -19,6 +20,8 @@ An EtherCAT master written in Rust.
number of bytes sent over the network.
- **(breaking)** [#101] `SendableFrame::write_ethernet_packet` is no longer `pub`. Instead, use
`SendableFrame::send_blocking` or `SendableFrame::send`.
- [#103] Removed inner `smoltcp::error::Error` from `PduError::Ethernet` and `PduError::CreateFrame`
as these don't add much meaning to the variant.

### Removed

Expand Down Expand Up @@ -188,5 +191,6 @@ An EtherCAT master written in Rust.
[#99]: https://github.com/ethercrab-rs/ethercrab/pull/99
[#101]: https://github.com/ethercrab-rs/ethercrab/pull/101
[#102]: https://github.com/ethercrab-rs/ethercrab/pull/102
[#103]: https://github.com/ethercrab-rs/ethercrab/pull/103
[0.2.0]: https://github.com/ethercrab-rs/ethercrab/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/ethercrab-rs/ethercrab/compare/fb37346...v0.1.0
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nom = { version = "7.1.3", default-features = false }
num_enum = { version = "0.6.1", default-features = false }
packed_struct = { version = "0.10.1", default-features = false }
sealed = "0.5.0"
serde = { version = "1.0.188", features = ["derive"], optional = true }
smlang = "0.6.0"
smoltcp = { version = "0.10.0", default-features = false, features = [
"proto-ipv4",
Expand Down Expand Up @@ -75,6 +76,7 @@ default = ["std"]
defmt = ["dep:defmt", "smoltcp/defmt"]
log = ["dep:log"]
std = ["pnet_datalink", "async-io", "smoltcp/phy-raw_socket", "log"]
serde = ["dep:serde"]
# Development only - DO NOT USE
bench-hacks = []

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ An EtherCAT master written in pure Rust.
- `defmt` - enable logging with the [`defmt`](https://docs.rs/defmt) crate.
- `log` - enable logging with the [`log`](https://docs.rs/log) crate. This is enabled by default
when the `std` feature is enabled.
- `serde` - enable `serde` impls for some public items.

For `no_std` targets, it is recommended to add this crate with

Expand Down
1 change: 1 addition & 0 deletions src/coe/abort_code.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Defined in ETG1000.6 Table 41 – SDO Abort Codes
#[derive(Debug, Copy, Clone, PartialEq, Eq, num_enum::FromPrimitive, num_enum::IntoPrimitive)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(u32)]
pub enum AbortCode {
/// Toggle bit not changed
Expand Down
3 changes: 3 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const LRW: u8 = 0x0c;
/// Write commands.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Writes {
/// BWR.
Bwr {
Expand Down Expand Up @@ -168,6 +169,7 @@ impl Writes {
/// Read commands that send no data.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Reads {
/// APRD.
Aprd {
Expand Down Expand Up @@ -297,6 +299,7 @@ impl Reads {
/// ```
#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Command {
/// No operation.
#[default]
Expand Down
18 changes: 12 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use packed_struct::PackingError;
/// An EtherCrab error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Error {
/// A low level error occurred when producing or consuming a PDU.
Pdu(PduError),
Expand Down Expand Up @@ -161,6 +162,7 @@ impl From<BorrowError> for Error {
/// The kind of item being looked for.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Item {
/// An EtherCAT slave device.
Slave,
Expand All @@ -181,15 +183,16 @@ pub enum Item {
/// Low-level PDU (Process Data Unit) error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum PduError {
/// Failed to decode raw PDU data into a given data type.
Decode,
/// Something went wrong when encoding/decoding the raw Ethernet II frame.
Ethernet(smoltcp::wire::Error),
Ethernet,
/// PDU data is too long to fit in the given buffer.
TooLong,
/// Failed to create an Ethernet II frame.
CreateFrame(smoltcp::wire::Error),
CreateFrame,
/// A frame index was given that does not point to a frame.
InvalidIndex(usize),
/// A received frame is invalid.
Expand All @@ -210,9 +213,9 @@ impl core::fmt::Display for PduError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
PduError::Decode => f.write_str("failed to decode raw PDU data into type"),
PduError::Ethernet(e) => write!(f, "network: {}", e),
PduError::Ethernet => f.write_str("network"),
PduError::TooLong => f.write_str("data is too long to fit in given buffer"),
PduError::CreateFrame(e) => write!(f, "failed to create frame: {}", e),
PduError::CreateFrame => f.write_str("failed to create frame"),
PduError::InvalidIndex(index) => write!(f, "invalid PDU index {}", index),
PduError::Validation(e) => write!(f, "received PDU validation failed: {}", e),
PduError::InvalidFrameState => f.write_str("invalid PDU frame state"),
Expand All @@ -224,6 +227,7 @@ impl core::fmt::Display for PduError {
/// CoE mailbox error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum MailboxError {
/// The mailbox operation was aborted.
Aborted {
Expand Down Expand Up @@ -278,6 +282,7 @@ impl core::fmt::Display for MailboxError {
/// EEPROM (SII) error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum EepromError {
/// Failed to decode data from EEPROM.
Decode,
Expand Down Expand Up @@ -331,6 +336,7 @@ impl core::fmt::Display for VisibleStringError {
/// A PDU response failed to validate.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum PduValidationError {
/// The index of the received PDU does not match that of the sent one.
IndexMismatch {
Expand Down Expand Up @@ -382,8 +388,8 @@ impl From<PduValidationError> for PduError {
}

impl From<smoltcp::wire::Error> for PduError {
fn from(e: smoltcp::wire::Error) -> Self {
Self::Ethernet(e)
fn from(_: smoltcp::wire::Error) -> Self {
Self::Ethernet
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! - `defmt` - enable logging with the [`defmt`](https://docs.rs/defmt) crate.
//! - `log` - enable logging with the [`log`](https://docs.rs/log) crate. This is enabled by default
//! when the `std` feature is enabled.
//! - `serde` - enable `serde` impls for some public items.
//!
//! For `no_std` targets, it is recommended to add this crate with
//!
Expand Down
3 changes: 2 additions & 1 deletion src/pdu_loop/frame_element/sendable_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ impl<'sto> SendableFrame<'sto> {

let buf = buf.get_mut(0..ethernet_len).ok_or(PduError::TooLong)?;

let mut ethernet_frame = EthernetFrame::new_checked(buf).map_err(PduError::CreateFrame)?;
let mut ethernet_frame =
EthernetFrame::new_checked(buf).map_err(|_| PduError::CreateFrame)?;

ethernet_frame.set_src_addr(MASTER_ADDR);
ethernet_frame.set_dst_addr(EthernetAddress::BROADCAST);
Expand Down
1 change: 1 addition & 0 deletions src/slave_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use packed_struct::prelude::*;
num_enum::IntoPrimitive,
)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(u8)]
pub enum SlaveState {
/// No state recorded/read/known.
Expand Down