PLOC SUPV Update to newer firmware #316

Merged
muellerr merged 99 commits from mueller/tas_ploc_supv_update into develop 2022-11-18 14:27:13 +01:00
2 changed files with 142 additions and 82 deletions
Showing only changes of commit dc024e5385 - Show all commits

View File

@ -90,15 +90,45 @@ ReturnValue_t PlocSupvHelper::initialize() {
} }
ReturnValue_t PlocSupvHelper::performOperation(uint8_t operationCode) { ReturnValue_t PlocSupvHelper::performOperation(uint8_t operationCode) {
ReturnValue_t result;
lock->lockMutex(); lock->lockMutex();
state = InternalState::IDLE; state = InternalState::IDLE;
lock->unlockMutex(); lock->unlockMutex();
bool putTaskToSleep = false;
while (true) { while (true) {
semaphore->acquire(); semaphore->acquire();
int bytesRead = 0;
while (true) { while (true) {
bytesRead = read(serialPort, reinterpret_cast<void*>(recBuf.data()), putTaskToSleep = handleUartReception();
if (putTaskToSleep) {
break;
}
lock->lockMutex();
InternalState currentState = state;
lock->unlockMutex();
switch (currentState) {
case InternalState::IDLE: {
putTaskToSleep = true;
break;
}
case InternalState::FINISH: {
state = InternalState::IDLE;
putTaskToSleep = true;
break;
}
case InternalState::RUNNING: {
putTaskToSleep = handleRunningRequest();
break;
}
}
if (putTaskToSleep) {
break;
}
}
}
}
bool PlocSupvHelper::handleUartReception() {
ReturnValue_t result = OK;
ssize_t bytesRead = read(serialPort, reinterpret_cast<void*>(recBuf.data()),
static_cast<unsigned int>(recBuf.size())); static_cast<unsigned int>(recBuf.size()));
if (bytesRead == 0) { if (bytesRead == 0) {
{ {
@ -107,88 +137,27 @@ ReturnValue_t PlocSupvHelper::performOperation(uint8_t operationCode) {
// Flush received and unread data // Flush received and unread data
tcflush(serialPort, TCIOFLUSH); tcflush(serialPort, TCIOFLUSH);
state = InternalState::IDLE; state = InternalState::IDLE;
break; return true;
} }
} }
while (result != NO_PACKET_FOUND) {
result = tryHdlcParsing();
}
} else if (bytesRead < 0) { } else if (bytesRead < 0) {
sif::warning << "ScexUartReader::performOperation: read call failed with error [" << errno sif::warning << "PlocSupvHelper::performOperation: read call failed with error [" << errno
<< ", " << strerror(errno) << "]" << std::endl; << ", " << strerror(errno) << "]" << std::endl;
break; return true;
} else if (bytesRead >= static_cast<int>(recBuf.size())) { } else if (bytesRead >= static_cast<int>(recBuf.size())) {
sif::error << "ScexUartReader::performOperation: Receive buffer too small for " << bytesRead sif::error << "PlocSupvHelper::performOperation: Receive buffer too small for " << bytesRead
<< " bytes" << std::endl; << " bytes" << std::endl;
} else if (bytesRead > 0) { } else if (bytesRead > 0) {
if (debugMode) { if (debugMode) {
sif::info << "Received " << bytesRead sif::info << "Received " << bytesRead << " bytes from the PLOC Supervisor:" << std::endl;
<< " bytes from the Solar Cell Experiment:" << std::endl;
} }
recRingBuf.writeData(recBuf.data(), bytesRead); recRingBuf.writeData(recBuf.data(), bytesRead);
// insert buffer into ring buffer here tryHdlcParsing();
// ReturnValue_t result = dleParser.passData(recBuf.data(), bytesRead);
// TODO: Parse ring buffer here
}
lock->lockMutex();
InternalState currentState = state;
lock->unlockMutex();
switch (currentState) {
case InternalState::IDLE: {
break;
}
case InternalState::UPDATE: {
result = executeUpdate();
if (result == returnvalue::OK) {
triggerEvent(SUPV_UPDATE_SUCCESSFUL, result);
} else if (result == PROCESS_TERMINATED) {
// Event already triggered
} else {
triggerEvent(SUPV_UPDATE_FAILED, result);
}
MutexGuard mg(lock);
state = InternalState::IDLE;
break;
}
case InternalState::CHECK_MEMORY: {
executeFullCheckMemoryCommand();
MutexGuard mg(lock);
state = InternalState::IDLE;
break;
}
case InternalState::CONTINUE_UPDATE: {
result = continueUpdate();
if (result == returnvalue::OK) {
triggerEvent(SUPV_CONTINUE_UPDATE_SUCCESSFUL, result);
} else if (result == PROCESS_TERMINATED) {
// Event already triggered
} else {
triggerEvent(SUPV_CONTINUE_UPDATE_FAILED, result);
}
MutexGuard mg(lock);
state = InternalState::IDLE;
break;
}
case InternalState::REQUEST_EVENT_BUFFER: {
result = performEventBufferRequest();
if (result == returnvalue::OK) {
triggerEvent(SUPV_EVENT_BUFFER_REQUEST_SUCCESSFUL, result);
} else if (result == PROCESS_TERMINATED) {
// Event already triggered
break;
} else {
triggerEvent(SUPV_EVENT_BUFFER_REQUEST_FAILED, result);
}
MutexGuard mg(lock);
state = InternalState::IDLE;
break;
}
case InternalState::HANDLER_DRIVEN: {
continue;
}
default:
sif::debug << "PlocSupvHelper::performOperation: Invalid state" << std::endl;
break;
}
}
} }
return false;
} }
ReturnValue_t PlocSupvHelper::setComIF(UartComIF* uartComIF_) { ReturnValue_t PlocSupvHelper::setComIF(UartComIF* uartComIF_) {
@ -253,7 +222,7 @@ ReturnValue_t PlocSupvHelper::performUpdate(const supv::UpdateParams& params) {
update.packetNum = 1; update.packetNum = 1;
update.deleteMemory = params.deleteMemory; update.deleteMemory = params.deleteMemory;
update.sequenceCount = params.seqCount; update.sequenceCount = params.seqCount;
state = InternalState::UPDATE; request = Request::UPDATE;
uartComIF->flushUartTxAndRxBuf(comCookie); uartComIF->flushUartTxAndRxBuf(comCookie);
semaphore->release(); semaphore->release();
return result; return result;
@ -272,14 +241,14 @@ ReturnValue_t PlocSupvHelper::performMemCheck(uint8_t memoryId, uint32_t startAd
update.startAddress = startAddress; update.startAddress = startAddress;
update.length = sizeToCheck; update.length = sizeToCheck;
update.crcShouldBeChecked = checkCrc; update.crcShouldBeChecked = checkCrc;
state = InternalState::CHECK_MEMORY; request = Request::CHECK_MEMORY;
uartComIF->flushUartTxAndRxBuf(comCookie); uartComIF->flushUartTxAndRxBuf(comCookie);
semaphore->release(); semaphore->release();
return returnvalue::OK; return returnvalue::OK;
} }
void PlocSupvHelper::initiateUpdateContinuation() { void PlocSupvHelper::initiateUpdateContinuation() {
state = InternalState::CONTINUE_UPDATE; request = Request::CONTINUE_UPDATE;
semaphore->release(); semaphore->release();
} }
@ -294,7 +263,7 @@ ReturnValue_t PlocSupvHelper::startEventBufferRequest(std::string path) {
return PATH_NOT_EXISTS; return PATH_NOT_EXISTS;
} }
eventBufferReq.path = path; eventBufferReq.path = path;
state = InternalState::REQUEST_EVENT_BUFFER; request = Request::REQUEST_EVENT_BUFFER;
uartComIF->flushUartTxAndRxBuf(comCookie); uartComIF->flushUartTxAndRxBuf(comCookie);
semaphore->release(); semaphore->release();
return returnvalue::OK; return returnvalue::OK;
@ -917,7 +886,7 @@ ReturnValue_t PlocSupvHelper::sendMessage(CookieIF* cookie, const uint8_t* sendD
if (result != OK) { if (result != OK) {
std::cout << "PlocSupvHelper::sendMessage: Releasing semaphore failed" << std::endl; std::cout << "PlocSupvHelper::sendMessage: Releasing semaphore failed" << std::endl;
} }
return returnvalue::OK; return encodeAndSendPacket(sendData, sendLen);
} }
ReturnValue_t PlocSupvHelper::getSendSuccess(CookieIF* cookie) { return returnvalue::OK; } ReturnValue_t PlocSupvHelper::getSendSuccess(CookieIF* cookie) { return returnvalue::OK; }
@ -926,6 +895,74 @@ ReturnValue_t PlocSupvHelper::requestReceiveMessage(CookieIF* cookie, size_t req
return returnvalue::OK; return returnvalue::OK;
} }
bool PlocSupvHelper::handleRunningRequest() {
ReturnValue_t result = OK;
switch (request) {
case Request::UPDATE: {
result = executeUpdate();
if (result == returnvalue::OK) {
triggerEvent(SUPV_UPDATE_SUCCESSFUL, result);
} else if (result == PROCESS_TERMINATED) {
// Event already triggered
} else {
triggerEvent(SUPV_UPDATE_FAILED, result);
}
MutexGuard mg(lock);
state = InternalState::IDLE;
return true;
}
case Request::CHECK_MEMORY: {
executeFullCheckMemoryCommand();
MutexGuard mg(lock);
state = InternalState::IDLE;
return true;
}
case Request::CONTINUE_UPDATE: {
result = continueUpdate();
if (result == returnvalue::OK) {
triggerEvent(SUPV_CONTINUE_UPDATE_SUCCESSFUL, result);
} else if (result == PROCESS_TERMINATED) {
// Event already triggered
} else {
triggerEvent(SUPV_CONTINUE_UPDATE_FAILED, result);
}
MutexGuard mg(lock);
state = InternalState::IDLE;
return true;
}
case Request::REQUEST_EVENT_BUFFER: {
result = performEventBufferRequest();
if (result == returnvalue::OK) {
triggerEvent(SUPV_EVENT_BUFFER_REQUEST_SUCCESSFUL, result);
} else if (result == PROCESS_TERMINATED) {
// Event already triggered
break;
} else {
triggerEvent(SUPV_EVENT_BUFFER_REQUEST_FAILED, result);
}
MutexGuard mg(lock);
state = InternalState::IDLE;
return true;
}
case Request::HANDLER_DRIVEN: {
break;
}
}
return false;
}
ReturnValue_t PlocSupvHelper::encodeAndSendPacket(const uint8_t* sendData, size_t sendLen) {
size_t encodedLen = 0;
hdlc_add_framing(sendData, sendLen, sendBuf.data(), &encodedLen);
size_t bytesWritten = write(serialPort, sendBuf.data(), encodedLen);
if (bytesWritten != encodedLen) {
sif::warning << "ScexUartReader::sendMessage: Sending ping command to solar experiment failed"
<< std::endl;
return FAILED;
}
return returnvalue::OK;
}
ReturnValue_t PlocSupvHelper::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, ReturnValue_t PlocSupvHelper::readReceivedMessage(CookieIF* cookie, uint8_t** buffer,
size_t* size) { size_t* size) {
MutexGuard mg(lock); MutexGuard mg(lock);
@ -937,11 +974,28 @@ ReturnValue_t PlocSupvHelper::readReceivedMessage(CookieIF* cookie, uint8_t** bu
*buffer = ipcBuffer.data(); *buffer = ipcBuffer.data();
ReturnValue_t result = ipcRingBuf.readData(ipcBuffer.data(), *size, true); ReturnValue_t result = ipcRingBuf.readData(ipcBuffer.data(), *size, true);
if (result != OK) { if (result != OK) {
sif::warning << "ScexUartReader::readReceivedMessage: Reading RingBuffer failed" << std::endl; sif::warning << "PlocSupvHelper::readReceivedMessage: Reading RingBuffer failed" << std::endl;
} }
return OK; return OK;
} }
ReturnValue_t PlocSupvHelper::tryHdlcParsing() {
size_t bytesRead = 0;
ReturnValue_t result = parseRecRingBufForHdlc(bytesRead);
if (result == returnvalue::OK) {
// Packet found, advance read pointer.
ipcRingBuf.writeData(decodedBuf.data(), bytesRead);
recRingBuf.deleteData(bytesRead);
} else if (result != NO_PACKET_FOUND) {
sif::warning << "ScexUartReader::performOperation: Possible packet loss" << std::endl;
// Markers found at wrong place
// which might be a hint for a possibly lost packet.
// Maybe trigger event?
recRingBuf.deleteData(bytesRead);
}
return result;
}
ReturnValue_t PlocSupvHelper::parseRecRingBufForHdlc(size_t& readSize) { ReturnValue_t PlocSupvHelper::parseRecRingBufForHdlc(size_t& readSize) {
size_t availableData = recRingBuf.getAvailableReadData(); size_t availableData = recRingBuf.getAvailableReadData();
if (availableData == 0) { if (availableData == 0) {

View File

@ -227,6 +227,7 @@ class PlocSupvHelper : public DeviceCommunicationIF,
SdCardManager* sdcMan = nullptr; SdCardManager* sdcMan = nullptr;
#endif #endif
SimpleRingBuffer recRingBuf; SimpleRingBuffer recRingBuf;
std::array<uint8_t, 2048> sendBuf = {};
std::array<uint8_t, 2048> recBuf = {}; std::array<uint8_t, 2048> recBuf = {};
std::array<uint8_t, 2048> encodedBuf = {}; std::array<uint8_t, 2048> encodedBuf = {};
std::array<uint8_t, 1200> decodedBuf = {}; std::array<uint8_t, 1200> decodedBuf = {};
@ -254,8 +255,13 @@ class PlocSupvHelper : public DeviceCommunicationIF,
// Remembers APID to know at which command a procedure failed // Remembers APID to know at which command a procedure failed
uint16_t rememberApid = 0; uint16_t rememberApid = 0;
bool handleRunningRequest();
bool handleUartReception();
ReturnValue_t encodeAndSendPacket(const uint8_t* sendData, size_t sendLen);
void executeFullCheckMemoryCommand(); void executeFullCheckMemoryCommand();
ReturnValue_t tryHdlcParsing();
ReturnValue_t parseRecRingBufForHdlc(size_t& readSize); ReturnValue_t parseRecRingBufForHdlc(size_t& readSize);
ReturnValue_t executeUpdate(); ReturnValue_t executeUpdate();
ReturnValue_t continueUpdate(); ReturnValue_t continueUpdate();