eive-obsw/linux/devices/ScexUartReader.cpp

200 lines
7.1 KiB
C++
Raw Normal View History

2022-03-25 17:48:38 +01:00
#include "ScexUartReader.h"
2022-04-08 19:12:21 +02:00
#include <fcntl.h> // Contains file controls like O_RDWR
#include <fsfw/globalfunctions/arrayprinter.h>
#include <fsfw/ipc/MutexFactory.h>
#include <fsfw/ipc/MutexGuard.h>
#include <fsfw/tasks/SemaphoreFactory.h>
#include <fsfw_hal/linux/uart/UartCookie.h>
2022-04-09 14:43:06 +02:00
#include <linux/devices/ScexDleParser.h>
2022-04-08 19:12:21 +02:00
#include <unistd.h> // write(), read(), close()
#include <cerrno> // Error integer and strerror() function
2022-03-25 17:48:38 +01:00
#include <iostream>
2022-04-08 19:12:21 +02:00
#include "OBSWConfig.h"
2022-03-25 17:48:38 +01:00
2022-04-08 19:12:21 +02:00
ScexUartReader::ScexUartReader(object_id_t objectId)
2022-04-09 14:43:06 +02:00
: SystemObject(objectId),
decodeRingBuf(4096, true),
ipcRingBuf(200 * 2048, true),
ipcQueue(200),
dleParser(decodeRingBuf, dleEncoder, {encodedBuf.data(), encodedBuf.size()},
{decodedBuf.data(), decodedBuf.size()}, &foundDlePacketHandler, this) {
2022-04-08 19:12:21 +02:00
semaphore = SemaphoreFactory::instance()->createBinarySemaphore();
semaphore->acquire();
lock = MutexFactory::instance()->createMutex();
2022-03-25 17:48:38 +01:00
}
ReturnValue_t ScexUartReader::performOperation(uint8_t operationCode) {
2022-04-08 19:12:21 +02:00
lock->lockMutex();
state = States::IDLE;
lock->unlockMutex();
while (true) {
semaphore->acquire();
2022-04-13 17:43:16 +02:00
sif::info << "task was started" << std::endl;
2022-04-08 19:12:21 +02:00
int bytesRead = 0;
2022-04-13 17:43:16 +02:00
while(true) {
2022-04-08 19:12:21 +02:00
bytesRead = read(serialPort, reinterpret_cast<void *>(recBuf.data()),
static_cast<unsigned int>(recBuf.size()));
if (bytesRead == 0) {
MutexGuard mg(lock);
States currentState = state;
if (currentState == States::FINISH) {
state = States::IDLE;
break;
}
} else if (bytesRead < 0) {
sif::warning << "ScexUartReader::performOperation: read call failed with error [" << errno
<< ", " << strerror(errno) << "]" << std::endl;
break;
} else if (bytesRead >= static_cast<int>(recBuf.size())) {
sif::error << "ScexUartReader::performOperation: Receive buffer too small" << std::endl;
} else if (bytesRead > 0) {
2022-04-09 14:43:06 +02:00
ReturnValue_t result = dleParser.passData(recBuf.data(), bytesRead);
2022-04-08 19:12:21 +02:00
if (debugMode) {
sif::info << "Received " << bytesRead
<< " bytes from the Solar Cell Experiment:" << std::endl;
}
2022-04-13 17:43:16 +02:00
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "ScexUartReader::performOperation: Passing data to DLE parser failed"
<< std::endl;
}
2022-04-08 19:12:21 +02:00
}
2022-04-13 17:43:16 +02:00
};
2022-04-08 19:12:21 +02:00
// task block comes here
std::cout << "done" << std::endl;
}
return RETURN_OK;
}
ReturnValue_t ScexUartReader::initializeInterface(CookieIF *cookie) {
UartCookie *uartCookie = dynamic_cast<UartCookie *>(cookie);
2022-04-13 17:43:16 +02:00
if (uartCookie == nullptr) {
2022-04-08 19:12:21 +02:00
return RETURN_FAILED;
}
std::string devname = uartCookie->getDeviceFile();
2022-04-13 17:43:16 +02:00
sif::info << devname << std::endl;
2022-04-08 19:12:21 +02:00
/* Get file descriptor */
serialPort = open(devname.c_str(), O_RDWR);
if (serialPort < 0) {
sif::warning << "open call failed with error [" << errno << ", " << strerror(errno)
<< std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
// Setting up UART parameters
tty.c_cflag &= ~PARENB; // Clear parity bit
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
tty.c_cflag &= ~CSIZE; // Clear all the size bits
tty.c_cflag |= CS8; // 8 bits per byte
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
// Use non-canonical mode and clear echo flag
tty.c_lflag &= ~(ICANON | ECHO);
2022-04-09 14:43:06 +02:00
// Non-blocking mode, use polling
2022-04-13 17:43:16 +02:00
tty.c_cc[VTIME] = 10; // Read for up to 1 seconds
tty.c_cc[VMIN] = 255; // Read as much as there is available
2022-04-08 19:12:21 +02:00
// Q7S UART Lite has fixed baud rate. For other linux systems, set baud rate here.
#if !defined(XIPHOS_Q7S)
if (cfsetispeed(&tty, B57600) != 0) {
sif::warning << "ScexUartReader::initializeInterface: Setting baud rate failed" << std::endl;
}
#endif
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
sif::warning << "tcsetattr call failed with error [" << errno << ", " << strerror(errno)
<< std::endl;
}
// Flush received and unread data
tcflush(serialPort, TCIFLUSH);
return RETURN_OK;
}
ReturnValue_t ScexUartReader::sendMessage(CookieIF *cookie, const uint8_t *sendData,
size_t sendLen) {
lock->lockMutex();
if (state == States::NOT_READY or state == States::RUNNING) {
lock->unlockMutex();
return HasReturnvaluesIF::RETURN_FAILED;
}
state = States::RUNNING;
lock->unlockMutex();
size_t encodedLen = 0;
ReturnValue_t result =
dleEncoder.encode(sendData, sendLen, cmdbuf.data(), cmdbuf.size(), &encodedLen, true);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "UartTestClass::scexInit: Encoding failed" << std::endl;
return RETURN_FAILED;
}
size_t bytesWritten = write(serialPort, cmdbuf.data(), encodedLen);
if (bytesWritten != encodedLen) {
sif::warning << "Sending ping command to solar experiment failed" << std::endl;
return RETURN_FAILED;
}
result = semaphore->release();
if (result != HasReturnvaluesIF::RETURN_OK) {
std::cout << "ScexUartReader::sendMessag: Releasing semaphore failed" << std::endl;
}
return RETURN_OK;
}
ReturnValue_t ScexUartReader::getSendSuccess(CookieIF *cookie) { return RETURN_OK; }
ReturnValue_t ScexUartReader::requestReceiveMessage(CookieIF *cookie, size_t requestLen) {
return RETURN_OK;
}
void ScexUartReader::setDebugMode(bool enable) { this->debugMode = enable; }
ReturnValue_t ScexUartReader::finish() {
MutexGuard mg(lock);
if (state == States::IDLE) {
return HasReturnvaluesIF::RETURN_FAILED;
}
state = States::FINISH;
return RETURN_OK;
}
2022-04-09 15:38:09 +02:00
void ScexUartReader::foundDlePacketHandler(const DleParser::Context &ctx) {
ScexUartReader *obj = reinterpret_cast<ScexUartReader *>(ctx.userArgs);
if (ctx.getType() == DleParser::ContextType::PACKET_FOUND) {
obj->handleFoundDlePacket(ctx.decodedPacket.first, ctx.decodedPacket.second);
} else {
DleParser::defaultErrorHandler(ctx.error.first, ctx.error.second);
}
2022-04-09 14:43:06 +02:00
}
void ScexUartReader::handleFoundDlePacket(uint8_t *packet, size_t len) {
// TODO: insert data into IPC ring buffer here
sif::info << "Detected DLE encoded packet with decoded size " << len << std::endl;
2022-04-13 17:43:16 +02:00
//MutexGuard mg(lock);
ReturnValue_t result = ipcQueue.insert(len);
if(result != RETURN_OK){
sif::warning<< "IPCQueue error" << std::endl;
}
result = ipcRingBuf.writeData(packet, len);
if(result != RETURN_OK){
sif::warning<< "IPCRingBuf error" << std::endl;
}
sif::info << "DLE handler done" << std::endl;
2022-04-09 14:43:06 +02:00
}
2022-04-08 19:12:21 +02:00
ReturnValue_t ScexUartReader::readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
size_t *size) {
2022-04-13 17:43:16 +02:00
//MutexGuard mg(lock);
2022-04-09 14:43:06 +02:00
if (ipcQueue.empty()) {
2022-04-08 19:12:21 +02:00
*size = 0;
return RETURN_OK;
}
2022-04-09 14:43:06 +02:00
*size = ipcQueue.pop();
2022-04-08 19:12:21 +02:00
*buffer = ipcBuffer.data();
2022-04-09 14:43:06 +02:00
ReturnValue_t result = ipcRingBuf.readData(ipcBuffer.data(), *size, true);
2022-04-08 19:12:21 +02:00
if (result != RETURN_OK) {
sif::warning << "ScexUartReader::readReceivedMessage: Reading RingBuffer failed" << std::endl;
}
return RETURN_OK;
2022-03-25 17:48:38 +01:00
}