fsfw/osal/common/TcpTmTcServer.cpp

227 lines
7.7 KiB
C++
Raw Normal View History

#include "TcpTmTcServer.h"
2021-05-05 12:59:42 +02:00
#include "TcpTmTcBridge.h"
2021-03-12 02:15:21 +01:00
#include "tcpipHelpers.h"
2021-05-05 12:59:42 +02:00
2021-05-12 16:47:53 +02:00
#include "../../platform.h"
2021-05-05 12:59:42 +02:00
#include "../../container/SharedRingBuffer.h"
#include "../../ipc/MessageQueueSenderIF.h"
#include "../../ipc/MutexGuard.h"
2021-05-05 12:59:42 +02:00
#include "../../objectmanager/ObjectManagerIF.h"
2021-03-11 14:47:47 +01:00
#include "../../serviceinterface/ServiceInterface.h"
2021-05-05 12:59:42 +02:00
#include "../../tmtcservices/TmTcMessage.h"
2021-03-11 14:47:47 +01:00
2021-05-12 16:47:53 +02:00
#ifdef PLATFORM_WIN
2021-03-11 14:47:47 +01:00
#include <winsock2.h>
#include <ws2tcpip.h>
2021-05-12 16:47:53 +02:00
#elif defined(PLATFORM_UNIX)
2021-03-21 12:51:28 +01:00
#include <netdb.h>
#endif
2021-03-11 14:47:47 +01:00
2021-05-05 12:59:42 +02:00
#ifndef FSFW_TCP_RECV_WIRETAPPING_ENABLED
#define FSFW_TCP_RECV_WIRETAPPING_ENABLED 0
#endif
const std::string TcpTmTcServer::DEFAULT_TCP_SERVER_PORT = "7303";
2021-03-11 14:47:47 +01:00
2021-05-05 12:59:42 +02:00
TcpTmTcServer::TcpTmTcServer(object_id_t objectId, object_id_t tmtcTcpBridge,
size_t receptionBufferSize, std::string customTcpServerPort):
SystemObject(objectId), tmtcBridgeId(tmtcTcpBridge),
tcpPort(customTcpServerPort), receptionBuffer(receptionBufferSize) {
2021-03-11 14:47:47 +01:00
if(tcpPort == "") {
tcpPort = DEFAULT_TCP_SERVER_PORT;
}
}
ReturnValue_t TcpTmTcServer::initialize() {
2021-03-12 02:15:21 +01:00
using namespace tcpip;
ReturnValue_t result = TcpIpBase::initialize();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
2021-05-05 12:59:42 +02:00
tcStore = objectManager->get<StorageManagerIF>(objects::TC_STORE);
if (tcStore == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcpTmTcServer::initialize: TC store uninitialized!" << std::endl;
#else
sif::printError("TcpTmTcServer::initialize: TC store uninitialized!\n");
#endif
return ObjectManagerIF::CHILD_INIT_FAILED;
}
tmtcBridge = objectManager->get<TcpTmTcBridge>(tmtcBridgeId);
2021-03-11 14:47:47 +01:00
int retval = 0;
struct addrinfo *addrResult = nullptr;
struct addrinfo hints = {};
2021-03-11 14:47:47 +01:00
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Listen to all addresses (0.0.0.0) by using AI_PASSIVE in the hint flags
2021-03-11 14:47:47 +01:00
retval = getaddrinfo(nullptr, tcpPort.c_str(), &hints, &addrResult);
if (retval != 0) {
2021-03-12 02:15:21 +01:00
handleError(Protocol::TCP, ErrorSources::GETADDRINFO_CALL);
2021-03-11 14:47:47 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
// Open TCP (stream) socket
2021-03-11 14:47:47 +01:00
listenerTcpSocket = socket(addrResult->ai_family, addrResult->ai_socktype,
addrResult->ai_protocol);
if(listenerTcpSocket == INVALID_SOCKET) {
freeaddrinfo(addrResult);
2021-03-12 02:15:21 +01:00
handleError(Protocol::TCP, ErrorSources::SOCKET_CALL);
2021-03-11 14:47:47 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
// Bind to the address found by getaddrinfo
2021-03-12 00:34:30 +01:00
retval = bind(listenerTcpSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
2021-03-11 14:47:47 +01:00
if(retval == SOCKET_ERROR) {
freeaddrinfo(addrResult);
2021-03-12 02:15:21 +01:00
handleError(Protocol::TCP, ErrorSources::BIND_CALL);
2021-04-08 18:48:44 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
2021-03-11 14:47:47 +01:00
}
freeaddrinfo(addrResult);
return HasReturnvaluesIF::RETURN_OK;
}
TcpTmTcServer::~TcpTmTcServer() {
2021-03-21 12:51:28 +01:00
closeSocket(listenerTcpSocket);
2021-03-11 14:47:47 +01:00
}
ReturnValue_t TcpTmTcServer::performOperation(uint8_t opCode) {
2021-03-12 02:15:21 +01:00
using namespace tcpip;
// If a connection is accepted, the corresponding socket will be assigned to the new socket
2021-05-05 12:59:42 +02:00
socket_t connSocket = 0;
2021-05-13 22:17:21 +02:00
// sockaddr clientSockAddr = {};
// socklen_t connectorSockAddrLen = 0;
2021-03-11 14:47:47 +01:00
int retval = 0;
2021-03-12 02:15:21 +01:00
// Listen for connection requests permanently for lifetime of program
2021-03-11 14:47:47 +01:00
while(true) {
retval = listen(listenerTcpSocket, tcpBacklog);
2021-03-11 14:47:47 +01:00
if(retval == SOCKET_ERROR) {
2021-03-12 02:15:21 +01:00
handleError(Protocol::TCP, ErrorSources::LISTEN_CALL, 500);
2021-03-11 14:47:47 +01:00
continue;
}
2021-05-13 22:17:21 +02:00
//connSocket = accept(listenerTcpSocket, &clientSockAddr, &connectorSockAddrLen);
connSocket = accept(listenerTcpSocket, nullptr, nullptr);
2021-03-11 14:47:47 +01:00
2021-05-05 12:59:42 +02:00
if(connSocket == INVALID_SOCKET) {
2021-03-12 02:15:21 +01:00
handleError(Protocol::TCP, ErrorSources::ACCEPT_CALL, 500);
2021-05-05 12:59:42 +02:00
closeSocket(connSocket);
2021-03-11 14:47:47 +01:00
continue;
};
2021-05-05 12:59:42 +02:00
handleServerOperation(connSocket);
// Done, shut down connection and go back to listening for client requests
2021-05-05 12:59:42 +02:00
retval = shutdown(connSocket, SHUT_SEND);
if(retval != 0) {
handleError(Protocol::TCP, ErrorSources::SHUTDOWN_CALL);
2021-03-11 14:47:47 +01:00
}
2021-05-05 12:59:42 +02:00
closeSocket(connSocket);
}
return HasReturnvaluesIF::RETURN_OK;
}
2021-03-11 14:47:47 +01:00
2021-05-05 12:59:42 +02:00
ReturnValue_t TcpTmTcServer::initializeAfterTaskCreation() {
if(tmtcBridge == nullptr) {
return ObjectManagerIF::CHILD_INIT_FAILED;
}
2021-05-05 12:59:42 +02:00
/* Initialize the destination after task creation. This ensures
that the destination has already been set in the TMTC bridge. */
targetTcDestination = tmtcBridge->getRequestQueue();
tcStore = tmtcBridge->tcStore;
tmStore = tmtcBridge->tmStore;
2021-05-05 12:59:42 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
void TcpTmTcServer::handleServerOperation(socket_t connSocket) {
int retval = 0;
do {
// Read all telecommands sent by the client
2021-05-05 12:59:42 +02:00
retval = recv(connSocket,
reinterpret_cast<char*>(receptionBuffer.data()),
receptionBuffer.capacity(),
tcpFlags);
if (retval > 0) {
handleTcReception(retval);
}
else if(retval == 0) {
// Client has finished sending telecommands, send telemetry now
handleTmSending(connSocket);
2021-03-11 14:47:47 +01:00
}
else {
// Should not happen
tcpip::handleError(tcpip::Protocol::TCP, tcpip::ErrorSources::RECV_CALL);
2021-03-11 14:47:47 +01:00
}
2021-05-05 12:59:42 +02:00
} while(retval > 0);
}
2021-03-11 14:47:47 +01:00
2021-05-05 12:59:42 +02:00
ReturnValue_t TcpTmTcServer::handleTcReception(size_t bytesRecvd) {
#if FSFW_TCP_RECV_WIRETAPPING_ENABLED == 1
arrayprinter::print(receptionBuffer.data(), bytesRead);
#endif
store_address_t storeId;
ReturnValue_t result = tcStore->addData(&storeId, receptionBuffer.data(), bytesRecvd);
if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning<< "TcpTmTcServer::handleServerOperation: Data storage failed." << std::endl;
sif::warning << "Packet size: " << bytesRecvd << std::endl;
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
2021-03-11 14:47:47 +01:00
}
2021-05-05 12:59:42 +02:00
TmTcMessage message(storeId);
2021-03-11 14:47:47 +01:00
2021-05-05 12:59:42 +02:00
result = MessageQueueSenderIF::sendMessage(targetTcDestination, &message);
if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UdpTcPollingTask::handleSuccessfullTcRead: "
" Sending message to queue failed" << std::endl;
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
tcStore->deleteData(storeId);
}
return result;
2021-03-11 14:47:47 +01:00
}
void TcpTmTcServer::setTcpBacklog(uint8_t tcpBacklog) {
this->tcpBacklog = tcpBacklog;
}
2021-03-11 14:47:47 +01:00
ReturnValue_t TcpTmTcServer::handleTmSending(socket_t connSocket) {
// Access to the FIFO is mutex protected because it is filled by the bridge
MutexGuard(tmtcBridge->mutex, tmtcBridge->timeoutType, tmtcBridge->mutexTimeoutMs);
store_address_t storeId;
while((not tmtcBridge->tmFifo->empty()) and
(tmtcBridge->packetSentCounter < tmtcBridge->sentPacketsPerCycle)) {
tmtcBridge->tmFifo->retrieve(&storeId);
// Using the store accessor will take care of deleting TM from the store automatically
ConstStorageAccessor storeAccessor(storeId);
ReturnValue_t result = tmStore->getData(storeId, storeAccessor);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
int retval = send(connSocket,
reinterpret_cast<const char*>(storeAccessor.data()),
storeAccessor.size(),
tcpTmFlags);
if(retval != static_cast<int>(storeAccessor.size())) {
tcpip::handleError(tcpip::Protocol::TCP, tcpip::ErrorSources::SEND_CALL);
}
}
return HasReturnvaluesIF::RETURN_OK;
}