eive-obsw/mission/tmtc/CCSDSHandler.cpp

304 lines
9.1 KiB
C++
Raw Normal View History

2021-09-26 08:29:30 +02:00
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "fsfw/serviceinterface/serviceInterfaceDefintions.h"
#include "fsfw/objectmanager/ObjectManager.h"
#include "fsfw/ipc/QueueFactory.h"
2021-11-22 18:01:16 +01:00
#include "fsfw/events/EventManagerIF.h"
#include <linux/obc/PdecHandler.h>
2021-09-19 12:27:48 +02:00
2021-09-26 08:29:30 +02:00
#include "CCSDSHandler.h"
2021-09-19 12:27:48 +02:00
2021-11-21 18:07:05 +01:00
CCSDSHandler::CCSDSHandler(object_id_t objectId, object_id_t ptmeId, object_id_t tcDestination,
2021-11-22 18:01:16 +01:00
TxRateSetterIF* txRateSetterIF, GpioIF* gpioIF, gpioId_t enTxClock, gpioId_t enTxData) :
2021-11-21 18:07:05 +01:00
SystemObject(objectId), ptmeId(ptmeId), tcDestination(tcDestination), parameterHelper(this), actionHelper(
2021-11-22 18:01:16 +01:00
this, nullptr), txRateSetterIF(txRateSetterIF), gpioIF(gpioIF), enTxClock(
enTxClock), enTxData(enTxData) {
2021-09-26 08:29:30 +02:00
commandQueue = QueueFactory::instance()->createMessageQueue(QUEUE_SIZE);
2021-11-22 18:01:16 +01:00
eventQueue = QueueFactory::instance()->createMessageQueue(EventMessage::EVENT_MESSAGE_SIZE * 2);
2021-09-19 12:27:48 +02:00
}
CCSDSHandler::~CCSDSHandler() {
}
2021-09-26 08:29:30 +02:00
ReturnValue_t CCSDSHandler::performOperation(uint8_t operationCode) {
2021-11-22 18:01:16 +01:00
checkEvents();
2021-09-19 12:27:48 +02:00
readCommandQueue();
2021-09-22 16:54:55 +02:00
handleTelemetry();
handleTelecommands();
2021-11-22 18:01:16 +01:00
checkTxTimer();
2021-09-19 12:27:48 +02:00
return RETURN_OK;
}
2021-09-22 16:54:55 +02:00
void CCSDSHandler::handleTelemetry() {
VirtualChannelMapIter iter;
2021-09-26 08:29:30 +02:00
for (iter = virtualChannelMap.begin(); iter != virtualChannelMap.end(); iter++) {
iter->second->performOperation();
2021-09-22 16:54:55 +02:00
}
}
void CCSDSHandler::handleTelecommands() {
}
2021-09-19 12:27:48 +02:00
ReturnValue_t CCSDSHandler::initialize() {
2021-09-26 08:29:30 +02:00
ReturnValue_t result = RETURN_OK;
PtmeIF* ptme = ObjectManager::instance()->get<PtmeIF>(ptmeId);
2021-09-19 12:27:48 +02:00
if (ptme == nullptr) {
2021-09-26 08:29:30 +02:00
sif::warning << "Invalid PTME object" << std::endl;
2021-09-19 12:27:48 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
2021-09-26 08:29:30 +02:00
2021-11-01 12:41:20 +01:00
AcceptsTelecommandsIF* tcDistributor =
ObjectManager::instance()->get<AcceptsTelecommandsIF>(tcDestination);
if (tcDistributor == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "CCSDSHandler::initialize: Invalid TC Distributor object" << std::endl;
#endif
return ObjectManagerIF::CHILD_INIT_FAILED;
}
tcDistributorQueueId = tcDistributor->getRequestQueue();
2021-09-26 08:29:30 +02:00
result = parameterHelper.initialize();
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
2021-09-19 12:27:48 +02:00
}
2021-09-29 14:47:20 +02:00
2021-11-21 18:07:05 +01:00
result = actionHelper.initialize(commandQueue);
if (result != RETURN_OK) {
return result;
}
2021-09-29 14:47:20 +02:00
VirtualChannelMapIter iter;
for (iter = virtualChannelMap.begin(); iter != virtualChannelMap.end(); iter++) {
result = iter->second->initialize();
if (result != RETURN_OK) {
return result;
}
iter->second->setPtmeObject(ptme);
}
2021-11-22 18:01:16 +01:00
EventManagerIF* manager = ObjectManager::instance()->get<EventManagerIF>(
objects::EVENT_MANAGER);
2021-12-02 08:05:33 +01:00
if (manager == nullptr) {
2021-11-22 18:01:16 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "CCSDSHandler::initialize: Invalid event manager" << std::endl;
#endif
2021-12-02 08:05:33 +01:00
return ObjectManagerIF::CHILD_INIT_FAILED;
2021-11-22 18:01:16 +01:00
}
result = manager->registerListener(eventQueue->getId());
if (result != HasReturnvaluesIF::RETURN_OK) {
2021-12-02 08:05:33 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "CCSDSHandler::initialize: Failed to register CCSDS handler as event "
"listener" << std::endl;
#endif
return ObjectManagerIF::CHILD_INIT_FAILED;;
2021-11-22 18:01:16 +01:00
}
result = manager->subscribeToEventRange(eventQueue->getId(),
2021-11-25 14:20:53 +01:00
event::getEventId(PdecHandler::CARRIER_LOCK),
event::getEventId(PdecHandler::BIT_LOCK_PDEC));
2021-11-22 18:01:16 +01:00
if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "CCSDSHandler::initialize: Failed to subscribe to events from PDEC "
"handler" << std::endl;
#endif
return result;
}
2021-09-26 08:29:30 +02:00
return result;
2021-09-19 12:27:48 +02:00
}
void CCSDSHandler::readCommandQueue(void) {
CommandMessage commandMessage;
ReturnValue_t result = RETURN_FAILED;
2021-09-26 08:29:30 +02:00
result = commandQueue->receiveMessage(&commandMessage);
2021-09-22 16:54:55 +02:00
if (result == RETURN_OK) {
result = parameterHelper.handleParameterMessage(&commandMessage);
if (result == RETURN_OK) {
return;
}
2021-11-21 18:07:05 +01:00
result = actionHelper.handleActionMessage(&commandMessage);
if (result == RETURN_OK) {
return;
}
2021-09-22 16:54:55 +02:00
CommandMessage reply;
2021-09-26 08:29:30 +02:00
reply.setReplyRejected(CommandMessage::UNKNOWN_COMMAND,
2021-09-22 16:54:55 +02:00
commandMessage.getCommand());
2021-09-26 08:29:30 +02:00
commandQueue->reply(&reply);
2021-11-21 18:07:05 +01:00
return;
2021-09-22 16:54:55 +02:00
}
2021-09-19 12:27:48 +02:00
}
MessageQueueId_t CCSDSHandler::getCommandQueue() const {
2021-09-26 08:29:30 +02:00
return commandQueue->getId();
2021-09-19 12:27:48 +02:00
}
2021-09-26 08:29:30 +02:00
void CCSDSHandler::addVirtualChannel(VcId_t vcId, VirtualChannel* virtualChannel) {
2021-10-11 19:34:34 +02:00
if (vcId > common::NUMBER_OF_VIRTUAL_CHANNELS) {
2021-09-26 08:29:30 +02:00
sif::warning << "CCSDSHandler::addVirtualChannel: Invalid virtual channel ID" << std::endl;
return;
}
2021-09-19 12:27:48 +02:00
2021-09-26 08:29:30 +02:00
if (virtualChannel == nullptr) {
sif::warning << "CCSDSHandler::addVirtualChannel: Invalid virtual channel interface" << std::endl;
return;
}
2021-09-19 12:27:48 +02:00
2021-09-26 08:29:30 +02:00
auto status = virtualChannelMap.emplace(vcId, virtualChannel);
if (status.second == false) {
sif::warning << "CCSDSHandler::addVirtualChannel: Failed to add virtual channel to "
"virtual channel map" << std::endl;
return;
}
2021-09-19 12:27:48 +02:00
}
MessageQueueId_t CCSDSHandler::getReportReceptionQueue(uint8_t virtualChannel) {
2021-10-11 19:34:34 +02:00
if (virtualChannel < common::NUMBER_OF_VIRTUAL_CHANNELS) {
2021-09-26 08:29:30 +02:00
VirtualChannelMapIter iter = virtualChannelMap.find(virtualChannel);
if (iter != virtualChannelMap.end()) {
return iter->second->getReportReceptionQueue();
}
else {
sif::warning << "CCSDSHandler::getReportReceptionQueue: Virtual channel with ID "
<< static_cast<unsigned int>(virtualChannel) << " not in virtual channel map"
<< std::endl;
return MessageQueueIF::NO_QUEUE;
}
2021-09-19 12:27:48 +02:00
} else {
sif::debug << "CCSDSHandler::getReportReceptionQueue: Invalid virtual channel requested";
2021-09-26 08:29:30 +02:00
2021-09-19 12:27:48 +02:00
}
2021-09-26 08:29:30 +02:00
return MessageQueueIF::NO_QUEUE;
2021-09-19 12:27:48 +02:00
}
2021-09-22 16:54:55 +02:00
ReturnValue_t CCSDSHandler::getParameter(uint8_t domainId, uint8_t uniqueIdentifier,
ParameterWrapper *parameterWrapper, const ParameterWrapper *newValues,
uint16_t startAtIndex) {
return RETURN_OK;
2021-09-19 12:27:48 +02:00
}
2021-11-01 12:41:20 +01:00
uint16_t CCSDSHandler::getIdentifier() {
return 0;
}
MessageQueueId_t CCSDSHandler::getRequestQueue() {
// Forward packets directly to TC distributor
return tcDistributorQueueId;
}
2021-11-21 18:07:05 +01:00
ReturnValue_t CCSDSHandler::executeAction(ActionId_t actionId,
MessageQueueId_t commandedBy, const uint8_t* data, size_t size) {
switch(actionId) {
2021-11-22 18:01:16 +01:00
case SET_LOW_RATE: {
2021-11-21 18:07:05 +01:00
txRateSetterIF->setRate(BitRates::RATE_400KHZ);
return EXECUTION_FINISHED;
2021-11-22 18:01:16 +01:00
}
case SET_HIGH_RATE: {
2021-11-21 18:07:05 +01:00
txRateSetterIF->setRate(BitRates::RATE_2000KHZ);
return EXECUTION_FINISHED;
2021-11-22 18:01:16 +01:00
}
case EN_TRANSMITTER: {
enableTransmit();
return EXECUTION_FINISHED;
}
case DIS_TRANSMITTER: {
disableTransmit();
return EXECUTION_FINISHED;
}
2021-11-21 18:07:05 +01:00
default:
return COMMAND_NOT_IMPLEMENTED;
}
}
2021-11-22 18:01:16 +01:00
void CCSDSHandler::checkEvents() {
EventMessage event;
for (ReturnValue_t result = eventQueue->receiveMessage(&event);
result == RETURN_OK; result = eventQueue->receiveMessage(&event)) {
switch (event.getMessageId()) {
case EventMessage::EVENT_MESSAGE:
handleEvent(&event);
break;
default:
sif::debug << "CCSDSHandler::checkEvents: Did not subscribe to this event message"
<< std::endl;
break;
}
}
}
void CCSDSHandler::handleEvent(EventMessage* eventMessage) {
Event event = eventMessage->getEvent();
switch(event){
2021-11-25 14:09:06 +01:00
case PdecHandler::BIT_LOCK_PDEC: {
2021-11-22 18:01:16 +01:00
handleBitLockEvent();
break;
}
case PdecHandler::CARRIER_LOCK: {
handleCarrierLockEvent();
break;
}
default:
sif::debug << "CCSDSHandler::handleEvent: Did not subscribe to this event"
<< std::endl;
break;
}
}
void CCSDSHandler::handleBitLockEvent() {
if(transmitterCountdown.isBusy()) {
// Transmitter already enabled
return;
}
enableTransmit();
}
void CCSDSHandler::handleCarrierLockEvent() {
if (!enableTxWhenCarrierLock) {
return;
}
enableTransmit();
}
void CCSDSHandler::forwardLinkstate() {
VirtualChannelMapIter iter;
for(iter = virtualChannelMap.begin(); iter != virtualChannelMap.end(); iter++) {
iter->second->setLinkState(linkState);
}
}
void CCSDSHandler::enableTransmit() {
if(transmitterCountdown.isBusy()) {
// Transmitter already enabled
return;
}
transmitterCountdown.setTimeout(TRANSMITTER_TIMEOUT);
#if BOARD_TE0720 == 0
gpioIF->pullHigh(enTxClock);
gpioIF->pullHigh(enTxData);
2021-11-22 18:01:16 +01:00
#endif /* BOARD_TE0720 == 0 */
linkState = UP;
// Set link state of all virtual channels to link up
forwardLinkstate();
}
void CCSDSHandler::checkTxTimer() {
if(linkState == DOWN) {
return;
}
if (transmitterCountdown.hasTimedOut()) {
disableTransmit();
}
}
void CCSDSHandler::disableTransmit() {
#if BOARD_TE0720 == 0
gpioIF->pullLow(enTxClock);
gpioIF->pullLow(enTxData);
2021-11-22 18:01:16 +01:00
#endif /* BOARD_TE0720 == 0 */
linkState = DOWN;
forwardLinkstate();
transmitterCountdown.setTimeout(0);
2021-11-22 18:01:16 +01:00
}