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

Make AlStatus conversion infallible #119

Merged
merged 4 commits into from
Oct 12, 2023
Merged

Conversation

jamwaffles
Copy link
Collaborator

@jamwaffles jamwaffles commented Oct 12, 2023

No description provided.

@jamwaffles
Copy link
Collaborator Author

I started an implementation but realised this doesn't affect the public API. I'll paste the code here for posterity

impl SlaveState {
    /// Get a set of bit flags representing potentially multiple states in the network.
    pub fn status_map(&self) -> SlaveStates {
        match self {
            SlaveState::None | SlaveState::Bootstrap => SlaveStates::NONE,
            SlaveState::Init => SlaveStates::INIT,
            SlaveState::PreOp => SlaveStates::PRE_OP,
            SlaveState::SafeOp => SlaveStates::SAFE_OP,
            SlaveState::Op => SlaveStates::OP,
            SlaveState::Other(value) => SlaveStates::from_bits_truncate(*value),
        }
    }
}

bitflags::bitflags! {
    /// AL (application layer) state for one or more devices.
    ///
    /// Read from register `0x0130` ([`RegisterAddress::AlStatus`](crate::register::RegisterAddress::AlStatus)).
    ///
    /// Defined in ETG1000.6 6.4.1, ETG1000.6 Table 9.
    #[derive(
        Debug,
        Default,
        Copy,
        Clone,
        PartialEq,
        Eq,
    )]
    pub struct SlaveStates: u8 {
        /// No state recorded/read/known.
        const NONE = 0x00;
        /// EtherCAT `INIT` state.
        const INIT = 0x01;
        /// EtherCAT `PRE-OP` state.
        const PRE_OP = 0x02;
        /// EtherCAT `SAFE-OP` state.
        const SAFE_OP = 0x04;
        /// EtherCAT `OP` state.
        const OP = 0x8;

    }
}

#[cfg(feature = "defmt")]
impl defmt::Format for SlaveStates {
    fn format(&self, fmt: defmt::Formatter) {
        defmt::write!(fmt, "SlaveStates({:02x})", self.bits());
    }
}

impl PduRead for SlaveStates {
    const LEN: u16 = u8::LEN;

    type Error = core::convert::Infallible;

    fn try_from_slice(slice: &[u8]) -> Result<Self, Self::Error> {
        Ok(Self::from_bits_truncate(slice[0]))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn known_status() {
        let expected = SlaveStates::PRE_OP | SlaveStates::SAFE_OP;

        let res = SlaveStates::try_from_slice(&[0x02 | 0x04]);

        assert_eq!(res, Ok(expected));
    }

    #[test]
    fn unknown_status() {
        let res = SlaveStates::try_from_slice(&[0xf0]);

        assert_eq!(res, Ok(SlaveStates::NONE));
    }

    #[test]
    fn unknown_status_and_op() {
        let expected = SlaveStates::OP;

        let res = SlaveStates::try_from_slice(&[0x08 | 0xf0]);

        assert_eq!(res, Ok(expected));
    }
}

@jamwaffles jamwaffles marked this pull request as ready for review October 12, 2023 20:27
@jamwaffles jamwaffles changed the title Add SlaveStates bitflags struct Make AlStatus conversion infallible Oct 12, 2023
@jamwaffles jamwaffles enabled auto-merge (squash) October 12, 2023 20:33
@jamwaffles jamwaffles merged commit ecaf165 into master Oct 12, 2023
@jamwaffles jamwaffles deleted the network-status-bitflags branch October 12, 2023 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant