aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2015-11-10 22:32:28 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2015-11-10 22:32:28 +0100
commit3303c77e2a46bab857ef57d6171a93eb2833bd7c (patch)
tree0a4eb94b49422ddd7450f5e3768a913a08590183
parent416f67d4aec97954cc16b4e31468b810aeef9a19 (diff)
add CRC in receiver
-rw-r--r--rpi/spark_counter_receiver.cpp60
1 files changed, 53 insertions, 7 deletions
diff --git a/rpi/spark_counter_receiver.cpp b/rpi/spark_counter_receiver.cpp
index ef6fe58..2ff4a6a 100644
--- a/rpi/spark_counter_receiver.cpp
+++ b/rpi/spark_counter_receiver.cpp
@@ -19,7 +19,6 @@
* they are then stored in an InfluxDB time series database using the HTTP API
* to send values to the database the curl library is required
*/
-
#include <unistd.h>
#include <string.h>
#include <stdio.h>
@@ -29,6 +28,9 @@
#include <RF24/RF24.h> // http://tmrh20.github.io/RF24 library to communicate to the nRF24L01+
#include <curl/curl.h> // curl library to send the measurement data to the influxDB.
#include "aes.h" // AES library (from tiny-AES128-C)
+#include "crc16.h" // CRC Computations (from AVR libc)
+
+//#define DEBUG 1
// Setup for RPi B1 GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ);
@@ -77,32 +79,76 @@ int main(int argc, char** argv){
uint8_t size = radio.getDynamicPayloadSize();
radio.read(&payload,size);
-/*
+#ifdef DEBUG
printf("got %d bytes:",size);
for (uint8_t i=0; i<size; i++) {
printf(" %02x",payload[i]);
}
printf("\n");
-*/
+#endif
// got through payload
- if (size!=1+16) {
+ if (size!=1+4*4+1) {
+#ifdef DEBUG
+ printf("wrong size: expected %d, got %d\n",18,size);
+#endif
continue;
}
uint8_t id = payload[0]; // the meter id (0 is myself, used for unknown source)
- float voltage, current, power, energy; // the values coming from the meter
+ if (id!=1) { // there is only one source for power measurement
+#ifdef DEBUG
+ printf("wrong id: expected %d, got %d\n",1,id);
+#endif
+ continue;
+ }
uint8_t values[16]; // the encrypted values block
- AES128_ECB_decrypt(&payload[1], key, values); // decrypt payload
+ AES128_ECB_decrypt(&payload[1], key, values); // decrypt values
+#ifdef DEBUG
+ printf("decrypted values:");
+ for (uint8_t i=0; i<sizeof(values); i++) {
+ printf(" %02x",values[i]);
+ }
+ printf("\n");
+#endif
for (uint8_t i=0; i<sizeof(values) && i<sizeof(iv); i++) { // use CBC mode
values[i] ^= iv[i]; // XOR with last IV
}
+#ifdef DEBUG
+ printf("XORed values:");
+ for (uint8_t i=0; i<sizeof(values); i++) {
+ printf(" %02x",values[i]);
+ }
+ printf("\n");
+#endif
memcpy(iv,&payload[1],sizeof(iv)); // save next IV
+ // check CRC of plain values
+ uint8_t crc = 0;
+ for (uint8_t i = 0; i < sizeof(values); i++) {
+ crc = _crc_ibutton_update(crc, values[i]); // calculate CRC
+ }
+ crc = _crc_ibutton_update(crc, payload[17]); // this is the CRC
+ if (crc) { // wrong key or IV
+#ifdef DEBUG
+ printf("wrong CRC: expected %d, got %d\n",payload[17],crc);
+#endif
+ continue;
+ }
+ uint8_t invalid[sizeof(values)]; // used to indicate that the values are invalid
+ memset(invalid,0xff,sizeof(invalid)); // 0xff... is the invalid value
+ if (memcmp(invalid,values,sizeof(values))==0) { // values are invalid
+#ifdef DEBUG
+ printf("invalid values\n");
+#endif
+ continue;
+ }
+ // read values
+ float voltage, current, power, energy; // the values coming from the meter
memcpy(&voltage,&values[0],4); // read voltage
memcpy(&current,&values[4],4); // read current
memcpy(&power,&values[8],4); // read power
memcpy(&energy,&values[12],4); // read energy
-
printf("meter: %d, voltage: %f V, current: %f A, power: %f W, energy: %f Wh\n",id,voltage,current,power,energy);
+ // submit values to database
if (curl) {
CURLcode res = CURLE_OK; // curl response
char post[128*4] = {0}; // string to submit data to DB using POST request