|
|
@@ -2,12 +2,15 @@
|
|
2
|
2
|
//! base station and all sensor/display nodes.
|
|
3
|
3
|
#![no_std]
|
|
4
|
4
|
|
|
|
5
|
+#[cfg(test)]
|
|
|
6
|
+extern crate std;
|
|
|
7
|
+
|
|
5
|
8
|
use core::convert::TryInto;
|
|
6
|
9
|
|
|
7
|
10
|
use crc16::{State, KERMIT};
|
|
8
|
11
|
use xxtea_nostd::{decrypt, encrypt};
|
|
9
|
12
|
|
|
10
|
|
-#[derive(Debug, Clone)]
|
|
|
13
|
+#[derive(Debug, Clone, PartialEq)]
|
|
11
|
14
|
pub enum Packet {
|
|
12
|
15
|
GetSalt,
|
|
13
|
16
|
Salt(u64),
|
|
|
@@ -83,7 +86,7 @@ impl Packet {
|
|
83
|
86
|
}
|
|
84
|
87
|
}
|
|
85
|
88
|
|
|
86
|
|
-#[derive(Debug, Clone)]
|
|
|
89
|
+#[derive(Debug, Clone, PartialEq)]
|
|
87
|
90
|
pub struct Report {
|
|
88
|
91
|
pub count: u8,
|
|
89
|
92
|
pub values: [Value; 8],
|
|
|
@@ -116,7 +119,7 @@ impl Report {
|
|
116
|
119
|
}
|
|
117
|
120
|
}
|
|
118
|
121
|
|
|
119
|
|
-#[derive(Debug, Clone)]
|
|
|
122
|
+#[derive(Debug, Clone, PartialEq)]
|
|
120
|
123
|
pub struct GetValues {
|
|
121
|
124
|
pub count: u8,
|
|
122
|
125
|
pub location: Location,
|
|
|
@@ -135,7 +138,7 @@ impl GetValues {
|
|
135
|
138
|
}
|
|
136
|
139
|
}
|
|
137
|
140
|
|
|
138
|
|
-#[derive(Debug, Clone)]
|
|
|
141
|
+#[derive(Debug, Clone, PartialEq)]
|
|
139
|
142
|
pub struct Values {
|
|
140
|
143
|
pub count: u8,
|
|
141
|
144
|
pub location: Location,
|
|
|
@@ -154,7 +157,7 @@ impl Values {
|
|
154
|
157
|
}
|
|
155
|
158
|
}
|
|
156
|
159
|
|
|
157
|
|
-#[derive(Debug, Clone, Copy)]
|
|
|
160
|
+#[derive(Debug, Clone, Copy, PartialEq)]
|
|
158
|
161
|
pub enum Location {
|
|
159
|
162
|
Livingroom,
|
|
160
|
163
|
Bathroom,
|
|
|
@@ -163,7 +166,7 @@ pub enum Location {
|
|
163
|
166
|
Balcony,
|
|
164
|
167
|
}
|
|
165
|
168
|
|
|
166
|
|
-#[derive(Debug, Clone, Copy)]
|
|
|
169
|
+#[derive(Debug, Clone, Copy, PartialEq)]
|
|
167
|
170
|
pub enum Value {
|
|
168
|
171
|
Invalid,
|
|
169
|
172
|
Time(u64),
|
|
|
@@ -230,7 +233,7 @@ impl Value {
|
|
230
|
233
|
}
|
|
231
|
234
|
}
|
|
232
|
235
|
|
|
233
|
|
-#[derive(Debug, Clone, Copy)]
|
|
|
236
|
+#[derive(Debug, Clone, Copy, PartialEq)]
|
|
234
|
237
|
pub enum ValueType {
|
|
235
|
238
|
Time,
|
|
236
|
239
|
Temperature,
|
|
|
@@ -272,3 +275,28 @@ pub enum Error {
|
|
272
|
275
|
InvalidLocation,
|
|
273
|
276
|
TooShort,
|
|
274
|
277
|
}
|
|
|
278
|
+
|
|
|
279
|
+#[cfg(test)]
|
|
|
280
|
+mod tests {
|
|
|
281
|
+ use super::*;
|
|
|
282
|
+ use std::println;
|
|
|
283
|
+
|
|
|
284
|
+ #[test]
|
|
|
285
|
+ fn test_identity() {
|
|
|
286
|
+ let packets = [Packet::GetSalt, Packet::Salt(0x12345678abcdef12)];
|
|
|
287
|
+
|
|
|
288
|
+ let key = [
|
|
|
289
|
+ 0x12, 0x34, 0x56, 0x78, 0xab, 0xcd, 0xef, 0x12, 0x12, 0x34, 0x56, 0x78, 0xab, 0xcd,
|
|
|
290
|
+ 0xef, 0x12,
|
|
|
291
|
+ ];
|
|
|
292
|
+
|
|
|
293
|
+ for packet in packets.iter() {
|
|
|
294
|
+ let mut encoded = [0u8; 32];
|
|
|
295
|
+ packet.encode_and_encrypt(&key, 0x1234123412341234, &mut encoded);
|
|
|
296
|
+ assert_eq!(encoded[0], 0x34);
|
|
|
297
|
+ assert_eq!(encoded[7], 0x12);
|
|
|
298
|
+ let decoded = Packet::decrypt_and_decode(&key, &mut encoded).unwrap();
|
|
|
299
|
+ // TODO: Compare encoded and decoded.
|
|
|
300
|
+ }
|
|
|
301
|
+ }
|
|
|
302
|
+}
|