From b7c4f5ce05ec1c235cbea3b825d21ce4497dd095 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Sun, 6 Sep 2020 10:36:39 +0200 Subject: [PATCH] continued win sockets --- osal/linux/TcUnixUdpPollingTask.cpp | 2 +- osal/linux/TmTcUnixUdpBridge.cpp | 3 +- osal/{host => windows}/TmTcWinUdpBridge.cpp | 73 ++++++++++++++++++--- osal/{host => windows}/TmTcWinUdpBridge.h | 10 +-- 4 files changed, 73 insertions(+), 15 deletions(-) rename osal/{host => windows}/TmTcWinUdpBridge.cpp (54%) rename osal/{host => windows}/TmTcWinUdpBridge.h (88%) diff --git a/osal/linux/TcUnixUdpPollingTask.cpp b/osal/linux/TcUnixUdpPollingTask.cpp index d8a398699..95910b028 100644 --- a/osal/linux/TcUnixUdpPollingTask.cpp +++ b/osal/linux/TcUnixUdpPollingTask.cpp @@ -1,4 +1,4 @@ -#include "../../osal/linux/TcUnixUdpPollingTask.h" +#include "TcUnixUdpPollingTask.h" #include "../../globalfunctions/arrayprinter.h" TcUnixUdpPollingTask::TcUnixUdpPollingTask(object_id_t objectId, diff --git a/osal/linux/TmTcUnixUdpBridge.cpp b/osal/linux/TmTcUnixUdpBridge.cpp index d0c9517fc..2549a4b79 100644 --- a/osal/linux/TmTcUnixUdpBridge.cpp +++ b/osal/linux/TmTcUnixUdpBridge.cpp @@ -1,10 +1,11 @@ -#include "../../osal/linux/TmTcUnixUdpBridge.h" +#include "TmTcUnixUdpBridge.h" #include "../../serviceinterface/ServiceInterfaceStream.h" #include "../../ipc/MutexHelper.h" #include #include + TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId, object_id_t tcDestination, object_id_t tmStoreId, object_id_t tcStoreId, uint16_t serverPort, uint16_t clientPort): diff --git a/osal/host/TmTcWinUdpBridge.cpp b/osal/windows/TmTcWinUdpBridge.cpp similarity index 54% rename from osal/host/TmTcWinUdpBridge.cpp rename to osal/windows/TmTcWinUdpBridge.cpp index 4bb9a3b1f..c5902e884 100644 --- a/osal/host/TmTcWinUdpBridge.cpp +++ b/osal/windows/TmTcWinUdpBridge.cpp @@ -6,6 +6,18 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId, TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) { mutex = MutexFactory::instance()->createMutex(); + // 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. */ + sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge:" + "WSAStartup failed with error: " << err << std::endl; + return; + } + uint16_t setServerPort = DEFAULT_UDP_SERVER_PORT; if(serverPort != 0xFFFF) { setServerPort = serverPort; @@ -45,8 +57,8 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId, int result = bind(serverSocket, reinterpret_cast(&serverAddress), serverAddressLen); - if(result == -1) { - sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not bind " + if(result != 0) { + sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind " "local port " << setServerPort << " to server socket!" << std::endl; handleBindError(); @@ -54,12 +66,8 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId, } } -TmTcWinUdpBridge::~TmTcWinUdpBridge() {} - -void TmTcWinUdpBridge::handleSocketError() { -} - -void TmTcWinUdpBridge::handleBindError() { +TmTcWinUdpBridge::~TmTcWinUdpBridge() { + WSACleanup(); } ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) { @@ -69,5 +77,52 @@ ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) { void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in clientAddress) { } -void TmTcWinUdpBridge::handleSendError() { +void TmTcWinUdpBridge::handleSocketError() { + int errCode = WSAGetLastError(); + switch(errCode) { + case(WSANOTINITIALISED): { + sif::info << "TmTcWinUdpBridge::handleSocketError: WSANOTINITIALISED: " + << "WSAStartup(...) call " << "necessary" << std::endl; + break; + } + default: { + sif::info << "TmTcWinUdpBridge::handleSocketError: Error code: " + << errCode << std::endl; + break; + } + } } + +void TmTcWinUdpBridge::handleBindError() { + int errCode = WSAGetLastError(); + switch(errCode) { + case(WSANOTINITIALISED): { + sif::info << "TmTcWinUdpBridge::handleBindError: WSANOTINITIALISED: " + << "WSAStartup(...) call " << "necessary" << std::endl; + break; + } + default: { + sif::info << "TmTcWinUdpBridge::handleBindError: Error code: " + << errCode << std::endl; + break; + } + } +} + +void TmTcWinUdpBridge::handleSendError() { + int errCode = WSAGetLastError(); + switch(errCode) { + case(WSANOTINITIALISED): { + sif::info << "TmTcWinUdpBridge::handleSendError: WSANOTINITIALISED: " + << "WSAStartup(...) call " << "necessary" << std::endl; + break; + } + default: { + sif::info << "TmTcWinUdpBridge::handleSendError: Error code: " + << errCode << std::endl; + break; + } + } +} + +#endif diff --git a/osal/host/TmTcWinUdpBridge.h b/osal/windows/TmTcWinUdpBridge.h similarity index 88% rename from osal/host/TmTcWinUdpBridge.h rename to osal/windows/TmTcWinUdpBridge.h index 8e6bb0fda..8188039c0 100644 --- a/osal/host/TmTcWinUdpBridge.h +++ b/osal/windows/TmTcWinUdpBridge.h @@ -1,14 +1,15 @@ -#ifndef FSFW_OSAL_HOST_TMTCWINUDPBRIDGE_H_ -#define FSFW_OSAL_HOST_TMTCWINUDPBRIDGE_H_ +#ifndef FSFW_OSAL_WINDOWS_TMTCWINUDPBRIDGE_H_ +#define FSFW_OSAL_WINDOWS_TMTCWINUDPBRIDGE_H_ #include "../../tmtcservices/TmTcBridge.h" -#include + +#include +#include class TmTcWinUdpBridge: public TmTcBridge { friend class TcWinUdpPollingTask; public: // The ports chosen here should not be used by any other process. - // List of used ports on Linux: /etc/services static constexpr uint16_t DEFAULT_UDP_SERVER_PORT = 7301; static constexpr uint16_t DEFAULT_UDP_CLIENT_PORT = 7302; @@ -45,3 +46,4 @@ private: #endif /* FSFW_OSAL_HOST_TMTCWINUDPBRIDGE_H_ */ +