reapply clang format
This commit is contained in:
@ -1,569 +1,557 @@
|
||||
#include "UartComIF.h"
|
||||
#include "fsfw/FSFW.h"
|
||||
|
||||
#include "fsfw_hal/linux/utility.h"
|
||||
#include "fsfw/serviceinterface.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
UartComIF::UartComIF(object_id_t objectId): SystemObject(objectId){
|
||||
}
|
||||
#include <cstring>
|
||||
|
||||
#include "fsfw/FSFW.h"
|
||||
#include "fsfw/serviceinterface.h"
|
||||
#include "fsfw_hal/linux/utility.h"
|
||||
|
||||
UartComIF::UartComIF(object_id_t objectId) : SystemObject(objectId) {}
|
||||
|
||||
UartComIF::~UartComIF() {}
|
||||
|
||||
ReturnValue_t UartComIF::initializeInterface(CookieIF* cookie) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
if (cookie == nullptr) {
|
||||
return NULLPOINTER;
|
||||
}
|
||||
|
||||
if(cookie == nullptr) {
|
||||
return NULLPOINTER;
|
||||
}
|
||||
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if (uartCookie == nullptr) {
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if (uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::error << "UartComIF::initializeInterface: Invalid UART Cookie!" << std::endl;
|
||||
sif::error << "UartComIF::initializeInterface: Invalid UART Cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
return NULLPOINTER;
|
||||
}
|
||||
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
||||
int fileDescriptor = configureUartPort(uartCookie);
|
||||
if (fileDescriptor < 0) {
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if(uartDeviceMapIter == uartDeviceMap.end()) {
|
||||
int fileDescriptor = configureUartPort(uartCookie);
|
||||
if (fileDescriptor < 0) {
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
size_t maxReplyLen = uartCookie->getMaxReplyLen();
|
||||
UartElements uartElements = {fileDescriptor, std::vector<uint8_t>(maxReplyLen), 0};
|
||||
auto status = uartDeviceMap.emplace(deviceFile, uartElements);
|
||||
if (status.second == false) {
|
||||
size_t maxReplyLen = uartCookie->getMaxReplyLen();
|
||||
UartElements uartElements = {fileDescriptor, std::vector<uint8_t>(maxReplyLen), 0};
|
||||
auto status = uartDeviceMap.emplace(deviceFile, uartElements);
|
||||
if (status.second == false) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::initializeInterface: Failed to insert device " <<
|
||||
deviceFile << "to UART device map" << std::endl;
|
||||
sif::warning << "UartComIF::initializeInterface: Failed to insert device " << deviceFile
|
||||
<< "to UART device map" << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::initializeInterface: UART device " << deviceFile <<
|
||||
" already in use" << std::endl;
|
||||
sif::warning << "UartComIF::initializeInterface: UART device " << deviceFile
|
||||
<< " already in use" << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
return RETURN_OK;
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
int UartComIF::configureUartPort(UartCookie* uartCookie) {
|
||||
struct termios options = {};
|
||||
|
||||
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);
|
||||
|
||||
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) {
|
||||
if (fd < 0) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::configureUartPort: Failed to open uart " << deviceFile <<
|
||||
"with error code " << errno << strerror(errno) << std::endl;
|
||||
sif::warning << "UartComIF::configureUartPort: Failed to open uart " << deviceFile
|
||||
<< "with error code " << errno << strerror(errno) << std::endl;
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* Read in existing settings */
|
||||
if(tcgetattr(fd, &options) != 0) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::configureUartPort: Error " << errno << "from tcgetattr: "
|
||||
<< strerror(errno) << std::endl;
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
|
||||
setParityOptions(&options, uartCookie);
|
||||
setStopBitOptions(&options, uartCookie);
|
||||
setDatasizeOptions(&options, uartCookie);
|
||||
setFixedOptions(&options);
|
||||
setUartMode(&options, *uartCookie);
|
||||
if(uartCookie->getInputShouldBeFlushed()) {
|
||||
tcflush(fd, TCIFLUSH);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
configureBaudrate(&options, uartCookie);
|
||||
|
||||
/* Save option settings */
|
||||
if (tcsetattr(fd, TCSANOW, &options) != 0) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::configureUartPort: Failed to set options with error " <<
|
||||
errno << ": " << strerror(errno);
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* Read in existing settings */
|
||||
if (tcgetattr(fd, &options) != 0) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::configureUartPort: Error " << errno
|
||||
<< "from tcgetattr: " << strerror(errno) << std::endl;
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
|
||||
setParityOptions(&options, uartCookie);
|
||||
setStopBitOptions(&options, uartCookie);
|
||||
setDatasizeOptions(&options, uartCookie);
|
||||
setFixedOptions(&options);
|
||||
setUartMode(&options, *uartCookie);
|
||||
if (uartCookie->getInputShouldBeFlushed()) {
|
||||
tcflush(fd, TCIFLUSH);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
configureBaudrate(&options, uartCookie);
|
||||
|
||||
/* Save option settings */
|
||||
if (tcsetattr(fd, TCSANOW, &options) != 0) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::configureUartPort: Failed to set options with error " << errno
|
||||
<< ": " << strerror(errno);
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
void UartComIF::setParityOptions(struct termios* options, UartCookie* uartCookie) {
|
||||
/* Clear parity bit */
|
||||
options->c_cflag &= ~PARENB;
|
||||
switch (uartCookie->getParity()) {
|
||||
/* Clear parity bit */
|
||||
options->c_cflag &= ~PARENB;
|
||||
switch (uartCookie->getParity()) {
|
||||
case Parity::EVEN:
|
||||
options->c_cflag |= PARENB;
|
||||
options->c_cflag &= ~PARODD;
|
||||
break;
|
||||
options->c_cflag |= PARENB;
|
||||
options->c_cflag &= ~PARODD;
|
||||
break;
|
||||
case Parity::ODD:
|
||||
options->c_cflag |= PARENB;
|
||||
options->c_cflag |= PARODD;
|
||||
break;
|
||||
options->c_cflag |= PARENB;
|
||||
options->c_cflag |= PARODD;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void UartComIF::setStopBitOptions(struct termios* options, UartCookie* uartCookie) {
|
||||
/* Clear stop field. Sets stop bit to one bit */
|
||||
options->c_cflag &= ~CSTOPB;
|
||||
switch (uartCookie->getStopBits()) {
|
||||
/* Clear stop field. Sets stop bit to one bit */
|
||||
options->c_cflag &= ~CSTOPB;
|
||||
switch (uartCookie->getStopBits()) {
|
||||
case StopBits::TWO_STOP_BITS:
|
||||
options->c_cflag |= CSTOPB;
|
||||
break;
|
||||
options->c_cflag |= CSTOPB;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void UartComIF::setDatasizeOptions(struct termios* options, UartCookie* uartCookie) {
|
||||
/* Clear size bits */
|
||||
options->c_cflag &= ~CSIZE;
|
||||
switch (uartCookie->getBitsPerWord()) {
|
||||
/* Clear size bits */
|
||||
options->c_cflag &= ~CSIZE;
|
||||
switch (uartCookie->getBitsPerWord()) {
|
||||
case 5:
|
||||
options->c_cflag |= CS5;
|
||||
break;
|
||||
options->c_cflag |= CS5;
|
||||
break;
|
||||
case 6:
|
||||
options->c_cflag |= CS6;
|
||||
break;
|
||||
options->c_cflag |= CS6;
|
||||
break;
|
||||
case 7:
|
||||
options->c_cflag |= CS7;
|
||||
break;
|
||||
options->c_cflag |= CS7;
|
||||
break;
|
||||
case 8:
|
||||
options->c_cflag |= CS8;
|
||||
break;
|
||||
options->c_cflag |= CS8;
|
||||
break;
|
||||
default:
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::setDatasizeOptions: Invalid size specified" << std::endl;
|
||||
sif::warning << "UartComIF::setDatasizeOptions: Invalid size specified" << std::endl;
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void UartComIF::setFixedOptions(struct termios* options) {
|
||||
/* 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;
|
||||
/* 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;
|
||||
}
|
||||
|
||||
void UartComIF::configureBaudrate(struct termios* options, UartCookie* uartCookie) {
|
||||
switch (uartCookie->getBaudrate()) {
|
||||
switch (uartCookie->getBaudrate()) {
|
||||
case 50:
|
||||
cfsetispeed(options, B50);
|
||||
cfsetospeed(options, B50);
|
||||
break;
|
||||
cfsetispeed(options, B50);
|
||||
cfsetospeed(options, B50);
|
||||
break;
|
||||
case 75:
|
||||
cfsetispeed(options, B75);
|
||||
cfsetospeed(options, B75);
|
||||
break;
|
||||
cfsetispeed(options, B75);
|
||||
cfsetospeed(options, B75);
|
||||
break;
|
||||
case 110:
|
||||
cfsetispeed(options, B110);
|
||||
cfsetospeed(options, B110);
|
||||
break;
|
||||
cfsetispeed(options, B110);
|
||||
cfsetospeed(options, B110);
|
||||
break;
|
||||
case 134:
|
||||
cfsetispeed(options, B134);
|
||||
cfsetospeed(options, B134);
|
||||
break;
|
||||
cfsetispeed(options, B134);
|
||||
cfsetospeed(options, B134);
|
||||
break;
|
||||
case 150:
|
||||
cfsetispeed(options, B150);
|
||||
cfsetospeed(options, B150);
|
||||
break;
|
||||
cfsetispeed(options, B150);
|
||||
cfsetospeed(options, B150);
|
||||
break;
|
||||
case 200:
|
||||
cfsetispeed(options, B200);
|
||||
cfsetospeed(options, B200);
|
||||
break;
|
||||
cfsetispeed(options, B200);
|
||||
cfsetospeed(options, B200);
|
||||
break;
|
||||
case 300:
|
||||
cfsetispeed(options, B300);
|
||||
cfsetospeed(options, B300);
|
||||
break;
|
||||
cfsetispeed(options, B300);
|
||||
cfsetospeed(options, B300);
|
||||
break;
|
||||
case 600:
|
||||
cfsetispeed(options, B600);
|
||||
cfsetospeed(options, B600);
|
||||
break;
|
||||
cfsetispeed(options, B600);
|
||||
cfsetospeed(options, B600);
|
||||
break;
|
||||
case 1200:
|
||||
cfsetispeed(options, B1200);
|
||||
cfsetospeed(options, B1200);
|
||||
break;
|
||||
cfsetispeed(options, B1200);
|
||||
cfsetospeed(options, B1200);
|
||||
break;
|
||||
case 1800:
|
||||
cfsetispeed(options, B1800);
|
||||
cfsetospeed(options, B1800);
|
||||
break;
|
||||
cfsetispeed(options, B1800);
|
||||
cfsetospeed(options, B1800);
|
||||
break;
|
||||
case 2400:
|
||||
cfsetispeed(options, B2400);
|
||||
cfsetospeed(options, B2400);
|
||||
break;
|
||||
cfsetispeed(options, B2400);
|
||||
cfsetospeed(options, B2400);
|
||||
break;
|
||||
case 4800:
|
||||
cfsetispeed(options, B4800);
|
||||
cfsetospeed(options, B4800);
|
||||
break;
|
||||
cfsetispeed(options, B4800);
|
||||
cfsetospeed(options, B4800);
|
||||
break;
|
||||
case 9600:
|
||||
cfsetispeed(options, B9600);
|
||||
cfsetospeed(options, B9600);
|
||||
break;
|
||||
cfsetispeed(options, B9600);
|
||||
cfsetospeed(options, B9600);
|
||||
break;
|
||||
case 19200:
|
||||
cfsetispeed(options, B19200);
|
||||
cfsetospeed(options, B19200);
|
||||
break;
|
||||
cfsetispeed(options, B19200);
|
||||
cfsetospeed(options, B19200);
|
||||
break;
|
||||
case 38400:
|
||||
cfsetispeed(options, B38400);
|
||||
cfsetospeed(options, B38400);
|
||||
break;
|
||||
cfsetispeed(options, B38400);
|
||||
cfsetospeed(options, B38400);
|
||||
break;
|
||||
case 57600:
|
||||
cfsetispeed(options, B57600);
|
||||
cfsetospeed(options, B57600);
|
||||
break;
|
||||
cfsetispeed(options, B57600);
|
||||
cfsetospeed(options, B57600);
|
||||
break;
|
||||
case 115200:
|
||||
cfsetispeed(options, B115200);
|
||||
cfsetospeed(options, B115200);
|
||||
break;
|
||||
cfsetispeed(options, B115200);
|
||||
cfsetospeed(options, B115200);
|
||||
break;
|
||||
case 230400:
|
||||
cfsetispeed(options, B230400);
|
||||
cfsetospeed(options, B230400);
|
||||
break;
|
||||
cfsetispeed(options, B230400);
|
||||
cfsetospeed(options, B230400);
|
||||
break;
|
||||
case 460800:
|
||||
cfsetispeed(options, B460800);
|
||||
cfsetospeed(options, B460800);
|
||||
break;
|
||||
cfsetispeed(options, B460800);
|
||||
cfsetospeed(options, B460800);
|
||||
break;
|
||||
default:
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::configureBaudrate: Baudrate not supported" << std::endl;
|
||||
sif::warning << "UartComIF::configureBaudrate: Baudrate not supported" << std::endl;
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::sendMessage(CookieIF *cookie,
|
||||
const uint8_t *sendData, size_t sendLen) {
|
||||
int fd = 0;
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
|
||||
if(sendLen == 0) {
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
if(sendData == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::sendMessage: Send data is nullptr" << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if(uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::sendMessasge: Invalid UART Cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::debug << "UartComIF::sendMessage: Device file " << deviceFile <<
|
||||
"not in UART map" << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
fd = uartDeviceMapIter->second.fileDescriptor;
|
||||
|
||||
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::error << "UartComIF::sendMessage: Failed to send data with error code " <<
|
||||
errno << ": Error description: " << strerror(errno) << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
ReturnValue_t UartComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) {
|
||||
int fd = 0;
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
|
||||
if (sendLen == 0) {
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
if (sendData == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::sendMessage: Send data is nullptr" << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if (uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::sendMessasge: Invalid UART Cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::debug << "UartComIF::sendMessage: Device file " << deviceFile << "not in UART map"
|
||||
<< std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
fd = uartDeviceMapIter->second.fileDescriptor;
|
||||
|
||||
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::error << "UartComIF::sendMessage: Failed to send data with error code " << errno
|
||||
<< ": Error description: " << strerror(errno) << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::getSendSuccess(CookieIF *cookie) {
|
||||
ReturnValue_t UartComIF::getSendSuccess(CookieIF* cookie) { return RETURN_OK; }
|
||||
|
||||
ReturnValue_t UartComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if (uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::debug << "UartComIF::requestReceiveMessage: Invalid Uart Cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
|
||||
UartModes uartMode = uartCookie->getUartMode();
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
|
||||
if (uartMode == UartModes::NON_CANONICAL and requestLen == 0) {
|
||||
return RETURN_OK;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLen) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if(uartCookie == nullptr) {
|
||||
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::debug << "UartComIF::requestReceiveMessage: Invalid Uart Cookie!" << std::endl;
|
||||
sif::debug << "UartComIF::requestReceiveMessage: Device file " << deviceFile
|
||||
<< " not in uart map" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
UartModes uartMode = uartCookie->getUartMode();
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
|
||||
if(uartMode == UartModes::NON_CANONICAL and requestLen == 0) {
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::debug << "UartComIF::requestReceiveMessage: Device file " << deviceFile
|
||||
<< " not in uart map" << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
if (uartMode == UartModes::CANONICAL) {
|
||||
return handleCanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
|
||||
}
|
||||
else if (uartMode == UartModes::NON_CANONICAL) {
|
||||
return handleNoncanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
|
||||
}
|
||||
else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
if (uartMode == UartModes::CANONICAL) {
|
||||
return handleCanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
|
||||
} else if (uartMode == UartModes::NON_CANONICAL) {
|
||||
return handleNoncanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
|
||||
} else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
|
||||
size_t requestLen) {
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
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.
|
||||
size_t requestLen) {
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
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.
|
||||
#if FSFW_VERBOSE_LEVEL >= 1
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
|
||||
<< std::endl;
|
||||
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
|
||||
<< std::endl;
|
||||
#else
|
||||
sif::printWarning("UartComIF::requestReceiveMessage: "
|
||||
"Next read would cause overflow!");
|
||||
sif::printWarning(
|
||||
"UartComIF::requestReceiveMessage: "
|
||||
"Next read would cause overflow!");
|
||||
#endif
|
||||
#endif
|
||||
result = UART_RX_BUFFER_TOO_SMALL;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
allowedReadSize = maxReplySize - currentBytesRead;
|
||||
}
|
||||
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) {
|
||||
bytesRead = read(fd, bufferPtr, allowedReadSize);
|
||||
if (bytesRead < 0) {
|
||||
// EAGAIN: No data available in non-blocking mode
|
||||
if (errno != EAGAIN) {
|
||||
#if FSFW_VERBOSE_LEVEL >= 1
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::handleCanonicalRead: read failed with code" <<
|
||||
errno << ": " << strerror(errno) << std::endl;
|
||||
sif::warning << "UartComIF::handleCanonicalRead: read failed with code" << errno << ": "
|
||||
<< strerror(errno) << std::endl;
|
||||
#else
|
||||
sif::printWarning("UartComIF::handleCanonicalRead: read failed with code %d: %s\n",
|
||||
errno, strerror(errno));
|
||||
sif::printWarning("UartComIF::handleCanonicalRead: read failed with code %d: %s\n", errno,
|
||||
strerror(errno));
|
||||
#endif
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
}
|
||||
else if(bytesRead > 0) {
|
||||
iter->second.replyLen += bytesRead;
|
||||
bufferPtr += bytesRead;
|
||||
currentBytesRead += bytesRead;
|
||||
}
|
||||
currentReadCycles++;
|
||||
} while(bytesRead > 0 and currentReadCycles < maxReadCycles);
|
||||
return result;
|
||||
} else if (bytesRead > 0) {
|
||||
iter->second.replyLen += bytesRead;
|
||||
bufferPtr += bytesRead;
|
||||
currentBytesRead += bytesRead;
|
||||
}
|
||||
currentReadCycles++;
|
||||
} while (bytesRead > 0 and currentReadCycles < maxReadCycles);
|
||||
return result;
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::handleNoncanonicalRead(UartCookie &uartCookie, UartDeviceMapIter &iter,
|
||||
size_t requestLen) {
|
||||
int fd = iter->second.fileDescriptor;
|
||||
auto bufferPtr = iter->second.replyBuffer.data();
|
||||
// Size check to prevent buffer overflow
|
||||
if(requestLen > uartCookie.getMaxReplyLen()) {
|
||||
ReturnValue_t UartComIF::handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
|
||||
size_t requestLen) {
|
||||
int fd = iter->second.fileDescriptor;
|
||||
auto bufferPtr = iter->second.replyBuffer.data();
|
||||
// Size check to prevent buffer overflow
|
||||
if (requestLen > uartCookie.getMaxReplyLen()) {
|
||||
#if OBSW_VERBOSE_LEVEL >= 1
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
|
||||
<< std::endl;
|
||||
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
|
||||
<< std::endl;
|
||||
#else
|
||||
sif::printWarning("UartComIF::requestReceiveMessage: "
|
||||
"Next read would cause overflow!");
|
||||
sif::printWarning(
|
||||
"UartComIF::requestReceiveMessage: "
|
||||
"Next read would cause overflow!");
|
||||
#endif
|
||||
#endif
|
||||
return UART_RX_BUFFER_TOO_SMALL;
|
||||
}
|
||||
int bytesRead = read(fd, bufferPtr, requestLen);
|
||||
if (bytesRead < 0) {
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
else if (bytesRead != static_cast<int>(requestLen)) {
|
||||
if(uartCookie.isReplySizeFixed()) {
|
||||
return UART_RX_BUFFER_TOO_SMALL;
|
||||
}
|
||||
int bytesRead = read(fd, bufferPtr, requestLen);
|
||||
if (bytesRead < 0) {
|
||||
return RETURN_FAILED;
|
||||
} else if (bytesRead != static_cast<int>(requestLen)) {
|
||||
if (uartCookie.isReplySizeFixed()) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::requestReceiveMessage: Only read " << bytesRead <<
|
||||
" of " << requestLen << " bytes" << std::endl;
|
||||
sif::warning << "UartComIF::requestReceiveMessage: Only read " << bytesRead << " of "
|
||||
<< requestLen << " bytes" << std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
iter->second.replyLen = bytesRead;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
iter->second.replyLen = bytesRead;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::readReceivedMessage(CookieIF *cookie,
|
||||
uint8_t **buffer, size_t* size) {
|
||||
ReturnValue_t UartComIF::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if(uartCookie == nullptr) {
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if (uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::debug << "UartComIF::readReceivedMessage: Invalid uart cookie!" << std::endl;
|
||||
sif::debug << "UartComIF::readReceivedMessage: Invalid uart cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
return NULLPOINTER;
|
||||
}
|
||||
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if (uartDeviceMapIter == uartDeviceMap.end()) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::debug << "UartComIF::readReceivedMessage: Device file " << deviceFile <<
|
||||
" not in uart map" << std::endl;
|
||||
sif::debug << "UartComIF::readReceivedMessage: Device file " << deviceFile << " not in uart map"
|
||||
<< std::endl;
|
||||
#endif
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
*buffer = uartDeviceMapIter->second.replyBuffer.data();
|
||||
*size = uartDeviceMapIter->second.replyLen;
|
||||
*buffer = uartDeviceMapIter->second.replyBuffer.data();
|
||||
*size = uartDeviceMapIter->second.replyLen;
|
||||
|
||||
/* Length is reset to 0 to prevent reading the same data twice */
|
||||
uartDeviceMapIter->second.replyLen = 0;
|
||||
/* Length is reset to 0 to prevent reading the same data twice */
|
||||
uartDeviceMapIter->second.replyLen = 0;
|
||||
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::flushUartRxBuffer(CookieIF* cookie) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if (uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::flushUartRxBuffer: Invalid uart cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if (uartDeviceMapIter != uartDeviceMap.end()) {
|
||||
int fd = uartDeviceMapIter->second.fileDescriptor;
|
||||
tcflush(fd, TCIFLUSH);
|
||||
return RETURN_OK;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::flushUartRxBuffer(CookieIF *cookie) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if(uartCookie == nullptr) {
|
||||
ReturnValue_t UartComIF::flushUartTxBuffer(CookieIF* cookie) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if (uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::flushUartRxBuffer: Invalid uart cookie!" << std::endl;
|
||||
sif::warning << "UartComIF::flushUartTxBuffer: Invalid uart cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if(uartDeviceMapIter != uartDeviceMap.end()) {
|
||||
int fd = uartDeviceMapIter->second.fileDescriptor;
|
||||
tcflush(fd, TCIFLUSH);
|
||||
return RETURN_OK;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
return NULLPOINTER;
|
||||
}
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if (uartDeviceMapIter != uartDeviceMap.end()) {
|
||||
int fd = uartDeviceMapIter->second.fileDescriptor;
|
||||
tcflush(fd, TCOFLUSH);
|
||||
return RETURN_OK;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::flushUartTxBuffer(CookieIF *cookie) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if(uartCookie == nullptr) {
|
||||
ReturnValue_t UartComIF::flushUartTxAndRxBuf(CookieIF* cookie) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if (uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::flushUartTxBuffer: Invalid uart cookie!" << std::endl;
|
||||
sif::warning << "UartComIF::flushUartTxAndRxBuf: Invalid uart cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if(uartDeviceMapIter != uartDeviceMap.end()) {
|
||||
int fd = uartDeviceMapIter->second.fileDescriptor;
|
||||
tcflush(fd, TCOFLUSH);
|
||||
return RETURN_OK;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
return NULLPOINTER;
|
||||
}
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if (uartDeviceMapIter != uartDeviceMap.end()) {
|
||||
int fd = uartDeviceMapIter->second.fileDescriptor;
|
||||
tcflush(fd, TCIOFLUSH);
|
||||
return RETURN_OK;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
ReturnValue_t UartComIF::flushUartTxAndRxBuf(CookieIF *cookie) {
|
||||
std::string deviceFile;
|
||||
UartDeviceMapIter uartDeviceMapIter;
|
||||
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
|
||||
if(uartCookie == nullptr) {
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::warning << "UartComIF::flushUartTxAndRxBuf: Invalid uart cookie!" << std::endl;
|
||||
#endif
|
||||
return NULLPOINTER;
|
||||
}
|
||||
deviceFile = uartCookie->getDeviceFile();
|
||||
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
|
||||
if(uartDeviceMapIter != uartDeviceMap.end()) {
|
||||
int fd = uartDeviceMapIter->second.fileDescriptor;
|
||||
tcflush(fd, TCIOFLUSH);
|
||||
return RETURN_OK;
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
|
||||
void UartComIF::setUartMode(struct termios *options, UartCookie &uartCookie) {
|
||||
UartModes uartMode = uartCookie.getUartMode();
|
||||
if(uartMode == UartModes::NON_CANONICAL) {
|
||||
/* Disable canonical mode */
|
||||
options->c_lflag &= ~ICANON;
|
||||
}
|
||||
else if(uartMode == UartModes::CANONICAL) {
|
||||
options->c_lflag |= ICANON;
|
||||
}
|
||||
void UartComIF::setUartMode(struct termios* options, UartCookie& uartCookie) {
|
||||
UartModes uartMode = uartCookie.getUartMode();
|
||||
if (uartMode == UartModes::NON_CANONICAL) {
|
||||
/* Disable canonical mode */
|
||||
options->c_lflag &= ~ICANON;
|
||||
} else if (uartMode == UartModes::CANONICAL) {
|
||||
options->c_lflag |= ICANON;
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
#ifndef BSP_Q7S_COMIF_UARTCOMIF_H_
|
||||
#define BSP_Q7S_COMIF_UARTCOMIF_H_
|
||||
|
||||
#include "UartCookie.h"
|
||||
#include <fsfw/objectmanager/SystemObject.h>
|
||||
#include <fsfw/devicehandlers/DeviceCommunicationIF.h>
|
||||
#include <fsfw/objectmanager/SystemObject.h>
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "UartCookie.h"
|
||||
|
||||
/**
|
||||
* @brief This is the communication interface to access serial ports on linux based operating
|
||||
* systems.
|
||||
@ -17,109 +18,104 @@
|
||||
*
|
||||
* @author J. Meier
|
||||
*/
|
||||
class UartComIF: public DeviceCommunicationIF, public SystemObject {
|
||||
public:
|
||||
static constexpr uint8_t uartRetvalId = CLASS_ID::HAL_UART;
|
||||
class UartComIF : public DeviceCommunicationIF, public SystemObject {
|
||||
public:
|
||||
static constexpr uint8_t uartRetvalId = CLASS_ID::HAL_UART;
|
||||
|
||||
static constexpr ReturnValue_t UART_READ_FAILURE =
|
||||
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 1);
|
||||
static constexpr ReturnValue_t UART_READ_SIZE_MISSMATCH =
|
||||
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 2);
|
||||
static constexpr ReturnValue_t UART_RX_BUFFER_TOO_SMALL =
|
||||
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 3);
|
||||
static constexpr ReturnValue_t UART_READ_FAILURE =
|
||||
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 1);
|
||||
static constexpr ReturnValue_t UART_READ_SIZE_MISSMATCH =
|
||||
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 2);
|
||||
static constexpr ReturnValue_t UART_RX_BUFFER_TOO_SMALL =
|
||||
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 3);
|
||||
|
||||
UartComIF(object_id_t objectId);
|
||||
UartComIF(object_id_t objectId);
|
||||
|
||||
virtual ~UartComIF();
|
||||
virtual ~UartComIF();
|
||||
|
||||
ReturnValue_t initializeInterface(CookieIF * cookie) override;
|
||||
ReturnValue_t sendMessage(CookieIF *cookie,const uint8_t *sendData,
|
||||
size_t sendLen) override;
|
||||
ReturnValue_t getSendSuccess(CookieIF *cookie) override;
|
||||
ReturnValue_t requestReceiveMessage(CookieIF *cookie,
|
||||
size_t requestLen) override;
|
||||
ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
|
||||
size_t *size) override;
|
||||
ReturnValue_t initializeInterface(CookieIF* cookie) override;
|
||||
ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) override;
|
||||
ReturnValue_t getSendSuccess(CookieIF* cookie) override;
|
||||
ReturnValue_t requestReceiveMessage(CookieIF* cookie, size_t requestLen) override;
|
||||
ReturnValue_t readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) override;
|
||||
|
||||
/**
|
||||
* @brief This function discards all data received but not read in the UART buffer.
|
||||
*/
|
||||
ReturnValue_t flushUartRxBuffer(CookieIF *cookie);
|
||||
/**
|
||||
* @brief This function discards all data received but not read in the UART buffer.
|
||||
*/
|
||||
ReturnValue_t flushUartRxBuffer(CookieIF* cookie);
|
||||
|
||||
/**
|
||||
* @brief This function discards all data in the transmit buffer of the UART driver.
|
||||
*/
|
||||
ReturnValue_t flushUartTxBuffer(CookieIF *cookie);
|
||||
/**
|
||||
* @brief This function discards all data in the transmit buffer of the UART driver.
|
||||
*/
|
||||
ReturnValue_t flushUartTxBuffer(CookieIF* cookie);
|
||||
|
||||
/**
|
||||
* @brief This function discards both data in the transmit and receive buffer of the UART.
|
||||
*/
|
||||
ReturnValue_t flushUartTxAndRxBuf(CookieIF *cookie);
|
||||
/**
|
||||
* @brief This function discards both data in the transmit and receive buffer of the UART.
|
||||
*/
|
||||
ReturnValue_t flushUartTxAndRxBuf(CookieIF* cookie);
|
||||
|
||||
private:
|
||||
private:
|
||||
using UartDeviceFile_t = std::string;
|
||||
|
||||
using UartDeviceFile_t = std::string;
|
||||
struct UartElements {
|
||||
int fileDescriptor;
|
||||
std::vector<uint8_t> replyBuffer;
|
||||
/** Number of bytes read will be written to this variable */
|
||||
size_t replyLen;
|
||||
};
|
||||
|
||||
struct UartElements {
|
||||
int fileDescriptor;
|
||||
std::vector<uint8_t> replyBuffer;
|
||||
/** Number of bytes read will be written to this variable */
|
||||
size_t replyLen;
|
||||
};
|
||||
using UartDeviceMap = std::unordered_map<UartDeviceFile_t, UartElements>;
|
||||
using UartDeviceMapIter = UartDeviceMap::iterator;
|
||||
|
||||
using UartDeviceMap = std::unordered_map<UartDeviceFile_t, UartElements>;
|
||||
using UartDeviceMapIter = UartDeviceMap::iterator;
|
||||
/**
|
||||
* The uart devie map stores informations of initialized uart ports.
|
||||
*/
|
||||
UartDeviceMap uartDeviceMap;
|
||||
|
||||
/**
|
||||
* The uart devie map stores informations of initialized uart ports.
|
||||
*/
|
||||
UartDeviceMap uartDeviceMap;
|
||||
/**
|
||||
* @brief This function opens and configures a uart device by using the information stored
|
||||
* in the uart cookie.
|
||||
* @param uartCookie Pointer to uart cookie with information about the uart. Contains the
|
||||
* uart device file, baudrate, parity, stopbits etc.
|
||||
* @return The file descriptor of the configured uart.
|
||||
*/
|
||||
int configureUartPort(UartCookie* uartCookie);
|
||||
|
||||
/**
|
||||
* @brief This function opens and configures a uart device by using the information stored
|
||||
* in the uart cookie.
|
||||
* @param uartCookie Pointer to uart cookie with information about the uart. Contains the
|
||||
* uart device file, baudrate, parity, stopbits etc.
|
||||
* @return The file descriptor of the configured uart.
|
||||
*/
|
||||
int configureUartPort(UartCookie* uartCookie);
|
||||
/**
|
||||
* @brief This function adds the parity settings to the termios options struct.
|
||||
*
|
||||
* @param options Pointer to termios options struct which will be modified to enable or disable
|
||||
* parity checking.
|
||||
* @param uartCookie Pointer to uart cookie containing the information about the desired
|
||||
* parity settings.
|
||||
*
|
||||
*/
|
||||
void setParityOptions(struct termios* options, UartCookie* uartCookie);
|
||||
|
||||
/**
|
||||
* @brief This function adds the parity settings to the termios options struct.
|
||||
*
|
||||
* @param options Pointer to termios options struct which will be modified to enable or disable
|
||||
* parity checking.
|
||||
* @param uartCookie Pointer to uart cookie containing the information about the desired
|
||||
* parity settings.
|
||||
*
|
||||
*/
|
||||
void setParityOptions(struct termios* options, UartCookie* uartCookie);
|
||||
void setStopBitOptions(struct termios* options, UartCookie* uartCookie);
|
||||
|
||||
void setStopBitOptions(struct termios* options, UartCookie* uartCookie);
|
||||
/**
|
||||
* @brief This function sets options which are not configurable by the uartCookie.
|
||||
*/
|
||||
void setFixedOptions(struct termios* options);
|
||||
|
||||
/**
|
||||
* @brief This function sets options which are not configurable by the uartCookie.
|
||||
*/
|
||||
void setFixedOptions(struct termios* options);
|
||||
/**
|
||||
* @brief With this function the datasize settings are added to the termios options struct.
|
||||
*/
|
||||
void setDatasizeOptions(struct termios* options, UartCookie* uartCookie);
|
||||
|
||||
/**
|
||||
* @brief With this function the datasize settings are added to the termios options struct.
|
||||
*/
|
||||
void setDatasizeOptions(struct termios* options, UartCookie* uartCookie);
|
||||
/**
|
||||
* @brief This functions adds the baudrate specified in the uartCookie to the termios options
|
||||
* struct.
|
||||
*/
|
||||
void configureBaudrate(struct termios* options, UartCookie* uartCookie);
|
||||
|
||||
/**
|
||||
* @brief This functions adds the baudrate specified in the uartCookie to the termios options
|
||||
* struct.
|
||||
*/
|
||||
void configureBaudrate(struct termios* options, UartCookie* uartCookie);
|
||||
|
||||
void setUartMode(struct termios* options, UartCookie& uartCookie);
|
||||
|
||||
ReturnValue_t handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
|
||||
size_t requestLen);
|
||||
ReturnValue_t handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
|
||||
size_t requestLen);
|
||||
void setUartMode(struct termios* options, UartCookie& uartCookie);
|
||||
|
||||
ReturnValue_t handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
|
||||
size_t requestLen);
|
||||
ReturnValue_t handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
|
||||
size_t requestLen);
|
||||
};
|
||||
|
||||
#endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */
|
||||
|
@ -3,97 +3,63 @@
|
||||
#include <fsfw/serviceinterface.h>
|
||||
|
||||
UartCookie::UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode,
|
||||
uint32_t baudrate, size_t maxReplyLen):
|
||||
handlerId(handlerId), deviceFile(deviceFile), uartMode(uartMode),
|
||||
baudrate(baudrate), maxReplyLen(maxReplyLen) {
|
||||
}
|
||||
uint32_t baudrate, size_t maxReplyLen)
|
||||
: handlerId(handlerId),
|
||||
deviceFile(deviceFile),
|
||||
uartMode(uartMode),
|
||||
baudrate(baudrate),
|
||||
maxReplyLen(maxReplyLen) {}
|
||||
|
||||
UartCookie::~UartCookie() {}
|
||||
|
||||
uint32_t UartCookie::getBaudrate() const {
|
||||
return baudrate;
|
||||
}
|
||||
uint32_t UartCookie::getBaudrate() const { return baudrate; }
|
||||
|
||||
size_t UartCookie::getMaxReplyLen() const {
|
||||
return maxReplyLen;
|
||||
}
|
||||
size_t UartCookie::getMaxReplyLen() const { return maxReplyLen; }
|
||||
|
||||
std::string UartCookie::getDeviceFile() const {
|
||||
return deviceFile;
|
||||
}
|
||||
std::string UartCookie::getDeviceFile() const { return deviceFile; }
|
||||
|
||||
void UartCookie::setParityOdd() {
|
||||
parity = Parity::ODD;
|
||||
}
|
||||
void UartCookie::setParityOdd() { parity = Parity::ODD; }
|
||||
|
||||
void UartCookie::setParityEven() {
|
||||
parity = Parity::EVEN;
|
||||
}
|
||||
void UartCookie::setParityEven() { parity = Parity::EVEN; }
|
||||
|
||||
Parity UartCookie::getParity() const {
|
||||
return parity;
|
||||
}
|
||||
Parity UartCookie::getParity() const { return parity; }
|
||||
|
||||
void UartCookie::setBitsPerWord(uint8_t bitsPerWord_) {
|
||||
switch(bitsPerWord_) {
|
||||
switch (bitsPerWord_) {
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||
sif::debug << "UartCookie::setBitsPerWord: Invalid bits per word specified" << std::endl;
|
||||
sif::debug << "UartCookie::setBitsPerWord: Invalid bits per word specified" << std::endl;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
bitsPerWord = bitsPerWord_;
|
||||
return;
|
||||
}
|
||||
bitsPerWord = bitsPerWord_;
|
||||
}
|
||||
|
||||
uint8_t UartCookie::getBitsPerWord() const {
|
||||
return bitsPerWord;
|
||||
}
|
||||
uint8_t UartCookie::getBitsPerWord() const { return bitsPerWord; }
|
||||
|
||||
StopBits UartCookie::getStopBits() const {
|
||||
return stopBits;
|
||||
}
|
||||
StopBits UartCookie::getStopBits() const { return stopBits; }
|
||||
|
||||
void UartCookie::setTwoStopBits() {
|
||||
stopBits = StopBits::TWO_STOP_BITS;
|
||||
}
|
||||
void UartCookie::setTwoStopBits() { stopBits = StopBits::TWO_STOP_BITS; }
|
||||
|
||||
void UartCookie::setOneStopBit() {
|
||||
stopBits = StopBits::ONE_STOP_BIT;
|
||||
}
|
||||
void UartCookie::setOneStopBit() { stopBits = StopBits::ONE_STOP_BIT; }
|
||||
|
||||
UartModes UartCookie::getUartMode() const {
|
||||
return uartMode;
|
||||
}
|
||||
UartModes UartCookie::getUartMode() const { return uartMode; }
|
||||
|
||||
void UartCookie::setReadCycles(uint8_t readCycles) {
|
||||
this->readCycles = readCycles;
|
||||
}
|
||||
void UartCookie::setReadCycles(uint8_t readCycles) { this->readCycles = readCycles; }
|
||||
|
||||
void UartCookie::setToFlushInput(bool enable) {
|
||||
this->flushInput = enable;
|
||||
}
|
||||
void UartCookie::setToFlushInput(bool enable) { this->flushInput = enable; }
|
||||
|
||||
uint8_t UartCookie::getReadCycles() const {
|
||||
return readCycles;
|
||||
}
|
||||
uint8_t UartCookie::getReadCycles() const { return readCycles; }
|
||||
|
||||
bool UartCookie::getInputShouldBeFlushed() {
|
||||
return this->flushInput;
|
||||
}
|
||||
bool UartCookie::getInputShouldBeFlushed() { return this->flushInput; }
|
||||
|
||||
object_id_t UartCookie::getHandlerId() const {
|
||||
return this->handlerId;
|
||||
}
|
||||
object_id_t UartCookie::getHandlerId() const { return this->handlerId; }
|
||||
|
||||
void UartCookie::setNoFixedSizeReply() {
|
||||
replySizeFixed = false;
|
||||
}
|
||||
void UartCookie::setNoFixedSizeReply() { replySizeFixed = false; }
|
||||
|
||||
bool UartCookie::isReplySizeFixed() {
|
||||
return replySizeFixed;
|
||||
}
|
||||
bool UartCookie::isReplySizeFixed() { return replySizeFixed; }
|
||||
|
@ -6,21 +6,11 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
enum class Parity {
|
||||
NONE,
|
||||
EVEN,
|
||||
ODD
|
||||
};
|
||||
enum class Parity { NONE, EVEN, ODD };
|
||||
|
||||
enum class StopBits {
|
||||
ONE_STOP_BIT,
|
||||
TWO_STOP_BITS
|
||||
};
|
||||
enum class StopBits { ONE_STOP_BIT, TWO_STOP_BITS };
|
||||
|
||||
enum class UartModes {
|
||||
CANONICAL,
|
||||
NON_CANONICAL
|
||||
};
|
||||
enum class UartModes { CANONICAL, NON_CANONICAL };
|
||||
|
||||
/**
|
||||
* @brief Cookie for the UartComIF. There are many options available to configure the UART driver.
|
||||
@ -29,93 +19,91 @@ enum class UartModes {
|
||||
*
|
||||
* @author J. Meier
|
||||
*/
|
||||
class UartCookie: public CookieIF {
|
||||
public:
|
||||
class UartCookie : public CookieIF {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for the uart cookie.
|
||||
* @param deviceFile The device file specifying the uart to use, e.g. "/dev/ttyPS1"
|
||||
* @param uartMode Specify the UART mode. The canonical mode should be used if the
|
||||
* messages are separated by a delimited character like '\n'. See the
|
||||
* termios documentation for more information
|
||||
* @param baudrate The baudrate to use for input and output. Possible Baudrates are: 50,
|
||||
* 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, B19200,
|
||||
* 38400, 57600, 115200, 230400, 460800
|
||||
* @param maxReplyLen The maximum size an object using this cookie expects
|
||||
* @details
|
||||
* Default configuration: No parity
|
||||
* 8 databits (number of bits transfered with one uart frame)
|
||||
* One stop bit
|
||||
*/
|
||||
UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode, uint32_t baudrate,
|
||||
size_t maxReplyLen);
|
||||
|
||||
/**
|
||||
* @brief Constructor for the uart cookie.
|
||||
* @param deviceFile The device file specifying the uart to use, e.g. "/dev/ttyPS1"
|
||||
* @param uartMode Specify the UART mode. The canonical mode should be used if the
|
||||
* messages are separated by a delimited character like '\n'. See the
|
||||
* termios documentation for more information
|
||||
* @param baudrate The baudrate to use for input and output. Possible Baudrates are: 50,
|
||||
* 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, B19200,
|
||||
* 38400, 57600, 115200, 230400, 460800
|
||||
* @param maxReplyLen The maximum size an object using this cookie expects
|
||||
* @details
|
||||
* Default configuration: No parity
|
||||
* 8 databits (number of bits transfered with one uart frame)
|
||||
* One stop bit
|
||||
*/
|
||||
UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode,
|
||||
uint32_t baudrate, size_t maxReplyLen);
|
||||
virtual ~UartCookie();
|
||||
|
||||
virtual ~UartCookie();
|
||||
uint32_t getBaudrate() const;
|
||||
size_t getMaxReplyLen() const;
|
||||
std::string getDeviceFile() const;
|
||||
Parity getParity() const;
|
||||
uint8_t getBitsPerWord() const;
|
||||
StopBits getStopBits() const;
|
||||
UartModes getUartMode() const;
|
||||
object_id_t getHandlerId() const;
|
||||
|
||||
uint32_t getBaudrate() const;
|
||||
size_t getMaxReplyLen() const;
|
||||
std::string getDeviceFile() const;
|
||||
Parity getParity() const;
|
||||
uint8_t getBitsPerWord() const;
|
||||
StopBits getStopBits() const;
|
||||
UartModes getUartMode() const;
|
||||
object_id_t getHandlerId() const;
|
||||
/**
|
||||
* The UART ComIF will only perform a specified number of read cycles for the canonical mode.
|
||||
* The user can specify how many of those read cycles are performed for one device handler
|
||||
* communication cycle. An example use-case would be to read all available GPS NMEA strings
|
||||
* at once.
|
||||
* @param readCycles
|
||||
*/
|
||||
void setReadCycles(uint8_t readCycles);
|
||||
uint8_t getReadCycles() const;
|
||||
|
||||
/**
|
||||
* The UART ComIF will only perform a specified number of read cycles for the canonical mode.
|
||||
* The user can specify how many of those read cycles are performed for one device handler
|
||||
* communication cycle. An example use-case would be to read all available GPS NMEA strings
|
||||
* at once.
|
||||
* @param readCycles
|
||||
*/
|
||||
void setReadCycles(uint8_t readCycles);
|
||||
uint8_t getReadCycles() const;
|
||||
/**
|
||||
* Allows to flush the data which was received but has not been read yet. This is useful
|
||||
* to discard obsolete data at software startup.
|
||||
*/
|
||||
void setToFlushInput(bool enable);
|
||||
bool getInputShouldBeFlushed();
|
||||
|
||||
/**
|
||||
* Allows to flush the data which was received but has not been read yet. This is useful
|
||||
* to discard obsolete data at software startup.
|
||||
*/
|
||||
void setToFlushInput(bool enable);
|
||||
bool getInputShouldBeFlushed();
|
||||
/**
|
||||
* Functions two enable parity checking.
|
||||
*/
|
||||
void setParityOdd();
|
||||
void setParityEven();
|
||||
|
||||
/**
|
||||
* Functions two enable parity checking.
|
||||
*/
|
||||
void setParityOdd();
|
||||
void setParityEven();
|
||||
/**
|
||||
* Function two set number of bits per UART frame.
|
||||
*/
|
||||
void setBitsPerWord(uint8_t bitsPerWord_);
|
||||
|
||||
/**
|
||||
* Function two set number of bits per UART frame.
|
||||
*/
|
||||
void setBitsPerWord(uint8_t bitsPerWord_);
|
||||
/**
|
||||
* Function to specify the number of stopbits.
|
||||
*/
|
||||
void setTwoStopBits();
|
||||
void setOneStopBit();
|
||||
|
||||
/**
|
||||
* Function to specify the number of stopbits.
|
||||
*/
|
||||
void setTwoStopBits();
|
||||
void setOneStopBit();
|
||||
/**
|
||||
* Calling this function prevents the UartComIF to return failed if not all requested bytes
|
||||
* could be read. This is required by a device handler when the size of a reply is not known.
|
||||
*/
|
||||
void setNoFixedSizeReply();
|
||||
|
||||
/**
|
||||
* Calling this function prevents the UartComIF to return failed if not all requested bytes
|
||||
* could be read. This is required by a device handler when the size of a reply is not known.
|
||||
*/
|
||||
void setNoFixedSizeReply();
|
||||
bool isReplySizeFixed();
|
||||
|
||||
bool isReplySizeFixed();
|
||||
|
||||
private:
|
||||
|
||||
const object_id_t handlerId;
|
||||
std::string deviceFile;
|
||||
const UartModes uartMode;
|
||||
bool flushInput = false;
|
||||
uint32_t baudrate;
|
||||
size_t maxReplyLen = 0;
|
||||
Parity parity = Parity::NONE;
|
||||
uint8_t bitsPerWord = 8;
|
||||
uint8_t readCycles = 1;
|
||||
StopBits stopBits = StopBits::ONE_STOP_BIT;
|
||||
bool replySizeFixed = true;
|
||||
private:
|
||||
const object_id_t handlerId;
|
||||
std::string deviceFile;
|
||||
const UartModes uartMode;
|
||||
bool flushInput = false;
|
||||
uint32_t baudrate;
|
||||
size_t maxReplyLen = 0;
|
||||
Parity parity = Parity::NONE;
|
||||
uint8_t bitsPerWord = 8;
|
||||
uint8_t readCycles = 1;
|
||||
StopBits stopBits = StopBits::ONE_STOP_BIT;
|
||||
bool replySizeFixed = true;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user