eive-obsw/linux/devices/ploc/PlocSupvHelper.cpp

509 lines
16 KiB
C++
Raw Normal View History

2022-04-06 08:36:34 +02:00
#include "PlocSupvHelper.h"
#include <filesystem>
#include <fstream>
#include "OBSWConfig.h"
#ifdef XIPHOS_Q7S
#include "bsp_q7s/memory/FilesystemHelper.h"
#include "bsp_q7s/memory/SDCardManager.h"
2022-04-06 08:36:34 +02:00
#endif
2022-04-10 18:46:39 +02:00
#include "fsfw/globalfunctions/CRC.h"
2022-04-19 13:33:37 +02:00
#include "fsfw/timemanager/Countdown.h"
2022-04-14 07:52:21 +02:00
#include "mission/utility/Filenaming.h"
#include "mission/utility/ProgressPrinter.h"
#include "mission/utility/Timestamp.h"
2022-04-06 08:36:34 +02:00
PlocSupvHelper::PlocSupvHelper(object_id_t objectId) : SystemObject(objectId) {}
PlocSupvHelper::~PlocSupvHelper() {}
ReturnValue_t PlocSupvHelper::initialize() {
#ifdef XIPHOS_Q7S
sdcMan = SdCardManager::instance();
if (sdcMan == nullptr) {
sif::warning << "PlocSupvHelper::initialize: Invalid SD Card Manager" << std::endl;
return RETURN_FAILED;
}
#endif
return RETURN_OK;
}
ReturnValue_t PlocSupvHelper::performOperation(uint8_t operationCode) {
ReturnValue_t result = RETURN_OK;
semaphore.acquire();
while (true) {
switch (internalState) {
case InternalState::IDLE: {
semaphore.acquire();
break;
}
2022-04-07 11:00:07 +02:00
case InternalState::UPDATE: {
2022-04-06 08:36:34 +02:00
result = performUpdate();
if (result == RETURN_OK) {
2022-04-13 11:56:37 +02:00
triggerEvent(SUPV_UPDATE_SUCCESSFUL, result);
} else if (result == PROCESS_TERMINATED) {
// Event already triggered
2022-04-06 08:36:34 +02:00
} else {
2022-04-13 11:56:37 +02:00
triggerEvent(SUPV_UPDATE_FAILED, result);
}
internalState = InternalState::IDLE;
break;
}
case InternalState::REQUEST_EVENT_BUFFER: {
result = performEventBufferRequest();
if (result == RETURN_OK) {
triggerEvent(SUPV_EVENT_BUFFER_REQUEST_SUCCESSFUL, result);
} else if (result == PROCESS_TERMINATED) {
// Event already triggered
break;
} else {
2022-04-13 11:56:37 +02:00
triggerEvent(SUPV_EVENT_BUFFER_REQUEST_FAILED, result);
2022-04-06 08:36:34 +02:00
}
internalState = InternalState::IDLE;
break;
}
default:
sif::debug << "PlocSupvHelper::performOperation: Invalid state" << std::endl;
break;
}
}
}
2022-04-10 18:46:39 +02:00
ReturnValue_t PlocSupvHelper::setComIF(UartComIF* uartComIF_) {
if (uartComIF_ == nullptr) {
sif::warning << "PlocSupvHelper::initialize: Provided invalid uart com if" << std::endl;
2022-04-06 08:36:34 +02:00
return RETURN_FAILED;
}
2022-04-10 18:46:39 +02:00
uartComIF = uartComIF_;
2022-04-06 08:36:34 +02:00
return RETURN_OK;
}
void PlocSupvHelper::setComCookie(CookieIF* comCookie_) { comCookie = comCookie_; }
2022-04-07 11:00:07 +02:00
ReturnValue_t PlocSupvHelper::startUpdate(std::string file, uint8_t memoryId,
uint32_t startAddress) {
2022-04-06 08:36:34 +02:00
ReturnValue_t result = RETURN_OK;
#ifdef XIPHOS_Q7S
2022-04-07 11:00:07 +02:00
result = FilesystemHelper::checkPath(file);
2022-04-06 08:36:34 +02:00
if (result != RETURN_OK) {
2022-04-10 18:46:39 +02:00
sif::warning << "PlocSupvHelper::startUpdate: File " << file << " does not exist" << std::endl;
2022-04-06 08:36:34 +02:00
return result;
}
result = FilesystemHelper::fileExists(file);
2022-04-10 18:46:39 +02:00
if (result != RETURN_OK) {
sif::warning << "PlocSupvHelper::startUpdate: The file " << file << " does not exist"
<< std::endl;
return result;
2022-04-10 18:46:39 +02:00
}
#endif
#ifdef TE0720_1CFA
if (not std::filesystem::exists(file)) {
sif::warning << "PlocSupvHelper::startUpdate: The file " << file << " does not exist"
<< std::endl;
return RETURN_FAILED;
}
2022-04-06 08:36:34 +02:00
#endif
2022-04-07 11:00:07 +02:00
update.file = file;
2022-04-10 18:46:39 +02:00
update.length = getFileSize(update.file);
2022-04-07 11:00:07 +02:00
update.memoryId = memoryId;
update.startAddress = startAddress;
internalState = InternalState::UPDATE;
2022-04-10 18:46:39 +02:00
uartComIF->flushUartTxAndRxBuf(comCookie);
semaphore.release();
2022-04-06 08:36:34 +02:00
return result;
}
2022-04-13 11:56:37 +02:00
ReturnValue_t PlocSupvHelper::startEventbBufferRequest(std::string path) {
#ifdef XIPHOS_Q7S
ReturnValue_t result = FilesystemHelper::checkPath(path);
if (result != RETURN_OK) {
return result;
}
#endif
if (not std::filesystem::exists(path)) {
return PATH_NOT_EXISTS;
}
eventBufferReq.path = path;
internalState = InternalState::REQUEST_EVENT_BUFFER;
uartComIF->flushUartTxAndRxBuf(comCookie);
semaphore.release();
return RETURN_OK;
}
2022-04-06 08:36:34 +02:00
void PlocSupvHelper::stopProcess() { terminate = true; }
ReturnValue_t PlocSupvHelper::performUpdate() {
ReturnValue_t result = RETURN_OK;
2022-04-19 13:33:37 +02:00
result = calcImageCrc();
if (result != RETURN_OK) {
return result;
}
2022-04-10 18:46:39 +02:00
result = prepareUpdate();
if (result != RETURN_OK) {
return result;
}
2022-04-06 08:36:34 +02:00
result = eraseMemory();
if (result != RETURN_OK) {
return result;
}
#if OBSW_DEBUG_PLOC_SUPERVISOR == 1
2022-04-19 13:33:37 +02:00
ProgressPrinter progressPrinter("Supervisor update", update.length, ProgressPrinter::ONE_PERCENT);
#endif /* OBSW_DEBUG_PLOC_SUPERVISOR == 1 */
2022-04-10 18:46:39 +02:00
uint8_t tempData[supv::WriteMemory::CHUNK_MAX];
2022-04-06 08:36:34 +02:00
std::ifstream file(update.file, std::ifstream::binary);
2022-04-10 18:46:39 +02:00
size_t remainingSize = update.length;
uint16_t dataLength = 0;
size_t bytesWritten = 0;
uint16_t sequenceCount = 1;
supv::SequenceFlags seqFlags = supv::SequenceFlags::FIRST_PKT;
2022-04-06 08:36:34 +02:00
while (remainingSize > 0) {
if (terminate) {
2022-04-10 18:46:39 +02:00
return PROCESS_TERMINATED;
2022-04-06 08:36:34 +02:00
}
2022-04-10 18:46:39 +02:00
if (remainingSize > supv::WriteMemory::CHUNK_MAX) {
dataLength = supv::WriteMemory::CHUNK_MAX;
2022-04-06 08:36:34 +02:00
} else {
2022-04-19 13:33:37 +02:00
dataLength = static_cast<uint16_t>(remainingSize);
2022-04-06 08:36:34 +02:00
}
if (file.is_open()) {
2022-04-10 18:46:39 +02:00
file.seekg(bytesWritten, file.beg);
2022-04-06 08:36:34 +02:00
file.read(reinterpret_cast<char*>(tempData), dataLength);
2022-04-19 13:33:37 +02:00
if (!file) {
sif::warning << "PlocSupvHelper::performUpdate: Read only " << file.gcount() << " of "
<< dataLength << " bytes" << std::endl;
sif::info << "PlocSupvHelper::performUpdate: Failed when trying to read byte "
<< bytesWritten << std::endl;
}
2022-04-06 08:36:34 +02:00
remainingSize -= dataLength;
} else {
return FILE_CLOSED_ACCIDENTALLY;
}
2022-04-10 18:46:39 +02:00
if (bytesWritten == 0) {
seqFlags = supv::SequenceFlags::FIRST_PKT;
} else if (remainingSize == 0) {
seqFlags = supv::SequenceFlags::LAST_PKT;
} else {
seqFlags = supv::SequenceFlags::CONTINUED_PKT;
}
supv::WriteMemory packet(seqFlags, sequenceCount++, update.memoryId,
update.startAddress + bytesWritten, dataLength, tempData);
result = handlePacketTransmission(packet);
2022-04-06 08:36:34 +02:00
if (result != RETURN_OK) {
return result;
}
2022-04-10 18:46:39 +02:00
bytesWritten += dataLength;
#if OBSW_DEBUG_PLOC_SUPERVISOR == 1
progressPrinter.print(bytesWritten);
#endif /* OBSW_DEBUG_PLOC_SUPERVISOR == 1 */
2022-04-17 14:52:43 +02:00
}
2022-04-10 18:46:39 +02:00
result = handleCheckMemoryCommand();
if (result != RETURN_OK) {
return result;
2022-04-06 08:36:34 +02:00
}
return result;
}
2022-04-13 11:56:37 +02:00
ReturnValue_t PlocSupvHelper::performEventBufferRequest() {
using namespace supv;
2022-04-14 07:52:21 +02:00
ReturnValue_t result = RETURN_OK;
2022-04-13 11:56:37 +02:00
RequestLoggingData packet(RequestLoggingData::Sa::REQUEST_EVENT_BUFFERS);
result = sendCommand(packet);
if (result != RETURN_OK) {
return result;
}
result = handleAck();
if (result != RETURN_OK) {
return result;
}
result = handleEventBufferReception();
if (result != RETURN_OK) {
return result;
}
result = handleExe();
if (result != RETURN_OK) {
return result;
}
2022-04-14 07:52:21 +02:00
return result;
2022-04-13 11:56:37 +02:00
}
2022-04-10 18:46:39 +02:00
ReturnValue_t PlocSupvHelper::prepareUpdate() {
2022-04-06 08:36:34 +02:00
ReturnValue_t result = RETURN_OK;
2022-04-10 18:46:39 +02:00
supv::ApidOnlyPacket packet(supv::APID_PREPARE_UPDATE);
result = handlePacketTransmission(packet);
2022-04-06 08:36:34 +02:00
if (result != RETURN_OK) {
return result;
}
return RETURN_OK;
}
2022-04-10 18:46:39 +02:00
ReturnValue_t PlocSupvHelper::eraseMemory() {
2022-04-06 08:36:34 +02:00
ReturnValue_t result = RETURN_OK;
2022-04-10 18:46:39 +02:00
supv::EraseMemory eraseMemory(update.memoryId, update.startAddress, update.length);
2022-04-19 13:33:37 +02:00
result = handlePacketTransmission(eraseMemory, supv::recv_timeout::ERASE_MEMORY);
2022-04-06 08:36:34 +02:00
if (result != RETURN_OK) {
return result;
}
return RETURN_OK;
}
2022-04-19 13:33:37 +02:00
ReturnValue_t PlocSupvHelper::handlePacketTransmission(SpacePacket& packet,
uint32_t timeoutExecutionReport) {
2022-04-06 08:36:34 +02:00
ReturnValue_t result = RETURN_OK;
2022-04-10 18:46:39 +02:00
result = sendCommand(packet);
2022-04-06 08:36:34 +02:00
if (result != RETURN_OK) {
return result;
}
result = handleAck();
if (result != RETURN_OK) {
return result;
}
2022-04-19 13:33:37 +02:00
result = handleExe(timeoutExecutionReport);
2022-04-06 08:36:34 +02:00
if (result != RETURN_OK) {
return result;
}
return RETURN_OK;
}
2022-04-10 18:46:39 +02:00
ReturnValue_t PlocSupvHelper::sendCommand(SpacePacket& packet) {
2022-04-06 08:36:34 +02:00
ReturnValue_t result = RETURN_OK;
2022-04-17 14:52:43 +02:00
rememberApid = packet.getAPID();
2022-04-10 18:46:39 +02:00
result = uartComIF->sendMessage(comCookie, packet.getWholeData(), packet.getFullSize());
2022-04-06 08:36:34 +02:00
if (result != RETURN_OK) {
sif::warning << "PlocSupvHelper::sendCommand: Failed to send command" << std::endl;
2022-04-10 18:46:39 +02:00
triggerEvent(SUPV_SENDING_COMMAND_FAILED, result, static_cast<uint32_t>(internalState));
2022-04-06 08:36:34 +02:00
return result;
}
return result;
}
ReturnValue_t PlocSupvHelper::handleAck() {
ReturnValue_t result = RETURN_OK;
supv::TmPacket tmPacket;
result = handleTmReception(&tmPacket, supv::SIZE_ACK_REPORT);
if (result != RETURN_OK) {
2022-04-17 14:52:43 +02:00
triggerEvent(ACK_RECEPTION_FAILURE, result, static_cast<uint32_t>(rememberApid));
2022-04-10 18:46:39 +02:00
sif::warning << "PlocSupvHelper::handleAck: Error in reception of acknowledgment report"
<< std::endl;
2022-04-06 08:36:34 +02:00
return result;
}
uint16_t apid = tmPacket.getAPID();
if (apid != supv::APID_ACK_SUCCESS) {
handleAckApidFailure(apid);
return RETURN_FAILED;
}
return RETURN_OK;
}
void PlocSupvHelper::handleAckApidFailure(uint16_t apid) {
if (apid == supv::APID_ACK_FAILURE) {
2022-04-10 18:46:39 +02:00
triggerEvent(SUPV_ACK_FAILURE_REPORT, static_cast<uint32_t>(internalState));
2022-04-06 08:36:34 +02:00
sif::warning << "PlocSupvHelper::handleAckApidFailure: Received acknowledgement failure "
<< "report" << std::endl;
} else {
2022-04-10 18:46:39 +02:00
triggerEvent(SUPV_ACK_INVALID_APID, apid, static_cast<uint32_t>(internalState));
2022-04-06 08:36:34 +02:00
sif::warning << "PlocSupvHelper::handleAckApidFailure: Expected acknowledgement report "
<< "but received space packet with apid " << std::hex << apid << std::endl;
}
}
2022-04-19 13:33:37 +02:00
ReturnValue_t PlocSupvHelper::handleExe(uint32_t timeout) {
2022-04-06 08:36:34 +02:00
ReturnValue_t result = RETURN_OK;
supv::TmPacket tmPacket;
2022-04-19 13:33:37 +02:00
result = handleTmReception(&tmPacket, supv::SIZE_EXE_REPORT, timeout);
2022-04-06 08:36:34 +02:00
if (result != RETURN_OK) {
2022-04-17 14:52:43 +02:00
triggerEvent(EXE_RECEPTION_FAILURE, result, static_cast<uint32_t>(rememberApid));
2022-04-10 18:46:39 +02:00
sif::warning << "PlocSupvHelper::handleExe: Error in reception of execution report"
<< std::endl;
2022-04-06 08:36:34 +02:00
return result;
}
uint16_t apid = tmPacket.getAPID();
2022-04-10 18:46:39 +02:00
if (apid != supv::APID_EXE_SUCCESS) {
2022-04-06 08:36:34 +02:00
handleExeApidFailure(apid);
return RETURN_FAILED;
}
return RETURN_OK;
}
void PlocSupvHelper::handleExeApidFailure(uint16_t apid) {
2022-04-10 18:46:39 +02:00
if (apid == supv::APID_EXE_FAILURE) {
2022-04-07 11:00:07 +02:00
triggerEvent(SUPV_EXE_FAILURE_REPORT, static_cast<uint32_t>(internalState));
2022-04-06 08:36:34 +02:00
sif::warning << "PlocSupvHelper::handleExeApidFailure: Received execution failure "
<< "report" << std::endl;
} else {
2022-04-07 11:00:07 +02:00
triggerEvent(SUPV_EXE_INVALID_APID, apid, static_cast<uint32_t>(internalState));
2022-04-06 08:36:34 +02:00
sif::warning << "PlocSupvHelper::handleExeApidFailure: Expected execution report "
<< "but received space packet with apid " << std::hex << apid << std::endl;
}
}
2022-04-19 13:33:37 +02:00
ReturnValue_t PlocSupvHelper::handleTmReception(supv::TmPacket* tmPacket, size_t remainingBytes,
uint32_t timeout) {
2022-04-06 08:36:34 +02:00
ReturnValue_t result = RETURN_OK;
size_t readBytes = 0;
size_t currentBytes = 0;
2022-04-19 13:33:37 +02:00
Countdown countdown(timeout);
while (!countdown.hasTimedOut()) {
2022-04-06 08:36:34 +02:00
result = receive(tmPacket->getWholeData() + readBytes, &currentBytes, remainingBytes);
if (result != RETURN_OK) {
return result;
}
readBytes += currentBytes;
remainingBytes = remainingBytes - currentBytes;
if (remainingBytes == 0) {
break;
}
}
if (remainingBytes != 0) {
2022-04-14 07:52:21 +02:00
sif::warning << "PlocSupvHelper::handleTmReception: Failed to read " << remainingBytes
<< " bytes" << std::endl;
2022-04-06 08:36:34 +02:00
return RETURN_FAILED;
}
result = tmPacket->checkCrc();
if (result != RETURN_OK) {
sif::warning << "PlocSupvHelper::handleTmReception: CRC check failed" << std::endl;
return result;
}
return result;
}
ReturnValue_t PlocSupvHelper::receive(uint8_t* data, size_t* readBytes, size_t requestBytes) {
ReturnValue_t result = RETURN_OK;
uint8_t* buffer = nullptr;
result = uartComIF->requestReceiveMessage(comCookie, requestBytes);
if (result != RETURN_OK) {
sif::warning << "PlocSupvHelper::receive: Failed to request reply" << std::endl;
2022-04-10 18:46:39 +02:00
triggerEvent(SUPV_HELPER_REQUESTING_REPLY_FAILED, result,
2022-04-06 08:36:34 +02:00
static_cast<uint32_t>(static_cast<uint32_t>(internalState)));
return RETURN_FAILED;
}
result = uartComIF->readReceivedMessage(comCookie, &buffer, readBytes);
if (result != RETURN_OK) {
sif::warning << "PlocSupvHelper::receive: Failed to read received message" << std::endl;
2022-04-10 18:46:39 +02:00
triggerEvent(SUPV_HELPER_READING_REPLY_FAILED, result, static_cast<uint32_t>(internalState));
2022-04-06 08:36:34 +02:00
return RETURN_FAILED;
}
if (*readBytes > 0) {
std::memcpy(data, buffer, *readBytes);
}
return result;
}
2022-04-10 18:46:39 +02:00
ReturnValue_t PlocSupvHelper::calcImageCrc() {
ReturnValue_t result = RETURN_OK;
#ifdef XIPHOS_Q7S
result = FilesystemHelper::checkPath(update.file);
#endif
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;
2022-04-19 13:33:37 +02:00
#if OBSW_DEBUG_PLOC_SUPERVISOR == 1
ProgressPrinter progress("Supervisor update crc calculation", update.length);
#endif /* OBSW_DEBUG_PLOC_SUPERVISOR == 1 */
uint32_t byteCount = 0;
for (byteCount = 0; byteCount < update.length; byteCount++) {
2022-04-10 18:46:39 +02:00
file.seekg(byteCount, file.beg);
file.read(reinterpret_cast<char*>(&input), 1);
remainder = CRC::crc16ccitt(&input, sizeof(input), remainder);
2022-04-19 13:33:37 +02:00
#if OBSW_DEBUG_PLOC_SUPERVISOR == 1
progress.print(byteCount);
#endif /* OBSW_DEBUG_PLOC_SUPERVISOR == 1 */
2022-04-10 18:46:39 +02:00
}
2022-04-19 13:33:37 +02:00
#if OBSW_DEBUG_PLOC_SUPERVISOR == 1
progress.print(byteCount);
#endif /* OBSW_DEBUG_PLOC_SUPERVISOR == 1 */
2022-04-10 18:46:39 +02:00
file.close();
update.crc = remainder;
return result;
}
ReturnValue_t PlocSupvHelper::handleCheckMemoryCommand() {
ReturnValue_t result = RETURN_OK;
// Verification of update write procedure
supv::CheckMemory packet(update.memoryId, update.startAddress, update.length);
result = sendCommand(packet);
if (result != RETURN_OK) {
return result;
}
result = handleAck();
if (result != RETURN_OK) {
return result;
}
supv::UpdateStatusReport updateStatusReport;
result = handleTmReception(&updateStatusReport,
2022-04-19 13:33:37 +02:00
static_cast<size_t>(updateStatusReport.getNominalSize()),
supv::recv_timeout::UPDATE_STATUS_REPORT);
2022-04-10 18:46:39 +02:00
if (result != RETURN_OK) {
return result;
}
result = handleExe();
if (result != RETURN_OK) {
return result;
}
result = updateStatusReport.parseDataField();
if (result != RETURN_OK) {
return result;
}
result = updateStatusReport.verifycrc(update.crc);
if (result != RETURN_OK) {
sif::warning << "PlocSupvHelper::performUpdate: CRC failure. Expected CRC 0x" << std::hex
<< update.crc << " but received CRC 0x" << updateStatusReport.getCrc()
<< std::endl;
return result;
}
return result;
}
uint32_t PlocSupvHelper::getFileSize(std::string filename) {
std::ifstream file(filename, std::ifstream::binary);
file.seekg(0, file.end);
uint32_t size = file.tellg();
file.close();
return size;
2022-04-10 18:46:39 +02:00
}
2022-04-13 11:56:37 +02:00
ReturnValue_t PlocSupvHelper::handleEventBufferReception() {
2022-04-14 07:52:21 +02:00
ReturnValue_t result = RETURN_OK;
std::string filename = Filenaming::generateAbsoluteFilename(
eventBufferReq.path, eventBufferReq.filename, timestamping);
2022-04-13 11:56:37 +02:00
std::ofstream file(filename, std::ios_base::app | std::ios_base::out);
uint32_t packetsRead = 0;
2022-04-14 07:52:21 +02:00
size_t requestLen = 0;
2022-04-13 11:56:37 +02:00
supv::TmPacket tmPacket;
2022-04-14 07:52:21 +02:00
for (packetsRead = 0; packetsRead < NUM_EVENT_BUFFER_PACKETS; packetsRead++) {
2022-04-13 11:56:37 +02:00
if (terminate) {
triggerEvent(SUPV_EVENT_BUFFER_REQUEST_TERMINATED, packetsRead - 1);
file.close();
return PROCESS_TERMINATED;
}
2022-04-14 07:52:21 +02:00
if (packetsRead == NUM_EVENT_BUFFER_PACKETS - 1) {
requestLen = SIZE_EVENT_BUFFER_LAST_PACKET;
} else {
requestLen = SIZE_EVENT_BUFFER_FULL_PACKET;
}
result = handleTmReception(&tmPacket, requestLen);
2022-04-13 11:56:37 +02:00
if (result != RETURN_OK) {
2022-04-14 07:52:21 +02:00
sif::debug << "PlocSupvHelper::handleEventBufferReception: Failed while trying to read packet"
<< " " << packetsRead + 1 << std::endl;
2022-04-14 07:52:21 +02:00
file.close();
2022-04-13 11:56:37 +02:00
return result;
}
uint16_t apid = tmPacket.getAPID();
if (apid != supv::APID_MRAM_DUMP_TM) {
sif::warning << "PlocSupvHelper::handleEventBufferReception: Did not expect space packet "
2022-04-14 07:52:21 +02:00
<< "with APID 0x" << std::hex << apid << std::endl;
file.close();
return EVENT_BUFFER_REPLY_INVALID_APID;
2022-04-13 11:56:37 +02:00
}
2022-04-14 07:52:21 +02:00
file.write(reinterpret_cast<const char*>(tmPacket.getPacketData()),
tmPacket.getPayloadDataLength());
2022-04-13 11:56:37 +02:00
}
2022-04-14 07:52:21 +02:00
return result;
2022-04-13 11:56:37 +02:00
}