fsfw/serviceinterface/ServiceInterfaceBuffer.h

158 lines
4.3 KiB
C
Raw Permalink Normal View History

#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
2020-08-13 20:53:35 +02:00
#include "../returnvalues/HasReturnvaluesIF.h"
2021-01-03 01:02:07 +01:00
#include <FSFWConfig.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 <iostream>
#include <sstream>
2020-05-29 20:31:08 +02:00
#include <iomanip>
2018-07-13 18:28:26 +02:00
#ifndef UT699
/**
* @brief This is the underlying stream buffer which implements the
* streambuf class and overloads the overflow() and sync() methods
* @details
* This class is used to modify the output of the stream, for example by adding.
* It also calls the char printing function which is implemented in the
* board supply package (BSP).
*/
2020-05-29 20:31:08 +02:00
class ServiceInterfaceBuffer:
public std::streambuf {
2018-07-13 15:56:37 +02:00
friend class ServiceInterfaceStream;
public:
2020-06-04 19:50:56 +02:00
static constexpr uint8_t MAX_PREAMBLE_SIZE = 40;
2020-06-04 14:08:26 +02:00
ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble,
bool buffered, bool errStream, uint16_t port);
2018-07-13 15:56:37 +02:00
protected:
bool isActive;
//! This is called when buffer becomes full. If
//! buffer is not used, then this is called every
//! time when characters are put to stream.
2020-12-21 17:16:44 +01:00
int overflow(int c = Traits::eof()) override;
2018-07-13 15:56:37 +02:00
//! This function is called when stream is flushed,
//! for example when std::endl is put to stream.
2020-05-29 20:31:08 +02:00
int sync(void) override;
2018-07-13 15:56:37 +02:00
2020-06-04 19:37:33 +02:00
bool isBuffered() const;
2018-07-13 15:56:37 +02:00
private:
//! For additional message information
std::string logMessage;
2020-06-04 14:08:26 +02:00
std::string preamble;
#if FSFW_COLORED_OUTPUT == 1
std::string colorPrefix;
#endif
2018-07-13 15:56:37 +02:00
// For EOF detection
typedef std::char_traits<char> Traits;
2020-06-04 19:50:56 +02:00
//! This is useful for some terminal programs which do not have
//! implicit carriage return with newline characters.
2020-05-29 20:31:08 +02:00
bool addCrToPreamble;
2020-06-04 19:37:33 +02:00
2020-06-04 19:50:56 +02:00
//! Specifies whether the stream operates in buffered or unbuffered mode.
bool buffered;
//! This specifies to print to stderr and work in unbuffered mode.
bool errStream;
2020-06-04 19:50:56 +02:00
//! Needed for buffered mode.
2021-01-03 01:02:07 +01:00
static size_t const BUF_SIZE = fsfwconfig::FSFW_PRINT_BUFFER_SIZE;
2018-07-13 15:56:37 +02:00
char buf[BUF_SIZE];
//! In this function, the characters are parsed.
2018-07-13 15:56:37 +02:00
void putChars(char const* begin, char const* end);
2020-05-29 20:31:08 +02:00
2020-07-21 15:15:53 +02:00
std::string* getPreamble(size_t * preambleSize = nullptr);
2021-01-15 18:07:32 +01:00
bool crAdditionEnabled() const;
2018-07-13 15:56:37 +02:00
};
2018-07-13 18:28:26 +02:00
#endif
2018-07-13 15:56:37 +02:00
#ifdef UT699
class ServiceInterfaceBuffer: public std::basic_streambuf<char,
std::char_traits<char> > {
friend class ServiceInterfaceStream;
public:
ServiceInterfaceBuffer(std::string set_message, uint16_t port);
protected:
bool isActive;
// This is called when buffer becomes full. If
// buffer is not used, then this is called every
// time when characters are put to stream.
virtual int overflow(int c = Traits::eof());
// This function is called when stream is flushed,
// for example when std::endl is put to stream.
virtual int sync(void);
private:
// For additional message information
std::string log_message;
// For EOF detection
typedef std::char_traits<char> Traits;
// Work in buffer mode. It is also possible to work without buffer.
static size_t const BUF_SIZE = 128;
char buf[BUF_SIZE];
// In this function, the characters are parsed.
void putChars(char const* begin, char const* end);
};
#endif //UT699
#ifdef ML505
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/udp.h>
class ServiceInterfaceBuffer: public std::basic_streambuf<char,
std::char_traits<char> > {
friend class ServiceInterfaceStream;
public:
ServiceInterfaceBuffer(std::string set_message, uint16_t port);
protected:
bool isActive;
// This is called when buffer becomes full. If
// buffer is not used, then this is called every
// time when characters are put to stream.
virtual int overflow(int c = Traits::eof());
// This function is called when stream is flushed,
// for example when std::endl is put to stream.
virtual int sync(void);
private:
// For additional message information
std::string log_message;
// For EOF detection
typedef std::char_traits<char> Traits;
// Work in buffer mode. It is also possible to work without buffer.
static size_t const BUF_SIZE = 128;
char buf[BUF_SIZE];
// In this function, the characters are parsed.
void putChars(char const* begin, char const* end);
int udpSocket;
sockaddr_in remoteAddress;
socklen_t remoteAddressLength;
void initSocket();
};
#endif //ML505
2021-01-03 14:16:52 +01:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ */