fsfw/osal/common/TcpTmTcServer.h

94 lines
3.2 KiB
C
Raw Normal View History

#ifndef FSFW_OSAL_COMMON_TCP_TMTC_SERVER_H_
#define FSFW_OSAL_COMMON_TCP_TMTC_SERVER_H_
2021-03-11 14:47:47 +01:00
#include "TcpIpBase.h"
2021-05-12 16:47:53 +02:00
#include "../../platform.h"
2021-05-05 12:59:42 +02:00
#include "../../ipc/messageQueueDefinitions.h"
#include "../../ipc/MessageQueueIF.h"
#include "../../objectmanager/frameworkObjects.h"
2021-03-11 14:47:47 +01:00
#include "../../objectmanager/SystemObject.h"
2021-05-05 12:59:42 +02:00
#include "../../storagemanager/StorageManagerIF.h"
2021-03-11 14:47:47 +01:00
#include "../../tasks/ExecutableObjectIF.h"
2021-05-12 16:47:53 +02:00
#ifdef PLATFORM_UNIX
2021-03-21 12:51:28 +01:00
#include <sys/socket.h>
#endif
2021-03-11 14:47:47 +01:00
#include <string>
#include <vector>
2021-05-05 12:59:42 +02:00
class TcpTmTcBridge;
2021-03-11 14:47:47 +01:00
/**
* @brief TCP server implementation
2021-03-11 14:47:47 +01:00
* @details
* This server will run for the whole program lifetime and will take care of serving client
* requests on a specified TCP server port. This server was written in a generic way and
* can be used on Unix and on Windows systems.
*
* If a connection is accepted, the server will read all telecommands sent by a client and then
* send all telemetry currently found in the TMTC bridge FIFO.
*
* Reading telemetry without sending telecommands is possible by connecting, shutting down the
* send operation immediately and then reading the telemetry. It is therefore recommended to
* connect to the server regularly, even if no telecommands need to be sent.
*
* The server will listen to a specific port on all addresses (0.0.0.0).
2021-03-11 14:47:47 +01:00
*/
class TcpTmTcServer:
2021-03-11 14:47:47 +01:00
public SystemObject,
public TcpIpBase,
2021-03-11 14:47:47 +01:00
public ExecutableObjectIF {
public:
/* The ports chosen here should not be used by any other process. */
static const std::string DEFAULT_TCP_SERVER_PORT;
2021-05-13 22:17:21 +02:00
2021-05-05 12:59:42 +02:00
static constexpr size_t ETHERNET_MTU_SIZE = 1500;
2021-03-11 14:47:47 +01:00
/**
* TCP Server Constructor
* @param objectId Object ID of the TCP Server
* @param tmtcTcpBridge Object ID of the TCP TMTC Bridge object
* @param receptionBufferSize This will be the size of the reception buffer. Default buffer
* size will be the Ethernet MTU size
* @param customTcpServerPort The user can specify another port than the default (7301) here.
*/
TcpTmTcServer(object_id_t objectId, object_id_t tmtcTcpBridge,
size_t receptionBufferSize = ETHERNET_MTU_SIZE + 1,
2021-03-11 14:47:47 +01:00
std::string customTcpServerPort = "");
virtual~ TcpTmTcServer();
2021-03-11 14:47:47 +01:00
void setTcpBacklog(uint8_t tcpBacklog);
2021-03-11 14:47:47 +01:00
ReturnValue_t initialize() override;
ReturnValue_t performOperation(uint8_t opCode) override;
2021-05-05 12:59:42 +02:00
ReturnValue_t initializeAfterTaskCreation() override;
2021-03-11 14:47:47 +01:00
2021-05-05 12:59:42 +02:00
protected:
StorageManagerIF* tcStore = nullptr;
StorageManagerIF* tmStore = nullptr;
2021-03-11 14:47:47 +01:00
private:
2021-05-05 12:59:42 +02:00
//! TMTC bridge is cached.
object_id_t tmtcBridgeId = objects::NO_OBJECT;
TcpTmTcBridge* tmtcBridge = nullptr;
2021-03-11 14:47:47 +01:00
std::string tcpPort;
2021-05-05 12:59:42 +02:00
int tcpFlags = 0;
socket_t listenerTcpSocket = 0;
2021-03-21 12:51:28 +01:00
struct sockaddr tcpAddress;
2021-05-05 12:59:42 +02:00
MessageQueueId_t targetTcDestination = MessageQueueIF::NO_QUEUE;
2021-03-11 14:47:47 +01:00
int tcpAddrLen = sizeof(tcpAddress);
int tcpBacklog = 3;
2021-03-11 14:47:47 +01:00
std::vector<uint8_t> receptionBuffer;
int tcpSockOpt = 0;
int tcpTmFlags = 0;
2021-03-11 14:47:47 +01:00
2021-05-05 12:59:42 +02:00
void handleServerOperation(socket_t connSocket);
ReturnValue_t handleTcReception(size_t bytesRecvd);
ReturnValue_t handleTmSending(socket_t connSocket);
2021-03-11 14:47:47 +01:00
};
#endif /* FSFW_OSAL_COMMON_TCP_TMTC_SERVER_H_ */