@@ -12,109 +12,109 @@ use crate::parse::error::{ParseError, Result};
12
12
13
13
#[ allow( dead_code) ]
14
14
pub struct Datagram {
15
- data : Vec < u8 > ,
16
- index : usize ,
17
- /// IEC 60780 control field
18
- packet_control : u8 ,
19
- /// IEC 60780 address field
20
- address : u8 ,
21
- /// IEC 13757 (mbus) control information (CI) field
22
- mbus_control : u8 ,
15
+ data : Vec < u8 > ,
16
+ index : usize ,
17
+ /// IEC 60780 control field
18
+ packet_control : u8 ,
19
+ /// IEC 60780 address field
20
+ address : u8 ,
21
+ /// IEC 13757 (mbus) control information (CI) field
22
+ mbus_control : u8 ,
23
23
}
24
24
25
25
impl Datagram {
26
- pub fn new ( data : Vec < u8 > , packet_control : u8 , address : u8 , mbus_control : u8 ) -> Self {
27
- Self {
28
- data,
29
- index : 0 ,
30
- packet_control,
31
- address,
32
- mbus_control,
33
- }
34
- }
26
+ pub fn new ( data : Vec < u8 > , packet_control : u8 , address : u8 , mbus_control : u8 ) -> Self {
27
+ Self {
28
+ data,
29
+ index : 0 ,
30
+ packet_control,
31
+ address,
32
+ mbus_control,
33
+ }
34
+ }
35
35
36
- pub fn parse ( data : Vec < u8 > ) -> Result < Self > {
37
- let len = data. len ( ) ;
38
- if len == 0 {
39
- return Err ( ParseError :: InvalidPacket ( "Packet is empty" ) ) ;
40
- }
41
- let start1 = data[ 0 ] ;
42
- match start1 {
43
- 0x68 => ( ) ,
44
- // TODO: Figure out where these are defined and why libmbus supports them,
45
- // because they're not in IEC 60870-5-2
46
- 0xE5 => todo ! ( "ACK packets aren't supported yet" ) ,
47
- 0x10 => todo ! ( "Short packets aren't supported yet" ) ,
48
- _ => return Err ( ParseError :: InvalidPacket ( "Start byte is invalid" ) ) ,
49
- }
36
+ pub fn parse ( data : Vec < u8 > ) -> Result < Self > {
37
+ let len = data. len ( ) ;
38
+ if len == 0 {
39
+ return Err ( ParseError :: InvalidPacket ( "Packet is empty" ) ) ;
40
+ }
41
+ let start1 = data[ 0 ] ;
42
+ match start1 {
43
+ 0x68 => ( ) ,
44
+ // TODO: Figure out where these are defined and why libmbus supports them,
45
+ // because they're not in IEC 60870-5-2
46
+ 0xE5 => todo ! ( "ACK packets aren't supported yet" ) ,
47
+ 0x10 => todo ! ( "Short packets aren't supported yet" ) ,
48
+ _ => return Err ( ParseError :: InvalidPacket ( "Start byte is invalid" ) ) ,
49
+ }
50
50
51
- let [ _, length1, length2, start2, packet_control, address, mbus_control, .., checksum, end] =
52
- data[ ..]
53
- else {
54
- return Err ( ParseError :: InvalidPacket ( "Packet is too short" ) ) ;
55
- } ;
56
- if start1 != 0x68 {
57
- return Err ( ParseError :: InvalidPacket ( "Start byte is invalid" ) ) ;
58
- } else if length1 != length2 {
59
- return Err ( ParseError :: InvalidPacket ( "Lengths don't match" ) ) ;
60
- } else if length1 as usize != len - 6 {
61
- return Err ( ParseError :: InvalidPacket ( "Packet length incorrect" ) ) ;
62
- } else if start2 != 0x68 {
63
- return Err ( ParseError :: InvalidPacket ( "Second start byte is invalid" ) ) ;
64
- } else if checksum
65
- != data
66
- . iter ( )
67
- . skip ( 4 )
68
- . take ( length1 as usize )
69
- . copied ( )
70
- . reduce ( u8:: wrapping_add)
71
- . unwrap_or ( 0 )
72
- {
73
- return Err ( ParseError :: InvalidPacket ( "Checksum doesn't match" ) ) ;
74
- } else if end != 0x16 {
75
- return Err ( ParseError :: InvalidPacket ( "End byte is invalid" ) ) ;
76
- }
77
- Ok ( Self :: new (
78
- ( data[ 6 ..len - 2 ] ) . into ( ) ,
79
- packet_control,
80
- address,
81
- mbus_control,
82
- ) )
83
- }
51
+ let [ _, length1, length2, start2, packet_control, address, mbus_control, .., checksum, end] =
52
+ data[ ..]
53
+ else {
54
+ return Err ( ParseError :: InvalidPacket ( "Packet is too short" ) ) ;
55
+ } ;
56
+ if start1 != 0x68 {
57
+ return Err ( ParseError :: InvalidPacket ( "Start byte is invalid" ) ) ;
58
+ } else if length1 != length2 {
59
+ return Err ( ParseError :: InvalidPacket ( "Lengths don't match" ) ) ;
60
+ } else if length1 as usize != len - 6 {
61
+ return Err ( ParseError :: InvalidPacket ( "Packet length incorrect" ) ) ;
62
+ } else if start2 != 0x68 {
63
+ return Err ( ParseError :: InvalidPacket ( "Second start byte is invalid" ) ) ;
64
+ } else if checksum
65
+ != data
66
+ . iter ( )
67
+ . skip ( 4 )
68
+ . take ( length1 as usize )
69
+ . copied ( )
70
+ . reduce ( u8:: wrapping_add)
71
+ . unwrap_or ( 0 )
72
+ {
73
+ return Err ( ParseError :: InvalidPacket ( "Checksum doesn't match" ) ) ;
74
+ } else if end != 0x16 {
75
+ return Err ( ParseError :: InvalidPacket ( "End byte is invalid" ) ) ;
76
+ }
77
+ Ok ( Self :: new (
78
+ ( data[ 6 ..len - 2 ] ) . into ( ) ,
79
+ packet_control,
80
+ address,
81
+ mbus_control,
82
+ ) )
83
+ }
84
84
85
- pub fn get_byte ( & self , index : usize ) -> Result < u8 > {
86
- self . data
87
- . get ( index)
88
- . copied ( )
89
- . ok_or ( ParseError :: UnexpectedEOF )
90
- }
85
+ pub fn get_byte ( & self , index : usize ) -> Result < u8 > {
86
+ self . data
87
+ . get ( index)
88
+ . copied ( )
89
+ . ok_or ( ParseError :: UnexpectedEOF )
90
+ }
91
91
92
- pub fn peek ( & self ) -> Result < u8 > {
93
- self . get_byte ( self . index )
94
- }
92
+ pub fn peek ( & self ) -> Result < u8 > {
93
+ self . get_byte ( self . index )
94
+ }
95
95
96
- pub fn last_byte ( & self ) -> Result < u8 > {
97
- if self . index > 0 {
98
- self . get_byte ( self . index - 1 )
99
- } else {
100
- Err ( ParseError :: UnexpectedEOF )
101
- }
102
- }
96
+ pub fn last_byte ( & self ) -> Result < u8 > {
97
+ if self . index > 0 {
98
+ self . get_byte ( self . index - 1 )
99
+ } else {
100
+ Err ( ParseError :: UnexpectedEOF )
101
+ }
102
+ }
103
103
104
- pub fn next_byte ( & mut self ) -> Result < u8 > {
105
- let ret = self . get_byte ( self . index ) ;
106
- if ret. is_ok ( ) {
107
- self . index += 1 ;
108
- }
109
- ret
110
- }
104
+ pub fn next_byte ( & mut self ) -> Result < u8 > {
105
+ let ret = self . get_byte ( self . index ) ;
106
+ if ret. is_ok ( ) {
107
+ self . index += 1 ;
108
+ }
109
+ ret
110
+ }
111
111
112
- pub fn take ( & mut self , n : usize ) -> Result < Vec < u8 > > {
113
- if self . index + n >= self . data . len ( ) {
114
- return Err ( ParseError :: UnexpectedEOF ) ;
115
- }
116
- let start = self . index ;
117
- self . index += n;
118
- Ok ( self . data [ start..self . index ] . into ( ) )
119
- }
112
+ pub fn take ( & mut self , n : usize ) -> Result < Vec < u8 > > {
113
+ if self . index + n >= self . data . len ( ) {
114
+ return Err ( ParseError :: UnexpectedEOF ) ;
115
+ }
116
+ let start = self . index ;
117
+ self . index += n;
118
+ Ok ( self . data [ start..self . index ] . into ( ) )
119
+ }
120
120
}
0 commit comments