fsfw/tmtcservices/TmTcBridge.cpp

273 lines
7.5 KiB
C++
Raw Normal View History

2020-12-14 21:30:39 +01:00
#include "TmTcBridge.h"
2020-08-13 20:53:35 +02:00
#include "../ipc/QueueFactory.h"
2021-01-29 17:16:34 +01:00
#include "../serviceinterface/ServiceInterface.h"
2020-08-13 20:53:35 +02:00
#include "../globalfunctions/arrayprinter.h"
2021-03-27 10:40:38 +01:00
#define TMTCBRIDGE_WIRETAPPING 0
2020-08-13 20:53:35 +02:00
TmTcBridge::TmTcBridge(object_id_t objectId, object_id_t tcDestination,
object_id_t tmStoreId, object_id_t tcStoreId):
SystemObject(objectId),tmStoreId(tmStoreId), tcStoreId(tcStoreId),
tcDestination(tcDestination)
{
tmTcReceptionQueue = QueueFactory::instance()->
createMessageQueue(TMTC_RECEPTION_QUEUE_DEPTH);
}
2021-04-11 21:54:48 +02:00
TmTcBridge::~TmTcBridge() {
QueueFactory::instance()->deleteMessageQueue(tmTcReceptionQueue);
}
2020-08-13 20:53:35 +02:00
ReturnValue_t TmTcBridge::setNumberOfSentPacketsPerCycle(
uint8_t sentPacketsPerCycle) {
if(sentPacketsPerCycle <= LIMIT_STORED_DATA_SENT_PER_CYCLE) {
this->sentPacketsPerCycle = sentPacketsPerCycle;
return RETURN_OK;
}
else {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::warning << "TmTcBridge::setNumberOfSentPacketsPerCycle: Number of "
<< "packets sent per cycle exceeds limits. "
<< "Keeping default value." << std::endl;
#endif
2020-08-13 20:53:35 +02:00
return RETURN_FAILED;
}
}
ReturnValue_t TmTcBridge::setMaxNumberOfPacketsStored(
uint8_t maxNumberOfPacketsStored) {
if(maxNumberOfPacketsStored <= LIMIT_DOWNLINK_PACKETS_STORED) {
this->maxNumberOfPacketsStored = maxNumberOfPacketsStored;
return RETURN_OK;
}
else {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::warning << "TmTcBridge::setMaxNumberOfPacketsStored: Number of "
<< "packets stored exceeds limits. "
<< "Keeping default value." << std::endl;
#endif
2020-08-13 20:53:35 +02:00
return RETURN_FAILED;
}
}
ReturnValue_t TmTcBridge::initialize() {
tcStore = objectManager->get<StorageManagerIF>(tcStoreId);
if (tcStore == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::error << "TmTcBridge::initialize: TC store invalid. Make sure"
"it is created and set up properly." << std::endl;
#endif
2020-08-13 20:53:35 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
tmStore = objectManager->get<StorageManagerIF>(tmStoreId);
if (tmStore == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::error << "TmTcBridge::initialize: TM store invalid. Make sure"
"it is created and set up properly." << std::endl;
#endif
2020-08-13 20:53:35 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
AcceptsTelecommandsIF* tcDistributor =
objectManager->get<AcceptsTelecommandsIF>(tcDestination);
if (tcDistributor == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::error << "TmTcBridge::initialize: TC Distributor invalid"
<< std::endl;
#endif
2020-08-13 20:53:35 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
tmFifo = new DynamicFIFO<store_address_t>(maxNumberOfPacketsStored);
2020-08-13 20:53:35 +02:00
tmTcReceptionQueue->setDefaultDestination(tcDistributor->getRequestQueue());
return RETURN_OK;
}
ReturnValue_t TmTcBridge::performOperation(uint8_t operationCode) {
ReturnValue_t result;
result = handleTc();
if(result != RETURN_OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::debug << "TmTcBridge::performOperation: "
<< "Error handling TCs" << std::endl;
#endif
2020-08-13 20:53:35 +02:00
}
result = handleTm();
if (result != RETURN_OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::debug << "TmTcBridge::performOperation: "
<< "Error handling TMs" << std::endl;
#endif
2020-08-13 20:53:35 +02:00
}
return result;
}
ReturnValue_t TmTcBridge::handleTc() {
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t TmTcBridge::handleTm() {
ReturnValue_t status = HasReturnvaluesIF::RETURN_OK;
2020-08-13 20:53:35 +02:00
ReturnValue_t result = handleTmQueue();
if(result != RETURN_OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-01-29 17:16:34 +01:00
sif::error << "TmTcBridge::handleTm: Error handling TM queue with error code 0x" <<
std::hex << result << std::dec << "!" << std::endl;
#endif
status = result;
2020-08-13 20:53:35 +02:00
}
if(tmStored and communicationLinkUp and
(packetSentCounter < sentPacketsPerCycle)) {
result = handleStoredTm();
if(result != RETURN_OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-01-29 17:16:34 +01:00
sif::error << "TmTcBridge::handleTm: Error handling stored TMs!" << std::endl;
#endif
status = result;
}
2020-08-13 20:53:35 +02:00
}
packetSentCounter = 0;
return status;
2020-08-13 20:53:35 +02:00
}
ReturnValue_t TmTcBridge::handleTmQueue() {
TmTcMessage message;
2020-06-23 10:45:47 +02:00
const uint8_t* data = nullptr;
size_t size = 0;
ReturnValue_t status = HasReturnvaluesIF::RETURN_OK;
2020-08-13 20:53:35 +02:00
for (ReturnValue_t result = tmTcReceptionQueue->receiveMessage(&message);
result == HasReturnvaluesIF::RETURN_OK;
result = tmTcReceptionQueue->receiveMessage(&message))
2020-08-13 20:53:35 +02:00
{
2021-01-29 17:16:34 +01:00
#if FSFW_VERBOSE_LEVEL >= 3
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-01-29 17:16:34 +01:00
sif::info << "Sent packet counter: " << static_cast<int>(packetSentCounter) << std::endl;
#else
sif::printInfo("Sent packet counter: %d\n", packetSentCounter);
#endif
2021-01-29 17:16:34 +01:00
#endif /* FSFW_VERBOSE_LEVEL >= 3 */
if(communicationLinkUp == false or
packetSentCounter >= sentPacketsPerCycle) {
storeDownlinkData(&message);
continue;
2020-08-13 20:53:35 +02:00
}
result = tmStore->getData(message.getStorageId(), &data, &size);
if (result != HasReturnvaluesIF::RETURN_OK) {
status = result;
2020-08-13 20:53:35 +02:00
continue;
}
result = sendTm(data, size);
if (result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
else {
tmStore->deleteData(message.getStorageId());
packetSentCounter++;
2020-08-13 20:53:35 +02:00
}
}
return status;
2020-08-13 20:53:35 +02:00
}
ReturnValue_t TmTcBridge::storeDownlinkData(TmTcMessage *message) {
store_address_t storeId = 0;
2021-03-12 00:45:32 +01:00
if(tmFifo == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
2020-08-13 20:53:35 +02:00
if(tmFifo->full()) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "TmTcBridge::storeDownlinkData: TM downlink max. number "
<< "of stored packet IDs reached! " << std::endl;
#endif
if(overwriteOld) {
tmFifo->retrieve(&storeId);
tmStore->deleteData(storeId);
}
else {
return HasReturnvaluesIF::RETURN_FAILED;
}
2020-08-13 20:53:35 +02:00
}
2020-08-13 20:53:35 +02:00
storeId = message->getStorageId();
tmFifo->insert(storeId);
2020-08-13 20:53:35 +02:00
tmStored = true;
return RETURN_OK;
}
ReturnValue_t TmTcBridge::handleStoredTm() {
ReturnValue_t status = RETURN_OK;
while(not tmFifo->empty() and packetSentCounter < sentPacketsPerCycle) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
//sif::info << "TMTC Bridge: Sending stored TM data. There are "
// << (int) tmFifo->size() << " left to send\r\n" << std::flush;
#endif
2020-08-13 20:53:35 +02:00
store_address_t storeId;
2020-06-23 10:45:47 +02:00
const uint8_t* data = nullptr;
size_t size = 0;
tmFifo->retrieve(&storeId);
ReturnValue_t result = tmStore->getData(storeId, &data, &size);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
2020-08-13 20:53:35 +02:00
result = sendTm(data,size);
2020-08-13 20:53:35 +02:00
if(result != RETURN_OK) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::error << "TMTC Bridge: Could not send stored downlink data"
<< std::endl;
#endif
status = result;
2020-08-13 20:53:35 +02:00
}
packetSentCounter ++;
2020-08-13 20:53:35 +02:00
if(tmFifo->empty()) {
2020-08-13 20:53:35 +02:00
tmStored = false;
}
tmStore->deleteData(storeId);
}
return status;
2020-08-13 20:53:35 +02:00
}
void TmTcBridge::registerCommConnect() {
if(not communicationLinkUp) {
communicationLinkUp = true;
}
}
void TmTcBridge::registerCommDisconnect() {
if(communicationLinkUp) {
communicationLinkUp = false;
}
}
MessageQueueId_t TmTcBridge::getReportReceptionQueue(uint8_t virtualChannel) {
return tmTcReceptionQueue->getId();
}
void TmTcBridge::printData(uint8_t * data, size_t dataLen) {
arrayprinter::print(data, dataLen);
}
uint16_t TmTcBridge::getIdentifier() {
// This is no PUS service, so we just return 0
return 0;
}
MessageQueueId_t TmTcBridge::getRequestQueue() {
// Default implementation: Relay TC messages to TC distributor directly.
return tmTcReceptionQueue->getDefaultDestination();
}
void TmTcBridge::setFifoToOverwriteOldData(bool overwriteOld) {
this->overwriteOld = overwriteOld;
}