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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
pub use platform_serial::PlatformSerial;

use serial_embedded_hal::*;

pub use serial_embedded_hal::{BaudRate, CharSize, FlowControl, Parity, PortSettings, StopBits};

use core::default::Default;
use lazy_static::lazy_static;
use std::cell::Cell;
use std::sync::Mutex;
use std::sync::Once;

lazy_static! {
    static ref CONFIGURATION: Mutex<Option<PortSettings>> = Mutex::new(None);
    static ref PORT_PATH: Mutex<Option<String>> = Mutex::new(None);
    static ref SERIAL: Mutex<Cell<Option<HiddenSerialParts>>> = Mutex::new(Cell::new(None));
    static ref INIT_ONCE: Once = Once::new();
}

const SERIAL_ERR_MSG: &str = "Serial not initialized";

pub fn configure_serial(port_path: String, configuration: PortSettings) {
    INIT_ONCE.call_once(|| {
        let mut port = PORT_PATH.lock().unwrap();
        *port = Some(port_path.clone());
        let mut config = CONFIGURATION.lock().unwrap();
        *config = Some(configuration);

        let serial = Serial::new(&port_path, &configuration)
            .expect("Could not initialize port with given configuration");

        let (writer, reader) = serial.split();

        let mut serial_ref = SERIAL.lock().unwrap();
        *serial_ref = Cell::new(Some(HiddenSerialParts { reader, writer }));
    });
}

struct HiddenSerialParts {
    pub reader: serial_embedded_hal::Rx,
    pub writer: serial_embedded_hal::Tx,
}

pub struct LinuxSerial;

impl Default for LinuxSerial {
    fn default() -> Self {
        LinuxSerial
    }
}

impl embedded_hal::serial::Read<u8> for LinuxSerial {
    type Error = serial_core::Error;

    fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
        SERIAL
            .lock()
            .as_mut()
            .expect(SERIAL_ERR_MSG)
            .get_mut()
            .as_mut()
            .expect(SERIAL_ERR_MSG)
            .reader
            .read()
    }
}

impl embedded_hal::serial::Write<u8> for LinuxSerial {
    type Error = serial_core::Error;

    fn write(&mut self, byte: u8) -> Result<(), nb::Error<Self::Error>> {
        SERIAL
            .lock()
            .as_mut()
            .expect(SERIAL_ERR_MSG)
            .get_mut()
            .as_mut()
            .expect(SERIAL_ERR_MSG)
            .writer
            .write(byte)
    }

    fn flush(&mut self) -> Result<(), nb::Error<Self::Error>> {
        SERIAL
            .lock()
            .as_mut()
            .expect(SERIAL_ERR_MSG)
            .get_mut()
            .as_mut()
            .expect(SERIAL_ERR_MSG)
            .writer
            .flush()
    }
}

impl PlatformSerial<u8> for LinuxSerial {}