fsfw/serviceinterface/ServiceInterfaceBuffer.h

142 lines
3.8 KiB
C
Raw Normal View History

#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
#include <iostream>
#include <sstream>
#include <cstdio>
2020-04-21 19:57:16 +02:00
#include <iomanip>
2018-07-13 18:28:26 +02:00
#ifndef UT699
2020-04-21 19:57:16 +02:00
class ServiceInterfaceBuffer:
public std::basic_streambuf<char,std::char_traits<char>> {
2018-07-13 15:56:37 +02:00
friend class ServiceInterfaceStream;
public:
2020-04-21 19:57:16 +02:00
ServiceInterfaceBuffer(std::string set_message, uint16_t port,
bool addCrToPreamble);
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-04-21 19:57:16 +02: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-04-21 19:57:16 +02:00
int sync(void) override;
2018-07-13 15:56:37 +02:00
private:
// For additional message information
std::string log_message;
// For EOF detection
typedef std::char_traits<char> Traits;
2020-04-21 19:57:16 +02:00
// This is useful for some terminal programs which do not have
// implicit carriage return with newline characters.
bool addCrToPreamble;
2018-07-13 15:56:37 +02:00
// 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);
2020-04-21 19:57:16 +02:00
template<typename T>
std::string zero_padded(const T& num, uint8_t width) {
std::ostringstream string_to_pad;
string_to_pad << std::setw(width) << std::setfill('0') << num;
std::string result = string_to_pad.str();
if (result.length() > width)
{
result.erase(0, result.length() - width);
}
return result;
}
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
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ */