fsfw/osal/common/UdpTmTcBridge.cpp

174 lines
5.8 KiB
C++
Raw Normal View History

2021-03-12 02:15:21 +01:00
#include "tcpipHelpers.h"
2020-09-06 15:46:49 +02:00
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>
#include <fsfw/osal/common/UdpTmTcBridge.h>
#ifdef _WIN32
2021-03-21 12:51:28 +01:00
2021-03-12 00:34:30 +01:00
#include <ws2tcpip.h>
2021-03-07 01:35:55 +01:00
#elif defined(__unix__)
2021-03-21 12:51:28 +01:00
#include <netdb.h>
#include <arpa/inet.h>
#endif
2021-03-12 17:15:53 +01:00
//! Debugging preprocessor define.
2021-03-15 21:33:45 +01:00
#define FSFW_UDP_SEND_WIRETAPPING_ENABLED 0
2021-03-12 17:15:53 +01:00
const std::string UdpTmTcBridge::DEFAULT_UDP_SERVER_PORT = tcpip::DEFAULT_SERVER_PORT;
2021-03-07 01:35:55 +01:00
UdpTmTcBridge::UdpTmTcBridge(object_id_t objectId, object_id_t tcDestination,
2021-03-12 18:20:54 +01:00
object_id_t tmStoreId, object_id_t tcStoreId, std::string udpServerPort):
2020-09-06 15:46:49 +02:00
TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) {
2021-03-12 00:34:30 +01:00
if(udpServerPort == "") {
2021-03-12 18:20:54 +01:00
this->udpServerPort = DEFAULT_UDP_SERVER_PORT;
2021-03-12 00:34:30 +01:00
}
else {
this->udpServerPort = udpServerPort;
}
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
ReturnValue_t UdpTmTcBridge::initialize() {
2021-03-12 00:45:32 +01:00
ReturnValue_t result = TmTcBridge::initialize();
if(result != HasReturnvaluesIF::RETURN_OK) {
2021-03-12 02:15:21 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcUdpBridge::initialize: TmTcBridge initialization failed!"
2021-03-12 02:15:21 +01:00
<< std::endl;
#endif
2021-03-12 00:45:32 +01:00
return result;
}
#ifdef _WIN32
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
sif::error << "TmTcUdpBridge::TmTcUdpBridge: WSAStartup failed with error: " <<
2021-03-12 00:34:30 +01:00
err << std::endl;
#else
sif::printError("TmTcUdpBridge::TmTcUdpBridge: WSAStartup failed with error: %d\n",
2021-03-12 00:34:30 +01:00
err);
#endif
2021-03-12 00:34:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
2020-09-06 15:46:49 +02:00
}
#endif
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
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
2020-09-06 15:46:49 +02:00
2021-03-08 23:02:06 +01:00
/* Set up UDP socket:
2021-03-12 02:15:21 +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-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
sif::warning << "TmTcUdpBridge::TmTcUdpBridge: 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
sif::warning << "TmTcUdpBridge::TmTcUdpBridge: Could not open UDP socket!" <<
2021-03-08 23:14:10 +01:00
std::endl;
#endif
2021-03-12 01:40:58 +01:00
freeaddrinfo(addrResult);
2021-03-12 02:15:21 +01:00
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::SOCKET_CALL);
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
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
sif::error << "TmTcUdpBridge::TmTcUdpBridge: Could not bind "
2021-03-12 14:08:58 +01:00
"local port (" << udpServerPort << ") to server socket!" << std::endl;
#endif
2021-03-12 01:40:58 +01:00
freeaddrinfo(addrResult);
2021-03-12 02:15:21 +01:00
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::BIND_CALL);
2021-04-08 18:53:22 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
2020-09-06 15:46:49 +02:00
}
2021-03-12 01:40:58 +01:00
freeaddrinfo(addrResult);
2021-03-12 00:34:30 +01:00
return HasReturnvaluesIF::RETURN_OK;
2020-09-06 15:46:49 +02:00
}
UdpTmTcBridge::~UdpTmTcBridge() {
2021-03-12 00:34:30 +01:00
if(mutex != nullptr) {
MutexFactory::instance()->deleteMutex(mutex);
}
2020-09-06 15:46:49 +02:00
}
ReturnValue_t UdpTmTcBridge::sendTm(const uint8_t *data, size_t dataLen) {
2020-09-06 15:46:49 +02:00
int flags = 0;
2021-03-12 18:12:38 +01:00
/* The target address can be set by different threads so this lock ensures thread-safety */
MutexGuard lock(mutex, timeoutType, mutexTimeoutMs);
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,
&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#endif
2020-09-06 15:46:49 +02:00
int bytesSent = sendto(
serverSocket,
reinterpret_cast<const char*>(data),
dataLen,
flags,
2021-03-21 12:51:28 +01:00
&clientAddress,
clientAddressLen
);
2020-09-06 15:46:49 +02:00
if(bytesSent == SOCKET_ERROR) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcUdpBridge::sendTm: Send operation failed." << std::endl;
#endif
2021-03-12 02:15:21 +01:00
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::SENDTO_CALL);
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
sif::debug << "TmTcUdpBridge::sendTm: " << bytesSent << " bytes were"
2021-03-08 23:00:53 +01:00
" sent." << std::endl;
#endif
2020-09-06 15:46:49 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2021-03-21 12:51:28 +01:00
void UdpTmTcBridge::checkAndSetClientAddress(sockaddr& newAddress) {
2021-03-12 18:12:38 +01:00
/* The target address can be set by different threads so this lock ensures thread-safety */
MutexGuard lock(mutex, timeoutType, mutexTimeoutMs);
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-20 12:38:51 +01:00
/* Set new IP address to reply to */
clientAddress = newAddress;
clientAddressLen = sizeof(clientAddress);
2020-09-06 15:46:49 +02:00
}
void UdpTmTcBridge::setMutexProperties(MutexIF::TimeoutType timeoutType,
2021-03-12 18:12:38 +01:00
dur_millis_t timeoutMs) {
this->timeoutType = timeoutType;
this->mutexTimeoutMs = timeoutMs;
}