Skip to main content

serialplugin_core/state/
handles.rs

1use crate::cmux::CmuxSession;
2use crate::hub::PortRxHub;
3use crate::port::tx_queue::PortTxQueue;
4#[cfg(target_os = "ios")]
5use crate::serialport;
6use serialport::SerialPort;
7use std::sync::Arc;
8use std::sync::{atomic::AtomicBool, Mutex};
9
10/// Cloneable Arc handles for I/O without holding the global port map lock.
11#[derive(Clone)]
12pub struct ConnectedPortHandle {
13    pub port: Arc<Mutex<Box<dyn SerialPort>>>,
14    pub rx_hub: Arc<Mutex<Option<Arc<PortRxHub>>>>,
15    pub mux: Arc<Mutex<Option<Arc<CmuxSession>>>>,
16    pub virtual_dlci: Option<u8>,
17    pub physical_path: Option<String>,
18    pub exchange_cancel: Arc<AtomicBool>,
19    pub tx_queue: Arc<PortTxQueue>,
20}
21
22/// Open serial port with optional background RX hub (poll loop on all platforms).
23pub struct ConnectedPort {
24    pub port: Arc<Mutex<Box<dyn SerialPort>>>,
25    pub rx_hub: Arc<Mutex<Option<Arc<PortRxHub>>>>,
26    pub mux: Arc<Mutex<Option<Arc<CmuxSession>>>>,
27    pub virtual_dlci: Option<u8>,
28    pub physical_path: Option<String>,
29    pub exchange_cancel: Arc<AtomicBool>,
30    pub tx_queue: Arc<PortTxQueue>,
31}
32
33/// Lightweight CMUX virtual channel handle (no duplicate RX hub).
34#[derive(Clone)]
35pub struct VirtualPortRef {
36    pub physical_path: String,
37    pub dlci: u8,
38    pub exchange_cancel: Arc<AtomicBool>,
39    pub tx_queue: Arc<PortTxQueue>,
40}
41
42pub type PhysicalPortRef = ConnectedPort;
43pub type PhysicalPortHandle = ConnectedPortHandle;
44
45/// Lifecycle state for a managed port.
46pub enum PortState {
47    Closed,
48    Opening,
49    Connected(ConnectedPort),
50}
51
52impl PortState {
53    pub fn not_connected_reason(&self) -> String {
54        match self {
55            PortState::Closed => "Port is closed".to_string(),
56            PortState::Opening => "Port is still opening".to_string(),
57            PortState::Connected(_) => "Port is connected".to_string(),
58        }
59    }
60}
61
62/// Per-port state container.
63pub struct SerialportInfo {
64    pub state: PortState,
65}
66
67impl SerialportInfo {
68    pub fn new(port: Box<dyn SerialPort>) -> Self {
69        Self {
70            state: PortState::Connected(ConnectedPort::new(port)),
71        }
72    }
73
74    #[doc(hidden)]
75    pub fn connected_port_mut(&mut self) -> Option<&mut ConnectedPort> {
76        match &mut self.state {
77            PortState::Connected(cp) => Some(cp),
78            _ => None,
79        }
80    }
81}
82
83impl ConnectedPort {
84    pub fn new(port: Box<dyn SerialPort>) -> Self {
85        let tx_queue = Arc::new(PortTxQueue::new());
86        Self {
87            port: Arc::new(Mutex::new(port)),
88            rx_hub: Arc::new(Mutex::new(None)),
89            mux: Arc::new(Mutex::new(None)),
90            virtual_dlci: None,
91            physical_path: None,
92            exchange_cancel: tx_queue.cancel_flag(),
93            tx_queue,
94        }
95    }
96
97    pub fn handle(&self) -> ConnectedPortHandle {
98        ConnectedPortHandle {
99            port: self.port.clone(),
100            rx_hub: self.rx_hub.clone(),
101            mux: self.mux.clone(),
102            virtual_dlci: self.virtual_dlci,
103            physical_path: self.physical_path.clone(),
104            exchange_cancel: self.exchange_cancel.clone(),
105            tx_queue: self.tx_queue.clone(),
106        }
107    }
108}
109
110impl ConnectedPort {
111    #[doc(hidden)]
112    pub fn test_port_mut(&self) -> std::sync::MutexGuard<'_, Box<dyn SerialPort>> {
113        self.port.lock().unwrap()
114    }
115}