Sfoglia il codice sorgente

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

Mathias Gottschlag 5 anni fa
parent
commit
0ef7cc7216
1 ha cambiato i file con 24 aggiunte e 23 eliminazioni
  1. 24
    23
      common/rust-protocol/src/lib.rs

+ 24
- 23
common/rust-protocol/src/lib.rs Vedi File

@@ -7,8 +7,9 @@ use core::convert::TryInto;
7 7
 use crc16::{State, KERMIT};
8 8
 use xxtea_nostd::decrypt;
9 9
 
10
-#[derive(Clone)]
10
+#[derive(Debug, Clone)]
11 11
 pub enum Packet {
12
+    GetSalt,
12 13
     Salt(u64),
13 14
     Report(Report),
14 15
     GetValues(GetValues),
@@ -19,9 +20,8 @@ impl Packet {
19 20
     pub fn decrypt_and_parse(key: &[u8], data: &mut [u8]) -> Option<Packet> {
20 21
         decrypt_cbc(key, data);
21 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 25
             return None;
26 26
         }
27 27
         Self::decode(&checksummed[..22])
@@ -33,22 +33,23 @@ impl Packet {
33 33
         // below.
34 34
         let count = data[0] >> 5;
35 35
         match type_ {
36
-            0 => Some(Self::Salt({
36
+            0 => Some(Self::GetSalt),
37
+            1 => Some(Self::Salt({
37 38
                 // The lowest 8 bit of the salt are the device ID and are filled in by the caller.
38 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 44
             _ => None,
44 45
         }
45 46
     }
46 47
 }
47 48
 
48
-#[derive(Clone)]
49
+#[derive(Debug, Clone)]
49 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 55
 impl Report {
@@ -60,15 +61,15 @@ impl Report {
60 61
         for i in 0..count {
61 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 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 75
 impl GetValues {
@@ -78,11 +79,11 @@ impl GetValues {
78 79
     }
79 80
 }
80 81
 
81
-#[derive(Clone)]
82
+#[derive(Debug, Clone)]
82 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 89
 impl Values {
@@ -92,7 +93,7 @@ impl Values {
92 93
     }
93 94
 }
94 95
 
95
-#[derive(Clone, Copy)]
96
+#[derive(Debug, Clone, Copy)]
96 97
 pub enum Location {
97 98
     Livingroom,
98 99
     Bathroom,
@@ -101,7 +102,7 @@ pub enum Location {
101 102
     Balcony,
102 103
 }
103 104
 
104
-#[derive(Clone, Copy)]
105
+#[derive(Debug, Clone, Copy)]
105 106
 pub enum Value {
106 107
     Invalid,
107 108
     Time(u64),
@@ -135,7 +136,7 @@ impl Value {
135 136
     }
136 137
 }
137 138
 
138
-#[derive(Clone, Copy)]
139
+#[derive(Debug, Clone, Copy)]
139 140
 pub enum ValueType {
140 141
     Time,
141 142
     Temperature,

Loading…
Annulla
Salva