1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! The raw dispatcher does not interpret the data in the packet.
//!
//! It can be used as a debug dispatcher when a dispatcher does not exist
//! for a particular packet.
use std::convert::From;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};

use crate::{Bytes, Event};
use super::{Dispatcher, DispatcherHandle};

/// Handles the dispatching of raw packets without interpreting the contents.
pub struct RawDispatcher {
    dispatch: u8,
    /// The receiver
    pub rx: Receiver<Event<Bytes>>,
    /// The sender
    pub tx: Sender<Event<Bytes>>,
    handle: Option<DispatcherHandle>,
}

impl RawDispatcher {
    /// Creates a raw packet dispatcher
    pub fn new(dispatch: u8) -> RawDispatcher {
        let (transport_tx, rx) = mpsc::channel();
        let (tx, transport_rx) = mpsc::channel();
        RawDispatcher {
            dispatch,
            rx,
            tx,
            handle: Some(DispatcherHandle::new(transport_tx, transport_rx)),
        }
    }

    /// Returns the receiver for data from the serial device.
    pub fn rx(&self) -> &Receiver<Event<Bytes>> {
        &self.rx
    }

    /// Returns the sender for data to the serial device.
    pub fn tx(&self) -> &Sender<Event<Bytes>> {
        &self.tx
    }
}

impl Dispatcher for RawDispatcher {
    fn dispatch_byte(&self) -> u8 {
        self.dispatch
    }

    fn get_handle(&mut self) -> DispatcherHandle {
        self.handle.take().unwrap()
    }
}

/// A raw packet payload
pub struct Packet {
    /// The payload of the packet
    pub payload: Vec<u8>,
}

// TODO(Kaarel): Technically, there are limits to payload sizes
impl From<Vec<u8>> for Packet {
    fn from(bytes: Vec<u8>) -> Self {
        Packet { payload: bytes }
    }
}