fsfw/osal/common/UdpTcPollingTask.cpp

159 lines
5.0 KiB
C++
Raw Normal View History

#include "UdpTcPollingTask.h"
2021-03-12 02:15:21 +01:00
#include "tcpipHelpers.h"
2020-09-06 15:46:49 +02:00
#include "../../globalfunctions/arrayprinter.h"
#include "../../serviceinterface/ServiceInterfaceStream.h"
#include <winsock2.h>
2021-03-15 13:06:13 +01:00
//! Debugging preprocessor define.
2021-03-15 21:33:45 +01:00
#define FSFW_UDP_RCV_WIRETAPPING_ENABLED 0
2021-03-15 13:06:13 +01:00
UdpTcPollingTask::UdpTcPollingTask(object_id_t objectId,
2020-09-06 15:46:49 +02:00
object_id_t tmtcUnixUdpBridge, size_t frameSize,
double timeoutSeconds): SystemObject(objectId),
tmtcBridgeId(tmtcUnixUdpBridge) {
if(frameSize > 0) {
this->frameSize = frameSize;
}
else {
this->frameSize = DEFAULT_MAX_FRAME_SIZE;
}
2021-03-08 23:00:53 +01:00
/* Set up reception buffer with specified frame size.
For now, it is assumed that only one frame is held in the buffer! */
2020-09-06 15:46:49 +02:00
receptionBuffer.reserve(this->frameSize);
receptionBuffer.resize(this->frameSize);
if(timeoutSeconds == -1) {
receptionTimeout = DEFAULT_TIMEOUT;
}
else {
receptionTimeout = timevalOperations::toTimeval(timeoutSeconds);
}
}
UdpTcPollingTask::~UdpTcPollingTask() {}
2020-09-06 15:46:49 +02:00
ReturnValue_t UdpTcPollingTask::performOperation(uint8_t opCode) {
2021-03-12 02:15:21 +01:00
/* Sender Address is cached here. */
struct sockaddr_in senderAddress;
int senderAddressSize = sizeof(senderAddress);
2021-03-08 23:00:53 +01:00
/* Poll for new UDP datagrams in permanent loop. */
2020-09-06 15:46:49 +02:00
while(true) {
2021-03-12 02:15:21 +01:00
int bytesReceived = recvfrom(
serverUdpSocket,
reinterpret_cast<char*>(receptionBuffer.data()),
frameSize,
receptionFlags,
reinterpret_cast<sockaddr*>(&senderAddress),
&senderAddressSize
);
2020-09-06 15:46:49 +02:00
if(bytesReceived == SOCKET_ERROR) {
2021-03-08 23:00:53 +01:00
/* Handle error */
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UdpTcPollingTask::performOperation: Reception error." << std::endl;
#endif
2021-03-12 02:15:21 +01:00
tcpip::handleError(tcpip::Protocol::UDP, tcpip::ErrorSources::RECVFROM_CALL, 1000);
2020-09-06 15:46:49 +02:00
continue;
}
2021-03-12 01:40:58 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1
sif::debug << "UdpTcPollingTask::performOperation: " << bytesReceived <<
2021-03-08 23:00:53 +01:00
" bytes received" << std::endl;
#endif
2020-09-06 15:46:49 +02:00
ReturnValue_t result = handleSuccessfullTcRead(bytesReceived);
if(result != HasReturnvaluesIF::RETURN_FAILED) {
}
tmtcBridge->checkAndSetClientAddress(senderAddress);
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t UdpTcPollingTask::handleSuccessfullTcRead(size_t bytesRead) {
2020-09-06 15:46:49 +02:00
store_address_t storeId;
2021-03-12 02:15:21 +01:00
2021-03-12 01:40:58 +01:00
#if FSFW_UDP_RCV_WIRETAPPING_ENABLED == 1
2021-03-12 02:15:21 +01:00
arrayprinter::print(receptionBuffer.data(), bytesRead);
2021-03-08 23:00:53 +01:00
#endif
2021-03-12 02:15:21 +01:00
ReturnValue_t result = tcStore->addData(&storeId, receptionBuffer.data(), bytesRead);
2020-09-06 15:46:49 +02:00
if (result != HasReturnvaluesIF::RETURN_OK) {
2021-03-12 02:15:21 +01:00
#if FSFW_VERBOSE_LEVEL >= 1
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning<< "UdpTcPollingTask::transferPusToSoftwareBus: Data storage failed." <<
2021-03-12 02:15:21 +01:00
std::endl;
2021-03-08 23:00:53 +01:00
sif::warning << "Packet size: " << bytesRead << std::endl;
2021-03-12 02:15:21 +01:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
2020-09-06 15:46:49 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
}
TmTcMessage message(storeId);
result = MessageQueueSenderIF::sendMessage(targetTcDestination, &message);
if (result != HasReturnvaluesIF::RETURN_OK) {
2021-03-12 02:15:21 +01:00
#if FSFW_VERBOSE_LEVEL >= 1
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UdpTcPollingTask::handleSuccessfullTcRead: "
2021-03-12 02:15:21 +01:00
" Sending message to queue failed" << std::endl;
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
2020-09-06 15:46:49 +02:00
tcStore->deleteData(storeId);
}
return result;
}
ReturnValue_t UdpTcPollingTask::initialize() {
2020-09-06 15:46:49 +02:00
tcStore = objectManager->get<StorageManagerIF>(objects::TC_STORE);
if (tcStore == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UdpTcPollingTask::initialize: TC store uninitialized!" << std::endl;
#endif
2020-09-06 15:46:49 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
tmtcBridge = objectManager->get<UdpTmTcBridge>(tmtcBridgeId);
2020-09-06 15:46:49 +02:00
if(tmtcBridge == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UdpTcPollingTask::initialize: Invalid TMTC bridge object!" <<
2021-03-12 02:15:21 +01:00
std::endl;
#endif
2020-09-06 15:46:49 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
ReturnValue_t result = TcpIpBase::initialize();
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
2020-09-06 15:46:49 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t UdpTcPollingTask::initializeAfterTaskCreation() {
2021-03-12 00:45:32 +01:00
/* Initialize the destination after task creation. This ensures
that the destination has already been set in the TMTC bridge. */
2020-09-06 15:46:49 +02:00
targetTcDestination = tmtcBridge->getRequestQueue();
2021-03-12 02:15:21 +01:00
/* 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 */
2021-03-12 02:15:21 +01:00
serverUdpSocket = tmtcBridge->serverSocket;
2020-09-06 15:46:49 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
void UdpTcPollingTask::setTimeout(double timeoutSeconds) {
#ifdef _WIN32
2020-09-06 15:46:49 +02:00
DWORD timeoutMs = timeoutSeconds * 1000.0;
int result = setsockopt(serverUdpSocket, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<const char*>(&timeoutMs), sizeof(DWORD));
if(result == -1) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-12-20 01:56:21 +01:00
sif::error << "TcWinUdpPollingTask::TcSocketPollingTask: Setting "
2020-09-06 15:46:49 +02:00
"receive timeout failed with " << strerror(errno) << std::endl;
#endif
2020-09-06 15:46:49 +02:00
}
#elif defined(__unix__)
#endif
2020-09-06 15:46:49 +02:00
}