eive-obsw/linux/boardtest/UartTestClass.cpp

407 lines
14 KiB
C++
Raw Normal View History

2021-06-16 18:38:06 +02:00
#include "UartTestClass.h"
2022-02-09 12:07:29 +01:00
2022-02-22 20:13:16 +01:00
#include <errno.h> // Error integer and strerror() function
#include <fcntl.h> // Contains file controls like O_RDWR
2022-02-09 12:07:29 +01:00
#include <fsfw/tasks/TaskFactory.h>
2022-04-08 19:12:21 +02:00
#include <fsfw_hal/linux/uart/UartCookie.h>
2022-04-09 14:43:06 +02:00
#include <linux/devices/ScexDleParser.h>
2022-04-21 19:47:09 +02:00
#include <linux/devices/ScexHelper.h>
2022-04-08 19:12:21 +02:00
#include <linux/devices/ScexUartReader.h>
2022-01-17 13:48:55 +01:00
#include <unistd.h> // write(), read(), close()
2021-06-16 18:38:06 +02:00
2022-04-29 13:31:14 +02:00
#include <random>
#include <string>
2022-02-22 20:13:16 +01:00
#include "OBSWConfig.h"
2022-02-04 17:48:05 +01:00
#include "fsfw/globalfunctions/CRC.h"
2022-02-09 12:07:29 +01:00
#include "fsfw/globalfunctions/DleEncoder.h"
2022-02-04 17:48:05 +01:00
#include "fsfw/globalfunctions/arrayprinter.h"
#include "fsfw/serviceinterface.h"
2022-04-29 15:46:16 +02:00
#include "mission/devices/devicedefinitions/ScexDefinitions.h"
2021-06-16 18:38:06 +02:00
2022-01-17 13:48:55 +01:00
#define GPS_REPLY_WIRETAPPING 0
2021-06-16 18:38:06 +02:00
2022-03-08 09:37:23 +01:00
#ifndef RPI_TEST_GPS_HANDLER
#define RPI_TEST_GPS_HANDLER 0
#endif
2022-08-29 16:05:05 +02:00
using namespace returnvalue;
2022-09-27 18:54:48 +02:00
UartTestClass::UartTestClass(object_id_t objectId) : TestTask(objectId) {
2022-04-15 01:39:47 +02:00
mode = TestModes::SCEX;
2022-09-27 18:54:48 +02:00
scexMode = ScexModes::SIMPLE;
2022-07-13 12:02:32 +02:00
// No one-cell and all-cell support implemented yet
2022-09-27 18:54:48 +02:00
currCmd = scex::Cmds::PING;
2022-04-15 01:39:47 +02:00
if (scexMode == ScexModes::SIMPLE) {
auto encodingBuf = new std::array<uint8_t, 4096>;
DleParser::BufPair encodingBufPair{encodingBuf->data(), encodingBuf->size()};
auto decodedBuf = new std::array<uint8_t, 4096>;
DleParser::BufPair decodingBufPair{decodedBuf->data(), decodedBuf->size()};
2022-10-04 20:52:11 +02:00
// TODO: Code changes but this test class has not, might not work like this anymore
2022-04-15 01:39:47 +02:00
dleParser = new ScexDleParser(*(new SimpleRingBuffer(4096, true)), dleEncoder, encodingBufPair,
2022-10-04 20:52:11 +02:00
decodingBufPair);
2022-09-27 18:54:48 +02:00
} else {
reader = new ScexUartReader(objects::SCEX_UART_READER);
2022-04-15 01:39:47 +02:00
}
2022-04-08 19:12:21 +02:00
}
2021-03-04 18:29:28 +01:00
2021-06-16 18:38:06 +02:00
ReturnValue_t UartTestClass::initialize() {
2022-04-15 01:39:47 +02:00
if (mode == TestModes::GPS) {
gpsInit();
} else if (mode == TestModes::SCEX) {
scexInit();
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-02-03 13:37:48 +01:00
}
2022-08-24 17:27:47 +02:00
ReturnValue_t UartTestClass::performOneShotAction() { return returnvalue::OK; }
2022-02-03 13:37:48 +01:00
ReturnValue_t UartTestClass::performPeriodicAction() {
2022-04-15 01:39:47 +02:00
if (mode == TestModes::GPS) {
gpsPeriodic();
} else if (mode == TestModes::SCEX) {
scexPeriodic();
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-02-03 13:37:48 +01:00
}
void UartTestClass::gpsInit() {
2022-02-22 20:13:16 +01:00
#if RPI_TEST_GPS_HANDLER == 1
2022-04-15 01:39:47 +02:00
int result = lwgps_init(&gpsData);
if (result == 0) {
2022-05-25 10:51:22 +02:00
sif::warning << "UartTestClass::gpsInit: lwgps_init error: " << result << std::endl;
2022-04-15 01:39:47 +02:00
}
2021-06-16 18:38:06 +02:00
2022-04-15 01:39:47 +02:00
/* Get file descriptor */
serialPort = open("/dev/serial0", O_RDWR);
if (serialPort < 0) {
sif::warning << "UartTestClass::gpsInit: open call failed with error [" << errno << ", "
<< strerror(errno) << std::endl;
2022-04-15 01:39:47 +02:00
}
/* 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 canonical mode for GPS device
tty.c_lflag |= ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
2021-06-16 18:38:06 +02:00
2022-04-15 01:39:47 +02:00
// Non-blocking mode
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 0;
2021-06-16 18:38:06 +02:00
2022-04-15 01:39:47 +02:00
cfsetispeed(&tty, B9600);
cfsetospeed(&tty, B9600);
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
sif::warning << "UartTestClass::gpsInit: tcsetattr call failed with error [" << errno << ", "
<< strerror(errno) << std::endl;
2022-04-15 01:39:47 +02:00
;
}
// Flush received and unread data. Those are old NMEA strings which are not relevant anymore
tcflush(serialPort, TCIFLUSH);
2021-06-16 18:38:06 +02:00
#endif
}
2022-02-03 13:37:48 +01:00
void UartTestClass::gpsPeriodic() {
2022-02-22 20:13:16 +01:00
#if RPI_TEST_GPS_HANDLER == 1
2022-04-15 01:39:47 +02:00
int bytesRead = 0;
do {
bytesRead = read(serialPort, reinterpret_cast<void*>(recBuf.data()),
static_cast<unsigned int>(recBuf.size()));
if (bytesRead < 0) {
sif::warning << "UartTestClass::gpsPeriodic: read call failed with error [" << errno << ", "
<< strerror(errno) << "]" << std::endl;
2022-04-15 01:39:47 +02:00
break;
} else if (bytesRead >= static_cast<int>(recBuf.size())) {
2022-05-25 10:51:22 +02:00
sif::debug << "UartTestClass::gpsPeriodic: "
2022-04-15 01:39:47 +02:00
"recv buffer might not be large enough"
<< std::endl;
} else if (bytesRead > 0) {
// pass data to lwgps for processing
2021-06-16 18:38:06 +02:00
#if GPS_REPLY_WIRETAPPING == 1
2022-04-15 01:39:47 +02:00
sif::info << recBuf.data() << std::endl;
2021-06-16 18:38:06 +02:00
#endif
2022-04-15 01:39:47 +02:00
int result = lwgps_process(&gpsData, recBuf.data(), bytesRead);
if (result == 0) {
2022-05-25 10:51:22 +02:00
sif::warning << "UartTestClass::gpsPeriodic: lwgps_process error" << std::endl;
2022-04-15 01:39:47 +02:00
}
recvCnt++;
if (recvCnt == 6) {
recvCnt = 0;
sif::info << "GPS Data" << std::endl;
// Print messages
printf("Valid status: %d\n", gpsData.is_valid);
printf("Latitude: %f degrees\n", gpsData.latitude);
printf("Longitude: %f degrees\n", gpsData.longitude);
printf("Altitude: %f meters\n", gpsData.altitude);
}
}
} while (bytesRead > 0);
2021-06-16 18:38:06 +02:00
#endif
2021-03-04 18:29:28 +01:00
}
2022-02-04 17:48:05 +01:00
void UartTestClass::scexInit() {
2022-04-15 01:39:47 +02:00
if (scexMode == ScexModes::SIMPLE) {
scexSimpleInit();
} else {
2022-09-27 18:54:48 +02:00
if (reader == nullptr) {
sif::warning << "UartTestClass::scexInit: Reader invalid" << std::endl;
return;
}
2022-04-08 19:12:21 +02:00
#if defined(RASPBERRY_PI)
2022-04-15 01:39:47 +02:00
std::string devname = "/dev/serial0";
2022-04-08 19:12:21 +02:00
#else
2022-04-15 01:39:47 +02:00
std::string devname = "/dev/ul-scex";
2022-04-08 19:12:21 +02:00
#endif
2022-04-15 01:39:47 +02:00
uartCookie = new UartCookie(this->getObjectId(), devname, UartBaudRate::RATE_57600, 4096);
2022-04-27 18:21:32 +02:00
reader->setDebugMode(false);
2022-04-15 01:39:47 +02:00
ReturnValue_t result = reader->initializeInterface(uartCookie);
2022-08-29 16:05:05 +02:00
if (result != OK) {
2022-05-25 10:51:22 +02:00
sif::warning << "UartTestClass::scexInit: Initializing SCEX reader "
2022-04-15 01:39:47 +02:00
"UART IF failed"
<< std::endl;
}
}
2022-02-04 17:48:05 +01:00
}
void UartTestClass::scexPeriodic() {
2022-04-15 01:39:47 +02:00
using namespace std;
2022-04-21 19:47:09 +02:00
using namespace scex;
2022-04-13 17:43:16 +02:00
2022-04-15 01:39:47 +02:00
if (scexMode == ScexModes::SIMPLE) {
scexSimplePeriodic();
} else {
2022-09-27 18:54:48 +02:00
if (reader == nullptr) {
return;
}
2022-04-15 01:39:47 +02:00
if (not cmdSent) {
size_t len = 0;
2022-04-26 13:22:56 +02:00
prepareScexCmd(currCmd, false, cmdBuf.data(), &len);
2022-04-15 01:39:47 +02:00
reader->sendMessage(uartCookie, cmdBuf.data(), len);
cmdSent = true;
cmdDone = false;
}
if (cmdSent and not cmdDone) {
uint8_t* decodedPacket = nullptr;
size_t len = 0;
2022-04-27 18:21:32 +02:00
do {
ReturnValue_t result = reader->readReceivedMessage(uartCookie, &decodedPacket, &len);
2022-04-29 12:48:13 +02:00
if (len == 0) {
break;
2022-04-27 18:21:32 +02:00
}
2022-04-21 19:47:09 +02:00
ScexHelper helper;
const uint8_t* helperPtr = decodedPacket;
result = helper.deSerialize(&helperPtr, &len);
if (result == ScexHelper::INVALID_CRC) {
2022-05-25 10:51:22 +02:00
sif::warning << "UartTestClass::scexPeriodic: CRC invalid" << std::endl;
2022-04-15 01:39:47 +02:00
}
2022-04-21 19:47:09 +02:00
sif::info << helper << endl;
2022-04-27 18:21:32 +02:00
// ping
// if ping cmd
if (helper.getCmd() == PING) {
ofstream out("/tmp/scex-ping.bin", ofstream::binary);
if (out.bad()) {
sif::warning << "bad" << std::endl;
}
out << helper;
2022-04-26 13:22:56 +02:00
}
2022-04-27 18:21:32 +02:00
// fram
2022-07-13 12:02:32 +02:00
// packetcounter eins h<>her, wenn mehr packet verloren -> merkt sich welches packet fehlt
2022-04-29 12:48:13 +02:00
// was wenn erstes packet fehlt; mit boolean var (firstpacketarrived=false) die immer mit
// finish false wird?
2022-04-27 18:21:32 +02:00
// countdown (max 2min), wenn nicht if (helper.getPacketCounter() ==
// helper.getTotalPacketCounter()) { nach 2min reader->finish();
if (helper.getCmd() == FRAM) {
2022-04-29 12:48:13 +02:00
if (not fileNameSet) {
2022-04-29 15:46:16 +02:00
fileId = random_string(6);
2022-04-29 12:48:13 +02:00
fileName = "/tmp/scex-fram_" + fileId + ".bin";
fileNameSet = true;
}
2022-04-27 18:21:32 +02:00
if (helper.getPacketCounter() == 1) {
2022-04-29 12:48:13 +02:00
// countdown starten
finishCountdown.resetTimer();
2022-04-29 12:26:18 +02:00
ofstream out(fileName,
ofstream::binary); // neues file anlegen
2022-04-27 18:21:32 +02:00
} else {
2022-04-29 12:26:18 +02:00
ofstream out(fileName,
ofstream::binary | ofstream::app); // an bestehendes file appenden
2022-04-27 18:21:32 +02:00
out << helper;
}
2022-04-29 12:48:13 +02:00
if (finishCountdown.hasTimedOut()) {
2022-04-29 15:46:16 +02:00
triggerEvent(scex::EXPERIMENT_TIMEDOUT, currCmd, 0);
2022-04-29 12:48:13 +02:00
reader->finish();
2022-05-25 10:51:22 +02:00
sif::warning << "UartTestClass::scexPeriodic: Reader timeout" << endl;
2022-04-29 12:48:13 +02:00
cmdDone = true;
fileNameSet = false;
2022-04-27 18:21:32 +02:00
}
2022-04-15 01:39:47 +02:00
}
2022-04-21 19:47:09 +02:00
if (helper.getPacketCounter() == helper.getTotalPacketCounter()) {
2022-04-15 01:39:47 +02:00
reader->finish();
2022-05-25 10:51:22 +02:00
sif::info << "UartTestClass::scexPeriodic: Reader is finished" << endl;
2022-04-15 01:39:47 +02:00
cmdDone = true;
2022-04-29 12:48:13 +02:00
fileNameSet = false;
2022-04-29 15:46:16 +02:00
if (helper.getCmd() == scex::Cmds::PING) {
2022-04-21 19:47:09 +02:00
cmdSent = false;
2022-04-29 12:48:13 +02:00
fileNameSet = true; // to not generate everytime new file
2022-04-21 19:47:09 +02:00
}
2022-04-15 01:39:47 +02:00
}
2022-04-27 18:21:32 +02:00
} while (len > 0);
2022-04-15 01:39:47 +02:00
}
}
2022-02-04 17:48:05 +01:00
}
2022-02-22 20:13:16 +01:00
2022-04-08 21:16:02 +02:00
void UartTestClass::scexSimpleInit() {
#if defined(RASPBERRY_PI)
2022-04-15 01:39:47 +02:00
std::string devname = "/dev/serial0";
2022-04-08 21:16:02 +02:00
#else
2022-04-15 01:39:47 +02:00
std::string devname = "/dev/ul-scex";
2022-04-08 21:16:02 +02:00
#endif
2022-04-15 01:39:47 +02:00
/* Get file descriptor */
serialPort = open(devname.c_str(), O_RDWR);
if (serialPort < 0) {
sif::warning << "UartTestClass::scexSimpleInit: Open call failed with error [" << errno << ", "
<< strerror(errno) << std::endl;
2022-04-15 01:39:47 +02:00
return;
}
// 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)
2022-04-08 21:16:02 +02:00
2022-04-15 01:39:47 +02:00
// Use non-canonical mode and clear echo flag
tty.c_lflag &= ~(ICANON | ECHO);
2022-04-08 21:16:02 +02:00
2022-04-15 01:39:47 +02:00
// Non-blocking mode, read until either line is 0.1 second idle or maximum of 255 bytes are
// received in one go
tty.c_cc[VTIME] = 0; // In units of 0.1 seconds
tty.c_cc[VMIN] = 0; // Read up to 255 bytes
2022-04-08 21:16:02 +02:00
2022-04-15 01:39:47 +02:00
// Q7S UART Lite has fixed baud rate. For other linux systems, set baud rate here.
2022-04-08 21:16:02 +02:00
#if !defined(XIPHOS_Q7S)
2022-04-15 01:39:47 +02:00
if (cfsetispeed(&tty, B57600) != 0) {
2022-05-25 10:51:22 +02:00
sif::warning << "UartTestClass::scexSimpleInit: Setting baud rate failed" << std::endl;
2022-04-15 01:39:47 +02:00
}
2022-04-08 21:16:02 +02:00
#endif
2022-04-15 01:39:47 +02:00
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
sif::warning << "UartTestClass::scexSimpleInit: tcsetattr call failed with error [" << errno
<< ", " << strerror(errno) << std::endl;
2022-04-15 01:39:47 +02:00
}
// Flush received and unread data
tcflush(serialPort, TCIOFLUSH);
2022-04-08 21:16:02 +02:00
}
2022-04-09 01:00:42 +02:00
void UartTestClass::scexSimplePeriodic() {
2022-04-15 01:39:47 +02:00
using namespace scex;
2022-08-29 16:05:05 +02:00
ReturnValue_t result = OK;
2022-04-15 01:39:47 +02:00
if (not cmdSent) {
// Flush received and unread data
tcflush(serialPort, TCIFLUSH);
uint8_t tmpCmdBuf[32] = {};
size_t len = 0;
sif::info << "UartTestClass::scexSimplePeriodic: Sending command to SCEX" << std::endl;
prepareScexCmd(currCmd, false, tmpCmdBuf, &len);
result = dleEncoder.encode(tmpCmdBuf, len, cmdBuf.data(), cmdBuf.size(), &encodedLen, true);
2022-08-29 16:05:05 +02:00
if (result != OK) {
2022-05-25 10:51:22 +02:00
sif::warning << "UartTestClass::scexSimplePeriodic: Encoding failed" << std::endl;
2022-04-15 01:39:47 +02:00
return;
}
if (result != 0) {
return;
};
size_t bytesWritten = write(serialPort, cmdBuf.data(), encodedLen);
if (bytesWritten != encodedLen) {
sif::warning
<< "UartTestClass::scexSimplePeriodic: Sending command to solar experiment failed"
<< std::endl;
2022-04-15 01:39:47 +02:00
}
cmdSent = true;
cmdDone = false;
}
if (not cmdDone) {
// Read back reply immediately
int bytesRead = 0;
do {
bytesRead = read(serialPort, reinterpret_cast<void*>(recBuf.data()),
static_cast<unsigned int>(recBuf.size()));
if (bytesRead == 0) {
sif::warning << "UartTestClass::scexSimplePeriodic: Reading SCEX: Timeout or no bytes read"
<< std::endl;
2022-04-15 01:39:47 +02:00
} else if (bytesRead < 0) {
sif::warning << "UartTestClass::scexSimplePeriodic: read call failed with error [" << errno
<< ", " << strerror(errno) << "]" << std::endl;
2022-04-15 01:39:47 +02:00
break;
} else if (bytesRead >= static_cast<int>(recBuf.size())) {
2022-05-25 10:51:22 +02:00
sif::debug << "UartTestClass::scexSimplePeriodic: recv buffer might not be large "
2022-04-27 18:21:32 +02:00
"enough, bytes read:"
<< bytesRead << std::endl;
2022-04-15 01:39:47 +02:00
} else if (bytesRead > 0) {
dleParser->passData(recBuf.data(), bytesRead);
2022-04-29 15:46:16 +02:00
if (currCmd == Cmds::PING) {
2022-04-15 01:39:47 +02:00
cmdDone = true;
cmdSent = false;
}
}
} while (bytesRead > 0);
}
2022-04-09 14:43:06 +02:00
}
2022-04-09 01:00:42 +02:00
2022-04-29 15:46:16 +02:00
int UartTestClass::prepareScexCmd(scex::Cmds cmd, bool tempCheck, uint8_t* cmdBuf, size_t* len) {
2022-04-15 01:39:47 +02:00
using namespace scex;
2022-04-26 13:22:56 +02:00
// Send command
2022-04-15 01:39:47 +02:00
cmdBuf[0] = scex::createCmdByte(cmd, false);
// These two fields are the packet counter and the total packet count. Those are 1 and 1 for each
// telecommand so far
cmdBuf[1] = 1;
cmdBuf[2] = 1;
uint16_t userDataLen = 0;
2022-08-29 16:05:05 +02:00
cmdBuf[3] = (userDataLen >> 8) & 0xff;
cmdBuf[4] = userDataLen & 0xff;
uint16_t crc = CRC::crc16ccitt(cmdBuf, 5);
cmdBuf[5] = (crc >> 8) & 0xff;
cmdBuf[6] = crc & 0xff;
2022-09-27 18:54:48 +02:00
*len = 7;
2022-04-15 01:39:47 +02:00
return 0;
2022-04-09 14:43:06 +02:00
}
void UartTestClass::handleFoundDlePacket(uint8_t* packet, size_t len) {
sif::info << "UartTestClass::handleFoundDlePacket: Detected DLE encoded packet with decoded size "
<< len << std::endl;
2022-04-09 01:00:42 +02:00
}
2022-04-29 12:26:18 +02:00
2022-04-29 13:31:14 +02:00
std::string UartTestClass::random_string(std::string::size_type length) {
static auto& chrs =
2022-04-29 12:48:13 +02:00
"0123456789"
2022-04-29 13:31:14 +02:00
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2022-04-29 12:26:18 +02:00
2022-04-29 13:31:14 +02:00
thread_local static std::mt19937 rg{std::random_device{}()};
thread_local static std::uniform_int_distribution<std::string::size_type> pick(0,
sizeof(chrs) - 2);
2022-04-29 12:26:18 +02:00
2022-04-29 13:31:14 +02:00
std::string s;
s.reserve(length);
while (length--) s += chrs[pick(rg)];
2022-04-29 12:26:18 +02:00
2022-04-29 13:31:14 +02:00
return s;
2022-04-29 12:26:18 +02:00
}