speed up crc proc significantly

This commit is contained in:
Robin Müller 2022-08-22 10:35:23 +02:00
parent 1f3365960d
commit f98411f421
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC

View File

@ -1,5 +1,7 @@
#include "PlocSupvHelper.h"
#include <etl/crc16_ccitt.h>
#include <cmath>
#include <filesystem>
#include <fstream>
@ -10,7 +12,6 @@
#include "bsp_q7s/memory/SdCardManager.h"
#endif
#include "fsfw/globalfunctions/CRC.h"
#include "fsfw/tasks/TaskFactory.h"
#include "fsfw/timemanager/Countdown.h"
#include "mission/utility/Filenaming.h"
@ -509,32 +510,39 @@ ReturnValue_t PlocSupvHelper::calcImageCrc() {
#ifdef XIPHOS_Q7S
result = FilesystemHelper::checkPath(update.file);
#endif
auto crc16Calcer = etl::crc16_ccitt();
if (result != RETURN_OK) {
sif::warning << "PlocSupvHelper::calcImageCrc: File " << update.file << " does not exist"
<< std::endl;
return result;
}
std::ifstream file(update.file, std::ifstream::binary);
uint16_t remainder = CRC16_INIT;
uint8_t input;
std::array<uint8_t, 1025> crcBuf;
#if OBSW_DEBUG_PLOC_SUPERVISOR == 1
ProgressPrinter progress("Supervisor update crc calculation", update.length,
ProgressPrinter::ONE_PERCENT);
#endif /* OBSW_DEBUG_PLOC_SUPERVISOR == 1 */
uint32_t byteCount = 0;
for (byteCount = 0; byteCount < update.length; byteCount++) {
while (byteCount < update.length) {
size_t bytesToRead = 1024;
size_t remLen = update.length - byteCount;
if (remLen < 1024) {
bytesToRead = remLen;
}
file.seekg(byteCount, file.beg);
file.read(reinterpret_cast<char*>(&input), 1);
remainder = CRC::crc16ccitt(&input, sizeof(input), remainder);
file.read(reinterpret_cast<char*>(crcBuf.data()), bytesToRead);
crc16Calcer.add(crcBuf.begin(), crcBuf.begin() + bytesToRead);
#if OBSW_DEBUG_PLOC_SUPERVISOR == 1
progress.print(byteCount);
#endif /* OBSW_DEBUG_PLOC_SUPERVISOR == 1 */
byteCount += bytesToRead;
}
#if OBSW_DEBUG_PLOC_SUPERVISOR == 1
progress.print(byteCount);
#endif /* OBSW_DEBUG_PLOC_SUPERVISOR == 1 */
file.close();
update.crc = remainder;
update.crc = crc16Calcer.value();
return result;
}