瀏覽代碼

Put send functions into a separate file

Bernd Gottschlag 5 年之前
父節點
當前提交
9cc2488996
共有 4 個檔案被更改,包括 114 行新增111 行删除
  1. 5
    108
      weather-sensor/firmware/main.c
  2. 3
    3
      weather-sensor/firmware/makefile
  3. 93
    0
      weather-sensor/firmware/radio.c
  4. 13
    0
      weather-sensor/firmware/radio.h

+ 5
- 108
weather-sensor/firmware/main.c 查看文件

@@ -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
-}

+ 3
- 3
weather-sensor/firmware/makefile 查看文件

@@ -1,5 +1,5 @@
1 1
 CFLAGS = -DF_CPU=1000000UL -D BME280_32BIT_ENABLE -I BME280_driver -mmcu=atmega88p -Os -flto -Wall -Wextra -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
2
-DEPS = spi.h nrf24l01.h nrf24l01_definitions.h bme280_interface.h BME280_driver/bme280.h led.h pin_programming.h crc.h encryption.h
2
+DEPS = spi.h nrf24l01.h nrf24l01_definitions.h bme280_interface.h BME280_driver/bme280.h led.h pin_programming.h crc.h encryption.h radio.h
3 3
 
4 4
 all: main.hex
5 5
 
@@ -9,7 +9,7 @@ clean:
9 9
 	rm -f obj/*.o
10 10
 
11 11
 flash: main.hex
12
-	sudo avrdude -c buspirate -b 115200 -P /dev/ttyUSB1 -p m88p -v -U flash:w:main.hex
12
+	sudo avrdude -c buspirate -b 115200 -P /dev/ttyUSB0 -p m88p -v -U flash:w:main.hex
13 13
 
14 14
 obj/%.o: %.c $(DEPS)
15 15
 	avr-gcc -c $< -o $@ $(CFLAGS)
@@ -17,7 +17,7 @@ obj/%.o: %.c $(DEPS)
17 17
 obj/%.o: BME280_driver/%.c $(DEPS)
18 18
 	avr-gcc -c $< -o $@ $(CFLAGS)
19 19
 
20
-main: obj/main.o obj/spi.o obj/nrf24l01.o obj/bme280_interface.o obj/bme280.o obj/pin_programming.o obj/crc.o obj/encryption.o
20
+main: obj/main.o obj/spi.o obj/nrf24l01.o obj/bme280_interface.o obj/bme280.o obj/pin_programming.o obj/crc.o obj/encryption.o obj/radio.o
21 21
 	avr-gcc $^ -o $@ $(CFLAGS)
22 22
 
23 23
 main.hex: main

+ 93
- 0
weather-sensor/firmware/radio.c 查看文件

@@ -0,0 +1,93 @@
1
+#include <string.h>
2
+#include "radio.h"
3
+#include "encryption.h"
4
+#include "crc.h"
5
+
6
+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 */
7
+
8
+bool Send_Message(PACKET * packet, uint64_t * salt);
9
+
10
+bool Send_Get_Salt_Message(PACKET * packet, uint64_t * salt) //TODO: put into own file
11
+{
12
+	memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
13
+
14
+	packet->payload.reportData.packetIdentifier.elementCount = 0;
15
+	packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_GET_SALT;
16
+
17
+	return Send_Message(packet, salt);
18
+}
19
+
20
+bool Send_Report_Message(PACKET * packet, uint64_t * salt, struct bme280_data * sensorData)
21
+{
22
+	packet->payload.reportData.packetIdentifier.elementCount = 3;
23
+	packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_REPORT;
24
+
25
+	/* Fill in the payload */
26
+	packet->payload.reportData.valueTypeTemperature = VALUE_TYPE_TEMPERATURE;
27
+	packet->payload.reportData.temperature = sensorData->temperature/10;
28
+	packet->payload.reportData.valueTypePressure = VALUE_TYPE_PRESSURE;
29
+	packet->payload.reportData.pressure = sensorData->pressure;
30
+	packet->payload.reportData.valueTypeHumidity = VALUE_TYPE_HUMIDITY;
31
+	packet->payload.reportData.humidity = sensorData->humidity * 100/1024;
32
+
33
+	return Send_Message(packet, salt);
34
+}
35
+
36
+bool Send_Message(PACKET * packet, uint64_t * salt)
37
+{
38
+	bool success;
39
+
40
+	uint16_t crc;
41
+	(*salt) &= ~(1ull<<63); /* Set the most significant bit to 0 to indicate a packet from the device to the base station */
42
+
43
+	packet->salt = *salt;
44
+
45
+	/* Calculate the CRC */
46
+	crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
47
+	packet->crc = crc;
48
+
49
+	Encrypt((uint32_t*) packet->payload.buffer,
50
+	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
51
+	        *salt,
52
+	        (uint32_t*) encryptionKey);
53
+
54
+	success = NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
55
+
56
+	/* Increment salt */
57
+	(*salt) += (1 << 8);
58
+
59
+	return success;
60
+}
61
+
62
+bool Read_Salt_Message(PACKET * packet, uint64_t * salt)
63
+{
64
+	uint16_t crcRemainder = 0xFFFF;
65
+	uint64_t baseStationSalt = 0x0;
66
+
67
+
68
+	/* TODO: check that the packet originated from the base station by checking the id */
69
+
70
+	baseStationSalt = packet->salt;
71
+	Decrypt((uint32_t*)packet->payload.buffer,
72
+	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc),
73
+	        baseStationSalt,
74
+	        (uint32_t*) encryptionKey);
75
+
76
+	crcRemainder = Calculate_Crc(packet->payload.buffer,
77
+	                             PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc));
78
+	if (crcRemainder != 0)
79
+	{
80
+		return false;
81
+	}
82
+
83
+	if ((packet->payload.saltData.packetIdentifier.packetType != PACKET_TYPE_SALT) ||
84
+	    (packet->payload.saltData.packetIdentifier.elementCount != 0))
85
+	{
86
+		return false;
87
+	}
88
+
89
+	memcpy((uint8_t*)salt, packet->payload.saltData.salt, 7);
90
+	*salt = (*salt) << 8;
91
+
92
+	return true;
93
+}

+ 13
- 0
weather-sensor/firmware/radio.h 查看文件

@@ -0,0 +1,13 @@
1
+#ifndef RADIO_H
2
+#define RADIO_H
3
+
4
+#include <stdint.h>
5
+#include <stdbool.h>
6
+#include "bme280_defs.h"
7
+#include "nrf24l01.h"
8
+
9
+bool Send_Get_Salt_Message(PACKET * packet, uint64_t * salt); //TODO: put into own file
10
+bool Read_Salt_Message(PACKET * packet, uint64_t * salt);
11
+bool Send_Report_Message(PACKET * packet, uint64_t * salt, struct bme280_data * sensorData);
12
+
13
+#endif

Loading…
取消
儲存