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
#[macro_use]
extern crate num_derive;

use std::{convert::TryFrom, num::TryFromIntError};

/// EtherCAT Slave Position
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct SlavePos(u16);

impl From<u16> for SlavePos {
    fn from(pos: u16) -> Self {
        Self(pos)
    }
}

impl From<SlavePos> for u16 {
    fn from(pos: SlavePos) -> Self {
        pos.0
    }
}

/// Object Directory Index
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct Idx(u16);

impl From<u16> for Idx {
    fn from(idx: u16) -> Self {
        Self(idx)
    }
}

impl From<Idx> for u16 {
    fn from(idx: Idx) -> Self {
        idx.0
    }
}

/// Object Directory Sub-index
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct SubIdx(u8);

impl From<u8> for SubIdx {
    fn from(sub: u8) -> Self {
        Self(sub)
    }
}

impl From<SubIdx> for u8 {
    fn from(sub: SubIdx) -> Self {
        sub.0
    }
}

/// SDO Position
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct SdoPos(u16);

impl From<u16> for SdoPos {
    fn from(pos: u16) -> Self {
        Self(pos)
    }
}

impl From<SdoPos> for u16 {
    fn from(pos: SdoPos) -> Self {
        pos.0
    }
}

/// PDO Position
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct PdoPos(u8);

impl From<u8> for PdoPos {
    fn from(pos: u8) -> Self {
        Self(pos)
    }
}

impl From<PdoPos> for u8 {
    fn from(pos: PdoPos) -> Self {
        pos.0
    }
}

/// PDO Entry Position
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct PdoEntryPos(u8);

impl From<u8> for PdoEntryPos {
    fn from(pos: u8) -> Self {
        Self(pos)
    }
}

impl From<PdoEntryPos> for u8 {
    fn from(pos: PdoEntryPos) -> Self {
        pos.0
    }
}

/// SDO Index
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SdoIdx {
    pub idx: Idx,
    pub sub_idx: SubIdx,
}

/// SDO Meta Information
#[derive(Debug, Clone, PartialEq)]
pub struct SdoInfo {
    pub pos: SdoPos, // TODO: do we need this info here?
    pub idx: Idx,
    pub max_sub_idx: SubIdx,
    pub object_code: Option<u8>,
    pub name: String,
}

/// SDO Entry Information
#[derive(Debug, Clone, PartialEq)]
pub struct SdoEntryInfo {
    pub data_type: DataType,
    pub bit_len: u16,
    pub access: SdoEntryAccess,
    pub description: String,
}

/// SDO Entry Address
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SdoEntryAddr {
    ByPos(SdoPos, SubIdx),
    ByIdx(SdoIdx),
}

/// SDO Entry Access
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SdoEntryAccess {
    pub pre_op: Access,
    pub safe_op: Access,
    pub op: Access,
}

/// PDO Index
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PdoIdx(u16);

impl From<u16> for PdoIdx {
    fn from(idx: u16) -> Self {
        Self(idx)
    }
}

/// PDO Meta Information
#[derive(Debug, Clone, PartialEq)]
pub struct PdoInfo {
    pub sm: SmIdx,
    pub pos: PdoPos,
    pub idx: Idx,
    pub entry_count: u8,
    pub name: String,
}

/// PDO Entry Meta Information
#[derive(Debug, Clone, PartialEq)]
pub struct PdoEntryInfo {
    pub pos: PdoEntryPos,
    pub entry_idx: PdoEntryIdx,
    pub bit_len: u8,
    pub name: String,
}

impl From<PdoIdx> for u16 {
    fn from(idx: PdoIdx) -> Self {
        idx.0
    }
}

/// PDO Entry Index
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PdoEntryIdx {
    pub idx: Idx,
    pub sub_idx: SubIdx,
}

/// Domain Index
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct DomainIdx(usize);

impl From<usize> for DomainIdx {
    fn from(idx: usize) -> Self {
        Self(idx)
    }
}

impl From<DomainIdx> for usize {
    fn from(idx: DomainIdx) -> Self {
        idx.0
    }
}

impl TryFrom<DomainIdx> for u32 {
    type Error = TryFromIntError;
    fn try_from(idx: DomainIdx) -> Result<Self, Self::Error> {
        u32::try_from(idx.0)
    }
}

impl TryFrom<DomainIdx> for u64 {
    type Error = TryFromIntError;
    fn try_from(idx: DomainIdx) -> Result<Self, Self::Error> {
        u64::try_from(idx.0)
    }
}

/// Sync Master Index
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct SmIdx(u8);

impl From<u8> for SmIdx {
    fn from(idx: u8) -> Self {
        Self(idx)
    }
}

impl From<SmIdx> for u8 {
    fn from(idx: SmIdx) -> Self {
        idx.0
    }
}

/// Data Access Type
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum Access {
    /// Read only
    ReadOnly,
    /// Read write
    ReadWrite,
    /// Write only
    WriteOnly,
    /// Unknown access
    Unknown,
}

/// Data Type
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, FromPrimitive)]
pub enum DataType {
    /// BIT
    Bool = 0x001,
    /// BYTE
    Byte = 0x01E,

    /// SINT
    I8 = 0x0002,
    /// INT
    I16 = 0x0003,
    /// DINT
    I32 = 0x0004,
    /// LINT
    I64 = 0x0015,

    /// USINT
    U8 = 0x0005,
    /// UINT
    U16 = 0x0006,
    /// UDINT
    U32 = 0x0007,
    /// ULINT
    U64 = 0x001B,

    /// REAL
    F32 = 0x0008,
    /// LREAL
    F64 = 0x0011,

    /// STRING(n) a.k.a. visiable string
    String = 0x0009,

    /// ARRAY of BYTE a.k.a. Octet String
    U8Array = 0x000A,

    /// ARRAY of UINT a.k.a. Unicode String
    U16Array = 0x000B,

    I24 = 0x0010,
    I40 = 0x0012,
    I48 = 0x0013,
    I56 = 0x0014,

    U24 = 0x0016,
    U40 = 0x0018,
    U48 = 0x0019,
    U56 = 0x001A,

    /// BIT 1
    Bit1 = 0x0030,
    /// BIT 2
    Bit2 = 0x0031,
    /// BIT 3
    Bit3 = 0x0032,
    /// BIT 4
    Bit4 = 040033,
    /// BIT 5
    Bit5 = 0x0034,
    /// BIT 6
    Bit6 = 0x0035,
    /// BIT 7
    Bit7 = 0x0036,
    /// BIT 8
    Bit8 = 0x0037,

    /// Time of Day
    TimeOfDay = 0x000C,
    /// Time Difference
    TimeDifference = 0x000D,

    /// Domain
    Domain = 0x000F,

    Raw = 0xFFFF,
}

/// Offset of a PDO entry in the domain image.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Offset {
    pub byte: usize,
    pub bit: u32,
}

/// ESM states
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AlState {
    Boot = 0x3,
    Init = 0x1,
    PreOp = 0x2,
    SafeOp = 0x4,
    Op = 0x8,
}

/// ESM states
#[derive(Debug)]
pub struct InvalidAlStateError;

impl TryFrom<u8> for AlState {
    type Error = InvalidAlStateError;
    fn try_from(st: u8) -> Result<Self, Self::Error> {
        match st {
            1 => Ok(AlState::Init),
            2 => Ok(AlState::PreOp),
            3 => Ok(AlState::Boot),
            4 => Ok(AlState::SafeOp),
            8 => Ok(AlState::Op),
            _ => Err(InvalidAlStateError),
        }
    }
}

impl From<AlState> for u8 {
    fn from(st: AlState) -> Self {
        st as u8
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn u8_from_al_state() {
        assert_eq!(u8::from(AlState::Init), 1);
        assert_eq!(u8::from(AlState::PreOp), 2);
        assert_eq!(u8::from(AlState::Boot), 3);
        assert_eq!(u8::from(AlState::SafeOp), 4);
        assert_eq!(u8::from(AlState::Op), 8);
    }

    #[test]
    fn try_al_state_from_u8() {
        assert_eq!(AlState::try_from(1).unwrap(), AlState::Init);
        assert_eq!(AlState::try_from(2).unwrap(), AlState::PreOp);
        assert_eq!(AlState::try_from(3).unwrap(), AlState::Boot);
        assert_eq!(AlState::try_from(4).unwrap(), AlState::SafeOp);
        assert_eq!(AlState::try_from(8).unwrap(), AlState::Op);
        assert!(AlState::try_from(0).is_err());
        assert!(AlState::try_from(5).is_err());
        assert!(AlState::try_from(6).is_err());
        assert!(AlState::try_from(7).is_err());
    }
}