Struct WirelessModemFake

Source
pub struct WirelessModemFake { /* private fields */ }

Implementations§

Source§

impl WirelessModemFake

Source

pub fn new(name: &str) -> Self

Examples found in repository?
examples/embedded_nano_mesh.rs (line 16)
9fn main() {
10    /* Create simulator, ether, and devices registered in that ether. */
11    let mut simulator = NetworkSimulator::new(1);
12
13    simulator.create_ether("1");
14    simulator.create_ether("2");
15
16    let mut driver_1 = WirelessModemFake::new("1");
17    let mut driver_2 = WirelessModemFake::new("2");
18    let mut driver_3 = WirelessModemFake::new("3");
19
20    {
21        let mut ether_1 = simulator.get_ether("1").expect("Failed to find ether 1");
22        ether_1.register_driver(driver_1.clone());
23        ether_1.register_driver(driver_2.clone());
24    }
25    {
26        let mut ether_2 = simulator.get_ether("2").expect("Failed to find ether 2");
27        ether_2.register_driver(driver_2.clone());
28        ether_2.register_driver(driver_3.clone());
29    }
30
31    /* Create tested nodes. */
32    let mut mesh_node_1 = Node::new(NodeConfig {
33        device_address: ExactAddressType::try_from(1).unwrap(),
34        listen_period: NODE_LISTEN_PERIOD,
35    });
36
37    let mut mesh_node_2 = Node::new(NodeConfig {
38        device_address: ExactAddressType::try_from(2).unwrap(),
39        listen_period: NODE_LISTEN_PERIOD,
40    });
41
42    let mut mesh_node_3 = Node::new(NodeConfig {
43        device_address: ExactAddressType::try_from(3).unwrap(),
44        listen_period: NODE_LISTEN_PERIOD,
45    });
46
47    /* Do testing scenario */
48    let _ = mesh_node_1.send_to_exact(
49        NodeString::try_from(MESSAGE)
50            .expect("Fail to pack message")
51            .into_bytes(),
52        ExactAddressType::try_from(3).expect("3 is 0"),
53        2 as LifeTimeType,
54        false,
55    );
56
57    simulator.start_simulation_thread();
58
59    let start_time = Instant::now();
60
61    loop {
62        let current_time = Instant::now().duration_since(start_time).as_millis() as ms;
63
64        let _ = mesh_node_1.update(&mut driver_1, current_time);
65        let _ = mesh_node_2.update(&mut driver_2, current_time);
66        let _ = mesh_node_3.update(&mut driver_3, current_time);
67
68        if current_time >= 200 {
69            panic!("Simulation timeout");
70        }
71
72        if let Some(packet) = mesh_node_3.receive() {
73            let got_message = NodeString::from_iter(packet.data.iter().map(|b| *b as char));
74            if got_message.starts_with(MESSAGE) {
75                break;
76            }
77        }
78    }
79
80    simulator.stop_simulation_thread();
81
82    println!("Simulation done");
83}
Source

pub fn read(&self, buf: &mut [u8]) -> Result<usize, Infallible>

Source

pub fn write(&self, buf: &[u8]) -> Result<usize, Infallible>

Source

pub fn flush(&self) -> Result<(), Infallible>

Source

pub fn clone(&self) -> Self

Examples found in repository?
examples/embedded_nano_mesh.rs (line 22)
9fn main() {
10    /* Create simulator, ether, and devices registered in that ether. */
11    let mut simulator = NetworkSimulator::new(1);
12
13    simulator.create_ether("1");
14    simulator.create_ether("2");
15
16    let mut driver_1 = WirelessModemFake::new("1");
17    let mut driver_2 = WirelessModemFake::new("2");
18    let mut driver_3 = WirelessModemFake::new("3");
19
20    {
21        let mut ether_1 = simulator.get_ether("1").expect("Failed to find ether 1");
22        ether_1.register_driver(driver_1.clone());
23        ether_1.register_driver(driver_2.clone());
24    }
25    {
26        let mut ether_2 = simulator.get_ether("2").expect("Failed to find ether 2");
27        ether_2.register_driver(driver_2.clone());
28        ether_2.register_driver(driver_3.clone());
29    }
30
31    /* Create tested nodes. */
32    let mut mesh_node_1 = Node::new(NodeConfig {
33        device_address: ExactAddressType::try_from(1).unwrap(),
34        listen_period: NODE_LISTEN_PERIOD,
35    });
36
37    let mut mesh_node_2 = Node::new(NodeConfig {
38        device_address: ExactAddressType::try_from(2).unwrap(),
39        listen_period: NODE_LISTEN_PERIOD,
40    });
41
42    let mut mesh_node_3 = Node::new(NodeConfig {
43        device_address: ExactAddressType::try_from(3).unwrap(),
44        listen_period: NODE_LISTEN_PERIOD,
45    });
46
47    /* Do testing scenario */
48    let _ = mesh_node_1.send_to_exact(
49        NodeString::try_from(MESSAGE)
50            .expect("Fail to pack message")
51            .into_bytes(),
52        ExactAddressType::try_from(3).expect("3 is 0"),
53        2 as LifeTimeType,
54        false,
55    );
56
57    simulator.start_simulation_thread();
58
59    let start_time = Instant::now();
60
61    loop {
62        let current_time = Instant::now().duration_since(start_time).as_millis() as ms;
63
64        let _ = mesh_node_1.update(&mut driver_1, current_time);
65        let _ = mesh_node_2.update(&mut driver_2, current_time);
66        let _ = mesh_node_3.update(&mut driver_3, current_time);
67
68        if current_time >= 200 {
69            panic!("Simulation timeout");
70        }
71
72        if let Some(packet) = mesh_node_3.receive() {
73            let got_message = NodeString::from_iter(packet.data.iter().map(|b| *b as char));
74            if got_message.starts_with(MESSAGE) {
75                break;
76            }
77        }
78    }
79
80    simulator.stop_simulation_thread();
81
82    println!("Simulation done");
83}

Trait Implementations§

Source§

impl ErrorType for WirelessModemFake

Source§

type Error = Infallible

Error type of all the IO operations on this type.
Source§

impl IODriverSimulator for WirelessModemFake

Source§

fn get_from_device_network_side(&self) -> Option<u8>

Simulates that the modem emits a byte towards the ether

use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;

let device = WirelessModemFake::new("my_modem");
device.start_tick();
assert_eq!(device.get_from_device_network_side(), None);
device.end_tick();

device.put_to_rx_pin(1);

device.start_tick();
assert_eq!(device.get_from_device_network_side(), Some(1));
device.end_tick();
Source§

fn put_to_device_network_side(&self, byte: u8)

Simulates that the modem caught a byte from the ether

use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;

let device = WirelessModemFake::new("my_modem");
assert_eq!(device.get_from_tx_pin(), None);
device.start_tick();
device.put_to_device_network_side(1);
device.end_tick();
assert_eq!(device.get_from_tx_pin(), Some(1));
Source§

fn get_from_tx_pin(&self) -> Option<u8>

Reads a byte on the TX pin

use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;

let device = WirelessModemFake::new("my_modem");
assert_eq!(device.get_from_tx_pin(), None);
device.start_tick();
device.put_to_device_network_side(1);
device.end_tick();
assert_eq!(device.get_from_tx_pin(), Some(1));
Source§

fn put_to_rx_pin(&self, byte: u8)

Writes a byte on the RX pin

use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;

let device = WirelessModemFake::new("my_modem");
device.put_to_rx_pin(1);
device.start_tick();
assert_eq!(device.get_from_device_network_side(), Some(1));
device.end_tick();
Source§

fn start_tick(&self)

Tick is needed only for simulating time during which ineraction with the ether is going. Other operations like put to pin or get from pin can be done not in tick.

Source§

fn end_tick(&self)

Tick is needed only for simulating time during which ineraction with the ether is going. Other operations like put to pin or get from pin can be done not in tick.

Source§

fn readable(&self) -> bool

Tells if the device has some bytes to be red from pin

use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;
let mut device = WirelessModemFake::new("");
assert!(
!device.readable());
device.start_tick();
device.put_to_device_network_side(1);
device.end_tick();
assert!(device.readable());
Source§

fn writable(&self) -> bool

Tells if the device is ready to be written in

use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;
assert!(WirelessModemFake::new("").writable());
Source§

fn get_name(&self) -> &str

Returns the name of the device

use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;
let device = WirelessModemFake::new("my_modem");
assert_eq!(device.get_name(), "my_modem");
Source§

impl Read for WirelessModemFake

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
Source§

impl ReadReady for WirelessModemFake

Source§

fn read_ready(&mut self) -> Result<bool, Self::Error>

Get whether the reader is ready for immediately reading. Read more
Source§

impl Write for WirelessModemFake

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, blocking until all intermediately buffered contents reach their destination.
Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer. Read more
Source§

fn write_fmt( &mut self, fmt: Arguments<'_>, ) -> Result<(), WriteFmtError<Self::Error>>

Write a formatted string into this writer, returning any error encountered. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.