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
use crate::GLOBAL_ADDRESS;
use core::convert::TryFrom;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum IdError {
    CanNotSendToDestination,
    DestinationRequired,
    InvalidId,
    InvalidPriority,
}

pub type Result<T> = core::result::Result<T, IdError>;

#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Priority {
    Priority0 = 0,
    Priority1 = 1,
    Priority2 = 2,
    Priority3 = 3,
    Priority4 = 4,
    Priority5 = 5,
    Priority6 = 6,
    Priority7 = 7,
}

#[derive(Clone, Copy, Debug)]
pub struct Id {
    id: u32,
}

impl Id {
    //TODO: figure out if this should be split into two constructors
    pub fn new(prio: Priority, pgn: u32, src: u8, dst: u8) -> Result<Self> {
        let mut id: u32 = 0x00;

        id = id | (src & 0xff) as u32;

        let pf = (pgn >> 8) & 0xff;
        if pf <= 239 {
            // PDU 1
            id |= ((dst & 0xff) as u32) << 8;
            id |= pgn << 8;
        } else {
            if dst != GLOBAL_ADDRESS {
                return Err(IdError::CanNotSendToDestination);
            }
            // PDU 2
            id |= pgn << 8;
        }
        id |= (prio as u32) << 26;

        Ok(Id { id })
    }

    pub fn priority(&self) -> Priority {
        let prio: u8 = ((self.id >> 26) & 0x7) as u8;
        match prio {
            0 => Priority::Priority0,
            1 => Priority::Priority1,
            2 => Priority::Priority2,
            3 => Priority::Priority3,
            4 => Priority::Priority4,
            5 => Priority::Priority5,
            6 => Priority::Priority6,
            7 => Priority::Priority7,
            _ => panic!("Invalid priority"),
        }
    }

    pub fn pgn(&self) -> u32 {
        let pf: u8 = (self.id >> 16) as u8;
        let dp: u8 = ((self.id >> 24) & 1) as u8;
        if pf <= 239 {
            // PDU1 format, the PS contains the destination address
            let pgn = ((dp as u32) << 16) + ((pf as u32) << 8);
            pgn as u32
        } else {
            // PDU2 format, the PGN is extended
            let ps: u8 = (self.id >> 8) as u8;
            let pgn = ((dp as u32) << 16) + ((pf as u32) << 8) + (ps as u32);
            pgn as u32
        }
    }

    pub fn source(&self) -> u8 {
        (self.id >> 0) as u8
    }

    pub fn destination(&self) -> u8 {
        let pf: u8 = (self.id >> 16) as u8;
        if pf <= 239 {
            // PDU1 format, the PS contains the destination address
            let ps: u8 = (self.id >> 8) as u8;
            ps
        } else {
            // PDU2 format, the destination is implied global and the PGN is extended
            0xff
        }
    }

    pub fn value(&self) -> u32 {
        self.id
    }
}

impl TryFrom<u32> for Id {
    type Error = IdError;

    fn try_from(val: u32) -> Result<Id> {
        validate_id(&val)?;

        Ok(Id { id: val })
    }
}

fn validate_id(id: &u32) -> Result<()> {
    if id & 0xe0000000 > 0 {
        return Err(IdError::InvalidId);
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use crate::{Id, Priority, GLOBAL_ADDRESS};
    use core::convert::TryFrom;

    #[test]
    fn id_new_destination() {
        struct TestCase {
            id: u32,
            prio: Priority,
            pgn: u32,
            src: u8,
            dst: u8,
        }
        let test_cases = [
            TestCase {
                id: 0x18eafc00,
                prio: Priority::Priority6,
                pgn: 59904,
                src: 0,
                dst: 252,
            },
            TestCase {
                id: 0x1cecff3d,
                prio: Priority::Priority7,
                pgn: 60416,
                src: 61,
                dst: GLOBAL_ADDRESS,
            },
            TestCase {
                id: 0xcfe6cee,
                prio: Priority::Priority3,
                pgn: 65132,
                src: 238,
                dst: 255,
            },
        ];
        for i in &test_cases {
            let id: u32 = Id::new(i.prio, i.pgn, i.src, i.dst).expect("Invalid parameter").value();
            assert_eq!(id, i.id)
        }
    }

    #[test]
    fn id_priority() {
        struct TestCase {
            id: u32,
            prio: Priority,
        }
        let test_cases = [
            TestCase {
                id: 0x18eafc00,
                prio: Priority::Priority6,
            },
            TestCase {
                id: 0x1cecff3d,
                prio: Priority::Priority7,
            },
            TestCase {
                id: 0xcfe6cee,
                prio: Priority::Priority3,
            },
        ];
        for i in &test_cases {
            let id = Id::try_from(i.id).expect("Invalid CanID");
            assert_eq!(id.priority(), i.prio)
        }
    }

    #[test]
    fn id_pgn() {
        struct TestCase {
            id: u32,
            pgn: u32,
        }
        let test_cases = [
            TestCase {
                id: 0x18eafc00,
                pgn: 59904,
            },
            TestCase {
                id: 0x1cecff3d,
                pgn: 60416,
            },
            TestCase {
                id: 0xcfe6cee,
                pgn: 65132,
            },
        ];
        for i in &test_cases {
            let id = Id::try_from(i.id).expect("Invalid CanID");
            assert_eq!(id.pgn(), i.pgn)
        }
    }

    #[test]
    fn id_source() {
        struct TestCase {
            id: u32,
            src: u8,
        }
        let test_cases = [
            TestCase {
                id: 0x18eafc00,
                src: 0,
            },
            TestCase {
                id: 0x1cecff3d,
                src: 61,
            },
            TestCase {
                id: 0xcfe6cee,
                src: 238,
            },
        ];
        for i in &test_cases {
            let id = Id::try_from(i.id).expect("Invalid CanID");
            assert_eq!(id.source(), i.src)
        }
    }

    #[test]
    fn id_destination() {
        struct TestCase {
            id: u32,
            dst: u8,
        }
        let test_cases = [
            TestCase {
                id: 0x18eafc00,
                dst: 252,
            },
            TestCase {
                id: 0x1cecff3d,
                dst: 255,
            },
            TestCase {
                id: 0xcfe6cee,
                dst: 255,
            },
        ];
        for i in &test_cases {
            let id = Id::try_from(i.id).expect("Invalid CanID");
            assert_eq!(id.destination(), i.dst)
        }
    }
}