Skip to main content

simple_someip/protocol/
byte_order.rs

1use crate::protocol::Error;
2use embedded_io::Error as _;
3
4/// Extension trait for reading big-endian values from a byte stream.
5///
6/// The only required method is [`read_bytes`](ReadBytesExt::read_bytes).
7/// Backed by `embedded_io::Read` via a blanket impl.
8pub trait ReadBytesExt {
9    /// Read exactly `buf.len()` bytes from the stream.
10    ///
11    /// # Errors
12    /// Returns [`Error::Io`] if the underlying reader fails.
13    fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Error>;
14
15    /// Read a single `u8`.
16    ///
17    /// # Errors
18    /// Returns [`Error::Io`] if the underlying reader fails.
19    fn read_u8(&mut self) -> Result<u8, Error> {
20        let mut buf = [0u8; 1];
21        self.read_bytes(&mut buf)?;
22        Ok(buf[0])
23    }
24
25    /// Read an `i8`.
26    ///
27    /// # Errors
28    /// Returns [`Error::Io`] if the underlying reader fails.
29    fn read_i8(&mut self) -> Result<i8, Error> {
30        self.read_u8().map(u8::cast_signed)
31    }
32
33    /// Read a `u16` in big-endian byte order.
34    ///
35    /// # Errors
36    /// Returns [`Error::Io`] if the underlying reader fails.
37    fn read_u16_be(&mut self) -> Result<u16, Error> {
38        let mut buf = [0u8; 2];
39        self.read_bytes(&mut buf)?;
40        Ok(u16::from_be_bytes(buf))
41    }
42
43    /// Read an `i16` in big-endian byte order.
44    ///
45    /// # Errors
46    /// Returns [`Error::Io`] if the underlying reader fails.
47    fn read_i16_be(&mut self) -> Result<i16, Error> {
48        let mut buf = [0u8; 2];
49        self.read_bytes(&mut buf)?;
50        Ok(i16::from_be_bytes(buf))
51    }
52
53    /// Read the next 3 bytes as the lower 3 bytes of a `u32` in big-endian byte order.
54    ///
55    /// # Errors
56    /// Returns [`Error::Io`] if the underlying reader fails.
57    fn read_u24_be(&mut self) -> Result<u32, Error> {
58        let mut buf = [0u8; 3];
59        self.read_bytes(&mut buf)?;
60        Ok(u32::from_be_bytes([0, buf[0], buf[1], buf[2]]))
61    }
62
63    /// Read a `u32` in big-endian byte order.
64    ///
65    /// # Errors
66    /// Returns [`Error::Io`] if the underlying reader fails.
67    fn read_u32_be(&mut self) -> Result<u32, Error> {
68        let mut buf = [0u8; 4];
69        self.read_bytes(&mut buf)?;
70        Ok(u32::from_be_bytes(buf))
71    }
72
73    /// Read an `i32` in big-endian byte order.
74    ///
75    /// # Errors
76    /// Returns [`Error::Io`] if the underlying reader fails.
77    fn read_i32_be(&mut self) -> Result<i32, Error> {
78        let mut buf = [0u8; 4];
79        self.read_bytes(&mut buf)?;
80        Ok(i32::from_be_bytes(buf))
81    }
82
83    /// Read a `u64` in big-endian byte order.
84    ///
85    /// # Errors
86    /// Returns [`Error::Io`] if the underlying reader fails.
87    fn read_u64_be(&mut self) -> Result<u64, Error> {
88        let mut buf = [0u8; 8];
89        self.read_bytes(&mut buf)?;
90        Ok(u64::from_be_bytes(buf))
91    }
92
93    /// Read an `i64` in big-endian byte order.
94    ///
95    /// # Errors
96    /// Returns [`Error::Io`] if the underlying reader fails.
97    fn read_i64_be(&mut self) -> Result<i64, Error> {
98        let mut buf = [0u8; 8];
99        self.read_bytes(&mut buf)?;
100        Ok(i64::from_be_bytes(buf))
101    }
102
103    /// Read a `u128` in big-endian byte order.
104    ///
105    /// # Errors
106    /// Returns [`Error::Io`] if the underlying reader fails.
107    fn read_u128_be(&mut self) -> Result<u128, Error> {
108        let mut buf = [0u8; 16];
109        self.read_bytes(&mut buf)?;
110        Ok(u128::from_be_bytes(buf))
111    }
112
113    /// Read an `i128` in big-endian byte order.
114    ///
115    /// # Errors
116    /// Returns [`Error::Io`] if the underlying reader fails.
117    fn read_i128_be(&mut self) -> Result<i128, Error> {
118        let mut buf = [0u8; 16];
119        self.read_bytes(&mut buf)?;
120        Ok(i128::from_be_bytes(buf))
121    }
122
123    /// Read an `f32` in big-endian byte order.
124    ///
125    /// # Errors
126    /// Returns [`Error::Io`] if the underlying reader fails.
127    fn read_f32_be(&mut self) -> Result<f32, Error> {
128        let mut buf = [0u8; 4];
129        self.read_bytes(&mut buf)?;
130        Ok(f32::from_be_bytes(buf))
131    }
132
133    /// Read an `f64` in big-endian byte order.
134    ///
135    /// # Errors
136    /// Returns [`Error::Io`] if the underlying reader fails.
137    fn read_f64_be(&mut self) -> Result<f64, Error> {
138        let mut buf = [0u8; 8];
139        self.read_bytes(&mut buf)?;
140        Ok(f64::from_be_bytes(buf))
141    }
142}
143
144impl<T: embedded_io::Read> ReadBytesExt for T {
145    fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Error> {
146        self.read_exact(buf).map_err(|e| match e {
147            embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
148            embedded_io::ReadExactError::Other(e) => Error::Io(e.kind()),
149        })
150    }
151}
152
153/// Extension trait for writing big-endian values to a byte stream.
154///
155/// The only required method is [`write_bytes`](WriteBytesExt::write_bytes).
156/// Backed by `embedded_io::Write` via a blanket impl.
157pub trait WriteBytesExt {
158    /// Write all bytes from `buf` to the stream.
159    ///
160    /// # Errors
161    /// Returns [`Error::Io`] if the underlying writer fails.
162    fn write_bytes(&mut self, buf: &[u8]) -> Result<(), Error>;
163
164    /// Write a single `u8`.
165    ///
166    /// # Errors
167    /// Returns [`Error::Io`] if the underlying writer fails.
168    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
169        self.write_bytes(&[val])
170    }
171
172    /// Write an `i8`.
173    ///
174    /// # Errors
175    /// Returns [`Error::Io`] if the underlying writer fails.
176    fn write_i8(&mut self, val: i8) -> Result<(), Error> {
177        self.write_bytes(&[val.cast_unsigned()])
178    }
179
180    /// Write a `u16` in big-endian byte order.
181    ///
182    /// # Errors
183    /// Returns [`Error::Io`] if the underlying writer fails.
184    fn write_u16_be(&mut self, val: u16) -> Result<(), Error> {
185        self.write_bytes(&val.to_be_bytes())
186    }
187
188    /// Write an `i16` in big-endian byte order.
189    ///
190    /// # Errors
191    /// Returns [`Error::Io`] if the underlying writer fails.
192    fn write_i16_be(&mut self, val: i16) -> Result<(), Error> {
193        self.write_bytes(&val.to_be_bytes())
194    }
195
196    /// Write the lower 3 bytes of a `u32` in big-endian byte order.
197    ///
198    /// # Errors
199    /// Returns [`Error::Io`] if the underlying writer fails.
200    fn write_u24_be(&mut self, val: u32) -> Result<(), Error> {
201        self.write_bytes(&val.to_be_bytes()[1..])
202    }
203
204    /// Write a `u32` in big-endian byte order.
205    ///
206    /// # Errors
207    /// Returns [`Error::Io`] if the underlying writer fails.
208    fn write_u32_be(&mut self, val: u32) -> Result<(), Error> {
209        self.write_bytes(&val.to_be_bytes())
210    }
211
212    /// Write an `i32` in big-endian byte order.
213    ///
214    /// # Errors
215    /// Returns [`Error::Io`] if the underlying writer fails.
216    fn write_i32_be(&mut self, val: i32) -> Result<(), Error> {
217        self.write_bytes(&val.to_be_bytes())
218    }
219
220    /// Write a `u64` in big-endian byte order.
221    ///
222    /// # Errors
223    /// Returns [`Error::Io`] if the underlying writer fails.
224    fn write_u64_be(&mut self, val: u64) -> Result<(), Error> {
225        self.write_bytes(&val.to_be_bytes())
226    }
227
228    /// Write an `i64` in big-endian byte order.
229    ///
230    /// # Errors
231    /// Returns [`Error::Io`] if the underlying writer fails.
232    fn write_i64_be(&mut self, val: i64) -> Result<(), Error> {
233        self.write_bytes(&val.to_be_bytes())
234    }
235
236    /// Write a `u128` in big-endian byte order.
237    ///
238    /// # Errors
239    /// Returns [`Error::Io`] if the underlying writer fails.
240    fn write_u128_be(&mut self, val: u128) -> Result<(), Error> {
241        self.write_bytes(&val.to_be_bytes())
242    }
243
244    /// Write an `i128` in big-endian byte order.
245    ///
246    /// # Errors
247    /// Returns [`Error::Io`] if the underlying writer fails.
248    fn write_i128_be(&mut self, val: i128) -> Result<(), Error> {
249        self.write_bytes(&val.to_be_bytes())
250    }
251
252    /// Write an `f32` in big-endian byte order.
253    ///
254    /// # Errors
255    /// Returns [`Error::Io`] if the underlying writer fails.
256    fn write_f32_be(&mut self, val: f32) -> Result<(), Error> {
257        self.write_bytes(&val.to_be_bytes())
258    }
259
260    /// Write an `f64` in big-endian byte order.
261    ///
262    /// # Errors
263    /// Returns [`Error::Io`] if the underlying writer fails.
264    fn write_f64_be(&mut self, val: f64) -> Result<(), Error> {
265        self.write_bytes(&val.to_be_bytes())
266    }
267}
268
269impl<T: embedded_io::Write> WriteBytesExt for T {
270    fn write_bytes(&mut self, buf: &[u8]) -> Result<(), Error> {
271        self.write_all(buf).map_err(|e| Error::Io(e.kind()))
272    }
273}
274
275#[cfg(test)]
276mod tests {
277    use super::*;
278
279    struct FailingWriter;
280
281    impl embedded_io::ErrorType for FailingWriter {
282        type Error = embedded_io::ErrorKind;
283    }
284
285    impl embedded_io::Write for FailingWriter {
286        fn write(&mut self, _buf: &[u8]) -> Result<usize, Self::Error> {
287            Err(embedded_io::ErrorKind::BrokenPipe)
288        }
289
290        fn flush(&mut self) -> Result<(), Self::Error> {
291            Ok(())
292        }
293    }
294
295    struct FailingReader;
296
297    impl embedded_io::ErrorType for FailingReader {
298        type Error = embedded_io::ErrorKind;
299    }
300
301    impl embedded_io::Read for FailingReader {
302        fn read(&mut self, _buf: &mut [u8]) -> Result<usize, Self::Error> {
303            Err(embedded_io::ErrorKind::BrokenPipe)
304        }
305    }
306
307    // --- Error mapping ---
308
309    #[test]
310    fn write_io_error_maps_to_error_io() {
311        assert!(matches!(
312            FailingWriter.write_u8(0),
313            Err(Error::Io(embedded_io::ErrorKind::BrokenPipe))
314        ));
315    }
316
317    #[test]
318    fn read_io_error_maps_to_error_io() {
319        assert!(matches!(
320            FailingReader.read_u8(),
321            Err(Error::Io(embedded_io::ErrorKind::BrokenPipe))
322        ));
323    }
324
325    // --- ReadBytesExt ---
326
327    #[test]
328    fn read_u8_decodes_correctly() {
329        let buf: &[u8] = &[0xAB];
330        assert_eq!((&mut &*buf).read_u8().unwrap(), 0xAB);
331    }
332
333    #[test]
334    fn read_i8_decodes_correctly() {
335        let buf: &[u8] = &[0xFF];
336        assert_eq!((&mut &*buf).read_i8().unwrap(), -1);
337    }
338
339    #[test]
340    fn read_u16_be_decodes_correctly() {
341        let buf: &[u8] = &[0x01, 0x02];
342        assert_eq!((&mut &*buf).read_u16_be().unwrap(), 0x0102);
343    }
344
345    #[test]
346    fn read_i16_be_decodes_correctly() {
347        let buf: &[u8] = &[0xFF, 0xFE];
348        assert_eq!((&mut &*buf).read_i16_be().unwrap(), -2);
349    }
350
351    #[test]
352    fn read_u24_be_decodes_correctly() {
353        let buf: &[u8] = &[0x01, 0x02, 0x03];
354        assert_eq!((&mut &*buf).read_u24_be().unwrap(), 0x0001_0203);
355    }
356
357    #[test]
358    fn read_u32_be_decodes_correctly() {
359        let buf: &[u8] = &[0x01, 0x02, 0x03, 0x04];
360        assert_eq!((&mut &*buf).read_u32_be().unwrap(), 0x0102_0304);
361    }
362
363    #[test]
364    fn read_i32_be_decodes_correctly() {
365        let buf: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFE];
366        assert_eq!((&mut &*buf).read_i32_be().unwrap(), -2);
367    }
368
369    #[test]
370    fn read_u64_be_decodes_correctly() {
371        let buf: &[u8] = &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
372        assert_eq!((&mut &*buf).read_u64_be().unwrap(), 0x0102_0304_0506_0708);
373    }
374
375    #[test]
376    fn read_i64_be_decodes_correctly() {
377        let buf: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE];
378        assert_eq!((&mut &*buf).read_i64_be().unwrap(), -2);
379    }
380
381    #[test]
382    fn read_u128_be_decodes_correctly() {
383        let buf: &[u8] = &[
384            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
385            0x00, 0x01,
386        ];
387        assert_eq!((&mut &*buf).read_u128_be().unwrap(), 1);
388    }
389
390    #[test]
391    fn read_i128_be_decodes_correctly() {
392        let buf: &[u8] = &[
393            0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
394            0xFF, 0xFE,
395        ];
396        assert_eq!((&mut &*buf).read_i128_be().unwrap(), -2);
397    }
398
399    #[test]
400    fn read_f32_be_decodes_correctly() {
401        let expected: f32 = 1.0;
402        let buf = expected.to_be_bytes();
403        assert_eq!((&mut buf.as_slice()).read_f32_be().unwrap(), expected);
404    }
405
406    #[test]
407    fn read_f64_be_decodes_correctly() {
408        let expected: f64 = 1.0;
409        let buf = expected.to_be_bytes();
410        assert_eq!((&mut buf.as_slice()).read_f64_be().unwrap(), expected);
411    }
412
413    // --- WriteBytesExt ---
414
415    #[test]
416    fn write_u8_encodes_correctly() {
417        let mut buf = [0u8; 1];
418        buf.as_mut_slice().write_u8(0xAB).unwrap();
419        assert_eq!(buf, [0xAB]);
420    }
421
422    #[test]
423    fn write_i8_encodes_correctly() {
424        let mut buf = [0u8; 1];
425        buf.as_mut_slice().write_i8(-1).unwrap();
426        assert_eq!(buf, [0xFF]);
427    }
428
429    #[test]
430    fn write_u16_be_encodes_correctly() {
431        let mut buf = [0u8; 2];
432        buf.as_mut_slice().write_u16_be(0x0102).unwrap();
433        assert_eq!(buf, [0x01, 0x02]);
434    }
435
436    #[test]
437    fn write_i16_be_encodes_correctly() {
438        let mut buf = [0u8; 2];
439        buf.as_mut_slice().write_i16_be(-2).unwrap();
440        assert_eq!(buf, [0xFF, 0xFE]);
441    }
442
443    #[test]
444    fn write_u24_be_encodes_correctly() {
445        let mut buf = [0u8; 3];
446        buf.as_mut_slice().write_u24_be(0x0001_0203).unwrap();
447        assert_eq!(buf, [0x01, 0x02, 0x03]);
448    }
449
450    #[test]
451    fn write_u32_be_encodes_correctly() {
452        let mut buf = [0u8; 4];
453        buf.as_mut_slice().write_u32_be(0x0102_0304).unwrap();
454        assert_eq!(buf, [0x01, 0x02, 0x03, 0x04]);
455    }
456
457    #[test]
458    fn write_i32_be_encodes_correctly() {
459        let mut buf = [0u8; 4];
460        buf.as_mut_slice().write_i32_be(-2).unwrap();
461        assert_eq!(buf, [0xFF, 0xFF, 0xFF, 0xFE]);
462    }
463
464    #[test]
465    fn write_u64_be_encodes_correctly() {
466        let mut buf = [0u8; 8];
467        buf.as_mut_slice()
468            .write_u64_be(0x0102_0304_0506_0708)
469            .unwrap();
470        assert_eq!(buf, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
471    }
472
473    #[test]
474    fn write_i64_be_encodes_correctly() {
475        let mut buf = [0u8; 8];
476        buf.as_mut_slice().write_i64_be(-2).unwrap();
477        assert_eq!(buf, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]);
478    }
479
480    #[test]
481    fn write_u128_be_encodes_correctly() {
482        let mut buf = [0u8; 16];
483        buf.as_mut_slice().write_u128_be(1).unwrap();
484        assert_eq!(buf, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01]);
485    }
486
487    #[test]
488    fn write_i128_be_encodes_correctly() {
489        let mut buf = [0u8; 16];
490        buf.as_mut_slice().write_i128_be(-2).unwrap();
491        let expected = (-2_i128).to_be_bytes();
492        assert_eq!(buf, expected);
493    }
494
495    #[test]
496    fn write_f32_be_encodes_correctly() {
497        let val: f32 = 1.0;
498        let mut buf = [0u8; 4];
499        buf.as_mut_slice().write_f32_be(val).unwrap();
500        assert_eq!(buf, val.to_be_bytes());
501    }
502
503    #[test]
504    fn write_f64_be_encodes_correctly() {
505        let val: f64 = 1.0;
506        let mut buf = [0u8; 8];
507        buf.as_mut_slice().write_f64_be(val).unwrap();
508        assert_eq!(buf, val.to_be_bytes());
509    }
510
511    // --- Round-trip ---
512
513    #[test]
514    fn round_trip_f32() {
515        let val: f32 = core::f32::consts::PI;
516        let mut buf = [0u8; 4];
517        buf.as_mut_slice().write_f32_be(val).unwrap();
518        assert_eq!((&mut buf.as_slice()).read_f32_be().unwrap(), val);
519    }
520
521    #[test]
522    fn round_trip_f64() {
523        let val: f64 = core::f64::consts::PI;
524        let mut buf = [0u8; 8];
525        buf.as_mut_slice().write_f64_be(val).unwrap();
526        assert_eq!((&mut buf.as_slice()).read_f64_be().unwrap(), val);
527    }
528}