fsfw/src/fsfw/osal/common/UdpTmTcBridge.cpp

154 lines
5.0 KiB
C++
Raw Normal View History

2021-07-13 21:02:53 +02:00
#include "fsfw/osal/common/UdpTmTcBridge.h"
2020-09-06 15:46:49 +02:00
2022-02-02 10:29:30 +01:00
#include "fsfw/ipc/MutexGuard.h"
#include "fsfw/osal/common/tcpipHelpers.h"
2021-07-13 21:02:53 +02:00
#include "fsfw/platform.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
2021-03-21 12:51:28 +01:00
2021-05-12 16:47:53 +02:00
#ifdef PLATFORM_WIN
2021-03-12 00:34:30 +01:00
#include <ws2tcpip.h>
2021-05-12 16:47:53 +02:00
#elif defined(PLATFORM_UNIX)
2021-03-21 12:51:28 +01:00
#include <arpa/inet.h>
2022-02-02 10:29:30 +01:00
#include <netdb.h>
#endif
2021-03-12 17:15:53 +01:00
//! Debugging preprocessor define.
2021-05-05 12:59:42 +02:00
#ifndef FSFW_UDP_SEND_WIRETAPPING_ENABLED
2022-02-02 10:29:30 +01:00
#define FSFW_UDP_SEND_WIRETAPPING_ENABLED 0
2021-05-05 12:59:42 +02:00
#endif
2021-03-12 17:15:53 +01:00
2022-02-02 10:29:30 +01:00
const std::string UdpTmTcBridge::DEFAULT_SERVER_PORT = tcpip::DEFAULT_SERVER_PORT;
2021-03-07 01:35:55 +01:00
UdpTmTcBridge::UdpTmTcBridge(object_id_t objectId, object_id_t tcDestination,
2023-03-03 15:45:44 +01:00
uint32_t msgQueueDepth, const std::string &udpServerPort_,
object_id_t tmStoreId, object_id_t tcStoreId)
: TmTcBridge("UDP TMTC Bridge", objectId, tcDestination, msgQueueDepth, tmStoreId, tcStoreId) {
2022-05-01 17:48:49 +02:00
if (udpServerPort_.empty()) {
udpServerPort = DEFAULT_SERVER_PORT;
2022-02-02 10:29:30 +01:00
} else {
2022-05-01 17:48:49 +02:00
udpServerPort = udpServerPort_;
2022-02-02 10:29:30 +01:00
}
mutex = MutexFactory::instance()->createMutex();
communicationLinkUp = false;
2021-03-12 00:34:30 +01:00
}
2020-09-06 15:46:49 +02:00
ReturnValue_t UdpTmTcBridge::initialize() {
2022-02-02 10:29:30 +01:00
ReturnValue_t result = TmTcBridge::initialize();
2022-08-16 01:08:26 +02:00
if (result != returnvalue::OK) {
2021-03-12 02:15:21 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "UdpTmTcBridge::initialize: TmTcBridge initialization failed!" << std::endl;
2021-03-12 02:15:21 +01:00
#endif
2022-02-02 10:29:30 +01:00
return result;
}
#ifdef _WIN32
2022-02-02 10:29:30 +01:00
/* 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. */
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "UdpTmTcBridge::UdpTmTcBridge: WSAStartup failed with error: " << err
<< std::endl;
2021-03-12 00:34:30 +01:00
#else
2022-02-02 10:29:30 +01:00
sif::printError("UdpTmTcBridge::UdpTmTcBridge: WSAStartup failed with error: %d\n", err);
#endif
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
#endif
2020-09-06 15:46:49 +02:00
2022-02-02 10:29:30 +01:00
struct addrinfo *addrResult = nullptr;
struct addrinfo hints = {};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
/* 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) */
int retval = getaddrinfo(nullptr, udpServerPort.c_str(), &hints, &addrResult);
if (retval != 0) {
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::GETADDRINFO_CALL);
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
serverSocket = socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol);
if (serverSocket == INVALID_SOCKET) {
freeaddrinfo(addrResult);
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::SOCKET_CALL);
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
2020-09-06 15:46:49 +02:00
#if FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
2022-02-02 10:29:30 +01:00
tcpip::printAddress(addrResult->ai_addr);
#endif
2022-02-02 10:29:30 +01:00
retval = bind(serverSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
if (retval != 0) {
2021-03-12 01:40:58 +01:00
freeaddrinfo(addrResult);
2022-02-02 10:29:30 +01:00
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::BIND_CALL);
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
freeaddrinfo(addrResult);
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2020-09-06 15:46:49 +02:00
}
UdpTmTcBridge::~UdpTmTcBridge() {
2022-02-02 10:29:30 +01:00
if (mutex != nullptr) {
MutexFactory::instance()->deleteMutex(mutex);
}
2020-09-06 15:46:49 +02:00
}
2022-02-02 10:29:30 +01:00
std::string UdpTmTcBridge::getUdpPort() const { return udpServerPort; }
2021-08-09 18:22:22 +02:00
ReturnValue_t UdpTmTcBridge::sendTm(const uint8_t *data, size_t dataLen) {
2022-02-02 10:29:30 +01:00
int flags = 0;
2020-09-06 15:46:49 +02:00
2022-02-02 10:29:30 +01:00
/* The target address can be set by different threads so this lock ensures thread-safety */
MutexGuard lock(mutex, timeoutType, mutexTimeoutMs);
2021-03-12 18:12:38 +01:00
#if FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
2022-02-02 10:29:30 +01:00
tcpip::printAddress(&clientAddress);
#endif
2020-09-06 15:46:49 +02:00
2022-05-01 17:48:49 +02:00
ssize_t bytesSent = sendto(serverSocket, reinterpret_cast<const char *>(data), dataLen, flags,
2022-05-09 11:06:45 +02:00
&clientAddress, clientAddressLen);
2022-02-02 10:29:30 +01:00
if (bytesSent == SOCKET_ERROR) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "TmTcUdpBridge::sendTm: Send operation failed." << std::endl;
#endif
2022-02-02 10:29:30 +01:00
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::SENDTO_CALL);
}
2021-03-08 23:00:53 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
2023-02-20 15:02:00 +01:00
sif::debug << "TmTcUdpBridge::sendTm: " << bytesSent << " bytes were sent" << std::endl;
#endif
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2020-09-06 15:46:49 +02:00
}
2022-02-02 10:29:30 +01:00
void UdpTmTcBridge::checkAndSetClientAddress(sockaddr &newAddress) {
/* 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
#if FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
2022-02-02 10:29:30 +01:00
tcpip::printAddress(&newAddress);
tcpip::printAddress(&clientAddress);
#endif
2022-02-02 10:29:30 +01:00
registerCommConnect();
2020-09-06 15:46:49 +02:00
2022-02-02 10:29:30 +01:00
/* Set new IP address to reply to */
clientAddress = newAddress;
clientAddressLen = sizeof(clientAddress);
2020-09-06 15:46:49 +02:00
}
2022-05-01 17:48:49 +02:00
void UdpTmTcBridge::setMutexProperties(MutexIF::TimeoutType timeoutType_, dur_millis_t timeoutMs) {
timeoutType = timeoutType_;
mutexTimeoutMs = timeoutMs;
2021-03-12 18:12:38 +01:00
}