platform_serial_linux/
lib.rs

1pub use platform_serial::PlatformSerial;
2
3use serial_embedded_hal::*;
4
5pub use serial_embedded_hal::{BaudRate, CharSize, FlowControl, Parity, PortSettings, StopBits};
6
7use core::default::Default;
8use lazy_static::lazy_static;
9use std::cell::Cell;
10use std::sync::Mutex;
11use std::sync::Once;
12
13lazy_static! {
14    static ref CONFIGURATION: Mutex<Option<PortSettings>> = Mutex::new(None);
15    static ref PORT_PATH: Mutex<Option<String>> = Mutex::new(None);
16    static ref SERIAL: Mutex<Cell<Option<HiddenSerialParts>>> = Mutex::new(Cell::new(None));
17    static ref INIT_ONCE: Once = Once::new();
18}
19
20const SERIAL_ERR_MSG: &str = "Serial not initialized";
21
22pub fn configure_serial(port_path: String, configuration: PortSettings) {
23    INIT_ONCE.call_once(|| {
24        let mut port = PORT_PATH.lock().unwrap();
25        *port = Some(port_path.clone());
26        let mut config = CONFIGURATION.lock().unwrap();
27        *config = Some(configuration);
28
29        let serial = Serial::new(&port_path, &configuration)
30            .expect("Could not initialize port with given configuration");
31
32        let (writer, reader) = serial.split();
33
34        let mut serial_ref = SERIAL.lock().unwrap();
35        *serial_ref = Cell::new(Some(HiddenSerialParts { reader, writer }));
36    });
37}
38
39struct HiddenSerialParts {
40    pub reader: serial_embedded_hal::Rx,
41    pub writer: serial_embedded_hal::Tx,
42}
43
44pub struct LinuxSerial;
45
46impl Default for LinuxSerial {
47    fn default() -> Self {
48        LinuxSerial
49    }
50}
51
52impl embedded_hal::serial::Read<u8> for LinuxSerial {
53    type Error = serial_core::Error;
54
55    fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
56        SERIAL
57            .lock()
58            .as_mut()
59            .expect(SERIAL_ERR_MSG)
60            .get_mut()
61            .as_mut()
62            .expect(SERIAL_ERR_MSG)
63            .reader
64            .read()
65    }
66}
67
68impl embedded_hal::serial::Write<u8> for LinuxSerial {
69    type Error = serial_core::Error;
70
71    fn write(&mut self, byte: u8) -> Result<(), nb::Error<Self::Error>> {
72        SERIAL
73            .lock()
74            .as_mut()
75            .expect(SERIAL_ERR_MSG)
76            .get_mut()
77            .as_mut()
78            .expect(SERIAL_ERR_MSG)
79            .writer
80            .write(byte)
81    }
82
83    fn flush(&mut self) -> Result<(), nb::Error<Self::Error>> {
84        SERIAL
85            .lock()
86            .as_mut()
87            .expect(SERIAL_ERR_MSG)
88            .get_mut()
89            .as_mut()
90            .expect(SERIAL_ERR_MSG)
91            .writer
92            .flush()
93    }
94}
95
96impl PlatformSerial<u8> for LinuxSerial {}
97
98impl ufmt::uWrite for LinuxSerial {
99    type Error = nb::Error<serial_core::Error>;
100
101    fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
102        for b in s.as_bytes().iter() {
103            nb::block!(<LinuxSerial as embedded_hal::serial::Write<u8>>::write(
104                self, *b
105            ))
106            .unwrap()
107        }
108        Ok(())
109    }
110
111    // fn write_char is implemented by default trait uWrite
112}