use serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, Copy, Serialize, Deserialize,
)]
pub enum ConnectionType {
PG = 1,
OP = 2,
Basic = 3
}
impl Default for ConnectionType {
fn default() -> Self {
Self::OP
}
}
#[derive(
Debug, Clone, Serialize, Deserialize,
)]
pub enum ConnectMode {
Tsap {
conn_type: ConnectionType,
local_tsap: u16,
remote_tsap: u16
},
RackSlot {
conn_type: ConnectionType,
rack: u16,
slot: u16
}
}
impl ConnectMode {
pub fn init_tsap(
conn_type: ConnectionType,
local_tsap: u16,
remote_tsap: u16
) -> Self {
Self::Tsap {
conn_type,
local_tsap,
remote_tsap
}
}
pub fn init_rack_slot(
conn_type: ConnectionType,
rack: u16,
slot: u16
) -> Self {
Self::RackSlot {
conn_type,
rack,
slot
}
}
pub fn conn_type(&self) -> &ConnectionType {
match self {
ConnectMode::Tsap {
conn_type,
..
} => conn_type,
ConnectMode::RackSlot {
conn_type,
..
} => conn_type
}
}
pub fn local_tsap(&self) -> [u8; 2] {
match self {
ConnectMode::Tsap {
local_tsap,
..
} => [
(local_tsap >> 8) as u8,
*local_tsap as u8
],
ConnectMode::RackSlot { .. } => {
[0x01, 0x00]
},
}
}
pub fn remote_tsap(&self) -> [u8; 2] {
let remote_tsap = match self {
ConnectMode::Tsap {
remote_tsap,
..
} => *remote_tsap,
ConnectMode::RackSlot {
rack,
slot,
conn_type
} => {
((*conn_type as u16) << 8)
+ (rack * 0x20)
+ slot
},
};
[
(remote_tsap >> 8) as u8,
remote_tsap as u8
]
}
}