Struct x328_proto::node::Node

source ·
pub struct Node { /* private fields */ }
Expand description

Bus node (listener/server) part of the X3.28 protocol

Create a new protocol instance with Node::new(address). The current protocol state can be retrieved by calling state(). The NodeState enum returned contains structs that should be acted upon in order to advance the protocol state machine.

Example

use x328_proto::node::{Node, NodeState};
use x328_proto::{addr, Value};
let mut node = Node::new(addr(10)); // new protocol instance with address 10
let mut serial = connect_serial_interface()?;
let mut token = node.reset();

'main: loop {
       match node.state(token) {
           NodeState::ReceiveData(recv) => {
               let mut buf = [0; 1];
               if let Ok(len) = serial.read(&mut buf) {
                   if len == 0 {
                       break 'main;
                   }
                   token = recv.receive_data(&buf[..len]);
               } else {
                   break 'main;
               }
           }

           NodeState::SendData(mut send) => {
               serial.write_all(send.send_data()).unwrap();
           }

           NodeState::ReadParameter(read_command) => {
               if read_command.parameter() == 3 {
                   read_command.send_invalid_parameter();
               } else {
                   read_command.send_reply_ok(4u16.into());
               }
           }

           NodeState::WriteParameter(write_command) => {
               let param = write_command.parameter();
               if param == 3 {
                   write_command.write_error();
               } else {
                   write_command.write_ok();
               }
           }
       };
}

Implementations§

source§

impl Node

source

pub fn new(address: Address) -> Self

Create a new protocol instance, accepting commands for the given address.

Example
use x328_proto::{addr, node::Node};
let mut node = Node::new(addr(10)); // new protocol instance with address 10
source

pub fn reset(&mut self) -> StateToken

Obtain a new StateToken by resetting the protocol state to “receive data”.

source

pub fn state(&mut self, token: StateToken) -> NodeState<'_>

Returns the current protocol state. Act on the inner structs in order to advance the protocol state machine.

source

pub fn no_reply(&mut self, _token: StateToken) -> StateToken

Do not send any reply to the bus controller. Transition to the idle ReceiveData state instead. You should avoid this, since this will leave the controller waiting until it times out.

Trait Implementations§

source§

impl Debug for Node

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl UnwindSafe for Node

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.