Przeglądaj źródła

Add simple decryption of the data packets

The cipher block chaining is not yet implemented
Bernd Gottschlag 5 lat temu
rodzic
commit
207868cdda
1 zmienionych plików z 25 dodań i 27 usunięć
  1. 25
    27
      base-station/software/src/radio.rs

+ 25
- 27
base-station/software/src/radio.rs Wyświetl plik

@@ -93,7 +93,7 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
93 93
     loop {
94 94
         sleep(Duration::from_millis(1));
95 95
         if let Some(pipe) = nrf24.can_read().unwrap() {
96
-            let payload = nrf24.read().unwrap();
96
+            let mut payload = nrf24.read().unwrap();
97 97
             info!(
98 98
                 "packet received on pipe {}: {:x?}, {}",
99 99
                 pipe,
@@ -104,14 +104,30 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
104 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 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 128
             let calculated_crc = State::<KERMIT>::calculate(&crc_calculation_buffer);
113 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 132
             if crc != calculated_crc {
117 133
                 info!("malformed packet received: crc mismatch!");
@@ -122,15 +138,13 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
122 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 143
             info!("sensor id: 0x{:x}", sensor_id);
128 144
             info!("crc: 0x{:x}", crc);
129 145
 
130 146
             let packet_type = packet_identifier & 0x1F;
131 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 149
             let mut temperature = 0;
136 150
             let mut pressure = 0;
@@ -140,7 +154,6 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
140 154
                 let mut i = 0;
141 155
                 let mut buffer_position = 0;
142 156
                 info!("report packet received");
143
-                info!("packet_payload: {:x?}", packet_payload.as_ref());
144 157
                 while i < element_count {
145 158
                     let value_type = packet_payload[buffer_position];
146 159
 
@@ -155,11 +168,6 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
155 168
                                 .try_into()
156 169
                                 .unwrap(),
157 170
                         );
158
-                        info!(
159
-                            "temp: {:x?}, {:x}",
160
-                            packet_payload[buffer_position..buffer_position + 2].to_vec(),
161
-                            temperature
162
-                        );
163 171
                         buffer_position += 2;
164 172
                     } else if value_type == 2 {
165 173
                         pressure = u32::from_le_bytes(
@@ -167,11 +175,6 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
167 175
                                 .try_into()
168 176
                                 .unwrap(),
169 177
                         );
170
-                        info!(
171
-                            "pressure: {:x?}, {:x}",
172
-                            packet_payload[buffer_position..buffer_position + 4].to_vec(),
173
-                            pressure
174
-                        );
175 178
                         buffer_position += 4;
176 179
                     } else if value_type == 3 {
177 180
                         humidity = u16::from_le_bytes(
@@ -179,11 +182,6 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
179 182
                                 .try_into()
180 183
                                 .unwrap(),
181 184
                         );
182
-                        info!(
183
-                            "humidity: {:x?}, {:x}",
184
-                            packet_payload[buffer_position..buffer_position + 2].to_vec(),
185
-                            humidity
186
-                        );
187 185
                         buffer_position += 2;
188 186
                     } else {
189 187
                         info!("unknown value type");
@@ -207,14 +205,14 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
207 205
                 continue;
208 206
             }
209 207
 
210
-            if humidity > 1000 {
208
+            if humidity > 10000 {
211 209
                 info!("humidity outside of range: {}", humidity);
212 210
                 continue;
213 211
             }
214 212
 
215 213
             let pressure = pressure as f32 / 100.0;
216 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 216
             info!("pressure: {} HPa", pressure);
219 217
             info!("temperature: {} °C", temperature);
220 218
             info!("humidity: {}%", humidity);

Ładowanie…
Anuluj
Zapisz