diff --git a/osal/windows/TcWinUdpPollingTask.cpp b/osal/windows/TcWinUdpPollingTask.cpp index 38633388..502b798f 100644 --- a/osal/windows/TcWinUdpPollingTask.cpp +++ b/osal/windows/TcWinUdpPollingTask.cpp @@ -49,7 +49,7 @@ ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) { handleReadError(); continue; } -#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_WIRETAPPING_ENABLED == 1 +#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1 sif::debug << "TcWinUdpPollingTask::performOperation: " << bytesReceived << " bytes received" << std::endl; #endif @@ -58,7 +58,6 @@ ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) { if(result != HasReturnvaluesIF::RETURN_FAILED) { } - tmtcBridge->registerCommConnect(); tmtcBridge->checkAndSetClientAddress(senderAddress); } return HasReturnvaluesIF::RETURN_OK; @@ -69,8 +68,8 @@ ReturnValue_t TcWinUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) { store_address_t storeId; ReturnValue_t result = tcStore->addData(&storeId, receptionBuffer.data(), bytesRead); -#if FSFW_UDP_WIRETAPPING_ENABLED == 1 - arrayprinter::print(receptionBuffer.data(), bytesRead);# +#if FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1 + arrayprinter::print(receptionBuffer.data(), bytesRead); #endif if (result != HasReturnvaluesIF::RETURN_OK) { #if FSFW_CPP_OSTREAM_ENABLED == 1 @@ -113,7 +112,7 @@ ReturnValue_t TcWinUdpPollingTask::initialize() { } serverUdpSocket = tmtcBridge->serverSocket; -#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_WIRETAPPING_ENABLED == 1 +#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1 sif::info << "TcWinUdpPollingTask::initialize: Server UDP socket " << serverUdpSocket << std::endl; #endif diff --git a/osal/windows/TmTcWinUdpBridge.cpp b/osal/windows/TmTcWinUdpBridge.cpp index 1a624355..346abcd4 100644 --- a/osal/windows/TmTcWinUdpBridge.cpp +++ b/osal/windows/TmTcWinUdpBridge.cpp @@ -12,13 +12,13 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId, std::string udpServerPort, std::string udpClientPort): TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) { if(udpServerPort == "") { - udpServerPort = DEFAULT_UDP_SERVER_PORT; + this->udpServerPort = DEFAULT_UDP_SERVER_PORT; } else { this->udpServerPort = udpServerPort; } if(udpClientPort == "") { - udpClientPort = DEFAULT_UDP_CLIENT_PORT; + this->udpClientPort = DEFAULT_UDP_CLIENT_PORT; } else { this->udpClientPort = udpClientPort; @@ -60,11 +60,12 @@ ReturnValue_t TmTcWinUdpBridge::initialize() { hints.ai_protocol = IPPROTO_UDP; hints.ai_flags = AI_PASSIVE; + sif::info << udpServerPort << std::endl; /* Set up UDP socket: - https://en.wikipedia.org/wiki/Getaddrinfo - Passing nullptr as the first parameter and specifying AI_PASSIVE in hints will cause - getaddrinfo to assign the address 0.0.0.0 (any address) - */ + https://en.wikipedia.org/wiki/Getaddrinfo + Passing nullptr as the first parameter and specifying AI_PASSIVE in hints will cause + getaddrinfo to assign the address 0.0.0.0 (any address) + */ int retval = getaddrinfo(nullptr, udpServerPort.c_str(), &hints, &addrResult); if (retval != 0) { #if FSFW_CPP_OSTREAM_ENABLED == 1 @@ -80,40 +81,22 @@ ReturnValue_t TmTcWinUdpBridge::initialize() { sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open UDP socket!" << std::endl; #endif + freeaddrinfo(addrResult); handleSocketError(); return HasReturnvaluesIF::RETURN_FAILED; } -// serverAddress.sin_family = AF_INET; -// -// /* Accept packets from any interface. (potentially insecure). */ -// serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); -// serverAddress.sin_port = htons(setServerPort); -// serverAddressLen = sizeof(serverAddress); -// int result = setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, -// reinterpret_cast(&serverSocketOptions), -// sizeof(serverSocketOptions)); -// if(result != 0) { -//#if FSFW_CPP_OSTREAM_ENABLED == 1 -// sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not set socket options!" << -// std::endl; -//#endif -// handleSocketError(); -// } -// - clientAddress.sin_family = AF_INET; - clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); - clientAddress.sin_port = htons(7302); - clientAddressLen = sizeof(clientAddress); - retval = bind(serverSocket, addrResult->ai_addr, static_cast(addrResult->ai_addrlen)); if(retval != 0) { #if FSFW_CPP_OSTREAM_ENABLED == 1 sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind " "local port " << udpServerPort << " to server socket!" << std::endl; #endif + freeaddrinfo(addrResult); handleBindError(); } + freeaddrinfo(addrResult); + return HasReturnvaluesIF::RETURN_OK; } @@ -126,6 +109,7 @@ TmTcWinUdpBridge::~TmTcWinUdpBridge() { } ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) { + MutexGuard lock(mutex, MutexIF::TimeoutType::WAITING, 10); int flags = 0; #if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1 @@ -153,7 +137,7 @@ ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) { return HasReturnvaluesIF::RETURN_OK; } -void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) { +void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in& newAddress) { MutexGuard lock(mutex, MutexIF::TimeoutType::WAITING, 10); #if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1 @@ -167,7 +151,7 @@ void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) { /* Set new IP address if it has changed. */ if(clientAddress.sin_addr.s_addr != newAddress.sin_addr.s_addr) { - clientAddress.sin_addr.s_addr = newAddress.sin_addr.s_addr; + clientAddress = newAddress; clientAddressLen = sizeof(clientAddress); } } @@ -257,3 +241,69 @@ void TmTcWinUdpBridge::handleSendError() { } } +ReturnValue_t TmTcWinUdpBridge::oldSetup() { + ReturnValue_t result = TmTcBridge::initialize(); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + // 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 + } + + uint16_t setServerPort = 7301; + uint16_t setClientPort = 7302; + + // Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html + //clientSocket = socket(AF_INET, SOCK_DGRAM, 0); + serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if(serverSocket == INVALID_SOCKET) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open" + " UDP socket!" << std::endl; +#endif + handleSocketError(); + } + + serverAddress.sin_family = AF_INET; + + // Accept packets from any interface. (potentially insecure). + serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); + serverAddress.sin_port = htons(setServerPort); + serverAddressLen = sizeof(serverAddress); + +// sif::info << serverAddress.sin_addr.s_addr << std::endl; +// sif::info << serverAddress.sin_port << std::endl; + + setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&serverSocketOptions), + sizeof(serverSocketOptions)); + + clientAddress.sin_family = AF_INET; + clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); + clientAddress.sin_port = htons(setClientPort); + clientAddressLen = sizeof(clientAddress); + + int retval = bind(serverSocket, + reinterpret_cast(&serverAddress), + serverAddressLen); + if(retval != 0) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind " + "local port " << setServerPort << " to server socket!" + << std::endl; +#endif + handleBindError(); + } + + return HasReturnvaluesIF::RETURN_OK; +} + diff --git a/osal/windows/TmTcWinUdpBridge.h b/osal/windows/TmTcWinUdpBridge.h index e32854b6..b433f6f1 100644 --- a/osal/windows/TmTcWinUdpBridge.h +++ b/osal/windows/TmTcWinUdpBridge.h @@ -23,7 +23,7 @@ public: ReturnValue_t initialize() override; - void checkAndSetClientAddress(sockaddr_in clientAddress); + void checkAndSetClientAddress(sockaddr_in& clientAddress); protected: virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override; @@ -53,6 +53,8 @@ private: void handleSocketError(); void handleBindError(); void handleSendError(); + + ReturnValue_t oldSetup(); };