plc-comm-slmp-rust 0.1.1

Async Rust SLMP client based on the plc-comm-slmp-dotnet implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::fmt;
use std::time::Duration;

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum SlmpTransportMode {
    Tcp,
    Udp,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum SlmpFrameType {
    Frame3E,
    Frame4E,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum SlmpCompatibilityMode {
    Legacy,
    Iqr,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum SlmpTraceDirection {
    Send,
    Receive,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum SlmpCommand {
    DeviceRead = 0x0401,
    DeviceWrite = 0x1401,
    DeviceReadRandom = 0x0403,
    DeviceWriteRandom = 0x1402,
    DeviceReadBlock = 0x0406,
    DeviceWriteBlock = 0x1406,
    MonitorRegister = 0x0801,
    Monitor = 0x0802,
    ReadTypeName = 0x0101,
    LabelArrayRead = 0x041A,
    LabelArrayWrite = 0x141A,
    LabelReadRandom = 0x041C,
    LabelWriteRandom = 0x141B,
    MemoryRead = 0x0613,
    MemoryWrite = 0x1613,
    ExtendUnitRead = 0x0601,
    ExtendUnitWrite = 0x1601,
    RemoteRun = 0x1001,
    RemoteStop = 0x1002,
    RemotePause = 0x1003,
    RemoteLatchClear = 0x1005,
    RemoteReset = 0x1006,
    RemotePasswordUnlock = 0x1630,
    RemotePasswordLock = 0x1631,
    SelfTest = 0x0619,
    ClearError = 0x1617,
}

impl SlmpCommand {
    pub fn as_u16(self) -> u16 {
        self as u16
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[repr(u16)]
pub enum SlmpDeviceCode {
    SM = 0x0091,
    SD = 0x00A9,
    X = 0x009C,
    Y = 0x009D,
    M = 0x0090,
    L = 0x0092,
    F = 0x0093,
    V = 0x0094,
    B = 0x00A0,
    D = 0x00A8,
    W = 0x00B4,
    TS = 0x00C1,
    TC = 0x00C0,
    TN = 0x00C2,
    LTS = 0x0051,
    LTC = 0x0050,
    LTN = 0x0052,
    STS = 0x00C7,
    STC = 0x00C6,
    STN = 0x00C8,
    LSTS = 0x0059,
    LSTC = 0x0058,
    LSTN = 0x005A,
    LCC = 0x0054,
    LCS = 0x0055,
    LCN = 0x0056,
    CS = 0x00C4,
    CC = 0x00C3,
    CN = 0x00C5,
    SB = 0x00A1,
    SW = 0x00B5,
    DX = 0x00A2,
    DY = 0x00A3,
    Z = 0x00CC,
    LZ = 0x0062,
    R = 0x00AF,
    ZR = 0x00B0,
    RD = 0x002C,
    G = 0x00AB,
    HG = 0x002E,
}

impl SlmpDeviceCode {
    pub fn as_u16(self) -> u16 {
        self as u16
    }

    pub fn as_u8(self) -> u8 {
        (self.as_u16() & 0x00FF) as u8
    }

    pub fn prefix(self) -> &'static str {
        match self {
            Self::SM => "SM",
            Self::SD => "SD",
            Self::X => "X",
            Self::Y => "Y",
            Self::M => "M",
            Self::L => "L",
            Self::F => "F",
            Self::V => "V",
            Self::B => "B",
            Self::D => "D",
            Self::W => "W",
            Self::TS => "TS",
            Self::TC => "TC",
            Self::TN => "TN",
            Self::LTS => "LTS",
            Self::LTC => "LTC",
            Self::LTN => "LTN",
            Self::STS => "STS",
            Self::STC => "STC",
            Self::STN => "STN",
            Self::LSTS => "LSTS",
            Self::LSTC => "LSTC",
            Self::LSTN => "LSTN",
            Self::LCC => "LCC",
            Self::LCS => "LCS",
            Self::LCN => "LCN",
            Self::CS => "CS",
            Self::CC => "CC",
            Self::CN => "CN",
            Self::SB => "SB",
            Self::SW => "SW",
            Self::DX => "DX",
            Self::DY => "DY",
            Self::Z => "Z",
            Self::LZ => "LZ",
            Self::R => "R",
            Self::ZR => "ZR",
            Self::RD => "RD",
            Self::G => "G",
            Self::HG => "HG",
        }
    }

    pub fn is_hex_addressed(self) -> bool {
        matches!(
            self,
            Self::X | Self::Y | Self::B | Self::W | Self::SB | Self::SW | Self::DX | Self::DY
        )
    }

    pub fn is_bit_device(self) -> bool {
        matches!(
            self,
            Self::SM
                | Self::X
                | Self::Y
                | Self::M
                | Self::L
                | Self::F
                | Self::V
                | Self::B
                | Self::TS
                | Self::TC
                | Self::LTS
                | Self::LTC
                | Self::STS
                | Self::STC
                | Self::LSTS
                | Self::LSTC
                | Self::CS
                | Self::CC
                | Self::LCS
                | Self::LCC
                | Self::SB
                | Self::DX
                | Self::DY
        )
    }

    pub fn is_word_device(self) -> bool {
        matches!(
            self,
            Self::SD
                | Self::D
                | Self::W
                | Self::TN
                | Self::LTN
                | Self::STN
                | Self::LSTN
                | Self::CN
                | Self::LCN
                | Self::SW
                | Self::Z
                | Self::LZ
                | Self::R
                | Self::ZR
                | Self::RD
                | Self::G
                | Self::HG
        )
    }

    pub fn is_word_batchable(self) -> bool {
        matches!(
            self,
            Self::SD
                | Self::D
                | Self::W
                | Self::TN
                | Self::LTN
                | Self::STN
                | Self::LSTN
                | Self::CN
                | Self::LCN
                | Self::SW
                | Self::Z
                | Self::LZ
                | Self::R
                | Self::ZR
                | Self::RD
        )
    }

    pub fn parse_prefix(prefix: &str) -> Option<Self> {
        match prefix {
            "LSTS" => Some(Self::LSTS),
            "LSTC" => Some(Self::LSTC),
            "LSTN" => Some(Self::LSTN),
            "LTS" => Some(Self::LTS),
            "LTC" => Some(Self::LTC),
            "LTN" => Some(Self::LTN),
            "STS" => Some(Self::STS),
            "STC" => Some(Self::STC),
            "STN" => Some(Self::STN),
            "SM" => Some(Self::SM),
            "SD" => Some(Self::SD),
            "TS" => Some(Self::TS),
            "TC" => Some(Self::TC),
            "TN" => Some(Self::TN),
            "CS" => Some(Self::CS),
            "CC" => Some(Self::CC),
            "CN" => Some(Self::CN),
            "SB" => Some(Self::SB),
            "SW" => Some(Self::SW),
            "DX" => Some(Self::DX),
            "DY" => Some(Self::DY),
            "LCS" => Some(Self::LCS),
            "LCC" => Some(Self::LCC),
            "LCN" => Some(Self::LCN),
            "LZ" => Some(Self::LZ),
            "ZR" => Some(Self::ZR),
            "RD" => Some(Self::RD),
            "HG" => Some(Self::HG),
            "X" => Some(Self::X),
            "Y" => Some(Self::Y),
            "M" => Some(Self::M),
            "L" => Some(Self::L),
            "F" => Some(Self::F),
            "V" => Some(Self::V),
            "B" => Some(Self::B),
            "D" => Some(Self::D),
            "W" => Some(Self::W),
            "Z" => Some(Self::Z),
            "R" => Some(Self::R),
            "G" => Some(Self::G),
            _ => None,
        }
    }
}

impl fmt::Display for SlmpDeviceCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.prefix())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct SlmpTargetAddress {
    pub network: u8,
    pub station: u8,
    pub module_io: u16,
    pub multidrop: u8,
}

impl Default for SlmpTargetAddress {
    fn default() -> Self {
        Self {
            network: 0x00,
            station: 0xFF,
            module_io: 0x03FF,
            multidrop: 0x00,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct SlmpDeviceAddress {
    pub code: SlmpDeviceCode,
    pub number: u32,
}

impl SlmpDeviceAddress {
    pub const fn new(code: SlmpDeviceCode, number: u32) -> Self {
        Self { code, number }
    }
}

impl fmt::Display for SlmpDeviceAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", self.code, self.number)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SlmpTypeNameInfo {
    pub model: String,
    pub model_code: u16,
    pub has_model_code: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SlmpBlockRead {
    pub device: SlmpDeviceAddress,
    pub points: u16,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SlmpBlockWrite {
    pub device: SlmpDeviceAddress,
    pub values: Vec<u16>,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)]
pub struct SlmpBlockWriteOptions {
    pub split_mixed_blocks: bool,
    pub retry_mixed_on_error: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)]
pub struct SlmpRandomReadResult {
    pub word_values: Vec<u16>,
    pub dword_values: Vec<u32>,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)]
pub struct SlmpBlockReadResult {
    pub word_values: Vec<u16>,
    pub bit_values: Vec<u16>,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SlmpLongTimerResult {
    pub index: u32,
    pub device: String,
    pub current_value: u32,
    pub contact: bool,
    pub coil: bool,
    pub status_word: u16,
    pub raw_words: Vec<u16>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SlmpExtensionSpec {
    pub extension_specification: u16,
    pub extension_specification_modification: u8,
    pub device_modification_index: u8,
    pub device_modification_flags: u8,
    pub direct_memory_specification: u8,
}

impl Default for SlmpExtensionSpec {
    fn default() -> Self {
        Self {
            extension_specification: 0x0000,
            extension_specification_modification: 0x00,
            device_modification_index: 0x00,
            device_modification_flags: 0x00,
            direct_memory_specification: 0x00,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SlmpQualifiedDeviceAddress {
    pub device: SlmpDeviceAddress,
    pub extension_specification: Option<u16>,
    pub direct_memory_specification: Option<u8>,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SlmpNamedTarget {
    pub name: String,
    pub target: SlmpTargetAddress,
}

#[derive(Debug, Clone)]
pub struct SlmpConnectionOptions {
    pub host: String,
    pub port: u16,
    pub timeout: Duration,
    pub frame_type: SlmpFrameType,
    pub compatibility_mode: SlmpCompatibilityMode,
    pub target: SlmpTargetAddress,
    pub transport_mode: SlmpTransportMode,
    pub monitoring_timer: u16,
}

impl SlmpConnectionOptions {
    pub fn new(host: impl Into<String>) -> Self {
        Self {
            host: host.into(),
            port: 1025,
            timeout: Duration::from_secs(3),
            frame_type: SlmpFrameType::Frame4E,
            compatibility_mode: SlmpCompatibilityMode::Iqr,
            target: SlmpTargetAddress::default(),
            transport_mode: SlmpTransportMode::Tcp,
            monitoring_timer: 0x0010,
        }
    }
}