fsfw/serviceinterface/ServiceInterfaceBuffer.cpp

263 lines
6.3 KiB
C++
Raw Normal View History

2020-08-13 20:53:35 +02:00
#include "ServiceInterfaceBuffer.h"
2021-01-03 14:08:40 +01:00
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-01-03 14:08:40 +01:00
#include "../timemanager/Clock.h"
#include "serviceInterfaceDefintions.h"
#include <cstring>
#include <inttypes.h>
2021-01-03 13:25:42 +01:00
#if defined(WIN32) && FSFW_COLORED_OUTPUT == 1
#include "Windows.h"
#endif
2018-07-13 15:56:37 +02:00
// to be implemented by bsp
extern "C" void printChar(const char*, bool errStream);
#ifndef UT699
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage,
2020-06-04 14:08:26 +02:00
bool addCrToPreamble, bool buffered , bool errStream, uint16_t port):
isActive(true), logMessage(setMessage),
2020-06-04 14:08:26 +02:00
addCrToPreamble(addCrToPreamble), buffered(buffered),
errStream(errStream) {
if(buffered) {
// Set pointers if the stream is buffered.
setp( buf, buf + BUF_SIZE );
}
#if FSFW_COLORED_OUTPUT == 1
2021-01-04 15:38:46 +01:00
if(setMessage.find("DEBUG") != std::string::npos) {
colorPrefix = fsfw::ANSI_COLOR_MAGENTA;
}
2021-01-04 15:38:46 +01:00
else if(setMessage.find("INFO") != std::string::npos) {
colorPrefix = fsfw::ANSI_COLOR_GREEN;
}
2021-01-04 15:38:46 +01:00
else if(setMessage.find("WARNING") != std::string::npos) {
colorPrefix = fsfw::ANSI_COLOR_YELLOW;
}
2021-01-04 15:38:46 +01:00
else if(setMessage.find("ERROR") != std::string::npos) {
colorPrefix = fsfw::ANSI_COLOR_RED;
}
2021-01-04 15:38:46 +01:00
else {
colorPrefix = fsfw::ANSI_COLOR_RESET;
}
2021-01-03 13:25:42 +01:00
#ifdef WIN32
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
#endif
#endif
2021-01-03 01:39:06 +01:00
2020-06-04 19:50:56 +02:00
preamble.reserve(MAX_PREAMBLE_SIZE);
preamble.resize(MAX_PREAMBLE_SIZE);
}
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
char array[BUF_SIZE];
uint32_t length = end - begin;
if (length > sizeof(array)) {
length = sizeof(array);
}
memcpy(array, begin, length);
for(; begin != end; begin++){
2020-06-04 14:08:26 +02:00
if(errStream) {
printChar(begin, true);
}
else {
printChar(begin, false);
}
}
}
#endif
int ServiceInterfaceBuffer::overflow(int c) {
2020-06-04 14:08:26 +02:00
if(not buffered and this->isActive) {
if (c != Traits::eof()) {
2020-06-04 14:08:26 +02:00
if(errStream) {
printChar(reinterpret_cast<const char*>(&c), true);
}
else {
printChar(reinterpret_cast<const char*>(&c), false);
}
}
return 0;
}
// Handle output
putChars(pbase(), pptr());
if (c != Traits::eof()) {
char c2 = c;
// Handle the one character that didn't fit to buffer
putChars(&c2, &c2 + 1);
}
// This tells that buffer is empty again
setp(buf, buf + BUF_SIZE - 1);
// I'm not sure about this return value!
return 0;
}
int ServiceInterfaceBuffer::sync(void) {
2020-06-04 19:37:33 +02:00
if(not this->isActive and not buffered) {
2020-06-04 14:08:26 +02:00
if(not buffered) {
setp(buf, buf + BUF_SIZE - 1);
2020-05-29 20:31:08 +02:00
}
return 0;
}
2020-06-04 19:37:33 +02:00
if(not buffered) {
return 0;
}
2020-06-04 14:08:26 +02:00
size_t preambleSize = 0;
2020-07-21 15:15:53 +02:00
std::string* preamble = getPreamble(&preambleSize);
// Write logMessage and time
2020-07-21 15:15:53 +02:00
this->putChars(preamble->data(), preamble->data() + preambleSize);
// Handle output
this->putChars(pbase(), pptr());
// This tells that buffer is empty again
setp(buf, buf + BUF_SIZE - 1);
return 0;
}
2020-06-04 19:37:33 +02:00
bool ServiceInterfaceBuffer::isBuffered() const {
return buffered;
}
2020-05-29 20:31:08 +02:00
2020-07-21 15:15:53 +02:00
std::string* ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
Clock::TimeOfDay_t loggerTime;
Clock::getDateAndTime(&loggerTime);
2020-06-04 14:08:26 +02:00
size_t currentSize = 0;
char* parsePosition = &preamble[0];
if(addCrToPreamble) {
2020-06-04 14:08:26 +02:00
preamble[0] = '\r';
currentSize += 1;
parsePosition += 1;
2018-07-13 15:56:37 +02:00
}
#if FSFW_COLORED_OUTPUT == 1
currentSize += sprintf(parsePosition, "%s", colorPrefix.c_str());
parsePosition += colorPrefix.size();
#endif
int32_t charCount = sprintf(parsePosition,
"%s: | %02" SCNu32 ":%02" SCNu32 ":%02" SCNu32 ".%03" SCNu32 " | ",
this->logMessage.c_str(), loggerTime.hour,
loggerTime.minute,
loggerTime.second,
loggerTime.usecond /1000);
2020-06-04 14:22:04 +02:00
if(charCount < 0) {
2020-06-04 19:50:56 +02:00
printf("ServiceInterfaceBuffer: Failure parsing preamble\r\n");
2020-07-21 15:15:53 +02:00
return &preamble;
2020-06-04 19:50:56 +02:00
}
if(charCount > MAX_PREAMBLE_SIZE) {
printf("ServiceInterfaceBuffer: Char count too large for maximum "
"preamble size");
2020-07-21 15:15:53 +02:00
return &preamble;
2020-06-04 14:22:04 +02:00
}
currentSize += charCount;
if(preambleSize != nullptr) {
*preambleSize = currentSize;
}
2020-07-21 15:15:53 +02:00
return &preamble;
2018-07-13 15:56:37 +02:00
}
2018-07-13 15:56:37 +02:00
2020-05-29 20:31:08 +02:00
#ifdef UT699
2020-08-13 20:53:35 +02:00
#include "../osal/rtems/Interrupt.h"
2018-07-13 18:28:26 +02:00
2020-05-29 20:31:08 +02:00
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message,
uint16_t port) {
this->log_message = set_message;
this->isActive = true;
setp( buf, buf + BUF_SIZE );
}
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
char array[BUF_SIZE];
uint32_t length = end - begin;
if (length > sizeof(array)) {
length = sizeof(array);
}
memcpy(array, begin, length);
if (!Interrupt::isInterruptInProgress()) {
std::cout << array;
} else {
//Uncomment the following line if you need ISR debug output.
// printk(array);
}
}
#endif //UT699
#ifdef ML505
#include <bsp_flp/network/networkconfig.h>
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message,
uint16_t port) :
isActive(true), log_message(set_message), udpSocket(0), remoteAddressLength(
sizeof(remoteAddress)) {
setp(buf, buf + BUF_SIZE);
memset((uint8_t*) &remoteAddress, 0, sizeof(remoteAddress));
remoteAddress.sin_family = AF_INET;
remoteAddress.sin_port = htons(port);
remoteAddress.sin_addr.s_addr = htonl(inet_addr("192.168.250.100"));
}
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
char array[BUF_SIZE];
uint32_t length = end - begin;
if (length > sizeof(array)) {
length = sizeof(array);
}
memcpy(array, begin, length);
if (udpSocket <= 0) {
initSocket();
}
if (udpSocket > 0) {
sendto(udpSocket, array, length, 0, (sockaddr*) &remoteAddress,
sizeof(remoteAddress));
}
}
void ServiceInterfaceBuffer::initSocket() {
sockaddr_in address;
memset((uint8_t*) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(0);
address.sin_addr.s_addr = htonl(INADDR_ANY);
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
if (socket < 0) {
printf("Error opening socket!\n");
return;
}
timeval timeout = { 0, 20 };
if (setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof(timeout)) < 0) {
printf("Error setting SO_RCVTIMEO socket options!\n");
return;
}
if (setsockopt(udpSocket, SOL_SOCKET, SO_SNDTIMEO, &timeout,
sizeof(timeout)) < 0) {
printf("Error setting SO_SNDTIMEO socket options!\n");
return;
}
if (bind(udpSocket, (sockaddr *) &address, sizeof(address)) < 0) {
printf("Error binding socket!\n");
}
}
#endif //ML505
2021-01-03 14:08:40 +01:00
2021-01-03 14:16:52 +01:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */