fixed for linux

This commit is contained in:
Robin Müller 2021-03-21 12:51:28 +01:00
parent d625642abc
commit 86577f4b80
13 changed files with 484 additions and 467 deletions

View File

@ -2,6 +2,7 @@
#ifdef __unix__ #ifdef __unix__
#include <errno.h>
#include <unistd.h> #include <unistd.h>
#endif #endif
@ -21,7 +22,7 @@ int TcpIpBase::closeSocket(socket_t socket) {
#ifdef _WIN32 #ifdef _WIN32
return closesocket(socket); return closesocket(socket);
#elif defined(__unix__) #elif defined(__unix__)
return close(socket) return close(socket);
#endif #endif
} }

View File

@ -9,6 +9,7 @@
#elif defined(__unix__) #elif defined(__unix__)
#include <sys/socket.h>
#endif #endif

View File

@ -5,6 +5,11 @@
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#elif defined(__unix__)
#include <netdb.h>
#endif #endif
const std::string TcpTmTcServer::DEFAULT_TCP_SERVER_PORT = "7301"; const std::string TcpTmTcServer::DEFAULT_TCP_SERVER_PORT = "7301";
@ -30,7 +35,6 @@ ReturnValue_t TcpTmTcServer::initialize() {
struct addrinfo *addrResult = nullptr; struct addrinfo *addrResult = nullptr;
struct addrinfo hints = { 0 }; struct addrinfo hints = { 0 };
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
@ -74,16 +78,15 @@ ReturnValue_t TcpTmTcServer::initialize() {
TcpTmTcServer::~TcpTmTcServer() { TcpTmTcServer::~TcpTmTcServer() {
closesocket(listenerTcpSocket); closeSocket(listenerTcpSocket);
WSACleanup();
} }
ReturnValue_t TcpTmTcServer::performOperation(uint8_t opCode) { ReturnValue_t TcpTmTcServer::performOperation(uint8_t opCode) {
using namespace tcpip; using namespace tcpip;
/* If a connection is accepted, the corresponding socket will be assigned to the new socket */ /* If a connection is accepted, the corresponding socket will be assigned to the new socket */
SOCKET clientSocket; socket_t clientSocket;
sockaddr_in clientSockAddr; sockaddr clientSockAddr;
int connectorSockAddrLen = 0; socklen_t connectorSockAddrLen = 0;
int retval = 0; int retval = 0;
/* Listen for connection requests permanently for lifetime of program */ /* Listen for connection requests permanently for lifetime of program */
@ -94,8 +97,7 @@ ReturnValue_t TcpTmTcServer::performOperation(uint8_t opCode) {
continue; continue;
} }
clientSocket = accept(listenerTcpSocket, reinterpret_cast<sockaddr*>(&clientSockAddr), clientSocket = accept(listenerTcpSocket, &clientSockAddr, &connectorSockAddrLen);
&connectorSockAddrLen);
if(clientSocket == INVALID_SOCKET) { if(clientSocket == INVALID_SOCKET) {
handleError(Protocol::TCP, ErrorSources::ACCEPT_CALL, 500); handleError(Protocol::TCP, ErrorSources::ACCEPT_CALL, 500);
@ -119,7 +121,7 @@ ReturnValue_t TcpTmTcServer::performOperation(uint8_t opCode) {
} }
/* Done, shut down connection */ /* Done, shut down connection */
retval = shutdown(clientSocket, SD_SEND); retval = shutdown(clientSocket, SHUT_SEND);
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -5,6 +5,10 @@
#include "../../objectmanager/SystemObject.h" #include "../../objectmanager/SystemObject.h"
#include "../../tasks/ExecutableObjectIF.h" #include "../../tasks/ExecutableObjectIF.h"
#ifdef __unix__
#include <sys/socket.h>
#endif
#include <string> #include <string>
#include <vector> #include <vector>
@ -36,7 +40,7 @@ private:
std::string tcpPort; std::string tcpPort;
socket_t listenerTcpSocket = 0; socket_t listenerTcpSocket = 0;
struct sockaddr_in tcpAddress; struct sockaddr tcpAddress;
int tcpAddrLen = sizeof(tcpAddress); int tcpAddrLen = sizeof(tcpAddress);
int currentBacklog = 3; int currentBacklog = 3;

View File

@ -3,8 +3,17 @@
#include "../../globalfunctions/arrayprinter.h" #include "../../globalfunctions/arrayprinter.h"
#include "../../serviceinterface/ServiceInterfaceStream.h" #include "../../serviceinterface/ServiceInterfaceStream.h"
#ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif
//! Debugging preprocessor define. //! Debugging preprocessor define.
#define FSFW_UDP_RCV_WIRETAPPING_ENABLED 0 #define FSFW_UDP_RCV_WIRETAPPING_ENABLED 0
@ -36,17 +45,17 @@ UdpTcPollingTask::~UdpTcPollingTask() {}
ReturnValue_t UdpTcPollingTask::performOperation(uint8_t opCode) { ReturnValue_t UdpTcPollingTask::performOperation(uint8_t opCode) {
/* Sender Address is cached here. */ /* Sender Address is cached here. */
struct sockaddr_in senderAddress; struct sockaddr senderAddress;
int senderAddressSize = sizeof(senderAddress); socklen_t senderAddressSize = sizeof(senderAddress);
/* Poll for new UDP datagrams in permanent loop. */ /* Poll for new UDP datagrams in permanent loop. */
while(true) { while(true) {
int bytesReceived = recvfrom( int bytesReceived = recvfrom(
serverUdpSocket, this->serverSocket,
reinterpret_cast<char*>(receptionBuffer.data()), reinterpret_cast<char*>(receptionBuffer.data()),
frameSize, frameSize,
receptionFlags, receptionFlags,
reinterpret_cast<sockaddr*>(&senderAddress), &senderAddress,
&senderAddressSize &senderAddressSize
); );
if(bytesReceived == SOCKET_ERROR) { if(bytesReceived == SOCKET_ERROR) {
@ -138,7 +147,7 @@ ReturnValue_t UdpTcPollingTask::initializeAfterTaskCreation() {
targetTcDestination = tmtcBridge->getRequestQueue(); targetTcDestination = tmtcBridge->getRequestQueue();
/* The server socket is set up in the bridge intialization. Calling this function here /* The server socket is set up in the bridge intialization. Calling this function here
ensures that it is set up regardless of which class was initialized first */ ensures that it is set up regardless of which class was initialized first */
serverUdpSocket = tmtcBridge->serverSocket; this->serverSocket = tmtcBridge->serverSocket;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -54,10 +54,6 @@ private:
//! See: https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom //! See: https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom
int receptionFlags = 0; int receptionFlags = 0;
//! Server socket, which is member of TMTC bridge.
//! Will be cached shortly after SW intialization.
SOCKET serverUdpSocket = 0;
std::vector<uint8_t> receptionBuffer; std::vector<uint8_t> receptionBuffer;
size_t frameSize = 0; size_t frameSize = 0;

View File

@ -5,11 +5,13 @@
#include <fsfw/osal/common/UdpTmTcBridge.h> #include <fsfw/osal/common/UdpTmTcBridge.h>
#ifdef _WIN32 #ifdef _WIN32
#include <ws2tcpip.h> #include <ws2tcpip.h>
#elif defined(__unix__) #elif defined(__unix__)
#include <arap/inet.h> #include <netdb.h>
#include <arpa/inet.h>
#endif #endif
@ -135,7 +137,7 @@ ReturnValue_t UdpTmTcBridge::sendTm(const uint8_t *data, size_t dataLen) {
reinterpret_cast<const char*>(data), reinterpret_cast<const char*>(data),
dataLen, dataLen,
flags, flags,
reinterpret_cast<sockaddr*>(&clientAddress), &clientAddress,
clientAddressLen clientAddressLen
); );
if(bytesSent == SOCKET_ERROR) { if(bytesSent == SOCKET_ERROR) {
@ -151,7 +153,7 @@ ReturnValue_t UdpTmTcBridge::sendTm(const uint8_t *data, size_t dataLen) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void UdpTmTcBridge::checkAndSetClientAddress(sockaddr_in& newAddress) { void UdpTmTcBridge::checkAndSetClientAddress(sockaddr& newAddress) {
/* The target address can be set by different threads so this lock ensures thread-safety */ /* The target address can be set by different threads so this lock ensures thread-safety */
MutexGuard lock(mutex, timeoutType, mutexTimeoutMs); MutexGuard lock(mutex, timeoutType, mutexTimeoutMs);

View File

@ -4,6 +4,10 @@
#include "TcpIpBase.h" #include "TcpIpBase.h"
#include "../../tmtcservices/TmTcBridge.h" #include "../../tmtcservices/TmTcBridge.h"
#ifdef __unix__
#include <sys/socket.h>
#endif
#include <string> #include <string>
class UdpTmTcBridge: class UdpTmTcBridge:
@ -25,7 +29,7 @@ public:
ReturnValue_t initialize() override; ReturnValue_t initialize() override;
void checkAndSetClientAddress(sockaddr_in& clientAddress); void checkAndSetClientAddress(sockaddr& clientAddress);
protected: protected:
virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override; virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override;
@ -33,8 +37,8 @@ protected:
private: private:
std::string udpServerPort; std::string udpServerPort;
struct sockaddr_in clientAddress; struct sockaddr clientAddress;
int clientAddressLen = 0; socklen_t clientAddressLen = 0;
//! Access to the client address is mutex protected as it is set by another task. //! Access to the client address is mutex protected as it is set by another task.
MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING; MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING;

View File

@ -13,8 +13,6 @@ target_sources(${LIB_FSFW_NAME}
QueueFactory.cpp QueueFactory.cpp
SemaphoreFactory.cpp SemaphoreFactory.cpp
TaskFactory.cpp TaskFactory.cpp
TcUnixUdpPollingTask.cpp
TmTcUnixUdpBridge.cpp
Timer.cpp Timer.cpp
tcpipHelpers.cpp tcpipHelpers.cpp
) )

View File

@ -1,152 +1,152 @@
#include "TcUnixUdpPollingTask.h" //#include "TcUnixUdpPollingTask.h"
#include "tcpipHelpers.h" //#include "tcpipHelpers.h"
//
#include "../../globalfunctions/arrayprinter.h" //#include "../../globalfunctions/arrayprinter.h"
//
#define FSFW_UDP_RCV_WIRETAPPING_ENABLED 0 //#define FSFW_UDP_RCV_WIRETAPPING_ENABLED 0
//
TcUnixUdpPollingTask::TcUnixUdpPollingTask(object_id_t objectId, //TcUnixUdpPollingTask::TcUnixUdpPollingTask(object_id_t objectId,
object_id_t tmtcUnixUdpBridge, size_t frameSize, // object_id_t tmtcUnixUdpBridge, size_t frameSize,
double timeoutSeconds): SystemObject(objectId), // double timeoutSeconds): SystemObject(objectId),
tmtcBridgeId(tmtcUnixUdpBridge) { // tmtcBridgeId(tmtcUnixUdpBridge) {
//
if(frameSize > 0) { // if(frameSize > 0) {
this->frameSize = frameSize; // this->frameSize = frameSize;
} // }
else { // else {
this->frameSize = DEFAULT_MAX_FRAME_SIZE; // this->frameSize = DEFAULT_MAX_FRAME_SIZE;
} // }
//
/* Set up reception buffer with specified frame size. // /* Set up reception buffer with specified frame size.
For now, it is assumed that only one frame is held in the buffer! */ // For now, it is assumed that only one frame is held in the buffer! */
receptionBuffer.reserve(this->frameSize); // receptionBuffer.reserve(this->frameSize);
receptionBuffer.resize(this->frameSize); // receptionBuffer.resize(this->frameSize);
//
if(timeoutSeconds == -1) { // if(timeoutSeconds == -1) {
receptionTimeout = DEFAULT_TIMEOUT; // receptionTimeout = DEFAULT_TIMEOUT;
} // }
else { // else {
receptionTimeout = timevalOperations::toTimeval(timeoutSeconds); // receptionTimeout = timevalOperations::toTimeval(timeoutSeconds);
} // }
} //}
//
TcUnixUdpPollingTask::~TcUnixUdpPollingTask() {} //TcUnixUdpPollingTask::~TcUnixUdpPollingTask() {}
//
ReturnValue_t TcUnixUdpPollingTask::performOperation(uint8_t opCode) { //ReturnValue_t TcUnixUdpPollingTask::performOperation(uint8_t opCode) {
/* Sender Address is cached here. */ // /* Sender Address is cached here. */
struct sockaddr_in senderAddress; // struct sockaddr_in senderAddress;
socklen_t senderAddressSize = sizeof(senderAddress); // socklen_t senderAddressSize = sizeof(senderAddress);
//
/* Poll for new UDP datagrams in permanent loop. */ // /* Poll for new UDP datagrams in permanent loop. */
while(true) { // while(true) {
ssize_t bytesReceived = recvfrom( // ssize_t bytesReceived = recvfrom(
serverUdpSocket, // serverUdpSocket,
receptionBuffer.data(), // receptionBuffer.data(),
frameSize, // frameSize,
receptionFlags, // receptionFlags,
reinterpret_cast<sockaddr*>(&senderAddress), // reinterpret_cast<sockaddr*>(&senderAddress),
&senderAddressSize // &senderAddressSize
); // );
if(bytesReceived < 0) { // if(bytesReceived < 0) {
/* Handle error */ // /* Handle error */
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcSocketPollingTask::performOperation: Reception error." << std::endl; // sif::error << "TcSocketPollingTask::performOperation: Reception error." << std::endl;
#endif //#endif
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::RECVFROM_CALL, 500); // tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::RECVFROM_CALL, 500);
continue; // continue;
} // }
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1
sif::debug << "TcSocketPollingTask::performOperation: " << bytesReceived // sif::debug << "TcSocketPollingTask::performOperation: " << bytesReceived
<< " bytes received" << std::endl; // << " bytes received" << std::endl;
#endif //#endif
//
ReturnValue_t result = handleSuccessfullTcRead(bytesReceived); // ReturnValue_t result = handleSuccessfullTcRead(bytesReceived);
if(result != HasReturnvaluesIF::RETURN_FAILED) { // if(result != HasReturnvaluesIF::RETURN_FAILED) {
//
} // }
tmtcBridge->checkAndSetClientAddress(senderAddress); // tmtcBridge->checkAndSetClientAddress(senderAddress);
} // }
return HasReturnvaluesIF::RETURN_OK; // return HasReturnvaluesIF::RETURN_OK;
} //}
//
//
ReturnValue_t TcUnixUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) { //ReturnValue_t TcUnixUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) {
store_address_t storeId; // store_address_t storeId;
//
#if FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1 //#if FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1
arrayprinter::print(receptionBuffer.data(), bytesRead); // arrayprinter::print(receptionBuffer.data(), bytesRead);
#endif //#endif
//
ReturnValue_t result = tcStore->addData(&storeId, receptionBuffer.data(), bytesRead); // ReturnValue_t result = tcStore->addData(&storeId, receptionBuffer.data(), bytesRead);
if (result != HasReturnvaluesIF::RETURN_OK) { // if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1 //#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcUnixUdpPollingTask::handleSuccessfullTcRead: Data " // sif::error << "TcUnixUdpPollingTask::handleSuccessfullTcRead: Data "
"storage failed" << std::endl; // "storage failed" << std::endl;
sif::error << "Packet size: " << bytesRead << std::endl; // sif::error << "Packet size: " << bytesRead << std::endl;
#else //#else
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ //#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ //#endif /* FSFW_VERBOSE_LEVEL >= 1 */
return HasReturnvaluesIF::RETURN_FAILED; // return HasReturnvaluesIF::RETURN_FAILED;
} // }
//
TmTcMessage message(storeId); // TmTcMessage message(storeId);
//
result = MessageQueueSenderIF::sendMessage(targetTcDestination, &message); // result = MessageQueueSenderIF::sendMessage(targetTcDestination, &message);
if (result != HasReturnvaluesIF::RETURN_OK) { // if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1 //#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcUnixUdpPollingTask::handleSuccessfullTcRead: Sending message to queue " // sif::error << "TcUnixUdpPollingTask::handleSuccessfullTcRead: Sending message to queue "
"failed" << std::endl; // "failed" << std::endl;
#else //#else
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ //#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ //#endif /* FSFW_VERBOSE_LEVEL >= 1 */
tcStore->deleteData(storeId); // tcStore->deleteData(storeId);
} // }
return result; // return result;
} //}
//
ReturnValue_t TcUnixUdpPollingTask::initialize() { //ReturnValue_t TcUnixUdpPollingTask::initialize() {
tcStore = objectManager->get<StorageManagerIF>(objects::TC_STORE); // tcStore = objectManager->get<StorageManagerIF>(objects::TC_STORE);
if (tcStore == nullptr) { // if (tcStore == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcSerialPollingTask::initialize: TC Store uninitialized!" // sif::error << "TcSerialPollingTask::initialize: TC Store uninitialized!"
<< std::endl; // << std::endl;
#endif //#endif
return ObjectManagerIF::CHILD_INIT_FAILED; // return ObjectManagerIF::CHILD_INIT_FAILED;
} // }
//
tmtcBridge = objectManager->get<TmTcUnixUdpBridge>(tmtcBridgeId); // tmtcBridge = objectManager->get<TmTcUnixUdpBridge>(tmtcBridgeId);
if(tmtcBridge == nullptr) { // if(tmtcBridge == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcSocketPollingTask::TcSocketPollingTask: Invalid" // sif::error << "TcSocketPollingTask::TcSocketPollingTask: Invalid"
" TMTC bridge object!" << std::endl; // " TMTC bridge object!" << std::endl;
#endif //#endif
return ObjectManagerIF::CHILD_INIT_FAILED; // return ObjectManagerIF::CHILD_INIT_FAILED;
} // }
//
return HasReturnvaluesIF::RETURN_OK; // return HasReturnvaluesIF::RETURN_OK;
} //}
//
ReturnValue_t TcUnixUdpPollingTask::initializeAfterTaskCreation() { //ReturnValue_t TcUnixUdpPollingTask::initializeAfterTaskCreation() {
/* Initialize the destination after task creation. This ensures // /* Initialize the destination after task creation. This ensures
that the destination has already been set in the TMTC bridge. */ // that the destination has already been set in the TMTC bridge. */
targetTcDestination = tmtcBridge->getRequestQueue(); // targetTcDestination = tmtcBridge->getRequestQueue();
/* The server socket is set up in the bridge intialization. Calling this function here // /* The server socket is set up in the bridge intialization. Calling this function here
ensures that it is set up properly in any case*/ // ensures that it is set up properly in any case*/
serverUdpSocket = tmtcBridge->serverSocket; // serverUdpSocket = tmtcBridge->serverSocket;
return HasReturnvaluesIF::RETURN_OK; // return HasReturnvaluesIF::RETURN_OK;
} //}
//
void TcUnixUdpPollingTask::setTimeout(double timeoutSeconds) { //void TcUnixUdpPollingTask::setTimeout(double timeoutSeconds) {
timeval tval; // timeval tval;
tval = timevalOperations::toTimeval(timeoutSeconds); // tval = timevalOperations::toTimeval(timeoutSeconds);
int result = setsockopt(serverUdpSocket, SOL_SOCKET, SO_RCVTIMEO, // int result = setsockopt(serverUdpSocket, SOL_SOCKET, SO_RCVTIMEO,
&tval, sizeof(receptionTimeout)); // &tval, sizeof(receptionTimeout));
if(result == -1) { // if(result == -1) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcSocketPollingTask::TcSocketPollingTask: Setting " // sif::error << "TcSocketPollingTask::TcSocketPollingTask: Setting "
"receive timeout failed with " << strerror(errno) << std::endl; // "receive timeout failed with " << strerror(errno) << std::endl;
#endif //#endif
} // }
} //}

View File

@ -1,67 +1,67 @@
#ifndef FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_ //#ifndef FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_
#define FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_ //#define FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_
//
#include "../../objectmanager/SystemObject.h" //#include "../../objectmanager/SystemObject.h"
#include "../../osal/linux/TmTcUnixUdpBridge.h" //#include "../../osal/linux/TmTcUnixUdpBridge.h"
#include "../../tasks/ExecutableObjectIF.h" //#include "../../tasks/ExecutableObjectIF.h"
//
#include <sys/socket.h> //#include <sys/socket.h>
#include <vector> //#include <vector>
//
/** ///**
* @brief This class can be used to implement the polling of a Unix socket, // * @brief This class can be used to implement the polling of a Unix socket,
* using UDP for now. // * using UDP for now.
* @details // * @details
* The task will be blocked while the specified number of bytes has not been // * The task will be blocked while the specified number of bytes has not been
* received, so TC reception is handled inside a separate task. // * received, so TC reception is handled inside a separate task.
* This class caches the IP address of the sender. It is assumed there // * This class caches the IP address of the sender. It is assumed there
* is only one sender for now. // * is only one sender for now.
*/ // */
class TcUnixUdpPollingTask: public SystemObject, //class TcUnixUdpPollingTask: public SystemObject,
public ExecutableObjectIF { // public ExecutableObjectIF {
friend class TmTcUnixUdpBridge; // friend class TmTcUnixUdpBridge;
public: //public:
static constexpr size_t DEFAULT_MAX_FRAME_SIZE = 2048; // static constexpr size_t DEFAULT_MAX_FRAME_SIZE = 2048;
//! 0.5 default milliseconds timeout for now. // //! 0.5 default milliseconds timeout for now.
static constexpr timeval DEFAULT_TIMEOUT = {.tv_sec = 0, .tv_usec = 500}; // static constexpr timeval DEFAULT_TIMEOUT = {.tv_sec = 0, .tv_usec = 500};
//
TcUnixUdpPollingTask(object_id_t objectId, object_id_t tmtcUnixUdpBridge, // TcUnixUdpPollingTask(object_id_t objectId, object_id_t tmtcUnixUdpBridge,
size_t frameSize = 0, double timeoutSeconds = -1); // size_t frameSize = 0, double timeoutSeconds = -1);
virtual~ TcUnixUdpPollingTask(); // virtual~ TcUnixUdpPollingTask();
//
/** // /**
* Turn on optional timeout for UDP polling. In the default mode, // * Turn on optional timeout for UDP polling. In the default mode,
* the receive function will block until a packet is received. // * the receive function will block until a packet is received.
* @param timeoutSeconds // * @param timeoutSeconds
*/ // */
void setTimeout(double timeoutSeconds); // void setTimeout(double timeoutSeconds);
//
virtual ReturnValue_t performOperation(uint8_t opCode) override; // virtual ReturnValue_t performOperation(uint8_t opCode) override;
virtual ReturnValue_t initialize() override; // virtual ReturnValue_t initialize() override;
virtual ReturnValue_t initializeAfterTaskCreation() override; // virtual ReturnValue_t initializeAfterTaskCreation() override;
//
protected: //protected:
StorageManagerIF* tcStore = nullptr; // StorageManagerIF* tcStore = nullptr;
//
private: //private:
//! TMTC bridge is cached. // //! TMTC bridge is cached.
object_id_t tmtcBridgeId = objects::NO_OBJECT; // object_id_t tmtcBridgeId = objects::NO_OBJECT;
TmTcUnixUdpBridge* tmtcBridge = nullptr; // TmTcUnixUdpBridge* tmtcBridge = nullptr;
MessageQueueId_t targetTcDestination = MessageQueueIF::NO_QUEUE; // MessageQueueId_t targetTcDestination = MessageQueueIF::NO_QUEUE;
//
//! Reception flags: https://linux.die.net/man/2/recvfrom. // //! Reception flags: https://linux.die.net/man/2/recvfrom.
int receptionFlags = 0; // int receptionFlags = 0;
//
//! Server socket, which is member of TMTC bridge and is assigned in // //! Server socket, which is member of TMTC bridge and is assigned in
//! constructor // //! constructor
int serverUdpSocket = 0; // int serverUdpSocket = 0;
//
std::vector<uint8_t> receptionBuffer; // std::vector<uint8_t> receptionBuffer;
//
size_t frameSize = 0; // size_t frameSize = 0;
timeval receptionTimeout; // timeval receptionTimeout;
//
ReturnValue_t handleSuccessfullTcRead(size_t bytesRead); // ReturnValue_t handleSuccessfullTcRead(size_t bytesRead);
}; //};
//
#endif /* FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_ */ //#endif /* FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_ */

View File

@ -1,167 +1,167 @@
#include "TmTcUnixUdpBridge.h" //#include "TmTcUnixUdpBridge.h"
#include "tcpipHelpers.h" //#include "tcpipHelpers.h"
#include "../../serviceinterface/ServiceInterface.h" //#include "../../serviceinterface/ServiceInterface.h"
#include "../../ipc/MutexGuard.h" //#include "../../ipc/MutexGuard.h"
//
#include <arpa/inet.h> //#include <arpa/inet.h>
#include <unistd.h> //#include <unistd.h>
#include <netdb.h> //#include <netdb.h>
//
#include <cstring> //#include <cstring>
//
//! Debugging preprocessor define. ////! Debugging preprocessor define.
#define FSFW_UDP_SEND_WIRETAPPING_ENABLED 0 //#define FSFW_UDP_SEND_WIRETAPPING_ENABLED 0
//
const std::string TmTcUnixUdpBridge::DEFAULT_UDP_SERVER_PORT = tcpip::DEFAULT_UDP_SERVER_PORT; //const std::string TmTcUnixUdpBridge::DEFAULT_UDP_SERVER_PORT = tcpip::DEFAULT_UDP_SERVER_PORT;
//
TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId, object_id_t tcDestination, //TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId, object_id_t tcDestination,
object_id_t tmStoreId, object_id_t tcStoreId, std::string udpServerPort): // object_id_t tmStoreId, object_id_t tcStoreId, std::string udpServerPort):
TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) { // TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) {
if(udpServerPort == "") { // if(udpServerPort == "") {
this->udpServerPort = DEFAULT_UDP_SERVER_PORT; // this->udpServerPort = DEFAULT_UDP_SERVER_PORT;
} // }
else { // else {
this->udpServerPort = udpServerPort; // this->udpServerPort = udpServerPort;
} // }
//
mutex = MutexFactory::instance()->createMutex(); // mutex = MutexFactory::instance()->createMutex();
communicationLinkUp = false; // communicationLinkUp = false;
} //}
//
ReturnValue_t TmTcUnixUdpBridge::initialize() { //ReturnValue_t TmTcUnixUdpBridge::initialize() {
using namespace tcpip; // using namespace tcpip;
//
ReturnValue_t result = TmTcBridge::initialize(); // ReturnValue_t result = TmTcBridge::initialize();
if(result != HasReturnvaluesIF::RETURN_OK) { // if(result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcUnixUdpBridge::initialize: TmTcBridge initialization failed!" // sif::error << "TmTcUnixUdpBridge::initialize: TmTcBridge initialization failed!"
<< std::endl; // << std::endl;
#endif //#endif
return result; // return result;
} // }
//
struct addrinfo *addrResult = nullptr; // struct addrinfo *addrResult = nullptr;
struct addrinfo hints; // struct addrinfo hints;
//
std::memset(&hints, 0, sizeof(hints)); // std::memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM; // hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP; // hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE; // hints.ai_flags = AI_PASSIVE;
//
/* Set up UDP socket: // /* Set up UDP socket:
https://man7.org/linux/man-pages/man3/getaddrinfo.3.html // https://man7.org/linux/man-pages/man3/getaddrinfo.3.html
Passing nullptr as the first parameter and specifying AI_PASSIVE in hints will cause // 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) */ // getaddrinfo to assign the address 0.0.0.0 (any address) */
int retval = getaddrinfo(nullptr, udpServerPort.c_str(), &hints, &addrResult); // int retval = getaddrinfo(nullptr, udpServerPort.c_str(), &hints, &addrResult);
if (retval != 0) { // if (retval != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Retrieving address info failed!" << // sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Retrieving address info failed!" <<
std::endl; // std::endl;
#endif //#endif
return HasReturnvaluesIF::RETURN_FAILED; // return HasReturnvaluesIF::RETURN_FAILED;
} // }
//
/* Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html */ // /* Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html */
serverSocket = socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol); // serverSocket = socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol);
if(serverSocket < 0) { // if(serverSocket < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not open UDP socket!" << // sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not open UDP socket!" <<
std::endl; // std::endl;
#else //#else
sif::printError("TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not open UDP socket!\n"); // sif::printError("TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not open UDP socket!\n");
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ //#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
freeaddrinfo(addrResult); // freeaddrinfo(addrResult);
handleError(Protocol::UDP, ErrorSources::SOCKET_CALL); // handleError(Protocol::UDP, ErrorSources::SOCKET_CALL);
return HasReturnvaluesIF::RETURN_FAILED; // return HasReturnvaluesIF::RETURN_FAILED;
} // }
//
retval = bind(serverSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen)); // retval = bind(serverSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
if(retval != 0) { // if(retval != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind " // sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind "
"local port (" << udpServerPort << ") to server socket!" << std::endl; // "local port (" << udpServerPort << ") to server socket!" << std::endl;
#endif //#endif
freeaddrinfo(addrResult); // freeaddrinfo(addrResult);
handleError(Protocol::UDP, ErrorSources::BIND_CALL); // handleError(Protocol::UDP, ErrorSources::BIND_CALL);
return HasReturnvaluesIF::RETURN_FAILED; // return HasReturnvaluesIF::RETURN_FAILED;
} // }
//
return HasReturnvaluesIF::RETURN_OK; // return HasReturnvaluesIF::RETURN_OK;
} //}
//
TmTcUnixUdpBridge::~TmTcUnixUdpBridge() { //TmTcUnixUdpBridge::~TmTcUnixUdpBridge() {
if(mutex != nullptr) { // if(mutex != nullptr) {
MutexFactory::instance()->deleteMutex(mutex); // MutexFactory::instance()->deleteMutex(mutex);
} // }
close(serverSocket); // close(serverSocket);
} //}
//
ReturnValue_t TmTcUnixUdpBridge::sendTm(const uint8_t *data, size_t dataLen) { //ReturnValue_t TmTcUnixUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
int flags = 0; // int flags = 0;
//
/* The target address can be set by different threads so this lock ensures thread-safety */ // /* The target address can be set by different threads so this lock ensures thread-safety */
MutexGuard lock(mutex, timeoutType, mutexTimeoutMs); // MutexGuard lock(mutex, timeoutType, mutexTimeoutMs);
//
if(ipAddrAnySet){ // if(ipAddrAnySet){
clientAddress.sin_addr.s_addr = htons(INADDR_ANY); // clientAddress.sin_addr.s_addr = htons(INADDR_ANY);
clientAddressLen = sizeof(clientAddress); // clientAddressLen = sizeof(clientAddress);
} // }
//
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
char ipAddress [15]; // char ipAddress [15];
sif::debug << "IP Address Sender: "<< // sif::debug << "IP Address Sender: "<<
inet_ntop(AF_INET,&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; // inet_ntop(AF_INET,&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#endif //#endif
//
ssize_t bytesSent = sendto( // ssize_t bytesSent = sendto(
serverSocket, // serverSocket,
data, // data,
dataLen, // dataLen,
flags, // flags,
reinterpret_cast<sockaddr*>(&clientAddress), // reinterpret_cast<sockaddr*>(&clientAddress),
clientAddressLen // clientAddressLen
); // );
if(bytesSent < 0) { // if(bytesSent < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcUnixUdpBridge::sendTm: Send operation failed." << std::endl; // sif::warning << "TmTcUnixUdpBridge::sendTm: Send operation failed." << std::endl;
#endif //#endif
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::SENDTO_CALL); // tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::SENDTO_CALL);
} // }
//
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were" // sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were"
" sent." << std::endl; // " sent." << std::endl;
#endif //#endif
//
return HasReturnvaluesIF::RETURN_OK; // return HasReturnvaluesIF::RETURN_OK;
} //}
//
void TmTcUnixUdpBridge::checkAndSetClientAddress(sockaddr_in& newAddress) { //void TmTcUnixUdpBridge::checkAndSetClientAddress(sockaddr_in& newAddress) {
/* The target address can be set by different threads so this lock ensures thread-safety */ // /* The target address can be set by different threads so this lock ensures thread-safety */
MutexGuard lock(mutex, timeoutType, mutexTimeoutMs); // MutexGuard lock(mutex, timeoutType, mutexTimeoutMs);
//
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1 //#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1
char ipAddress [15]; // char ipAddress [15];
sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET, // sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
&newAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; // &newAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
sif::debug << "IP Address Old: " << inet_ntop(AF_INET, // sif::debug << "IP Address Old: " << inet_ntop(AF_INET,
&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; // &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#endif //#endif
registerCommConnect(); // registerCommConnect();
//
/* Set new IP address to reply to. */ // /* Set new IP address to reply to. */
clientAddress = newAddress; // clientAddress = newAddress;
clientAddressLen = sizeof(clientAddress); // clientAddressLen = sizeof(clientAddress);
} //}
//
void TmTcUnixUdpBridge::setMutexProperties(MutexIF::TimeoutType timeoutType, //void TmTcUnixUdpBridge::setMutexProperties(MutexIF::TimeoutType timeoutType,
dur_millis_t timeoutMs) { // dur_millis_t timeoutMs) {
this->timeoutType = timeoutType; // this->timeoutType = timeoutType;
this->mutexTimeoutMs = timeoutMs; // this->mutexTimeoutMs = timeoutMs;
} //}
//
void TmTcUnixUdpBridge::setClientAddressToAny(bool ipAddrAnySet){ //void TmTcUnixUdpBridge::setClientAddressToAny(bool ipAddrAnySet){
this->ipAddrAnySet = ipAddrAnySet; // this->ipAddrAnySet = ipAddrAnySet;
} //}
//

View File

@ -1,53 +1,53 @@
#ifndef FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_ //#ifndef FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_
#define FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_ //#define FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_
//
#include "../../tmtcservices/AcceptsTelecommandsIF.h" //#include "../../tmtcservices/AcceptsTelecommandsIF.h"
#include "../../tmtcservices/TmTcBridge.h" //#include "../../tmtcservices/TmTcBridge.h"
#include <sys/socket.h> //#include <sys/socket.h>
#include <netinet/in.h> //#include <netinet/in.h>
#include <netinet/udp.h> //#include <netinet/udp.h>
//
class TmTcUnixUdpBridge: //class TmTcUnixUdpBridge:
public TmTcBridge { // public TmTcBridge {
friend class TcUnixUdpPollingTask; // friend class TcUnixUdpPollingTask;
public: //public:
//
/* The ports chosen here should not be used by any other process. // /* The ports chosen here should not be used by any other process.
List of used ports on Linux: /etc/services */ // List of used ports on Linux: /etc/services */
static const std::string DEFAULT_UDP_SERVER_PORT; // static const std::string DEFAULT_UDP_SERVER_PORT;
//
TmTcUnixUdpBridge(object_id_t objectId, object_id_t tcDestination, // TmTcUnixUdpBridge(object_id_t objectId, object_id_t tcDestination,
object_id_t tmStoreId, object_id_t tcStoreId, // object_id_t tmStoreId, object_id_t tcStoreId,
std::string serverPort = ""); // std::string serverPort = "");
virtual~ TmTcUnixUdpBridge(); // virtual~ TmTcUnixUdpBridge();
//
/** // /**
* Set properties of internal mutex. // * Set properties of internal mutex.
*/ // */
void setMutexProperties(MutexIF::TimeoutType timeoutType, dur_millis_t timeoutMs); // void setMutexProperties(MutexIF::TimeoutType timeoutType, dur_millis_t timeoutMs);
//
ReturnValue_t initialize() override; // ReturnValue_t initialize() override;
//
void checkAndSetClientAddress(sockaddr_in& clientAddress); // void checkAndSetClientAddress(sockaddr_in& clientAddress);
//
void setClientAddressToAny(bool ipAddrAnySet); // void setClientAddressToAny(bool ipAddrAnySet);
//
protected: //protected:
virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override; // virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override;
//
private: //private:
int serverSocket = 0; // int serverSocket = 0;
std::string udpServerPort; // std::string udpServerPort;
//
struct sockaddr_in clientAddress; // struct sockaddr_in clientAddress;
socklen_t clientAddressLen = 0; // socklen_t clientAddressLen = 0;
//
bool ipAddrAnySet = false; // bool ipAddrAnySet = false;
//
//! Access to the client address is mutex protected as it is set by another task. // //! Access to the client address is mutex protected as it is set by another task.
MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING; // MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING;
dur_millis_t mutexTimeoutMs = 20; // dur_millis_t mutexTimeoutMs = 20;
MutexIF* mutex; // MutexIF* mutex;
}; //};
//
#endif /* FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_ */ //#endif /* FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_ */