fsfw/osal/windows/TmTcWinUdpBridge.cpp

255 lines
8.3 KiB
C++
Raw Normal View History

2020-09-06 15:46:49 +02:00
#include "TmTcWinUdpBridge.h"
2021-03-12 00:34:30 +01:00
#include <fsfw/serviceinterface/ServiceInterface.h>
2021-03-09 21:25:22 +01:00
#include <fsfw/ipc/MutexGuard.h>
2021-03-12 00:34:30 +01:00
#include <ws2tcpip.h>
2021-03-07 01:35:55 +01:00
2021-03-12 00:34:30 +01:00
const std::string TmTcWinUdpBridge::DEFAULT_UDP_SERVER_PORT = "7301";
const std::string TmTcWinUdpBridge::DEFAULT_UDP_CLIENT_PORT = "7302";
2021-03-07 01:35:55 +01:00
2020-09-06 15:46:49 +02:00
TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
object_id_t tcDestination, object_id_t tmStoreId, object_id_t tcStoreId,
2021-03-12 00:34:30 +01:00
std::string udpServerPort, std::string udpClientPort):
2020-09-06 15:46:49 +02:00
TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) {
2021-03-12 00:34:30 +01:00
if(udpServerPort == "") {
udpServerPort = DEFAULT_UDP_SERVER_PORT;
}
else {
this->udpServerPort = udpServerPort;
}
if(udpClientPort == "") {
udpClientPort = DEFAULT_UDP_CLIENT_PORT;
}
else {
this->udpClientPort = udpClientPort;
}
2020-09-06 15:46:49 +02:00
mutex = MutexFactory::instance()->createMutex();
2020-12-02 00:27:53 +01:00
communicationLinkUp = false;
2021-03-12 00:34:30 +01:00
}
2020-09-06 15:46:49 +02:00
2021-03-12 00:34:30 +01:00
ReturnValue_t TmTcWinUdpBridge::initialize() {
2021-03-08 23:55:58 +01:00
/* Initiates Winsock DLL. */
2020-09-06 15:46:49 +02:00
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. */
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-03-12 00:34:30 +01:00
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: " <<
err << std::endl;
#else
sif::printError("TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: %d\n",
err);
#endif
2021-03-12 00:34:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
2020-09-06 15:46:49 +02:00
}
2021-03-12 00:34:30 +01:00
struct addrinfo *addrResult = nullptr;
struct addrinfo hints;
2020-09-06 15:46:49 +02:00
2021-03-12 00:34:30 +01:00
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
2020-09-06 15:46:49 +02:00
2021-03-08 23:02:06 +01:00
/* Set up UDP socket:
2021-03-12 00:34:30 +01:00
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)
2021-03-08 23:02:06 +01:00
*/
2021-03-12 00:34:30 +01:00
int retval = getaddrinfo(nullptr, udpServerPort.c_str(), &hints, &addrResult);
if (retval != 0) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-03-12 00:34:30 +01:00
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Retrieving address info failed!" <<
2021-03-08 23:14:10 +01:00
std::endl;
#endif
2021-03-12 00:34:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
2020-09-06 15:46:49 +02:00
}
2021-03-12 00:34:30 +01:00
serverSocket = socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol);
if(serverSocket == INVALID_SOCKET) {
2021-03-08 23:14:10 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-03-12 00:34:30 +01:00
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open UDP socket!" <<
2021-03-08 23:14:10 +01:00
std::endl;
#endif
handleSocketError();
2021-03-12 00:34:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
2021-03-08 23:14:10 +01:00
}
2020-09-06 15:46:49 +02:00
2021-03-12 00:34:30 +01:00
// 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<const char*>(&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();
// }
//
2020-09-06 15:46:49 +02:00
clientAddress.sin_family = AF_INET;
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
2021-03-12 00:34:30 +01:00
clientAddress.sin_port = htons(7302);
2020-09-06 15:46:49 +02:00
clientAddressLen = sizeof(clientAddress);
2021-03-12 00:34:30 +01:00
retval = bind(serverSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
if(retval != 0) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-06 15:46:49 +02:00
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind "
2021-03-12 00:34:30 +01:00
"local port " << udpServerPort << " to server socket!" << std::endl;
#endif
2020-09-06 15:46:49 +02:00
handleBindError();
}
2021-03-12 00:34:30 +01:00
return HasReturnvaluesIF::RETURN_OK;
2020-09-06 15:46:49 +02:00
}
TmTcWinUdpBridge::~TmTcWinUdpBridge() {
2021-03-12 00:34:30 +01:00
if(mutex != nullptr) {
MutexFactory::instance()->deleteMutex(mutex);
}
2021-03-08 23:55:58 +01:00
closesocket(serverSocket);
2020-09-06 15:46:49 +02:00
WSACleanup();
}
ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
int flags = 0;
2021-03-08 23:00:53 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
clientAddress.sin_addr.s_addr = htons(INADDR_ANY);
clientAddressLen = sizeof(serverAddress);
char ipAddress [15];
sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#endif
2020-09-06 15:46:49 +02:00
2021-03-12 00:34:30 +01:00
int bytesSent = sendto(serverSocket,
2020-09-06 15:46:49 +02:00
reinterpret_cast<const char*>(data), dataLen, flags,
reinterpret_cast<sockaddr*>(&clientAddress), clientAddressLen);
if(bytesSent == SOCKET_ERROR) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-06 15:46:49 +02:00
sif::error << "TmTcWinUdpBridge::sendTm: Send operation failed."
<< std::endl;
#endif
2020-09-06 15:46:49 +02:00
handleSendError();
}
2021-03-08 23:00:53 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were"
" sent." << std::endl;
#endif
2020-09-06 15:46:49 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) {
2021-03-09 11:25:13 +01:00
MutexGuard lock(mutex, MutexIF::TimeoutType::WAITING, 10);
2020-09-06 15:46:49 +02:00
2021-03-08 23:00:53 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
char ipAddress [15];
sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
&newAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
sif::debug << "IP Address Old: " << inet_ntop(AF_INET,
&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#endif
2020-12-02 00:27:53 +01:00
registerCommConnect();
2020-09-06 15:46:49 +02:00
2021-03-08 23:00:53 +01:00
/* Set new IP address if it has changed. */
2020-09-06 15:46:49 +02:00
if(clientAddress.sin_addr.s_addr != newAddress.sin_addr.s_addr) {
clientAddress.sin_addr.s_addr = newAddress.sin_addr.s_addr;
clientAddressLen = sizeof(clientAddress);
}
}
void TmTcWinUdpBridge::handleSocketError() {
int errCode = WSAGetLastError();
switch(errCode) {
case(WSANOTINITIALISED): {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-03-08 23:00:53 +01:00
sif::warning << "TmTcWinUdpBridge::handleSocketError: WSANOTINITIALISED: WSAStartup"
" call necessary" << std::endl;
#endif
2020-09-06 15:46:49 +02:00
break;
}
default: {
/*
https://docs.microsoft.com/en-us/windows/win32/winsock/
windows-sockets-error-codes-2
*/
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-03-08 23:00:53 +01:00
sif::warning << "TmTcWinUdpBridge::handleSocketError: Error code: " << errCode << std::endl;
#endif
2020-09-06 15:46:49 +02:00
break;
}
}
}
void TmTcWinUdpBridge::handleBindError() {
int errCode = WSAGetLastError();
switch(errCode) {
case(WSANOTINITIALISED): {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-06 15:46:49 +02:00
sif::info << "TmTcWinUdpBridge::handleBindError: WSANOTINITIALISED: "
2021-03-08 23:14:10 +01:00
<< "WSAStartup call necessary" << std::endl;
#endif
2020-09-06 15:46:49 +02:00
break;
}
2020-12-22 16:15:40 +01:00
case(WSAEADDRINUSE): {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-12-22 16:15:40 +01:00
sif::warning << "TmTcWinUdpBridge::handleBindError: WSAEADDRINUSE: "
2021-03-08 23:14:10 +01:00
"Port is already in use!" << std::endl;
#endif
2020-12-22 16:15:40 +01:00
break;
}
2020-09-06 15:46:49 +02:00
default: {
/*
https://docs.microsoft.com/en-us/windows/win32/winsock/
windows-sockets-error-codes-2
*/
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-06 15:46:49 +02:00
sif::info << "TmTcWinUdpBridge::handleBindError: Error code: "
<< errCode << std::endl;
#endif
2020-09-06 15:46:49 +02:00
break;
}
}
}
void TmTcWinUdpBridge::handleSendError() {
int errCode = WSAGetLastError();
switch(errCode) {
case(WSANOTINITIALISED): {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-06 15:46:49 +02:00
sif::info << "TmTcWinUdpBridge::handleSendError: WSANOTINITIALISED: "
2020-12-02 00:27:53 +01:00
<< "WSAStartup(...) call necessary" << std::endl;
#endif
2020-09-06 15:46:49 +02:00
break;
}
case(WSAEADDRNOTAVAIL): {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-12-02 00:27:53 +01:00
sif::info << "TmTcWinUdpBridge::handleSendError: WSAEADDRNOTAVAIL: "
2020-09-06 15:46:49 +02:00
<< "Check target address. " << std::endl;
#endif
2020-09-06 15:46:49 +02:00
break;
}
default: {
/*
https://docs.microsoft.com/en-us/windows/win32/winsock/
windows-sockets-error-codes-2
*/
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-09-06 15:46:49 +02:00
sif::info << "TmTcWinUdpBridge::handleSendError: Error code: "
<< errCode << std::endl;
#endif
2020-09-06 15:46:49 +02:00
break;
}
}
}