2 İşlemeler

Yazar SHA1 Mesaj Tarih
  Bernd Gottschlag 8f2658655e Merge branch 'master' of git.gottschlag.net:bernd/smart-home 5 yıl önce
  Bernd Gottschlag 207868cdda Add simple decryption of the data packets 5 yıl önce
1 değiştirilmiş dosya ile 25 ekleme ve 27 silme
  1. 25
    27
      base-station/software/src/radio.rs

+ 25
- 27
base-station/software/src/radio.rs Dosyayı Görüntüle

93
     loop {
93
     loop {
94
         sleep(Duration::from_millis(1));
94
         sleep(Duration::from_millis(1));
95
         if let Some(pipe) = nrf24.can_read().unwrap() {
95
         if let Some(pipe) = nrf24.can_read().unwrap() {
96
-            let payload = nrf24.read().unwrap();
96
+            let mut payload = nrf24.read().unwrap();
97
             info!(
97
             info!(
98
                 "packet received on pipe {}: {:x?}, {}",
98
                 "packet received on pipe {}: {:x?}, {}",
99
                 pipe,
99
                 pipe,
104
                 continue;
104
                 continue;
105
             }
105
             }
106
 
106
 
107
-            let sensor_id = payload.as_ref()[0];
108
-            let remainder_of_salt = payload.as_ref()[1..7].to_vec();
107
+            let mut payload = payload.as_ref().to_vec();
108
+
109
+            let sensor_id = payload[0];
110
+            let salt = payload[0..8].to_vec();
111
+
112
+            // Decrypt the package
113
+            let key = [
114
+                0x9e, 0x37, 0x79, 0xb9, 0x9b, 0x97, 0x73, 0xe9, 0xb9, 0x79, 0x37, 0x9e, 0x6b, 0x69,
115
+                0x51, 0x56,
116
+            ]; //TODO: define somewhere else
117
+            decrypt(&key, &mut payload.as_mut_slice()[8..32]);
118
+
119
+            info!("decrypted: {:x?}", payload);
120
+            let mut current_c
121
+            while (
122
+            {
123
+                
124
+            }
109
 
125
 
110
             // Calculate the CRC
126
             // Calculate the CRC
111
-            let crc_calculation_buffer = payload.as_ref()[8..30].to_vec();
127
+            let crc_calculation_buffer = payload[8..30].to_vec();
112
             let calculated_crc = State::<KERMIT>::calculate(&crc_calculation_buffer);
128
             let calculated_crc = State::<KERMIT>::calculate(&crc_calculation_buffer);
113
             info!("calculated crc: 0x{:x}", calculated_crc);
129
             info!("calculated crc: 0x{:x}", calculated_crc);
114
-            let crc = u16::from_le_bytes(payload.as_ref()[30..32].try_into().unwrap());
130
+            let crc = u16::from_le_bytes(payload[30..32].try_into().unwrap());
115
 
131
 
116
             if crc != calculated_crc {
132
             if crc != calculated_crc {
117
                 info!("malformed packet received: crc mismatch!");
133
                 info!("malformed packet received: crc mismatch!");
122
                 continue;
138
                 continue;
123
             }
139
             }
124
 
140
 
125
-            let packet_identifier = payload.as_ref()[8];
126
-            let packet_payload = &payload.as_ref()[9..29];
141
+            let packet_identifier = payload[8];
142
+            let packet_payload = &payload[9..30];
127
             info!("sensor id: 0x{:x}", sensor_id);
143
             info!("sensor id: 0x{:x}", sensor_id);
128
             info!("crc: 0x{:x}", crc);
144
             info!("crc: 0x{:x}", crc);
129
 
145
 
130
             let packet_type = packet_identifier & 0x1F;
146
             let packet_type = packet_identifier & 0x1F;
131
             let element_count = (packet_identifier & 0xE0) >> 5;
147
             let element_count = (packet_identifier & 0xE0) >> 5;
132
-            info!("packet_type: 0x{:x}", packet_type);
133
-            info!("element_count: 0x{:x}", element_count);
134
 
148
 
135
             let mut temperature = 0;
149
             let mut temperature = 0;
136
             let mut pressure = 0;
150
             let mut pressure = 0;
140
                 let mut i = 0;
154
                 let mut i = 0;
141
                 let mut buffer_position = 0;
155
                 let mut buffer_position = 0;
142
                 info!("report packet received");
156
                 info!("report packet received");
143
-                info!("packet_payload: {:x?}", packet_payload.as_ref());
144
                 while i < element_count {
157
                 while i < element_count {
145
                     let value_type = packet_payload[buffer_position];
158
                     let value_type = packet_payload[buffer_position];
146
 
159
 
155
                                 .try_into()
168
                                 .try_into()
156
                                 .unwrap(),
169
                                 .unwrap(),
157
                         );
170
                         );
158
-                        info!(
159
-                            "temp: {:x?}, {:x}",
160
-                            packet_payload[buffer_position..buffer_position + 2].to_vec(),
161
-                            temperature
162
-                        );
163
                         buffer_position += 2;
171
                         buffer_position += 2;
164
                     } else if value_type == 2 {
172
                     } else if value_type == 2 {
165
                         pressure = u32::from_le_bytes(
173
                         pressure = u32::from_le_bytes(
167
                                 .try_into()
175
                                 .try_into()
168
                                 .unwrap(),
176
                                 .unwrap(),
169
                         );
177
                         );
170
-                        info!(
171
-                            "pressure: {:x?}, {:x}",
172
-                            packet_payload[buffer_position..buffer_position + 4].to_vec(),
173
-                            pressure
174
-                        );
175
                         buffer_position += 4;
178
                         buffer_position += 4;
176
                     } else if value_type == 3 {
179
                     } else if value_type == 3 {
177
                         humidity = u16::from_le_bytes(
180
                         humidity = u16::from_le_bytes(
179
                                 .try_into()
182
                                 .try_into()
180
                                 .unwrap(),
183
                                 .unwrap(),
181
                         );
184
                         );
182
-                        info!(
183
-                            "humidity: {:x?}, {:x}",
184
-                            packet_payload[buffer_position..buffer_position + 2].to_vec(),
185
-                            humidity
186
-                        );
187
                         buffer_position += 2;
185
                         buffer_position += 2;
188
                     } else {
186
                     } else {
189
                         info!("unknown value type");
187
                         info!("unknown value type");
207
                 continue;
205
                 continue;
208
             }
206
             }
209
 
207
 
210
-            if humidity > 1000 {
208
+            if humidity > 10000 {
211
                 info!("humidity outside of range: {}", humidity);
209
                 info!("humidity outside of range: {}", humidity);
212
                 continue;
210
                 continue;
213
             }
211
             }
214
 
212
 
215
             let pressure = pressure as f32 / 100.0;
213
             let pressure = pressure as f32 / 100.0;
216
             let temperature = temperature as f32 / 10.0;
214
             let temperature = temperature as f32 / 10.0;
217
-            let humidity = humidity as f32 / 102.4;
215
+            let humidity = humidity as f32 / 100.0;
218
             info!("pressure: {} HPa", pressure);
216
             info!("pressure: {} HPa", pressure);
219
             info!("temperature: {} °C", temperature);
217
             info!("temperature: {} °C", temperature);
220
             info!("humidity: {}%", humidity);
218
             info!("humidity: {}%", humidity);

Loading…
İptal
Kaydet