|
|
@@ -75,11 +75,11 @@ impl Packet {
|
|
75
|
75
|
report.encode(&mut data[1..])
|
|
76
|
76
|
}
|
|
77
|
77
|
Self::GetValues(get_values) => {
|
|
78
|
|
- data[0] = (get_values.count << 5) | 2;
|
|
|
78
|
+ data[0] = (get_values.count << 5) | 3;
|
|
79
|
79
|
get_values.encode(&mut data[1..])
|
|
80
|
80
|
}
|
|
81
|
81
|
Self::Values(values) => {
|
|
82
|
|
- data[0] = (values.count << 5) | 2;
|
|
|
82
|
+ data[0] = (values.count << 5) | 4;
|
|
83
|
83
|
values.encode(&mut data[1..])
|
|
84
|
84
|
}
|
|
85
|
85
|
}
|
|
|
@@ -142,18 +142,29 @@ impl Report {
|
|
142
|
142
|
pub struct GetValues {
|
|
143
|
143
|
pub count: u8,
|
|
144
|
144
|
pub location: Location,
|
|
145
|
|
- pub types_: [ValueType; 8],
|
|
|
145
|
+ pub types: [ValueType; 8],
|
|
146
|
146
|
}
|
|
147
|
147
|
|
|
148
|
148
|
impl GetValues {
|
|
149
|
|
- fn decode(_count: u8, _data: &[u8]) -> Result<GetValues, Error> {
|
|
150
|
|
- // TODO
|
|
151
|
|
- Err(Error::InvalidPacketType)
|
|
|
149
|
+ fn decode(count: u8, data: &[u8]) -> Result<GetValues, Error> {
|
|
|
150
|
+ let location = Location::decode(data[0])?;
|
|
|
151
|
+ let mut get_values = GetValues {
|
|
|
152
|
+ count,
|
|
|
153
|
+ location,
|
|
|
154
|
+ types: [ValueType::Time; 8],
|
|
|
155
|
+ };
|
|
|
156
|
+ for i in 0..count as usize {
|
|
|
157
|
+ get_values.types[i] = ValueType::decode(data[i + 1])?;
|
|
|
158
|
+ }
|
|
|
159
|
+ Ok(get_values)
|
|
152
|
160
|
}
|
|
153
|
161
|
|
|
154
|
|
- fn encode(&self, mut _data: &[u8]) -> bool {
|
|
155
|
|
- // TODO
|
|
156
|
|
- false
|
|
|
162
|
+ fn encode(&self, data: &mut [u8]) -> bool {
|
|
|
163
|
+ data[0] = self.location.encode();
|
|
|
164
|
+ for i in 0..self.count as usize {
|
|
|
165
|
+ data[i + 1] = self.types[i].encode();
|
|
|
166
|
+ }
|
|
|
167
|
+ true
|
|
157
|
168
|
}
|
|
158
|
169
|
}
|
|
159
|
170
|
|
|
|
@@ -165,14 +176,33 @@ pub struct Values {
|
|
165
|
176
|
}
|
|
166
|
177
|
|
|
167
|
178
|
impl Values {
|
|
168
|
|
- fn decode(_count: u8, _data: &[u8]) -> Result<Values, Error> {
|
|
169
|
|
- // TODO
|
|
170
|
|
- Err(Error::InvalidPacketType)
|
|
|
179
|
+ fn decode(count: u8, mut data: &[u8]) -> Result<Values, Error> {
|
|
|
180
|
+ let mut report = Self {
|
|
|
181
|
+ count,
|
|
|
182
|
+ location: Location::decode(data[0])?,
|
|
|
183
|
+ values: [Value::Invalid; 8],
|
|
|
184
|
+ };
|
|
|
185
|
+ data = &data[1..];
|
|
|
186
|
+ for i in 0..count {
|
|
|
187
|
+ report.values[i as usize] = Value::decode(&mut data)?;
|
|
|
188
|
+ }
|
|
|
189
|
+ Ok(report)
|
|
171
|
190
|
}
|
|
172
|
191
|
|
|
173
|
|
- fn encode(&self, mut _data: &[u8]) -> bool {
|
|
174
|
|
- // TODO
|
|
175
|
|
- false
|
|
|
192
|
+ fn encode(&self, mut data: &mut [u8]) -> bool {
|
|
|
193
|
+ data[0] = self.location.encode();
|
|
|
194
|
+ data = &mut data[1..];
|
|
|
195
|
+ for i in 0..self.count {
|
|
|
196
|
+ let value_len = self.values[i as usize].encoded_length();
|
|
|
197
|
+ if data.len() < value_len {
|
|
|
198
|
+ return false;
|
|
|
199
|
+ }
|
|
|
200
|
+ if !self.values[i as usize].encode(data) {
|
|
|
201
|
+ return false;
|
|
|
202
|
+ }
|
|
|
203
|
+ data = &mut data[value_len..];
|
|
|
204
|
+ }
|
|
|
205
|
+ true
|
|
176
|
206
|
}
|
|
177
|
207
|
}
|
|
178
|
208
|
|
|
|
@@ -185,6 +215,29 @@ pub enum Location {
|
|
185
|
215
|
Balcony,
|
|
186
|
216
|
}
|
|
187
|
217
|
|
|
|
218
|
+impl Location {
|
|
|
219
|
+ fn encode(self) -> u8 {
|
|
|
220
|
+ match self {
|
|
|
221
|
+ Self::Livingroom => 0,
|
|
|
222
|
+ Self::Bathroom => 1,
|
|
|
223
|
+ Self::Bedroom => 2,
|
|
|
224
|
+ Self::Kitchen => 3,
|
|
|
225
|
+ Self::Balcony => 4,
|
|
|
226
|
+ }
|
|
|
227
|
+ }
|
|
|
228
|
+
|
|
|
229
|
+ fn decode(value: u8) -> Result<Location, Error> {
|
|
|
230
|
+ match value {
|
|
|
231
|
+ 0 => Ok(Self::Livingroom),
|
|
|
232
|
+ 1 => Ok(Self::Bathroom),
|
|
|
233
|
+ 2 => Ok(Self::Bedroom),
|
|
|
234
|
+ 3 => Ok(Self::Kitchen),
|
|
|
235
|
+ 4 => Ok(Self::Balcony),
|
|
|
236
|
+ _ => Err(Error::InvalidLocation),
|
|
|
237
|
+ }
|
|
|
238
|
+ }
|
|
|
239
|
+}
|
|
|
240
|
+
|
|
188
|
241
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
189
|
242
|
pub enum Value {
|
|
190
|
243
|
Invalid,
|
|
|
@@ -279,6 +332,27 @@ pub enum ValueType {
|
|
279
|
332
|
Humidity,
|
|
280
|
333
|
}
|
|
281
|
334
|
|
|
|
335
|
+impl ValueType {
|
|
|
336
|
+ fn encode(self) -> u8 {
|
|
|
337
|
+ match self {
|
|
|
338
|
+ Self::Time => 0,
|
|
|
339
|
+ Self::Temperature => 1,
|
|
|
340
|
+ Self::Pressure => 2,
|
|
|
341
|
+ Self::Humidity => 3,
|
|
|
342
|
+ }
|
|
|
343
|
+ }
|
|
|
344
|
+
|
|
|
345
|
+ fn decode(value: u8) -> Result<ValueType, Error> {
|
|
|
346
|
+ match value {
|
|
|
347
|
+ 0 => Ok(Self::Time),
|
|
|
348
|
+ 1 => Ok(Self::Temperature),
|
|
|
349
|
+ 2 => Ok(Self::Pressure),
|
|
|
350
|
+ 3 => Ok(Self::Humidity),
|
|
|
351
|
+ _ => Err(Error::InvalidValueType),
|
|
|
352
|
+ }
|
|
|
353
|
+ }
|
|
|
354
|
+}
|
|
|
355
|
+
|
|
282
|
356
|
fn encrypt_cbc(key: &[u8], data: &mut [u8]) {
|
|
283
|
357
|
for i in 0..data.len() / 8 - 1 {
|
|
284
|
358
|
let (prev, block) = data[i * 8..i * 8 + 16].split_at_mut(8);
|