created common OSAL stuff to unify UDP code

This commit is contained in:
Robin Müller 2021-03-21 00:30:33 +01:00
parent e44f8bfea3
commit d625642abc
13 changed files with 214 additions and 89 deletions

View File

@ -1,3 +1,16 @@
target_sources(${LIB_FSFW_NAME} PRIVATE if(DEFINED WIN32 OR DEFINED UNIX)
tcpipCommon.cpp target_sources(${LIB_FSFW_NAME} PRIVATE
) tcpipCommon.cpp
TcpIpBase.cpp
UdpTcPollingTask.cpp
UdpTmTcBridge.cpp
TcpTmTcServer.cpp
)
endif()
if(WIN32)
target_link_libraries(${LIB_FSFW_NAME} PRIVATE
wsock32
ws2_32
)
endif()

52
osal/common/TcpIpBase.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "TcpIpBase.h"
#ifdef __unix__
#include <unistd.h>
#endif
TcpIpBase::TcpIpBase() {
closeSocket(serverSocket);
}
TcpIpBase::~TcpIpBase() {
#ifdef _WIN32
WSACleanup();
#endif
}
int TcpIpBase::closeSocket(socket_t socket) {
#ifdef _WIN32
return closesocket(socket);
#elif defined(__unix__)
return close(socket)
#endif
}
int TcpIpBase::getLastSocketError() {
#ifdef _WIN32
return WSAGetLastError();
#elif defined(__unix__)
return errno;
#endif
}
ReturnValue_t TcpIpBase::initialize() {
#ifdef _WIN32
/* Initiates Winsock DLL. */
WSAData wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
int err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable Winsock DLL. */
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: " <<
err << std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
#endif
return HasReturnvaluesIF::RETURN_OK;
}

49
osal/common/TcpIpBase.h Normal file
View File

@ -0,0 +1,49 @@
#ifndef FSFW_OSAL_COMMON_TCPIPIF_H_
#define FSFW_OSAL_COMMON_TCPIPIF_H_
#include <fsfw/returnvalues/HasReturnvaluesIF.h>
#ifdef _WIN32
#include <winsock2.h>
#elif defined(__unix__)
#endif
class TcpIpBase {
protected:
#ifdef _WIN32
static constexpr int SHUT_RECV = SD_RECEIVE;
static constexpr int SHUT_SEND = SD_SEND;
static constexpr int SHUT_BOTH = SD_BOTH;
using socket_t = SOCKET;
#elif defined(__unix__)
using socket_t = int;
static constexpr int INVALID_SOCKET = -1;
static constexpr int SOCKET_ERROR = -1;
static constexpr int SHUT_RECV = SHUT_RD;
static constexpr int SHUT_SEND = SHUT_WR;
static constexpr int SHUT_BOTH = SHUT_RDWR;
#endif
TcpIpBase();
virtual ~TcpIpBase();
ReturnValue_t initialize();
int closeSocket(socket_t socket);
int getLastSocketError();
socket_t serverSocket = 0;
};
#endif /* FSFW_OSAL_COMMON_TCPIPIF_H_ */

View File

@ -1,14 +1,16 @@
#include "TcWinTcpServer.h" #include "TcpTmTcServer.h"
#include "tcpipHelpers.h" #include "tcpipHelpers.h"
#include "../../serviceinterface/ServiceInterface.h" #include "../../serviceinterface/ServiceInterface.h"
#ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#endif
const std::string TcWinTcpServer::DEFAULT_TCP_SERVER_PORT = "7301"; const std::string TcpTmTcServer::DEFAULT_TCP_SERVER_PORT = "7301";
const std::string TcWinTcpServer::DEFAULT_TCP_CLIENT_PORT = "7302"; const std::string TcpTmTcServer::DEFAULT_TCP_CLIENT_PORT = "7302";
TcWinTcpServer::TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge, TcpTmTcServer::TcpTmTcServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge,
std::string customTcpServerPort): std::string customTcpServerPort):
SystemObject(objectId), tcpPort(customTcpServerPort) { SystemObject(objectId), tcpPort(customTcpServerPort) {
if(tcpPort == "") { if(tcpPort == "") {
@ -16,23 +18,17 @@ TcWinTcpServer::TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBrid
} }
} }
ReturnValue_t TcWinTcpServer::initialize() { ReturnValue_t TcpTmTcServer::initialize() {
using namespace tcpip; using namespace tcpip;
ReturnValue_t result = TcpIpBase::initialize();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
int retval = 0; int retval = 0;
struct addrinfo *addrResult = nullptr; struct addrinfo *addrResult = nullptr;
struct addrinfo hints; struct addrinfo hints = { 0 };
/* Initiates Winsock DLL. */
WSAData wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
int err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable Winsock DLL. */
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: " <<
err << std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
ZeroMemory(&hints, sizeof (hints)); ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
@ -43,7 +39,7 @@ ReturnValue_t TcWinTcpServer::initialize() {
retval = getaddrinfo(nullptr, tcpPort.c_str(), &hints, &addrResult); retval = getaddrinfo(nullptr, tcpPort.c_str(), &hints, &addrResult);
if (retval != 0) { if (retval != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TcWinTcpServer::TcWinTcpServer: Retrieving address info failed!" << sif::warning << "TcWinTcpServer::TcpTmTcServer: Retrieving address info failed!" <<
std::endl; std::endl;
#endif #endif
handleError(Protocol::TCP, ErrorSources::GETADDRINFO_CALL); handleError(Protocol::TCP, ErrorSources::GETADDRINFO_CALL);
@ -65,7 +61,7 @@ ReturnValue_t TcWinTcpServer::initialize() {
retval = bind(listenerTcpSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen)); retval = bind(listenerTcpSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
if(retval == SOCKET_ERROR) { if(retval == SOCKET_ERROR) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TcWinTcpServer::TcWinTcpServer: Binding socket failed!" << sif::warning << "TcWinTcpServer::TcpTmTcServer: Binding socket failed!" <<
std::endl; std::endl;
#endif #endif
freeaddrinfo(addrResult); freeaddrinfo(addrResult);
@ -77,12 +73,12 @@ ReturnValue_t TcWinTcpServer::initialize() {
} }
TcWinTcpServer::~TcWinTcpServer() { TcpTmTcServer::~TcpTmTcServer() {
closesocket(listenerTcpSocket); closesocket(listenerTcpSocket);
WSACleanup(); WSACleanup();
} }
ReturnValue_t TcWinTcpServer::performOperation(uint8_t opCode) { ReturnValue_t TcpTmTcServer::performOperation(uint8_t opCode) {
using namespace tcpip; using namespace tcpip;
/* If a connection is accepted, the corresponding socket will be assigned to the new socket */ /* If a connection is accepted, the corresponding socket will be assigned to the new socket */
SOCKET clientSocket; SOCKET clientSocket;
@ -110,7 +106,7 @@ ReturnValue_t TcWinTcpServer::performOperation(uint8_t opCode) {
receptionBuffer.size(), 0); receptionBuffer.size(), 0);
if(retval > 0) { if(retval > 0) {
#if FSFW_TCP_RCV_WIRETAPPING_ENABLED == 1 #if FSFW_TCP_RCV_WIRETAPPING_ENABLED == 1
sif::info << "TcWinTcpServer::performOperation: Received " << retval << " bytes." sif::info << "TcpTmTcServer::performOperation: Received " << retval << " bytes."
std::endl; std::endl;
#endif #endif
handleError(Protocol::TCP, ErrorSources::RECV_CALL, 500); handleError(Protocol::TCP, ErrorSources::RECV_CALL, 500);

View File

@ -1,6 +1,7 @@
#ifndef FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_ #ifndef FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_
#define FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_ #define FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_
#include "TcpIpBase.h"
#include "../../objectmanager/SystemObject.h" #include "../../objectmanager/SystemObject.h"
#include "../../tasks/ExecutableObjectIF.h" #include "../../tasks/ExecutableObjectIF.h"
@ -15,17 +16,18 @@
* @details * @details
* Based on: https://docs.microsoft.com/en-us/windows/win32/winsock/complete-server-code * Based on: https://docs.microsoft.com/en-us/windows/win32/winsock/complete-server-code
*/ */
class TcWinTcpServer: class TcpTmTcServer:
public SystemObject, public SystemObject,
public TcpIpBase,
public ExecutableObjectIF { public ExecutableObjectIF {
public: public:
/* The ports chosen here should not be used by any other process. */ /* The ports chosen here should not be used by any other process. */
static const std::string DEFAULT_TCP_SERVER_PORT; static const std::string DEFAULT_TCP_SERVER_PORT;
static const std::string DEFAULT_TCP_CLIENT_PORT; static const std::string DEFAULT_TCP_CLIENT_PORT;
TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge, TcpTmTcServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge,
std::string customTcpServerPort = ""); std::string customTcpServerPort = "");
virtual~ TcWinTcpServer(); virtual~ TcpTmTcServer();
ReturnValue_t initialize() override; ReturnValue_t initialize() override;
ReturnValue_t performOperation(uint8_t opCode) override; ReturnValue_t performOperation(uint8_t opCode) override;
@ -33,7 +35,7 @@ public:
private: private:
std::string tcpPort; std::string tcpPort;
SOCKET listenerTcpSocket = 0; socket_t listenerTcpSocket = 0;
struct sockaddr_in tcpAddress; struct sockaddr_in tcpAddress;
int tcpAddrLen = sizeof(tcpAddress); int tcpAddrLen = sizeof(tcpAddress);
int currentBacklog = 3; int currentBacklog = 3;

View File

@ -1,4 +1,4 @@
#include "TcWinUdpPollingTask.h" #include "UdpTcPollingTask.h"
#include "tcpipHelpers.h" #include "tcpipHelpers.h"
#include "../../globalfunctions/arrayprinter.h" #include "../../globalfunctions/arrayprinter.h"
#include "../../serviceinterface/ServiceInterfaceStream.h" #include "../../serviceinterface/ServiceInterfaceStream.h"
@ -8,7 +8,7 @@
//! Debugging preprocessor define. //! Debugging preprocessor define.
#define FSFW_UDP_RCV_WIRETAPPING_ENABLED 0 #define FSFW_UDP_RCV_WIRETAPPING_ENABLED 0
TcWinUdpPollingTask::TcWinUdpPollingTask(object_id_t objectId, UdpTcPollingTask::UdpTcPollingTask(object_id_t objectId,
object_id_t tmtcUnixUdpBridge, size_t frameSize, object_id_t tmtcUnixUdpBridge, size_t frameSize,
double timeoutSeconds): SystemObject(objectId), double timeoutSeconds): SystemObject(objectId),
tmtcBridgeId(tmtcUnixUdpBridge) { tmtcBridgeId(tmtcUnixUdpBridge) {
@ -32,9 +32,9 @@ TcWinUdpPollingTask::TcWinUdpPollingTask(object_id_t objectId,
} }
} }
TcWinUdpPollingTask::~TcWinUdpPollingTask() {} UdpTcPollingTask::~UdpTcPollingTask() {}
ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) { ReturnValue_t UdpTcPollingTask::performOperation(uint8_t opCode) {
/* Sender Address is cached here. */ /* Sender Address is cached here. */
struct sockaddr_in senderAddress; struct sockaddr_in senderAddress;
int senderAddressSize = sizeof(senderAddress); int senderAddressSize = sizeof(senderAddress);
@ -52,13 +52,13 @@ ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) {
if(bytesReceived == SOCKET_ERROR) { if(bytesReceived == SOCKET_ERROR) {
/* Handle error */ /* Handle error */
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcWinUdpPollingTask::performOperation: Reception error." << std::endl; sif::error << "UdpTcPollingTask::performOperation: Reception error." << std::endl;
#endif #endif
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::RECVFROM_CALL, 1000); tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::RECVFROM_CALL, 1000);
continue; continue;
} }
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1
sif::debug << "TcWinUdpPollingTask::performOperation: " << bytesReceived << sif::debug << "UdpTcPollingTask::performOperation: " << bytesReceived <<
" bytes received" << std::endl; " bytes received" << std::endl;
#endif #endif
@ -72,7 +72,7 @@ ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) {
} }
ReturnValue_t TcWinUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) { ReturnValue_t UdpTcPollingTask::handleSuccessfullTcRead(size_t bytesRead) {
store_address_t storeId; store_address_t storeId;
#if FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1 #if FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1
@ -83,7 +83,7 @@ ReturnValue_t TcWinUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) {
if (result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning<< "TcWinUdpPollingTask::transferPusToSoftwareBus: Data storage failed." << sif::warning<< "UdpTcPollingTask::transferPusToSoftwareBus: Data storage failed." <<
std::endl; std::endl;
sif::warning << "Packet size: " << bytesRead << std::endl; sif::warning << "Packet size: " << bytesRead << std::endl;
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
@ -97,7 +97,7 @@ ReturnValue_t TcWinUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) {
if (result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TcWinUdpPollingTask::handleSuccessfullTcRead: " sif::warning << "UdpTcPollingTask::handleSuccessfullTcRead: "
" Sending message to queue failed" << std::endl; " Sending message to queue failed" << std::endl;
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
@ -106,37 +106,44 @@ ReturnValue_t TcWinUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) {
return result; return result;
} }
ReturnValue_t TcWinUdpPollingTask::initialize() { ReturnValue_t UdpTcPollingTask::initialize() {
tcStore = objectManager->get<StorageManagerIF>(objects::TC_STORE); tcStore = objectManager->get<StorageManagerIF>(objects::TC_STORE);
if (tcStore == nullptr) { if (tcStore == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcWinUdpPollingTask::initialize: TC store uninitialized!" << std::endl; sif::error << "UdpTcPollingTask::initialize: TC store uninitialized!" << std::endl;
#endif #endif
return ObjectManagerIF::CHILD_INIT_FAILED; return ObjectManagerIF::CHILD_INIT_FAILED;
} }
tmtcBridge = objectManager->get<TmTcWinUdpBridge>(tmtcBridgeId); tmtcBridge = objectManager->get<UdpTmTcBridge>(tmtcBridgeId);
if(tmtcBridge == nullptr) { if(tmtcBridge == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcWinUdpPollingTask::initialize: Invalid TMTC bridge object!" << sif::error << "UdpTcPollingTask::initialize: Invalid TMTC bridge object!" <<
std::endl; std::endl;
#endif #endif
return ObjectManagerIF::CHILD_INIT_FAILED; return ObjectManagerIF::CHILD_INIT_FAILED;
} }
ReturnValue_t result = TcpIpBase::initialize();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t TcWinUdpPollingTask::initializeAfterTaskCreation() { ReturnValue_t UdpTcPollingTask::initializeAfterTaskCreation() {
/* Initialize the destination after task creation. This ensures /* Initialize the destination after task creation. This ensures
that the destination has already been set in the TMTC bridge. */ that the destination has already been set in the TMTC bridge. */
targetTcDestination = tmtcBridge->getRequestQueue(); targetTcDestination = tmtcBridge->getRequestQueue();
/* The server socket is set up in the bridge intialization. Calling this function here /* The server socket is set up in the bridge intialization. Calling this function here
ensures that it is set up properly in any case*/ ensures that it is set up regardless of which class was initialized first */
serverUdpSocket = tmtcBridge->serverSocket; serverUdpSocket = tmtcBridge->serverSocket;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void TcWinUdpPollingTask::setTimeout(double timeoutSeconds) { void UdpTcPollingTask::setTimeout(double timeoutSeconds) {
#ifdef _WIN32
DWORD timeoutMs = timeoutSeconds * 1000.0; DWORD timeoutMs = timeoutSeconds * 1000.0;
int result = setsockopt(serverUdpSocket, SOL_SOCKET, SO_RCVTIMEO, int result = setsockopt(serverUdpSocket, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<const char*>(&timeoutMs), sizeof(DWORD)); reinterpret_cast<const char*>(&timeoutMs), sizeof(DWORD));
@ -146,4 +153,6 @@ void TcWinUdpPollingTask::setTimeout(double timeoutSeconds) {
"receive timeout failed with " << strerror(errno) << std::endl; "receive timeout failed with " << strerror(errno) << std::endl;
#endif #endif
} }
#elif defined(__unix__)
#endif
} }

View File

@ -1,7 +1,7 @@
#ifndef FSFW_OSAL_WINDOWS_TCSOCKETPOLLINGTASK_H_ #ifndef FSFW_OSAL_WINDOWS_TCSOCKETPOLLINGTASK_H_
#define FSFW_OSAL_WINDOWS_TCSOCKETPOLLINGTASK_H_ #define FSFW_OSAL_WINDOWS_TCSOCKETPOLLINGTASK_H_
#include "TmTcWinUdpBridge.h" #include "UdpTmTcBridge.h"
#include "../../objectmanager/SystemObject.h" #include "../../objectmanager/SystemObject.h"
#include "../../tasks/ExecutableObjectIF.h" #include "../../tasks/ExecutableObjectIF.h"
#include "../../storagemanager/StorageManagerIF.h" #include "../../storagemanager/StorageManagerIF.h"
@ -17,7 +17,9 @@
* This class caches the IP address of the sender. It is assumed there * This class caches the IP address of the sender. It is assumed there
* is only one sender for now. * is only one sender for now.
*/ */
class TcWinUdpPollingTask: public SystemObject, class UdpTcPollingTask:
public TcpIpBase,
public SystemObject,
public ExecutableObjectIF { public ExecutableObjectIF {
friend class TmTcWinUdpBridge; friend class TmTcWinUdpBridge;
public: public:
@ -25,9 +27,9 @@ public:
//! 0.5 default milliseconds timeout for now. //! 0.5 default milliseconds timeout for now.
static constexpr timeval DEFAULT_TIMEOUT = {0, 500}; static constexpr timeval DEFAULT_TIMEOUT = {0, 500};
TcWinUdpPollingTask(object_id_t objectId, object_id_t tmtcUnixUdpBridge, UdpTcPollingTask(object_id_t objectId, object_id_t tmtcUnixUdpBridge,
size_t frameSize = 0, double timeoutSeconds = -1); size_t frameSize = 0, double timeoutSeconds = -1);
virtual~ TcWinUdpPollingTask(); virtual~ UdpTcPollingTask();
/** /**
* Turn on optional timeout for UDP polling. In the default mode, * Turn on optional timeout for UDP polling. In the default mode,
@ -46,7 +48,7 @@ protected:
private: private:
//! TMTC bridge is cached. //! TMTC bridge is cached.
object_id_t tmtcBridgeId = objects::NO_OBJECT; object_id_t tmtcBridgeId = objects::NO_OBJECT;
TmTcWinUdpBridge* tmtcBridge = nullptr; UdpTmTcBridge* tmtcBridge = nullptr;
MessageQueueId_t targetTcDestination = MessageQueueIF::NO_QUEUE; MessageQueueId_t targetTcDestination = MessageQueueIF::NO_QUEUE;
//! See: https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom //! See: https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom

View File

@ -1,16 +1,24 @@
#include "TmTcWinUdpBridge.h"
#include "tcpipHelpers.h" #include "tcpipHelpers.h"
#include <fsfw/serviceinterface/ServiceInterface.h> #include <fsfw/serviceinterface/ServiceInterface.h>
#include <fsfw/ipc/MutexGuard.h> #include <fsfw/ipc/MutexGuard.h>
#include <fsfw/osal/common/UdpTmTcBridge.h>
#ifdef _WIN32
#include <ws2tcpip.h> #include <ws2tcpip.h>
#elif defined(__unix__)
#include <arap/inet.h>
#endif
//! Debugging preprocessor define. //! Debugging preprocessor define.
#define FSFW_UDP_SEND_WIRETAPPING_ENABLED 0 #define FSFW_UDP_SEND_WIRETAPPING_ENABLED 0
const std::string TmTcWinUdpBridge::DEFAULT_UDP_SERVER_PORT = tcpip::DEFAULT_UDP_SERVER_PORT; const std::string UdpTmTcBridge::DEFAULT_UDP_SERVER_PORT = tcpip::DEFAULT_SERVER_PORT;
TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId, object_id_t tcDestination, UdpTmTcBridge::UdpTmTcBridge(object_id_t objectId, object_id_t tcDestination,
object_id_t tmStoreId, object_id_t tcStoreId, std::string udpServerPort): object_id_t tmStoreId, object_id_t tcStoreId, std::string udpServerPort):
TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) { TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) {
if(udpServerPort == "") { if(udpServerPort == "") {
@ -24,16 +32,18 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId, object_id_t tcDestinati
communicationLinkUp = false; communicationLinkUp = false;
} }
ReturnValue_t TmTcWinUdpBridge::initialize() { ReturnValue_t UdpTmTcBridge::initialize() {
ReturnValue_t result = TmTcBridge::initialize(); ReturnValue_t result = TmTcBridge::initialize();
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::initialize: TmTcBridge initialization failed!" sif::error << "TmTcUdpBridge::initialize: TmTcBridge initialization failed!"
<< std::endl; << std::endl;
#endif #endif
return result; return result;
} }
#ifdef _WIN32
/* Initiates Winsock DLL. */ /* Initiates Winsock DLL. */
WSAData wsaData; WSAData wsaData;
WORD wVersionRequested = MAKEWORD(2, 2); WORD wVersionRequested = MAKEWORD(2, 2);
@ -42,26 +52,28 @@ ReturnValue_t TmTcWinUdpBridge::initialize() {
/* Tell the user that we could not find a usable */ /* Tell the user that we could not find a usable */
/* Winsock DLL. */ /* Winsock DLL. */
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: " << sif::error << "TmTcUdpBridge::TmTcUdpBridge: WSAStartup failed with error: " <<
err << std::endl; err << std::endl;
#else #else
sif::printError("TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: %d\n", sif::printError("TmTcUdpBridge::TmTcUdpBridge: WSAStartup failed with error: %d\n",
err); err);
#endif #endif
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
#endif
struct addrinfo *addrResult = nullptr; struct addrinfo *addrResult = nullptr;
struct addrinfo hints; struct addrinfo hints = { 0 };
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP; hints.ai_protocol = IPPROTO_UDP;
#ifdef _WIN32
/* See: /* See:
https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo
for information about AI_PASSIVE. */ for information about AI_PASSIVE. */
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
#endif
/* Set up UDP socket: /* Set up UDP socket:
https://en.wikipedia.org/wiki/Getaddrinfo https://en.wikipedia.org/wiki/Getaddrinfo
@ -70,7 +82,7 @@ ReturnValue_t TmTcWinUdpBridge::initialize() {
int retval = getaddrinfo(nullptr, udpServerPort.c_str(), &hints, &addrResult); int retval = getaddrinfo(nullptr, udpServerPort.c_str(), &hints, &addrResult);
if (retval != 0) { if (retval != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Retrieving address info failed!" << sif::warning << "TmTcUdpBridge::TmTcUdpBridge: Retrieving address info failed!" <<
std::endl; std::endl;
#endif #endif
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
@ -79,7 +91,7 @@ ReturnValue_t TmTcWinUdpBridge::initialize() {
serverSocket = socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol); serverSocket = socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol);
if(serverSocket == INVALID_SOCKET) { if(serverSocket == INVALID_SOCKET) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open UDP socket!" << sif::warning << "TmTcUdpBridge::TmTcUdpBridge: Could not open UDP socket!" <<
std::endl; std::endl;
#endif #endif
freeaddrinfo(addrResult); freeaddrinfo(addrResult);
@ -90,7 +102,7 @@ ReturnValue_t TmTcWinUdpBridge::initialize() {
retval = bind(serverSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen)); retval = bind(serverSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
if(retval != 0) { if(retval != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind " sif::error << "TmTcUdpBridge::TmTcUdpBridge: Could not bind "
"local port (" << udpServerPort << ") to server socket!" << std::endl; "local port (" << udpServerPort << ") to server socket!" << std::endl;
#endif #endif
freeaddrinfo(addrResult); freeaddrinfo(addrResult);
@ -100,15 +112,13 @@ ReturnValue_t TmTcWinUdpBridge::initialize() {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
TmTcWinUdpBridge::~TmTcWinUdpBridge() { UdpTmTcBridge::~UdpTmTcBridge() {
if(mutex != nullptr) { if(mutex != nullptr) {
MutexFactory::instance()->deleteMutex(mutex); MutexFactory::instance()->deleteMutex(mutex);
} }
closesocket(serverSocket);
WSACleanup();
} }
ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) { ReturnValue_t UdpTmTcBridge::sendTm(const uint8_t *data, size_t dataLen) {
int flags = 0; int flags = 0;
/* The target address can be set by different threads so this lock ensures thread-safety */ /* The target address can be set by different threads so this lock ensures thread-safety */
@ -130,18 +140,18 @@ ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
); );
if(bytesSent == SOCKET_ERROR) { if(bytesSent == SOCKET_ERROR) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::sendTm: Send operation failed." << std::endl; sif::warning << "TmTcUdpBridge::sendTm: Send operation failed." << std::endl;
#endif #endif
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::SENDTO_CALL); tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::SENDTO_CALL);
} }
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were" sif::debug << "TmTcUdpBridge::sendTm: " << bytesSent << " bytes were"
" sent." << std::endl; " sent." << std::endl;
#endif #endif
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in& newAddress) { void UdpTmTcBridge::checkAndSetClientAddress(sockaddr_in& newAddress) {
/* The target address can be set by different threads so this lock ensures thread-safety */ /* The target address can be set by different threads so this lock ensures thread-safety */
MutexGuard lock(mutex, timeoutType, mutexTimeoutMs); MutexGuard lock(mutex, timeoutType, mutexTimeoutMs);
@ -159,7 +169,7 @@ void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in& newAddress) {
clientAddressLen = sizeof(clientAddress); clientAddressLen = sizeof(clientAddress);
} }
void TmTcWinUdpBridge::setMutexProperties(MutexIF::TimeoutType timeoutType, void UdpTmTcBridge::setMutexProperties(MutexIF::TimeoutType timeoutType,
dur_millis_t timeoutMs) { dur_millis_t timeoutMs) {
this->timeoutType = timeoutType; this->timeoutType = timeoutType;
this->mutexTimeoutMs = timeoutMs; this->mutexTimeoutMs = timeoutMs;

View File

@ -1,20 +1,22 @@
#ifndef FSFW_OSAL_WINDOWS_TMTCWINUDPBRIDGE_H_ #ifndef FSFW_OSAL_WINDOWS_TMTCWINUDPBRIDGE_H_
#define FSFW_OSAL_WINDOWS_TMTCWINUDPBRIDGE_H_ #define FSFW_OSAL_WINDOWS_TMTCWINUDPBRIDGE_H_
#include "TcpIpBase.h"
#include "../../tmtcservices/TmTcBridge.h" #include "../../tmtcservices/TmTcBridge.h"
#include <string> #include <string>
#include <winsock2.h>
class TmTcWinUdpBridge: public TmTcBridge { class UdpTmTcBridge:
friend class TcWinUdpPollingTask; public TmTcBridge,
public TcpIpBase {
friend class UdpTcPollingTask;
public: public:
/* The ports chosen here should not be used by any other process. */ /* The ports chosen here should not be used by any other process. */
static const std::string DEFAULT_UDP_SERVER_PORT; static const std::string DEFAULT_UDP_SERVER_PORT;
TmTcWinUdpBridge(object_id_t objectId, object_id_t tcDestination, UdpTmTcBridge(object_id_t objectId, object_id_t tcDestination,
object_id_t tmStoreId, object_id_t tcStoreId, std::string udpServerPort = ""); object_id_t tmStoreId, object_id_t tcStoreId, std::string udpServerPort = "");
virtual~ TmTcWinUdpBridge(); virtual~ UdpTmTcBridge();
/** /**
* Set properties of internal mutex. * Set properties of internal mutex.
@ -29,7 +31,6 @@ protected:
virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override; virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override;
private: private:
SOCKET serverSocket = 0;
std::string udpServerPort; std::string udpServerPort;
struct sockaddr_in clientAddress; struct sockaddr_in clientAddress;

View File

@ -6,8 +6,7 @@
namespace tcpip { namespace tcpip {
const char* const DEFAULT_UDP_SERVER_PORT = "7301"; const char* const DEFAULT_SERVER_PORT = "7301";
const char* const DEFAULT_TCP_SERVER_PORT = "7303";
enum class Protocol { enum class Protocol {
UDP, UDP,

View File

@ -2,7 +2,7 @@
#define FSFW_OSAL_WINDOWS_TCPIPHELPERS_H_ #define FSFW_OSAL_WINDOWS_TCPIPHELPERS_H_
#include "../../timemanager/clockDefinitions.h" #include "../../timemanager/clockDefinitions.h"
#include "../common/tcpipCommon.h" #include "tcpipCommon.h"
namespace tcpip { namespace tcpip {

View File

@ -1,11 +1,3 @@
target_sources(${LIB_FSFW_NAME} PRIVATE target_sources(${LIB_FSFW_NAME} PRIVATE
TcWinUdpPollingTask.cpp
TmTcWinUdpBridge.cpp
TcWinTcpServer.cpp
tcpipHelpers.cpp tcpipHelpers.cpp
) )
target_link_libraries(${LIB_FSFW_NAME} PRIVATE
wsock32
ws2_32
)

View File

@ -1,4 +1,4 @@
#include "tcpipHelpers.h" #include "../common/tcpipHelpers.h"
#include <FSFWConfig.h> #include <FSFWConfig.h>
#include "../../tasks/TaskFactory.h" #include "../../tasks/TaskFactory.h"