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>
|
2022-04-15 01:39:47 +02:00
|
|
|
#include <fsfw/tasks/TaskFactory.h>
|
2022-11-10 17:32:11 +01:00
|
|
|
#include <fsfw_hal/linux/serial/SerialCookie.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-08-29 16:05:05 +02:00
|
|
|
using namespace returnvalue;
|
|
|
|
|
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()},
|
2022-10-04 20:52:11 +02:00
|
|
|
{decodedBuf.data(), decodedBuf.size()}) {
|
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();
|
|
|
|
int bytesRead = 0;
|
2022-10-04 23:04:50 +02:00
|
|
|
// debugMode = true;
|
2022-04-15 01:39:47 +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) {
|
2022-10-04 20:52:11 +02:00
|
|
|
{
|
|
|
|
MutexGuard mg(lock);
|
|
|
|
if (state == States::FINISH) {
|
2022-10-04 21:17:54 +02:00
|
|
|
dleParser.reset();
|
|
|
|
// Flush received and unread data
|
|
|
|
tcflush(serialPort, TCIOFLUSH);
|
2022-10-04 20:52:11 +02:00
|
|
|
state = States::IDLE;
|
|
|
|
break;
|
|
|
|
}
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
2022-10-04 18:38:53 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-10-06 11:15:55 +02:00
|
|
|
// Can be used to read frame, parity and overrun errors
|
|
|
|
// serial_icounter_struct icounter{};
|
|
|
|
// uart::readCountersAndErrors(serialPort, icounter);
|
2022-10-04 18:38:53 +02:00
|
|
|
while (result != DleParser::NO_PACKET_FOUND) {
|
2022-10-04 23:04:50 +02:00
|
|
|
result = tryDleParsing();
|
2022-10-04 18:38:53 +02:00
|
|
|
}
|
2022-10-04 23:04:50 +02:00
|
|
|
|
2022-11-09 13:07:20 +01:00
|
|
|
TaskFactory::delayTask(150);
|
2022-04-08 19:12:21 +02:00
|
|
|
} 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())) {
|
2022-04-27 18:21:32 +02:00
|
|
|
sif::error << "ScexUartReader::performOperation: Receive buffer too small for " << bytesRead
|
|
|
|
<< " bytes" << std::endl;
|
2022-04-08 19:12:21 +02:00
|
|
|
} else if (bytesRead > 0) {
|
|
|
|
if (debugMode) {
|
|
|
|
sif::info << "Received " << bytesRead
|
|
|
|
<< " bytes from the Solar Cell Experiment:" << std::endl;
|
|
|
|
}
|
2022-10-04 23:04:50 +02:00
|
|
|
ReturnValue_t result = dleParser.passData(recBuf.data(), bytesRead);
|
2022-08-29 16:05:05 +02:00
|
|
|
if (result != OK) {
|
2022-04-13 17:43:16 +02:00
|
|
|
sif::warning << "ScexUartReader::performOperation: Passing data to DLE parser failed"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2022-10-04 23:04:50 +02:00
|
|
|
result = tryDleParsing();
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
2022-04-13 17:43:16 +02:00
|
|
|
};
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
2022-08-29 16:05:05 +02:00
|
|
|
return OK;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ScexUartReader::initializeInterface(CookieIF *cookie) {
|
2022-11-11 11:41:40 +01:00
|
|
|
SerialCookie *uartCookie = dynamic_cast<SerialCookie *>(cookie);
|
2022-04-13 17:43:16 +02:00
|
|
|
if (uartCookie == nullptr) {
|
2022-08-29 16:05:05 +02:00
|
|
|
return FAILED;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
std::string devname = uartCookie->getDeviceFile();
|
|
|
|
/* Get file descriptor */
|
|
|
|
serialPort = open(devname.c_str(), O_RDWR);
|
|
|
|
if (serialPort < 0) {
|
2022-06-21 16:44:14 +02:00
|
|
|
sif::warning << "ScexUartReader::initializeInterface: open call failed with error [" << errno
|
|
|
|
<< ", " << strerror(errno) << std::endl;
|
2022-08-29 16:05:05 +02:00
|
|
|
return FAILED;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
// Setting up UART parameters
|
2022-10-06 11:15:55 +02:00
|
|
|
tty.c_cflag &= ~PARENB; // Clear parity bit
|
2022-11-04 11:08:47 +01:00
|
|
|
uart::setStopbits(tty, uartCookie->getStopBits());
|
|
|
|
uart::setBitsPerWord(tty, BitsPerWord::BITS_8);
|
|
|
|
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
|
|
|
|
uart::enableRead(tty);
|
|
|
|
uart::ignoreCtrlLines(tty);
|
2022-04-08 19:12:21 +02:00
|
|
|
|
|
|
|
// 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-15 01:39:47 +02:00
|
|
|
tty.c_cc[VTIME] = 0;
|
|
|
|
tty.c_cc[VMIN] = 0;
|
2022-04-08 19:12:21 +02:00
|
|
|
|
2022-10-26 18:32:53 +02:00
|
|
|
uart::setBaudrate(tty, uartCookie->getBaudrate());
|
2022-04-08 19:12:21 +02:00
|
|
|
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
|
2022-06-21 16:44:14 +02:00
|
|
|
sif::warning << "ScexUartReader::initializeInterface: tcsetattr call failed with error ["
|
|
|
|
<< errno << ", " << strerror(errno) << std::endl;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
// Flush received and unread data
|
2022-04-15 01:39:47 +02:00
|
|
|
tcflush(serialPort, TCIOFLUSH);
|
2022-08-29 16:05:05 +02:00
|
|
|
return OK;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ScexUartReader::sendMessage(CookieIF *cookie, const uint8_t *sendData,
|
|
|
|
size_t sendLen) {
|
2022-10-04 23:04:50 +02:00
|
|
|
ReturnValue_t result;
|
2022-06-21 16:44:14 +02:00
|
|
|
if (sendData == nullptr or sendLen == 0) {
|
2022-08-29 16:05:05 +02:00
|
|
|
return FAILED;
|
2022-06-21 16:44:14 +02:00
|
|
|
}
|
2022-04-08 19:12:21 +02:00
|
|
|
lock->lockMutex();
|
|
|
|
if (state == States::NOT_READY or state == States::RUNNING) {
|
|
|
|
lock->unlockMutex();
|
2022-08-29 16:05:05 +02:00
|
|
|
return FAILED;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
2022-10-04 23:04:50 +02:00
|
|
|
tcflush(serialPort, TCIFLUSH);
|
2022-04-08 19:12:21 +02:00
|
|
|
state = States::RUNNING;
|
|
|
|
lock->unlockMutex();
|
2022-10-04 21:17:54 +02:00
|
|
|
|
2022-10-04 23:04:50 +02:00
|
|
|
result = semaphore->release();
|
|
|
|
if (result != OK) {
|
|
|
|
std::cout << "ScexUartReader::sendMessage: Releasing semaphore failed" << std::endl;
|
|
|
|
}
|
2022-04-08 19:12:21 +02:00
|
|
|
size_t encodedLen = 0;
|
2022-10-04 23:04:50 +02:00
|
|
|
result = dleEncoder.encode(sendData, sendLen, cmdbuf.data(), cmdbuf.size(), &encodedLen, true);
|
2022-08-29 16:05:05 +02:00
|
|
|
if (result != OK) {
|
2022-06-21 16:44:14 +02:00
|
|
|
sif::warning << "ScexUartReader::sendMessage: Encoding failed" << std::endl;
|
2022-08-29 16:05:05 +02:00
|
|
|
return FAILED;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
size_t bytesWritten = write(serialPort, cmdbuf.data(), encodedLen);
|
|
|
|
if (bytesWritten != encodedLen) {
|
2023-03-21 20:35:28 +01:00
|
|
|
sif::warning << "ScexUartReader::sendMessage: Sending command failed" << std::endl;
|
2022-08-29 16:05:05 +02:00
|
|
|
return FAILED;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
2022-10-04 23:04:50 +02:00
|
|
|
|
2022-08-29 16:05:05 +02:00
|
|
|
return OK;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 16:05:05 +02:00
|
|
|
ReturnValue_t ScexUartReader::getSendSuccess(CookieIF *cookie) { return OK; }
|
2022-04-08 19:12:21 +02:00
|
|
|
|
|
|
|
ReturnValue_t ScexUartReader::requestReceiveMessage(CookieIF *cookie, size_t requestLen) {
|
2022-08-29 16:05:05 +02:00
|
|
|
return OK;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScexUartReader::setDebugMode(bool enable) { this->debugMode = enable; }
|
|
|
|
|
|
|
|
ReturnValue_t ScexUartReader::finish() {
|
|
|
|
MutexGuard mg(lock);
|
|
|
|
if (state == States::IDLE) {
|
2022-08-29 16:05:05 +02:00
|
|
|
return FAILED;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
state = States::FINISH;
|
2022-08-29 16:05:05 +02:00
|
|
|
return OK;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
|
|
|
|
2022-04-09 14:43:06 +02:00
|
|
|
void ScexUartReader::handleFoundDlePacket(uint8_t *packet, size_t len) {
|
2022-04-13 17:47:24 +02:00
|
|
|
MutexGuard mg(lock);
|
2022-04-13 17:43:16 +02:00
|
|
|
ReturnValue_t result = ipcQueue.insert(len);
|
2022-08-29 16:05:05 +02:00
|
|
|
if (result != OK) {
|
2022-06-21 16:44:14 +02:00
|
|
|
sif::warning << "ScexUartReader::handleFoundDlePacket: IPCQueue error" << std::endl;
|
2022-04-13 17:43:16 +02:00
|
|
|
}
|
|
|
|
result = ipcRingBuf.writeData(packet, len);
|
2022-08-29 16:05:05 +02:00
|
|
|
if (result != OK) {
|
2022-06-21 16:44:14 +02:00
|
|
|
sif::warning << "ScexUartReader::handleFoundDlePacket: IPCRingBuf error" << std::endl;
|
2022-04-15 01:39:47 +02:00
|
|
|
}
|
2022-04-09 14:43:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 23:04:50 +02:00
|
|
|
ReturnValue_t ScexUartReader::tryDleParsing() {
|
|
|
|
size_t bytesRead = 0;
|
|
|
|
ReturnValue_t result = dleParser.parseRingBuf(bytesRead);
|
|
|
|
if (result == returnvalue::OK) {
|
|
|
|
// Packet found, advance read pointer.
|
|
|
|
auto &decodedPacket = dleParser.getContext().decodedPacket;
|
|
|
|
handleFoundDlePacket(decodedPacket.first, decodedPacket.second);
|
|
|
|
dleParser.confirmBytesRead(bytesRead);
|
|
|
|
} else if (result != DleParser::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.
|
|
|
|
dleParser.defaultErrorHandler();
|
|
|
|
dleParser.confirmBytesRead(bytesRead);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScexUartReader::reset() {
|
|
|
|
lock->lockMutex();
|
|
|
|
state = States::FINISH;
|
2022-11-09 13:07:20 +01:00
|
|
|
ipcRingBuf.clear();
|
|
|
|
while (not ipcQueue.empty()) {
|
|
|
|
ipcQueue.pop();
|
|
|
|
}
|
2022-10-04 23:04:50 +02:00
|
|
|
lock->unlockMutex();
|
|
|
|
}
|
|
|
|
|
2022-04-08 19:12:21 +02:00
|
|
|
ReturnValue_t ScexUartReader::readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
|
|
|
|
size_t *size) {
|
2022-04-13 17:47:24 +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;
|
2022-08-29 16:05:05 +02:00
|
|
|
return OK;
|
2022-04-08 19:12:21 +02:00
|
|
|
}
|
2022-04-15 01:39:47 +02:00
|
|
|
ipcQueue.retrieve(size);
|
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-08-29 16:05:05 +02:00
|
|
|
if (result != OK) {
|
2022-04-08 19:12:21 +02:00
|
|
|
sif::warning << "ScexUartReader::readReceivedMessage: Reading RingBuffer failed" << std::endl;
|
|
|
|
}
|
2022-08-29 16:05:05 +02:00
|
|
|
return OK;
|
2022-03-25 17:48:38 +01:00
|
|
|
}
|