From 029b2133e6df5ecee9341226706ad6f8bcdf6b8b Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Mon, 23 Mar 2020 18:03:00 +0100 Subject: [PATCH] new adaptions for cookie + comIF changes hook for performOp() added --- devicehandlers/DeviceHandlerBase.cpp | 101 +++++++++-------- devicehandlers/DeviceHandlerBase.h | 164 ++++++++++++++++----------- 2 files changed, 151 insertions(+), 114 deletions(-) diff --git a/devicehandlers/DeviceHandlerBase.cpp b/devicehandlers/DeviceHandlerBase.cpp index c4f7465d..5834db2f 100644 --- a/devicehandlers/DeviceHandlerBase.cpp +++ b/devicehandlers/DeviceHandlerBase.cpp @@ -16,37 +16,34 @@ object_id_t DeviceHandlerBase::powerSwitcherId = 0; object_id_t DeviceHandlerBase::rawDataReceiverId = 0; object_id_t DeviceHandlerBase::defaultFDIRParentId = 0; -DeviceHandlerBase::DeviceHandlerBase(uint32_t logicalAddress_, - object_id_t setObjectId, uint32_t maxDeviceReplyLen, - uint8_t setDeviceSwitch, object_id_t deviceCommunication, - uint32_t thermalStatePoolId, uint32_t thermalRequestPoolId, - FailureIsolationBase* fdirInstance, uint32_t cmdQueueSize) : - SystemObject(setObjectId), rawPacket(0), rawPacketLen(0), mode(MODE_OFF), - submode(SUBMODE_NONE), pstStep(0), maxDeviceReplyLen(maxDeviceReplyLen), - wiretappingMode(OFF), defaultRawReceiver(0), storedRawData(StorageManagerIF::INVALID_ADDRESS), - requestedRawTraffic(0), powerSwitcher(NULL), IPCStore(NULL), - deviceCommunicationId(deviceCommunication), communicationInterface(NULL), - cookie(NULL), commandQueue(NULL), deviceThermalStatePoolId(thermalStatePoolId), - deviceThermalRequestPoolId(thermalRequestPoolId), healthHelper(this, setObjectId), - modeHelper(this), parameterHelper(this), childTransitionFailure(RETURN_OK), - ignoreMissedRepliesCount(0), fdirInstance(fdirInstance), hkSwitcher(this), - defaultFDIRUsed(fdirInstance == NULL), switchOffWasReported(false), - executingTask(NULL), actionHelper(this, NULL), cookieInfo(), logicalAddress(logicalAddress_), - timeoutStart(0), childTransitionDelay(5000), transitionSourceMode(_MODE_POWER_DOWN), - transitionSourceSubMode(SUBMODE_NONE), deviceSwitch(setDeviceSwitch) +DeviceHandlerBase::DeviceHandlerBase(object_id_t setObjectId, address_t logicalAddress_, + object_id_t deviceCommunication, Cookie * cookie_, size_t maxReplyLen, + uint8_t setDeviceSwitch, uint32_t thermalStatePoolId, uint32_t thermalRequestPoolId, + FailureIsolationBase* fdirInstance, size_t cmdQueueSize) : + SystemObject(setObjectId), mode(MODE_OFF), submode(SUBMODE_NONE), + wiretappingMode(OFF), storedRawData(StorageManagerIF::INVALID_ADDRESS), + deviceCommunicationId(deviceCommunication), cookie(cookie_), + deviceThermalStatePoolId(thermalStatePoolId), deviceThermalRequestPoolId(thermalRequestPoolId), + healthHelper(this, setObjectId), modeHelper(this), parameterHelper(this), + fdirInstance(fdirInstance), hkSwitcher(this), + defaultFDIRUsed(fdirInstance == nullptr), switchOffWasReported(false), + executingTask(nullptr), actionHelper(this, nullptr), cookieInfo(), + logicalAddress(logicalAddress_), childTransitionDelay(5000), + transitionSourceMode(_MODE_POWER_DOWN), transitionSourceSubMode(SUBMODE_NONE), + deviceSwitch(setDeviceSwitch) { + this->cookie->setMaxReplyLen(maxReplyLen); commandQueue = QueueFactory::instance()-> createMessageQueue(cmdQueueSize, CommandMessage::MAX_MESSAGE_SIZE); cookieInfo.state = COOKIE_UNUSED; insertInCommandMap(RAW_COMMAND_ID); - if (this->fdirInstance == NULL) { + if (this->fdirInstance == nullptr) { this->fdirInstance = new DeviceHandlerFailureIsolation(setObjectId, defaultFDIRParentId); } } DeviceHandlerBase::~DeviceHandlerBase() { - communicationInterface->close(cookie); if (defaultFDIRUsed) { delete fdirInstance; } @@ -63,10 +60,12 @@ ReturnValue_t DeviceHandlerBase::performOperation(uint8_t counter) { decrementDeviceReplyMap(); fdirInstance->checkForFailures(); hkSwitcher.performOperation(); + performOperationHook(); } if (mode == MODE_OFF) { return RETURN_OK; } + switch (getRmapAction()) { case SEND_WRITE: if ((cookieInfo.state == COOKIE_UNUSED)) { @@ -87,6 +86,7 @@ ReturnValue_t DeviceHandlerBase::performOperation(uint8_t counter) { default: break; } + return RETURN_OK; } @@ -102,11 +102,11 @@ ReturnValue_t DeviceHandlerBase::initialize() { return RETURN_FAILED; } - result = communicationInterface->open(&cookie, logicalAddress, - maxDeviceReplyLen); - if (result != RETURN_OK) { - return result; - } +// result = communicationInterface->open(&cookie, logicalAddress, +// maxDeviceReplyLen, comParameter1, comParameter2); +// if (result != RETURN_OK) { +// return result; +// } IPCStore = objectManager->get(objects::IPC_STORE); if (IPCStore == NULL) { @@ -166,7 +166,6 @@ ReturnValue_t DeviceHandlerBase::initialize() { mySet.commit(PoolVariableIF::VALID); return RETURN_OK; - } void DeviceHandlerBase::decrementDeviceReplyMap() { @@ -549,7 +548,7 @@ void DeviceHandlerBase::doGetWrite() { void DeviceHandlerBase::doSendRead() { ReturnValue_t result; - result = communicationInterface->requestReceiveMessage(cookie); + result = communicationInterface->requestReceiveMessage(cookie, requestLen); if (result == RETURN_OK) { cookieInfo.state = COOKIE_READ_SENT; } else { @@ -563,10 +562,10 @@ void DeviceHandlerBase::doSendRead() { } void DeviceHandlerBase::doGetRead() { - uint32_t receivedDataLen; + size_t receivedDataLen; uint8_t *receivedData; DeviceCommandId_t foundId = 0xFFFFFFFF; - uint32_t foundLen = 0; + size_t foundLen = 0; ReturnValue_t result; if (cookieInfo.state != COOKIE_READ_SENT) { @@ -637,8 +636,8 @@ void DeviceHandlerBase::doGetRead() { } ReturnValue_t DeviceHandlerBase::getStorageData(store_address_t storageAddress, - uint8_t * *data, uint32_t * len) { - uint32_t lenTmp; + uint8_t ** data, size_t * len) { + size_t lenTmp; if (IPCStore == NULL) { *data = NULL; @@ -689,7 +688,7 @@ void DeviceHandlerBase::replyRawData(const uint8_t *data, size_t len, //Default child implementations -DeviceHandlerBase::RmapAction_t DeviceHandlerBase::getRmapAction() { +DeviceHandlerBase::CommunicationAction_t DeviceHandlerBase::getRmapAction() { switch (pstStep) { case 0: return SEND_WRITE; @@ -749,20 +748,20 @@ void DeviceHandlerBase::handleReply(const uint8_t* receivedData, } } -ReturnValue_t DeviceHandlerBase::switchCookieChannel(object_id_t newChannelId) { - DeviceCommunicationIF *newCommunication = objectManager->get< - DeviceCommunicationIF>(newChannelId); - - if (newCommunication != NULL) { - ReturnValue_t result = newCommunication->reOpen(cookie, logicalAddress, - maxDeviceReplyLen); - if (result != RETURN_OK) { - return result; - } - return RETURN_OK; - } - return RETURN_FAILED; -} +//ReturnValue_t DeviceHandlerBase::switchCookieChannel(object_id_t newChannelId) { +// DeviceCommunicationIF *newCommunication = objectManager->get< +// DeviceCommunicationIF>(newChannelId); +// +// if (newCommunication != NULL) { +// ReturnValue_t result = newCommunication->reOpen(cookie, logicalAddress, +// maxDeviceReplyLen, comParameter1, comParameter2); +// if (result != RETURN_OK) { +// return result; +// } +// return RETURN_OK; +// } +// return RETURN_FAILED; +//} void DeviceHandlerBase::buildRawDeviceCommand(CommandMessage* commandMessage) { storedRawData = DeviceHandlerMessage::getStoreAddress(commandMessage); @@ -1046,12 +1045,13 @@ ReturnValue_t DeviceHandlerBase::handleDeviceHandlerMessage( } replyReturnvalueToCommand(RETURN_OK); return RETURN_OK; - case DeviceHandlerMessage::CMD_SWITCH_IOBOARD: + case DeviceHandlerMessage::CMD_SWITCH_ADDRESS: if (mode != MODE_OFF) { replyReturnvalueToCommand(WRONG_MODE_FOR_COMMAND); } else { - result = switchCookieChannel( - DeviceHandlerMessage::getIoBoardObjectId(message)); + // rework in progress + //result = switchCookieChannel( + // DeviceHandlerMessage::getIoBoardObjectId(message)); if (result == RETURN_OK) { replyReturnvalueToCommand(RETURN_OK); } else { @@ -1278,3 +1278,6 @@ void DeviceHandlerBase::debugInterface(uint8_t positionTracker, object_id_t obje uint32_t DeviceHandlerBase::getLogicalAddress() { return logicalAddress; } + +void DeviceHandlerBase::performOperationHook() { +} diff --git a/devicehandlers/DeviceHandlerBase.h b/devicehandlers/DeviceHandlerBase.h index f16a4ff9..e9288921 100644 --- a/devicehandlers/DeviceHandlerBase.h +++ b/devicehandlers/DeviceHandlerBase.h @@ -18,9 +18,9 @@ #include #include #include -#include #include #include +#include namespace Factory{ void setStaticFrameworkObjectIds(); @@ -29,7 +29,7 @@ void setStaticFrameworkObjectIds(); class StorageManagerIF; /** - * \defgroup devices Devices + * @defgroup devices Devices * Contains all devices and the DeviceHandlerBase class. */ @@ -38,18 +38,20 @@ class StorageManagerIF; * @details * Documentation: Dissertation Baetz p.138,139, p.141-149 * - * It features handling of @link DeviceHandlerIF::Mode_t Modes @endlink, communication with - * physical devices, using the @link DeviceCommunicationIF @endlink, and communication with commanding objects. + * It features handling of @link DeviceHandlerIF::Mode_t Modes @endlink, + * communication with physical devices, using the @link DeviceCommunicationIF @endlink, + * and communication with commanding objects. * It inherits SystemObject and thus can be created by the ObjectManagerIF. * * This class uses the opcode of ExecutableObjectIF to perform a step-wise execution. * For each step an RMAP action is selected and executed. * If data has been received (GET_READ), the data will be interpreted. - * The action for each step can be defined by the child class but as most device handlers share a 4-call - * (sendRead-getRead-sendWrite-getWrite) structure, a default implementation is provided. - * NOTE: RMAP is a standard which is used for FLP. + * The action for each step can be defined by the child class but as most + * device handlers share a 4-call (sendRead-getRead-sendWrite-getWrite) structure, + * a default implementation is provided. NOTE: RMAP is a standard which is used for FLP. * RMAP communication is not mandatory for projects implementing the FSFW. - * However, the communication principles are similar to RMAP as there are two write and two send calls involved. + * However, the communication principles are similar to RMAP as there are + * two write and two send calls involved. * * Device handler instances should extend this class and implement the abstract functions. * Components and drivers can send so called cookies which are used for communication @@ -83,7 +85,7 @@ public: * The constructor passes the objectId to the SystemObject(). * * @param setObjectId the ObjectId to pass to the SystemObject() Constructor - * @param maxDeviceReplyLen the length the RMAP getRead call will be sent with + * @param maxDeviceReplyLen the largest allowed reply size * @param setDeviceSwitch the switch the device is connected to, for devices using two switches, overwrite getSwitches() * @param deviceCommuncation Communcation Interface object which is used to implement communication functions * @param thermalStatePoolId @@ -91,12 +93,11 @@ public: * @param fdirInstance * @param cmdQueueSize */ - DeviceHandlerBase(uint32_t logicalAddress, object_id_t setObjectId, - uint32_t maxDeviceReplyLen, uint8_t setDeviceSwitch, - object_id_t deviceCommunication, - uint32_t thermalStatePoolId = PoolVariableIF::NO_PARAMETER, + DeviceHandlerBase(object_id_t setObjectId, address_t logicalAddress_, + object_id_t deviceCommunication, Cookie* cookie_, size_t maxReplyLen, + uint8_t setDeviceSwitch, uint32_t thermalStatePoolId = PoolVariableIF::NO_PARAMETER, uint32_t thermalRequestPoolId = PoolVariableIF::NO_PARAMETER, - FailureIsolationBase* fdirInstance = NULL, uint32_t cmdQueueSize = 20); + FailureIsolationBase* fdirInstance = nullptr, size_t cmdQueueSize = 20); /** * @brief This function is the device handler base core component and is called periodically. @@ -281,8 +282,8 @@ protected: * - @c DeviceHandlerIF::IGNORE_FULL_PACKET Ignore the packet * - @c APERIODIC_REPLY if a valid reply is received that has not been requested by a command, but should be handled anyway (@see also fillCommandAndCookieMap() ) */ - virtual ReturnValue_t scanForReply(const uint8_t *start, uint32_t len, - DeviceCommandId_t *foundId, uint32_t *foundLen) = 0; + virtual ReturnValue_t scanForReply(const uint8_t *start, size_t len, + DeviceCommandId_t *foundId, size_t *foundLen) = 0; /** * @brief Interpret a reply from the device. @@ -301,6 +302,14 @@ protected: virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) = 0; + /** + * set all datapool variables that are update periodically in normal mode invalid + * + * Child classes should provide an implementation which sets all those variables invalid + * which are set periodically during any normal mode. + */ + virtual void setNormalDatapoolEntriesInvalid() = 0; + /** * @brief Can be implemented by child handler to * perform debugging @@ -344,6 +353,12 @@ protected: virtual ReturnValue_t getSwitches(const uint8_t **switches, uint8_t *numberOfSwitches); + /** + * Can be used to perform device specific periodic operations. + * This is called on the SEND_READ step of the performOperation() call + */ + virtual void performOperationHook(); + public: /** * @param parentQueueId @@ -396,11 +411,32 @@ protected: /** * Pointer to the raw packet that will be sent. */ - uint8_t *rawPacket; + uint8_t *rawPacket = nullptr; /** * Size of the #rawPacket. */ - uint32_t rawPacketLen; + size_t rawPacketLen = 0; + + /** + * Size of data to request. + */ + size_t requestLen = 0; + +// /** +// * This union (or std::variant) type can be used to set comParameters which +// * are passed in the open() and reOpen() calls to the communication +// * interface +// * TODO: I don't know if we should use C++17 features but if we do we propably +// * should also write a function to get either a storeId handle +// * or an array handle so these values can be set conveniently. +// * The good think is that if there are many parameters, they can +// * be stored in the IPC pool. But maybe two uint32_t parameters are enough. +// * Simple = Good. It is downwards compatible and one can still store +// * 4 bytes of parameters AND a store ID. +// */ +// comParameters_t comParameters; + // uint32_t comParameter1 = 0; + // uint32_t comParameter2 = 0; /** * The mode the device handler is currently in. @@ -419,12 +455,12 @@ protected: /** * This is the counter value from performOperation(). */ - uint8_t pstStep; + uint8_t pstStep = 0; /** * This will be used in the RMAP getRead command as expected length, is set by the constructor, can be modiefied at will. */ - const uint32_t maxDeviceReplyLen; + const uint32_t maxDeviceReplyLen = 0; /** * wiretapping flag: @@ -442,7 +478,7 @@ protected: * Statically initialized in initialize() to a configurable object. Used when there is no method * of finding a recipient, ie raw mode and reporting erreonous replies */ - MessageQueueId_t defaultRawReceiver; + MessageQueueId_t defaultRawReceiver = 0; store_address_t storedRawData; @@ -451,19 +487,19 @@ protected: * * if #isWiretappingActive all raw communication from and to the device will be sent to this queue */ - MessageQueueId_t requestedRawTraffic; + MessageQueueId_t requestedRawTraffic = 0; /** * the object used to set power switches */ - PowerSwitchIF *powerSwitcher; + PowerSwitchIF *powerSwitcher = nullptr; /** * Pointer to the IPCStore. * * This caches the pointer received from the objectManager in the constructor. */ - StorageManagerIF *IPCStore; + StorageManagerIF *IPCStore = nullptr; /** * cached for init @@ -473,17 +509,18 @@ protected: /** * Communication object used for device communication */ - DeviceCommunicationIF *communicationInterface; + DeviceCommunicationIF *communicationInterface = nullptr; /** - * Cookie used for communication + * Cookie used for communication. This is passed to the communication + * interface. */ Cookie *cookie; /** * The MessageQueue used to receive device handler commands and to send replies. */ - MessageQueueIF* commandQueue; + MessageQueueIF* commandQueue = nullptr; /** * this is the datapool variable with the thermal state of the device @@ -512,9 +549,9 @@ protected: * Optional Error code * Can be set in doStartUp(), doShutDown() and doTransition() to signal cause for Transition failure. */ - ReturnValue_t childTransitionFailure; + ReturnValue_t childTransitionFailure = RETURN_OK; - uint32_t ignoreMissedRepliesCount; //!< Counts if communication channel lost a reply, so some missed replys can be ignored. + uint32_t ignoreMissedRepliesCount = 0; //!< Counts if communication channel lost a reply, so some missed replys can be ignored. FailureIsolationBase* fdirInstance; //!< Pointer to the used FDIR instance. If not provided by child, default class is instantiated. @@ -531,6 +568,28 @@ protected: static object_id_t rawDataReceiverId; //!< Object which receives RAW data by default. static object_id_t defaultFDIRParentId; //!< Object which may be the root cause of an identified fault. + + /** + * Set the device handler mode + * + * Sets #timeoutStart with every call. + * + * Sets #transitionTargetMode if necessary so transitional states can be entered from everywhere without breaking the state machine + * (which relies on a correct #transitionTargetMode). + * + * The submode is left unchanged. + * + * + * @param newMode + */ + void setMode(Mode_t newMode); + + /** + * @overload + * @param submode + */ + void setMode(Mode_t newMode, Submode_t submode); + /** * Helper function to report a missed reply * @@ -559,27 +618,6 @@ protected: */ void replyToCommand(ReturnValue_t status, uint32_t parameter = 0); - /** - * Set the device handler mode - * - * Sets #timeoutStart with every call. - * - * Sets #transitionTargetMode if necessary so transitional states can be entered from everywhere without breaking the state machine - * (which relies on a correct #transitionTargetMode). - * - * The submode is left unchanged. - * - * - * @param newMode - */ - void setMode(Mode_t newMode); - - /** - * @overload - * @param submode - */ - void setMode(Mode_t newMode, Submode_t submode); - /** * Do the transition to the main modes (MODE_ON, MODE_NORMAL and MODE_RAW). * @@ -628,7 +666,7 @@ protected: * * @return The Rmap action to execute in this step */ - virtual RmapAction_t getRmapAction(); + virtual CommunicationAction_t getRmapAction(); /** * Build the device command to send for raw mode. @@ -655,8 +693,10 @@ protected: * This is a helper method to facilitate inserting entries in the command map. * @param deviceCommand Identifier of the command to add. * @param maxDelayCycles The maximum number of delay cycles the command waits until it times out. - * @param periodic Indicates if the command is periodic (i.e. it is sent by the device repeatedly without request) or not. + * @param periodic Indicates if the reply is periodic (i.e. it is sent by the device repeatedly without request) or not. * Default is aperiodic (0) + * @param hasDifferentReplyId + * @param replyId * @return RETURN_OK when the command was successfully inserted, COMMAND_MAP_ERROR else. */ ReturnValue_t insertInCommandAndReplyMap(DeviceCommandId_t deviceCommand, @@ -769,14 +809,6 @@ protected: */ ReturnValue_t getStateOfSwitches(void); - /** - * set all datapool variables that are update periodically in normal mode invalid - * - * Child classes should provide an implementation which sets all those variables invalid - * which are set periodically during any normal mode. - */ - virtual void setNormalDatapoolEntriesInvalid() = 0; - /** * build a list of sids and pass it to the #hkSwitcher */ @@ -897,7 +929,7 @@ private: /** * State a cookie is in. * - * Used to keep track of the state of the RMAP communication. + * Used to keep track of the state of the communication. */ enum CookieState_t { COOKIE_UNUSED, //!< The Cookie is unused @@ -934,7 +966,7 @@ private: * * Set when setMode() is called. */ - uint32_t timeoutStart; + uint32_t timeoutStart = 0; /** * Delay for the current mode transition, used for time out @@ -976,6 +1008,8 @@ private: * - checks whether commanded mode transitions are required and calls handleCommandedModeTransition() * - does the necessary action for the current mode or calls doChildStateMachine in modes @c MODE_TO_ON and @c MODE_TO_OFF * - actions that happen in transitions (eg setting a timeout) are handled in setMode() + * - Maybe export this into own class to increase modularity of software + * and reduce the massive class size ? */ void doStateMachine(void); @@ -1054,8 +1088,8 @@ private: * - @c RETURN_FAILED IPCStore is NULL * - the return value from the IPCStore if it was not @c RETURN_OK */ - ReturnValue_t getStorageData(store_address_t storageAddress, uint8_t **data, - uint32_t *len); + ReturnValue_t getStorageData(store_address_t storageAddress, + uint8_t ** data, size_t * len); /** * set all switches returned by getSwitches() @@ -1084,7 +1118,7 @@ private: * - @c RETURN_FAILED when cookies could not be changed, eg because the newChannel is not enabled * - @c returnvalues of RMAPChannelIF::isActive() */ - ReturnValue_t switchCookieChannel(object_id_t newChannelId); + //ReturnValue_t switchCookieChannel(object_id_t newChannelId); /** * Handle device handler messages (e.g. commands sent by PUS Service 2)