瀏覽代碼

rust-protocol: Add GetSalt, fix decoding, derive Debug.

Mathias Gottschlag 5 年之前
父節點
當前提交
0ef7cc7216
共有 1 個檔案被更改,包括 24 行新增23 行删除
  1. 24
    23
      common/rust-protocol/src/lib.rs

+ 24
- 23
common/rust-protocol/src/lib.rs 查看文件

7
 use crc16::{State, KERMIT};
7
 use crc16::{State, KERMIT};
8
 use xxtea_nostd::decrypt;
8
 use xxtea_nostd::decrypt;
9
 
9
 
10
-#[derive(Clone)]
10
+#[derive(Debug, Clone)]
11
 pub enum Packet {
11
 pub enum Packet {
12
+    GetSalt,
12
     Salt(u64),
13
     Salt(u64),
13
     Report(Report),
14
     Report(Report),
14
     GetValues(GetValues),
15
     GetValues(GetValues),
19
     pub fn decrypt_and_parse(key: &[u8], data: &mut [u8]) -> Option<Packet> {
20
     pub fn decrypt_and_parse(key: &[u8], data: &mut [u8]) -> Option<Packet> {
20
         decrypt_cbc(key, data);
21
         decrypt_cbc(key, data);
21
         let checksummed = &data[8..];
22
         let checksummed = &data[8..];
22
-        let calculated_crc = State::<KERMIT>::calculate(&checksummed);
23
-        let crc = u16::from_le_bytes(data[30..].try_into().unwrap());
24
-        if crc != calculated_crc {
23
+        let remainder = State::<KERMIT>::calculate(&checksummed);
24
+        if remainder != 0 {
25
             return None;
25
             return None;
26
         }
26
         }
27
         Self::decode(&checksummed[..22])
27
         Self::decode(&checksummed[..22])
33
         // below.
33
         // below.
34
         let count = data[0] >> 5;
34
         let count = data[0] >> 5;
35
         match type_ {
35
         match type_ {
36
-            0 => Some(Self::Salt({
36
+            0 => Some(Self::GetSalt),
37
+            1 => Some(Self::Salt({
37
                 // The lowest 8 bit of the salt are the device ID and are filled in by the caller.
38
                 // The lowest 8 bit of the salt are the device ID and are filled in by the caller.
38
                 u64::from_le_bytes(data[1..9].try_into().unwrap()) << 8
39
                 u64::from_le_bytes(data[1..9].try_into().unwrap()) << 8
39
             })),
40
             })),
40
-            1 => Some(Self::Report(Report::decode(count, &data[1..])?)),
41
-            2 => Some(Self::GetValues(GetValues::decode(count, &data[1..])?)),
42
-            3 => Some(Self::Values(Values::decode(count, &data[1..])?)),
41
+            2 => Some(Self::Report(Report::decode(count, &data[1..])?)),
42
+            3 => Some(Self::GetValues(GetValues::decode(count, &data[1..])?)),
43
+            4 => Some(Self::Values(Values::decode(count, &data[1..])?)),
43
             _ => None,
44
             _ => None,
44
         }
45
         }
45
     }
46
     }
46
 }
47
 }
47
 
48
 
48
-#[derive(Clone)]
49
+#[derive(Debug, Clone)]
49
 pub struct Report {
50
 pub struct Report {
50
-    count: u8,
51
-    values: [Value; 8],
51
+    pub count: u8,
52
+    pub values: [Value; 8],
52
 }
53
 }
53
 
54
 
54
 impl Report {
55
 impl Report {
60
         for i in 0..count {
61
         for i in 0..count {
61
             report.values[i as usize] = Value::decode(&mut data)?;
62
             report.values[i as usize] = Value::decode(&mut data)?;
62
         }
63
         }
63
-        None
64
+        Some(report)
64
     }
65
     }
65
 }
66
 }
66
 
67
 
67
-#[derive(Clone)]
68
+#[derive(Debug, Clone)]
68
 pub struct GetValues {
69
 pub struct GetValues {
69
-    count: u8,
70
-    location: Location,
71
-    types_: [ValueType; 8],
70
+    pub count: u8,
71
+    pub location: Location,
72
+    pub types_: [ValueType; 8],
72
 }
73
 }
73
 
74
 
74
 impl GetValues {
75
 impl GetValues {
78
     }
79
     }
79
 }
80
 }
80
 
81
 
81
-#[derive(Clone)]
82
+#[derive(Debug, Clone)]
82
 pub struct Values {
83
 pub struct Values {
83
-    count: u8,
84
-    location: Location,
85
-    values: [Value; 8],
84
+    pub count: u8,
85
+    pub location: Location,
86
+    pub values: [Value; 8],
86
 }
87
 }
87
 
88
 
88
 impl Values {
89
 impl Values {
92
     }
93
     }
93
 }
94
 }
94
 
95
 
95
-#[derive(Clone, Copy)]
96
+#[derive(Debug, Clone, Copy)]
96
 pub enum Location {
97
 pub enum Location {
97
     Livingroom,
98
     Livingroom,
98
     Bathroom,
99
     Bathroom,
101
     Balcony,
102
     Balcony,
102
 }
103
 }
103
 
104
 
104
-#[derive(Clone, Copy)]
105
+#[derive(Debug, Clone, Copy)]
105
 pub enum Value {
106
 pub enum Value {
106
     Invalid,
107
     Invalid,
107
     Time(u64),
108
     Time(u64),
135
     }
136
     }
136
 }
137
 }
137
 
138
 
138
-#[derive(Clone, Copy)]
139
+#[derive(Debug, Clone, Copy)]
139
 pub enum ValueType {
140
 pub enum ValueType {
140
     Time,
141
     Time,
141
     Temperature,
142
     Temperature,

Loading…
取消
儲存