Merge pull request #3 from K-Lab-Students/refactor

refactor
sensor-connect
Artem-Darius Weber 1 year ago committed by GitHub
commit ddc7babecf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,8 +8,22 @@
#define ECC_LENGTH 4 #define ECC_LENGTH 4
#define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH) #define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH)
void TransmissionModule::decrypt(uint8_t *data, size_t data_size, uint8_t *output_data)
bool TransmissionModule::receive(uint8_t &sensor_id, MeasureData &data)
{ {
uint8_t buf[TOTAL_LENGTH];
if (!transmitterModule->receive(buf, TOTAL_LENGTH)) {
return false;
}
uint8_t msg[MSG_LENGTH];
if (!reedSolomonModule->decode(buf, msg)) {
return false;
}
sensor_id = msg[0];
size_t posn, len, inc = 5; size_t posn, len, inc = 5;
cipher->clear(); cipher->clear();
cipher->setKey(encryptionKey, key_size); cipher->setKey(encryptionKey, key_size);
@ -23,30 +37,16 @@ void TransmissionModule::decrypt(uint8_t *data, size_t data_size, uint8_t *outpu
cipher->addAuthData(authdata + posn, len); cipher->addAuthData(authdata + posn, len);
} }
size_t data_size = 4;
for (posn = 0; posn < data_size; posn += inc) { for (posn = 0; posn < data_size; posn += inc) {
len = data_size - posn; len = data_size - posn;
if (len > inc) if (len > inc)
len = inc; len = inc;
cipher->decrypt(buffer + posn, data + posn, len); cipher->decrypt(buffer + posn, msg + 1 + posn, len);
} }
memcpy(output_data, buffer, data_size); memcpy((void *) &data, buffer, 4);
}
bool TransmissionModule::receive(uint8_t &sensor_id, MeasureData &data)
{
uint8_t buf[TOTAL_LENGTH];
if (!transmitterModule->receive(buf, TOTAL_LENGTH)) {
return false;
}
uint8_t msg[MSG_LENGTH];
if (!reedSolomonModule->decode(buf, msg)) {
return false;
}
sensor_id = msg[0];
memcpy((void *) &data, msg+1, 4);
return true; return true;
} }

@ -33,7 +33,6 @@ public:
reedSolomonModule(&reedSolomonModule) { reedSolomonModule(&reedSolomonModule) {
cipher = new ChaChaPoly(); cipher = new ChaChaPoly();
} }
void decrypt(uint8_t * data, size_t data_size, uint8_t* output_data);
bool receive(uint8_t &sendor_id, MeasureData &data); bool receive(uint8_t &sendor_id, MeasureData &data);
}; };

@ -8,9 +8,11 @@
#define ECC_LENGTH 4 #define ECC_LENGTH 4
#define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH) #define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH)
void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t* output_data) void TransmissionModule::transmit(MeasureData &data)
{ {
size_t posn, len, inc = 5; size_t posn, len, inc = 5;
size_t data_size = 4;
cipher->clear(); cipher->clear();
cipher->setKey(encryptionKey, key_size); cipher->setKey(encryptionKey, key_size);
cipher->setIV(iv, iv_size); cipher->setIV(iv, iv_size);
@ -27,7 +29,7 @@ void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t
len = data_size - posn; len = data_size - posn;
if (len > inc) if (len > inc)
len = inc; len = inc;
cipher->encrypt(buffer + posn, input_data + posn, len); cipher->encrypt(buffer + posn, (uint8_t*)&data + 1 + posn, len);
} }
//************************* //*************************
@ -47,17 +49,12 @@ void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t
// cipher->decrypt(buffer2 + posn, buffer + posn, len); // cipher->decrypt(buffer2 + posn, buffer + posn, len);
// } // }
// if (memcmp(buffer2, input_data, data_size) != 0) { // if (memcmp(buffer2, (uint8_t*)&data + 1, data_size) != 0) {
// Serial.println("doesn't work"); // Serial.println("doesn't work");
// } else{ // } else{
// Serial.println("works"); // Serial.println("works");
// } // }
memcpy(output_data, buffer, data_size);
}
void TransmissionModule::transmit(MeasureData &data)
{
uint8_t msg[MSG_LENGTH]; uint8_t msg[MSG_LENGTH];
msg[0] = data.sensor_id; msg[0] = data.sensor_id;
memcpy(msg + 1, (void *) &data, 4); memcpy(msg + 1, (void *) &data, 4);

@ -37,7 +37,6 @@ public:
reedSolomonModule(&reedSolomonModule) { reedSolomonModule(&reedSolomonModule) {
cipher = new ChaChaPoly(); cipher = new ChaChaPoly();
} }
void encrypt(uint8_t * data, size_t data_size, uint8_t* output_data);
void transmit(MeasureData &data); void transmit(MeasureData &data);
}; };

@ -21,7 +21,6 @@ void loop() {
measureData.sensor_id = 0xA5; measureData.sensor_id = 0xA5;
measureData.sensor_type = 0x01; measureData.sensor_type = 0x01;
measureData.payload = getMockSensorData(); measureData.payload = getMockSensorData();
transmissionModule.encrypt((uint8_t*)&measureData+1, 4, (uint8_t*)&measureData+1);
transmissionModule.transmit(measureData); transmissionModule.transmit(measureData);
delay(1000); delay(1000);
} }
Loading…
Cancel
Save