From f918be204c0c9ba9100753ab111223bb95e3fc04 Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 03:07:13 +0300 Subject: [PATCH] refactor --- .../TransmissionModule/TransmissionModule.cpp | 38 +++++++++---------- .../TransmissionModule/TransmissionModule.h | 1 - 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp index 5e6818f..7ee88f4 100644 --- a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp @@ -8,8 +8,22 @@ #define ECC_LENGTH 4 #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; cipher->clear(); 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); } + size_t data_size = 4; + for (posn = 0; posn < data_size; posn += inc) { len = data_size - posn; if (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); -} - -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); + memcpy((void *) &data, buffer, 4); return true; } diff --git a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h index 92efcf5..8fb9a43 100644 --- a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h +++ b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h @@ -33,7 +33,6 @@ public: reedSolomonModule(&reedSolomonModule) { cipher = new ChaChaPoly(); } - void decrypt(uint8_t * data, size_t data_size, uint8_t* output_data); bool receive(uint8_t &sendor_id, MeasureData &data); };