s7_client/client/
param.rs

1use serde::{Deserialize, Serialize};
2
3/// Client Connection Type
4/// 16 possible connections limited by the
5/// hardware The types are defined from the
6/// highest to lowest priority
7/// The basic connections are the first which
8/// would be closed if there aren't enough
9/// resources
10#[derive(
11    Debug, Clone, Copy, Serialize, Deserialize,
12)]
13pub enum ConnectionType {
14    /// Connect to the PLC programming console
15    /// (Programmiergeräte). German for
16    /// programming device.
17    PG    = 1,
18    /// Connect to the PLC Siemens HMI panel
19    OP    = 2,
20    /// Basic connection for generic data
21    /// transfer connection
22    /// 14 Basic connections
23    Basic = 3
24}
25
26impl Default for ConnectionType {
27    fn default() -> Self {
28        Self::OP
29    }
30}
31
32#[derive(
33    Debug, Clone, Serialize, Deserialize,
34)]
35pub enum ConnectMode {
36    Tsap {
37        conn_type:   ConnectionType,
38        local_tsap:  u16,
39        remote_tsap: u16
40    },
41    RackSlot {
42        conn_type: ConnectionType,
43        rack:      u16,
44        slot:      u16
45    }
46}
47impl ConnectMode {
48    pub fn init_tsap(
49        conn_type: ConnectionType,
50        local_tsap: u16,
51        remote_tsap: u16
52    ) -> Self {
53        Self::Tsap {
54            conn_type,
55            local_tsap,
56            remote_tsap
57        }
58    }
59
60    pub fn init_rack_slot(
61        conn_type: ConnectionType,
62        rack: u16,
63        slot: u16
64    ) -> Self {
65        Self::RackSlot {
66            conn_type,
67            rack,
68            slot
69        }
70    }
71
72    pub fn conn_type(&self) -> &ConnectionType {
73        match self {
74            ConnectMode::Tsap {
75                conn_type,
76                ..
77            } => conn_type,
78            ConnectMode::RackSlot {
79                conn_type,
80                ..
81            } => conn_type
82        }
83    }
84
85    pub fn local_tsap(&self) -> [u8; 2] {
86        match self {
87            ConnectMode::Tsap {
88                local_tsap,
89                ..
90            } => [
91                (local_tsap >> 8) as u8,
92                *local_tsap as u8
93            ],
94            ConnectMode::RackSlot { .. } => {
95                [0x01, 0x00]
96            },
97        }
98    }
99
100    pub fn remote_tsap(&self) -> [u8; 2] {
101        let remote_tsap = match self {
102            ConnectMode::Tsap {
103                remote_tsap,
104                ..
105            } => *remote_tsap,
106            ConnectMode::RackSlot {
107                rack,
108                slot,
109                conn_type
110            } => {
111                ((*conn_type as u16) << 8)
112                    + (rack * 0x20)
113                    + slot
114            },
115        };
116        [
117            (remote_tsap >> 8) as u8,
118            remote_tsap as u8
119        ]
120    }
121}