1
0
forked from fsfw/fsfw

Split up FIFO into StaticFIFO and normale FIFO

This commit is contained in:
2020-07-05 23:53:13 +02:00
parent 2395e487ae
commit ebec074655
12 changed files with 309 additions and 84 deletions

View File

@ -1,16 +1,80 @@
#include <framework/osal/linux/TcUnixUdpPollingTask.h>
TcSocketPollingTask::TcSocketPollingTask(object_id_t objectId,
object_id_t tmtcUnixUdpBridge): SystemObject(objectId) {
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;
}
// Set up reception buffer with specified frame size.
// For now, it is assumed that only one frame is held in the buffer!
receptionBuffer.reserve(this->frameSize);
receptionBuffer.resize(this->frameSize);
if(timeoutSeconds == -1) {
receptionTimeout = DEFAULT_TIMEOUT;
}
else {
receptionTimeout = timevalOperations::toTimeval(timeoutSeconds);
}
// Set receive timeout.
int result = setsockopt(serverUdpSocket, SOL_SOCKET, SO_RCVTIMEO,
&receptionTimeout, sizeof(receptionTimeout));
if(result == -1) {
sif::error << "TcSocketPollingTask::TcSocketPollingTask: Setting receive"
"timeout failed with " << strerror(errno) << std::endl;
return;
}
}
TcSocketPollingTask::~TcSocketPollingTask() {
}
ReturnValue_t TcSocketPollingTask::performOperation(uint8_t opCode) {
// Poll for new data permanently. The call will block until the specified
// length of bytes has been received or a timeout occured.
while(1) {
//! Sender Address is cached here.
struct sockaddr_in senderAddress;
socklen_t senderSockLen = 0;
ssize_t bytesReceived = recvfrom(serverUdpSocket,
receptionBuffer.data(), frameSize, receptionFlags,
reinterpret_cast<sockaddr*>(&senderAddress), &senderSockLen);
if(bytesReceived < 0) {
//handle error
sif::error << "TcSocketPollingTask::performOperation: recvfrom "
"failed with " << strerror(errno) << std::endl;
continue;
}
sif::debug << "TcSocketPollingTask::performOperation: " << bytesReceived
<< " bytes received" << std::endl;
ReturnValue_t result = handleSuccessfullTcRead();
tmtcBridge->checkAndSetClientAddress(senderAddress);
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t TcSocketPollingTask::initialize() {
tmtcBridge = objectManager->get<TmTcUnixUdpBridge>(tmtcBridgeId);
if(tmtcBridge == nullptr) {
sif::error << "TcSocketPollingTask::TcSocketPollingTask: Invalid"
" TMTC bridge object!" << std::endl;
return ObjectManagerIF::CHILD_INIT_FAILED;
}
serverUdpSocket = tmtcBridge->serverSocket;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t TcSocketPollingTask::handleSuccessfullTcRead() {
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -1,8 +1,11 @@
#ifndef FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_
#define FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_
#include <framework/objectmanager/SystemObject.h>
#include <framework/osal/linux/TmTcUnixUdpBridge.h>
#include <framework/tasks/ExecutableObjectIF.h>
#include <sys/socket.h>
#include <vector>
/**
* @brief This class can be used to implement the polling of a Unix socket,
@ -17,15 +20,34 @@ class TcSocketPollingTask: public SystemObject,
public ExecutableObjectIF {
friend class TmTcUnixUdpBridge;
public:
TcSocketPollingTask(object_id_t objectId, object_id_t tmtcUnixUdpBridge);
static constexpr size_t DEFAULT_MAX_FRAME_SIZE = 2048;
//! 0.5 default milliseconds timeout for now.
static constexpr timeval DEFAULT_TIMEOUT = {.tv_sec = 0, .tv_usec = 500};
TcSocketPollingTask(object_id_t objectId, object_id_t tmtcUnixUdpBridge,
size_t frameSize = 0, double timeoutSeconds = -1);
virtual~ TcSocketPollingTask();
virtual ReturnValue_t performOperation(uint8_t opCode) override;
virtual ReturnValue_t initialize() override;
private:
//! Sender Address is cached here.
const struct sockaddr senderAddress;
};
//! TMTC bridge is cached.
object_id_t tmtcBridgeId = objects::NO_OBJECT;
TmTcUnixUdpBridge* tmtcBridge = nullptr;
//! Reception flags: https://linux.die.net/man/2/recvfrom.
int receptionFlags = 0;
//! Server socket, which is member of TMTC bridge and is assigned in
//! constructor
int serverUdpSocket = 0;
std::vector<uint8_t> receptionBuffer;
size_t frameSize = 0;
timeval receptionTimeout;
ReturnValue_t handleSuccessfullTcRead();

View File

@ -1,11 +1,14 @@
#include <framework/osal/linux/TmTcUnixUdpBridge.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <errno.h>
#include <framework/ipc/MutexHelper.h>
TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId,
object_id_t ccsdsPacketDistributor, uint16_t serverPort,
uint16_t clientPort):
TmTcBridge(objectId, ccsdsPacketDistributor) {
mutex = MutexFactory::instance()->createMutex();
uint16_t setServerPort = DEFAULT_UDP_SERVER_PORT;
if(serverPort != 0xFFFF) {
setServerPort = serverPort;
@ -16,6 +19,7 @@ TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId,
setClientPort = clientPort;
}
// Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html
serverSocket = socket(AF_INET, SOCK_DGRAM, 0);
if(socket < 0) {
sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not open"
@ -26,14 +30,16 @@ TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId,
}
serverAddress.sin_family = AF_INET;
// Accept packets from any interface.
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(setServerPort);
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &serverSocketOptions,
sizeof(serverSocketOptions));
serverSocketLen = sizeof(serverAddress);
int result = bind(serverSocket,
reinterpret_cast<struct sockaddr*>(&serverAddress),
sizeof(serverAddress));
serverSocketLen);
if(result == -1) {
sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not bind "
"local port " << setServerPort << " to server socket!"
@ -81,6 +87,17 @@ void TmTcUnixUdpBridge::handleSocketError() {
}
}
void TmTcUnixUdpBridge::setTimeout(float timeoutSeconds) {
}
void TmTcUnixUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) {
MutexHelper lock(mutex, 10);
// Set new IP address if it has changed.
if(clientAddress.sin_addr.s_addr != newAddress.sin_addr.s_addr) {
clientAddress.sin_addr.s_addr = newAddress.sin_addr.s_addr;
}
}
void TmTcUnixUdpBridge::handleBindError() {
// See: https://man7.org/linux/man-pages/man2/bind.2.html
switch(errno) {

View File

@ -7,16 +7,20 @@
#include <netinet/udp.h>
class TmTcUnixUdpBridge: public TmTcBridge {
friend class TcSocketPollingTask;
public:
// The ports chosen here should not be used by any other process.
// List of used ports on Linux: /etc/services
static constexpr int DEFAULT_UDP_SERVER_PORT = 7301;
static constexpr int DEFAULT_UDP_CLIENT_PORT = 7302;
static constexpr uint16_t DEFAULT_UDP_SERVER_PORT = 7301;
static constexpr uint16_t DEFAULT_UDP_CLIENT_PORT = 7302;
TmTcUnixUdpBridge(object_id_t objectId, object_id_t ccsdsPacketDistributor,
uint16_t serverPort = 0xFFFF,uint16_t clientPort = 0xFFFF);
virtual~ TmTcUnixUdpBridge();
void setTimeout(float timeoutSeconds);
void checkAndSetClientAddress(sockaddr_in clientAddress);
protected:
virtual ReturnValue_t receiveTc(uint8_t ** recvBuffer,
size_t * size) override;
@ -24,8 +28,16 @@ protected:
private:
int serverSocket = 0;
const int serverSocketOptions = 0;
struct sockaddr_in clientAddress;
socklen_t clientSocketLen = 0;
struct sockaddr_in serverAddress;
socklen_t serverSocketLen = 0;
//! Access to the client address is mutex protected as it is set
//! by another task.
MutexIF* mutex;
void handleSocketError();
void handleBindError();