2021-07-13 19:19:25 +02:00
|
|
|
#include <errno.h>
|
2022-02-02 10:29:30 +01:00
|
|
|
#include <fcntl.h>
|
2022-11-10 17:31:11 +01:00
|
|
|
#include <fsfw_hal/linux/serial/SerialComIF.h>
|
2021-07-13 19:19:25 +02:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "fsfw/FSFW.h"
|
|
|
|
#include "fsfw/serviceinterface.h"
|
|
|
|
#include "fsfw_hal/linux/utility.h"
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
SerialComIF::SerialComIF(object_id_t objectId) : SystemObject(objectId) {}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
SerialComIF::~SerialComIF() {}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::initializeInterface(CookieIF* cookie) {
|
2022-02-02 10:29:30 +01:00
|
|
|
std::string deviceFile;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
if (cookie == nullptr) {
|
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
|
|
|
if (uartCookie == nullptr) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "UartComIF::initializeInterface: Invalid UART Cookie!" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceFile = uartCookie->getDeviceFile();
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-10-26 17:06:24 +02:00
|
|
|
auto uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
|
|
|
int fileDescriptor = configureUartPort(uartCookie);
|
|
|
|
if (fileDescriptor < 0) {
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
size_t maxReplyLen = uartCookie->getMaxReplyLen();
|
|
|
|
UartElements uartElements = {fileDescriptor, std::vector<uint8_t>(maxReplyLen), 0};
|
|
|
|
auto status = uartDeviceMap.emplace(deviceFile, uartElements);
|
|
|
|
if (status.second == false) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::initializeInterface: Failed to insert device " << deviceFile
|
|
|
|
<< "to UART device map" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
} else {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::initializeInterface: UART device " << deviceFile
|
|
|
|
<< " already in use" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
int SerialComIF::configureUartPort(UartCookie* uartCookie) {
|
2022-02-02 10:29:30 +01:00
|
|
|
struct termios options = {};
|
|
|
|
|
|
|
|
std::string deviceFile = uartCookie->getDeviceFile();
|
|
|
|
int flags = O_RDWR;
|
|
|
|
if (uartCookie->getUartMode() == UartModes::CANONICAL) {
|
|
|
|
// In non-canonical mode, don't specify O_NONBLOCK because these properties will be
|
|
|
|
// controlled by the VTIME and VMIN parameters and O_NONBLOCK would override this
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
}
|
|
|
|
int fd = open(deviceFile.c_str(), flags);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::configureUartPort: Failed to open uart " << deviceFile
|
|
|
|
<< "with error code " << errno << strerror(errno) << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return fd;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
/* Read in existing settings */
|
|
|
|
if (tcgetattr(fd, &options) != 0) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::configureUartPort: Error " << errno
|
|
|
|
<< "from tcgetattr: " << strerror(errno) << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return fd;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-11-04 11:08:23 +01:00
|
|
|
uart::setParity(options, uartCookie->getParity());
|
2022-02-02 10:29:30 +01:00
|
|
|
setStopBitOptions(&options, uartCookie);
|
|
|
|
setDatasizeOptions(&options, uartCookie);
|
|
|
|
setFixedOptions(&options);
|
2022-10-06 11:14:00 +02:00
|
|
|
uart::setMode(options, uartCookie->getUartMode());
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartCookie->getInputShouldBeFlushed()) {
|
|
|
|
tcflush(fd, TCIFLUSH);
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
/* Sets uart to non-blocking mode. Read returns immediately when there are no data available */
|
|
|
|
options.c_cc[VTIME] = 0;
|
|
|
|
options.c_cc[VMIN] = 0;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-10-06 11:14:00 +02:00
|
|
|
uart::setBaudrate(options, uartCookie->getBaudrate());
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
/* Save option settings */
|
|
|
|
if (tcsetattr(fd, TCSANOW, &options) != 0) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::configureUartPort: Failed to set options with error " << errno
|
|
|
|
<< ": " << strerror(errno);
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2021-07-13 19:19:25 +02:00
|
|
|
return fd;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
return fd;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
void SerialComIF::setStopBitOptions(struct termios* options, UartCookie* uartCookie) {
|
2022-02-02 10:29:30 +01:00
|
|
|
/* Clear stop field. Sets stop bit to one bit */
|
|
|
|
options->c_cflag &= ~CSTOPB;
|
|
|
|
switch (uartCookie->getStopBits()) {
|
2021-07-13 19:19:25 +02:00
|
|
|
case StopBits::TWO_STOP_BITS:
|
2022-02-02 10:29:30 +01:00
|
|
|
options->c_cflag |= CSTOPB;
|
|
|
|
break;
|
2021-07-13 19:19:25 +02:00
|
|
|
default:
|
2022-02-02 10:29:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
void SerialComIF::setDatasizeOptions(struct termios* options, UartCookie* uartCookie) {
|
2022-02-02 10:29:30 +01:00
|
|
|
/* Clear size bits */
|
|
|
|
options->c_cflag &= ~CSIZE;
|
|
|
|
switch (uartCookie->getBitsPerWord()) {
|
2022-03-24 15:44:32 +01:00
|
|
|
case BitsPerWord::BITS_5:
|
2022-02-02 10:29:30 +01:00
|
|
|
options->c_cflag |= CS5;
|
|
|
|
break;
|
2022-03-24 15:44:32 +01:00
|
|
|
case BitsPerWord::BITS_6:
|
2022-02-02 10:29:30 +01:00
|
|
|
options->c_cflag |= CS6;
|
|
|
|
break;
|
2022-03-24 15:44:32 +01:00
|
|
|
case BitsPerWord::BITS_7:
|
2022-02-02 10:29:30 +01:00
|
|
|
options->c_cflag |= CS7;
|
|
|
|
break;
|
2022-03-24 15:44:32 +01:00
|
|
|
case BitsPerWord::BITS_8:
|
2022-02-02 10:29:30 +01:00
|
|
|
options->c_cflag |= CS8;
|
|
|
|
break;
|
2021-07-13 19:19:25 +02:00
|
|
|
default:
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::setDatasizeOptions: Invalid size specified" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
void SerialComIF::setFixedOptions(struct termios* options) {
|
2022-02-02 10:29:30 +01:00
|
|
|
/* Disable RTS/CTS hardware flow control */
|
|
|
|
options->c_cflag &= ~CRTSCTS;
|
|
|
|
/* Turn on READ & ignore ctrl lines (CLOCAL = 1) */
|
|
|
|
options->c_cflag |= CREAD | CLOCAL;
|
|
|
|
/* Disable echo */
|
|
|
|
options->c_lflag &= ~ECHO;
|
|
|
|
/* Disable erasure */
|
|
|
|
options->c_lflag &= ~ECHOE;
|
|
|
|
/* Disable new-line echo */
|
|
|
|
options->c_lflag &= ~ECHONL;
|
|
|
|
/* Disable interpretation of INTR, QUIT and SUSP */
|
|
|
|
options->c_lflag &= ~ISIG;
|
|
|
|
/* Turn off s/w flow ctrl */
|
|
|
|
options->c_iflag &= ~(IXON | IXOFF | IXANY);
|
|
|
|
/* Disable any special handling of received bytes */
|
|
|
|
options->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
|
|
|
|
/* Prevent special interpretation of output bytes (e.g. newline chars) */
|
|
|
|
options->c_oflag &= ~OPOST;
|
|
|
|
/* Prevent conversion of newline to carriage return/line feed */
|
|
|
|
options->c_oflag &= ~ONLCR;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) {
|
2022-02-02 10:29:30 +01:00
|
|
|
int fd = 0;
|
|
|
|
std::string deviceFile;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
if (sendLen == 0) {
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
if (sendData == nullptr) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::sendMessage: Send data is nullptr" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-09-08 16:10:18 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
|
|
|
if (uartCookie == nullptr) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::sendMessasge: Invalid UART Cookie!" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
deviceFile = uartCookie->getDeviceFile();
|
2022-10-26 17:06:24 +02:00
|
|
|
auto uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::debug << "UartComIF::sendMessage: Device file " << deviceFile << "not in UART map"
|
|
|
|
<< std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
fd = uartDeviceMapIter->second.fileDescriptor;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "UartComIF::sendMessage: Failed to send data with error code " << errno
|
|
|
|
<< ": Error description: " << strerror(errno) << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::getSendSuccess(CookieIF* cookie) { return returnvalue::OK; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
|
2022-02-02 10:29:30 +01:00
|
|
|
std::string deviceFile;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
|
|
|
if (uartCookie == nullptr) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::debug << "UartComIF::requestReceiveMessage: Invalid Uart Cookie!" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
UartModes uartMode = uartCookie->getUartMode();
|
|
|
|
deviceFile = uartCookie->getDeviceFile();
|
2022-10-26 17:06:24 +02:00
|
|
|
auto uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartMode == UartModes::NON_CANONICAL and requestLen == 0) {
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::debug << "UartComIF::requestReceiveMessage: Device file " << deviceFile
|
|
|
|
<< " not in uart map" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uartMode == UartModes::CANONICAL) {
|
|
|
|
return handleCanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
|
|
|
|
} else if (uartMode == UartModes::NON_CANONICAL) {
|
|
|
|
return handleNoncanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
|
|
|
|
} else {
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::handleCanonicalRead(UartCookie& uartCookie,
|
|
|
|
UartDeviceMap::iterator& iter, size_t requestLen) {
|
2022-08-16 01:08:26 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
uint8_t maxReadCycles = uartCookie.getReadCycles();
|
|
|
|
uint8_t currentReadCycles = 0;
|
|
|
|
int bytesRead = 0;
|
|
|
|
size_t currentBytesRead = 0;
|
|
|
|
size_t maxReplySize = uartCookie.getMaxReplyLen();
|
|
|
|
int fd = iter->second.fileDescriptor;
|
|
|
|
auto bufferPtr = iter->second.replyBuffer.data();
|
|
|
|
iter->second.replyLen = 0;
|
|
|
|
do {
|
|
|
|
size_t allowedReadSize = 0;
|
|
|
|
if (currentBytesRead >= maxReplySize) {
|
|
|
|
// Overflow risk. Emit warning, trigger event and break. If this happens,
|
|
|
|
// the reception buffer is not large enough or data is not polled often enough.
|
2021-09-08 16:10:18 +02:00
|
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
2021-07-13 19:19:25 +02:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
|
|
|
|
<< std::endl;
|
2021-07-13 19:19:25 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning(
|
|
|
|
"UartComIF::requestReceiveMessage: "
|
|
|
|
"Next read would cause overflow!");
|
2021-07-13 19:19:25 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
result = UART_RX_BUFFER_TOO_SMALL;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
allowedReadSize = maxReplySize - currentBytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesRead = read(fd, bufferPtr, allowedReadSize);
|
|
|
|
if (bytesRead < 0) {
|
|
|
|
// EAGAIN: No data available in non-blocking mode
|
|
|
|
if (errno != EAGAIN) {
|
2021-09-08 16:10:18 +02:00
|
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::handleCanonicalRead: read failed with code" << errno << ": "
|
|
|
|
<< strerror(errno) << std::endl;
|
2021-09-08 16:10:18 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning("UartComIF::handleCanonicalRead: read failed with code %d: %s\n", errno,
|
|
|
|
strerror(errno));
|
2021-09-08 16:10:18 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (bytesRead > 0) {
|
|
|
|
iter->second.replyLen += bytesRead;
|
|
|
|
bufferPtr += bytesRead;
|
|
|
|
currentBytesRead += bytesRead;
|
|
|
|
}
|
|
|
|
currentReadCycles++;
|
|
|
|
} while (bytesRead > 0 and currentReadCycles < maxReadCycles);
|
|
|
|
return result;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::handleNoncanonicalRead(UartCookie& uartCookie,
|
|
|
|
UartDeviceMap::iterator& iter,
|
|
|
|
size_t requestLen) {
|
2022-02-02 10:29:30 +01:00
|
|
|
int fd = iter->second.fileDescriptor;
|
|
|
|
auto bufferPtr = iter->second.replyBuffer.data();
|
|
|
|
// Size check to prevent buffer overflow
|
|
|
|
if (requestLen > uartCookie.getMaxReplyLen()) {
|
2022-02-23 18:23:22 +01:00
|
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
2021-07-13 19:19:25 +02:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
|
|
|
|
<< std::endl;
|
2021-07-13 19:19:25 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning(
|
|
|
|
"UartComIF::requestReceiveMessage: "
|
|
|
|
"Next read would cause overflow!");
|
2021-07-13 19:19:25 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return UART_RX_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
int bytesRead = read(fd, bufferPtr, requestLen);
|
|
|
|
if (bytesRead < 0) {
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
} else if (bytesRead != static_cast<int>(requestLen)) {
|
|
|
|
if (uartCookie.isReplySizeFixed()) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::requestReceiveMessage: Only read " << bytesRead << " of "
|
|
|
|
<< requestLen << " bytes" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
iter->second.replyLen = bytesRead;
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
|
2022-02-02 10:29:30 +01:00
|
|
|
std::string deviceFile;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
|
|
|
if (uartCookie == nullptr) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::debug << "UartComIF::readReceivedMessage: Invalid uart cookie!" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
deviceFile = uartCookie->getDeviceFile();
|
2022-10-26 17:06:24 +02:00
|
|
|
auto uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::debug << "UartComIF::readReceivedMessage: Device file " << deviceFile << " not in uart map"
|
|
|
|
<< std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
*buffer = uartDeviceMapIter->second.replyBuffer.data();
|
|
|
|
*size = uartDeviceMapIter->second.replyLen;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
/* Length is reset to 0 to prevent reading the same data twice */
|
|
|
|
uartDeviceMapIter->second.replyLen = 0;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::flushUartRxBuffer(CookieIF* cookie) {
|
2022-02-02 10:29:30 +01:00
|
|
|
std::string deviceFile;
|
|
|
|
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
|
|
|
if (uartCookie == nullptr) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::flushUartRxBuffer: Invalid uart cookie!" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
deviceFile = uartCookie->getDeviceFile();
|
2022-10-26 17:06:24 +02:00
|
|
|
auto uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartDeviceMapIter != uartDeviceMap.end()) {
|
|
|
|
int fd = uartDeviceMapIter->second.fileDescriptor;
|
|
|
|
tcflush(fd, TCIFLUSH);
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-08-06 11:23:31 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::flushUartTxBuffer(CookieIF* cookie) {
|
2022-02-02 10:29:30 +01:00
|
|
|
std::string deviceFile;
|
|
|
|
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
|
|
|
if (uartCookie == nullptr) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::flushUartTxBuffer: Invalid uart cookie!" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
deviceFile = uartCookie->getDeviceFile();
|
2022-10-26 17:06:24 +02:00
|
|
|
auto uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartDeviceMapIter != uartDeviceMap.end()) {
|
|
|
|
int fd = uartDeviceMapIter->second.fileDescriptor;
|
|
|
|
tcflush(fd, TCOFLUSH);
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-08-06 11:23:31 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:31:11 +01:00
|
|
|
ReturnValue_t SerialComIF::flushUartTxAndRxBuf(CookieIF* cookie) {
|
2022-02-02 10:29:30 +01:00
|
|
|
std::string deviceFile;
|
|
|
|
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
|
|
|
if (uartCookie == nullptr) {
|
2022-01-26 12:11:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "UartComIF::flushUartTxAndRxBuf: Invalid uart cookie!" << std::endl;
|
2022-01-26 12:11:52 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
deviceFile = uartCookie->getDeviceFile();
|
2022-10-26 17:06:24 +02:00
|
|
|
auto uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uartDeviceMapIter != uartDeviceMap.end()) {
|
|
|
|
int fd = uartDeviceMapIter->second.fileDescriptor;
|
|
|
|
tcflush(fd, TCIOFLUSH);
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-08-06 11:23:31 +02:00
|
|
|
}
|