fsfw/tmtcservices/TmTcBridge.cpp

234 lines
7.0 KiB
C++
Raw Normal View History

2020-08-18 13:09:15 +02:00
#include "../tmtcservices/TmTcBridge.h"
2019-12-26 19:47:46 +01:00
2020-08-18 13:09:15 +02:00
#include "../ipc/QueueFactory.h"
#include "../tmtcservices/AcceptsTelecommandsIF.h"
#include "../serviceinterface/ServiceInterfaceStream.h"
#include "../globalfunctions/arrayprinter.h"
2019-12-26 19:47:46 +01:00
2020-07-08 15:06:46 +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);
2019-12-26 19:47:46 +01:00
}
TmTcBridge::~TmTcBridge() {}
2019-12-26 19:47:46 +01:00
ReturnValue_t TmTcBridge::setNumberOfSentPacketsPerCycle(
uint8_t sentPacketsPerCycle) {
2020-04-23 10:20:19 +02:00
if(sentPacketsPerCycle <= LIMIT_STORED_DATA_SENT_PER_CYCLE) {
this->sentPacketsPerCycle = sentPacketsPerCycle;
return RETURN_OK;
}
else {
2020-07-09 19:41:52 +02:00
sif::warning << "TmTcBridge::setNumberOfSentPacketsPerCycle: Number of "
<< "packets sent per cycle exceeds limits. "
<< "Keeping default value." << std::endl;
return RETURN_FAILED;
}
}
2020-04-23 10:20:19 +02:00
ReturnValue_t TmTcBridge::setMaxNumberOfPacketsStored(
uint8_t maxNumberOfPacketsStored) {
if(maxNumberOfPacketsStored <= LIMIT_DOWNLINK_PACKETS_STORED) {
this->maxNumberOfPacketsStored = maxNumberOfPacketsStored;
return RETURN_OK;
}
else {
2020-07-09 19:41:52 +02:00
sif::warning << "TmTcBridge::setMaxNumberOfPacketsStored: Number of "
<< "packets stored exceeds limits. "
<< "Keeping default value." << std::endl;
2020-04-23 10:20:19 +02:00
return RETURN_FAILED;
}
}
2019-12-26 19:47:46 +01:00
ReturnValue_t TmTcBridge::initialize() {
2020-07-08 15:06:46 +02:00
tcStore = objectManager->get<StorageManagerIF>(tcStoreId);
if (tcStore == nullptr) {
sif::error << "TmTcBridge::initialize: TC store invalid. Make sure"
"it is created and set up properly." << std::endl;
return ObjectManagerIF::CHILD_INIT_FAILED;
2019-12-26 19:47:46 +01:00
}
2020-07-08 15:06:46 +02:00
tmStore = objectManager->get<StorageManagerIF>(tmStoreId);
if (tmStore == nullptr) {
sif::error << "TmTcBridge::initialize: TM store invalid. Make sure"
"it is created and set up properly." << std::endl;
return ObjectManagerIF::CHILD_INIT_FAILED;
2019-12-26 19:47:46 +01:00
}
AcceptsTelecommandsIF* tcDistributor =
2020-07-08 15:06:46 +02:00
objectManager->get<AcceptsTelecommandsIF>(tcDestination);
if (tcDistributor == nullptr) {
sif::error << "TmTcBridge::initialize: TC Distributor invalid"
<< std::endl;
return ObjectManagerIF::CHILD_INIT_FAILED;
2019-12-26 19:47:46 +01:00
}
2020-08-26 14:55:27 +02:00
tmFifo = new DynamicFIFO<store_address_t>(maxNumberOfPacketsStored);
tmTcReceptionQueue->setDefaultDestination(tcDistributor->getRequestQueue());
2019-12-26 19:47:46 +01:00
return RETURN_OK;
}
ReturnValue_t TmTcBridge::performOperation(uint8_t operationCode) {
ReturnValue_t result;
result = handleTc();
if(result != RETURN_OK) {
2020-07-09 19:41:52 +02:00
sif::debug << "TmTcBridge::performOperation: "
<< "Error handling TCs" << std::endl;
2019-12-26 19:47:46 +01:00
}
result = handleTm();
if (result != RETURN_OK) {
2020-07-09 19:41:52 +02:00
sif::debug << "TmTcBridge::performOperation: "
<< "Error handling TMs" << std::endl;
2019-12-26 19:47:46 +01:00
}
return result;
}
ReturnValue_t TmTcBridge::handleTc() {
return HasReturnvaluesIF::RETURN_OK;
2019-12-26 19:47:46 +01:00
}
ReturnValue_t TmTcBridge::handleTm() {
2020-08-26 14:55:27 +02:00
ReturnValue_t status = HasReturnvaluesIF::RETURN_OK;
ReturnValue_t result = handleTmQueue();
2019-12-26 19:47:46 +01:00
if(result != RETURN_OK) {
2020-08-26 14:55:27 +02:00
sif::error << "TmTcBridge::handleTm: Error handling TM queue!"
<< std::endl;
status = result;
2019-12-26 19:47:46 +01:00
}
2020-08-26 14:55:27 +02:00
if(tmStored and communicationLinkUp and
(packetSentCounter < sentPacketsPerCycle)) {
result = handleStoredTm();
if(result != RETURN_OK) {
sif::error << "TmTcBridge::handleTm: Error handling stored TMs!"
<< std::endl;
status = result;
}
2019-12-26 19:47:46 +01:00
}
2020-08-26 14:55:27 +02:00
packetSentCounter = 0;
return status;
2019-12-26 19:47:46 +01:00
}
ReturnValue_t TmTcBridge::handleTmQueue() {
2019-12-26 19:47:46 +01:00
TmTcMessage message;
2020-08-18 13:09:15 +02:00
const uint8_t* data = nullptr;
size_t size = 0;
2020-08-21 16:15:40 +02:00
ReturnValue_t status = HasReturnvaluesIF::RETURN_OK;
for (ReturnValue_t result = tmTcReceptionQueue->receiveMessage(&message);
2020-08-26 14:55:27 +02:00
result == HasReturnvaluesIF::RETURN_OK;
result = tmTcReceptionQueue->receiveMessage(&message))
2019-12-26 19:47:46 +01:00
{
2020-08-26 14:55:27 +02:00
if(communicationLinkUp == false or
packetSentCounter >= sentPacketsPerCycle) {
2020-08-21 16:15:40 +02:00
storeDownlinkData(&message);
continue;
2019-12-26 19:47:46 +01:00
}
result = tmStore->getData(message.getStorageId(), &data, &size);
if (result != HasReturnvaluesIF::RETURN_OK) {
2020-08-26 14:55:27 +02:00
status = result;
2019-12-26 19:47:46 +01:00
continue;
}
result = sendTm(data, size);
2020-08-26 14:55:27 +02:00
if (result != HasReturnvaluesIF::RETURN_OK) {
status = result;
2019-12-26 19:47:46 +01:00
}
tmStore->deleteData(message.getStorageId());
2020-08-26 14:55:27 +02:00
packetSentCounter++;
2019-12-26 19:47:46 +01:00
}
2020-08-21 16:15:40 +02:00
return status;
2019-12-26 19:47:46 +01:00
}
ReturnValue_t TmTcBridge::storeDownlinkData(TmTcMessage *message) {
store_address_t storeId = 0;
2019-12-26 19:47:46 +01:00
2020-08-26 14:55:27 +02:00
if(tmFifo->full()) {
sif::debug << "TmTcBridge::storeDownlinkData: TM downlink max. number "
<< "of stored packet IDs reached! " << std::endl;
if(overwriteOld) {
tmFifo->retrieve(&storeId);
tmStore->deleteData(storeId);
}
else {
return HasReturnvaluesIF::RETURN_FAILED;
}
2019-12-26 19:47:46 +01:00
}
2020-08-26 14:55:27 +02:00
2019-12-26 19:47:46 +01:00
storeId = message->getStorageId();
2020-08-26 14:55:27 +02:00
tmFifo->insert(storeId);
2019-12-26 19:47:46 +01:00
tmStored = true;
return RETURN_OK;
}
2020-04-23 10:20:19 +02:00
ReturnValue_t TmTcBridge::handleStoredTm() {
2020-08-26 14:55:27 +02:00
ReturnValue_t status = RETURN_OK;
while(not tmFifo->empty() and packetSentCounter < sentPacketsPerCycle) {
//info << "TMTC Bridge: Sending stored TM data. There are "
// << (int) fifo.size() << " left to send\r\n" << std::flush;
2019-12-26 19:47:46 +01:00
store_address_t storeId;
2020-08-18 13:09:15 +02:00
const uint8_t* data = nullptr;
size_t size = 0;
2020-08-26 14:55:27 +02:00
tmFifo->retrieve(&storeId);
ReturnValue_t result = tmStore->getData(storeId, &data, &size);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
2020-04-22 18:21:50 +02:00
2020-08-26 14:55:27 +02:00
result = sendTm(data,size);
2019-12-26 19:47:46 +01:00
if(result != RETURN_OK) {
2020-04-23 21:59:24 +02:00
sif::error << "TMTC Bridge: Could not send stored downlink data"
2020-04-18 13:38:05 +02:00
<< std::endl;
2020-08-26 14:55:27 +02:00
status = result;
2019-12-26 19:47:46 +01:00
}
2020-08-26 14:55:27 +02:00
packetSentCounter ++;
2019-12-26 19:47:46 +01:00
2020-08-26 14:55:27 +02:00
if(tmFifo->empty()) {
2019-12-26 19:47:46 +01:00
tmStored = false;
}
tmStore->deleteData(storeId);
}
2020-08-26 14:55:27 +02:00
return status;
2019-12-26 19:47:46 +01:00
}
void TmTcBridge::registerCommConnect() {
2020-04-23 10:24:34 +02:00
if(not communicationLinkUp) {
2020-08-26 14:55:27 +02:00
//sif::info << "TMTC Bridge: Registered Comm Link Connect" << std::endl;
2019-12-26 19:47:46 +01:00
communicationLinkUp = true;
}
}
void TmTcBridge::registerCommDisconnect() {
2020-08-26 14:55:27 +02:00
//sif::info << "TMTC Bridge: Registered Comm Link Disconnect" << std::endl;
2019-12-26 19:47:46 +01:00
if(communicationLinkUp) {
communicationLinkUp = false;
}
}
MessageQueueId_t TmTcBridge::getReportReceptionQueue(uint8_t virtualChannel) {
return tmTcReceptionQueue->getId();
2019-12-26 19:47:46 +01:00
}
2020-04-23 10:20:19 +02:00
void TmTcBridge::printData(uint8_t * data, size_t dataLen) {
2020-06-23 10:47:31 +02:00
arrayprinter::print(data, dataLen);
2019-12-26 19:47:46 +01:00
}
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();
}
2020-08-26 14:55:27 +02:00
void TmTcBridge::setFifoToOverwriteOldData(bool overwriteOld) {
this->overwriteOld = overwriteOld;
}