use crate::cmux::frame::{
encode_frame, encode_uih, DecodedFrame, Deframer, DISC, DM, PF, SABM, UA, UI, UIH,
};
use crate::cmux::io::CmuxPhysicalIo;
use crate::events::SerialEvent;
use crate::hub::shared::{Outbox, PORT_CLOSED};
use crate::hub::stream::{EventStream, Target, WatchOwner};
use crate::hub::ExchangeWaiter;
use serial_codec::framing::Framer;
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc;
use std::sync::{Arc, Condvar, Mutex, OnceLock};
use std::time::{Duration, Instant};
pub struct DlciChannel {
pub path: String,
pub exchange_waiter: Mutex<Option<Arc<ExchangeWaiter>>>,
watches: Mutex<DlciWatches>,
closing: AtomicBool,
outbox: Arc<Outbox>,
}
#[derive(Default)]
struct DlciWatches {
old: Option<DlciWatchSlot>,
core: Vec<(u64, DlciWatchSlot)>,
}
impl DlciWatches {
fn each(&mut self, mut f: impl FnMut(&mut DlciWatchSlot)) -> Option<DlciWatchSlot> {
self.core.retain(|(_, w)| !w.target.gone());
self.core.iter_mut().for_each(|(_, w)| f(w));
match self.old.as_mut() {
Some(old) if !old.target.gone() => {
f(old);
None
}
_ => self.old.take(),
}
}
}
struct DlciWatchSlot {
target: Target,
batch_timeout_ms: u64,
flush_at: Instant,
framer: Option<Box<dyn Framer>>,
buffer: Vec<u8>,
}
impl DlciWatchSlot {
fn new(target: Target, batch_timeout_ms: u64, framer: Option<Box<dyn Framer>>) -> Self {
Self {
target,
batch_timeout_ms,
flush_at: Instant::now(),
framer,
buffer: Vec::new(),
}
}
fn take_due(&mut self) -> bool {
if self.flush_at.elapsed() >= Duration::from_millis(self.batch_timeout_ms) {
self.flush_at = Instant::now();
true
} else {
false
}
}
fn push(&mut self, payload: &[u8]) -> Vec<Vec<u8>> {
let mut frames = Vec::new();
if let Some(f) = self.framer.as_mut() {
f.push(payload, Instant::now(), &mut frames);
return frames;
}
self.buffer.extend_from_slice(payload);
if self.take_due() {
frames.push(std::mem::take(&mut self.buffer));
}
frames
}
fn poll(&mut self) -> Vec<Vec<u8>> {
let mut frames = Vec::new();
if let Some(f) = self.framer.as_mut() {
f.poll(Instant::now(), &mut frames);
} else if self.take_due() && !self.buffer.is_empty() {
frames.push(std::mem::take(&mut self.buffer));
}
frames
}
fn finish(&mut self) -> Vec<Vec<u8>> {
let batch = std::mem::take(&mut self.buffer);
let rest = self.framer.as_mut().and_then(|f| f.flush());
[batch, rest.unwrap_or_default()]
.into_iter()
.filter(|d| !d.is_empty())
.collect()
}
}
impl DlciChannel {
fn new(path: String, outbox: Arc<Outbox>) -> Self {
Self {
path,
exchange_waiter: Mutex::new(None),
watches: Mutex::new(DlciWatches::default()),
closing: AtomicBool::new(false),
outbox,
}
}
#[cfg(feature = "tauri")]
pub fn attach_watch(&self, channel: tauri::ipc::Channel<SerialEvent>, batch_timeout_ms: u64) {
self.attach_watch_with(crate::events::channel_sink(channel), batch_timeout_ms, None);
}
pub fn attach_watch_with(
&self,
channel: crate::events::EventSink,
batch_timeout_ms: u64,
framer: Option<Box<dyn Framer>>,
) {
self.attach_stream(channel, batch_timeout_ms, framer);
}
fn attach_stream(
&self,
channel: crate::events::EventSink,
batch_timeout_ms: u64,
framer: Option<Box<dyn Framer>>,
) -> Arc<EventStream> {
let stream = self.outbox.stream(channel);
let slot = DlciWatchSlot::new(Target::Old(stream.clone()), batch_timeout_ms, framer);
let mut guard = crate::sync_util::lock_or_recover(&self.watches);
let mut replaced = guard.old.replace(slot);
if let Some(old) = replaced.as_mut() {
self.end_slot(old, None);
}
drop(guard);
stream
}
fn take_watch(&self, which: impl Fn(&DlciWatchSlot) -> bool) -> Option<DlciWatchSlot> {
let mut guard = crate::sync_util::lock_or_recover(&self.watches);
if !guard.old.as_ref().is_some_and(which) {
return None;
}
let mut slot = guard.old.take();
if let Some(slot) = slot.as_mut() {
self.end_slot(slot, None);
}
slot
}
pub fn detach_watch(&self) {
self.take_watch(|_| true);
}
fn attach_core_watch(&self, id: u64, slot: DlciWatchSlot) {
crate::sync_util::lock_or_recover(&self.watches)
.core
.push((id, slot));
}
fn detach_core_watch(&self, id: u64) -> bool {
let mut guard = crate::sync_util::lock_or_recover(&self.watches);
let Some(i) = guard.core.iter().position(|(c, _)| *c == id) else {
return false;
};
let (_, mut slot) = guard.core.remove(i);
self.end_slot(&mut slot, None);
drop(guard);
true
}
fn end_watches(&self, last: Option<&SerialEvent>) {
let ended = {
let mut guard = crate::sync_util::lock_or_recover(&self.watches);
let mut watches = std::mem::take(&mut *guard);
let gone = watches.each(|slot| self.end_slot(slot, last));
(watches, gone)
};
drop(ended);
}
fn end_slot(&self, slot: &mut DlciWatchSlot, last: Option<&SerialEvent>) {
let mut events = self.data_events(slot.finish());
events.extend(last.cloned());
slot.target.end(&mut events);
}
fn fail_exchange(&self, reason: &str) {
if let Some(waiter) = crate::sync_util::lock_or_recover(&self.exchange_waiter).as_ref() {
waiter.fail_with_reason(reason.to_string());
}
}
fn is_closing(&self) -> bool {
self.closing.load(Ordering::SeqCst)
}
fn begin_close(&self) {
self.closing.store(true, Ordering::SeqCst);
self.fail_exchange(PORT_CLOSED);
}
fn set_exchange_waiter(&self, waiter: Arc<ExchangeWaiter>) {
*crate::sync_util::lock_or_recover(&self.exchange_waiter) = Some(waiter);
if self.is_closing() {
self.fail_exchange(PORT_CLOSED);
}
}
fn data_events(&self, frames: Vec<Vec<u8>>) -> Vec<SerialEvent> {
let event = |data: Vec<u8>| SerialEvent::Data {
path: self.path.clone(),
size: data.len(),
data,
};
frames.into_iter().map(event).collect()
}
fn each_watch(&self, mut f: impl FnMut(&mut DlciWatchSlot) -> Vec<Vec<u8>>) {
let gone = crate::sync_util::lock_or_recover(&self.watches).each(|slot| {
let frames = f(slot);
slot.target.send(&mut self.data_events(frames));
});
drop(gone);
}
fn push_stream(&self, payload: &[u8]) {
self.each_watch(|slot| slot.push(payload));
}
fn tick(&self) {
self.each_watch(DlciWatchSlot::poll);
}
}
impl WatchOwner for DlciChannel {
fn stop(&self, stream: &Arc<EventStream>) {
drop(self.take_watch(|slot| slot.target.is(stream)));
self.outbox.settle(stream);
}
}
fn cmux_param(command: &str, index: usize) -> Option<u64> {
let upper = command.to_ascii_uppercase();
let at = upper.find("+CMUX=")?;
upper[at + "+CMUX=".len()..]
.split(',')
.nth(index)?
.trim()
.parse()
.ok()
}
pub fn cmux_n1(command: &str) -> usize {
cmux_param(command, 3).map_or(31, |n1| n1 as usize)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CmuxTimers {
pub t1: Duration,
pub n2: u32,
pub t2: Duration,
}
impl Default for CmuxTimers {
fn default() -> Self {
Self {
t1: Duration::from_millis(100),
n2: 3,
t2: Duration::from_millis(300),
}
}
}
impl CmuxTimers {
pub fn from_command(command: &str) -> Self {
let d = Self::default();
let tens = |i, min, default| {
cmux_param(command, i)
.filter(|v| (min..=255).contains(v))
.map_or(default, |v| Duration::from_millis(v * 10))
};
let retransmit = cmux_param(command, 4).is_some() || cmux_param(command, 5).is_some();
Self {
t1: tens(4, 1, d.t1),
n2: if retransmit {
cmux_param(command, 5)
.filter(|v| *v <= 100)
.map_or(d.n2, |v| v as u32)
} else {
0
},
t2: tens(6, 2, d.t2),
}
}
}
const CR: u8 = 0x02;
const PN_CMD: u8 = 0x83;
const PSC_CMD: u8 = 0x43;
const TEST_CMD: u8 = 0x23;
const MSC_CMD: u8 = 0xE3;
const RPN_CMD: u8 = 0x93;
const RLS_CMD: u8 = 0x53;
const FCON_CMD: u8 = 0xA3;
const FCOFF_CMD: u8 = 0x63;
const CLD_CMD: u8 = 0xC3;
const CLD_RSP: u8 = 0xC1;
const NSC_RSP: u8 = 0x11;
const ECHOED: [u8; 9] = [
PN_CMD, PSC_CMD, TEST_CMD, MSC_CMD, RPN_CMD, RLS_CMD, FCON_CMD, FCOFF_CMD, CLD_CMD,
];
const V24_READY: u8 = 0x8D;
const V24_FC: u8 = 0x02;
#[derive(Default)]
struct TxGate {
fc_off: bool,
held_dlcis: HashSet<u8>,
}
pub(crate) type ClosedHook = Box<dyn Fn(&[String], &str, bool) + Send + Sync>;
pub struct CmuxSession {
physical_path: String,
io: Arc<dyn CmuxPhysicalIo>,
deframer: Mutex<Deframer>,
channels: Mutex<HashMap<u8, Arc<DlciChannel>>>,
pending_open: Mutex<HashMap<u8, mpsc::Sender<bool>>>,
pending_cld: Mutex<Option<mpsc::Sender<()>>>,
closed: Mutex<HashSet<u8>>,
tx: Mutex<TxGate>,
tx_cv: Condvar,
dead: Mutex<Option<String>>,
on_closed: OnceLock<ClosedHook>,
timers: CmuxTimers,
outbox: Arc<Outbox>,
}
impl CmuxSession {
pub fn new(physical_path: String, io: Arc<dyn CmuxPhysicalIo>) -> Arc<Self> {
Self::with_n1(physical_path, io, serial_codec::cmux::TX_N1)
}
pub fn with_n1(physical_path: String, io: Arc<dyn CmuxPhysicalIo>, n1: usize) -> Arc<Self> {
let timers = CmuxTimers {
n2: 0,
..CmuxTimers::default()
};
Self::with_timers(physical_path, io, n1, timers)
}
pub fn for_command(
physical_path: String,
io: Arc<dyn CmuxPhysicalIo>,
command: &str,
) -> Arc<Self> {
Self::with_timers(
physical_path,
io,
cmux_n1(command),
CmuxTimers::from_command(command),
)
}
fn with_timers(
physical_path: String,
io: Arc<dyn CmuxPhysicalIo>,
n1: usize,
timers: CmuxTimers,
) -> Arc<Self> {
Arc::new(Self {
physical_path,
io,
deframer: Mutex::new(Deframer::with_max_info(
n1.max(crate::cmux::frame::DEFAULT_MAX_RX_INFO),
)),
channels: Mutex::new(HashMap::new()),
pending_open: Mutex::new(HashMap::new()),
pending_cld: Mutex::new(None),
closed: Mutex::new(HashSet::new()),
tx: Mutex::default(),
tx_cv: Condvar::new(),
dead: Mutex::new(None),
on_closed: OnceLock::new(),
timers,
outbox: Arc::default(),
})
}
pub fn physical_path(&self) -> &str {
&self.physical_path
}
pub(crate) fn set_on_closed(&self, hook: ClosedHook) {
let _ = self.on_closed.set(hook);
}
fn check_alive(&self) -> Result<(), String> {
match crate::sync_util::lock_or_recover(&self.dead).as_ref() {
Some(why) => Err(why.clone()),
None => Ok(()),
}
}
fn end(&self, why: &str) {
crate::sync_util::lock_or_recover(&self.dead).get_or_insert_with(|| why.to_string());
self.wake_writers();
}
fn wake_writers(&self) {
drop(crate::sync_util::lock_or_recover(&self.tx));
self.tx_cv.notify_all();
}
pub fn register_dlci(&self, dlci: u8, virtual_path: String) -> Arc<DlciChannel> {
let ch = Arc::new(DlciChannel::new(virtual_path, self.outbox.clone()));
let replaced = crate::sync_util::lock_or_recover(&self.channels).insert(dlci, ch.clone());
if let Some(old) = replaced {
old.begin_close();
old.end_watches(None);
}
crate::sync_util::lock_or_recover(&self.closed).remove(&dlci);
crate::sync_util::lock_or_recover(&self.tx)
.held_dlcis
.remove(&dlci);
ch
}
pub fn unregister_dlci(&self, dlci: u8) {
crate::sync_util::lock_or_recover(&self.closed).remove(&dlci);
crate::sync_util::lock_or_recover(&self.tx)
.held_dlcis
.remove(&dlci);
let removed = crate::sync_util::lock_or_recover(&self.channels).remove(&dlci);
self.wake_writers();
if let Some(ch) = removed {
ch.begin_close();
ch.end_watches(None);
let (tx, rx) = mpsc::channel();
crate::sync_util::lock_or_recover(&self.pending_open).insert(dlci, tx);
if self
.io
.write_all(&encode_frame(dlci, true, DISC | PF, &[]))
.is_ok()
{
let _ = rx.recv_timeout(self.timers.t1);
}
crate::sync_util::lock_or_recover(&self.pending_open).remove(&dlci);
}
}
pub fn has_dlci(&self, dlci: u8) -> bool {
crate::sync_util::lock_or_recover(&self.channels).contains_key(&dlci)
}
pub fn open_dlci(&self, dlci: u8, timeout: Duration) -> Result<(), String> {
self.check_alive()?;
let (tx, rx) = mpsc::channel();
crate::sync_util::lock_or_recover(&self.pending_open).insert(dlci, tx);
let CmuxTimers { t1, n2, .. } = self.timers;
let total = timeout.max(t1 * (n2 + 1));
let sabm = encode_frame(dlci, true, SABM | PF, &[]);
let start = Instant::now();
let mut answer = Ok(None);
for attempt in 0..=n2 {
if let Err(e) = self.io.write_all(&sabm) {
answer = Err(e);
break;
}
let wait = if attempt == n2 {
total.saturating_sub(start.elapsed())
} else {
t1
};
if let Ok(ua) = rx.recv_timeout(wait) {
answer = Ok(Some(ua));
break;
}
}
crate::sync_util::lock_or_recover(&self.pending_open).remove(&dlci);
match answer? {
Some(true) => {
if dlci != 0 {
let msc = [MSC_CMD, 0x05, (dlci << 2) | 0x03, V24_READY];
let _ = self.io.write_all(&encode_frame(0, true, UIH, &msc));
}
Ok(())
}
Some(false) => Err(format!("modem refused CMUX DLCI {dlci} (DM)")),
None => Err(format!(
"no UA for CMUX DLCI {dlci} within {} ms",
total.as_millis()
)),
}
}
pub fn close_down(&self) {
self.end("CMUX multiplexer is closed");
let (tx, rx) = mpsc::channel();
*crate::sync_util::lock_or_recover(&self.pending_cld) = Some(tx);
let cld = encode_frame(0, true, UIH, &[CLD_CMD, 0x01]);
if self.io.write_all(&cld).is_ok() {
let _ = rx.recv_timeout(self.timers.t2);
}
*crate::sync_util::lock_or_recover(&self.pending_cld) = None;
}
fn closed_by_modem(&self, dlci: u8) {
let gone: Vec<(u8, Arc<DlciChannel>)> = {
let mut channels = crate::sync_util::lock_or_recover(&self.channels);
if dlci == 0 {
channels.drain().collect()
} else {
channels
.remove(&dlci)
.map(|ch| (dlci, ch))
.into_iter()
.collect()
}
};
if dlci == 0 {
self.end("CMUX multiplexer closed by the modem");
}
let mut paths = Vec::new();
for (d, ch) in gone {
crate::sync_util::lock_or_recover(&self.closed).insert(d);
let reason = format!("CMUX DLCI {d} closed by the modem");
ch.fail_exchange(&reason);
ch.end_watches(Some(&SerialEvent::Disconnect {
path: ch.path.clone(),
reason,
}));
paths.push(ch.path.clone());
}
self.wake_writers();
if let Some(hook) = self.on_closed.get() {
let reason = if dlci == 0 {
"CMUX multiplexer closed by the modem".to_string()
} else {
format!("CMUX DLCI {dlci} closed by the modem")
};
hook(&paths, &reason, dlci == 0);
}
}
pub fn tick(&self) {
for ch in self.channels() {
ch.tick();
}
self.outbox.deliver();
}
fn channels(&self) -> Vec<Arc<DlciChannel>> {
crate::sync_util::lock_or_recover(&self.channels)
.values()
.cloned()
.collect()
}
fn channel(&self, dlci: u8) -> Option<Arc<DlciChannel>> {
crate::sync_util::lock_or_recover(&self.channels)
.get(&dlci)
.cloned()
}
pub fn emit_disconnect(&self, reason: &str) {
self.disconnect_queued(reason);
self.deliver_queued();
}
pub(crate) fn disconnect_queued(&self, reason: &str) {
self.end(reason);
let channels = self.channels();
for ch in &channels {
ch.fail_exchange(reason);
}
for ch in channels {
ch.end_watches(Some(&SerialEvent::Disconnect {
path: ch.path.clone(),
reason: reason.to_string(),
}));
}
}
pub(crate) fn deliver_queued(&self) {
self.outbox.deliver();
}
pub(crate) fn begin_close(&self, dlci: Option<u8>) {
let channels = match dlci {
Some(dlci) => self.channel(dlci).into_iter().collect(),
None => self.channels(),
};
channels.iter().for_each(|ch| ch.begin_close());
self.wake_writers();
}
#[cfg(feature = "tauri")]
pub fn set_watch(
&self,
dlci: u8,
channel: tauri::ipc::Channel<SerialEvent>,
batch_timeout_ms: u64,
) {
self.set_watch_with(
dlci,
crate::events::channel_sink(channel),
batch_timeout_ms,
None,
);
}
pub fn set_watch_with(
&self,
dlci: u8,
channel: crate::events::EventSink,
batch_timeout_ms: u64,
framer: Option<Box<dyn Framer>>,
) {
self.attach_stream(dlci, channel, batch_timeout_ms, framer);
}
pub(crate) fn attach_stream(
&self,
dlci: u8,
channel: crate::events::EventSink,
batch_timeout_ms: u64,
framer: Option<Box<dyn Framer>>,
) -> Option<(Arc<DlciChannel>, Arc<EventStream>)> {
let ch = self.channel(dlci)?;
let stream = ch.attach_stream(channel, batch_timeout_ms, framer);
Some((ch, stream))
}
pub fn clear_watch(&self, dlci: u8) {
let slot = self.channel(dlci).and_then(|ch| ch.take_watch(|_| true));
if let Some(DlciWatchSlot {
target: Target::Old(stream),
..
}) = slot
{
self.outbox.settle(&stream);
}
}
pub(crate) fn add_core_watch(
&self,
dlci: u8,
id: u64,
target: Target,
batch_timeout_ms: u64,
framer: Option<Box<dyn Framer>>,
) -> bool {
let Some(ch) = self.channel(dlci) else {
return false;
};
ch.attach_core_watch(id, DlciWatchSlot::new(target, batch_timeout_ms, framer));
true
}
pub(crate) fn remove_core_watch(&self, dlci: u8, id: u64) -> bool {
self.channel(dlci)
.is_some_and(|ch| ch.detach_core_watch(id))
}
pub fn set_exchange_waiter(&self, dlci: u8, waiter: Arc<ExchangeWaiter>) {
if let Some(ch) = crate::sync_util::lock_or_recover(&self.channels).get(&dlci) {
ch.set_exchange_waiter(waiter);
}
}
pub fn clear_exchange_waiter(&self, dlci: u8) {
if let Some(ch) = crate::sync_util::lock_or_recover(&self.channels).get(&dlci) {
*crate::sync_util::lock_or_recover(&ch.exchange_waiter) = None;
}
}
pub fn cancel_active_exchange(&self, dlci: u8) {
if let Some(ch) = crate::sync_util::lock_or_recover(&self.channels).get(&dlci) {
ch.fail_exchange("exchange cancelled");
}
}
pub fn send_uih(&self, dlci: u8, payload: &[u8]) -> Result<usize, String> {
let frame = encode_uih(dlci, payload);
let deadline = Instant::now() + self.io.write_timeout(frame.len());
let mut gate = crate::sync_util::lock_or_recover(&self.tx);
loop {
self.check_alive()?;
if crate::sync_util::lock_or_recover(&self.closed).contains(&dlci) {
return Err(format!("CMUX DLCI {dlci} closed by the modem"));
}
match self.channel(dlci) {
None => return Err(format!("CMUX DLCI {dlci} is closed")),
Some(ch) if ch.is_closing() => return Err(PORT_CLOSED.into()),
Some(_) => {}
}
let held = if gate.fc_off {
"CMUX flow control off (FCoff)".to_string()
} else if gate.held_dlcis.contains(&dlci) {
format!("CMUX DLCI {dlci} flow control off (MSC FC)")
} else {
break;
};
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
return Err(held);
}
gate = self
.tx_cv
.wait_timeout(gate, left)
.unwrap_or_else(|e| e.into_inner())
.0;
}
self.io.write_all(&frame)?;
drop(gate);
Ok(payload.len())
}
pub fn feed_physical_rx(&self, chunk: &[u8]) {
let frames = crate::sync_util::lock_or_recover(&self.deframer).feed(chunk);
for frame in frames {
self.dispatch_frame(frame);
}
self.outbox.deliver();
}
fn dispatch_frame(&self, frame: DecodedFrame) {
match frame.kind {
UA | DM => {
if let Some(tx) =
crate::sync_util::lock_or_recover(&self.pending_open).get(&frame.dlci)
{
let _ = tx.send(frame.kind == UA);
} else if frame.kind == DM {
self.closed_by_modem(frame.dlci);
}
return;
}
DISC => {
let open = frame.dlci == 0 || self.has_dlci(frame.dlci);
let reply = if open { UA } else { DM };
let _ = self
.io
.write_all(&encode_frame(frame.dlci, false, reply | PF, &[]));
if open {
self.closed_by_modem(frame.dlci);
}
return;
}
UIH | UI => {}
_ => return,
}
if frame.dlci == 0 {
self.handle_control(&frame.payload);
return;
}
let Some(ch) = self.channel(frame.dlci) else {
return;
};
let waiter = crate::sync_util::lock_or_recover(&ch.exchange_waiter).clone();
if let Some(waiter) = waiter {
waiter.push_bytes(&frame.payload);
return;
}
ch.push_stream(&frame.payload);
}
fn handle_control(&self, payload: &[u8]) {
let Some(&type_octet) = payload.first() else {
return;
};
if type_octet & CR == 0 {
if type_octet == CLD_RSP {
if let Some(tx) = crate::sync_util::lock_or_recover(&self.pending_cld).as_ref() {
let _ = tx.send(());
}
}
return;
}
if type_octet == FCOFF_CMD {
crate::sync_util::lock_or_recover(&self.tx).fc_off = true;
}
if let (MSC_CMD, Some(&address), Some(&v24)) = (type_octet, payload.get(2), payload.get(3))
{
let dlci = address >> 2;
let mut gate = crate::sync_util::lock_or_recover(&self.tx);
if v24 & V24_FC != 0 {
gate.held_dlcis.insert(dlci);
} else {
gate.held_dlcis.remove(&dlci);
}
drop(gate);
self.tx_cv.notify_all();
}
let reply = if ECHOED.contains(&type_octet) {
let mut reply = payload.to_vec();
reply[0] &= !CR;
reply
} else {
vec![NSC_RSP, 0x03, type_octet]
};
let _ = self.io.write_all(&encode_frame(0, true, UIH, &reply));
match type_octet {
FCON_CMD => {
crate::sync_util::lock_or_recover(&self.tx).fc_off = false;
self.tx_cv.notify_all();
}
CLD_CMD => self.closed_by_modem(0),
_ => {}
}
}
}