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
impl Node
sourcepub fn new(address: Address) -> Self
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
sourcepub fn reset(&mut self) -> StateToken
pub fn reset(&mut self) -> StateToken
Obtain a new StateToken by resetting the protocol state to “receive data”.
sourcepub fn state(&mut self, token: StateToken) -> NodeState<'_>
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.
sourcepub fn no_reply(&mut self, _token: StateToken) -> StateToken
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more