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
//! # sensirion-hdlc
//! Only frames the data.  Rust implementation of Sensirion High-level Data Link Control (HDLC)
//! library.
//!
//! ## Usage
//!
//! ### Encode packet
//! ```rust
//! extern crate sensirion_hdlc;
//! use sensirion_hdlc::{SpecialChars, encode};
//!
//! let msg = [0x01, 0x50, 0x00, 0x00, 0x00, 0x05, 0x80, 0x09, 0x20];
//! let cmp = [0x7E, 0x01, 0x50, 0x00, 0x00, 0x00, 0x05, 0x80, 0x09, 0x20, 0x7E];
//!
//! let result = encode(&msg, SpecialChars::default()).unwrap();
//!
//! assert_eq!(result[0..result.len()], cmp);
//! ```
//!
//! ### Custom Special Characters
//! ```rust
//! extern crate sensirion_hdlc;
//! use sensirion_hdlc::{SpecialChars, encode};
//!
//! let msg = [0x01, 0x7E, 0x70, 0x50, 0x00, 0x05, 0x80, 0x09, 0xe2];
//! let cmp = [0x71, 0x01, 0x7E, 0x70, 0x50, 0x50, 0x00, 0x05, 0x80, 0x09, 0xe2, 0x71];
//! let chars = SpecialChars::new(0x71, 0x70, 0x51, 0x50, 0x11, 0x31, 0x13, 0x33).unwrap();
//!
//! let result = encode(&msg, chars).unwrap();
//!
//! assert_eq!(result[0..result.len()], cmp)
//! ```
//!
//! ### Decode packet
//! ```rust
//! extern crate sensirion_hdlc;
//! use sensirion_hdlc::{SpecialChars, decode};
//!
//! let chars = SpecialChars::default();
//! let msg = [
//!     chars.fend, 0x01, 0x50, 0x00, 0x00, 0x00, 0x05, 0x80, 0x29, chars.fend,
//! ];
//! let cmp = [0x01, 0x50, 0x00, 0x00, 0x00, 0x05, 0x80, 0x29];
//!
//! let result = decode(&msg, chars).unwrap();
//!
//! assert_eq!(result[0..result.len()], cmp);
//! ```

#![no_std]
#![deny(missing_docs)]

use arrayvec::ArrayVec;

/// Special Character structure for holding the encode and decode values.
///
/// # Default
///
/// * **FEND**  = 0x7E;
/// * **FESC**  = 0x7D;
/// * **TFEND** = 0x5E;
/// * **TFESC** = 0x5D;
/// * **OB1**   = 0x11;
/// * **TFOB1** = 0x31;
/// * **OB2**   = 0x13;
/// * **TFOB2** = 0x33;
#[derive(Debug, Copy, Clone)]
pub struct SpecialChars {
    /// Frame END. Byte that marks the beginning and end of a packet
    pub fend: u8,
    /// Frame ESCape. Byte that marks the start of a swap byte
    pub fesc: u8,
    /// Trade Frame END. Byte that is substituted for the FEND byte
    pub tfend: u8,
    /// Trade Frame ESCape. Byte that is substituted for the FESC byte
    pub tfesc: u8,
    /// Original Byte 1. Byte that will be substituted for the TFOB1 byte
    pub ob1: u8,
    /// Trade Frame ESCape. Byte that is substituted for the Original Byte 1
    pub tfob1: u8,
    /// Original Byte 2. Byte that is substituted for the TFOB2 byte
    pub ob2: u8,
    /// Trade Frame Origina Byte 2. Byte that is substituted for the Original Byte 2
    pub tfob2: u8,
}

impl Default for SpecialChars {
    /// Creates the default SpecialChars structure for encoding/decoding a packet
    fn default() -> SpecialChars {
        SpecialChars {
            fend: 0x7E,
            fesc: 0x7D,
            tfend: 0x5E,
            tfesc: 0x5D,
            ob1: 0x11,
            tfob1: 0x31,
            ob2: 0x13,
            tfob2: 0x33,
        }
    }
}
impl SpecialChars {
    /// Creates a new SpecialChars structure for encoding/decoding a packet
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        fend: u8,
        fesc: u8,
        tfend: u8,
        tfesc: u8,
        ob1: u8,
        tfob1: u8,
        ob2: u8,
        tfob2: u8,
    ) -> Result<SpecialChars, HDLCError> {
        // Safety check to make sure the special character values are all unique
        let values: [u8; 8] = [fend, fesc, tfend, tfesc, ob1, tfob1, ob2, tfob2];
        for i in 0..values.len() - 1 {
            if values[i + 1..].contains(&values[i]) {
                return Err(HDLCError::DuplicateSpecialChar);
            }
        }

        Ok(SpecialChars {
            fend,
            fesc,
            tfend,
            tfesc,
            ob1,
            tfob1,
            ob2,
            tfob2,
        })
    }
}

/// Calculate the SHDLC checksum
pub fn calculate_checksum(data: &[u8]) -> u8 {
    data.iter().fold(0u8, |acc, x| acc.wrapping_add(*x)) ^ 0xFFu8
}

/// Produces escaped (encoded) message surrounded with `FEND`
///
/// # Inputs
/// * **Vec<u8>**: A vector of the bytes you want to encode
/// * **SpecialChars**: The special characters you want to swap
///
/// # Output
///
/// * **Result<ArrayVec<u8;1024>>**: Encoded output message
///
///
/// # Error
///
/// * **HDLCError::TooMuchData**: More than 260 bytes to be encoded
///
/// # Example
/// ```rust
/// extern crate sensirion_hdlc;
/// let chars = sensirion_hdlc::SpecialChars::default();
/// let input: Vec<u8> = vec![0x01, 0x50, 0x00, 0x00, 0x00, 0x05, 0x80, 0x09];
/// let op_vec = sensirion_hdlc::encode(&input.to_vec(), chars);
/// ```
pub fn encode(data: &[u8], s_chars: SpecialChars) -> Result<ArrayVec<[u8; 1024]>, HDLCError> {
    if data.len() > 260 {
        return Err(HDLCError::TooMuchData);
    }

    // Iterator over the input that allows peeking
    let input_iter = data.iter();

    let mut output = ArrayVec::<[_; 1024]>::new();
    //Push initial FEND
    output.push(s_chars.fend);

    // Loop over every byte of the message
    for value in input_iter {
        match *value {
            // FEND , FESC, ob1 and ob2
            val if val == s_chars.fesc => {
                output.push(s_chars.fesc);
                output.push(s_chars.tfesc);
            }
            val if val == s_chars.fend => {
                output.push(s_chars.fesc);
                output.push(s_chars.tfend);
            }
            val if val == s_chars.ob1 => {
                output.push(s_chars.fesc);
                output.push(s_chars.tfob1);
            }
            val if val == s_chars.ob2 => {
                output.push(s_chars.fesc);
                output.push(s_chars.tfob2);
            }
            // Handle any other bytes
            _ => output.push(*value),
        }
    }

    // Push final FEND
    output.push(s_chars.fend);

    Ok(output)
}

/// Produces unescaped (decoded) message without `FEND` characters.
///
/// # Inputs
/// * **Vec<u8>**: A vector of the bytes you want to decode
/// * **SpecialChars**: The special characters you want to swap
///
/// # Output
///
/// * **Result<ArrayVec<u8;1024>>**: Decoded output message
///
/// # Error
///
/// * **HDLCError::TooMuchDecodedData**: More than expected 260 bytes of decoded data
/// * **HDLCError::FendCharInData**: Checks to make sure the full decoded message is the full
/// length.  Found the `SpecialChars::fend` inside the message.
/// * **HDLCError::MissingTradeChar**: Checks to make sure every frame escape character `fesc`
/// is followed by either a `tfend` or a `tfesc`.
/// * **HDLCError::MissingFirstFend**: Input vector is missing a first `SpecialChars::fend`
/// * **HDLCError::MissingFinalFend**: Input vector is missing a final `SpecialChars::fend`
/// * **HDLCError::TooFewData**: Data to decode is fewer than 4 bytes`
/// * **HDLCError::TooMuchData**: Data to decode is larger than 1000 bytes`
///
///
/// # Example
/// ```rust
/// extern crate sensirion_hdlc;
/// let chars = sensirion_hdlc::SpecialChars::default();
/// let input =[ 0x7E, 0x01, 0x50, 0x00, 0x00, 0x00, 0x05, 0x80, 0x09, 0x7E];
/// let op_vec = sensirion_hdlc::decode(&input.to_vec(), chars);
/// ```
pub fn decode(input: &[u8], s_chars: SpecialChars) -> Result<ArrayVec<[u8; 1024]>, HDLCError> {
    if input.len() < 4 {
        return Err(HDLCError::TooFewData);
    }

    if input.len() > 1000 {
        return Err(HDLCError::TooMuchData);
    }

    // Verify input begins with a FEND
    if input[0] != s_chars.fend {
        return Err(HDLCError::MissingFirstFend);
    }
    // Verify input ends with a FEND
    if input[input.len() - 1] != s_chars.fend {
        return Err(HDLCError::MissingFinalFend);
    }

    let mut output = ArrayVec::<[u8; 1024]>::new();

    // Iterator over the input that allows peeking
    let mut input_iter = input[1..input.len() - 1].iter().peekable();

    // Loop over every byte of the message
    while let Some(value) = input_iter.next() {
        match *value {
            // Handle a FESC
            val if val == s_chars.fesc => match input_iter.next() {
                Some(&val) if val == s_chars.tfend => output.push(s_chars.fend),
                Some(&val) if val == s_chars.tfesc => output.push(s_chars.fesc),
                Some(&val) if val == s_chars.tfob1 => output.push(s_chars.ob1),
                Some(&val) if val == s_chars.tfob2 => output.push(s_chars.ob2),
                _ => return Err(HDLCError::MissingTradeChar),
            },
            // Handle a FEND
            val if val == s_chars.fend => {
                return Err(HDLCError::FendCharInData);
            }
            // Handle any other bytes
            _ => output.push(*value),
        }
    }

    if output.len() > 260 {
        return Err(HDLCError::TooMuchDecodedData);
    }

    Ok(output)
}

#[derive(Debug, PartialEq)]
/// Common error for HDLC actions.
pub enum HDLCError {
    /// Catches duplicate special characters.   
    DuplicateSpecialChar,
    /// Catches a random sync char in the data.
    FendCharInData,
    /// Catches a random swap char, `fesc`, in the data with no `tfend` or `tfesc`.
    MissingTradeChar,
    /// No first fend on the message.    
    MissingFirstFend,
    /// No final fend on the message.
    MissingFinalFend,
    /// Too much data to be converted into a SHDLC frame
    TooMuchData,
    /// Too few data to be converted from a SHDLC frame
    TooFewData,
    /// Checksum for decoded Frame is invalid
    InvalidChecksum,
    /// More than 259 bytes resulted after decoding SHDLC frame
    TooMuchDecodedData,
}

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

    #[test]
    fn encode_start_measumement() {
        let mosi_data = [0x00, 0x00, 0x02, 0x01, 0x03, 0xf9];
        let expected = [0x7e, 0x00, 0x00, 0x02, 0x01, 0x03, 0xf9, 0x7e];
        let encoded = encode(&mosi_data, SpecialChars::default()).unwrap();
        assert_eq!(encoded[0..encoded.len()], expected);
    }

    #[test]
    fn encode_test() {
        let mosi_data = [0x00, 0x01, 0x00, 0xfe];
        let expected = [0x7e, 0x00, 0x01, 0x00, 0xfe, 0x7e];
        let encoded = encode(&mosi_data, SpecialChars::default()).unwrap();
        assert_eq!(encoded[0..encoded.len()], expected);
    }

    #[test]
    fn decode_test() {
        let expected = [0x00, 0x01, 0x00, 0xfe];
        let mosi_data = [0x7e, 0x00, 0x01, 0x00, 0xfe, 0x7e];
        let encoded = decode(&mosi_data, SpecialChars::default()).unwrap();
        assert_eq!(encoded[0..encoded.len()], expected);
    }

    #[test]
    fn test_calculate_checksum() {
        let checksum = calculate_checksum(&[1, 2, 3, 4, 5, 6]);
        assert_eq!(checksum, 234);
    }
    #[test]
    fn test_calculate_checksum_overflow() {
        let checksum = calculate_checksum(&[200, 201, 202]);
        assert_eq!(checksum, 164);
    }
}