|
|
@@ -15,13 +15,11 @@
|
|
15
|
15
|
#include "bme280_interface.h"
|
|
16
|
16
|
#include "bme280_defs.h"
|
|
17
|
17
|
#include "pin_programming.h"
|
|
18
|
|
-#include "crc.h"
|
|
19
|
|
-#include "encryption.h"
|
|
|
18
|
+#include "radio.h"
|
|
20
|
19
|
|
|
21
|
20
|
|
|
22
|
21
|
|
|
23
|
22
|
uint8_t ownId;
|
|
24
|
|
-const uint8_t encryptionKey[16] = {0x9e, 0x37, 0x79, 0xb9, 0x9b, 0x97, 0x73, 0xe9, 0xb9, 0x79, 0x37, 0x9e, 0x6b, 0x69, 0x51, 0x56}; /* TODO: use exernal file with the keys */
|
|
25
|
23
|
uint64_t salt;
|
|
26
|
24
|
|
|
27
|
25
|
char bool_case = 0;
|
|
|
@@ -44,8 +42,6 @@ void Enter_Power_Save_Mode(void);
|
|
44
|
42
|
void Exit_Power_Save_Mode(void);
|
|
45
|
43
|
|
|
46
|
44
|
|
|
47
|
|
-bool Send_Get_Salt_Message(PACKET * packet, uint64_t salt); //TODO: put into own file
|
|
48
|
|
-bool Read_Salt_Message(PACKET * packet, uint64_t * salt);
|
|
49
|
45
|
|
|
50
|
46
|
void Set_Up_Power_Save_Mode(void);
|
|
51
|
47
|
void Enter_Power_Save_Mode(void);
|
|
|
@@ -64,7 +60,6 @@ ISR(PCINT2_vect)
|
|
64
|
60
|
int main (void)
|
|
65
|
61
|
{
|
|
66
|
62
|
struct bme280_data sensorData;
|
|
67
|
|
- uint16_t crc;
|
|
68
|
63
|
bool saltReceived = false;
|
|
69
|
64
|
|
|
70
|
65
|
/* Enable the debug LED */
|
|
|
@@ -93,17 +88,11 @@ int main (void)
|
|
93
|
88
|
salt |= ownId;
|
|
94
|
89
|
|
|
95
|
90
|
|
|
96
|
|
- /* TODO: Request a salt from the base station:
|
|
97
|
|
- * - loop as long as no reply from the base station was received:
|
|
98
|
|
- * - send the packet that requests the salt
|
|
99
|
|
- * - if no ack is received for the packet go direclty to sleep for 100 ms
|
|
100
|
|
- * - if no salt is received within 100 ms send the message again
|
|
101
|
|
- * - keep the LED on for the duration to allow the user to see that the device is ready for operation.
|
|
102
|
|
- */
|
|
|
91
|
+ /* Request a salt from the base station */
|
|
103
|
92
|
LED_PORT |= (1 << LED_PIN);
|
|
104
|
93
|
do
|
|
105
|
94
|
{
|
|
106
|
|
- if (Send_Get_Salt_Message(packet, salt) == true)
|
|
|
95
|
+ if (Send_Get_Salt_Message(packet, &salt) == true)
|
|
107
|
96
|
{
|
|
108
|
97
|
memset(messageBuffer, 0x0F, 32);
|
|
109
|
98
|
if (NRF24L01_Receive_Message(messageBuffer, 100 /*ms*/) == true)
|
|
|
@@ -144,40 +133,8 @@ int main (void)
|
|
144
|
133
|
/* Get measurement and send it */
|
|
145
|
134
|
BME280_Get_Measurement(&sensorData);
|
|
146
|
135
|
|
|
147
|
|
-
|
|
148
|
|
- /* TODO: put the following into a Send_Report function */
|
|
149
|
|
- memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
|
|
150
|
|
-
|
|
151
|
|
- salt &= ~(1ull<<63);
|
|
152
|
|
- packet->salt = salt;
|
|
153
|
|
- packet->payload.reportData.packetIdentifier.elementCount = 3;
|
|
154
|
|
- packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_REPORT;
|
|
155
|
|
-
|
|
156
|
|
- /* Fill in the payload */
|
|
157
|
|
- packet->payload.reportData.valueTypeTemperature = VALUE_TYPE_TEMPERATURE;
|
|
158
|
|
- packet->payload.reportData.temperature = sensorData.temperature/10;
|
|
159
|
|
- packet->payload.reportData.valueTypePressure = VALUE_TYPE_PRESSURE;
|
|
160
|
|
- packet->payload.reportData.pressure = sensorData.pressure;
|
|
161
|
|
- packet->payload.reportData.valueTypeHumidity = VALUE_TYPE_HUMIDITY;
|
|
162
|
|
- packet->payload.reportData.humidity = sensorData.humidity * 100/1024;
|
|
163
|
|
-
|
|
164
|
|
- /* Calculate the CRC */
|
|
165
|
|
- crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
|
|
166
|
|
- packet->crc = crc;
|
|
167
|
|
-
|
|
168
|
|
- /* Encrypt the packet */
|
|
169
|
|
- /* TODO:
|
|
170
|
|
- * - increment the salt for every packet
|
|
171
|
|
- * - Receive salt from the base station
|
|
172
|
|
- */
|
|
173
|
|
- Encrypt((uint32_t*) packet->payload.buffer,
|
|
174
|
|
- PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
|
|
175
|
|
- salt, (uint32_t*) encryptionKey);
|
|
176
|
|
-
|
|
177
|
|
- NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
|
|
178
|
|
-
|
|
179
|
|
- /* Increment salt */
|
|
180
|
|
- salt += (1 << 8);
|
|
|
136
|
+ /* Send the report */
|
|
|
137
|
+ Send_Report_Message(packet, &salt, &sensorData);
|
|
181
|
138
|
|
|
182
|
139
|
LED_PORT |= (1 << LED_PIN);
|
|
183
|
140
|
_delay_ms(100); /* TODO: only for debugging, remove this later! */
|
|
|
@@ -256,63 +213,3 @@ void Exit_Power_Save_Mode(void)
|
|
256
|
213
|
Initialize_SPI(); // reinitalize SPI
|
|
257
|
214
|
}
|
|
258
|
215
|
|
|
259
|
|
-bool Send_Get_Salt_Message(PACKET * packet, uint64_t salt) //TODO: put into own file
|
|
260
|
|
-{
|
|
261
|
|
- bool success = false;
|
|
262
|
|
- uint16_t crc;
|
|
263
|
|
-
|
|
264
|
|
- memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
|
|
265
|
|
-
|
|
266
|
|
- salt &= ~(1ull<<63); /* Set the most significant bit to 0 to indicate a packet from the device to the base station */
|
|
267
|
|
-
|
|
268
|
|
- packet->salt = salt;
|
|
269
|
|
- packet->payload.reportData.packetIdentifier.elementCount = 0;
|
|
270
|
|
- packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_GET_SALT;
|
|
271
|
|
-
|
|
272
|
|
- /* Calculate the CRC */
|
|
273
|
|
- crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
|
|
274
|
|
- packet->crc = crc;
|
|
275
|
|
-
|
|
276
|
|
- Encrypt((uint32_t*) packet->payload.buffer,
|
|
277
|
|
- PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
|
|
278
|
|
- salt,
|
|
279
|
|
- (uint32_t*) encryptionKey);
|
|
280
|
|
-
|
|
281
|
|
- success = NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
|
|
282
|
|
-
|
|
283
|
|
-
|
|
284
|
|
- return success;
|
|
285
|
|
-}
|
|
286
|
|
-
|
|
287
|
|
-bool Read_Salt_Message(PACKET * packet, uint64_t * salt)
|
|
288
|
|
-{
|
|
289
|
|
- uint16_t crcRemainder = 0xFFFF;
|
|
290
|
|
- uint64_t baseStationSalt = 0x0;
|
|
291
|
|
-
|
|
292
|
|
-
|
|
293
|
|
- /* TODO: check that the packet originated from the base station by checking the id */
|
|
294
|
|
-
|
|
295
|
|
- baseStationSalt = packet->salt;
|
|
296
|
|
- Decrypt((uint32_t*)packet->payload.buffer,
|
|
297
|
|
- PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc),
|
|
298
|
|
- baseStationSalt,
|
|
299
|
|
- (uint32_t*) encryptionKey);
|
|
300
|
|
-
|
|
301
|
|
- crcRemainder = Calculate_Crc(packet->payload.buffer,
|
|
302
|
|
- PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc));
|
|
303
|
|
- if (crcRemainder != 0)
|
|
304
|
|
- {
|
|
305
|
|
- return false;
|
|
306
|
|
- }
|
|
307
|
|
-
|
|
308
|
|
- if ((packet->payload.saltData.packetIdentifier.packetType != PACKET_TYPE_SALT) ||
|
|
309
|
|
- (packet->payload.saltData.packetIdentifier.elementCount != 0))
|
|
310
|
|
- {
|
|
311
|
|
- return false;
|
|
312
|
|
- }
|
|
313
|
|
-
|
|
314
|
|
- memcpy((uint8_t*)salt, packet->payload.saltData.salt, 7);
|
|
315
|
|
- *salt = (*salt) << 8;
|
|
316
|
|
-
|
|
317
|
|
- return true;
|
|
318
|
|
-}
|