From caea75b0a8e3ea185606a2b0cab1e0cf4603ae28 Mon Sep 17 00:00:00 2001 From: mmode Date: Mon, 21 Jun 2021 15:04:15 +0200 Subject: [PATCH] release 0.0.1 of fsfw added as a core --- fsfw/.gitignore | 4 + fsfw/.gitmodules | 0 fsfw/FSFWVersion.h | 12 + fsfw/LICENSE | 202 ++ fsfw/NOTICE | 23 + fsfw/README.md | 159 ++ fsfw/action/ActionHelper.cpp | 162 ++ fsfw/action/ActionHelper.h | 125 + fsfw/action/ActionMessage.cpp | 79 + fsfw/action/ActionMessage.h | 36 + fsfw/action/CommandActionHelper.cpp | 127 + fsfw/action/CommandActionHelper.h | 36 + fsfw/action/CommandsActionsIF.h | 37 + fsfw/action/HasActionsIF.h | 63 + fsfw/action/SimpleActionHelper.cpp | 75 + fsfw/action/SimpleActionHelper.h | 30 + fsfw/container/ArrayList.h | 253 ++ fsfw/container/BinaryTree.h | 153 ++ fsfw/container/DynamicFIFO.h | 55 + fsfw/container/FIFO.h | 47 + fsfw/container/FIFOBase.h | 79 + fsfw/container/FIFOBase.tpp | 93 + fsfw/container/FixedArrayList.h | 38 + fsfw/container/FixedMap.h | 230 ++ fsfw/container/FixedOrderedMultimap.h | 206 ++ fsfw/container/FixedOrderedMultimap.tpp | 109 + fsfw/container/HybridIterator.h | 90 + fsfw/container/IndexedRingMemoryArray.h | 700 ++++++ fsfw/container/PlacementFactory.h | 71 + fsfw/container/RingBufferBase.h | 113 + fsfw/container/SharedRingBuffer.cpp | 55 + fsfw/container/SharedRingBuffer.h | 92 + fsfw/container/SimpleRingBuffer.cpp | 131 ++ fsfw/container/SimpleRingBuffer.h | 129 + fsfw/container/SinglyLinkedList.h | 154 ++ fsfw/container/group.h | 15 + fsfw/contrib/sgp4/LICENSE | 4 + fsfw/contrib/sgp4/sgp4unit.cpp | 2090 +++++++++++++++++ fsfw/contrib/sgp4/sgp4unit.h | 117 + fsfw/controller/ControllerBase.cpp | 137 ++ fsfw/controller/ControllerBase.h | 79 + .../coordinates/CoordinateTransformations.cpp | 227 ++ fsfw/coordinates/CoordinateTransformations.h | 34 + fsfw/coordinates/Jgm3Model.h | 180 ++ fsfw/coordinates/Sgp4Propagator.cpp | 228 ++ fsfw/coordinates/Sgp4Propagator.h | 44 + fsfw/datalinklayer/BCFrame.h | 62 + fsfw/datalinklayer/CCSDSReturnValuesIF.h | 56 + fsfw/datalinklayer/Clcw.cpp | 63 + fsfw/datalinklayer/Clcw.h | 66 + fsfw/datalinklayer/ClcwIF.h | 70 + fsfw/datalinklayer/DataLinkLayer.cpp | 139 ++ fsfw/datalinklayer/DataLinkLayer.h | 112 + fsfw/datalinklayer/Farm1StateIF.h | 54 + fsfw/datalinklayer/Farm1StateLockout.cpp | 35 + fsfw/datalinklayer/Farm1StateLockout.h | 59 + fsfw/datalinklayer/Farm1StateOpen.cpp | 49 + fsfw/datalinklayer/Farm1StateOpen.h | 62 + fsfw/datalinklayer/Farm1StateWait.cpp | 43 + fsfw/datalinklayer/Farm1StateWait.h | 58 + fsfw/datalinklayer/MapPacketExtraction.cpp | 154 ++ fsfw/datalinklayer/MapPacketExtraction.h | 78 + fsfw/datalinklayer/MapPacketExtractionIF.h | 47 + fsfw/datalinklayer/TcTransferFrame.cpp | 102 + fsfw/datalinklayer/TcTransferFrame.h | 137 ++ fsfw/datalinklayer/TcTransferFrameLocal.cpp | 49 + fsfw/datalinklayer/TcTransferFrameLocal.h | 49 + .../datalinklayer/VirtualChannelReception.cpp | 121 + fsfw/datalinklayer/VirtualChannelReception.h | 114 + .../datalinklayer/VirtualChannelReceptionIF.h | 57 + fsfw/datapool/ControllerSet.cpp | 14 + fsfw/datapool/ControllerSet.h | 15 + fsfw/datapool/DataPool.cpp | 131 ++ fsfw/datapool/DataPool.h | 135 ++ fsfw/datapool/DataPoolAdmin.cpp | 300 +++ fsfw/datapool/DataPoolAdmin.h | 58 + fsfw/datapool/DataPoolParameterWrapper.cpp | 181 ++ fsfw/datapool/DataPoolParameterWrapper.h | 38 + fsfw/datapool/DataSet.cpp | 150 ++ fsfw/datapool/DataSet.h | 159 ++ fsfw/datapool/DataSetIF.h | 39 + fsfw/datapool/HkSwitchHelper.cpp | 76 + fsfw/datapool/HkSwitchHelper.h | 46 + fsfw/datapool/PIDReader.h | 147 ++ fsfw/datapool/PIDReaderList.h | 27 + fsfw/datapool/PoolEntry.cpp | 87 + fsfw/datapool/PoolEntry.h | 130 + fsfw/datapool/PoolEntryIF.h | 63 + fsfw/datapool/PoolRawAccess.cpp | 187 ++ fsfw/datapool/PoolRawAccess.h | 152 ++ fsfw/datapool/PoolVarList.h | 28 + fsfw/datapool/PoolVariable.h | 295 +++ fsfw/datapool/PoolVariableIF.h | 71 + fsfw/datapool/PoolVector.h | 233 ++ fsfw/defaultcfg/README.md | 6 + fsfw/defaultcfg/fsfwconfig/FSFWConfig.h | 55 + fsfw/defaultcfg/fsfwconfig/OBSWConfig.h | 16 + fsfw/defaultcfg/fsfwconfig/OBSWVersion.h | 9 + .../fsfwconfig/devices/logicalAddresses.cpp | 5 + .../fsfwconfig/devices/logicalAddresses.h | 18 + .../fsfwconfig/devices/powerSwitcherList.cpp | 4 + .../fsfwconfig/devices/powerSwitcherList.h | 12 + .../fsfwconfig/events/subsystemIdRanges.h | 18 + fsfw/defaultcfg/fsfwconfig/fsfwconfig.mk | 15 + .../fsfwconfig/ipc/missionMessageTypes.cpp | 12 + .../fsfwconfig/ipc/missionMessageTypes.h | 21 + .../defaultcfg/fsfwconfig/objects/Factory.cpp | 54 + fsfw/defaultcfg/fsfwconfig/objects/Factory.h | 17 + .../fsfwconfig/objects/systemObjectList.h | 16 + .../PollingSequenceFactory.cpp | 23 + .../pollingsequence/PollingSequenceFactory.h | 32 + .../fsfwconfig/returnvalues/classIds.h | 16 + fsfw/defaultcfg/fsfwconfig/tmtc/apid.h | 18 + fsfw/defaultcfg/fsfwconfig/tmtc/pusIds.h | 23 + .../devicehandlers/AcceptsDeviceResponsesIF.h | 23 + fsfw/devicehandlers/AssemblyBase.cpp | 274 +++ fsfw/devicehandlers/AssemblyBase.h | 132 ++ fsfw/devicehandlers/ChildHandlerBase.cpp | 45 + fsfw/devicehandlers/ChildHandlerBase.h | 26 + fsfw/devicehandlers/ChildHandlerFDIR.cpp | 10 + fsfw/devicehandlers/ChildHandlerFDIR.h | 20 + fsfw/devicehandlers/CookieIF.h | 34 + fsfw/devicehandlers/DeviceCommunicationIF.h | 131 ++ fsfw/devicehandlers/DeviceHandlerBase.cpp | 1351 +++++++++++ fsfw/devicehandlers/DeviceHandlerBase.h | 1194 ++++++++++ .../DeviceHandlerFailureIsolation.cpp | 256 ++ .../DeviceHandlerFailureIsolation.h | 57 + fsfw/devicehandlers/DeviceHandlerIF.h | 153 ++ fsfw/devicehandlers/DeviceHandlerMessage.cpp | 101 + fsfw/devicehandlers/DeviceHandlerMessage.h | 91 + .../DeviceTmReportingWrapper.cpp | 46 + .../devicehandlers/DeviceTmReportingWrapper.h | 27 + fsfw/devicehandlers/HealthDevice.cpp | 59 + fsfw/devicehandlers/HealthDevice.h | 40 + fsfw/events/Event.cpp | 14 + fsfw/events/Event.h | 44 + fsfw/events/EventManager.cpp | 166 ++ fsfw/events/EventManager.h | 67 + fsfw/events/EventManagerIF.h | 52 + fsfw/events/EventMessage.cpp | 114 + fsfw/events/EventMessage.h | 52 + fsfw/events/EventReportingProxyIF.h | 19 + .../eventmatching/EventIdRangeMatcher.cpp | 12 + .../eventmatching/EventIdRangeMatcher.h | 13 + fsfw/events/eventmatching/EventMatchTree.cpp | 149 ++ fsfw/events/eventmatching/EventMatchTree.h | 36 + .../eventmatching/EventRangeMatcherBase.h | 29 + .../eventmatching/ReporterRangeMatcher.cpp | 13 + .../eventmatching/ReporterRangeMatcher.h | 13 + .../eventmatching/SeverityRangeMatcher.cpp | 14 + .../eventmatching/SeverityRangeMatcher.h | 13 + fsfw/events/eventmatching/eventmatching.h | 10 + fsfw/events/fwSubsystemIdRanges.h | 30 + fsfw/fdir/ConfirmsFailuresIF.h | 19 + fsfw/fdir/EventCorrelation.cpp | 44 + fsfw/fdir/EventCorrelation.h | 23 + fsfw/fdir/FailureIsolationBase.cpp | 164 ++ fsfw/fdir/FailureIsolationBase.h | 56 + fsfw/fdir/FaultCounter.cpp | 86 + fsfw/fdir/FaultCounter.h | 38 + fsfw/fsfw.mk | 72 + fsfw/globalfunctions/AsciiConverter.cpp | 230 ++ fsfw/globalfunctions/AsciiConverter.h | 39 + fsfw/globalfunctions/CRC.cpp | 139 ++ fsfw/globalfunctions/CRC.h | 16 + fsfw/globalfunctions/DleEncoder.cpp | 124 + fsfw/globalfunctions/DleEncoder.h | 79 + .../PeriodicOperationDivider.cpp | 34 + .../PeriodicOperationDivider.h | 55 + fsfw/globalfunctions/Type.cpp | 182 ++ fsfw/globalfunctions/Type.h | 91 + fsfw/globalfunctions/arrayprinter.cpp | 61 + fsfw/globalfunctions/arrayprinter.h | 20 + fsfw/globalfunctions/constants.h | 15 + fsfw/globalfunctions/matching/BinaryMatcher.h | 41 + .../globalfunctions/matching/DecimalMatcher.h | 50 + fsfw/globalfunctions/matching/MatchTree.h | 216 ++ fsfw/globalfunctions/matching/MatcherIF.h | 16 + fsfw/globalfunctions/matching/RangeMatcher.h | 70 + .../matching/SerializeableMatcherIF.h | 13 + fsfw/globalfunctions/math/MatrixOperations.h | 115 + .../math/QuaternionOperations.cpp | 156 ++ .../math/QuaternionOperations.h | 81 + fsfw/globalfunctions/math/VectorOperations.h | 103 + fsfw/globalfunctions/sign.h | 8 + fsfw/globalfunctions/timevalOperations.cpp | 99 + fsfw/globalfunctions/timevalOperations.h | 47 + fsfw/health/HasHealthIF.h | 51 + fsfw/health/HealthHelper.cpp | 105 + fsfw/health/HealthHelper.h | 131 ++ fsfw/health/HealthMessage.cpp | 30 + fsfw/health/HealthMessage.h | 36 + fsfw/health/HealthTable.cpp | 94 + fsfw/health/HealthTable.h | 47 + fsfw/health/HealthTableIF.h | 24 + fsfw/health/ManagesHealthIF.h | 53 + fsfw/internalError/InternalErrorReporter.cpp | 126 + fsfw/internalError/InternalErrorReporter.h | 50 + fsfw/internalError/InternalErrorReporterIF.h | 27 + fsfw/ipc/CommandMessage.cpp | 111 + fsfw/ipc/CommandMessage.h | 131 ++ fsfw/ipc/CommandMessageCleaner.cpp | 45 + fsfw/ipc/CommandMessageCleaner.h | 16 + fsfw/ipc/CommandMessageIF.h | 73 + fsfw/ipc/FwMessageTypes.h | 21 + fsfw/ipc/MessageQueueIF.h | 173 ++ fsfw/ipc/MessageQueueMessage.cpp | 84 + fsfw/ipc/MessageQueueMessage.h | 149 ++ fsfw/ipc/MessageQueueMessageIF.h | 80 + fsfw/ipc/MessageQueueSenderIF.h | 26 + fsfw/ipc/MutexFactory.h | 34 + fsfw/ipc/MutexHelper.h | 30 + fsfw/ipc/MutexIF.h | 89 + fsfw/ipc/QueueFactory.h | 35 + fsfw/ipc/messageQueueDefinitions.h | 18 + fsfw/logo/FSFW_Logo_V3.png | Bin 0 -> 137423 bytes fsfw/logo/FSFW_Logo_V3.svg | 711 ++++++ fsfw/logo/FSFW_Logo_V3_bw.png | Bin 0 -> 13369 bytes fsfw/memory/AcceptsMemoryMessagesIF.h | 14 + fsfw/memory/HasMemoryIF.h | 47 + fsfw/memory/MemoryHelper.cpp | 194 ++ fsfw/memory/MemoryHelper.h | 49 + fsfw/memory/MemoryMessage.cpp | 107 + fsfw/memory/MemoryMessage.h | 48 + fsfw/modes/HasModesIF.h | 51 + fsfw/modes/ModeHelper.cpp | 137 ++ fsfw/modes/ModeHelper.h | 53 + fsfw/modes/ModeMessage.cpp | 31 + fsfw/modes/ModeMessage.h | 34 + fsfw/monitoring/AbsLimitMonitor.h | 73 + fsfw/monitoring/HasMonitorsIF.h | 30 + fsfw/monitoring/LimitMonitor.h | 94 + fsfw/monitoring/LimitViolationReporter.cpp | 60 + fsfw/monitoring/LimitViolationReporter.h | 31 + fsfw/monitoring/MonitorBase.h | 62 + fsfw/monitoring/MonitorReporter.h | 178 ++ fsfw/monitoring/MonitoringIF.h | 67 + fsfw/monitoring/MonitoringMessage.cpp | 38 + fsfw/monitoring/MonitoringMessage.h | 21 + fsfw/monitoring/MonitoringMessageContent.h | 77 + fsfw/monitoring/ReceivesMonitoringReportsIF.h | 15 + fsfw/monitoring/TriplexMonitor.h | 155 ++ fsfw/monitoring/TwoValueLimitMonitor.h | 44 + fsfw/objectmanager/ObjectManager.cpp | 107 + fsfw/objectmanager/ObjectManager.h | 62 + fsfw/objectmanager/ObjectManagerIF.h | 96 + fsfw/objectmanager/SystemObject.cpp | 37 + fsfw/objectmanager/SystemObject.h | 58 + fsfw/objectmanager/SystemObjectIF.h | 62 + fsfw/objectmanager/frameworkObjects.h | 31 + fsfw/osal/Endiness.h | 33 + fsfw/osal/FreeRTOS/BinSemaphUsingTask.cpp | 95 + fsfw/osal/FreeRTOS/BinSemaphUsingTask.h | 76 + fsfw/osal/FreeRTOS/BinarySemaphore.cpp | 108 + fsfw/osal/FreeRTOS/BinarySemaphore.h | 107 + fsfw/osal/FreeRTOS/Clock.cpp | 196 ++ .../osal/FreeRTOS/CountingSemaphUsingTask.cpp | 114 + fsfw/osal/FreeRTOS/CountingSemaphUsingTask.h | 102 + fsfw/osal/FreeRTOS/CountingSemaphore.cpp | 43 + fsfw/osal/FreeRTOS/CountingSemaphore.h | 34 + fsfw/osal/FreeRTOS/FixedTimeslotTask.cpp | 162 ++ fsfw/osal/FreeRTOS/FixedTimeslotTask.h | 101 + fsfw/osal/FreeRTOS/FreeRTOSTaskIF.h | 13 + fsfw/osal/FreeRTOS/MessageQueue.cpp | 156 ++ fsfw/osal/FreeRTOS/MessageQueue.h | 150 ++ fsfw/osal/FreeRTOS/Mutex.cpp | 51 + fsfw/osal/FreeRTOS/Mutex.h | 29 + fsfw/osal/FreeRTOS/MutexFactory.cpp | 28 + fsfw/osal/FreeRTOS/PeriodicTask.cpp | 139 ++ fsfw/osal/FreeRTOS/PeriodicTask.h | 126 + fsfw/osal/FreeRTOS/QueueFactory.cpp | 37 + fsfw/osal/FreeRTOS/README.md | 14 + fsfw/osal/FreeRTOS/SemaphoreFactory.cpp | 59 + fsfw/osal/FreeRTOS/TaskFactory.cpp | 55 + fsfw/osal/FreeRTOS/TaskManagement.cpp | 24 + fsfw/osal/FreeRTOS/TaskManagement.h | 64 + fsfw/osal/FreeRTOS/Timekeeper.cpp | 41 + fsfw/osal/FreeRTOS/Timekeeper.h | 40 + fsfw/osal/InternalErrorCodes.h | 39 + fsfw/osal/host/Clock.cpp | 227 ++ fsfw/osal/host/FixedTimeslotTask.cpp | 197 ++ fsfw/osal/host/FixedTimeslotTask.h | 130 + fsfw/osal/host/MessageQueue.cpp | 159 ++ fsfw/osal/host/MessageQueue.h | 231 ++ fsfw/osal/host/Mutex.cpp | 39 + fsfw/osal/host/Mutex.h | 29 + fsfw/osal/host/MutexFactory.cpp | 28 + fsfw/osal/host/PeriodicTask.cpp | 176 ++ fsfw/osal/host/PeriodicTask.h | 123 + fsfw/osal/host/QueueFactory.cpp | 46 + fsfw/osal/host/QueueMapManager.cpp | 52 + fsfw/osal/host/QueueMapManager.h | 47 + fsfw/osal/host/SemaphoreFactory.cpp | 39 + fsfw/osal/host/TaskFactory.cpp | 51 + fsfw/osal/linux/BinarySemaphore.cpp | 149 ++ fsfw/osal/linux/BinarySemaphore.h | 81 + fsfw/osal/linux/Clock.cpp | 221 ++ fsfw/osal/linux/CountingSemaphore.cpp | 54 + fsfw/osal/linux/CountingSemaphore.h | 37 + fsfw/osal/linux/FixedTimeslotTask.cpp | 97 + fsfw/osal/linux/FixedTimeslotTask.h | 77 + fsfw/osal/linux/InternalErrorCodes.cpp | 14 + fsfw/osal/linux/MessageQueue.cpp | 370 +++ fsfw/osal/linux/MessageQueue.h | 187 ++ fsfw/osal/linux/Mutex.cpp | 111 + fsfw/osal/linux/Mutex.h | 19 + fsfw/osal/linux/MutexFactory.cpp | 23 + fsfw/osal/linux/PeriodicPosixTask.cpp | 86 + fsfw/osal/linux/PeriodicPosixTask.h | 90 + fsfw/osal/linux/PosixThread.cpp | 215 ++ fsfw/osal/linux/PosixThread.h | 77 + fsfw/osal/linux/QueueFactory.cpp | 44 + fsfw/osal/linux/SemaphoreFactory.cpp | 33 + fsfw/osal/linux/TaskFactory.cpp | 42 + fsfw/osal/linux/TcUnixUdpPollingTask.cpp | 138 ++ fsfw/osal/linux/TcUnixUdpPollingTask.h | 67 + fsfw/osal/linux/Timer.cpp | 42 + fsfw/osal/linux/Timer.h | 45 + fsfw/osal/linux/TmTcUnixUdpBridge.cpp | 178 ++ fsfw/osal/linux/TmTcUnixUdpBridge.h | 51 + fsfw/osal/rtems/Clock.cpp | 192 ++ fsfw/osal/rtems/CpuUsage.cpp | 183 ++ fsfw/osal/rtems/CpuUsage.h | 53 + fsfw/osal/rtems/InitTask.cpp | 11 + fsfw/osal/rtems/InitTask.h | 19 + fsfw/osal/rtems/InternalErrorCodes.cpp | 60 + fsfw/osal/rtems/MessageQueue.cpp | 150 ++ fsfw/osal/rtems/MessageQueue.h | 172 ++ fsfw/osal/rtems/MultiObjectTask.cpp | 88 + fsfw/osal/rtems/MultiObjectTask.h | 107 + fsfw/osal/rtems/Mutex.cpp | 79 + fsfw/osal/rtems/Mutex.h | 18 + fsfw/osal/rtems/MutexFactory.cpp | 23 + fsfw/osal/rtems/PollingTask.cpp | 123 + fsfw/osal/rtems/PollingTask.h | 85 + fsfw/osal/rtems/QueueFactory.cpp | 60 + fsfw/osal/rtems/RtemsBasic.cpp | 70 + fsfw/osal/rtems/RtemsBasic.h | 25 + fsfw/osal/rtems/TaskBase.cpp | 82 + fsfw/osal/rtems/TaskBase.h | 47 + fsfw/osal/rtems/TaskFactory.cpp | 41 + fsfw/osal/windows/TcWinUdpPollingTask.cpp | 148 ++ fsfw/osal/windows/TcWinUdpPollingTask.h | 67 + fsfw/osal/windows/TmTcWinUdpBridge.cpp | 176 ++ fsfw/osal/windows/TmTcWinUdpBridge.h | 49 + fsfw/parameters/HasParametersIF.h | 67 + fsfw/parameters/ParameterHelper.cpp | 132 ++ fsfw/parameters/ParameterHelper.h | 36 + fsfw/parameters/ParameterMessage.cpp | 48 + fsfw/parameters/ParameterMessage.h | 29 + fsfw/parameters/ParameterWrapper.cpp | 281 +++ fsfw/parameters/ParameterWrapper.h | 162 ++ fsfw/parameters/ReceivesParameterMessagesIF.h | 19 + fsfw/power/Fuse.cpp | 260 ++ fsfw/power/Fuse.h | 105 + fsfw/power/PowerComponent.cpp | 86 + fsfw/power/PowerComponent.h | 48 + fsfw/power/PowerComponentIF.h | 24 + fsfw/power/PowerSensor.cpp | 128 + fsfw/power/PowerSensor.h | 71 + fsfw/power/PowerSwitchIF.h | 75 + fsfw/power/PowerSwitcher.cpp | 127 + fsfw/power/PowerSwitcher.h | 45 + fsfw/pus/CService200ModeCommanding.cpp | 129 + fsfw/pus/CService200ModeCommanding.h | 85 + fsfw/pus/Service17Test.cpp | 41 + fsfw/pus/Service17Test.h | 44 + fsfw/pus/Service1TelecommandVerification.cpp | 100 + fsfw/pus/Service1TelecommandVerification.h | 94 + fsfw/pus/Service2DeviceAccess.cpp | 167 ++ fsfw/pus/Service2DeviceAccess.h | 92 + fsfw/pus/Service5EventReporting.cpp | 91 + fsfw/pus/Service5EventReporting.h | 86 + fsfw/pus/Service8FunctionManagement.cpp | 142 ++ fsfw/pus/Service8FunctionManagement.h | 67 + fsfw/pus/Service9TimeManagement.cpp | 58 + fsfw/pus/Service9TimeManagement.h | 41 + fsfw/pus/servicepackets/Service1Packets.h | 166 ++ fsfw/pus/servicepackets/Service200Packets.h | 63 + fsfw/pus/servicepackets/Service2Packets.h | 76 + fsfw/pus/servicepackets/Service5Packets.h | 76 + fsfw/pus/servicepackets/Service8Packets.h | 121 + fsfw/pus/servicepackets/Service9Packets.h | 32 + fsfw/returnvalues/FwClassIds.h | 73 + fsfw/returnvalues/HasReturnvaluesIF.h | 31 + fsfw/rmap/RMAP.cpp | 89 + fsfw/rmap/RMAP.h | 227 ++ fsfw/rmap/RMAPChannelIF.h | 115 + fsfw/rmap/RMAPCookie.cpp | 124 + fsfw/rmap/RMAPCookie.h | 58 + fsfw/rmap/RmapDeviceCommunicationIF.cpp | 47 + fsfw/rmap/RmapDeviceCommunicationIF.h | 80 + fsfw/rmap/rmapStructs.h | 98 + fsfw/serialize/EndianConverter.h | 124 + fsfw/serialize/SerialArrayListAdapter.h | 86 + fsfw/serialize/SerialBufferAdapter.cpp | 129 + fsfw/serialize/SerialBufferAdapter.h | 78 + fsfw/serialize/SerialFixedArrayListAdapter.h | 57 + fsfw/serialize/SerialLinkedListAdapter.h | 128 + fsfw/serialize/SerializeAdapter.h | 193 ++ fsfw/serialize/SerializeElement.h | 63 + fsfw/serialize/SerializeIF.h | 87 + .../ServiceInterfaceBuffer.cpp | 217 ++ .../serviceinterface/ServiceInterfaceBuffer.h | 145 ++ .../ServiceInterfaceStream.cpp | 32 + .../serviceinterface/ServiceInterfaceStream.h | 58 + fsfw/storagemanager/ConstStorageAccessor.cpp | 88 + fsfw/storagemanager/ConstStorageAccessor.h | 116 + fsfw/storagemanager/LocalPool.h | 190 ++ fsfw/storagemanager/LocalPool.tpp | 305 +++ fsfw/storagemanager/PoolManager.h | 56 + fsfw/storagemanager/PoolManager.tpp | 56 + fsfw/storagemanager/StorageAccessor.cpp | 67 + fsfw/storagemanager/StorageAccessor.h | 45 + fsfw/storagemanager/StorageManagerIF.h | 169 ++ fsfw/storagemanager/storeAddress.h | 55 + fsfw/subsystem/Subsystem.cpp | 663 ++++++ fsfw/subsystem/Subsystem.h | 164 ++ fsfw/subsystem/SubsystemBase.cpp | 356 +++ fsfw/subsystem/SubsystemBase.h | 125 + fsfw/subsystem/modes/HasModeSequenceIF.h | 20 + fsfw/subsystem/modes/ModeDefinitions.h | 152 ++ fsfw/subsystem/modes/ModeSequenceMessage.cpp | 89 + fsfw/subsystem/modes/ModeSequenceMessage.h | 48 + fsfw/subsystem/modes/ModeStore.cpp | 126 + fsfw/subsystem/modes/ModeStore.h | 45 + fsfw/subsystem/modes/ModeStoreIF.h | 37 + fsfw/tasks/ExecutableObjectIF.h | 53 + fsfw/tasks/FixedSequenceSlot.cpp | 17 + fsfw/tasks/FixedSequenceSlot.h | 60 + fsfw/tasks/FixedSlotSequence.cpp | 162 ++ fsfw/tasks/FixedSlotSequence.h | 181 ++ fsfw/tasks/FixedTimeslotTaskIF.h | 19 + fsfw/tasks/PeriodicTaskIF.h | 49 + fsfw/tasks/SemaphoreFactory.h | 50 + fsfw/tasks/SemaphoreIF.h | 68 + fsfw/tasks/TaskFactory.h | 72 + fsfw/tasks/Typedef.h | 10 + fsfw/tcdistribution/CCSDSDistributor.cpp | 86 + fsfw/tcdistribution/CCSDSDistributor.h | 71 + fsfw/tcdistribution/CCSDSDistributorIF.h | 44 + fsfw/tcdistribution/PUSDistributor.cpp | 113 + fsfw/tcdistribution/PUSDistributor.h | 79 + fsfw/tcdistribution/PUSDistributorIF.h | 27 + fsfw/tcdistribution/TcDistributor.cpp | 55 + fsfw/tcdistribution/TcDistributor.h | 122 + fsfw/tcdistribution/TcPacketCheck.cpp | 39 + fsfw/tcdistribution/TcPacketCheck.h | 60 + fsfw/thermal/AbstractTemperatureSensor.cpp | 70 + fsfw/thermal/AbstractTemperatureSensor.h | 54 + fsfw/thermal/CoreComponent.cpp | 257 ++ fsfw/thermal/CoreComponent.h | 95 + fsfw/thermal/Heater.cpp | 350 +++ fsfw/thermal/Heater.h | 90 + fsfw/thermal/RedundantHeater.cpp | 40 + fsfw/thermal/RedundantHeater.h | 51 + fsfw/thermal/TemperatureSensor.h | 174 ++ fsfw/thermal/ThermalComponent.cpp | 170 ++ fsfw/thermal/ThermalComponent.h | 53 + fsfw/thermal/ThermalComponentIF.h | 114 + fsfw/thermal/ThermalModule.cpp | 288 +++ fsfw/thermal/ThermalModule.h | 92 + fsfw/thermal/ThermalModuleIF.h | 45 + fsfw/thermal/ThermalMonitor.cpp | 68 + fsfw/thermal/ThermalMonitor.h | 23 + fsfw/thermal/tcsDefinitions.h | 8 + fsfw/timemanager/CCSDSTime.cpp | 606 +++++ fsfw/timemanager/CCSDSTime.h | 233 ++ fsfw/timemanager/Clock.h | 154 ++ fsfw/timemanager/Countdown.cpp | 45 + fsfw/timemanager/Countdown.h | 31 + fsfw/timemanager/ReceivesTimeInfoIF.h | 31 + fsfw/timemanager/Stopwatch.cpp | 60 + fsfw/timemanager/Stopwatch.h | 70 + fsfw/timemanager/TimeMessage.cpp | 37 + fsfw/timemanager/TimeMessage.h | 56 + fsfw/timemanager/TimeStamper.cpp | 23 + fsfw/timemanager/TimeStamper.h | 36 + fsfw/timemanager/TimeStamperIF.h | 28 + fsfw/tmstorage/TmStoreBackendIF.h | 76 + fsfw/tmstorage/TmStoreFrontendIF.h | 55 + fsfw/tmstorage/TmStoreMessage.cpp | 165 ++ fsfw/tmstorage/TmStoreMessage.h | 63 + fsfw/tmstorage/TmStorePackets.h | 300 +++ fsfw/tmtcpacket/SpacePacket.cpp | 27 + fsfw/tmtcpacket/SpacePacket.h | 70 + fsfw/tmtcpacket/SpacePacketBase.cpp | 104 + fsfw/tmtcpacket/SpacePacketBase.h | 178 ++ fsfw/tmtcpacket/ccsds_header.h | 15 + fsfw/tmtcpacket/packetmatcher/ApidMatcher.h | 40 + .../packetmatcher/PacketMatchTree.cpp | 195 ++ .../packetmatcher/PacketMatchTree.h | 36 + .../tmtcpacket/packetmatcher/ServiceMatcher.h | 39 + .../packetmatcher/SubserviceMatcher.h | 40 + .../pus/PacketTimestampInterpreterIF.h | 17 + fsfw/tmtcpacket/pus/TcPacketBase.cpp | 86 + fsfw/tmtcpacket/pus/TcPacketBase.h | 187 ++ fsfw/tmtcpacket/pus/TcPacketStored.cpp | 117 + fsfw/tmtcpacket/pus/TcPacketStored.h | 117 + fsfw/tmtcpacket/pus/TmPacketBase.cpp | 117 + fsfw/tmtcpacket/pus/TmPacketBase.h | 196 ++ fsfw/tmtcpacket/pus/TmPacketMinimal.cpp | 45 + fsfw/tmtcpacket/pus/TmPacketMinimal.h | 84 + fsfw/tmtcpacket/pus/TmPacketStored.cpp | 145 ++ fsfw/tmtcpacket/pus/TmPacketStored.h | 108 + fsfw/tmtcservices/AcceptsTelecommandsIF.h | 43 + fsfw/tmtcservices/AcceptsTelemetryIF.h | 26 + fsfw/tmtcservices/AcceptsVerifyMessageIF.h | 15 + fsfw/tmtcservices/CommandingServiceBase.cpp | 427 ++++ fsfw/tmtcservices/CommandingServiceBase.h | 365 +++ fsfw/tmtcservices/PusServiceBase.cpp | 121 + fsfw/tmtcservices/PusServiceBase.h | 159 ++ fsfw/tmtcservices/PusVerificationReport.cpp | 138 ++ fsfw/tmtcservices/PusVerificationReport.h | 81 + fsfw/tmtcservices/ServiceTypes.h | 32 + fsfw/tmtcservices/SourceSequenceCounter.h | 30 + fsfw/tmtcservices/TmTcBridge.cpp | 237 ++ fsfw/tmtcservices/TmTcBridge.h | 162 ++ fsfw/tmtcservices/TmTcMessage.cpp | 29 + fsfw/tmtcservices/TmTcMessage.h | 50 + fsfw/tmtcservices/VerificationCodes.h | 28 + fsfw/tmtcservices/VerificationReporter.cpp | 106 + fsfw/tmtcservices/VerificationReporter.h | 50 + fsfw/unittest/README.md | 59 + fsfw/unittest/core/CatchDefinitions.cpp | 11 + fsfw/unittest/core/CatchDefinitions.h | 21 + fsfw/unittest/core/CatchRunner.cpp | 31 + fsfw/unittest/core/CatchSetup.cpp | 42 + fsfw/unittest/core/core.mk | 3 + fsfw/unittest/core/printChar.cpp | 10 + fsfw/unittest/core/printChar.h | 8 + fsfw/unittest/internal/InternalUnitTester.cpp | 27 + fsfw/unittest/internal/InternalUnitTester.h | 29 + fsfw/unittest/internal/UnittDefinitions.cpp | 7 + fsfw/unittest/internal/UnittDefinitions.h | 33 + fsfw/unittest/internal/internal.mk | 3 + fsfw/unittest/internal/osal/IntTestMq.cpp | 52 + fsfw/unittest/internal/osal/IntTestMq.h | 9 + fsfw/unittest/internal/osal/IntTestMutex.cpp | 42 + fsfw/unittest/internal/osal/IntTestMutex.h | 10 + .../internal/osal/IntTestSemaphore.cpp | 160 ++ .../unittest/internal/osal/IntTestSemaphore.h | 15 + .../serialize/IntTestSerialization.cpp | 230 ++ .../internal/serialize/IntTestSerialization.h | 15 + fsfw/unittest/lcov.sh | 3 + fsfw/unittest/testcfg/FSFWConfig.h | 46 + fsfw/unittest/testcfg/Makefile-FSFW-Tests | 415 ++++ fsfw/unittest/testcfg/TestsConfig.h | 8 + .../testcfg/cdatapool/dataPoolInit.cpp | 5 + .../unittest/testcfg/cdatapool/dataPoolInit.h | 17 + .../testcfg/devices/logicalAddresses.cpp | 5 + .../testcfg/devices/logicalAddresses.h | 15 + .../testcfg/devices/powerSwitcherList.cpp | 4 + .../testcfg/devices/powerSwitcherList.h | 12 + .../testcfg/events/subsystemIdRanges.h | 18 + .../testcfg/ipc/MissionMessageTypes.cpp | 12 + .../testcfg/ipc/MissionMessageTypes.h | 22 + fsfw/unittest/testcfg/objects/Factory.cpp | 34 + fsfw/unittest/testcfg/objects/Factory.h | 16 + .../testcfg/objects/systemObjectList.h | 16 + .../PollingSequenceFactory.cpp | 23 + .../pollingsequence/PollingSequenceFactory.h | 32 + fsfw/unittest/testcfg/returnvalues/classIds.h | 16 + fsfw/unittest/testcfg/testcfg.mk | 15 + fsfw/unittest/testcfg/tmtc/apid.h | 18 + fsfw/unittest/testcfg/tmtc/pusIds.h | 25 + .../tests/action/TestActionHelper.cpp | 106 + fsfw/unittest/tests/action/TestActionHelper.h | 131 ++ .../tests/container/RingBufferTest.cpp | 327 +++ .../tests/container/TestArrayList.cpp | 90 + .../tests/container/TestDynamicFifo.cpp | 149 ++ fsfw/unittest/tests/container/TestFifo.cpp | 138 ++ .../tests/container/TestFixedArrayList.cpp | 42 + .../unittest/tests/container/TestFixedMap.cpp | 172 ++ .../container/TestFixedOrderedMultimap.cpp | 203 ++ .../tests/container/TestPlacementFactory.cpp | 45 + fsfw/unittest/tests/osal/TestMessageQueue.cpp | 40 + fsfw/unittest/tests/osal/TestSemaphore.cpp | 46 + .../serialize/TestSerialBufferAdapter.cpp | 143 ++ .../serialize/TestSerialLinkedPacket.cpp | 73 + .../tests/serialize/TestSerialLinkedPacket.h | 61 + .../tests/serialize/TestSerialization.cpp | 129 + .../tests/storagemanager/TestNewAccessor.cpp | 161 ++ .../tests/storagemanager/TestPool.cpp | 296 +++ fsfw/unittest/tests/tests.mk | 8 + fsfw/unittest/testtemplate/TestTemplate.cpp | 31 + fsfw/unittest/unlockRealtime.sh | 34 + 587 files changed, 55346 insertions(+) create mode 100644 fsfw/.gitignore create mode 100644 fsfw/.gitmodules create mode 100644 fsfw/FSFWVersion.h create mode 100644 fsfw/LICENSE create mode 100644 fsfw/NOTICE create mode 100644 fsfw/README.md create mode 100644 fsfw/action/ActionHelper.cpp create mode 100644 fsfw/action/ActionHelper.h create mode 100644 fsfw/action/ActionMessage.cpp create mode 100644 fsfw/action/ActionMessage.h create mode 100644 fsfw/action/CommandActionHelper.cpp create mode 100644 fsfw/action/CommandActionHelper.h create mode 100644 fsfw/action/CommandsActionsIF.h create mode 100644 fsfw/action/HasActionsIF.h create mode 100644 fsfw/action/SimpleActionHelper.cpp create mode 100644 fsfw/action/SimpleActionHelper.h create mode 100644 fsfw/container/ArrayList.h create mode 100644 fsfw/container/BinaryTree.h create mode 100644 fsfw/container/DynamicFIFO.h create mode 100644 fsfw/container/FIFO.h create mode 100644 fsfw/container/FIFOBase.h create mode 100644 fsfw/container/FIFOBase.tpp create mode 100644 fsfw/container/FixedArrayList.h create mode 100644 fsfw/container/FixedMap.h create mode 100644 fsfw/container/FixedOrderedMultimap.h create mode 100644 fsfw/container/FixedOrderedMultimap.tpp create mode 100644 fsfw/container/HybridIterator.h create mode 100644 fsfw/container/IndexedRingMemoryArray.h create mode 100644 fsfw/container/PlacementFactory.h create mode 100644 fsfw/container/RingBufferBase.h create mode 100644 fsfw/container/SharedRingBuffer.cpp create mode 100644 fsfw/container/SharedRingBuffer.h create mode 100644 fsfw/container/SimpleRingBuffer.cpp create mode 100644 fsfw/container/SimpleRingBuffer.h create mode 100644 fsfw/container/SinglyLinkedList.h create mode 100644 fsfw/container/group.h create mode 100644 fsfw/contrib/sgp4/LICENSE create mode 100644 fsfw/contrib/sgp4/sgp4unit.cpp create mode 100644 fsfw/contrib/sgp4/sgp4unit.h create mode 100644 fsfw/controller/ControllerBase.cpp create mode 100644 fsfw/controller/ControllerBase.h create mode 100644 fsfw/coordinates/CoordinateTransformations.cpp create mode 100644 fsfw/coordinates/CoordinateTransformations.h create mode 100644 fsfw/coordinates/Jgm3Model.h create mode 100644 fsfw/coordinates/Sgp4Propagator.cpp create mode 100644 fsfw/coordinates/Sgp4Propagator.h create mode 100644 fsfw/datalinklayer/BCFrame.h create mode 100644 fsfw/datalinklayer/CCSDSReturnValuesIF.h create mode 100644 fsfw/datalinklayer/Clcw.cpp create mode 100644 fsfw/datalinklayer/Clcw.h create mode 100644 fsfw/datalinklayer/ClcwIF.h create mode 100644 fsfw/datalinklayer/DataLinkLayer.cpp create mode 100644 fsfw/datalinklayer/DataLinkLayer.h create mode 100644 fsfw/datalinklayer/Farm1StateIF.h create mode 100644 fsfw/datalinklayer/Farm1StateLockout.cpp create mode 100644 fsfw/datalinklayer/Farm1StateLockout.h create mode 100644 fsfw/datalinklayer/Farm1StateOpen.cpp create mode 100644 fsfw/datalinklayer/Farm1StateOpen.h create mode 100644 fsfw/datalinklayer/Farm1StateWait.cpp create mode 100644 fsfw/datalinklayer/Farm1StateWait.h create mode 100644 fsfw/datalinklayer/MapPacketExtraction.cpp create mode 100644 fsfw/datalinklayer/MapPacketExtraction.h create mode 100644 fsfw/datalinklayer/MapPacketExtractionIF.h create mode 100644 fsfw/datalinklayer/TcTransferFrame.cpp create mode 100644 fsfw/datalinklayer/TcTransferFrame.h create mode 100644 fsfw/datalinklayer/TcTransferFrameLocal.cpp create mode 100644 fsfw/datalinklayer/TcTransferFrameLocal.h create mode 100644 fsfw/datalinklayer/VirtualChannelReception.cpp create mode 100644 fsfw/datalinklayer/VirtualChannelReception.h create mode 100644 fsfw/datalinklayer/VirtualChannelReceptionIF.h create mode 100644 fsfw/datapool/ControllerSet.cpp create mode 100644 fsfw/datapool/ControllerSet.h create mode 100644 fsfw/datapool/DataPool.cpp create mode 100644 fsfw/datapool/DataPool.h create mode 100644 fsfw/datapool/DataPoolAdmin.cpp create mode 100644 fsfw/datapool/DataPoolAdmin.h create mode 100644 fsfw/datapool/DataPoolParameterWrapper.cpp create mode 100644 fsfw/datapool/DataPoolParameterWrapper.h create mode 100644 fsfw/datapool/DataSet.cpp create mode 100644 fsfw/datapool/DataSet.h create mode 100644 fsfw/datapool/DataSetIF.h create mode 100644 fsfw/datapool/HkSwitchHelper.cpp create mode 100644 fsfw/datapool/HkSwitchHelper.h create mode 100644 fsfw/datapool/PIDReader.h create mode 100644 fsfw/datapool/PIDReaderList.h create mode 100644 fsfw/datapool/PoolEntry.cpp create mode 100644 fsfw/datapool/PoolEntry.h create mode 100644 fsfw/datapool/PoolEntryIF.h create mode 100644 fsfw/datapool/PoolRawAccess.cpp create mode 100644 fsfw/datapool/PoolRawAccess.h create mode 100644 fsfw/datapool/PoolVarList.h create mode 100644 fsfw/datapool/PoolVariable.h create mode 100644 fsfw/datapool/PoolVariableIF.h create mode 100644 fsfw/datapool/PoolVector.h create mode 100644 fsfw/defaultcfg/README.md create mode 100644 fsfw/defaultcfg/fsfwconfig/FSFWConfig.h create mode 100644 fsfw/defaultcfg/fsfwconfig/OBSWConfig.h create mode 100644 fsfw/defaultcfg/fsfwconfig/OBSWVersion.h create mode 100644 fsfw/defaultcfg/fsfwconfig/devices/logicalAddresses.cpp create mode 100644 fsfw/defaultcfg/fsfwconfig/devices/logicalAddresses.h create mode 100644 fsfw/defaultcfg/fsfwconfig/devices/powerSwitcherList.cpp create mode 100644 fsfw/defaultcfg/fsfwconfig/devices/powerSwitcherList.h create mode 100644 fsfw/defaultcfg/fsfwconfig/events/subsystemIdRanges.h create mode 100644 fsfw/defaultcfg/fsfwconfig/fsfwconfig.mk create mode 100644 fsfw/defaultcfg/fsfwconfig/ipc/missionMessageTypes.cpp create mode 100644 fsfw/defaultcfg/fsfwconfig/ipc/missionMessageTypes.h create mode 100644 fsfw/defaultcfg/fsfwconfig/objects/Factory.cpp create mode 100644 fsfw/defaultcfg/fsfwconfig/objects/Factory.h create mode 100644 fsfw/defaultcfg/fsfwconfig/objects/systemObjectList.h create mode 100644 fsfw/defaultcfg/fsfwconfig/pollingsequence/PollingSequenceFactory.cpp create mode 100644 fsfw/defaultcfg/fsfwconfig/pollingsequence/PollingSequenceFactory.h create mode 100644 fsfw/defaultcfg/fsfwconfig/returnvalues/classIds.h create mode 100644 fsfw/defaultcfg/fsfwconfig/tmtc/apid.h create mode 100644 fsfw/defaultcfg/fsfwconfig/tmtc/pusIds.h create mode 100644 fsfw/devicehandlers/AcceptsDeviceResponsesIF.h create mode 100644 fsfw/devicehandlers/AssemblyBase.cpp create mode 100644 fsfw/devicehandlers/AssemblyBase.h create mode 100644 fsfw/devicehandlers/ChildHandlerBase.cpp create mode 100644 fsfw/devicehandlers/ChildHandlerBase.h create mode 100644 fsfw/devicehandlers/ChildHandlerFDIR.cpp create mode 100644 fsfw/devicehandlers/ChildHandlerFDIR.h create mode 100644 fsfw/devicehandlers/CookieIF.h create mode 100644 fsfw/devicehandlers/DeviceCommunicationIF.h create mode 100644 fsfw/devicehandlers/DeviceHandlerBase.cpp create mode 100644 fsfw/devicehandlers/DeviceHandlerBase.h create mode 100644 fsfw/devicehandlers/DeviceHandlerFailureIsolation.cpp create mode 100644 fsfw/devicehandlers/DeviceHandlerFailureIsolation.h create mode 100644 fsfw/devicehandlers/DeviceHandlerIF.h create mode 100644 fsfw/devicehandlers/DeviceHandlerMessage.cpp create mode 100644 fsfw/devicehandlers/DeviceHandlerMessage.h create mode 100644 fsfw/devicehandlers/DeviceTmReportingWrapper.cpp create mode 100644 fsfw/devicehandlers/DeviceTmReportingWrapper.h create mode 100644 fsfw/devicehandlers/HealthDevice.cpp create mode 100644 fsfw/devicehandlers/HealthDevice.h create mode 100644 fsfw/events/Event.cpp create mode 100644 fsfw/events/Event.h create mode 100644 fsfw/events/EventManager.cpp create mode 100644 fsfw/events/EventManager.h create mode 100644 fsfw/events/EventManagerIF.h create mode 100644 fsfw/events/EventMessage.cpp create mode 100644 fsfw/events/EventMessage.h create mode 100644 fsfw/events/EventReportingProxyIF.h create mode 100644 fsfw/events/eventmatching/EventIdRangeMatcher.cpp create mode 100644 fsfw/events/eventmatching/EventIdRangeMatcher.h create mode 100644 fsfw/events/eventmatching/EventMatchTree.cpp create mode 100644 fsfw/events/eventmatching/EventMatchTree.h create mode 100644 fsfw/events/eventmatching/EventRangeMatcherBase.h create mode 100644 fsfw/events/eventmatching/ReporterRangeMatcher.cpp create mode 100644 fsfw/events/eventmatching/ReporterRangeMatcher.h create mode 100644 fsfw/events/eventmatching/SeverityRangeMatcher.cpp create mode 100644 fsfw/events/eventmatching/SeverityRangeMatcher.h create mode 100644 fsfw/events/eventmatching/eventmatching.h create mode 100644 fsfw/events/fwSubsystemIdRanges.h create mode 100644 fsfw/fdir/ConfirmsFailuresIF.h create mode 100644 fsfw/fdir/EventCorrelation.cpp create mode 100644 fsfw/fdir/EventCorrelation.h create mode 100644 fsfw/fdir/FailureIsolationBase.cpp create mode 100644 fsfw/fdir/FailureIsolationBase.h create mode 100644 fsfw/fdir/FaultCounter.cpp create mode 100644 fsfw/fdir/FaultCounter.h create mode 100644 fsfw/fsfw.mk create mode 100644 fsfw/globalfunctions/AsciiConverter.cpp create mode 100644 fsfw/globalfunctions/AsciiConverter.h create mode 100644 fsfw/globalfunctions/CRC.cpp create mode 100644 fsfw/globalfunctions/CRC.h create mode 100644 fsfw/globalfunctions/DleEncoder.cpp create mode 100644 fsfw/globalfunctions/DleEncoder.h create mode 100644 fsfw/globalfunctions/PeriodicOperationDivider.cpp create mode 100644 fsfw/globalfunctions/PeriodicOperationDivider.h create mode 100644 fsfw/globalfunctions/Type.cpp create mode 100644 fsfw/globalfunctions/Type.h create mode 100644 fsfw/globalfunctions/arrayprinter.cpp create mode 100644 fsfw/globalfunctions/arrayprinter.h create mode 100644 fsfw/globalfunctions/constants.h create mode 100644 fsfw/globalfunctions/matching/BinaryMatcher.h create mode 100644 fsfw/globalfunctions/matching/DecimalMatcher.h create mode 100644 fsfw/globalfunctions/matching/MatchTree.h create mode 100644 fsfw/globalfunctions/matching/MatcherIF.h create mode 100644 fsfw/globalfunctions/matching/RangeMatcher.h create mode 100644 fsfw/globalfunctions/matching/SerializeableMatcherIF.h create mode 100644 fsfw/globalfunctions/math/MatrixOperations.h create mode 100644 fsfw/globalfunctions/math/QuaternionOperations.cpp create mode 100644 fsfw/globalfunctions/math/QuaternionOperations.h create mode 100644 fsfw/globalfunctions/math/VectorOperations.h create mode 100644 fsfw/globalfunctions/sign.h create mode 100644 fsfw/globalfunctions/timevalOperations.cpp create mode 100644 fsfw/globalfunctions/timevalOperations.h create mode 100644 fsfw/health/HasHealthIF.h create mode 100644 fsfw/health/HealthHelper.cpp create mode 100644 fsfw/health/HealthHelper.h create mode 100644 fsfw/health/HealthMessage.cpp create mode 100644 fsfw/health/HealthMessage.h create mode 100644 fsfw/health/HealthTable.cpp create mode 100644 fsfw/health/HealthTable.h create mode 100644 fsfw/health/HealthTableIF.h create mode 100644 fsfw/health/ManagesHealthIF.h create mode 100644 fsfw/internalError/InternalErrorReporter.cpp create mode 100644 fsfw/internalError/InternalErrorReporter.h create mode 100644 fsfw/internalError/InternalErrorReporterIF.h create mode 100644 fsfw/ipc/CommandMessage.cpp create mode 100644 fsfw/ipc/CommandMessage.h create mode 100644 fsfw/ipc/CommandMessageCleaner.cpp create mode 100644 fsfw/ipc/CommandMessageCleaner.h create mode 100644 fsfw/ipc/CommandMessageIF.h create mode 100644 fsfw/ipc/FwMessageTypes.h create mode 100644 fsfw/ipc/MessageQueueIF.h create mode 100644 fsfw/ipc/MessageQueueMessage.cpp create mode 100644 fsfw/ipc/MessageQueueMessage.h create mode 100644 fsfw/ipc/MessageQueueMessageIF.h create mode 100644 fsfw/ipc/MessageQueueSenderIF.h create mode 100644 fsfw/ipc/MutexFactory.h create mode 100644 fsfw/ipc/MutexHelper.h create mode 100644 fsfw/ipc/MutexIF.h create mode 100644 fsfw/ipc/QueueFactory.h create mode 100644 fsfw/ipc/messageQueueDefinitions.h create mode 100644 fsfw/logo/FSFW_Logo_V3.png create mode 100644 fsfw/logo/FSFW_Logo_V3.svg create mode 100644 fsfw/logo/FSFW_Logo_V3_bw.png create mode 100644 fsfw/memory/AcceptsMemoryMessagesIF.h create mode 100644 fsfw/memory/HasMemoryIF.h create mode 100644 fsfw/memory/MemoryHelper.cpp create mode 100644 fsfw/memory/MemoryHelper.h create mode 100644 fsfw/memory/MemoryMessage.cpp create mode 100644 fsfw/memory/MemoryMessage.h create mode 100644 fsfw/modes/HasModesIF.h create mode 100644 fsfw/modes/ModeHelper.cpp create mode 100644 fsfw/modes/ModeHelper.h create mode 100644 fsfw/modes/ModeMessage.cpp create mode 100644 fsfw/modes/ModeMessage.h create mode 100644 fsfw/monitoring/AbsLimitMonitor.h create mode 100644 fsfw/monitoring/HasMonitorsIF.h create mode 100644 fsfw/monitoring/LimitMonitor.h create mode 100644 fsfw/monitoring/LimitViolationReporter.cpp create mode 100644 fsfw/monitoring/LimitViolationReporter.h create mode 100644 fsfw/monitoring/MonitorBase.h create mode 100644 fsfw/monitoring/MonitorReporter.h create mode 100644 fsfw/monitoring/MonitoringIF.h create mode 100644 fsfw/monitoring/MonitoringMessage.cpp create mode 100644 fsfw/monitoring/MonitoringMessage.h create mode 100644 fsfw/monitoring/MonitoringMessageContent.h create mode 100644 fsfw/monitoring/ReceivesMonitoringReportsIF.h create mode 100644 fsfw/monitoring/TriplexMonitor.h create mode 100644 fsfw/monitoring/TwoValueLimitMonitor.h create mode 100644 fsfw/objectmanager/ObjectManager.cpp create mode 100644 fsfw/objectmanager/ObjectManager.h create mode 100644 fsfw/objectmanager/ObjectManagerIF.h create mode 100644 fsfw/objectmanager/SystemObject.cpp create mode 100644 fsfw/objectmanager/SystemObject.h create mode 100644 fsfw/objectmanager/SystemObjectIF.h create mode 100644 fsfw/objectmanager/frameworkObjects.h create mode 100644 fsfw/osal/Endiness.h create mode 100644 fsfw/osal/FreeRTOS/BinSemaphUsingTask.cpp create mode 100644 fsfw/osal/FreeRTOS/BinSemaphUsingTask.h create mode 100644 fsfw/osal/FreeRTOS/BinarySemaphore.cpp create mode 100644 fsfw/osal/FreeRTOS/BinarySemaphore.h create mode 100644 fsfw/osal/FreeRTOS/Clock.cpp create mode 100644 fsfw/osal/FreeRTOS/CountingSemaphUsingTask.cpp create mode 100644 fsfw/osal/FreeRTOS/CountingSemaphUsingTask.h create mode 100644 fsfw/osal/FreeRTOS/CountingSemaphore.cpp create mode 100644 fsfw/osal/FreeRTOS/CountingSemaphore.h create mode 100644 fsfw/osal/FreeRTOS/FixedTimeslotTask.cpp create mode 100644 fsfw/osal/FreeRTOS/FixedTimeslotTask.h create mode 100644 fsfw/osal/FreeRTOS/FreeRTOSTaskIF.h create mode 100644 fsfw/osal/FreeRTOS/MessageQueue.cpp create mode 100644 fsfw/osal/FreeRTOS/MessageQueue.h create mode 100644 fsfw/osal/FreeRTOS/Mutex.cpp create mode 100644 fsfw/osal/FreeRTOS/Mutex.h create mode 100644 fsfw/osal/FreeRTOS/MutexFactory.cpp create mode 100644 fsfw/osal/FreeRTOS/PeriodicTask.cpp create mode 100644 fsfw/osal/FreeRTOS/PeriodicTask.h create mode 100644 fsfw/osal/FreeRTOS/QueueFactory.cpp create mode 100644 fsfw/osal/FreeRTOS/README.md create mode 100644 fsfw/osal/FreeRTOS/SemaphoreFactory.cpp create mode 100644 fsfw/osal/FreeRTOS/TaskFactory.cpp create mode 100644 fsfw/osal/FreeRTOS/TaskManagement.cpp create mode 100644 fsfw/osal/FreeRTOS/TaskManagement.h create mode 100644 fsfw/osal/FreeRTOS/Timekeeper.cpp create mode 100644 fsfw/osal/FreeRTOS/Timekeeper.h create mode 100644 fsfw/osal/InternalErrorCodes.h create mode 100644 fsfw/osal/host/Clock.cpp create mode 100644 fsfw/osal/host/FixedTimeslotTask.cpp create mode 100644 fsfw/osal/host/FixedTimeslotTask.h create mode 100644 fsfw/osal/host/MessageQueue.cpp create mode 100644 fsfw/osal/host/MessageQueue.h create mode 100644 fsfw/osal/host/Mutex.cpp create mode 100644 fsfw/osal/host/Mutex.h create mode 100644 fsfw/osal/host/MutexFactory.cpp create mode 100644 fsfw/osal/host/PeriodicTask.cpp create mode 100644 fsfw/osal/host/PeriodicTask.h create mode 100644 fsfw/osal/host/QueueFactory.cpp create mode 100644 fsfw/osal/host/QueueMapManager.cpp create mode 100644 fsfw/osal/host/QueueMapManager.h create mode 100644 fsfw/osal/host/SemaphoreFactory.cpp create mode 100644 fsfw/osal/host/TaskFactory.cpp create mode 100644 fsfw/osal/linux/BinarySemaphore.cpp create mode 100644 fsfw/osal/linux/BinarySemaphore.h create mode 100644 fsfw/osal/linux/Clock.cpp create mode 100644 fsfw/osal/linux/CountingSemaphore.cpp create mode 100644 fsfw/osal/linux/CountingSemaphore.h create mode 100644 fsfw/osal/linux/FixedTimeslotTask.cpp create mode 100644 fsfw/osal/linux/FixedTimeslotTask.h create mode 100644 fsfw/osal/linux/InternalErrorCodes.cpp create mode 100644 fsfw/osal/linux/MessageQueue.cpp create mode 100644 fsfw/osal/linux/MessageQueue.h create mode 100644 fsfw/osal/linux/Mutex.cpp create mode 100644 fsfw/osal/linux/Mutex.h create mode 100644 fsfw/osal/linux/MutexFactory.cpp create mode 100644 fsfw/osal/linux/PeriodicPosixTask.cpp create mode 100644 fsfw/osal/linux/PeriodicPosixTask.h create mode 100644 fsfw/osal/linux/PosixThread.cpp create mode 100644 fsfw/osal/linux/PosixThread.h create mode 100644 fsfw/osal/linux/QueueFactory.cpp create mode 100644 fsfw/osal/linux/SemaphoreFactory.cpp create mode 100644 fsfw/osal/linux/TaskFactory.cpp create mode 100644 fsfw/osal/linux/TcUnixUdpPollingTask.cpp create mode 100644 fsfw/osal/linux/TcUnixUdpPollingTask.h create mode 100644 fsfw/osal/linux/Timer.cpp create mode 100644 fsfw/osal/linux/Timer.h create mode 100644 fsfw/osal/linux/TmTcUnixUdpBridge.cpp create mode 100644 fsfw/osal/linux/TmTcUnixUdpBridge.h create mode 100644 fsfw/osal/rtems/Clock.cpp create mode 100644 fsfw/osal/rtems/CpuUsage.cpp create mode 100644 fsfw/osal/rtems/CpuUsage.h create mode 100644 fsfw/osal/rtems/InitTask.cpp create mode 100644 fsfw/osal/rtems/InitTask.h create mode 100644 fsfw/osal/rtems/InternalErrorCodes.cpp create mode 100644 fsfw/osal/rtems/MessageQueue.cpp create mode 100644 fsfw/osal/rtems/MessageQueue.h create mode 100644 fsfw/osal/rtems/MultiObjectTask.cpp create mode 100644 fsfw/osal/rtems/MultiObjectTask.h create mode 100644 fsfw/osal/rtems/Mutex.cpp create mode 100644 fsfw/osal/rtems/Mutex.h create mode 100644 fsfw/osal/rtems/MutexFactory.cpp create mode 100644 fsfw/osal/rtems/PollingTask.cpp create mode 100644 fsfw/osal/rtems/PollingTask.h create mode 100644 fsfw/osal/rtems/QueueFactory.cpp create mode 100644 fsfw/osal/rtems/RtemsBasic.cpp create mode 100644 fsfw/osal/rtems/RtemsBasic.h create mode 100644 fsfw/osal/rtems/TaskBase.cpp create mode 100644 fsfw/osal/rtems/TaskBase.h create mode 100644 fsfw/osal/rtems/TaskFactory.cpp create mode 100644 fsfw/osal/windows/TcWinUdpPollingTask.cpp create mode 100644 fsfw/osal/windows/TcWinUdpPollingTask.h create mode 100644 fsfw/osal/windows/TmTcWinUdpBridge.cpp create mode 100644 fsfw/osal/windows/TmTcWinUdpBridge.h create mode 100644 fsfw/parameters/HasParametersIF.h create mode 100644 fsfw/parameters/ParameterHelper.cpp create mode 100644 fsfw/parameters/ParameterHelper.h create mode 100644 fsfw/parameters/ParameterMessage.cpp create mode 100644 fsfw/parameters/ParameterMessage.h create mode 100644 fsfw/parameters/ParameterWrapper.cpp create mode 100644 fsfw/parameters/ParameterWrapper.h create mode 100644 fsfw/parameters/ReceivesParameterMessagesIF.h create mode 100644 fsfw/power/Fuse.cpp create mode 100644 fsfw/power/Fuse.h create mode 100644 fsfw/power/PowerComponent.cpp create mode 100644 fsfw/power/PowerComponent.h create mode 100644 fsfw/power/PowerComponentIF.h create mode 100644 fsfw/power/PowerSensor.cpp create mode 100644 fsfw/power/PowerSensor.h create mode 100644 fsfw/power/PowerSwitchIF.h create mode 100644 fsfw/power/PowerSwitcher.cpp create mode 100644 fsfw/power/PowerSwitcher.h create mode 100644 fsfw/pus/CService200ModeCommanding.cpp create mode 100644 fsfw/pus/CService200ModeCommanding.h create mode 100644 fsfw/pus/Service17Test.cpp create mode 100644 fsfw/pus/Service17Test.h create mode 100644 fsfw/pus/Service1TelecommandVerification.cpp create mode 100644 fsfw/pus/Service1TelecommandVerification.h create mode 100644 fsfw/pus/Service2DeviceAccess.cpp create mode 100644 fsfw/pus/Service2DeviceAccess.h create mode 100644 fsfw/pus/Service5EventReporting.cpp create mode 100644 fsfw/pus/Service5EventReporting.h create mode 100644 fsfw/pus/Service8FunctionManagement.cpp create mode 100644 fsfw/pus/Service8FunctionManagement.h create mode 100644 fsfw/pus/Service9TimeManagement.cpp create mode 100644 fsfw/pus/Service9TimeManagement.h create mode 100644 fsfw/pus/servicepackets/Service1Packets.h create mode 100644 fsfw/pus/servicepackets/Service200Packets.h create mode 100644 fsfw/pus/servicepackets/Service2Packets.h create mode 100644 fsfw/pus/servicepackets/Service5Packets.h create mode 100644 fsfw/pus/servicepackets/Service8Packets.h create mode 100644 fsfw/pus/servicepackets/Service9Packets.h create mode 100644 fsfw/returnvalues/FwClassIds.h create mode 100644 fsfw/returnvalues/HasReturnvaluesIF.h create mode 100644 fsfw/rmap/RMAP.cpp create mode 100644 fsfw/rmap/RMAP.h create mode 100644 fsfw/rmap/RMAPChannelIF.h create mode 100644 fsfw/rmap/RMAPCookie.cpp create mode 100644 fsfw/rmap/RMAPCookie.h create mode 100644 fsfw/rmap/RmapDeviceCommunicationIF.cpp create mode 100644 fsfw/rmap/RmapDeviceCommunicationIF.h create mode 100644 fsfw/rmap/rmapStructs.h create mode 100644 fsfw/serialize/EndianConverter.h create mode 100644 fsfw/serialize/SerialArrayListAdapter.h create mode 100644 fsfw/serialize/SerialBufferAdapter.cpp create mode 100644 fsfw/serialize/SerialBufferAdapter.h create mode 100644 fsfw/serialize/SerialFixedArrayListAdapter.h create mode 100644 fsfw/serialize/SerialLinkedListAdapter.h create mode 100644 fsfw/serialize/SerializeAdapter.h create mode 100644 fsfw/serialize/SerializeElement.h create mode 100644 fsfw/serialize/SerializeIF.h create mode 100644 fsfw/serviceinterface/ServiceInterfaceBuffer.cpp create mode 100644 fsfw/serviceinterface/ServiceInterfaceBuffer.h create mode 100644 fsfw/serviceinterface/ServiceInterfaceStream.cpp create mode 100644 fsfw/serviceinterface/ServiceInterfaceStream.h create mode 100644 fsfw/storagemanager/ConstStorageAccessor.cpp create mode 100644 fsfw/storagemanager/ConstStorageAccessor.h create mode 100644 fsfw/storagemanager/LocalPool.h create mode 100644 fsfw/storagemanager/LocalPool.tpp create mode 100644 fsfw/storagemanager/PoolManager.h create mode 100644 fsfw/storagemanager/PoolManager.tpp create mode 100644 fsfw/storagemanager/StorageAccessor.cpp create mode 100644 fsfw/storagemanager/StorageAccessor.h create mode 100644 fsfw/storagemanager/StorageManagerIF.h create mode 100644 fsfw/storagemanager/storeAddress.h create mode 100644 fsfw/subsystem/Subsystem.cpp create mode 100644 fsfw/subsystem/Subsystem.h create mode 100644 fsfw/subsystem/SubsystemBase.cpp create mode 100644 fsfw/subsystem/SubsystemBase.h create mode 100644 fsfw/subsystem/modes/HasModeSequenceIF.h create mode 100644 fsfw/subsystem/modes/ModeDefinitions.h create mode 100644 fsfw/subsystem/modes/ModeSequenceMessage.cpp create mode 100644 fsfw/subsystem/modes/ModeSequenceMessage.h create mode 100644 fsfw/subsystem/modes/ModeStore.cpp create mode 100644 fsfw/subsystem/modes/ModeStore.h create mode 100644 fsfw/subsystem/modes/ModeStoreIF.h create mode 100644 fsfw/tasks/ExecutableObjectIF.h create mode 100644 fsfw/tasks/FixedSequenceSlot.cpp create mode 100644 fsfw/tasks/FixedSequenceSlot.h create mode 100644 fsfw/tasks/FixedSlotSequence.cpp create mode 100644 fsfw/tasks/FixedSlotSequence.h create mode 100644 fsfw/tasks/FixedTimeslotTaskIF.h create mode 100644 fsfw/tasks/PeriodicTaskIF.h create mode 100644 fsfw/tasks/SemaphoreFactory.h create mode 100644 fsfw/tasks/SemaphoreIF.h create mode 100644 fsfw/tasks/TaskFactory.h create mode 100644 fsfw/tasks/Typedef.h create mode 100644 fsfw/tcdistribution/CCSDSDistributor.cpp create mode 100644 fsfw/tcdistribution/CCSDSDistributor.h create mode 100644 fsfw/tcdistribution/CCSDSDistributorIF.h create mode 100644 fsfw/tcdistribution/PUSDistributor.cpp create mode 100644 fsfw/tcdistribution/PUSDistributor.h create mode 100644 fsfw/tcdistribution/PUSDistributorIF.h create mode 100644 fsfw/tcdistribution/TcDistributor.cpp create mode 100644 fsfw/tcdistribution/TcDistributor.h create mode 100644 fsfw/tcdistribution/TcPacketCheck.cpp create mode 100644 fsfw/tcdistribution/TcPacketCheck.h create mode 100644 fsfw/thermal/AbstractTemperatureSensor.cpp create mode 100644 fsfw/thermal/AbstractTemperatureSensor.h create mode 100644 fsfw/thermal/CoreComponent.cpp create mode 100644 fsfw/thermal/CoreComponent.h create mode 100644 fsfw/thermal/Heater.cpp create mode 100644 fsfw/thermal/Heater.h create mode 100644 fsfw/thermal/RedundantHeater.cpp create mode 100644 fsfw/thermal/RedundantHeater.h create mode 100644 fsfw/thermal/TemperatureSensor.h create mode 100644 fsfw/thermal/ThermalComponent.cpp create mode 100644 fsfw/thermal/ThermalComponent.h create mode 100644 fsfw/thermal/ThermalComponentIF.h create mode 100644 fsfw/thermal/ThermalModule.cpp create mode 100644 fsfw/thermal/ThermalModule.h create mode 100644 fsfw/thermal/ThermalModuleIF.h create mode 100644 fsfw/thermal/ThermalMonitor.cpp create mode 100644 fsfw/thermal/ThermalMonitor.h create mode 100644 fsfw/thermal/tcsDefinitions.h create mode 100644 fsfw/timemanager/CCSDSTime.cpp create mode 100644 fsfw/timemanager/CCSDSTime.h create mode 100644 fsfw/timemanager/Clock.h create mode 100644 fsfw/timemanager/Countdown.cpp create mode 100644 fsfw/timemanager/Countdown.h create mode 100644 fsfw/timemanager/ReceivesTimeInfoIF.h create mode 100644 fsfw/timemanager/Stopwatch.cpp create mode 100644 fsfw/timemanager/Stopwatch.h create mode 100644 fsfw/timemanager/TimeMessage.cpp create mode 100644 fsfw/timemanager/TimeMessage.h create mode 100644 fsfw/timemanager/TimeStamper.cpp create mode 100644 fsfw/timemanager/TimeStamper.h create mode 100644 fsfw/timemanager/TimeStamperIF.h create mode 100644 fsfw/tmstorage/TmStoreBackendIF.h create mode 100644 fsfw/tmstorage/TmStoreFrontendIF.h create mode 100644 fsfw/tmstorage/TmStoreMessage.cpp create mode 100644 fsfw/tmstorage/TmStoreMessage.h create mode 100644 fsfw/tmstorage/TmStorePackets.h create mode 100644 fsfw/tmtcpacket/SpacePacket.cpp create mode 100644 fsfw/tmtcpacket/SpacePacket.h create mode 100644 fsfw/tmtcpacket/SpacePacketBase.cpp create mode 100644 fsfw/tmtcpacket/SpacePacketBase.h create mode 100644 fsfw/tmtcpacket/ccsds_header.h create mode 100644 fsfw/tmtcpacket/packetmatcher/ApidMatcher.h create mode 100644 fsfw/tmtcpacket/packetmatcher/PacketMatchTree.cpp create mode 100644 fsfw/tmtcpacket/packetmatcher/PacketMatchTree.h create mode 100644 fsfw/tmtcpacket/packetmatcher/ServiceMatcher.h create mode 100644 fsfw/tmtcpacket/packetmatcher/SubserviceMatcher.h create mode 100644 fsfw/tmtcpacket/pus/PacketTimestampInterpreterIF.h create mode 100644 fsfw/tmtcpacket/pus/TcPacketBase.cpp create mode 100644 fsfw/tmtcpacket/pus/TcPacketBase.h create mode 100644 fsfw/tmtcpacket/pus/TcPacketStored.cpp create mode 100644 fsfw/tmtcpacket/pus/TcPacketStored.h create mode 100644 fsfw/tmtcpacket/pus/TmPacketBase.cpp create mode 100644 fsfw/tmtcpacket/pus/TmPacketBase.h create mode 100644 fsfw/tmtcpacket/pus/TmPacketMinimal.cpp create mode 100644 fsfw/tmtcpacket/pus/TmPacketMinimal.h create mode 100644 fsfw/tmtcpacket/pus/TmPacketStored.cpp create mode 100644 fsfw/tmtcpacket/pus/TmPacketStored.h create mode 100644 fsfw/tmtcservices/AcceptsTelecommandsIF.h create mode 100644 fsfw/tmtcservices/AcceptsTelemetryIF.h create mode 100644 fsfw/tmtcservices/AcceptsVerifyMessageIF.h create mode 100644 fsfw/tmtcservices/CommandingServiceBase.cpp create mode 100644 fsfw/tmtcservices/CommandingServiceBase.h create mode 100644 fsfw/tmtcservices/PusServiceBase.cpp create mode 100644 fsfw/tmtcservices/PusServiceBase.h create mode 100644 fsfw/tmtcservices/PusVerificationReport.cpp create mode 100644 fsfw/tmtcservices/PusVerificationReport.h create mode 100644 fsfw/tmtcservices/ServiceTypes.h create mode 100644 fsfw/tmtcservices/SourceSequenceCounter.h create mode 100644 fsfw/tmtcservices/TmTcBridge.cpp create mode 100644 fsfw/tmtcservices/TmTcBridge.h create mode 100644 fsfw/tmtcservices/TmTcMessage.cpp create mode 100644 fsfw/tmtcservices/TmTcMessage.h create mode 100644 fsfw/tmtcservices/VerificationCodes.h create mode 100644 fsfw/tmtcservices/VerificationReporter.cpp create mode 100644 fsfw/tmtcservices/VerificationReporter.h create mode 100644 fsfw/unittest/README.md create mode 100644 fsfw/unittest/core/CatchDefinitions.cpp create mode 100644 fsfw/unittest/core/CatchDefinitions.h create mode 100644 fsfw/unittest/core/CatchRunner.cpp create mode 100644 fsfw/unittest/core/CatchSetup.cpp create mode 100644 fsfw/unittest/core/core.mk create mode 100644 fsfw/unittest/core/printChar.cpp create mode 100644 fsfw/unittest/core/printChar.h create mode 100644 fsfw/unittest/internal/InternalUnitTester.cpp create mode 100644 fsfw/unittest/internal/InternalUnitTester.h create mode 100644 fsfw/unittest/internal/UnittDefinitions.cpp create mode 100644 fsfw/unittest/internal/UnittDefinitions.h create mode 100644 fsfw/unittest/internal/internal.mk create mode 100644 fsfw/unittest/internal/osal/IntTestMq.cpp create mode 100644 fsfw/unittest/internal/osal/IntTestMq.h create mode 100644 fsfw/unittest/internal/osal/IntTestMutex.cpp create mode 100644 fsfw/unittest/internal/osal/IntTestMutex.h create mode 100644 fsfw/unittest/internal/osal/IntTestSemaphore.cpp create mode 100644 fsfw/unittest/internal/osal/IntTestSemaphore.h create mode 100644 fsfw/unittest/internal/serialize/IntTestSerialization.cpp create mode 100644 fsfw/unittest/internal/serialize/IntTestSerialization.h create mode 100644 fsfw/unittest/lcov.sh create mode 100644 fsfw/unittest/testcfg/FSFWConfig.h create mode 100644 fsfw/unittest/testcfg/Makefile-FSFW-Tests create mode 100644 fsfw/unittest/testcfg/TestsConfig.h create mode 100644 fsfw/unittest/testcfg/cdatapool/dataPoolInit.cpp create mode 100644 fsfw/unittest/testcfg/cdatapool/dataPoolInit.h create mode 100644 fsfw/unittest/testcfg/devices/logicalAddresses.cpp create mode 100644 fsfw/unittest/testcfg/devices/logicalAddresses.h create mode 100644 fsfw/unittest/testcfg/devices/powerSwitcherList.cpp create mode 100644 fsfw/unittest/testcfg/devices/powerSwitcherList.h create mode 100644 fsfw/unittest/testcfg/events/subsystemIdRanges.h create mode 100644 fsfw/unittest/testcfg/ipc/MissionMessageTypes.cpp create mode 100644 fsfw/unittest/testcfg/ipc/MissionMessageTypes.h create mode 100644 fsfw/unittest/testcfg/objects/Factory.cpp create mode 100644 fsfw/unittest/testcfg/objects/Factory.h create mode 100644 fsfw/unittest/testcfg/objects/systemObjectList.h create mode 100644 fsfw/unittest/testcfg/pollingsequence/PollingSequenceFactory.cpp create mode 100644 fsfw/unittest/testcfg/pollingsequence/PollingSequenceFactory.h create mode 100644 fsfw/unittest/testcfg/returnvalues/classIds.h create mode 100644 fsfw/unittest/testcfg/testcfg.mk create mode 100644 fsfw/unittest/testcfg/tmtc/apid.h create mode 100644 fsfw/unittest/testcfg/tmtc/pusIds.h create mode 100644 fsfw/unittest/tests/action/TestActionHelper.cpp create mode 100644 fsfw/unittest/tests/action/TestActionHelper.h create mode 100644 fsfw/unittest/tests/container/RingBufferTest.cpp create mode 100644 fsfw/unittest/tests/container/TestArrayList.cpp create mode 100644 fsfw/unittest/tests/container/TestDynamicFifo.cpp create mode 100644 fsfw/unittest/tests/container/TestFifo.cpp create mode 100644 fsfw/unittest/tests/container/TestFixedArrayList.cpp create mode 100644 fsfw/unittest/tests/container/TestFixedMap.cpp create mode 100644 fsfw/unittest/tests/container/TestFixedOrderedMultimap.cpp create mode 100644 fsfw/unittest/tests/container/TestPlacementFactory.cpp create mode 100644 fsfw/unittest/tests/osal/TestMessageQueue.cpp create mode 100644 fsfw/unittest/tests/osal/TestSemaphore.cpp create mode 100644 fsfw/unittest/tests/serialize/TestSerialBufferAdapter.cpp create mode 100644 fsfw/unittest/tests/serialize/TestSerialLinkedPacket.cpp create mode 100644 fsfw/unittest/tests/serialize/TestSerialLinkedPacket.h create mode 100644 fsfw/unittest/tests/serialize/TestSerialization.cpp create mode 100644 fsfw/unittest/tests/storagemanager/TestNewAccessor.cpp create mode 100644 fsfw/unittest/tests/storagemanager/TestPool.cpp create mode 100644 fsfw/unittest/tests/tests.mk create mode 100644 fsfw/unittest/testtemplate/TestTemplate.cpp create mode 100644 fsfw/unittest/unlockRealtime.sh diff --git a/fsfw/.gitignore b/fsfw/.gitignore new file mode 100644 index 0000000..fbd138e --- /dev/null +++ b/fsfw/.gitignore @@ -0,0 +1,4 @@ +.cproject +.project +.settings +.metadata diff --git a/fsfw/.gitmodules b/fsfw/.gitmodules new file mode 100644 index 0000000..e69de29 diff --git a/fsfw/FSFWVersion.h b/fsfw/FSFWVersion.h new file mode 100644 index 0000000..11a6089 --- /dev/null +++ b/fsfw/FSFWVersion.h @@ -0,0 +1,12 @@ +#ifndef FSFW_DEFAULTCFG_VERSION_H_ +#define FSFW_DEFAULTCFG_VERSION_H_ + +const char* const FSFW_VERSION_NAME = "ASTP"; + +#define FSFW_VERSION 0 +#define FSFW_SUBVERSION 0 +#define FSFW_REVISION 1 + + + +#endif /* FSFW_DEFAULTCFG_VERSION_H_ */ diff --git a/fsfw/LICENSE b/fsfw/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/fsfw/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/fsfw/NOTICE b/fsfw/NOTICE new file mode 100644 index 0000000..be1a37c --- /dev/null +++ b/fsfw/NOTICE @@ -0,0 +1,23 @@ +Flight Software Framework + +The initial version of the Flight Software Framework was developed during +the Flying Laptop Project by the Universität Stuttgart in coorporation +with Airbus Defence and Space GmbH. + +The supreme FSFW Logo was designed by Markus Koller and Luise Trilsbach. + +Copyrights in the Flight Software Framework are retained by their contributors. +No copyright assignment is required to contribute to the Flight Software Framework. + +Some files include explicit copyright notices and/or license notices. +For full authorship information, see the version control history. + +Except as otherwise noted (below and/or in individual files), the +Flight Software Framework is licensed under the Apache License, Version 2.0. + +The Flight Software Framework includes modules written by third parties. +The following third party modules are included, and carry +their own copyright notices and license terms: + +under contrib/: + * sgp4: sgp4 code developed by david vallado under public domain, see https://www.celestrak.com/publications/AIAA/2006-6753/ diff --git a/fsfw/README.md b/fsfw/README.md new file mode 100644 index 0000000..4aabe36 --- /dev/null +++ b/fsfw/README.md @@ -0,0 +1,159 @@ +![FSFW Logo](logo/FSFW_Logo_V3_bw.png) +# Flight Software Framework (FSFW) + +The Flight Software Framework is a C++ Object Oriented Framework for unmanned, +automated systems like Satellites. + +The initial version of the Flight Software Framework was developed during +the Flying Laptop Project by the University of Stuttgart in cooperation +with Airbus Defence and Space GmbH. + +## Intended Use + +The framework is designed for systems, which communicate with external devices, perform control loops, receive telecommands and send telemetry, and need to maintain a high level of availability. +Therefore, a mode and health system provides control over the states of the software and the controlled devices. +In addition, a simple mechanism of event based fault detection, isolation and recovery is implemented as well. + +The recommended hardware is a microprocessor with more than 2 MB of RAM and 1 MB of non-volatile Memory. +For reference, current Applications use a Cobham Gaisler UT699 (LEON3FT), a ISISPACE IOBC or a Zynq-7020 SoC. + + +## Structure + +The general structure is driven by the usage of interfaces provided by objects. The FSFW uses C++11 as baseline. The intention behind this is that this C++ Standard should be widely available, even with older compilers. +The FSFW uses dynamic allocation during the initialization but provides static containers during runtime. +This simplifies the instantiation of objects and allows the usage of some standard containers. +Dynamic Allocation after initialization is discouraged and different solutions are provided in the FSFW to achieve that. +The fsfw uses Run-time type information. +Exceptions are not allowed. + +### Failure Handling + +Functions should return a defined ReturnValue_t to signal to the caller that something is gone wrong. +Returnvalues must be unique. For this the function HasReturnvaluesIF::makeReturnCode or the Macro MAKE_RETURN can be used. +The CLASS_ID is a unique id for that type of object. See returnvalues/FwClassIds. + +### OSAL +The FSFW provides operation system abstraction layers for Linux, FreeRTOS and RTEMS. A independent OSAL called "host" is currently not finished. This aims to be running on windows as well. +The OSAL provides periodic tasks, message queues, clocks and Semaphores as well as Mutexes. + +### Core Components + +Clock: + * This is a class of static functions that can be used at anytime + * Leap Seconds must be set if any time conversions from UTC to other times is used + +ObjectManager (must be created): + +* The component which handles all references. All SystemObjects register at this component. +* Any SystemObject needs to have a unique ObjectId. Those can be managed like objects::framework_objects. +* A reference to an object can be get by calling the following function. T must be the specific Interface you want to call. +A nullptr check of the returning Pointer must be done. This function is based on Run-time type information. + +``` c++ + template T* ObjectManagerIF::get( object_id_t id ) + +``` +* A typical way to create all objects on startup is a handing a static produce function to the ObjectManager on creation. +By calling objectManager->initialize() the produce function will be called and all SystemObjects will be initialized afterwards. + +Event Manager: + +* Component which allows routing of events +* Other objects can subscribe to specific events, ranges of events or all events of an object. +* Subscriptions can be done during runtime but should be done during initialization +* Amounts of allowed subscriptions must be configured by setting this parameters: + +``` c++ +namespace fsfwconfig { +//! Configure the allocated pool sizes for the event manager. +static constexpr size_t FSFW_EVENTMGMR_MATCHTREE_NODES = 240; +static constexpr size_t FSFW_EVENTMGMT_EVENTIDMATCHERS = 120; +static constexpr size_t FSFW_EVENTMGMR_RANGEMATCHERS = 120; +} +``` + + +Health Table: + +* A component which holds every health state +* Provides a thread safe way to access all health states without the need of message exchanges + +Stores + +* The message based communication can only exchange a few bytes of information inside the message itself. Therefore, additional information can be exchanged with Stores. With this, only the store address must be exchanged in the message. +* Internally, the FSFW uses an IPC Store to exchange data between processes. For incoming TCs a TC Store is used. For outgoing TM a TM store is used. +* All of them should use the Thread Safe Class storagemanager/PoolManager + +Tasks + +There are two different types of tasks: + * The PeriodicTask just executes objects that are of type ExecutableObjectIF in the order of the insertion to the Tasks. + * FixedTimeslotTask executes a list of calls in the order of the given list. This is intended for DeviceHandlers, where polling should be in a defined order. An example can be found in defaultcfg/fsfwconfig/pollingSequence + + +### Static Ids in the framework + +Some parts of the framework use a static routing address for communication. +An example setup of ids can be found in the example config in "defaultcft/fsfwconfig/objects/Factory::setStaticFrameworkObjectIds()". + +### Events + +Events are tied to objects. EventIds can be generated by calling the Macro MAKE_EVENT. This works analog to the returnvalues. +Every object that needs own EventIds has to get a unique SUBSYSTEM_ID. +Every SystemObject can call triggerEvent from the parent class. +Therefore, event messages contain the specific EventId and the objectId of the object that has triggered. + +### Internal Communication + +Components communicate mostly over Message through Queues. +Those queues are created by calling the singleton QueueFactory::instance()->create(). + +### External Communication + +The external communication with the mission control system is mostly up to the user implementation. +The FSFW provides PUS Services which can be used to but don't need to be used. +The services can be seen as a conversion from a TC to a message based communication and back. + +#### CCSDS Frames, CCSDS Space Packets and PUS + +If the communication is based on CCSDS Frames and Space Packets, several classes can be used to distributed the packets to the corresponding services. Those can be found in tcdistribution. +If Space Packets are used, a timestamper must be created. +An example can be found in the timemanager folder, this uses CCSDSTime::CDS_short. + +#### DeviceHandling + +DeviceHandlers are a core component of the FSFW. +The idea is, to have a software counterpart of every physical device to provide a simple mode, health and commanding interface. +By separating the underlying Communication Interface with DeviceCommunicationIF, a DH can be tested on different hardware. +The DH has mechanisms to monitor the communication with the physical device which allow for FDIR reaction. +A standard FDIR component for the DH will be created automatically but can be overwritten by the user. + +#### Modes, Health + +The two interfaces HasModesIF and HasHealthIF provide access for commanding and monitoring of components. +On-board Mode Management is implement in hierarchy system. +DeviceHandlers and Controllers are the lowest part of the hierarchy. +The next layer are Assemblies. Those assemblies act as a component which handle redundancies of handlers. +Assemblies share a common core with the next level which are the Subsystems. + +Those Assemblies are intended to act as auto-generated components from a database which describes the subsystem modes. +The definitions contain transition and target tables which contain the DH, Assembly and Controller Modes to be commanded. +Transition tables contain as many steps as needed to reach the mode from any other mode, e.g. a switch into any higher AOCS mode might first turn on the sensors, than the actuators and the controller as last component. +The target table is used to describe the state that is checked continuously by the subsystem. +All of this allows System Modes to be generated as Subsystem object as well from the same database. +This System contains list of subsystem modes in the transition and target tables. +Therefore, it allows a modular system to create system modes and easy commanding of those, because only the highest components must be commanded. + +The health state represents if the component is able to perform its tasks. +This can be used to signal the system to avoid using this component instead of a redundant one. +The on-board FDIR uses the health state for isolation and recovery. + +## Example config + +A example config can be found in defaultcfg/fsfwconfig. + +## Unit Tests + +Unit Tests are provided in the unittest folder. Those use the catch2 framework but do not include catch2 itself. +See README.md in the unittest Folder. \ No newline at end of file diff --git a/fsfw/action/ActionHelper.cpp b/fsfw/action/ActionHelper.cpp new file mode 100644 index 0000000..2855791 --- /dev/null +++ b/fsfw/action/ActionHelper.cpp @@ -0,0 +1,162 @@ +#include "ActionHelper.h" +#include "HasActionsIF.h" + +#include "../ipc/MessageQueueSenderIF.h" +#include "../objectmanager/ObjectManagerIF.h" + +ActionHelper::ActionHelper(HasActionsIF* setOwner, + MessageQueueIF* useThisQueue) : + owner(setOwner), queueToUse(useThisQueue) { +} + +ActionHelper::~ActionHelper() { +} + +ReturnValue_t ActionHelper::handleActionMessage(CommandMessage* command) { + if (command->getCommand() == ActionMessage::EXECUTE_ACTION) { + ActionId_t currentAction = ActionMessage::getActionId(command); + prepareExecution(command->getSender(), currentAction, + ActionMessage::getStoreId(command)); + return HasReturnvaluesIF::RETURN_OK; + } else { + return CommandMessage::UNKNOWN_COMMAND; + } +} + +ReturnValue_t ActionHelper::initialize(MessageQueueIF* queueToUse_) { + ipcStore = objectManager->get(objects::IPC_STORE); + if (ipcStore == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + if(queueToUse_ != nullptr) { + setQueueToUse(queueToUse_); + } + + return HasReturnvaluesIF::RETURN_OK; +} + +void ActionHelper::step(uint8_t step, MessageQueueId_t reportTo, + ActionId_t commandId, ReturnValue_t result) { + CommandMessage reply; + ActionMessage::setStepReply(&reply, commandId, step + STEP_OFFSET, result); + queueToUse->sendMessage(reportTo, &reply); +} + +void ActionHelper::finish(MessageQueueId_t reportTo, ActionId_t commandId, + ReturnValue_t result) { + CommandMessage reply; + ActionMessage::setCompletionReply(&reply, commandId, result); + queueToUse->sendMessage(reportTo, &reply); +} + +void ActionHelper::setQueueToUse(MessageQueueIF* queue) { + queueToUse = queue; +} + +void ActionHelper::prepareExecution(MessageQueueId_t commandedBy, + ActionId_t actionId, store_address_t dataAddress) { + const uint8_t* dataPtr = NULL; + size_t size = 0; + ReturnValue_t result = ipcStore->getData(dataAddress, &dataPtr, &size); + if (result != HasReturnvaluesIF::RETURN_OK) { + CommandMessage reply; + ActionMessage::setStepReply(&reply, actionId, 0, result); + queueToUse->sendMessage(commandedBy, &reply); + return; + } + result = owner->executeAction(actionId, commandedBy, dataPtr, size); + ipcStore->deleteData(dataAddress); + if(result == HasActionsIF::EXECUTION_FINISHED) { + CommandMessage reply; + ActionMessage::setCompletionReply(&reply, actionId, result); + queueToUse->sendMessage(commandedBy, &reply); + } + if (result != HasReturnvaluesIF::RETURN_OK) { + CommandMessage reply; + ActionMessage::setStepReply(&reply, actionId, 0, result); + queueToUse->sendMessage(commandedBy, &reply); + return; + } +} + +ReturnValue_t ActionHelper::reportData(MessageQueueId_t reportTo, + ActionId_t replyId, SerializeIF* data, bool hideSender) { + CommandMessage reply; + store_address_t storeAddress; + uint8_t *dataPtr; + size_t maxSize = data->getSerializedSize(); + if (maxSize == 0) { + //No error, there's simply nothing to report. + return HasReturnvaluesIF::RETURN_OK; + } + size_t size = 0; + ReturnValue_t result = ipcStore->getFreeElement(&storeAddress, maxSize, + &dataPtr); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = data->serialize(&dataPtr, &size, maxSize, + SerializeIF::Endianness::BIG); + if (result != HasReturnvaluesIF::RETURN_OK) { + ipcStore->deleteData(storeAddress); + return result; + } + // We don't need to report the objectId, as we receive REQUESTED data + // before the completion success message. + // True aperiodic replies need to be reported with + // another dedicated message. + ActionMessage::setDataReply(&reply, replyId, storeAddress); + + // If the sender needs to be hidden, for example to handle packet + // as unrequested reply, this will be done here. + if (hideSender) { + result = MessageQueueSenderIF::sendMessage(reportTo, &reply); + } + else { + result = queueToUse->sendMessage(reportTo, &reply); + } + + if (result != HasReturnvaluesIF::RETURN_OK){ + ipcStore->deleteData(storeAddress); + } + return result; +} + +void ActionHelper::resetHelper() { +} + +ReturnValue_t ActionHelper::reportData(MessageQueueId_t reportTo, + ActionId_t replyId, const uint8_t *data, size_t dataSize, + bool hideSender) { + CommandMessage reply; + store_address_t storeAddress; + ReturnValue_t result = ipcStore->addData(&storeAddress, data, dataSize); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + if (result != HasReturnvaluesIF::RETURN_OK) { + ipcStore->deleteData(storeAddress); + return result; + } + + // We don't need to report the objectId, as we receive REQUESTED data + // before the completion success message. + // True aperiodic replies need to be reported with + // another dedicated message. + ActionMessage::setDataReply(&reply, replyId, storeAddress); + + // If the sender needs to be hidden, for example to handle packet + // as unrequested reply, this will be done here. + if (hideSender) { + result = MessageQueueSenderIF::sendMessage(reportTo, &reply); + } + else { + result = queueToUse->sendMessage(reportTo, &reply); + } + + if (result != HasReturnvaluesIF::RETURN_OK){ + ipcStore->deleteData(storeAddress); + } + return result; +} diff --git a/fsfw/action/ActionHelper.h b/fsfw/action/ActionHelper.h new file mode 100644 index 0000000..a91722f --- /dev/null +++ b/fsfw/action/ActionHelper.h @@ -0,0 +1,125 @@ +#ifndef FSFW_ACTION_ACTIONHELPER_H_ +#define FSFW_ACTION_ACTIONHELPER_H_ + +#include "ActionMessage.h" +#include "../serialize/SerializeIF.h" +#include "../ipc/MessageQueueIF.h" +/** + * @brief Action Helper is a helper class which handles action messages + * + * Components which use the HasActionIF this helper can be used to handle + * the action messages. + * It does handle step messages as well as other answers to action calls. + * It uses the executeAction function of its owner as callback. + * The call of the initialize function is mandatory and needs a + * valid MessageQueueIF pointer! + */ +class HasActionsIF; + +class ActionHelper { +public: + /** + * Constructor of the action helper + * @param setOwner Pointer to the owner of the interface + * @param useThisQueue messageQueue to be used, can be set during + * initialize function as well. + */ + ActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue); + + virtual ~ActionHelper(); + /** + * Function to be called from the owner with a new command message + * + * If the message is a valid action message the helper will use the + * executeAction function from HasActionsIF. + * If the message is invalid or the callback fails a message reply will be + * send to the sender of the message automatically. + * + * @param command Pointer to a command message received by the owner + * @return HasReturnvaluesIF::RETURN_OK if the message is a action message, + * CommandMessage::UNKNOW_COMMAND if this message ID is unkown + */ + ReturnValue_t handleActionMessage(CommandMessage* command); + /** + * Helper initialize function. Must be called before use of any other + * helper function + * @param queueToUse_ Pointer to the messageQueue to be used, optional + * if queue was set in constructor + * @return Returns RETURN_OK if successful + */ + ReturnValue_t initialize(MessageQueueIF* queueToUse_ = nullptr); + /** + * Function to be called from the owner to send a step message. + * Success or failure will be determined by the result value. + * + * @param step Number of steps already done + * @param reportTo The messageQueueId to report the step message to + * @param commandId ID of the executed command + * @param result Result of the execution + */ + void step(uint8_t step, MessageQueueId_t reportTo, + ActionId_t commandId, + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); + /** + * Function to be called by the owner to send a action completion message + * + * @param reportTo MessageQueueId_t to report the action completion message to + * @param commandId ID of the executed command + * @param result Result of the execution + */ + void finish(MessageQueueId_t reportTo, ActionId_t commandId, + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); + /** + * Function to be called by the owner if an action does report data. + * Takes a SerializeIF* pointer and serializes it into the IPC store. + * @param reportTo MessageQueueId_t to report the action completion + * message to + * @param replyId ID of the executed command + * @param data Pointer to the data + * @return Returns RETURN_OK if successful, otherwise failure code + */ + ReturnValue_t reportData(MessageQueueId_t reportTo, ActionId_t replyId, + SerializeIF* data, bool hideSender = false); + /** + * Function to be called by the owner if an action does report data. + * Takes the raw data and writes it into the IPC store. + * @param reportTo MessageQueueId_t to report the action completion + * message to + * @param replyId ID of the executed command + * @param data Pointer to the data + * @return Returns RETURN_OK if successful, otherwise failure code + */ + ReturnValue_t reportData(MessageQueueId_t reportTo, ActionId_t replyId, + const uint8_t* data, size_t dataSize, bool hideSender = false); + /** + * Function to setup the MessageQueueIF* of the helper. Can be used to + * set the MessageQueueIF* if message queue is unavailable at construction + * and initialize but must be setup before first call of other functions. + * @param queue Queue to be used by the helper + */ + void setQueueToUse(MessageQueueIF *queue); +protected: + //!< Increase of value of this per step + static const uint8_t STEP_OFFSET = 1; + HasActionsIF* owner;//!< Pointer to the owner + //! Queue to be used as response sender, has to be set in ctor or with + //! setQueueToUse + MessageQueueIF* queueToUse; + //! Pointer to an IPC Store, initialized during construction or + StorageManagerIF* ipcStore = nullptr; + + /** + * Internal function called by handleActionMessage + * @param commandedBy MessageQueueID of Commander + * @param actionId ID of action to be done + * @param dataAddress Address of additional data in IPC Store + */ + virtual void prepareExecution(MessageQueueId_t commandedBy, + ActionId_t actionId, store_address_t dataAddress); + /** + * @brief Default implementation is empty. + */ + virtual void resetHelper(); +}; + +#endif /* FSFW_ACTION_ACTIONHELPER_H_ */ diff --git a/fsfw/action/ActionMessage.cpp b/fsfw/action/ActionMessage.cpp new file mode 100644 index 0000000..b0bcaba --- /dev/null +++ b/fsfw/action/ActionMessage.cpp @@ -0,0 +1,79 @@ +#include "ActionMessage.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../storagemanager/StorageManagerIF.h" + +ActionMessage::ActionMessage() { +} + +ActionMessage::~ActionMessage() { +} + +void ActionMessage::setCommand(CommandMessage* message, ActionId_t fid, + store_address_t parameters) { + message->setCommand(EXECUTE_ACTION); + message->setParameter(fid); + message->setParameter2(parameters.raw); +} + +ActionId_t ActionMessage::getActionId(const CommandMessage* message) { + return ActionId_t(message->getParameter()); +} + +store_address_t ActionMessage::getStoreId(const CommandMessage* message) { + store_address_t temp; + temp.raw = message->getParameter2(); + return temp; +} + +void ActionMessage::setStepReply(CommandMessage* message, ActionId_t fid, uint8_t step, + ReturnValue_t result) { + if (result == HasReturnvaluesIF::RETURN_OK) { + message->setCommand(STEP_SUCCESS); + } else { + message->setCommand(STEP_FAILED); + } + message->setParameter(fid); + message->setParameter2((step << 16) + result); +} + +uint8_t ActionMessage::getStep(const CommandMessage* message) { + return uint8_t((message->getParameter2() >> 16) & 0xFF); +} + +ReturnValue_t ActionMessage::getReturnCode(const CommandMessage* message) { + return message->getParameter2() & 0xFFFF; +} + +void ActionMessage::setDataReply(CommandMessage* message, ActionId_t actionId, + store_address_t data) { + message->setCommand(DATA_REPLY); + message->setParameter(actionId); + message->setParameter2(data.raw); +} + +void ActionMessage::setCompletionReply(CommandMessage* message, + ActionId_t fid, ReturnValue_t result) { + if (result == HasReturnvaluesIF::RETURN_OK) { + message->setCommand(COMPLETION_SUCCESS); + } else { + message->setCommand(COMPLETION_FAILED); + } + message->setParameter(fid); + message->setParameter2(result); +} + +void ActionMessage::clear(CommandMessage* message) { + switch(message->getCommand()) { + case EXECUTE_ACTION: + case DATA_REPLY: { + StorageManagerIF *ipcStore = objectManager->get( + objects::IPC_STORE); + if (ipcStore != NULL) { + ipcStore->deleteData(getStoreId(message)); + } + break; + } + default: + break; + } +} diff --git a/fsfw/action/ActionMessage.h b/fsfw/action/ActionMessage.h new file mode 100644 index 0000000..7a859de --- /dev/null +++ b/fsfw/action/ActionMessage.h @@ -0,0 +1,36 @@ +#ifndef FSFW_ACTION_ACTIONMESSAGE_H_ +#define FSFW_ACTION_ACTIONMESSAGE_H_ + +#include "../ipc/CommandMessage.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../storagemanager/StorageManagerIF.h" +typedef uint32_t ActionId_t; + +class ActionMessage { +private: + ActionMessage(); +public: + static const uint8_t MESSAGE_ID = messagetypes::ACTION; + static const Command_t EXECUTE_ACTION = MAKE_COMMAND_ID(1); + static const Command_t STEP_SUCCESS = MAKE_COMMAND_ID(2); + static const Command_t STEP_FAILED = MAKE_COMMAND_ID(3); + static const Command_t DATA_REPLY = MAKE_COMMAND_ID(4); + static const Command_t COMPLETION_SUCCESS = MAKE_COMMAND_ID(5); + static const Command_t COMPLETION_FAILED = MAKE_COMMAND_ID(6); + virtual ~ActionMessage(); + static void setCommand(CommandMessage* message, ActionId_t fid, + store_address_t parameters); + static ActionId_t getActionId(const CommandMessage* message ); + static store_address_t getStoreId(const CommandMessage* message ); + static void setStepReply(CommandMessage* message, ActionId_t fid, + uint8_t step, ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); + static uint8_t getStep(const CommandMessage* message ); + static ReturnValue_t getReturnCode(const CommandMessage* message ); + static void setDataReply(CommandMessage* message, ActionId_t actionId, + store_address_t data); + static void setCompletionReply(CommandMessage* message, ActionId_t fid, + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); + static void clear(CommandMessage* message); +}; + +#endif /* FSFW_ACTION_ACTIONMESSAGE_H_ */ diff --git a/fsfw/action/CommandActionHelper.cpp b/fsfw/action/CommandActionHelper.cpp new file mode 100644 index 0000000..4e88f3c --- /dev/null +++ b/fsfw/action/CommandActionHelper.cpp @@ -0,0 +1,127 @@ +#include "ActionMessage.h" +#include "CommandActionHelper.h" +#include "CommandsActionsIF.h" +#include "HasActionsIF.h" +#include "../objectmanager/ObjectManagerIF.h" + +CommandActionHelper::CommandActionHelper(CommandsActionsIF *setOwner) : + owner(setOwner), queueToUse(NULL), ipcStore( + NULL), commandCount(0), lastTarget(0) { +} + +CommandActionHelper::~CommandActionHelper() { +} + +ReturnValue_t CommandActionHelper::commandAction(object_id_t commandTo, + ActionId_t actionId, SerializeIF *data) { + HasActionsIF *receiver = objectManager->get(commandTo); + if (receiver == NULL) { + return CommandsActionsIF::OBJECT_HAS_NO_FUNCTIONS; + } + store_address_t storeId; + uint8_t *storePointer; + size_t maxSize = data->getSerializedSize(); + ReturnValue_t result = ipcStore->getFreeElement(&storeId, maxSize, + &storePointer); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + size_t size = 0; + result = data->serialize(&storePointer, &size, maxSize, + SerializeIF::Endianness::BIG); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return sendCommand(receiver->getCommandQueue(), actionId, storeId); +} + +ReturnValue_t CommandActionHelper::commandAction(object_id_t commandTo, + ActionId_t actionId, const uint8_t *data, uint32_t size) { +// if (commandCount != 0) { +// return CommandsFunctionsIF::ALREADY_COMMANDING; +// } + HasActionsIF *receiver = objectManager->get(commandTo); + if (receiver == NULL) { + return CommandsActionsIF::OBJECT_HAS_NO_FUNCTIONS; + } + store_address_t storeId; + ReturnValue_t result = ipcStore->addData(&storeId, data, size); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return sendCommand(receiver->getCommandQueue(), actionId, storeId); +} + +ReturnValue_t CommandActionHelper::sendCommand(MessageQueueId_t queueId, + ActionId_t actionId, store_address_t storeId) { + CommandMessage command; + ActionMessage::setCommand(&command, actionId, storeId); + ReturnValue_t result = queueToUse->sendMessage(queueId, &command); + if (result != HasReturnvaluesIF::RETURN_OK) { + ipcStore->deleteData(storeId); + } + lastTarget = queueId; + commandCount++; + return result; +} + +ReturnValue_t CommandActionHelper::initialize() { + ipcStore = objectManager->get(objects::IPC_STORE); + if (ipcStore == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + + queueToUse = owner->getCommandQueuePtr(); + if (queueToUse == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t CommandActionHelper::handleReply(CommandMessage *reply) { + if (reply->getSender() != lastTarget) { + return HasReturnvaluesIF::RETURN_FAILED; + } + switch (reply->getCommand()) { + case ActionMessage::COMPLETION_SUCCESS: + commandCount--; + owner->completionSuccessfulReceived(ActionMessage::getActionId(reply)); + return HasReturnvaluesIF::RETURN_OK; + case ActionMessage::COMPLETION_FAILED: + commandCount--; + owner->completionFailedReceived(ActionMessage::getActionId(reply), + ActionMessage::getReturnCode(reply)); + return HasReturnvaluesIF::RETURN_OK; + case ActionMessage::STEP_SUCCESS: + owner->stepSuccessfulReceived(ActionMessage::getActionId(reply), + ActionMessage::getStep(reply)); + return HasReturnvaluesIF::RETURN_OK; + case ActionMessage::STEP_FAILED: + commandCount--; + owner->stepFailedReceived(ActionMessage::getActionId(reply), + ActionMessage::getStep(reply), + ActionMessage::getReturnCode(reply)); + return HasReturnvaluesIF::RETURN_OK; + case ActionMessage::DATA_REPLY: + extractDataForOwner(ActionMessage::getActionId(reply), + ActionMessage::getStoreId(reply)); + return HasReturnvaluesIF::RETURN_OK; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +uint8_t CommandActionHelper::getCommandCount() const { + return commandCount; +} + +void CommandActionHelper::extractDataForOwner(ActionId_t actionId, store_address_t storeId) { + const uint8_t * data = NULL; + size_t size = 0; + ReturnValue_t result = ipcStore->getData(storeId, &data, &size); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + owner->dataReceived(actionId, data, size); + ipcStore->deleteData(storeId); +} diff --git a/fsfw/action/CommandActionHelper.h b/fsfw/action/CommandActionHelper.h new file mode 100644 index 0000000..cfed8c9 --- /dev/null +++ b/fsfw/action/CommandActionHelper.h @@ -0,0 +1,36 @@ +#ifndef COMMANDACTIONHELPER_H_ +#define COMMANDACTIONHELPER_H_ + +#include "ActionMessage.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerializeIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../ipc/MessageQueueIF.h" + +class CommandsActionsIF; + +class CommandActionHelper { + friend class CommandsActionsIF; +public: + CommandActionHelper(CommandsActionsIF* owner); + virtual ~CommandActionHelper(); + ReturnValue_t commandAction(object_id_t commandTo, + ActionId_t actionId, const uint8_t* data, uint32_t size); + ReturnValue_t commandAction(object_id_t commandTo, + ActionId_t actionId, SerializeIF* data); + ReturnValue_t initialize(); + ReturnValue_t handleReply(CommandMessage* reply); + uint8_t getCommandCount() const; +private: + CommandsActionsIF* owner; + MessageQueueIF* queueToUse; + StorageManagerIF* ipcStore; + uint8_t commandCount; + MessageQueueId_t lastTarget; + void extractDataForOwner(ActionId_t actionId, store_address_t storeId); + ReturnValue_t sendCommand(MessageQueueId_t queueId, ActionId_t actionId, + store_address_t storeId); +}; + +#endif /* COMMANDACTIONHELPER_H_ */ diff --git a/fsfw/action/CommandsActionsIF.h b/fsfw/action/CommandsActionsIF.h new file mode 100644 index 0000000..491bfc7 --- /dev/null +++ b/fsfw/action/CommandsActionsIF.h @@ -0,0 +1,37 @@ +#ifndef FSFW_ACTION_COMMANDSACTIONSIF_H_ +#define FSFW_ACTION_COMMANDSACTIONSIF_H_ + +#include "CommandActionHelper.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MessageQueueIF.h" + +/** + * Interface to separate commanding actions of other objects. + * In next iteration, IF should be shortened to three calls: + * - dataReceived(data) + * - successReceived(id, step) + * - failureReceived(id, step, cause) + * or even + * - replyReceived(id, step, cause) (if cause == OK, it's a success). + */ +class CommandsActionsIF { + friend class CommandActionHelper; +public: + static const uint8_t INTERFACE_ID = CLASS_ID::COMMANDS_ACTIONS_IF; + static const ReturnValue_t OBJECT_HAS_NO_FUNCTIONS = MAKE_RETURN_CODE(1); + static const ReturnValue_t ALREADY_COMMANDING = MAKE_RETURN_CODE(2); + virtual ~CommandsActionsIF() {} + virtual MessageQueueIF* getCommandQueuePtr() = 0; +protected: + virtual void stepSuccessfulReceived(ActionId_t actionId, uint8_t step) = 0; + virtual void stepFailedReceived(ActionId_t actionId, uint8_t step, + ReturnValue_t returnCode) = 0; + virtual void dataReceived(ActionId_t actionId, const uint8_t* data, + uint32_t size) = 0; + virtual void completionSuccessfulReceived(ActionId_t actionId) = 0; + virtual void completionFailedReceived(ActionId_t actionId, + ReturnValue_t returnCode) = 0; +}; + + +#endif /* FSFW_ACTION_COMMANDSACTIONSIF_H_ */ diff --git a/fsfw/action/HasActionsIF.h b/fsfw/action/HasActionsIF.h new file mode 100644 index 0000000..690f036 --- /dev/null +++ b/fsfw/action/HasActionsIF.h @@ -0,0 +1,63 @@ +#ifndef FSFW_ACTION_HASACTIONSIF_H_ +#define FSFW_ACTION_HASACTIONSIF_H_ + +#include "ActionHelper.h" +#include "ActionMessage.h" +#include "SimpleActionHelper.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MessageQueueIF.h" + +/** + * @brief + * Interface for component which uses actions + * + * @details + * This interface is used to execute actions in the component. Actions, in the + * sense of this interface, are activities with a well-defined beginning and + * end in time. They may adjust sub-states of components, but are not supposed + * to change the main mode of operation, which is handled with the HasModesIF + * described below. + * + * The HasActionsIF allows components to define such actions and make them + * available for other components to use. Implementing the interface is + * straightforward: There’s a single executeAction call, which provides an + * identifier for the action to execute, as well as arbitrary parameters for + * input. + * Aside from direct, software-based actions, it is used in device handler + * components as an interface to forward commands to devices. + * Implementing components of the interface are supposed to check identifier + * (ID) and parameters and immediately start execution of the action. + * It is, however, not required to immediately finish execution. + * Instead, this may be deferred to a later point in time, at which the + * component needs to inform the caller about finished or failed execution. + * + * @ingroup interfaces + */ +class HasActionsIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::HAS_ACTIONS_IF; + static const ReturnValue_t IS_BUSY = MAKE_RETURN_CODE(1); + static const ReturnValue_t INVALID_PARAMETERS = MAKE_RETURN_CODE(2); + static const ReturnValue_t EXECUTION_FINISHED = MAKE_RETURN_CODE(3); + static const ReturnValue_t INVALID_ACTION_ID = MAKE_RETURN_CODE(4); + virtual ~HasActionsIF() { } + /** + * Function to get the MessageQueueId_t of the implementing object + * @return MessageQueueId_t of the object + */ + virtual MessageQueueId_t getCommandQueue() const = 0; + /** + * Execute or initialize the execution of a certain function. + * The ActionHelpers will execute this function and behave differently + * depending on the returnvalue. + * + * @return + * -@c EXECUTION_FINISHED Finish reply will be generated + * -@c Not RETURN_OK Step failure reply will be generated + */ + virtual ReturnValue_t executeAction(ActionId_t actionId, + MessageQueueId_t commandedBy, const uint8_t* data, size_t size) = 0; +}; + + +#endif /* FSFW_ACTION_HASACTIONSIF_H_ */ diff --git a/fsfw/action/SimpleActionHelper.cpp b/fsfw/action/SimpleActionHelper.cpp new file mode 100644 index 0000000..af57736 --- /dev/null +++ b/fsfw/action/SimpleActionHelper.cpp @@ -0,0 +1,75 @@ +#include "HasActionsIF.h" +#include "SimpleActionHelper.h" + +SimpleActionHelper::SimpleActionHelper(HasActionsIF* setOwner, + MessageQueueIF* useThisQueue) : + ActionHelper(setOwner, useThisQueue), isExecuting(false) { +} + +SimpleActionHelper::~SimpleActionHelper() { +} + +void SimpleActionHelper::step(ReturnValue_t result) { + // STEP_OFFESET is subtracted to compensate for adding offset in base + // method, which is not necessary here. + ActionHelper::step(stepCount - STEP_OFFSET, lastCommander, lastAction, + result); + if (result != HasReturnvaluesIF::RETURN_OK) { + resetHelper(); + } +} + +void SimpleActionHelper::finish(ReturnValue_t result) { + ActionHelper::finish(lastCommander, lastAction, result); + resetHelper(); +} + +ReturnValue_t SimpleActionHelper::reportData(SerializeIF* data) { + return ActionHelper::reportData(lastCommander, lastAction, data); +} + +void SimpleActionHelper::resetHelper() { + stepCount = 0; + isExecuting = false; + lastAction = 0; + lastCommander = 0; +} + +void SimpleActionHelper::prepareExecution(MessageQueueId_t commandedBy, + ActionId_t actionId, store_address_t dataAddress) { + CommandMessage reply; + if (isExecuting) { + ipcStore->deleteData(dataAddress); + ActionMessage::setStepReply(&reply, actionId, 0, + HasActionsIF::IS_BUSY); + queueToUse->sendMessage(commandedBy, &reply); + } + const uint8_t* dataPtr = NULL; + size_t size = 0; + ReturnValue_t result = ipcStore->getData(dataAddress, &dataPtr, &size); + if (result != HasReturnvaluesIF::RETURN_OK) { + ActionMessage::setStepReply(&reply, actionId, 0, result); + queueToUse->sendMessage(commandedBy, &reply); + return; + } + lastCommander = commandedBy; + lastAction = actionId; + result = owner->executeAction(actionId, commandedBy, dataPtr, size); + ipcStore->deleteData(dataAddress); + switch (result) { + case HasReturnvaluesIF::RETURN_OK: + isExecuting = true; + stepCount++; + break; + case HasActionsIF::EXECUTION_FINISHED: + ActionMessage::setCompletionReply(&reply, actionId, + HasReturnvaluesIF::RETURN_OK); + queueToUse->sendMessage(commandedBy, &reply); + break; + default: + ActionMessage::setStepReply(&reply, actionId, 0, result); + queueToUse->sendMessage(commandedBy, &reply); + break; + } + +} diff --git a/fsfw/action/SimpleActionHelper.h b/fsfw/action/SimpleActionHelper.h new file mode 100644 index 0000000..1f35d9f --- /dev/null +++ b/fsfw/action/SimpleActionHelper.h @@ -0,0 +1,30 @@ +#ifndef FSFW_ACTION_SIMPLEACTIONHELPER_H_ +#define FSFW_ACTION_SIMPLEACTIONHELPER_H_ + +#include "ActionHelper.h" + +/** + * @brief This is an action helper which is only able to service one action + * at a time but remembers last commander and last action which + * simplifies usage + */ +class SimpleActionHelper: public ActionHelper { +public: + SimpleActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue); + virtual ~SimpleActionHelper(); + void step(ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); + void finish(ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); + ReturnValue_t reportData(SerializeIF* data); + +protected: + void prepareExecution(MessageQueueId_t commandedBy, ActionId_t actionId, + store_address_t dataAddress); + virtual void resetHelper(); +private: + bool isExecuting; + MessageQueueId_t lastCommander = MessageQueueIF::NO_QUEUE; + ActionId_t lastAction = 0; + uint8_t stepCount = 0; +}; + +#endif /* SIMPLEACTIONHELPER_H_ */ diff --git a/fsfw/container/ArrayList.h b/fsfw/container/ArrayList.h new file mode 100644 index 0000000..6bd5c1d --- /dev/null +++ b/fsfw/container/ArrayList.h @@ -0,0 +1,253 @@ +#ifndef FSFW_CONTAINER_ARRAYLIST_H_ +#define FSFW_CONTAINER_ARRAYLIST_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerializeAdapter.h" +#include "../serialize/SerializeIF.h" + +/** + * @brief A List that stores its values in an array. + * @details + * The underlying storage is an array that can be allocated by the class + * itself or supplied via ctor. + * + * @ingroup container + */ +template +class ArrayList { + template friend class SerialArrayListAdapter; +public: + static const uint8_t INTERFACE_ID = CLASS_ID::ARRAY_LIST; + static const ReturnValue_t FULL = MAKE_RETURN_CODE(0x01); + + /** + * This is the allocating constructor. + * It allocates an array of the specified size. + * @param maxSize + */ + ArrayList(count_t maxSize) : + size(0), maxSize_(maxSize), allocated(true) { + entries = new T[maxSize]; + } + + /** + * This is the non-allocating constructor + * + * It expects a pointer to an array of a certain size and initializes + * itself to it. + * + * @param storage the array to use as backend + * @param maxSize size of storage + * @param size size of data already present in storage + */ + ArrayList(T *storage, count_t maxSize, count_t size = 0) : + size(size), entries(storage), maxSize_(maxSize), allocated(false) { + } + + /** + * Copying is forbiden by declaring copy ctor and copy assignment deleted + * It is too ambigous in this case. + * (Allocate a new backend? Use the same? What to do in an modifying call?) + */ + ArrayList(const ArrayList& other) = delete; + const ArrayList& operator=(const ArrayList& other) = delete; + + /** + * Number of Elements stored in this List + */ + count_t size; + + + /** + * Destructor, if the allocating constructor was used, it deletes the array. + */ + virtual ~ArrayList() { + if (allocated) { + delete[] entries; + } + } + + /** + * An Iterator to go trough an ArrayList + * + * It stores a pointer to an element and increments the + * pointer when incremented itself. + */ + class Iterator { + public: + /** + * Empty ctor, points to NULL + */ + Iterator(): value(0) {} + + /** + * Initializes the Iterator to point to an element + * + * @param initialize + */ + Iterator(T *initialize) { + value = initialize; + } + + /** + * The current element the iterator points to + */ + T *value; + + Iterator& operator++() { + value++; + return *this; + } + + Iterator operator++(int) { + Iterator tmp(*this); + operator++(); + return tmp; + } + + Iterator& operator--() { + value--; + return *this; + } + + Iterator operator--(int) { + Iterator tmp(*this); + operator--(); + return tmp; + } + + T& operator*() { + return *value; + } + + const T& operator*() const { + return *value; + } + + T *operator->() { + return value; + } + + const T *operator->() const { + return value; + } + }; + + friend bool operator==(const ArrayList::Iterator& lhs, + const ArrayList::Iterator& rhs) { + return (lhs.value == rhs.value); + } + + friend bool operator!=(const ArrayList::Iterator& lhs, + const ArrayList::Iterator& rhs) { + return not (lhs.value == rhs.value); + } + + /** + * Iterator pointing to the first stored elmement + * + * @return Iterator to the first element + */ + Iterator begin() const { + return Iterator(&entries[0]); + } + + /** + * returns an Iterator pointing to the element after the last stored entry + * + * @return Iterator to the element after the last entry + */ + Iterator end() const { + return Iterator(&entries[size]); + } + + T & operator[](count_t i) const { + return entries[i]; + } + + /** + * The first element + * + * @return pointer to the first stored element + */ + T *front() { + return entries; + } + + /** + * The last element + * + * does not return a valid pointer if called on an empty list. + * + * @return pointer to the last stored element + */ + T *back() { + return &entries[size - 1]; + //Alternative solution + //return const_cast(static_cast(*this).back()); + } + + const T* back() const{ + return &entries[size-1]; + } + + /** + * The maximum number of elements this List can contain + * + * @return maximum number of elements + */ + size_t maxSize() const { + return this->maxSize_; + } + + /** + * Insert a new element into the list. + * + * The new element is inserted after the last stored element. + * + * @param entry + * @return + * -@c FULL if the List is full + * -@c RETURN_OK else + */ + ReturnValue_t insert(T entry) { + if (size >= maxSize_) { + return FULL; + } + entries[size] = entry; + ++size; + return HasReturnvaluesIF::RETURN_OK; + } + + /** + * clear the List + * + * This does not actually clear all entries, it only sets the size to 0. + */ + void clear() { + size = 0; + } + + count_t remaining() { + return (maxSize_ - size); + } + +protected: + /** + * pointer to the array in which the entries are stored + */ + T *entries; + /** + * remembering the maximum size + */ + size_t maxSize_; + + /** + * true if the array was allocated and needs to be deleted in the destructor. + */ + bool allocated; +}; + + + +#endif /* FSFW_CONTAINER_ARRAYLIST_H_ */ diff --git a/fsfw/container/BinaryTree.h b/fsfw/container/BinaryTree.h new file mode 100644 index 0000000..73073de --- /dev/null +++ b/fsfw/container/BinaryTree.h @@ -0,0 +1,153 @@ +#ifndef FRAMEWORK_CONTAINER_BINARYTREE_H_ +#define FRAMEWORK_CONTAINER_BINARYTREE_H_ + +#include +#include +#include +template +class BinaryNode { +public: + BinaryNode(Tp* setValue) : + value(setValue), left(NULL), right(NULL), parent(NULL) { + } + Tp *value; + BinaryNode* left; + BinaryNode* right; + BinaryNode* parent; +}; + +template +class ExplicitNodeIterator { +public: + typedef ExplicitNodeIterator _Self; + typedef BinaryNode _Node; + typedef Tp value_type; + typedef Tp* pointer; + typedef Tp& reference; + ExplicitNodeIterator() : + element(NULL) { + } + ExplicitNodeIterator(_Node* node) : + element(node) { + } + BinaryNode* element; + _Self up() { + return _Self(element->parent); + } + _Self left() { + if (element != NULL) { + return _Self(element->left); + } else { + return _Self(NULL); + } + + } + _Self right() { + if (element != NULL) { + return _Self(element->right); + } else { + return _Self(NULL); + } + + } + bool operator==(const _Self& __x) const { + return element == __x.element; + } + bool operator!=(const _Self& __x) const { + return element != __x.element; + } + pointer + operator->() const { + if (element != NULL) { + return element->value; + } else { + return NULL; + } + } + pointer operator*() const { + return this->operator->(); + } +}; + +/** + * Pretty rudimentary version of a simple binary tree (not a binary search tree!). + */ +template +class BinaryTree { +public: + typedef ExplicitNodeIterator iterator; + typedef BinaryNode Node; + typedef std::pair children; + BinaryTree() : + rootNode(NULL) { + } + BinaryTree(Node* rootNode) : + rootNode(rootNode) { + } + iterator begin() const { + return iterator(rootNode); + } + static iterator end() { + return iterator(NULL); + } + iterator insert(bool insertLeft, iterator parentNode, Node* newNode ) { + newNode->parent = parentNode.element; + if (parentNode.element != NULL) { + if (insertLeft) { + parentNode.element->left = newNode; + } else { + parentNode.element->right = newNode; + } + } else { + //Insert first element. + rootNode = newNode; + } + return iterator(newNode); + } + //No recursion to children. Needs to be done externally. + children erase(iterator node) { + if (node.element == rootNode) { + //We're root node + rootNode = NULL; + } else { + //Delete parent's reference + if (node.up().left() == node) { + node.up().element->left = NULL; + } else { + node.up().element->right = NULL; + } + } + return children(node.element->left, node.element->right); + } + static uint32_t countLeft(iterator start) { + if (start == end()) { + return 0; + } + //We also count the start node itself. + uint32_t count = 1; + while (start.left() != end()) { + count++; + start = start.left(); + } + return count; + } + static uint32_t countRight(iterator start) { + if (start == end()) { + return 0; + } + //We also count the start node itself. + uint32_t count = 1; + while (start.right() != end()) { + count++; + start = start.right(); + } + return count; + } + +protected: + Node* rootNode; +}; + + + +#endif /* FRAMEWORK_CONTAINER_BINARYTREE_H_ */ diff --git a/fsfw/container/DynamicFIFO.h b/fsfw/container/DynamicFIFO.h new file mode 100644 index 0000000..86d43f7 --- /dev/null +++ b/fsfw/container/DynamicFIFO.h @@ -0,0 +1,55 @@ +#ifndef FSFW_CONTAINER_DYNAMICFIFO_H_ +#define FSFW_CONTAINER_DYNAMICFIFO_H_ + +#include "FIFOBase.h" +#include + +/** + * @brief Simple First-In-First-Out data structure. The maximum size + * can be set in the constructor. + * @details + * The maximum capacity can be determined at run-time, so this container + * performs dynamic memory allocation! + * The public interface of FIFOBase exposes the user interface for the FIFO. + * @tparam T Entry Type + * @tparam capacity Maximum capacity + */ +template +class DynamicFIFO: public FIFOBase { +public: + DynamicFIFO(size_t maxCapacity): FIFOBase(nullptr, maxCapacity), + fifoVector(maxCapacity) { + // trying to pass the pointer of the uninitialized vector + // to the FIFOBase constructor directly lead to a super evil bug. + // So we do it like this now. + this->setContainer(fifoVector.data()); + }; + + /** + * @brief Custom copy constructor which prevents setting the + * underlying pointer wrong. This function allocates memory! + * @details This is a very heavy operation so try to avoid this! + * + */ + DynamicFIFO(const DynamicFIFO& other): FIFOBase(other), + fifoVector(other.maxCapacity) { + this->fifoVector = other.fifoVector; + this->setContainer(fifoVector.data()); + } + + /** + * @brief Custom assignment operator + * @details This is a very heavy operation so try to avoid this! + * @param other DyamicFIFO to copy from + */ + DynamicFIFO& operator=(const DynamicFIFO& other){ + FIFOBase::operator=(other); + this->fifoVector = other.fifoVector; + this->setContainer(fifoVector.data()); + return *this; + } +private: + std::vector fifoVector; +}; + +#endif /* FSFW_CONTAINER_DYNAMICFIFO_H_ */ diff --git a/fsfw/container/FIFO.h b/fsfw/container/FIFO.h new file mode 100644 index 0000000..7011385 --- /dev/null +++ b/fsfw/container/FIFO.h @@ -0,0 +1,47 @@ +#ifndef FSFW_CONTAINER_FIFO_H_ +#define FSFW_CONTAINER_FIFO_H_ + +#include "FIFOBase.h" +#include + +/** + * @brief Simple First-In-First-Out data structure with size fixed at + * compile time + * @details + * Performs no dynamic memory allocation. + * The public interface of FIFOBase exposes the user interface for the FIFO. + * @tparam T Entry Type + * @tparam capacity Maximum capacity + */ +template +class FIFO: public FIFOBase { +public: + FIFO(): FIFOBase(nullptr, capacity) { + this->setContainer(fifoArray.data()); + }; + + /** + * @brief Custom copy constructor to set pointer correctly. + * @param other + */ + FIFO(const FIFO& other): FIFOBase(other) { + this->fifoArray = other.fifoArray; + this->setContainer(fifoArray.data()); + } + + /** + * @brief Custom assignment operator + * @param other + */ + FIFO& operator=(const FIFO& other){ + FIFOBase::operator=(other); + this->fifoArray = other.fifoArray; + this->setContainer(fifoArray.data()); + return *this; + } + +private: + std::array fifoArray; +}; + +#endif /* FSFW_CONTAINER_FIFO_H_ */ diff --git a/fsfw/container/FIFOBase.h b/fsfw/container/FIFOBase.h new file mode 100644 index 0000000..edd66d3 --- /dev/null +++ b/fsfw/container/FIFOBase.h @@ -0,0 +1,79 @@ +#ifndef FSFW_CONTAINER_FIFOBASE_H_ +#define FSFW_CONTAINER_FIFOBASE_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include +#include + +template +class FIFOBase { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::FIFO_CLASS; + static const ReturnValue_t FULL = MAKE_RETURN_CODE(1); + static const ReturnValue_t EMPTY = MAKE_RETURN_CODE(2); + + /** Default ctor, takes pointer to first entry of underlying container + * and maximum capacity */ + FIFOBase(T* values, const size_t maxCapacity); + + /** + * Insert value into FIFO + * @param value + * @return RETURN_OK on success, FULL if full + */ + ReturnValue_t insert(T value); + /** + * Retrieve item from FIFO. This removes the item from the FIFO. + * @param value Must point to a valid T + * @return RETURN_OK on success, EMPTY if empty and FAILED if nullptr check failed + */ + ReturnValue_t retrieve(T *value); + /** + * Retrieve item from FIFO without removing it from FIFO. + * @param value Must point to a valid T + * @return RETURN_OK on success, EMPTY if empty and FAILED if nullptr check failed + */ + ReturnValue_t peek(T * value); + /** + * Remove item from FIFO. + * @return RETURN_OK on success, EMPTY if empty + */ + ReturnValue_t pop(); + + /*** + * Check if FIFO is empty + * @return True if empty, False if not + */ + bool empty(); + /*** + * Check if FIFO is Full + * @return True if full, False if not + */ + bool full(); + /*** + * Current used size (elements) used + * @return size_t in elements + */ + size_t size(); + /*** + * Get maximal capacity of fifo + * @return size_t with max capacity of this fifo + */ + size_t getMaxCapacity() const; + +protected: + void setContainer(T* data); + size_t maxCapacity = 0; + + T* values; + + size_t readIndex = 0; + size_t writeIndex = 0; + size_t currentSize = 0; + + size_t next(size_t current); +}; + +#include "FIFOBase.tpp" + +#endif /* FSFW_CONTAINER_FIFOBASE_H_ */ diff --git a/fsfw/container/FIFOBase.tpp b/fsfw/container/FIFOBase.tpp new file mode 100644 index 0000000..763004b --- /dev/null +++ b/fsfw/container/FIFOBase.tpp @@ -0,0 +1,93 @@ +#ifndef FSFW_CONTAINER_FIFOBASE_TPP_ +#define FSFW_CONTAINER_FIFOBASE_TPP_ + +#ifndef FSFW_CONTAINER_FIFOBASE_H_ +#error Include FIFOBase.h before FIFOBase.tpp! +#endif + +template +inline FIFOBase::FIFOBase(T* values, const size_t maxCapacity): + maxCapacity(maxCapacity), values(values){}; + +template +inline ReturnValue_t FIFOBase::insert(T value) { + if (full()) { + return FULL; + } else { + values[writeIndex] = value; + writeIndex = next(writeIndex); + ++currentSize; + return HasReturnvaluesIF::RETURN_OK; + } +}; + +template +inline ReturnValue_t FIFOBase::retrieve(T* value) { + if (empty()) { + return EMPTY; + } else { + if (value == nullptr){ + return HasReturnvaluesIF::RETURN_FAILED; + } + *value = values[readIndex]; + readIndex = next(readIndex); + --currentSize; + return HasReturnvaluesIF::RETURN_OK; + } +}; + +template +inline ReturnValue_t FIFOBase::peek(T* value) { + if(empty()) { + return EMPTY; + } else { + if (value == nullptr){ + return HasReturnvaluesIF::RETURN_FAILED; + } + *value = values[readIndex]; + return HasReturnvaluesIF::RETURN_OK; + } +}; + +template +inline ReturnValue_t FIFOBase::pop() { + T value; + return this->retrieve(&value); +}; + +template +inline bool FIFOBase::empty() { + return (currentSize == 0); +}; + +template +inline bool FIFOBase::full() { + return (currentSize == maxCapacity); +} + +template +inline size_t FIFOBase::size() { + return currentSize; +} + +template +inline size_t FIFOBase::next(size_t current) { + ++current; + if (current == maxCapacity) { + current = 0; + } + return current; +} + +template +inline size_t FIFOBase::getMaxCapacity() const { + return maxCapacity; +} + + +template +inline void FIFOBase::setContainer(T *data) { + this->values = data; +} + +#endif diff --git a/fsfw/container/FixedArrayList.h b/fsfw/container/FixedArrayList.h new file mode 100644 index 0000000..e9e127c --- /dev/null +++ b/fsfw/container/FixedArrayList.h @@ -0,0 +1,38 @@ +#ifndef FIXEDARRAYLIST_H_ +#define FIXEDARRAYLIST_H_ + +#include "ArrayList.h" +#include +/** + * \ingroup container + */ +template +class FixedArrayList: public ArrayList { + static_assert(MAX_SIZE <= (pow(2,sizeof(count_t)*8)-1), "count_t is not large enough to hold MAX_SIZE"); +private: + T data[MAX_SIZE]; +public: + FixedArrayList() : + ArrayList(data, MAX_SIZE) { + } + + FixedArrayList(const FixedArrayList& other) : + ArrayList(data, MAX_SIZE) { + memcpy(this->data, other.data, sizeof(this->data)); + this->entries = data; + this->size = other.size; + } + + FixedArrayList& operator=(FixedArrayList other) { + memcpy(this->data, other.data, sizeof(this->data)); + this->entries = data; + this->size = other.size; + return *this; + } + + virtual ~FixedArrayList() { + } + +}; + +#endif /* FIXEDARRAYLIST_H_ */ diff --git a/fsfw/container/FixedMap.h b/fsfw/container/FixedMap.h new file mode 100644 index 0000000..7a5220f --- /dev/null +++ b/fsfw/container/FixedMap.h @@ -0,0 +1,230 @@ +#ifndef FSFW_CONTAINER_FIXEDMAP_H_ +#define FSFW_CONTAINER_FIXEDMAP_H_ + +#include "ArrayList.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include +#include + +/** + * @brief Map implementation for maps with a pre-defined size. + * @details + * Can be initialized with desired maximum size. + * Iterator is used to access pair and iterate through map entries. + * Complexity O(n). + * @warning Iterators return a non-const key_t in the pair. + * @warning A User is not allowed to change the key, otherwise the map is corrupted. + * @ingroup container + */ +template +class FixedMap: public SerializeIF { + static_assert (std::is_trivially_copyable::value or + std::is_base_of::value, + "Types used in FixedMap must either be trivial copy-able or a " + "derived class from SerializeIF to be serialize-able"); +public: + static const uint8_t INTERFACE_ID = CLASS_ID::FIXED_MAP; + static const ReturnValue_t KEY_ALREADY_EXISTS = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t MAP_FULL = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t KEY_DOES_NOT_EXIST = MAKE_RETURN_CODE(0x03); + +private: + static const key_t EMPTY_SLOT = -1; + ArrayList, uint32_t> theMap; + uint32_t _size; + + uint32_t findIndex(key_t key) const { + if (_size == 0) { + return 1; + } + uint32_t i = 0; + for (i = 0; i < _size; ++i) { + if (theMap[i].first == key) { + return i; + } + } + return i; + } +public: + FixedMap(uint32_t maxSize) : + theMap(maxSize), _size(0) { + } + + class Iterator: public ArrayList, uint32_t>::Iterator { + public: + Iterator() : + ArrayList, uint32_t>::Iterator() { + } + + Iterator(std::pair *pair) : + ArrayList, uint32_t>::Iterator(pair) { + } + }; + + friend bool operator==(const typename FixedMap::Iterator& lhs, + const typename FixedMap::Iterator& rhs) { + return (lhs.value == rhs.value); + } + + friend bool operator!=(const typename FixedMap::Iterator& lhs, + const typename FixedMap::Iterator& rhs) { + return not (lhs.value == rhs.value); + } + + Iterator begin() const { + return Iterator(&theMap[0]); + } + + Iterator end() const { + return Iterator(&theMap[_size]); + } + + uint32_t size() const { + return _size; + } + + ReturnValue_t insert(key_t key, T value, Iterator *storedValue = nullptr) { + if (exists(key) == HasReturnvaluesIF::RETURN_OK) { + return KEY_ALREADY_EXISTS; + } + if (_size == theMap.maxSize()) { + return MAP_FULL; + } + theMap[_size].first = key; + theMap[_size].second = value; + if (storedValue != nullptr) { + *storedValue = Iterator(&theMap[_size]); + } + ++_size; + return HasReturnvaluesIF::RETURN_OK; + } + + ReturnValue_t insert(std::pair pair) { + return insert(pair.first, pair.second); + } + + ReturnValue_t exists(key_t key) const { + ReturnValue_t result = KEY_DOES_NOT_EXIST; + if (findIndex(key) < _size) { + result = HasReturnvaluesIF::RETURN_OK; + } + return result; + } + + ReturnValue_t erase(Iterator *iter) { + uint32_t i; + if ((i = findIndex((*iter).value->first)) >= _size) { + return KEY_DOES_NOT_EXIST; + } + theMap[i] = theMap[_size - 1]; + --_size; + --((*iter).value); + return HasReturnvaluesIF::RETURN_OK; + } + + ReturnValue_t erase(key_t key) { + uint32_t i; + if ((i = findIndex(key)) >= _size) { + return KEY_DOES_NOT_EXIST; + } + theMap[i] = theMap[_size - 1]; + --_size; + return HasReturnvaluesIF::RETURN_OK; + } + + T *findValue(key_t key) const { + return &theMap[findIndex(key)].second; + } + + Iterator find(key_t key) const { + ReturnValue_t result = exists(key); + if (result != HasReturnvaluesIF::RETURN_OK) { + return end(); + } + return Iterator(&theMap[findIndex(key)]); + } + + ReturnValue_t find(key_t key, T **value) const { + ReturnValue_t result = exists(key); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + *value = &theMap[findIndex(key)].second; + return HasReturnvaluesIF::RETURN_OK; + } + + bool empty() { + if(_size == 0) { + return true; + } + else { + return false; + } + } + + bool full() { + if(_size >= theMap.maxSize()) { + return true; + } + else { + return false; + } + } + + void clear() { + _size = 0; + } + + uint32_t maxSize() const { + return theMap.maxSize(); + } + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = SerializeAdapter::serialize(&this->_size, + buffer, size, maxSize, streamEndianness); + uint32_t i = 0; + while ((result == HasReturnvaluesIF::RETURN_OK) && (i < this->_size)) { + result = SerializeAdapter::serialize(&theMap[i].first, buffer, + size, maxSize, streamEndianness); + result = SerializeAdapter::serialize(&theMap[i].second, buffer, size, + maxSize, streamEndianness); + ++i; + } + return result; + } + + virtual size_t getSerializedSize() const { + uint32_t printSize = sizeof(_size); + uint32_t i = 0; + + for (i = 0; i < _size; ++i) { + printSize += SerializeAdapter::getSerializedSize( + &theMap[i].first); + printSize += SerializeAdapter::getSerializedSize(&theMap[i].second); + } + + return printSize; + } + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + ReturnValue_t result = SerializeAdapter::deSerialize(&this->_size, + buffer, size, streamEndianness); + if (this->_size > theMap.maxSize()) { + return SerializeIF::TOO_MANY_ELEMENTS; + } + uint32_t i = 0; + while ((result == HasReturnvaluesIF::RETURN_OK) && (i < this->_size)) { + result = SerializeAdapter::deSerialize(&theMap[i].first, buffer, + size, streamEndianness); + result = SerializeAdapter::deSerialize(&theMap[i].second, buffer, size, + streamEndianness); + ++i; + } + return result; + } + +}; + +#endif /* FSFW_CONTAINER_FIXEDMAP_H_ */ diff --git a/fsfw/container/FixedOrderedMultimap.h b/fsfw/container/FixedOrderedMultimap.h new file mode 100644 index 0000000..96bc007 --- /dev/null +++ b/fsfw/container/FixedOrderedMultimap.h @@ -0,0 +1,206 @@ +#ifndef FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_ +#define FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_ + +#include "ArrayList.h" +#include + +/** + * @brief An associative container which allows multiple entries of the same key. + * @details + * Same keys are ordered by KEY_COMPARE function which is std::less > by default. + * + * It uses the ArrayList, so technically this is not a real map, it is an array of pairs + * of type key_t, T. It is ordered by key_t as FixedMap but allows same keys. Thus it has a linear + * complexity O(n). As long as the number of entries remains low, this + * should not be an issue. + * The number of insertion and deletion operation should be minimized + * as those incur extensive memory move operations (the underlying container + * is not node based). + * + * Its of fixed size so no allocations are performed after the construction. + * + * The maximum size is given as first parameter of the constructor. + * + * It provides an iterator to do list iterations. + * + * The type T must have a copy constructor if it is not trivial copy-able. + * + * @warning Iterators return a non-const key_t in the pair. + * @warning A User is not allowed to change the key, otherwise the map is corrupted. + * + * \ingroup container + */ +template> +class FixedOrderedMultimap { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::FIXED_MULTIMAP; + static const ReturnValue_t MAP_FULL = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t KEY_DOES_NOT_EXIST = MAKE_RETURN_CODE(0x02); + + /*** + * Constructor which needs a size_t for the maximum allowed size + * + * Can not be resized during runtime + * + * Allocates memory at construction + * @param maxSize size_t of Maximum allowed size + */ + FixedOrderedMultimap(size_t maxSize):theMap(maxSize), _size(0){ + } + + /*** + * Virtual destructor frees Memory by deleting its member + */ + virtual ~FixedOrderedMultimap() { + } + + /*** + * Special iterator for FixedOrderedMultimap + */ + class Iterator: public ArrayList, size_t>::Iterator { + public: + Iterator() : + ArrayList, size_t>::Iterator() { + } + + Iterator(std::pair *pair) : + ArrayList, size_t>::Iterator(pair) { + } + }; + + /*** + * Returns an iterator pointing to the first element + * @return Iterator pointing to first element + */ + Iterator begin() const { + return Iterator(&theMap[0]); + } + + /** + * Returns an iterator pointing to one element past the end + * @return Iterator pointing to one element past the end + */ + Iterator end() const { + return Iterator(&theMap[_size]); + } + + /*** + * Returns the current size of the map (not maximum size!) + * @return Current size + */ + size_t size() const{ + return _size; + } + + /** + * Clears the map, does not deallocate any memory + */ + void clear(){ + _size = 0; + } + + /** + * Returns the maximum size of the map + * @return Maximum size of the map + */ + size_t maxSize() const{ + return theMap.maxSize(); + } + + /*** + * Used to insert a key and value separately. + * + * @param[in] key Key of the new element + * @param[in] value Value of the new element + * @param[in/out] (optional) storedValue On success this points to the new value, otherwise a nullptr + * @return RETURN_OK if insert was successful, MAP_FULL if no space is available + */ + ReturnValue_t insert(key_t key, T value, Iterator *storedValue = nullptr); + + /*** + * Used to insert new pair instead of single values + * + * @param pair Pair to be inserted + * @return RETURN_OK if insert was successful, MAP_FULL if no space is available + */ + ReturnValue_t insert(std::pair pair); + + /*** + * Can be used to check if a certain key is in the map + * @param key Key to be checked + * @return RETURN_OK if the key exists KEY_DOES_NOT_EXIST otherwise + */ + ReturnValue_t exists(key_t key) const; + + /*** + * Used to delete the element in the iterator + * + * The iterator will point to the element before or begin(), + * but never to one element in front of the map. + * + * @warning The iterator needs to be valid and dereferenceable + * @param[in/out] iter Pointer to iterator to the element that needs to be ereased + * @return RETURN_OK if erased, KEY_DOES_NOT_EXIST if the there is no element like this + */ + ReturnValue_t erase(Iterator *iter); + + /*** + * Used to erase by key + * @param key Key to be erased + * @return RETURN_OK if erased, KEY_DOES_NOT_EXIST if the there is no element like this + */ + ReturnValue_t erase(key_t key); + + /*** + * Find returns the first appearance of the key + * + * If the key does not exist, it points to end() + * + * @param key Key to search for + * @return Iterator pointing to the first entry of key + */ + Iterator find(key_t key) const{ + ReturnValue_t result = exists(key); + if (result != HasReturnvaluesIF::RETURN_OK) { + return end(); + } + return Iterator(&theMap[findFirstIndex(key)]); + }; + + /*** + * Finds first entry of the given key and returns a + * pointer to the value + * + * @param key Key to search for + * @param value Found value + * @return RETURN_OK if it points to the value, + * KEY_DOES_NOT_EXIST if the key is not in the map + */ + ReturnValue_t find(key_t key, T **value) const; + + friend bool operator==(const typename FixedOrderedMultimap::Iterator& lhs, + const typename FixedOrderedMultimap::Iterator& rhs) { + return (lhs.value == rhs.value); + } + + friend bool operator!=(const typename FixedOrderedMultimap::Iterator& lhs, + const typename FixedOrderedMultimap::Iterator& rhs) { + return not (lhs.value == rhs.value); + } + +private: + typedef KEY_COMPARE compare; + compare myComp; + ArrayList, size_t> theMap; + size_t _size; + + size_t findFirstIndex(key_t key, size_t startAt = 0) const; + + size_t findNicePlace(key_t key) const; + + void removeFromPosition(size_t position); +}; + +#include "FixedOrderedMultimap.tpp" + +#endif /* FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_ */ diff --git a/fsfw/container/FixedOrderedMultimap.tpp b/fsfw/container/FixedOrderedMultimap.tpp new file mode 100644 index 0000000..4aa85e9 --- /dev/null +++ b/fsfw/container/FixedOrderedMultimap.tpp @@ -0,0 +1,109 @@ +#ifndef FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_TPP_ +#define FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_TPP_ + + +template +inline ReturnValue_t FixedOrderedMultimap::insert(key_t key, T value, Iterator *storedValue) { + if (_size == theMap.maxSize()) { + return MAP_FULL; + } + size_t position = findNicePlace(key); + memmove(static_cast(&theMap[position + 1]),static_cast(&theMap[position]), + (_size - position) * sizeof(std::pair)); + theMap[position].first = key; + theMap[position].second = value; + ++_size; + if (storedValue != nullptr) { + *storedValue = Iterator(&theMap[position]); + } + return HasReturnvaluesIF::RETURN_OK; +} +template +inline ReturnValue_t FixedOrderedMultimap::insert(std::pair pair) { + return insert(pair.first, pair.second); +} + +template +inline ReturnValue_t FixedOrderedMultimap::exists(key_t key) const { + ReturnValue_t result = KEY_DOES_NOT_EXIST; + if (findFirstIndex(key) < _size) { + result = HasReturnvaluesIF::RETURN_OK; + } + return result; +} + +template +inline ReturnValue_t FixedOrderedMultimap::erase(Iterator *iter) { + size_t i; + if ((i = findFirstIndex((*iter).value->first)) >= _size) { + return KEY_DOES_NOT_EXIST; + } + removeFromPosition(i); + if (*iter != begin()) { + (*iter)--; + } else { + *iter = begin(); + } + return HasReturnvaluesIF::RETURN_OK; +} + +template +inline ReturnValue_t FixedOrderedMultimap::erase(key_t key) { + size_t i; + if ((i = findFirstIndex(key)) >= _size) { + return KEY_DOES_NOT_EXIST; + } + do { + removeFromPosition(i); + i = findFirstIndex(key, i); + } while (i < _size); + return HasReturnvaluesIF::RETURN_OK; +} + +template +inline ReturnValue_t FixedOrderedMultimap::find(key_t key, T **value) const { + ReturnValue_t result = exists(key); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + *value = &theMap[findFirstIndex(key)].second; + return HasReturnvaluesIF::RETURN_OK; +} + +template +inline size_t FixedOrderedMultimap::findFirstIndex(key_t key, size_t startAt) const { + if (startAt >= _size) { + return startAt + 1; + } + size_t i = startAt; + for (i = startAt; i < _size; ++i) { + if (theMap[i].first == key) { + return i; + } + } + return i; +} + +template +inline size_t FixedOrderedMultimap::findNicePlace(key_t key) const { + size_t i = 0; + for (i = 0; i < _size; ++i) { + if (myComp(key, theMap[i].first)) { + return i; + } + } + return i; +} + +template +inline void FixedOrderedMultimap::removeFromPosition(size_t position) { + if (_size <= position) { + return; + } + memmove(static_cast(&theMap[position]), static_cast(&theMap[position + 1]), + (_size - position - 1) * sizeof(std::pair)); + --_size; +} + + +#endif /* FRAMEWORK_CONTAINER_FIXEDORDEREDMULTIMAP_TPP_ */ diff --git a/fsfw/container/HybridIterator.h b/fsfw/container/HybridIterator.h new file mode 100644 index 0000000..8d020cb --- /dev/null +++ b/fsfw/container/HybridIterator.h @@ -0,0 +1,90 @@ +#ifndef FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_ +#define FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_ + +#include "ArrayList.h" +#include "SinglyLinkedList.h" + +template +class HybridIterator: public LinkedElement::Iterator, + public ArrayList::Iterator { +public: + HybridIterator() {} + + HybridIterator(typename LinkedElement::Iterator *iter) : + LinkedElement::Iterator(*iter), value(iter->value), + linked(true) { + + } + + HybridIterator(LinkedElement *start) : + LinkedElement::Iterator(start), value(start->value), + linked(true) { + + } + + HybridIterator(typename ArrayList::Iterator start, + typename ArrayList::Iterator end) : + ArrayList::Iterator(start), value(start.value), + linked(false), end(end.value) { + if (value == this->end) { + value = NULL; + } + } + + HybridIterator(T *firstElement, T *lastElement) : + ArrayList::Iterator(firstElement), value(firstElement), + linked(false), end(++lastElement) { + if (value == end) { + value = NULL; + } + } + + HybridIterator& operator++() { + if (linked) { + LinkedElement::Iterator::operator++(); + if (LinkedElement::Iterator::value != nullptr) { + value = LinkedElement::Iterator::value->value; + } else { + value = nullptr; + } + } else { + ArrayList::Iterator::operator++(); + value = ArrayList::Iterator::value; + + if (value == end) { + value = nullptr; + } + } + return *this; + } + + HybridIterator operator++(int) { + HybridIterator tmp(*this); + operator++(); + return tmp; + } + + bool operator==(const HybridIterator& other) const { + return value == other.value; + } + + bool operator!=(const HybridIterator& other) const { + return !(*this == other); + } + + T operator*() { + return *value; + } + + T *operator->() { + return value; + } + + T* value = nullptr; + +private: + bool linked = false; + T *end = nullptr; +}; + +#endif /* FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_ */ diff --git a/fsfw/container/IndexedRingMemoryArray.h b/fsfw/container/IndexedRingMemoryArray.h new file mode 100644 index 0000000..0d85b49 --- /dev/null +++ b/fsfw/container/IndexedRingMemoryArray.h @@ -0,0 +1,700 @@ +#ifndef FRAMEWORK_CONTAINER_INDEXEDRINGMEMORY_H_ +#define FRAMEWORK_CONTAINER_INDEXEDRINGMEMORY_H_ + +#include "ArrayList.h" +#include "../globalfunctions/CRC.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerialArrayListAdapter.h" +#include + +template +class Index: public SerializeIF{ + /** + * Index is the Type used for the list of indices. The template parameter is the type which describes the index, it needs to be a child of SerializeIF to be able to make it persistent + */ + static_assert(std::is_base_of::value,"Wrong Type for Index, Type must implement SerializeIF"); +public: + Index():blockStartAddress(0),size(0),storedPackets(0){} + + Index(uint32_t startAddress):blockStartAddress(startAddress),size(0),storedPackets(0){ + + } + + void setBlockStartAddress(uint32_t newAddress){ + this->blockStartAddress = newAddress; + } + + uint32_t getBlockStartAddress() const { + return blockStartAddress; + } + + const T* getIndexType() const { + return &indexType; + } + + T* modifyIndexType(){ + return &indexType; + } + /** + * Updates the index Type. Uses = operator + * @param indexType Type to copy from + */ + void setIndexType(T* indexType) { + this->indexType = *indexType; + } + + uint32_t getSize() const { + return size; + } + + void setSize(uint32_t size) { + this->size = size; + } + + void addSize(uint32_t size){ + this->size += size; + } + + void setStoredPackets(uint32_t newStoredPackets){ + this->storedPackets = newStoredPackets; + } + + void addStoredPackets(uint32_t packets){ + this->storedPackets += packets; + } + + uint32_t getStoredPackets() const{ + return this->storedPackets; + } + + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = SerializeAdapter::serialize(&blockStartAddress,buffer,size,maxSize,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = indexType.serialize(buffer,size,maxSize,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::serialize(&this->size,buffer,size,maxSize,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::serialize(&this->storedPackets,buffer,size,maxSize,streamEndianness); + return result; + } + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness){ + ReturnValue_t result = SerializeAdapter::deSerialize(&blockStartAddress,buffer,size,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = indexType.deSerialize(buffer,size,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::deSerialize(&this->size,buffer,size,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::deSerialize(&this->storedPackets,buffer,size,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + return result; + } + + size_t getSerializedSize() const { + uint32_t size = SerializeAdapter::getSerializedSize(&blockStartAddress); + size += indexType.getSerializedSize(); + size += SerializeAdapter::getSerializedSize(&this->size); + size += SerializeAdapter::getSerializedSize(&this->storedPackets); + return size; + } + + + bool operator==(const Index& other){ + return ((blockStartAddress == other.getBlockStartAddress()) && (size==other.getSize())) && (indexType == *(other.getIndexType())); + } + +private: + uint32_t blockStartAddress; + uint32_t size; + uint32_t storedPackets; + T indexType; +}; + + + +template +class IndexedRingMemoryArray: public SerializeIF, public ArrayList, uint32_t>{ + /** + * Indexed Ring Memory Array is a class for a ring memory with indices. It assumes that the newest data comes in last + * It uses the currentWriteBlock as pointer to the current writing position + * The currentReadBlock must be set manually + */ +public: + IndexedRingMemoryArray(uint32_t startAddress, uint32_t size, uint32_t bytesPerBlock, SerializeIF* additionalInfo, + bool overwriteOld) :ArrayList,uint32_t>(NULL,(uint32_t)10,(uint32_t)0),totalSize(size),indexAddress(startAddress),currentReadSize(0),currentReadBlockSizeCached(0),lastBlockToReadSize(0), additionalInfo(additionalInfo),overwriteOld(overwriteOld){ + + //Calculate the maximum number of indices needed for this blocksize + uint32_t maxNrOfIndices = floor(static_cast(size)/static_cast(bytesPerBlock)); + + //Calculate the Size needeed for the index itself + uint32_t serializedSize = 0; + if(additionalInfo!=NULL){ + serializedSize += additionalInfo->getSerializedSize(); + } + //Size of current iterator type + Index tempIndex; + serializedSize += tempIndex.getSerializedSize(); + + //Add Size of Array + serializedSize += sizeof(uint32_t); //size of array + serializedSize += (tempIndex.getSerializedSize() * maxNrOfIndices); //size of elements + serializedSize += sizeof(uint16_t); //size of crc + + //Calculate new size after index + if(serializedSize > totalSize){ + error << "IndexedRingMemory: Store is too small for index" << std::endl; + } + uint32_t useableSize = totalSize - serializedSize; + //Update the totalSize for calculations + totalSize = useableSize; + + //True StartAddress + uint32_t trueStartAddress = indexAddress + serializedSize; + + //Calculate True number of Blocks and reset size of true Number of Blocks + uint32_t trueNumberOfBlocks = floor(static_cast(totalSize) / static_cast(bytesPerBlock)); + + //allocate memory now + this->entries = new Index[trueNumberOfBlocks]; + this->size = trueNumberOfBlocks; + this->maxSize_ = trueNumberOfBlocks; + this->allocated = true; + + //Check trueNumberOfBlocks + if(trueNumberOfBlocks<1){ + error << "IndexedRingMemory: Invalid Number of Blocks: " << trueNumberOfBlocks; + } + + + + //Fill address into index + uint32_t address = trueStartAddress; + for (typename IndexedRingMemoryArray::Iterator it = this->begin();it!=this->end();++it) { + it->setBlockStartAddress(address); + it->setSize(0); + it->setStoredPackets(0); + address += bytesPerBlock; + } + + + //Initialize iterators + currentWriteBlock = this->begin(); + currentReadBlock = this->begin(); + lastBlockToRead = this->begin(); + + //Check last blockSize + uint32_t lastBlockSize = (trueStartAddress + useableSize) - (this->back()->getBlockStartAddress()); + if((lastBlockSizesize > 1)){ + //remove the last Block so the second last block has more size + this->size -= 1; + debug << "IndexedRingMemory: Last Block is smaller than bytesPerBlock, removed last block" << std::endl; + } + } + + /** + * Resets the whole index, the iterators and executes the given reset function on every index type + * @param typeResetFnc static reset function which accepts a pointer to the index Type + */ + void reset(void (*typeResetFnc)(T*)){ + currentReadBlock = this->begin(); + currentWriteBlock = this->begin(); + lastBlockToRead = this->begin(); + currentReadSize = 0; + currentReadBlockSizeCached = 0; + lastBlockToReadSize = 0; + for(typename IndexedRingMemoryArray::Iterator it = this->begin();it!=this->end();++it){ + it->setSize(0); + it->setStoredPackets(0); + (*typeResetFnc)(it->modifyIndexType()); + } + } + + void resetBlock(typename IndexedRingMemoryArray::Iterator it,void (*typeResetFnc)(T*)){ + it->setSize(0); + it->setStoredPackets(0); + (*typeResetFnc)(it->modifyIndexType()); + } + + /* + * Reading + */ + + void setCurrentReadBlock(typename IndexedRingMemoryArray::Iterator it){ + currentReadBlock = it; + currentReadBlockSizeCached = it->getSize(); + } + + void resetRead(){ + currentReadBlock = this->begin(); + currentReadSize = 0; + currentReadBlockSizeCached = this->begin()->getSize(); + lastBlockToRead = currentWriteBlock; + lastBlockToReadSize = currentWriteBlock->getSize(); + } + /** + * Sets the last block to read to this iterator. + * Can be used to dump until block x + * @param it The iterator for the last read block + */ + void setLastBlockToRead(typename IndexedRingMemoryArray::Iterator it){ + lastBlockToRead = it; + lastBlockToReadSize = it->getSize(); + } + + /** + * Set the read pointer to the first written Block, which is the first non empty block in front of the write block + * Can be the currentWriteBlock as well + */ + void readOldest(){ + resetRead(); + currentReadBlock = getNextNonEmptyBlock(); + currentReadBlockSizeCached = currentReadBlock->getSize(); + + } + + /** + * Sets the current read iterator to the next Block and resets the current read size + * The current size of the block will be cached to avoid race condition between write and read + * If the end of the ring is reached the read pointer will be set to the begin + */ + void readNext(){ + currentReadSize = 0; + if((this->size != 0) && (currentReadBlock.value ==this->back())){ + currentReadBlock = this->begin(); + }else{ + currentReadBlock++; + } + + currentReadBlockSizeCached = currentReadBlock->getSize(); + } + + /** + * Returns the address which is currently read from + * @return Address to read from + */ + uint32_t getCurrentReadAddress() const { + return getAddressOfCurrentReadBlock() + currentReadSize; + } + /** + * Adds readSize to the current size and checks if the read has no more data left and advances the read block + * @param readSize The size that was read + * @return Returns true if the read can go on + */ + bool addReadSize(uint32_t readSize) { + if(currentReadBlock == lastBlockToRead){ + //The current read block is the last to read + if((currentReadSize+readSize) return true + currentReadSize += readSize; + return true; + }else{ + //Reached end of read -> return false + currentReadSize = lastBlockToReadSize; + return false; + } + }else{ + //We are not in the last Block + if((currentReadSize + readSize)::Iterator it(currentReadBlock); + //Search if any block between this and the last block is not empty + for(;it!=lastBlockToRead;++it){ + if(it == this->end()){ + //This is the end, next block is the begin + it = this->begin(); + if(it == lastBlockToRead){ + //Break if the begin is the lastBlockToRead + break; + } + } + if(it->getSize()!=0){ + //This is a non empty block. Go on reading with this block + currentReadBlock = it; + currentReadBlockSizeCached = it->getSize(); + return true; + } + } + //reached lastBlockToRead and every block was empty, check if the last block is also empty + if(lastBlockToReadSize!=0){ + //go on with last Block + currentReadBlock = lastBlockToRead; + currentReadBlockSizeCached = lastBlockToReadSize; + return true; + } + //There is no non empty block left + return false; + } + //Size is larger than 0 + return true; + } + } + } + uint32_t getRemainigSizeOfCurrentReadBlock() const{ + if(currentReadBlock == lastBlockToRead){ + return (lastBlockToReadSize - currentReadSize); + }else{ + return (currentReadBlockSizeCached - currentReadSize); + } + } + + uint32_t getAddressOfCurrentReadBlock() const { + return currentReadBlock->getBlockStartAddress(); + } + + /** + * Gets the next non empty Block after the current write block, + * @return Returns the iterator to the block. If there is non, the current write block is returned + */ + typename IndexedRingMemoryArray::Iterator getNextNonEmptyBlock() const { + for(typename IndexedRingMemoryArray::Iterator it = getNextWrite();it!=currentWriteBlock;++it){ + if(it == this->end()){ + it = this->begin(); + if(it == currentWriteBlock){ + break; + } + } + if(it->getSize()!=0){ + return it; + } + } + return currentWriteBlock; + } + + /** + * Returns a copy of the oldest Index type + * @return Type of Index + */ + T* getOldest(){ + return (getNextNonEmptyBlock()->modifyIndexType()); + } + + + /* + * Writing + */ + uint32_t getAddressOfCurrentWriteBlock() const{ + return currentWriteBlock->getBlockStartAddress(); + } + + uint32_t getSizeOfCurrentWriteBlock() const{ + return currentWriteBlock->getSize(); + } + + uint32_t getCurrentWriteAddress() const{ + return getAddressOfCurrentWriteBlock() + getSizeOfCurrentWriteBlock(); + } + + void clearCurrentWriteBlock(){ + currentWriteBlock->setSize(0); + currentWriteBlock->setStoredPackets(0); + } + + void addCurrentWriteBlock(uint32_t size, uint32_t storedPackets){ + currentWriteBlock->addSize(size); + currentWriteBlock->addStoredPackets(storedPackets); + } + + T* modifyCurrentWriteBlockIndexType(){ + return currentWriteBlock->modifyIndexType(); + } + void updatePreviousWriteSize(uint32_t size, uint32_t storedPackets){ + typename IndexedRingMemoryArray::Iterator it = getPreviousBlock(currentWriteBlock); + it->addSize(size); + it->addStoredPackets(storedPackets); + } + + + /** + * Checks if the block has enough space for sizeToWrite + * @param sizeToWrite The data to be written in the Block + * @return Returns true if size to write is smaller the remaining size of the block + */ + bool hasCurrentWriteBlockEnoughSpace(uint32_t sizeToWrite){ + typename IndexedRingMemoryArray::Iterator next = getNextWrite(); + uint32_t addressOfNextBlock = next->getBlockStartAddress(); + uint32_t availableSize = ((addressOfNextBlock+totalSize) - (getAddressOfCurrentWriteBlock()+getSizeOfCurrentWriteBlock()))%totalSize; + return (sizeToWrite < availableSize); + } + + /** + * Checks if the store is full if overwrite old is false + * @return Returns true if it is writeable and false if not + */ + bool isNextBlockWritable(){ + //First check if this is the end of the list + typename IndexedRingMemoryArray::Iterator next; + next = getNextWrite(); + if((next->getSize()!=0) && (!overwriteOld)){ + return false; + } + return true; + } + + /** + * Updates current write Block Index Type + * @param infoOfNewBlock + */ + void updateCurrentBlock(T* infoOfNewBlock){ + currentWriteBlock->setIndexType(infoOfNewBlock); + } + + + /** + * Succeed to next block, returns FAILED if overwrite is false and the store is full + * @return + */ + ReturnValue_t writeNext(){ + //Check Next Block + if(!isNextBlockWritable()){ + //The Index is full and does not overwrite old + return HasReturnvaluesIF::RETURN_FAILED; + } + //Next block can be written, update Metadata + currentWriteBlock = getNextWrite(); + currentWriteBlock->setSize(0); + currentWriteBlock->setStoredPackets(0); + return HasReturnvaluesIF::RETURN_OK; + } + + /** + * Serializes the Index and calculates the CRC. + * Parameters according to HasSerializeIF + * @param buffer + * @param size + * @param maxSize + * @param streamEndianness + * @return + */ + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const{ + uint8_t* crcBuffer = *buffer; + uint32_t oldSize = *size; + if(additionalInfo!=NULL){ + additionalInfo->serialize(buffer,size,maxSize,streamEndianness); + } + ReturnValue_t result = currentWriteBlock->serialize(buffer,size,maxSize,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::serialize(&this->size,buffer,size,maxSize,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + + uint32_t i = 0; + while ((result == HasReturnvaluesIF::RETURN_OK) && (i < this->size)) { + result = SerializeAdapter::serialize(&this->entries[i], buffer, size, + maxSize, streamEndianness); + ++i; + } + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + uint16_t crc = Calculate_CRC(crcBuffer,(*size-oldSize)); + result = SerializeAdapter::serialize(&crc,buffer,size,maxSize,streamEndianness); + return result; + } + + + /** + * Get the serialized Size of the index + * @return The serialized size of the index + */ + size_t getSerializedSize() const { + + uint32_t size = 0; + if(additionalInfo!=NULL){ + size += additionalInfo->getSerializedSize(); + } + size += currentWriteBlock->getSerializedSize(); + size += SerializeAdapter::getSerializedSize(&this->size); + size += (this->entries[0].getSerializedSize()) * this->size; + uint16_t crc = 0; + size += SerializeAdapter::getSerializedSize(&crc); + return size; + } + /** + * DeSerialize the Indexed Ring from a buffer, deSerializes the current write iterator + * CRC Has to be checked before! + * @param buffer + * @param size + * @param streamEndianness + * @return + */ + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness){ + + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + if(additionalInfo!=NULL){ + result = additionalInfo->deSerialize(buffer,size,streamEndianness); + } + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + + Index tempIndex; + result = tempIndex.deSerialize(buffer,size,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + uint32_t tempSize = 0; + result = SerializeAdapter::deSerialize(&tempSize,buffer,size,streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + if(this->size != tempSize){ + return HasReturnvaluesIF::RETURN_FAILED; + } + uint32_t i = 0; + while ((result == HasReturnvaluesIF::RETURN_OK) && (i < this->size)) { + result = SerializeAdapter::deSerialize( + &this->entries[i], buffer, size, + streamEndianness); + ++i; + } + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + typename IndexedRingMemoryArray::Iterator cmp(&tempIndex); + for(typename IndexedRingMemoryArray::Iterator it= this->begin();it!=this->end();++it){ + if(*(cmp.value) == *(it.value)){ + currentWriteBlock = it; + return HasReturnvaluesIF::RETURN_OK; + } + } + //Reached if current write block iterator is not found + return HasReturnvaluesIF::RETURN_FAILED; + } + + uint32_t getIndexAddress() const { + return indexAddress; + } + + + /* + * Statistics + */ + uint32_t getStoredPackets() const { + uint32_t size = 0; + for(typename IndexedRingMemoryArray::Iterator it= this->begin();it!=this->end();++it){ + size += it->getStoredPackets(); + } + return size; + } + + uint32_t getTotalSize() const { + return totalSize; + } + + uint32_t getCurrentSize() const{ + uint32_t size = 0; + for(typename IndexedRingMemoryArray::Iterator it= this->begin();it!=this->end();++it){ + size += it->getSize(); + } + return size; + } + + bool isEmpty() const{ + return getCurrentSize()==0; + } + + double getPercentageFilled() const{ + uint32_t filledSize = 0; + for(typename IndexedRingMemoryArray::Iterator it= this->begin();it!=this->end();++it){ + filledSize += it->getSize(); + } + + return (double)filledSize/(double)this->totalSize; + } + + typename IndexedRingMemoryArray::Iterator getCurrentWriteBlock() const{ + return currentWriteBlock; + } + /** + * Get the next block of the currentWriteBlock. + * Returns the first one if currentWriteBlock is the last one + * @return Iterator pointing to the next block after currentWriteBlock + */ + typename IndexedRingMemoryArray::Iterator getNextWrite() const{ + typename IndexedRingMemoryArray::Iterator next(currentWriteBlock); + if((this->size != 0) && (currentWriteBlock.value == this->back())){ + next = this->begin(); + }else{ + ++next; + } + return next; + } + /** + * Get the block in front of the Iterator + * Returns the last block if it is the first block + * @param it iterator which you want the previous block from + * @return pointing to the block before it + */ + typename IndexedRingMemoryArray::Iterator getPreviousBlock(typename IndexedRingMemoryArray::Iterator it) { + if(this->begin() == it){ + typename IndexedRingMemoryArray::Iterator next((this->back())); + return next; + } + typename IndexedRingMemoryArray::Iterator next(it); + --next; + return next; + } +private: + //The total size used by the blocks (without index) + uint32_t totalSize; + + //The address of the index + const uint32_t indexAddress; + + //The iterators for writing and reading + typename IndexedRingMemoryArray::Iterator currentWriteBlock; + typename IndexedRingMemoryArray::Iterator currentReadBlock; + + //How much of the current read block is read already + uint32_t currentReadSize; + + //Cached Size of current read block + uint32_t currentReadBlockSizeCached; + + //Last block of current write (should be write block) + typename IndexedRingMemoryArray::Iterator lastBlockToRead; + //current size of last Block to read + uint32_t lastBlockToReadSize; + + //Additional Info to be serialized with the index + SerializeIF* additionalInfo; + + //Does it overwrite old blocks? + const bool overwriteOld; + +}; + + + + +#endif /* FRAMEWORK_CONTAINER_INDEXEDRINGMEMORY_H_ */ diff --git a/fsfw/container/PlacementFactory.h b/fsfw/container/PlacementFactory.h new file mode 100644 index 0000000..a0aebb7 --- /dev/null +++ b/fsfw/container/PlacementFactory.h @@ -0,0 +1,71 @@ +#ifndef FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_ +#define FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_ + +#include "../storagemanager/StorageManagerIF.h" +#include +/** + * The Placement Factory is used to create objects at runtime in a specific pool. + * In general, this should be avoided and it should only be used if you know what you are doing. + * You are not allowed to use this container with a type that allocates memory internally like ArrayList. + * + * Also, you have to check the returned pointer in generate against nullptr! + * + * A backend of Type StorageManagerIF must be given as a place to store the new objects. + * Therefore ThreadSafety is only provided by your StorageManager Implementation. + * + * Objects must be destroyed by the user with "destroy"! Otherwise the pool will not be cleared. + * + * The concept is based on the placement new operator. + * + * @warning Do not use with any Type that allocates memory internally! + * @ingroup container + */ +class PlacementFactory { +public: + PlacementFactory(StorageManagerIF* backend) : + dataBackend(backend) { + } + + /*** + * Generates an object of type T in the backend storage. + * + * @warning Do not use with any Type that allocates memory internally! + * + * @tparam T Type of Object + * @param args Constructor Arguments to be passed + * @return A pointer to the new object or a nullptr in case of failure + */ + template + T* generate(Args&&... args) { + store_address_t tempId; + uint8_t* pData = nullptr; + ReturnValue_t result = dataBackend->getFreeElement(&tempId, sizeof(T), + &pData); + if (result != HasReturnvaluesIF::RETURN_OK) { + return nullptr; + } + T* temp = new (pData) T(std::forward(args)...); + return temp; + } + /*** + * Function to destroy the object allocated with generate and free space in backend. + * This must be called by the user. + * + * @param thisElement Element to be destroyed + * @return RETURN_OK if the element was destroyed, different errors on failure + */ + template + ReturnValue_t destroy(T* thisElement) { + if (thisElement == nullptr){ + return HasReturnvaluesIF::RETURN_FAILED; + } + //Need to call destructor first, in case something was allocated by the object (shouldn't do that, however). + thisElement->~T(); + uint8_t* pointer = (uint8_t*) (thisElement); + return dataBackend->deleteData(pointer, sizeof(T)); + } +private: + StorageManagerIF* dataBackend; +}; + +#endif /* FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_ */ diff --git a/fsfw/container/RingBufferBase.h b/fsfw/container/RingBufferBase.h new file mode 100644 index 0000000..886b9fa --- /dev/null +++ b/fsfw/container/RingBufferBase.h @@ -0,0 +1,113 @@ +#ifndef FSFW_CONTAINER_RINGBUFFERBASE_H_ +#define FSFW_CONTAINER_RINGBUFFERBASE_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +template +class RingBufferBase { +public: + RingBufferBase(size_t startAddress, const size_t size, bool overwriteOld) : + start(startAddress), write(startAddress), size(size), + overwriteOld(overwriteOld) { + for (uint8_t count = 0; count < N_READ_PTRS; count++) { + read[count] = startAddress; + } + } + + virtual ~RingBufferBase() {} + + bool isFull(uint8_t n = 0) { + return (availableWriteSpace(n) == 0); + } + bool isEmpty(uint8_t n = 0) { + return (getAvailableReadData(n) == 0); + } + + size_t getAvailableReadData(uint8_t n = 0) const { + return ((write + size) - read[n]) % size; + } + size_t availableWriteSpace(uint8_t n = 0) const { + //One less to avoid ambiguous full/empty problem. + return (((read[n] + size) - write - 1) % size); + } + + bool overwritesOld() const { + return overwriteOld; + } + + size_t getMaxSize() const { + return size - 1; + } + + void clear() { + write = start; + for (uint8_t count = 0; count < N_READ_PTRS; count++) { + read[count] = start; + } + } + + size_t writeTillWrap() { + return (start + size) - write; + } + + size_t readTillWrap(uint8_t n = 0) { + return (start + size) - read[n]; + } + + size_t getStart() const { + return start; + } + +protected: + const size_t start; + size_t write; + size_t read[N_READ_PTRS]; + const size_t size; + const bool overwriteOld; + + void incrementWrite(uint32_t amount) { + write = ((write + amount - start) % size) + start; + } + void incrementRead(uint32_t amount, uint8_t n = 0) { + read[n] = ((read[n] + amount - start) % size) + start; + } + + ReturnValue_t readData(uint32_t amount, uint8_t n = 0) { + if (getAvailableReadData(n) >= amount) { + incrementRead(amount, n); + return HasReturnvaluesIF::RETURN_OK; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + + ReturnValue_t writeData(uint32_t amount) { + if (availableWriteSpace() >= amount or overwriteOld) { + incrementWrite(amount); + return HasReturnvaluesIF::RETURN_OK; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + + size_t getRead(uint8_t n = 0) const { + return read[n]; + } + + void setRead(uint32_t read, uint8_t n = 0) { + if (read >= start && read < (start+size)) { + this->read[n] = read; + } + } + + uint32_t getWrite() const { + return write; + } + + void setWrite(uint32_t write) { + this->write = write; + } +}; + +#endif /* FSFW_CONTAINER_RINGBUFFERBASE_H_ */ diff --git a/fsfw/container/SharedRingBuffer.cpp b/fsfw/container/SharedRingBuffer.cpp new file mode 100644 index 0000000..6ddb3d3 --- /dev/null +++ b/fsfw/container/SharedRingBuffer.cpp @@ -0,0 +1,55 @@ +#include "SharedRingBuffer.h" +#include "../ipc/MutexFactory.h" +#include "../ipc/MutexHelper.h" + +SharedRingBuffer::SharedRingBuffer(object_id_t objectId, const size_t size, + bool overwriteOld, size_t maxExcessBytes): + SystemObject(objectId), SimpleRingBuffer(size, overwriteOld, + maxExcessBytes) { + mutex = MutexFactory::instance()->createMutex(); +} + + +SharedRingBuffer::SharedRingBuffer(object_id_t objectId, uint8_t *buffer, + const size_t size, bool overwriteOld, size_t maxExcessBytes): + SystemObject(objectId), SimpleRingBuffer(buffer, size, overwriteOld, + maxExcessBytes) { + mutex = MutexFactory::instance()->createMutex(); +} + + +void SharedRingBuffer::setToUseReceiveSizeFIFO(size_t fifoDepth) { + this->fifoDepth = fifoDepth; +} + +ReturnValue_t SharedRingBuffer::lockRingBufferMutex( + MutexIF::TimeoutType timeoutType, dur_millis_t timeout) { + return mutex->lockMutex(timeoutType, timeout); +} + +ReturnValue_t SharedRingBuffer::unlockRingBufferMutex() { + return mutex->unlockMutex(); +} + + + +MutexIF* SharedRingBuffer::getMutexHandle() const { + return mutex; +} + +ReturnValue_t SharedRingBuffer::initialize() { + if(fifoDepth > 0) { + receiveSizesFIFO = new DynamicFIFO(fifoDepth); + } + return SystemObject::initialize(); +} + +DynamicFIFO* SharedRingBuffer::getReceiveSizesFIFO() { + if(receiveSizesFIFO == nullptr) { + // Configuration error. + sif::warning << "SharedRingBuffer::getReceiveSizesFIFO: Ring buffer" + << " was not configured to have sizes FIFO, returning nullptr!" + << std::endl; + } + return receiveSizesFIFO; +} diff --git a/fsfw/container/SharedRingBuffer.h b/fsfw/container/SharedRingBuffer.h new file mode 100644 index 0000000..64d7ee2 --- /dev/null +++ b/fsfw/container/SharedRingBuffer.h @@ -0,0 +1,92 @@ +#ifndef FSFW_CONTAINER_SHAREDRINGBUFFER_H_ +#define FSFW_CONTAINER_SHAREDRINGBUFFER_H_ + +#include "SimpleRingBuffer.h" +#include "DynamicFIFO.h" +#include "../ipc/MutexIF.h" +#include "../objectmanager/SystemObject.h" +#include "../timemanager/Clock.h" + +/** + * @brief Ring buffer which can be shared among multiple objects + * @details + * This class offers a mutex to perform thread-safe operation on the ring + * buffer. It is still up to the developer to actually perform the lock + * and unlock operations. + */ +class SharedRingBuffer: public SystemObject, + public SimpleRingBuffer { +public: + /** + * This constructor allocates a new internal buffer with the supplied size. + * @param size + * @param overwriteOld + * If the ring buffer is overflowing at a write operartion, the oldest data + * will be overwritten. + */ + SharedRingBuffer(object_id_t objectId, const size_t size, + bool overwriteOld, size_t maxExcessBytes); + + /** + * @brief This function can be used to add an optional FIFO to the class + * @details + * This FIFO will be allocated in the initialize function (and will + * have a fixed maximum size after that). It can be used to store + * values like packet sizes, for example for a shared ring buffer + * used by producer/consumer tasks. + */ + void setToUseReceiveSizeFIFO(size_t fifoDepth); + + /** + * This constructor takes an external buffer with the specified size. + * @param buffer + * @param size + * @param overwriteOld + * If the ring buffer is overflowing at a write operartion, the oldest data + * will be overwritten. + */ + SharedRingBuffer(object_id_t objectId, uint8_t* buffer, const size_t size, + bool overwriteOld, size_t maxExcessBytes); + + /** + * Unless a read-only constant value is read, all operations on the + * shared ring buffer should be protected by calling this function. + * @param timeoutType + * @param timeout + * @return + */ + virtual ReturnValue_t lockRingBufferMutex(MutexIF::TimeoutType timeoutType, + dur_millis_t timeout); + /** + * Any locked mutex also has to be unlocked, otherwise, access to the + * shared ring buffer will be blocked. + * @return + */ + virtual ReturnValue_t unlockRingBufferMutex(); + + /** + * The mutex handle can be accessed directly, for example to perform + * the lock with the #MutexHelper for a RAII compliant lock operation. + * @return + */ + MutexIF* getMutexHandle() const; + + ReturnValue_t initialize() override; + + /** + * If the shared ring buffer was configured to have a sizes FIFO, a handle + * to that FIFO can be retrieved with this function. + * Do not forget to protect access with a lock if required! + * @return + */ + DynamicFIFO* getReceiveSizesFIFO(); +private: + MutexIF* mutex = nullptr; + + size_t fifoDepth = 0; + DynamicFIFO* receiveSizesFIFO = nullptr; +}; + + + +#endif /* FSFW_CONTAINER_SHAREDRINGBUFFER_H_ */ diff --git a/fsfw/container/SimpleRingBuffer.cpp b/fsfw/container/SimpleRingBuffer.cpp new file mode 100644 index 0000000..88c9290 --- /dev/null +++ b/fsfw/container/SimpleRingBuffer.cpp @@ -0,0 +1,131 @@ +#include "SimpleRingBuffer.h" +#include + +SimpleRingBuffer::SimpleRingBuffer(const size_t size, bool overwriteOld, + size_t maxExcessBytes) : + RingBufferBase<>(0, size, overwriteOld), + maxExcessBytes(maxExcessBytes) { + if(maxExcessBytes > size) { + this->maxExcessBytes = size; + } + else { + this->maxExcessBytes = maxExcessBytes; + } + buffer = new uint8_t[size + maxExcessBytes]; +} + +SimpleRingBuffer::SimpleRingBuffer(uint8_t *buffer, const size_t size, + bool overwriteOld, size_t maxExcessBytes): + RingBufferBase<>(0, size, overwriteOld), buffer(buffer) { + if(maxExcessBytes > size) { + this->maxExcessBytes = size; + } + else { + this->maxExcessBytes = maxExcessBytes; + } +} + +SimpleRingBuffer::~SimpleRingBuffer() { + delete[] buffer; +} + +ReturnValue_t SimpleRingBuffer::getFreeElement(uint8_t **writePointer, + size_t amount) { + if (availableWriteSpace() >= amount or overwriteOld) { + size_t amountTillWrap = writeTillWrap(); + if (amountTillWrap < amount) { + if((amount - amountTillWrap + excessBytes) > maxExcessBytes) { + return HasReturnvaluesIF::RETURN_FAILED; + } + excessBytes = amount - amountTillWrap; + } + *writePointer = &buffer[write]; + return HasReturnvaluesIF::RETURN_OK; + } + else { + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +void SimpleRingBuffer::confirmBytesWritten(size_t amount) { + if(getExcessBytes() > 0) { + moveExcessBytesToStart(); + } + incrementWrite(amount); + +} + +ReturnValue_t SimpleRingBuffer::writeData(const uint8_t* data, + size_t amount) { + if (availableWriteSpace() >= amount or overwriteOld) { + size_t amountTillWrap = writeTillWrap(); + if (amountTillWrap >= amount) { + // remaining size in buffer is sufficient to fit full amount. + memcpy(&buffer[write], data, amount); + } + else { + memcpy(&buffer[write], data, amountTillWrap); + memcpy(buffer, data + amountTillWrap, amount - amountTillWrap); + } + incrementWrite(amount); + return HasReturnvaluesIF::RETURN_OK; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t SimpleRingBuffer::readData(uint8_t* data, size_t amount, + bool incrementReadPtr, bool readRemaining, size_t* trueAmount) { + size_t availableData = getAvailableReadData(READ_PTR); + size_t amountTillWrap = readTillWrap(READ_PTR); + if (availableData < amount) { + if (readRemaining) { + // more data available than amount specified. + amount = availableData; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + if (trueAmount != nullptr) { + *trueAmount = amount; + } + if (amountTillWrap >= amount) { + memcpy(data, &buffer[read[READ_PTR]], amount); + } else { + memcpy(data, &buffer[read[READ_PTR]], amountTillWrap); + memcpy(data + amountTillWrap, buffer, amount - amountTillWrap); + } + + if(incrementReadPtr) { + deleteData(amount, readRemaining); + } + return HasReturnvaluesIF::RETURN_OK; +} + +size_t SimpleRingBuffer::getExcessBytes() const { + return excessBytes; +} + +void SimpleRingBuffer::moveExcessBytesToStart() { + if(excessBytes > 0) { + std::memcpy(buffer, &buffer[size], excessBytes); + excessBytes = 0; + } +} + +ReturnValue_t SimpleRingBuffer::deleteData(size_t amount, + bool deleteRemaining, size_t* trueAmount) { + size_t availableData = getAvailableReadData(READ_PTR); + if (availableData < amount) { + if (deleteRemaining) { + amount = availableData; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + if (trueAmount != nullptr) { + *trueAmount = amount; + } + incrementRead(amount, READ_PTR); + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/container/SimpleRingBuffer.h b/fsfw/container/SimpleRingBuffer.h new file mode 100644 index 0000000..37ad567 --- /dev/null +++ b/fsfw/container/SimpleRingBuffer.h @@ -0,0 +1,129 @@ +#ifndef FSFW_CONTAINER_SIMPLERINGBUFFER_H_ +#define FSFW_CONTAINER_SIMPLERINGBUFFER_H_ + +#include "RingBufferBase.h" +#include + +/** + * @brief Circular buffer implementation, useful for buffering + * into data streams. + * @details + * Note that the deleteData() has to be called to increment the read pointer. + * This class allocated dynamically, so + * @ingroup containers + */ +class SimpleRingBuffer: public RingBufferBase<> { +public: + /** + * This constructor allocates a new internal buffer with the supplied size. + * + * @param size + * @param overwriteOld If the ring buffer is overflowing at a write + * operation, the oldest data will be overwritten. + * @param maxExcessBytes These additional bytes will be allocated in addtion + * to the specified size to accomodate contiguous write operations + * with getFreeElement. + * + */ + SimpleRingBuffer(const size_t size, bool overwriteOld, + size_t maxExcessBytes = 0); + /** + * This constructor takes an external buffer with the specified size. + * @param buffer + * @param size + * @param overwriteOld + * If the ring buffer is overflowing at a write operartion, the oldest data + * will be overwritten. + * @param maxExcessBytes + * If the buffer can accomodate additional bytes for contigous write + * operations with getFreeElement, this is the maximum allowed additional + * size + */ + SimpleRingBuffer(uint8_t* buffer, const size_t size, bool overwriteOld, + size_t maxExcessBytes = 0); + + virtual ~SimpleRingBuffer(); + + /** + * Write to circular buffer and increment write pointer by amount. + * @param data + * @param amount + * @return -@c RETURN_OK if write operation was successfull + * -@c RETURN_FAILED if + */ + ReturnValue_t writeData(const uint8_t* data, size_t amount); + + /** + * Returns a pointer to a free element. If the remaining buffer is + * not large enough, the data will be written past the actual size + * and the amount of excess bytes will be cached. This function + * does not increment the write pointer! + * @param writePointer Pointer to a pointer which can be used to write + * contiguous blocks into the ring buffer + * @param amount + * @return + */ + ReturnValue_t getFreeElement(uint8_t** writePointer, size_t amount); + + /** + * This increments the write pointer and also copies the excess bytes + * to the beginning. It should be called if the write operation + * conducted after calling getFreeElement() was performed. + * @return + */ + void confirmBytesWritten(size_t amount); + + virtual size_t getExcessBytes() const; + /** + * Helper functions which moves any excess bytes to the start + * of the ring buffer. + * @return + */ + virtual void moveExcessBytesToStart(); + + /** + * Read from circular buffer at read pointer. + * @param data + * @param amount + * @param incrementReadPtr + * If this is set to true, the read pointer will be incremented. + * If readRemaining is set to true, the read pointer will be incremented + * accordingly. + * @param readRemaining + * If this is set to true, the data will be read even if the amount + * specified exceeds the read data available. + * @param trueAmount [out] + * If readRemaining was set to true, the true amount read will be assigned + * to the passed value. + * @return + * - @c RETURN_OK if data was read successfully + * - @c RETURN_FAILED if not enough data was available and readRemaining + * was set to false. + */ + ReturnValue_t readData(uint8_t* data, size_t amount, + bool incrementReadPtr = false, bool readRemaining = false, + size_t* trueAmount = nullptr); + + /** + * Delete data by incrementing read pointer. + * @param amount + * @param deleteRemaining + * If the amount specified is larger than the remaing size to read and this + * is set to true, the remaining amount will be deleted as well + * @param trueAmount [out] + * If deleteRemaining was set to true, the amount deleted will be assigned + * to the passed value. + * @return + */ + ReturnValue_t deleteData(size_t amount, bool deleteRemaining = false, + size_t* trueAmount = nullptr); + +private: + static const uint8_t READ_PTR = 0; + uint8_t* buffer = nullptr; + size_t maxExcessBytes; + size_t excessBytes = 0; +}; + +#endif /* FSFW_CONTAINER_SIMPLERINGBUFFER_H_ */ + diff --git a/fsfw/container/SinglyLinkedList.h b/fsfw/container/SinglyLinkedList.h new file mode 100644 index 0000000..eb6ae27 --- /dev/null +++ b/fsfw/container/SinglyLinkedList.h @@ -0,0 +1,154 @@ +#ifndef FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_ +#define FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_ + +#include +#include + +/** + * @brief Linked list data structure, + * each entry has a pointer to the next entry (singly) + * @ingroup container + */ +template +class LinkedElement { +public: + T *value; + class Iterator { + public: + LinkedElement *value = nullptr; + Iterator() {} + + Iterator(LinkedElement *element) : + value(element) { + } + + Iterator& operator++() { + value = value->getNext(); + return *this; + } + + Iterator operator++(int) { + Iterator tmp(*this); + operator++(); + return tmp; + } + + bool operator==(Iterator other) { + return value == other.value; + } + + bool operator!=(Iterator other) { + return !(*this == other); + } + T *operator->() { + return value->value; + } + }; + + LinkedElement(T* setElement, LinkedElement* setNext = nullptr): + value(setElement), next(setNext) {} + + virtual ~LinkedElement(){} + + virtual LinkedElement* getNext() const { + return next; + } + + virtual void setNext(LinkedElement* next) { + this->next = next; + } + + virtual void setEnd() { + this->next = nullptr; + } + + LinkedElement* begin() { + return this; + } + LinkedElement* end() { + return nullptr; + } +private: + LinkedElement *next; +}; + +template +class SinglyLinkedList { +public: + using ElementIterator = typename LinkedElement::Iterator; + + SinglyLinkedList() {} + + SinglyLinkedList(ElementIterator start) : + start(start.value) {} + + SinglyLinkedList(LinkedElement* startElement) : + start(startElement) {} + + ElementIterator begin() const { + return ElementIterator::Iterator(start); + } + + /** Returns iterator to nulltr */ + ElementIterator end() const { + return ElementIterator::Iterator(); + } + + /** + * Returns last element in singly linked list. + * @return + */ + ElementIterator back() const { + LinkedElement *element = start; + while (element->getNext() != nullptr) { + element = element->getNext(); + } + return ElementIterator::Iterator(element); + } + + size_t getSize() const { + size_t size = 0; + LinkedElement *element = start; + while (element != nullptr) { + size++; + element = element->getNext(); + } + return size; + } + void setStart(LinkedElement* firstElement) { + start = firstElement; + } + + void setNext(LinkedElement* currentElement, + LinkedElement* nextElement) { + currentElement->setNext(nextElement); + } + + void setLast(LinkedElement* lastElement) { + lastElement->setEnd(); + } + + void insertElement(LinkedElement* element, size_t position) { + LinkedElement *currentElement = start; + for(size_t count = 0; count < position; count++) { + if(currentElement == nullptr) { + return; + } + currentElement = currentElement->getNext(); + } + LinkedElement* elementAfterCurrent = currentElement->next; + currentElement->setNext(element); + if(elementAfterCurrent != nullptr) { + element->setNext(elementAfterCurrent); + } + } + + void insertBack(LinkedElement* lastElement) { + back().value->setNext(lastElement); + } + +protected: + LinkedElement *start = nullptr; +}; + +#endif /* SINGLYLINKEDLIST_H_ */ diff --git a/fsfw/container/group.h b/fsfw/container/group.h new file mode 100644 index 0000000..5a39d34 --- /dev/null +++ b/fsfw/container/group.h @@ -0,0 +1,15 @@ +#ifndef GROUP_H_ +#define GROUP_H_ + +/** + * @defgroup container Container + * + * General Purpose Container to store various elements. + * + * Also contains Adapter classes to print elements to a + * bytestream and to read them from a bytestream, as well + * as an Adapter to swap the endianness. + */ + + +#endif /* GROUP_H_ */ diff --git a/fsfw/contrib/sgp4/LICENSE b/fsfw/contrib/sgp4/LICENSE new file mode 100644 index 0000000..9672956 --- /dev/null +++ b/fsfw/contrib/sgp4/LICENSE @@ -0,0 +1,4 @@ +There is no license associated with the code and you may use it for any purpose —personal or commercial— as you +wish. We ask only that you include citations in your documentation and source code to show the source of the code +and provide links to https://www.celestrak.com/publications/AIAA/2006-6753/, to facilitate communications +regarding any questions on the theory or source code. \ No newline at end of file diff --git a/fsfw/contrib/sgp4/sgp4unit.cpp b/fsfw/contrib/sgp4/sgp4unit.cpp new file mode 100644 index 0000000..b707a7a --- /dev/null +++ b/fsfw/contrib/sgp4/sgp4unit.cpp @@ -0,0 +1,2090 @@ +/* ---------------------------------------------------------------- +* +* sgp4unit.cpp +* +* this file contains the sgp4 procedures for analytical propagation +* of a satellite. the code was originally released in the 1980 and 1986 +* spacetrack papers. a detailed discussion of the theory and history +* may be found in the 2006 aiaa paper by vallado, crawford, hujsak, +* and kelso. +* +* companion code for +* fundamentals of astrodynamics and applications +* 2007 +* by david vallado +* +* (w) 719-573-2600, email dvallado@agi.com +* +* current : +* 16 nov 07 david vallado +* misc fixes for better compliance +* changes : +* 20 apr 07 david vallado +* misc fixes for constants +* 11 aug 06 david vallado +* chg lyddane choice back to strn3, constants, misc doc +* 15 dec 05 david vallado +* misc fixes +* 26 jul 05 david vallado +* fixes for paper +* note that each fix is preceded by a +* comment with "sgp4fix" and an explanation of +* what was changed +* 10 aug 04 david vallado +* 2nd printing baseline working +* 14 may 01 david vallado +* 2nd edition baseline +* 80 norad +* original baseline +* ---------------------------------------------------------------- */ + +#include "sgp4unit.h" + +const char help = 'n'; +FILE *dbgfile; + +#define pi 3.14159265358979323846 + + +/* ----------- local functions - only ever used internally by sgp4 ---------- */ +static void dpper + ( + double e3, double ee2, double peo, double pgho, double pho, + double pinco, double plo, double se2, double se3, double sgh2, + double sgh3, double sgh4, double sh2, double sh3, double si2, + double si3, double sl2, double sl3, double sl4, double t, + double xgh2, double xgh3, double xgh4, double xh2, double xh3, + double xi2, double xi3, double xl2, double xl3, double xl4, + double zmol, double zmos, double inclo, + char init, + double& ep, double& inclp, double& nodep, double& argpp, double& mp + ); + +static void dscom + ( + double epoch, double ep, double argpp, double tc, double inclp, + double nodep, double np, + double& snodm, double& cnodm, double& sinim, double& cosim, double& sinomm, + double& cosomm,double& day, double& e3, double& ee2, double& em, + double& emsq, double& gam, double& peo, double& pgho, double& pho, + double& pinco, double& plo, double& rtemsq, double& se2, double& se3, + double& sgh2, double& sgh3, double& sgh4, double& sh2, double& sh3, + double& si2, double& si3, double& sl2, double& sl3, double& sl4, + double& s1, double& s2, double& s3, double& s4, double& s5, + double& s6, double& s7, double& ss1, double& ss2, double& ss3, + double& ss4, double& ss5, double& ss6, double& ss7, double& sz1, + double& sz2, double& sz3, double& sz11, double& sz12, double& sz13, + double& sz21, double& sz22, double& sz23, double& sz31, double& sz32, + double& sz33, double& xgh2, double& xgh3, double& xgh4, double& xh2, + double& xh3, double& xi2, double& xi3, double& xl2, double& xl3, + double& xl4, double& nm, double& z1, double& z2, double& z3, + double& z11, double& z12, double& z13, double& z21, double& z22, + double& z23, double& z31, double& z32, double& z33, double& zmol, + double& zmos + ); + +static void dsinit + ( + gravconsttype whichconst, + double cosim, double emsq, double argpo, double s1, double s2, + double s3, double s4, double s5, double sinim, double ss1, + double ss2, double ss3, double ss4, double ss5, double sz1, + double sz3, double sz11, double sz13, double sz21, double sz23, + double sz31, double sz33, double t, double tc, double gsto, + double mo, double mdot, double no, double nodeo, double nodedot, + double xpidot, double z1, double z3, double z11, double z13, + double z21, double z23, double z31, double z33, double ecco, + double eccsq, double& em, double& argpm, double& inclm, double& mm, + double& nm, double& nodem, + int& irez, + double& atime, double& d2201, double& d2211, double& d3210, double& d3222, + double& d4410, double& d4422, double& d5220, double& d5232, double& d5421, + double& d5433, double& dedt, double& didt, double& dmdt, double& dndt, + double& dnodt, double& domdt, double& del1, double& del2, double& del3, + double& xfact, double& xlamo, double& xli, double& xni + ); + +static void dspace + ( + int irez, + double d2201, double d2211, double d3210, double d3222, double d4410, + double d4422, double d5220, double d5232, double d5421, double d5433, + double dedt, double del1, double del2, double del3, double didt, + double dmdt, double dnodt, double domdt, double argpo, double argpdot, + double t, double tc, double gsto, double xfact, double xlamo, + double no, + double& atime, double& em, double& argpm, double& inclm, double& xli, + double& mm, double& xni, double& nodem, double& dndt, double& nm + ); + +static void initl + ( + int satn, gravconsttype whichconst, + double ecco, double epoch, double inclo, double& no, + char& method, + double& ainv, double& ao, double& con41, double& con42, double& cosio, + double& cosio2,double& eccsq, double& omeosq, double& posq, + double& rp, double& rteosq,double& sinio , double& gsto + ); + +/* ----------------------------------------------------------------------------- +* +* procedure dpper +* +* this procedure provides deep space long period periodic contributions +* to the mean elements. by design, these periodics are zero at epoch. +* this used to be dscom which included initialization, but it's really a +* recurring function. +* +* author : david vallado 719-573-2600 28 jun 2005 +* +* inputs : +* e3 - +* ee2 - +* peo - +* pgho - +* pho - +* pinco - +* plo - +* se2 , se3 , sgh2, sgh3, sgh4, sh2, sh3, si2, si3, sl2, sl3, sl4 - +* t - +* xh2, xh3, xi2, xi3, xl2, xl3, xl4 - +* zmol - +* zmos - +* ep - eccentricity 0.0 - 1.0 +* inclo - inclination - needed for lyddane modification +* nodep - right ascension of ascending node +* argpp - argument of perigee +* mp - mean anomaly +* +* outputs : +* ep - eccentricity 0.0 - 1.0 +* inclp - inclination +* nodep - right ascension of ascending node +* argpp - argument of perigee +* mp - mean anomaly +* +* locals : +* alfdp - +* betdp - +* cosip , sinip , cosop , sinop , +* dalf - +* dbet - +* dls - +* f2, f3 - +* pe - +* pgh - +* ph - +* pinc - +* pl - +* sel , ses , sghl , sghs , shl , shs , sil , sinzf , sis , +* sll , sls +* xls - +* xnoh - +* zf - +* zm - +* +* coupling : +* none. +* +* references : +* hoots, roehrich, norad spacetrack report #3 1980 +* hoots, norad spacetrack report #6 1986 +* hoots, schumacher and glover 2004 +* vallado, crawford, hujsak, kelso 2006 + ----------------------------------------------------------------------------*/ + +static void dpper + ( + double e3, double ee2, double peo, double pgho, double pho, + double pinco, double plo, double se2, double se3, double sgh2, + double sgh3, double sgh4, double sh2, double sh3, double si2, + double si3, double sl2, double sl3, double sl4, double t, + double xgh2, double xgh3, double xgh4, double xh2, double xh3, + double xi2, double xi3, double xl2, double xl3, double xl4, + double zmol, double zmos, double inclo, + char init, + double& ep, double& inclp, double& nodep, double& argpp, double& mp + ) +{ + /* --------------------- local variables ------------------------ */ + const double twopi = 2.0 * pi; + double alfdp, betdp, cosip, cosop, dalf, dbet, dls, + f2, f3, pe, pgh, ph, pinc, pl , + sel, ses, sghl, sghs, shll, shs, sil, + sinip, sinop, sinzf, sis, sll, sls, xls, + xnoh, zf, zm, zel, zes, znl, zns; + + /* ---------------------- constants ----------------------------- */ + zns = 1.19459e-5; + zes = 0.01675; + znl = 1.5835218e-4; + zel = 0.05490; + + /* --------------- calculate time varying periodics ----------- */ + zm = zmos + zns * t; + // be sure that the initial call has time set to zero + if (init == 'y') + zm = zmos; + zf = zm + 2.0 * zes * sin(zm); + sinzf = sin(zf); + f2 = 0.5 * sinzf * sinzf - 0.25; + f3 = -0.5 * sinzf * cos(zf); + ses = se2* f2 + se3 * f3; + sis = si2 * f2 + si3 * f3; + sls = sl2 * f2 + sl3 * f3 + sl4 * sinzf; + sghs = sgh2 * f2 + sgh3 * f3 + sgh4 * sinzf; + shs = sh2 * f2 + sh3 * f3; + zm = zmol + znl * t; + if (init == 'y') + zm = zmol; + zf = zm + 2.0 * zel * sin(zm); + sinzf = sin(zf); + f2 = 0.5 * sinzf * sinzf - 0.25; + f3 = -0.5 * sinzf * cos(zf); + sel = ee2 * f2 + e3 * f3; + sil = xi2 * f2 + xi3 * f3; + sll = xl2 * f2 + xl3 * f3 + xl4 * sinzf; + sghl = xgh2 * f2 + xgh3 * f3 + xgh4 * sinzf; + shll = xh2 * f2 + xh3 * f3; + pe = ses + sel; + pinc = sis + sil; + pl = sls + sll; + pgh = sghs + sghl; + ph = shs + shll; + + if (init == 'n') + { + pe = pe - peo; + pinc = pinc - pinco; + pl = pl - plo; + pgh = pgh - pgho; + ph = ph - pho; + inclp = inclp + pinc; + ep = ep + pe; + sinip = sin(inclp); + cosip = cos(inclp); + + /* ----------------- apply periodics directly ------------ */ + // sgp4fix for lyddane choice + // strn3 used original inclination - this is technically feasible + // gsfc used perturbed inclination - also technically feasible + // probably best to readjust the 0.2 limit value and limit discontinuity + // 0.2 rad = 11.45916 deg + // use next line for original strn3 approach and original inclination + // if (inclo >= 0.2) + // use next line for gsfc version and perturbed inclination + if (inclp >= 0.2) + { + ph = ph / sinip; + pgh = pgh - cosip * ph; + argpp = argpp + pgh; + nodep = nodep + ph; + mp = mp + pl; + } + else + { + /* ---- apply periodics with lyddane modification ---- */ + sinop = sin(nodep); + cosop = cos(nodep); + alfdp = sinip * sinop; + betdp = sinip * cosop; + dalf = ph * cosop + pinc * cosip * sinop; + dbet = -ph * sinop + pinc * cosip * cosop; + alfdp = alfdp + dalf; + betdp = betdp + dbet; + nodep = fmod(nodep, twopi); + // sgp4fix for afspc written intrinsic functions + // nodep used without a trigonometric function ahead + if (nodep < 0.0) + nodep = nodep + twopi; + xls = mp + argpp + cosip * nodep; + dls = pl + pgh - pinc * nodep * sinip; + xls = xls + dls; + xnoh = nodep; + nodep = atan2(alfdp, betdp); + // sgp4fix for afspc written intrinsic functions + // nodep used without a trigonometric function ahead + if (nodep < 0.0) + nodep = nodep + twopi; + if (fabs(xnoh - nodep) > pi){ + if (nodep < xnoh) + nodep = nodep + twopi; + else + nodep = nodep - twopi; + } + mp = mp + pl; + argpp = xls - mp - cosip * nodep; + } + } // if init == 'n' + +//#include "debug1.cpp" +} // end dpper + +/*----------------------------------------------------------------------------- +* +* procedure dscom +* +* this procedure provides deep space common items used by both the secular +* and periodics subroutines. input is provided as shown. this routine +* used to be called dpper, but the functions inside weren't well organized. +* +* author : david vallado 719-573-2600 28 jun 2005 +* +* inputs : +* epoch - +* ep - eccentricity +* argpp - argument of perigee +* tc - +* inclp - inclination +* nodep - right ascension of ascending node +* np - mean motion +* +* outputs : +* sinim , cosim , sinomm , cosomm , snodm , cnodm +* day - +* e3 - +* ee2 - +* em - eccentricity +* emsq - eccentricity squared +* gam - +* peo - +* pgho - +* pho - +* pinco - +* plo - +* rtemsq - +* se2, se3 - +* sgh2, sgh3, sgh4 - +* sh2, sh3, si2, si3, sl2, sl3, sl4 - +* s1, s2, s3, s4, s5, s6, s7 - +* ss1, ss2, ss3, ss4, ss5, ss6, ss7, sz1, sz2, sz3 - +* sz11, sz12, sz13, sz21, sz22, sz23, sz31, sz32, sz33 - +* xgh2, xgh3, xgh4, xh2, xh3, xi2, xi3, xl2, xl3, xl4 - +* nm - mean motion +* z1, z2, z3, z11, z12, z13, z21, z22, z23, z31, z32, z33 - +* zmol - +* zmos - +* +* locals : +* a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 - +* betasq - +* cc - +* ctem, stem - +* x1, x2, x3, x4, x5, x6, x7, x8 - +* xnodce - +* xnoi - +* zcosg , zsing , zcosgl , zsingl , zcosh , zsinh , zcoshl , zsinhl , +* zcosi , zsini , zcosil , zsinil , +* zx - +* zy - +* +* coupling : +* none. +* +* references : +* hoots, roehrich, norad spacetrack report #3 1980 +* hoots, norad spacetrack report #6 1986 +* hoots, schumacher and glover 2004 +* vallado, crawford, hujsak, kelso 2006 + ----------------------------------------------------------------------------*/ + +static void dscom + ( + double epoch, double ep, double argpp, double tc, double inclp, + double nodep, double np, + double& snodm, double& cnodm, double& sinim, double& cosim, double& sinomm, + double& cosomm,double& day, double& e3, double& ee2, double& em, + double& emsq, double& gam, double& peo, double& pgho, double& pho, + double& pinco, double& plo, double& rtemsq, double& se2, double& se3, + double& sgh2, double& sgh3, double& sgh4, double& sh2, double& sh3, + double& si2, double& si3, double& sl2, double& sl3, double& sl4, + double& s1, double& s2, double& s3, double& s4, double& s5, + double& s6, double& s7, double& ss1, double& ss2, double& ss3, + double& ss4, double& ss5, double& ss6, double& ss7, double& sz1, + double& sz2, double& sz3, double& sz11, double& sz12, double& sz13, + double& sz21, double& sz22, double& sz23, double& sz31, double& sz32, + double& sz33, double& xgh2, double& xgh3, double& xgh4, double& xh2, + double& xh3, double& xi2, double& xi3, double& xl2, double& xl3, + double& xl4, double& nm, double& z1, double& z2, double& z3, + double& z11, double& z12, double& z13, double& z21, double& z22, + double& z23, double& z31, double& z32, double& z33, double& zmol, + double& zmos + ) +{ + /* -------------------------- constants ------------------------- */ + const double zes = 0.01675; + const double zel = 0.05490; + const double c1ss = 2.9864797e-6; + const double c1l = 4.7968065e-7; + const double zsinis = 0.39785416; + const double zcosis = 0.91744867; + const double zcosgs = 0.1945905; + const double zsings = -0.98088458; + const double twopi = 2.0 * pi; + + /* --------------------- local variables ------------------------ */ + int lsflg; + double a1 , a2 , a3 , a4 , a5 , a6 , a7 , + a8 , a9 , a10 , betasq, cc , ctem , stem , + x1 , x2 , x3 , x4 , x5 , x6 , x7 , + x8 , xnodce, xnoi , zcosg , zcosgl, zcosh , zcoshl, + zcosi , zcosil, zsing , zsingl, zsinh , zsinhl, zsini , + zsinil, zx , zy; + + nm = np; + em = ep; + snodm = sin(nodep); + cnodm = cos(nodep); + sinomm = sin(argpp); + cosomm = cos(argpp); + sinim = sin(inclp); + cosim = cos(inclp); + emsq = em * em; + betasq = 1.0 - emsq; + rtemsq = sqrt(betasq); + + /* ----------------- initialize lunar solar terms --------------- */ + peo = 0.0; + pinco = 0.0; + plo = 0.0; + pgho = 0.0; + pho = 0.0; + day = epoch + 18261.5 + tc / 1440.0; + xnodce = fmod(4.5236020 - 9.2422029e-4 * day, twopi); + stem = sin(xnodce); + ctem = cos(xnodce); + zcosil = 0.91375164 - 0.03568096 * ctem; + zsinil = sqrt(1.0 - zcosil * zcosil); + zsinhl = 0.089683511 * stem / zsinil; + zcoshl = sqrt(1.0 - zsinhl * zsinhl); + gam = 5.8351514 + 0.0019443680 * day; + zx = 0.39785416 * stem / zsinil; + zy = zcoshl * ctem + 0.91744867 * zsinhl * stem; + zx = atan2(zx, zy); + zx = gam + zx - xnodce; + zcosgl = cos(zx); + zsingl = sin(zx); + + /* ------------------------- do solar terms --------------------- */ + zcosg = zcosgs; + zsing = zsings; + zcosi = zcosis; + zsini = zsinis; + zcosh = cnodm; + zsinh = snodm; + cc = c1ss; + xnoi = 1.0 / nm; + + for (lsflg = 1; lsflg <= 2; lsflg++) + { + a1 = zcosg * zcosh + zsing * zcosi * zsinh; + a3 = -zsing * zcosh + zcosg * zcosi * zsinh; + a7 = -zcosg * zsinh + zsing * zcosi * zcosh; + a8 = zsing * zsini; + a9 = zsing * zsinh + zcosg * zcosi * zcosh; + a10 = zcosg * zsini; + a2 = cosim * a7 + sinim * a8; + a4 = cosim * a9 + sinim * a10; + a5 = -sinim * a7 + cosim * a8; + a6 = -sinim * a9 + cosim * a10; + + x1 = a1 * cosomm + a2 * sinomm; + x2 = a3 * cosomm + a4 * sinomm; + x3 = -a1 * sinomm + a2 * cosomm; + x4 = -a3 * sinomm + a4 * cosomm; + x5 = a5 * sinomm; + x6 = a6 * sinomm; + x7 = a5 * cosomm; + x8 = a6 * cosomm; + + z31 = 12.0 * x1 * x1 - 3.0 * x3 * x3; + z32 = 24.0 * x1 * x2 - 6.0 * x3 * x4; + z33 = 12.0 * x2 * x2 - 3.0 * x4 * x4; + z1 = 3.0 * (a1 * a1 + a2 * a2) + z31 * emsq; + z2 = 6.0 * (a1 * a3 + a2 * a4) + z32 * emsq; + z3 = 3.0 * (a3 * a3 + a4 * a4) + z33 * emsq; + z11 = -6.0 * a1 * a5 + emsq * (-24.0 * x1 * x7-6.0 * x3 * x5); + z12 = -6.0 * (a1 * a6 + a3 * a5) + emsq * + (-24.0 * (x2 * x7 + x1 * x8) - 6.0 * (x3 * x6 + x4 * x5)); + z13 = -6.0 * a3 * a6 + emsq * (-24.0 * x2 * x8 - 6.0 * x4 * x6); + z21 = 6.0 * a2 * a5 + emsq * (24.0 * x1 * x5 - 6.0 * x3 * x7); + z22 = 6.0 * (a4 * a5 + a2 * a6) + emsq * + (24.0 * (x2 * x5 + x1 * x6) - 6.0 * (x4 * x7 + x3 * x8)); + z23 = 6.0 * a4 * a6 + emsq * (24.0 * x2 * x6 - 6.0 * x4 * x8); + z1 = z1 + z1 + betasq * z31; + z2 = z2 + z2 + betasq * z32; + z3 = z3 + z3 + betasq * z33; + s3 = cc * xnoi; + s2 = -0.5 * s3 / rtemsq; + s4 = s3 * rtemsq; + s1 = -15.0 * em * s4; + s5 = x1 * x3 + x2 * x4; + s6 = x2 * x3 + x1 * x4; + s7 = x2 * x4 - x1 * x3; + + /* ----------------------- do lunar terms ------------------- */ + if (lsflg == 1) + { + ss1 = s1; + ss2 = s2; + ss3 = s3; + ss4 = s4; + ss5 = s5; + ss6 = s6; + ss7 = s7; + sz1 = z1; + sz2 = z2; + sz3 = z3; + sz11 = z11; + sz12 = z12; + sz13 = z13; + sz21 = z21; + sz22 = z22; + sz23 = z23; + sz31 = z31; + sz32 = z32; + sz33 = z33; + zcosg = zcosgl; + zsing = zsingl; + zcosi = zcosil; + zsini = zsinil; + zcosh = zcoshl * cnodm + zsinhl * snodm; + zsinh = snodm * zcoshl - cnodm * zsinhl; + cc = c1l; + } + } + + zmol = fmod(4.7199672 + 0.22997150 * day - gam, twopi); + zmos = fmod(6.2565837 + 0.017201977 * day, twopi); + + /* ------------------------ do solar terms ---------------------- */ + se2 = 2.0 * ss1 * ss6; + se3 = 2.0 * ss1 * ss7; + si2 = 2.0 * ss2 * sz12; + si3 = 2.0 * ss2 * (sz13 - sz11); + sl2 = -2.0 * ss3 * sz2; + sl3 = -2.0 * ss3 * (sz3 - sz1); + sl4 = -2.0 * ss3 * (-21.0 - 9.0 * emsq) * zes; + sgh2 = 2.0 * ss4 * sz32; + sgh3 = 2.0 * ss4 * (sz33 - sz31); + sgh4 = -18.0 * ss4 * zes; + sh2 = -2.0 * ss2 * sz22; + sh3 = -2.0 * ss2 * (sz23 - sz21); + + /* ------------------------ do lunar terms ---------------------- */ + ee2 = 2.0 * s1 * s6; + e3 = 2.0 * s1 * s7; + xi2 = 2.0 * s2 * z12; + xi3 = 2.0 * s2 * (z13 - z11); + xl2 = -2.0 * s3 * z2; + xl3 = -2.0 * s3 * (z3 - z1); + xl4 = -2.0 * s3 * (-21.0 - 9.0 * emsq) * zel; + xgh2 = 2.0 * s4 * z32; + xgh3 = 2.0 * s4 * (z33 - z31); + xgh4 = -18.0 * s4 * zel; + xh2 = -2.0 * s2 * z22; + xh3 = -2.0 * s2 * (z23 - z21); + +//#include "debug2.cpp" +} // end dscom + +/*----------------------------------------------------------------------------- +* +* procedure dsinit +* +* this procedure provides deep space contributions to mean motion dot due +* to geopotential resonance with half day and one day orbits. +* +* author : david vallado 719-573-2600 28 jun 2005 +* +* inputs : +* cosim, sinim- +* emsq - eccentricity squared +* argpo - argument of perigee +* s1, s2, s3, s4, s5 - +* ss1, ss2, ss3, ss4, ss5 - +* sz1, sz3, sz11, sz13, sz21, sz23, sz31, sz33 - +* t - time +* tc - +* gsto - greenwich sidereal time rad +* mo - mean anomaly +* mdot - mean anomaly dot (rate) +* no - mean motion +* nodeo - right ascension of ascending node +* nodedot - right ascension of ascending node dot (rate) +* xpidot - +* z1, z3, z11, z13, z21, z23, z31, z33 - +* eccm - eccentricity +* argpm - argument of perigee +* inclm - inclination +* mm - mean anomaly +* xn - mean motion +* nodem - right ascension of ascending node +* +* outputs : +* em - eccentricity +* argpm - argument of perigee +* inclm - inclination +* mm - mean anomaly +* nm - mean motion +* nodem - right ascension of ascending node +* irez - flag for resonance 0-none, 1-one day, 2-half day +* atime - +* d2201, d2211, d3210, d3222, d4410, d4422, d5220, d5232, d5421, d5433 - +* dedt - +* didt - +* dmdt - +* dndt - +* dnodt - +* domdt - +* del1, del2, del3 - +* ses , sghl , sghs , sgs , shl , shs , sis , sls +* theta - +* xfact - +* xlamo - +* xli - +* xni +* +* locals : +* ainv2 - +* aonv - +* cosisq - +* eoc - +* f220, f221, f311, f321, f322, f330, f441, f442, f522, f523, f542, f543 - +* g200, g201, g211, g300, g310, g322, g410, g422, g520, g521, g532, g533 - +* sini2 - +* temp - +* temp1 - +* theta - +* xno2 - +* +* coupling : +* getgravconst +* +* references : +* hoots, roehrich, norad spacetrack report #3 1980 +* hoots, norad spacetrack report #6 1986 +* hoots, schumacher and glover 2004 +* vallado, crawford, hujsak, kelso 2006 + ----------------------------------------------------------------------------*/ + +static void dsinit + ( + gravconsttype whichconst, + double cosim, double emsq, double argpo, double s1, double s2, + double s3, double s4, double s5, double sinim, double ss1, + double ss2, double ss3, double ss4, double ss5, double sz1, + double sz3, double sz11, double sz13, double sz21, double sz23, + double sz31, double sz33, double t, double tc, double gsto, + double mo, double mdot, double no, double nodeo, double nodedot, + double xpidot, double z1, double z3, double z11, double z13, + double z21, double z23, double z31, double z33, double ecco, + double eccsq, double& em, double& argpm, double& inclm, double& mm, + double& nm, double& nodem, + int& irez, + double& atime, double& d2201, double& d2211, double& d3210, double& d3222, + double& d4410, double& d4422, double& d5220, double& d5232, double& d5421, + double& d5433, double& dedt, double& didt, double& dmdt, double& dndt, + double& dnodt, double& domdt, double& del1, double& del2, double& del3, + double& xfact, double& xlamo, double& xli, double& xni + ) +{ + /* --------------------- local variables ------------------------ */ + const double twopi = 2.0 * pi; + + double ainv2 , aonv=0.0, cosisq, eoc, f220 , f221 , f311 , + f321 , f322 , f330 , f441 , f442 , f522 , f523 , + f542 , f543 , g200 , g201 , g211 , g300 , g310 , + g322 , g410 , g422 , g520 , g521 , g532 , g533 , + ses , sgs , sghl , sghs , shs , shll , sis , + sini2 , sls , temp , temp1 , theta , xno2 , q22 , + q31 , q33 , root22, root44, root54, rptim , root32, + root52, x2o3 , xke , znl , emo , zns , emsqo, + tumin, mu, radiusearthkm, j2, j3, j4, j3oj2; + + q22 = 1.7891679e-6; + q31 = 2.1460748e-6; + q33 = 2.2123015e-7; + root22 = 1.7891679e-6; + root44 = 7.3636953e-9; + root54 = 2.1765803e-9; + rptim = 4.37526908801129966e-3; // this equates to 7.29211514668855e-5 rad/sec + root32 = 3.7393792e-7; + root52 = 1.1428639e-7; + x2o3 = 2.0 / 3.0; + znl = 1.5835218e-4; + zns = 1.19459e-5; + + // sgp4fix identify constants and allow alternate values + getgravconst( whichconst, tumin, mu, radiusearthkm, xke, j2, j3, j4, j3oj2 ); + + /* -------------------- deep space initialization ------------ */ + irez = 0; + if ((nm < 0.0052359877) && (nm > 0.0034906585)) + irez = 1; + if ((nm >= 8.26e-3) && (nm <= 9.24e-3) && (em >= 0.5)) + irez = 2; + + /* ------------------------ do solar terms ------------------- */ + ses = ss1 * zns * ss5; + sis = ss2 * zns * (sz11 + sz13); + sls = -zns * ss3 * (sz1 + sz3 - 14.0 - 6.0 * emsq); + sghs = ss4 * zns * (sz31 + sz33 - 6.0); + shs = -zns * ss2 * (sz21 + sz23); + // sgp4fix for 180 deg incl + if ((inclm < 5.2359877e-2) || (inclm > pi - 5.2359877e-2)) + shs = 0.0; + if (sinim != 0.0) + shs = shs / sinim; + sgs = sghs - cosim * shs; + + /* ------------------------- do lunar terms ------------------ */ + dedt = ses + s1 * znl * s5; + didt = sis + s2 * znl * (z11 + z13); + dmdt = sls - znl * s3 * (z1 + z3 - 14.0 - 6.0 * emsq); + sghl = s4 * znl * (z31 + z33 - 6.0); + shll = -znl * s2 * (z21 + z23); + // sgp4fix for 180 deg incl + if ((inclm < 5.2359877e-2) || (inclm > pi - 5.2359877e-2)) + shll = 0.0; + domdt = sgs + sghl; + dnodt = shs; + if (sinim != 0.0) + { + domdt = domdt - cosim / sinim * shll; + dnodt = dnodt + shll / sinim; + } + + /* ----------- calculate deep space resonance effects -------- */ + dndt = 0.0; + theta = fmod(gsto + tc * rptim, twopi); + em = em + dedt * t; + inclm = inclm + didt * t; + argpm = argpm + domdt * t; + nodem = nodem + dnodt * t; + mm = mm + dmdt * t; + // sgp4fix for negative inclinations + // the following if statement should be commented out + //if (inclm < 0.0) + // { + // inclm = -inclm; + // argpm = argpm - pi; + // nodem = nodem + pi; + // } + + /* -------------- initialize the resonance terms ------------- */ + if (irez != 0) + { + aonv = pow(nm / xke, x2o3); + + /* ---------- geopotential resonance for 12 hour orbits ------ */ + if (irez == 2) + { + cosisq = cosim * cosim; + emo = em; + em = ecco; + emsqo = emsq; + emsq = eccsq; + eoc = em * emsq; + g201 = -0.306 - (em - 0.64) * 0.440; + + if (em <= 0.65) + { + g211 = 3.616 - 13.2470 * em + 16.2900 * emsq; + g310 = -19.302 + 117.3900 * em - 228.4190 * emsq + 156.5910 * eoc; + g322 = -18.9068 + 109.7927 * em - 214.6334 * emsq + 146.5816 * eoc; + g410 = -41.122 + 242.6940 * em - 471.0940 * emsq + 313.9530 * eoc; + g422 = -146.407 + 841.8800 * em - 1629.014 * emsq + 1083.4350 * eoc; + g520 = -532.114 + 3017.977 * em - 5740.032 * emsq + 3708.2760 * eoc; + } + else + { + g211 = -72.099 + 331.819 * em - 508.738 * emsq + 266.724 * eoc; + g310 = -346.844 + 1582.851 * em - 2415.925 * emsq + 1246.113 * eoc; + g322 = -342.585 + 1554.908 * em - 2366.899 * emsq + 1215.972 * eoc; + g410 = -1052.797 + 4758.686 * em - 7193.992 * emsq + 3651.957 * eoc; + g422 = -3581.690 + 16178.110 * em - 24462.770 * emsq + 12422.520 * eoc; + if (em > 0.715) + g520 =-5149.66 + 29936.92 * em - 54087.36 * emsq + 31324.56 * eoc; + else + g520 = 1464.74 - 4664.75 * em + 3763.64 * emsq; + } + if (em < 0.7) + { + g533 = -919.22770 + 4988.6100 * em - 9064.7700 * emsq + 5542.21 * eoc; + g521 = -822.71072 + 4568.6173 * em - 8491.4146 * emsq + 5337.524 * eoc; + g532 = -853.66600 + 4690.2500 * em - 8624.7700 * emsq + 5341.4 * eoc; + } + else + { + g533 =-37995.780 + 161616.52 * em - 229838.20 * emsq + 109377.94 * eoc; + g521 =-51752.104 + 218913.95 * em - 309468.16 * emsq + 146349.42 * eoc; + g532 =-40023.880 + 170470.89 * em - 242699.48 * emsq + 115605.82 * eoc; + } + + sini2= sinim * sinim; + f220 = 0.75 * (1.0 + 2.0 * cosim+cosisq); + f221 = 1.5 * sini2; + f321 = 1.875 * sinim * (1.0 - 2.0 * cosim - 3.0 * cosisq); + f322 = -1.875 * sinim * (1.0 + 2.0 * cosim - 3.0 * cosisq); + f441 = 35.0 * sini2 * f220; + f442 = 39.3750 * sini2 * sini2; + f522 = 9.84375 * sinim * (sini2 * (1.0 - 2.0 * cosim- 5.0 * cosisq) + + 0.33333333 * (-2.0 + 4.0 * cosim + 6.0 * cosisq) ); + f523 = sinim * (4.92187512 * sini2 * (-2.0 - 4.0 * cosim + + 10.0 * cosisq) + 6.56250012 * (1.0+2.0 * cosim - 3.0 * cosisq)); + f542 = 29.53125 * sinim * (2.0 - 8.0 * cosim+cosisq * + (-12.0 + 8.0 * cosim + 10.0 * cosisq)); + f543 = 29.53125 * sinim * (-2.0 - 8.0 * cosim+cosisq * + (12.0 + 8.0 * cosim - 10.0 * cosisq)); + xno2 = nm * nm; + ainv2 = aonv * aonv; + temp1 = 3.0 * xno2 * ainv2; + temp = temp1 * root22; + d2201 = temp * f220 * g201; + d2211 = temp * f221 * g211; + temp1 = temp1 * aonv; + temp = temp1 * root32; + d3210 = temp * f321 * g310; + d3222 = temp * f322 * g322; + temp1 = temp1 * aonv; + temp = 2.0 * temp1 * root44; + d4410 = temp * f441 * g410; + d4422 = temp * f442 * g422; + temp1 = temp1 * aonv; + temp = temp1 * root52; + d5220 = temp * f522 * g520; + d5232 = temp * f523 * g532; + temp = 2.0 * temp1 * root54; + d5421 = temp * f542 * g521; + d5433 = temp * f543 * g533; + xlamo = fmod(mo + nodeo + nodeo-theta - theta, twopi); + xfact = mdot + dmdt + 2.0 * (nodedot + dnodt - rptim) - no; + em = emo; + emsq = emsqo; + } + + /* ---------------- synchronous resonance terms -------------- */ + if (irez == 1) + { + g200 = 1.0 + emsq * (-2.5 + 0.8125 * emsq); + g310 = 1.0 + 2.0 * emsq; + g300 = 1.0 + emsq * (-6.0 + 6.60937 * emsq); + f220 = 0.75 * (1.0 + cosim) * (1.0 + cosim); + f311 = 0.9375 * sinim * sinim * (1.0 + 3.0 * cosim) - 0.75 * (1.0 + cosim); + f330 = 1.0 + cosim; + f330 = 1.875 * f330 * f330 * f330; + del1 = 3.0 * nm * nm * aonv * aonv; + del2 = 2.0 * del1 * f220 * g200 * q22; + del3 = 3.0 * del1 * f330 * g300 * q33 * aonv; + del1 = del1 * f311 * g310 * q31 * aonv; + xlamo = fmod(mo + nodeo + argpo - theta, twopi); + xfact = mdot + xpidot - rptim + dmdt + domdt + dnodt - no; + } + + /* ------------ for sgp4, initialize the integrator ---------- */ + xli = xlamo; + xni = no; + atime = 0.0; + nm = no + dndt; + } + +//#include "debug3.cpp" +} // end dsinit + +/*----------------------------------------------------------------------------- +* +* procedure dspace +* +* this procedure provides deep space contributions to mean elements for +* perturbing third body. these effects have been averaged over one +* revolution of the sun and moon. for earth resonance effects, the +* effects have been averaged over no revolutions of the satellite. +* (mean motion) +* +* author : david vallado 719-573-2600 28 jun 2005 +* +* inputs : +* d2201, d2211, d3210, d3222, d4410, d4422, d5220, d5232, d5421, d5433 - +* dedt - +* del1, del2, del3 - +* didt - +* dmdt - +* dnodt - +* domdt - +* irez - flag for resonance 0-none, 1-one day, 2-half day +* argpo - argument of perigee +* argpdot - argument of perigee dot (rate) +* t - time +* tc - +* gsto - gst +* xfact - +* xlamo - +* no - mean motion +* atime - +* em - eccentricity +* ft - +* argpm - argument of perigee +* inclm - inclination +* xli - +* mm - mean anomaly +* xni - mean motion +* nodem - right ascension of ascending node +* +* outputs : +* atime - +* em - eccentricity +* argpm - argument of perigee +* inclm - inclination +* xli - +* mm - mean anomaly +* xni - +* nodem - right ascension of ascending node +* dndt - +* nm - mean motion +* +* locals : +* delt - +* ft - +* theta - +* x2li - +* x2omi - +* xl - +* xldot - +* xnddt - +* xndt - +* xomi - +* +* coupling : +* none - +* +* references : +* hoots, roehrich, norad spacetrack report #3 1980 +* hoots, norad spacetrack report #6 1986 +* hoots, schumacher and glover 2004 +* vallado, crawford, hujsak, kelso 2006 + ----------------------------------------------------------------------------*/ + +static void dspace + ( + int irez, + double d2201, double d2211, double d3210, double d3222, double d4410, + double d4422, double d5220, double d5232, double d5421, double d5433, + double dedt, double del1, double del2, double del3, double didt, + double dmdt, double dnodt, double domdt, double argpo, double argpdot, + double t, double tc, double gsto, double xfact, double xlamo, + double no, + double& atime, double& em, double& argpm, double& inclm, double& xli, + double& mm, double& xni, double& nodem, double& dndt, double& nm + ) +{ + const double twopi = 2.0 * pi; + int iretn , iret; + double delt, ft, theta, x2li, x2omi, xl, xldot , xnddt, xndt, xomi, g22, g32, + g44, g52, g54, fasx2, fasx4, fasx6, rptim , step2, stepn , stepp; + + ft = 0.0; + fasx2 = 0.13130908; + fasx4 = 2.8843198; + fasx6 = 0.37448087; + g22 = 5.7686396; + g32 = 0.95240898; + g44 = 1.8014998; + g52 = 1.0508330; + g54 = 4.4108898; + rptim = 4.37526908801129966e-3; // this equates to 7.29211514668855e-5 rad/sec + stepp = 720.0; + stepn = -720.0; + step2 = 259200.0; + + /* ----------- calculate deep space resonance effects ----------- */ + dndt = 0.0; + theta = fmod(gsto + tc * rptim, twopi); + em = em + dedt * t; + + inclm = inclm + didt * t; + argpm = argpm + domdt * t; + nodem = nodem + dnodt * t; + mm = mm + dmdt * t; + + // sgp4fix for negative inclinations + // the following if statement should be commented out + // if (inclm < 0.0) + // { + // inclm = -inclm; + // argpm = argpm - pi; + // nodem = nodem + pi; + // } + + /* - update resonances : numerical (euler-maclaurin) integration - */ + /* ------------------------- epoch restart ---------------------- */ + // sgp4fix for propagator problems + // the following integration works for negative time steps and periods + // the specific changes are unknown because the original code was so convoluted + + ft = 0.0; + atime = 0.0; + if (irez != 0) + { + if ((atime == 0.0) || ((t >= 0.0) && (atime < 0.0)) || + ((t < 0.0) && (atime >= 0.0))) + { + if (t >= 0.0) + delt = stepp; + else + delt = stepn; + atime = 0.0; + xni = no; + xli = xlamo; + } + iretn = 381; // added for do loop + iret = 0; // added for loop + while (iretn == 381) + { + if ((fabs(t) < fabs(atime)) || (iret == 351)) + { + if (t >= 0.0) + delt = stepn; + else + delt = stepp; + iret = 351; + iretn = 381; + } + else + { + if (t > 0.0) // error if prev if has atime:=0.0 and t:=0.0 (ge) + delt = stepp; + else + delt = stepn; + if (fabs(t - atime) >= stepp) + { + iret = 0; + iretn = 381; + } + else + { + ft = t - atime; + iretn = 0; + } + } + + /* ------------------- dot terms calculated ------------- */ + /* ----------- near - synchronous resonance terms ------- */ + if (irez != 2) + { + xndt = del1 * sin(xli - fasx2) + del2 * sin(2.0 * (xli - fasx4)) + + del3 * sin(3.0 * (xli - fasx6)); + xldot = xni + xfact; + xnddt = del1 * cos(xli - fasx2) + + 2.0 * del2 * cos(2.0 * (xli - fasx4)) + + 3.0 * del3 * cos(3.0 * (xli - fasx6)); + xnddt = xnddt * xldot; + } + else + { + /* --------- near - half-day resonance terms -------- */ + xomi = argpo + argpdot * atime; + x2omi = xomi + xomi; + x2li = xli + xli; + xndt = d2201 * sin(x2omi + xli - g22) + d2211 * sin(xli - g22) + + d3210 * sin(xomi + xli - g32) + d3222 * sin(-xomi + xli - g32)+ + d4410 * sin(x2omi + x2li - g44)+ d4422 * sin(x2li - g44) + + d5220 * sin(xomi + xli - g52) + d5232 * sin(-xomi + xli - g52)+ + d5421 * sin(xomi + x2li - g54) + d5433 * sin(-xomi + x2li - g54); + xldot = xni + xfact; + xnddt = d2201 * cos(x2omi + xli - g22) + d2211 * cos(xli - g22) + + d3210 * cos(xomi + xli - g32) + d3222 * cos(-xomi + xli - g32) + + d5220 * cos(xomi + xli - g52) + d5232 * cos(-xomi + xli - g52) + + 2.0 * (d4410 * cos(x2omi + x2li - g44) + + d4422 * cos(x2li - g44) + d5421 * cos(xomi + x2li - g54) + + d5433 * cos(-xomi + x2li - g54)); + xnddt = xnddt * xldot; + } + + /* ----------------------- integrator ------------------- */ + if (iretn == 381) + { + xli = xli + xldot * delt + xndt * step2; + xni = xni + xndt * delt + xnddt * step2; + atime = atime + delt; + } + } // while iretn = 381 + + nm = xni + xndt * ft + xnddt * ft * ft * 0.5; + xl = xli + xldot * ft + xndt * ft * ft * 0.5; + if (irez != 1) + { + mm = xl - 2.0 * nodem + 2.0 * theta; + dndt = nm - no; + } + else + { + mm = xl - nodem - argpm + theta; + dndt = nm - no; + } + nm = no + dndt; + } + +//#include "debug4.cpp" +} // end dsspace + +/*----------------------------------------------------------------------------- +* +* procedure initl +* +* this procedure initializes the spg4 propagator. all the initialization is +* consolidated here instead of having multiple loops inside other routines. +* +* author : david vallado 719-573-2600 28 jun 2005 +* +* inputs : +* ecco - eccentricity 0.0 - 1.0 +* epoch - epoch time in days from jan 0, 1950. 0 hr +* inclo - inclination of satellite +* no - mean motion of satellite +* satn - satellite number +* +* outputs : +* ainv - 1.0 / a +* ao - semi major axis +* con41 - +* con42 - 1.0 - 5.0 cos(i) +* cosio - cosine of inclination +* cosio2 - cosio squared +* eccsq - eccentricity squared +* method - flag for deep space 'd', 'n' +* omeosq - 1.0 - ecco * ecco +* posq - semi-parameter squared +* rp - radius of perigee +* rteosq - square root of (1.0 - ecco*ecco) +* sinio - sine of inclination +* gsto - gst at time of observation rad +* no - mean motion of satellite +* +* locals : +* ak - +* d1 - +* del - +* adel - +* po - +* +* coupling : +* getgravconst +* gstime - find greenwich sidereal time from the julian date +* +* references : +* hoots, roehrich, norad spacetrack report #3 1980 +* hoots, norad spacetrack report #6 1986 +* hoots, schumacher and glover 2004 +* vallado, crawford, hujsak, kelso 2006 + ----------------------------------------------------------------------------*/ + +static void initl + ( + int satn, gravconsttype whichconst, + double ecco, double epoch, double inclo, double& no, + char& method, + double& ainv, double& ao, double& con41, double& con42, double& cosio, + double& cosio2,double& eccsq, double& omeosq, double& posq, + double& rp, double& rteosq,double& sinio , double& gsto + ) +{ + /* --------------------- local variables ------------------------ */ + double ak, d1, del, adel, po, x2o3, j2, xke, + tumin, mu, radiusearthkm, j3, j4, j3oj2; + + // sgp4fix use old way of finding gst + int ids70; + double ts70, ds70, tfrac, c1, thgr70, fk5r, c1p2p; + const double twopi = 2.0 * pi; + + /* ----------------------- earth constants ---------------------- */ + // sgp4fix identify constants and allow alternate values + getgravconst( whichconst, tumin, mu, radiusearthkm, xke, j2, j3, j4, j3oj2 ); + x2o3 = 2.0 / 3.0; + + /* ------------- calculate auxillary epoch quantities ---------- */ + eccsq = ecco * ecco; + omeosq = 1.0 - eccsq; + rteosq = sqrt(omeosq); + cosio = cos(inclo); + cosio2 = cosio * cosio; + + /* ------------------ un-kozai the mean motion ----------------- */ + ak = pow(xke / no, x2o3); + d1 = 0.75 * j2 * (3.0 * cosio2 - 1.0) / (rteosq * omeosq); + del = d1 / (ak * ak); + adel = ak * (1.0 - del * del - del * + (1.0 / 3.0 + 134.0 * del * del / 81.0)); + del = d1/(adel * adel); + no = no / (1.0 + del); + + ao = pow(xke / no, x2o3); + sinio = sin(inclo); + po = ao * omeosq; + con42 = 1.0 - 5.0 * cosio2; + con41 = -con42-cosio2-cosio2; + ainv = 1.0 / ao; + posq = po * po; + rp = ao * (1.0 - ecco); + method = 'n'; + + // sgp4fix modern approach to finding sidereal timew + // gsto = gstime(epoch + 2433281.5); + + // sgp4fix use old way of finding gst + // count integer number of days from 0 jan 1970 + ts70 = epoch - 7305.0; + ids70 = floor(ts70 + 1.0e-8); + ds70 = ids70; + tfrac = ts70 - ds70; + // find greenwich location at epoch + c1 = 1.72027916940703639e-2; + thgr70= 1.7321343856509374; + fk5r = 5.07551419432269442e-15; + c1p2p = c1 + twopi; + gsto = fmod( thgr70 + c1*ds70 + c1p2p*tfrac + ts70*ts70*fk5r, twopi); + if ( gsto < 0.0 ) + gsto = gsto + twopi; + +//#include "debug5.cpp" +} // end initl + +/*----------------------------------------------------------------------------- +* +* procedure sgp4init +* +* this procedure initializes variables for sgp4. +* +* author : david vallado 719-573-2600 28 jun 2005 +* +* inputs : +* satn - satellite number +* bstar - sgp4 type drag coefficient kg/m2er +* ecco - eccentricity +* epoch - epoch time in days from jan 0, 1950. 0 hr +* argpo - argument of perigee (output if ds) +* inclo - inclination +* mo - mean anomaly (output if ds) +* no - mean motion +* nodeo - right ascension of ascending node +* +* outputs : +* satrec - common values for subsequent calls +* return code - non-zero on error. +* 1 - mean elements, ecc >= 1.0 or ecc < -0.001 or a < 0.95 er +* 2 - mean motion less than 0.0 +* 3 - pert elements, ecc < 0.0 or ecc > 1.0 +* 4 - semi-latus rectum < 0.0 +* 5 - epoch elements are sub-orbital +* 6 - satellite has decayed +* +* locals : +* cnodm , snodm , cosim , sinim , cosomm , sinomm +* cc1sq , cc2 , cc3 +* coef , coef1 +* cosio4 - +* day - +* dndt - +* em - eccentricity +* emsq - eccentricity squared +* eeta - +* etasq - +* gam - +* argpm - argument of perigee +* nodem - +* inclm - inclination +* mm - mean anomaly +* nm - mean motion +* perige - perigee +* pinvsq - +* psisq - +* qzms24 - +* rtemsq - +* s1, s2, s3, s4, s5, s6, s7 - +* sfour - +* ss1, ss2, ss3, ss4, ss5, ss6, ss7 - +* sz1, sz2, sz3 +* sz11, sz12, sz13, sz21, sz22, sz23, sz31, sz32, sz33 - +* tc - +* temp - +* temp1, temp2, temp3 - +* tsi - +* xpidot - +* xhdot1 - +* z1, z2, z3 - +* z11, z12, z13, z21, z22, z23, z31, z32, z33 - +* +* coupling : +* getgravconst- +* initl - +* dscom - +* dpper - +* dsinit - +* sgp4 - +* +* references : +* hoots, roehrich, norad spacetrack report #3 1980 +* hoots, norad spacetrack report #6 1986 +* hoots, schumacher and glover 2004 +* vallado, crawford, hujsak, kelso 2006 + ----------------------------------------------------------------------------*/ + +int sgp4init + ( + gravconsttype whichconst, const int satn, const double epoch, + const double xbstar, const double xecco, const double xargpo, + const double xinclo, const double xmo, const double xno, + const double xnodeo, elsetrec& satrec + ) +{ + /* --------------------- local variables ------------------------ */ + double ao, ainv, con42, cosio, sinio, cosio2, eccsq, + omeosq, posq, rp, rteosq, + cnodm , snodm , cosim , sinim , cosomm, sinomm, cc1sq , + cc2 , cc3 , coef , coef1 , cosio4, day , dndt , + em , emsq , eeta , etasq , gam , argpm , nodem , + inclm , mm , nm , perige, pinvsq, psisq , qzms24, + rtemsq, s1 , s2 , s3 , s4 , s5 , s6 , + s7 , sfour , ss1 = 0 , ss2 = 0 , ss3 = 0 , ss4 = 0 , ss5 = 0 , + ss6 = 0 , ss7 = 0 , sz1 = 0 , sz2 = 0 , sz3 = 0 , sz11 = 0 , sz12 = 0 , + sz13 = 0 , sz21 = 0 , sz22 = 0 , sz23 = 0 , sz31 = 0 , sz32 = 0 , sz33 = 0 , + tc , temp , temp1 , temp2 , temp3 , tsi , xpidot, + xhdot1, z1 , z2 , z3 , z11 , z12 , z13 , + z21 , z22 , z23 , z31 , z32 , z33, + qzms2t, ss, j2, j3oj2, j4, x2o3, r[3], v[3], + tumin, mu, radiusearthkm, xke, j3; + + /* ------------------------ initialization --------------------- */ + // sgp4fix divisor for divide by zero check on inclination + const double temp4 = 1.0 + cos(pi-1.0e-9); + + /* ----------- set all near earth variables to zero ------------ */ + satrec.isimp = 0; satrec.method = 'n'; satrec.aycof = 0.0; + satrec.con41 = 0.0; satrec.cc1 = 0.0; satrec.cc4 = 0.0; + satrec.cc5 = 0.0; satrec.d2 = 0.0; satrec.d3 = 0.0; + satrec.d4 = 0.0; satrec.delmo = 0.0; satrec.eta = 0.0; + satrec.argpdot = 0.0; satrec.omgcof = 0.0; satrec.sinmao = 0.0; + satrec.t = 0.0; satrec.t2cof = 0.0; satrec.t3cof = 0.0; + satrec.t4cof = 0.0; satrec.t5cof = 0.0; satrec.x1mth2 = 0.0; + satrec.x7thm1 = 0.0; satrec.mdot = 0.0; satrec.nodedot = 0.0; + satrec.xlcof = 0.0; satrec.xmcof = 0.0; satrec.nodecf = 0.0; + + /* ----------- set all deep space variables to zero ------------ */ + satrec.irez = 0; satrec.d2201 = 0.0; satrec.d2211 = 0.0; + satrec.d3210 = 0.0; satrec.d3222 = 0.0; satrec.d4410 = 0.0; + satrec.d4422 = 0.0; satrec.d5220 = 0.0; satrec.d5232 = 0.0; + satrec.d5421 = 0.0; satrec.d5433 = 0.0; satrec.dedt = 0.0; + satrec.del1 = 0.0; satrec.del2 = 0.0; satrec.del3 = 0.0; + satrec.didt = 0.0; satrec.dmdt = 0.0; satrec.dnodt = 0.0; + satrec.domdt = 0.0; satrec.e3 = 0.0; satrec.ee2 = 0.0; + satrec.peo = 0.0; satrec.pgho = 0.0; satrec.pho = 0.0; + satrec.pinco = 0.0; satrec.plo = 0.0; satrec.se2 = 0.0; + satrec.se3 = 0.0; satrec.sgh2 = 0.0; satrec.sgh3 = 0.0; + satrec.sgh4 = 0.0; satrec.sh2 = 0.0; satrec.sh3 = 0.0; + satrec.si2 = 0.0; satrec.si3 = 0.0; satrec.sl2 = 0.0; + satrec.sl3 = 0.0; satrec.sl4 = 0.0; satrec.gsto = 0.0; + satrec.xfact = 0.0; satrec.xgh2 = 0.0; satrec.xgh3 = 0.0; + satrec.xgh4 = 0.0; satrec.xh2 = 0.0; satrec.xh3 = 0.0; + satrec.xi2 = 0.0; satrec.xi3 = 0.0; satrec.xl2 = 0.0; + satrec.xl3 = 0.0; satrec.xl4 = 0.0; satrec.xlamo = 0.0; + satrec.zmol = 0.0; satrec.zmos = 0.0; satrec.atime = 0.0; + satrec.xli = 0.0; satrec.xni = 0.0; + + // sgp4fix - note the following variables are also passed directly via satrec. + // it is possible to streamline the sgp4init call by deleting the "x" + // variables, but the user would need to set the satrec.* values first. we + // include the additional assignments in case twoline2rv is not used. + satrec.bstar = xbstar; + satrec.ecco = xecco; + satrec.argpo = xargpo; + satrec.inclo = xinclo; + satrec.mo = xmo; + satrec.no = xno; + satrec.nodeo = xnodeo; + + /* ------------------------ earth constants ----------------------- */ + // sgp4fix identify constants and allow alternate values + getgravconst( whichconst, tumin, mu, radiusearthkm, xke, j2, j3, j4, j3oj2 ); + ss = 78.0 / radiusearthkm + 1.0; + qzms2t = pow(((120.0 - 78.0) / radiusearthkm), 4); + x2o3 = 2.0 / 3.0; + + satrec.init = 'y'; + satrec.t = 0.0; + + initl + ( + satn, whichconst, satrec.ecco, epoch, satrec.inclo, satrec.no, satrec.method, + ainv, ao, satrec.con41, con42, cosio, cosio2, eccsq, omeosq, + posq, rp, rteosq, sinio, satrec.gsto + ); + satrec.error = 0; + + if (rp < 1.0) + { +// printf("# *** satn%d epoch elts sub-orbital ***\n", satn); + satrec.error = 5; + } + + if ((omeosq >= 0.0 ) || ( satrec.no >= 0.0)) + { + satrec.isimp = 0; + if (rp < (220.0 / radiusearthkm + 1.0)) + satrec.isimp = 1; + sfour = ss; + qzms24 = qzms2t; + perige = (rp - 1.0) * radiusearthkm; + + /* - for perigees below 156 km, s and qoms2t are altered - */ + if (perige < 156.0) + { + sfour = perige - 78.0; + if (perige < 98.0) + sfour = 20.0; + qzms24 = pow(((120.0 - sfour) / radiusearthkm), 4.0); + sfour = sfour / radiusearthkm + 1.0; + } + pinvsq = 1.0 / posq; + + tsi = 1.0 / (ao - sfour); + satrec.eta = ao * satrec.ecco * tsi; + etasq = satrec.eta * satrec.eta; + eeta = satrec.ecco * satrec.eta; + psisq = fabs(1.0 - etasq); + coef = qzms24 * pow(tsi, 4.0); + coef1 = coef / pow(psisq, 3.5); + cc2 = coef1 * satrec.no * (ao * (1.0 + 1.5 * etasq + eeta * + (4.0 + etasq)) + 0.375 * j2 * tsi / psisq * satrec.con41 * + (8.0 + 3.0 * etasq * (8.0 + etasq))); + satrec.cc1 = satrec.bstar * cc2; + cc3 = 0.0; + if (satrec.ecco > 1.0e-4) + cc3 = -2.0 * coef * tsi * j3oj2 * satrec.no * sinio / satrec.ecco; + satrec.x1mth2 = 1.0 - cosio2; + satrec.cc4 = 2.0* satrec.no * coef1 * ao * omeosq * + (satrec.eta * (2.0 + 0.5 * etasq) + satrec.ecco * + (0.5 + 2.0 * etasq) - j2 * tsi / (ao * psisq) * + (-3.0 * satrec.con41 * (1.0 - 2.0 * eeta + etasq * + (1.5 - 0.5 * eeta)) + 0.75 * satrec.x1mth2 * + (2.0 * etasq - eeta * (1.0 + etasq)) * cos(2.0 * satrec.argpo))); + satrec.cc5 = 2.0 * coef1 * ao * omeosq * (1.0 + 2.75 * + (etasq + eeta) + eeta * etasq); + cosio4 = cosio2 * cosio2; + temp1 = 1.5 * j2 * pinvsq * satrec.no; + temp2 = 0.5 * temp1 * j2 * pinvsq; + temp3 = -0.46875 * j4 * pinvsq * pinvsq * satrec.no; + satrec.mdot = satrec.no + 0.5 * temp1 * rteosq * satrec.con41 + 0.0625 * + temp2 * rteosq * (13.0 - 78.0 * cosio2 + 137.0 * cosio4); + satrec.argpdot = -0.5 * temp1 * con42 + 0.0625 * temp2 * + (7.0 - 114.0 * cosio2 + 395.0 * cosio4) + + temp3 * (3.0 - 36.0 * cosio2 + 49.0 * cosio4); + xhdot1 = -temp1 * cosio; + satrec.nodedot = xhdot1 + (0.5 * temp2 * (4.0 - 19.0 * cosio2) + + 2.0 * temp3 * (3.0 - 7.0 * cosio2)) * cosio; + xpidot = satrec.argpdot+ satrec.nodedot; + satrec.omgcof = satrec.bstar * cc3 * cos(satrec.argpo); + satrec.xmcof = 0.0; + if (satrec.ecco > 1.0e-4) + satrec.xmcof = -x2o3 * coef * satrec.bstar / eeta; + satrec.nodecf = 3.5 * omeosq * xhdot1 * satrec.cc1; + satrec.t2cof = 1.5 * satrec.cc1; + // sgp4fix for divide by zero with xinco = 180 deg + if (fabs(cosio+1.0) > 1.5e-12) + satrec.xlcof = -0.25 * j3oj2 * sinio * (3.0 + 5.0 * cosio) / (1.0 + cosio); + else + satrec.xlcof = -0.25 * j3oj2 * sinio * (3.0 + 5.0 * cosio) / temp4; + satrec.aycof = -0.5 * j3oj2 * sinio; + satrec.delmo = pow((1.0 + satrec.eta * cos(satrec.mo)), 3); + satrec.sinmao = sin(satrec.mo); + satrec.x7thm1 = 7.0 * cosio2 - 1.0; + + /* --------------- deep space initialization ------------- */ + if ((2*pi / satrec.no) >= 225.0) + { + satrec.method = 'd'; + satrec.isimp = 1; + tc = 0.0; + inclm = satrec.inclo; + + dscom + ( + epoch, satrec.ecco, satrec.argpo, tc, satrec.inclo, satrec.nodeo, + satrec.no, snodm, cnodm, sinim, cosim,sinomm, cosomm, + day, satrec.e3, satrec.ee2, em, emsq, gam, + satrec.peo, satrec.pgho, satrec.pho, satrec.pinco, + satrec.plo, rtemsq, satrec.se2, satrec.se3, + satrec.sgh2, satrec.sgh3, satrec.sgh4, + satrec.sh2, satrec.sh3, satrec.si2, satrec.si3, + satrec.sl2, satrec.sl3, satrec.sl4, s1, s2, s3, s4, s5, + s6, s7, ss1, ss2, ss3, ss4, ss5, ss6, ss7, sz1, sz2, sz3, + sz11, sz12, sz13, sz21, sz22, sz23, sz31, sz32, sz33, + satrec.xgh2, satrec.xgh3, satrec.xgh4, satrec.xh2, + satrec.xh3, satrec.xi2, satrec.xi3, satrec.xl2, + satrec.xl3, satrec.xl4, nm, z1, z2, z3, z11, + z12, z13, z21, z22, z23, z31, z32, z33, + satrec.zmol, satrec.zmos + ); + dpper + ( + satrec.e3, satrec.ee2, satrec.peo, satrec.pgho, + satrec.pho, satrec.pinco, satrec.plo, satrec.se2, + satrec.se3, satrec.sgh2, satrec.sgh3, satrec.sgh4, + satrec.sh2, satrec.sh3, satrec.si2, satrec.si3, + satrec.sl2, satrec.sl3, satrec.sl4, satrec.t, + satrec.xgh2,satrec.xgh3,satrec.xgh4, satrec.xh2, + satrec.xh3, satrec.xi2, satrec.xi3, satrec.xl2, + satrec.xl3, satrec.xl4, satrec.zmol, satrec.zmos, inclm, satrec.init, + satrec.ecco, satrec.inclo, satrec.nodeo, satrec.argpo, satrec.mo + ); + + argpm = 0.0; + nodem = 0.0; + mm = 0.0; + + dsinit + ( + whichconst, + cosim, emsq, satrec.argpo, s1, s2, s3, s4, s5, sinim, ss1, ss2, ss3, ss4, + ss5, sz1, sz3, sz11, sz13, sz21, sz23, sz31, sz33, satrec.t, tc, + satrec.gsto, satrec.mo, satrec.mdot, satrec.no, satrec.nodeo, + satrec.nodedot, xpidot, z1, z3, z11, z13, z21, z23, z31, z33, + satrec.ecco, eccsq, em, argpm, inclm, mm, nm, nodem, + satrec.irez, satrec.atime, + satrec.d2201, satrec.d2211, satrec.d3210, satrec.d3222 , + satrec.d4410, satrec.d4422, satrec.d5220, satrec.d5232, + satrec.d5421, satrec.d5433, satrec.dedt, satrec.didt, + satrec.dmdt, dndt, satrec.dnodt, satrec.domdt , + satrec.del1, satrec.del2, satrec.del3, satrec.xfact, + satrec.xlamo, satrec.xli, satrec.xni + ); + } + + /* ----------- set variables if not deep space ----------- */ + if (satrec.isimp != 1) + { + cc1sq = satrec.cc1 * satrec.cc1; + satrec.d2 = 4.0 * ao * tsi * cc1sq; + temp = satrec.d2 * tsi * satrec.cc1 / 3.0; + satrec.d3 = (17.0 * ao + sfour) * temp; + satrec.d4 = 0.5 * temp * ao * tsi * (221.0 * ao + 31.0 * sfour) * + satrec.cc1; + satrec.t3cof = satrec.d2 + 2.0 * cc1sq; + satrec.t4cof = 0.25 * (3.0 * satrec.d3 + satrec.cc1 * + (12.0 * satrec.d2 + 10.0 * cc1sq)); + satrec.t5cof = 0.2 * (3.0 * satrec.d4 + + 12.0 * satrec.cc1 * satrec.d3 + + 6.0 * satrec.d2 * satrec.d2 + + 15.0 * cc1sq * (2.0 * satrec.d2 + cc1sq)); + } + } // if omeosq = 0 ... + + /* finally propogate to zero epoch to initialise all others. */ + if(satrec.error == 0) + sgp4(whichconst, satrec, 0.0, r, v); + + satrec.init = 'n'; + +//#include "debug6.cpp" + return satrec.error; +} // end sgp4init + +/*----------------------------------------------------------------------------- +* +* procedure sgp4 +* +* this procedure is the sgp4 prediction model from space command. this is an +* updated and combined version of sgp4 and sdp4, which were originally +* published separately in spacetrack report #3. this version follows the +* methodology from the aiaa paper (2006) describing the history and +* development of the code. +* +* author : david vallado 719-573-2600 28 jun 2005 +* +* inputs : +* satrec - initialised structure from sgp4init() call. +* tsince - time eince epoch (minutes) +* +* outputs : +* r - position vector km +* v - velocity km/sec +* return code - non-zero on error. +* 1 - mean elements, ecc >= 1.0 or ecc < -0.001 or a < 0.95 er +* 2 - mean motion less than 0.0 +* 3 - pert elements, ecc < 0.0 or ecc > 1.0 +* 4 - semi-latus rectum < 0.0 +* 5 - epoch elements are sub-orbital +* 6 - satellite has decayed +* +* locals : +* am - +* axnl, aynl - +* betal - +* cosim , sinim , cosomm , sinomm , cnod , snod , cos2u , +* sin2u , coseo1 , sineo1 , cosi , sini , cosip , sinip , +* cosisq , cossu , sinsu , cosu , sinu +* delm - +* delomg - +* dndt - +* eccm - +* emsq - +* ecose - +* el2 - +* eo1 - +* eccp - +* esine - +* argpm - +* argpp - +* omgadf - +* pl - +* r - +* rtemsq - +* rdotl - +* rl - +* rvdot - +* rvdotl - +* su - +* t2 , t3 , t4 , tc +* tem5, temp , temp1 , temp2 , tempa , tempe , templ +* u , ux , uy , uz , vx , vy , vz +* inclm - inclination +* mm - mean anomaly +* nm - mean motion +* nodem - right asc of ascending node +* xinc - +* xincp - +* xl - +* xlm - +* mp - +* xmdf - +* xmx - +* xmy - +* nodedf - +* xnode - +* nodep - +* np - +* +* coupling : +* getgravconst- +* dpper +* dpspace +* +* references : +* hoots, roehrich, norad spacetrack report #3 1980 +* hoots, norad spacetrack report #6 1986 +* hoots, schumacher and glover 2004 +* vallado, crawford, hujsak, kelso 2006 + ----------------------------------------------------------------------------*/ + +int sgp4 + ( + gravconsttype whichconst, elsetrec& satrec, double tsince, + double r[3], double v[3] + ) +{ + double am , axnl , aynl , betal , cosim , cnod , + cos2u, coseo1, cosi , cosip , cosisq, cossu , cosu, + delm , delomg, em , emsq , ecose , el2 , eo1 , + ep , esine , argpm, argpp , argpdf, pl, mrt = 0.0, + mvt , rdotl , rl , rvdot , rvdotl, sinim , + sin2u, sineo1, sini , sinip , sinsu , sinu , + snod , su , t2 , t3 , t4 , tem5 , temp, + temp1, temp2 , tempa, tempe , templ , u , ux , + uy , uz , vx , vy , vz , inclm , mm , + nm , nodem, xinc , xincp , xl , xlm , mp , + xmdf , xmx , xmy , nodedf, xnode , nodep, tc , dndt, + twopi, x2o3 , j2 , j3 , tumin, j4 , xke , j3oj2, radiusearthkm, + mu, vkmpersec; + int ktr; + + /* ------------------ set mathematical constants --------------- */ + // sgp4fix divisor for divide by zero check on inclination + const double temp4 = 1.0 + cos(pi-1.0e-9); + twopi = 2.0 * pi; + x2o3 = 2.0 / 3.0; + // sgp4fix identify constants and allow alternate values + getgravconst( whichconst, tumin, mu, radiusearthkm, xke, j2, j3, j4, j3oj2 ); + vkmpersec = radiusearthkm * xke/60.0; + + /* --------------------- clear sgp4 error flag ----------------- */ + satrec.t = tsince; + satrec.error = 0; + + /* ------- update for secular gravity and atmospheric drag ----- */ + xmdf = satrec.mo + satrec.mdot * satrec.t; + argpdf = satrec.argpo + satrec.argpdot * satrec.t; + nodedf = satrec.nodeo + satrec.nodedot * satrec.t; + argpm = argpdf; + mm = xmdf; + t2 = satrec.t * satrec.t; + nodem = nodedf + satrec.nodecf * t2; + tempa = 1.0 - satrec.cc1 * satrec.t; + tempe = satrec.bstar * satrec.cc4 * satrec.t; + templ = satrec.t2cof * t2; + + if (satrec.isimp != 1) + { + delomg = satrec.omgcof * satrec.t; + delm = satrec.xmcof * + (pow((1.0 + satrec.eta * cos(xmdf)), 3) - + satrec.delmo); + temp = delomg + delm; + mm = xmdf + temp; + argpm = argpdf - temp; + t3 = t2 * satrec.t; + t4 = t3 * satrec.t; + tempa = tempa - satrec.d2 * t2 - satrec.d3 * t3 - + satrec.d4 * t4; + tempe = tempe + satrec.bstar * satrec.cc5 * (sin(mm) - + satrec.sinmao); + templ = templ + satrec.t3cof * t3 + t4 * (satrec.t4cof + + satrec.t * satrec.t5cof); + } + + nm = satrec.no; + em = satrec.ecco; + inclm = satrec.inclo; + if (satrec.method == 'd') + { + tc = satrec.t; + dspace + ( + satrec.irez, + satrec.d2201, satrec.d2211, satrec.d3210, + satrec.d3222, satrec.d4410, satrec.d4422, + satrec.d5220, satrec.d5232, satrec.d5421, + satrec.d5433, satrec.dedt, satrec.del1, + satrec.del2, satrec.del3, satrec.didt, + satrec.dmdt, satrec.dnodt, satrec.domdt, + satrec.argpo, satrec.argpdot, satrec.t, tc, + satrec.gsto, satrec.xfact, satrec.xlamo, + satrec.no, satrec.atime, + em, argpm, inclm, satrec.xli, mm, satrec.xni, + nodem, dndt, nm + ); + } // if method = d + + if (nm <= 0.0) + { +// printf("# error nm %f\n", nm); + satrec.error = 2; + } + am = pow((xke / nm),x2o3) * tempa * tempa; + nm = xke / pow(am, 1.5); + em = em - tempe; + + // fix tolerance for error recognition + if ((em >= 1.0) || (em < -0.001) || (am < 0.95)) + { +// printf("# error em %f\n", em); + satrec.error = 1; + } + if (em < 0.0) + em = 1.0e-6; + mm = mm + satrec.no * templ; + xlm = mm + argpm + nodem; + emsq = em * em; + temp = 1.0 - emsq; + + nodem = fmod(nodem, twopi); + argpm = fmod(argpm, twopi); + xlm = fmod(xlm, twopi); + mm = fmod(xlm - argpm - nodem, twopi); + + /* ----------------- compute extra mean quantities ------------- */ + sinim = sin(inclm); + cosim = cos(inclm); + + /* -------------------- add lunar-solar periodics -------------- */ + ep = em; + xincp = inclm; + argpp = argpm; + nodep = nodem; + mp = mm; + sinip = sinim; + cosip = cosim; + if (satrec.method == 'd') + { + dpper + ( + satrec.e3, satrec.ee2, satrec.peo, + satrec.pgho, satrec.pho, satrec.pinco, + satrec.plo, satrec.se2, satrec.se3, + satrec.sgh2, satrec.sgh3, satrec.sgh4, + satrec.sh2, satrec.sh3, satrec.si2, + satrec.si3, satrec.sl2, satrec.sl3, + satrec.sl4, satrec.t, satrec.xgh2, + satrec.xgh3, satrec.xgh4, satrec.xh2, + satrec.xh3, satrec.xi2, satrec.xi3, + satrec.xl2, satrec.xl3, satrec.xl4, + satrec.zmol, satrec.zmos, satrec.inclo, + 'n', ep, xincp, nodep, argpp, mp + ); + if (xincp < 0.0) + { + xincp = -xincp; + nodep = nodep + pi; + argpp = argpp - pi; + } + if ((ep < 0.0 ) || ( ep > 1.0)) + { + // printf("# error ep %f\n", ep); + satrec.error = 3; + } + } // if method = d + + /* -------------------- long period periodics ------------------ */ + if (satrec.method == 'd') + { + sinip = sin(xincp); + cosip = cos(xincp); + satrec.aycof = -0.5*j3oj2*sinip; + // sgp4fix for divide by zero for xincp = 180 deg + if (fabs(cosip+1.0) > 1.5e-12) + satrec.xlcof = -0.25 * j3oj2 * sinip * (3.0 + 5.0 * cosip) / (1.0 + cosip); + else + satrec.xlcof = -0.25 * j3oj2 * sinip * (3.0 + 5.0 * cosip) / temp4; + } + axnl = ep * cos(argpp); + temp = 1.0 / (am * (1.0 - ep * ep)); + aynl = ep* sin(argpp) + temp * satrec.aycof; + xl = mp + argpp + nodep + temp * satrec.xlcof * axnl; + + /* --------------------- solve kepler's equation --------------- */ + u = fmod(xl - nodep, twopi); + eo1 = u; + tem5 = 9999.9; + ktr = 1; + // sgp4fix for kepler iteration + // the following iteration needs better limits on corrections + while (( fabs(tem5) >= 1.0e-12) && (ktr <= 10) ) + { + sineo1 = sin(eo1); + coseo1 = cos(eo1); + tem5 = 1.0 - coseo1 * axnl - sineo1 * aynl; + tem5 = (u - aynl * coseo1 + axnl * sineo1 - eo1) / tem5; + if(fabs(tem5) >= 0.95) + tem5 = tem5 > 0.0 ? 0.95 : -0.95; + eo1 = eo1 + tem5; + ktr = ktr + 1; + } + + /* ------------- short period preliminary quantities ----------- */ + ecose = axnl*coseo1 + aynl*sineo1; + esine = axnl*sineo1 - aynl*coseo1; + el2 = axnl*axnl + aynl*aynl; + pl = am*(1.0-el2); + if (pl < 0.0) + { +// printf("# error pl %f\n", pl); + satrec.error = 4; + } + else + { + rl = am * (1.0 - ecose); + rdotl = sqrt(am) * esine/rl; + rvdotl = sqrt(pl) / rl; + betal = sqrt(1.0 - el2); + temp = esine / (1.0 + betal); + sinu = am / rl * (sineo1 - aynl - axnl * temp); + cosu = am / rl * (coseo1 - axnl + aynl * temp); + su = atan2(sinu, cosu); + sin2u = (cosu + cosu) * sinu; + cos2u = 1.0 - 2.0 * sinu * sinu; + temp = 1.0 / pl; + temp1 = 0.5 * j2 * temp; + temp2 = temp1 * temp; + + /* -------------- update for short period periodics ------------ */ + if (satrec.method == 'd') + { + cosisq = cosip * cosip; + satrec.con41 = 3.0*cosisq - 1.0; + satrec.x1mth2 = 1.0 - cosisq; + satrec.x7thm1 = 7.0*cosisq - 1.0; + } + mrt = rl * (1.0 - 1.5 * temp2 * betal * satrec.con41) + + 0.5 * temp1 * satrec.x1mth2 * cos2u; + su = su - 0.25 * temp2 * satrec.x7thm1 * sin2u; + xnode = nodep + 1.5 * temp2 * cosip * sin2u; + xinc = xincp + 1.5 * temp2 * cosip * sinip * cos2u; + mvt = rdotl - nm * temp1 * satrec.x1mth2 * sin2u / xke; + rvdot = rvdotl + nm * temp1 * (satrec.x1mth2 * cos2u + + 1.5 * satrec.con41) / xke; + + /* --------------------- orientation vectors ------------------- */ + sinsu = sin(su); + cossu = cos(su); + snod = sin(xnode); + cnod = cos(xnode); + sini = sin(xinc); + cosi = cos(xinc); + xmx = -snod * cosi; + xmy = cnod * cosi; + ux = xmx * sinsu + cnod * cossu; + uy = xmy * sinsu + snod * cossu; + uz = sini * sinsu; + vx = xmx * cossu - cnod * sinsu; + vy = xmy * cossu - snod * sinsu; + vz = sini * cossu; + + /* --------- position and velocity (in km and km/sec) ---------- */ + r[0] = (mrt * ux)* radiusearthkm; + r[1] = (mrt * uy)* radiusearthkm; + r[2] = (mrt * uz)* radiusearthkm; + v[0] = (mvt * ux + rvdot * vx) * vkmpersec; + v[1] = (mvt * uy + rvdot * vy) * vkmpersec; + v[2] = (mvt * uz + rvdot * vz) * vkmpersec; + } // if pl > 0 + + // sgp4fix for decaying satellites + if (mrt < 1.0) + { +// printf("# decay condition %11.6f \n",mrt); + satrec.error = 6; + } + + +//#include "debug7.cpp" + return satrec.error; +} // end sgp4 + + +/* ----------------------------------------------------------------------------- +* +* function gstime +* +* this function finds the greenwich sidereal time. +* +* author : david vallado 719-573-2600 1 mar 2001 +* +* inputs description range / units +* jdut1 - julian date in ut1 days from 4713 bc +* +* outputs : +* gstime - greenwich sidereal time 0 to 2pi rad +* +* locals : +* temp - temporary variable for doubles rad +* tut1 - julian centuries from the +* jan 1, 2000 12 h epoch (ut1) +* +* coupling : +* none +* +* references : +* vallado 2004, 191, eq 3-45 +* --------------------------------------------------------------------------- */ + +double gstime + ( + double jdut1 + ) + { + const double twopi = 2.0 * pi; + const double deg2rad = pi / 180.0; + double temp, tut1; + + tut1 = (jdut1 - 2451545.0) / 36525.0; + temp = -6.2e-6* tut1 * tut1 * tut1 + 0.093104 * tut1 * tut1 + + (876600.0*3600 + 8640184.812866) * tut1 + 67310.54841; // sec + temp = fmod(temp * deg2rad / 240.0, twopi); //360/86400 = 1/240, to deg, to rad + + // ------------------------ check quadrants --------------------- + if (temp < 0.0) + temp += twopi; + + return temp; + } // end gstime + +/* ----------------------------------------------------------------------------- +* +* function getgravconst +* +* this function gets constants for the propagator. note that mu is identified to +* facilitiate comparisons with newer models. the common useage is wgs72. +* +* author : david vallado 719-573-2600 21 jul 2006 +* +* inputs : +* whichconst - which set of constants to use wgs72old, wgs72, wgs84 +* +* outputs : +* tumin - minutes in one time unit +* mu - earth gravitational parameter +* radiusearthkm - radius of the earth in km +* xke - reciprocal of tumin +* j2, j3, j4 - un-normalized zonal harmonic values +* j3oj2 - j3 divided by j2 +* +* locals : +* +* coupling : +* none +* +* references : +* norad spacetrack report #3 +* vallado, crawford, hujsak, kelso 2006 + --------------------------------------------------------------------------- */ + +void getgravconst + ( + gravconsttype whichconst, + double& tumin, + double& mu, + double& radiusearthkm, + double& xke, + double& j2, + double& j3, + double& j4, + double& j3oj2 + ) + { + + switch (whichconst) + { + // -- wgs-72 low precision str#3 constants -- + case wgs72old: + mu = 398600.79964; // in km3 / s2 + radiusearthkm = 6378.135; // km + xke = 0.0743669161; + tumin = 1.0 / xke; + j2 = 0.001082616; + j3 = -0.00000253881; + j4 = -0.00000165597; + j3oj2 = j3 / j2; + break; + // ------------ wgs-72 constants ------------ + case wgs72: + mu = 398600.8; // in km3 / s2 + radiusearthkm = 6378.135; // km + xke = 60.0 / sqrt(radiusearthkm*radiusearthkm*radiusearthkm/mu); + tumin = 1.0 / xke; + j2 = 0.001082616; + j3 = -0.00000253881; + j4 = -0.00000165597; + j3oj2 = j3 / j2; + break; + case wgs84: + // ------------ wgs-84 constants ------------ + mu = 398600.5; // in km3 / s2 + radiusearthkm = 6378.137; // km + xke = 60.0 / sqrt(radiusearthkm*radiusearthkm*radiusearthkm/mu); + tumin = 1.0 / xke; + j2 = 0.00108262998905; + j3 = -0.00000253215306; + j4 = -0.00000161098761; + j3oj2 = j3 / j2; + break; + default: + fprintf(stderr,"unknown gravity option (%d)\n",whichconst); + break; + } + + } // end getgravconst + + + + + diff --git a/fsfw/contrib/sgp4/sgp4unit.h b/fsfw/contrib/sgp4/sgp4unit.h new file mode 100644 index 0000000..f16acbd --- /dev/null +++ b/fsfw/contrib/sgp4/sgp4unit.h @@ -0,0 +1,117 @@ +#ifndef _sgp4unit_ +#define _sgp4unit_ +/* ---------------------------------------------------------------- +* +* sgp4unit.h +* +* this file contains the sgp4 procedures for analytical propagation +* of a satellite. the code was originally released in the 1980 and 1986 +* spacetrack papers. a detailed discussion of the theory and history +* may be found in the 2006 aiaa paper by vallado, crawford, hujsak, +* and kelso. +* +* companion code for +* fundamentals of astrodynamics and applications +* 2007 +* by david vallado +* +* (w) 719-573-2600, email dvallado@agi.com +* +* current : +* 20 apr 07 david vallado +* misc fixes for constants +* changes : +* 11 aug 06 david vallado +* chg lyddane choice back to strn3, constants, misc doc +* 15 dec 05 david vallado +* misc fixes +* 26 jul 05 david vallado +* fixes for paper +* note that each fix is preceded by a +* comment with "sgp4fix" and an explanation of +* what was changed +* 10 aug 04 david vallado +* 2nd printing baseline working +* 14 may 01 david vallado +* 2nd edition baseline +* 80 norad +* original baseline +* ---------------------------------------------------------------- */ + +#include +#include + +// -------------------------- structure declarations ---------------------------- +typedef enum +{ + wgs72old, + wgs72, + wgs84 +} gravconsttype; + +typedef struct elsetrec +{ + long int satnum; + int epochyr, epochtynumrev; + int error; + char init, method; + + /* Near Earth */ + int isimp; + double aycof , con41 , cc1 , cc4 , cc5 , d2 , d3 , d4 , + delmo , eta , argpdot, omgcof , sinmao , t , t2cof, t3cof , + t4cof , t5cof , x1mth2 , x7thm1 , mdot , nodedot, xlcof , xmcof , + nodecf; + + /* Deep Space */ + int irez; + double d2201 , d2211 , d3210 , d3222 , d4410 , d4422 , d5220 , d5232 , + d5421 , d5433 , dedt , del1 , del2 , del3 , didt , dmdt , + dnodt , domdt , e3 , ee2 , peo , pgho , pho , pinco , + plo , se2 , se3 , sgh2 , sgh3 , sgh4 , sh2 , sh3 , + si2 , si3 , sl2 , sl3 , sl4 , gsto , xfact , xgh2 , + xgh3 , xgh4 , xh2 , xh3 , xi2 , xi3 , xl2 , xl3 , + xl4 , xlamo , zmol , zmos , atime , xli , xni; + + double a , altp , alta , epochdays, jdsatepoch , nddot , ndot , + bstar , rcse , inclo , nodeo , ecco , argpo , mo , + no; +} elsetrec; + +// --------------------------- function declarations ---------------------------- +int sgp4init + ( + gravconsttype whichconst, const int satn, const double epoch, + const double xbstar, const double xecco, const double xargpo, + const double xinclo, const double xmo, const double xno, + const double xnodeo, + elsetrec& satrec + ); + +int sgp4 + ( + gravconsttype whichconst, + elsetrec& satrec, double tsince, + double r[], double v[] + ); + +double gstime + ( + double + ); + +void getgravconst + ( + gravconsttype, + double&, + double&, + double&, + double&, + double&, + double&, + double&, + double& + ); + +#endif + diff --git a/fsfw/controller/ControllerBase.cpp b/fsfw/controller/ControllerBase.cpp new file mode 100644 index 0000000..6955173 --- /dev/null +++ b/fsfw/controller/ControllerBase.cpp @@ -0,0 +1,137 @@ +#include "../subsystem/SubsystemBase.h" +#include "ControllerBase.h" +#include "../subsystem/SubsystemBase.h" +#include "../ipc/QueueFactory.h" +#include "../action/HasActionsIF.h" + +ControllerBase::ControllerBase(uint32_t setObjectId, uint32_t parentId, + size_t commandQueueDepth) : + SystemObject(setObjectId), parentId(parentId), mode(MODE_OFF), submode( + SUBMODE_NONE), commandQueue(NULL), modeHelper( + this), healthHelper(this, setObjectId),hkSwitcher(this),executingTask(NULL) { + commandQueue = QueueFactory::instance()->createMessageQueue(commandQueueDepth); + +} + +ControllerBase::~ControllerBase() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); +} + +ReturnValue_t ControllerBase::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != RETURN_OK) { + return result; + } + + MessageQueueId_t parentQueue = 0; + if (parentId != 0) { + SubsystemBase *parent = objectManager->get(parentId); + if (parent == NULL) { + return RETURN_FAILED; + } + parentQueue = parent->getCommandQueue(); + + parent->registerChild(getObjectId()); + } + + result = healthHelper.initialize(parentQueue); + if (result != RETURN_OK) { + return result; + } + + result = modeHelper.initialize(parentQueue); + if (result != RETURN_OK) { + return result; + } + + result = hkSwitcher.initialize(); + if (result != RETURN_OK) { + return result; + } + return RETURN_OK; +} + +MessageQueueId_t ControllerBase::getCommandQueue() const { + return commandQueue->getId(); +} + +void ControllerBase::handleQueue() { + CommandMessage message; + ReturnValue_t result; + for (result = commandQueue->receiveMessage(&message); result == RETURN_OK; + result = commandQueue->receiveMessage(&message)) { + + result = modeHelper.handleModeCommand(&message); + if (result == RETURN_OK) { + continue; + } + + result = healthHelper.handleHealthCommand(&message); + if (result == RETURN_OK) { + continue; + } + result = handleCommandMessage(&message); + if (result == RETURN_OK) { + continue; + } + message.setToUnknownCommand(); + commandQueue->reply(&message); + } + +} + +void ControllerBase::startTransition(Mode_t mode, Submode_t submode) { + changeHK(this->mode, this->submode, false); + triggerEvent(CHANGING_MODE, mode, submode); + this->mode = mode; + this->submode = submode; + modeHelper.modeChanged(mode, submode); + modeChanged(mode, submode); + announceMode(false); + changeHK(this->mode, this->submode, true); +} + +void ControllerBase::getMode(Mode_t* mode, Submode_t* submode) { + *mode = this->mode; + *submode = this->submode; +} + +void ControllerBase::setToExternalControl() { + healthHelper.setHealth(EXTERNAL_CONTROL); +} + +void ControllerBase::announceMode(bool recursive) { + triggerEvent(MODE_INFO, mode, submode); +} + +ReturnValue_t ControllerBase::performOperation(uint8_t opCode) { + handleQueue(); + hkSwitcher.performOperation(); + performControlOperation(); + return RETURN_OK; +} + +void ControllerBase::modeChanged(Mode_t mode, Submode_t submode) { + return; +} + +ReturnValue_t ControllerBase::setHealth(HealthState health) { + switch (health) { + case HEALTHY: + case EXTERNAL_CONTROL: + healthHelper.setHealth(health); + return RETURN_OK; + default: + return INVALID_HEALTH_STATE; + } +} + +HasHealthIF::HealthState ControllerBase::getHealth() { + return healthHelper.getHealth(); +} +void ControllerBase::setTaskIF(PeriodicTaskIF* task_){ + executingTask = task_; +} + +void ControllerBase::changeHK(Mode_t mode, Submode_t submode, bool enable) { +} diff --git a/fsfw/controller/ControllerBase.h b/fsfw/controller/ControllerBase.h new file mode 100644 index 0000000..f5182ce --- /dev/null +++ b/fsfw/controller/ControllerBase.h @@ -0,0 +1,79 @@ +#ifndef CONTROLLERBASE_H_ +#define CONTROLLERBASE_H_ + +#include "../health/HasHealthIF.h" +#include "../health/HealthHelper.h" +#include "../modes/HasModesIF.h" +#include "../modes/ModeHelper.h" +#include "../objectmanager/SystemObject.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../datapool/HkSwitchHelper.h" + + +class ControllerBase: public HasModesIF, + public HasHealthIF, + public ExecutableObjectIF, + public SystemObject, + public HasReturnvaluesIF { +public: + + static const Mode_t MODE_NORMAL = 2; + + ControllerBase(uint32_t setObjectId, uint32_t parentId, + size_t commandQueueDepth = 3); + virtual ~ControllerBase(); + + ReturnValue_t initialize(); + + virtual MessageQueueId_t getCommandQueue() const; + + virtual ReturnValue_t performOperation(uint8_t opCode); + + virtual ReturnValue_t setHealth(HealthState health); + + virtual HasHealthIF::HealthState getHealth(); + + /** + * Implementation of ExecutableObjectIF function + * + * Used to setup the reference of the task, that executes this component + * @param task_ Pointer to the taskIF of this task + */ + virtual void setTaskIF(PeriodicTaskIF* task_); + + +protected: + const uint32_t parentId; + + Mode_t mode; + + Submode_t submode; + + MessageQueueIF* commandQueue; + + ModeHelper modeHelper; + + HealthHelper healthHelper; + + HkSwitchHelper hkSwitcher; + + /** + * Pointer to the task which executes this component, is invalid before setTaskIF was called. + */ + PeriodicTaskIF* executingTask; + + void handleQueue(); + + virtual ReturnValue_t handleCommandMessage(CommandMessage *message) = 0; + virtual void performControlOperation() = 0; + virtual ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, + uint32_t *msToReachTheMode) = 0; + virtual void modeChanged(Mode_t mode, Submode_t submode); + virtual void startTransition(Mode_t mode, Submode_t submode); + virtual void getMode(Mode_t *mode, Submode_t *submode); + virtual void setToExternalControl(); + virtual void announceMode(bool recursive); + virtual void changeHK(Mode_t mode, Submode_t submode, bool enable); +}; + +#endif /* CONTROLLERBASE_H_ */ diff --git a/fsfw/coordinates/CoordinateTransformations.cpp b/fsfw/coordinates/CoordinateTransformations.cpp new file mode 100644 index 0000000..4e2debb --- /dev/null +++ b/fsfw/coordinates/CoordinateTransformations.cpp @@ -0,0 +1,227 @@ +#include "CoordinateTransformations.h" +#include "../globalfunctions/constants.h" +#include "../globalfunctions/math/MatrixOperations.h" +#include "../globalfunctions/math/VectorOperations.h" +#include +#include + + +void CoordinateTransformations::positionEcfToEci(const double* ecfPosition, + double* eciPosition, timeval *timeUTC) { + ecfToEci(ecfPosition, eciPosition, NULL, timeUTC); + +} + +void CoordinateTransformations::velocityEcfToEci(const double* ecfVelocity, + const double* ecfPosition, double* eciVelocity, timeval *timeUTC) { + ecfToEci(ecfVelocity, eciVelocity, ecfPosition, timeUTC); +} + +void CoordinateTransformations::positionEciToEcf(const double* eciCoordinates, double* ecfCoordinates,timeval *timeUTC){ + eciToEcf(eciCoordinates,ecfCoordinates,NULL,timeUTC); +}; + +void CoordinateTransformations::velocityEciToEcf(const double* eciVelocity,const double* eciPosition, double* ecfVelocity,timeval* timeUTC){ + eciToEcf(eciVelocity,ecfVelocity,eciPosition,timeUTC); +} + +double CoordinateTransformations::getEarthRotationAngle(timeval timeUTC) { + + double jD2000UTC; + Clock::convertTimevalToJD2000(timeUTC, &jD2000UTC); + + double TTt2000 = getJuleanCenturiesTT(timeUTC); + + double theta = 2 * Math::PI + * (0.779057273264 + 1.00273781191135448 * jD2000UTC); + + //Correct theta according to IAU 2000 precession-nutation model + theta = theta + 7.03270725817493E-008 + 0.0223603701 * TTt2000 + + 6.77128219501896E-006 * TTt2000 * TTt2000 + + 4.5300990362875E-010 * TTt2000 * TTt2000 * TTt2000 + + 9.12419347848147E-011 * TTt2000 * TTt2000 * TTt2000 * TTt2000; + return theta; +} + +void CoordinateTransformations::getEarthRotationMatrix(timeval timeUTC, + double matrix[][3]) { + double theta = getEarthRotationAngle(timeUTC); + + matrix[0][0] = cos(theta); + matrix[0][1] = sin(theta); + matrix[0][2] = 0; + matrix[1][0] = -sin(theta); + matrix[1][1] = cos(theta); + matrix[1][2] = 0; + matrix[2][0] = 0; + matrix[2][1] = 0; + matrix[2][2] = 1; +} + +void CoordinateTransformations::ecfToEci(const double* ecfCoordinates, + double* eciCoordinates, + const double* ecfPositionIfCoordinatesAreVelocity, timeval *timeUTCin) { + + timeval timeUTC; + if (timeUTCin != NULL) { + timeUTC = *timeUTCin; + } else { + Clock::getClock_timeval(&timeUTC); + } + + double Tfi[3][3]; + double Tif[3][3]; + getTransMatrixECITOECF(timeUTC,Tfi); + + MatrixOperations::transpose(Tfi[0], Tif[0], 3); + + MatrixOperations::multiply(Tif[0], ecfCoordinates, eciCoordinates, + 3, 3, 1); + + + + + if (ecfPositionIfCoordinatesAreVelocity != NULL) { + + double Tdotfi[3][3]; + double Tdotif[3][3]; + double Trot[3][3] = { { 0, Earth::OMEGA, 0 }, + { 0 - Earth::OMEGA, 0, 0 }, { 0, 0, 0 } }; + + MatrixOperations::multiply(Trot[0], Tfi[0], Tdotfi[0], 3, 3, + 3); + + MatrixOperations::transpose(Tdotfi[0], Tdotif[0], 3); + + double velocityCorrection[3]; + + MatrixOperations::multiply(Tdotif[0], + ecfPositionIfCoordinatesAreVelocity, velocityCorrection, 3, 3, + 1); + + VectorOperations::add(velocityCorrection, eciCoordinates, + eciCoordinates, 3); + } +} + +double CoordinateTransformations::getJuleanCenturiesTT(timeval timeUTC) { + timeval timeTT; + Clock::convertUTCToTT(timeUTC, &timeTT); + double jD2000TT; + Clock::convertTimevalToJD2000(timeTT, &jD2000TT); + + return jD2000TT / 36525.; +} + +void CoordinateTransformations::eciToEcf(const double* eciCoordinates, + double* ecfCoordinates, + const double* eciPositionIfCoordinatesAreVelocity,timeval *timeUTCin){ + timeval timeUTC; + if (timeUTCin != NULL) { + timeUTC = *timeUTCin; + }else{ + Clock::getClock_timeval(&timeUTC); + } + + double Tfi[3][3]; + + getTransMatrixECITOECF(timeUTC,Tfi); + + MatrixOperations::multiply(Tfi[0],eciCoordinates,ecfCoordinates,3,3,1); + + if (eciPositionIfCoordinatesAreVelocity != NULL) { + + double Tdotfi[3][3]; + double Trot[3][3] = { { 0, Earth::OMEGA, 0 }, + { 0 - Earth::OMEGA, 0, 0 }, { 0, 0, 0 } }; + + MatrixOperations::multiply(Trot[0], Tfi[0], Tdotfi[0], 3, 3, + 3); + + double velocityCorrection[3]; + + MatrixOperations::multiply(Tdotfi[0], + eciPositionIfCoordinatesAreVelocity, velocityCorrection, 3, 3, + 1); + + VectorOperations::add(ecfCoordinates, velocityCorrection, + ecfCoordinates, 3); + } +}; + +void CoordinateTransformations::getTransMatrixECITOECF(timeval timeUTC,double Tfi[3][3]){ + double TTt2000 = getJuleanCenturiesTT(timeUTC); + + ////////////////////////////////////////////////////////// + // Calculate Precession Matrix + + double zeta = 0.0111808609 * TTt2000 + + 1.46355554053347E-006 * TTt2000 * TTt2000 + + 8.72567663260943E-008 * TTt2000 * TTt2000 * TTt2000; + double theta_p = 0.0097171735 * TTt2000 + - 2.06845757045384E-006 * TTt2000 * TTt2000 + - 2.02812107218552E-007 * TTt2000 * TTt2000 * TTt2000; + double z = zeta + 3.8436028638364E-006 * TTt2000 * TTt2000 + + 0.000000001 * TTt2000 * TTt2000 * TTt2000; + + double mPrecession[3][3]; + + mPrecession[0][0] = -sin(z) * sin(zeta) + cos(z) * cos(theta_p) * cos(zeta); + mPrecession[1][0] = cos(z) * sin(zeta) + sin(z) * cos(theta_p) * cos(zeta); + mPrecession[2][0] = sin(theta_p) * cos(zeta); + + mPrecession[0][1] = -sin(z) * cos(zeta) - cos(z) * cos(theta_p) * sin(zeta); + mPrecession[1][1] = cos(z) * cos(zeta) - sin(z) * cos(theta_p) * sin(zeta); + mPrecession[2][1] = -sin(theta_p) * sin(zeta); + + mPrecession[0][2] = -cos(z) * sin(theta_p); + mPrecession[1][2] = -sin(z) * sin(theta_p); + mPrecession[2][2] = cos(theta_p); + + ////////////////////////////////////////////////////////// + // Calculate Nutation Matrix + + double omega_moon = 2.1824386244 - 33.7570459338 * TTt2000 + + 3.61428599267159E-005 * TTt2000 * TTt2000 + + 3.87850944887629E-008 * TTt2000 * TTt2000 * TTt2000; + + double deltaPsi = -0.000083388 * sin(omega_moon); + double deltaEpsilon = 4.46174030725106E-005 * cos(omega_moon); + + double epsilon = 0.4090928042 - 0.0002269655 * TTt2000 + - 2.86040071854626E-009 * TTt2000 * TTt2000 + + 8.78967203851589E-009 * TTt2000 * TTt2000 * TTt2000; + + + double mNutation[3][3]; + + mNutation[0][0] = cos(deltaPsi); + mNutation[1][0] = cos(epsilon + deltaEpsilon) * sin(deltaPsi); + mNutation[2][0] = sin(epsilon + deltaEpsilon) * sin(deltaPsi); + + mNutation[0][1] = -cos(epsilon) * sin(deltaPsi); + mNutation[1][1] = cos(epsilon) * cos(epsilon + deltaEpsilon) * cos(deltaPsi) + + sin(epsilon) * sin(epsilon + deltaEpsilon); + mNutation[2][1] = cos(epsilon) * sin(epsilon + deltaEpsilon) * cos(deltaPsi) + - sin(epsilon) * cos(epsilon + deltaEpsilon); + + mNutation[0][2] = -sin(epsilon) * sin(deltaPsi); + mNutation[1][2] = sin(epsilon) * cos(epsilon + deltaEpsilon) * cos(deltaPsi) + - cos(epsilon) * sin(epsilon + deltaEpsilon); + mNutation[2][2] = sin(epsilon) * sin(epsilon + deltaEpsilon) * cos(deltaPsi) + + cos(epsilon) * cos(epsilon + deltaEpsilon); + + ////////////////////////////////////////////////////////// + // Calculate Earth rotation matrix + //calculate theta + + double mTheta[3][3]; + double Ttemp[3][3]; + getEarthRotationMatrix(timeUTC, mTheta); + + //polar motion is neglected + MatrixOperations::multiply(mNutation[0], mPrecession[0], Ttemp[0], + 3, 3, 3); + + MatrixOperations::multiply(mTheta[0], Ttemp[0], Tfi[0], 3, 3, 3); +}; diff --git a/fsfw/coordinates/CoordinateTransformations.h b/fsfw/coordinates/CoordinateTransformations.h new file mode 100644 index 0000000..09ea2c4 --- /dev/null +++ b/fsfw/coordinates/CoordinateTransformations.h @@ -0,0 +1,34 @@ +#ifndef COORDINATETRANSFORMATIONS_H_ +#define COORDINATETRANSFORMATIONS_H_ + +#include "../timemanager/Clock.h" +#include + +class CoordinateTransformations { +public: + static void positionEcfToEci(const double* ecfCoordinates, double* eciCoordinates, timeval *timeUTC = NULL); + + static void velocityEcfToEci(const double* ecfVelocity, + const double* ecfPosition, + double* eciVelocity, timeval *timeUTC = NULL); + + static void positionEciToEcf(const double* eciCoordinates, double* ecfCoordinates,timeval *timeUTC = NULL); + static void velocityEciToEcf(const double* eciVelocity,const double* eciPosition, double* ecfVelocity,timeval* timeUTC = NULL); + + static double getEarthRotationAngle(timeval timeUTC); + + static void getEarthRotationMatrix(timeval timeUTC, double matrix[][3]); +private: + CoordinateTransformations(); + static void ecfToEci(const double* ecfCoordinates, double* eciCoordinates, + const double* ecfPositionIfCoordinatesAreVelocity, timeval *timeUTCin); + static void eciToEcf(const double* eciCoordinates, + double* ecfCoordinates, + const double* eciPositionIfCoordinatesAreVelocity,timeval *timeUTCin); + + static double getJuleanCenturiesTT(timeval timeUTC); + static void getTransMatrixECITOECF(timeval time,double Tfi[3][3]); + +}; + +#endif /* COORDINATETRANSFORMATIONS_H_ */ diff --git a/fsfw/coordinates/Jgm3Model.h b/fsfw/coordinates/Jgm3Model.h new file mode 100644 index 0000000..884ed14 --- /dev/null +++ b/fsfw/coordinates/Jgm3Model.h @@ -0,0 +1,180 @@ +#ifndef FRAMEWORK_COORDINATES_JGM3MODEL_H_ +#define FRAMEWORK_COORDINATES_JGM3MODEL_H_ + +#include +#include "CoordinateTransformations.h" +#include "../globalfunctions/math/VectorOperations.h" +#include "../globalfunctions/timevalOperations.h" +#include "../globalfunctions/constants.h" +#include + + +template +class Jgm3Model { +public: + static const uint32_t factorialLookupTable[DEGREE+3]; //This table is used instead of factorial calculation, must be increased if order or degree is higher + + Jgm3Model() { + y0[0] = 0; + y0[1] = 0; + y0[2] = 0; + y0[3] = 0; + y0[4] = 0; + y0[5] = 0; + + lastExecutionTime.tv_sec = 0; + lastExecutionTime.tv_usec = 0; + } + virtual ~Jgm3Model(){}; + + //double acsNavOrbit(double posECF[3],double velECF[3],timeval gpsTime); + + double y0[6]; //position and velocity at beginning of RK step in EC + timeval lastExecutionTime; //Time of last execution + + + void accelDegOrd(const double pos[3],const double S[ORDER+1][DEGREE+1],const double C[ORDER+1][DEGREE+1],double* accel){ + //Get radius of this position + double r = VectorOperations::norm(pos,3); + + + + //Initialize the V and W matrix + double V[DEGREE+2][ORDER+2] = {{0}}; + double W[DEGREE+2][ORDER+2] = {{0}}; + + for(uint8_t m=0;m<(ORDER+2);m++){ + for(uint8_t n=m;n<(DEGREE+2);n++){ + if((n==0) && (m==0)){ + //Montenbruck "Satellite Orbits Eq.3.31" + V[0][0] = Earth::MEAN_RADIUS / r; + W[0][0] = 0; + }else{ + if(n==m){ + //Montenbruck "Satellite Orbits Eq.3.29" + V[m][m] = (2*m-1)* (pos[0]*Earth::MEAN_RADIUS/pow(r,2)*V[m-1][m-1] - pos[1]*Earth::MEAN_RADIUS/pow(r,2)*W[m-1][m-1]); + W[m][m] = (2*m-1)* (pos[0]*Earth::MEAN_RADIUS/pow(r,2)*W[m-1][m-1] + pos[1]*Earth::MEAN_RADIUS/pow(r,2)*V[m-1][m-1]); + }else{ + //Montenbruck "Satellite Orbits Eq.3.30" + V[n][m] = ((2*n-1)/(double)(n-m))*pos[2]*Earth::MEAN_RADIUS / pow(r,2)*V[n-1][m]; + W[n][m] = ((2*n-1)/(double)(n-m))*pos[2]*Earth::MEAN_RADIUS / pow(r,2)*W[n-1][m]; + if(n!=(m+1)){ + V[n][m] = V[n][m] - (((n+m-1)/(double)(n-m)) * (pow(Earth::MEAN_RADIUS,2) / pow(r,2)) * V[n-2][m]); + W[n][m] = W[n][m] - (((n+m-1)/(double)(n-m)) * (pow(Earth::MEAN_RADIUS,2) / pow(r,2)) * W[n-2][m]); + }//End of if(n!=(m+1)) + }//End of if(n==m){ + }//End of if(n==0 and m==0) + }//End of for(uint8_t n=0;n<(DEGREE+1);n++) + }//End of for(uint8_t m=0;m<(ORDER+1);m++) + + + //overwrite accel if not properly initialized + accel[0] = 0; + accel[1] = 0; + accel[2] = 0; + + for(uint8_t m=0;m<(ORDER+1);m++){ + for(uint8_t n=m;n<(DEGREE+1);n++){ + //Use table lookup to get factorial + double partAccel[3] = {0}; + double factor = Earth::STANDARD_GRAVITATIONAL_PARAMETER/pow(Earth::MEAN_RADIUS,2); + if(m==0){ + //Montenbruck "Satellite Orbits Eq.3.33" + partAccel[0] = factor * (-C[n][0]*V[n+1][1]); + partAccel[1] = factor * (-C[n][0]*W[n+1][1]); + }else{ + double factMN = static_cast(factorialLookupTable[n-m+2]) / static_cast(factorialLookupTable[n-m]); + partAccel[0] = factor * 0.5 * ((-C[n][m]*V[n+1][m+1]-S[n][m]*W[n+1][m+1])+factMN*(C[n][m]*V[n+1][m-1]+S[n][m]*W[n+1][m-1])); + partAccel[1] = factor * 0.5 * ((-C[n][m]*W[n+1][m+1]+S[n][m]*V[n+1][m+1])+factMN*(-C[n][m]*W[n+1][m-1]+S[n][m]*V[n+1][m-1])); + } + + partAccel[2] = factor * ((n-m+1)*(-C[n][m]*V[n+1][m]-S[n][m]*W[n+1][m])); + + + accel[0] += partAccel[0]; + accel[1] += partAccel[1]; + accel[2] += partAccel[2]; + }//End of for(uint8_t n=0;n::mulScalar(y0dot,deltaT/2,yA,6); + VectorOperations::add(y0,yA,yA,6); + rungeKuttaStep(yA,yAdot,lastExecutionTime,S,C); + + //Step Three + VectorOperations::mulScalar(yAdot,deltaT/2,yB,6); + VectorOperations::add(y0,yB,yB,6); + rungeKuttaStep(yB,yBdot,lastExecutionTime,S,C); + + //Step Four + VectorOperations::mulScalar(yBdot,deltaT,yC,6); + VectorOperations::add(y0,yC,yC,6); + rungeKuttaStep(yC,yCdot,lastExecutionTime,S,C); + + //Calc new State + VectorOperations::mulScalar(yAdot,2,yAdot,6); + VectorOperations::mulScalar(yBdot,2,yBdot,6); + VectorOperations::add(y0dot,yAdot,y0dot,6); + VectorOperations::add(y0dot,yBdot,y0dot,6); + VectorOperations::add(y0dot,yCdot,y0dot,6); + VectorOperations::mulScalar(y0dot,1./6.*deltaT,y0dot,6); + VectorOperations::add(y0,y0dot,y0,6); + + CoordinateTransformations::positionEciToEcf(&y0[0],outputPos,&timeUTC); + CoordinateTransformations::velocityEciToEcf(&y0[3],&y0[0],outputVel,&timeUTC); + + lastExecutionTime = timeUTC; + + + } + + + void rungeKuttaStep(const double* yIn,double* yOut,timeval time, const double S[ORDER+1][DEGREE+1],const double C[ORDER+1][DEGREE+1]){ + double rECF[3] = {0,0,0}; + double rDotECF[3] = {0,0,0}; + double accelECF[3] = {0,0,0}; + double accelECI[3] = {0,0,0}; + + + CoordinateTransformations::positionEciToEcf(&yIn[0],rECF,&time); + CoordinateTransformations::velocityEciToEcf(&yIn[3],&yIn[0],rDotECF,&time); + accelDegOrd(rECF,S,C,accelECF); + //This is not correct, as the acceleration would have derived terms but we don't know the velocity and position at that time + //Tests showed that a wrong velocity does make the equation worse than neglecting it + CoordinateTransformations::positionEcfToEci(accelECF,accelECI,&time); + memcpy(&yOut[0],&yIn[3],sizeof(yOut[0])*3); + memcpy(&yOut[3],accelECI,sizeof(yOut[0])*3); + } +}; + + + + + + +#endif /* FRAMEWORK_COORDINATES_JGM3MODEL_H_ */ diff --git a/fsfw/coordinates/Sgp4Propagator.cpp b/fsfw/coordinates/Sgp4Propagator.cpp new file mode 100644 index 0000000..5cb1497 --- /dev/null +++ b/fsfw/coordinates/Sgp4Propagator.cpp @@ -0,0 +1,228 @@ +#include "CoordinateTransformations.h" +#include "Sgp4Propagator.h" +#include "../globalfunctions/constants.h" +#include "../globalfunctions/math/MatrixOperations.h" +#include "../globalfunctions/math/VectorOperations.h" +#include "../globalfunctions/timevalOperations.h" +#include +Sgp4Propagator::Sgp4Propagator() : + initialized(false), epoch({0, 0}), whichconst(wgs84) { + +} + +Sgp4Propagator::~Sgp4Propagator() { + +} + +void jday(int year, int mon, int day, int hr, int minute, double sec, + double& jd) { + jd = 367.0 * year - floor((7 * (year + floor((mon + 9) / 12.0))) * 0.25) + + floor(275 * mon / 9.0) + day + 1721013.5 + + ((sec / 60.0 + minute) / 60.0 + hr) / 24.0; // ut in days + // - 0.5*sgn(100.0*year + mon - 190002.5) + 0.5; +} + +void days2mdhms(int year, double days, int& mon, int& day, int& hr, int& minute, + double& sec) { + int i, inttemp, dayofyr; + double temp; + int lmonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + dayofyr = (int) floor(days); + /* ----------------- find month and day of month ---------------- */ + if ((year % 4) == 0) + lmonth[1] = 29; + + i = 1; + inttemp = 0; + while ((dayofyr > inttemp + lmonth[i - 1]) && (i < 12)) { + inttemp = inttemp + lmonth[i - 1]; + i++; + } + mon = i; + day = dayofyr - inttemp; + + /* ----------------- find hours minutes and seconds ------------- */ + temp = (days - dayofyr) * 24.0; + hr = (int) floor(temp); + temp = (temp - hr) * 60.0; + minute = (int) floor(temp); + sec = (temp - minute) * 60.0; +} + +ReturnValue_t Sgp4Propagator::initialize(const uint8_t* line1, + const uint8_t* line2) { + + char longstr1[130]; + char longstr2[130]; + + //need some space for decimal points + memcpy(longstr1, line1, 69); + memcpy(longstr2, line2, 69); + + const double deg2rad = Math::PI / 180.0; // 0.0174532925199433 + const double xpdotp = 1440.0 / (2.0 * Math::PI); // 229.1831180523293 + + double sec, mu, radiusearthkm, tumin, xke, j2, j3, j4, j3oj2; + int cardnumb, numb, j; + long revnum = 0, elnum = 0; + char classification, intldesg[11]; + int year = 0; + int mon, day, hr, minute, nexp, ibexp; + + getgravconst(whichconst, tumin, mu, radiusearthkm, xke, j2, j3, j4, j3oj2); + + satrec.error = 0; + + // set the implied decimal points since doing a formated read + // fixes for bad input data values (missing, ...) + for (j = 10; j <= 15; j++) + if (longstr1[j] == ' ') + longstr1[j] = '_'; + + if (longstr1[44] != ' ') + longstr1[43] = longstr1[44]; + longstr1[44] = '.'; + if (longstr1[7] == ' ') + longstr1[7] = 'U'; + if (longstr1[9] == ' ') + longstr1[9] = '.'; + for (j = 45; j <= 49; j++) + if (longstr1[j] == ' ') + longstr1[j] = '0'; + if (longstr1[51] == ' ') + longstr1[51] = '0'; + if (longstr1[53] != ' ') + longstr1[52] = longstr1[53]; + longstr1[53] = '.'; + longstr2[25] = '.'; + for (j = 26; j <= 32; j++) + if (longstr2[j] == ' ') + longstr2[j] = '0'; + if (longstr1[62] == ' ') + longstr1[62] = '0'; + if (longstr1[68] == ' ') + longstr1[68] = '0'; + + sscanf(longstr1, + "%2d %5ld %1c %10s %2d %12lf %11lf %7lf %2d %7lf %2d %2d %6ld ", + &cardnumb, &satrec.satnum, &classification, intldesg, + &satrec.epochyr, &satrec.epochdays, &satrec.ndot, &satrec.nddot, + &nexp, &satrec.bstar, &ibexp, &numb, &elnum); + + if (longstr2[52] == ' ') { + sscanf(longstr2, "%2d %5ld %9lf %9lf %8lf %9lf %9lf %10lf %6ld \n", + &cardnumb, &satrec.satnum, &satrec.inclo, &satrec.nodeo, + &satrec.ecco, &satrec.argpo, &satrec.mo, &satrec.no, &revnum); + } else { + sscanf(longstr2, "%2d %5ld %9lf %9lf %8lf %9lf %9lf %11lf %6ld \n", + &cardnumb, &satrec.satnum, &satrec.inclo, &satrec.nodeo, + &satrec.ecco, &satrec.argpo, &satrec.mo, &satrec.no, &revnum); + } + + // ---- find no, ndot, nddot ---- + satrec.no = satrec.no / xpdotp; //* rad/min + satrec.nddot = satrec.nddot * pow(10.0, nexp); + satrec.bstar = satrec.bstar * pow(10.0, ibexp); + + // ---- convert to sgp4 units ---- + satrec.a = pow(satrec.no * tumin, (-2.0 / 3.0)); + satrec.ndot = satrec.ndot / (xpdotp * 1440.0); //* ? * minperday + satrec.nddot = satrec.nddot / (xpdotp * 1440.0 * 1440); + + // ---- find standard orbital elements ---- + satrec.inclo = satrec.inclo * deg2rad; + satrec.nodeo = satrec.nodeo * deg2rad; + satrec.argpo = satrec.argpo * deg2rad; + satrec.mo = satrec.mo * deg2rad; + + satrec.alta = satrec.a * (1.0 + satrec.ecco) - 1.0; + satrec.altp = satrec.a * (1.0 - satrec.ecco) - 1.0; + + // ---------------------------------------------------------------- + // find sgp4epoch time of element set + // remember that sgp4 uses units of days from 0 jan 1950 (sgp4epoch) + // and minutes from the epoch (time) + // ---------------------------------------------------------------- + + // ---------------- temp fix for years from 1957-2056 ------------------- + // --------- correct fix will occur when year is 4-digit in tle --------- + if (satrec.epochyr < 57) { + year = satrec.epochyr + 2000; + } else { + year = satrec.epochyr + 1900; + } + + days2mdhms(year, satrec.epochdays, mon, day, hr, minute, sec); + jday(year, mon, day, hr, minute, sec, satrec.jdsatepoch); + + double unixSeconds = (satrec.jdsatepoch - 2451544.5) * 24 * 3600 + + 946684800; + + epoch.tv_sec = unixSeconds; + double subseconds = unixSeconds - epoch.tv_sec; + epoch.tv_usec = subseconds * 1000000; + + // ---------------- initialize the orbit at sgp4epoch ------------------- + uint8_t result = sgp4init(whichconst, satrec.satnum, + satrec.jdsatepoch - 2433281.5, satrec.bstar, satrec.ecco, + satrec.argpo, satrec.inclo, satrec.mo, satrec.no, satrec.nodeo, + satrec); + + if (result != 00) { + return MAKE_RETURN_CODE(result); + } else { + initialized = true; + return HasReturnvaluesIF::RETURN_OK; + } + +} + +ReturnValue_t Sgp4Propagator::propagate(double* position, double* velocity, + timeval time, uint8_t gpsUtcOffset) { + + if (!initialized) { + return TLE_NOT_INITIALIZED; + } + + //Time since epoch in minutes + timeval timeSinceEpoch = time - epoch; + double minutesSinceEpoch = timeSinceEpoch.tv_sec / 60. + + timeSinceEpoch.tv_usec / 60000000.; + + double yearsSinceEpoch = minutesSinceEpoch / 60 / 24 / 365; + + if ((yearsSinceEpoch > 1) || (yearsSinceEpoch < -1)) { + return TLE_TOO_OLD; + } + + double positionTEME[3]; + double velocityTEME[3]; + + uint8_t result = sgp4(whichconst, satrec, minutesSinceEpoch, positionTEME, + velocityTEME); + + VectorOperations::mulScalar(positionTEME, 1000, positionTEME, 3); + VectorOperations::mulScalar(velocityTEME, 1000, velocityTEME, 3); + + //Transform to ECF + double earthRotationMatrix[3][3]; + CoordinateTransformations::getEarthRotationMatrix(time, + earthRotationMatrix); + + MatrixOperations::multiply(earthRotationMatrix[0], positionTEME, + position, 3, 3, 1); + MatrixOperations::multiply(earthRotationMatrix[0], velocityTEME, + velocity, 3, 3, 1); + + double omegaEarth[3] = { 0, 0, Earth::OMEGA }; + double velocityCorrection[3]; + VectorOperations::cross(omegaEarth, position, velocityCorrection); + VectorOperations::subtract(velocity, velocityCorrection, velocity); + + if (result != 0) { + return MAKE_RETURN_CODE(result || 0xB0); + } else { + return HasReturnvaluesIF::RETURN_OK; + } +} diff --git a/fsfw/coordinates/Sgp4Propagator.h b/fsfw/coordinates/Sgp4Propagator.h new file mode 100644 index 0000000..3949547 --- /dev/null +++ b/fsfw/coordinates/Sgp4Propagator.h @@ -0,0 +1,44 @@ +#ifndef SGP4PROPAGATOR_H_ +#define SGP4PROPAGATOR_H_ + +#include +#include "../contrib/sgp4/sgp4unit.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +class Sgp4Propagator { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::SGP4PROPAGATOR_CLASS; + static const ReturnValue_t INVALID_ECCENTRICITY = MAKE_RETURN_CODE(0xA1); + static const ReturnValue_t INVALID_MEAN_MOTION = MAKE_RETURN_CODE(0xA2); + static const ReturnValue_t INVALID_PERTURBATION_ELEMENTS = MAKE_RETURN_CODE(0xA3); + static const ReturnValue_t INVALID_SEMI_LATUS_RECTUM = MAKE_RETURN_CODE(0xA4); + static const ReturnValue_t INVALID_EPOCH_ELEMENTS = MAKE_RETURN_CODE(0xA5); + static const ReturnValue_t SATELLITE_HAS_DECAYED = MAKE_RETURN_CODE(0xA6); + static const ReturnValue_t TLE_TOO_OLD = MAKE_RETURN_CODE(0xB1); + static const ReturnValue_t TLE_NOT_INITIALIZED = MAKE_RETURN_CODE(0xB2); + + + + Sgp4Propagator(); + virtual ~Sgp4Propagator(); + + ReturnValue_t initialize(const uint8_t *line1, const uint8_t *line2); + + /** + * + * @param[out] position in ECF + * @param[out] velocity in ECF + * @param time to which to propagate + * @return + */ + ReturnValue_t propagate(double *position, double *velocity, timeval time, uint8_t gpsUtcOffset); + +private: + bool initialized; + timeval epoch; + elsetrec satrec; + gravconsttype whichconst; + +}; + +#endif /* SGP4PROPAGATOR_H_ */ diff --git a/fsfw/datalinklayer/BCFrame.h b/fsfw/datalinklayer/BCFrame.h new file mode 100644 index 0000000..b779555 --- /dev/null +++ b/fsfw/datalinklayer/BCFrame.h @@ -0,0 +1,62 @@ +/** + * @file BCFrame.h + * @brief This file defines the BCFrame class. + * @date 24.04.2013 + * @author baetz + */ + +#ifndef BCFRAME_H_ +#define BCFRAME_H_ + +#include "CCSDSReturnValuesIF.h" + +/** + * Small helper class to identify a BcFrame. + * @ingroup ccsds_handling + */ +class BcFrame: public CCSDSReturnValuesIF { +private: + static const uint8_t UNLOCK_COMMAND = 0b00000000;//! Identifier for a certain BC Command. + static const uint8_t SET_V_R_1 = 0b10000010;//! Identifier for a certain BC Command. + static const uint8_t SET_V_R_2 = 0b00000000;//! Identifier for a certain BC Command. + +public: + uint8_t byte1; //!< First content byte + uint8_t byte2; //!< Second content byte + uint8_t vR; //!< vR byte + /** + * Simple default constructor. + */ + BcFrame() : + byte1(0), byte2(0), vR(0) { + } + /** + * Main and only useful method of the class. + * With the buffer and size information passed, the class passes the content + * and checks if it is one of the two valid BC Command Frames. + * @param inBuffer Content of the frame to check, + * @param inSize Size of the data to check. + * @return - #BC_ILLEGAL_COMMAND if it is no command. + * - #BC_IS_UNLOCK_COMMAND if it is an unlock command. + * - #BC_IS_SET_VR_COMMAND if it is such. + */ + ReturnValue_t initialize(const uint8_t* inBuffer, uint16_t inSize) { + ReturnValue_t returnValue = BC_ILLEGAL_COMMAND; + if (inSize == 1) { + byte1 = inBuffer[0]; + if (byte1 == UNLOCK_COMMAND) { + returnValue = BC_IS_UNLOCK_COMMAND; + } + } else if (inSize == 3) { + byte1 = inBuffer[0]; + byte2 = inBuffer[1]; + vR = inBuffer[2]; + if (byte1 == SET_V_R_1 && byte2 == SET_V_R_2) { + returnValue = BC_IS_SET_VR_COMMAND; + } + } + return returnValue; + } +}; + +#endif /* BCFRAME_H_ */ diff --git a/fsfw/datalinklayer/CCSDSReturnValuesIF.h b/fsfw/datalinklayer/CCSDSReturnValuesIF.h new file mode 100644 index 0000000..805b696 --- /dev/null +++ b/fsfw/datalinklayer/CCSDSReturnValuesIF.h @@ -0,0 +1,56 @@ +/** + * @file CCSDSReturnValuesIF.h + * @brief This file defines the CCSDSReturnValuesIF class. + * @date 24.04.2013 + * @author baetz + */ + +#ifndef CCSDSRETURNVALUESIF_H_ +#define CCSDSRETURNVALUESIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +/** + * This is a helper class to collect special return values that come up during CCSDS Handling. + * @ingroup ccsds_handling + */ +class CCSDSReturnValuesIF: public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::CCSDS_HANDLER_IF; //!< Basic ID of the interface. + + static const ReturnValue_t BC_IS_SET_VR_COMMAND = MAKE_RETURN_CODE( 0x01 ); //!< A value to describe a BC frame. + static const ReturnValue_t BC_IS_UNLOCK_COMMAND = MAKE_RETURN_CODE( 0x02 ); //!< A value to describe a BC frame. + static const ReturnValue_t BC_ILLEGAL_COMMAND = MAKE_RETURN_CODE( 0xB0 );//!< A value to describe an illegal BC frame. + static const ReturnValue_t BOARD_READING_NOT_FINISHED = MAKE_RETURN_CODE( 0xB1 ); //! The CCSDS Board is not yet finished reading, it requires another cycle. + + static const ReturnValue_t NS_POSITIVE_W = MAKE_RETURN_CODE( 0xF0 );//!< NS is in the positive window + static const ReturnValue_t NS_NEGATIVE_W = MAKE_RETURN_CODE( 0xF1 );//!< NS is in the negative window + static const ReturnValue_t NS_LOCKOUT = MAKE_RETURN_CODE( 0xF2 ); //!< NS is in lockout state + static const ReturnValue_t FARM_IN_LOCKOUT = MAKE_RETURN_CODE( 0xF3 );//!< FARM-1 is currently in lockout state + static const ReturnValue_t FARM_IN_WAIT = MAKE_RETURN_CODE( 0xF4 ); //!< FARM-1 is currently in wait state + + static const ReturnValue_t WRONG_SYMBOL = MAKE_RETURN_CODE( 0xE0 ); //!< An error code in the FrameFinder. + static const ReturnValue_t DOUBLE_START = MAKE_RETURN_CODE( 0xE1 ); //!< An error code in the FrameFinder. + static const ReturnValue_t START_SYMBOL_MISSED = MAKE_RETURN_CODE( 0xE2 );//!< An error code in the FrameFinder. + static const ReturnValue_t END_WITHOUT_START = MAKE_RETURN_CODE( 0xE3 );//!< An error code in the FrameFinder. + static const ReturnValue_t TOO_LARGE = MAKE_RETURN_CODE( 0xE4 );//!< An error code for a frame. + static const ReturnValue_t TOO_SHORT = MAKE_RETURN_CODE( 0xE5 );//!< An error code for a frame. + static const ReturnValue_t WRONG_TF_VERSION = MAKE_RETURN_CODE( 0xE6 ); //!< An error code for a frame. + static const ReturnValue_t WRONG_SPACECRAFT_ID = MAKE_RETURN_CODE( 0xE7 );//!< An error code for a frame. + static const ReturnValue_t NO_VALID_FRAME_TYPE = MAKE_RETURN_CODE( 0xE8 );//!< An error code for a frame. + static const ReturnValue_t CRC_FAILED = MAKE_RETURN_CODE( 0xE9 );//!< An error code for a frame. + static const ReturnValue_t VC_NOT_FOUND = MAKE_RETURN_CODE( 0xEA ); //!< An error code for a frame. + static const ReturnValue_t FORWARDING_FAILED = MAKE_RETURN_CODE( 0xEB );//!< An error code for a frame. + static const ReturnValue_t CONTENT_TOO_LARGE = MAKE_RETURN_CODE( 0xEC );//!< An error code for a frame. + static const ReturnValue_t RESIDUAL_DATA = MAKE_RETURN_CODE( 0xED );//!< An error code for a frame. + static const ReturnValue_t DATA_CORRUPTED = MAKE_RETURN_CODE( 0xEE );//!< An error code for a frame. + static const ReturnValue_t ILLEGAL_SEGMENTATION_FLAG = MAKE_RETURN_CODE( 0xEF );//!< An error code for a frame. + static const ReturnValue_t ILLEGAL_FLAG_COMBINATION = MAKE_RETURN_CODE( 0xD0 ); //!< An error code for a frame. + static const ReturnValue_t SHORTER_THAN_HEADER = MAKE_RETURN_CODE( 0xD1 ); //!< An error code for a frame. + static const ReturnValue_t TOO_SHORT_BLOCKED_PACKET = MAKE_RETURN_CODE( 0xD2 ); //!< An error code for a frame. + static const ReturnValue_t TOO_SHORT_MAP_EXTRACTION = MAKE_RETURN_CODE( 0xD3 ); //!< An error code for a frame. + + virtual ~CCSDSReturnValuesIF() { + } //!< Empty virtual destructor +}; + +#endif /* CCSDSRETURNVALUESIF_H_ */ diff --git a/fsfw/datalinklayer/Clcw.cpp b/fsfw/datalinklayer/Clcw.cpp new file mode 100644 index 0000000..dc435a2 --- /dev/null +++ b/fsfw/datalinklayer/Clcw.cpp @@ -0,0 +1,63 @@ +/** + * @file Clcw.cpp + * @brief This file defines the Clcw class. + * @date 17.04.2013 + * @author baetz + */ + + + +#include "Clcw.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +Clcw::Clcw() { + content.raw = 0; + content.status = STATUS_FIELD_DEFAULT; +} + +Clcw::~Clcw() { +} + +void Clcw::setVirtualChannel(uint8_t setChannel) { + content.virtualChannelIdSpare = ((setChannel & 0x3F) << 2); +} + +void Clcw::setLockoutFlag(bool lockout) { + content.flags = (content.flags & LOCKOUT_FLAG_MASK) | (lockout << LOCKOUT_FLAG_POSITION); +} + +void Clcw::setWaitFlag(bool waitFlag) { + content.flags = (content.flags & WAIT_FLAG_MASK) | (waitFlag << WAIT_FLAG_POSITION); +} + +void Clcw::setRetransmitFlag(bool retransmitFlag) { + content.flags = (content.flags & RETRANSMIT_FLAG_MASK) | (retransmitFlag << RETRANSMIT_FLAG_POSITION); +} + +void Clcw::setFarmBCount(uint8_t count) { + content.flags = (content.flags & FARM_B_COUNT_MASK) | ((count & 0x03) << 1); +} + +void Clcw::setReceiverFrameSequenceNumber(uint8_t vR) { + content.vRValue = vR; +} + +uint32_t Clcw::getAsWhole() { + return content.raw; +} + +void Clcw::setRFAvailable(bool rfAvailable) { + content.flags = (content.flags & NO_RF_AVIALABLE_MASK) | (!rfAvailable << NO_RF_AVIALABLE_POSITION); +} + +void Clcw::setBitLock(bool bitLock) { + content.flags = (content.flags & NO_BIT_LOCK_MASK) | (!bitLock << NO_BIT_LOCK_POSITION); +} + +void Clcw::print() { + sif::debug << "Clcw::print: Clcw is: " << std::hex << getAsWhole() << std::dec << std::endl; +} + +void Clcw::setWhole(uint32_t rawClcw) { + content.raw = rawClcw; +} diff --git a/fsfw/datalinklayer/Clcw.h b/fsfw/datalinklayer/Clcw.h new file mode 100644 index 0000000..8116d63 --- /dev/null +++ b/fsfw/datalinklayer/Clcw.h @@ -0,0 +1,66 @@ +/** + * @file Clcw.h + * @brief This file defines the Clcw class. + * @date 17.04.2013 + * @author baetz + */ + +#ifndef CLCW_H_ +#define CLCW_H_ + +#include "ClcwIF.h" +/** + * Small helper method to handle the Clcw values. + * It has a content struct that manages the register and can be set externally. + * @ingroup ccsds_handling + */ +class Clcw : public ClcwIF { +private: + static const uint8_t STATUS_FIELD_DEFAULT = 0b00000001; //!< Default for the status field. + static const uint8_t NO_RF_AVIALABLE_POSITION = 7; //!< Position of a flag in the register (starting with 0). + static const uint8_t NO_BIT_LOCK_POSITION = 6; //!< Position of a flag in the register (starting with 0). + static const uint8_t LOCKOUT_FLAG_POSITION = 5; //!< Position of a flag in the register (starting with 0). + static const uint8_t WAIT_FLAG_POSITION = 4; //!< Position of a flag in the register (starting with 0). + static const uint8_t RETRANSMIT_FLAG_POSITION = 3; //!< Position of a flag in the register (starting with 0). + static const uint8_t NO_RF_AVIALABLE_MASK = 0xFF xor (1 << NO_RF_AVIALABLE_POSITION); //!< Mask for a flag in the register. + static const uint8_t NO_BIT_LOCK_MASK = 0xFF xor (1 << NO_BIT_LOCK_POSITION); //!< Mask for a flag in the register. + static const uint8_t LOCKOUT_FLAG_MASK = 0xFF xor (1 << LOCKOUT_FLAG_POSITION); //!< Mask for a flag in the register. + static const uint8_t WAIT_FLAG_MASK = 0xFF xor (1 << WAIT_FLAG_POSITION); //!< Mask for a flag in the register. + static const uint8_t RETRANSMIT_FLAG_MASK = 0xFF xor (1 << RETRANSMIT_FLAG_POSITION); //!< Mask for a flag in the register. + static const uint8_t FARM_B_COUNT_MASK = 0b11111001; //!< Mask for a counter in the register. + /** + * This is the data structure of the CLCW register. + */ + union clcwContent { + uint32_t raw; + struct { + uint8_t status; + uint8_t virtualChannelIdSpare; + uint8_t flags; + uint8_t vRValue; + }; + }; + clcwContent content; //!< Represents the content of the register. +public: + /** + * The constructor sets everything to default values. + */ + Clcw(); + /** + * Nothing happens in the destructor. + */ + ~Clcw(); + void setVirtualChannel( uint8_t setChannel ); + void setLockoutFlag( bool lockout ); + void setWaitFlag( bool waitFlag ); + void setRetransmitFlag( bool retransmitFlag ); + void setFarmBCount( uint8_t count ); + void setReceiverFrameSequenceNumber( uint8_t vR ); + void setRFAvailable( bool rfAvailable ); + void setBitLock( bool bitLock ); + uint32_t getAsWhole(); + void setWhole( uint32_t rawClcw ); + void print(); +}; + +#endif /* CLCW_H_ */ diff --git a/fsfw/datalinklayer/ClcwIF.h b/fsfw/datalinklayer/ClcwIF.h new file mode 100644 index 0000000..fc51785 --- /dev/null +++ b/fsfw/datalinklayer/ClcwIF.h @@ -0,0 +1,70 @@ +/** + * @file ClcwIF.h + * @brief This file defines the ClcwIF class. + * @date 17.04.2013 + * @author baetz + */ + +#ifndef CLCWIF_H_ +#define CLCWIF_H_ + +#include + +/** + * Interface to manage a CLCW register. + * @ingroup ccsds_handling + */ +class ClcwIF { +public: + /** + * Empty virtual destructor. + */ + virtual ~ClcwIF() { } + /** + * Simple setter. + * @param setChannel The virtual channel id to set. + */ + virtual void setVirtualChannel( uint8_t setChannel ) = 0; + /** + * Simple setter. + * @param lockout status of the flag. + */ + virtual void setLockoutFlag( bool lockout ) = 0; + /** + * Simple setter. + * @param waitFlag status of the flag. + */ + virtual void setWaitFlag( bool waitFlag ) = 0; + /** + * Simple setter. + * @param retransmitFlag status of the flag. + */ + virtual void setRetransmitFlag( bool retransmitFlag ) = 0; + /** + * Sets the farm B count. + * @param count A full 8bit counter value can be passed. Only the last three bits are used. + */ + virtual void setFarmBCount( uint8_t count ) = 0; + /** + * Simple setter. + * @param vR Value of vR. + */ + virtual void setReceiverFrameSequenceNumber( uint8_t vR ) = 0; + /** + * Returns the register as a full 32bit value. + * @return The value. + */ + virtual uint32_t getAsWhole() = 0; + /** + * Sets the whole content to this value. + * @param rawClcw The value to set the content. + */ + virtual void setWhole( uint32_t rawClcw ) = 0; + /** + * Debug method to print the CLCW. + */ + virtual void print() = 0; +}; + + +#endif /* CLCWIF_H_ */ diff --git a/fsfw/datalinklayer/DataLinkLayer.cpp b/fsfw/datalinklayer/DataLinkLayer.cpp new file mode 100644 index 0000000..75f2edd --- /dev/null +++ b/fsfw/datalinklayer/DataLinkLayer.cpp @@ -0,0 +1,139 @@ +#include "DataLinkLayer.h" +#include "../globalfunctions/CRC.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +DataLinkLayer::DataLinkLayer(uint8_t* set_frame_buffer, ClcwIF* setClcw, + uint8_t set_start_sequence_length, uint16_t set_scid) : + spacecraftId(set_scid), frameBuffer(set_frame_buffer), clcw(setClcw), receivedDataLength(0), currentFrame( + NULL), startSequenceLength(set_start_sequence_length) { + //Nothing to do except from setting the values above. +} + +DataLinkLayer::~DataLinkLayer() { + +} + +ReturnValue_t DataLinkLayer::frameDelimitingAndFillRemoval() { + if ((receivedDataLength - startSequenceLength) < FRAME_PRIMARY_HEADER_LENGTH) { + return SHORTER_THAN_HEADER; + } + //Removing start sequence. + //SHOULDDO: Not implemented here. + while ( *frameBuffer == START_SEQUENCE_PATTERN ) { + frameBuffer++; + } + TcTransferFrame frame_candidate(frameBuffer); + this->currentFrame = frame_candidate; //should work with shallow copy. + + return RETURN_OK; +} + +ReturnValue_t DataLinkLayer::frameValidationCheck() { + //Check TF_version number + if (this->currentFrame.getVersionNumber() != FRAME_VERSION_NUMBER_DEFAULT) { + return WRONG_TF_VERSION; + } + //Check SpaceCraft ID + if (this->currentFrame.getSpacecraftId() != this->spacecraftId) { + return WRONG_SPACECRAFT_ID; + } + //Check other header limitations: + if (!this->currentFrame.bypassFlagSet() && this->currentFrame.controlCommandFlagSet()) { + return NO_VALID_FRAME_TYPE; + } + //- Spares are zero + if (!this->currentFrame.spareIsZero()) { + return NO_VALID_FRAME_TYPE; + } + //Compare detected frame length with the one in the header + uint16_t length = currentFrame.getFullSize(); + if (length > receivedDataLength) { + //Frame is too long or just right +// error << "frameValidationCheck: Too short."; +// currentFrame.print(); + return TOO_SHORT; + } + if (USE_CRC) { + return this->frameCheckCRC(); + } + return RETURN_OK; +} + +ReturnValue_t DataLinkLayer::frameCheckCRC() { + uint16_t checkValue = CRC::crc16ccitt(this->currentFrame.getFullFrame(), + this->currentFrame.getFullSize()); + if (checkValue == 0) { + return RETURN_OK; + } else { + return CRC_FAILED; + } + +} + +ReturnValue_t DataLinkLayer::allFramesReception() { + ReturnValue_t status = this->frameDelimitingAndFillRemoval(); + if (status != RETURN_OK) { + return status; + } + return this->frameValidationCheck(); +} + +ReturnValue_t DataLinkLayer::masterChannelDemultiplexing() { + //Nothing to do at present. Ideally, there would be a map of MCID's identifying which MC to use. + return virtualChannelDemultiplexing(); +} + +ReturnValue_t DataLinkLayer::virtualChannelDemultiplexing() { + uint8_t vcId = currentFrame.getVirtualChannelId(); + virtualChannelIterator iter = virtualChannels.find(vcId); + if (iter == virtualChannels.end()) { + //Do not report because passive board will get this error all the time. + return RETURN_OK; + } else { + return (iter->second)->frameAcceptanceAndReportingMechanism(¤tFrame, clcw); + } +} + +ReturnValue_t DataLinkLayer::processFrame(uint16_t length) { + receivedDataLength = length; + ReturnValue_t status = allFramesReception(); + if (status != RETURN_OK) { + sif::error << "DataLinkLayer::processFrame: frame reception failed. " + "Error code: " << std::hex << status << std::dec << std::endl; +// currentFrame.print(); + return status; + } else { + return masterChannelDemultiplexing(); + } +} + +ReturnValue_t DataLinkLayer::addVirtualChannel(uint8_t virtualChannelId, + VirtualChannelReceptionIF* object) { + std::pair returnValue = virtualChannels.insert( + std::pair(virtualChannelId, object)); + if (returnValue.second == true) { + return RETURN_OK; + } else { + return RETURN_FAILED; + } +} + +ReturnValue_t DataLinkLayer::initialize() { + ReturnValue_t returnValue = RETURN_FAILED; + //Set Virtual Channel ID to first virtual channel instance in this DataLinkLayer instance to avoid faulty information (e.g. 0) in the VCID. + if ( virtualChannels.begin() != virtualChannels.end() ) { + clcw->setVirtualChannel( virtualChannels.begin()->second->getChannelId() ); + } else { + sif::error << "DataLinkLayer::initialize: No VC assigned to this DLL instance! " << std::endl; + return RETURN_FAILED; + } + + for (virtualChannelIterator iterator = virtualChannels.begin(); + iterator != virtualChannels.end(); iterator++) { + returnValue = iterator->second->initialize(); + if (returnValue != RETURN_OK) + break; + } + return returnValue; + +} diff --git a/fsfw/datalinklayer/DataLinkLayer.h b/fsfw/datalinklayer/DataLinkLayer.h new file mode 100644 index 0000000..5a90009 --- /dev/null +++ b/fsfw/datalinklayer/DataLinkLayer.h @@ -0,0 +1,112 @@ +#ifndef DATALINKLAYER_H_ +#define DATALINKLAYER_H_ + +#include "CCSDSReturnValuesIF.h" +#include "ClcwIF.h" +#include "TcTransferFrame.h" +#include "VirtualChannelReceptionIF.h" +#include "../events/Event.h" +#include + + +class VirtualChannelReception; +/** + * A complete representation of the CCSDS Data Link Layer. + * The operations of this layer are defined in the CCSDS TC Space Data Link Protocol + * document. It is configured to handle a VC Demultiplexing function. All reception + * steps are performed. + */ +class DataLinkLayer : public CCSDSReturnValuesIF { +public: + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::SYSTEM_1; + static const Event RF_AVAILABLE = MAKE_EVENT(0, SEVERITY::INFO); //!< A RF available signal was detected. P1: raw RFA state, P2: 0 + static const Event RF_LOST = MAKE_EVENT(1, SEVERITY::INFO); //!< A previously found RF available signal was lost. P1: raw RFA state, P2: 0 + static const Event BIT_LOCK = MAKE_EVENT(2, SEVERITY::INFO); //!< A Bit Lock signal. Was detected. P1: raw BLO state, P2: 0 + static const Event BIT_LOCK_LOST = MAKE_EVENT(3, SEVERITY::INFO); //!< A previously found Bit Lock signal was lost. P1: raw BLO state, P2: 0 +// static const Event RF_CHAIN_LOST = MAKE_EVENT(4, SEVERITY::INFO); //!< The CCSDS Board detected that either bit lock or RF available or both are lost. No parameters. + static const Event FRAME_PROCESSING_FAILED = MAKE_EVENT(5, SEVERITY::LOW); //!< The CCSDS Board could not interpret a TC + /** + * The Constructor sets the passed parameters and nothing else. + * @param set_frame_buffer The buffer in which incoming frame candidates are stored. + * @param setClcw The CLCW class to work on when returning CLCW information. + * @param set_start_sequence_length Length of the Start sequence in front of every TC Transfer Frame. + * @param set_scid The SCID to operate on. + */ + DataLinkLayer( uint8_t* set_frame_buffer, ClcwIF* setClcw, uint8_t set_start_sequence_length, uint16_t set_scid ); + /** + * Empty virtual destructor. + */ + ~DataLinkLayer(); + /** + * This method tries to process a frame that is placed in #frameBuffer. + * The procedures described in the Standard are performed. + * @param length Length of the incoming frame candidate. + * @return @c RETURN_OK on successful handling, otherwise the return codes of the higher methods. + */ + ReturnValue_t processFrame( uint16_t length ); + /** + * Configuration method to add a new TC Virtual Channel. + * Shall only be called during initialization. As soon as the method was called, the layer can + * handle Frames directed to this VC. + * @param virtualChannelId Id of the VC. Shall be smaller than 64. + * @param object Reference to the object that handles the Frame. + * @return @c RETURN_OK on success, @c RETURN_FAILED otherwise. + */ + ReturnValue_t addVirtualChannel( uint8_t virtualChannelId, VirtualChannelReceptionIF* object ); + /** + * The initialization method calls the @c initialize routine of all virtual channels. + * @return The return code of the first failed VC initialization or @c RETURN_OK. + */ + ReturnValue_t initialize(); +private: + typedef std::map::iterator virtualChannelIterator; //!< Typedef to simplify handling the #virtualChannels map. + static const uint8_t FRAME_VERSION_NUMBER_DEFAULT = 0x00; //!< Constant for the default value of Frame Version Numbers. + static const uint8_t FRAME_PRIMARY_HEADER_LENGTH = 5; //!< Length of the frame's primary header. + static const uint8_t START_SEQUENCE_PATTERN = 0x00; //!< The start sequence pattern which might be with the frame. + static const bool USE_CRC = true; //!< A global, so called "Managed Parameter" that identifies if incoming frames have CRC's or not. + uint16_t spacecraftId; //!< The Space Craft Identifier (SCID) configured. + uint8_t* frameBuffer; //!< A pointer to point to the current incoming frame. + ClcwIF* clcw; //!< Pointer to store the CLCW to work on. + uint16_t receivedDataLength; //!< Stores the length of the currently processed frame. + TcTransferFrame currentFrame; //!< Stores a more convenient access to the current frame. + uint8_t startSequenceLength; //!< Configured length of the start sequence. Maybe this must be done more variable. + std::map virtualChannels; //!< Map of all virtual channels assigned. + /** + * Method that performs all possible frame validity checks (as specified). + * @return Various error codes or @c RETURN_OK on success. + */ + ReturnValue_t frameValidationCheck(); + /** + * First method to call. + * Removes start sequence bytes and checks if the complete frame was received. + * SHOULDDO: Maybe handling the start sequence must be done more variable. + * @return @c RETURN_OK or @c TOO_SHORT. + */ + ReturnValue_t frameDelimitingAndFillRemoval(); + /** + * Small helper method to check the CRC of the Frame. + * @return @c RETURN_OK or @c CRC_FAILED. + */ + ReturnValue_t frameCheckCRC(); + /** + * Method that groups the reception process of all Frames. + * Calls #frameDelimitingAndFillRemoval and #frameValidationCheck. + * @return The return codes of the sub calls. + */ + ReturnValue_t allFramesReception(); + /** + * Dummy method for master channel demultiplexing. + * As there's only one Master Channel here, the method calls #virtualChannelDemultiplexing. + * @return The return codes of #virtualChannelDemultiplexing. + */ + ReturnValue_t masterChannelDemultiplexing(); + /** + * Method to demultiplex the Frames to Virtual Channels (VC's). + * Looks up the requested VC in #virtualChannels and forwards the Frame to its + * #frameAcceptanceAndReportingMechanism method, if found. + * @return The higher method codes or @c VC_NOT_FOUND. + */ + ReturnValue_t virtualChannelDemultiplexing(); +}; + +#endif /* DATALINKLAYER_H_ */ diff --git a/fsfw/datalinklayer/Farm1StateIF.h b/fsfw/datalinklayer/Farm1StateIF.h new file mode 100644 index 0000000..71794d7 --- /dev/null +++ b/fsfw/datalinklayer/Farm1StateIF.h @@ -0,0 +1,54 @@ +/** + * @file Farm1StateIF.h + * @brief This file defines the Farm1StateIF class. + * @date 24.04.2013 + * @author baetz + */ + +#ifndef FARM1STATEIF_H_ +#define FARM1STATEIF_H_ + +#include "CCSDSReturnValuesIF.h" +class VirtualChannelReception; +class TcTransferFrame; +class ClcwIF; + +/** + * This is the interface for states of the FARM-1 state machine. + * Classes implementing this interface can be used as FARM-1 states. This is a simple implementation + * of the state pattern. + */ +class Farm1StateIF : public CCSDSReturnValuesIF { +public: + /** + * A method that shall handle an incoming frame as AD Frame. + * @param frame The frame to handle. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @return If forwarding to a MAP Channel is required, the return value shall be #FRAME_OK. + * Otherwise, an appropriate return value or error code shall be generated. + */ + virtual ReturnValue_t handleADFrame( TcTransferFrame* frame, ClcwIF* clcw ) = 0; + /** + * This method shall handle frames that have been successfully identified as BC Unlock frames. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @return If forwarding to a MAP Channel is required, the return value shall be #FRAME_OK. + * Otherwise, an appropriate return value or error code shall be generated. + */ + virtual ReturnValue_t handleBCUnlockCommand( ClcwIF* clcw ) = 0; + /** + * This method shall handle frames that have been successfully identified as BC Set VR frames. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @param vr The V(r) value found in the frame. + * @return If forwarding to a MAP Channel is required, the return value shall be #FRAME_OK. + * Otherwise, an appropriate return value or error code shall be generated. + */ + virtual ReturnValue_t handleBCSetVrCommand( ClcwIF* clcw, uint8_t vr ) = 0; + /** + * Empty virtual destructor. + */ + virtual ~Farm1StateIF() { + + } +}; + +#endif /* FARM1STATEIF_H_ */ diff --git a/fsfw/datalinklayer/Farm1StateLockout.cpp b/fsfw/datalinklayer/Farm1StateLockout.cpp new file mode 100644 index 0000000..1b339a8 --- /dev/null +++ b/fsfw/datalinklayer/Farm1StateLockout.cpp @@ -0,0 +1,35 @@ +/** + * @file Farm1StateLockout.cpp + * @brief This file defines the Farm1StateLockout class. + * @date 24.04.2013 + * @author baetz + */ + + + +#include "ClcwIF.h" +#include "Farm1StateLockout.h" +#include "TcTransferFrame.h" +#include "VirtualChannelReception.h" +Farm1StateLockout::Farm1StateLockout(VirtualChannelReception* setMyVC) : myVC(setMyVC) { +} + +ReturnValue_t Farm1StateLockout::handleADFrame(TcTransferFrame* frame, + ClcwIF* clcw) { + return FARM_IN_LOCKOUT; +} + +ReturnValue_t Farm1StateLockout::handleBCUnlockCommand(ClcwIF* clcw) { + myVC->farmBCounter++; + clcw->setRetransmitFlag(false); + clcw->setLockoutFlag( false ); + clcw->setWaitFlag( false ); + myVC->currentState = &(myVC->openState); + return BC_IS_UNLOCK_COMMAND; +} + +ReturnValue_t Farm1StateLockout::handleBCSetVrCommand(ClcwIF* clcw, + uint8_t vr) { + myVC->farmBCounter++; + return BC_IS_SET_VR_COMMAND; +} diff --git a/fsfw/datalinklayer/Farm1StateLockout.h b/fsfw/datalinklayer/Farm1StateLockout.h new file mode 100644 index 0000000..63cdc4d --- /dev/null +++ b/fsfw/datalinklayer/Farm1StateLockout.h @@ -0,0 +1,59 @@ +/** + * @file Farm1StateLockout.h + * @brief This file defines the Farm1StateLockout class. + * @date 24.04.2013 + * @author baetz + */ + +#ifndef FARM1STATELOCKOUT_H_ +#define FARM1STATELOCKOUT_H_ + +#include "Farm1StateIF.h" + +/** + * This class represents the FARM-1 "Lockout" State. + * The Lockout state is reached if the received Transfer Frame Sequence Number is completely wrong + * (i.e. within the Lockout Window). No AD Frames are forwarded. To leave the State, a BC Unlock + * command is required. + */ +class Farm1StateLockout : public Farm1StateIF { +private: + /** + * This is a reference to the "owner" class the State works on. + */ + VirtualChannelReception* myVC; +public: + /** + * The default constructor if the State. + * Sets the "owner" of the State. + * @param setMyVC The "owner" class. + */ + Farm1StateLockout( VirtualChannelReception* setMyVC ); + /** + * All AD Frames are rejected with FARM_IN_LOCKOUT + * @param frame The frame to handle. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @return FARM_IN_LOCKOUT + */ + ReturnValue_t handleADFrame( TcTransferFrame* frame, ClcwIF* clcw ); + /** + * These commands are handled as specified. + * State changes to Farm1StateOpen. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @return As the frame needs no forwarding to a MAP Channel, #BC_IS_UNLOCK_COMMAND + * is returned. + */ + ReturnValue_t handleBCUnlockCommand( ClcwIF* clcw ); + /** + * These commands are handled as specified. + * The V(r) value is not set in Lockout State, even though the Command itself is accepted. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @param vr The V(r) value found in the frame. + * @return As the frame needs no forwarding to a MAP Channel, #BC_IS_SET_VR_COMMAND + * is returned. + */ + ReturnValue_t handleBCSetVrCommand( ClcwIF* clcw, uint8_t vr ); +}; + + +#endif /* FARM1STATELOCKOUT_H_ */ diff --git a/fsfw/datalinklayer/Farm1StateOpen.cpp b/fsfw/datalinklayer/Farm1StateOpen.cpp new file mode 100644 index 0000000..61c0997 --- /dev/null +++ b/fsfw/datalinklayer/Farm1StateOpen.cpp @@ -0,0 +1,49 @@ +/** + * @file Farm1StateOpen.cpp + * @brief This file defines the Farm1StateOpen class. + * @date 24.04.2013 + * @author baetz + */ + + + + +#include "ClcwIF.h" +#include "Farm1StateOpen.h" +#include "TcTransferFrame.h" +#include "VirtualChannelReception.h" + +Farm1StateOpen::Farm1StateOpen(VirtualChannelReception* setMyVC) : myVC(setMyVC) { +} + +ReturnValue_t Farm1StateOpen::handleADFrame(TcTransferFrame* frame, + ClcwIF* clcw) { + int8_t diff = frame->getSequenceNumber() - myVC->vR; + if (diff == 0 ) { + myVC->vR++; + clcw->setRetransmitFlag(false); + return RETURN_OK; + } else if (diff < myVC->positiveWindow && diff > 0 ) { + clcw->setRetransmitFlag(true); + return NS_POSITIVE_W; + } else if (diff < 0 && diff >= -myVC->negativeWindow) { + return NS_NEGATIVE_W; + } else { + clcw->setLockoutFlag(true); + myVC->currentState = &(myVC->lockoutState); + return NS_LOCKOUT; + } +} + +ReturnValue_t Farm1StateOpen::handleBCUnlockCommand( ClcwIF* clcw ) { + myVC->farmBCounter++; + clcw->setRetransmitFlag(false); + return BC_IS_UNLOCK_COMMAND; +} + +ReturnValue_t Farm1StateOpen::handleBCSetVrCommand( ClcwIF* clcw, uint8_t vr ) { + myVC->farmBCounter++; + clcw->setRetransmitFlag(false); + myVC->vR = vr; + return BC_IS_SET_VR_COMMAND; +} diff --git a/fsfw/datalinklayer/Farm1StateOpen.h b/fsfw/datalinklayer/Farm1StateOpen.h new file mode 100644 index 0000000..3b3a260 --- /dev/null +++ b/fsfw/datalinklayer/Farm1StateOpen.h @@ -0,0 +1,62 @@ +/** + * @file Farm1StateOpen.h + * @brief This file defines the Farm1StateOpen class. + * @date 24.04.2013 + * @author baetz + */ + +#ifndef FARM1STATEOPEN_H_ +#define FARM1STATEOPEN_H_ + +#include "Farm1StateIF.h" + +/** + * This class represents the FARM-1 "Open" State. + * The Open state is the state of normal operation. It handles all types of frames, + * including AD Frames. If a wrong Frame Sequence Number is detected in an AD Frame, the + * State reacts as specified. + */ +class Farm1StateOpen : public Farm1StateIF { +private: + /** + * This is a reference to the "owner" class the State works on. + */ + VirtualChannelReception* myVC; +public: + /** + * The default constructor if the State. + * Sets the "owner" of the State. + * @param setMyVC The "owner" class. + */ + Farm1StateOpen( VirtualChannelReception* setMyVC ); + /** + * Method to check the validity of AD Frames. + * It checks the Frame Sequence Number and reacts as specified in the standard. The state may + * change to Farm1StateLockout. + * @param frame The frame to handle. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @return If the Sequence Number is ok, it returns #RETURN_OK. Otherwise either #NS_POSITIVE_W, + * #NS_NEGATIVE_W or NS_LOCKOUT is returned. + */ + ReturnValue_t handleADFrame( TcTransferFrame* frame, ClcwIF* clcw ); + /** + * These commands are handled as specified. + * State does not change. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @return As the frame needs no forwarding to a MAP Channel, #BC_IS_UNLOCK_COMMAND + * is returned. + */ + ReturnValue_t handleBCUnlockCommand( ClcwIF* clcw ); + /** + * These commands are handled as specified. + * State does not change. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @param vr The V(r) value found in the frame. + * @return As the frame needs no forwarding to a MAP Channel, #BC_IS_SET_VR_COMMAND + * is returned. + */ + ReturnValue_t handleBCSetVrCommand( ClcwIF* clcw, uint8_t vr ); +}; + + +#endif /* FARM1STATEOPEN_H_ */ diff --git a/fsfw/datalinklayer/Farm1StateWait.cpp b/fsfw/datalinklayer/Farm1StateWait.cpp new file mode 100644 index 0000000..9001e1f --- /dev/null +++ b/fsfw/datalinklayer/Farm1StateWait.cpp @@ -0,0 +1,43 @@ +/** + * @file Farm1StateWait.cpp + * @brief This file defines the Farm1StateWait class. + * @date 24.04.2013 + * @author baetz + */ + + +#include "ClcwIF.h" +#include "Farm1StateWait.h" +#include "TcTransferFrame.h" +#include "VirtualChannelReception.h" + +Farm1StateWait::Farm1StateWait(VirtualChannelReception* setMyVC) : myVC(setMyVC) { +} + +ReturnValue_t Farm1StateWait::handleADFrame(TcTransferFrame* frame, + ClcwIF* clcw) { + + int8_t diff = frame->getSequenceNumber() - myVC->vR; + if ( diff < -myVC->negativeWindow || diff >= myVC->positiveWindow ) { + clcw->setLockoutFlag(true); + myVC->currentState = &(myVC->lockoutState); + } + return FARM_IN_WAIT; +} + +ReturnValue_t Farm1StateWait::handleBCUnlockCommand(ClcwIF* clcw) { + myVC->farmBCounter++; + clcw->setRetransmitFlag(false); + clcw->setWaitFlag( false ); + myVC->currentState = &(myVC->openState); + return BC_IS_UNLOCK_COMMAND; +} + +ReturnValue_t Farm1StateWait::handleBCSetVrCommand(ClcwIF* clcw, uint8_t vr) { + myVC->farmBCounter++; + clcw->setWaitFlag( false ); + clcw->setRetransmitFlag(false); + myVC->vR = vr; + myVC->currentState = &(myVC->openState); + return BC_IS_SET_VR_COMMAND; +} diff --git a/fsfw/datalinklayer/Farm1StateWait.h b/fsfw/datalinklayer/Farm1StateWait.h new file mode 100644 index 0000000..877c36c --- /dev/null +++ b/fsfw/datalinklayer/Farm1StateWait.h @@ -0,0 +1,58 @@ +/** + * @file Farm1StateWait.h + * @brief This file defines the Farm1StateWait class. + * @date 24.04.2013 + * @author baetz + */ + +#ifndef FARM1STATEWAIT_H_ +#define FARM1STATEWAIT_H_ + +#include "Farm1StateIF.h" + +/** + * This class represents the FARM-1 "Wait" State. + * The Wait state is reached if higher level procedures inform the FARM-1 Machine to wait + * for a certain period. Currently, it is not in use. + */ +class Farm1StateWait : public Farm1StateIF { +private: + /** + * This is a reference to the "owner" class the State works on. + */ + VirtualChannelReception* myVC; +public: + /** + * The default constructor if the State. + * Sets the "owner" of the State. + * @param setMyVC The "owner" class. + */ + Farm1StateWait( VirtualChannelReception* setMyVC ); + /** + * AD Frames are always discarded. + * If the frame number is in the lockout window, the state changes to Farm1StateLockout. + * @param frame The frame to handle. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @return Always returns FARM_IN_WAIT. + */ + ReturnValue_t handleADFrame( TcTransferFrame* frame, ClcwIF* clcw ); + /** + * These commands are handled as specified. + * State changes to Farm1StateOpen. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @return As the frame needs no forwarding to a MAP Channel, #BC_IS_UNLOCK_COMMAND + * is returned. + */ + ReturnValue_t handleBCUnlockCommand( ClcwIF* clcw ); + /** + * These commands are handled as specified. + * @param clcw Any changes to the CLCW shall be done with the help of this interface. + * @param vr The V(r) value found in the frame. + * @return As the frame needs no forwarding to a MAP Channel, #BC_IS_SET_VR_COMMAND + * is returned. + */ + ReturnValue_t handleBCSetVrCommand( ClcwIF* clcw, uint8_t vr ); +}; + + +#endif /* FARM1STATEWAIT_H_ */ diff --git a/fsfw/datalinklayer/MapPacketExtraction.cpp b/fsfw/datalinklayer/MapPacketExtraction.cpp new file mode 100644 index 0000000..d64348e --- /dev/null +++ b/fsfw/datalinklayer/MapPacketExtraction.cpp @@ -0,0 +1,154 @@ +/** + * @file MapPacketExtraction.cpp + * @brief This file defines the MapPacketExtraction class. + * @date 26.03.2013 + * @author baetz + */ + +#include "MapPacketExtraction.h" +#include "../ipc/QueueFactory.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tmtcpacket/SpacePacketBase.h" +#include "../tmtcservices/AcceptsTelecommandsIF.h" +#include "../tmtcservices/TmTcMessage.h" +#include + +MapPacketExtraction::MapPacketExtraction(uint8_t setMapId, + object_id_t setPacketDestination) : + lastSegmentationFlag(NO_SEGMENTATION), mapId(setMapId), packetLength(0), bufferPosition( + packetBuffer), packetDestination(setPacketDestination), packetStore( + NULL), tcQueueId(MessageQueueIF::NO_QUEUE) { + memset(packetBuffer, 0, sizeof(packetBuffer)); +} + +ReturnValue_t MapPacketExtraction::extractPackets(TcTransferFrame* frame) { + uint8_t segmentationFlag = frame->getSequenceFlags(); + ReturnValue_t status = TOO_SHORT_MAP_EXTRACTION; + switch (segmentationFlag) { + case NO_SEGMENTATION: + status = unpackBlockingPackets(frame); + break; + case FIRST_PORTION: + packetLength = frame->getDataLength(); + if (packetLength <= MAX_PACKET_SIZE) { + memcpy(packetBuffer, frame->getDataField(), packetLength); + bufferPosition = &packetBuffer[packetLength]; + status = RETURN_OK; + } else { + sif::error + << "MapPacketExtraction::extractPackets. Packet too large! Size: " + << packetLength << std::endl; + clearBuffers(); + status = CONTENT_TOO_LARGE; + } + break; + case CONTINUING_PORTION: + case LAST_PORTION: + if (lastSegmentationFlag == FIRST_PORTION + || lastSegmentationFlag == CONTINUING_PORTION) { + packetLength += frame->getDataLength(); + if (packetLength <= MAX_PACKET_SIZE) { + memcpy(bufferPosition, frame->getDataField(), + frame->getDataLength()); + bufferPosition = &packetBuffer[packetLength]; + if (segmentationFlag == LAST_PORTION) { + status = sendCompletePacket(packetBuffer, packetLength); + clearBuffers(); + } + status = RETURN_OK; + } else { + sif::error + << "MapPacketExtraction::extractPackets. Packet too large! Size: " + << packetLength << std::endl; + clearBuffers(); + status = CONTENT_TOO_LARGE; + } + } else { + sif::error + << "MapPacketExtraction::extractPackets. Illegal segment! Last flag: " + << (int) lastSegmentationFlag << std::endl; + clearBuffers(); + status = ILLEGAL_SEGMENTATION_FLAG; + } + break; + default: + sif::error + << "MapPacketExtraction::extractPackets. Illegal segmentationFlag: " + << (int) segmentationFlag << std::endl; + clearBuffers(); + status = DATA_CORRUPTED; + break; + } + lastSegmentationFlag = segmentationFlag; + return status; +} + +ReturnValue_t MapPacketExtraction::unpackBlockingPackets( + TcTransferFrame* frame) { + ReturnValue_t status = TOO_SHORT_BLOCKED_PACKET; + uint32_t totalLength = frame->getDataLength(); + if (totalLength > MAX_PACKET_SIZE) + return CONTENT_TOO_LARGE; + uint8_t* position = frame->getDataField(); + while ((totalLength > SpacePacketBase::MINIMUM_SIZE)) { + SpacePacketBase packet(position); + uint32_t packetSize = packet.getFullSize(); + if (packetSize <= totalLength) { + status = sendCompletePacket(packet.getWholeData(), + packet.getFullSize()); + totalLength -= packet.getFullSize(); + position += packet.getFullSize(); + status = RETURN_OK; + } else { + status = DATA_CORRUPTED; + totalLength = 0; + } + } + if (totalLength > 0) { + status = RESIDUAL_DATA; + } + return status; +} + +ReturnValue_t MapPacketExtraction::sendCompletePacket(uint8_t* data, + uint32_t size) { + store_address_t store_id; + ReturnValue_t status = this->packetStore->addData(&store_id, data, size); + if (status == RETURN_OK) { + TmTcMessage message(store_id); + status = MessageQueueSenderIF::sendMessage(tcQueueId,&message); + } + return status; +} + +void MapPacketExtraction::clearBuffers() { + memset(packetBuffer, 0, sizeof(packetBuffer)); + bufferPosition = packetBuffer; + packetLength = 0; + lastSegmentationFlag = NO_SEGMENTATION; +} + +ReturnValue_t MapPacketExtraction::initialize() { + packetStore = objectManager->get(objects::TC_STORE); + AcceptsTelecommandsIF* distributor = objectManager->get< + AcceptsTelecommandsIF>(packetDestination); + if ((packetStore != NULL) && (distributor != NULL)) { + tcQueueId = distributor->getRequestQueue(); + return RETURN_OK; + } else { + return RETURN_FAILED; + } +} + +void MapPacketExtraction::printPacketBuffer(void) { + sif::debug << "DLL: packet_buffer contains: " << std::endl; + for (uint32_t i = 0; i < this->packetLength; ++i) { + sif::debug << "packet_buffer[" << std::dec << i << "]: 0x" << std::hex + << (uint16_t) this->packetBuffer[i] << std::endl; + } +} + +uint8_t MapPacketExtraction::getMapId() const { + return mapId; +} diff --git a/fsfw/datalinklayer/MapPacketExtraction.h b/fsfw/datalinklayer/MapPacketExtraction.h new file mode 100644 index 0000000..507f13d --- /dev/null +++ b/fsfw/datalinklayer/MapPacketExtraction.h @@ -0,0 +1,78 @@ +/** + * @file MapPacketExtraction.h + * @brief This file defines the MapPacketExtraction class. + * @date 26.03.2013 + * @author baetz + */ + +#ifndef MAPPACKETEXTRACTION_H_ +#define MAPPACKETEXTRACTION_H_ + +#include "MapPacketExtractionIF.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +class StorageManagerIF; + +/** + * Implementation of a MAP Packet Extraction class. + * The class implements the full MAP Packet Extraction functionality as described in the CCSDS + * TC Space Data Link Protocol. It internally stores incomplete segmented packets until they are + * fully received. All found packets are forwarded to a single distribution entity. + */ +class MapPacketExtraction: public MapPacketExtractionIF { +private: + static const uint32_t MAX_PACKET_SIZE = 4096; + uint8_t lastSegmentationFlag; //!< The segmentation flag of the last received frame. + uint8_t mapId; //!< MAP ID of this MAP Channel. + uint32_t packetLength; //!< Complete length of the current Space Packet. + uint8_t* bufferPosition; //!< Position to write to in the internal Packet buffer. + uint8_t packetBuffer[MAX_PACKET_SIZE]; //!< The internal Space Packet Buffer. + object_id_t packetDestination; + StorageManagerIF* packetStore; //!< Pointer to the store where full TC packets are stored. + MessageQueueId_t tcQueueId; //!< QueueId to send found packets to the distributor. + /** + * Debug method to print the packet Buffer's content. + */ + void printPacketBuffer(); + /** + * Method that is called if the segmentation flag is @c NO_SEGMENTATION. + * The method extracts one or more packets within the frame and forwards them to the OBSW. + * @param frame The TC Transfer Frame to work on. + * @return @c RETURN_OK if all Packets were extracted. If something is entirely wrong, + * @c DATA_CORRUPTED is returned, if some bytes are left over @c RESIDUAL_DATA. + */ + ReturnValue_t unpackBlockingPackets(TcTransferFrame* frame); + /** + * Helper method to forward a complete packet to the OBSW. + * @param data Pointer to the data, either directly from the frame or from the packetBuffer. + * @param size Complete total size of the packet. + * @return Return Code of the Packet Store or the Message Queue. + */ + ReturnValue_t sendCompletePacket( uint8_t* data, uint32_t size ); + /** + * Helper method to reset the internal buffer. + */ + void clearBuffers(); +public: + /** + * Default constructor. + * Members are set to default values. + * @param setMapId The MAP ID of the instance. + */ + MapPacketExtraction( uint8_t setMapId, object_id_t setPacketDestination ); + ReturnValue_t extractPackets(TcTransferFrame* frame); + /** + * The #packetStore and the default destination of #tcQueue are initialized here. + * @return @c RETURN_OK on success, @c RETURN_FAILED otherwise. + */ + ReturnValue_t initialize(); + /** + * Getter. + * @return The MAP ID of this instance. + */ + uint8_t getMapId() const; +}; + +#endif /* MAPPACKETEXTRACTION_H_ */ diff --git a/fsfw/datalinklayer/MapPacketExtractionIF.h b/fsfw/datalinklayer/MapPacketExtractionIF.h new file mode 100644 index 0000000..e29ac66 --- /dev/null +++ b/fsfw/datalinklayer/MapPacketExtractionIF.h @@ -0,0 +1,47 @@ +/** + * @file MapPacketExtractionIF.h + * @brief This file defines the MapPacketExtractionIF class. + * @date 25.03.2013 + * @author baetz + */ + +#ifndef MAPPACKETEXTRACTIONIF_H_ +#define MAPPACKETEXTRACTIONIF_H_ + +#include "CCSDSReturnValuesIF.h" +#include "TcTransferFrame.h" + +/** + * This is the interface for MAP Packet Extraction classes. + * All classes implementing this interface shall be able to extract blocked or segmented Space + * Packets on a certain MAP channel. This is done in accordance with the CCSDS TC Space Data Link + * Protocol. + */ +class MapPacketExtractionIF : public CCSDSReturnValuesIF { +protected: + static const uint8_t FIRST_PORTION = 0b01; //!< Identification of the first part of a segmented Packet. + static const uint8_t CONTINUING_PORTION = 0b00; //!< Identification of a continuing part of segmented Packets. + static const uint8_t LAST_PORTION = 0b10; //!< The last portion of a segmented Packet. + static const uint8_t NO_SEGMENTATION = 0b11; //!< A Frame without segmentation but maybe with blocking. +public: + /** + * Empty virtual destructor. + */ + virtual ~MapPacketExtractionIF() { + } + /** + * Method to call to handle a single Transfer Frame. + * The method tries to extract Packets from the frame as stated in the Standard. + * @param frame + * @return + */ + virtual ReturnValue_t extractPackets( TcTransferFrame* frame ) = 0; + /** + * Any post-instantiation initialization shall be done in this method. + * @return + */ + virtual ReturnValue_t initialize() = 0; +}; + + +#endif /* MAPPACKETEXTRACTIONIF_H_ */ diff --git a/fsfw/datalinklayer/TcTransferFrame.cpp b/fsfw/datalinklayer/TcTransferFrame.cpp new file mode 100644 index 0000000..f1a70bd --- /dev/null +++ b/fsfw/datalinklayer/TcTransferFrame.cpp @@ -0,0 +1,102 @@ +/** + * @file TcTransferFrame.cpp + * @brief This file defines the TcTransferFrame class. + * @date 27.04.2013 + * @author baetz + */ + + + +#include "TcTransferFrame.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +TcTransferFrame::TcTransferFrame() { + frame = NULL; +} + +TcTransferFrame::TcTransferFrame(uint8_t* setData) { + this->frame = (tc_transfer_frame*)setData; +} + +uint8_t TcTransferFrame::getVersionNumber() { + return (this->frame->header.flagsAndScid & 0b11000000) >> 6; +} + +bool TcTransferFrame::bypassFlagSet() { + return (this->frame->header.flagsAndScid & 0b00100000) != 0; +} + +bool TcTransferFrame::controlCommandFlagSet() { + return (this->frame->header.flagsAndScid & 0b00010000) != 0; +} + +bool TcTransferFrame::spareIsZero() { + return ( (this->frame->header.flagsAndScid & 0b00001100) == 0 ); +} + +uint16_t TcTransferFrame::getSpacecraftId() { + return ( (this->frame->header.flagsAndScid & 0b00000011) << 8 ) + this->frame->header.spacecraftId_l; +} + +uint8_t TcTransferFrame::getVirtualChannelId() { + return (this->frame->header.vcidAndLength_h & 0b11111100) >> 2; +} + +uint16_t TcTransferFrame::getFrameLength() { + return ( (this->frame->header.vcidAndLength_h & 0b00000011) << 8 ) + this->frame->header.length_l; +} + +uint16_t TcTransferFrame::getDataLength() { + return this->getFrameLength() - this->getHeaderSize() -1 - FRAME_CRC_SIZE + 1; // -1 for the segment header. +} + +uint8_t TcTransferFrame::getSequenceNumber() { + return this->frame->header.sequenceNumber; +} + +uint8_t TcTransferFrame::getSequenceFlags() { + return (this->frame->dataField & 0b11000000)>>6; +} + +uint8_t TcTransferFrame::getMAPId() { + return this->frame->dataField & 0b00111111; +} + +uint8_t* TcTransferFrame::getDataField() { + return &(this->frame->dataField) + 1; +} + +uint8_t* TcTransferFrame::getFullFrame() { + return (uint8_t*)this->frame; +} + +uint16_t TcTransferFrame::getFullSize() { + return this->getFrameLength() + 1; +} + +uint16_t TcTransferFrame::getHeaderSize() { + return sizeof(frame->header); +} + +uint16_t TcTransferFrame::getFullDataLength() { + return this->getFrameLength() - this->getHeaderSize() - FRAME_CRC_SIZE + 1; +} + +uint8_t* TcTransferFrame::getFullDataField() { + return &frame->dataField; +} + +void TcTransferFrame::print() { + sif::debug << "Raw Frame: " << std::hex << std::endl; + for (uint16_t count = 0; count < this->getFullSize(); count++ ) { + sif::debug << (uint16_t)this->getFullFrame()[count] << " "; + } + sif::debug << std::dec << std::endl; +// debug << "Frame Header:" << std::endl; +// debug << "Version Number: " << std::hex << (uint16_t)this->current_frame.getVersionNumber() << std::endl; +// debug << "Bypass Flag set?| Ctrl Cmd Flag set?: " << (uint16_t)this->current_frame.bypassFlagSet() << " | " << (uint16_t)this->current_frame.controlCommandFlagSet() << std::endl; +// debug << "SCID : " << this->current_frame.getSpacecraftId() << std::endl; +// debug << "VCID : " << (uint16_t)this->current_frame.getVirtualChannelId() << std::endl; +// debug << "Frame length: " << std::dec << this->current_frame.getFrameLength() << std::endl; +// debug << "Sequence Number: " << (uint16_t)this->current_frame.getSequenceNumber() << std::endl; +} diff --git a/fsfw/datalinklayer/TcTransferFrame.h b/fsfw/datalinklayer/TcTransferFrame.h new file mode 100644 index 0000000..b58441f --- /dev/null +++ b/fsfw/datalinklayer/TcTransferFrame.h @@ -0,0 +1,137 @@ +#ifndef TCTRANSFERFRAME_H_ +#define TCTRANSFERFRAME_H_ + +#include +#include + +/** + * The TcTransferFrame class simplifies handling of such Frames. + * It operates on any buffer passed on construction. The data length + * is determined by the length field in the frame itself. + * It has a lot of getters for convenient access to the content. + * @ingroup ccsds_handling + */ +class TcTransferFrame { +protected: + /** + * The struct that defines the Frame's Primary Header. + */ + struct TcTransferFramePrimaryHeader { + uint8_t flagsAndScid; //!< Highest byte with Flags and part of SCID. + uint8_t spacecraftId_l; //!< Byte with rest of SCID + uint8_t vcidAndLength_h; //!< Byte with VCID and part of length. + uint8_t length_l; //!< Byte with rest of length. + uint8_t sequenceNumber; //!< Lowest byte with Frame Sequence Number N(S). + }; + /** + * The struct defining the whole Transfer Frame. + */ + struct tc_transfer_frame { + TcTransferFramePrimaryHeader header; //!< The header struct. + uint8_t dataField; //!< The data field of the Transfer Frame. + }; + tc_transfer_frame* frame; //!< Pointer to a buffer where a Frame is placed. +public: + static const uint8_t FRAME_CRC_SIZE = 2; //!< Constant for the CRC size. + /** + * Empty Constructor that sets the data pointer to NULL. + */ + TcTransferFrame(); + /** + * The data pointer passed in this Constructor is casted to the #tc_transfer_frame struct. + * @param setData The data on which the class shall operate. + */ + TcTransferFrame(uint8_t* setData); + /** + * Getter. + * @return The Version number. + */ + uint8_t getVersionNumber(); + /** + * Getter. + * @return If the bypass flag is set or not. + */ + bool bypassFlagSet(); + /** + * Getter. + * @return If the control command flag is set or not. + */ + bool controlCommandFlagSet(); + /** + * Getter. + * @return If the spare bits in the Header are zero or not. + */ + bool spareIsZero(); + /** + * Getter. + * @return The Spacecraft Identifier. + */ + uint16_t getSpacecraftId(); + /** + * Getter. + * @return The Virtual Channel Identifier. + */ + uint8_t getVirtualChannelId(); + /** + * Getter. + * @return The Frame length as stored in the Header. + */ + uint16_t getFrameLength(); + /** + * Getter. + * @return The length of pure data (without CRC), assuming that a Segment Header is present. + */ + uint16_t getDataLength(); + /** + * Getter. + * @return The length of pure data (without CRC), assuming that no Segment Header is present (for BC Frames). + */ + uint16_t getFullDataLength(); + /** + * Getter. + * @return The sequence number from the header. + */ + uint8_t getSequenceNumber(); + /** + * Getter. + * @return The Sequence Flags in the Segment Header byte (right aligned). + */ + uint8_t getSequenceFlags(); + /** + * Getter. + * @return The Multiplexer Access Point Identifier from the Segment Header byte. + */ + uint8_t getMAPId(); + /** + * Getter. + * @return A pointer to the date field AFTER a Segment Header. + */ + uint8_t* getDataField(); + /** + * Getter. + * @return A pointer to the first byte in the Data Field (ignoring potential Segment Headers, for BC Frames). + */ + uint8_t* getFullDataField(); + /** + * Getter. + * @return A pointer to the beginning of the Frame. + */ + uint8_t* getFullFrame(); + /** + * Getter + * @return The total size of the Frame, which is the size stated in the Header + 1. + */ + uint16_t getFullSize(); + /** + * Getter. + * @return Size of the #TcTransferFramePrimaryHeader. + */ + uint16_t getHeaderSize(); + /** + * Debug method to print the whole Frame to screen. + */ + void print(); + +}; + +#endif /* TCTRANSFERFRAME_H_ */ diff --git a/fsfw/datalinklayer/TcTransferFrameLocal.cpp b/fsfw/datalinklayer/TcTransferFrameLocal.cpp new file mode 100644 index 0000000..e8785c9 --- /dev/null +++ b/fsfw/datalinklayer/TcTransferFrameLocal.cpp @@ -0,0 +1,49 @@ +/** + * @file TcTransferFrameLocal.cpp + * @brief This file defines the TcTransferFrameLocal class. + * @date 27.04.2013 + * @author baetz + */ + +#include "TcTransferFrameLocal.h" +#include "../globalfunctions/CRC.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include + +TcTransferFrameLocal::TcTransferFrameLocal(bool bypass, bool controlCommand, uint16_t scid, + uint8_t vcId, uint8_t sequenceNumber, uint8_t setSegmentHeader, uint8_t* data, uint16_t dataSize, uint16_t forceCrc) { + this->frame = (tc_transfer_frame*)&localData; + frame->header.flagsAndScid = (bypass << 5) + (controlCommand << 4) + ((scid & 0x0300) >> 8); + frame->header.spacecraftId_l = (scid & 0x00FF); + frame->header.vcidAndLength_h = (vcId & 0b00111111) << 2; + frame->header.length_l = sizeof(TcTransferFramePrimaryHeader) -1; + frame->header.sequenceNumber = sequenceNumber; + frame->dataField = setSegmentHeader; + if (data != NULL) { + if (bypass && controlCommand) { + memcpy(&(frame->dataField), data, dataSize); + uint16_t totalSize = sizeof(TcTransferFramePrimaryHeader) + dataSize + FRAME_CRC_SIZE -1; + frame->header.vcidAndLength_h |= (totalSize & 0x0300) >> 8; + frame->header.length_l = (totalSize & 0x00FF); + uint16_t crc = CRC::crc16ccitt(getFullFrame(), getFullSize() -2); + this->getFullFrame()[getFullSize()-2] = (crc & 0xFF00) >> 8; + this->getFullFrame()[getFullSize()-1] = (crc & 0x00FF); + } else if (dataSize <= 1016) { + memcpy(&(frame->dataField) +1, data, dataSize); + uint16_t dataCrcSize = sizeof(TcTransferFramePrimaryHeader) + 1 + dataSize + FRAME_CRC_SIZE -1; + frame->header.vcidAndLength_h |= (dataCrcSize & 0x0300) >> 8; + frame->header.length_l = (dataCrcSize & 0x00FF); + uint16_t crc = CRC::crc16ccitt(getFullFrame(), getFullSize() -2); + this->getFullFrame()[getFullSize()-2] = (crc & 0xFF00) >> 8; + this->getFullFrame()[getFullSize()-1] = (crc & 0x00FF); + } else { + sif::debug << "TcTransferFrameLocal: dataSize too large: " << dataSize << std::endl; + } + } else { + //No data in frame + } + if (forceCrc != 0 ) { + localData.data[getFullSize()-2] = (forceCrc & 0xFF00) >> 8; + localData.data[getFullSize()-1] = (forceCrc & 0x00FF); + } +} diff --git a/fsfw/datalinklayer/TcTransferFrameLocal.h b/fsfw/datalinklayer/TcTransferFrameLocal.h new file mode 100644 index 0000000..487d894 --- /dev/null +++ b/fsfw/datalinklayer/TcTransferFrameLocal.h @@ -0,0 +1,49 @@ +/** + * @file TcTransferFrameLocal.h + * @brief This file defines the TcTransferFrameLocal class. + * @date 27.04.2013 + * @author baetz + */ + +#ifndef TCTRANSFERFRAMELOCAL_H_ +#define TCTRANSFERFRAMELOCAL_H_ + +#include "TcTransferFrame.h" + +/** + * This is a helper class to locally create TC Transfer Frames. + * This is mainly required for testing purposes and therefore not very sophisticated. + * @ingroup ccsds_handling + */ +class TcTransferFrameLocal : public TcTransferFrame { +private: + /** + * A stuct to locally store the complete data. + */ + struct frameData { + TcTransferFramePrimaryHeader header; //!< The primary header. + uint8_t data[1019]; //!< The data field. + }; +public: + frameData localData; //!< The local data in the Frame. + /** + * The default Constructor. + * All parameters in the Header are passed. + * If a BC Frame is detected no segment header is created. + * Otherwise (AD and BD), the Segment Header is set. + * @param bypass The bypass flag. + * @param controlCommand The Control Command flag. + * @param scid The SCID. + * @param vcId The VCID. + * @param sequenceNumber The Frame Sequence Number N(s) + * @param setSegmentHeader A value for the Segment Header. + * @param data Data to put into the Frame Data Field. + * @param dataSize Size of the Data. + * @param forceCrc if != 0, the value is used as CRC. + */ + TcTransferFrameLocal(bool bypass, bool controlCommand, uint16_t scid, uint8_t vcId, uint8_t sequenceNumber, + uint8_t setSegmentHeader = 0xC0, uint8_t* data = NULL, uint16_t dataSize = 0, uint16_t forceCrc = 0); +}; + + +#endif /* TCTRANSFERFRAMELOCAL_H_ */ diff --git a/fsfw/datalinklayer/VirtualChannelReception.cpp b/fsfw/datalinklayer/VirtualChannelReception.cpp new file mode 100644 index 0000000..cde28de --- /dev/null +++ b/fsfw/datalinklayer/VirtualChannelReception.cpp @@ -0,0 +1,121 @@ +/** + * @file VirtualChannelReception.cpp + * @brief This file defines the VirtualChannelReception class. + * @date 26.03.2013 + * @author baetz + */ + +#include "BCFrame.h" +#include "VirtualChannelReception.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +VirtualChannelReception::VirtualChannelReception(uint8_t setChannelId, + uint8_t setSlidingWindowWidth) : + channelId(setChannelId), slidingWindowWidth(setSlidingWindowWidth), positiveWindow( + setSlidingWindowWidth / 2), negativeWindow(setSlidingWindowWidth / 2), currentState( + &openState), openState(this), waitState(this), lockoutState(this), vR(0), farmBCounter( + 0) { + internalClcw.setVirtualChannel(channelId); +} + +ReturnValue_t VirtualChannelReception::mapDemultiplexing(TcTransferFrame* frame) { + uint8_t mapId = frame->getMAPId(); + mapChannelIterator iter = mapChannels.find(mapId); + if (iter == mapChannels.end()) { +// error << "VirtualChannelReception::mapDemultiplexing on VC " << std::hex << (int) channelId +// << ": MapChannel " << (int) mapId << std::dec << " not found." << std::endl; + return VC_NOT_FOUND; + } else { + return (iter->second)->extractPackets(frame); + } +} + +ReturnValue_t VirtualChannelReception::doFARM(TcTransferFrame* frame, ClcwIF* clcw) { + uint8_t bypass = frame->bypassFlagSet(); + uint8_t controlCommand = frame->controlCommandFlagSet(); + uint8_t typeValue = (bypass << 1) + controlCommand; + switch (typeValue) { + case AD_FRAME: + return currentState->handleADFrame(frame, clcw); + case BD_FRAME: + return handleBDFrame(frame, clcw); + case BC_FRAME: + return handleBCFrame(frame, clcw); + default: + return ILLEGAL_FLAG_COMBINATION; + } +} + +ReturnValue_t VirtualChannelReception::frameAcceptanceAndReportingMechanism(TcTransferFrame* frame, + ClcwIF* clcw) { + ReturnValue_t result = RETURN_OK; + result = doFARM(frame, &internalClcw); + internalClcw.setReceiverFrameSequenceNumber(vR); + internalClcw.setFarmBCount(farmBCounter); + clcw->setWhole(internalClcw.getAsWhole()); + switch (result) { + case RETURN_OK: + return mapDemultiplexing(frame); + case BC_IS_SET_VR_COMMAND: + case BC_IS_UNLOCK_COMMAND: + //Need to catch these codes to avoid error reporting later. + return RETURN_OK; + default: + break; + } + return result; +} + +ReturnValue_t VirtualChannelReception::addMapChannel(uint8_t mapId, MapPacketExtractionIF* object) { + std::pair returnValue = mapChannels.insert( + std::pair(mapId, object)); + if (returnValue.second == true) { + return RETURN_OK; + } else { + return RETURN_FAILED; + } +} + +ReturnValue_t VirtualChannelReception::handleBDFrame(TcTransferFrame* frame, ClcwIF* clcw) { + farmBCounter++; + return RETURN_OK; +} + +ReturnValue_t VirtualChannelReception::handleBCFrame(TcTransferFrame* frame, ClcwIF* clcw) { + BcFrame content; + ReturnValue_t returnValue = content.initialize(frame->getFullDataField(), + frame->getFullDataLength()); + if (returnValue == BC_IS_UNLOCK_COMMAND) { + returnValue = currentState->handleBCUnlockCommand(clcw); + } else if (returnValue == BC_IS_SET_VR_COMMAND) { + returnValue = currentState->handleBCSetVrCommand(clcw, content.vR); + } else { + //Do nothing + } + return returnValue; +} + +uint8_t VirtualChannelReception::getChannelId() const { + return channelId; +} + +ReturnValue_t VirtualChannelReception::initialize() { + ReturnValue_t returnValue = RETURN_FAILED; + if ((slidingWindowWidth > 254) || (slidingWindowWidth % 2 != 0)) { + sif::error << "VirtualChannelReception::initialize: Illegal sliding window width: " + << (int) slidingWindowWidth << std::endl; + return RETURN_FAILED; + } + for (mapChannelIterator iterator = mapChannels.begin(); iterator != mapChannels.end(); + iterator++) { + returnValue = iterator->second->initialize(); + if (returnValue != RETURN_OK) + break; + } + return returnValue; +} + +void VirtualChannelReception::setToWaitState() { + internalClcw.setWaitFlag(true); + this->currentState = &waitState; +} diff --git a/fsfw/datalinklayer/VirtualChannelReception.h b/fsfw/datalinklayer/VirtualChannelReception.h new file mode 100644 index 0000000..9b4e298 --- /dev/null +++ b/fsfw/datalinklayer/VirtualChannelReception.h @@ -0,0 +1,114 @@ +/** + * @file VirtualChannelReception.h + * @brief This file defines the VirtualChannelReception class. + * @date 25.03.2013 + * @author baetz + */ + +#ifndef VIRTUALCHANNELRECEPTION_H_ +#define VIRTUALCHANNELRECEPTION_H_ + +#include "CCSDSReturnValuesIF.h" +#include "Clcw.h" +#include "Farm1StateIF.h" +#include "Farm1StateLockout.h" +#include "Farm1StateOpen.h" +#include "Farm1StateWait.h" +#include "MapPacketExtractionIF.h" +#include "VirtualChannelReceptionIF.h" +#include +/** + * Implementation of a TC Virtual Channel. + * This is a full implementation of a virtual channel as specified in the CCSDS TC Space Data Link + * Protocol. It is designed to operate within an instance of the @c DataLinkLayer class. + * Features: + * - any (6bit) Virtual Channel ID is assignable. + * - Supports an arbitrary number of MAP Channels (with a map). + * - Has a complete FARM-1 Machine built-in. + * + * The FARM-1 state machine uses the State Pattern. + */ +class VirtualChannelReception : public VirtualChannelReceptionIF, public CCSDSReturnValuesIF { + friend class Farm1StateOpen; + friend class Farm1StateWait; + friend class Farm1StateLockout; +private: + uint8_t channelId; //!< Stores the VCID that was assigned on construction. + uint8_t slidingWindowWidth; //!< A constant to set the FARM-1 sliding window width. + uint8_t positiveWindow; //!< The positive window for the FARM-1 machine. + uint8_t negativeWindow; //!< The negative window for the FARM-1 machine. +protected: + Farm1StateIF* currentState; //!< The current state. To change, one of the other states must be assigned to this pointer. + Farm1StateOpen openState; //!< Instance of the FARM-1 State "Open". + Farm1StateWait waitState; //!< Instance of the FARM-1 State "Wait". + Farm1StateLockout lockoutState; //!< Instance of the FARM-1 State "Lockout". + Clcw internalClcw; //!< A CLCW class to internally set the values before writing them back to the TTC System. + uint8_t vR; //!< The Receiver Frame Sequence Number V(R) as it shall be maintained for every Virtual Channel. + uint8_t farmBCounter; //!< The FARM-B COunter as it shall be maintained for every Virtual Channel. + typedef std::map::iterator mapChannelIterator; //!< Typedef to simplify handling of the mapChannels map. + std::map mapChannels; //!< A map that maintains all map Channels. Channels must be configured on initialization. MAy be omitted in a simplified version. + /** + * This method handles demultiplexing to different map channels. + * It parses the entries of #mapChannels and forwards the Frame to a found MAP Channel. + * @param frame The frame to forward. + * @return #VC_NOT_FOUND or the return value of the map channel extraction. + */ + ReturnValue_t mapDemultiplexing( TcTransferFrame* frame ); + /** + * A sub-method that actually does the FARM-1 handling for different Frame types. + * @param frame The Tc Transfer Frame to handle. + * @param clcw Any changes on the CLCW shall be done with this method. + * @return The return code of higher methods or @c ILLEGAL_FLAG_COMBINATION. + */ + ReturnValue_t doFARM(TcTransferFrame* frame, ClcwIF* clcw); + /** + * Handles incoming BD Frames. + * Handling these Frames is independent of the State, so no subcall to #currentState is + * required. + * @param frame The Tc Transfer Frame to handle. + * @param clcw Any changes on the CLCW shall be done with this method. + * @return Always returns @c RETURN_OK. + */ + ReturnValue_t handleBDFrame( TcTransferFrame* frame, ClcwIF* clcw ); + /** + * Handles incoming BC Frames. + * The type of the BC Frame is detected and checked first, then methods of #currentState are called. + * @param frame The Tc Transfer Frame to handle. + * @param clcw Any changes on the CLCW shall be done with this method. + * @return The failure code of BC Frame interpretation or the return code of higher methods. + */ + ReturnValue_t handleBCFrame( TcTransferFrame* frame, ClcwIF* clcw ); +public: + /** + * Default constructor. + * Only sets the channelId of the channel. Setting the Sliding Window width is possible as well. + * @param setChannelId Virtual Channel Identifier (VCID) of the channel. + */ + VirtualChannelReception( uint8_t setChannelId, uint8_t setSlidingWindowWidth ); + ReturnValue_t frameAcceptanceAndReportingMechanism( TcTransferFrame* frame, ClcwIF* clcw ); + /** + * Helper method to simplify adding a mapChannel during construction. + * @param mapId The mapId of the object to add. + * @param object Pointer to the MapPacketExtraction object itself. + * @return @c RETURN_OK if the channel was successfully inserted, @c RETURN_FAILED otherwise. + */ + ReturnValue_t addMapChannel( uint8_t mapId, MapPacketExtractionIF* object ); + /** + * The initialization routine checks the set #slidingWindowWidth and initializes all MAP + * channels. + * @return @c RETURN_OK on successful initialization, @c RETURN_FAILED otherwise. + */ + ReturnValue_t initialize(); + /** + * Getter for the VCID. + * @return The #channelId. + */ + uint8_t getChannelId() const; + /** + * Small method to set the state to Farm1StateWait. + */ + void setToWaitState(); +}; + + +#endif /* VIRTUALCHANNELRECEPTION_H_ */ diff --git a/fsfw/datalinklayer/VirtualChannelReceptionIF.h b/fsfw/datalinklayer/VirtualChannelReceptionIF.h new file mode 100644 index 0000000..36f60e8 --- /dev/null +++ b/fsfw/datalinklayer/VirtualChannelReceptionIF.h @@ -0,0 +1,57 @@ +/** + * @file VirtualChannelReceptionIF.h + * @brief This file defines the VirtualChannelReceptionIF class. + * @date 25.03.2013 + * @author baetz + */ + +#ifndef VIRTUALCHANNELRECEPTIONIF_H_ +#define VIRTUALCHANNELRECEPTIONIF_H_ + +#include "ClcwIF.h" +#include "TcTransferFrame.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +/** + * This is the interface for Virtual Channel reception classes. + * It represents a single TC Virtual Channel that operates on one IO + */ +class VirtualChannelReceptionIF { +public: + /** + * Enum including all valid types of frames. + * The type is made up by two flags, so 0b1111 is definitely illegal. + */ + enum frameType { + AD_FRAME = 0b00, + BC_FRAME = 0b11, + BD_FRAME = 0b10, + ILLEGAL_FRAME = 0b1111 + }; + /** + * Empty virtual destructor. + */ + virtual ~VirtualChannelReceptionIF() { + } + /** + * This method shall accept frames and do all FARM-1 stuff. + * Handling the Frame includes forwarding to higher-level procedures. + * @param frame The Tc Transfer Frame that was received and checked. + * @param clcw Any changes to the CLCW value are forwarded by using this parameter. + * @return The return Value shall indicate successful processing with @c RETURN_OK. + */ + virtual ReturnValue_t frameAcceptanceAndReportingMechanism( TcTransferFrame* frame, ClcwIF* clcw ) = 0; + /** + * If any other System Objects are required for operation they shall be initialized here. + * @return @c RETURN_OK for successful initialization. + */ + virtual ReturnValue_t initialize() = 0; + /** + * Getter for the VCID. + * @return The #channelId. + */ + virtual uint8_t getChannelId() const = 0; +}; + + +#endif /* VIRTUALCHANNELRECEPTIONIF_H_ */ diff --git a/fsfw/datapool/ControllerSet.cpp b/fsfw/datapool/ControllerSet.cpp new file mode 100644 index 0000000..6339034 --- /dev/null +++ b/fsfw/datapool/ControllerSet.cpp @@ -0,0 +1,14 @@ +#include "ControllerSet.h" + +ControllerSet::ControllerSet() { + +} + +ControllerSet::~ControllerSet() { +} + +void ControllerSet::setInvalid() { + read(); + setToDefault(); + commit(PoolVariableIF::INVALID); +} diff --git a/fsfw/datapool/ControllerSet.h b/fsfw/datapool/ControllerSet.h new file mode 100644 index 0000000..e27debf --- /dev/null +++ b/fsfw/datapool/ControllerSet.h @@ -0,0 +1,15 @@ +#ifndef CONTROLLERSET_H_ +#define CONTROLLERSET_H_ + +#include "DataSet.h" + +class ControllerSet :public DataSet { +public: + ControllerSet(); + virtual ~ControllerSet(); + + virtual void setToDefault() = 0; + void setInvalid(); +}; + +#endif /* CONTROLLERSET_H_ */ diff --git a/fsfw/datapool/DataPool.cpp b/fsfw/datapool/DataPool.cpp new file mode 100644 index 0000000..1b6cb7a --- /dev/null +++ b/fsfw/datapool/DataPool.cpp @@ -0,0 +1,131 @@ +#include "DataPool.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../ipc/MutexFactory.h" + +DataPool::DataPool( void ( *initFunction )( std::map* pool_map ) ) { + mutex = MutexFactory::instance()->createMutex(); + if (initFunction != NULL ) { + initFunction( &this->data_pool ); + } +} + +DataPool::~DataPool() { + MutexFactory::instance()->deleteMutex(mutex); + for ( std::map::iterator it = this->data_pool.begin(); it != this->data_pool.end(); ++it ) { + delete it->second; + } +} + +//The function checks PID, type and array length before returning a copy of the PoolEntry. In failure case, it returns a temp-Entry with size 0 and NULL-ptr. +template PoolEntry* DataPool::getData( uint32_t data_pool_id, uint8_t sizeOrPosition ) { + std::map::iterator it = this->data_pool.find( data_pool_id ); + if ( it != this->data_pool.end() ) { + PoolEntry* entry = dynamic_cast< PoolEntry* >( it->second ); + if (entry != NULL ) { + if ( sizeOrPosition <= entry->length ) { + return entry; + } + } + } + return NULL; +} + +PoolEntryIF* DataPool::getRawData( uint32_t data_pool_id ) { + std::map::iterator it = this->data_pool.find( data_pool_id ); + if ( it != this->data_pool.end() ) { + return it->second; + } else { + return NULL; + } +} + +//uint8_t DataPool::getRawData( uint32_t data_pool_id, uint8_t* address, uint16_t* size, uint32_t maxSize ) { +// std::map::iterator it = this->data_pool.find( data_pool_id ); +// if ( it != this->data_pool.end() ) { +// if ( it->second->getByteSize() <= maxSize ) { +// *size = it->second->getByteSize(); +// memcpy( address, it->second->getRawData(), *size ); +// return DP_SUCCESSFUL; +// } +// } +// *size = 0; +// return DP_FAILURE; +//} + +ReturnValue_t DataPool::freeDataPoolLock() { + ReturnValue_t status = mutex->unlockMutex(); + if ( status != RETURN_OK ) { + sif::error << "DataPool::DataPool: unlock of mutex failed with error code: " << status << std::endl; + } + return status; +} + +ReturnValue_t DataPool::lockDataPool() { + ReturnValue_t status = mutex->lockMutex(MutexIF::BLOCKING); + if ( status != RETURN_OK ) { + sif::error << "DataPool::DataPool: lock of mutex failed with error code: " << status << std::endl; + } + return status; +} + +void DataPool::print() { + sif::debug << "DataPool contains: " << std::endl; + std::map::iterator dataPoolIt; + dataPoolIt = this->data_pool.begin(); + while( dataPoolIt != this->data_pool.end() ) { + sif::debug << std::hex << dataPoolIt->first << std::dec << " |"; + dataPoolIt->second->print(); + dataPoolIt++; + } +} + +template PoolEntry* DataPool::getData( uint32_t data_pool_id, uint8_t size ); +template PoolEntry* DataPool::getData( uint32_t data_pool_id, uint8_t size ); +template PoolEntry* DataPool::getData( uint32_t data_pool_id, uint8_t size ); +template PoolEntry* DataPool::getData(uint32_t data_pool_id, + uint8_t size); +template PoolEntry* DataPool::getData( uint32_t data_pool_id, uint8_t size ); +template PoolEntry* DataPool::getData( uint32_t data_pool_id, uint8_t size ); +template PoolEntry* DataPool::getData( uint32_t data_pool_id, uint8_t size ); +template PoolEntry* DataPool::getData( uint32_t data_pool_id, uint8_t size ); +template PoolEntry* DataPool::getData(uint32_t data_pool_id, + uint8_t size); + + +uint32_t DataPool::PIDToDataPoolId(uint32_t parameter_id) { + return (parameter_id >> 8) & 0x00FFFFFF; +} + +uint8_t DataPool::PIDToArrayIndex(uint32_t parameter_id) { + return (parameter_id & 0x000000FF); +} + +uint32_t DataPool::poolIdAndPositionToPid(uint32_t poolId, uint8_t index) { + return (poolId << 8) + index; +} + + +//SHOULDDO: Do we need a mutex lock here... I don't think so, as we only check static const values of elements in a list that do not change. +//there is no guarantee in the standard, but it seems to me that the implementation is safe -UM +ReturnValue_t DataPool::getType(uint32_t parameter_id, Type* type) { + std::map::iterator it = this->data_pool.find( PIDToDataPoolId(parameter_id)); + if ( it != this->data_pool.end() ) { + *type = it->second->getType(); + return RETURN_OK; + } else { + *type = Type::UNKNOWN_TYPE; + return RETURN_FAILED; + } +} + +bool DataPool::exists(uint32_t parameterId) { + uint32_t poolId = PIDToDataPoolId(parameterId); + uint32_t index = PIDToArrayIndex(parameterId); + std::map::iterator it = this->data_pool.find( poolId ); + if (it != data_pool.end()) { + if (it->second->getSize() >= index) { + return true; + } + } + return false; +} diff --git a/fsfw/datapool/DataPool.h b/fsfw/datapool/DataPool.h new file mode 100644 index 0000000..7abf289 --- /dev/null +++ b/fsfw/datapool/DataPool.h @@ -0,0 +1,135 @@ +/** + * \file DataPool.h + * + * \date 10/17/2012 + * \author Bastian Baetz + * + * \brief This file contains the definition of the DataPool class and (temporarily) + * the "extern" definition of the global dataPool instance. + */ + +#ifndef DATAPOOL_H_ +#define DATAPOOL_H_ + +#include "PoolEntry.h" +#include "../globalfunctions/Type.h" +#include "../ipc/MutexIF.h" +#include + +/** + * \defgroup data_pool Data Pool + * This is the group, where all classes associated with Data Pool Handling belong to. + * This includes classes to access Data Pool variables. + */ + +#define DP_SUCCESSFUL 0 +#define DP_FAILURE 1 + +/** + * \brief This class represents the OBSW global data-pool. + * + * \details All variables are registered and space is allocated in an initialization + * function, which is passed do the constructor. + * Space for the variables is allocated on the heap (with a new call). + * The data is found by a data pool id, which uniquely represents a variable. + * Data pool variables should be used with a blackboard logic in mind, + * which means read data is valid (if flagged so), but not necessarily up-to-date. + * Variables are either single values or arrays. + * \ingroup data_pool + */ +class DataPool : public HasReturnvaluesIF { +private: + /** + * \brief This is the actual data pool itself. + * \details It is represented by a map + * with the data pool id as index and a pointer to a single PoolEntry as value. + */ + std::map data_pool; +public: + /** + * \brief The mutex is created in the constructor and makes access mutual exclusive. + * \details Locking and unlocking the pool is only done by the DataSet class. + */ + MutexIF* mutex; + /** + * \brief In the classes constructor, the passed initialization function is called. + * \details To enable filling the pool, + * a pointer to the map is passed, allowing direct access to the pool's content. + * On runtime, adding or removing variables is forbidden. + */ + DataPool( void ( *initFunction )( std::map* pool_map ) ); + /** + * \brief The destructor iterates through the data_pool map and calls all Entries destructors to clean up the heap. + */ + ~DataPool(); + /** + * \brief This is the default call to access the pool. + * \details A pointer to the PoolEntry object is returned. + * The call checks data pool id, type and array size. Returns NULL in case of failure. + * \param data_pool_id The data pool id to search. + * \param sizeOrPosition The array size (not byte size!) of the pool entry, or the position the user wants to read. + * If smaller than the entry size, everything's ok. + */ + template PoolEntry* getData( uint32_t data_pool_id, uint8_t sizeOrPosition ); + /** + * \brief An alternative call to get a data pool entry in case the type is not implicitly known + * (i.e. in Housekeeping Telemetry). + * \details It returns a basic interface and does NOT perform + * a size check. The caller has to assure he does not copy too much data. + * Returns NULL in case the entry is not found. + * \param data_pool_id The data pool id to search. + */ + PoolEntryIF* getRawData( uint32_t data_pool_id ); + /** + * \brief This is a small helper function to facilitate locking the global data pool. + * \details It fetches the pool's mutex id and tries to acquire the mutex. + */ + ReturnValue_t lockDataPool(); + /** + * \brief This is a small helper function to facilitate unlocking the global data pool. + * \details It fetches the pool's mutex id and tries to free the mutex. + */ + ReturnValue_t freeDataPoolLock(); + /** + * \brief The print call is a simple debug method. + * \details It prints the current content of the data pool. + * It iterates through the data_pool map and calls each entry's print() method. + */ + void print(); + /** + * Extracts the data pool id from a SCOS 2000 PID. + * @param parameter_id The passed Parameter ID. + * @return The data pool id as used within the OBSW. + */ + static uint32_t PIDToDataPoolId( uint32_t parameter_id ); + /** + * Extracts an array index out of a SCOS 2000 PID. + * @param parameter_id The passed Parameter ID. + * @return The index of the corresponding data pool entry. + */ + static uint8_t PIDToArrayIndex( uint32_t parameter_id ); + /** + * Retransforms a data pool id and an array index to a SCOS 2000 PID. + */ + static uint32_t poolIdAndPositionToPid( uint32_t poolId, uint8_t index ); + + /** + * Method to return the type of a pool variable. + * @param parameter_id A parameterID (not pool id) of a DP member. + * @param type Returns the type or TYPE::UNKNOWN_TYPE + * @return RETURN_OK if parameter exists, RETURN_FAILED else. + */ + ReturnValue_t getType( uint32_t parameter_id, Type* type ); + + /** + * Method to check if a PID exists. + * Does not lock, as there's no possibility to alter the list that is checked during run-time. + * @param parameterId The PID (not pool id!) of a parameter. + * @return true if exists, false else. + */ + bool exists(uint32_t parameterId); +}; + +//We assume someone globally instantiates a DataPool. +extern DataPool dataPool; +#endif /* DATAPOOL_H_ */ diff --git a/fsfw/datapool/DataPoolAdmin.cpp b/fsfw/datapool/DataPoolAdmin.cpp new file mode 100644 index 0000000..f298d53 --- /dev/null +++ b/fsfw/datapool/DataPoolAdmin.cpp @@ -0,0 +1,300 @@ +#include "DataPool.h" +#include "DataPoolAdmin.h" +#include "DataSet.h" +#include "PoolRawAccess.h" +#include "../ipc/CommandMessage.h" +#include "../ipc/QueueFactory.h" +#include "../parameters/ParameterMessage.h" + +DataPoolAdmin::DataPoolAdmin(object_id_t objectId) : + SystemObject(objectId), storage(NULL), commandQueue(NULL), memoryHelper( + this, NULL), actionHelper(this, NULL) { + commandQueue = QueueFactory::instance()->createMessageQueue(); +} + +DataPoolAdmin::~DataPoolAdmin() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); +} + +ReturnValue_t DataPoolAdmin::performOperation(uint8_t opCode) { + handleCommand(); + return RETURN_OK; +} + +MessageQueueId_t DataPoolAdmin::getCommandQueue() const { + return commandQueue->getId(); +} + +ReturnValue_t DataPoolAdmin::executeAction(ActionId_t actionId, + MessageQueueId_t commandedBy, const uint8_t* data, size_t size) { + if (actionId != SET_VALIDITY) { + return INVALID_ACTION_ID; + } + + if (size != 5) { + return INVALID_PARAMETERS; + } + + uint32_t address = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) + | data[3]; + + uint8_t valid = data[4]; + + uint32_t poolId = ::dataPool.PIDToDataPoolId(address); + + DataSet mySet; + PoolRawAccess variable(poolId, 0, &mySet, PoolVariableIF::VAR_READ_WRITE); + ReturnValue_t status = mySet.read(); + if (status != RETURN_OK) { + return INVALID_ADDRESS; + } + if (valid != 0) { + variable.setValid(PoolVariableIF::VALID); + } else { + variable.setValid(PoolVariableIF::INVALID); + } + + mySet.commit(); + + return EXECUTION_FINISHED; +} + +ReturnValue_t DataPoolAdmin::getParameter(uint8_t domainId, + uint16_t parameterId, ParameterWrapper* parameterWrapper, + const ParameterWrapper* newValues, uint16_t startAtIndex) { + return HasReturnvaluesIF::RETURN_FAILED; +} + +void DataPoolAdmin::handleCommand() { + CommandMessage command; + ReturnValue_t result = commandQueue->receiveMessage(&command); + if (result != RETURN_OK) { + return; + } + + result = actionHelper.handleActionMessage(&command); + + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + + result = handleParameterCommand(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + + result = memoryHelper.handleMemoryCommand(&command); + if (result != RETURN_OK) { + command.setToUnknownCommand(); + commandQueue->reply(&command); + } +} + +ReturnValue_t DataPoolAdmin::handleMemoryLoad(uint32_t address, + const uint8_t* data, size_t size, uint8_t** dataPointer) { + uint32_t poolId = ::dataPool.PIDToDataPoolId(address); + uint8_t arrayIndex = ::dataPool.PIDToArrayIndex(address); + DataSet testSet; + PoolRawAccess varToGetSize(poolId, arrayIndex, &testSet, + PoolVariableIF::VAR_READ); + ReturnValue_t status = testSet.read(); + if (status != RETURN_OK) { + return INVALID_ADDRESS; + } + uint8_t typeSize = varToGetSize.getSizeOfType(); + + if (size % typeSize != 0) { + return INVALID_SIZE; + } + + if (size > varToGetSize.getSizeTillEnd()) { + return INVALID_SIZE; + } + const uint8_t* readPosition = data; + + for (; size > 0; size -= typeSize) { + DataSet rawSet; + PoolRawAccess variable(poolId, arrayIndex, &rawSet, + PoolVariableIF::VAR_READ_WRITE); + status = rawSet.read(); + if (status == RETURN_OK) { + status = variable.setEntryFromBigEndian(readPosition, typeSize); + if (status == RETURN_OK) { + status = rawSet.commit(); + } + } + arrayIndex += 1; + readPosition += typeSize; + } + return ACTIVITY_COMPLETED; +} + +ReturnValue_t DataPoolAdmin::handleMemoryDump(uint32_t address, size_t size, + uint8_t** dataPointer, uint8_t* copyHere) { + uint32_t poolId = ::dataPool.PIDToDataPoolId(address); + uint8_t arrayIndex = ::dataPool.PIDToArrayIndex(address); + DataSet testSet; + PoolRawAccess varToGetSize(poolId, arrayIndex, &testSet, + PoolVariableIF::VAR_READ); + ReturnValue_t status = testSet.read(); + if (status != RETURN_OK) { + return INVALID_ADDRESS; + } + uint8_t typeSize = varToGetSize.getSizeOfType(); + if (size > varToGetSize.getSizeTillEnd()) { + return INVALID_SIZE; + } + uint8_t* ptrToCopy = copyHere; + for (; size > 0; size -= typeSize) { + DataSet rawSet; + PoolRawAccess variable(poolId, arrayIndex, &rawSet, + PoolVariableIF::VAR_READ); + status = rawSet.read(); + if (status == RETURN_OK) { + size_t temp = 0; + status = variable.getEntryEndianSafe(ptrToCopy, &temp, size); + if (status != RETURN_OK) { + return RETURN_FAILED; + } + } else { + //Error reading parameter. + } + arrayIndex += 1; + ptrToCopy += typeSize; + } + return ACTIVITY_COMPLETED; +} + +ReturnValue_t DataPoolAdmin::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = memoryHelper.initialize(commandQueue); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + storage = objectManager->get(objects::IPC_STORE); + if (storage == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + + result = actionHelper.initialize(commandQueue); + + return result; +} + +//mostly identical to ParameterHelper::handleParameterMessage() +ReturnValue_t DataPoolAdmin::handleParameterCommand(CommandMessage* command) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED; + switch (command->getCommand()) { + case ParameterMessage::CMD_PARAMETER_DUMP: { + uint8_t domain = HasParametersIF::getDomain( + ParameterMessage::getParameterId(command)); + uint16_t parameterId = HasParametersIF::getMatrixId( + ParameterMessage::getParameterId(command)); + + DataPoolParameterWrapper wrapper; + result = wrapper.set(domain, parameterId); + + if (result == HasReturnvaluesIF::RETURN_OK) { + result = sendParameter(command->getSender(), + ParameterMessage::getParameterId(command), &wrapper); + } + } + break; + case ParameterMessage::CMD_PARAMETER_LOAD: { + + uint8_t domain = HasParametersIF::getDomain( + ParameterMessage::getParameterId(command)); + uint16_t parameterId = HasParametersIF::getMatrixId( + ParameterMessage::getParameterId(command)); + uint8_t index = HasParametersIF::getIndex( + ParameterMessage::getParameterId(command)); + + const uint8_t *storedStream; + size_t storedStreamSize; + result = storage->getData(ParameterMessage::getStoreId(command), + &storedStream, &storedStreamSize); + if (result != HasReturnvaluesIF::RETURN_OK) { + break; + } + + ParameterWrapper streamWrapper; + result = streamWrapper.set(storedStream, storedStreamSize); + if (result != HasReturnvaluesIF::RETURN_OK) { + storage->deleteData(ParameterMessage::getStoreId(command)); + break; + } + + DataPoolParameterWrapper poolWrapper; + result = poolWrapper.set(domain, parameterId); + if (result != HasReturnvaluesIF::RETURN_OK) { + storage->deleteData(ParameterMessage::getStoreId(command)); + break; + } + + result = poolWrapper.copyFrom(&streamWrapper, index); + + storage->deleteData(ParameterMessage::getStoreId(command)); + + if (result == HasReturnvaluesIF::RETURN_OK) { + result = sendParameter(command->getSender(), + ParameterMessage::getParameterId(command), &poolWrapper); + } + } + break; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + + if (result != HasReturnvaluesIF::RETURN_OK) { + rejectCommand(command->getSender(), result, command->getCommand()); + } + + return HasReturnvaluesIF::RETURN_OK; + +} + +//identical to ParameterHelper::sendParameter() +ReturnValue_t DataPoolAdmin::sendParameter(MessageQueueId_t to, uint32_t id, + const DataPoolParameterWrapper* wrapper) { + size_t serializedSize = wrapper->getSerializedSize(); + + uint8_t *storeElement; + store_address_t address; + + ReturnValue_t result = storage->getFreeElement(&address, serializedSize, + &storeElement); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + size_t storeElementSize = 0; + + result = wrapper->serialize(&storeElement, &storeElementSize, + serializedSize, SerializeIF::Endianness::BIG); + + if (result != HasReturnvaluesIF::RETURN_OK) { + storage->deleteData(address); + return result; + } + + CommandMessage reply; + + ParameterMessage::setParameterDumpReply(&reply, id, address); + + commandQueue->sendMessage(to, &reply); + + return HasReturnvaluesIF::RETURN_OK; +} + +//identical to ParameterHelper::rejectCommand() +void DataPoolAdmin::rejectCommand(MessageQueueId_t to, ReturnValue_t reason, + Command_t initialCommand) { + CommandMessage reply; + reply.setReplyRejected(reason, initialCommand); + commandQueue->sendMessage(to, &reply); +} diff --git a/fsfw/datapool/DataPoolAdmin.h b/fsfw/datapool/DataPoolAdmin.h new file mode 100644 index 0000000..e2d1e9e --- /dev/null +++ b/fsfw/datapool/DataPoolAdmin.h @@ -0,0 +1,58 @@ +#ifndef DATAPOOLADMIN_H_ +#define DATAPOOLADMIN_H_ + +#include "../memory/MemoryHelper.h" +#include "../action/HasActionsIF.h" +#include "../action/SimpleActionHelper.h" +#include "../objectmanager/SystemObject.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../parameters/ReceivesParameterMessagesIF.h" +#include "DataPoolParameterWrapper.h" +#include "../ipc/MessageQueueIF.h" + +class DataPoolAdmin: public HasActionsIF, + public ExecutableObjectIF, + public AcceptsMemoryMessagesIF, + public HasReturnvaluesIF, + public ReceivesParameterMessagesIF, + public SystemObject { +public: + static const ActionId_t SET_VALIDITY = 1; + + DataPoolAdmin(object_id_t objectId); + + ~DataPoolAdmin(); + + ReturnValue_t performOperation(uint8_t opCode); + + MessageQueueId_t getCommandQueue() const; + + ReturnValue_t handleMemoryLoad(uint32_t address, const uint8_t* data, + size_t size, uint8_t** dataPointer); + ReturnValue_t handleMemoryDump(uint32_t address, size_t size, + uint8_t** dataPointer, uint8_t* copyHere); + + ReturnValue_t executeAction(ActionId_t actionId, + MessageQueueId_t commandedBy, const uint8_t* data, size_t size); + + //not implemented as ParameterHelper is no used + ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); + + ReturnValue_t initialize(); +private: + StorageManagerIF *storage; + MessageQueueIF* commandQueue; + MemoryHelper memoryHelper; + SimpleActionHelper actionHelper; + void handleCommand(); + ReturnValue_t handleParameterCommand(CommandMessage *command); + ReturnValue_t sendParameter(MessageQueueId_t to, uint32_t id, + const DataPoolParameterWrapper* wrapper); + void rejectCommand(MessageQueueId_t to, ReturnValue_t reason, + Command_t initialCommand); +}; + +#endif /* DATAPOOLADMIN_H_ */ diff --git a/fsfw/datapool/DataPoolParameterWrapper.cpp b/fsfw/datapool/DataPoolParameterWrapper.cpp new file mode 100644 index 0000000..7936740 --- /dev/null +++ b/fsfw/datapool/DataPoolParameterWrapper.cpp @@ -0,0 +1,181 @@ +#include "DataPoolParameterWrapper.h" + +//for returncodes +#include "../parameters/HasParametersIF.h" + +#include "DataSet.h" +#include "PoolRawAccess.h" + +DataPoolParameterWrapper::DataPoolParameterWrapper() : + type(Type::UNKNOWN_TYPE), rows(0), columns(0), poolId( + PoolVariableIF::NO_PARAMETER) { + +} + +DataPoolParameterWrapper::~DataPoolParameterWrapper() { + +} + +ReturnValue_t DataPoolParameterWrapper::set(uint8_t domainId, + uint16_t parameterId) { + poolId = (domainId << 16) + parameterId; + + DataSet mySet; + PoolRawAccess raw(poolId, 0, &mySet, PoolVariableIF::VAR_READ); + ReturnValue_t status = mySet.read(); + if (status != HasReturnvaluesIF::RETURN_OK) { + //should only fail for invalid pool id + return HasParametersIF::INVALID_MATRIX_ID; + } + + type = raw.getType(); + rows = raw.getArraySize(); + columns = 1; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t DataPoolParameterWrapper::serialize(uint8_t** buffer, + size_t* size, size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result; + + result = SerializeAdapter::serialize(&type, buffer, size, maxSize, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = SerializeAdapter::serialize(&columns, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&rows, buffer, size, maxSize, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + for (uint8_t index = 0; index < rows; index++){ + DataSet mySet; + PoolRawAccess raw(poolId, index, &mySet,PoolVariableIF::VAR_READ); + mySet.read(); + result = raw.serialize(buffer,size,maxSize,streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + } + return HasReturnvaluesIF::RETURN_OK; +} + +//same as ParameterWrapper +size_t DataPoolParameterWrapper::getSerializedSize() const { + size_t serializedSize = 0; + serializedSize += type.getSerializedSize(); + serializedSize += sizeof(rows); + serializedSize += sizeof(columns); + serializedSize += rows * columns * type.getSize(); + + return serializedSize; +} + +ReturnValue_t DataPoolParameterWrapper::deSerialize(const uint8_t** buffer, + size_t* size, Endianness streamEndianness) { + return HasReturnvaluesIF::RETURN_FAILED; +} + +template +ReturnValue_t DataPoolParameterWrapper::deSerializeData(uint8_t startingRow, + uint8_t startingColumn, const void* from, uint8_t fromRows) { + //treat from as a continuous Stream as we copy all of it + const uint8_t *fromAsStream = (const uint8_t *) from; + + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + + for (uint8_t fromRow = 0; fromRow < fromRows; fromRow++) { + + DataSet mySet; + PoolRawAccess raw(poolId, startingRow + fromRow, &mySet, + PoolVariableIF::VAR_READ_WRITE); + mySet.read(); + + result = raw.setEntryFromBigEndian(fromAsStream, sizeof(T)); + + fromAsStream += sizeof(T); + + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + mySet.commit(); + } + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t DataPoolParameterWrapper::copyFrom(const ParameterWrapper* from, + uint16_t startWritingAtIndex) { + if (poolId == PoolVariableIF::NO_PARAMETER) { + return ParameterWrapper::NOT_SET; + } + + if (type != from->type) { + return ParameterWrapper::DATATYPE_MISSMATCH; + } + + //check if from fits into this + uint8_t startingRow = startWritingAtIndex / columns; + uint8_t startingColumn = startWritingAtIndex % columns; + + if ((from->rows > (rows - startingRow)) + || (from->columns > (columns - startingColumn))) { + return ParameterWrapper::TOO_BIG; + } + + ReturnValue_t result; + //copy data + if (from->pointsToStream) { + switch (type) { + case Type::UINT8_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows); + break; + case Type::INT8_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows); + break; + case Type::UINT16_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows); + break; + case Type::INT16_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows); + break; + case Type::UINT32_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows); + break; + case Type::INT32_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows); + break; + case Type::FLOAT: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows); + break; + case Type::DOUBLE: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows); + break; + default: + result = ParameterWrapper::UNKNOW_DATATYPE; + break; + } + } else { + //not supported + return HasReturnvaluesIF::RETURN_FAILED; + } + + return result; +} diff --git a/fsfw/datapool/DataPoolParameterWrapper.h b/fsfw/datapool/DataPoolParameterWrapper.h new file mode 100644 index 0000000..b1e505a --- /dev/null +++ b/fsfw/datapool/DataPoolParameterWrapper.h @@ -0,0 +1,38 @@ +#ifndef DATAPOOLPARAMETERWRAPPER_H_ +#define DATAPOOLPARAMETERWRAPPER_H_ + +#include "../globalfunctions/Type.h" +#include "../parameters/ParameterWrapper.h" + +class DataPoolParameterWrapper: public SerializeIF { +public: + DataPoolParameterWrapper(); + virtual ~DataPoolParameterWrapper(); + + ReturnValue_t set(uint8_t domainId, uint16_t parameterId); + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override; + + virtual size_t getSerializedSize() const override; + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; + + ReturnValue_t copyFrom(const ParameterWrapper *from, + uint16_t startWritingAtIndex); + +private: + Type type; + uint8_t rows; + uint8_t columns; + + uint32_t poolId; + + template + ReturnValue_t deSerializeData(uint8_t startingRow, uint8_t startingColumn, + const void *from, uint8_t fromRows); + +}; + +#endif /* DATAPOOLPARAMETERWRAPPER_H_ */ diff --git a/fsfw/datapool/DataSet.cpp b/fsfw/datapool/DataSet.cpp new file mode 100644 index 0000000..e41489b --- /dev/null +++ b/fsfw/datapool/DataSet.cpp @@ -0,0 +1,150 @@ +#include "DataSet.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +DataSet::DataSet() : + fill_count(0), state(DATA_SET_UNINITIALISED) { + for (unsigned count = 0; count < DATA_SET_MAX_SIZE; count++) { + registeredVariables[count] = NULL; + } +} + +DataSet::~DataSet() { + //Don't do anything with your variables, they are dead already! (Destructor is already called) +} + +ReturnValue_t DataSet::read() { + ReturnValue_t result = RETURN_OK; + if (state == DATA_SET_UNINITIALISED) { + lockDataPool(); + for (uint16_t count = 0; count < fill_count; count++) { + if (registeredVariables[count]->getReadWriteMode() + != PoolVariableIF::VAR_WRITE + && registeredVariables[count]->getDataPoolId() + != PoolVariableIF::NO_PARAMETER) { + ReturnValue_t status = registeredVariables[count]->read(); + if (status != RETURN_OK) { + result = INVALID_PARAMETER_DEFINITION; + break; + } + } + } + state = DATA_SET_WAS_READ; + freeDataPoolLock(); + } else { + sif::error << "DataSet::read(): Call made in wrong position." << std::endl; + result = SET_WAS_ALREADY_READ; + } + return result; +} + +ReturnValue_t DataSet::commit(uint8_t valid) { + setValid(valid); + return commit(); +} + +ReturnValue_t DataSet::commit() { + if (state == DATA_SET_WAS_READ) { + lockDataPool(); + for (uint16_t count = 0; count < fill_count; count++) { + if (registeredVariables[count]->getReadWriteMode() + != PoolVariableIF::VAR_READ + && registeredVariables[count]->getDataPoolId() + != PoolVariableIF::NO_PARAMETER) { + registeredVariables[count]->commit(); + } + } + state = DATA_SET_UNINITIALISED; + freeDataPoolLock(); + return RETURN_OK; + } else { + ReturnValue_t result = RETURN_OK; + lockDataPool(); + for (uint16_t count = 0; count < fill_count; count++) { + if (registeredVariables[count]->getReadWriteMode() + == PoolVariableIF::VAR_WRITE + && registeredVariables[count]->getDataPoolId() + != PoolVariableIF::NO_PARAMETER) { + registeredVariables[count]->commit(); + } else if (registeredVariables[count]->getDataPoolId() + != PoolVariableIF::NO_PARAMETER) { + if (result != COMMITING_WITHOUT_READING) { + sif::error << + "DataSet::commit(): commit-without-read " + "call made with non write-only variable." << std::endl; + result = COMMITING_WITHOUT_READING; + } + } + } + state = DATA_SET_UNINITIALISED; + freeDataPoolLock(); + return result; + } + +} + +void DataSet::registerVariable(PoolVariableIF* variable) { + if (state == DATA_SET_UNINITIALISED) { + if (variable != NULL) { + if (fill_count < DATA_SET_MAX_SIZE) { + registeredVariables[fill_count] = variable; + fill_count++; + return; + } + } + } + sif::error + << "DataSet::registerVariable: failed. Either NULL, or set is full, or call made in wrong position." + << std::endl; + return; +} + +uint8_t DataSet::freeDataPoolLock() { + return ::dataPool.freeDataPoolLock(); +} + +uint8_t DataSet::lockDataPool() { + return ::dataPool.lockDataPool(); +} + +ReturnValue_t DataSet::serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = RETURN_FAILED; + for (uint16_t count = 0; count < fill_count; count++) { + result = registeredVariables[count]->serialize(buffer, size, maxSize, + streamEndianness); + if (result != RETURN_OK) { + return result; + } + } + return result; +} + +size_t DataSet::getSerializedSize() const { + size_t size = 0; + for (uint16_t count = 0; count < fill_count; count++) { + size += registeredVariables[count]->getSerializedSize(); + } + return size; +} + +void DataSet::setValid(uint8_t valid) { + for (uint16_t count = 0; count < fill_count; count++) { + if (registeredVariables[count]->getReadWriteMode() + != PoolVariableIF::VAR_READ) { + registeredVariables[count]->setValid(valid); + } + } +} + +ReturnValue_t DataSet::deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + ReturnValue_t result = RETURN_FAILED; + for (uint16_t count = 0; count < fill_count; count++) { + result = registeredVariables[count]->deSerialize(buffer, size, + streamEndianness); + if (result != RETURN_OK) { + return result; + } + } + return result; +} diff --git a/fsfw/datapool/DataSet.h b/fsfw/datapool/DataSet.h new file mode 100644 index 0000000..04044bf --- /dev/null +++ b/fsfw/datapool/DataSet.h @@ -0,0 +1,159 @@ +/* + * \file DataSet.h + * + * \brief This file contains the DataSet class and a small structure called DataSetContent. + * + * \date 10/17/2012 + * + * \author Bastian Baetz + * + */ + +#ifndef DATASET_H_ +#define DATASET_H_ + +#include "DataPool.h" +#include "DataSetIF.h" +#include "PoolRawAccess.h" +#include "PoolVariable.h" +#include "PoolVarList.h" +#include "PoolVector.h" +#include "../serialize/SerializeAdapter.h" +/** + * \brief The DataSet class manages a set of locally checked out variables. + * + * \details This class manages a list, where a set of local variables (or pool variables) are + * registered. They are checked-out (i.e. their values are looked up and copied) + * with the read call. After the user finishes working with the pool variables, + * he can write back all variable values to the pool with the commit call. + * The data set manages locking and freeing the data pool, to ensure that all values + * are read and written back at once. + * An internal state manages usage of this class. Variables may only be registered before + * the read call is made, and the commit call only after the read call. + * If pool variables are writable and not committed until destruction of the set, the + * DataSet class automatically sets the valid flag in the data pool to invalid (without) + * changing the variable's value. + * + * \ingroup data_pool + */ +class DataSet: public DataSetIF, public HasReturnvaluesIF, public SerializeIF { +private: + //SHOULDDO we could use a linked list of datapool variables + static const uint8_t DATA_SET_MAX_SIZE = 63; //!< This definition sets the maximum number of variables to register in one DataSet. + + /** + * \brief This array represents all pool variables registered in this set. + * \details It has a maximum size of DATA_SET_MAX_SIZE. + */ + PoolVariableIF* registeredVariables[DATA_SET_MAX_SIZE]; + /** + * \brief The fill_count attribute ensures that the variables register in the correct array + * position and that the maximum number of variables is not exceeded. + */ + uint16_t fill_count; + /** + * States of the seet. + */ + enum States { + DATA_SET_UNINITIALISED, //!< DATA_SET_UNINITIALISED + DATA_SET_WAS_READ //!< DATA_SET_WAS_READ + }; + /** + * \brief state manages the internal state of the data set, which is important e.g. for the + * behavior on destruction. + */ + States state; + /** + * \brief This is a small helper function to facilitate locking the global data pool. + * \details It makes use of the lockDataPool method offered by the DataPool class. + */ + uint8_t lockDataPool(); + /** + * \brief This is a small helper function to facilitate unlocking the global data pool. + * \details It makes use of the freeDataPoolLock method offered by the DataPool class. + */ + uint8_t freeDataPoolLock(); + +public: + static const uint8_t INTERFACE_ID = CLASS_ID::DATA_SET_CLASS; + static const ReturnValue_t INVALID_PARAMETER_DEFINITION = + MAKE_RETURN_CODE( 0x01 ); + static const ReturnValue_t SET_WAS_ALREADY_READ = MAKE_RETURN_CODE( 0x02 ); + static const ReturnValue_t COMMITING_WITHOUT_READING = + MAKE_RETURN_CODE(0x03); + + /** + * \brief The constructor simply sets the fill_count to zero and sets the state to "uninitialized". + */ + DataSet(); + /** + * \brief The destructor automatically manages writing the valid information of variables. + * \details In case the data set was read out, but not committed (indicated by state), + * the destructor parses all variables that are still registered to the set. + * For each, the valid flag in the data pool is set to "invalid". + */ + ~DataSet(); + /** + * \brief The read call initializes reading out all registered variables. + * \details It iterates through the list of registered variables and calls all read() + * functions of the registered pool variables (which read out their values from the + * data pool) which are not write-only. In case of an error (e.g. a wrong data type, + * or an invalid data pool id), the operation is aborted and + * \c INVALID_PARAMETER_DEFINITION returned. + * The data pool is locked during the whole read operation and freed afterwards. + * The state changes to "was written" after this operation. + * \return - \c RETURN_OK if all variables were read successfully. + * - \c INVALID_PARAMETER_DEFINITION if PID, size or type of the + * requested variable is invalid. + * - \c SET_WAS_ALREADY_READ if read() is called twice without calling + * commit() in between + */ + ReturnValue_t read(); + /** + * \brief The commit call initializes writing back the registered variables. + * \details It iterates through the list of registered variables and calls + * the commit() method of the remaining registered variables (which write back + * their values to the pool). + * The data pool is locked during the whole commit operation and freed afterwards. + * The state changes to "was committed" after this operation. + * If the set does contain at least one variable which is not write-only commit() + * can only be called after read(). If the set only contains variables which are + * write only, commit() can be called without a preceding read() call. + * \return - \c RETURN_OK if all variables were read successfully. + * - \c COMMITING_WITHOUT_READING if set was not read yet and contains non write-only + * variables + */ + ReturnValue_t commit(void); + /** + * Variant of method above which sets validity of all elements of the set. + * @param valid Validity information from PoolVariableIF. + * \return - \c RETURN_OK if all variables were read successfully. + * - \c COMMITING_WITHOUT_READING if set was not read yet and contains non write-only + * variables + */ + ReturnValue_t commit(uint8_t valid); + /** + * \brief This operation is used to register the local variables in the set. + * \details It copies all required information to the currently + * free space in the registeredVariables list. + */ + void registerVariable(PoolVariableIF* variable); + + /** + * Set the valid information of all variables contained in the set which are not readonly + * + * @param valid Validity information from PoolVariableIF. + */ + void setValid(uint8_t valid); + + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override; + + size_t getSerializedSize() const override; + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; + +}; + +#endif /* DATASET_H_ */ diff --git a/fsfw/datapool/DataSetIF.h b/fsfw/datapool/DataSetIF.h new file mode 100644 index 0000000..7741477 --- /dev/null +++ b/fsfw/datapool/DataSetIF.h @@ -0,0 +1,39 @@ +/** + * \file DataSetIF.h + * + * \brief This file contains the small interface to access the DataSet class. + * + * \date 10/23/2012 + * + * \author Bastian Baetz + * + */ + +#ifndef DATASETIF_H_ +#define DATASETIF_H_ + +class PoolVariableIF; + +/** + * \brief This class defines a small interface to register on a DataSet. + * + * \details Currently, the only purpose of this interface is to provide a method for locally + * checked-out variables to register on a data set. Still, it may become useful for + * other purposes as well. + * + * \ingroup data_pool + */ +class DataSetIF { +public: + /** + * \brief This is an empty virtual destructor, as it is proposed for C++ interfaces. + */ + virtual ~DataSetIF() {} + /** + * \brief This operation provides a method to register local data pool variables + * to register in a data set by passing itself to this DataSet operation. + */ + virtual void registerVariable( PoolVariableIF* variable ) = 0; +}; + +#endif /* DATASETIF_H_ */ diff --git a/fsfw/datapool/HkSwitchHelper.cpp b/fsfw/datapool/HkSwitchHelper.cpp new file mode 100644 index 0000000..04096ac --- /dev/null +++ b/fsfw/datapool/HkSwitchHelper.cpp @@ -0,0 +1,76 @@ +#include "HkSwitchHelper.h" +//#include +#include "../ipc/QueueFactory.h" + +HkSwitchHelper::HkSwitchHelper(EventReportingProxyIF* eventProxy) : + commandActionHelper(this), eventProxy(eventProxy) { + actionQueue = QueueFactory::instance()->createMessageQueue(); +} + +HkSwitchHelper::~HkSwitchHelper() { + // TODO Auto-generated destructor stub +} + +ReturnValue_t HkSwitchHelper::initialize() { + ReturnValue_t result = commandActionHelper.initialize(); + + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + return result; +} + +ReturnValue_t HkSwitchHelper::performOperation(uint8_t operationCode) { + CommandMessage message; + while (actionQueue->receiveMessage(&message) == HasReturnvaluesIF::RETURN_OK) { + ReturnValue_t result = commandActionHelper.handleReply(&message); + if (result == HasReturnvaluesIF::RETURN_OK) { + continue; + } + message.setToUnknownCommand(); + actionQueue->reply(&message); + } + + return HasReturnvaluesIF::RETURN_OK; +} + +void HkSwitchHelper::stepSuccessfulReceived(ActionId_t actionId, uint8_t step) { +} + +void HkSwitchHelper::stepFailedReceived(ActionId_t actionId, uint8_t step, + ReturnValue_t returnCode) { + eventProxy->forwardEvent(SWITCHING_TM_FAILED, returnCode, actionId); +} + +void HkSwitchHelper::dataReceived(ActionId_t actionId, const uint8_t* data, + uint32_t size) { +} + +void HkSwitchHelper::completionSuccessfulReceived(ActionId_t actionId) { +} + +void HkSwitchHelper::completionFailedReceived(ActionId_t actionId, + ReturnValue_t returnCode) { + eventProxy->forwardEvent(SWITCHING_TM_FAILED, returnCode, actionId); +} + +ReturnValue_t HkSwitchHelper::switchHK(SerializeIF* sids, bool enable) { +// ActionId_t action = HKService::DISABLE_HK; +// if (enable) { +// action = HKService::ENABLE_HK; +// } +// +// ReturnValue_t result = commandActionHelper.commandAction( +// objects::PUS_HK_SERVICE, action, sids); +// +// if (result != HasReturnvaluesIF::RETURN_OK) { +// eventProxy->forwardEvent(SWITCHING_TM_FAILED, result); +// } +// return result; + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueIF* HkSwitchHelper::getCommandQueuePtr() { + return actionQueue; +} diff --git a/fsfw/datapool/HkSwitchHelper.h b/fsfw/datapool/HkSwitchHelper.h new file mode 100644 index 0000000..385b204 --- /dev/null +++ b/fsfw/datapool/HkSwitchHelper.h @@ -0,0 +1,46 @@ +#ifndef FRAMEWORK_DATAPOOL_HKSWITCHHELPER_H_ +#define FRAMEWORK_DATAPOOL_HKSWITCHHELPER_H_ + +#include "../tasks/ExecutableObjectIF.h" +#include "../action/CommandsActionsIF.h" +#include "../events/EventReportingProxyIF.h" + +//TODO this class violations separation between mission and framework +//but it is only a transitional solution until the Datapool is +//implemented decentrally + +class HkSwitchHelper: public ExecutableObjectIF, public CommandsActionsIF { +public: + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::HK; + static const Event SWITCHING_TM_FAILED = MAKE_EVENT(1, SEVERITY::LOW); //!< Commanding the HK Service failed, p1: error code, p2 action: 0 disable / 1 enable + + HkSwitchHelper(EventReportingProxyIF *eventProxy); + virtual ~HkSwitchHelper(); + + ReturnValue_t initialize(); + + virtual ReturnValue_t performOperation(uint8_t operationCode = 0); + + ReturnValue_t switchHK(SerializeIF *sids, bool enable); + + virtual void setTaskIF(PeriodicTaskIF* task_){}; + +protected: + virtual void stepSuccessfulReceived(ActionId_t actionId, uint8_t step); + virtual void stepFailedReceived(ActionId_t actionId, uint8_t step, + ReturnValue_t returnCode); + virtual void dataReceived(ActionId_t actionId, const uint8_t* data, + uint32_t size); + virtual void completionSuccessfulReceived(ActionId_t actionId); + virtual void completionFailedReceived(ActionId_t actionId, + ReturnValue_t returnCode); + virtual MessageQueueIF* getCommandQueuePtr(); + +private: + CommandActionHelper commandActionHelper; + MessageQueueIF* actionQueue; + EventReportingProxyIF *eventProxy; +}; + +#endif /* FRAMEWORK_DATAPOOL_HKSWITCHHELPER_H_ */ diff --git a/fsfw/datapool/PIDReader.h b/fsfw/datapool/PIDReader.h new file mode 100644 index 0000000..3b38b51 --- /dev/null +++ b/fsfw/datapool/PIDReader.h @@ -0,0 +1,147 @@ +#ifndef PIDREADER_H_ +#define PIDREADER_H_ +#include "DataPool.h" +#include "DataSetIF.h" +#include "PoolEntry.h" +#include "PoolVariableIF.h" +#include "../serialize/SerializeAdapter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +template class PIDReaderList; + +template +class PIDReader: public PoolVariableIF { + template friend class PIDReaderList; +protected: + uint32_t parameterId; + uint8_t valid; + ReturnValue_t read() { + uint8_t arrayIndex = DataPool::PIDToArrayIndex(parameterId); + PoolEntry *read_out = ::dataPool.getData( + DataPool::PIDToDataPoolId(parameterId), arrayIndex); + if (read_out != NULL) { + valid = read_out->valid; + value = read_out->address[arrayIndex]; + return HasReturnvaluesIF::RETURN_OK; + } else { + value = 0; + valid = false; + sif::error << "PIDReader: read of PID 0x" << std::hex << parameterId + << std::dec << " failed." << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + } + /** + * Never commit, is read-only. + * Reason is the possibility to access a single DP vector element, but if we commit, + * we set validity of the whole vector. + */ + ReturnValue_t commit() { + return HasReturnvaluesIF::RETURN_FAILED; + } + /** + * Empty ctor for List initialization + */ + PIDReader() : + parameterId(PoolVariableIF::NO_PARAMETER), valid( + PoolVariableIF::INVALID), value(0) { + + } +public: + /** + * \brief This is the local copy of the data pool entry. + */ + T value; + /** + * \brief In the constructor, the variable can register itself in a DataSet (if not NULL is + * passed). + * \details It DOES NOT fetch the current value from the data pool, but sets the value + * attribute to default (0). The value is fetched within the read() operation. + * \param set_id This is the id in the global data pool this instance of the access class + * corresponds to. + * \param dataSet The data set in which the variable shall register itself. If NULL, + * the variable is not registered. + * \param setWritable If this flag is set to true, changes in the value attribute can be + * written back to the data pool, otherwise not. + */ + PIDReader(uint32_t setParameterId, DataSetIF *dataSet) : + parameterId(setParameterId), valid(PoolVariableIF::INVALID), value( + 0) { + if (dataSet != NULL) { + dataSet->registerVariable(this); + } + } + + /** + * Copy ctor to copy classes containing Pool Variables. + */ + PIDReader(const PIDReader &rhs) : + parameterId(rhs.parameterId), valid(rhs.valid), value(rhs.value) { + } + + /** + * \brief The classes destructor is empty. + */ + ~PIDReader() { + + } + /** + * \brief This operation returns the data pool id of the variable. + */ + uint32_t getDataPoolId() const { + return DataPool::PIDToDataPoolId(parameterId); + } + uint32_t getParameterId() const { + return parameterId; + } + /** + * This method returns if the variable is write-only, read-write or read-only. + */ + ReadWriteMode_t getReadWriteMode() const { + return VAR_READ; + } + /** + * \brief With this call, the valid information of the variable is returned. + */ + bool isValid() const { + if (valid) + return true; + else + return false; + } + + uint8_t getValid() { + return valid; + } + + void setValid(uint8_t valid) { + this->valid = valid; + } + + operator T() { + return value; + } + + PIDReader& operator=(T newValue) { + value = newValue; + return *this; + } + + virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const override { + return SerializeAdapter::serialize(&value, buffer, size, maxSize, + streamEndianness); + } + + virtual size_t getSerializedSize() const override { + return SerializeAdapter::getSerializedSize(&value); + } + + virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) override { + return SerializeAdapter::deSerialize(&value, buffer, size, + streamEndianness); + } +}; + +#endif /* PIDREADER_H_ */ diff --git a/fsfw/datapool/PIDReaderList.h b/fsfw/datapool/PIDReaderList.h new file mode 100644 index 0000000..1f6aa99 --- /dev/null +++ b/fsfw/datapool/PIDReaderList.h @@ -0,0 +1,27 @@ +#ifndef FRAMEWORK_DATAPOOL_PIDREADERLIST_H_ +#define FRAMEWORK_DATAPOOL_PIDREADERLIST_H_ + +#include "PIDReader.h" +#include "PoolVariableIF.h" +template +class PIDReaderList { +private: + PIDReader variables[n_var]; +public: + PIDReaderList( const uint32_t setPid[n_var], DataSetIF* dataSet) { + //I really should have a look at the new init list c++ syntax. + if (dataSet == NULL) { + return; + } + for (uint8_t count = 0; count < n_var; count++) { + variables[count].parameterId = setPid[count]; + dataSet->registerVariable(&variables[count]); + } + } + + PIDReader &operator [](int i) { return variables[i]; } +}; + + + +#endif /* FRAMEWORK_DATAPOOL_PIDREADERLIST_H_ */ diff --git a/fsfw/datapool/PoolEntry.cpp b/fsfw/datapool/PoolEntry.cpp new file mode 100644 index 0000000..56e489a --- /dev/null +++ b/fsfw/datapool/PoolEntry.cpp @@ -0,0 +1,87 @@ +#include "PoolEntry.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../globalfunctions/arrayprinter.h" +#include + +template +PoolEntry::PoolEntry(std::initializer_list initValue, uint8_t setLength, + bool setValid ) : length(setLength), valid(setValid) { + this->address = new T[this->length]; + if(initValue.size() == 0) { + std::memset(this->address, 0, this->getByteSize()); + } + else if (initValue.size() != setLength){ + sif::warning << "PoolEntry: setLength is not equal to initializer list" + "length! Performing zero initialization with given setLength" + << std::endl; + std::memset(this->address, 0, this->getByteSize()); + } + else { + std::copy(initValue.begin(), initValue.end(), this->address); + } +} + +template +PoolEntry::PoolEntry( T* initValue, uint8_t setLength, bool setValid ) : + length(setLength), valid(setValid) { + this->address = new T[this->length]; + if (initValue != nullptr) { + std::memcpy(this->address, initValue, this->getByteSize() ); + } else { + std::memset(this->address, 0, this->getByteSize() ); + } +} + +//As the data pool is global, this dtor is only be called on program exit. +//Warning! Never copy pool entries! +template +PoolEntry::~PoolEntry() { + delete[] this->address; +} + +template +uint16_t PoolEntry::getByteSize() { + return ( sizeof(T) * this->length ); +} + +template +uint8_t PoolEntry::getSize() { + return this->length; +} + +template +void* PoolEntry::getRawData() { + return this->address; +} + +template +void PoolEntry::setValid(bool isValid) { + this->valid = isValid; +} + +template +bool PoolEntry::getValid() { + return valid; +} + +template +void PoolEntry::print() { + sif::debug << "Pool Entry Validity: " << + (this->valid? " (valid) " : " (invalid) ") << std::endl; + arrayprinter::print(reinterpret_cast(address), length); + sif::debug << std::dec << std::endl; +} + +template +Type PoolEntry::getType() { + return PodTypeConversion::type; +} + +template class PoolEntry; +template class PoolEntry; +template class PoolEntry; +template class PoolEntry; +template class PoolEntry; +template class PoolEntry; +template class PoolEntry; +template class PoolEntry; diff --git a/fsfw/datapool/PoolEntry.h b/fsfw/datapool/PoolEntry.h new file mode 100644 index 0000000..7b47c67 --- /dev/null +++ b/fsfw/datapool/PoolEntry.h @@ -0,0 +1,130 @@ +#ifndef FRAMEWORK_DATAPOOL_POOLENTRY_H_ +#define FRAMEWORK_DATAPOOL_POOLENTRY_H_ + +#include "PoolEntryIF.h" + +#include +#include +#include + +/** + * @brief This is a small helper class that defines a single data pool entry. + * @details + * The helper is used to store all information together with the data as a + * single data pool entry. The content's type is defined by the template + * argument. + * + * It is prepared for use with plain old data types, but may be + * extended to complex types if necessary. It can be initialized with a + * certain value, size and validity flag. + * + * It holds a pointer to the real data and offers methods to access this data + * and to acquire additional information (such as validity and array/byte size). + * It is NOT intended to be used outside DataPool implementations as it performs + * dynamic memory allocation. + * + * @ingroup data_pool + */ +template +class PoolEntry : public PoolEntryIF { +public: + static_assert(not std::is_same::value, + "Do not use boolean for the PoolEntry type, use uint8_t " + "instead! The ECSS standard defines a boolean as a one bit " + "field. Therefore it is preferred to store a boolean as an " + "uint8_t"); + /** + * @brief In the classe's constructor, space is allocated on the heap and + * potential init values are copied to that space. + * @details + * Not passing any arguments will initialize an non-array pool entry + * (setLength = 1) with an initial invalid state. + * Please note that if an initializer list is passed, the correct + * corresponding length should be passed too, otherwise a zero + * initialization will be performed with the given setLength. + * @param initValue + * Initializer list with values to initialize with, for example {0,0} to + * initialize the two entries to zero. + * @param setLength + * Defines the array length of this entry. Should be equal to the + * intializer list length. + * @param setValid + * Sets the initialization flag. It is invalid by default. + */ + PoolEntry(std::initializer_list initValue = {}, uint8_t setLength = 1, + bool setValid = false); + /** + * @brief In the classe's constructor, space is allocated on the heap and + * potential init values are copied to that space. + * @param initValue + * A pointer to the single value or array that holds the init value. + * With the default value (nullptr), the entry is initalized with all 0. + * @param setLength + * Defines the array length of this entry. + * @param setValid + * Sets the initialization flag. It is invalid by default. + */ + PoolEntry(T* initValue, uint8_t setLength = 1, bool setValid = false); + + //! Explicitely deleted copy ctor, copying is not allowed! + PoolEntry(const PoolEntry&) = delete; + //! Explicitely deleted copy assignment, copying is not allowed! + PoolEntry& operator=(const PoolEntry&) = delete; + + /** + * @brief The allocated memory for the variable is freed + * in the destructor. + * @details + * As the data pool is global, this dtor is only called on program exit. + * PoolEntries shall never be copied, as a copy might delete the variable + * on the heap. + */ + ~PoolEntry(); + + /** + * @brief This is the address pointing to the allocated memory. + */ + T* address; + /** + * @brief This attribute stores the length information. + */ + uint8_t length; + /** + * @brief Here, the validity information for a variable is stored. + * Every entry (single variable or vector) has one valid flag. + */ + uint8_t valid; + /** + * @brief getSize returns the array size of the entry. + * @details A single parameter has size 1. + */ + uint8_t getSize(); + /** + * @brief This operation returns the size in bytes. + * @details The size is calculated by sizeof(type) * array_size. + */ + uint16_t getByteSize(); + /** + * @brief This operation returns a the address pointer casted to void*. + */ + void* getRawData(); + /** + * @brief This method allows to set the valid information + * of the pool entry. + */ + void setValid( bool isValid ); + /** + * @brief This method allows to get the valid information + * of the pool entry. + */ + bool getValid(); + /** + * @brief This is a debug method that prints all values and the valid + * information to the screen. It prints all array entries in a row. + */ + void print(); + + Type getType(); +}; + +#endif /* POOLENTRY_H_ */ diff --git a/fsfw/datapool/PoolEntryIF.h b/fsfw/datapool/PoolEntryIF.h new file mode 100644 index 0000000..462de18 --- /dev/null +++ b/fsfw/datapool/PoolEntryIF.h @@ -0,0 +1,63 @@ +#ifndef FRAMEWORK_DATAPOOL_POOLENTRYIF_H_ +#define FRAMEWORK_DATAPOOL_POOLENTRYIF_H_ + +#include "../globalfunctions/Type.h" +#include + +/** + * @brief This interface defines the access possibilities to a + * single data pool entry. + * @details + * The interface provides methods to determine the size and the validity + * information of a value. It also defines a method to receive a pointer to the + * raw data content. It is mainly used by DataPool itself, but also as a + * return pointer. + * + * @author Bastian Baetz + * @ingroup data_pool + * + */ +class PoolEntryIF { +public: + /** + * @brief This is an empty virtual destructor, + * as it is required for C++ interfaces. + */ + virtual ~PoolEntryIF() { + } + /** + * @brief getSize returns the array size of the entry. + * A single variable parameter has size 1. + */ + virtual uint8_t getSize() = 0; + /** + * @brief This operation returns the size in bytes, which is calculated by + * sizeof(type) * array_size. + */ + virtual uint16_t getByteSize() = 0; + /** + * @brief This operation returns a the address pointer casted to void*. + */ + virtual void* getRawData() = 0; + /** + * @brief This method allows to set the valid information of the pool entry. + */ + virtual void setValid(bool isValid) = 0; + /** + * @brief This method allows to set the valid information of the pool entry. + */ + virtual bool getValid() = 0; + /** + * @brief This is a debug method that prints all values and the valid + * information to the screen. It prints all array entries in a row. + * @details + * Also displays whether the pool entry is valid or invalid. + */ + virtual void print() = 0; + /** + * Returns the type of the entry. + */ + virtual Type getType() = 0; +}; + +#endif /* POOLENTRYIF_H_ */ diff --git a/fsfw/datapool/PoolRawAccess.cpp b/fsfw/datapool/PoolRawAccess.cpp new file mode 100644 index 0000000..f926408 --- /dev/null +++ b/fsfw/datapool/PoolRawAccess.cpp @@ -0,0 +1,187 @@ +#include "DataPool.h" +#include "PoolEntryIF.h" +#include "PoolRawAccess.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../serialize/EndianConverter.h" + +#include + +PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry, + DataSetIF *data_set, ReadWriteMode_t setReadWriteMode) : + dataPoolId(set_id), arrayEntry(setArrayEntry), valid(false), type( + Type::UNKNOWN_TYPE), typeSize(0), arraySize(0), sizeTillEnd(0), readWriteMode( + setReadWriteMode) { + memset(value, 0, sizeof(value)); + if (data_set != NULL) { + data_set->registerVariable(this); + } +} + +PoolRawAccess::~PoolRawAccess() { + +} + +ReturnValue_t PoolRawAccess::read() { + PoolEntryIF *read_out = ::dataPool.getRawData(dataPoolId); + if (read_out != NULL) { + valid = read_out->getValid(); + if (read_out->getSize() > arrayEntry) { + arraySize = read_out->getSize(); + typeSize = read_out->getByteSize() / read_out->getSize(); + type = read_out->getType(); + if (typeSize <= sizeof(value)) { + uint16_t arrayPosition = arrayEntry * typeSize; + sizeTillEnd = read_out->getByteSize() - arrayPosition; + uint8_t *ptr = + &((uint8_t*) read_out->getRawData())[arrayPosition]; + memcpy(value, ptr, typeSize); + return HasReturnvaluesIF::RETURN_OK; + } else { + //Error value type too large. + } + } else { + //Error index requested too large + } + } else { + //Error entry does not exist. + } + sif::error << "PoolRawAccess: read of DP Variable 0x" << std::hex + << dataPoolId << std::dec << " failed." << std::endl; + valid = INVALID; + typeSize = 0; + sizeTillEnd = 0; + memset(value, 0, sizeof(value)); + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t PoolRawAccess::commit() { + PoolEntryIF *write_back = ::dataPool.getRawData(dataPoolId); + if ((write_back != NULL) && (readWriteMode != VAR_READ)) { + write_back->setValid(valid); + uint8_t array_position = arrayEntry * typeSize; + uint8_t *ptr = &((uint8_t*) write_back->getRawData())[array_position]; + memcpy(ptr, value, typeSize); + return HasReturnvaluesIF::RETURN_OK; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +uint8_t* PoolRawAccess::getEntry() { + return value; +} + +ReturnValue_t PoolRawAccess::getEntryEndianSafe(uint8_t *buffer, + size_t *writtenBytes, size_t maxSize) { + uint8_t *data_ptr = getEntry(); +// debug << "PoolRawAccess::getEntry: Array position: " << index * size_of_type << " Size of T: " << (int)size_of_type << " ByteSize: " << byte_size << " Position: " << *size << std::endl; + if (typeSize == 0) { + return DATA_POOL_ACCESS_FAILED; + } + if (typeSize > maxSize) { + return INCORRECT_SIZE; + } + EndianConverter::convertBigEndian(buffer, data_ptr, typeSize); + *writtenBytes = typeSize; + return HasReturnvaluesIF::RETURN_OK; +} + +Type PoolRawAccess::getType() { + return type; +} + +size_t PoolRawAccess::getSizeOfType() { + return typeSize; +} + +size_t PoolRawAccess::getArraySize() { + return arraySize; +} + +uint32_t PoolRawAccess::getDataPoolId() const { + return dataPoolId; +} + +PoolVariableIF::ReadWriteMode_t PoolRawAccess::getReadWriteMode() const { + return readWriteMode; +} + +ReturnValue_t PoolRawAccess::setEntryFromBigEndian(const uint8_t *buffer, + size_t setSize) { + if (typeSize == setSize) { + EndianConverter::convertBigEndian(value, buffer, typeSize); + return HasReturnvaluesIF::RETURN_OK; + } else { + sif::error + << "PoolRawAccess::setEntryFromBigEndian: Illegal sizes: Internal" + << (uint32_t) typeSize << ", Requested: " << setSize + << std::endl; + return INCORRECT_SIZE; + } +} + +bool PoolRawAccess::isValid() const { + if (valid != INVALID) + return true; + else + return false; +} + +void PoolRawAccess::setValid(uint8_t valid) { + this->valid = valid; +} + +size_t PoolRawAccess::getSizeTillEnd() const { + return sizeTillEnd; +} + +ReturnValue_t PoolRawAccess::serialize(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const { + if (typeSize + *size <= maxSize) { + switch (streamEndianness) { + case (Endianness::BIG): + EndianConverter::convertBigEndian(*buffer, value, typeSize); + break; + case (Endianness::LITTLE): + EndianConverter::convertLittleEndian(*buffer, value, typeSize); + break; + default: + case (Endianness::MACHINE): + memcpy(*buffer, value, typeSize); + break; + } + *size += typeSize; + (*buffer) += typeSize; + return HasReturnvaluesIF::RETURN_OK; + } else { + return SerializeIF::BUFFER_TOO_SHORT; + } +} + +size_t PoolRawAccess::getSerializedSize() const { + return typeSize; +} + +ReturnValue_t PoolRawAccess::deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) { + + if (*size >= typeSize) { + switch (streamEndianness) { + case (Endianness::BIG): + EndianConverter::convertBigEndian(value, *buffer, typeSize); + break; + case (Endianness::LITTLE): + EndianConverter::convertLittleEndian(value, *buffer, typeSize); + break; + default: + case (Endianness::MACHINE): + memcpy(value, *buffer, typeSize); + break; + } + *size -= typeSize; + *buffer += typeSize; + return HasReturnvaluesIF::RETURN_OK; + } else { + return SerializeIF::STREAM_TOO_SHORT; + } +} diff --git a/fsfw/datapool/PoolRawAccess.h b/fsfw/datapool/PoolRawAccess.h new file mode 100644 index 0000000..0c7a06b --- /dev/null +++ b/fsfw/datapool/PoolRawAccess.h @@ -0,0 +1,152 @@ +#ifndef POOLRAWACCESS_H_ +#define POOLRAWACCESS_H_ + +#include "DataSetIF.h" +#include "PoolVariableIF.h" + +/** + * This class allows accessing Data Pool variables as raw bytes. + * This is necessary to have an access method for HK data, as the PID's alone do not + * provide a type information. + * \ingroup data_pool + */ +class PoolRawAccess: public PoolVariableIF { +private: + /** + * \brief To access the correct data pool entry on read and commit calls, the data pool id + * is stored. + */ + uint32_t dataPoolId; + /** + * \brief The array entry that is fetched from the data pool. + */ + uint8_t arrayEntry; + /** + * \brief The valid information as it was stored in the data pool is copied to this attribute. + */ + uint8_t valid; + /** + * \brief This value contains the type of the data pool entry. + */ + Type type; + /** + * \brief This value contains the size of the data pool entry in bytes. + */ + size_t typeSize; + /** + * The size of the DP array (single values return 1) + */ + size_t arraySize; + /** + * The size (in bytes) from the selected entry till the end of this DataPool variable. + */ + size_t sizeTillEnd; + /** + * \brief The information whether the class is read-write or read-only is stored here. + */ + ReadWriteMode_t readWriteMode; + static const uint8_t RAW_MAX_SIZE = sizeof(double); +protected: + /** + * \brief This is a call to read the value from the global data pool. + * \details When executed, this operation tries to fetch the pool entry with matching + * data pool id from the global data pool and copies the value and the valid + * information to its local attributes. In case of a failure (wrong type or + * pool id not found), the variable is set to zero and invalid. + * The operation does NOT provide any mutual exclusive protection by itself. + */ + ReturnValue_t read(); + /** + * \brief The commit call writes back the variable's value to the data pool. + * \details It checks type and size, as well as if the variable is writable. If so, + * the value is copied and the valid flag is automatically set to "valid". + * The operation does NOT provide any mutual exclusive protection by itself. + * + */ + ReturnValue_t commit(); +public: + static const uint8_t INTERFACE_ID = CLASS_ID::POOL_RAW_ACCESS_CLASS; + static const ReturnValue_t INCORRECT_SIZE = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t DATA_POOL_ACCESS_FAILED = MAKE_RETURN_CODE(0x02); + uint8_t value[RAW_MAX_SIZE]; + PoolRawAccess(uint32_t data_pool_id, uint8_t arrayEntry, + DataSetIF *data_set, ReadWriteMode_t setReadWriteMode = + PoolVariableIF::VAR_READ); + /** + * \brief The classes destructor is empty. If commit() was not called, the local value is + * discarded and not written back to the data pool. + */ + ~PoolRawAccess(); + /** + * \brief This operation returns a pointer to the entry fetched. + * \details This means, it does not return a pointer to byte "index", but to the start byte of + * array entry "index". Example: If the original data pool array consists of an double + * array of size four, getEntry(1) returns &(this->value[8]). + */ + uint8_t* getEntry(); + /** + * \brief This operation returns the fetched entry from the data pool and + * flips the bytes, if necessary. + * \details It makes use of the getEntry call of this function, but additionally flips the + * bytes to big endian, which is the default for external communication (as House- + * keeping telemetry). To achieve this, the data is copied directly to the passed + * buffer, if it fits in the given maxSize. + * \param buffer A pointer to a buffer to write to + * \param writtenBytes The number of bytes written is returned with this value. + * \param maxSize The maximum size that the function may write to buffer. + * \return - \c RETURN_OK if entry could be acquired + * - \c RETURN_FAILED else. + */ + ReturnValue_t getEntryEndianSafe(uint8_t *buffer, size_t *size, + size_t maxSize); + /** + * With this method, the content can be set from a big endian buffer safely. + * @param buffer Pointer to the data to set + * @param size Size of the data to write. Must fit this->size. + * @return - \c RETURN_OK on success + * - \c RETURN_FAILED on failure + */ + ReturnValue_t setEntryFromBigEndian(const uint8_t *buffer, + size_t setSize); + /** + * \brief This operation returns the type of the entry currently stored. + */ + Type getType(); + /** + * \brief This operation returns the size of the entry currently stored. + */ + size_t getSizeOfType(); + /** + * + * @return the size of the datapool array + */ + size_t getArraySize(); + /** + * \brief This operation returns the data pool id of the variable. + */ + uint32_t getDataPoolId() const; + /** + * This method returns if the variable is read-write or read-only. + */ + ReadWriteMode_t getReadWriteMode() const; + /** + * \brief With this call, the valid information of the variable is returned. + */ + bool isValid() const; + + void setValid(uint8_t valid); + /** + * Getter for the remaining size. + */ + size_t getSizeTillEnd() const; + + ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, + Endianness streamEndianness) const override; + + size_t getSerializedSize() const override; + + ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) override; +}; + +#endif /* POOLRAWACCESS_H_ */ diff --git a/fsfw/datapool/PoolVarList.h b/fsfw/datapool/PoolVarList.h new file mode 100644 index 0000000..035ff2a --- /dev/null +++ b/fsfw/datapool/PoolVarList.h @@ -0,0 +1,28 @@ +#ifndef POOLVARLIST_H_ +#define POOLVARLIST_H_ + +#include "PoolVariable.h" +#include "PoolVariableIF.h" +template +class PoolVarList { +private: + PoolVariable variables[n_var]; +public: + PoolVarList( const uint32_t set_id[n_var], DataSetIF* dataSet, PoolVariableIF::ReadWriteMode_t setReadWriteMode ) { + //I really should have a look at the new init list c++ syntax. + if (dataSet == NULL) { + return; + } + for (uint8_t count = 0; count < n_var; count++) { + variables[count].dataPoolId = set_id[count]; + variables[count].readWriteMode = setReadWriteMode; + dataSet->registerVariable(&variables[count]); + } + } + + PoolVariable &operator [](int i) { return variables[i]; } +}; + + + +#endif /* POOLVARLIST_H_ */ diff --git a/fsfw/datapool/PoolVariable.h b/fsfw/datapool/PoolVariable.h new file mode 100644 index 0000000..a9d3407 --- /dev/null +++ b/fsfw/datapool/PoolVariable.h @@ -0,0 +1,295 @@ +/* + * \file PoolVariable.h + * + * \brief This file contains the PoolVariable class, which locally represents a non-array data pool variable. + * + * \date 10/17/2012 + * + * \author Bastian Baetz + */ + +#ifndef POOLVARIABLE_H_ +#define POOLVARIABLE_H_ + +#include "DataSetIF.h" +#include "PoolEntry.h" +#include "PoolVariableIF.h" +#include "../serialize/SerializeAdapter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +template class PoolVarList; + +/** + * \brief This is the access class for non-array data pool entries. + * + * \details To ensure safe usage of the data pool, operation is not done directly on the data pool + * entries, but on local copies. This class provides simple type-safe access to single + * data pool entries (i.e. entries with length = 1). + * The class can be instantiated as read-write and read only. + * It provides a commit-and-roll-back semantic, which means that the variable's value in + * the data pool is not changed until the commit call is executed. + * \tparam T The template parameter sets the type of the variable. Currently, all plain data types + * are supported, but in principle any type is possible. + * \ingroup data_pool + */ +template +class PoolVariable: public PoolVariableIF { + template friend class PoolVarList; +protected: + /** + * \brief To access the correct data pool entry on read and commit calls, the data pool id + * is stored. + */ + uint32_t dataPoolId; + /** + * \brief The valid information as it was stored in the data pool is copied to this attribute. + */ + uint8_t valid; + /** + * \brief The information whether the class is read-write or read-only is stored here. + */ + ReadWriteMode_t readWriteMode; + /** + * \brief This is a call to read the value from the global data pool. + * \details When executed, this operation tries to fetch the pool entry with matching + * data pool id from the global data pool and copies the value and the valid + * information to its local attributes. In case of a failure (wrong type or + * pool id not found), the variable is set to zero and invalid. + * The operation does NOT provide any mutual exclusive protection by itself. + */ + ReturnValue_t read() { + PoolEntry *read_out = ::dataPool.getData < T > (dataPoolId, 1); + if (read_out != NULL) { + valid = read_out->valid; + value = *(read_out->address); + return HasReturnvaluesIF::RETURN_OK; + } else { + value = 0; + valid = false; + sif::error << "PoolVariable: read of DP Variable 0x" << std::hex + << dataPoolId << std::dec << " failed." << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + } + /** + * \brief The commit call writes back the variable's value to the data pool. + * \details It checks type and size, as well as if the variable is writable. If so, + * the value is copied and the valid flag is automatically set to "valid". + * The operation does NOT provide any mutual exclusive protection by itself. + * + */ + ReturnValue_t commit() { + PoolEntry *write_back = ::dataPool.getData < T > (dataPoolId, 1); + if ((write_back != NULL) && (readWriteMode != VAR_READ)) { + write_back->valid = valid; + *(write_back->address) = value; + return HasReturnvaluesIF::RETURN_OK; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + /** + * Empty ctor for List initialization + */ + PoolVariable() : + dataPoolId(PoolVariableIF::NO_PARAMETER), valid( + PoolVariableIF::INVALID), readWriteMode(VAR_READ), value(0) { + + } +public: + /** + * \brief This is the local copy of the data pool entry. + * \details The user can work on this attribute + * just like he would on a simple local variable. + */ + T value; + /** + * \brief In the constructor, the variable can register itself in a DataSet (if not NULL is + * passed). + * \details It DOES NOT fetch the current value from the data pool, but sets the value + * attribute to default (0). The value is fetched within the read() operation. + * \param set_id This is the id in the global data pool this instance of the access class + * corresponds to. + * \param dataSet The data set in which the variable shall register itself. If NULL, + * the variable is not registered. + * \param setWritable If this flag is set to true, changes in the value attribute can be + * written back to the data pool, otherwise not. + */ + PoolVariable(uint32_t set_id, DataSetIF *dataSet, + ReadWriteMode_t setReadWriteMode) : + dataPoolId(set_id), valid(PoolVariableIF::INVALID), readWriteMode( + setReadWriteMode), value(0) { + if (dataSet != NULL) { + dataSet->registerVariable(this); + } + } + /** + * Copy ctor to copy classes containing Pool Variables. + */ + PoolVariable(const PoolVariable &rhs) : + dataPoolId(rhs.dataPoolId), valid(rhs.valid), readWriteMode( + rhs.readWriteMode), value(rhs.value) { + } + + /** + * \brief The classes destructor is empty. + * \details If commit() was not called, the local value is + * discarded and not written back to the data pool. + */ + ~PoolVariable() { + + } + /** + * \brief This operation returns the data pool id of the variable. + */ + uint32_t getDataPoolId() const { + return dataPoolId; + } + /** + * This operation sets the data pool id of the variable. + * The method is necessary to set id's of data pool member variables with bad initialization. + */ + void setDataPoolId(uint32_t poolId) { + dataPoolId = poolId; + } + /** + * This method returns if the variable is write-only, read-write or read-only. + */ + ReadWriteMode_t getReadWriteMode() const { + return readWriteMode; + } + /** + * \brief With this call, the valid information of the variable is returned. + */ + bool isValid() const { + if (valid) + return true; + else + return false; + } + + uint8_t getValid() { + return valid; + } + + void setValid(uint8_t valid) { + this->valid = valid; + } + + operator T() { + return value; + } + + operator T() const { + return value; + } + + PoolVariable& operator=(T newValue) { + value = newValue; + return *this; + } + + PoolVariable& operator=(PoolVariable newPoolVariable) { + value = newPoolVariable.value; + return *this; + } + + virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const override { + return SerializeAdapter::serialize(&value, buffer, size, maxSize, + streamEndianness); + } + + virtual size_t getSerializedSize() const override { + return SerializeAdapter::getSerializedSize(&value); + } + + virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) override { + return SerializeAdapter::deSerialize(&value, buffer, size, streamEndianness); + } +}; + +typedef PoolVariable db_uint8_t; +typedef PoolVariable db_uint16_t; +typedef PoolVariable db_uint32_t; +typedef PoolVariable db_int8_t; +typedef PoolVariable db_int16_t; +typedef PoolVariable db_int32_t; +typedef PoolVariable db_bool_t; +typedef PoolVariable db_float_t; +typedef PoolVariable db_double_t; +//Alternative (but I thing this is not as useful: code duplication, differences too small): + +//template +//class PoolReader : public PoolVariableIF { +//private: +// uint32_t parameter_id; +// uint8_t valid; +//public: +// T value; +// PoolReader( uint32_t set_id, DataSetIF* set ) : parameter_id(set_id), valid(false), value(0) { +// set->registerVariable( this ); +// } +// +// ~PoolReader() {}; +// +// uint8_t commit() { +// return HasReturnvaluesIF::RETURN_OK; +// } +// +// uint8_t read() { +// PoolEntry* read_out = ::dataPool.getData( parameter_id, 1 ); +// if ( read_out != NULL ) { +// valid = read_out->valid; +// value = *(read_out->address); +// return HasReturnvaluesIF::RETURN_OK; +// } else { +// value = 0; +// valid = false; +// return CHECKOUT_FAILED; +// } +// } +// uint32_t getParameterId() { return parameter_id; } +// bool isWritable() { return false; }; +// bool isValid() { if (valid) return true; else return false; } +//}; +// +//template +//class PoolWriter : public PoolVariableIF { +//private: +// uint32_t parameter_id; +//public: +// T value; +// PoolWriter( uint32_t set_id, DataSetIF* set ) : parameter_id(set_id), value(0) { +// set->registerVariable( this ); +// } +// +// ~PoolWriter() {}; +// +// uint8_t commit() { +// PoolEntry* write_back = ::dataPool.getData( parameter_id, 1 ); +// if ( write_back != NULL ) { +// write_back->valid = true; +// *(write_back->address) = value; +// return HasReturnvaluesIF::RETURN_OK; +// } else { +// return CHECKOUT_FAILED; +// } +// } +// uint8_t read() { +// PoolEntry* read_out = ::dataPool.getData( parameter_id, 1 ); +// if ( read_out != NULL ) { +// value = *(read_out->address); +// return HasReturnvaluesIF::RETURN_OK; +// } else { +// value = 0; +// return CHECKOUT_FAILED; +// } +// } +// uint32_t getParameterId() { return parameter_id; } +// bool isWritable() { return true; }; +// bool isValid() { return false; } +//}; + +#endif /* POOLVARIABLE_H_ */ diff --git a/fsfw/datapool/PoolVariableIF.h b/fsfw/datapool/PoolVariableIF.h new file mode 100644 index 0000000..f47173d --- /dev/null +++ b/fsfw/datapool/PoolVariableIF.h @@ -0,0 +1,71 @@ +/* + * \file PoolVariableIF.h + * + * \brief This file contains the interface definition for pool variables. + * + * \date 10/17/2012 + * + * \author Bastian Baetz + */ + +#ifndef POOLVARIABLEIF_H_ +#define POOLVARIABLEIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerializeIF.h" + +/** + * \brief This interface is used to control local data pool variable representations. + * + * \details To securely handle data pool variables, all pool entries are locally managed by + * data pool variable access classes, which are called pool variables. To ensure a + * common state of a set of variables needed in a function, these local pool variables + * again are managed by other classes, e.g. the DataSet. This interface provides unified + * access to local pool variables for such manager classes. + * \ingroup data_pool + */ +class PoolVariableIF : public SerializeIF { + friend class DataSet; +protected: + /** + * \brief The commit call shall write back a newly calculated local value to the data pool. + */ + virtual ReturnValue_t commit() = 0; + /** + * \brief The read call shall read the value of this parameter from the data pool and store + * the content locally. + */ + virtual ReturnValue_t read() = 0; +public: + static const uint8_t VALID = 1; + static const uint8_t INVALID = 0; + static const uint32_t NO_PARAMETER = 0; + enum ReadWriteMode_t { + VAR_READ, VAR_WRITE, VAR_READ_WRITE + }; + + /** + * \brief This is an empty virtual destructor, as it is proposed for C++ interfaces. + */ + virtual ~PoolVariableIF() { + } + /** + * \brief This method returns if the variable is write-only, read-write or read-only. + */ + virtual ReadWriteMode_t getReadWriteMode() const = 0; + /** + * \brief This operation shall return the data pool id of the variable. + */ + virtual uint32_t getDataPoolId() const = 0; + /** + * \brief With this call, the valid information of the variable is returned. + */ + virtual bool isValid() const = 0; + /** + * \brief With this call, the valid information of the variable is set. + */ + virtual void setValid(uint8_t validity) = 0; + +}; + +#endif /* POOLVARIABLEIF_H_ */ diff --git a/fsfw/datapool/PoolVector.h b/fsfw/datapool/PoolVector.h new file mode 100644 index 0000000..d4ca084 --- /dev/null +++ b/fsfw/datapool/PoolVector.h @@ -0,0 +1,233 @@ +/* + * \file PoolVector.h + * + * \brief This file contains the PoolVector class, the header only class to handle data pool vectors. + * + * \date 10/23/2012 + * + * \author Bastian Baetz + */ + +#ifndef POOLVECTOR_H_ +#define POOLVECTOR_H_ + +#include "DataSetIF.h" +#include "PoolEntry.h" +#include "PoolVariableIF.h" +#include "../serialize/SerializeAdapter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +/** + * \brief This is the access class for array-type data pool entries. + * + * \details To ensure safe usage of the data pool, operation is not done directly on the data pool + * entries, but on local copies. This class provides simple type- and length-safe access + * to vector-style data pool entries (i.e. entries with length > 1). + * The class can be instantiated as read-write and read only. + * It provides a commit-and-roll-back semantic, which means that no array entry in + * the data pool is changed until the commit call is executed. + * There are two template parameters: + * \tparam T This template parameter specifies the data type of an array entry. Currently, all + * plain data types are supported, but in principle any type is possible. + * \tparam vector_size This template parameter specifies the vector size of this entry. + * Using a template parameter for this is not perfect, but avoids dynamic memory allocation. + * \ingroup data_pool + */ +template +class PoolVector: public PoolVariableIF { +private: + /** + * \brief To access the correct data pool entry on read and commit calls, the data pool id + * is stored. + */ + uint32_t dataPoolId; + /** + * \brief The valid information as it was stored in the data pool is copied to this attribute. + */ + uint8_t valid; + /** + * \brief The information whether the class is read-write or read-only is stored here. + */ + ReadWriteMode_t readWriteMode; + +protected: + /** + * \brief This is a call to read the array's values from the global data pool. + * \details When executed, this operation tries to fetch the pool entry with matching + * data pool id from the global data pool and copies all array values and the valid + * information to its local attributes. In case of a failure (wrong type, size or + * pool id not found), the variable is set to zero and invalid. + * The operation does NOT provide any mutual exclusive protection by itself. + */ + ReturnValue_t read() { + PoolEntry* read_out = ::dataPool.getData(this->dataPoolId, + vector_size); + if (read_out != NULL) { + this->valid = read_out->valid; + memcpy(this->value, read_out->address, read_out->getByteSize()); + + return HasReturnvaluesIF::RETURN_OK; + + } else { + memset(this->value, 0, vector_size * sizeof(T)); + sif::error << "PoolVector: read of DP Variable 0x" << std::hex + << dataPoolId << std::dec << " failed." << std::endl; + this->valid = INVALID; + return HasReturnvaluesIF::RETURN_FAILED; + } + } + /** + * \brief The commit call copies the array values back to the data pool. + * \details It checks type and size, as well as if the variable is writable. If so, + * the value is copied and the valid flag is automatically set to "valid". + * The operation does NOT provide any mutual exclusive protection by itself. + * + */ + ReturnValue_t commit() { + PoolEntry* write_back = ::dataPool.getData(this->dataPoolId, + vector_size); + if ((write_back != NULL) && (this->readWriteMode != VAR_READ)) { + write_back->valid = valid; + memcpy(write_back->address, this->value, write_back->getByteSize()); + return HasReturnvaluesIF::RETURN_OK; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } + } +public: + /** + * \brief This is the local copy of the data pool entry. + * \detials The user can work on this attribute + * just like he would on a local array of this type. + */ + T value[vector_size]; + /** + * \brief In the constructor, the variable can register itself in a DataSet (if not NULL is + * passed). + * \details It DOES NOT fetch the current value from the data pool, but sets the value + * attribute to default (0). The value is fetched within the read() operation. + * \param set_id This is the id in the global data pool this instance of the access class + * corresponds to. + * \param dataSet The data set in which the variable shall register itself. If NULL, + * the variable is not registered. + * \param setWritable If this flag is set to true, changes in the value attribute can be + * written back to the data pool, otherwise not. + */ + PoolVector(uint32_t set_id, DataSetIF* set, + ReadWriteMode_t setReadWriteMode) : + dataPoolId(set_id), valid(false), readWriteMode(setReadWriteMode) { + memset(this->value, 0, vector_size * sizeof(T)); + if (set != NULL) { + set->registerVariable(this); + } + } + /** + * Copy ctor to copy classes containing Pool Variables. + */ +// PoolVector(const PoolVector& rhs) { +// PoolVector temp(rhs.dataPoolId, rhs.) +// memcpy(value, rhs.value, sizeof(T)*vector_size); +// } + /** + * \brief The classes destructor is empty. + * \details If commit() was not called, the local value is + * discarded and not written back to the data pool. + */ + ~PoolVector() { + } + ; + /** + * \brief The operation returns the number of array entries in this variable. + */ + uint8_t getSize() { + return vector_size; + } + /** + * \brief This operation returns the data pool id of the variable. + */ + uint32_t getDataPoolId() const { + return dataPoolId; + } + /** + * This operation sets the data pool id of the variable. + * The method is necessary to set id's of data pool member variables with bad initialization. + */ + void setDataPoolId(uint32_t poolId) { + dataPoolId = poolId; + } + /** + * This method returns if the variable is write-only, read-write or read-only. + */ + ReadWriteMode_t getReadWriteMode() const { + return readWriteMode; + } + ; + /** + * \brief With this call, the valid information of the variable is returned. + */ + bool isValid() const { + if (valid != INVALID) + return true; + else + return false; + } + + void setValid(uint8_t valid) { + this->valid = valid; + } + + uint8_t getValid() { + return valid; + } + + T &operator [](int i) { + return value[i]; + } + + const T &operator [](int i) const { + return value[i]; + } + + PoolVector &operator=( + PoolVector newPoolVector) { + + for (uint16_t i = 0; i < vector_size; i++) { + this->value[i] = newPoolVector.value[i]; + } + return *this; + } + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + uint16_t i; + ReturnValue_t result; + for (i = 0; i < vector_size; i++) { + result = SerializeAdapter::serialize(&(value[i]), buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + return result; + } + + virtual size_t getSerializedSize() const { + return vector_size * SerializeAdapter::getSerializedSize(value); + } + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + uint16_t i; + ReturnValue_t result; + for (i = 0; i < vector_size; i++) { + result = SerializeAdapter::deSerialize(&(value[i]), buffer, size, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + return result; + } +}; + +#endif /* POOLVECTOR_H_ */ diff --git a/fsfw/defaultcfg/README.md b/fsfw/defaultcfg/README.md new file mode 100644 index 0000000..8446cda --- /dev/null +++ b/fsfw/defaultcfg/README.md @@ -0,0 +1,6 @@ +# How to setup configuration folder for FSFW + +It is recommended to copy the content of the defaultcfg folder +into a config folder which is in the same directory as the Flight +Software Framework submodule. After that, the config.mk folder should be +included by the primary Makefile with CURRENTPATH set correctly. diff --git a/fsfw/defaultcfg/fsfwconfig/FSFWConfig.h b/fsfw/defaultcfg/fsfwconfig/FSFWConfig.h new file mode 100644 index 0000000..be3717b --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/FSFWConfig.h @@ -0,0 +1,55 @@ +#ifndef CONFIG_FSFWCONFIG_H_ +#define CONFIG_FSFWCONFIG_H_ + +#include +#include +#include + +//! Used to determine whether C++ ostreams are used +//! Those can lead to code bloat. +#define FSFW_CPP_OSTREAM_ENABLED 1 + +//! Reduced printout to further decrese code size +//! Be careful, this also turns off most diagnostic prinouts! +#define FSFW_REDUCED_PRINTOUT 0 + +//! Can be used to enable debugging printouts for developing the FSFW +#define FSFW_DEBUGGING 0 + +//! Defines the FIFO depth of each commanding service base which +//! also determines how many commands a CSB service can handle in one cycle +//! simulataneously. This will increase the required RAM for +//! each CSB service ! +#define FSFW_CSB_FIFO_DEPTH 6 + +//! If FSFW_OBJ_EVENT_TRANSLATION is set to one, +//! additional output which requires the translation files translateObjects +//! and translateEvents (and their compiled source files) +#define FSFW_OBJ_EVENT_TRANSLATION 0 + +#if FSFW_OBJ_EVENT_TRANSLATION == 1 +#define FSFW_DEBUG_OUTPUT 1 +//! Specify whether info events are printed too. +#define FSFW_DEBUG_INFO 1 +#include +#include +#else +#define FSFW_DEBUG_OUTPUT 0 +#endif + +//! When using the newlib nano library, C99 support for stdio facilities +//! will not be provided. This define should be set to 1 if this is the case. +#define FSFW_NO_C99_IO 1 + +namespace fsfwconfig { +//! Default timestamp size. The default timestamp will be an eight byte CDC +//! short timestamp. +static constexpr uint8_t FSFW_MISSION_TIMESTAMP_SIZE = 8; + +//! Configure the allocated pool sizes for the event manager. +static constexpr size_t FSFW_EVENTMGMR_MATCHTREE_NODES = 240; +static constexpr size_t FSFW_EVENTMGMT_EVENTIDMATCHERS = 120; +static constexpr size_t FSFW_EVENTMGMR_RANGEMATCHERS = 120; +} + +#endif /* CONFIG_FSFWCONFIG_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/OBSWConfig.h b/fsfw/defaultcfg/fsfwconfig/OBSWConfig.h new file mode 100644 index 0000000..a9f5763 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/OBSWConfig.h @@ -0,0 +1,16 @@ +#ifndef CONFIG_OBSWCONFIG_H_ +#define CONFIG_OBSWCONFIG_H_ + +#include "OBSWVersion.h" + +#ifdef __cplusplus +namespace config { +#endif + +/* Add mission configuration flags here */ + +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_OBSWCONFIG_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/OBSWVersion.h b/fsfw/defaultcfg/fsfwconfig/OBSWVersion.h new file mode 100644 index 0000000..3c60317 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/OBSWVersion.h @@ -0,0 +1,9 @@ +#ifndef CONFIG_VERSION_H_ +#define CONFIG_VERSION_H_ + +/* OBSW versioning can be specified in this file */ + +#define OBSW_VERSION 0 +#define OBSW_SUBVERSION 0 + +#endif /* CONFIG_VERSION_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/devices/logicalAddresses.cpp b/fsfw/defaultcfg/fsfwconfig/devices/logicalAddresses.cpp new file mode 100644 index 0000000..c7ce314 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/devices/logicalAddresses.cpp @@ -0,0 +1,5 @@ +#include "logicalAddresses.h" + + + + diff --git a/fsfw/defaultcfg/fsfwconfig/devices/logicalAddresses.h b/fsfw/defaultcfg/fsfwconfig/devices/logicalAddresses.h new file mode 100644 index 0000000..e0827ba --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/devices/logicalAddresses.h @@ -0,0 +1,18 @@ +#ifndef CONFIG_DEVICES_LOGICALADDRESSES_H_ +#define CONFIG_DEVICES_LOGICALADDRESSES_H_ + +#include +#include "../objects/systemObjectList.h" +#include + +/** + * Can be used for addresses for physical devices like I2C adresses. + */ +namespace addresses { + /* Logical addresses have uint32_t datatype */ + enum logicalAddresses: address_t { + }; +} + + +#endif /* CONFIG_DEVICES_LOGICALADDRESSES_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/devices/powerSwitcherList.cpp b/fsfw/defaultcfg/fsfwconfig/devices/powerSwitcherList.cpp new file mode 100644 index 0000000..343f78d --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/devices/powerSwitcherList.cpp @@ -0,0 +1,4 @@ +#include "powerSwitcherList.h" + + + diff --git a/fsfw/defaultcfg/fsfwconfig/devices/powerSwitcherList.h b/fsfw/defaultcfg/fsfwconfig/devices/powerSwitcherList.h new file mode 100644 index 0000000..86ddea5 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/devices/powerSwitcherList.h @@ -0,0 +1,12 @@ +#ifndef CONFIG_DEVICES_POWERSWITCHERLIST_H_ +#define CONFIG_DEVICES_POWERSWITCHERLIST_H_ + +namespace switches { + /* Switches are uint8_t datatype and go from 0 to 255 */ + enum switcherList { + }; + +} + + +#endif /* CONFIG_DEVICES_POWERSWITCHERLIST_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/events/subsystemIdRanges.h b/fsfw/defaultcfg/fsfwconfig/events/subsystemIdRanges.h new file mode 100644 index 0000000..24eee81 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/events/subsystemIdRanges.h @@ -0,0 +1,18 @@ +#ifndef CONFIG_EVENTS_SUBSYSTEMIDRANGES_H_ +#define CONFIG_EVENTS_SUBSYSTEMIDRANGES_H_ + +#include +#include + +/** + * @brief Custom subsystem IDs can be added here + * @details + * Subsystem IDs are used to create unique events. + */ +namespace SUBSYSTEM_ID { +enum: uint8_t { + SUBSYSTEM_ID_START = FW_SUBSYSTEM_ID_RANGE, +}; +} + +#endif /* CONFIG_EVENTS_SUBSYSTEMIDRANGES_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/fsfwconfig.mk b/fsfw/defaultcfg/fsfwconfig/fsfwconfig.mk new file mode 100644 index 0000000..51543eb --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/fsfwconfig.mk @@ -0,0 +1,15 @@ +CXXSRC += $(wildcard $(CURRENTPATH)/ipc/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/objects/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/pollingsequence/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/events/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/tmtc/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/devices/*.cpp) + +INCLUDES += $(CURRENTPATH) +INCLUDES += $(CURRENTPATH)/objects +INCLUDES += $(CURRENTPATH)/returnvalues +INCLUDES += $(CURRENTPATH)/tmtc +INCLUDES += $(CURRENTPATH)/events +INCLUDES += $(CURRENTPATH)/devices +INCLUDES += $(CURRENTPATH)/pollingsequence +INCLUDES += $(CURRENTPATH)/ipc diff --git a/fsfw/defaultcfg/fsfwconfig/ipc/missionMessageTypes.cpp b/fsfw/defaultcfg/fsfwconfig/ipc/missionMessageTypes.cpp new file mode 100644 index 0000000..b41b87b --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/ipc/missionMessageTypes.cpp @@ -0,0 +1,12 @@ +#include "missionMessageTypes.h" + +#include + +void messagetypes::clearMissionMessage(CommandMessage* message) { + switch(message->getMessageType()) { + default: + break; + } +} + + diff --git a/fsfw/defaultcfg/fsfwconfig/ipc/missionMessageTypes.h b/fsfw/defaultcfg/fsfwconfig/ipc/missionMessageTypes.h new file mode 100644 index 0000000..8b2e2fc --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/ipc/missionMessageTypes.h @@ -0,0 +1,21 @@ +#ifndef CONFIG_IPC_MISSIONMESSAGETYPES_H_ +#define CONFIG_IPC_MISSIONMESSAGETYPES_H_ + +#include +#include + +/** + * Custom command messages are specified here. + * Most messages needed to use FSFW are already located in + * + * @param message Generic Command Message + */ +namespace messagetypes { +enum CustomMessageTypes { + MISSION_MESSAGE_TYPE_START = FW_MESSAGES_COUNT +}; + +void clearMissionMessage(CommandMessage* message); +} + +#endif /* CONFIG_IPC_MISSIONMESSAGETYPES_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/objects/Factory.cpp b/fsfw/defaultcfg/fsfwconfig/objects/Factory.cpp new file mode 100644 index 0000000..41333b1 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/objects/Factory.cpp @@ -0,0 +1,54 @@ +#include "Factory.h" +#include "../tmtc/apid.h" +#include "../tmtc/pusIds.h" +#include "../objects/systemObjectList.h" +#include "../devices/logicalAddresses.h" +#include "../devices/powerSwitcherList.h" + +#include +#include +#include +#include +#include +#include +#include + +#include + +/** + * This class should be used to create all system objects required for + * the on-board software, using the object ID list from the configuration + * folder. + * + * The objects are registered in the internal object manager automatically. + * This is used later to add objects to tasks. + * + * This file also sets static framework IDs. + * + * Framework objects are created first. + * @ingroup init + */ +void Factory::produce(void) { + setStaticFrameworkObjectIds(); + new EventManager(objects::EVENT_MANAGER); + new HealthTable(objects::HEALTH_TABLE); + new InternalErrorReporter(objects::INTERNAL_ERROR_REPORTER); +} + +void Factory::setStaticFrameworkObjectIds() { + PusServiceBase::packetSource = objects::NO_OBJECT; + PusServiceBase::packetDestination = objects::NO_OBJECT; + + CommandingServiceBase::defaultPacketSource = objects::NO_OBJECT; + CommandingServiceBase::defaultPacketDestination = objects::NO_OBJECT; + + VerificationReporter::messageReceiver = objects::PUS_SERVICE_1_VERIFICATION; + + DeviceHandlerBase::powerSwitcherId = objects::NO_OBJECT; + DeviceHandlerBase::rawDataReceiverId = objects::PUS_SERVICE_2_DEVICE_ACCESS; + + DeviceHandlerFailureIsolation::powerConfirmationId = objects::NO_OBJECT; + + TmPacketStored::timeStamperId = objects::NO_OBJECT; +} + diff --git a/fsfw/defaultcfg/fsfwconfig/objects/Factory.h b/fsfw/defaultcfg/fsfwconfig/objects/Factory.h new file mode 100644 index 0000000..fe55def --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/objects/Factory.h @@ -0,0 +1,17 @@ +#ifndef FACTORY_H_ +#define FACTORY_H_ + +#include +#include + +namespace Factory { + /** + * @brief Creates all SystemObject elements which are persistent + * during execution. + */ + void produce(); + void setStaticFrameworkObjectIds(); +} + + +#endif /* FACTORY_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/objects/systemObjectList.h b/fsfw/defaultcfg/fsfwconfig/objects/systemObjectList.h new file mode 100644 index 0000000..f4292f6 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/objects/systemObjectList.h @@ -0,0 +1,16 @@ +#ifndef CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ +#define CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ + +#include +#include + +// The objects will be instantiated in the ID order +namespace objects { + enum sourceObjects: uint32_t { + /* All addresses between start and end are reserved for the FSFW */ + FSFW_CONFIG_RESERVED_START = PUS_SERVICE_1_VERIFICATION, + FSFW_CONFIG_RESERVED_END = TM_STORE + }; +} + +#endif /* BSP_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/pollingsequence/PollingSequenceFactory.cpp b/fsfw/defaultcfg/fsfwconfig/pollingsequence/PollingSequenceFactory.cpp new file mode 100644 index 0000000..f836a74 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/pollingsequence/PollingSequenceFactory.cpp @@ -0,0 +1,23 @@ +#include "PollingSequenceFactory.h" + +#include +#include +#include + +ReturnValue_t pst::pollingSequenceInitDefault( + FixedTimeslotTaskIF *thisSequence) { + /* Length of a communication cycle */ + uint32_t length = thisSequence->getPeriodMs(); + + /* Add polling sequence table here */ + + if (thisSequence->checkSequence() == HasReturnvaluesIF::RETURN_OK) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + sif::error << "pst::pollingSequenceInitDefault: Sequence invalid!" + << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } +} + diff --git a/fsfw/defaultcfg/fsfwconfig/pollingsequence/PollingSequenceFactory.h b/fsfw/defaultcfg/fsfwconfig/pollingsequence/PollingSequenceFactory.h new file mode 100644 index 0000000..c5d41b7 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/pollingsequence/PollingSequenceFactory.h @@ -0,0 +1,32 @@ +#ifndef POLLINGSEQUENCEFACTORY_H_ +#define POLLINGSEQUENCEFACTORY_H_ + +#include + +class FixedTimeslotTaskIF; + +/** + * All device handlers are scheduled by adding them into Polling Sequence Tables (PST) + * to satisfy stricter timing requirements of device communication, + * A device handler has four different communication steps: + * 1. DeviceHandlerIF::SEND_WRITE -> Send write via interface + * 2. DeviceHandlerIF::GET_WRITE -> Get confirmation for write + * 3. DeviceHandlerIF::SEND_READ -> Send read request + * 4. DeviceHandlerIF::GET_READ -> Read from interface + * The PST specifies precisely when the respective ComIF functions are called + * during the communication cycle time. + * The task is created using the FixedTimeslotTaskIF, + * which utilises the underlying Operating System Abstraction Layer (OSAL) + * + * @param thisSequence FixedTimeslotTaskIF * object is passed inside the Factory class when creating the PST + * @return + */ +namespace pst { + +/* Default PST */ +ReturnValue_t pollingSequenceInitDefault(FixedTimeslotTaskIF *thisSequence); + + +} + +#endif /* POLLINGSEQUENCEINIT_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/returnvalues/classIds.h b/fsfw/defaultcfg/fsfwconfig/returnvalues/classIds.h new file mode 100644 index 0000000..606cc60 --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/returnvalues/classIds.h @@ -0,0 +1,16 @@ +#ifndef CONFIG_RETURNVALUES_CLASSIDS_H_ +#define CONFIG_RETURNVALUES_CLASSIDS_H_ + +#include + +/** + * @brief CLASS_ID defintions which are required for custom returnvalues. + */ +namespace CLASS_ID { +enum { + MISSION_CLASS_ID_START = FW_CLASS_ID_COUNT, +}; +} + + +#endif /* CONFIG_RETURNVALUES_CLASSIDS_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/tmtc/apid.h b/fsfw/defaultcfg/fsfwconfig/tmtc/apid.h new file mode 100644 index 0000000..c0231bc --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/tmtc/apid.h @@ -0,0 +1,18 @@ +#ifndef CONFIG_TMTC_APID_H_ +#define CONFIG_TMTC_APID_H_ + +#include + +/** + * Application Process Definition: entity, uniquely identified by an + * application process ID (APID), capable of generating telemetry source + * packets and receiving telecommand packets. + * + * Chose APID(s) for mission and define it here. + */ +namespace apid { + static const uint16_t DEFAULT_APID = 0x00; +} + + +#endif /* CONFIG_TMTC_APID_H_ */ diff --git a/fsfw/defaultcfg/fsfwconfig/tmtc/pusIds.h b/fsfw/defaultcfg/fsfwconfig/tmtc/pusIds.h new file mode 100644 index 0000000..cc0db9f --- /dev/null +++ b/fsfw/defaultcfg/fsfwconfig/tmtc/pusIds.h @@ -0,0 +1,23 @@ +#ifndef CONFIG_TMTC_PUSIDS_HPP_ +#define CONFIG_TMTC_PUSIDS_HPP_ + +namespace pus { +enum Ids: uint8_t { + PUS_SERVICE_1 = 1, + PUS_SERVICE_2 = 2, + PUS_SERVICE_3 = 3, + PUS_SERVICE_5 = 5, + PUS_SERVICE_6 = 6, + PUS_SERVICE_8 = 8, + PUS_SERVICE_9 = 9, + PUS_SERVICE_11 = 11, + PUS_SERVICE_17 = 17, + PUS_SERVICE_19 = 19, + PUS_SERVICE_20 = 20, + PUS_SERVICE_23 = 23, + PUS_SERVICE_200 = 200, + PUS_SERVICE_201 = 201, +}; +}; + +#endif /* CONFIG_TMTC_PUSIDS_HPP_ */ diff --git a/fsfw/devicehandlers/AcceptsDeviceResponsesIF.h b/fsfw/devicehandlers/AcceptsDeviceResponsesIF.h new file mode 100644 index 0000000..8dc69de --- /dev/null +++ b/fsfw/devicehandlers/AcceptsDeviceResponsesIF.h @@ -0,0 +1,23 @@ +/** + * @file AcceptsDeviceResponsesIF.h + * @brief This file defines the AcceptsDeviceResponsesIF class. + * @date 15.05.2013 + * @author baetz + */ + +#ifndef ACCEPTSDEVICERESPONSESIF_H_ +#define ACCEPTSDEVICERESPONSESIF_H_ + +#include "../ipc/MessageQueueSenderIF.h" + +class AcceptsDeviceResponsesIF { +public: + /** + * Default empty virtual destructor. + */ + virtual ~AcceptsDeviceResponsesIF() { +} +virtual MessageQueueId_t getDeviceQueue() = 0; +}; + +#endif /* ACCEPTSDEVICERESPONSESIF_H_ */ diff --git a/fsfw/devicehandlers/AssemblyBase.cpp b/fsfw/devicehandlers/AssemblyBase.cpp new file mode 100644 index 0000000..8b3f7f4 --- /dev/null +++ b/fsfw/devicehandlers/AssemblyBase.cpp @@ -0,0 +1,274 @@ +#include "AssemblyBase.h" + +AssemblyBase::AssemblyBase(object_id_t objectId, object_id_t parentId, + uint16_t commandQueueDepth) : + SubsystemBase(objectId, parentId, MODE_OFF, commandQueueDepth), internalState( + STATE_NONE), recoveryState(RECOVERY_IDLE), recoveringDevice( + childrenMap.end()), targetMode(MODE_OFF), targetSubmode( + SUBMODE_NONE) { + recoveryOffTimer.setTimeout(POWER_OFF_TIME_MS); +} + +AssemblyBase::~AssemblyBase() { +} + +ReturnValue_t AssemblyBase::handleCommandMessage(CommandMessage* message) { + return handleHealthReply(message); +} + +void AssemblyBase::performChildOperation() { + if (isInTransition()) { + handleChildrenTransition(); + } else { + handleChildrenChanged(); + } +} + +void AssemblyBase::startTransition(Mode_t mode, Submode_t submode) { + doStartTransition(mode, submode); + if (modeHelper.isForced()) { + triggerEvent(FORCING_MODE, mode, submode); + } else { + triggerEvent(CHANGING_MODE, mode, submode); + } +} + +void AssemblyBase::doStartTransition(Mode_t mode, Submode_t submode) { + targetMode = mode; + targetSubmode = submode; + internalState = STATE_SINGLE_STEP; + ReturnValue_t result = commandChildren(mode, submode); + if (result == NEED_SECOND_STEP) { + internalState = STATE_NEED_SECOND_STEP; + } +} + +bool AssemblyBase::isInTransition() { + return (internalState != STATE_NONE) || (recoveryState != RECOVERY_IDLE); +} + +bool AssemblyBase::handleChildrenChanged() { + if (childrenChangedMode) { + ReturnValue_t result = checkChildrenState(); + if (result != RETURN_OK) { + handleChildrenLostMode(result); + } + return true; + } else { + return handleChildrenChangedHealth(); + } +} + +void AssemblyBase::handleChildrenLostMode(ReturnValue_t result) { + triggerEvent(CANT_KEEP_MODE, mode, submode); + startTransition(MODE_OFF, SUBMODE_NONE); +} + +bool AssemblyBase::handleChildrenChangedHealth() { + auto iter = childrenMap.begin(); + for (; iter != childrenMap.end(); iter++) { + if (iter->second.healthChanged) { + iter->second.healthChanged = false; + break; + } + } + if (iter == childrenMap.end()) { + return false; + } + HealthState healthState = healthHelper.healthTable->getHealth(iter->first); + if (healthState == HasHealthIF::NEEDS_RECOVERY) { + triggerEvent(TRYING_RECOVERY); + recoveryState = RECOVERY_STARTED; + recoveringDevice = iter; + doStartTransition(targetMode, targetSubmode); + } else { + triggerEvent(CHILD_CHANGED_HEALTH); + doStartTransition(mode, submode); + } + if (modeHelper.isForced()) { + triggerEvent(FORCING_MODE, targetMode, targetSubmode); + } + return true; +} + +void AssemblyBase::handleChildrenTransition() { + if (commandsOutstanding <= 0) { + switch (internalState) { + case STATE_NEED_SECOND_STEP: + internalState = STATE_SECOND_STEP; + commandChildren(targetMode, targetSubmode); + return; + case STATE_OVERWRITE_HEALTH: { + internalState = STATE_SINGLE_STEP; + ReturnValue_t result = commandChildren(mode, submode); + if (result == NEED_SECOND_STEP) { + internalState = STATE_NEED_SECOND_STEP; + } + return; + } + case STATE_NONE: + //Valid state, used in recovery. + case STATE_SINGLE_STEP: + case STATE_SECOND_STEP: + if (checkAndHandleRecovery()) { + return; + } + break; + } + ReturnValue_t result = checkChildrenState(); + if (result == RETURN_OK) { + handleModeReached(); + } else { + handleModeTransitionFailed(result); + } + } +} + +void AssemblyBase::handleModeReached() { + internalState = STATE_NONE; + setMode(targetMode, targetSubmode); +} + +void AssemblyBase::handleModeTransitionFailed(ReturnValue_t result) { +//always accept transition to OFF, there is nothing we can do except sending an info event +//In theory this should never happen, but we would risk an infinite loop otherwise + if (targetMode == MODE_OFF) { + triggerEvent(CHILD_PROBLEMS, result); + internalState = STATE_NONE; + setMode(targetMode, targetSubmode); + } else { + if (handleChildrenChangedHealth()) { + //If any health change is pending, handle that first. + return; + } + triggerEvent(MODE_TRANSITION_FAILED, result); + startTransition(MODE_OFF, SUBMODE_NONE); + } +} + +void AssemblyBase::sendHealthCommand(MessageQueueId_t sendTo, + HealthState health) { + CommandMessage command; + HealthMessage::setHealthMessage(&command, HealthMessage::HEALTH_SET, + health); + if (commandQueue->sendMessage(sendTo, &command) == RETURN_OK) { + commandsOutstanding++; + } +} + +ReturnValue_t AssemblyBase::checkChildrenState() { + if (targetMode == MODE_OFF) { + return checkChildrenStateOff(); + } else { + return checkChildrenStateOn(targetMode, targetSubmode); + } +} + +ReturnValue_t AssemblyBase::checkChildrenStateOff() { + for (std::map::iterator iter = childrenMap.begin(); + iter != childrenMap.end(); iter++) { + if (checkChildOff(iter->first) != RETURN_OK) { + return NOT_ENOUGH_CHILDREN_IN_CORRECT_STATE; + } + } + return RETURN_OK; +} + +ReturnValue_t AssemblyBase::checkChildOff(uint32_t objectId) { + ChildInfo childInfo = childrenMap.find(objectId)->second; + if (healthHelper.healthTable->isCommandable(objectId)) { + if (childInfo.submode != SUBMODE_NONE) { + return RETURN_FAILED; + } else { + if ((childInfo.mode != MODE_OFF) + && (childInfo.mode != DeviceHandlerIF::MODE_ERROR_ON)) { + return RETURN_FAILED; + } + } + } + return RETURN_OK; +} + +ReturnValue_t AssemblyBase::checkModeCommand(Mode_t mode, Submode_t submode, + uint32_t* msToReachTheMode) { + +//always accept transition to OFF + if (mode == MODE_OFF) { + if (submode != SUBMODE_NONE) { + return INVALID_SUBMODE; + } + return RETURN_OK; + } + + if ((mode != MODE_ON) && (mode != DeviceHandlerIF::MODE_NORMAL)) { + return INVALID_MODE; + } + + if (internalState != STATE_NONE) { + return IN_TRANSITION; + } + + return isModeCombinationValid(mode, submode); +} + +ReturnValue_t AssemblyBase::handleHealthReply(CommandMessage* message) { + if (message->getCommand() == HealthMessage::HEALTH_INFO) { + HealthState health = HealthMessage::getHealth(message); + if (health != EXTERNAL_CONTROL) { + updateChildChangedHealth(message->getSender(), true); + } + return RETURN_OK; + } + if (message->getCommand() == HealthMessage::REPLY_HEALTH_SET + || (message->getCommand() == CommandMessage::REPLY_REJECTED + && message->getParameter2() == HealthMessage::HEALTH_SET)) { + if (isInTransition()) { + commandsOutstanding--; + } + return RETURN_OK; + } + return RETURN_FAILED; +} + +bool AssemblyBase::checkAndHandleRecovery() { + switch (recoveryState) { + case RECOVERY_STARTED: + recoveryState = RECOVERY_WAIT; + recoveryOffTimer.resetTimer(); + return true; + case RECOVERY_WAIT: + if (recoveryOffTimer.isBusy()) { + return true; + } + triggerEvent(RECOVERY_STEP, 0); + sendHealthCommand(recoveringDevice->second.commandQueue, HEALTHY); + internalState = STATE_NONE; + recoveryState = RECOVERY_ONGOING; + //Don't check state! + return true; + case RECOVERY_ONGOING: + triggerEvent(RECOVERY_STEP, 1); + recoveryState = RECOVERY_ONGOING_2; + recoveringDevice->second.healthChanged = false; + //Device should be healthy again, so restart a transition. + //Might be including second step, but that's already handled. + doStartTransition(targetMode, targetSubmode); + return true; + case RECOVERY_ONGOING_2: + triggerEvent(RECOVERY_DONE); + //Now we're through, but not sure if it was successful. + recoveryState = RECOVERY_IDLE; + return false; + case RECOVERY_IDLE: + default: + return false; + } +} + +void AssemblyBase::overwriteDeviceHealth(object_id_t objectId, + HasHealthIF::HealthState oldHealth) { + triggerEvent(OVERWRITING_HEALTH, objectId, oldHealth); + internalState = STATE_OVERWRITE_HEALTH; + modeHelper.setForced(true); + sendHealthCommand(childrenMap[objectId].commandQueue, EXTERNAL_CONTROL); +} diff --git a/fsfw/devicehandlers/AssemblyBase.h b/fsfw/devicehandlers/AssemblyBase.h new file mode 100644 index 0000000..bd2e8fa --- /dev/null +++ b/fsfw/devicehandlers/AssemblyBase.h @@ -0,0 +1,132 @@ +#ifndef ASSEMBLYBASE_H_ +#define ASSEMBLYBASE_H_ + +#include "../container/FixedArrayList.h" +#include "DeviceHandlerBase.h" +#include "../subsystem/SubsystemBase.h" + +class AssemblyBase: public SubsystemBase { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::ASSEMBLY_BASE; + static const ReturnValue_t NEED_SECOND_STEP = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t NEED_TO_RECONFIGURE = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t MODE_FALLBACK = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t CHILD_NOT_COMMANDABLE = MAKE_RETURN_CODE(0x04); + static const ReturnValue_t NEED_TO_CHANGE_HEALTH = MAKE_RETURN_CODE(0x05); + static const ReturnValue_t NOT_ENOUGH_CHILDREN_IN_CORRECT_STATE = + MAKE_RETURN_CODE(0xa1); + + AssemblyBase(object_id_t objectId, object_id_t parentId, uint16_t commandQueueDepth = 8); + virtual ~AssemblyBase(); + +protected: + enum InternalState { + STATE_NONE, + STATE_OVERWRITE_HEALTH, + STATE_NEED_SECOND_STEP, + STATE_SINGLE_STEP, + STATE_SECOND_STEP, + } internalState; + + enum RecoveryState { + RECOVERY_IDLE, + RECOVERY_STARTED, + RECOVERY_ONGOING, + RECOVERY_ONGOING_2, + RECOVERY_WAIT + } recoveryState; //!< Indicates if one of the children requested a recovery. + ChildrenMap::iterator recoveringDevice; + /** + * the mode the current transition is trying to achieve. + * Can be different from the modehelper.commandedMode! + */ + Mode_t targetMode; + + /** + * the submode the current transition is trying to achieve. + * Can be different from the modehelper.commandedSubmode! + */ + Submode_t targetSubmode; + + Countdown recoveryOffTimer; + + static const uint32_t POWER_OFF_TIME_MS = 1000; + + virtual ReturnValue_t handleCommandMessage(CommandMessage *message); + + virtual ReturnValue_t handleHealthReply(CommandMessage *message); + + virtual void performChildOperation(); + + bool handleChildrenChanged(); + + /** + * This method is called if the children changed its mode in a way that the current + * mode can't be kept. + * Default behavior is to go to MODE_OFF. + * @param result The failure code which was returned by checkChildrenState. + */ + virtual void handleChildrenLostMode(ReturnValue_t result); + + bool handleChildrenChangedHealth(); + + virtual void handleChildrenTransition(); + + ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, + uint32_t *msToReachTheMode); + + virtual ReturnValue_t isModeCombinationValid(Mode_t mode, + Submode_t submode) = 0; + + virtual void startTransition(Mode_t mode, Submode_t submode); + + virtual void doStartTransition(Mode_t mode, Submode_t submode); + + virtual bool isInTransition(); + + virtual void handleModeReached(); + + virtual void handleModeTransitionFailed(ReturnValue_t result); + + void sendHealthCommand(MessageQueueId_t sendTo, HealthState health); + + //SHOULDDO: Change that OVERWRITE_HEALTH may be returned (or return internalState directly?) + /** + * command children to reach mode,submode + * + * set #commandsOutstanding correctly, or use executeTable() + * + * @param mode + * @param submode + * @return + * - @c RETURN_OK if ok + * - @c NEED_SECOND_STEP if children need to be commanded again + */ + virtual ReturnValue_t commandChildren(Mode_t mode, Submode_t submode) = 0; + + //SHOULDDO: Remove wantedMode, wantedSubmode, as targetMode/submode is available? + virtual ReturnValue_t checkChildrenStateOn(Mode_t wantedMode, + Submode_t wantedSubmode) = 0; + + virtual ReturnValue_t checkChildrenStateOff(); + + ReturnValue_t checkChildrenState(); + + virtual ReturnValue_t checkChildOff(uint32_t objectId); + + /** + * Manages recovery of a device + * @return true if recovery is still ongoing, false else. + */ + bool checkAndHandleRecovery(); + + /** + * Helper method to overwrite health state of one of the children. + * Also sets state to STATE_OVERWRITE_HEATH. + * @param objectId Must be a registered child. + */ + void overwriteDeviceHealth(object_id_t objectId, HasHealthIF::HealthState oldHealth); + +}; + +#endif /* ASSEMBLYBASE_H_ */ diff --git a/fsfw/devicehandlers/ChildHandlerBase.cpp b/fsfw/devicehandlers/ChildHandlerBase.cpp new file mode 100644 index 0000000..e800cd5 --- /dev/null +++ b/fsfw/devicehandlers/ChildHandlerBase.cpp @@ -0,0 +1,45 @@ +#include "../subsystem/SubsystemBase.h" +#include "../devicehandlers/ChildHandlerBase.h" +#include "../subsystem/SubsystemBase.h" + +ChildHandlerBase::ChildHandlerBase(object_id_t setObjectId, + object_id_t deviceCommunication, CookieIF * cookie, + uint32_t thermalStatePoolId, uint32_t thermalRequestPoolId, + object_id_t parent, FailureIsolationBase* customFdir, + size_t cmdQueueSize) : + DeviceHandlerBase(setObjectId, deviceCommunication, cookie, + (customFdir == nullptr? &childHandlerFdir : customFdir), + cmdQueueSize), + parentId(parent), childHandlerFdir(setObjectId) { + this->setThermalStateRequestPoolIds(thermalStatePoolId, + thermalRequestPoolId); + +} + +ChildHandlerBase::~ChildHandlerBase() { +} + +ReturnValue_t ChildHandlerBase::initialize() { + ReturnValue_t result = DeviceHandlerBase::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + MessageQueueId_t parentQueue = 0; + + if (parentId != objects::NO_OBJECT) { + SubsystemBase *parent = objectManager->get(parentId); + if (parent == NULL) { + return RETURN_FAILED; + } + parentQueue = parent->getCommandQueue(); + + parent->registerChild(getObjectId()); + } + + healthHelper.setParentQueue(parentQueue); + + modeHelper.setParentQueue(parentQueue); + + return RETURN_OK; +} diff --git a/fsfw/devicehandlers/ChildHandlerBase.h b/fsfw/devicehandlers/ChildHandlerBase.h new file mode 100644 index 0000000..f6bf318 --- /dev/null +++ b/fsfw/devicehandlers/ChildHandlerBase.h @@ -0,0 +1,26 @@ +#ifndef FSFW_DEVICES_CHILDHANDLERBASE_H_ +#define FSFW_DEVICES_CHILDHANDLERBASE_H_ + +#include "ChildHandlerFDIR.h" +#include "DeviceHandlerBase.h" + +class ChildHandlerBase: public DeviceHandlerBase { +public: + ChildHandlerBase(object_id_t setObjectId, object_id_t deviceCommunication, + CookieIF * cookie, uint32_t thermalStatePoolId, + uint32_t thermalRequestPoolId, + object_id_t parent = objects::NO_OBJECT, + FailureIsolationBase* customFdir = nullptr, + size_t cmdQueueSize = 20); + virtual ~ChildHandlerBase(); + + virtual ReturnValue_t initialize(); + +protected: + const uint32_t parentId; + ChildHandlerFDIR childHandlerFdir; + +}; + +#endif /* FSFW_DEVICES_CHILDHANDLERBASE_H_ */ + diff --git a/fsfw/devicehandlers/ChildHandlerFDIR.cpp b/fsfw/devicehandlers/ChildHandlerFDIR.cpp new file mode 100644 index 0000000..14043a7 --- /dev/null +++ b/fsfw/devicehandlers/ChildHandlerFDIR.cpp @@ -0,0 +1,10 @@ +#include "ChildHandlerFDIR.h" + +ChildHandlerFDIR::ChildHandlerFDIR(object_id_t owner, object_id_t faultTreeParent, uint32_t recoveryCount) : + DeviceHandlerFailureIsolation(owner, faultTreeParent) { + recoveryCounter.setFailureThreshold(recoveryCount); +} + +ChildHandlerFDIR::~ChildHandlerFDIR() { +} + diff --git a/fsfw/devicehandlers/ChildHandlerFDIR.h b/fsfw/devicehandlers/ChildHandlerFDIR.h new file mode 100644 index 0000000..ce84451 --- /dev/null +++ b/fsfw/devicehandlers/ChildHandlerFDIR.h @@ -0,0 +1,20 @@ +#ifndef FRAMEWORK_DEVICEHANDLERS_CHILDHANDLERFDIR_H_ +#define FRAMEWORK_DEVICEHANDLERS_CHILDHANDLERFDIR_H_ + +#include "DeviceHandlerFailureIsolation.h" + +/** + * Very simple extension to normal FDIR. + * Does not have a default fault tree parent and + * allows to make the recovery count settable to 0. + */ +class ChildHandlerFDIR: public DeviceHandlerFailureIsolation { +public: + ChildHandlerFDIR(object_id_t owner, object_id_t faultTreeParent = + NO_FAULT_TREE_PARENT, uint32_t recoveryCount = 0); + virtual ~ChildHandlerFDIR(); +protected: + static const object_id_t NO_FAULT_TREE_PARENT = 0; +}; + +#endif /* FRAMEWORK_DEVICEHANDLERS_CHILDHANDLERFDIR_H_ */ diff --git a/fsfw/devicehandlers/CookieIF.h b/fsfw/devicehandlers/CookieIF.h new file mode 100644 index 0000000..496cf0d --- /dev/null +++ b/fsfw/devicehandlers/CookieIF.h @@ -0,0 +1,34 @@ +#ifndef COOKIE_H_ +#define COOKIE_H_ +#include + +/** + * @brief Physical address type + */ +typedef std::uint32_t address_t; + +/** + * @brief This datatype is used to identify different connection over a + * single interface (like RMAP or I2C) + * @details + * To use this class, implement a communication specific child cookie which + * inherits Cookie. Cookie instances are created in config/Factory.cpp by + * calling @code{.cpp} CookieIF* childCookie = new ChildCookie(...) + * @endcode . + * + * [not implemented yet] + * This cookie is then passed to the child device handlers, which stores the + * pointer and passes it to the communication interface functions. + * + * The cookie can be used to store all kinds of information + * about the communication, like slave addresses, communication status, + * communication parameters etc. + * + * @ingroup comm + */ +class CookieIF { +public: + virtual ~CookieIF() {}; +}; + +#endif /* COOKIE_H_ */ diff --git a/fsfw/devicehandlers/DeviceCommunicationIF.h b/fsfw/devicehandlers/DeviceCommunicationIF.h new file mode 100644 index 0000000..11960d8 --- /dev/null +++ b/fsfw/devicehandlers/DeviceCommunicationIF.h @@ -0,0 +1,131 @@ +#ifndef DEVICECOMMUNICATIONIF_H_ +#define DEVICECOMMUNICATIONIF_H_ + +#include "CookieIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include +/** + * @defgroup interfaces Interfaces + * @brief Interfaces for flight software objects + */ + +/** + * @defgroup comm Communication + * @brief Communication software components. + */ + +/** + * @brief This is an interface to decouple device communication from + * the device handler to allow reuse of these components. + * @details + * Documentation: Dissertation Baetz p.138. + * It works with the assumption that received data + * is polled by a component. There are four generic steps of device communication: + * + * 1. Send data to a device + * 2. Get acknowledgement for sending + * 3. Request reading data from a device + * 4. Read received data + * + * To identify different connection over a single interface can return + * so-called cookies to components. + * The CommunicationMessage message type can be used to extend the + * functionality of the ComIF if a separate polling task is required. + * @ingroup interfaces + * @ingroup comm + */ +class DeviceCommunicationIF: public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::DEVICE_COMMUNICATION_IF; + + //! Standard Error Codes + //! General protocol error. Define more concrete errors in child handler + static const ReturnValue_t PROTOCOL_ERROR = MAKE_RETURN_CODE(0x01); + //! If cookie is a null pointer + static const ReturnValue_t NULLPOINTER = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t INVALID_COOKIE_TYPE = MAKE_RETURN_CODE(0x03); + // is this needed if there is no open/close call? + static const ReturnValue_t NOT_ACTIVE = MAKE_RETURN_CODE(0x05); + static const ReturnValue_t INVALID_ADDRESS = MAKE_RETURN_CODE(0x06); + static const ReturnValue_t TOO_MUCH_DATA = MAKE_RETURN_CODE(0x07); + static const ReturnValue_t CANT_CHANGE_REPLY_LEN = MAKE_RETURN_CODE(0x08); + + //! Can be used in readReceivedMessage() if no reply was received. + static const ReturnValue_t NO_REPLY_RECEIVED = MAKE_RETURN_CODE(0xA1); + + virtual ~DeviceCommunicationIF() {} + + + /** + * @brief Device specific initialization, using the cookie. + * @details + * The cookie is already prepared in the factory. If the communication + * interface needs to be set up in some way and requires cookie information, + * this can be performed in this function, which is called on device handler + * initialization. + * @param cookie + * @return + * - @c RETURN_OK if initialization was successfull + * - Everything else triggers failure event with returnvalue as parameter 1 + */ + virtual ReturnValue_t initializeInterface(CookieIF * cookie) = 0; + + /** + * Called by DHB in the SEND_WRITE doSendWrite(). + * This function is used to send data to the physical device + * by implementing and calling related drivers or wrapper functions. + * @param cookie + * @param data + * @param len + * @return + * - @c RETURN_OK for successfull send + * - Everything else triggers failure event with returnvalue as parameter 1 + */ + virtual ReturnValue_t sendMessage(CookieIF *cookie, const uint8_t * sendData, + size_t sendLen) = 0; + + /** + * Called by DHB in the GET_WRITE doGetWrite(). + * Get send confirmation that the data in sendMessage() was sent successfully. + * @param cookie + * @return - @c RETURN_OK if data was sent successfull + * - Everything else triggers falure event with + * returnvalue as parameter 1 + */ + virtual ReturnValue_t getSendSuccess(CookieIF *cookie) = 0; + + /** + * Called by DHB in the SEND_WRITE doSendRead(). + * It is assumed that it is always possible to request a reply + * from a device. If a requestLen of 0 is supplied, no reply was enabled + * and communication specific action should be taken (e.g. read nothing + * or read everything). + * + * @param cookie + * @param requestLen Size of data to read + * @return - @c RETURN_OK to confirm the request for data has been sent. + * - Everything else triggers failure event with + * returnvalue as parameter 1 + */ + virtual ReturnValue_t requestReceiveMessage(CookieIF *cookie, + size_t requestLen) = 0; + + /** + * Called by DHB in the GET_WRITE doGetRead(). + * This function is used to receive data from the physical device + * by implementing and calling related drivers or wrapper functions. + * @param cookie + * @param buffer [out] Set reply here (by using *buffer = ...) + * @param size [out] size pointer to set (by using *size = ...). + * Set to 0 if no reply was received + * @return - @c RETURN_OK for successfull receive + * - @c NO_REPLY_RECEIVED if not reply was received. Setting size to + * 0 has the same effect + * - Everything else triggers failure event with + * returnvalue as parameter 1 + */ + virtual ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, + size_t *size) = 0; +}; + +#endif /* DEVICECOMMUNICATIONIF_H_ */ diff --git a/fsfw/devicehandlers/DeviceHandlerBase.cpp b/fsfw/devicehandlers/DeviceHandlerBase.cpp new file mode 100644 index 0000000..710e8a8 --- /dev/null +++ b/fsfw/devicehandlers/DeviceHandlerBase.cpp @@ -0,0 +1,1351 @@ +#include "DeviceHandlerBase.h" +#include "AcceptsDeviceResponsesIF.h" +#include "DeviceTmReportingWrapper.h" + +#include "../objectmanager/ObjectManager.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../thermal/ThermalComponentIF.h" +#include "../datapool/DataSet.h" +#include "../datapool/PoolVariable.h" +#include "../globalfunctions/CRC.h" +#include "../subsystem/SubsystemBase.h" +#include "../ipc/QueueFactory.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +#include + +object_id_t DeviceHandlerBase::powerSwitcherId = objects::NO_OBJECT; +object_id_t DeviceHandlerBase::rawDataReceiverId = objects::NO_OBJECT; +object_id_t DeviceHandlerBase::defaultFdirParentId = objects::NO_OBJECT; + +DeviceHandlerBase::DeviceHandlerBase(object_id_t setObjectId, + object_id_t deviceCommunication, CookieIF * comCookie, + FailureIsolationBase* fdirInstance, size_t cmdQueueSize) : + SystemObject(setObjectId), mode(MODE_OFF), submode(SUBMODE_NONE), + wiretappingMode(OFF), storedRawData(StorageManagerIF::INVALID_ADDRESS), + deviceCommunicationId(deviceCommunication), comCookie(comCookie), + healthHelper(this,setObjectId), modeHelper(this), parameterHelper(this), + actionHelper(this, nullptr), childTransitionFailure(RETURN_OK), + fdirInstance(fdirInstance), hkSwitcher(this), + defaultFDIRUsed(fdirInstance == nullptr), switchOffWasReported(false), + childTransitionDelay(5000), transitionSourceMode(_MODE_POWER_DOWN), + transitionSourceSubMode(SUBMODE_NONE) { + commandQueue = QueueFactory::instance()->createMessageQueue(cmdQueueSize, + MessageQueueMessage::MAX_MESSAGE_SIZE); + insertInCommandMap(RAW_COMMAND_ID); + cookieInfo.state = COOKIE_UNUSED; + cookieInfo.pendingCommand = deviceCommandMap.end(); + if (comCookie == nullptr) { + sif::error << "DeviceHandlerBase: ObjectID 0x" << std::hex + << std::setw(8) << std::setfill('0') << this->getObjectId() + << std::dec << ": Do not pass nullptr as a cookie, consider " + << std::setfill(' ') << "passing a dummy cookie instead!" + << std::endl; + } + if (this->fdirInstance == nullptr) { + this->fdirInstance = new DeviceHandlerFailureIsolation(setObjectId, + defaultFdirParentId); + } +} + +void DeviceHandlerBase::setThermalStateRequestPoolIds( + uint32_t thermalStatePoolId, uint32_t thermalRequestPoolId) { + this->deviceThermalRequestPoolId = thermalStatePoolId; + this->deviceThermalRequestPoolId = thermalRequestPoolId; +} + + +DeviceHandlerBase::~DeviceHandlerBase() { + delete comCookie; + if (defaultFDIRUsed) { + delete fdirInstance; + } + QueueFactory::instance()->deleteMessageQueue(commandQueue); +} + +ReturnValue_t DeviceHandlerBase::performOperation(uint8_t counter) { + this->pstStep = counter; + + if (getComAction() == SEND_WRITE) { + cookieInfo.state = COOKIE_UNUSED; + readCommandQueue(); + doStateMachine(); + checkSwitchState(); + decrementDeviceReplyMap(); + fdirInstance->checkForFailures(); + hkSwitcher.performOperation(); + performOperationHook(); + } + if (mode == MODE_OFF) { + return RETURN_OK; + } + switch (getComAction()) { + case SEND_WRITE: + if ((cookieInfo.state == COOKIE_UNUSED)) { + buildInternalCommand(); + } + doSendWrite(); + break; + case GET_WRITE: + doGetWrite(); + break; + case SEND_READ: + doSendRead(); + break; + case GET_READ: + doGetRead(); + cookieInfo.state = COOKIE_UNUSED; + break; + default: + break; + } + return RETURN_OK; +} + +ReturnValue_t DeviceHandlerBase::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != RETURN_OK) { + return result; + } + + communicationInterface = objectManager->get( + deviceCommunicationId); + if (communicationInterface == nullptr) { + sif::error << "DeviceHandlerBase::initialize: Communication interface " + "invalid." << std::endl; + sif::error << "Make sure it is set up properly and implements" + " DeviceCommunicationIF" << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + result = communicationInterface->initializeInterface(comCookie); + if (result != RETURN_OK) { + return result; + } + + IPCStore = objectManager->get(objects::IPC_STORE); + if (IPCStore == nullptr) { + sif::error << "DeviceHandlerBase::initialize: IPC store not set up in " + "factory." << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + if(rawDataReceiverId != objects::NO_OBJECT) { + AcceptsDeviceResponsesIF *rawReceiver = objectManager->get< + AcceptsDeviceResponsesIF>(rawDataReceiverId); + + if (rawReceiver == nullptr) { + sif::error << "DeviceHandlerBase::initialize: Raw receiver object " + "ID set but no valid object found." << std::endl; + sif::error << "Make sure the raw receiver object is set up properly" + " and implements AcceptsDeviceResponsesIF" << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + defaultRawReceiver = rawReceiver->getDeviceQueue(); + } + + if(powerSwitcherId != objects::NO_OBJECT) { + powerSwitcher = objectManager->get(powerSwitcherId); + if (powerSwitcher == nullptr) { + sif::error << "DeviceHandlerBase::initialize: Power switcher " + << "object ID set but no valid object found." << std::endl; + sif::error << "Make sure the raw receiver object is set up properly" + << " and implements PowerSwitchIF" << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + } + + result = healthHelper.initialize(); + if (result != RETURN_OK) { + return result; + } + + result = modeHelper.initialize(); + if (result != RETURN_OK) { + return result; + } + result = actionHelper.initialize(commandQueue); + if (result != RETURN_OK) { + return result; + } + result = fdirInstance->initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = parameterHelper.initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = hkSwitcher.initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + fillCommandAndReplyMap(); + + //Set temperature target state to NON_OP. + DataSet mySet; + db_int8_t thermalRequest(deviceThermalRequestPoolId, &mySet, + PoolVariableIF::VAR_WRITE); + mySet.read(); + thermalRequest = ThermalComponentIF::STATE_REQUEST_NON_OPERATIONAL; + mySet.commit(PoolVariableIF::VALID); + + return RETURN_OK; + +} + +void DeviceHandlerBase::decrementDeviceReplyMap() { + for (std::map::iterator iter = + deviceReplyMap.begin(); iter != deviceReplyMap.end(); iter++) { + if (iter->second.delayCycles != 0) { + iter->second.delayCycles--; + if (iter->second.delayCycles == 0) { + if (iter->second.periodic) { + iter->second.delayCycles = iter->second.maxDelayCycles; + } + replyToReply(iter, TIMEOUT); + missedReply(iter->first); + } + } + } +} + +void DeviceHandlerBase::readCommandQueue() { + + if (dontCheckQueue()) { + return; + } + + CommandMessage command; + ReturnValue_t result = commandQueue->receiveMessage(&command); + if (result != RETURN_OK) { + return; + } + + result = healthHelper.handleHealthCommand(&command); + if (result == RETURN_OK) { + return; + } + + result = modeHelper.handleModeCommand(&command); + if (result == RETURN_OK) { + return; + } + + result = actionHelper.handleActionMessage(&command); + if (result == RETURN_OK) { + return; + } + + result = parameterHelper.handleParameterMessage(&command); + if (result == RETURN_OK) { + return; + } + +// result = hkManager.handleHousekeepingMessage(&command); +// if (result == RETURN_OK) { +// return; +// } + + result = handleDeviceHandlerMessage(&command); + if (result == RETURN_OK) { + return; + } + + result = letChildHandleMessage(&command); + if (result == RETURN_OK) { + return; + } + + replyReturnvalueToCommand(CommandMessage::UNKNOWN_COMMAND); + +} + +void DeviceHandlerBase::doStateMachine() { + switch (mode) { + case _MODE_START_UP: + case _MODE_SHUT_DOWN: + case _MODE_TO_NORMAL: + case _MODE_TO_ON: + case _MODE_TO_RAW: { + Mode_t currentMode = mode; + callChildStatemachine(); + //Only do timeout if child did not change anything + if (mode != currentMode) { + break; + } + uint32_t currentUptime; + Clock::getUptime(¤tUptime); + if (currentUptime - timeoutStart >= childTransitionDelay) { + triggerEvent(MODE_TRANSITION_FAILED, childTransitionFailure, 0); + setMode(transitionSourceMode, transitionSourceSubMode); + break; + } + } + break; + case _MODE_POWER_DOWN: + commandSwitch(PowerSwitchIF::SWITCH_OFF); + setMode(_MODE_WAIT_OFF); + break; + case _MODE_POWER_ON: + commandSwitch(PowerSwitchIF::SWITCH_ON); + setMode(_MODE_WAIT_ON); + break; + case _MODE_WAIT_ON: { + uint32_t currentUptime; + Clock::getUptime(¤tUptime); + if (powerSwitcher != nullptr and currentUptime - timeoutStart >= + powerSwitcher->getSwitchDelayMs()) { + triggerEvent(MODE_TRANSITION_FAILED, PowerSwitchIF::SWITCH_TIMEOUT, + 0); + setMode(_MODE_POWER_DOWN); + callChildStatemachine(); + break; + } + ReturnValue_t switchState = getStateOfSwitches(); + if ((switchState == PowerSwitchIF::SWITCH_ON) + || (switchState == NO_SWITCH)) { + //NOTE: TransitionSourceMode and -SubMode are set by handleCommandedModeTransition + childTransitionFailure = CHILD_TIMEOUT; + setMode(_MODE_START_UP); + callChildStatemachine(); + } + } + break; + case _MODE_WAIT_OFF: { + uint32_t currentUptime; + Clock::getUptime(¤tUptime); + + if(powerSwitcher == nullptr) { + setMode(MODE_OFF); + break; + } + + if (currentUptime - timeoutStart >= powerSwitcher->getSwitchDelayMs()) { + triggerEvent(MODE_TRANSITION_FAILED, PowerSwitchIF::SWITCH_TIMEOUT, + 0); + setMode(MODE_ERROR_ON); + break; + } + ReturnValue_t switchState = getStateOfSwitches(); + if ((switchState == PowerSwitchIF::SWITCH_OFF) + || (switchState == NO_SWITCH)) { + setMode(_MODE_SWITCH_IS_OFF); + } + } + break; + case MODE_OFF: + doOffActivity(); + break; + case MODE_ON: + doOnActivity(); + break; + case MODE_RAW: + case MODE_NORMAL: + case MODE_ERROR_ON: + break; + case _MODE_SWITCH_IS_OFF: + setMode(MODE_OFF, SUBMODE_NONE); + break; + default: + triggerEvent(OBJECT_IN_INVALID_MODE, mode, submode); + setMode(_MODE_POWER_DOWN, 0); + break; + } +} + +ReturnValue_t DeviceHandlerBase::isModeCombinationValid(Mode_t mode, + Submode_t submode) { + switch (mode) { + case MODE_OFF: + case MODE_ON: + case MODE_NORMAL: + case MODE_RAW: + if (submode == SUBMODE_NONE) { + return RETURN_OK; + } else { + return INVALID_SUBMODE; + } + default: + return HasModesIF::INVALID_MODE; + } +} + +ReturnValue_t DeviceHandlerBase::insertInCommandAndReplyMap( + DeviceCommandId_t deviceCommand, uint16_t maxDelayCycles, + size_t replyLen, bool periodic, bool hasDifferentReplyId, + DeviceCommandId_t replyId) { + //No need to check, as we may try to insert multiple times. + insertInCommandMap(deviceCommand); + if (hasDifferentReplyId) { + return insertInReplyMap(replyId, maxDelayCycles, replyLen, periodic); + } else { + return insertInReplyMap(deviceCommand, maxDelayCycles, replyLen, periodic); + } +} + +ReturnValue_t DeviceHandlerBase::insertInReplyMap(DeviceCommandId_t replyId, + uint16_t maxDelayCycles, size_t replyLen, bool periodic) { + DeviceReplyInfo info; + info.maxDelayCycles = maxDelayCycles; + info.periodic = periodic; + info.delayCycles = 0; + info.replyLen = replyLen; + info.command = deviceCommandMap.end(); + auto resultPair = deviceReplyMap.emplace(replyId, info); + if (resultPair.second) { + return RETURN_OK; + } else { + return RETURN_FAILED; + } +} + +ReturnValue_t DeviceHandlerBase::insertInCommandMap( + DeviceCommandId_t deviceCommand) { + DeviceCommandInfo info; + info.expectedReplies = 0; + info.isExecuting = false; + info.sendReplyTo = NO_COMMANDER; + auto resultPair = deviceCommandMap.emplace(deviceCommand, info); + if (resultPair.second) { + return RETURN_OK; + } else { + return RETURN_FAILED; + } +} + +ReturnValue_t DeviceHandlerBase::updateReplyMapEntry(DeviceCommandId_t deviceReply, + uint16_t delayCycles, uint16_t maxDelayCycles, bool periodic) { + std::map::iterator iter = + deviceReplyMap.find(deviceReply); + if (iter == deviceReplyMap.end()) { + triggerEvent(INVALID_DEVICE_COMMAND, deviceReply); + return RETURN_FAILED; + } else { + DeviceReplyInfo *info = &(iter->second); + if (maxDelayCycles != 0) { + info->maxDelayCycles = maxDelayCycles; + } + info->delayCycles = delayCycles; + info->periodic = periodic; + return RETURN_OK; + } +} + +void DeviceHandlerBase::callChildStatemachine() { + if (mode == _MODE_START_UP) { + doStartUp(); + } else if (mode == _MODE_SHUT_DOWN) { + doShutDown(); + } else if (mode & TRANSITION_MODE_CHILD_ACTION_MASK) { + doTransition(transitionSourceMode, transitionSourceSubMode); + } +} + +void DeviceHandlerBase::setTransition(Mode_t modeTo, Submode_t submodeTo) { + triggerEvent(CHANGING_MODE, modeTo, submodeTo); + childTransitionDelay = getTransitionDelayMs(mode, modeTo); + transitionSourceMode = mode; + transitionSourceSubMode = submode; + childTransitionFailure = CHILD_TIMEOUT; + + // transitionTargetMode is set by setMode + setMode((modeTo | TRANSITION_MODE_CHILD_ACTION_MASK), submodeTo); +} + +void DeviceHandlerBase::setMode(Mode_t newMode, uint8_t newSubmode) { + changeHK(mode, submode, false); + submode = newSubmode; + mode = newMode; + modeChanged(); + setNormalDatapoolEntriesInvalid(); + if (!isTransitionalMode()) { + modeHelper.modeChanged(newMode, newSubmode); + announceMode(false); + } + Clock::getUptime(&timeoutStart); + + if (mode == MODE_OFF) { + DataSet mySet; + db_int8_t thermalRequest(deviceThermalRequestPoolId, &mySet, + PoolVariableIF::VAR_READ_WRITE); + mySet.read(); + if (thermalRequest != ThermalComponentIF::STATE_REQUEST_IGNORE) { + thermalRequest = ThermalComponentIF::STATE_REQUEST_NON_OPERATIONAL; + } + mySet.commit(PoolVariableIF::VALID); + } + changeHK(mode, submode, true); +} + +void DeviceHandlerBase::setMode(Mode_t newMode) { + setMode(newMode, submode); +} + +void DeviceHandlerBase::replyReturnvalueToCommand(ReturnValue_t status, + uint32_t parameter) { + //This is actually the reply protocol for raw and misc DH commands. + if (status == RETURN_OK) { + CommandMessage reply(CommandMessage::REPLY_COMMAND_OK, 0, parameter); + commandQueue->reply(&reply); + } else { + CommandMessage reply(CommandMessage::REPLY_REJECTED, status, parameter); + commandQueue->reply(&reply); + } +} + +void DeviceHandlerBase::replyToCommand(ReturnValue_t status, + uint32_t parameter) { +//Check if we reply to a raw command. + if (cookieInfo.pendingCommand->first == RAW_COMMAND_ID) { + if (status == NO_REPLY_EXPECTED) { + status = RETURN_OK; + } + replyReturnvalueToCommand(status, parameter); + //Always delete data from a raw command. + IPCStore->deleteData(storedRawData); + return; + } +//Check if we were externally commanded. + if (cookieInfo.pendingCommand->second.sendReplyTo != NO_COMMANDER) { + MessageQueueId_t queueId = cookieInfo.pendingCommand->second.sendReplyTo; + if (status == NO_REPLY_EXPECTED) { + actionHelper.finish(queueId, cookieInfo.pendingCommand->first, + RETURN_OK); + } else { + actionHelper.step(1, queueId, cookieInfo.pendingCommand->first, + status); + } + } +} + +void DeviceHandlerBase::replyToReply(DeviceReplyMap::iterator iter, + ReturnValue_t status) { +//No need to check if iter exists, as this is checked by callers. If someone else uses the method, add check. + if (iter->second.command == deviceCommandMap.end()) { + //Is most likely periodic reply. Silent return. + return; + } +//Check if more replies are expected. If so, do nothing. + DeviceCommandInfo* info = &(iter->second.command->second); + if (--info->expectedReplies == 0) { + //Check if it was transition or internal command. Don't send any replies in that case. + if (info->sendReplyTo != NO_COMMANDER) { + actionHelper.finish(info->sendReplyTo, iter->first, status); + } + info->isExecuting = false; + } +} + +void DeviceHandlerBase::doSendWrite() { + if (cookieInfo.state == COOKIE_WRITE_READY) { + + ReturnValue_t result = communicationInterface->sendMessage(comCookie, + rawPacket, rawPacketLen); + + if (result == RETURN_OK) { + cookieInfo.state = COOKIE_WRITE_SENT; + } else { + //always generate a failure event, so that FDIR knows what's up + triggerEvent(DEVICE_SENDING_COMMAND_FAILED, result, + cookieInfo.pendingCommand->first); + replyToCommand(result); + cookieInfo.state = COOKIE_UNUSED; + cookieInfo.pendingCommand->second.isExecuting = false; + } + } +} + +void DeviceHandlerBase::doGetWrite() { + if (cookieInfo.state != COOKIE_WRITE_SENT) { + return; + } + cookieInfo.state = COOKIE_UNUSED; + ReturnValue_t result = communicationInterface->getSendSuccess(comCookie); + if (result == RETURN_OK) { + if (wiretappingMode == RAW) { + replyRawData(rawPacket, rawPacketLen, requestedRawTraffic, true); + } + + //We need to distinguish here, because a raw command never expects a reply. + //(Could be done in eRIRM, but then child implementations need to be careful. + result = enableReplyInReplyMap(cookieInfo.pendingCommand); + } else { + //always generate a failure event, so that FDIR knows what's up + triggerEvent(DEVICE_SENDING_COMMAND_FAILED, result, + cookieInfo.pendingCommand->first); + } + if (result != RETURN_OK) { + cookieInfo.pendingCommand->second.isExecuting = false; + } + replyToCommand(result); +} + +void DeviceHandlerBase::doSendRead() { + ReturnValue_t result; + + size_t requestLen = 0; + if(cookieInfo.pendingCommand != deviceCommandMap.end()) { + DeviceReplyIter iter = deviceReplyMap.find( + cookieInfo.pendingCommand->first); + if(iter != deviceReplyMap.end()) { + requestLen = iter->second.replyLen; + } + } + + result = communicationInterface->requestReceiveMessage(comCookie, requestLen); + + if (result == RETURN_OK) { + cookieInfo.state = COOKIE_READ_SENT; + } else { + triggerEvent(DEVICE_REQUESTING_REPLY_FAILED, result); + //We can't inform anyone, because we don't know which command was sent last. + //So, we need to wait for a timeout. + //but I think we can allow to ignore one missedReply. + ignoreMissedRepliesCount++; + cookieInfo.state = COOKIE_UNUSED; + } +} + +void DeviceHandlerBase::doGetRead() { + size_t receivedDataLen = 0; + uint8_t *receivedData = nullptr; + + if (cookieInfo.state != COOKIE_READ_SENT) { + cookieInfo.state = COOKIE_UNUSED; + return; + } + + cookieInfo.state = COOKIE_UNUSED; + + ReturnValue_t result = communicationInterface->readReceivedMessage( + comCookie, &receivedData, &receivedDataLen); + + if (result != RETURN_OK) { + triggerEvent(DEVICE_REQUESTING_REPLY_FAILED, result); + //I think we can allow to ignore one missedReply. + ignoreMissedRepliesCount++; + return; + } + + if (receivedDataLen == 0 or result == DeviceCommunicationIF::NO_REPLY_RECEIVED) + return; + + if (wiretappingMode == RAW) { + replyRawData(receivedData, receivedDataLen, requestedRawTraffic); + } + + if (mode == MODE_RAW and defaultRawReceiver != MessageQueueIF::NO_QUEUE) { + replyRawReplyIfnotWiretapped(receivedData, receivedDataLen); + } + else { + parseReply(receivedData, receivedDataLen); + } +} + +void DeviceHandlerBase::parseReply(const uint8_t* receivedData, + size_t receivedDataLen) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED; + DeviceCommandId_t foundId = 0xFFFFFFFF; + size_t foundLen = 0; + // The loop may not execute more often than the number of received bytes + // (worst case). This approach avoids infinite loops due to buggy + // scanForReply routines. + uint32_t remainingLength = receivedDataLen; + for (uint32_t count = 0; count < receivedDataLen; count++) { + result = scanForReply(receivedData, remainingLength, &foundId, + &foundLen); + switch (result) { + case RETURN_OK: + handleReply(receivedData, foundId, foundLen); + break; + case APERIODIC_REPLY: { + result = interpretDeviceReply(foundId, receivedData); + if (result != RETURN_OK) { + replyRawReplyIfnotWiretapped(receivedData, foundLen); + triggerEvent(DEVICE_INTERPRETING_REPLY_FAILED, result, + foundId); + } + } + break; + case IGNORE_REPLY_DATA: + break; + case IGNORE_FULL_PACKET: + return; + default: + //We need to wait for timeout.. don't know what command failed and who sent it. + replyRawReplyIfnotWiretapped(receivedData, foundLen); + triggerEvent(DEVICE_READING_REPLY_FAILED, result, foundLen); + break; + } + receivedData += foundLen; + if (remainingLength > foundLen) { + remainingLength -= foundLen; + } else { + return; + } + } +} + +void DeviceHandlerBase::handleReply(const uint8_t* receivedData, + DeviceCommandId_t foundId, uint32_t foundLen) { + ReturnValue_t result; + DeviceReplyMap::iterator iter = deviceReplyMap.find(foundId); + + if (iter == deviceReplyMap.end()) { + replyRawReplyIfnotWiretapped(receivedData, foundLen); + triggerEvent(DEVICE_UNKNOWN_REPLY, foundId); + return; + } + + DeviceReplyInfo *info = &(iter->second); + + if (info->delayCycles != 0) { + + if (info->periodic != false) { + info->delayCycles = info->maxDelayCycles; + } + else { + info->delayCycles = 0; + } + + result = interpretDeviceReply(foundId, receivedData); + + if (result != RETURN_OK) { + // Report failed interpretation to FDIR. + replyRawReplyIfnotWiretapped(receivedData, foundLen); + triggerEvent(DEVICE_INTERPRETING_REPLY_FAILED, result, foundId); + } + replyToReply(iter, result); + } + else { + // Other completion failure messages are created by timeout. + // Powering down the device might take some time during which periodic + // replies may still come in. + if (mode != _MODE_WAIT_OFF) { + triggerEvent(DEVICE_UNREQUESTED_REPLY, foundId); + } + } +} + +ReturnValue_t DeviceHandlerBase::getStorageData(store_address_t storageAddress, + uint8_t** data, uint32_t * len) { + size_t lenTmp; + + if (IPCStore == nullptr) { + *data = nullptr; + *len = 0; + return RETURN_FAILED; + } + ReturnValue_t result = IPCStore->modifyData(storageAddress, data, &lenTmp); + if (result == RETURN_OK) { + *len = lenTmp; + return RETURN_OK; + } else { + triggerEvent(StorageManagerIF::GET_DATA_FAILED, result, + storageAddress.raw); + *data = nullptr; + *len = 0; + return result; + } +} + +void DeviceHandlerBase::replyRawData(const uint8_t *data, size_t len, + MessageQueueId_t sendTo, bool isCommand) { + if (IPCStore == nullptr or len == 0 or sendTo == MessageQueueIF::NO_QUEUE) { + return; + } + store_address_t address; + ReturnValue_t result = IPCStore->addData(&address, data, len); + + if (result != RETURN_OK) { + triggerEvent(StorageManagerIF::STORE_DATA_FAILED, result); + return; + } + + CommandMessage command; + + DeviceHandlerMessage::setDeviceHandlerRawReplyMessage(&command, + getObjectId(), address, isCommand); + + result = commandQueue->sendMessage(sendTo, &command); + + if (result != RETURN_OK) { + IPCStore->deleteData(address); + // Silently discard data, this indicates heavy TM traffic which + // should not be increased by additional events. + } +} + +//Default child implementations +DeviceHandlerIF::CommunicationAction_t DeviceHandlerBase::getComAction() { + switch (pstStep) { + case 0: + return SEND_WRITE; + break; + case 1: + return GET_WRITE; + break; + case 2: + return SEND_READ; + break; + case 3: + return GET_READ; + break; + default: + break; + } + return NOTHING; +} + +MessageQueueId_t DeviceHandlerBase::getCommandQueue() const { + return commandQueue->getId(); +} + +void DeviceHandlerBase::buildRawDeviceCommand(CommandMessage* commandMessage) { + storedRawData = DeviceHandlerMessage::getStoreAddress(commandMessage); + ReturnValue_t result = getStorageData(storedRawData, &rawPacket, + &rawPacketLen); + if (result != RETURN_OK) { + replyReturnvalueToCommand(result, RAW_COMMAND_ID); + storedRawData.raw = StorageManagerIF::INVALID_ADDRESS; + } else { + cookieInfo.pendingCommand = deviceCommandMap.find( + (DeviceCommandId_t) RAW_COMMAND_ID); + cookieInfo.pendingCommand->second.isExecuting = true; + cookieInfo.state = COOKIE_WRITE_READY; + } +} + +void DeviceHandlerBase::commandSwitch(ReturnValue_t onOff) { + if(powerSwitcher == nullptr) { + return; + } + const uint8_t *switches; + uint8_t numberOfSwitches = 0; + ReturnValue_t result = getSwitches(&switches, &numberOfSwitches); + if (result == RETURN_OK) { + while (numberOfSwitches > 0) { + powerSwitcher->sendSwitchCommand(switches[numberOfSwitches - 1], + onOff); + numberOfSwitches--; + } + } +} + +ReturnValue_t DeviceHandlerBase::getSwitches(const uint8_t **switches, + uint8_t *numberOfSwitches) { + return DeviceHandlerBase::NO_SWITCH; +} + +void DeviceHandlerBase::modeChanged(void) { +} + +ReturnValue_t DeviceHandlerBase::enableReplyInReplyMap( + DeviceCommandMap::iterator command, uint8_t expectedReplies, + bool useAlternativeId, DeviceCommandId_t alternativeReply) { + DeviceReplyMap::iterator iter; + if (useAlternativeId) { + iter = deviceReplyMap.find(alternativeReply); + } else { + iter = deviceReplyMap.find(command->first); + } + if (iter != deviceReplyMap.end()) { + DeviceReplyInfo *info = &(iter->second); + info->delayCycles = info->maxDelayCycles; + info->command = command; + command->second.expectedReplies = expectedReplies; + return RETURN_OK; + } else { + return NO_REPLY_EXPECTED; + } +} + +void DeviceHandlerBase::doTransition(Mode_t modeFrom, Submode_t subModeFrom) { + setMode(getBaseMode(mode)); +} + +uint32_t DeviceHandlerBase::getTransitionDelayMs(Mode_t modeFrom, + Mode_t modeTo) { + return 0; +} + +ReturnValue_t DeviceHandlerBase::getStateOfSwitches(void) { + if(powerSwitcher == nullptr) { + return NO_SWITCH; + } + uint8_t numberOfSwitches = 0; + const uint8_t *switches; + + ReturnValue_t result = getSwitches(&switches, &numberOfSwitches); + if ((result == RETURN_OK) && (numberOfSwitches != 0)) { + while (numberOfSwitches > 0) { + if (powerSwitcher->getSwitchState(switches[numberOfSwitches - 1]) + == PowerSwitchIF::SWITCH_OFF) { + return PowerSwitchIF::SWITCH_OFF; + } + numberOfSwitches--; + } + return PowerSwitchIF::SWITCH_ON; + } + + return NO_SWITCH; +} + +Mode_t DeviceHandlerBase::getBaseMode(Mode_t transitionMode) { +//only child action special modes are handled, as a child should never see any base action modes + if (transitionMode == _MODE_START_UP) { + return _MODE_TO_ON; + } + if (transitionMode == _MODE_SHUT_DOWN) { + return _MODE_POWER_DOWN; + } + return transitionMode + & ~(TRANSITION_MODE_BASE_ACTION_MASK + | TRANSITION_MODE_CHILD_ACTION_MASK); +} + +//SHOULDDO: Allow transition from OFF to NORMAL to reduce complexity in assemblies. And, by the way, throw away DHB and write a new one: +// - Include power and thermal completely, but more modular :-) +// - Don't use modes for state transitions, reduce FSM (Finte State Machine) complexity. +// - Modularization? +ReturnValue_t DeviceHandlerBase::checkModeCommand(Mode_t commandedMode, + Submode_t commandedSubmode, uint32_t* msToReachTheMode) { + if (isTransitionalMode()) { + return IN_TRANSITION; + } + if ((mode == MODE_ERROR_ON) && (commandedMode != MODE_OFF)) { + return TRANS_NOT_ALLOWED; + } + if ((commandedMode == MODE_NORMAL) && (mode == MODE_OFF)) { + return TRANS_NOT_ALLOWED; + } + + if ((commandedMode == MODE_ON) && (mode == MODE_OFF) + && (deviceThermalStatePoolId != PoolVariableIF::NO_PARAMETER)) { + DataSet mySet; + db_int8_t thermalState(deviceThermalStatePoolId, &mySet, + PoolVariableIF::VAR_READ); + db_int8_t thermalRequest(deviceThermalRequestPoolId, &mySet, + PoolVariableIF::VAR_READ); + mySet.read(); + if (thermalRequest != ThermalComponentIF::STATE_REQUEST_IGNORE) { + if (!ThermalComponentIF::isOperational(thermalState)) { + triggerEvent(ThermalComponentIF::TEMP_NOT_IN_OP_RANGE, + thermalState); + return NON_OP_TEMPERATURE; + } + } + } + + return isModeCombinationValid(commandedMode, commandedSubmode); +} + +void DeviceHandlerBase::startTransition(Mode_t commandedMode, + Submode_t commandedSubmode) { + switch (commandedMode) { + case MODE_ON: + if (mode == MODE_OFF) { + transitionSourceMode = _MODE_POWER_DOWN; + transitionSourceSubMode = SUBMODE_NONE; + setMode(_MODE_POWER_ON, commandedSubmode); + //already set the delay for the child transition so we don't need to call it twice + childTransitionDelay = getTransitionDelayMs(_MODE_START_UP, + MODE_ON); + triggerEvent(CHANGING_MODE, commandedMode, commandedSubmode); + DataSet mySet; + db_int8_t thermalRequest(deviceThermalRequestPoolId, + &mySet, PoolVariableIF::VAR_READ_WRITE); + mySet.read(); + if (thermalRequest != ThermalComponentIF::STATE_REQUEST_IGNORE) { + thermalRequest = ThermalComponentIF::STATE_REQUEST_OPERATIONAL; + mySet.commit(PoolVariableIF::VALID); + } + } else { + setTransition(MODE_ON, commandedSubmode); + } + break; + case MODE_OFF: + if (mode == MODE_OFF) { + triggerEvent(CHANGING_MODE, commandedMode, commandedSubmode); + setMode(_MODE_POWER_DOWN, commandedSubmode); + } else { + //already set the delay for the child transition so we don't need to call it twice + childTransitionDelay = getTransitionDelayMs(mode, _MODE_POWER_DOWN); + transitionSourceMode = _MODE_POWER_DOWN; + transitionSourceSubMode = commandedSubmode; + childTransitionFailure = CHILD_TIMEOUT; + setMode(_MODE_SHUT_DOWN, commandedSubmode); + triggerEvent(CHANGING_MODE, commandedMode, commandedSubmode); + } + break; + case MODE_RAW: + if (mode != MODE_OFF) { + setTransition(MODE_RAW, commandedSubmode); + } else { + setMode(MODE_RAW, commandedSubmode); + } + break; + case MODE_NORMAL: + if (mode != MODE_OFF) { + setTransition(MODE_NORMAL, commandedSubmode); + } else { + replyReturnvalueToCommand(HasModesIF::TRANS_NOT_ALLOWED); + } + break; + } +} + +void DeviceHandlerBase::getMode(Mode_t* mode, Submode_t* submode) { + *mode = this->mode; + *submode = this->submode; +} + +void DeviceHandlerBase::setToExternalControl() { + healthHelper.setHealth(EXTERNAL_CONTROL); +} + +void DeviceHandlerBase::announceMode(bool recursive) { + triggerEvent(MODE_INFO, mode, submode); +} + +bool DeviceHandlerBase::dontCheckQueue() { + return false; +} + +void DeviceHandlerBase::missedReply(DeviceCommandId_t id) { + if (ignoreMissedRepliesCount > 0) { + ignoreMissedRepliesCount--; + } else { + triggerEvent(DEVICE_MISSED_REPLY, id); + } +} + +HasHealthIF::HealthState DeviceHandlerBase::getHealth() { + return healthHelper.getHealth(); +} + +ReturnValue_t DeviceHandlerBase::setHealth(HealthState health) { + healthHelper.setHealth(health); + return HasReturnvaluesIF::RETURN_OK; +} + +void DeviceHandlerBase::checkSwitchState() { + if ((mode == MODE_ON || mode == MODE_NORMAL)) { + //We only check in ON and NORMAL, ignore RAW and ERROR_ON. + ReturnValue_t result = getStateOfSwitches(); + if (result == PowerSwitchIF::SWITCH_OFF && !switchOffWasReported) { + triggerEvent(PowerSwitchIF::SWITCH_WENT_OFF); + switchOffWasReported = true; + } + } else { + switchOffWasReported = false; + } +} + +void DeviceHandlerBase::doOnActivity() { +} + +ReturnValue_t DeviceHandlerBase::acceptExternalDeviceCommands() { + if ((mode != MODE_ON) && (mode != MODE_NORMAL)) { + return WRONG_MODE_FOR_COMMAND; + } + return RETURN_OK; +} + +void DeviceHandlerBase::replyRawReplyIfnotWiretapped(const uint8_t* data, + size_t len) { + if ((wiretappingMode == RAW) + && (defaultRawReceiver == requestedRawTraffic)) { + //The raw packet was already sent by the wiretapping service + } else { + replyRawData(data, len, defaultRawReceiver); + } +} + +ReturnValue_t DeviceHandlerBase::handleDeviceHandlerMessage( + CommandMessage * message) { + switch (message->getCommand()) { + case DeviceHandlerMessage::CMD_WIRETAPPING: + switch (DeviceHandlerMessage::getWiretappingMode(message)) { + case RAW: + wiretappingMode = RAW; + requestedRawTraffic = commandQueue->getLastPartner(); + break; + case TM: + wiretappingMode = TM; + requestedRawTraffic = commandQueue->getLastPartner(); + break; + case OFF: + wiretappingMode = OFF; + break; + default: + replyReturnvalueToCommand(INVALID_COMMAND_PARAMETER); + wiretappingMode = OFF; + return RETURN_OK; + } + replyReturnvalueToCommand(RETURN_OK); + return RETURN_OK; +// case DeviceHandlerMessage::CMD_SWITCH_IOBOARD: +// if (mode != MODE_OFF) { +// replyReturnvalueToCommand(WRONG_MODE_FOR_COMMAND); +// } else { +// result = switchCookieChannel( +// DeviceHandlerMessage::getIoBoardObjectId(message)); +// if (result == RETURN_OK) { +// replyReturnvalueToCommand(RETURN_OK); +// } else { +// replyReturnvalueToCommand(CANT_SWITCH_IO_ADDRESS); +// } +// } +// return RETURN_OK; + case DeviceHandlerMessage::CMD_RAW: + if ((mode != MODE_RAW)) { + DeviceHandlerMessage::clear(message); + replyReturnvalueToCommand(WRONG_MODE_FOR_COMMAND); + } else { + buildRawDeviceCommand(message); + } + return RETURN_OK; + default: + return RETURN_FAILED; + } +} + +void DeviceHandlerBase::setParentQueue(MessageQueueId_t parentQueueId) { + modeHelper.setParentQueue(parentQueueId); + healthHelper.setParentQueue(parentQueueId); +} + +bool DeviceHandlerBase::isAwaitingReply() { + std::map::iterator iter; + for (iter = deviceReplyMap.begin(); iter != deviceReplyMap.end(); ++iter) { + if (iter->second.delayCycles != 0) { + return true; + } + } + return false; +} + +ReturnValue_t DeviceHandlerBase::letChildHandleMessage( + CommandMessage * message) { + return RETURN_FAILED; +} + +void DeviceHandlerBase::handleDeviceTM(SerializeIF* data, + DeviceCommandId_t replyId, bool neverInDataPool, bool forceDirectTm) { + DeviceReplyMap::iterator iter = deviceReplyMap.find(replyId); + if (iter == deviceReplyMap.end()) { + triggerEvent(DEVICE_UNKNOWN_REPLY, replyId); + return; + } + DeviceTmReportingWrapper wrapper(getObjectId(), replyId, data); + //replies to a command + if (iter->second.command != deviceCommandMap.end()) + { + MessageQueueId_t queueId = iter->second.command->second.sendReplyTo; + + if (queueId != NO_COMMANDER) { + //This may fail, but we'll ignore the fault. + actionHelper.reportData(queueId, replyId, data); + } + + //This check should make sure we get any TM but don't get anything doubled. + if (wiretappingMode == TM && (requestedRawTraffic != queueId)) { + actionHelper.reportData(requestedRawTraffic, replyId, &wrapper); + } + else if (forceDirectTm and (defaultRawReceiver != queueId) and + (defaultRawReceiver != MessageQueueIF::NO_QUEUE)) + { + // hiding of sender needed so the service will handle it as + // unexpected Data, no matter what state (progress or completed) + // it is in + actionHelper.reportData(defaultRawReceiver, replyId, &wrapper, + true); + } + } + //unrequested/aperiodic replies + else + { + if (wiretappingMode == TM) { + actionHelper.reportData(requestedRawTraffic, replyId, &wrapper); + } + else if (forceDirectTm and defaultRawReceiver != + MessageQueueIF::NO_QUEUE) + { + // hiding of sender needed so the service will handle it as + // unexpected Data, no matter what state (progress or completed) + // it is in + actionHelper.reportData(defaultRawReceiver, replyId, &wrapper, + true); + } + } + //Try to cast to GlobDataSet and commit data. + if (!neverInDataPool) { + DataSet* dataSet = dynamic_cast(data); + if (dataSet != NULL) { + dataSet->commit(PoolVariableIF::VALID); + } + } +} + +ReturnValue_t DeviceHandlerBase::executeAction(ActionId_t actionId, + MessageQueueId_t commandedBy, const uint8_t* data, size_t size) { + ReturnValue_t result = acceptExternalDeviceCommands(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + DeviceCommandMap::iterator iter = deviceCommandMap.find(actionId); + if (iter == deviceCommandMap.end()) { + result = COMMAND_NOT_SUPPORTED; + } else if (iter->second.isExecuting) { + result = COMMAND_ALREADY_SENT; + } else { + result = buildCommandFromCommand(actionId, data, size); + } + if (result == RETURN_OK) { + iter->second.sendReplyTo = commandedBy; + iter->second.isExecuting = true; + cookieInfo.pendingCommand = iter; + cookieInfo.state = COOKIE_WRITE_READY; + } + return result; +} + +void DeviceHandlerBase::buildInternalCommand(void) { +//Neither Raw nor Direct could build a command + ReturnValue_t result = NOTHING_TO_SEND; + DeviceCommandId_t deviceCommandId = NO_COMMAND_ID; + if (mode == MODE_NORMAL) { + result = buildNormalDeviceCommand(&deviceCommandId); + if (result == BUSY) { + //so we can track misconfigurations + sif::debug << std::hex << getObjectId() + << ": DHB::buildInternalCommand: Busy" << std::dec << std::endl; + result = NOTHING_TO_SEND; //no need to report this + } + } + else if (mode == MODE_RAW) { + result = buildChildRawCommand(); + deviceCommandId = RAW_COMMAND_ID; + } + else if (mode & TRANSITION_MODE_CHILD_ACTION_MASK) { + result = buildTransitionDeviceCommand(&deviceCommandId); + } + else { + return; + } + + if (result == NOTHING_TO_SEND) { + return; + } + if (result == RETURN_OK) { + DeviceCommandMap::iterator iter = deviceCommandMap.find( + deviceCommandId); + if (iter == deviceCommandMap.end()) { + result = COMMAND_NOT_SUPPORTED; + } else if (iter->second.isExecuting) { + sif::debug << std::hex << getObjectId() + << ": DHB::buildInternalCommand: Command " + << deviceCommandId << " isExecuting" << std::endl; //so we can track misconfigurations + return; //this is an internal command, no need to report a failure here, missed reply will track if a reply is too late, otherwise, it's ok + } else { + iter->second.sendReplyTo = NO_COMMANDER; + iter->second.isExecuting = true; + cookieInfo.pendingCommand = iter; + cookieInfo.state = COOKIE_WRITE_READY; + } + } + if (result != RETURN_OK) { + triggerEvent(DEVICE_BUILDING_COMMAND_FAILED, result, deviceCommandId); + } +} + +ReturnValue_t DeviceHandlerBase::buildChildRawCommand() { + return NOTHING_TO_SEND; +} + +uint8_t DeviceHandlerBase::getReplyDelayCycles( + DeviceCommandId_t deviceCommand) { + DeviceReplyMap::iterator iter = deviceReplyMap.find(deviceCommand); + if (iter == deviceReplyMap.end()) { + return 0; + } + return iter->second.delayCycles; +} + +Mode_t DeviceHandlerBase::getTransitionSourceMode() const { + return transitionSourceMode; +} + +Submode_t DeviceHandlerBase::getTransitionSourceSubMode() const { + return transitionSourceSubMode; +} + +void DeviceHandlerBase::triggerEvent(Event event, uint32_t parameter1, + uint32_t parameter2) { + fdirInstance->triggerEvent(event, parameter1, parameter2); +} + +void DeviceHandlerBase::forwardEvent(Event event, uint32_t parameter1, + uint32_t parameter2) const { + fdirInstance->triggerEvent(event, parameter1, parameter2); +} + +void DeviceHandlerBase::doOffActivity() { +} + +ReturnValue_t DeviceHandlerBase::getParameter(uint8_t domainId, + uint16_t parameterId, ParameterWrapper* parameterWrapper, + const ParameterWrapper* newValues, uint16_t startAtIndex) { + ReturnValue_t result = fdirInstance->getParameter(domainId, parameterId, + parameterWrapper, newValues, startAtIndex); + if (result != INVALID_DOMAIN_ID) { + return result; + } + return INVALID_DOMAIN_ID; + +} + +bool DeviceHandlerBase::isTransitionalMode() { + return ((mode + & (TRANSITION_MODE_BASE_ACTION_MASK + | TRANSITION_MODE_CHILD_ACTION_MASK)) != 0); +} + +bool DeviceHandlerBase::commandIsExecuting(DeviceCommandId_t commandId) { + auto iter = deviceCommandMap.find(commandId); + if (iter != deviceCommandMap.end()) { + return iter->second.isExecuting; + } else { + return false; + } + +} + +void DeviceHandlerBase::changeHK(Mode_t mode, Submode_t submode, bool enable) { +} + +void DeviceHandlerBase::setTaskIF(PeriodicTaskIF* task_){ + executingTask = task_; +} + +// Default implementations empty. +void DeviceHandlerBase::debugInterface(uint8_t positionTracker, + object_id_t objectId, uint32_t parameter) {} + +void DeviceHandlerBase::performOperationHook() { +} + +ReturnValue_t DeviceHandlerBase::initializeAfterTaskCreation() { + // In this function, the task handle should be valid if the task + // was implemented correctly. We still check to be 1000 % sure :-) + if(executingTask != nullptr) { + pstIntervalMs = executingTask->getPeriodMs(); + } + return HasReturnvaluesIF::RETURN_OK; +} + diff --git a/fsfw/devicehandlers/DeviceHandlerBase.h b/fsfw/devicehandlers/DeviceHandlerBase.h new file mode 100644 index 0000000..5666f35 --- /dev/null +++ b/fsfw/devicehandlers/DeviceHandlerBase.h @@ -0,0 +1,1194 @@ +#ifndef FRAMEWORK_DEVICEHANDLERS_DEVICEHANDLERBASE_H_ +#define FRAMEWORK_DEVICEHANDLERS_DEVICEHANDLERBASE_H_ + +#include "DeviceHandlerIF.h" +#include "DeviceCommunicationIF.h" +#include "DeviceHandlerFailureIsolation.h" + +#include "../objectmanager/SystemObject.h" +#include "../tasks/PeriodicTaskIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../action/HasActionsIF.h" +#include "../datapool/PoolVariableIF.h" +#include "../modes/HasModesIF.h" +#include "../power/PowerSwitchIF.h" +#include "../ipc/MessageQueueIF.h" +#include "../action/ActionHelper.h" +#include "../health/HealthHelper.h" +#include "../parameters/ParameterHelper.h" +#include "../datapool/HkSwitchHelper.h" + +#include + +namespace Factory{ +void setStaticFrameworkObjectIds(); +} + +class StorageManagerIF; + +/** + * @defgroup devices Devices + * Contains all devices and the DeviceHandlerBase class. + */ + +/** + * @brief This is the abstract base class for device handlers. + * @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 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. + * 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. + * + * 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 and contain information about the communcation (e.g. slave + * address for I2C or RMAP structs). + * The following abstract methods must be implemented by a device handler: + * 1. doStartUp() + * 2. doShutDown() + * 3. buildTransitionDeviceCommand() + * 4. buildNormalDeviceCommand() + * 5. buildCommandFromCommand() + * 6. fillCommandAndReplyMap() + * 7. scanForReply() + * 8. interpretDeviceReply() + * + * Other important virtual methods with a default implementation + * are the getTransitionDelayMs() function and the getSwitches() function. + * Please ensure that getSwitches() returns DeviceHandlerIF::NO_SWITCHES if + * power switches are not implemented yet. Otherwise, the device handler will + * not transition to MODE_ON, even if setMode(MODE_ON) is called. + * If a transition to MODE_ON is desired without commanding, override the + * intialize() function and call setMode(_MODE_START_UP) before calling + * DeviceHandlerBase::initialize(). + * + * @ingroup devices + */ +class DeviceHandlerBase: public DeviceHandlerIF, + public HasReturnvaluesIF, + public ExecutableObjectIF, + public SystemObject, + public HasModesIF, + public HasHealthIF, + public HasActionsIF, + public ReceivesParameterMessagesIF { + friend void (Factory::setStaticFrameworkObjectIds)(); +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 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 + * @param thermalRequestPoolId + * @param fdirInstance + * @param cmdQueueSize + */ + DeviceHandlerBase(object_id_t setObjectId, object_id_t deviceCommunication, + CookieIF * comCookie, FailureIsolationBase* fdirInstance = nullptr, + size_t cmdQueueSize = 20); + + void setThermalStateRequestPoolIds(uint32_t thermalStatePoolId, + uint32_t thermalRequestPoolId); + + /** + * @brief This function is the device handler base core component and is + * called periodically. + * @details + * General sequence, showing where abstract virtual functions are called: + * If the State is SEND_WRITE: + * 1. Set the cookie state to COOKIE_UNUSED and read the command queue + * 2. Handles Device State Modes by calling doStateMachine(). + * This function calls callChildStatemachine() which calls the + * abstract functions doStartUp() and doShutDown() + * 3. Check switch states by calling checkSwitchStates() + * 4. Decrements counter for timeout of replies by calling + * decrementDeviceReplyMap() + * 5. Performs FDIR check for failures + * 6. Calls hkSwitcher.performOperation() + * 7. If the device mode is MODE_OFF, return RETURN_OK. + * Otherwise, perform the Action property and performs depending + * on value specified by input value counter (incremented in PST). + * The child class tells base class what to do by setting this value. + * - SEND_WRITE: Send data or commands to device by calling + * doSendWrite() which calls sendMessage function + * of #communicationInterface + * and calls buildInternalCommand if the cookie state is COOKIE_UNUSED + * - GET_WRITE: Get ackknowledgement for sending by calling doGetWrite() + * which calls getSendSuccess of #communicationInterface. + * Calls abstract functions scanForReply() and interpretDeviceReply(). + * - SEND_READ: Request reading data from device by calling doSendRead() + * which calls requestReceiveMessage of #communcationInterface + * - GET_READ: Access requested reading data by calling doGetRead() + * which calls readReceivedMessage of #communicationInterface + * @param counter Specifies which Action to perform + * @return RETURN_OK for successful execution + */ + virtual ReturnValue_t performOperation(uint8_t counter); + + /** + * @brief Initializes the device handler + * @details + * Initialize Device Handler as system object and + * initializes all important helper classes. + * Calls fillCommandAndReplyMap(). + * @return + */ + virtual ReturnValue_t initialize(); + /** Destructor. */ + virtual ~DeviceHandlerBase(); + +protected: + /** + * @brief This is used to let the child class handle the transition from + * mode @c _MODE_START_UP to @c MODE_ON + * @details + * It is only called when the device handler is in mode @c _MODE_START_UP. + * That means, the device switch(es) are already set to on. + * Device handler commands are read and can be handled by the child class. + * If the child class handles a command, it should also send + * an reply accordingly. + * If an Command is not handled (ie #DeviceHandlerCommand is not @c CMD_NONE, + * the base class handles rejecting the command and sends a reply. + * The replies for mode transitions are handled by the base class. + * + * - If the device is started and ready for operation, the mode should be + * set to MODE_ON. It is possible to set the mode to _MODE_TO_ON to + * use the to on transition if available. + * - If the power-up fails, the mode should be set to _MODE_POWER_DOWN + * which will lead to the device being powered off. + * - If the device does not change the mode, the mode will be changed + * to _MODE_POWER_DOWN, after the timeout (from getTransitionDelay()) + * has passed. + * + * #transitionFailure can be set to a failure code indicating the reason + * for a failed transition + */ + virtual void doStartUp() = 0; + + /** + * @brief This is used to let the child class handle the transition + * from mode @c _MODE_SHUT_DOWN to @c _MODE_POWER_DOWN + * @details + * It is only called when the device handler is in mode @c _MODE_SHUT_DOWN. + * Device handler commands are read and can be handled by the child class. + * If the child class handles a command, it should also send an reply + * accordingly. + * If an Command is not handled (ie #DeviceHandlerCommand is not + * @c CMD_NONE, the base class handles rejecting the command and sends a + * reply. The replies for mode transitions are handled by the base class. + * + * - If the device ready to be switched off, + * the mode should be set to _MODE_POWER_DOWN. + * - If the device should not be switched off, the mode can be changed to + * _MODE_TO_ON (or MODE_ON if no transition is needed). + * - If the device does not change the mode, the mode will be changed to + * _MODE_POWER_DOWN, when the timeout (from getTransitionDelay()) + * has passed. + * + * #transitionFailure can be set to a failure code indicating the reason + * for a failed transition + */ + virtual void doShutDown() = 0; + + /** + * Build the device command to send for normal mode. + * + * This is only called in @c MODE_NORMAL. If multiple submodes for + * @c MODE_NORMAL are supported, different commands can built, + * depending on the submode. + * + * #rawPacket and #rawPacketLen must be set by this method to the + * packet to be sent. If variable command frequence is required, a counter + * can be used and the frequency in the reply map has to be set manually + * by calling updateReplyMap(). + * + * @param[out] id the device command id that has been built + * @return + * - @c RETURN_OK to send command after setting #rawPacket and #rawPacketLen. + * - @c NOTHING_TO_SEND when no command is to be sent. + * - Anything else triggers an even with the returnvalue as a parameter. + */ + virtual ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t * id) = 0; + + /** + * Build the device command to send for a transitional mode. + * + * This is only called in @c _MODE_TO_NORMAL, @c _MODE_TO_ON, @c _MODE_TO_RAW, + * @c _MODE_START_UP and @c _MODE_SHUT_DOWN. So it is used by doStartUp() + * and doShutDown() as well as doTransition(), by setting those + * modes in the respective functions. + * + * A good idea is to implement a flag indicating a command has to be built + * and a variable containing the command number to be built + * and filling them in doStartUp(), doShutDown() and doTransition() so no + * modes have to be checked here. + * + * #rawPacket and #rawPacketLen must be set by this method to the packet to be sent. + * + * @param[out] id the device command id built + * @return + * - @c RETURN_OK when a command is to be sent + * - @c NOTHING_TO_SEND when no command is to be sent + * - Anything else triggers an even with the returnvalue as a parameter + */ + virtual ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t * id) = 0; + + /** + * @brief Build a device command packet from data supplied by a direct command. + * + * @details + * #rawPacket and #rawPacketLen should be set by this method to the packet to be sent. + * The existence of the command in the command map and the command size check + * against 0 are done by the base class. + * + * @param deviceCommand the command to build, already checked against deviceCommandMap + * @param commandData pointer to the data from the direct command + * @param commandDataLen length of commandData + * @return + * - @c RETURN_OK to send command after #rawPacket and #rawPacketLen have been set. + * - Anything else triggers an event with the returnvalue as a parameter + */ + virtual ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, + const uint8_t * commandData, size_t commandDataLen) = 0; + + /** + * @brief Scans a buffer for a valid reply. + * @details + * This is used by the base class to check the data received for valid packets. + * It only checks if a valid packet starts at @c start. + * It also only checks the structural validy of the packet, + * e.g. checksums lengths and protocol data. No information check is done, + * e.g. range checks etc. + * + * Errors should be reported directly, the base class does NOT report any + * errors based on the return value of this function. + * + * @param start start of remaining buffer to be scanned + * @param len length of remaining buffer to be scanned + * @param[out] foundId the id of the data found in the buffer. + * @param[out] foundLen length of the data found. Is to be set in function, + * buffer is scanned at previous position + foundLen. + * @return + * - @c RETURN_OK a valid packet was found at @c start, @c foundLen is valid + * - @c RETURN_FAILED no reply could be found starting at @c start, + * implies @c foundLen is not valid, base class will call scanForReply() + * again with ++start + * - @c DeviceHandlerIF::INVALID_DATA a packet was found but it is invalid, + * e.g. checksum error, implies @c foundLen is valid, can be used to + * skip some bytes + * - @c DeviceHandlerIF::LENGTH_MISSMATCH @c len is invalid + * - @c DeviceHandlerIF::IGNORE_REPLY_DATA Ignore this specific part of + * the packet + * - @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, size_t len, + DeviceCommandId_t *foundId, size_t *foundLen) = 0; + + /** + * @brief Interpret a reply from the device. + * @details + * This is called after scanForReply() found a valid packet, it can be + * assumed that the length and structure is valid. + * This routine extracts the data from the packet into a DataSet and then + * calls handleDeviceTM(), which either sends a TM packet or stores the + * data in the DataPool depending on whether it was an external command. + * No packet length is given, as it should be defined implicitly by the id. + * + * @param id the id found by scanForReply() + * @param packet + * @return + * - @c RETURN_OK when the reply was interpreted. + * - @c RETURN_FAILED when the reply could not be interpreted, + * e.g. logical errors or range violations occurred + */ + virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, + const uint8_t *packet) = 0; + + /** + * @brief fill the #DeviceCommandMap and #DeviceReplyMap + * called by the initialize() of the base class + * @details + * This is used to let the base class know which replies are expected. + * There are different scenarios regarding this: + * + * - "Normal" commands. These are commands, that trigger a direct reply + * from the device. In this case, the id of the command should be added + * to the command map with a commandData_t where maxDelayCycles is set + * to the maximum expected number of PST cycles the reply will take. + * Then, scanForReply returns the id of the command and the base class + * can handle time-out and missing replies. + * + * - Periodic, unrequested replies. These are replies that, once enabled, + * are sent by the device on its own in a defined interval. + * In this case, the id of the reply or a placeholder id should be added + * to the deviceCommandMap with a commandData_t where maxDelayCycles is + * set to the maximum expected number of PST cycles between two replies + * (also a tolerance should be added, as an FDIR message will be + * generated if it is missed). + * + * (Robin) This part confuses me. "must do as soon as" implies that + * the developer must do something somewhere else in the code. Is + * that really the case? If I understood correctly, DHB performs + * almost everything (e.g. in erirm function) as long as the commands + * are inserted correctly. + * + * As soon as the replies are enabled, DeviceCommandInfo.periodic must + * be set to true, DeviceCommandInfo.delayCycles to + * DeviceCommandInfo.maxDelayCycles. + * From then on, the base class handles the reception. + * Then, scanForReply returns the id of the reply or the placeholder id + * and the base class will take care of checking that all replies are + * received and the interval is correct. + * When the replies are disabled, DeviceCommandInfo.periodic must be set + * to 0, DeviceCommandInfo.delayCycles to 0; + * + * - Aperiodic, unrequested replies. These are replies that are sent + * by the device without any preceding command and not in a defined + * interval. These are not entered in the deviceCommandMap but + * handled by returning @c APERIODIC_REPLY in scanForReply(). + */ + virtual void fillCommandAndReplyMap() = 0; + + /** + * 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. Default is aperiodic (0) + * @return - @c RETURN_OK when the command was successfully inserted, + * - @c RETURN_FAILED else. + */ + ReturnValue_t insertInCommandAndReplyMap(DeviceCommandId_t deviceCommand, + uint16_t maxDelayCycles, size_t replyLen = 0, bool periodic = false, + bool hasDifferentReplyId = false, DeviceCommandId_t replyId = 0); + + /** + * @brief This is a helper method to insert replies in the reply map. + * @param deviceCommand Identifier of the reply to add. + * @param maxDelayCycles The maximum number of delay cycles the reply 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. Default is aperiodic (0) + * @return - @c RETURN_OK when the command was successfully inserted, + * - @c RETURN_FAILED else. + */ + ReturnValue_t insertInReplyMap(DeviceCommandId_t deviceCommand, + uint16_t maxDelayCycles, size_t replyLen = 0, bool periodic = false); + + /** + * @brief A simple command to add a command to the commandList. + * @param deviceCommand The command to add + * @return - @c RETURN_OK when the command was successfully inserted, + * - @c RETURN_FAILED else. + */ + ReturnValue_t insertInCommandMap(DeviceCommandId_t deviceCommand); + /** + * @brief This is a helper method to facilitate updating entries + * in the reply map. + * @param deviceCommand Identifier of the reply to update. + * @param delayCycles The current number of delay cycles to wait. + * As stated in #fillCommandAndCookieMap, to disable periodic commands, + * this is set to zero. + * @param maxDelayCycles The maximum number of delay cycles the reply waits + * until it times out. By passing 0 the entry remains untouched. + * @param periodic Indicates if the command is periodic (i.e. it is sent + * by the device repeatedly without request) or not.Default is aperiodic (0). + * Warning: The setting always overrides the value that was entered in the map. + * @return - @c RETURN_OK when the command was successfully inserted, + * - @c RETURN_FAILED else. + */ + ReturnValue_t updateReplyMapEntry(DeviceCommandId_t deviceReply, + uint16_t delayCycles, uint16_t maxDelayCycles, + bool periodic = false); + + /** + * @brief Can be implemented by child handler to + * perform debugging + * @details Example: Calling this in performOperation + * to track values like mode. + * @param positionTracker Provide the child handler a way to know + * where the debugInterface was called + * @param objectId Provide the child handler object Id to + * specify actions for spefic devices + * @param parameter Supply a parameter of interest + * Please delete all debugInterface calls in DHB after debugging is finished ! + */ + virtual void debugInterface(uint8_t positionTracker = 0, + object_id_t objectId = 0, uint32_t parameter = 0); + + /** + * Get the time needed to transit from modeFrom to modeTo. + * + * Used for the following transitions: + * modeFrom -> modeTo: + * MODE_ON -> [MODE_ON, MODE_NORMAL, MODE_RAW, _MODE_POWER_DOWN] + * MODE_NORMAL -> [MODE_ON, MODE_NORMAL, MODE_RAW, _MODE_POWER_DOWN] + * MODE_RAW -> [MODE_ON, MODE_NORMAL, MODE_RAW, _MODE_POWER_DOWN] + * _MODE_START_UP -> MODE_ON (do not include time to set the switches, + * the base class got you covered) + * + * The default implementation returns 0 ! + * @param modeFrom + * @param modeTo + * @return time in ms + */ + virtual uint32_t getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo); + + /** + * Return the switches connected to the device. + * + * The default implementation returns one switch set in the ctor. + * + * @param[out] switches pointer to an array of switches + * @param[out] numberOfSwitches length of returned array + * @return + * - @c RETURN_OK if the parameters were set + * - @c RETURN_FAILED if no switches exist + */ + virtual ReturnValue_t getSwitches(const uint8_t **switches, + uint8_t *numberOfSwitches); + + /** + * This function is used to initialize the local housekeeping pool + * entries. The default implementation leaves the pool empty. + * @param localDataPoolMap + * @return + */ + //virtual ReturnValue_t initializePoolEntries( + // LocalDataPool& localDataPoolMap) override; + + /** Get the HK manager object handle */ + //virtual LocalDataPoolManager* getHkManagerHandle() override; + + /** + * @brief Hook function for child handlers which is called once per + * performOperation(). Default implementation is empty. + */ + virtual void performOperationHook(); +public: + /** + * @param parentQueueId + */ + virtual void setParentQueue(MessageQueueId_t parentQueueId); + + /** @brief Implementation required for HasActionIF */ + ReturnValue_t executeAction(ActionId_t actionId, + MessageQueueId_t commandedBy, const uint8_t* data, + size_t size) override; + + Mode_t getTransitionSourceMode() const; + Submode_t getTransitionSourceSubMode() const; + virtual void getMode(Mode_t *mode, Submode_t *submode); + HealthState getHealth(); + ReturnValue_t setHealth(HealthState health); + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex) override; + /** + * Implementation of ExecutableObjectIF function + * + * Used to setup the reference of the task, that executes this component + * @param task_ Pointer to the taskIF of this task + */ + virtual void setTaskIF(PeriodicTaskIF* task_); + virtual MessageQueueId_t getCommandQueue(void) const; + +protected: + /** + * The Returnvalues id of this class, required by HasReturnvaluesIF + */ + static const uint8_t INTERFACE_ID = CLASS_ID::DEVICE_HANDLER_BASE; + + static const ReturnValue_t INVALID_CHANNEL = MAKE_RETURN_CODE(0xA0); + // Returnvalues for scanForReply() + static const ReturnValue_t APERIODIC_REPLY = MAKE_RETURN_CODE(0xB0); //!< This is used to specify for replies from a device which are not replies to requests + static const ReturnValue_t IGNORE_REPLY_DATA = MAKE_RETURN_CODE(0xB1); //!< Ignore parts of the received packet + static const ReturnValue_t IGNORE_FULL_PACKET = MAKE_RETURN_CODE(0xB2); //!< Ignore full received packet + // Returnvalues for command building + static const ReturnValue_t NOTHING_TO_SEND = MAKE_RETURN_CODE(0xC0); //!< Return this if no command sending in required + static const ReturnValue_t COMMAND_MAP_ERROR = MAKE_RETURN_CODE(0xC2); + // Returnvalues for getSwitches() + static const ReturnValue_t NO_SWITCH = MAKE_RETURN_CODE(0xD0); + // Mode handling error Codes + static const ReturnValue_t CHILD_TIMEOUT = MAKE_RETURN_CODE(0xE0); + static const ReturnValue_t SWITCH_FAILED = MAKE_RETURN_CODE(0xE1); + + static const DeviceCommandId_t RAW_COMMAND_ID = -1; + static const DeviceCommandId_t NO_COMMAND_ID = -2; + static const MessageQueueId_t NO_COMMANDER = 0; + + /** Pointer to the raw packet that will be sent.*/ + uint8_t *rawPacket = nullptr; + /** Size of the #rawPacket. */ + uint32_t rawPacketLen = 0; + + /** + * The mode the device handler is currently in. + * This should never be changed directly but only with setMode() + */ + Mode_t mode; + /** + * The submode the device handler is currently in. + * This should never be changed directly but only with setMode() + */ + Submode_t submode; + + /** This is the counter value from performOperation(). */ + uint8_t pstStep = 0; + uint32_t pstIntervalMs = 0; + + /** + * Wiretapping flag: + * + * indicates either that all raw messages to and from the device should be + * sent to #defaultRawReceiver + * or that all device TM should be downlinked to #defaultRawReceiver. + */ + enum WiretappingMode { + OFF = 0, RAW = 1, TM = 2 + } wiretappingMode; + /** + * @brief A message queue that accepts raw replies + * + * Statically initialized in initialize() to a configurable object. + * Used when there is no method of finding a recipient, ie raw mode and + * reporting erroneous replies + */ + MessageQueueId_t defaultRawReceiver = MessageQueueIF::NO_QUEUE; + store_address_t storedRawData; + + /** + * @brief The message queue which wants to read all raw traffic + * If #isWiretappingActive all raw communication from and to the device + * will be sent to this queue + */ + MessageQueueId_t requestedRawTraffic = 0; + + /** + * Pointer to the IPCStore. + * This caches the pointer received from the objectManager in the constructor. + */ + StorageManagerIF *IPCStore = nullptr; + /** The comIF object ID is cached for the intialize() function */ + object_id_t deviceCommunicationId; + /** Communication object used for device communication */ + DeviceCommunicationIF * communicationInterface = nullptr; + /** Cookie used for communication */ + CookieIF * comCookie; + + /** Health helper for HasHealthIF */ + HealthHelper healthHelper; + /** Mode helper for HasModesIF */ + ModeHelper modeHelper; + /** Parameter helper for ReceivesParameterMessagesIF */ + ParameterHelper parameterHelper; + /** Action helper for HasActionsIF */ + ActionHelper actionHelper; + /** Housekeeping Manager */ + //LocalDataPoolManager hkManager; + + /** + * @brief Information about commands + */ + struct DeviceCommandInfo { + //! Indicates if the command is already executing. + bool isExecuting; + //! Dynamic value to indicate how many replies are expected. + //! Inititated with 0. + uint8_t expectedReplies; + //! if this is != NO_COMMANDER, DHB was commanded externally and shall + //! report everything to commander. + MessageQueueId_t sendReplyTo; + }; + using DeviceCommandMap = std::map ; + /** + * Information about commands + */ + DeviceCommandMap deviceCommandMap; + + /** + * @brief Information about expected replies + * This is used to keep track of pending replies. + */ + struct DeviceReplyInfo { + //! The maximum number of cycles the handler should wait for a reply + //! to this command. + uint16_t maxDelayCycles; + //! The currently remaining cycles the handler should wait for a reply, + //! 0 means there is no reply expected + uint16_t delayCycles; + size_t replyLen = 0; //!< Expected size of the reply. + //! if this is !=0, the delayCycles will not be reset to 0 but to + //! maxDelayCycles + bool periodic = false; + //! The dataset used to access housekeeping data related to the + //! respective device reply. Will point to a dataset held by + //! the child handler (if one is specified) + // DataSetIF* dataSet = nullptr; + //! The command that expects this reply. + DeviceCommandMap::iterator command; + }; + + using DeviceReplyMap = std::map ; + using DeviceReplyIter = DeviceReplyMap::iterator; + /** + * This map is used to check and track correct reception of all replies. + * + * It has multiple use: + * - It stores the information on pending replies. If a command is sent, + * the DeviceCommandInfo.count is incremented. + * - It is used to time-out missing replies. If a command is sent, the + * DeviceCommandInfo.DelayCycles is set to MaxDelayCycles. + * - It is queried to check if a reply from the device can be interpreted. + * scanForReply() returns the id of the command a reply was found for. + * The reply is ignored in the following cases: + * - No entry for the returned id was found + * - The deviceReplyInfo.delayCycles is == 0 + */ + DeviceReplyMap deviceReplyMap; + + //! The MessageQueue used to receive device handler commands + //! and to send replies. + MessageQueueIF* commandQueue = nullptr; + + /** + * this is the datapool variable with the thermal state of the device + * + * can be set to PoolVariableIF::NO_PARAMETER to deactivate thermal checking + */ + uint32_t deviceThermalStatePoolId = PoolVariableIF::NO_PARAMETER; + + /** + * this is the datapool variable with the thermal request of the device + * + * can be set to PoolVariableIF::NO_PARAMETER to deactivate thermal checking + */ + uint32_t deviceThermalRequestPoolId = PoolVariableIF::NO_PARAMETER; + + /** + * Optional Error code + * Can be set in doStartUp(), doShutDown() and doTransition() to signal cause for Transition failure. + */ + ReturnValue_t childTransitionFailure; + + 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. + + HkSwitchHelper hkSwitcher; + + bool defaultFDIRUsed; //!< To correctly delete the default instance. + + bool switchOffWasReported; //!< Indicates if SWITCH_WENT_OFF was already thrown. + + //! Pointer to the task which executes this component, is invalid + //! before setTaskIF was called. + PeriodicTaskIF* executingTask = nullptr; + + static object_id_t powerSwitcherId; //!< Object which switches power on and off. + + 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. + /** + * Helper function to report a missed reply + * + * Can be overwritten by children to act on missed replies or to fake reporting Id. + * + * @param id of the missed reply + */ + virtual void missedReply(DeviceCommandId_t id); + + /** + * Send a reply to a received device handler command. + * + * This also resets #DeviceHandlerCommand to 0. + * + * @param reply the reply type + * @param parameter parameter for the reply + */ + void replyReturnvalueToCommand(ReturnValue_t status, + uint32_t parameter = 0); + + 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). + * + * If the transition is complete, the mode should be set to the target mode, + * which can be deduced from the current mode which is + * [_MODE_TO_ON, _MODE_TO_NORMAL, _MODE_TO_RAW] + * + * The intended target submode is already set. + * The origin submode can be read in subModeFrom. + * + * If the transition can not be completed, the child class can try to reach + * an working mode by setting the mode either directly + * or setting the mode to an transitional mode (TO_ON, TO_NORMAL, TO_RAW) + * if the device needs to be reconfigured. + * + * If nothing works, the child class can wait for the timeout and the base + * class will reset the mode to the mode where the transition + * originated from (the child should report the reason for the failed transition). + * + * The intended way to send commands is to set a flag (enum) indicating + * which command is to be sent here and then to check in + * buildTransitionCommand() for the flag. This flag can also be used by + * doStartUp() and doShutDown() to get a nice and clean implementation of + * buildTransitionCommand() without switching through modes. + * + * When the the condition for the completion of the transition is met, the + * mode can be set, for example in the scanForReply() function. + * + * The default implementation goes into the target mode directly. + * + * #transitionFailure can be set to a failure code indicating the reason + * for a failed transition + * + * @param modeFrom + * The mode the transition originated from: + * [MODE_ON, MODE_NORMAL, MODE_RAW and _MODE_POWER_DOWN (if the mode changed + * from _MODE_START_UP to _MODE_TO_ON)] + * @param subModeFrom the subMode of modeFrom + */ + virtual void doTransition(Mode_t modeFrom, Submode_t subModeFrom); + + /** + * Is the combination of mode and submode valid? + * + * @param mode + * @param submode + * @return + * - @c RETURN_OK if valid + * - @c RETURN_FAILED if invalid + */ + virtual ReturnValue_t isModeCombinationValid(Mode_t mode, + Submode_t submode); + + /** + * Get the Rmap action for the current step. + * + * The step number can be read from #pstStep. + * + * @return The Rmap action to execute in this step + */ + + virtual CommunicationAction_t getComAction(); + + /** + * Build the device command to send for raw mode. + * + * This is only called in @c MODE_RAW. It is for the rare case that in raw mode packets + * are to be sent by the handler itself. It is NOT needed for the raw commanding service. + * Its only current use is in the STR handler which gets its raw packets from a different + * source. + * Also it can be used for transitional commands, to get the device ready for @c MODE_RAW + * + * As it is almost never used, there is a default implementation returning @c NOTHING_TO_SEND. + * + * #rawPacket and #rawPacketLen must be set by this method to the packet to be sent. + * + * @param[out] id the device command id built + * @return + * - @c RETURN_OK when a command is to be sent + * - not @c NOTHING_TO_SEND when no command is to be sent + */ + virtual ReturnValue_t buildChildRawCommand(); + + /** + * Returns the delay cycle count of a reply. + * A count != 0 indicates that the command is already executed. + * @param deviceCommand The command to look for + * @return The current delay count. If the command does not exist (should never happen) it returns 0. + */ + uint8_t getReplyDelayCycles(DeviceCommandId_t deviceCommand); + + /** + * Construct a command reply containing a raw reply. + * + * It gets space in the #IPCStore, copies data there, then sends a raw reply + * containing the store address. + * + * This method is virtual, as the STR has a different channel to send raw replies + * and overwrites it accordingly. + * + * @param data data to send + * @param len length of @c data + * @param sendTo the messageQueueId of the one to send to + * @param isCommand marks the raw data as a command, the message then will be of type raw_command + */ + virtual void replyRawData(const uint8_t *data, size_t len, + MessageQueueId_t sendTo, bool isCommand = false); + + /** + * Calls replyRawData() with #defaultRawReceiver, but checks if wiretapping is active and if so, + * does not send the Data as the wiretapping will have sent it already + */ + void replyRawReplyIfnotWiretapped(const uint8_t *data, size_t len); + + /** + * notify child about mode change + */ + virtual void modeChanged(void); + + /** + * Enable the reply checking for a command + * + * Is only called, if the command was sent (ie the getWriteReply was successful). + * Must ensure that all replies are activated and correctly linked to the command that initiated it. + * The default implementation looks for a reply with the same id as the command id in the replyMap or + * uses the alternativeReplyId if flagged so. + * When found, copies maxDelayCycles to delayCycles in the reply information and sets the command to + * expect one reply. + * + * Can be overwritten by the child, if a command activates multiple replies + * or replyId differs from commandId. + * Notes for child implementations: + * - If the command was not found in the reply map, NO_REPLY_EXPECTED MUST be returned. + * - A failure code may be returned if something went fundamentally wrong. + * + * @param deviceCommand + * @return - RETURN_OK if a reply was activated. + * - NO_REPLY_EXPECTED if there was no reply found. This is not an + * error case as many commands do not expect a reply. + */ + virtual ReturnValue_t enableReplyInReplyMap(DeviceCommandMap::iterator cmd, + uint8_t expectedReplies = 1, bool useAlternateId = false, + DeviceCommandId_t alternateReplyID = 0); + + /** + * get the state of the PCDU switches in the datapool + * + * @return + * - @c PowerSwitchIF::SWITCH_ON if all switches specified by #switches are on + * - @c PowerSwitchIF::SWITCH_OFF one of the switches specified by #switches are off + * - @c PowerSwitchIF::RETURN_FAILED if an error occured + */ + 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 + */ + virtual void changeHK(Mode_t mode, Submode_t submode, bool enable); + + /** + * Children can overwrite this function to suppress checking of the command Queue + * + * This can be used when the child does not want to receive a command in a certain + * situation. Care must be taken that checking is not permanentely disabled as this + * would render the handler unusable. + * + * @return whether checking the queue should NOT be done + */ + virtual bool dontCheckQueue(); + + Mode_t getBaseMode(Mode_t transitionMode); + + bool isAwaitingReply(); + + void handleDeviceTM(SerializeIF *dataSet, DeviceCommandId_t commandId, + bool neverInDataPool = false, bool forceDirectTm = false); + + virtual ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, + uint32_t *msToReachTheMode); + virtual void startTransition(Mode_t mode, Submode_t submode); + virtual void setToExternalControl(); + virtual void announceMode(bool recursive); + + virtual ReturnValue_t letChildHandleMessage(CommandMessage *message); + + /** + * Overwrites SystemObject::triggerEvent in order to inform FDIR"Helper" faster about executed events. + * This is a bit sneaky, but improves responsiveness of the device FDIR. + * @param event The event to be thrown + * @param parameter1 Optional parameter 1 + * @param parameter2 Optional parameter 2 + */ + void triggerEvent(Event event, uint32_t parameter1 = 0, + uint32_t parameter2 = 0); + /** + * Same as triggerEvent, but for forwarding if object is used as proxy. + */ + virtual void forwardEvent(Event event, uint32_t parameter1 = 0, + uint32_t parameter2 = 0) const; + /** + * Checks state of switches in conjunction with mode and triggers an event if they don't fit. + */ + virtual void checkSwitchState(); + + /** + * Reserved for the rare case where a device needs to perform additional operation cyclically in OFF mode. + */ + virtual void doOffActivity(); + + /** + * Reserved for the rare case where a device needs to perform additional operation cyclically in ON mode. + */ + virtual void doOnActivity(); + + /** + * Checks if current mode is transitional mode. + * @return true if mode is transitional, false else. + */ + bool isTransitionalMode(); + + /** + * Checks if current handler state allows reception of external device commands. + * Default implementation allows commands only in plain MODE_ON and MODE_NORMAL. + * @return RETURN_OK if commands are accepted, anything else otherwise. + */ + virtual ReturnValue_t acceptExternalDeviceCommands(); + + bool commandIsExecuting(DeviceCommandId_t commandId); + + /** + * set all switches returned by getSwitches() + * + * @param onOff on == @c SWITCH_ON; off != @c SWITCH_ON + */ + void commandSwitch(ReturnValue_t onOff); +private: + + /** + * State a cookie is in. + * + * Used to keep track of the state of the RMAP communication. + */ + enum CookieState_t { + COOKIE_UNUSED, //!< The Cookie is unused + COOKIE_WRITE_READY, //!< There's data available to send. + COOKIE_READ_SENT, //!< A sendRead command was sent with this cookie + COOKIE_WRITE_SENT //!< A sendWrite command was sent with this cookie + }; + /** + * Information about a cookie. + * + * This is stored in a map for each cookie, to not only track the state, but also information + * about the sent command. Tracking this information is needed as + * the state of a commandId (waiting for reply) is done when a RMAP write reply is received. + */ + struct CookieInfo { + CookieState_t state; + DeviceCommandMap::iterator pendingCommand; + }; + + /** + * @brief Info about the #cookie + * Used to track the state of the communication + */ + CookieInfo cookieInfo; + + /** the object used to set power switches */ + PowerSwitchIF *powerSwitcher = nullptr; + + /** + * @brief Used for timing out mode transitions. + * Set when setMode() is called. + */ + uint32_t timeoutStart = 0; + + /** + * Delay for the current mode transition, used for time out + */ + uint32_t childTransitionDelay; + + /** + * @brief The mode the current transition originated from + * + * This is private so the child can not change it and fuck up the timeouts + * + * IMPORTANT: This is not valid during _MODE_SHUT_DOWN and _MODE_START_UP!! + * (it is _MODE_POWER_DOWN during this modes) + * + * is element of [MODE_ON, MODE_NORMAL, MODE_RAW] + */ + Mode_t transitionSourceMode; + + /** + * the submode of the source mode during a transition + */ + Submode_t transitionSourceSubMode; + + /** + * read the command queue + */ + void readCommandQueue(void); + + /** + * Handle the device handler mode. + * + * - checks whether commands are valid for the current mode, rejects them accordingly + * - 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() + */ + void doStateMachine(void); + + void buildRawDeviceCommand(CommandMessage* message); + void buildInternalCommand(void); + +// /** +// * Send a reply with the current mode and submode. +// */ +// void announceMode(void); + + /** + * Decrement the counter for the timout of replies. + * + * This is called at the beginning of each cycle. It checks whether a reply has timed out (that means a reply was expected + * but not received). + */ + void decrementDeviceReplyMap(void); + + /** + * Convenience function to handle a reply. + * + * Called after scanForReply() has found a packet. Checks if the found id is in the #deviceCommandMap, if so, + * calls interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) for further action. + * + * It also resets the timeout counter for the command id. + * + * @param data the found packet + * @param id the found id + * @foundLen the length of the packet + */ + void handleReply(const uint8_t *data, DeviceCommandId_t id, uint32_t foundLen); + + void replyToReply(DeviceReplyMap::iterator iter, ReturnValue_t status); + /** + * Build and send a command to the device. + * + * This routine checks whether a raw or direct command has been received, checks the content of the received command and + * calls buildCommandFromCommand() for direct commands or sets #rawpacket to the received raw packet. + * If no external command is received or the received command is invalid and the current mode is @c MODE_NORMAL or a transitional mode, + * it asks the child class to build a command (via getNormalDeviceCommand() or getTransitionalDeviceCommand() and buildCommand()) and + * sends the command via RMAP. + */ + void doSendWrite(void); + + /** + * Check if the RMAP sendWrite action was successful. + * + * Depending on the result, the following is done + * - if the device command was external commanded, a reply is sent indicating the result + * - if the action was successful, the reply timout counter is initialized + */ + void doGetWrite(void); + + /** + * Send a RMAP getRead command. + * + * The size of the getRead command is #maxDeviceReplyLen. + * This is always executed, independently from the current mode. + */ + void doSendRead(void); + + /** + * Check the getRead reply and the contained data. + * + * If data was received scanForReply() and, if successful, handleReply() are called. + * If the current mode is @c MODE_RAW, the received packet is sent to the commanding object + * via commandQueue. + */ + void doGetRead(void); + + /** + * Retrive data from the #IPCStore. + * + * @param storageAddress + * @param[out] data + * @param[out] len + * @return + * - @c RETURN_OK @c data is valid + * - @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); + + + /** + * @param modeTo either @c MODE_ON, MODE_NORMAL or MODE_RAW NOTHING ELSE!!! + */ + void setTransition(Mode_t modeTo, Submode_t submodeTo); + + /** + * calls the right child function for the transitional submodes + */ + void callChildStatemachine(); + + /** + * Switches the channel of the cookie used for the communication + * + * + * @param newChannel the object Id of the channel to switch to + * @return + * - @c RETURN_OK when cookie was changed + * - @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 handleDeviceHandlerMessage(CommandMessage *message); + + virtual ReturnValue_t initializeAfterTaskCreation() override; + + void parseReply(const uint8_t* receivedData, + size_t receivedDataLen); +}; + +#endif /* FRAMEWORK_DEVICEHANDLERS_DEVICEHANDLERBASE_H_ */ + diff --git a/fsfw/devicehandlers/DeviceHandlerFailureIsolation.cpp b/fsfw/devicehandlers/DeviceHandlerFailureIsolation.cpp new file mode 100644 index 0000000..9fbe71d --- /dev/null +++ b/fsfw/devicehandlers/DeviceHandlerFailureIsolation.cpp @@ -0,0 +1,256 @@ +#include "DeviceHandlerFailureIsolation.h" + +#include "../devicehandlers/DeviceHandlerIF.h" +#include "../modes/HasModesIF.h" +#include "../health/HealthTableIF.h" +#include "../power/Fuse.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../thermal/ThermalComponentIF.h" + +object_id_t DeviceHandlerFailureIsolation::powerConfirmationId = + objects::NO_OBJECT; + +DeviceHandlerFailureIsolation::DeviceHandlerFailureIsolation(object_id_t owner, + object_id_t parent) : + FailureIsolationBase(owner, parent), + strangeReplyCount(DEFAULT_MAX_STRANGE_REPLIES, + DEFAULT_STRANGE_REPLIES_TIME_MS, + parameterDomainBase++), + missedReplyCount( DEFAULT_MAX_MISSED_REPLY_COUNT, + DEFAULT_MISSED_REPLY_TIME_MS, + parameterDomainBase++), + recoveryCounter(DEFAULT_MAX_REBOOT, DEFAULT_REBOOT_TIME_MS, + parameterDomainBase++), + fdirState(NONE) { +} + +DeviceHandlerFailureIsolation::~DeviceHandlerFailureIsolation() { +} + +ReturnValue_t DeviceHandlerFailureIsolation::eventReceived(EventMessage* event) { + if(isFdirInActionOrAreWeFaulty(event)) { + return RETURN_OK; + } + ReturnValue_t result = RETURN_FAILED; + switch (event->getEvent()) { + case HasModesIF::MODE_TRANSITION_FAILED: + case HasModesIF::OBJECT_IN_INVALID_MODE: + //We'll try a recovery as long as defined in MAX_REBOOT. + //Might cause some AssemblyBase cycles, so keep number low. + handleRecovery(event->getEvent()); + break; + case DeviceHandlerIF::DEVICE_INTERPRETING_REPLY_FAILED: + case DeviceHandlerIF::DEVICE_READING_REPLY_FAILED: + case DeviceHandlerIF::DEVICE_UNREQUESTED_REPLY: + case DeviceHandlerIF::DEVICE_UNKNOWN_REPLY: //Some DH's generate generic reply-ids. + case DeviceHandlerIF::DEVICE_BUILDING_COMMAND_FAILED: + //These faults all mean that there were stupid replies from a device. + if (strangeReplyCount.incrementAndCheck()) { + handleRecovery(event->getEvent()); + } + break; + case DeviceHandlerIF::DEVICE_SENDING_COMMAND_FAILED: + case DeviceHandlerIF::DEVICE_REQUESTING_REPLY_FAILED: + //The two above should never be confirmed. + case DeviceHandlerIF::DEVICE_MISSED_REPLY: + result = sendConfirmationRequest(event); + if (result == HasReturnvaluesIF::RETURN_OK) { + break; + } + //else + if (missedReplyCount.incrementAndCheck()) { + handleRecovery(event->getEvent()); + } + break; + case StorageManagerIF::GET_DATA_FAILED: + case StorageManagerIF::STORE_DATA_FAILED: + //Rather strange bugs, occur in RAW mode only. Ignore. + break; + case DeviceHandlerIF::INVALID_DEVICE_COMMAND: + //Ignore, is bad configuration. We can't do anything in flight. + break; + case HasHealthIF::HEALTH_INFO: + case HasModesIF::MODE_INFO: + case HasModesIF::CHANGING_MODE: + //Do nothing, but mark as handled. + break; + //****Power***** + case PowerSwitchIF::SWITCH_WENT_OFF: + if(powerConfirmation != MessageQueueIF::NO_QUEUE) { + result = sendConfirmationRequest(event, powerConfirmation); + if (result == RETURN_OK) { + setFdirState(DEVICE_MIGHT_BE_OFF); + } + } + break; + case Fuse::FUSE_WENT_OFF: + //Not so good, because PCDU reacted. + case Fuse::POWER_ABOVE_HIGH_LIMIT: + //Better, because software detected over-current. + setFaulty(event->getEvent()); + break; + case Fuse::POWER_BELOW_LOW_LIMIT: + //Device might got stuck during boot, retry. + handleRecovery(event->getEvent()); + break; + //****Thermal***** + case ThermalComponentIF::COMPONENT_TEMP_LOW: + case ThermalComponentIF::COMPONENT_TEMP_HIGH: + case ThermalComponentIF::COMPONENT_TEMP_OOL_LOW: + case ThermalComponentIF::COMPONENT_TEMP_OOL_HIGH: + //Well, the device is not really faulty, but it is required to stay off as long as possible. + setFaulty(event->getEvent()); + break; + case ThermalComponentIF::TEMP_NOT_IN_OP_RANGE: + //Ignore, is information only. + break; + //*******Default monitoring variables. Are currently not used.***** +// case DeviceHandlerIF::MONITORING_LIMIT_EXCEEDED: +// setFaulty(event->getEvent()); +// break; +// case DeviceHandlerIF::MONITORING_AMBIGUOUS: +// break; + default: + //We don't know the event, someone else should handle it. + return RETURN_FAILED; + } + return RETURN_OK; +} + +void DeviceHandlerFailureIsolation::eventConfirmed(EventMessage* event) { + switch (event->getEvent()) { + case DeviceHandlerIF::DEVICE_SENDING_COMMAND_FAILED: + case DeviceHandlerIF::DEVICE_REQUESTING_REPLY_FAILED: + case DeviceHandlerIF::DEVICE_MISSED_REPLY: + if (missedReplyCount.incrementAndCheck()) { + handleRecovery(event->getEvent()); + } + break; + case PowerSwitchIF::SWITCH_WENT_OFF: + //This means the switch went off only for one device. + handleRecovery(event->getEvent()); + break; + default: + break; + } +} + +void DeviceHandlerFailureIsolation::decrementFaultCounters() { + strangeReplyCount.checkForDecrement(); + missedReplyCount.checkForDecrement(); + recoveryCounter.checkForDecrement(); +} + +void DeviceHandlerFailureIsolation::handleRecovery(Event reason) { + clearFaultCounters(); + if (not recoveryCounter.incrementAndCheck()) { + startRecovery(reason); + } else { + setFaulty(reason); + } +} + +void DeviceHandlerFailureIsolation::wasParentsFault(EventMessage* event) { + //We'll better ignore the SWITCH_WENT_OFF event and await a system-wide reset. + //This means, no fault message will come through until a MODE_ or + //HEALTH_INFO message comes through -> Is that ok? + //Same issue in TxFailureIsolation! +// if ((event->getEvent() == PowerSwitchIF::SWITCH_WENT_OFF) +// && (fdirState != RECOVERY_ONGOING)) { +// setFdirState(NONE); +// } +} + +void DeviceHandlerFailureIsolation::clearFaultCounters() { + strangeReplyCount.clear(); + missedReplyCount.clear(); +} + +ReturnValue_t DeviceHandlerFailureIsolation::initialize() { + ReturnValue_t result = FailureIsolationBase::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "DeviceHandlerFailureIsolation::initialize: Could not" + " initialize FailureIsolationBase." << std::endl; + return result; + } + ConfirmsFailuresIF* power = objectManager->get( + powerConfirmationId); + if (power != nullptr) { + powerConfirmation = power->getEventReceptionQueue(); + } + + return RETURN_OK; +} + +void DeviceHandlerFailureIsolation::setFdirState(FDIRState state) { + FailureIsolationBase::throwFdirEvent(FDIR_CHANGED_STATE, state, fdirState); + fdirState = state; +} + +void DeviceHandlerFailureIsolation::triggerEvent(Event event, uint32_t parameter1, + uint32_t parameter2) { + //Do not throw error events if fdirState != none. + //This will still forward MODE and HEALTH INFO events in any case. + if (fdirState == NONE || EVENT::getSeverity(event) == SEVERITY::INFO) { + FailureIsolationBase::triggerEvent(event, parameter1, parameter2); + } +} + +bool DeviceHandlerFailureIsolation::isFdirActionInProgress() { + return (fdirState != NONE); +} + +void DeviceHandlerFailureIsolation::startRecovery(Event reason) { + throwFdirEvent(FDIR_STARTS_RECOVERY, EVENT::getEventId(reason)); + setOwnerHealth(HasHealthIF::NEEDS_RECOVERY); + setFdirState(RECOVERY_ONGOING); +} + +ReturnValue_t DeviceHandlerFailureIsolation::getParameter(uint8_t domainId, + uint16_t parameterId, ParameterWrapper* parameterWrapper, + const ParameterWrapper* newValues, uint16_t startAtIndex) { + ReturnValue_t result = strangeReplyCount.getParameter(domainId, parameterId, + parameterWrapper, newValues, startAtIndex); + if (result != INVALID_DOMAIN_ID) { + return result; + } + result = missedReplyCount.getParameter(domainId, parameterId, + parameterWrapper, newValues, startAtIndex); + if (result != INVALID_DOMAIN_ID) { + return result; + } + result = recoveryCounter.getParameter(domainId, parameterId, + parameterWrapper, newValues, startAtIndex); + if (result != INVALID_DOMAIN_ID) { + return result; + } + return INVALID_DOMAIN_ID; +} + +void DeviceHandlerFailureIsolation::setFaulty(Event reason) { + throwFdirEvent(FDIR_TURNS_OFF_DEVICE, EVENT::getEventId(reason)); + setOwnerHealth(HasHealthIF::FAULTY); + setFdirState(AWAIT_SHUTDOWN); +} + +bool DeviceHandlerFailureIsolation::isFdirInActionOrAreWeFaulty( + EventMessage* event) { + if (fdirState != NONE) { + //Only wait for those events, ignore all others. + if (event->getParameter1() == HasHealthIF::HEALTHY + && event->getEvent() == HasHealthIF::HEALTH_INFO) { + setFdirState(NONE); + } + if (event->getEvent() == HasModesIF::MODE_INFO + && fdirState != RECOVERY_ONGOING) { + setFdirState(NONE); + } + return true; + } + if (owner->getHealth() == HasHealthIF::FAULTY + || owner->getHealth() == HasHealthIF::PERMANENT_FAULTY) { + //Ignore all events in case device is already faulty. + return true; + } + return false; +} diff --git a/fsfw/devicehandlers/DeviceHandlerFailureIsolation.h b/fsfw/devicehandlers/DeviceHandlerFailureIsolation.h new file mode 100644 index 0000000..8a3fd9d --- /dev/null +++ b/fsfw/devicehandlers/DeviceHandlerFailureIsolation.h @@ -0,0 +1,57 @@ +#ifndef FSFW_DEVICEHANDLERS_DEVICEHANDLERFAILUREISOLATION_H_ +#define FSFW_DEVICEHANDLERS_DEVICEHANDLERFAILUREISOLATION_H_ + +#include "../fdir/FaultCounter.h" +#include "../fdir/FailureIsolationBase.h" + +namespace Factory{ +void setStaticFrameworkObjectIds(); +} + +class DeviceHandlerFailureIsolation: public FailureIsolationBase { + friend void (Factory::setStaticFrameworkObjectIds)(); + friend class Heater; +public: + DeviceHandlerFailureIsolation(object_id_t owner, object_id_t parent); + ~DeviceHandlerFailureIsolation(); + ReturnValue_t initialize(); + void triggerEvent(Event event, uint32_t parameter1 = 0, + uint32_t parameter2 = 0);bool isFdirActionInProgress(); + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); + +protected: + FaultCounter strangeReplyCount; + FaultCounter missedReplyCount; + FaultCounter recoveryCounter; + + enum FDIRState { + NONE, RECOVERY_ONGOING, DEVICE_MIGHT_BE_OFF, AWAIT_SHUTDOWN + }; + FDIRState fdirState; + + MessageQueueId_t powerConfirmation = MessageQueueIF::NO_QUEUE; + static object_id_t powerConfirmationId; + + static const uint32_t DEFAULT_MAX_REBOOT = 1; + static const uint32_t DEFAULT_REBOOT_TIME_MS = 180000; + static const uint32_t DEFAULT_MAX_STRANGE_REPLIES = 10; + static const uint32_t DEFAULT_STRANGE_REPLIES_TIME_MS = 10000; + static const uint32_t DEFAULT_MAX_MISSED_REPLY_COUNT = 5; + static const uint32_t DEFAULT_MISSED_REPLY_TIME_MS = 10000; + + virtual ReturnValue_t eventReceived(EventMessage* event); + virtual void eventConfirmed(EventMessage* event); + void wasParentsFault(EventMessage* event); + void decrementFaultCounters(); + void handleRecovery(Event reason); + virtual void clearFaultCounters(); + void setFdirState(FDIRState state); + void startRecovery(Event reason); + void setFaulty(Event reason); + + bool isFdirInActionOrAreWeFaulty(EventMessage* event); +}; + +#endif /* FSFW_DEVICEHANDLERS_DEVICEHANDLERFAILUREISOLATION_H_ */ diff --git a/fsfw/devicehandlers/DeviceHandlerIF.h b/fsfw/devicehandlers/DeviceHandlerIF.h new file mode 100644 index 0000000..52a3be4 --- /dev/null +++ b/fsfw/devicehandlers/DeviceHandlerIF.h @@ -0,0 +1,153 @@ +#ifndef DEVICEHANDLERIF_H_ +#define DEVICEHANDLERIF_H_ + +#include "../action/HasActionsIF.h" +#include "DeviceHandlerMessage.h" +#include "../events/Event.h" +#include "../modes/HasModesIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +/** + * @brief This is the Interface used to communicate with a device handler. + * @details Includes all expected return values, events and modes. + * + */ +class DeviceHandlerIF { +public: + + static const uint8_t TRANSITION_MODE_CHILD_ACTION_MASK = 0x20; + static const uint8_t TRANSITION_MODE_BASE_ACTION_MASK = 0x10; + + /** + * @brief This is the mode the device handler is in. + * + * @details The mode of the device handler must not be confused with the mode the device is in. + * The mode of the device itself is transparent to the user but related to the mode of the handler. + * MODE_ON and MODE_OFF are included in hasModesIF.h + */ + + // MODE_ON = 0, //!< The device is powered and ready to perform operations. In this mode, no commands are sent by the device handler itself, but direct commands van be commanded and will be interpreted + // MODE_OFF = 1, //!< The device is powered off. The only command accepted in this mode is a mode change to on. + //! The device is powered on and the device handler periodically sends + //! commands. The commands to be sent are selected by the handler + //! according to the submode. + static const Mode_t MODE_NORMAL = 2; + //! The device is powered on and ready to perform operations. In this mode, + //! raw commands can be sent. The device handler will send all replies + //! received from the command back to the commanding object. + static const Mode_t MODE_RAW = 3; + //! The device is shut down but the switch could not be turned off, so the + //! device still is powered. In this mode, only a mode change to @c MODE_OFF + //! can be commanded, which tries to switch off the device again. + static const Mode_t MODE_ERROR_ON = 4; + //! This is a transitional state which can not be commanded. The device + //! handler performs all commands to get the device in a state ready to + //! perform commands. When this is completed, the mode changes to @c MODE_ON. + static const Mode_t _MODE_START_UP = TRANSITION_MODE_CHILD_ACTION_MASK | 5; + //! This is a transitional state which can not be commanded. + //! The device handler performs all actions and commands to get the device + //! shut down. When the device is off, the mode changes to @c MODE_OFF. + static const Mode_t _MODE_SHUT_DOWN = TRANSITION_MODE_CHILD_ACTION_MASK | 6; + //! It is possible to set the mode to _MODE_TO_ON to use the to on + //! transition if available. + static const Mode_t _MODE_TO_ON = TRANSITION_MODE_CHILD_ACTION_MASK | HasModesIF::MODE_ON; + //! It is possible to set the mode to _MODE_TO_RAW to use the to raw + //! transition if available. + static const Mode_t _MODE_TO_RAW = TRANSITION_MODE_CHILD_ACTION_MASK | MODE_RAW; + //! It is possible to set the mode to _MODE_TO_NORMAL to use the to normal + //! transition if available. + static const Mode_t _MODE_TO_NORMAL = TRANSITION_MODE_CHILD_ACTION_MASK | MODE_NORMAL; + //! This is a transitional state which can not be commanded. + //! The device is shut down and ready to be switched off. + //! After the command to set the switch off has been sent, + //! the mode changes to @c MODE_WAIT_OFF + static const Mode_t _MODE_POWER_DOWN = TRANSITION_MODE_BASE_ACTION_MASK | 1; + //! This is a transitional state which can not be commanded. The device + //! will be switched on in this state. After the command to set the switch + //! on has been sent, the mode changes to @c MODE_WAIT_ON. + static const Mode_t _MODE_POWER_ON = TRANSITION_MODE_BASE_ACTION_MASK | 2; + //! This is a transitional state which can not be commanded. The switch has + //! been commanded off and the handler waits for it to be off. + //! When the switch is off, the mode changes to @c MODE_OFF. + static const Mode_t _MODE_WAIT_OFF = TRANSITION_MODE_BASE_ACTION_MASK | 3; + //! This is a transitional state which can not be commanded. The switch + //! has been commanded on and the handler waits for it to be on. + //! When the switch is on, the mode changes to @c MODE_TO_ON. + static const Mode_t _MODE_WAIT_ON = TRANSITION_MODE_BASE_ACTION_MASK | 4; + //! This is a transitional state which can not be commanded. The switch has + //! been commanded off and is off now. This state is only to do an RMAP + //! cycle once more where the doSendRead() function will set the mode to + //! MODE_OFF. The reason to do this is to get rid of stuck packets in the IO Board. + static const Mode_t _MODE_SWITCH_IS_OFF = TRANSITION_MODE_BASE_ACTION_MASK | 5; + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::CDH; + static const Event DEVICE_BUILDING_COMMAND_FAILED = MAKE_EVENT(0, SEVERITY::LOW); + static const Event DEVICE_SENDING_COMMAND_FAILED = MAKE_EVENT(1, SEVERITY::LOW); + static const Event DEVICE_REQUESTING_REPLY_FAILED = MAKE_EVENT(2, SEVERITY::LOW); + static const Event DEVICE_READING_REPLY_FAILED = MAKE_EVENT(3, SEVERITY::LOW); + static const Event DEVICE_INTERPRETING_REPLY_FAILED = MAKE_EVENT(4, SEVERITY::LOW); + static const Event DEVICE_MISSED_REPLY = MAKE_EVENT(5, SEVERITY::LOW); + static const Event DEVICE_UNKNOWN_REPLY = MAKE_EVENT(6, SEVERITY::LOW); + static const Event DEVICE_UNREQUESTED_REPLY = MAKE_EVENT(7, SEVERITY::LOW); + static const Event INVALID_DEVICE_COMMAND = MAKE_EVENT(8, SEVERITY::LOW); //!< Indicates a SW bug in child class. + static const Event MONITORING_LIMIT_EXCEEDED = MAKE_EVENT(9, SEVERITY::LOW); + static const Event MONITORING_AMBIGUOUS = MAKE_EVENT(10, SEVERITY::HIGH); + + static const uint8_t INTERFACE_ID = CLASS_ID::DEVICE_HANDLER_IF; + + // Standard codes used when building commands. + static const ReturnValue_t NO_COMMAND_DATA = MAKE_RETURN_CODE(0xA0); //!< If the command size is 0. Checked in DHB + static const ReturnValue_t COMMAND_NOT_SUPPORTED = MAKE_RETURN_CODE(0xA1); //!< Command ID not in commandMap. Checked in DHB + static const ReturnValue_t COMMAND_ALREADY_SENT = MAKE_RETURN_CODE(0xA2); //!< Command was already executed. Checked in DHB + static const ReturnValue_t COMMAND_WAS_NOT_SENT = MAKE_RETURN_CODE(0xA3); + static const ReturnValue_t CANT_SWITCH_ADDRESS = MAKE_RETURN_CODE(0xA4); + static const ReturnValue_t WRONG_MODE_FOR_COMMAND = MAKE_RETURN_CODE(0xA5); + static const ReturnValue_t TIMEOUT = MAKE_RETURN_CODE(0xA6); + static const ReturnValue_t BUSY = MAKE_RETURN_CODE(0xA7); + static const ReturnValue_t NO_REPLY_EXPECTED = MAKE_RETURN_CODE(0xA8); //!< Used to indicate that this is a command-only command. + static const ReturnValue_t NON_OP_TEMPERATURE = MAKE_RETURN_CODE(0xA9); + static const ReturnValue_t COMMAND_NOT_IMPLEMENTED = MAKE_RETURN_CODE(0xAA); + + // Standard codes used in scanForReply + static const ReturnValue_t CHECKSUM_ERROR = MAKE_RETURN_CODE(0xB0); + static const ReturnValue_t LENGTH_MISSMATCH = MAKE_RETURN_CODE(0xB1); + static const ReturnValue_t INVALID_DATA = MAKE_RETURN_CODE(0xB2); + static const ReturnValue_t PROTOCOL_ERROR = MAKE_RETURN_CODE(0xB3); + + // Standard codes used in interpretDeviceReply + static const ReturnValue_t DEVICE_DID_NOT_EXECUTE = MAKE_RETURN_CODE(0xC0); //the device reported, that it did not execute the command + static const ReturnValue_t DEVICE_REPORTED_ERROR = MAKE_RETURN_CODE(0xC1); + static const ReturnValue_t UNKNOW_DEVICE_REPLY = MAKE_RETURN_CODE(0xC2); //the deviceCommandId reported by scanforReply is unknown + static const ReturnValue_t DEVICE_REPLY_INVALID = MAKE_RETURN_CODE(0xC3); //syntax etc is correct but still not ok, eg parameters where none are expected + + // Standard codes used in buildCommandFromCommand + static const ReturnValue_t INVALID_COMMAND_PARAMETER = MAKE_RETURN_CODE(0xD0); + static const ReturnValue_t INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS = MAKE_RETURN_CODE(0xD1); + + /** + * Communication action that will be executed. + * + * This is used by the child class to tell the base class what to do. + */ + enum CommunicationAction_t: uint8_t { + SEND_WRITE,//!< Send write + GET_WRITE, //!< Get write + SEND_READ, //!< Send read + GET_READ, //!< Get read + NOTHING //!< Do nothing. + }; + + /** + * Default Destructor + */ + virtual ~DeviceHandlerIF() {} + + /** + * This MessageQueue is used to command the device handler. + * @return the id of the MessageQueue + */ + virtual MessageQueueId_t getCommandQueue() const = 0; + +}; + +#endif /* DEVICEHANDLERIF_H_ */ diff --git a/fsfw/devicehandlers/DeviceHandlerMessage.cpp b/fsfw/devicehandlers/DeviceHandlerMessage.cpp new file mode 100644 index 0000000..564fae2 --- /dev/null +++ b/fsfw/devicehandlers/DeviceHandlerMessage.cpp @@ -0,0 +1,101 @@ +#include "../objectmanager/ObjectManagerIF.h" +#include "DeviceHandlerMessage.h" +#include "../objectmanager/ObjectManagerIF.h" + +DeviceHandlerMessage::DeviceHandlerMessage() { +} + +store_address_t DeviceHandlerMessage::getStoreAddress( + const CommandMessage* message) { + return store_address_t(message->getParameter2()); +} + +uint32_t DeviceHandlerMessage::getDeviceCommandId( + const CommandMessage* message) { + return message->getParameter(); +} + +object_id_t DeviceHandlerMessage::getIoBoardObjectId( + const CommandMessage* message) { + return message->getParameter(); +} + +uint8_t DeviceHandlerMessage::getWiretappingMode( + const CommandMessage* message) { + return message->getParameter(); +} + +//void DeviceHandlerMessage::setDeviceHandlerDirectCommandMessage( +// CommandMessage* message, DeviceCommandId_t deviceCommand, +// store_address_t commandParametersStoreId) { +// message->setCommand(CMD_DIRECT); +// message->setParameter(deviceCommand); +// message->setParameter2(commandParametersStoreId.raw); +//} + +void DeviceHandlerMessage::setDeviceHandlerRawCommandMessage( + CommandMessage* message, store_address_t rawPacketStoreId) { + message->setCommand(CMD_RAW); + message->setParameter2(rawPacketStoreId.raw); +} + +void DeviceHandlerMessage::setDeviceHandlerWiretappingMessage( + CommandMessage* message, uint8_t wiretappingMode) { + message->setCommand(CMD_WIRETAPPING); + message->setParameter(wiretappingMode); +} + +void DeviceHandlerMessage::setDeviceHandlerSwitchIoBoardMessage( + CommandMessage* message, uint32_t ioBoardIdentifier) { + message->setCommand(CMD_SWITCH_IOBOARD); + message->setParameter(ioBoardIdentifier); +} + +object_id_t DeviceHandlerMessage::getDeviceObjectId( + const CommandMessage* message) { + return message->getParameter(); +} + +void DeviceHandlerMessage::setDeviceHandlerRawReplyMessage( + CommandMessage* message, object_id_t deviceObjectid, + store_address_t rawPacketStoreId, bool isCommand) { + if (isCommand) { + message->setCommand(REPLY_RAW_COMMAND); + } else { + message->setCommand(REPLY_RAW_REPLY); + } + message->setParameter(deviceObjectid); + message->setParameter2(rawPacketStoreId.raw); +} + +void DeviceHandlerMessage::setDeviceHandlerDirectCommandReply( + CommandMessage* message, object_id_t deviceObjectid, + store_address_t commandParametersStoreId) { + message->setCommand(REPLY_DIRECT_COMMAND_DATA); + message->setParameter(deviceObjectid); + message->setParameter2(commandParametersStoreId.raw); +} + +void DeviceHandlerMessage::clear(CommandMessage* message) { + switch (message->getCommand()) { + case CMD_RAW: +// case CMD_DIRECT: + case REPLY_RAW_COMMAND: + case REPLY_RAW_REPLY: + case REPLY_DIRECT_COMMAND_DATA: { + StorageManagerIF *ipcStore = objectManager->get( + objects::IPC_STORE); + if (ipcStore != NULL) { + ipcStore->deleteData(getStoreAddress(message)); + } + } + /* NO BREAK falls through*/ + case CMD_SWITCH_IOBOARD: + case CMD_WIRETAPPING: + message->setCommand(CommandMessage::CMD_NONE); + message->setParameter(0); + message->setParameter2(0); + break; + } + +} diff --git a/fsfw/devicehandlers/DeviceHandlerMessage.h b/fsfw/devicehandlers/DeviceHandlerMessage.h new file mode 100644 index 0000000..8d1c94f --- /dev/null +++ b/fsfw/devicehandlers/DeviceHandlerMessage.h @@ -0,0 +1,91 @@ +#ifndef DEVICEHANDLERMESSAGE_H_ +#define DEVICEHANDLERMESSAGE_H_ + +#include "../action/ActionMessage.h" +#include "../ipc/CommandMessage.h" +#include "../objectmanager/SystemObjectIF.h" +#include "../storagemanager/StorageManagerIF.h" +//SHOULDDO: rework the static constructors to name the type of command they are building, maybe even hide setting the commandID. + +/** + * This is used to uniquely identify commands that are sent to a device + * + * The values are defined in the device-specific implementations + */ +typedef uint32_t DeviceCommandId_t; + +/** + * The DeviceHandlerMessage is used to send Commands to a DeviceHandlerIF + */ +class DeviceHandlerMessage { +private: + DeviceHandlerMessage(); +public: + + /** + * These are the commands that can be sent to a DeviceHandlerBase + */ + static const uint8_t MESSAGE_ID = messagetypes::DEVICE_HANDLER_COMMAND; + static const Command_t CMD_RAW = MAKE_COMMAND_ID( 1 ); //!< Sends a raw command, setParameter is a ::store_id_t containing the raw packet to send +// static const Command_t CMD_DIRECT = MAKE_COMMAND_ID( 2 ); //!< Sends a direct command, setParameter is a ::DeviceCommandId_t, setParameter2 is a ::store_id_t containing the data needed for the command + static const Command_t CMD_SWITCH_IOBOARD = MAKE_COMMAND_ID( 3 ); //!< Requests a IO-Board switch, setParameter() is the IO-Board identifier + static const Command_t CMD_WIRETAPPING = MAKE_COMMAND_ID( 4 ); //!< (De)Activates the monitoring of all raw traffic in DeviceHandlers, setParameter is 0 to deactivate, 1 to activate + + /*static const Command_t REPLY_SWITCHED_IOBOARD = MAKE_COMMAND_ID(1 );//!< Reply to a @c CMD_SWITCH_IOBOARD, indicates switch was successful, getParameter() contains the board switched to (0: nominal, 1: redundant) + static const Command_t REPLY_CANT_SWITCH_IOBOARD = MAKE_COMMAND_ID( 2); //!< Reply to a @c CMD_SWITCH_IOBOARD, indicating the switch could not be performed, getParameter() contains the error message + static const Command_t REPLY_WIRETAPPING = MAKE_COMMAND_ID( 3); //!< Reply to a @c CMD_WIRETAPPING, getParameter() is the current state, 1 enabled, 0 disabled + + static const Command_t REPLY_COMMAND_WAS_SENT = MAKE_COMMAND_ID(4 );//!< Reply to a @c CMD_RAW or @c CMD_DIRECT, indicates the command was successfully sent to the device, getParameter() contains the ::DeviceCommandId_t + static const Command_t REPLY_COMMAND_NOT_SUPPORTED = MAKE_COMMAND_ID(5 );//!< Reply to a @c CMD_DIRECT, the requested ::DeviceCommand_t is not supported, getParameter() contains the requested ::DeviceCommand_t, getParameter2() contains the ::DeviceCommandId_t + static const Command_t REPLY_COMMAND_WAS_NOT_SENT = MAKE_COMMAND_ID(6 );//!< Reply to a @c CMD_RAW or @c CMD_DIRECT, indicates the command was not sent, getParameter contains the RMAP Return code (@see rmap.h), getParameter2() contains the ::DeviceCommandId_t + + static const Command_t REPLY_COMMAND_ALREADY_SENT = MAKE_COMMAND_ID(7 );//!< Reply to a @c CMD_DIRECT, the requested ::DeviceCommand_t has already been sent to the device and not ye been answered + static const Command_t REPLY_WRONG_MODE_FOR_CMD = MAKE_COMMAND_ID(8 );//!< Reply to a @c CMD_RAW or @c CMD_DIRECT, indicates that the requested command can not be sent in the curent mode, getParameter() contains the DeviceHandlerCommand_t + static const Command_t REPLY_NO_DATA = MAKE_COMMAND_ID(9 ); //!< Reply to a CMD_RAW or @c CMD_DIRECT, indicates that the ::store_id_t was invalid, getParameter() contains the ::DeviceCommandId_t, getPrameter2() contains the error code + */ + static const Command_t REPLY_DIRECT_COMMAND_SENT = ActionMessage::STEP_SUCCESS; //!< Signals that a direct command was sent + static const Command_t REPLY_RAW_COMMAND = MAKE_COMMAND_ID(0x11 ); //!< Contains a raw command sent to the Device + static const Command_t REPLY_RAW_REPLY = MAKE_COMMAND_ID( 0x12); //!< Contains a raw reply from the Device, getParameter() is the ObjcetId of the sender, getParameter2() is a ::store_id_t containing the raw packet received + static const Command_t REPLY_DIRECT_COMMAND_DATA = ActionMessage::DATA_REPLY; + + /** + * Default Destructor + */ + virtual ~DeviceHandlerMessage() { + } + + static store_address_t getStoreAddress(const CommandMessage* message); + static uint32_t getDeviceCommandId(const CommandMessage* message); + static object_id_t getDeviceObjectId(const CommandMessage *message); + static object_id_t getIoBoardObjectId(const CommandMessage* message); + static uint8_t getWiretappingMode(const CommandMessage* message); + +// static void setDeviceHandlerDirectCommandMessage(CommandMessage* message, +// DeviceCommandId_t deviceCommand, +// store_address_t commandParametersStoreId); + + static void setDeviceHandlerDirectCommandReply(CommandMessage* message, + object_id_t deviceObjectid, + store_address_t commandParametersStoreId); + + static void setDeviceHandlerRawCommandMessage(CommandMessage* message, + store_address_t rawPacketStoreId); + + static void setDeviceHandlerRawReplyMessage(CommandMessage* message, + object_id_t deviceObjectid, store_address_t rawPacketStoreId, + bool isCommand); + +// static void setDeviceHandlerMessage(CommandMessage* message, +// Command_t command, DeviceCommandId_t deviceCommand, +// store_address_t commandParametersStoreId); +// static void setDeviceHandlerMessage(CommandMessage* message, +// Command_t command, store_address_t rawPacketStoreId); + static void setDeviceHandlerWiretappingMessage(CommandMessage* message, + uint8_t wiretappingMode); + static void setDeviceHandlerSwitchIoBoardMessage(CommandMessage* message, + object_id_t ioBoardIdentifier); + + static void clear(CommandMessage* message); +}; + +#endif /* DEVICEHANDLERMESSAGE_H_ */ diff --git a/fsfw/devicehandlers/DeviceTmReportingWrapper.cpp b/fsfw/devicehandlers/DeviceTmReportingWrapper.cpp new file mode 100644 index 0000000..84f926f --- /dev/null +++ b/fsfw/devicehandlers/DeviceTmReportingWrapper.cpp @@ -0,0 +1,46 @@ +#include "../serialize/SerializeAdapter.h" +#include "DeviceTmReportingWrapper.h" +#include "../serialize/SerializeAdapter.h" + +DeviceTmReportingWrapper::DeviceTmReportingWrapper(object_id_t objectId, + ActionId_t actionId, SerializeIF* data) : + objectId(objectId), actionId(actionId), data(data) { +} + +DeviceTmReportingWrapper::~DeviceTmReportingWrapper() { + +} + +ReturnValue_t DeviceTmReportingWrapper::serialize(uint8_t** buffer, + size_t* size, size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = SerializeAdapter::serialize(&objectId, + buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&actionId, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return data->serialize(buffer, size, maxSize, streamEndianness); +} + +size_t DeviceTmReportingWrapper::getSerializedSize() const { + return sizeof(objectId) + sizeof(ActionId_t) + data->getSerializedSize(); +} + +ReturnValue_t DeviceTmReportingWrapper::deSerialize(const uint8_t** buffer, + size_t* size, Endianness streamEndianness) { + ReturnValue_t result = SerializeAdapter::deSerialize(&objectId, + buffer, size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&actionId, buffer, + size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return data->deSerialize(buffer, size, streamEndianness); +} diff --git a/fsfw/devicehandlers/DeviceTmReportingWrapper.h b/fsfw/devicehandlers/DeviceTmReportingWrapper.h new file mode 100644 index 0000000..a14c426 --- /dev/null +++ b/fsfw/devicehandlers/DeviceTmReportingWrapper.h @@ -0,0 +1,27 @@ +#ifndef DEVICETMREPORTINGWRAPPER_H_ +#define DEVICETMREPORTINGWRAPPER_H_ + +#include "../action/HasActionsIF.h" +#include "../objectmanager/SystemObjectIF.h" +#include "../serialize/SerializeIF.h" + +class DeviceTmReportingWrapper: public SerializeIF { +public: + DeviceTmReportingWrapper(object_id_t objectId, ActionId_t actionId, + SerializeIF *data); + virtual ~DeviceTmReportingWrapper(); + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override; + + virtual size_t getSerializedSize() const override; + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; +private: + object_id_t objectId; + ActionId_t actionId; + SerializeIF *data; +}; + +#endif /* DEVICETMREPORTINGWRAPPER_H_ */ diff --git a/fsfw/devicehandlers/HealthDevice.cpp b/fsfw/devicehandlers/HealthDevice.cpp new file mode 100644 index 0000000..b15e5d2 --- /dev/null +++ b/fsfw/devicehandlers/HealthDevice.cpp @@ -0,0 +1,59 @@ +#include "HealthDevice.h" +#include "../ipc/QueueFactory.h" + +HealthDevice::HealthDevice(object_id_t setObjectId, + MessageQueueId_t parentQueue) : + SystemObject(setObjectId), lastHealth(HEALTHY), parentQueue( + parentQueue), commandQueue(), healthHelper(this, setObjectId) { + commandQueue = QueueFactory::instance()->createMessageQueue(3); +} + +HealthDevice::~HealthDevice() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); +} + +ReturnValue_t HealthDevice::performOperation(uint8_t opCode) { + CommandMessage message; + ReturnValue_t result = commandQueue->receiveMessage(&message); + if (result == HasReturnvaluesIF::RETURN_OK) { + healthHelper.handleHealthCommand(&message); + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t HealthDevice::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (parentQueue != 0) { + return healthHelper.initialize(parentQueue); + } else { + return healthHelper.initialize(); + } +} + +MessageQueueId_t HealthDevice::getCommandQueue() const { + return commandQueue->getId(); +} + +void HealthDevice::setParentQueue(MessageQueueId_t parentQueue) { + healthHelper.setParentQueue(parentQueue); +} + +bool HealthDevice::hasHealthChanged() { + bool changed; + HealthState currentHealth = healthHelper.getHealth(); + changed = currentHealth != lastHealth; + lastHealth = currentHealth; + return changed; +} + +ReturnValue_t HealthDevice::setHealth(HealthState health) { + healthHelper.setHealth(health); + return HasReturnvaluesIF::RETURN_OK; +} + +HasHealthIF::HealthState HealthDevice::getHealth() { + return healthHelper.getHealth(); +} diff --git a/fsfw/devicehandlers/HealthDevice.h b/fsfw/devicehandlers/HealthDevice.h new file mode 100644 index 0000000..53b0ab0 --- /dev/null +++ b/fsfw/devicehandlers/HealthDevice.h @@ -0,0 +1,40 @@ +#ifndef HEALTHDEVICE_H_ +#define HEALTHDEVICE_H_ + +#include "../health/HasHealthIF.h" +#include "../health/HealthHelper.h" +#include "../objectmanager/SystemObject.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../ipc/MessageQueueIF.h" + +class HealthDevice: public SystemObject, + public ExecutableObjectIF, + public HasHealthIF { +public: + HealthDevice(object_id_t setObjectId, MessageQueueId_t parentQueue); + virtual ~HealthDevice(); + + ReturnValue_t performOperation(uint8_t opCode); + + ReturnValue_t initialize(); + + virtual MessageQueueId_t getCommandQueue() const; + + void setParentQueue(MessageQueueId_t parentQueue); + + bool hasHealthChanged(); + + virtual ReturnValue_t setHealth(HealthState health); + + virtual HealthState getHealth(); + +protected: + HealthState lastHealth; + + MessageQueueId_t parentQueue; + MessageQueueIF* commandQueue; +public: + HealthHelper healthHelper; +}; + +#endif /* HEALTHDEVICE_H_ */ diff --git a/fsfw/events/Event.cpp b/fsfw/events/Event.cpp new file mode 100644 index 0000000..ea3d46f --- /dev/null +++ b/fsfw/events/Event.cpp @@ -0,0 +1,14 @@ +#include "Event.h" +namespace EVENT { +EventId_t getEventId(Event event) { + return (event & 0xFFFF); +} + +EventSeverity_t getSeverity(Event event) { + return ((event >> 16) & 0xFF); +} + +Event makeEvent(EventId_t eventId, EventSeverity_t eventSeverity) { + return (eventSeverity << 16) + (eventId & 0xFFFF); +} +} diff --git a/fsfw/events/Event.h b/fsfw/events/Event.h new file mode 100644 index 0000000..aba156f --- /dev/null +++ b/fsfw/events/Event.h @@ -0,0 +1,44 @@ +#ifndef EVENTOBJECT_EVENT_H_ +#define EVENTOBJECT_EVENT_H_ + +#include +#include "fwSubsystemIdRanges.h" +//could be move to more suitable location +#include + +typedef uint16_t EventId_t; +typedef uint8_t EventSeverity_t; + +#define MAKE_EVENT(id, severity) (((severity)<<16)+(SUBSYSTEM_ID*100)+(id)) + +typedef uint32_t Event; + +namespace EVENT { +EventId_t getEventId(Event event); + +EventSeverity_t getSeverity(Event event); + +Event makeEvent(EventId_t eventId, EventSeverity_t eventSeverity); + +} +namespace SEVERITY { + static const EventSeverity_t INFO = 1; + static const EventSeverity_t LOW = 2; + static const EventSeverity_t MEDIUM = 3; + static const EventSeverity_t HIGH = 4; +} + +//Unfortunately, this does not work nicely because of the inability to define static classes in headers. +//struct Event { +// Event(uint8_t domain, uint8_t counter, EventSeverity_t severity) : +// id(domain*100+counter), severity(severity) { +// } +// EventId_t id; +// EventSeverity_t severity; +// static const EventSeverity_t INFO = 1; +// static const EventSeverity_t LOW = 2; +// static const EventSeverity_t MEDIUM = 3; +// static const EventSeverity_t HIGH = 4; +//}; + +#endif /* EVENTOBJECT_EVENT_H_ */ diff --git a/fsfw/events/EventManager.cpp b/fsfw/events/EventManager.cpp new file mode 100644 index 0000000..f60a8a6 --- /dev/null +++ b/fsfw/events/EventManager.cpp @@ -0,0 +1,166 @@ +#include "EventManager.h" +#include "EventMessage.h" +#include + +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../ipc/QueueFactory.h" +#include "../ipc/MutexFactory.h" + + +const uint16_t EventManager::POOL_SIZES[N_POOLS] = { + sizeof(EventMatchTree::Node), sizeof(EventIdRangeMatcher), + sizeof(ReporterRangeMatcher) }; +// If one checks registerListener calls, there are around 40 (to max 50) +// objects registering for certain events. +// Each listener requires 1 or 2 EventIdMatcher and 1 or 2 ReportRangeMatcher. +// So a good guess is 75 to a max of 100 pools required for each, which fits well. +const uint16_t EventManager::N_ELEMENTS[N_POOLS] = { + fsfwconfig::FSFW_EVENTMGMR_MATCHTREE_NODES , + fsfwconfig::FSFW_EVENTMGMT_EVENTIDMATCHERS, + fsfwconfig::FSFW_EVENTMGMR_RANGEMATCHERS }; + +EventManager::EventManager(object_id_t setObjectId) : + SystemObject(setObjectId), + factoryBackend(0, POOL_SIZES, N_ELEMENTS, false, true) { + mutex = MutexFactory::instance()->createMutex(); + eventReportQueue = QueueFactory::instance()->createMessageQueue( + MAX_EVENTS_PER_CYCLE, EventMessage::EVENT_MESSAGE_SIZE); +} + +EventManager::~EventManager() { + QueueFactory::instance()->deleteMessageQueue(eventReportQueue); + MutexFactory::instance()->deleteMutex(mutex); +} + +MessageQueueId_t EventManager::getEventReportQueue() { + return eventReportQueue->getId(); +} + +ReturnValue_t EventManager::performOperation(uint8_t opCode) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + while (result == HasReturnvaluesIF::RETURN_OK) { + EventMessage message; + result = eventReportQueue->receiveMessage(&message); + if (result == HasReturnvaluesIF::RETURN_OK) { +#ifdef DEBUG + printEvent(&message); +#endif + notifyListeners(&message); + } + } + return HasReturnvaluesIF::RETURN_OK; +} + +void EventManager::notifyListeners(EventMessage* message) { + lockMutex(); + for (auto iter = listenerList.begin(); iter != listenerList.end(); ++iter) { + if (iter->second.match(message)) { + MessageQueueSenderIF::sendMessage(iter->first, message, + message->getSender()); + } + } + unlockMutex(); +} + +ReturnValue_t EventManager::registerListener(MessageQueueId_t listener, +bool forwardAllButSelected) { + auto result = listenerList.insert( + std::pair(listener, + EventMatchTree(&factoryBackend, forwardAllButSelected))); + if (!result.second) { + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t EventManager::subscribeToEvent(MessageQueueId_t listener, + EventId_t event) { + return subscribeToEventRange(listener, event); +} + +ReturnValue_t EventManager::subscribeToAllEventsFrom(MessageQueueId_t listener, + object_id_t object) { + return subscribeToEventRange(listener, 0, 0, true, object); +} + +ReturnValue_t EventManager::subscribeToEventRange(MessageQueueId_t listener, + EventId_t idFrom, EventId_t idTo, bool idInverted, + object_id_t reporterFrom, object_id_t reporterTo, + bool reporterInverted) { + auto iter = listenerList.find(listener); + if (iter == listenerList.end()) { + return LISTENER_NOT_FOUND; + } + lockMutex(); + ReturnValue_t result = iter->second.addMatch(idFrom, idTo, idInverted, + reporterFrom, reporterTo, reporterInverted); + unlockMutex(); + return result; +} + +ReturnValue_t EventManager::unsubscribeFromEventRange(MessageQueueId_t listener, + EventId_t idFrom, EventId_t idTo, bool idInverted, + object_id_t reporterFrom, object_id_t reporterTo, + bool reporterInverted) { + auto iter = listenerList.find(listener); + if (iter == listenerList.end()) { + return LISTENER_NOT_FOUND; + } + lockMutex(); + ReturnValue_t result = iter->second.removeMatch(idFrom, idTo, idInverted, + reporterFrom, reporterTo, reporterInverted); + unlockMutex(); + return result; +} + +#ifdef DEBUG + +void EventManager::printEvent(EventMessage* message) { + const char *string = 0; + switch (message->getSeverity()) { + case SEVERITY::INFO: +#ifdef DEBUG_INFO_EVENT + string = translateObject(message->getReporter()); + sif::info << "EVENT: "; + if (string != 0) { + sif::info << string; + } else { + sif::info << "0x" << std::hex << message->getReporter() << std::dec; + } + sif::info << " reported " << translateEvents(message->getEvent()) << " (" + << std::dec << message->getEventId() << std::hex << ") P1: 0x" + << message->getParameter1() << " P2: 0x" + << message->getParameter2() << std::dec << std::endl; +#endif + break; + default: + string = translateObject(message->getReporter()); + sif::debug << "EventManager: "; + if (string != 0) { + sif::debug << string; + } + else { + sif::debug << "0x" << std::hex << message->getReporter() << std::dec; + } + sif::debug << " reported " << translateEvents(message->getEvent()) + << " (" << std::dec << message->getEventId() << ") " + << std::endl; + + sif::debug << std::hex << "P1 Hex: 0x" << message->getParameter1() + << ", P1 Dec: " << std::dec << message->getParameter1() + << std::endl; + sif::debug << std::hex << "P2 Hex: 0x" << message->getParameter2() + << ", P2 Dec: " << std::dec << message->getParameter2() + << std::endl; + break; + } +} +#endif + +void EventManager::lockMutex() { + mutex->lockMutex(MutexIF::BLOCKING); +} + +void EventManager::unlockMutex() { + mutex->unlockMutex(); +} diff --git a/fsfw/events/EventManager.h b/fsfw/events/EventManager.h new file mode 100644 index 0000000..2602aeb --- /dev/null +++ b/fsfw/events/EventManager.h @@ -0,0 +1,67 @@ +#ifndef EVENTMANAGER_H_ +#define EVENTMANAGER_H_ + +#include "eventmatching/EventMatchTree.h" +#include "EventManagerIF.h" +#include "../objectmanager/SystemObject.h" +#include "../storagemanager/LocalPool.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../ipc/MessageQueueIF.h" +#include "../ipc/MutexIF.h" +#include + +#ifdef DEBUG +// forward declaration, should be implemented by mission +extern const char* translateObject(object_id_t object); +extern const char* translateEvents(Event event); +#endif + +class EventManager: public EventManagerIF, + public ExecutableObjectIF, + public SystemObject { +public: + static const uint16_t MAX_EVENTS_PER_CYCLE = 80; + + EventManager(object_id_t setObjectId); + virtual ~EventManager(); + + MessageQueueId_t getEventReportQueue(); + + ReturnValue_t registerListener(MessageQueueId_t listener, bool forwardAllButSelected = false); + ReturnValue_t subscribeToEvent(MessageQueueId_t listener, EventId_t event); + ReturnValue_t subscribeToAllEventsFrom(MessageQueueId_t listener, + object_id_t object); + ReturnValue_t subscribeToEventRange(MessageQueueId_t listener, + EventId_t idFrom = 0, EventId_t idTo = 0, bool idInverted = false, + object_id_t reporterFrom = 0, object_id_t reporterTo = 0, + bool reporterInverted = false); + ReturnValue_t unsubscribeFromEventRange(MessageQueueId_t listener, + EventId_t idFrom = 0, EventId_t idTo = 0, bool idInverted = false, + object_id_t reporterFrom = 0, object_id_t reporterTo = 0, + bool reporterInverted = false); + ReturnValue_t performOperation(uint8_t opCode); +protected: + + MessageQueueIF* eventReportQueue = nullptr; + + std::map listenerList; + + MutexIF* mutex = nullptr; + + static const uint8_t N_POOLS = 3; + LocalPool factoryBackend; + static const uint16_t POOL_SIZES[N_POOLS]; + static const uint16_t N_ELEMENTS[N_POOLS]; + + void notifyListeners(EventMessage *message); + +#ifdef DEBUG + void printEvent(EventMessage *message); +#endif + + void lockMutex(); + + void unlockMutex(); +}; + +#endif /* EVENTMANAGER_H_ */ diff --git a/fsfw/events/EventManagerIF.h b/fsfw/events/EventManagerIF.h new file mode 100644 index 0000000..f9ac420 --- /dev/null +++ b/fsfw/events/EventManagerIF.h @@ -0,0 +1,52 @@ +#ifndef EVENTMANAGERIF_H_ +#define EVENTMANAGERIF_H_ + +#include "eventmatching/eventmatching.h" +#include "EventMessage.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +class EventManagerIF { +public: + + static const uint8_t INTERFACE_ID = CLASS_ID::EVENT_MANAGER_IF; + static const ReturnValue_t LISTENER_NOT_FOUND = MAKE_RETURN_CODE(1); + virtual ~EventManagerIF() { + } + + virtual MessageQueueId_t getEventReportQueue() = 0; + + virtual ReturnValue_t registerListener(MessageQueueId_t listener, bool forwardAllButSelected = false) = 0; + virtual ReturnValue_t subscribeToEvent(MessageQueueId_t listener, + EventId_t event) = 0; + virtual ReturnValue_t subscribeToAllEventsFrom(MessageQueueId_t listener, + object_id_t object) = 0; + virtual ReturnValue_t subscribeToEventRange(MessageQueueId_t listener, + EventId_t idFrom = 0, EventId_t idTo = 0, bool idInverted = false, + object_id_t reporterFrom = 0, object_id_t reporterTo = 0, + bool reporterInverted = false) = 0; + virtual ReturnValue_t unsubscribeFromEventRange(MessageQueueId_t listener, + EventId_t idFrom = 0, EventId_t idTo = 0, bool idInverted = false, + object_id_t reporterFrom = 0, object_id_t reporterTo = 0, + bool reporterInverted = false) = 0; + + static void triggerEvent(object_id_t reportingObject, Event event, + uint32_t parameter1 = 0, uint32_t parameter2 = 0, MessageQueueId_t sentFrom = 0) { + EventMessage message(event, reportingObject, parameter1, parameter2); + triggerEvent(&message, sentFrom); + } + static void triggerEvent(EventMessage* message, MessageQueueId_t sentFrom = 0) { + static MessageQueueId_t eventmanagerQueue = 0; + if (eventmanagerQueue == 0) { + EventManagerIF *eventmanager = objectManager->get( + objects::EVENT_MANAGER); + if (eventmanager != NULL) { + eventmanagerQueue = eventmanager->getEventReportQueue(); + } + } + MessageQueueSenderIF::sendMessage(eventmanagerQueue, message, sentFrom); + } + +}; + +#endif /* EVENTMANAGERIF_H_ */ diff --git a/fsfw/events/EventMessage.cpp b/fsfw/events/EventMessage.cpp new file mode 100644 index 0000000..b911aba --- /dev/null +++ b/fsfw/events/EventMessage.cpp @@ -0,0 +1,114 @@ +#include "EventMessage.h" +#include + +EventMessage::EventMessage() { + messageSize = EVENT_MESSAGE_SIZE; + clearEventMessage(); +} + +EventMessage::EventMessage(Event event, object_id_t reporter, + uint32_t parameter1, uint32_t parameter2) { + messageSize = EVENT_MESSAGE_SIZE; + setMessageId(EVENT_MESSAGE); + setEvent(event); + setReporter(reporter); + setParameter1(parameter1); + setParameter2(parameter2); +} + +EventMessage::~EventMessage() { +} + +Event EventMessage::getEvent() { + Event event; + memcpy(&event, getData(), sizeof(Event)); + return (event & 0xFFFFFF); +} + +void EventMessage::setEvent(Event event) { + Event tempEvent; + memcpy(&tempEvent, getData(), sizeof(Event)); + tempEvent = (tempEvent & 0xFF000000) + (event & 0xFFFFFF); + memcpy(getData(), &tempEvent, sizeof(Event)); +} + +uint8_t EventMessage::getMessageId() { + Event event; + memcpy(&event, getData(), sizeof(Event)); + return (event & 0xFF000000) >> 24; +} + +void EventMessage::setMessageId(uint8_t id) { + Event event; + memcpy(&event, getData(), sizeof(Event)); + event = (event & 0x00FFFFFF) + (id << 24); + memcpy(getData(), &event, sizeof(Event)); +} + +EventSeverity_t EventMessage::getSeverity() { + Event event; + memcpy(&event, getData(), sizeof(Event)); + return EVENT::getSeverity(event); +} + +void EventMessage::setSeverity(EventSeverity_t severity) { + Event event; + memcpy(&event, getData(), sizeof(Event)); + event = (event & 0xFF00FFFF) + (severity << 16); + memcpy(getData(), &event, sizeof(Event)); +} + +EventId_t EventMessage::getEventId() { + Event event; + memcpy(&event, getData(), sizeof(Event)); + return EVENT::getEventId(event); +} + +void EventMessage::setEventId(EventId_t eventId) { + Event event; + memcpy(&event, getData(), sizeof(Event)); + event = (event & 0xFFFF0000) + eventId; + memcpy(getData(), &event, sizeof(Event)); +} + +object_id_t EventMessage::getReporter() { + object_id_t parameter; + memcpy(¶meter, getData() + sizeof(Event), sizeof(object_id_t)); + return parameter; +} + +void EventMessage::setReporter(object_id_t reporter) { + memcpy(getData() + sizeof(Event), &reporter, sizeof(object_id_t)); +} + +uint32_t EventMessage::getParameter1() { + uint32_t parameter; + memcpy(¶meter, getData() + sizeof(Event) + sizeof(object_id_t), 4); + return parameter; +} + +void EventMessage::setParameter1(uint32_t parameter) { + memcpy(getData() + sizeof(Event) + sizeof(object_id_t), ¶meter, 4); +} + +uint32_t EventMessage::getParameter2() { + uint32_t parameter; + memcpy(¶meter, getData() + sizeof(Event) + sizeof(object_id_t) + 4, 4); + return parameter; +} + +void EventMessage::setParameter2(uint32_t parameter) { + memcpy(getData() + sizeof(Event) + sizeof(object_id_t) + 4, ¶meter, 4); +} + +void EventMessage::clearEventMessage() { + setEvent(INVALID_EVENT); +} + +bool EventMessage::isClearedEventMessage() { + return getEvent() == INVALID_EVENT; +} + +size_t EventMessage::getMinimumMessageSize() { + return EVENT_MESSAGE_SIZE; +} diff --git a/fsfw/events/EventMessage.h b/fsfw/events/EventMessage.h new file mode 100644 index 0000000..4d003bd --- /dev/null +++ b/fsfw/events/EventMessage.h @@ -0,0 +1,52 @@ +#ifndef EVENTMESSAGE_H_ +#define EVENTMESSAGE_H_ + +#include "Event.h" +#include "../ipc/MessageQueueMessage.h" +#include "../objectmanager/ObjectManagerIF.h" + +/** + * Passing on events through IPC. + * Event Id, severity and message type retrieval is a bit difficult. + */ +class EventMessage: public MessageQueueMessage { +public: + static const uint8_t EVENT_MESSAGE = 0; //!< Normal event + static const uint8_t CONFIRMATION_REQUEST = 1; //!< Request to parent if event is caused by child or not. + static const uint8_t YOUR_FAULT = 2; //!< The fault was caused by child, parent believes it's ok. + static const uint8_t MY_FAULT = 3; //!< The fault was caused by the parent, child is ok. + //Add other messageIDs here if necessary. + static const uint8_t EVENT_MESSAGE_SIZE = HEADER_SIZE + sizeof(Event) + + 3 * sizeof(uint32_t); + + EventMessage(); +// EventMessage(EventId_t event, EventSeverity_t severity, +// object_id_t reporter, uint32_t parameter1, uint32_t parameter2 = 0); + EventMessage(Event event, object_id_t reporter, uint32_t parameter1, + uint32_t parameter2 = 0); + virtual ~EventMessage(); + Event getEvent(); + void setEvent(Event event); + uint8_t getMessageId(); + void setMessageId(uint8_t id); + EventSeverity_t getSeverity(); + void setSeverity(EventSeverity_t severity); + EventId_t getEventId(); + void setEventId(EventId_t event); + object_id_t getReporter(); + void setReporter(object_id_t reporter); + uint32_t getParameter1(); + void setParameter1(uint32_t parameter); + uint32_t getParameter2(); + void setParameter2(uint32_t parameter); + + void clearEventMessage(); + bool isClearedEventMessage(); + +protected: + static const Event INVALID_EVENT = 0; + virtual size_t getMinimumMessageSize(); + +}; + +#endif /* EVENTMESSAGE_H_ */ diff --git a/fsfw/events/EventReportingProxyIF.h b/fsfw/events/EventReportingProxyIF.h new file mode 100644 index 0000000..7a96e3d --- /dev/null +++ b/fsfw/events/EventReportingProxyIF.h @@ -0,0 +1,19 @@ +#ifndef FRAMEWORK_EVENTS_EVENTREPORTINGPROXYIF_H_ +#define FRAMEWORK_EVENTS_EVENTREPORTINGPROXYIF_H_ + +#include "Event.h" + + +class EventReportingProxyIF { +public: + virtual ~EventReportingProxyIF() { + } + + virtual void forwardEvent(Event event, uint32_t parameter1 = 0, uint32_t parameter2 = 0) const = 0; + +}; + + + + +#endif /* FRAMEWORK_EVENTS_EVENTREPORTINGPROXYIF_H_ */ diff --git a/fsfw/events/eventmatching/EventIdRangeMatcher.cpp b/fsfw/events/eventmatching/EventIdRangeMatcher.cpp new file mode 100644 index 0000000..974567d --- /dev/null +++ b/fsfw/events/eventmatching/EventIdRangeMatcher.cpp @@ -0,0 +1,12 @@ +#include "EventIdRangeMatcher.h" + +EventIdRangeMatcher::EventIdRangeMatcher(EventId_t lower, EventId_t upper, + bool inverted) : EventRangeMatcherBase(lower, upper, inverted) { +} + +EventIdRangeMatcher::~EventIdRangeMatcher() { +} + +bool EventIdRangeMatcher::match(EventMessage* message) { + return rangeMatcher.match(message->getEventId()); +} diff --git a/fsfw/events/eventmatching/EventIdRangeMatcher.h b/fsfw/events/eventmatching/EventIdRangeMatcher.h new file mode 100644 index 0000000..540d372 --- /dev/null +++ b/fsfw/events/eventmatching/EventIdRangeMatcher.h @@ -0,0 +1,13 @@ +#ifndef FRAMEWORK_EVENTS_EVENTMATCHING_EVENTIDRANGEMATCHER_H_ +#define FRAMEWORK_EVENTS_EVENTMATCHING_EVENTIDRANGEMATCHER_H_ + +#include "EventRangeMatcherBase.h" + +class EventIdRangeMatcher: public EventRangeMatcherBase { +public: + EventIdRangeMatcher(EventId_t lower, EventId_t upper, bool inverted); + ~EventIdRangeMatcher(); + bool match(EventMessage* message); +}; + +#endif /* FRAMEWORK_EVENTS_EVENTMATCHING_EVENTIDRANGEMATCHER_H_ */ diff --git a/fsfw/events/eventmatching/EventMatchTree.cpp b/fsfw/events/eventmatching/EventMatchTree.cpp new file mode 100644 index 0000000..55e4083 --- /dev/null +++ b/fsfw/events/eventmatching/EventMatchTree.cpp @@ -0,0 +1,149 @@ +#include "EventIdRangeMatcher.h" +#include "EventMatchTree.h" +#include "ReporterRangeMatcher.h" +#include "SeverityRangeMatcher.h" + +EventMatchTree::EventMatchTree(StorageManagerIF* storageBackend, + bool invertedMatch) : + MatchTree(end(), 1), factory(storageBackend), invertedMatch( + invertedMatch) { +} + +EventMatchTree::~EventMatchTree() { +} + +bool EventMatchTree::match(EventMessage* number) { + if (invertedMatch) { + return !MatchTree::match(number); + } else { + return MatchTree::match(number); + } +} + +ReturnValue_t EventMatchTree::addMatch(EventId_t idFrom, EventId_t idTo, + bool idInverted, object_id_t reporterFrom, object_id_t reporterTo, + bool reporterInverted) { + if (idFrom == 0) { + //Assuming all events shall be forwarded. + idTo = 0; + idInverted = true; + } + if (idTo == 0) { + idTo = idFrom; + } + iterator lastTest; + ReturnValue_t result = findOrInsertRangeMatcher(begin(), idFrom, idTo, idInverted, &lastTest); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (reporterFrom == 0) { + //No need to add another AND branch + return RETURN_OK; + } + if (reporterTo == 0) { + reporterTo = reporterFrom; + } + return findOrInsertRangeMatcher( + lastTest.left(), reporterFrom, reporterTo, reporterInverted, + &lastTest); +} + +ReturnValue_t EventMatchTree::removeMatch(EventId_t idFrom, EventId_t idTo, + bool idInverted, object_id_t reporterFrom, object_id_t reporterTo, + bool reporterInverted) { + + iterator foundElement; + + if (idFrom == 0) { + //Assuming a "forward all events" request. + idTo = 0; + idInverted = true; + } + if (idTo == 0) { + idTo = idFrom; + } + foundElement = findRangeMatcher(begin(), + idFrom, idTo, idInverted); + if (foundElement == end()) { + return NO_MATCH; //Can't tell if too detailed or just not found. + } + if (reporterFrom == 0) { + if (foundElement.left() == end()) { + return removeElementAndReconnectChildren(foundElement); + } else { + return TOO_GENERAL_REQUEST; + } + } + if (reporterTo == 0) { + reporterTo = reporterFrom; + } + foundElement = findRangeMatcher( + foundElement.left(), reporterFrom, reporterTo, reporterInverted); + if (foundElement == end()) { + return NO_MATCH; + } else { + return removeElementAndReconnectChildren(foundElement); + } +} + +template +inline ReturnValue_t EventMatchTree::findOrInsertRangeMatcher(iterator start, + VALUE_T idFrom, VALUE_T idTo, bool inverted, iterator* lastTest) { + bool attachToBranch = AND; + iterator iter = start; + while (iter != end()) { + INSERTION_T* matcher = static_cast(*iter); + attachToBranch = OR; + *lastTest = iter; + if ((matcher->rangeMatcher.lowerBound == idFrom) + && (matcher->rangeMatcher.upperBound == idTo) + && (matcher->rangeMatcher.inverted == inverted)) { + return RETURN_OK; + } else { + iter = iter.right(); + } + } + //Only reached if nothing was found. + SerializeableMatcherIF* newContent = factory.generate< + INSERTION_T>(idFrom, idTo, inverted); + if (newContent == NULL) { + return FULL; + } + Node* newNode = factory.generate(newContent); + if (newNode == NULL) { + //Need to make sure partially generated content is deleted, otherwise, that's a leak. + factory.destroy(static_cast(newContent)); + return FULL; + } + *lastTest = insert(attachToBranch, *lastTest, newNode); + if (*lastTest == end()) { + //This actaully never fails, so creating a dedicated returncode seems an overshoot. + return RETURN_FAILED; + } + return RETURN_OK; +} + +template +EventMatchTree::iterator EventMatchTree::findRangeMatcher(iterator start, + VALUE_T idFrom, VALUE_T idTo, bool inverted) { + iterator iter = start; + while (iter != end()) { + INSERTION_T* matcher = static_cast(*iter); + if ((matcher->rangeMatcher.lowerBound == idFrom) + && (matcher->rangeMatcher.upperBound == idTo) + && (matcher->rangeMatcher.inverted == inverted)) { + break; + } else { + iter = iter.right(); //next OR element + } + } + return iter; +} + +ReturnValue_t EventMatchTree::cleanUpElement(iterator position) { + factory.destroy(position.element->value); + //If deletion fails, delete element anyway, nothing we can do. + //SHOULDO: Throw event, or write debug output. + return factory.destroy(position.element); +} diff --git a/fsfw/events/eventmatching/EventMatchTree.h b/fsfw/events/eventmatching/EventMatchTree.h new file mode 100644 index 0000000..82a6de8 --- /dev/null +++ b/fsfw/events/eventmatching/EventMatchTree.h @@ -0,0 +1,36 @@ +#ifndef FRAMEWORK_EVENTS_EVENTMATCHING_EVENTMATCHTREE_H_ +#define FRAMEWORK_EVENTS_EVENTMATCHING_EVENTMATCHTREE_H_ + +#include "../../container/PlacementFactory.h" +#include "../../events/EventMessage.h" +#include "../../globalfunctions/matching/MatchTree.h" +#include "../../returnvalues/HasReturnvaluesIF.h" +class StorageManagerIF; + +class EventMatchTree: public MatchTree, public HasReturnvaluesIF { +public: + EventMatchTree(StorageManagerIF* storageBackend, bool invertedMatch); + virtual ~EventMatchTree(); + ReturnValue_t addMatch(EventId_t idFrom = 0, EventId_t idTo = 0, bool idInverted = false, + object_id_t reporterFrom = 0, object_id_t reporterTo = 0, + bool reporterInverted = false); + ReturnValue_t removeMatch(EventId_t idFrom = 0, EventId_t idTo = 0, bool idInverted = false, + object_id_t reporterFrom = 0, object_id_t reporterTo = 0, + bool reporterInverted = false); + bool match(EventMessage* number); +protected: + ReturnValue_t cleanUpElement(iterator position); +private: + PlacementFactory factory; + bool invertedMatch; + template + ReturnValue_t findOrInsertRangeMatcher(iterator start, VALUE_T idFrom, + VALUE_T idTo, bool inverted, iterator* lastTest); + + template + iterator findRangeMatcher(iterator start, VALUE_T idFrom, VALUE_T idTo, + bool inverted); + +}; + +#endif /* FRAMEWORK_EVENTS_EVENTMATCHING_EVENTMATCHTREE_H_ */ diff --git a/fsfw/events/eventmatching/EventRangeMatcherBase.h b/fsfw/events/eventmatching/EventRangeMatcherBase.h new file mode 100644 index 0000000..e9d57f1 --- /dev/null +++ b/fsfw/events/eventmatching/EventRangeMatcherBase.h @@ -0,0 +1,29 @@ +#ifndef FRAMEWORK_EVENTS_EVENTMATCHING_EVENTRANGEMATCHERBASE_H_ +#define FRAMEWORK_EVENTS_EVENTMATCHING_EVENTRANGEMATCHERBASE_H_ + +#include "../../events/EventMessage.h" +#include "../../globalfunctions/matching/RangeMatcher.h" +#include "../../globalfunctions/matching/SerializeableMatcherIF.h" + +template +class EventRangeMatcherBase: public SerializeableMatcherIF { + friend class EventMatchTree; +public: + EventRangeMatcherBase(T from, T till, bool inverted) : rangeMatcher(from, till, inverted) { } + virtual ~EventRangeMatcherBase() { } + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + return rangeMatcher.serialize(buffer, size, maxSize, streamEndianness); + } + size_t getSerializedSize() const { + return rangeMatcher.getSerializedSize(); + } + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + return rangeMatcher.deSerialize(buffer, size, streamEndianness); + } +protected: + RangeMatcher rangeMatcher; +}; + +#endif /* FRAMEWORK_EVENTS_EVENTMATCHING_EVENTRANGEMATCHERBASE_H_ */ diff --git a/fsfw/events/eventmatching/ReporterRangeMatcher.cpp b/fsfw/events/eventmatching/ReporterRangeMatcher.cpp new file mode 100644 index 0000000..6117972 --- /dev/null +++ b/fsfw/events/eventmatching/ReporterRangeMatcher.cpp @@ -0,0 +1,13 @@ +#include "ReporterRangeMatcher.h" + +ReporterRangeMatcher::ReporterRangeMatcher(object_id_t lower, object_id_t upper, + bool inverted) : EventRangeMatcherBase(lower, upper, inverted) { +} + +ReporterRangeMatcher::~ReporterRangeMatcher() { + +} + +bool ReporterRangeMatcher::match(EventMessage* message) { + return rangeMatcher.match(message->getReporter()); +} diff --git a/fsfw/events/eventmatching/ReporterRangeMatcher.h b/fsfw/events/eventmatching/ReporterRangeMatcher.h new file mode 100644 index 0000000..9a0a5fc --- /dev/null +++ b/fsfw/events/eventmatching/ReporterRangeMatcher.h @@ -0,0 +1,13 @@ +#ifndef FRAMEWORK_EVENTS_EVENTMATCHING_REPORTERRANGEMATCHER_H_ +#define FRAMEWORK_EVENTS_EVENTMATCHING_REPORTERRANGEMATCHER_H_ + +#include "EventRangeMatcherBase.h" + +class ReporterRangeMatcher: public EventRangeMatcherBase { +public: + ReporterRangeMatcher(object_id_t lower, object_id_t upper, bool inverted); + ~ReporterRangeMatcher(); + bool match(EventMessage* message); +}; + +#endif /* FRAMEWORK_EVENTS_EVENTMATCHING_REPORTERRANGEMATCHER_H_ */ diff --git a/fsfw/events/eventmatching/SeverityRangeMatcher.cpp b/fsfw/events/eventmatching/SeverityRangeMatcher.cpp new file mode 100644 index 0000000..6fd3827 --- /dev/null +++ b/fsfw/events/eventmatching/SeverityRangeMatcher.cpp @@ -0,0 +1,14 @@ +#include "SeverityRangeMatcher.h" +#include "../../events/EventMessage.h" +#include "../../serialize/SerializeAdapter.h" + +SeverityRangeMatcher::SeverityRangeMatcher(EventSeverity_t from, + EventSeverity_t till, bool inverted) : EventRangeMatcherBase(from, till, inverted) { +} + +SeverityRangeMatcher::~SeverityRangeMatcher() { +} + +bool SeverityRangeMatcher::match(EventMessage* message) { + return rangeMatcher.match(message->getSeverity()); +} diff --git a/fsfw/events/eventmatching/SeverityRangeMatcher.h b/fsfw/events/eventmatching/SeverityRangeMatcher.h new file mode 100644 index 0000000..3f7cc41 --- /dev/null +++ b/fsfw/events/eventmatching/SeverityRangeMatcher.h @@ -0,0 +1,13 @@ +#ifndef FRAMEWORK_EVENTS_EVENTMATCHING_SEVERITYRANGEMATCHER_H_ +#define FRAMEWORK_EVENTS_EVENTMATCHING_SEVERITYRANGEMATCHER_H_ + +#include "EventRangeMatcherBase.h" + +class SeverityRangeMatcher: public EventRangeMatcherBase { +public: + SeverityRangeMatcher(EventSeverity_t from, EventSeverity_t till, bool inverted); + ~SeverityRangeMatcher(); + bool match(EventMessage* message); +}; + +#endif /* FRAMEWORK_EVENTS_EVENTMATCHING_SEVERITYRANGEMATCHER_H_ */ diff --git a/fsfw/events/eventmatching/eventmatching.h b/fsfw/events/eventmatching/eventmatching.h new file mode 100644 index 0000000..7a87e17 --- /dev/null +++ b/fsfw/events/eventmatching/eventmatching.h @@ -0,0 +1,10 @@ +#ifndef EVENTMATCHING_H_ +#define EVENTMATCHING_H_ + +#include "EventIdRangeMatcher.h" +#include "EventMatchTree.h" +#include "ReporterRangeMatcher.h" +#include "SeverityRangeMatcher.h" + + +#endif /* EVENTMATCHING_H_ */ diff --git a/fsfw/events/fwSubsystemIdRanges.h b/fsfw/events/fwSubsystemIdRanges.h new file mode 100644 index 0000000..1b0b238 --- /dev/null +++ b/fsfw/events/fwSubsystemIdRanges.h @@ -0,0 +1,30 @@ +#ifndef FSFW_EVENTS_FWSUBSYSTEMIDRANGES_H_ +#define FSFW_EVENTS_FWSUBSYSTEMIDRANGES_H_ + +namespace SUBSYSTEM_ID { +enum { + MEMORY = 22, + OBSW = 26, + CDH = 28, + TCS_1 = 59, + PCDU_1 = 42, + PCDU_2 = 43, + HEATER = 50, + T_SENSORS = 52, + FDIR = 70, + FDIR_1 = 71, + FDIR_2 = 72, + HK = 73, + SYSTEM_MANAGER = 74, + SYSTEM_MANAGER_1 = 75, + SYSTEM_1 = 79, + PUS_SERVICE_1 = 80, + PUS_SERVICE_9 = 89, + PUS_SERVICE_17 = 97, + FW_SUBSYSTEM_ID_RANGE +}; +} + + + +#endif /* FSFW_EVENTS_FWSUBSYSTEMIDRANGES_H_ */ diff --git a/fsfw/fdir/ConfirmsFailuresIF.h b/fsfw/fdir/ConfirmsFailuresIF.h new file mode 100644 index 0000000..1441146 --- /dev/null +++ b/fsfw/fdir/ConfirmsFailuresIF.h @@ -0,0 +1,19 @@ +#ifndef FRAMEWORK_FDIR_CONFIRMSFAILURESIF_H_ +#define FRAMEWORK_FDIR_CONFIRMSFAILURESIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +class ConfirmsFailuresIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::HANDLES_FAILURES_IF; + static const ReturnValue_t YOUR_FAULT = MAKE_RETURN_CODE(0); + static const ReturnValue_t MY_FAULT = MAKE_RETURN_CODE(1); + static const ReturnValue_t CONFIRM_LATER = MAKE_RETURN_CODE(2); + virtual ~ConfirmsFailuresIF() {} + virtual MessageQueueId_t getEventReceptionQueue() = 0; +}; + + + +#endif /* FRAMEWORK_FDIR_CONFIRMSFAILURESIF_H_ */ diff --git a/fsfw/fdir/EventCorrelation.cpp b/fsfw/fdir/EventCorrelation.cpp new file mode 100644 index 0000000..d60fc6c --- /dev/null +++ b/fsfw/fdir/EventCorrelation.cpp @@ -0,0 +1,44 @@ +#include "EventCorrelation.h" + +EventCorrelation::EventCorrelation(uint32_t timeout) : + eventPending(false) { + correlationTimer.setTimeout(timeout); +} + +EventCorrelation::~EventCorrelation() { +} + +EventCorrelation::State EventCorrelation::doesEventCorrelate() { + if (correlationTimer.isBusy()) { + eventPending = false; + return CORRELATED; + } else { + if (eventPending) { + return ALREADY_STARTED; + } else { + eventPending = true; + correlationTimer.resetTimer(); + return CORRELATION_STARTED; + } + } +} + +bool EventCorrelation::isEventPending() { + if (eventPending) { + eventPending = false; + return true; + } else { + correlationTimer.resetTimer(); + return false; + } +} + +bool EventCorrelation::hasPendingEventTimedOut() { + if (correlationTimer.hasTimedOut()) { + bool temp = eventPending; + eventPending = false; + return temp; + } else { + return false; + } +} diff --git a/fsfw/fdir/EventCorrelation.h b/fsfw/fdir/EventCorrelation.h new file mode 100644 index 0000000..b8793ed --- /dev/null +++ b/fsfw/fdir/EventCorrelation.h @@ -0,0 +1,23 @@ +#ifndef FRAMEWORK_FDIR_EVENTCORRELATION_H_ +#define FRAMEWORK_FDIR_EVENTCORRELATION_H_ + +#include "../timemanager/Countdown.h" + +class EventCorrelation { +public: + enum State { + CORRELATION_STARTED, + CORRELATED, + ALREADY_STARTED + }; + EventCorrelation(uint32_t timeout); + ~EventCorrelation(); + EventCorrelation::State doesEventCorrelate(); + bool isEventPending(); + bool hasPendingEventTimedOut(); + Countdown correlationTimer; +private: + bool eventPending; +}; + +#endif /* FRAMEWORK_FDIR_EVENTCORRELATION_H_ */ diff --git a/fsfw/fdir/FailureIsolationBase.cpp b/fsfw/fdir/FailureIsolationBase.cpp new file mode 100644 index 0000000..f3b34f0 --- /dev/null +++ b/fsfw/fdir/FailureIsolationBase.cpp @@ -0,0 +1,164 @@ +#include "../events/EventManagerIF.h" +#include "FailureIsolationBase.h" +#include "../health/HasHealthIF.h" +#include "../health/HealthMessage.h" +#include "../ipc/QueueFactory.h" +#include "../objectmanager/ObjectManagerIF.h" + +FailureIsolationBase::FailureIsolationBase(object_id_t owner, + object_id_t parent, uint8_t messageDepth, uint8_t parameterDomainBase) : + ownerId(owner), faultTreeParent(parent), + parameterDomainBase(parameterDomainBase) { + eventQueue = QueueFactory::instance()->createMessageQueue(messageDepth, + EventMessage::EVENT_MESSAGE_SIZE); +} + +FailureIsolationBase::~FailureIsolationBase() { + QueueFactory::instance()->deleteMessageQueue(eventQueue); +} + +ReturnValue_t FailureIsolationBase::initialize() { + EventManagerIF* manager = objectManager->get( + objects::EVENT_MANAGER); + if (manager == nullptr) { + sif::error << "FailureIsolationBase::initialize: Event Manager has not" + " been initialized!" << std::endl; + return RETURN_FAILED; + } + ReturnValue_t result = manager->registerListener(eventQueue->getId()); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (ownerId != objects::NO_OBJECT) { + result = manager->subscribeToAllEventsFrom(eventQueue->getId(), ownerId); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + owner = objectManager->get(ownerId); + if (owner == nullptr) { + sif::error << "FailureIsolationBase::intialize: Owner object " + "invalid. Make sure it implements HasHealthIF" << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + } + if (faultTreeParent != objects::NO_OBJECT) { + ConfirmsFailuresIF* parentIF = objectManager->get( + faultTreeParent); + if (parentIF == nullptr) { + sif::error << "FailureIsolationBase::intialize: Parent object" + << "invalid." << std::endl; + sif::error << "Make sure it implements ConfirmsFailuresIF." + << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + return RETURN_FAILED; + } + eventQueue->setDefaultDestination(parentIF->getEventReceptionQueue()); + } + return RETURN_OK; +} + +void FailureIsolationBase::checkForFailures() { + EventMessage event; + for (ReturnValue_t result = eventQueue->receiveMessage(&event); + result == RETURN_OK; result = eventQueue->receiveMessage(&event)) { + if (event.getSender() == eventQueue->getId()) { + //We already got this event, because we sent it. + continue; + } + switch (event.getMessageId()) { + case EventMessage::EVENT_MESSAGE: + if (isFdirDisabledForSeverity(event.getSeverity())) { + //We do not handle events when disabled. + continue; + } + eventReceived(&event); + break; + case EventMessage::CONFIRMATION_REQUEST: + doConfirmFault(&event); + break; + case EventMessage::YOUR_FAULT: + eventConfirmed(&event); + break; + case EventMessage::MY_FAULT: + wasParentsFault(&event); + break; + default: + break; + } + } + decrementFaultCounters(); +} + +void FailureIsolationBase::setOwnerHealth(HasHealthIF::HealthState health) { + if (owner != NULL) { + owner->setHealth(health); + } + //else: owner has no health. + +} + +MessageQueueId_t FailureIsolationBase::getEventReceptionQueue() { + return eventQueue->getId(); +} + +ReturnValue_t FailureIsolationBase::sendConfirmationRequest(EventMessage* event, + MessageQueueId_t destination) { + event->setMessageId(EventMessage::CONFIRMATION_REQUEST); + if (destination != MessageQueueIF::NO_QUEUE) { + return eventQueue->sendMessage(destination, event); + } else if (faultTreeParent != objects::NO_OBJECT) { + return eventQueue->sendToDefault(event); + } + return RETURN_FAILED; +} + +void FailureIsolationBase::eventConfirmed(EventMessage* event) { +} + +void FailureIsolationBase::wasParentsFault(EventMessage* event) { +} + +void FailureIsolationBase::doConfirmFault(EventMessage* event) { + ReturnValue_t result = confirmFault(event); + if (result == YOUR_FAULT) { + event->setMessageId(EventMessage::YOUR_FAULT); + eventQueue->reply(event); + } else if (result == MY_FAULT) { + event->setMessageId(EventMessage::MY_FAULT); + eventQueue->reply(event); + } else { + + } +} + +ReturnValue_t FailureIsolationBase::confirmFault(EventMessage* event) { + return YOUR_FAULT; +} + +void FailureIsolationBase::triggerEvent(Event event, uint32_t parameter1, + uint32_t parameter2) { + //With this mechanism, all events are disabled for a certain device. + //That's not so good for visibility. + if (isFdirDisabledForSeverity(EVENT::getSeverity(event))) { + return; + } + EventMessage message(event, ownerId, parameter1, parameter2); + EventManagerIF::triggerEvent(&message, eventQueue->getId()); + eventReceived(&message); +} + +bool FailureIsolationBase::isFdirDisabledForSeverity(EventSeverity_t severity) { + if ((owner != NULL) && (severity != SEVERITY::INFO)) { + if (owner->getHealth() == HasHealthIF::EXTERNAL_CONTROL) { + //External control disables handling of fault messages. + return true; + } + } + return false; +} + +void FailureIsolationBase::throwFdirEvent(Event event, uint32_t parameter1, + uint32_t parameter2) { + EventMessage message(event, ownerId, parameter1, parameter2); + EventManagerIF::triggerEvent(&message, eventQueue->getId()); +} diff --git a/fsfw/fdir/FailureIsolationBase.h b/fsfw/fdir/FailureIsolationBase.h new file mode 100644 index 0000000..5b2c099 --- /dev/null +++ b/fsfw/fdir/FailureIsolationBase.h @@ -0,0 +1,56 @@ +#ifndef FRAMEWORK_FDIR_FAILUREISOLATIONBASE_H_ +#define FRAMEWORK_FDIR_FAILUREISOLATIONBASE_H_ + +#include "../events/EventMessage.h" +#include "ConfirmsFailuresIF.h" +#include "FaultCounter.h" +#include "../health/HealthMessage.h" +#include "../parameters/HasParametersIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MessageQueueIF.h" + +class FailureIsolationBase: public HasReturnvaluesIF, + public ConfirmsFailuresIF, + public HasParametersIF { +public: + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::FDIR_1; + static const Event FDIR_CHANGED_STATE = MAKE_EVENT(1, SEVERITY::INFO); //!< FDIR has an internal state, which changed from par2 (oldState) to par1 (newState). + static const Event FDIR_STARTS_RECOVERY = MAKE_EVENT(2, SEVERITY::MEDIUM); //!< FDIR tries to restart device. Par1: event that caused recovery. + static const Event FDIR_TURNS_OFF_DEVICE = MAKE_EVENT(3, SEVERITY::MEDIUM); //!< FDIR turns off device. Par1: event that caused recovery. + + FailureIsolationBase(object_id_t owner, + object_id_t parent = objects::NO_OBJECT, + uint8_t messageDepth = 10, uint8_t parameterDomainBase = 0xF0); + + virtual ~FailureIsolationBase(); + virtual ReturnValue_t initialize(); + + /** + * This is called by the DHB in performOperation() + */ + void checkForFailures(); + MessageQueueId_t getEventReceptionQueue() override; + virtual void triggerEvent(Event event, uint32_t parameter1 = 0, + uint32_t parameter2 = 0); +protected: + MessageQueueIF* eventQueue = nullptr; + object_id_t ownerId; + HasHealthIF* owner = nullptr; + object_id_t faultTreeParent; + uint8_t parameterDomainBase; + void setOwnerHealth(HasHealthIF::HealthState health); + virtual ReturnValue_t eventReceived(EventMessage* event) = 0; + virtual void eventConfirmed(EventMessage* event); + virtual void wasParentsFault(EventMessage* event); + virtual ReturnValue_t confirmFault(EventMessage* event); + virtual void decrementFaultCounters() = 0; + ReturnValue_t sendConfirmationRequest(EventMessage* event, + MessageQueueId_t destination = MessageQueueIF::NO_QUEUE); + void throwFdirEvent(Event event, uint32_t parameter1 = 0, + uint32_t parameter2 = 0); +private: + void doConfirmFault(EventMessage* event);bool isFdirDisabledForSeverity( + EventSeverity_t severity); +}; + +#endif /* FRAMEWORK_FDIR_FAILUREISOLATIONBASE_H_ */ diff --git a/fsfw/fdir/FaultCounter.cpp b/fsfw/fdir/FaultCounter.cpp new file mode 100644 index 0000000..443adb5 --- /dev/null +++ b/fsfw/fdir/FaultCounter.cpp @@ -0,0 +1,86 @@ +#include "FaultCounter.h" + +FaultCounter::FaultCounter(uint32_t failureThreshold, uint32_t decrementAfterMs, + uint8_t setParameterDomain) : + parameterDomain(setParameterDomain), timer(), faultCount(0), failureThreshold( + failureThreshold) { + timer.setTimeout(decrementAfterMs); +} + +FaultCounter::~FaultCounter() { +} + +void FaultCounter::increment(uint32_t amount) { + if (faultCount == 0) { + timer.resetTimer(); + } + faultCount += amount; +} + +bool FaultCounter::checkForDecrement() { + if (timer.hasTimedOut()) { + timer.resetTimer(); + if (faultCount > 0) { + faultCount--; + return true; + } + } + return false; +} + +bool FaultCounter::incrementAndCheck(uint32_t amount) { + increment(amount); + return aboveThreshold(); +} + +bool FaultCounter::aboveThreshold() { + if (faultCount > failureThreshold) { + faultCount = 0; + return true; + } else { + return false; + } +} + +void FaultCounter::clear() { + faultCount = 0; +} + +void FaultCounter::setFailureThreshold(uint32_t failureThreshold) { + this->failureThreshold = failureThreshold; +} + +void FaultCounter::setFaultDecrementTimeMs(uint32_t timeMs) { + timer.setTimeout(timeMs); +} + +FaultCounter::FaultCounter() : + parameterDomain(0), timer(), faultCount(0), failureThreshold(0) { +} + +ReturnValue_t FaultCounter::getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper* parameterWrapper, const ParameterWrapper* newValues, + uint16_t startAtIndex) { + if (domainId != parameterDomain) { + return INVALID_DOMAIN_ID; + } + + switch (parameterId) { + case 0: + parameterWrapper->set(failureThreshold); + break; + case 1: + parameterWrapper->set(faultCount); + break; + case 2: + parameterWrapper->set(timer.timeout); + break; + default: + return INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; +} + +void FaultCounter::setParameterDomain(uint8_t domain) { + parameterDomain = domain; +} diff --git a/fsfw/fdir/FaultCounter.h b/fsfw/fdir/FaultCounter.h new file mode 100644 index 0000000..dc929de --- /dev/null +++ b/fsfw/fdir/FaultCounter.h @@ -0,0 +1,38 @@ +#ifndef FRAMEWORK_FDIR_FAULTCOUNTER_H_ +#define FRAMEWORK_FDIR_FAULTCOUNTER_H_ + +#include "../parameters/HasParametersIF.h" +#include "../timemanager/Countdown.h" + +class FaultCounter: public HasParametersIF { +public: + FaultCounter(); + FaultCounter(uint32_t failureThreshold, uint32_t decrementAfterMs, + uint8_t setParameterDomain = 0); + virtual ~FaultCounter(); + + bool incrementAndCheck(uint32_t amount = 1); + + void increment(uint32_t amount = 1); + + bool checkForDecrement(); + + bool aboveThreshold(); + + void clear(); + void setFailureThreshold(uint32_t failureThreshold); + void setFaultDecrementTimeMs(uint32_t timeMs); + + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); + + void setParameterDomain(uint8_t domain); +private: + uint8_t parameterDomain; + Countdown timer; + uint32_t faultCount; + uint32_t failureThreshold; +}; + +#endif /* FRAMEWORK_FDIR_FAULTCOUNTER_H_ */ diff --git a/fsfw/fsfw.mk b/fsfw/fsfw.mk new file mode 100644 index 0000000..3639717 --- /dev/null +++ b/fsfw/fsfw.mk @@ -0,0 +1,72 @@ +# This submake file needs to be included by the primary Makefile. +# This file needs FRAMEWORK_PATH and OS_FSFW set correctly by another Makefile. +# Valid API settings: rtems, linux, freeRTOS, host + +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/action/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/container/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/contrib/sgp4/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/controller/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/coordinates/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/datalinklayer/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/datapool/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/devicehandlers/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/events/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/events/eventmatching/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/fdir/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/globalfunctions/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/globalfunctions/matching/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/globalfunctions/math/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/health/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/internalError/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/ipc/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/memory/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/modes/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/monitoring/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/objectmanager/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/osal/*.cpp) + +# select the OS +ifeq ($(OS_FSFW),rtems) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/osal/rtems/*.cpp) + +else ifeq ($(OS_FSFW),linux) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/osal/linux/*.cpp) + +else ifeq ($(OS_FSFW),freeRTOS) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/osal/FreeRTOS/*.cpp) + +else ifeq ($(OS_FSFW),host) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/osal/host/*.cpp) +ifeq ($(OS),Windows_NT) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/osal/windows/*.cpp) +else +# For now, the linux UDP bridge sources needs to be included manually by upper makefile +# for host OS because we can't be sure the OS is linux. +# Following lines can be used to do this: +# CXXSRC += $(FRAMEWORK_PATH)/osal/linux/TcUnixUdpPollingTask.cpp +# CXXSRC += $(FRAMEWORK_PATH)/osal/linux/TmTcUnixUdpBridge.cpp +endif + +else +$(error invalid OS_FSFW specified, valid OS_FSFW are rtems, linux, freeRTOS, host) +endif + +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/parameters/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/power/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/returnvalues/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/rmap/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/serialize/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/serviceinterface/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/storagemanager/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/subsystem/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/subsystem/modes/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/tasks/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/tcdistribution/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/thermal/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/timemanager/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/tmstorage/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/tmtcpacket/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/tmtcpacket/packetmatcher/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/tmtcpacket/pus/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/tmtcservices/*.cpp) +CXXSRC += $(wildcard $(FRAMEWORK_PATH)/pus/*.cpp) diff --git a/fsfw/globalfunctions/AsciiConverter.cpp b/fsfw/globalfunctions/AsciiConverter.cpp new file mode 100644 index 0000000..9eb3698 --- /dev/null +++ b/fsfw/globalfunctions/AsciiConverter.cpp @@ -0,0 +1,230 @@ +#include "AsciiConverter.h" +#include +#include + +template +ReturnValue_t AsciiConverter::scanAsciiDecimalNumber(const uint8_t** dataPtr, + uint8_t len, T* value) { + if (len > std::numeric_limits().digits10) { + return TOO_LONG_FOR_TARGET_TYPE; + } + + double temp; + + ReturnValue_t result = scanAsciiDecimalNumber_(dataPtr, len, &temp); + + *value = temp; + + return result; +} + +ReturnValue_t AsciiConverter::scanAsciiHexByte(const uint8_t** dataPtr, + uint8_t* value) { + int8_t tmp; + + tmp = convertHexChar(*dataPtr); + (*dataPtr)++; + if (tmp == -1) { + return INVALID_CHARACTERS; + } + if (tmp == -2) { + tmp = 0; + } + + *value = tmp << 4; + + tmp = convertHexChar(*dataPtr); + (*dataPtr)++; + if (tmp == -1) { + return INVALID_CHARACTERS; + } + if (tmp != -2) { + *value += tmp; + } else { + *value = *value >> 4; + } + + return RETURN_OK; +} + +ReturnValue_t AsciiConverter::scanAsciiDecimalNumber_(uint8_t const ** dataPtr, + uint8_t len, double* value) { + + uint8_t const *ptr = *dataPtr; + int8_t sign = 1; + float decimal = 0; + bool abort = false; + + *value = 0; + + //ignore leading space + ptr = clearSpace(ptr, len); + + while ((ptr - *dataPtr < len) && !abort) { + switch (*ptr) { + case '+': + sign = 1; + break; + case '-': + sign = -1; + break; + case '.': + decimal = 1; + break; + case ' ': + case 0x0d: + case 0x0a: + //ignore trailing space + ptr = clearSpace(ptr, len - (ptr - *dataPtr)) - 1; //before aborting the loop, ptr will be incremented + abort = true; + break; + default: + if ((*ptr < 0x30) || (*ptr > 0x39)) { + return INVALID_CHARACTERS; + } + *value = *value * 10 + (*ptr - 0x30); + if (decimal > 0) { + decimal *= 10; + } + break; + } + ptr++; + } + + if (decimal == 0) { + decimal = 1; + } + + *value = *value / (decimal) * sign; + + *dataPtr = ptr; + + return RETURN_OK; +} + +ReturnValue_t AsciiConverter::printFloat(uint8_t* buffer, uint32_t bufferLength, + float value, uint8_t decimalPlaces, uint32_t *printedSize) { + *printedSize = 0; + uint32_t streamposition = 0, integerSize; + bool negative = (value < 0); + int32_t digits = bufferLength - decimalPlaces - 1; + if (digits <= 0) { + return BUFFER_TOO_SMALL; + } + if (negative) { + digits -= 1; + buffer[streamposition++] = '-'; + value = -value; + } + float maximumNumber = pow(10, digits); + if (value >= maximumNumber) { + return BUFFER_TOO_SMALL; + } + //print the numbers before the decimal point; + ReturnValue_t result = printInteger(buffer + streamposition, + bufferLength - streamposition - decimalPlaces - 1, value, + &integerSize); + if (result != RETURN_OK) { + return result; + } + streamposition += integerSize; + //The decimal Point + buffer[streamposition++] = '.'; + + //Print the decimals + uint32_t integerValue = value; + value -= integerValue; + value = value * pow(10, decimalPlaces); + result = printInteger(buffer + streamposition, decimalPlaces, round(value), + &integerSize, true); + *printedSize = integerSize + streamposition; + return result; +} + +ReturnValue_t AsciiConverter::printInteger(uint8_t* buffer, + uint32_t bufferLength, uint32_t value, uint32_t *printedSize, + bool leadingZeros) { + *printedSize = 0; + if (bufferLength == 0) { + return BUFFER_TOO_SMALL; + } + uint32_t maximumNumber = -1; + if (bufferLength < 10) { + maximumNumber = pow(10, bufferLength); + if (value >= maximumNumber) { + return BUFFER_TOO_SMALL; + } + maximumNumber /= 10; + } else { + if (!(value <= maximumNumber)) { + return BUFFER_TOO_SMALL; + } + maximumNumber = 1000000000; + } + if (!leadingZeros && (value == 0)) { + buffer[(*printedSize)++] = '0'; + return RETURN_OK; + } + while (maximumNumber >= 1) { + uint8_t number = value / maximumNumber; + value = value - (number * maximumNumber); + if (!leadingZeros && number == 0) { + maximumNumber /= 10; + } else { + leadingZeros = true; + buffer[(*printedSize)++] = '0' + number; + maximumNumber /= 10; + } + } + return RETURN_OK; +} + +ReturnValue_t AsciiConverter::printSignedInteger(uint8_t* buffer, + uint32_t bufferLength, int32_t value, uint32_t *printedSize) { + bool negative = false; + if ((bufferLength > 0) && (value < 0)) { + *buffer++ = '-'; + bufferLength--; + value = -value; + negative = true; + } + ReturnValue_t result = printInteger(buffer, bufferLength, value, + printedSize); + if (negative) { + (*printedSize)++; + } + return result; +} + +int8_t AsciiConverter::convertHexChar(const uint8_t* character) { + if ((*character > 0x60) && (*character < 0x67)) { + return *character - 0x61 + 10; + } else if ((*character > 0x40) && (*character < 0x47)) { + return *character - 0x41 + 10; + } else if ((*character > 0x2F) && (*character < 0x3A)) { + return *character - 0x30; + } else if (*character == ' ') { + return -2; + } + return -1; +} + +template ReturnValue_t AsciiConverter::scanAsciiDecimalNumber( + const uint8_t** dataPtr, uint8_t len, float* value); +template ReturnValue_t AsciiConverter::scanAsciiDecimalNumber( + const uint8_t** dataPtr, uint8_t len, uint8_t* value); +template ReturnValue_t AsciiConverter::scanAsciiDecimalNumber( + const uint8_t** dataPtr, uint8_t len, uint16_t* value); +template ReturnValue_t AsciiConverter::scanAsciiDecimalNumber( + const uint8_t** dataPtr, uint8_t len, double* value); + +const uint8_t* AsciiConverter::clearSpace(const uint8_t* data, uint8_t len) { + while (len > 0) { + if ((*data != ' ') && (*data != 0x0a) && (*data != 0x0d)) { + return data; + } + data++; + len--; + } + return data; +} diff --git a/fsfw/globalfunctions/AsciiConverter.h b/fsfw/globalfunctions/AsciiConverter.h new file mode 100644 index 0000000..7af3cb3 --- /dev/null +++ b/fsfw/globalfunctions/AsciiConverter.h @@ -0,0 +1,39 @@ +#ifndef ASCIICONVERTER_H_ +#define ASCIICONVERTER_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" + +class AsciiConverter: public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::ASCII_CONVERTER; + static const ReturnValue_t TOO_LONG_FOR_TARGET_TYPE = MAKE_RETURN_CODE(1); + static const ReturnValue_t INVALID_CHARACTERS = MAKE_RETURN_CODE(2); + static const ReturnValue_t BUFFER_TOO_SMALL = MAKE_RETURN_CODE(0x3); + + template + static ReturnValue_t scanAsciiDecimalNumber(const uint8_t **dataPtr, + uint8_t len, T *value); + + static ReturnValue_t scanAsciiHexByte(const uint8_t **dataPtr, + uint8_t *value); + + static ReturnValue_t printFloat(uint8_t *buffer, uint32_t bufferLength, + float value, uint8_t decimalPlaces, uint32_t *printedSize); + + static ReturnValue_t printInteger(uint8_t *buffer, uint32_t bufferLength, + uint32_t value, uint32_t *printedSize, bool leadingZeros = false); + + static ReturnValue_t printSignedInteger(uint8_t *buffer, + uint32_t bufferLength, int32_t value, uint32_t *printedSize); + +private: + AsciiConverter(); + static ReturnValue_t scanAsciiDecimalNumber_(const uint8_t **dataPtr, + uint8_t len, double *value); + + static int8_t convertHexChar(const uint8_t *character); + + static const uint8_t *clearSpace(const uint8_t *data, uint8_t len); +}; + +#endif /* ASCIICONVERTER_H_ */ diff --git a/fsfw/globalfunctions/CRC.cpp b/fsfw/globalfunctions/CRC.cpp new file mode 100644 index 0000000..7bb5680 --- /dev/null +++ b/fsfw/globalfunctions/CRC.cpp @@ -0,0 +1,139 @@ +#include "CRC.h" +#include + +const uint16_t CRC::crc16ccitt_table[256] = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 +}; + + +// CRC implementation +uint16_t CRC::crc16ccitt(uint8_t const input[], uint32_t length, uint16_t startingCrc) +{ + uint8_t *data = (uint8_t *)input; + unsigned int tbl_idx; + + while (length--) { + tbl_idx = ((startingCrc >> 8) ^ *data) & 0xff; + startingCrc = (crc16ccitt_table[tbl_idx] ^ (startingCrc << 8)) & 0xffff; + + data++; + } + return startingCrc & 0xffff; + + //The part below is not used! +// bool temr[16]; +// bool xor_out[16]; +// bool r[16]; +// bool d[8]; +// uint16_t crc_value = 0; +// +// +// for (int i=0; i<16 ;i++) { +// temr[i] = false; +// xor_out[i] = false; +// } +// +// +// for (int i=0; i<16 ;i++) +// r[i] = true; // initialize with 0xFFFF +// +// +// +// for (int j=0; j + +class CRC { +public: + static uint16_t crc16ccitt(uint8_t const input[], uint32_t length, + uint16_t startingCrc = 0xffff); +private: + CRC(); + + static const uint16_t crc16ccitt_table[256]; +}; + +#endif /* CRC_H_ */ diff --git a/fsfw/globalfunctions/DleEncoder.cpp b/fsfw/globalfunctions/DleEncoder.cpp new file mode 100644 index 0000000..8520389 --- /dev/null +++ b/fsfw/globalfunctions/DleEncoder.cpp @@ -0,0 +1,124 @@ +#include "../globalfunctions/DleEncoder.h" + +DleEncoder::DleEncoder() {} + +DleEncoder::~DleEncoder() {} + +ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream, + size_t sourceLen, uint8_t* destStream, size_t maxDestLen, + size_t* encodedLen, bool addStxEtx) { + if (maxDestLen < 2) { + return STREAM_TOO_SHORT; + } + size_t encodedIndex = 0, sourceIndex = 0; + uint8_t nextByte; + if (addStxEtx) { + destStream[0] = STX_CHAR; + ++encodedIndex; + } + + while (encodedIndex < maxDestLen and sourceIndex < sourceLen) + { + nextByte = sourceStream[sourceIndex]; + // STX, ETX and CR characters in the stream need to be escaped with DLE + if (nextByte == STX_CHAR or nextByte == ETX_CHAR or nextByte == CARRIAGE_RETURN) { + if (encodedIndex + 1 >= maxDestLen) { + return STREAM_TOO_SHORT; + } + else { + destStream[encodedIndex] = DLE_CHAR; + ++encodedIndex; + /* Escaped byte will be actual byte + 0x40. This prevents + * STX, ETX, and carriage return characters from appearing + * in the encoded data stream at all, so when polling an + * encoded stream, the transmission can be stopped at ETX. + * 0x40 was chosen at random with special requirements: + * - Prevent going from one control char to another + * - Prevent overflow for common characters */ + destStream[encodedIndex] = nextByte + 0x40; + } + } + // DLE characters are simply escaped with DLE. + else if (nextByte == DLE_CHAR) { + if (encodedIndex + 1 >= maxDestLen) { + return STREAM_TOO_SHORT; + } + else { + destStream[encodedIndex] = DLE_CHAR; + ++encodedIndex; + destStream[encodedIndex] = DLE_CHAR; + } + } + else { + destStream[encodedIndex] = nextByte; + } + ++encodedIndex; + ++sourceIndex; + } + + if (sourceIndex == sourceLen and encodedIndex < maxDestLen) { + if (addStxEtx) { + destStream[encodedIndex] = ETX_CHAR; + ++encodedIndex; + } + *encodedLen = encodedIndex; + return RETURN_OK; + } + else { + return STREAM_TOO_SHORT; + } +} + +ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream, + size_t sourceStreamLen, size_t *readLen, uint8_t *destStream, + size_t maxDestStreamlen, size_t *decodedLen) { + size_t encodedIndex = 0, decodedIndex = 0; + uint8_t nextByte; + if (*sourceStream != STX_CHAR) { + return DECODING_ERROR; + } + ++encodedIndex; + + while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen) + && (sourceStream[encodedIndex] != ETX_CHAR) + && (sourceStream[encodedIndex] != STX_CHAR)) { + if (sourceStream[encodedIndex] == DLE_CHAR) { + nextByte = sourceStream[encodedIndex + 1]; + // The next byte is a DLE character that was escaped by another + // DLE character, so we can write it to the destination stream. + if (nextByte == DLE_CHAR) { + destStream[decodedIndex] = nextByte; + } + else { + /* The next byte is a STX, DTX or 0x0D character which + * was escaped by a DLE character. The actual byte was + * also encoded by adding + 0x40 to prevent having control chars, + * in the stream at all, so we convert it back. */ + if (nextByte == 0x42 or nextByte == 0x43 or nextByte == 0x4D) { + destStream[decodedIndex] = nextByte - 0x40; + } + else { + return DECODING_ERROR; + } + } + ++encodedIndex; + } + else { + destStream[decodedIndex] = sourceStream[encodedIndex]; + } + + ++encodedIndex; + ++decodedIndex; + } + + if (sourceStream[encodedIndex] != ETX_CHAR) { + *readLen = ++encodedIndex; + return DECODING_ERROR; + } + else { + *readLen = ++encodedIndex; + *decodedLen = decodedIndex; + return RETURN_OK; + } +} + diff --git a/fsfw/globalfunctions/DleEncoder.h b/fsfw/globalfunctions/DleEncoder.h new file mode 100644 index 0000000..6d073f9 --- /dev/null +++ b/fsfw/globalfunctions/DleEncoder.h @@ -0,0 +1,79 @@ +#ifndef FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ +#define FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +/** + * @brief This DLE Encoder (Data Link Encoder) can be used to encode and + * decode arbitrary data with ASCII control characters + * @details + * List of control codes: + * https://en.wikipedia.org/wiki/C0_and_C1_control_codes + * + * This encoder can be used to achieve a basic transport layer when using + * char based transmission systems. + * The passed source strean is converted into a encoded stream by adding + * a STX marker at the start of the stream and an ETX marker at the end of + * the stream. Any STX, ETX, DLE and CR occurrences in the source stream are + * escaped by a DLE character. The encoder also replaces escaped control chars + * by another char, so STX, ETX and CR should not appear anywhere in the actual + * encoded data stream. + * + * When using a strictly char based reception of packets encoded with DLE, + * STX can be used to notify a reader that actual data will start to arrive + * while ETX can be used to notify the reader that the data has ended. + */ +class DleEncoder: public HasReturnvaluesIF { +private: + DleEncoder(); + virtual ~DleEncoder(); + +public: + static constexpr uint8_t INTERFACE_ID = CLASS_ID::DLE_ENCODER; + static constexpr ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(0x01); + static constexpr ReturnValue_t DECODING_ERROR = MAKE_RETURN_CODE(0x02); + + //! Start Of Text character. First character is encoded stream + static constexpr uint8_t STX_CHAR = 0x02; + //! End Of Text character. Last character in encoded stream + static constexpr uint8_t ETX_CHAR = 0x03; + //! Data Link Escape character. Used to escape STX, ETX and DLE occurrences + //! in the source stream. + static constexpr uint8_t DLE_CHAR = 0x10; + static constexpr uint8_t CARRIAGE_RETURN = 0x0D; + + /** + * Encodes the give data stream by preceding it with the STX marker + * and ending it with an ETX marker. STX, ETX and DLE characters inside + * the stream are escaped by DLE characters and also replaced by adding + * 0x40 (which is reverted in the decoding process). + * @param sourceStream + * @param sourceLen + * @param destStream + * @param maxDestLen + * @param encodedLen + * @param addStxEtx + * Adding STX and ETX can be omitted, if they are added manually. + * @return + */ + static ReturnValue_t encode(const uint8_t *sourceStream, size_t sourceLen, + uint8_t *destStream, size_t maxDestLen, size_t *encodedLen, + bool addStxEtx = true); + + /** + * Converts an encoded stream back. + * @param sourceStream + * @param sourceStreamLen + * @param readLen + * @param destStream + * @param maxDestStreamlen + * @param decodedLen + * @return + */ + static ReturnValue_t decode(const uint8_t *sourceStream, + size_t sourceStreamLen, size_t *readLen, uint8_t *destStream, + size_t maxDestStreamlen, size_t *decodedLen); +}; + +#endif /* FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ */ diff --git a/fsfw/globalfunctions/PeriodicOperationDivider.cpp b/fsfw/globalfunctions/PeriodicOperationDivider.cpp new file mode 100644 index 0000000..ad3b8bb --- /dev/null +++ b/fsfw/globalfunctions/PeriodicOperationDivider.cpp @@ -0,0 +1,34 @@ +#include "PeriodicOperationDivider.h" + + +PeriodicOperationDivider::PeriodicOperationDivider(uint32_t divider, + bool resetAutomatically): resetAutomatically(resetAutomatically), + counter(divider), divider(divider) { +} + +bool PeriodicOperationDivider::checkAndIncrement() { + if(counter >= divider) { + if(resetAutomatically) { + counter = 0; + } + return true; + } + counter ++; + return false; +} + +void PeriodicOperationDivider::resetCounter() { + counter = 0; +} + +void PeriodicOperationDivider::setDivider(uint32_t newDivider) { + divider = newDivider; +} + +uint32_t PeriodicOperationDivider::getCounter() const { + return counter; +} + +uint32_t PeriodicOperationDivider::getDivider() const { + return divider; +} diff --git a/fsfw/globalfunctions/PeriodicOperationDivider.h b/fsfw/globalfunctions/PeriodicOperationDivider.h new file mode 100644 index 0000000..dd970fb --- /dev/null +++ b/fsfw/globalfunctions/PeriodicOperationDivider.h @@ -0,0 +1,55 @@ +#ifndef FSFW_GLOBALFUNCTIONS_PERIODICOPERATIONDIVIDER_H_ +#define FSFW_GLOBALFUNCTIONS_PERIODICOPERATIONDIVIDER_H_ + +#include + +/** + * @brief Lightweight helper class to facilitate periodic operation with + * decreased frequencies. + * @details + * This class is useful to perform operations which have to be performed + * with a reduced frequency, like debugging printouts in high periodic tasks + * or low priority operations. + */ +class PeriodicOperationDivider { +public: + /** + * Initialize with the desired divider and specify whether the internal + * counter will be reset automatically. + * @param divider + * @param resetAutomatically + */ + PeriodicOperationDivider(uint32_t divider, bool resetAutomatically = true); + + /** + * Check whether operation is necessary. + * If an operation is necessary and the class has been + * configured to be reset automatically, the counter will be reset. + * If not, the counter will be incremented. + * @return + * -@c true if the counter is larger or equal to the divider + * -@c false otherwise + */ + bool checkAndIncrement(); + + /** + * Can be used to reset the counter to 0 manually. + */ + void resetCounter(); + uint32_t getCounter() const; + + /** + * Can be used to set a new divider value. + * @param newDivider + */ + void setDivider(uint32_t newDivider); + uint32_t getDivider() const; +private: + bool resetAutomatically = true; + uint32_t counter = 0; + uint32_t divider = 0; +}; + + + +#endif /* FSFW_GLOBALFUNCTIONS_PERIODICOPERATIONDIVIDER_H_ */ diff --git a/fsfw/globalfunctions/Type.cpp b/fsfw/globalfunctions/Type.cpp new file mode 100644 index 0000000..ae595b8 --- /dev/null +++ b/fsfw/globalfunctions/Type.cpp @@ -0,0 +1,182 @@ +#include "../serialize/SerializeAdapter.h" +#include "Type.h" +#include "../serialize/SerializeAdapter.h" + +Type::Type() : + actualType(UNKNOWN_TYPE) { +} + +Type::Type(ActualType_t actualType) : + actualType(actualType) { +} + +Type::Type(const Type& type) : + actualType(type.actualType) { +} + +Type& Type::operator =(Type rhs) { + this->actualType = rhs.actualType; + return *this; +} + +Type& Type::operator =(ActualType_t actualType) { + this->actualType = actualType; + return *this; +} + +Type::operator Type::ActualType_t() const { + return actualType; +} + +bool Type::operator ==(const Type& rhs) { + return this->actualType == rhs.actualType; +} + +bool Type::operator !=(const Type& rhs) { + return !operator==(rhs); +} + +uint8_t Type::getSize() const { + switch (actualType) { + case UINT8_T: + return sizeof(uint8_t); + case INT8_T: + return sizeof(int8_t); + case UINT16_T: + return sizeof(uint16_t); + case INT16_T: + return sizeof(int16_t); + case UINT32_T: + return sizeof(uint32_t); + case INT32_T: + return sizeof(int32_t); + case FLOAT: + return sizeof(float); + case DOUBLE: + return sizeof(double); + default: + return 0; + } +} + +ReturnValue_t Type::serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + uint8_t ptc; + uint8_t pfc; + ReturnValue_t result = getPtcPfc(&ptc, &pfc); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = SerializeAdapter::serialize(&ptc, buffer, size, maxSize, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = SerializeAdapter::serialize(&pfc, buffer, size, maxSize, + streamEndianness); + + return result; + +} + +size_t Type::getSerializedSize() const { + uint8_t dontcare = 0; + return 2 * SerializeAdapter::getSerializedSize(&dontcare); +} + +ReturnValue_t Type::deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + uint8_t ptc; + uint8_t pfc; + ReturnValue_t result = SerializeAdapter::deSerialize(&ptc, buffer, + size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = SerializeAdapter::deSerialize(&pfc, buffer, size, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + actualType = getActualType(ptc, pfc); + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Type::getPtcPfc(uint8_t* ptc, uint8_t* pfc) const { + switch (actualType) { + case UINT8_T: + *ptc = 3; + *pfc = 4; + break; + case INT8_T: + *ptc = 4; + *pfc = 4; + break; + case UINT16_T: + *ptc = 3; + *pfc = 12; + break; + case INT16_T: + *ptc = 4; + *pfc = 12; + break; + case UINT32_T: + *ptc = 3; + *pfc = 14; + break; + case INT32_T: + *ptc = 4; + *pfc = 14; + break; + case FLOAT: + *ptc = 5; + *pfc = 1; + break; + case DOUBLE: + *ptc = 5; + *pfc = 2; + break; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +Type::ActualType_t Type::getActualType(uint8_t ptc, uint8_t pfc) { + switch (ptc) { + case 3: + switch (pfc) { + case 4: + return UINT8_T; + case 12: + return UINT16_T; + case 14: + return UINT32_T; + } + break; + case 4: + switch (pfc) { + case 4: + return INT8_T; + case 12: + return INT16_T; + case 14: + return INT32_T; + } + break; + case 5: + switch (pfc) { + case 1: + return FLOAT; + case 2: + return DOUBLE; + } + break; + } + return UNKNOWN_TYPE; +} diff --git a/fsfw/globalfunctions/Type.h b/fsfw/globalfunctions/Type.h new file mode 100644 index 0000000..be499ff --- /dev/null +++ b/fsfw/globalfunctions/Type.h @@ -0,0 +1,91 @@ +#ifndef TYPE_H_ +#define TYPE_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerializeIF.h" + +class Type: public SerializeIF { +public: + enum ActualType_t { + UINT8_T, + INT8_T, + UINT16_T, + INT16_T, + UINT32_T, + INT32_T, + FLOAT, + DOUBLE, + UNKNOWN_TYPE + }; + + Type(); + + Type(ActualType_t actualType); + + Type(const Type &type); + + Type& operator=(Type rhs); + + Type& operator=(ActualType_t actualType); + + operator ActualType_t() const; + + bool operator==(const Type &rhs); + bool operator!=(const Type &rhs); + + uint8_t getSize() const; + + ReturnValue_t getPtcPfc(uint8_t *ptc, uint8_t *pfc) const; + + static ActualType_t getActualType(uint8_t ptc, uint8_t pfc); + + virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const override; + + virtual size_t getSerializedSize() const override; + + virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) override; + +private: + ActualType_t actualType; +}; + +template +struct PodTypeConversion { + static const Type::ActualType_t type = Type::UNKNOWN_TYPE; +}; +template<> +struct PodTypeConversion { + static const Type::ActualType_t type = Type::UINT8_T; +}; +template<> +struct PodTypeConversion { + static const Type::ActualType_t type = Type::UINT16_T; +}; +template<> +struct PodTypeConversion { + static const Type::ActualType_t type = Type::UINT32_T; +}; +template<> +struct PodTypeConversion { + static const Type::ActualType_t type = Type::INT8_T; +}; +template<> +struct PodTypeConversion { + static const Type::ActualType_t type = Type::INT16_T; +}; +template<> +struct PodTypeConversion { + static const Type::ActualType_t type = Type::INT32_T; +}; +template<> +struct PodTypeConversion { + static const Type::ActualType_t type = Type::FLOAT; +}; +template<> +struct PodTypeConversion { + static const Type::ActualType_t type = Type::DOUBLE; +}; + +#endif /* TYPE_H_ */ diff --git a/fsfw/globalfunctions/arrayprinter.cpp b/fsfw/globalfunctions/arrayprinter.cpp new file mode 100644 index 0000000..b1e888c --- /dev/null +++ b/fsfw/globalfunctions/arrayprinter.cpp @@ -0,0 +1,61 @@ +#include "arrayprinter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include + +void arrayprinter::print(const uint8_t *data, size_t size, OutputType type, + bool printInfo, size_t maxCharPerLine) { + if(printInfo) { + sif::info << "Printing data with size " << size << ": "; + } + sif::info << "["; + if(type == OutputType::HEX) { + arrayprinter::printHex(data, size, maxCharPerLine); + } + else if (type == OutputType::DEC) { + arrayprinter::printDec(data, size, maxCharPerLine); + } + else if(type == OutputType::BIN) { + arrayprinter::printBin(data, size); + } +} + +void arrayprinter::printHex(const uint8_t *data, size_t size, + size_t maxCharPerLine) { + sif::info << std::hex; + for(size_t i = 0; i < size; i++) { + sif::info << "0x" << static_cast(data[i]); + if(i < size - 1){ + sif::info << " , "; + if(i > 0 and i % maxCharPerLine == 0) { + sif::info << std::endl; + } + } + + } + sif::info << std::dec; + sif::info << "]" << std::endl; +} + +void arrayprinter::printDec(const uint8_t *data, size_t size, + size_t maxCharPerLine) { + sif::info << std::dec; + for(size_t i = 0; i < size; i++) { + sif::info << static_cast(data[i]); + if(i < size - 1){ + sif::info << " , "; + if(i > 0 and i % maxCharPerLine == 0) { + sif::info << std::endl; + } + } + } + sif::info << "]" << std::endl; +} + +void arrayprinter::printBin(const uint8_t *data, size_t size) { + sif::info << "\n" << std::flush; + for(size_t i = 0; i < size; i++) { + sif::info << "Byte " << i + 1 << ": 0b"<< + std::bitset<8>(data[i]) << ",\n" << std::flush; + } + sif::info << "]" << std::endl; +} diff --git a/fsfw/globalfunctions/arrayprinter.h b/fsfw/globalfunctions/arrayprinter.h new file mode 100644 index 0000000..e57d8e0 --- /dev/null +++ b/fsfw/globalfunctions/arrayprinter.h @@ -0,0 +1,20 @@ +#ifndef FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ +#define FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ +#include +#include + +enum class OutputType { + DEC, + HEX, + BIN +}; + +namespace arrayprinter { +void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX, + bool printInfo = true, size_t maxCharPerLine = 12); +void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); +void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); +void printBin(const uint8_t* data, size_t size); +} + +#endif /* FRAMEWORK_GLOBALFUNCTIONS_ARRAYPRINTER_H_ */ diff --git a/fsfw/globalfunctions/constants.h b/fsfw/globalfunctions/constants.h new file mode 100644 index 0000000..2c3b66c --- /dev/null +++ b/fsfw/globalfunctions/constants.h @@ -0,0 +1,15 @@ +#ifndef CONSTANTS_H_ +#define CONSTANTS_H_ + +namespace Math { +static const double PI = 3.1415926535897932384626433; + +} +namespace Earth { +static const double OMEGA = 7.2921158553E-5; +static const double GRAVITATIONAL_CONSTANT = 3.987095392E14; +static const double MEAN_RADIUS = 6371008.8; +static const double STANDARD_GRAVITATIONAL_PARAMETER = 3.9860044189e14; +} + +#endif /* CONSTANTS_H_ */ diff --git a/fsfw/globalfunctions/matching/BinaryMatcher.h b/fsfw/globalfunctions/matching/BinaryMatcher.h new file mode 100644 index 0000000..4f6db85 --- /dev/null +++ b/fsfw/globalfunctions/matching/BinaryMatcher.h @@ -0,0 +1,41 @@ +#ifndef BINARYMATCHER_H_ +#define BINARYMATCHER_H_ + +#include "MatcherIF.h" + +template +class BinaryMatcher: public MatcherIF { +public: + bool inverted; + T mask, matchField; + + BinaryMatcher() : + inverted(false), mask(0), matchField(0) { + } + + BinaryMatcher(T mask, T match, bool inverted = false) : + inverted(inverted), mask(mask), matchField(match) { + } + + bool match(T input) { + if (inverted) { + return ~doMatch(input, mask, matchField); + } else { + return doMatch(input, mask, matchField); + } + } + +protected: + + bool doMatch(T input, T mask, T match) { + match = match & mask; + input = input & mask; + if (input == match) { + return true; + } else { + return false; + } + } +}; + +#endif /* BINARYMATCHER_H_ */ diff --git a/fsfw/globalfunctions/matching/DecimalMatcher.h b/fsfw/globalfunctions/matching/DecimalMatcher.h new file mode 100644 index 0000000..5350502 --- /dev/null +++ b/fsfw/globalfunctions/matching/DecimalMatcher.h @@ -0,0 +1,50 @@ +#ifndef DECIMALMATCHER_H_ +#define DECIMALMATCHER_H_ + +#include "MatcherIF.h" + +template +class DecimalMatcher: public MatcherIF { +public: + bool inverted; + T mask, matchField; + + DecimalMatcher() : + inverted(false), mask(0), matchField(0) { + } + + DecimalMatcher(T mask, T match, bool inverted = false) : + inverted(inverted), mask(mask), matchField(match) { + } + + bool match(T input) { + if (inverted) { + return ~doMatch(input, mask, matchField); + } else { + return doMatch(input, mask, matchField); + } + } + +protected: + bool doMatch(T input, T mask, T match) { + T decimal = 1, remainderMask, remainderMatch, remainderInput; + + while (mask != 0) { + remainderMask = mask % (decimal * 10); + remainderMatch = match % (decimal * 10); + remainderInput = input % (decimal * 10); + if (remainderMask != 0) { + if (remainderMatch != remainderInput) { + return false; + } + } + mask -= remainderMask; + match -= remainderMatch; + input -= remainderInput; + decimal *= 10; + } + return true; + } +}; + +#endif /* DECIMALMATCHER_H_ */ diff --git a/fsfw/globalfunctions/matching/MatchTree.h b/fsfw/globalfunctions/matching/MatchTree.h new file mode 100644 index 0000000..755687b --- /dev/null +++ b/fsfw/globalfunctions/matching/MatchTree.h @@ -0,0 +1,216 @@ +#ifndef FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_ +#define FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_ + +#include "../../container/BinaryTree.h" +#include "SerializeableMatcherIF.h" +#include "../../serialize/SerializeAdapter.h" + +template +class MatchTree: public SerializeableMatcherIF, public BinaryTree< + SerializeableMatcherIF> { +public: + + static const uint8_t INTERFACE_ID = CLASS_ID::MATCH_TREE_CLASS; + static const ReturnValue_t TOO_DETAILED_REQUEST = MAKE_RETURN_CODE(1); + static const ReturnValue_t TOO_GENERAL_REQUEST = MAKE_RETURN_CODE(2); + static const ReturnValue_t NO_MATCH = MAKE_RETURN_CODE(3); + static const ReturnValue_t FULL = MAKE_RETURN_CODE(4); + static const ReturnValue_t NEW_NODE_CREATED = MAKE_RETURN_CODE(5); + + typedef typename BinaryTree>::iterator iterator; + typedef BinaryNode> Node; + static const bool AND = true; //LEFT + static const bool OR = false; //RIGHT + MatchTree(BinaryNode>* root, + uint8_t maxDepth = -1) : + BinaryTree>(root), maxDepth(maxDepth) { + } + MatchTree(iterator root, uint8_t maxDepth = -1) : + BinaryTree>(root.element), maxDepth( + maxDepth) { + } + MatchTree() : + BinaryTree>(), maxDepth(-1) { + } + virtual ~MatchTree() { + } + virtual bool match(T number) { + return matchesTree(number); + } + bool matchesTree(T number) { + iterator iter = this->begin(); + if (iter == this->end()) { + return false; + } + return matchSubtree(iter, number); + } + + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, SerializeIF::Endianness streamEndianness) const override { + iterator iter = this->begin(); + uint8_t count = this->countRight(iter); + ReturnValue_t result = SerializeAdapter::serialize(&count, + buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (iter == this->end()) { + return HasReturnvaluesIF::RETURN_OK; + } + result = iter->serialize(buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (maxDepth > 0) { + MatchTree temp(iter.left(), maxDepth - 1); + result = temp.serialize(buffer, size, maxSize, streamEndianness); + } + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + iter = iter.right(); + while (iter != this->end()) { + result = iter->serialize(buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (maxDepth > 0) { + MatchTree temp(iter.left(), maxDepth - 1); + result = temp.serialize(buffer, size, maxSize, streamEndianness); + } + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + iter = iter.right(); + } + return result; + } + + size_t getSerializedSize() const override { + //Analogous to serialize! + uint32_t size = 1; //One for count + iterator iter = this->begin(); + if (iter == this->end()) { + return size; + } + //Count object itself + size += iter->getSerializedSize(); + //Handle everything below on AND side + if (maxDepth > 0) { + MatchTree temp(iter.left(), maxDepth - 1); + size += temp.getSerializedSize(); + } + //Handle everything on OR side + iter = iter.right(); + //Iterate over every object on the OR branch + while (iter != this->end()) { + size += iter->getSerializedSize(); + if (maxDepth > 0) { + //If we are allowed to go deeper, handle AND elements. + MatchTree temp(iter.left(), maxDepth - 1); + size += temp.getSerializedSize(); + } + iter = iter.right(); + } + return size; + } + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + SerializeIF::Endianness streamEndianness) override { + return HasReturnvaluesIF::RETURN_OK; + } + +protected: + + bool isOnAndBranch(iterator position) { + if ((position == this->end()) || (position.up() == this->end())) { + return false; + } + if (position.up().left() == position) { + return true; + } else { + return false; + } + } + + //SHOULDDO: What to do if insertion/deletion fails. Throw event? + ReturnValue_t removeElementAndAllChildren(iterator position) { + auto children = this->erase(position); + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + if (children.first != this->end()) { + result = removeElementAndAllChildren(children.first); + } + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (children.second != this->end()) { + result = removeElementAndAllChildren(children.second); + } + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + //Delete element itself. + return cleanUpElement(position); + } + + ReturnValue_t removeElementAndReconnectChildren(iterator position) { + if (position == this->end()) { + return HasReturnvaluesIF::RETURN_OK; + } + //Delete everything from the AND branch. + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + if (position.left() != this->end()) { + result = removeElementAndAllChildren(position.left()); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + + if (position.right() != this->end()) { + //There's something at the OR branch, reconnect to parent. + if (isOnAndBranch(position)) { + //Either one hierarchy up AND branch... + this->insert(AND, position.up(), position.right().element); + } else { + //or on another OR'ed element (or install new root node). + this->insert(OR, position.up(), position.right().element); + } + } else { + if (isOnAndBranch(position)) { + //Recursively delete parent node as well, because it is not expected to be there anymore. + return removeElementAndReconnectChildren(position.up()); + } else { + //simply delete self. + this->erase(position); + } + + } + //Delete element itself. + return cleanUpElement(position); + } + + virtual ReturnValue_t cleanUpElement(iterator position) { + return HasReturnvaluesIF::RETURN_OK; + } + + bool matchSubtree(iterator iter, T number) { + bool isMatch = iter->match(number); + if (isMatch) { + if (iter.left() == this->end()) { + return true; + } + isMatch = matchSubtree(iter.left(), number); + if (isMatch) { + return true; + } + } + if (iter.right() == this->end()) { + return false; + } + return matchSubtree(iter.right(), number); + } +private: + uint8_t maxDepth; +}; + +#endif /* FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_ */ diff --git a/fsfw/globalfunctions/matching/MatcherIF.h b/fsfw/globalfunctions/matching/MatcherIF.h new file mode 100644 index 0000000..4a6a05a --- /dev/null +++ b/fsfw/globalfunctions/matching/MatcherIF.h @@ -0,0 +1,16 @@ +#ifndef MATCHERIF_H_ +#define MATCHERIF_H_ + +template +class MatcherIF { +public: + virtual ~MatcherIF() { + } + + virtual bool match(T number) { + return false; + } + +}; + +#endif /* MATCHERIF_H_ */ diff --git a/fsfw/globalfunctions/matching/RangeMatcher.h b/fsfw/globalfunctions/matching/RangeMatcher.h new file mode 100644 index 0000000..b64b9e3 --- /dev/null +++ b/fsfw/globalfunctions/matching/RangeMatcher.h @@ -0,0 +1,70 @@ +#ifndef RANGEMATCHER_H_ +#define RANGEMATCHER_H_ + +#include "SerializeableMatcherIF.h" +#include "../../serialize/SerializeAdapter.h" + +template +class RangeMatcher: public SerializeableMatcherIF { +public: + bool inverted; + T lowerBound; + T upperBound; + + RangeMatcher() : + inverted(true), lowerBound(1), upperBound(0) { + } + RangeMatcher(T lowerBound, T upperBound, bool inverted = false) : + inverted(inverted), lowerBound(lowerBound), upperBound(upperBound) { + } + + bool match(T input) { + if (inverted) { + return !doMatch(input); + } else { + return doMatch(input); + } + } + + ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, + SerializeIF::Endianness streamEndianness) const override { + ReturnValue_t result = SerializeAdapter::serialize(&lowerBound, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&upperBound, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return SerializeAdapter::serialize(&inverted, buffer, size, maxSize, + streamEndianness); + } + + size_t getSerializedSize() const override { + return sizeof(lowerBound) + sizeof(upperBound) + sizeof(bool); + } + + ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + SerializeIF::Endianness streamEndianness) override { + ReturnValue_t result = SerializeAdapter::deSerialize(&lowerBound, + buffer, size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&upperBound, buffer, size, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return SerializeAdapter::deSerialize(&inverted, buffer, size, + streamEndianness); + } +protected: + bool doMatch(T input) { + return (input >= lowerBound) && (input <= upperBound); + } +}; + +#endif /* RANGEMATCHER_H_ */ diff --git a/fsfw/globalfunctions/matching/SerializeableMatcherIF.h b/fsfw/globalfunctions/matching/SerializeableMatcherIF.h new file mode 100644 index 0000000..067b025 --- /dev/null +++ b/fsfw/globalfunctions/matching/SerializeableMatcherIF.h @@ -0,0 +1,13 @@ +#ifndef FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ +#define FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ + +#include "MatcherIF.h" +#include "../../serialize/SerializeIF.h" + +template +class SerializeableMatcherIF : public MatcherIF, public SerializeIF { +public: + virtual ~SerializeableMatcherIF() {} +}; + +#endif /* FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ */ diff --git a/fsfw/globalfunctions/math/MatrixOperations.h b/fsfw/globalfunctions/math/MatrixOperations.h new file mode 100644 index 0000000..66f89c5 --- /dev/null +++ b/fsfw/globalfunctions/math/MatrixOperations.h @@ -0,0 +1,115 @@ +#ifndef MATRIXOPERATIONS_H_ +#define MATRIXOPERATIONS_H_ + +#include +#include + +template +class MatrixOperations { +public: + //do not use with result == matrix1 or matrix2 + static void multiply(const T1 *matrix1, const T2 *matrix2, T3 *result, + uint8_t rows1, uint8_t columns1, uint8_t columns2) { + if ((matrix1 == (T1*)result) || (matrix2 == (T2*)result)){ + //SHOULDDO find an implementation that is tolerant to this + return; + } + for (uint8_t resultColumn = 0; resultColumn < columns2; + resultColumn++) { + for (uint8_t resultRow = 0; resultRow < rows1; resultRow++) { + result[resultColumn + columns2 * resultRow] = 0; + for (uint8_t i = 0; i < columns1; i++) { + result[resultColumn + columns2 * resultRow] += matrix1[i + + resultRow * columns1] + * matrix2[resultColumn + i * columns2]; + } + } + } + } + + static void transpose(const T1 *matrix, T2 *transposed, uint8_t size) { + uint8_t row, column; + transposed[0] = matrix[0]; + for (column = 1; column < size; column++) { + transposed[column + size * column] = matrix[column + size * column]; + for (row = 0; row < column; row++) { + T1 temp = matrix[column + size * row]; + transposed[column + size * row] = matrix[row + size * column]; + transposed[row + size * column] = temp; + } + } + } + + // Overload transpose to support non symmetrical matrices + //do not use with transposed == matrix && columns != rows + static void transpose(const T1 *matrix, T2 *transposed, uint8_t rows, uint8_t columns) { + uint8_t row, column; + transposed[0] = matrix[0]; + if (matrix == transposed && columns == rows) + { + transpose(matrix, transposed, rows); + } + else if (matrix == transposed && columns != rows) + { + // not permitted + return; + } + for (column = 0; column < columns; column++) { + for (row = 0; row < rows; row++) { + transposed[row + column * rows] = matrix[column + row * columns]; + } + } + } + + static void add(const T1 *matrix1, const T2 *matrix2, T3 *result, + uint8_t rows, uint8_t columns) + { + for (uint8_t resultColumn = 0; resultColumn < columns; resultColumn++) + { + for (uint8_t resultRow = 0; resultRow < rows; resultRow++) + { + result[resultColumn + columns * resultRow] = matrix1[resultColumn + columns * resultRow]+ + matrix2[resultColumn + columns * resultRow]; + } + } + } + + static void subtract(const T1 *matrix1, const T2 *matrix2, T3 *result, + uint8_t rows, uint8_t columns) + { + for (uint8_t resultColumn = 0; resultColumn < columns; resultColumn++) + { + for (uint8_t resultRow = 0; resultRow < rows; resultRow++) + { + result[resultColumn + columns * resultRow] = matrix1[resultColumn + columns * resultRow]- + matrix2[resultColumn + columns * resultRow]; + } + } + } + + static void addScalar(const T1 *matrix1, const T2 scalar, T3 *result, + uint8_t rows, uint8_t columns) + { + for (uint8_t resultColumn = 0; resultColumn < columns; resultColumn++) + { + for (uint8_t resultRow = 0; resultRow < rows; resultRow++) + { + result[resultColumn + columns * resultRow] = matrix1[resultColumn + columns * resultRow]+scalar; + } + } + } + + static void multiplyScalar(const T1 *matrix1, const T2 scalar, T3 *result, + uint8_t rows, uint8_t columns) + { + for (uint8_t resultColumn = 0; resultColumn < columns; resultColumn++) + { + for (uint8_t resultRow = 0; resultRow < rows; resultRow++) + { + result[resultColumn + columns * resultRow] = matrix1[resultColumn + columns * resultRow]*scalar; + } + } + } +}; + +#endif /* MATRIXOPERATIONS_H_ */ diff --git a/fsfw/globalfunctions/math/QuaternionOperations.cpp b/fsfw/globalfunctions/math/QuaternionOperations.cpp new file mode 100644 index 0000000..c09426d --- /dev/null +++ b/fsfw/globalfunctions/math/QuaternionOperations.cpp @@ -0,0 +1,156 @@ +#include "QuaternionOperations.h" +#include "VectorOperations.h" + +#include +#include +#include + +QuaternionOperations::~QuaternionOperations() { +} + +void QuaternionOperations::multiply(const double* q1, const double* q2, + double* q) { + double out[4]; + + out[0] = q1[3] * q2[0] + q1[2] * q2[1] - q1[1] * q2[2] + q1[0] * q2[3]; + out[1] = -q1[2] * q2[0] + q1[3] * q2[1] + q1[0] * q2[2] + q1[1] * q2[3]; + out[2] = q1[1] * q2[0] - q1[0] * q2[1] + q1[3] * q2[2] + q1[2] * q2[3]; + out[3] = -q1[0] * q2[0] - q1[1] * q2[1] - q1[2] * q2[2] + q1[3] * q2[3]; + + memcpy(q, out, 4 * sizeof(*q)); +} + +void QuaternionOperations::toDcm(const double* quaternion, double dcm[][3]) { + dcm[0][0] = 2 + * (quaternion[0] * quaternion[0] + quaternion[3] * quaternion[3]) + - 1; + dcm[0][1] = 2 + * (quaternion[0] * quaternion[1] + quaternion[2] * quaternion[3]); + dcm[0][2] = 2 + * (quaternion[0] * quaternion[2] - quaternion[1] * quaternion[3]); + + dcm[1][0] = 2 + * (quaternion[0] * quaternion[1] - quaternion[2] * quaternion[3]); + dcm[1][1] = 2 + * (quaternion[1] * quaternion[1] + quaternion[3] * quaternion[3]) + - 1; + dcm[1][2] = 2 + * (quaternion[1] * quaternion[2] + quaternion[0] * quaternion[3]); + + dcm[2][0] = 2 + * (quaternion[0] * quaternion[2] + quaternion[1] * quaternion[3]); + dcm[2][1] = 2 + * (quaternion[1] * quaternion[2] - quaternion[0] * quaternion[3]); + dcm[2][2] = 2 + * (quaternion[2] * quaternion[2] + quaternion[3] * quaternion[3]) + - 1; +} + +void QuaternionOperations::inverse(const double* quaternion, + double* inverseQuaternion) { + memcpy(inverseQuaternion, quaternion, 4 * sizeof(*quaternion)); + VectorOperations::mulScalar(inverseQuaternion, -1, + inverseQuaternion, 3); +} + +QuaternionOperations::QuaternionOperations() { + +} + +void QuaternionOperations::normalize(const double* quaternion, + double* unitQuaternion) { + VectorOperations::normalize(quaternion, unitQuaternion, 4); +} + +float QuaternionOperations::norm(const double* quaternion) { + return VectorOperations::norm(quaternion, 4); +} + +void QuaternionOperations::fromDcm(const double dcm[][3], double* quaternion, + uint8_t *index) { + + double a[4]; + + a[0] = 1 + dcm[0][0] - dcm[1][1] - dcm[2][2]; + a[1] = 1 - dcm[0][0] + dcm[1][1] - dcm[2][2]; + a[2] = 1 - dcm[0][0] - dcm[1][1] + dcm[2][2]; + a[3] = 1 + dcm[0][0] + dcm[1][1] + dcm[2][2]; + + uint8_t maxAIndex = 0; + + VectorOperations::maxValue(a, 4, &maxAIndex); + + if (index != 0) { + *index = maxAIndex; + } + + switch (maxAIndex) { + case 0: + quaternion[0] = 0.5 * sqrt(a[0]); + quaternion[1] = (dcm[0][1] + dcm[1][0]) / (2 * sqrt(a[0])); + quaternion[2] = (dcm[0][2] + dcm[2][0]) / (2 * sqrt(a[0])); + quaternion[3] = (dcm[1][2] - dcm[2][1]) / (2 * sqrt(a[0])); + break; + case 1: + quaternion[0] = (dcm[0][1] + dcm[1][0]) / (2 * sqrt(a[1])); + quaternion[1] = 0.5 * sqrt(a[1]); + quaternion[2] = (dcm[1][2] + dcm[2][1]) / (2 * sqrt(a[1])); + quaternion[3] = (dcm[2][0] - dcm[0][2]) / (2 * sqrt(a[1])); + break; + case 2: + quaternion[0] = (dcm[0][2] + dcm[2][0]) / (2 * sqrt(a[2])); + quaternion[1] = (dcm[1][2] + dcm[2][1]) / (2 * sqrt(a[2])); + quaternion[2] = 0.5 * sqrt(a[2]); + quaternion[3] = (dcm[0][1] - dcm[1][0]) / (2 * sqrt(a[2])); + break; + case 3: + quaternion[0] = (dcm[1][2] - dcm[2][1]) / (2 * sqrt(a[3])); + quaternion[1] = (dcm[2][0] - dcm[0][2]) / (2 * sqrt(a[3])); + quaternion[2] = (dcm[0][1] - dcm[1][0]) / (2 * sqrt(a[3])); + quaternion[3] = 0.5 * sqrt(a[3]); + break; + } + +} + +void QuaternionOperations::toDcm(const double* quaternion, float dcm[][3]) { + dcm[0][0] = 2 + * (quaternion[0] * quaternion[0] + quaternion[3] * quaternion[3]) + - 1; + dcm[0][1] = 2 + * (quaternion[0] * quaternion[1] + quaternion[2] * quaternion[3]); + dcm[0][2] = 2 + * (quaternion[0] * quaternion[2] - quaternion[1] * quaternion[3]); + + dcm[1][0] = 2 + * (quaternion[0] * quaternion[1] - quaternion[2] * quaternion[3]); + dcm[1][1] = 2 + * (quaternion[1] * quaternion[1] + quaternion[3] * quaternion[3]) + - 1; + dcm[1][2] = 2 + * (quaternion[1] * quaternion[2] + quaternion[0] * quaternion[3]); + + dcm[2][0] = 2 + * (quaternion[0] * quaternion[2] + quaternion[1] * quaternion[3]); + dcm[2][1] = 2 + * (quaternion[1] * quaternion[2] - quaternion[0] * quaternion[3]); + dcm[2][2] = 2 + * (quaternion[2] * quaternion[2] + quaternion[3] * quaternion[3]) + - 1; +} + +void QuaternionOperations::normalize(double* quaternion) { + normalize(quaternion, quaternion); +} + +double QuaternionOperations::getAngle(const double* quaternion, bool abs) { + if (quaternion[3] >= 0) { + return 2 * acos(quaternion[3]); + } else { + if (abs) { + return 2 * acos(-quaternion[3]); + } else { + return -2 * acos(-quaternion[3]); + } + } +} diff --git a/fsfw/globalfunctions/math/QuaternionOperations.h b/fsfw/globalfunctions/math/QuaternionOperations.h new file mode 100644 index 0000000..0d6fe86 --- /dev/null +++ b/fsfw/globalfunctions/math/QuaternionOperations.h @@ -0,0 +1,81 @@ +#ifndef QUATERNIONOPERATIONS_H_ +#define QUATERNIONOPERATIONS_H_ + +#include + +class QuaternionOperations { +public: + virtual ~QuaternionOperations(); + + static void multiply(const double *q1, const double *q2, double *q); + + static void fromDcm(const double dcm[][3], double *quaternion, + uint8_t *index = 0); + + static void toDcm(const double *quaternion, double dcm[][3]); + + static void toDcm(const double *quaternion, float dcm[][3]); + + static float norm(const double *quaternion); + + static void normalize(double *quaternion); + + static void normalize(const double *quaternion, double *unitQuaternion); + + static void inverse(const double *quaternion, double *inverseQuaternion); + + /** + * returns angle in ]-Pi;Pi] or [0;Pi] if abs == true + */ + static double getAngle(const double *quaternion, bool abs = false); + + //multiplies 3d vector with dcm derived from quaternion + template + static void multiplyVector(const double *quaternion, const T *vector, + T * result) { + result[0] = + (2. + * (quaternion[0] * quaternion[0] + + quaternion[3] * quaternion[3]) - 1.) + * vector[0] + + 2. + * (quaternion[0] * quaternion[1] + + quaternion[2] * quaternion[3]) + * vector[1] + + 2. + * (quaternion[0] * quaternion[2] + - quaternion[1] * quaternion[3]) + * vector[2]; + + result[1] = + 2. + * (quaternion[0] * quaternion[1] + - quaternion[2] * quaternion[3]) * vector[0] + + (2. + * (quaternion[1] * quaternion[1] + + quaternion[3] * quaternion[3]) - 1.) + * vector[1] + + 2. + * (quaternion[1] * quaternion[2] + + quaternion[0] * quaternion[3]) + * vector[2]; + + result[2] = + 2. + * (quaternion[0] * quaternion[2] + + quaternion[1] * quaternion[3]) * vector[0] + + 2. + * (quaternion[1] * quaternion[2] + - quaternion[0] * quaternion[3]) + * vector[1] + + (2. + * (quaternion[2] * quaternion[2] + + quaternion[3] * quaternion[3]) - 1.) + * vector[2]; + } + +private: + QuaternionOperations(); +}; + +#endif /* QUATERNIONOPERATIONS_H_ */ diff --git a/fsfw/globalfunctions/math/VectorOperations.h b/fsfw/globalfunctions/math/VectorOperations.h new file mode 100644 index 0000000..f65d226 --- /dev/null +++ b/fsfw/globalfunctions/math/VectorOperations.h @@ -0,0 +1,103 @@ +#ifndef VECTOROPERATIONS_ +#define VECTOROPERATIONS_ + +#include +#include + +template +class VectorOperations { +public: + virtual ~VectorOperations() { + } + + static void cross(const T left[], const T right[], T out[]) { + T temp[3] = { 0, 0, 0 }; + temp[0] = left[1] * right[2] - left[2] * right[1]; + temp[1] = left[2] * right[0] - left[0] * right[2]; + temp[2] = left[0] * right[1] - left[1] * right[0]; + out[0] = temp[0]; + out[1] = temp[1]; + out[2] = temp[2]; + } + + static T dot(const T a[], const T b[]) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; + } + + static void mulScalar(const T vector[], T scalar, T out[], uint8_t size) { + for (; size > 0; size--) { + out[size - 1] = vector[size - 1] * scalar; + } + } + + static void add(const T vector1[], const T vector2[], T sum[], + uint8_t size = 3) { + for (; size > 0; size--) { + sum[size - 1] = vector1[size - 1] + vector2[size - 1]; + } + } + + static void subtract(const T vector1[], const T vector2[], T sum[], + uint8_t size = 3) { + for (; size > 0; size--) { + sum[size - 1] = vector1[size - 1] - vector2[size - 1]; + } + } + + static T norm(const T *vector, uint8_t size) { + T result = 0; + for (; size > 0; size--) { + result += vector[size - 1] * vector[size - 1]; + } + result = sqrt(result); + return result; + } + + static void normalize(const T *vector, T *normalizedVector, uint8_t size) { + mulScalar(vector, 1 / norm(vector, size), normalizedVector, size); + } + + static T maxAbsValue(const T *vector, uint8_t size, uint8_t *index = 0) { + + T max = -1; + + for (; size > 0; size--) { + T abs = vector[size - 1]; + if (abs < 0) { + abs = -abs; + } + if (abs > max) { + max = abs; + if (index != 0) { + *index = size - 1; + } + } + } + return max; + } + + static T maxValue(const T *vector, uint8_t size, uint8_t *index = 0) { + + T max = -1; + + for (; size > 0; size--) { + if (vector[size - 1] > max) { + max = vector[size - 1]; + if (index != 0) { + *index = size - 1; + } + } + } + return max; + } + + + static void copy(const T *in, T *out, uint8_t size) { + mulScalar(in, 1, out, size); + } + +private: + VectorOperations(); +}; + +#endif /* VECTOROPERATIONS_ */ diff --git a/fsfw/globalfunctions/sign.h b/fsfw/globalfunctions/sign.h new file mode 100644 index 0000000..8828fca --- /dev/null +++ b/fsfw/globalfunctions/sign.h @@ -0,0 +1,8 @@ +#ifndef SIGN_H_ +#define SIGN_H_ + +template int sign(T val) { + return (T(0) < val) - (val < T(0)); +} + +#endif /* SIGN_H_ */ diff --git a/fsfw/globalfunctions/timevalOperations.cpp b/fsfw/globalfunctions/timevalOperations.cpp new file mode 100644 index 0000000..1228da0 --- /dev/null +++ b/fsfw/globalfunctions/timevalOperations.cpp @@ -0,0 +1,99 @@ +#include "timevalOperations.h" + +timeval& operator+=(timeval& lhs, const timeval& rhs) { + int64_t sum = lhs.tv_sec * 1000000. + lhs.tv_usec; + sum += rhs.tv_sec * 1000000. + rhs.tv_usec; + lhs.tv_sec = sum / 1000000; + lhs.tv_usec = sum - lhs.tv_sec * 1000000; + return lhs; +} + +timeval operator+(timeval lhs, const timeval& rhs) { + lhs += rhs; + return lhs; +} + +timeval& operator-=(timeval& lhs, const timeval& rhs) { + int64_t sum = lhs.tv_sec * 1000000. + lhs.tv_usec; + sum -= rhs.tv_sec * 1000000. + rhs.tv_usec; + lhs.tv_sec = sum / 1000000; + lhs.tv_usec = sum - lhs.tv_sec * 1000000; + return lhs; +} + +timeval operator-(timeval lhs, const timeval& rhs) { + lhs -= rhs; + return lhs; +} + +double operator/(const timeval& lhs, const timeval& rhs) { + double lhs64 = lhs.tv_sec * 1000000. + lhs.tv_usec; + double rhs64 = rhs.tv_sec * 1000000. + rhs.tv_usec; + return lhs64 / rhs64; +} + +timeval& operator/=(timeval& lhs, double scalar) { + int64_t product = lhs.tv_sec * 1000000. + lhs.tv_usec; + product /= scalar; + lhs.tv_sec = product / 1000000; + lhs.tv_usec = product - lhs.tv_sec * 1000000; + return lhs; +} + +timeval operator/(timeval lhs, double scalar) { + lhs /= scalar; + return lhs; +} + +timeval& operator*=(timeval& lhs, double scalar) { + int64_t product = lhs.tv_sec * 1000000. + lhs.tv_usec; + product *= scalar; + lhs.tv_sec = product / 1000000; + lhs.tv_usec = product - lhs.tv_sec * 1000000; + return lhs; +} + +timeval operator*(timeval lhs, double scalar) { + lhs *= scalar; + return lhs; +} + +timeval operator*(double scalar, timeval rhs) { + rhs *= scalar; + return rhs; +} + +bool operator==(const timeval& lhs, const timeval& rhs) { + int64_t lhs64 = lhs.tv_sec * 1000000. + lhs.tv_usec; + int64_t rhs64 = rhs.tv_sec * 1000000. + rhs.tv_usec; + return lhs64 == rhs64; +} +bool operator!=(const timeval& lhs, const timeval& rhs) { + return !operator==(lhs, rhs); +} +bool operator<(const timeval& lhs, const timeval& rhs) { + int64_t lhs64 = lhs.tv_sec * 1000000. + lhs.tv_usec; + int64_t rhs64 = rhs.tv_sec * 1000000. + rhs.tv_usec; + return lhs64 < rhs64; +} +bool operator>(const timeval& lhs, const timeval& rhs) { + return operator<(rhs, lhs); +} +bool operator<=(const timeval& lhs, const timeval& rhs) { + return !operator>(lhs, rhs); +} +bool operator>=(const timeval& lhs, const timeval& rhs) { + return !operator<(lhs, rhs); +} + +double timevalOperations::toDouble(const timeval timeval) { + double result = timeval.tv_sec * 1000000. + timeval.tv_usec; + return result / 1000000.; +} + +timeval timevalOperations::toTimeval(const double seconds) { + timeval tval; + tval.tv_sec = seconds; + tval.tv_usec = seconds *(double) 1e6 - (tval.tv_sec *1e6); + return tval; +} diff --git a/fsfw/globalfunctions/timevalOperations.h b/fsfw/globalfunctions/timevalOperations.h new file mode 100644 index 0000000..3977d5d --- /dev/null +++ b/fsfw/globalfunctions/timevalOperations.h @@ -0,0 +1,47 @@ +#ifndef TIMEVALOPERATIONS_H_ +#define TIMEVALOPERATIONS_H_ + +#include +#include + +timeval& operator+=(timeval& lhs, const timeval& rhs); + +timeval operator+(timeval lhs, const timeval& rhs); + +timeval& operator-=(timeval& lhs, const timeval& rhs); + +timeval operator-(timeval lhs, const timeval& rhs); + +double operator/(const timeval& lhs, const timeval& rhs); + +timeval& operator/=(timeval& lhs, double scalar); + +timeval operator/(timeval lhs, double scalar); + +timeval& operator*=(timeval& lhs, double scalar); + +timeval operator*(timeval lhs, double scalar); + +timeval operator*(double scalar, timeval rhs); + +bool operator==(const timeval& lhs, const timeval& rhs); +bool operator!=(const timeval& lhs, const timeval& rhs); +bool operator<(const timeval& lhs, const timeval& rhs); +bool operator>(const timeval& lhs, const timeval& rhs); +bool operator<=(const timeval& lhs, const timeval& rhs); +bool operator>=(const timeval& lhs, const timeval& rhs); + +namespace timevalOperations { +/** + * returns the seconds and subseconds stored in the timeval + * as double [s] + * + * + * @param timeval + * @return seconds + */ +double toDouble(const timeval timeval); +timeval toTimeval(const double seconds); +} + +#endif /* TIMEVALOPERATIONS_H_ */ diff --git a/fsfw/health/HasHealthIF.h b/fsfw/health/HasHealthIF.h new file mode 100644 index 0000000..86863ea --- /dev/null +++ b/fsfw/health/HasHealthIF.h @@ -0,0 +1,51 @@ +#ifndef FSFW_HEALTH_HASHEALTHIF_H_ +#define FSFW_HEALTH_HASHEALTHIF_H_ + +#include "../events/Event.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +class HasHealthIF { +public: + + enum HealthState: uint8_t { + HEALTHY = 1, + FAULTY = 0, + EXTERNAL_CONTROL = 2, + NEEDS_RECOVERY = 3, + PERMANENT_FAULTY = 4 + }; + + static const uint8_t INTERFACE_ID = CLASS_ID::HAS_HEALTH_IF; + static const ReturnValue_t OBJECT_NOT_HEALTHY = MAKE_RETURN_CODE(1); + static const ReturnValue_t INVALID_HEALTH_STATE = MAKE_RETURN_CODE(2); + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::SYSTEM_MANAGER_1; + static const Event HEALTH_INFO = MAKE_EVENT(6, SEVERITY::INFO); + static const Event CHILD_CHANGED_HEALTH = MAKE_EVENT(7, SEVERITY::INFO); + static const Event CHILD_PROBLEMS = MAKE_EVENT(8, SEVERITY::LOW); + static const Event OVERWRITING_HEALTH = MAKE_EVENT(9, SEVERITY::LOW); //!< Assembly overwrites health information of children to keep satellite alive. + static const Event TRYING_RECOVERY = MAKE_EVENT(10, SEVERITY::MEDIUM); //!< Someone starts a recovery of a component (typically power-cycle). No parameters. + static const Event RECOVERY_STEP = MAKE_EVENT(11, SEVERITY::MEDIUM); //!< Recovery is ongoing. Comes twice during recovery. P1: 0 for the first, 1 for the second event. P2: 0 + static const Event RECOVERY_DONE = MAKE_EVENT(12, SEVERITY::MEDIUM); //!< Recovery was completed. Not necessarily successful. No parameters. + + virtual ~HasHealthIF() { + } + + virtual MessageQueueId_t getCommandQueue() const = 0; + + /** + * @brief Set the Health State + * The parent will be informed, if the Health changes + * @param health + */ + virtual ReturnValue_t setHealth(HealthState health) = 0; + + /** + * @brief Get Health State + * @return Health State of the object + */ + virtual HasHealthIF::HealthState getHealth() = 0; +}; + +#endif /* FSFW_HEALTH_HASHEALTHIF_H_ */ diff --git a/fsfw/health/HealthHelper.cpp b/fsfw/health/HealthHelper.cpp new file mode 100644 index 0000000..d574634 --- /dev/null +++ b/fsfw/health/HealthHelper.cpp @@ -0,0 +1,105 @@ +#include "HealthHelper.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +HealthHelper::HealthHelper(HasHealthIF* owner, object_id_t objectId) : + objectId(objectId), owner(owner) { +} + +HealthHelper::~HealthHelper() { +} + +ReturnValue_t HealthHelper::handleHealthCommand(CommandMessage* message) { + switch (message->getCommand()) { + case HealthMessage::HEALTH_SET: + handleSetHealthCommand(message); + return HasReturnvaluesIF::RETURN_OK; + case HealthMessage::HEALTH_ANNOUNCE: { + eventSender->forwardEvent(HasHealthIF::HEALTH_INFO, getHealth(), + getHealth()); + } + return HasReturnvaluesIF::RETURN_OK; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +HasHealthIF::HealthState HealthHelper::getHealth() { + return healthTable->getHealth(objectId); +} + +ReturnValue_t HealthHelper::initialize(MessageQueueId_t parentQueue) { + setParentQueue(parentQueue); + return initialize(); +} + +void HealthHelper::setParentQueue(MessageQueueId_t parentQueue) { + this->parentQueue = parentQueue; +} + +ReturnValue_t HealthHelper::initialize() { + healthTable = objectManager->get(objects::HEALTH_TABLE); + eventSender = objectManager->get(objectId); + + if (healthTable == nullptr) { + sif::error << "HealthHelper::initialize: Health table object needs" + "to be created in factory." << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + if(eventSender == nullptr) { + sif::error << "HealthHelper::initialize: Owner has to implement " + "ReportingProxyIF." << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + ReturnValue_t result = healthTable->registerObject(objectId, + HasHealthIF::HEALTHY); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return HasReturnvaluesIF::RETURN_OK; +} + +void HealthHelper::setHealth(HasHealthIF::HealthState health) { + HasHealthIF::HealthState oldHealth = getHealth(); + eventSender->forwardEvent(HasHealthIF::HEALTH_INFO, health, oldHealth); + if (health != oldHealth) { + healthTable->setHealth(objectId, health); + informParent(health, oldHealth); + } +} + +void HealthHelper::informParent(HasHealthIF::HealthState health, + HasHealthIF::HealthState oldHealth) { + if (parentQueue == MessageQueueIF::NO_QUEUE) { + return; + } + CommandMessage information; + HealthMessage::setHealthMessage(&information, HealthMessage::HEALTH_INFO, + health, oldHealth); + if (MessageQueueSenderIF::sendMessage(parentQueue, &information, + owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) { + sif::debug << "HealthHelper::informParent: sending health reply failed." + << std::endl; + } +} + +void HealthHelper::handleSetHealthCommand(CommandMessage* command) { + ReturnValue_t result = owner->setHealth(HealthMessage::getHealth(command)); + if (command->getSender() == MessageQueueIF::NO_QUEUE) { + return; + } + CommandMessage reply; + if (result == HasReturnvaluesIF::RETURN_OK) { + HealthMessage::setHealthMessage(&reply, + HealthMessage::REPLY_HEALTH_SET); + } else { + reply.setReplyRejected(result, command->getCommand()); + } + if (MessageQueueSenderIF::sendMessage(command->getSender(), &reply, + owner->getCommandQueue()) != HasReturnvaluesIF::RETURN_OK) { + sif::debug << "HealthHelper::handleHealthCommand: sending health " + "reply failed." << std::endl; + + } +} diff --git a/fsfw/health/HealthHelper.h b/fsfw/health/HealthHelper.h new file mode 100644 index 0000000..08889fb --- /dev/null +++ b/fsfw/health/HealthHelper.h @@ -0,0 +1,131 @@ +#ifndef FSFW_HEALTH_HEALTHHELPER_H_ +#define FSFW_HEALTH_HEALTHHELPER_H_ + +#include "HasHealthIF.h" +#include "HealthMessage.h" +#include "HealthTableIF.h" + +#include "../events/EventManagerIF.h" +#include "../events/EventReportingProxyIF.h" +#include "../ipc/MessageQueueIF.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +/** + * @brief Helper class for Objects that implement HasHealthIF + * @details + * It takes care of registering with the Health Table as well as handling + * health commands (including replying to the sender) and updating + * the Health Table. + * + * If a parent is set in the ctor, the parent will be informed with a + * @c HEALTH_INFO message about changes in the health state. + * Note that a @c HEALTH_INFO is only generated if the Health + * changes, not for all @c HEALTH_SET commands received. + * + * It does NOT handle @c HEALTH_INFO messages + */ +class HealthHelper { +public: + + /** + * @param owner + * @param objectId The object Id to use when communication with + * the HealthTable + */ + HealthHelper(HasHealthIF* owner, object_id_t objectId); + + virtual ~HealthHelper(); + + /** + * Pointer to the Health Table + * + * only valid after initialize() has been called + */ + HealthTableIF *healthTable = nullptr; + + /** + * Proxy to forward events. + */ + EventReportingProxyIF* eventSender = nullptr; + + /** + * Try to handle the message. + * + * This function handles @c HEALTH_SET and @c HEALTH_READ commands. + * it updates the Health Table and generates a reply to the sender. + * + * @param message + * @return + * -@c RETURN_OK if the message was handled + * -@c RETURN_FAILED if the message could not be handled + * (ie it was not a @c HEALTH_SET or @c HEALTH_READ message) + */ + ReturnValue_t handleHealthCommand(CommandMessage *message); + + /** + * set the Health State + * + * The parent will be informed, if the Health changes + * + * @param health + */ + void setHealth(HasHealthIF::HealthState health); + + /** + * get Health State + * + * @return Health State of the object + */ + HasHealthIF::HealthState getHealth(); + + /** + * @param parentQueue The queue ID of the parent object. + * Set to 0 if no parent present + */ + void setParentQueue(MessageQueueId_t parentQueue); + + /** + * + * @param parentQueue The queue ID of the parent object. + * Set to 0 if no parent present + * @return + * -@c RETURN_OK if the Health Table was found and the object + * could be registered + * -@c RETURN_FAILED else + */ + ReturnValue_t initialize(MessageQueueId_t parentQueue ); + + ReturnValue_t initialize(); + +private: + /** + * the object id to use when communicating with the Health Table + */ + object_id_t objectId; + + /** + * The Queue of the parent + */ + MessageQueueId_t parentQueue = MessageQueueIF::NO_QUEUE; + + /** + * The one using the healthHelper. + */ + HasHealthIF* owner; + + /** + * if the #parentQueue is not NULL, a @c HEALTH_INFO message + * will be sent to this queue + * @param health + * The health is passed as parameter so that the number of + * calls to the health table can be minimized + * @param oldHealth information of the previous health state. + */ + void informParent(HasHealthIF::HealthState health, + HasHealthIF::HealthState oldHealth); + + void handleSetHealthCommand(CommandMessage *message); +}; + +#endif /* FSFW_HEALTH_HEALTHHELPER_H_ */ diff --git a/fsfw/health/HealthMessage.cpp b/fsfw/health/HealthMessage.cpp new file mode 100644 index 0000000..52479c2 --- /dev/null +++ b/fsfw/health/HealthMessage.cpp @@ -0,0 +1,30 @@ +#include "HealthMessage.h" + +void HealthMessage::setHealthMessage(CommandMessage* message, Command_t command, + HasHealthIF::HealthState health, HasHealthIF::HealthState oldHealth) { + message->setCommand(command); + message->setParameter(health); + message->setParameter2(oldHealth); +} + +void HealthMessage::setHealthMessage(CommandMessage* message, + Command_t command) { + message->setCommand(command); +} + +HasHealthIF::HealthState HealthMessage::getHealth( + const CommandMessage* message) { + return (HasHealthIF::HealthState) message->getParameter(); +} + +void HealthMessage::clear(CommandMessage* message) { + message->setCommand(CommandMessage::CMD_NONE); +} + +HealthMessage::HealthMessage() { +} + +HasHealthIF::HealthState HealthMessage::getOldHealth( + const CommandMessage* message) { + return (HasHealthIF::HealthState) message->getParameter2(); +} diff --git a/fsfw/health/HealthMessage.h b/fsfw/health/HealthMessage.h new file mode 100644 index 0000000..fb979c6 --- /dev/null +++ b/fsfw/health/HealthMessage.h @@ -0,0 +1,36 @@ +#ifndef FSFW_HEALTH_HEALTHMESSAGE_H_ +#define FSFW_HEALTH_HEALTHMESSAGE_H_ + +#include "HasHealthIF.h" +#include "../ipc/CommandMessage.h" + +class HealthMessage { +public: + static const uint8_t MESSAGE_ID = messagetypes::HEALTH_COMMAND; + + static const Command_t HEALTH_SET = MAKE_COMMAND_ID(1); + // No reply expected, health will be announced as event! + static const Command_t HEALTH_ANNOUNCE = MAKE_COMMAND_ID(2); + // Same as before, but all objects in health table will + // announce their health as events. + static const Command_t HEALTH_ANNOUNCE_ALL = MAKE_COMMAND_ID(3); + + static const Command_t HEALTH_INFO = MAKE_COMMAND_ID(5); + static const Command_t REPLY_HEALTH_SET = MAKE_COMMAND_ID(6); + + static void setHealthMessage(CommandMessage *message, Command_t command, + HasHealthIF::HealthState health, + HasHealthIF::HealthState oldHealth = HasHealthIF::FAULTY); + static void setHealthMessage(CommandMessage *message, Command_t command); + + static HasHealthIF::HealthState getHealth(const CommandMessage *message); + + static HasHealthIF::HealthState getOldHealth(const CommandMessage *message); + + static void clear(CommandMessage *message); + +private: + HealthMessage(); +}; + +#endif /* FSFW_HEALTH_HEALTHMESSAGE_H_ */ diff --git a/fsfw/health/HealthTable.cpp b/fsfw/health/HealthTable.cpp new file mode 100644 index 0000000..2b8b671 --- /dev/null +++ b/fsfw/health/HealthTable.cpp @@ -0,0 +1,94 @@ +#include "HealthTable.h" +#include "../ipc/MutexHelper.h" +#include "../ipc/MutexFactory.h" +#include "../serialize/SerializeAdapter.h" + +HealthTable::HealthTable(object_id_t objectid) : + SystemObject(objectid) { + mutex = MutexFactory::instance()->createMutex();; + + mapIterator = healthMap.begin(); +} + +void HealthTable::setMutexTimeout(MutexIF::TimeoutType timeoutType, + uint32_t timeoutMs) { + this->timeoutType = timeoutType; + this->mutexTimeoutMs = timeoutMs; +} + +HealthTable::~HealthTable() { + MutexFactory::instance()->deleteMutex(mutex); +} + +ReturnValue_t HealthTable::registerObject(object_id_t object, + HasHealthIF::HealthState initilialState) { + if (healthMap.count(object) != 0) { + return HasReturnvaluesIF::RETURN_FAILED; + } + healthMap.emplace(object, initilialState); + return HasReturnvaluesIF::RETURN_OK; +} + +void HealthTable::setHealth(object_id_t object, + HasHealthIF::HealthState newState) { + MutexHelper(mutex, timeoutType, mutexTimeoutMs); + HealthMap::iterator iter = healthMap.find(object); + if (iter != healthMap.end()) { + iter->second = newState; + } +} + +HasHealthIF::HealthState HealthTable::getHealth(object_id_t object) { + HasHealthIF::HealthState state = HasHealthIF::HEALTHY; + MutexHelper(mutex, timeoutType, mutexTimeoutMs); + HealthMap::iterator iter = healthMap.find(object); + if (iter != healthMap.end()) { + state = iter->second; + } + return state; +} + +bool HealthTable::hasHealth(object_id_t object) { + MutexHelper(mutex, timeoutType, mutexTimeoutMs); + HealthMap::iterator iter = healthMap.find(object); + if (iter != healthMap.end()) { + return true; + } + return false; +} + +size_t HealthTable::getPrintSize() { + MutexHelper(mutex, timeoutType, mutexTimeoutMs); + uint32_t size = healthMap.size() * sizeof(object_id_t) + + sizeof(HasHealthIF::HealthState) + sizeof(uint16_t); + return size; +} + +void HealthTable::printAll(uint8_t* pointer, size_t maxSize) { + MutexHelper(mutex, timeoutType, mutexTimeoutMs); + size_t size = 0; + uint16_t count = healthMap.size(); + SerializeAdapter::serialize(&count, + &pointer, &size, maxSize, SerializeIF::Endianness::BIG); + for (const auto& health: healthMap) { + SerializeAdapter::serialize(&health.first, + &pointer, &size, maxSize, SerializeIF::Endianness::BIG); + uint8_t healthValue = health.second; + SerializeAdapter::serialize(&healthValue, &pointer, &size, + maxSize, SerializeIF::Endianness::BIG); + } +} + +ReturnValue_t HealthTable::iterate(HealthEntry *value, bool reset) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + MutexHelper(mutex, timeoutType, mutexTimeoutMs); + if (reset) { + mapIterator = healthMap.begin(); + } + if (mapIterator == healthMap.end()) { + result = HasReturnvaluesIF::RETURN_FAILED; + } + *value = *mapIterator; + mapIterator++; + return result; +} diff --git a/fsfw/health/HealthTable.h b/fsfw/health/HealthTable.h new file mode 100644 index 0000000..cea34f6 --- /dev/null +++ b/fsfw/health/HealthTable.h @@ -0,0 +1,47 @@ +#ifndef FSFW_HEALTH_HEALTHTABLE_H_ +#define FSFW_HEALTH_HEALTHTABLE_H_ + +#include "HealthTableIF.h" +#include "../objectmanager/SystemObject.h" +#include "../ipc/MutexIF.h" +#include + + +class HealthTable: public HealthTableIF, public SystemObject { +public: + HealthTable(object_id_t objectid); + virtual ~HealthTable(); + + void setMutexTimeout(MutexIF::TimeoutType timeoutType, uint32_t timeoutMs); + + /** HealthTableIF overrides */ + virtual ReturnValue_t registerObject(object_id_t object, + HasHealthIF::HealthState initilialState = + HasHealthIF::HEALTHY) override; + virtual size_t getPrintSize() override; + virtual void printAll(uint8_t *pointer, size_t maxSize) override; + + /** ManagesHealthIF overrides */ + virtual bool hasHealth(object_id_t object) override; + virtual void setHealth(object_id_t object, + HasHealthIF::HealthState newState) override; + virtual HasHealthIF::HealthState getHealth(object_id_t) override; + +protected: + using HealthMap = std::map; + using HealthEntry = std::pair; + + MutexIF* mutex; + MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING; + uint32_t mutexTimeoutMs = 20; + + HealthMap healthMap; + + HealthMap::iterator mapIterator; + + virtual ReturnValue_t iterate( + HealthEntry* value, + bool reset = false) override; +}; + +#endif /* FSFW_HEALTH_HEALTHTABLE_H_ */ diff --git a/fsfw/health/HealthTableIF.h b/fsfw/health/HealthTableIF.h new file mode 100644 index 0000000..d61e676 --- /dev/null +++ b/fsfw/health/HealthTableIF.h @@ -0,0 +1,24 @@ +#ifndef FSFW_HEALTH_HEALTHTABLEIF_H_ +#define FSFW_HEALTH_HEALTHTABLEIF_H_ + +#include "ManagesHealthIF.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +class HealthTableIF: public ManagesHealthIF { +public: + virtual ~HealthTableIF() {} + + virtual ReturnValue_t registerObject(object_id_t object, + HasHealthIF::HealthState initilialState = HasHealthIF::HEALTHY) = 0; + + virtual size_t getPrintSize() = 0; + virtual void printAll(uint8_t *pointer, size_t maxSize) = 0; + +protected: + virtual ReturnValue_t iterate( + std::pair *value, + bool reset = false) = 0; +}; + +#endif /* FRAMEWORK_HEALTH_HEALTHTABLEIF_H_ */ diff --git a/fsfw/health/ManagesHealthIF.h b/fsfw/health/ManagesHealthIF.h new file mode 100644 index 0000000..2edfcec --- /dev/null +++ b/fsfw/health/ManagesHealthIF.h @@ -0,0 +1,53 @@ +#ifndef FSFW_HEALTH_MANAGESHEALTHIF_H_ +#define FSFW_HEALTH_MANAGESHEALTHIF_H_ + +#include "HasHealthIF.h" +#include "../objectmanager/ObjectManagerIF.h" + +class ManagesHealthIF { +public: + virtual ~ManagesHealthIF() { + } + virtual bool hasHealth(object_id_t object) = 0; + virtual void setHealth(object_id_t object, + HasHealthIF::HealthState newState) = 0; + virtual HasHealthIF::HealthState getHealth(object_id_t) = 0; + + virtual bool isHealthy(object_id_t object) { + return (getHealth(object) == HasHealthIF::HEALTHY); + } + + virtual bool isHealthy(HasHealthIF::HealthState health) { + return (health == HasHealthIF::HEALTHY); + } + + virtual bool isFaulty(object_id_t object) { + HasHealthIF::HealthState health = getHealth(object); + return isFaulty(health); + } + + virtual bool isPermanentFaulty(object_id_t object) { + HasHealthIF::HealthState health = getHealth(object); + return isPermanentFaulty(health); + } + + virtual bool isPermanentFaulty(HasHealthIF::HealthState health) { + return (health == HasHealthIF::PERMANENT_FAULTY); + } + + static bool isFaulty(HasHealthIF::HealthState health) { + return ((health == HasHealthIF::FAULTY) + || (health == HasHealthIF::PERMANENT_FAULTY) + || (health == HasHealthIF::NEEDS_RECOVERY)); + } + + virtual bool isCommandable(object_id_t object) { + return (getHealth(object) != HasHealthIF::EXTERNAL_CONTROL); + } + + virtual bool isCommandable(HasHealthIF::HealthState health) { + return (health != HasHealthIF::EXTERNAL_CONTROL); + } +}; + +#endif /* FSFW_HEALTH_MANAGESHEALTHIF_H_ */ diff --git a/fsfw/internalError/InternalErrorReporter.cpp b/fsfw/internalError/InternalErrorReporter.cpp new file mode 100644 index 0000000..861e159 --- /dev/null +++ b/fsfw/internalError/InternalErrorReporter.cpp @@ -0,0 +1,126 @@ +#include "InternalErrorReporter.h" + +#include "../datapool/DataSet.h" +#include "../datapool/PoolVariable.h" +#include "../ipc/MutexFactory.h" + +#include "../serviceinterface/ServiceInterfaceStream.h" + +InternalErrorReporter::InternalErrorReporter(object_id_t setObjectId, + uint32_t queuePoolId, uint32_t tmPoolId, uint32_t storePoolId) : + SystemObject(setObjectId), mutex(NULL), queuePoolId(queuePoolId), tmPoolId( + tmPoolId), storePoolId( + storePoolId), queueHits(0), tmHits(0), storeHits( + 0) { + mutex = MutexFactory::instance()->createMutex(); +} + +InternalErrorReporter::~InternalErrorReporter() { + MutexFactory::instance()->deleteMutex(mutex); +} + +ReturnValue_t InternalErrorReporter::performOperation(uint8_t opCode) { + + DataSet mySet; + PoolVariable queueHitsInPool(queuePoolId, &mySet, + PoolVariableIF::VAR_READ_WRITE); + PoolVariable tmHitsInPool(tmPoolId, &mySet, + PoolVariableIF::VAR_READ_WRITE); + + PoolVariable storeHitsInPool(storePoolId, &mySet, + PoolVariableIF::VAR_READ_WRITE); + mySet.read(); + + uint32_t newQueueHits = getAndResetQueueHits(); + uint32_t newTmHits = getAndResetTmHits(); + uint32_t newStoreHits = getAndResetStoreHits(); + + queueHitsInPool.value += newQueueHits; + tmHitsInPool.value += newTmHits; + storeHitsInPool.value += newStoreHits; + + mySet.commit(PoolVariableIF::VALID); + + return HasReturnvaluesIF::RETURN_OK; +} + +void InternalErrorReporter::queueMessageNotSent() { + incrementQueueHits(); +} + +void InternalErrorReporter::lostTm() { + incrementTmHits(); +} + +uint32_t InternalErrorReporter::getAndResetQueueHits() { + uint32_t value; + mutex->lockMutex(MutexIF::BLOCKING); + value = queueHits; + queueHits = 0; + mutex->unlockMutex(); + return value; +} + +uint32_t InternalErrorReporter::getQueueHits() { + uint32_t value; + mutex->lockMutex(MutexIF::BLOCKING); + value = queueHits; + mutex->unlockMutex(); + return value; +} + +void InternalErrorReporter::incrementQueueHits() { + mutex->lockMutex(MutexIF::BLOCKING); + queueHits++; + mutex->unlockMutex(); +} + +uint32_t InternalErrorReporter::getAndResetTmHits() { + uint32_t value; + mutex->lockMutex(MutexIF::BLOCKING); + value = tmHits; + tmHits = 0; + mutex->unlockMutex(); + return value; +} + +uint32_t InternalErrorReporter::getTmHits() { + uint32_t value; + mutex->lockMutex(MutexIF::BLOCKING); + value = tmHits; + mutex->unlockMutex(); + return value; +} + +void InternalErrorReporter::incrementTmHits() { + mutex->lockMutex(MutexIF::BLOCKING); + tmHits++; + mutex->unlockMutex(); +} + +void InternalErrorReporter::storeFull() { + incrementStoreHits(); +} + +uint32_t InternalErrorReporter::getAndResetStoreHits() { + uint32_t value; + mutex->lockMutex(MutexIF::BLOCKING); + value = storeHits; + storeHits = 0; + mutex->unlockMutex(); + return value; +} + +uint32_t InternalErrorReporter::getStoreHits() { + uint32_t value; + mutex->lockMutex(MutexIF::BLOCKING); + value = storeHits; + mutex->unlockMutex(); + return value; +} + +void InternalErrorReporter::incrementStoreHits() { + mutex->lockMutex(MutexIF::BLOCKING); + storeHits++; + mutex->unlockMutex(); +} diff --git a/fsfw/internalError/InternalErrorReporter.h b/fsfw/internalError/InternalErrorReporter.h new file mode 100644 index 0000000..9aa24a0 --- /dev/null +++ b/fsfw/internalError/InternalErrorReporter.h @@ -0,0 +1,50 @@ +#ifndef INTERNALERRORREPORTER_H_ +#define INTERNALERRORREPORTER_H_ + +#include "InternalErrorReporterIF.h" + +#include "../tasks/ExecutableObjectIF.h" +#include "../objectmanager/SystemObject.h" +#include "../ipc/MutexIF.h" + +class InternalErrorReporter: public SystemObject, + public ExecutableObjectIF, + public InternalErrorReporterIF { +public: + InternalErrorReporter(object_id_t setObjectId, uint32_t queuePoolId, + uint32_t tmPoolId, uint32_t storePoolId); + virtual ~InternalErrorReporter(); + + virtual ReturnValue_t performOperation(uint8_t opCode); + + virtual void queueMessageNotSent(); + + virtual void lostTm(); + + virtual void storeFull(); +protected: + MutexIF* mutex; + + uint32_t queuePoolId; + uint32_t tmPoolId; + uint32_t storePoolId; + + uint32_t queueHits; + uint32_t tmHits; + uint32_t storeHits; + + uint32_t getAndResetQueueHits(); + uint32_t getQueueHits(); + void incrementQueueHits(); + + uint32_t getAndResetTmHits(); + uint32_t getTmHits(); + void incrementTmHits(); + + uint32_t getAndResetStoreHits(); + uint32_t getStoreHits(); + void incrementStoreHits(); + +}; + +#endif /* INTERNALERRORREPORTER_H_ */ diff --git a/fsfw/internalError/InternalErrorReporterIF.h b/fsfw/internalError/InternalErrorReporterIF.h new file mode 100644 index 0000000..3484c18 --- /dev/null +++ b/fsfw/internalError/InternalErrorReporterIF.h @@ -0,0 +1,27 @@ +#ifndef INTERNALERRORREPORTERIF_H_ +#define INTERNALERRORREPORTERIF_H_ + +class InternalErrorReporterIF { +public: + virtual ~InternalErrorReporterIF() { + } + + /** + * Thread safe + */ + virtual void queueMessageNotSent() = 0; + + /** + * Thread safe + */ + virtual void lostTm() = 0; + + /** + * Thread safe + */ + virtual void storeFull() = 0; + + +}; + +#endif /* INTERNALERRORREPORTERIF_H_ */ diff --git a/fsfw/ipc/CommandMessage.cpp b/fsfw/ipc/CommandMessage.cpp new file mode 100644 index 0000000..513debd --- /dev/null +++ b/fsfw/ipc/CommandMessage.cpp @@ -0,0 +1,111 @@ +#include "CommandMessage.h" +#include "CommandMessageCleaner.h" +#include + +CommandMessage::CommandMessage() { + MessageQueueMessage::setMessageSize(DEFAULT_COMMAND_MESSAGE_SIZE); + setCommand(CMD_NONE); +} + +CommandMessage::CommandMessage(Command_t command, uint32_t parameter1, + uint32_t parameter2) { + MessageQueueMessage::setMessageSize(DEFAULT_COMMAND_MESSAGE_SIZE); + setCommand(command); + setParameter(parameter1); + setParameter2(parameter2); +} + +Command_t CommandMessage::getCommand() const { + Command_t command; + std::memcpy(&command, MessageQueueMessage::getData(), sizeof(Command_t)); + return command; +} + +void CommandMessage::setCommand(Command_t command) { + std::memcpy(MessageQueueMessage::getData(), &command, sizeof(Command_t)); +} + +uint8_t CommandMessage::getMessageType() const { + // first byte of command ID. + return getCommand() >> 8 & 0xff; +} + +uint32_t CommandMessage::getParameter() const { + uint32_t parameter1; + std::memcpy(¶meter1, this->getData(), sizeof(parameter1)); + return parameter1; +} + +void CommandMessage::setParameter(uint32_t parameter1) { + std::memcpy(this->getData(), ¶meter1, sizeof(parameter1)); +} + +uint32_t CommandMessage::getParameter2() const { + uint32_t parameter2; + std::memcpy(¶meter2, this->getData() + sizeof(uint32_t), + sizeof(parameter2)); + return parameter2; +} + +void CommandMessage::setParameter2(uint32_t parameter2) { + std::memcpy(this->getData() + sizeof(uint32_t), ¶meter2, + sizeof(parameter2)); +} + +uint32_t CommandMessage::getParameter3() const { + uint32_t parameter3; + std::memcpy(¶meter3, this->getData() + 2 * sizeof(uint32_t), + sizeof(parameter3)); + return parameter3; +} + +void CommandMessage::setParameter3(uint32_t parameter3) { + std::memcpy(this->getData() + 2 * sizeof(uint32_t), ¶meter3, + sizeof(parameter3)); +} + +size_t CommandMessage::getMinimumMessageSize() const { + return MINIMUM_COMMAND_MESSAGE_SIZE; +} + +void CommandMessage::clearCommandMessage() { + clear(); +} + +void CommandMessage::clear() { + CommandMessageCleaner::clearCommandMessage(this); +} + +bool CommandMessage::isClearedCommandMessage() { + return getCommand() == CMD_NONE; +} + +void CommandMessage::setToUnknownCommand() { + Command_t initialCommand = getCommand(); + this->clear(); + setReplyRejected(UNKNOWN_COMMAND, initialCommand); +} + +void CommandMessage::setReplyRejected(ReturnValue_t reason, + Command_t initialCommand) { + setCommand(REPLY_REJECTED); + setParameter(reason); + setParameter2(initialCommand); +} + +ReturnValue_t CommandMessage::getReplyRejectedReason( + Command_t *initialCommand) const { + ReturnValue_t reason = getParameter(); + if(initialCommand != nullptr) { + *initialCommand = getParameter2(); + } + return reason; +} + +uint8_t* CommandMessage::getData() { + return MessageQueueMessage::getData() + sizeof(Command_t); +} + +const uint8_t* CommandMessage::getData() const { + return MessageQueueMessage::getData() + sizeof(Command_t); +} diff --git a/fsfw/ipc/CommandMessage.h b/fsfw/ipc/CommandMessage.h new file mode 100644 index 0000000..28822dd --- /dev/null +++ b/fsfw/ipc/CommandMessage.h @@ -0,0 +1,131 @@ +#ifndef FSFW_IPC_COMMANDMESSAGE_H_ +#define FSFW_IPC_COMMANDMESSAGE_H_ + +#include "CommandMessageIF.h" + +#include "MessageQueueMessage.h" +#include "FwMessageTypes.h" + +/** + * @brief Default command message used to pass command messages between tasks. + * Primary message type for IPC. Contains sender, 2-byte command ID + * field, and 3 4-byte parameter + * @details + * It operates on an external memory which is contained inside a + * class implementing MessageQueueMessageIF by taking its address. + * This allows for a more flexible designs of message implementations. + * The pointer can be passed to different message implementations without + * the need of unnecessary copying. + * + * The command message is based of the generic MessageQueueMessage which + * currently has an internal message size of 28 bytes. + * @author Bastian Baetz + */ +class CommandMessage: public MessageQueueMessage, public CommandMessageIF { +public: + /** + * Default size can accomodate 3 4-byte parameters. + */ + static constexpr size_t DEFAULT_COMMAND_MESSAGE_SIZE = + CommandMessageIF::MINIMUM_COMMAND_MESSAGE_SIZE + + 3 * sizeof(uint32_t); + + /** + * @brief Default Constructor, does not initialize anything. + * @details + * This constructor should be used when receiving a Message, as the + * content is filled by the MessageQueue. + */ + CommandMessage(); + /** + * This constructor creates a new message with all message content + * initialized + * + * @param command The DeviceHandlerCommand_t that will be sent + * @param parameter1 The first parameter + * @param parameter2 The second parameter + */ + CommandMessage(Command_t command, uint32_t parameter1, uint32_t parameter2); + + /** + * @brief Default Destructor + */ + virtual ~CommandMessage() {} + + /** + * Read the DeviceHandlerCommand_t that is stored in the message, + * usually used after receiving. + * + * @return the Command stored in the Message + */ + virtual Command_t getCommand() const override; + /** + * Set the command type of the message. Default implementation also + * sets the message type, which will be the first byte of the command ID. + * @param the Command to be sent + */ + virtual void setCommand(Command_t command); + + virtual uint8_t* getData() override; + virtual const uint8_t* getData() const override; + + /** + * Get the first parameter of the message + * @return the first Parameter of the message + */ + uint32_t getParameter() const; + /** + * Set the first parameter of the message + * @param the first parameter of the message + */ + void setParameter(uint32_t parameter1); + uint32_t getParameter2() const; + void setParameter2(uint32_t parameter2); + uint32_t getParameter3() const; + void setParameter3(uint32_t parameter3); + + /** + * check if a message was cleared + * + * @return if the command is CMD_NONE + */ + bool isClearedCommandMessage(); + + /** + * Sets the command to REPLY_REJECTED with parameter UNKNOWN_COMMAND. + * Is needed quite often, so we better code it once only. + */ + void setToUnknownCommand() override; + + /** + * A command message can be rejected and needs to offer a function + * to set a rejected reply + * @param reason + * @param initialCommand + */ + void setReplyRejected(ReturnValue_t reason, + Command_t initialCommand) override; + /** + * Corrensonding getter function. + * @param initialCommand + * @return + */ + ReturnValue_t getReplyRejectedReason( + Command_t* initialCommand = nullptr) const override; + + + virtual void clear() override; + void clearCommandMessage(); + + /** + * Extract message ID, which is the first byte of the command ID for the + * default implementation. + * @return + */ + virtual uint8_t getMessageType() const override; + + /** MessageQueueMessageIF functions used for minimum size check. */ + size_t getMinimumMessageSize() const override; +}; + +#endif /* FSFW_IPC_COMMANDMESSAGE_H_ */ diff --git a/fsfw/ipc/CommandMessageCleaner.cpp b/fsfw/ipc/CommandMessageCleaner.cpp new file mode 100644 index 0000000..6a99b4d --- /dev/null +++ b/fsfw/ipc/CommandMessageCleaner.cpp @@ -0,0 +1,45 @@ +#include "../ipc/CommandMessageCleaner.h" + +#include "../devicehandlers/DeviceHandlerMessage.h" +#include "../health/HealthMessage.h" +#include "../memory/MemoryMessage.h" +#include "../modes/ModeMessage.h" +#include "../monitoring/MonitoringMessage.h" +#include "../subsystem/modes/ModeSequenceMessage.h" +#include "../tmstorage/TmStoreMessage.h" +#include "../parameters/ParameterMessage.h" + +void CommandMessageCleaner::clearCommandMessage(CommandMessage* message) { + switch(message->getMessageType()){ + case messagetypes::MODE_COMMAND: + ModeMessage::clear(message); + break; + case messagetypes::HEALTH_COMMAND: + HealthMessage::clear(message); + break; + case messagetypes::MODE_SEQUENCE: + ModeSequenceMessage::clear(message); + break; + case messagetypes::ACTION: + ActionMessage::clear(message); + break; + case messagetypes::DEVICE_HANDLER_COMMAND: + DeviceHandlerMessage::clear(message); + break; + case messagetypes::MEMORY: + MemoryMessage::clear(message); + break; + case messagetypes::MONITORING: + MonitoringMessage::clear(message); + break; + case messagetypes::TM_STORE: + TmStoreMessage::clear(message); + break; + case messagetypes::PARAMETER: + ParameterMessage::clear(message); + break; + default: + messagetypes::clearMissionMessage(message); + break; + } +} diff --git a/fsfw/ipc/CommandMessageCleaner.h b/fsfw/ipc/CommandMessageCleaner.h new file mode 100644 index 0000000..2bf4a19 --- /dev/null +++ b/fsfw/ipc/CommandMessageCleaner.h @@ -0,0 +1,16 @@ +#ifndef FRAMEWORK_IPC_COMMANDMESSAGECLEANER_H_ +#define FRAMEWORK_IPC_COMMANDMESSAGECLEANER_H_ +#include "../ipc/CommandMessage.h" + +namespace messagetypes { +// Implemented in config. +void clearMissionMessage(CommandMessage* message); +} + +class CommandMessageCleaner { +public: + static void clearCommandMessage(CommandMessage* message); +}; + + +#endif /* FRAMEWORK_IPC_COMMANDMESSAGECLEANER_H_ */ diff --git a/fsfw/ipc/CommandMessageIF.h b/fsfw/ipc/CommandMessageIF.h new file mode 100644 index 0000000..aafa40e --- /dev/null +++ b/fsfw/ipc/CommandMessageIF.h @@ -0,0 +1,73 @@ +#ifndef FSFW_IPC_COMMANDMESSAGEIF_H_ +#define FSFW_IPC_COMMANDMESSAGEIF_H_ + +#include "MessageQueueMessageIF.h" +#include "FwMessageTypes.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +#define MAKE_COMMAND_ID( number ) ((MESSAGE_ID << 8) + (number)) +typedef uint16_t Command_t; + +class CommandMessageIF { +public: + /** + * Header consists of sender ID and command ID. + */ + static constexpr size_t HEADER_SIZE = MessageQueueMessageIF::HEADER_SIZE + + sizeof(Command_t); + /** + * This minimum size is derived from the interface requirement to be able + * to set a rejected reply, which contains a returnvalue and the initial + * command. + */ + static constexpr size_t MINIMUM_COMMAND_MESSAGE_SIZE = + CommandMessageIF::HEADER_SIZE + sizeof(ReturnValue_t) + + sizeof(Command_t); + + static const uint8_t INTERFACE_ID = CLASS_ID::COMMAND_MESSAGE; + static const ReturnValue_t UNKNOWN_COMMAND = MAKE_RETURN_CODE(0x01); + + static const uint8_t MESSAGE_ID = messagetypes::COMMAND; + //! Used internally, shall be ignored + static const Command_t CMD_NONE = MAKE_COMMAND_ID( 0 ); + static const Command_t REPLY_COMMAND_OK = MAKE_COMMAND_ID( 1 ); + //! Reply indicating that the current command was rejected, + //! par1 should contain the error code + static const Command_t REPLY_REJECTED = MAKE_COMMAND_ID( 2 ); + + virtual ~CommandMessageIF() {}; + + /** + * A command message shall have a uint16_t command ID field. + * @return + */ + virtual Command_t getCommand() const = 0; + /** + * A command message shall have a uint8_t message type ID field. + * @return + */ + virtual uint8_t getMessageType() const = 0; + + /** + * A command message can be rejected and needs to offer a function + * to set a rejected reply + * @param reason + * @param initialCommand + */ + virtual void setReplyRejected(ReturnValue_t reason, + Command_t initialCommand) = 0; + /** + * Corrensonding getter function. + * @param initialCommand + * @return + */ + virtual ReturnValue_t getReplyRejectedReason( + Command_t* initialCommand = nullptr) const = 0; + + virtual void setToUnknownCommand() = 0; + + virtual void clear() = 0; + +}; + +#endif /* FSFW_IPC_COMMANDMESSAGEIF_H_ */ diff --git a/fsfw/ipc/FwMessageTypes.h b/fsfw/ipc/FwMessageTypes.h new file mode 100644 index 0000000..e820b1d --- /dev/null +++ b/fsfw/ipc/FwMessageTypes.h @@ -0,0 +1,21 @@ +#ifndef FRAMEWORK_IPC_FWMESSAGETYPES_H_ +#define FRAMEWORK_IPC_FWMESSAGETYPES_H_ + +namespace messagetypes { +//Remember to add new Message Types to the clearCommandMessage function! +enum FW_MESSAGE_TYPE { + COMMAND = 0, + MODE_COMMAND, + HEALTH_COMMAND, + MODE_SEQUENCE, + ACTION, + TM_STORE, + DEVICE_HANDLER_COMMAND, + MONITORING, + MEMORY, + PARAMETER, + FW_MESSAGES_COUNT +}; +} + +#endif /* FRAMEWORK_IPC_FWMESSAGETYPES_H_ */ diff --git a/fsfw/ipc/MessageQueueIF.h b/fsfw/ipc/MessageQueueIF.h new file mode 100644 index 0000000..1c06521 --- /dev/null +++ b/fsfw/ipc/MessageQueueIF.h @@ -0,0 +1,173 @@ +#ifndef FSFW_IPC_MESSAGEQUEUEIF_H_ +#define FSFW_IPC_MESSAGEQUEUEIF_H_ + +#include "messageQueueDefinitions.h" +#include "MessageQueueMessageIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +#include + + +// COULDDO: We could support blocking calls +// semaphores are being implemented, which makes this idea even more iteresting. + +/** + * @defgroup message_queue Message Queue + * @brief Message Queue related software components + */ +class MessageQueueIF { +public: + static const MessageQueueId_t NO_QUEUE = 0; + + static const uint8_t INTERFACE_ID = CLASS_ID::MESSAGE_QUEUE_IF; + //! No new messages on the queue + static const ReturnValue_t EMPTY = MAKE_RETURN_CODE(1); + //! No space left for more messages + static const ReturnValue_t FULL = MAKE_RETURN_CODE(2); + //! Returned if a reply method was called without partner + static const ReturnValue_t NO_REPLY_PARTNER = MAKE_RETURN_CODE(3); + //! Returned if the target destination is invalid. + static constexpr ReturnValue_t DESTINVATION_INVALID = MAKE_RETURN_CODE(4); + + virtual ~MessageQueueIF() {} + /** + * @brief This operation sends a message to the last communication partner. + * @details + * This operation simplifies answering an incoming message by using the + * stored lastParnter information as destination. If there was no message + * received yet (i.e. lastPartner is zero), an error code is returned. + * @param message + * A pointer to a previously created message, which is sent. + * @return + * -@c RETURN_OK if ok + * -@c NO_REPLY_PARTNER Should return NO_REPLY_PARTNER if partner was found. + */ + virtual ReturnValue_t reply(MessageQueueMessageIF* message) = 0; + + /** + * @brief This function reads available messages from the message queue + * and returns the sender. + * @details + * It works identically to the other receiveMessage call, but in addition + * returns the sender's queue id. + * @param message + * A pointer to a message in which the received data is stored. + * @param receivedFrom + * A pointer to a queue id in which the sender's id is stored. + */ + virtual ReturnValue_t receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t *receivedFrom) = 0; + + /** + * @brief This function reads available messages from the message queue. + * @details + * If data is available it is stored in the passed message pointer. + * The message's original content is overwritten and the sendFrom + * information is stored in theblastPartner attribute. Else, the lastPartner + * information remains untouched, the message's content is cleared and the + * function returns immediately. + * @param message + * A pointer to a message in which the received data is stored. + * @return -@c RETURN_OK on success + * -@c MessageQueueIF::EMPTY if queue is empty + */ + virtual ReturnValue_t receiveMessage(MessageQueueMessageIF* message) = 0; + /** + * Deletes all pending messages in the queue. + * @param count The number of flushed messages. + * @return RETURN_OK on success. + */ + virtual ReturnValue_t flush(uint32_t* count) = 0; + /** + * @brief This method returns the message queue + * id of the last communication partner. + */ + virtual MessageQueueId_t getLastPartner() const = 0; + /** + * @brief This method returns the message queue + * id of this class's message queue. + */ + virtual MessageQueueId_t getId() const = 0; + + /** + * @brief With the sendMessage call, a queue message + * is sent to a receiving queue. + * @details + * This method takes the message provided, adds the sentFrom information + * and passes it on to the destination provided with an operating system + * call. The OS's returnvalue is returned. + * @param sendTo + * This parameter specifies the message queue id to send the message to. + * @param message + * This is a pointer to a previously created message, which is sent. + * @param sentFrom + * The sentFrom information can be set to inject the sender's queue id + * into the message. This variable is set to zero by default. + * @param ignoreFault + * If set to true, the internal software fault counter is not incremented + * if queue is full (if implemented). + * @return -@c RETURN_OK on success + * -@c MessageQueueIF::FULL if queue is full + */ + virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault = false ) = 0; + + /** + * @brief This operation sends a message to the given destination. + * @details + * It directly uses the sendMessage call of the MessageQueueSender parent, + * but passes its queue id as "sentFrom" parameter. + * @param sendTo + * This parameter specifies the message queue id of the destination + * message queue. + * @param message + * A pointer to a previously created message, which is sent. + * @param ignoreFault + * If set to true, the internal software fault counter is not incremented + * if queue is full. + */ + virtual ReturnValue_t sendMessage( MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault = false ) = 0; + + /** + * @brief The sendToDefaultFrom method sends a queue message + * to the default destination. + * @details + * In all other aspects, it works identical to the sendMessage method. + * @param message + * This is a pointer to a previously created message, which is sent. + * @param sentFrom + * The sentFrom information can be set to inject the sender's queue id + * into the message. This variable is set to zero by default. + * @return -@c RETURN_OK on success + * -@c MessageQueueIF::FULL if queue is full + */ + virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message, + MessageQueueId_t sentFrom, bool ignoreFault = false ) = 0; + /** + * @brief This operation sends a message to the default destination. + * @details + * As in the sendMessage method, this function uses the sendToDefault + * call of the Implementation class and adds its queue id as + * "sentFrom" information. + * @param message A pointer to a previously created message, which is sent. + * @return -@c RETURN_OK on success + * -@c MessageQueueIF::FULL if queue is full + */ + virtual ReturnValue_t sendToDefault( MessageQueueMessageIF* message ) = 0; + /** + * @brief This method is a simple setter for the default destination. + */ + virtual void setDefaultDestination(MessageQueueId_t defaultDestination) = 0; + /** + * @brief This method is a simple getter for the default destination. + */ + virtual MessageQueueId_t getDefaultDestination() const = 0; + + virtual bool isDefaultDestinationSet() const = 0; +}; + + + +#endif /* FSFW_IPC_MESSAGEQUEUEIF_H_ */ diff --git a/fsfw/ipc/MessageQueueMessage.cpp b/fsfw/ipc/MessageQueueMessage.cpp new file mode 100644 index 0000000..dcd4def --- /dev/null +++ b/fsfw/ipc/MessageQueueMessage.cpp @@ -0,0 +1,84 @@ +#include "MessageQueueMessage.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../globalfunctions/arrayprinter.h" +#include + +MessageQueueMessage::MessageQueueMessage() : + messageSize(getMinimumMessageSize()) { + memset(this->internalBuffer, 0, sizeof(this->internalBuffer)); +} + +MessageQueueMessage::MessageQueueMessage(uint8_t* data, size_t size) : + messageSize(this->HEADER_SIZE + size) { + if (size <= this->MAX_DATA_SIZE) { + memcpy(this->getData(), data, size); + this->messageSize = this->HEADER_SIZE + size; + } + else { + sif::warning << "MessageQueueMessage: Passed size larger than maximum" + "allowed size! Setting content to 0" << std::endl; + memset(this->internalBuffer, 0, sizeof(this->internalBuffer)); + this->messageSize = this->HEADER_SIZE; + } +} + +MessageQueueMessage::~MessageQueueMessage() { +} + +const uint8_t* MessageQueueMessage::getBuffer() const { + return this->internalBuffer; +} + +uint8_t* MessageQueueMessage::getBuffer() { + return this->internalBuffer; +} + +const uint8_t* MessageQueueMessage::getData() const { + return this->internalBuffer + this->HEADER_SIZE; +} + +uint8_t* MessageQueueMessage::getData() { + return this->internalBuffer + this->HEADER_SIZE; +} + +MessageQueueId_t MessageQueueMessage::getSender() const { + MessageQueueId_t temp_id; + memcpy(&temp_id, this->internalBuffer, sizeof(MessageQueueId_t)); + return temp_id; +} + +void MessageQueueMessage::setSender(MessageQueueId_t setId) { + memcpy(this->internalBuffer, &setId, sizeof(MessageQueueId_t)); +} + +void MessageQueueMessage::print(bool printWholeMessage) { + sif::debug << "MessageQueueMessage content: " << std::endl; + if(printWholeMessage) { + arrayprinter::print(getData(), getMaximumMessageSize()); + } + else { + arrayprinter::print(getData(), getMessageSize()); + } + +} + +void MessageQueueMessage::clear() { + memset(this->getBuffer(), 0, this->MAX_MESSAGE_SIZE); +} + +size_t MessageQueueMessage::getMessageSize() const { + return this->messageSize; +} + +void MessageQueueMessage::setMessageSize(size_t messageSize) { + this->messageSize = messageSize; +} + +size_t MessageQueueMessage::getMinimumMessageSize() const { + return this->MIN_MESSAGE_SIZE; +} + +size_t MessageQueueMessage::getMaximumMessageSize() const { + return this->MAX_MESSAGE_SIZE; +} + diff --git a/fsfw/ipc/MessageQueueMessage.h b/fsfw/ipc/MessageQueueMessage.h new file mode 100644 index 0000000..5234f64 --- /dev/null +++ b/fsfw/ipc/MessageQueueMessage.h @@ -0,0 +1,149 @@ +#ifndef FSFW_IPC_MESSAGEQUEUEMESSAGE_H_ +#define FSFW_IPC_MESSAGEQUEUEMESSAGE_H_ + +#include "../ipc/MessageQueueMessageIF.h" +#include + +/** + * @brief This class is the representation and data organizer + * for interprocess messages. + * @details + * To facilitate and standardize interprocess communication, this class was + * created to handle a lightweight "interprocess message protocol". + * + * It adds a header with the sender's queue id to every sent message and + * defines the maximum total message size. Specialized messages, such as + * device commanding messages, can be created by inheriting from this class + * and filling the buffer provided by getData with additional content. + * + * If larger amounts of data must be sent between processes, the data shall + * be stored in the IPC Store object and only the storage id is passed in a + * queue message.The class is used both to generate and send messages and to + * receive messages from other tasks. + * @ingroup message_queue + */ +class MessageQueueMessage: public MessageQueueMessageIF { +public: + /** + * @brief The class is initialized empty with this constructor. + * @details + * The messageSize attribute is set to the header's size and the whole + * content is set to zero. + */ + MessageQueueMessage(); + /** + * @brief With this constructor the class is initialized with + * the given content. + * @details + * If the passed message size fits into the buffer, the passed data is + * copied to the internal buffer and the messageSize information is set. + * Otherwise, messageSize is set to the header's size and the whole + * content is set to zero. + * @param data The data to be put in the message. + * @param size Size of the data to be copied. Must be smaller than + * MAX_MESSAGE_SIZE and larger than MIN_MESSAGE_SIZE. + */ + MessageQueueMessage(uint8_t* data, size_t size); + + /** + * @brief As no memory is allocated in this class, + * the destructor is empty. + */ + virtual ~MessageQueueMessage(); + + /** + * @brief The size information of each message is stored in + * this attribute. + * @details + * It is public to simplify usage and to allow for passing the size + * address as a pointer. Care must be taken when inheriting from this class, + * as every child class is responsible for managing the size information by + * itself. When using the class to receive a message, the size information + * is updated automatically. + * + * Please note that the minimum size is limited by the size of the header + * while the maximum size is limited by the maximum allowed message size. + */ + size_t messageSize; + /** + * @brief This constant defines the maximum size of the data content, + * excluding the header. + * @details + * It may be changed if necessary, but in general should be kept + * as small as possible. + */ + static const size_t MAX_DATA_SIZE = 24; + + /** + * @brief This constant defines the maximum total size in bytes + * of a sent message. + * @details + * It is the sum of the maximum data and the header size. Be aware that + * this constant is used to define the buffer sizes for every message + * queue in the system. So, a change here may have significant impact on + * the required resources. + */ + static constexpr size_t MAX_MESSAGE_SIZE = MAX_DATA_SIZE + HEADER_SIZE; + /** + * @brief Defines the minimum size of a message where only the + * header is included + */ + static constexpr size_t MIN_MESSAGE_SIZE = HEADER_SIZE; +private: + /** + * @brief This is the internal buffer that contains the + * actual message data. + */ + uint8_t internalBuffer[MAX_MESSAGE_SIZE]; +public: + /** + * @brief This method is used to get the complete data of the message. + */ + const uint8_t* getBuffer() const override; + /** + * @brief This method is used to get the complete data of the message. + */ + uint8_t* getBuffer() override; + /** + * @brief This method is used to fetch the data content of the message. + * @details + * It shall be used by child classes to add data at the right position. + */ + const uint8_t* getData() const override; + /** + * @brief This method is used to fetch the data content of the message. + * @details + * It shall be used by child classes to add data at the right position. + */ + uint8_t* getData() override; + /** + * @brief This method is used to extract the sender's message + * queue id information from a received message. + */ + MessageQueueId_t getSender() const override; + /** + * @brief With this method, the whole content + * and the message size is set to zero. + */ + void clear() override; + + /** + * @brief This method is used to set the sender's message queue id + * information prior to ing the message. + * @param setId + * The message queue id that identifies the sending message queue. + */ + void setSender(MessageQueueId_t setId) override; + + virtual size_t getMessageSize() const override; + virtual void setMessageSize(size_t messageSize) override; + virtual size_t getMinimumMessageSize() const override; + virtual size_t getMaximumMessageSize() const override; + + /** + * @brief This is a debug method that prints the content. + */ + void print(bool printWholeMessage); +}; + +#endif /* FSFW_IPC_MESSAGEQUEUEMESSAGE_H_ */ diff --git a/fsfw/ipc/MessageQueueMessageIF.h b/fsfw/ipc/MessageQueueMessageIF.h new file mode 100644 index 0000000..b5a30c0 --- /dev/null +++ b/fsfw/ipc/MessageQueueMessageIF.h @@ -0,0 +1,80 @@ +#ifndef FRAMEWORK_IPC_MESSAGEQUEUEMESSAGEIF_H_ +#define FRAMEWORK_IPC_MESSAGEQUEUEMESSAGEIF_H_ + +#include +#include +#include + +class MessageQueueMessageIF { +public: + + /** + * @brief This constants defines the size of the header, + * which is added to every message. + */ + static const size_t HEADER_SIZE = sizeof(MessageQueueId_t); + + virtual ~MessageQueueMessageIF() {}; + + /** + * @brief With this method, the whole content and the message + * size is set to zero. + * @details + * Implementations should also take care to clear data which is stored + * indirectly (e.g. storage data). + */ + virtual void clear() = 0; + + /** + * @brief Get read-only pointer to the complete data of the message. + * @return + */ + virtual const uint8_t* getBuffer() const = 0; + + /** + * @brief This method is used to get the complete data of the message. + */ + virtual uint8_t* getBuffer() = 0; + + /** + * @brief This method is used to set the sender's message queue id + * information prior to sending the message. + * @param setId + * The message queue id that identifies the sending message queue. + */ + virtual void setSender(MessageQueueId_t setId) = 0; + + /** + * @brief This method is used to extract the sender's message queue id + * information from a received message. + */ + virtual MessageQueueId_t getSender() const = 0; + + /** + * @brief This method is used to fetch the data content of the message. + * @details + * It shall be used by child classes to add data at the right position. + */ + virtual const uint8_t* getData() const = 0; + /** + * @brief This method is used to fetch the data content of the message. + * @details + * It shall be used by child classes to add data at the right position. + */ + virtual uint8_t* getData() = 0; + + /** + * Get constant message size of current message implementation. + * @return + */ + virtual size_t getMessageSize() const = 0; + + virtual void setMessageSize(size_t messageSize) = 0; + virtual size_t getMinimumMessageSize() const = 0; + virtual size_t getMaximumMessageSize() const = 0; + +}; + + + +#endif /* FRAMEWORK_IPC_MESSAGEQUEUEMESSAGEIF_H_ */ diff --git a/fsfw/ipc/MessageQueueSenderIF.h b/fsfw/ipc/MessageQueueSenderIF.h new file mode 100644 index 0000000..683c290 --- /dev/null +++ b/fsfw/ipc/MessageQueueSenderIF.h @@ -0,0 +1,26 @@ +#ifndef FSFW_IPC_MESSAGEQUEUESENDERIF_H_ +#define FSFW_IPC_MESSAGEQUEUESENDERIF_H_ + +#include "MessageQueueIF.h" +#include "MessageQueueMessageIF.h" +#include "../objectmanager/ObjectManagerIF.h" + +class MessageQueueSenderIF { +public: + + virtual ~MessageQueueSenderIF() {} + + /** + * Allows sending messages without actually "owning" a message queue. + * Not sure whether this is actually a good idea. + */ + static ReturnValue_t sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, + MessageQueueId_t sentFrom = MessageQueueIF::NO_QUEUE, + bool ignoreFault = false); +private: + MessageQueueSenderIF() {} +}; + + +#endif /* FSFW_IPC_MESSAGEQUEUESENDERIF_H_ */ diff --git a/fsfw/ipc/MutexFactory.h b/fsfw/ipc/MutexFactory.h new file mode 100644 index 0000000..f8133d8 --- /dev/null +++ b/fsfw/ipc/MutexFactory.h @@ -0,0 +1,34 @@ +#ifndef FRAMEWORK_IPC_MUTEXFACTORY_H_ +#define FRAMEWORK_IPC_MUTEXFACTORY_H_ + +#include "MutexIF.h" +/** + * Creates Mutex. + * This class is a "singleton" interface, i.e. it provides an + * interface, but also is the base class for a singleton. + */ +class MutexFactory { +public: + virtual ~MutexFactory(); + /** + * Returns the single instance of MutexFactory. + * The implementation of #instance is found in its subclasses. + * Thus, we choose link-time variability of the instance. + */ + static MutexFactory* instance(); + + MutexIF* createMutex(); + + void deleteMutex(MutexIF* mutex); + +private: + /** + * External instantiation is not allowed. + */ + MutexFactory(); + static MutexFactory* factoryInstance; +}; + + + +#endif /* FRAMEWORK_IPC_MUTEXFACTORY_H_ */ diff --git a/fsfw/ipc/MutexHelper.h b/fsfw/ipc/MutexHelper.h new file mode 100644 index 0000000..3dc3692 --- /dev/null +++ b/fsfw/ipc/MutexHelper.h @@ -0,0 +1,30 @@ +#ifndef FRAMEWORK_IPC_MUTEXHELPER_H_ +#define FRAMEWORK_IPC_MUTEXHELPER_H_ + +#include "MutexFactory.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +class MutexHelper { +public: + MutexHelper(MutexIF* mutex, MutexIF::TimeoutType timeoutType = + MutexIF::TimeoutType::BLOCKING, uint32_t timeoutMs = 0) : + internalMutex(mutex) { + ReturnValue_t status = mutex->lockMutex(timeoutType, + timeoutMs); + if(status == MutexIF::MUTEX_TIMEOUT) { + sif::error << "MutexHelper: Lock of mutex failed with timeout of " + << timeoutMs << " milliseconds!" << std::endl; + } + else if(status != HasReturnvaluesIF::RETURN_OK){ + sif::error << "MutexHelper: Lock of Mutex failed with code " << + status << std::endl; + } + } + + ~MutexHelper() { + internalMutex->unlockMutex(); + } +private: + MutexIF* internalMutex; +}; +#endif /* FRAMEWORK_IPC_MUTEXHELPER_H_ */ diff --git a/fsfw/ipc/MutexIF.h b/fsfw/ipc/MutexIF.h new file mode 100644 index 0000000..5673e4d --- /dev/null +++ b/fsfw/ipc/MutexIF.h @@ -0,0 +1,89 @@ +#ifndef FRAMEWORK_IPC_MUTEXIF_H_ +#define FRAMEWORK_IPC_MUTEXIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" + +/** + * @brief Common interface for OS Mutex objects which provide MUTual EXclusion. + * @details https://en.wikipedia.org/wiki/Lock_(computer_science) + * @ingroup osal + * @ingroup interface + */ +class MutexIF { +public: + /** + * Different types of timeout for the mutex lock. + */ + enum TimeoutType { + POLLING, //!< If mutex is not available, return immediately + WAITING, //!< Wait a specified time for the mutex to become available + BLOCKING //!< Block indefinitely until the mutex becomes available. + }; + + /** + * Lock the mutex. The timeout value will only be used for + * TimeoutType::WAITING + * @param timeoutType + * @param timeoutMs + * @return + */ + virtual ReturnValue_t lockMutex(TimeoutType timeoutType = + TimeoutType::BLOCKING, uint32_t timeoutMs = 0) = 0; + virtual ReturnValue_t unlockMutex() = 0; + + static const uint8_t INTERFACE_ID = CLASS_ID::MUTEX_IF; + /** + * The system lacked the necessary resources (other than memory) to initialize another mutex. + */ + static const ReturnValue_t NOT_ENOUGH_RESOURCES = MAKE_RETURN_CODE(1); + /** + * Insufficient memory to create or init Mutex + */ + static const ReturnValue_t INSUFFICIENT_MEMORY = MAKE_RETURN_CODE(2); + /** + * Caller does not have enough or right privilege + */ + static const ReturnValue_t NO_PRIVILEGE = MAKE_RETURN_CODE(3); + /** + * Wrong Attribute Setting + */ + static const ReturnValue_t WRONG_ATTRIBUTE_SETTING = MAKE_RETURN_CODE(4); + /** + * The mutex is already locked + */ + static const ReturnValue_t MUTEX_ALREADY_LOCKED = MAKE_RETURN_CODE(5); + /** + * Mutex object not found + */ + static const ReturnValue_t MUTEX_NOT_FOUND = MAKE_RETURN_CODE(6); + /** + * Mutex could not be locked because max amount of recursive locks + */ + static const ReturnValue_t MUTEX_MAX_LOCKS = MAKE_RETURN_CODE(7); + /** + * The current thread already owns this mutex + */ + static const ReturnValue_t CURR_THREAD_ALREADY_OWNS_MUTEX = MAKE_RETURN_CODE(8); + /** + * Current thread does not own this mutex + */ + static const ReturnValue_t CURR_THREAD_DOES_NOT_OWN_MUTEX = MAKE_RETURN_CODE(9); + /** + * The Mutex could not be blocked before timeout + */ + static const ReturnValue_t MUTEX_TIMEOUT = MAKE_RETURN_CODE(10); + /** + * Invalid Mutex ID + */ + static const ReturnValue_t MUTEX_INVALID_ID = MAKE_RETURN_CODE(11); + /** + * Mutex destroyed while waiting + */ + static const ReturnValue_t MUTEX_DESTROYED_WHILE_WAITING = MAKE_RETURN_CODE(12); + + virtual ~MutexIF() {} +}; + + + +#endif /* FRAMEWORK_IPC_MUTEXIF_H_ */ diff --git a/fsfw/ipc/QueueFactory.h b/fsfw/ipc/QueueFactory.h new file mode 100644 index 0000000..9853d25 --- /dev/null +++ b/fsfw/ipc/QueueFactory.h @@ -0,0 +1,35 @@ +#ifndef FSFW_IPC_QUEUEFACTORY_H_ +#define FSFW_IPC_QUEUEFACTORY_H_ + +#include "MessageQueueIF.h" +#include "MessageQueueMessage.h" +#include + +/** + * Creates message queues. + * This class is a "singleton" interface, i.e. it provides an + * interface, but also is the base class for a singleton. + */ +class QueueFactory { +public: + virtual ~QueueFactory(); + /** + * Returns the single instance of QueueFactory. + * The implementation of #instance is found in its subclasses. + * Thus, we choose link-time variability of the instance. + */ + static QueueFactory* instance(); + + MessageQueueIF* createMessageQueue(uint32_t messageDepth = 3, + size_t maxMessageSize = MessageQueueMessage::MAX_MESSAGE_SIZE); + + void deleteMessageQueue(MessageQueueIF* queue); +private: + /** + * External instantiation is not allowed. + */ + QueueFactory(); + static QueueFactory* factoryInstance; +}; + +#endif /* FSFW_IPC_QUEUEFACTORY_H_ */ diff --git a/fsfw/ipc/messageQueueDefinitions.h b/fsfw/ipc/messageQueueDefinitions.h new file mode 100644 index 0000000..d250da8 --- /dev/null +++ b/fsfw/ipc/messageQueueDefinitions.h @@ -0,0 +1,18 @@ +#ifndef FSFW_IPC_MESSAGEQUEUEDEFINITIONS_H_ +#define FSFW_IPC_MESSAGEQUEUEDEFINITIONS_H_ + +#include + +/* + * TODO: Actually, the definition of this ID to be a uint32_t is not ideal and + * breaks layering. However, it is difficult to keep layering, as the ID is + * stored in many places and sent around in MessageQueueMessage. + * Ideally, one would use the (current) object_id_t only, however, doing a + * lookup of queueIDs for every call does not sound ideal. + * In a first step, I'll circumvent the issue by not touching it, + * maybe in a second step. This also influences Interface design + * (getCommandQueue) and some other issues.. + */ +using MessageQueueId_t = uint32_t; + +#endif /* FSFW_IPC_MESSAGEQUEUEDEFINITIONS_H_ */ diff --git a/fsfw/logo/FSFW_Logo_V3.png b/fsfw/logo/FSFW_Logo_V3.png new file mode 100644 index 0000000000000000000000000000000000000000..2ac710dd2069446e8f10108716f4c0e97bc961b9 GIT binary patch literal 137423 zcmYIv1yI!A_ckHYDcy}UOCzN;E1^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c;`591%;pYwms+Xyj~%~j6BjGu#2 zlmE{Mw5zl?fXW_oNqd~2p&o{4eF?;-yO8L`YWT`){olKO( z(Du(=HLqLxE^(W#(5B#poD>vI=?yRswq%DfFM3(vyyMHzhDV3Wl6LxiIVVAiL<#yg zH~xlyo&Z7Ub5X?nJ*;gB;vbn7#1(`TaIlF_}>nHs$$!25d2t=A{Rcv&|TM zjN1>1PAs+>d-YU0?AqrRf^DaB{knp#hNudK8+q%a^WpZ6kTL&K_~Ko{KD;_Y&o8js zP-tla&iOD$N8rui2i#zcGvjM!hyDWxV}b&HQ>=z$?F$@I?~iHaavrcZ(MPvINkCM@bH|-=#ZSnMpiSd>bvWjs~ zfDQ*t;Oq!|Ws=)?@kE?oJ?NO*8?5@O$6d|nof4dx=DdB}rfzlUHxK}d;Opt({aCCn zE?`3Ae)@LE?W&P_&@bav@-^{@CVmFZ_kauNQS+Ych4@=te&uD(gq$S*NLFarNfeV; zV!q*7Kulxx{h{-#Fcq2J$4yCLVx>EZo$l+|Hq$c=iqA9|e^=Bp`+Wt{c9(TRqPe zNJ;)_i1z>&XN-J*?*nP24^8KU@E);4*PoA=M^W1Ez>ACL01iwmfb(v<|18g!&8h!H z#Z&Y6>5YE{aE(?CXM#zI4mT_$J61|rf+A87`5&DvZ)G{66j@Kku$k>~IP;d?Ith&9 z<<&%#!S4mJ{4<;_DV(uNs`}!5?`H(44&Gpdga}q?=+yhDTk)l93DN)g?=cE|S;$vY z-VkiaR>gMVFQB&n_)mL8&W++bN-t<(c + + + + + + + + + + + + + + + + + + + + + + + fs fw + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + fs fw + + + + + + + + + + + + + fs fw + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/fsfw/logo/FSFW_Logo_V3_bw.png b/fsfw/logo/FSFW_Logo_V3_bw.png new file mode 100644 index 0000000000000000000000000000000000000000..99b9b4f9608f4e53b86ff83747a114477004c9f1 GI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22J@#Me50FyM28~KVLxb@W6o$0)T6jshB+8~MPuue5N zIeu$u@n3k@lw~*n;l_vIO6J3bw24-_M`)?V^Rn@Z7Kc*5u_!wWr2t1;K0Vdn6pf>_ z=F##DkqwbP!Ixtk_xwzBetqF&k+r7U=@nlxAMT=%(d7okMpOM=zWv{4gXd|EfT09I z=99PRN3dMYWVYa^{c2G<=TuI3)y#uW=R4x(*1iZfqFl--YfgVkW%LDfs3e-HvkEgIj3-y2b_+h@i;F!-%&c`Rkf43*z(lqwO zSJ`{%e%hQ%G@0oVNaBA7PWynUd1UY;@}?z8|zNUVE)S5#6l&K=M^x z;{zwx^_jlM4Md9kZFcLstXJ3V$!ZS_G9JBNjhNSIR(u>@kMG4?(getSender(); + lastCommand = message->getCommand(); + if (busy) { + sif::debug << "MemHelper: Busy!" << std::endl; + } + switch (lastCommand) { + case MemoryMessage::CMD_MEMORY_DUMP: + handleMemoryCheckOrDump(message); + return RETURN_OK; + case MemoryMessage::CMD_MEMORY_LOAD: + handleMemoryLoad(message); + return RETURN_OK; + case MemoryMessage::CMD_MEMORY_CHECK: + handleMemoryCheckOrDump(message); + return RETURN_OK; + default: + lastCommand = CommandMessage::CMD_NONE; + return UNKNOWN_CMD; + } +} + +void MemoryHelper::completeLoad(ReturnValue_t errorCode, + const uint8_t* dataToCopy, const size_t size, uint8_t* copyHere) { + busy = false; + switch (errorCode) { + case HasMemoryIF::DO_IT_MYSELF: + busy = true; + return; + case HasMemoryIF::POINTS_TO_MEMORY: + memcpy(copyHere, dataToCopy, size); + break; + case HasMemoryIF::POINTS_TO_VARIABLE: + EndianConverter::convertBigEndian(copyHere, dataToCopy, size); + break; + case HasMemoryIF::ACTIVITY_COMPLETED: + case RETURN_OK: + break; + default: + ipcStore->deleteData(ipcAddress); + CommandMessage reply; + MemoryMessage::setMemoryReplyFailed(&reply, errorCode, + MemoryMessage::CMD_MEMORY_LOAD); + queueToUse->sendMessage(lastSender, &reply); + return; + } + //Only reached on success + CommandMessage reply( CommandMessage::REPLY_COMMAND_OK, 0, 0); + queueToUse->sendMessage(lastSender, &reply); + ipcStore->deleteData(ipcAddress); +} + +void MemoryHelper::completeDump(ReturnValue_t errorCode, + const uint8_t* dataToCopy, const size_t size) { + busy = false; + CommandMessage reply; + MemoryMessage::setMemoryReplyFailed(&reply, errorCode, lastCommand); + switch (errorCode) { + case HasMemoryIF::DO_IT_MYSELF: + busy = true; + return; + case HasReturnvaluesIF::RETURN_OK: + case HasMemoryIF::POINTS_TO_MEMORY: + case HasMemoryIF::POINTS_TO_VARIABLE: + //"data" must be valid pointer! + if (errorCode == HasMemoryIF::POINTS_TO_VARIABLE) { + EndianConverter::convertBigEndian(reservedSpaceInIPC, dataToCopy, size); + } else { + memcpy(reservedSpaceInIPC, dataToCopy, size); + } + /* NO BREAK falls through*/ + case HasMemoryIF::ACTIVITY_COMPLETED: + switch (lastCommand) { + case MemoryMessage::CMD_MEMORY_DUMP: { + MemoryMessage::setMemoryDumpReply(&reply, ipcAddress); + break; + } + case MemoryMessage::CMD_MEMORY_CHECK: { + uint16_t crc = CRC::crc16ccitt(reservedSpaceInIPC, size); + //Delete data immediately, was temporary. + ipcStore->deleteData(ipcAddress); + MemoryMessage::setMemoryCheckReply(&reply, crc); + break; + } + default: + //This should never happen! + //Is it ok to send message? Otherwise: return; + ipcStore->deleteData(ipcAddress); + reply.setParameter(STATE_MISMATCH); + break; + } + break; + case HasMemoryIF::DUMP_NOT_SUPPORTED: + if (lastCommand == MemoryMessage::CMD_MEMORY_CHECK){ + MemoryMessage::setMemoryCheckReply(&reply, 0); + MemoryMessage::setCrcReturnValue(&reply,HasMemoryIF::DUMP_NOT_SUPPORTED); + } + ipcStore->deleteData(ipcAddress); + break; + default: + //Reply is already set to REJECTED. + ipcStore->deleteData(ipcAddress); + break; + } + if (queueToUse->sendMessage(lastSender, &reply) != RETURN_OK) { + reply.clear(); + } +} + +void MemoryHelper::swapMatrixCopy(uint8_t* out, const uint8_t *in, + size_t totalSize, uint8_t datatypeSize) { + if (totalSize % datatypeSize != 0){ + return; + } + + while (totalSize > 0){ + EndianConverter::convertBigEndian(out,in,datatypeSize); + out += datatypeSize; + in += datatypeSize; + totalSize -= datatypeSize; + } +} + +MemoryHelper::~MemoryHelper() { + //Nothing to destroy +} + +void MemoryHelper::handleMemoryLoad(CommandMessage* message) { + uint32_t address = MemoryMessage::getAddress(message); + ipcAddress = MemoryMessage::getStoreID(message); + const uint8_t* p_data = NULL; + uint8_t* dataPointer = NULL; + size_t size = 0; + ReturnValue_t returnCode = ipcStore->getData(ipcAddress, &p_data, &size); + if (returnCode == RETURN_OK) { + returnCode = workOnThis->handleMemoryLoad(address, p_data, size, + &dataPointer); + completeLoad(returnCode, p_data, size, dataPointer); + } else { + //At least inform sender. + CommandMessage reply; + MemoryMessage::setMemoryReplyFailed(&reply, returnCode, + MemoryMessage::CMD_MEMORY_LOAD); + queueToUse->sendMessage(lastSender, &reply); + } +} + +void MemoryHelper::handleMemoryCheckOrDump(CommandMessage* message) { + uint32_t address = MemoryMessage::getAddress(message); + uint32_t size = MemoryMessage::getLength(message); + uint8_t* dataPointer = NULL; + ReturnValue_t returnCode = ipcStore->getFreeElement(&ipcAddress, size, + &reservedSpaceInIPC); + if (returnCode == RETURN_OK) { + returnCode = workOnThis->handleMemoryDump(address, size, &dataPointer, + reservedSpaceInIPC); + completeDump(returnCode, dataPointer, size); + } else { + CommandMessage reply; + MemoryMessage::setMemoryReplyFailed(&reply, returnCode, lastCommand); + queueToUse->sendMessage(lastSender, &reply); + } +} + +ReturnValue_t MemoryHelper::initialize(MessageQueueIF* queueToUse_) { + if(queueToUse_ == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + this->queueToUse = queueToUse_; + return initialize(); +} + +ReturnValue_t MemoryHelper::initialize() { + ipcStore = objectManager->get(objects::IPC_STORE); + if (ipcStore != nullptr) { + return RETURN_OK; + } else { + return RETURN_FAILED; + } +} diff --git a/fsfw/memory/MemoryHelper.h b/fsfw/memory/MemoryHelper.h new file mode 100644 index 0000000..a651861 --- /dev/null +++ b/fsfw/memory/MemoryHelper.h @@ -0,0 +1,49 @@ +#ifndef FSFW_MEMORY_MEMORYHELPER_H_ +#define FSFW_MEMORY_MEMORYHELPER_H_ + +#include "AcceptsMemoryMessagesIF.h" + +#include "../ipc/CommandMessage.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../ipc/MessageQueueIF.h" + +/** + * @brief TODO: documentation. + */ +class MemoryHelper : public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::MEMORY_HELPER; + static const ReturnValue_t UNKNOWN_CMD = MAKE_RETURN_CODE(0xE0); + static const ReturnValue_t INVALID_ADDRESS = MAKE_RETURN_CODE(0xE1); + static const ReturnValue_t INVALID_SIZE = MAKE_RETURN_CODE(0xE2); + static const ReturnValue_t STATE_MISMATCH = MAKE_RETURN_CODE(0xE3); + + MemoryHelper(HasMemoryIF* workOnThis, MessageQueueIF* useThisQueue); + ~MemoryHelper(); + + ReturnValue_t handleMemoryCommand(CommandMessage* message); + void completeLoad(ReturnValue_t errorCode, + const uint8_t* dataToCopy = nullptr, const size_t size = 0, + uint8_t* copyHere = nullptr); + void completeDump(ReturnValue_t errorCode, + const uint8_t* dataToCopy = nullptr, const size_t size = 0); + void swapMatrixCopy(uint8_t *out, const uint8_t *in, size_t totalSize, + uint8_t datatypeSize); + ReturnValue_t initialize(MessageQueueIF* queueToUse_); + +private: + HasMemoryIF* workOnThis; + MessageQueueIF* queueToUse; + StorageManagerIF* ipcStore = nullptr; + store_address_t ipcAddress; + Command_t lastCommand; + MessageQueueId_t lastSender = MessageQueueIF::NO_QUEUE; + uint8_t* reservedSpaceInIPC = nullptr; + bool busy; + void handleMemoryLoad(CommandMessage* message); + void handleMemoryCheckOrDump(CommandMessage* message); + ReturnValue_t initialize(); + +}; +#endif /* FSFW_MEMORY_MEMORYHELPER_H_ */ diff --git a/fsfw/memory/MemoryMessage.cpp b/fsfw/memory/MemoryMessage.cpp new file mode 100644 index 0000000..94fa469 --- /dev/null +++ b/fsfw/memory/MemoryMessage.cpp @@ -0,0 +1,107 @@ +#include "MemoryMessage.h" + +#include "../objectmanager/ObjectManagerIF.h" + +uint32_t MemoryMessage::getAddress(const CommandMessage* message) { + return message->getParameter(); +} + +store_address_t MemoryMessage::getStoreID(const CommandMessage* message) { + store_address_t temp; + temp.raw = message->getParameter2(); + return temp; +} + +uint32_t MemoryMessage::getLength(const CommandMessage* message) { + return message->getParameter2(); +} + +void MemoryMessage::setMemoryDumpCommand(CommandMessage* message, + uint32_t address, uint32_t length) { + message->setCommand(CMD_MEMORY_DUMP); + message->setParameter( address ); + message->setParameter2( length ); +} + +void MemoryMessage::setMemoryDumpReply(CommandMessage* message, + store_address_t storageID) { + message->setCommand(REPLY_MEMORY_DUMP); + message->setParameter2( storageID.raw ); +} + +void MemoryMessage::setMemoryLoadCommand(CommandMessage* message, + uint32_t address, store_address_t storageID) { + message->setCommand(CMD_MEMORY_LOAD); + message->setParameter( address ); + message->setParameter2( storageID.raw ); +} + +ReturnValue_t MemoryMessage::getErrorCode(const CommandMessage* message) { + return message->getParameter(); +} + +void MemoryMessage::clear(CommandMessage* message) { + switch (message->getCommand()) { + case CMD_MEMORY_LOAD: + case REPLY_MEMORY_DUMP: { + StorageManagerIF *ipcStore = objectManager->get( + objects::IPC_STORE); + if (ipcStore != NULL) { + ipcStore->deleteData(getStoreID(message)); + } + } + /* NO BREAK falls through*/ + case CMD_MEMORY_DUMP: + case CMD_MEMORY_CHECK: + case REPLY_MEMORY_CHECK: + case END_OF_MEMORY_COPY: + message->setCommand(CommandMessage::CMD_NONE); + message->setParameter(0); + message->setParameter2(0); + break; + } +} + +void MemoryMessage::setMemoryCheckCommand(CommandMessage* message, + uint32_t address, uint32_t length) { + message->setCommand(CMD_MEMORY_CHECK); + message->setParameter( address ); + message->setParameter2( length ); +} + +void MemoryMessage::setMemoryCheckReply(CommandMessage* message, + uint16_t crc) { + message->setCommand(REPLY_MEMORY_CHECK); + message->setParameter( crc ); +} + +void MemoryMessage::setCrcReturnValue(CommandMessage* message, + ReturnValue_t returnValue){ + message->setParameter(returnValue<<16); +}; + +uint16_t MemoryMessage::getCrc(const CommandMessage* message) { + return (uint16_t)(message->getParameter()); +} + +ReturnValue_t MemoryMessage::getCrcReturnValue(const CommandMessage* message){ + return (message->getParameter()>>16); +} + +Command_t MemoryMessage::getInitialCommand(const CommandMessage* message) { + return message->getParameter2(); +} + +void MemoryMessage::setMemoryReplyFailed(CommandMessage* message, + ReturnValue_t errorCode, Command_t initialCommand) { + message->setCommand(REPLY_MEMORY_FAILED); + message->setParameter(errorCode); + message->setParameter2(initialCommand); +} + +void MemoryMessage::setMemoryCopyEnd(CommandMessage* message) { + message->setCommand(END_OF_MEMORY_COPY); + message->setParameter(0); + message->setParameter2(0); +} + diff --git a/fsfw/memory/MemoryMessage.h b/fsfw/memory/MemoryMessage.h new file mode 100644 index 0000000..05b9926 --- /dev/null +++ b/fsfw/memory/MemoryMessage.h @@ -0,0 +1,48 @@ +#ifndef FSFW_MEMORY_MEMORYMESSAGE_H_ +#define FSFW_MEMORY_MEMORYMESSAGE_H_ + +#include "../ipc/CommandMessage.h" +#include "../storagemanager/StorageManagerIF.h" + + +class MemoryMessage { +public: + // Instantiation forbidden. + MemoryMessage() = delete; + + static const uint8_t MESSAGE_ID = messagetypes::MEMORY; + static const Command_t CMD_MEMORY_LOAD = MAKE_COMMAND_ID( 0x01 ); + static const Command_t CMD_MEMORY_DUMP = MAKE_COMMAND_ID( 0x02 ); + static const Command_t CMD_MEMORY_CHECK = MAKE_COMMAND_ID( 0x03 ); + static const Command_t REPLY_MEMORY_DUMP = MAKE_COMMAND_ID( 0x10 ); + static const Command_t REPLY_MEMORY_CHECK = MAKE_COMMAND_ID( 0x30 ); + static const Command_t REPLY_MEMORY_FAILED = MAKE_COMMAND_ID( 0xE0 ); + static const Command_t END_OF_MEMORY_COPY = MAKE_COMMAND_ID(0xF0); + + static uint32_t getAddress( const CommandMessage* message ); + static store_address_t getStoreID(const CommandMessage* message); + static uint32_t getLength( const CommandMessage* message ); + static ReturnValue_t getErrorCode(const CommandMessage* message); + static uint16_t getCrc(const CommandMessage* message ); + static ReturnValue_t getCrcReturnValue(const CommandMessage* message); + static Command_t getInitialCommand(const CommandMessage* message); + + static void setMemoryDumpCommand(CommandMessage* message, + uint32_t address, uint32_t length ); + static void setMemoryDumpReply(CommandMessage* message, + store_address_t storageID); + static void setMemoryLoadCommand(CommandMessage* message, + uint32_t address, store_address_t storageID ); + static void setMemoryCheckCommand(CommandMessage* message, + uint32_t address, uint32_t length); + static void setMemoryCheckReply(CommandMessage* message, + uint16_t crc); + static void setMemoryReplyFailed(CommandMessage* message, + ReturnValue_t errorCode, Command_t initialCommand); + static void setMemoryCopyEnd(CommandMessage* message); + static void setCrcReturnValue(CommandMessage*, ReturnValue_t returnValue); + + static void clear(CommandMessage* message); +}; + +#endif /* FSFW_MEMORY_MEMORYMESSAGE_H_ */ diff --git a/fsfw/modes/HasModesIF.h b/fsfw/modes/HasModesIF.h new file mode 100644 index 0000000..34a1593 --- /dev/null +++ b/fsfw/modes/HasModesIF.h @@ -0,0 +1,51 @@ +#ifndef FSFW_MODES_HASMODESIF_H_ +#define FSFW_MODES_HASMODESIF_H_ + +#include "ModeHelper.h" +#include "ModeMessage.h" +#include "../events/Event.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include + + +class HasModesIF { + friend class ModeHelper; +public: + static const uint8_t INTERFACE_ID = CLASS_ID::HAS_MODES_IF; + static const ReturnValue_t INVALID_MODE = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t TRANS_NOT_ALLOWED = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t IN_TRANSITION = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t INVALID_SUBMODE = MAKE_RETURN_CODE(0x04); + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::SYSTEM_MANAGER; + static const Event CHANGING_MODE = MAKE_EVENT(0, SEVERITY::INFO); //!< An object announces changing the mode. p1: target mode. p2: target submode + static const Event MODE_INFO = MAKE_EVENT(1, SEVERITY::INFO); //!< An Object announces its mode; parameter1 is mode, parameter2 is submode + static const Event FALLBACK_FAILED = MAKE_EVENT(2, SEVERITY::HIGH); + static const Event MODE_TRANSITION_FAILED = MAKE_EVENT(3, SEVERITY::LOW); + static const Event CANT_KEEP_MODE = MAKE_EVENT(4, SEVERITY::HIGH); + static const Event OBJECT_IN_INVALID_MODE = MAKE_EVENT(5, SEVERITY::LOW); //!< Indicates a bug or configuration failure: Object is in a mode it should never be in. + static const Event FORCING_MODE = MAKE_EVENT(6, SEVERITY::MEDIUM); //!< The mode is changed, but for some reason, the change is forced, i.e. EXTERNAL_CONTROL ignored. p1: target mode. p2: target submode + static const Event MODE_CMD_REJECTED = MAKE_EVENT(7, SEVERITY::LOW); //!< A mode command was rejected by the called object. Par1: called object id, Par2: return code. + + static const Mode_t MODE_ON = 1; //!< The device is powered and ready to perform operations. In this mode, no commands are sent by the device handler itself, but direct commands van be commanded and will be interpreted + static const Mode_t MODE_OFF = 0; //!< The device is powered off. The only command accepted in this mode is a mode change to on. + static const Submode_t SUBMODE_NONE = 0; //!< To avoid checks against magic number "0". + + virtual ~HasModesIF() {} + virtual MessageQueueId_t getCommandQueue() const = 0; + virtual void getMode(Mode_t *mode, Submode_t *submode) = 0; + +protected: + virtual ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, + uint32_t *msToReachTheMode) { + return HasReturnvaluesIF::RETURN_FAILED; + } + + virtual void startTransition(Mode_t mode, Submode_t submode) {} + + virtual void setToExternalControl() {} + + virtual void announceMode(bool recursive) {} +}; + +#endif /*FSFW_MODES_HASMODESIF_H_ */ diff --git a/fsfw/modes/ModeHelper.cpp b/fsfw/modes/ModeHelper.cpp new file mode 100644 index 0000000..6be4f77 --- /dev/null +++ b/fsfw/modes/ModeHelper.cpp @@ -0,0 +1,137 @@ +#include "HasModesIF.h" +#include "ModeHelper.h" + +#include "../ipc/MessageQueueSenderIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +ModeHelper::ModeHelper(HasModesIF *owner) : + commandedMode(HasModesIF::MODE_OFF), + commandedSubmode(HasModesIF::SUBMODE_NONE), + owner(owner), forced(false) {} + +ModeHelper::~ModeHelper() { + +} + +ReturnValue_t ModeHelper::handleModeCommand(CommandMessage* command) { + CommandMessage reply; + Mode_t mode; + Submode_t submode; + switch (command->getCommand()) { + case ModeMessage::CMD_MODE_COMMAND_FORCED: + forced = true; + /* NO BREAK falls through*/ + case ModeMessage::CMD_MODE_COMMAND: { + mode = ModeMessage::getMode(command); + submode = ModeMessage::getSubmode(command); + uint32_t timeout; + ReturnValue_t result = owner->checkModeCommand(mode, submode, &timeout); + if (result != HasReturnvaluesIF::RETURN_OK) { + ModeMessage::setCantReachMode(&reply, result); + MessageQueueSenderIF::sendMessage(command->getSender(), &reply, + owner->getCommandQueue()); + break; + } + //Free to start transition + theOneWhoCommandedAMode = command->getSender(); + commandedMode = mode; + commandedSubmode = submode; + + if ((parentQueueId != MessageQueueIF::NO_QUEUE) + && (theOneWhoCommandedAMode != parentQueueId)) { + owner->setToExternalControl(); + } + + countdown.setTimeout(timeout); + owner->startTransition(mode, submode); + } + break; + case ModeMessage::CMD_MODE_READ: { + owner->getMode(&mode, &submode); + ModeMessage::setModeMessage(&reply, ModeMessage::REPLY_MODE_REPLY, mode, + submode); + MessageQueueSenderIF::sendMessage(command->getSender(), &reply, + owner->getCommandQueue()); + } + break; + case ModeMessage::CMD_MODE_ANNOUNCE: + owner->announceMode(false); + break; + case ModeMessage::CMD_MODE_ANNOUNCE_RECURSIVELY: + owner->announceMode(true); + break; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t ModeHelper::initialize(MessageQueueId_t parentQueueId) { + setParentQueue(parentQueueId); + return initialize(); +} + +void ModeHelper::modeChanged(Mode_t ownerMode, Submode_t ownerSubmode) { + forced = false; + sendModeReplyMessage(ownerMode, ownerSubmode); + sendModeInfoMessage(ownerMode, ownerSubmode); + theOneWhoCommandedAMode = MessageQueueIF::NO_QUEUE; +} + +void ModeHelper::sendModeReplyMessage(Mode_t ownerMode, + Submode_t ownerSubmode) { + CommandMessage reply; + if (theOneWhoCommandedAMode != MessageQueueIF::NO_QUEUE) + { + if (ownerMode != commandedMode or ownerSubmode != commandedSubmode) + { + ModeMessage::setModeMessage(&reply, + ModeMessage::REPLY_WRONG_MODE_REPLY, ownerMode, + ownerSubmode); + } + else + { + ModeMessage::setModeMessage(&reply, ModeMessage::REPLY_MODE_REPLY, + ownerMode, ownerSubmode); + } + MessageQueueSenderIF::sendMessage(theOneWhoCommandedAMode, &reply, + owner->getCommandQueue()); + } +} + +void ModeHelper::sendModeInfoMessage(Mode_t ownerMode, + Submode_t ownerSubmode) { + CommandMessage reply; + if (theOneWhoCommandedAMode != parentQueueId + and parentQueueId != MessageQueueIF::NO_QUEUE) + { + ModeMessage::setModeMessage(&reply, ModeMessage::REPLY_MODE_INFO, + ownerMode, ownerSubmode); + MessageQueueSenderIF::sendMessage(parentQueueId, &reply, + owner->getCommandQueue()); + } +} + +void ModeHelper::startTimer(uint32_t timeoutMs) { + countdown.setTimeout(timeoutMs); +} + +void ModeHelper::setParentQueue(MessageQueueId_t parentQueueId) { + this->parentQueueId = parentQueueId; +} + +ReturnValue_t ModeHelper::initialize(void) { + return HasReturnvaluesIF::RETURN_OK; +} + +bool ModeHelper::isTimedOut() { + return countdown.hasTimedOut(); +} + +bool ModeHelper::isForced() { + return forced; +} + +void ModeHelper::setForced(bool forced) { + this->forced = forced; +} diff --git a/fsfw/modes/ModeHelper.h b/fsfw/modes/ModeHelper.h new file mode 100644 index 0000000..c2f089e --- /dev/null +++ b/fsfw/modes/ModeHelper.h @@ -0,0 +1,53 @@ +#ifndef FSFW_MODES_MODEHELPER_H_ +#define FSFW_MODES_MODEHELPER_H_ + +#include "ModeMessage.h" +#include "../ipc/MessageQueueIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../timemanager/Countdown.h" + +class HasModesIF; + +class ModeHelper { +public: + MessageQueueId_t theOneWhoCommandedAMode = MessageQueueIF::NO_QUEUE; + Mode_t commandedMode; + Submode_t commandedSubmode; + + ModeHelper(HasModesIF *owner); + virtual ~ModeHelper(); + + ReturnValue_t handleModeCommand(CommandMessage *message); + + /** + * @param parentQueue the Queue id of the parent object. + * Set to 0 if no parent present + */ + void setParentQueue(MessageQueueId_t parentQueueId); + + ReturnValue_t initialize(MessageQueueId_t parentQueueId); + + ReturnValue_t initialize(void); + + void modeChanged(Mode_t mode, Submode_t submode); + + void startTimer(uint32_t timeoutMs); + + bool isTimedOut(); + + bool isForced(); + + void setForced(bool forced); +protected: + HasModesIF *owner; + MessageQueueId_t parentQueueId = MessageQueueIF::NO_QUEUE; + + Countdown countdown; + + bool forced; +private: + void sendModeReplyMessage(Mode_t ownerMode, Submode_t ownerSubmode); + void sendModeInfoMessage(Mode_t ownerMode, Submode_t ownerSubmode); +}; + +#endif /* FSFW_MODES_MODEHELPER_H_ */ diff --git a/fsfw/modes/ModeMessage.cpp b/fsfw/modes/ModeMessage.cpp new file mode 100644 index 0000000..b33bba6 --- /dev/null +++ b/fsfw/modes/ModeMessage.cpp @@ -0,0 +1,31 @@ +#include "ModeMessage.h" + +Mode_t ModeMessage::getMode(const CommandMessage* message) { + return message->getParameter(); +} + +Submode_t ModeMessage::getSubmode(const CommandMessage* message) { + return message->getParameter2(); +} + +void ModeMessage::setModeMessage(CommandMessage* message, + Command_t command, Mode_t mode, Submode_t submode) { + message->setCommand( command ); + message->setParameter( mode ); + message->setParameter2( submode ); +} + +ReturnValue_t ModeMessage::getCantReachModeReason(const CommandMessage* message) { + return message->getParameter(); +} + +void ModeMessage::clear(CommandMessage* message) { + message->setCommand(CommandMessage::CMD_NONE); +} + +void ModeMessage::setCantReachMode(CommandMessage* message, + ReturnValue_t reason) { + message->setCommand(REPLY_CANT_REACH_MODE); + message->setParameter(reason); + message->setParameter2(0); +} diff --git a/fsfw/modes/ModeMessage.h b/fsfw/modes/ModeMessage.h new file mode 100644 index 0000000..856996c --- /dev/null +++ b/fsfw/modes/ModeMessage.h @@ -0,0 +1,34 @@ +#ifndef FSFW_MODES_MODEMESSAGE_H_ +#define FSFW_MODES_MODEMESSAGE_H_ + +#include "../ipc/CommandMessage.h" + +typedef uint32_t Mode_t; +typedef uint8_t Submode_t; + +class ModeMessage { +private: + ModeMessage(); +public: + static const uint8_t MESSAGE_ID = messagetypes::MODE_COMMAND; + static const Command_t CMD_MODE_COMMAND = MAKE_COMMAND_ID(0x01);//!> Command to set the specified Mode, replies are: REPLY_MODE_REPLY, REPLY_WRONG_MODE_REPLY, and REPLY_REJECTED; don't add any replies, as this will break the subsystem mode machine!! + static const Command_t CMD_MODE_COMMAND_FORCED = MAKE_COMMAND_ID(0xF1);//!> Command to set the specified Mode, regardless of external control flag, replies are: REPLY_MODE_REPLY, REPLY_WRONG_MODE_REPLY, and REPLY_REJECTED; don't add any replies, as this will break the subsystem mode machine!! + static const Command_t REPLY_MODE_REPLY = MAKE_COMMAND_ID(0x02);//!> Reply to a CMD_MODE_COMMAND or CMD_MODE_READ + static const Command_t REPLY_MODE_INFO = MAKE_COMMAND_ID(0x03); //!> Unrequested info about the current mode (used for composites to inform their container of a changed mode) + static const Command_t REPLY_CANT_REACH_MODE = MAKE_COMMAND_ID(0x04); //!> Reply in case a mode command can't be executed. Par1: returnCode, Par2: 0 + static const Command_t REPLY_WRONG_MODE_REPLY = MAKE_COMMAND_ID(0x05);//!> Reply to a CMD_MODE_COMMAND, indicating that a mode was commanded and a transition started but was aborted; the parameters contain the mode that was reached + static const Command_t CMD_MODE_READ = MAKE_COMMAND_ID(0x06);//!> Command to read the current mode and reply with a REPLY_MODE_REPLY + static const Command_t CMD_MODE_ANNOUNCE = MAKE_COMMAND_ID(0x07);//!> Command to trigger an ModeInfo Event. This command does NOT have a reply. + static const Command_t CMD_MODE_ANNOUNCE_RECURSIVELY = MAKE_COMMAND_ID(0x08);//!> Command to trigger an ModeInfo Event and to send this command to every child. This command does NOT have a reply. + + static Mode_t getMode(const CommandMessage* message); + static Submode_t getSubmode(const CommandMessage* message); + static ReturnValue_t getCantReachModeReason(const CommandMessage* message); + + static void setModeMessage(CommandMessage* message, + Command_t command, Mode_t mode, Submode_t submode); + static void setCantReachMode(CommandMessage* message, ReturnValue_t reason); + static void clear(CommandMessage* message); +}; + +#endif /* FSFW_MODES_MODEMESSAGE_H_ */ diff --git a/fsfw/monitoring/AbsLimitMonitor.h b/fsfw/monitoring/AbsLimitMonitor.h new file mode 100644 index 0000000..2e60f6f --- /dev/null +++ b/fsfw/monitoring/AbsLimitMonitor.h @@ -0,0 +1,73 @@ +#ifndef FRAMEWORK_MONITORING_ABSLIMITMONITOR_H_ +#define FRAMEWORK_MONITORING_ABSLIMITMONITOR_H_ + +#include "MonitorBase.h" +#include + +template +class AbsLimitMonitor: public MonitorBase { +public: + AbsLimitMonitor(object_id_t reporterId, uint8_t monitorId, uint32_t parameterId, + uint16_t confirmationLimit, T limit, Event violationEvent = MonitoringIF::VALUE_OUT_OF_RANGE, bool aboveIsViolation = true) : + MonitorBase(reporterId, monitorId, parameterId, confirmationLimit), limit(limit), violationEvent(violationEvent), aboveIsViolation(aboveIsViolation) { + } + virtual ~AbsLimitMonitor() { + } + virtual ReturnValue_t checkSample(T sample, T* crossedLimit) { + *crossedLimit = limit; + if (aboveIsViolation) { + if ((std::abs(sample) > limit)) { + return MonitoringIF::OUT_OF_RANGE; + } + } else { + if ((std::abs(sample) < limit)) { + return MonitoringIF::OUT_OF_RANGE; + } + } + return HasReturnvaluesIF::RETURN_OK; //We're not out of range. + } + + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex) { + ReturnValue_t result = this->MonitorBase::getParameter(domainId, + parameterId, parameterWrapper, newValues, startAtIndex); + //We'll reuse the DOMAIN_ID of MonitorReporter, as we know the parameterIds used there. + if (result != this->INVALID_MATRIX_ID) { + return result; + } + switch (parameterId) { + case 10: + parameterWrapper->set(this->limit); + break; + default: + return this->INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; + } + bool isOutOfLimits() { + if (this->oldState == MonitoringIF::OUT_OF_RANGE) { + return true; + } else { + return false; + } + } + void setLimit(T value) { + limit = value; + } +protected: + void sendTransitionEvent(T currentValue, ReturnValue_t state) { + switch (state) { + case MonitoringIF::OUT_OF_RANGE: + EventManagerIF::triggerEvent(this->reportingId, violationEvent, this->parameterId); + break; + default: + break; + } + } + T limit; + const Event violationEvent; + const bool aboveIsViolation; +}; + +#endif /* FRAMEWORK_MONITORING_ABSLIMITMONITOR_H_ */ diff --git a/fsfw/monitoring/HasMonitorsIF.h b/fsfw/monitoring/HasMonitorsIF.h new file mode 100644 index 0000000..85d92b6 --- /dev/null +++ b/fsfw/monitoring/HasMonitorsIF.h @@ -0,0 +1,30 @@ +/** + * @file HasMonitorsIF.h + * @brief This file defines the HasMonitorsIF class. + * @date 28.07.2014 + * @author baetz + */ +#ifndef HASMONITORSIF_H_ +#define HASMONITORSIF_H_ + +#include "../events/EventReportingProxyIF.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +class HasMonitorsIF { +public: + static const uint8_t MAX_N_PARAMETER = 10; +// static const uint8_t MAX_N_LIMIT_ID = 10; + virtual ReturnValue_t setCheckingOfParameters(uint8_t checkingStrategy, + bool forOnePid = false, uint32_t parameterId = 0) = 0; + virtual ReturnValue_t modifyParameterMonitor(uint8_t limitType, + uint32_t parameterId, const uint8_t* data, uint32_t size) = 0; + virtual ReturnValue_t modifyObjectMonitor(uint32_t objectId, + const uint8_t* data, const uint32_t size) = 0; + virtual void setAllMonitorsToUnchecked() = 0; + virtual MessageQueueId_t getCommandQueue() const = 0; + virtual ~HasMonitorsIF() { + } +}; + +#endif /* HASMONITORSIF_H_ */ diff --git a/fsfw/monitoring/LimitMonitor.h b/fsfw/monitoring/LimitMonitor.h new file mode 100644 index 0000000..66e6725 --- /dev/null +++ b/fsfw/monitoring/LimitMonitor.h @@ -0,0 +1,94 @@ +#ifndef FRAMEWORK_MONITORING_LIMITMONITOR_H_ +#define FRAMEWORK_MONITORING_LIMITMONITOR_H_ + +#include "MonitorBase.h" + +/** + * Variant of a limit checking class. + * Newer version as compared to LimitCheckMonitor. + * Functionality is more or less the same, but does not use + * heavy weight MonitoringIF. + */ +template +class LimitMonitor: public MonitorBase { +public: + LimitMonitor(object_id_t reporterId, uint8_t monitorId, uint32_t parameterId, + uint16_t confirmationLimit, T lowerLimit, T upperLimit, + Event belowLowEvent = MonitoringIF::VALUE_BELOW_LOW_LIMIT, + Event aboveHighEvent = MonitoringIF::VALUE_ABOVE_HIGH_LIMIT) : + MonitorBase(reporterId, monitorId, parameterId, confirmationLimit), lowerLimit( + lowerLimit), upperLimit(upperLimit), belowLowEvent( + belowLowEvent), aboveHighEvent(aboveHighEvent) { + } + virtual ~LimitMonitor() { + } + virtual ReturnValue_t checkSample(T sample, T* crossedLimit) { + *crossedLimit = 0.0; + if (sample > upperLimit) { + *crossedLimit = upperLimit; + return MonitoringIF::ABOVE_HIGH_LIMIT; + } else if (sample < lowerLimit) { + *crossedLimit = lowerLimit; + return MonitoringIF::BELOW_LOW_LIMIT; + } else { + return HasReturnvaluesIF::RETURN_OK; //Within limits. + } + } + + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex) { + ReturnValue_t result = this->MonitorBase::getParameter(domainId, + parameterId, parameterWrapper, newValues, startAtIndex); + //We'll reuse the DOMAIN_ID of MonitorReporter, as we know the parameterIds used there. + if (result != this->INVALID_MATRIX_ID) { + return result; + } + switch (parameterId) { + case 10: + parameterWrapper->set(this->lowerLimit); + break; + case 11: + parameterWrapper->set(this->upperLimit); + break; + default: + return this->INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; + } + bool isOutOfLimits() { + if (this->oldState == MonitoringIF::ABOVE_HIGH_LIMIT || this->oldState == MonitoringIF::BELOW_LOW_LIMIT) { + return true; + } else { + return false; + } + } + + T getLowerLimit() const { + return lowerLimit; + } + + T getUpperLimit() const { + return upperLimit; + } + +protected: + void sendTransitionEvent(T currentValue, ReturnValue_t state) { + switch (state) { + case MonitoringIF::BELOW_LOW_LIMIT: + EventManagerIF::triggerEvent(this->reportingId, belowLowEvent, this->parameterId); + break; + case MonitoringIF::ABOVE_HIGH_LIMIT: + EventManagerIF::triggerEvent(this->reportingId, aboveHighEvent, this->parameterId); + break; + default: + break; + } + } + T lowerLimit; + T upperLimit; + const Event belowLowEvent; + const Event aboveHighEvent; +}; + +#endif /* FRAMEWORK_MONITORING_LIMITMONITOR_H_ */ diff --git a/fsfw/monitoring/LimitViolationReporter.cpp b/fsfw/monitoring/LimitViolationReporter.cpp new file mode 100644 index 0000000..c531a6e --- /dev/null +++ b/fsfw/monitoring/LimitViolationReporter.cpp @@ -0,0 +1,60 @@ +/** + * @file LimitViolationReporter.cpp + * @brief This file defines the LimitViolationReporter class. + * @date 17.07.2014 + * @author baetz + */ +#include "LimitViolationReporter.h" +#include "MonitoringIF.h" +#include "ReceivesMonitoringReportsIF.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../serialize/SerializeAdapter.h" + +ReturnValue_t LimitViolationReporter::sendLimitViolationReport(const SerializeIF* data) { + ReturnValue_t result = checkClassLoaded(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + store_address_t storeId; + uint8_t* dataTarget = NULL; + size_t maxSize = data->getSerializedSize(); + if (maxSize > MonitoringIF::VIOLATION_REPORT_MAX_SIZE) { + return MonitoringIF::INVALID_SIZE; + } + result = ipcStore->getFreeElement(&storeId, maxSize, + &dataTarget); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + size_t size = 0; + result = data->serialize(&dataTarget, &size, maxSize, SerializeIF::Endianness::BIG); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + CommandMessage report; + MonitoringMessage::setLimitViolationReport(&report, storeId); + return MessageQueueSenderIF::sendMessage(reportQueue, &report); +} + +ReturnValue_t LimitViolationReporter::checkClassLoaded() { + if (reportQueue == 0) { + ReceivesMonitoringReportsIF* receiver = objectManager->get< + ReceivesMonitoringReportsIF>(reportingTarget); + if (receiver == NULL) { + return ObjectManagerIF::NOT_FOUND; + } + reportQueue = receiver->getCommandQueue(); + } + if (ipcStore == NULL) { + ipcStore = objectManager->get(objects::IPC_STORE); + if (ipcStore == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + return HasReturnvaluesIF::RETURN_OK; +} + +//Lazy initialization. +MessageQueueId_t LimitViolationReporter::reportQueue = 0; +StorageManagerIF* LimitViolationReporter::ipcStore = NULL; +object_id_t LimitViolationReporter::reportingTarget = 0; diff --git a/fsfw/monitoring/LimitViolationReporter.h b/fsfw/monitoring/LimitViolationReporter.h new file mode 100644 index 0000000..a71b972 --- /dev/null +++ b/fsfw/monitoring/LimitViolationReporter.h @@ -0,0 +1,31 @@ +/** + * @file LimitViolationReporter.h + * @brief This file defines the LimitViolationReporter class. + * @date 17.07.2014 + * @author baetz + */ +#ifndef LIMITVIOLATIONREPORTER_H_ +#define LIMITVIOLATIONREPORTER_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerializeIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +namespace Factory{ +void setStaticFrameworkObjectIds(); +} + +class LimitViolationReporter { + friend void (Factory::setStaticFrameworkObjectIds)(); +public: + static ReturnValue_t sendLimitViolationReport(const SerializeIF* data); +private: + static object_id_t reportingTarget; + static MessageQueueId_t reportQueue; + static StorageManagerIF* ipcStore; + static ReturnValue_t checkClassLoaded(); + LimitViolationReporter(); +}; + +#endif /* LIMITVIOLATIONREPORTER_H_ */ diff --git a/fsfw/monitoring/MonitorBase.h b/fsfw/monitoring/MonitorBase.h new file mode 100644 index 0000000..b2d0e6c --- /dev/null +++ b/fsfw/monitoring/MonitorBase.h @@ -0,0 +1,62 @@ +#ifndef MONITORBASE_H_ +#define MONITORBASE_H_ + +#include "../datapool/DataSet.h" +#include "../datapool/PIDReader.h" +#include "LimitViolationReporter.h" +#include "MonitoringIF.h" +#include "MonitoringMessageContent.h" +#include "MonitorReporter.h" + +/** + * Base class for monitoring of parameters. + * Can be used anywhere, specializations need to implement checkSample and should override sendTransitionEvent. + * Manages state handling, enabling and disabling of events/reports and forwarding of transition + * reports via MonitorReporter. In addition, it provides default implementations for fetching the parameter sample from + * the data pool and a simple confirmation counter. + */ +template +class MonitorBase: public MonitorReporter { +public: + MonitorBase(object_id_t reporterId, uint8_t monitorId, + uint32_t parameterId, uint16_t confirmationLimit) : + MonitorReporter(reporterId, monitorId, parameterId, confirmationLimit) { + } + virtual ~MonitorBase() { + } + virtual ReturnValue_t check() { + //1. Fetch sample of type T, return validity. + T sample = 0; + ReturnValue_t validity = fetchSample(&sample); + + //2. If returning from fetch != OK, parameter is invalid. Report (if oldState is != invalidity). + if (validity != HasReturnvaluesIF::RETURN_OK) { + this->monitorStateIs(validity, sample, 0); + //3. Otherwise, check sample. + } else { + this->oldState = doCheck(sample); + } + return this->oldState; + } + virtual ReturnValue_t doCheck(T sample) { + T crossedLimit = 0.0; + ReturnValue_t currentState = checkSample(sample, &crossedLimit); + return this->monitorStateIs(currentState,sample, crossedLimit); + } + //Abstract or default. + virtual ReturnValue_t checkSample(T sample, T* crossedLimit) = 0; + +protected: + virtual ReturnValue_t fetchSample(T* sample) { + DataSet mySet; + PIDReader parameter(this->parameterId, &mySet); + mySet.read(); + if (!parameter.isValid()) { + return MonitoringIF::INVALID; + } + *sample = parameter.value; + return HasReturnvaluesIF::RETURN_OK; + } +}; + +#endif /* MONITORBASE_H_ */ diff --git a/fsfw/monitoring/MonitorReporter.h b/fsfw/monitoring/MonitorReporter.h new file mode 100644 index 0000000..ca2b534 --- /dev/null +++ b/fsfw/monitoring/MonitorReporter.h @@ -0,0 +1,178 @@ +#ifndef FRAMEWORK_MONITORING_MONITORREPORTER_H_ +#define FRAMEWORK_MONITORING_MONITORREPORTER_H_ + +#include "../events/EventManagerIF.h" +#include "LimitViolationReporter.h" +#include "MonitoringIF.h" +#include "MonitoringMessageContent.h" +#include "../parameters/HasParametersIF.h" + +template +class MonitorReporter: public HasParametersIF { +public: + + static const uint8_t ENABLED = 1; + static const uint8_t DISABLED = 0; + + MonitorReporter(object_id_t reportingId, uint8_t monitorId, uint32_t parameterId, uint16_t confirmationLimit) : + monitorId(monitorId), parameterId(parameterId), reportingId( + reportingId), oldState(MonitoringIF::UNCHECKED), reportingEnabled( + ENABLED), eventEnabled(ENABLED), currentCounter(0), confirmationLimit( + confirmationLimit) { + } + + virtual ~MonitorReporter() { + } + + ReturnValue_t monitorStateIs(ReturnValue_t state, T parameterValue = 0, + T crossedLimit = 0) { + if (state != oldState) { + if (isConfirmed(state)) { + if (eventEnabled == ENABLED) { + sendTransitionEvent(parameterValue, state); + } + if (reportingEnabled == ENABLED) { + sendTransitionReport(parameterValue, crossedLimit, state); + } + oldState = state; + } else { + //This is to ensure confirmation works. + //Needs to be reset to be able to confirm against oldState again next time. + return oldState; + } + } else { + resetConfirmation(); + } + return state; + } + + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex) { + if (domainId != monitorId) { + return INVALID_DOMAIN_ID; + } + switch (parameterId) { + case 0: + parameterWrapper->set(this->confirmationLimit); + break; + case 1: + parameterWrapper->set(this->reportingEnabled); + break; + case 2: + parameterWrapper->set(this->eventEnabled); + break; + default: + return INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; + } + virtual ReturnValue_t setToUnchecked() { + return setToState(MonitoringIF::UNCHECKED); + } + virtual ReturnValue_t setToInvalid() { + return setToState(MonitoringIF::INVALID); + } + object_id_t getReporterId() const { + return reportingId; + } + + void setEventEnabled(uint8_t eventEnabled) { + this->eventEnabled = eventEnabled; + } + + void setReportingEnabled(uint8_t reportingEnabled) { + this->reportingEnabled = reportingEnabled; + } + + bool isEventEnabled() const { + return (eventEnabled == ENABLED); + } + +protected: + const uint8_t monitorId; + const uint32_t parameterId; + object_id_t reportingId; + ReturnValue_t oldState; + + uint8_t reportingEnabled; + + uint8_t eventEnabled; + + uint16_t currentCounter; + uint16_t confirmationLimit; + + bool isConfirmed(ReturnValue_t state) { + //Confirm INVALID and UNCHECKED immediately. + if (state == MonitoringIF::INVALID + || state == MonitoringIF::UNCHECKED) { + currentCounter = 0; + return true; + } + return doesChildConfirm(state); + } + + /** + * This is the most simple form of confirmation. + * A counter counts any violation and compares the number to maxCounter. + * @param state The state, indicating the type of violation. Not used here. + * @return true if counter > maxCounter, else false. + */ + virtual bool doesChildConfirm(ReturnValue_t state) { + currentCounter += 1; + if (currentCounter > confirmationLimit) { + currentCounter = 0; + return true; + } else { + return false; + } + } + /** + * This method needs to reset the confirmation in case a valid sample was found. + * Here, simply resets the current counter. + */ + virtual void resetConfirmation() { + currentCounter = 0; + } + /** + * Default version of sending transitional events. + * Should be overridden from specialized monitors. + * @param currentValue The current value which was monitored. + * @param state The state the monitor changed to. + */ + virtual void sendTransitionEvent(T currentValue, ReturnValue_t state) { + switch(state) { + case MonitoringIF::UNCHECKED: + case MonitoringIF::UNSELECTED: + case MonitoringIF::INVALID: + case HasReturnvaluesIF::RETURN_OK: + break; + default: + EventManagerIF::triggerEvent(reportingId, MonitoringIF::MONITOR_CHANGED_STATE, state); + break; + } + } + /** + * Default implementation for sending transition report. + * May be overridden, but is seldom necessary. + * @param parameterValue Current value of the parameter + * @param crossedLimit The limit crossed (if applicable). + * @param state Current state the monitor is in. + */ + virtual void sendTransitionReport(T parameterValue, T crossedLimit, ReturnValue_t state) { + MonitoringReportContent report(parameterId, + parameterValue, crossedLimit, oldState, state); + LimitViolationReporter::sendLimitViolationReport(&report); + } + ReturnValue_t setToState(ReturnValue_t state) { + if (oldState != state && reportingEnabled) { + MonitoringReportContent report(parameterId, 0, 0, oldState, + state); + LimitViolationReporter::sendLimitViolationReport(&report); + oldState = state; + } + return HasReturnvaluesIF::RETURN_OK; + } +}; + +#endif /* FRAMEWORK_MONITORING_MONITORREPORTER_H_ */ diff --git a/fsfw/monitoring/MonitoringIF.h b/fsfw/monitoring/MonitoringIF.h new file mode 100644 index 0000000..44218c3 --- /dev/null +++ b/fsfw/monitoring/MonitoringIF.h @@ -0,0 +1,67 @@ +#ifndef MONITORINGIF_H_ +#define MONITORINGIF_H_ + +#include "../memory/HasMemoryIF.h" +#include "MonitoringMessage.h" +#include "../serialize/SerializeIF.h" + +class MonitoringIF : public SerializeIF { +public: + static const uint8_t VIOLATION_REPORT_MAX_SIZE = 32; + static const uint8_t LIMIT_TYPE_NO_TYPE = 0xFF; + static const uint8_t LIMIT_TYPE_LIMIT_CHECK = 0; + static const uint8_t LIMIT_TYPE_DELTA_CHECK = 1; + static const uint8_t LIMIT_TYPE_ABSOLUTE_CHECK = 2; + static const uint8_t LIMIT_TYPE_OBJECT = 128; + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::FDIR_2; + static const Event MONITOR_CHANGED_STATE = MAKE_EVENT(1, SEVERITY::LOW); + static const Event VALUE_BELOW_LOW_LIMIT = MAKE_EVENT(2, SEVERITY::LOW); + static const Event VALUE_ABOVE_HIGH_LIMIT = MAKE_EVENT(3, SEVERITY::LOW); + static const Event VALUE_OUT_OF_RANGE = MAKE_EVENT(4, SEVERITY::LOW); + + static const uint8_t INTERFACE_ID = CLASS_ID::LIMITS_IF; + static const ReturnValue_t UNCHECKED = MAKE_RETURN_CODE(1); + static const ReturnValue_t INVALID = MAKE_RETURN_CODE(2); + static const ReturnValue_t UNSELECTED = MAKE_RETURN_CODE(3); + static const ReturnValue_t BELOW_LOW_LIMIT = MAKE_RETURN_CODE(4); +// static const ReturnValue_t CHECKING_STATUS_BELOW_LOW_THRESHOLD = MAKE_RETURN_CODE(4); +// static const ReturnValue_t CHECKING_STATUS_ABOVE_HIGH_THRESHOLD = MAKE_RETURN_CODE(5); + static const ReturnValue_t ABOVE_HIGH_LIMIT = MAKE_RETURN_CODE(5); + static const ReturnValue_t UNEXPECTED_VALUE = MAKE_RETURN_CODE(6); + static const ReturnValue_t OUT_OF_RANGE = MAKE_RETURN_CODE(7); + + + static const ReturnValue_t FIRST_SAMPLE = MAKE_RETURN_CODE(0xA0); + static const ReturnValue_t INVALID_SIZE = MAKE_RETURN_CODE(0xE0); + static const ReturnValue_t WRONG_TYPE = MAKE_RETURN_CODE(0xE1); + static const ReturnValue_t WRONG_PID = MAKE_RETURN_CODE(0xE2); + static const ReturnValue_t WRONG_LIMIT_ID = MAKE_RETURN_CODE(0xE3); + static const ReturnValue_t MONITOR_NOT_FOUND = MAKE_RETURN_CODE(0xEE); + + static const uint8_t REPORT_NONE = 0; + static const uint8_t REPORT_EVENTS_ONLY = 1; + static const uint8_t REPORT_REPORTS_ONLY = 2; + static const uint8_t REPORT_ALL = 3; + +// static const ReturnValue_t STILL_IN_LOW_WARNING = MAKE_RETURN_CODE(0x11); +// static const ReturnValue_t STILL_IN_LOW_LIMIT = MAKE_RETURN_CODE(0x12); +// static const ReturnValue_t STILL_IN_HIGH_WARNING = MAKE_RETURN_CODE(0x13); +// static const ReturnValue_t STILL_IN_HIGH_LIMIT = MAKE_RETURN_CODE(0x14); +// static const ReturnValue_t VARIABLE_IS_INVALID = MAKE_RETURN_CODE(0xE0); +// static const ReturnValue_t INVALID_SIZE = MAKE_RETURN_CODE(0xE1); +// static const ReturnValue_t INVALID_ID = MAKE_RETURN_CODE(0xE2); + virtual ReturnValue_t check() = 0; + virtual ReturnValue_t setLimits( uint8_t type, const uint8_t* data, uint32_t size) = 0; + virtual ReturnValue_t setChecking(uint8_t strategy) = 0; + virtual ReturnValue_t setToUnchecked() = 0; + virtual uint8_t getLimitType() const = 0; + virtual uint32_t getLimitId() const = 0; +// virtual ReturnValue_t setEventReporting(bool active) = 0; + virtual ~MonitoringIF() { + } +}; + + + +#endif /* MONITORINGIF_H_ */ diff --git a/fsfw/monitoring/MonitoringMessage.cpp b/fsfw/monitoring/MonitoringMessage.cpp new file mode 100644 index 0000000..8caa27a --- /dev/null +++ b/fsfw/monitoring/MonitoringMessage.cpp @@ -0,0 +1,38 @@ +#include "MonitoringMessage.h" +#include "../objectmanager/ObjectManagerIF.h" + +MonitoringMessage::~MonitoringMessage() { +} + +void MonitoringMessage::setLimitViolationReport(CommandMessage* message, + store_address_t storeId) { + setTypicalMessage(message, LIMIT_VIOLATION_REPORT, storeId); +} + +void MonitoringMessage::setTypicalMessage(CommandMessage* message, + Command_t type, store_address_t storeId) { + message->setCommand(type); + message->setParameter2(storeId.raw); +} + +store_address_t MonitoringMessage::getStoreId(const CommandMessage* message) { + store_address_t temp; + temp.raw = message->getParameter2(); + return temp; +} + +void MonitoringMessage::clear(CommandMessage* message) { + message->setCommand(CommandMessage::CMD_NONE); + switch (message->getCommand()) { + case MonitoringMessage::LIMIT_VIOLATION_REPORT: { + StorageManagerIF *ipcStore = objectManager->get( + objects::IPC_STORE); + if (ipcStore != NULL) { + ipcStore->deleteData(getStoreId(message)); + } + break; + } + default: + break; + } +} diff --git a/fsfw/monitoring/MonitoringMessage.h b/fsfw/monitoring/MonitoringMessage.h new file mode 100644 index 0000000..84b0275 --- /dev/null +++ b/fsfw/monitoring/MonitoringMessage.h @@ -0,0 +1,21 @@ +#ifndef MONITORINGMESSAGE_H_ +#define MONITORINGMESSAGE_H_ + +#include "../ipc/CommandMessage.h" +#include "../storagemanager/StorageManagerIF.h" + +class MonitoringMessage: public CommandMessage { +public: + static const uint8_t MESSAGE_ID = messagetypes::MONITORING; + //Object id could be useful, but we better manage that on service level (register potential reporters). + static const Command_t LIMIT_VIOLATION_REPORT = MAKE_COMMAND_ID(10); + virtual ~MonitoringMessage(); + static void setLimitViolationReport(CommandMessage* message, store_address_t storeId); + static void clear(CommandMessage* message); + static store_address_t getStoreId(const CommandMessage* message); + static void setTypicalMessage(CommandMessage* message, Command_t type, store_address_t storeId); + +}; + + +#endif /* MONITORINGMESSAGE_H_ */ diff --git a/fsfw/monitoring/MonitoringMessageContent.h b/fsfw/monitoring/MonitoringMessageContent.h new file mode 100644 index 0000000..c82506f --- /dev/null +++ b/fsfw/monitoring/MonitoringMessageContent.h @@ -0,0 +1,77 @@ +#ifndef MONITORINGMESSAGECONTENT_H_ +#define MONITORINGMESSAGECONTENT_H_ + +#include "HasMonitorsIF.h" +#include "MonitoringIF.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../serialize/SerialBufferAdapter.h" +#include "../serialize/SerialFixedArrayListAdapter.h" +#include "../serialize/SerializeElement.h" +#include "../serialize/SerialLinkedListAdapter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../timemanager/TimeStamperIF.h" + +namespace Factory{ +void setStaticFrameworkObjectIds(); +} + +//PID(uint32_t), TYPE, LIMIT_ID, value,limitValue, previous, later, timestamp +template +class MonitoringReportContent: public SerialLinkedListAdapter { + friend void (Factory::setStaticFrameworkObjectIds)(); +public: + SerializeElement monitorId; + SerializeElement parameterId; + SerializeElement parameterValue; + SerializeElement limitValue; + SerializeElement oldState; + SerializeElement newState; + uint8_t rawTimestamp[TimeStamperIF::MISSION_TIMESTAMP_SIZE]; + SerializeElement> timestampSerializer; + TimeStamperIF* timeStamper; + MonitoringReportContent() : + SerialLinkedListAdapter( + LinkedElement::Iterator(¶meterId)), monitorId(0), parameterId( + 0), parameterValue(0), limitValue(0), oldState(0), newState( + 0), rawTimestamp( { 0 }), timestampSerializer(rawTimestamp, + sizeof(rawTimestamp)), timeStamper(NULL) { + setAllNext(); + } + MonitoringReportContent(uint32_t setPID, T value, T limitValue, + ReturnValue_t oldState, ReturnValue_t newState) : + SerialLinkedListAdapter( + LinkedElement::Iterator(¶meterId)), monitorId(0), parameterId( + setPID), parameterValue(value), limitValue(limitValue), oldState( + oldState), newState(newState), timestampSerializer(rawTimestamp, + sizeof(rawTimestamp)), timeStamper(NULL) { + setAllNext(); + if (checkAndSetStamper()) { + timeStamper->addTimeStamp(rawTimestamp, sizeof(rawTimestamp)); + } + } +private: + + static object_id_t timeStamperId; + void setAllNext() { + parameterId.setNext(¶meterValue); + parameterValue.setNext(&limitValue); + limitValue.setNext(&oldState); + oldState.setNext(&newState); + newState.setNext(×tampSerializer); + } + bool checkAndSetStamper() { + if (timeStamper == NULL) { + timeStamper = objectManager->get( timeStamperId ); + if ( timeStamper == NULL ) { + sif::error << "MonitoringReportContent::checkAndSetStamper: " + "Stamper not found!" << std::endl; + return false; + } + } + return true; + } +}; +template +object_id_t MonitoringReportContent::timeStamperId = 0; + +#endif /* MONITORINGMESSAGECONTENT_H_ */ diff --git a/fsfw/monitoring/ReceivesMonitoringReportsIF.h b/fsfw/monitoring/ReceivesMonitoringReportsIF.h new file mode 100644 index 0000000..fb37c16 --- /dev/null +++ b/fsfw/monitoring/ReceivesMonitoringReportsIF.h @@ -0,0 +1,15 @@ +#ifndef RECEIVESMONITORINGREPORTSIF_H_ +#define RECEIVESMONITORINGREPORTSIF_H_ + +#include "../ipc/MessageQueueSenderIF.h" + +class ReceivesMonitoringReportsIF { +public: + virtual MessageQueueId_t getCommandQueue() const = 0; + virtual ~ReceivesMonitoringReportsIF() { + } +}; + + + +#endif /* RECEIVESMONITORINGREPORTSIF_H_ */ diff --git a/fsfw/monitoring/TriplexMonitor.h b/fsfw/monitoring/TriplexMonitor.h new file mode 100644 index 0000000..9b60aeb --- /dev/null +++ b/fsfw/monitoring/TriplexMonitor.h @@ -0,0 +1,155 @@ +#ifndef FRAMEWORK_MONITORING_TRIPLEXMONITOR_H_ +#define FRAMEWORK_MONITORING_TRIPLEXMONITOR_H_ + +#include "../datapool/DataSet.h" +#include "../datapool/PIDReaderList.h" +#include "../health/HealthTableIF.h" +#include "../parameters/HasParametersIF.h" +#include "../objectmanager/ObjectManagerIF.h" + + +//SHOULDDO: This is by far not perfect. Could be merged with new Monitor classes. But still, it's over-engineering. +template +class TriplexMonitor : public HasParametersIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::TRIPLE_REDUNDACY_CHECK; + static const ReturnValue_t NOT_ENOUGH_SENSORS = MAKE_RETURN_CODE(1); + static const ReturnValue_t LOWEST_VALUE_OOL = MAKE_RETURN_CODE(2); + static const ReturnValue_t HIGHEST_VALUE_OOL = MAKE_RETURN_CODE(3); + static const ReturnValue_t BOTH_VALUES_OOL = MAKE_RETURN_CODE(4); + static const ReturnValue_t DUPLEX_OOL = MAKE_RETURN_CODE(5); + + static const uint8_t THREE = 3; + + TriplexMonitor(const uint32_t parameterIds[3], uint8_t domainId, const T initialLimit, + Event eventTripleCheck, Event eventDualCheck) : + values(parameterIds, &dataSet), limit(initialLimit), eventTripleCheck( + eventTripleCheck), eventDualCheck(eventDualCheck), healthTable( + NULL), domainId(domainId) { + + } + virtual ~TriplexMonitor() { + } + ReturnValue_t check() { + dataSet.read(); + //check health and validity + uint8_t availableIndex[2] = { 0, 0 }; + bool first = true; + uint8_t nAvailable = 0; + for (uint8_t count = 0; count < THREE; count++) { + if (values[count].isValid() && checkObjectHealthState(count)) { + if (first) { + availableIndex[0] = count; + first = false; + } else { + //Might be filled twice, but then it's not needed anyway. + availableIndex[1] = count; + } + nAvailable++; + } + } + ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED; + switch (nAvailable) { + case 3: + result = doTriplexMonitoring(); + break; + case 2: + result = doDuplexMonitoring(availableIndex); + break; + default: + result = NOT_ENOUGH_SENSORS; + break; + } + dataSet.commit(); + return result; + } + ReturnValue_t initialize() { + healthTable = objectManager->get(objects::HEALTH_TABLE); + if (healthTable == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; + } + + ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex) { + if (domainId != this->domainId) { + return INVALID_DOMAIN_ID; + } + switch (parameterId) { + case 0: + parameterWrapper->set(limit); + break; + default: + return INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; + } + +protected: + DataSet dataSet; + PIDReaderList values; + T limit; + Event eventTripleCheck; + Event eventDualCheck; + HealthTableIF* healthTable; + uint8_t domainId; + ReturnValue_t doTriplexMonitoring() { + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + //Find middle value, by ordering indices + uint8_t index[3] = { 0, 1, 2 }; + if (values[index[0]].value > values[index[1]].value) { + std::swap(index[0], index[1]); + } + if (values[index[0]].value > values[index[2]].value) { + std::swap(index[0], index[2]); + } + if (values[index[1]].value > values[index[2]].value) { + std::swap(index[1], index[2]); + } + //Test if smallest value is out-of-limit. + if (values[index[0]] < (values[index[1]] - limit)) { + EventManagerIF::triggerEvent(getRefereneceObject(index[0]), + eventTripleCheck, LOWEST_VALUE_OOL, 0); + result = LOWEST_VALUE_OOL; + } + //Test if largest value is out-of-limit. + if (values[index[2]] > (values[index[1]] + limit)) { + EventManagerIF::triggerEvent(getRefereneceObject(index[2]), + eventTripleCheck, HIGHEST_VALUE_OOL, 0); + if (result == HasReturnvaluesIF::RETURN_OK) { + result = HIGHEST_VALUE_OOL; + } else { + result = BOTH_VALUES_OOL; + } + } + return result; + } + + ReturnValue_t doDuplexMonitoring(uint8_t index[2]) { + T mean = (values[index[0]] + values[index[1]]) / 2; + if (values[index[0]] > values[index[1]]) { + if (values[index[0]] > (mean + limit)) { + EventManagerIF::triggerEvent(getRefereneceObject(index[0]), + eventDualCheck, 0, 0); + EventManagerIF::triggerEvent(getRefereneceObject(index[1]), + eventDualCheck, 0, 0); + return DUPLEX_OOL; + } + } else { + if (values[index[1]] > (mean + limit)) { + EventManagerIF::triggerEvent(getRefereneceObject(index[0]), + eventDualCheck, 0, 0); + EventManagerIF::triggerEvent(getRefereneceObject(index[1]), + eventDualCheck, 0, 0); + return DUPLEX_OOL; + } + } + return HasReturnvaluesIF::RETURN_OK; + } + virtual bool checkObjectHealthState(uint8_t valueIndex) = 0; + virtual object_id_t getRefereneceObject(uint8_t valueIndex) = 0; +}; + +#endif /* FRAMEWORK_MONITORING_TRIPLEXMONITOR_H_ */ diff --git a/fsfw/monitoring/TwoValueLimitMonitor.h b/fsfw/monitoring/TwoValueLimitMonitor.h new file mode 100644 index 0000000..e690cda --- /dev/null +++ b/fsfw/monitoring/TwoValueLimitMonitor.h @@ -0,0 +1,44 @@ +#ifndef FRAMEWORK_MONITORING_TWOVALUELIMITMONITOR_H_ +#define FRAMEWORK_MONITORING_TWOVALUELIMITMONITOR_H_ + +#include "LimitMonitor.h" + +template +class TwoValueLimitMonitor: public LimitMonitor { +public: + TwoValueLimitMonitor(object_id_t reporterId, uint8_t monitorId, + uint32_t lowParameterId, uint32_t highParameterId, + uint16_t confirmationLimit, T lowerLimit, T upperLimit, + Event belowLowEvent = MonitoringIF::VALUE_BELOW_LOW_LIMIT, + Event aboveHighEvent = MonitoringIF::VALUE_ABOVE_HIGH_LIMIT) : + LimitMonitor(reporterId, monitorId, lowParameterId, + confirmationLimit, lowerLimit, upperLimit, belowLowEvent, + aboveHighEvent), highValueParameterId(highParameterId) { + } + virtual ~TwoValueLimitMonitor() { + } + ReturnValue_t doCheck(T lowSample, T highSample) { + T crossedLimit; + ReturnValue_t currentState = this->checkSample(lowSample, &crossedLimit); + if (currentState != HasReturnvaluesIF::RETURN_OK) { + return this->monitorStateIs(currentState, lowSample, crossedLimit); + } + currentState = this->checkSample(highSample, &crossedLimit); + return this->monitorStateIs(currentState, highSample, crossedLimit); + } +protected: + virtual void sendTransitionReport(T parameterValue, T crossedLimit, + ReturnValue_t state) { + uint32_t usedParameterId = this->parameterId; + if (state == MonitoringIF::ABOVE_HIGH_LIMIT) { + usedParameterId = this->highValueParameterId; + } + MonitoringReportContent report(usedParameterId, parameterValue, + crossedLimit, this->oldState, state); + LimitViolationReporter::sendLimitViolationReport(&report); + } +private: + const uint32_t highValueParameterId; +}; + +#endif /* FRAMEWORK_MONITORING_TWOVALUELIMITMONITOR_H_ */ diff --git a/fsfw/objectmanager/ObjectManager.cpp b/fsfw/objectmanager/ObjectManager.cpp new file mode 100644 index 0000000..5871bfc --- /dev/null +++ b/fsfw/objectmanager/ObjectManager.cpp @@ -0,0 +1,107 @@ +#include "ObjectManager.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include +#include + +ObjectManager::ObjectManager( void (*setProducer)() ): + produceObjects(setProducer) { + //There's nothing special to do in the constructor. +} + + +ObjectManager::~ObjectManager() { + for (auto const& iter : objectList) { + delete iter.second; + } +} + +ReturnValue_t ObjectManager::insert( object_id_t id, SystemObjectIF* object) { + auto returnPair = objectList.emplace(id, object); + if (returnPair.second) { + // sif::debug << "ObjectManager::insert: Object " << std::hex + // << (int)id << std::dec << " inserted." << std::endl; + return this->RETURN_OK; + } else { + sif::error << "ObjectManager::insert: Object id " << std::hex + << (int)id << std::dec << " is already in use!" << std::endl; + sif::error << "Terminating program." << std::endl; + //This is very severe and difficult to handle in other places. + std::exit(INSERTION_FAILED); + } +} + +ReturnValue_t ObjectManager::remove( object_id_t id ) { + if ( this->getSystemObject(id) != NULL ) { + this->objectList.erase( id ); + //sif::debug << "ObjectManager::removeObject: Object " << std::hex + // << (int)id << std::dec << " removed." << std::endl; + return RETURN_OK; + } else { + sif::error << "ObjectManager::removeObject: Requested object " + << std::hex << (int)id << std::dec << " not found." << std::endl; + return NOT_FOUND; + } +} + + + +SystemObjectIF* ObjectManager::getSystemObject( object_id_t id ) { + auto listIter = this->objectList.find( id ); + if (listIter == this->objectList.end() ) { + return nullptr; + } else { + return listIter->second; + } +} + +ObjectManager::ObjectManager() : produceObjects(nullptr) { + +} + +void ObjectManager::initialize() { + if(produceObjects == nullptr) { + sif::error << "ObjectManager::initialize: Passed produceObjects " + "functions is nullptr!" << std::endl; + return; + } + this->produceObjects(); + ReturnValue_t result = RETURN_FAILED; + uint32_t errorCount = 0; + for (auto const& it : objectList) { + result = it.second->initialize(); + if ( result != RETURN_OK ) { + object_id_t var = it.first; + sif::error << "ObjectManager::initialize: Object 0x" << std::hex << + std::setw(8) << std::setfill('0')<< var << " failed to " + "initialize with code 0x" << result << std::dec << + std::setfill(' ') << std::endl; + errorCount++; + } + } + if (errorCount > 0) { + sif::error << "ObjectManager::ObjectManager: Counted " << errorCount + << " failed initializations." << std::endl; + } + //Init was successful. Now check successful interconnections. + errorCount = 0; + for (auto const& it : objectList) { + result = it.second->checkObjectConnections(); + if ( result != RETURN_OK ) { + sif::error << "ObjectManager::ObjectManager: Object " << std::hex << + (int) it.first << " connection check failed with code 0x" + << result << std::dec << std::endl; + errorCount++; + } + } + if (errorCount > 0) { + sif::error << "ObjectManager::ObjectManager: Counted " << errorCount + << " failed connection checks." << std::endl; + } +} + +void ObjectManager::printList() { + sif::debug << "ObjectManager: Object List contains:" << std::endl; + for (auto const& it : objectList) { + sif::debug << std::hex << it.first << " | " << it.second << std::endl; + } +} diff --git a/fsfw/objectmanager/ObjectManager.h b/fsfw/objectmanager/ObjectManager.h new file mode 100644 index 0000000..69a74f7 --- /dev/null +++ b/fsfw/objectmanager/ObjectManager.h @@ -0,0 +1,62 @@ +#ifndef FSFW_OBJECTMANAGER_OBJECTMANAGER_H_ +#define FSFW_OBJECTMANAGER_OBJECTMANAGER_H_ + +#include "ObjectManagerIF.h" +#include "SystemObjectIF.h" +#include + +/** + * @brief This class implements a global object manager. + * @details This manager handles a list of available objects with system-wide + * relevance, such as device handlers, and TM/TC services. Objects can + * be inserted, removed and retrieved from the list. In addition, the + * class holds a so-called factory, that creates and inserts new + * objects if they are not already in the list. This feature automates + * most of the system initialization. + * As the system is static after initialization, no new objects are + * created or inserted into the list after startup. + * @ingroup system_objects + * @author Bastian Baetz + */ +class ObjectManager : public ObjectManagerIF { +private: + //comparison? + /** + * @brief This is the map of all initialized objects in the manager. + * @details Objects in the List must inherit the SystemObjectIF. + */ + std::map objectList; +protected: + SystemObjectIF* getSystemObject( object_id_t id ); + /** + * @brief This attribute is initialized with the factory function + * that creates new objects. + * @details The function is called if an object was requested with + * getSystemObject, but not found in objectList. + * @param The id of the object to be created. + * @return Returns a pointer to the newly created object or NULL. + */ + void (*produceObjects)(); +public: + /** + * @brief Apart from setting the producer function, nothing special + * happens in the constructor. + * @param setProducer A pointer to a factory function. + */ + ObjectManager( void (*produce)() ); + ObjectManager(); + /** + * @brief In the class's destructor, all objects in the list are deleted. + */ + // SHOULDDO: If, for some reason, deleting an ObjectManager instance is + // required, check if this works. + virtual ~ObjectManager( void ); + ReturnValue_t insert( object_id_t id, SystemObjectIF* object ); + ReturnValue_t remove( object_id_t id ); + void initialize(); + void printList(); +}; + + + +#endif /* FSFW_OBJECTMANAGER_OBJECTMANAGER_H_ */ diff --git a/fsfw/objectmanager/ObjectManagerIF.h b/fsfw/objectmanager/ObjectManagerIF.h new file mode 100644 index 0000000..4bd1d91 --- /dev/null +++ b/fsfw/objectmanager/ObjectManagerIF.h @@ -0,0 +1,96 @@ +#ifndef FSFW_OBJECTMANAGER_OBJECTMANAGERIF_H_ +#define FSFW_OBJECTMANAGER_OBJECTMANAGERIF_H_ + +#include "frameworkObjects.h" +#include "SystemObjectIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +/** + * @brief This class provides an interface to the global object manager. + * @details This manager handles a list of available objects with system-wide + * relevance, such as device handlers, and TM/TC services. They can be + * inserted, removed and retrieved from the list. On getting the + * object, the call checks if the object implements the requested + * interface. + * @author Bastian Baetz + * @ingroup system_objects + */ +class ObjectManagerIF : public HasReturnvaluesIF { +public: + static constexpr uint8_t INTERFACE_ID = CLASS_ID::OBJECT_MANAGER_IF; + static constexpr ReturnValue_t INSERTION_FAILED = MAKE_RETURN_CODE( 1 ); + static constexpr ReturnValue_t NOT_FOUND = MAKE_RETURN_CODE( 2 ); + static constexpr ReturnValue_t CHILD_INIT_FAILED = MAKE_RETURN_CODE( 3 ); //!< Can be used if the initialization of a SystemObject failed. + static constexpr ReturnValue_t INTERNAL_ERR_REPORTER_UNINIT = MAKE_RETURN_CODE( 4 ); + +protected: + /** + * @brief This method is used to hide the template-based get call from + * a specific implementation. + * @details So, an implementation only has to implement this call. + * @param id The object id of the requested object. + * @return The method returns a pointer to an object implementing (at + * least) the SystemObjectIF, or NULL. + */ + virtual SystemObjectIF* getSystemObject( object_id_t id ) = 0; +public: + /** + * @brief This is the empty virtual destructor as requested by C++ interfaces. + */ + virtual ~ObjectManagerIF( void ) {}; + /** + * @brief With this call, new objects are inserted to the list. + * @details The implementation shall return an error code in case the + * object can't be added (e.g. the id is already in use). + * @param id The new id to be added to the list. + * @param object A pointer to the object to be added. + * @return @li INSERTION_FAILED in case the object could not be inserted. + * @li RETURN_OK in case the object was successfully inserted + */ + virtual ReturnValue_t insert( object_id_t id, SystemObjectIF* object ) = 0; + /** + * @brief With the get call, interfaces of an object can be retrieved in + * a type-safe manner. + * @details With the template-based call, the object list is searched with the + * getSystemObject method and afterwards it is checked, if the object + * implements the requested interface (with a dynamic_cast). + * @param id The object id of the requested object. + * @return The method returns a pointer to an object implementing the + * requested interface, or NULL. + */ + template T* get( object_id_t id ); + /** + * @brief With this call, an object is removed from the list. + * @param id The object id of the object to be removed. + * @return \li NOT_FOUND in case the object was not found + * \li RETURN_OK in case the object was successfully removed + */ + virtual ReturnValue_t remove( object_id_t id ) = 0; + virtual void initialize() = 0; + /** + * @brief This is a debug function, that prints the current content of the + * object list. + */ + virtual void printList() = 0; +}; + + +/** + * @brief This is the forward declaration of the global objectManager instance. + */ +// SHOULDDO: maybe put this in the glob namespace to explicitely mark it global? +extern ObjectManagerIF *objectManager; + +/*Documentation can be found in the class method declaration above.*/ +template +T* ObjectManagerIF::get( object_id_t id ) { + if(objectManager == nullptr) { + sif::error << "ObjectManagerIF: Global object manager has not " + "been initialized yet!" << std::endl; + } + SystemObjectIF* temp = this->getSystemObject(id); + return dynamic_cast(temp); +} + +#endif /* OBJECTMANAGERIF_H_ */ diff --git a/fsfw/objectmanager/SystemObject.cpp b/fsfw/objectmanager/SystemObject.cpp new file mode 100644 index 0000000..64330fb --- /dev/null +++ b/fsfw/objectmanager/SystemObject.cpp @@ -0,0 +1,37 @@ +#include "ObjectManager.h" +#include "SystemObject.h" +#include "../events/EventManagerIF.h" + +SystemObject::SystemObject(object_id_t setObjectId, bool doRegister) : + objectId(setObjectId), registered(doRegister) { + if (registered) { + objectManager->insert(objectId, this); + } +} + +SystemObject::~SystemObject() { + if (registered) { + objectManager->remove(objectId); + } +} + +object_id_t SystemObject::getObjectId() const { + return objectId; +} + +void SystemObject::triggerEvent(Event event, uint32_t parameter1, + uint32_t parameter2) { + EventManagerIF::triggerEvent(objectId, event, parameter1, parameter2); +} + +ReturnValue_t SystemObject::initialize() { + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t SystemObject::checkObjectConnections() { + return HasReturnvaluesIF::RETURN_OK; +} + +void SystemObject::forwardEvent(Event event, uint32_t parameter1, uint32_t parameter2) const { + EventManagerIF::triggerEvent(objectId, event, parameter1, parameter2); +} diff --git a/fsfw/objectmanager/SystemObject.h b/fsfw/objectmanager/SystemObject.h new file mode 100644 index 0000000..d9c4236 --- /dev/null +++ b/fsfw/objectmanager/SystemObject.h @@ -0,0 +1,58 @@ +#ifndef FSFW_OBJECTMANAGER_SYSTEMOBJECT_H_ +#define FSFW_OBJECTMANAGER_SYSTEMOBJECT_H_ + +#include "SystemObjectIF.h" +#include "../events/Event.h" +#include "../events/EventReportingProxyIF.h" +#include "../timemanager/Clock.h" + +/** + * @brief This class automates insertion into the ObjectManager and + * management of the object id. + * @details This class is more a base class, which shall be inherited by any + * class that is announced to ObjectManager. It automatically includes + * itself (and therefore the inheriting class) in the object manager's + * list. + * @author Ulrich Mohr + * @ingroup system_objects + */ +class SystemObject: public SystemObjectIF { +private: + /** + * @brief This is the id the class instant got assigned. + */ + const object_id_t objectId; + const bool registered; +public: + + /** + * Helper function to send Event Messages to the Event Manager + * @param event + * @param parameter1 + * @param parameter2 + */ + virtual void triggerEvent(Event event, uint32_t parameter1 = 0, + uint32_t parameter2 = 0); + + /** + * @brief The class's constructor. + * @details In the constructor, the object id is set and the class is + * inserted in the object manager. + * @param setObjectId The id the object shall have. + * @param doRegister Determines if the object is registered in + * the global object manager. + */ + SystemObject(object_id_t setObjectId, bool doRegister = true); + /** + * @brief On destruction, the object removes itself from the list. + */ + virtual ~SystemObject(); + object_id_t getObjectId() const override; + virtual ReturnValue_t initialize() override; + virtual ReturnValue_t checkObjectConnections(); + + virtual void forwardEvent(Event event, uint32_t parameter1 = 0, + uint32_t parameter2 = 0) const; +}; + +#endif /* FSFW_OBJECTMANAGER_SYSTEMOBJECT_H_ */ diff --git a/fsfw/objectmanager/SystemObjectIF.h b/fsfw/objectmanager/SystemObjectIF.h new file mode 100644 index 0000000..315fde9 --- /dev/null +++ b/fsfw/objectmanager/SystemObjectIF.h @@ -0,0 +1,62 @@ +#ifndef FSFW_OBJECTMANAGER_SYSTEMOBJECTIF_H_ +#define FSFW_OBJECTMANAGER_SYSTEMOBJECTIF_H_ + +#include "../events/EventReportingProxyIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include +/** + * @defgroup system_objects Software System Object Management + * The classes to create System Objects and classes to manage these are + * contained in this group. System Objects are software elements that can be + * controlled externally. They all have a unique object identifier. + */ + +/** + * This is the typedef for object identifiers. + * @ingroup system_objects + */ +typedef uint32_t object_id_t; + +/** + * This interface allows a class to be included in the object manager + * list. + * It does not provide any method definitions, still it is required to + * perform a type check with dynamic_cast. + * @author Bastian Baetz + * @ingroup system_objects + */ +class SystemObjectIF : public EventReportingProxyIF { +public: + /** + * This is a simple getter to return the object identifier. + * @return Returns the object id of this object. + */ + virtual object_id_t getObjectId() const = 0; + /** + * The empty virtual destructor as required for C++ interfaces. + */ + virtual ~SystemObjectIF() {} + /** + * @brief Initializes the object. + * There are initialization steps which can also be done in the constructor. + * However, there is no clean way to get a returnvalue from a constructor. + * Furthermore some components require other system object to be created + * which might not have been built yet. + * Therefore, a two-step initialization resolves this problem and prevents + * circular dependencies of not-fully initialized objects on start up. + * @return - @c RETURN_OK in case the initialization was successful + * - @c RETURN_FAILED otherwise + */ + virtual ReturnValue_t initialize() = 0; + /** + * @brief Checks if all object-object interconnections are satisfying + * for operation. + * Some objects need certain other objects (or a certain number), to be + * registered as children. These checks can be done in this method. + * @return - @c RETURN_OK in case the check was successful + * - @c any other code otherwise + */ + virtual ReturnValue_t checkObjectConnections() = 0; +}; + +#endif /* #ifndef FSFW_OBJECTMANAGER_SYSTEMOBJECTIF_H_ */ diff --git a/fsfw/objectmanager/frameworkObjects.h b/fsfw/objectmanager/frameworkObjects.h new file mode 100644 index 0000000..4d08f08 --- /dev/null +++ b/fsfw/objectmanager/frameworkObjects.h @@ -0,0 +1,31 @@ +#ifndef FSFW_OBJECTMANAGER_FRAMEWORKOBJECTS_H_ +#define FSFW_OBJECTMANAGER_FRAMEWORKOBJECTS_H_ + +namespace objects { +enum framework_objects { + // Default verification reporter. + PUS_SERVICE_1_VERIFICATION = 0x53000001, + PUS_SERVICE_2_DEVICE_ACCESS = 0x53000002, + PUS_SERVICE_5_EVENT_REPORTING = 0x53000005, + PUS_SERVICE_8_FUNCTION_MGMT = 0x53000008, + PUS_SERVICE_9_TIME_MGMT = 0x53000009, + PUS_SERVICE_17_TEST = 0x53000017, + PUS_SERVICE_200_MODE_MGMT = 0x53000200, + + //Generic IDs for IPC, modes, health, events + HEALTH_TABLE = 0x53010000, +// MODE_STORE = 0x53010100, + EVENT_MANAGER = 0x53030000, + INTERNAL_ERROR_REPORTER = 0x53040000, + IPC_STORE = 0x534f0300, + //IDs for PUS Packet Communication + TC_STORE = 0x534f0100, + TM_STORE = 0x534f0200, + + NO_OBJECT = 0xFFFFFFFF +}; +} + + + +#endif /* FSFW_OBJECTMANAGER_FRAMEWORKOBJECTS_H_ */ diff --git a/fsfw/osal/Endiness.h b/fsfw/osal/Endiness.h new file mode 100644 index 0000000..65cc0a1 --- /dev/null +++ b/fsfw/osal/Endiness.h @@ -0,0 +1,33 @@ +#ifndef FRAMEWORK_OSAL_ENDINESS_H_ +#define FRAMEWORK_OSAL_ENDINESS_H_ + + +/* + * BSD-style endian declaration + */ +#ifndef BIG_ENDIAN +#define BIG_ENDIAN 4321 +#endif +#ifndef LITTLE_ENDIAN +#define LITTLE_ENDIAN 1234 +#endif + +// This is a GCC C extension +#ifndef BYTE_ORDER_SYSTEM +#ifdef __BYTE_ORDER__ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define BYTE_ORDER_SYSTEM LITTLE_ENDIAN +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define BYTE_ORDER_SYSTEM BIG_ENDIAN +#else +#error "Can't decide which end is which!" +#endif +#else +#error __BYTE_ORDER__ not defined +#endif +#endif + + + + +#endif /* FRAMEWORK_OSAL_ENDINESS_H_ */ diff --git a/fsfw/osal/FreeRTOS/BinSemaphUsingTask.cpp b/fsfw/osal/FreeRTOS/BinSemaphUsingTask.cpp new file mode 100644 index 0000000..dd1e48c --- /dev/null +++ b/fsfw/osal/FreeRTOS/BinSemaphUsingTask.cpp @@ -0,0 +1,95 @@ +#include "../../osal/FreeRTOS/BinSemaphUsingTask.h" +#include "../../osal/FreeRTOS/TaskManagement.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() { + handle = TaskManagement::getCurrentTaskHandle(); + if(handle == nullptr) { + sif::error << "Could not retrieve task handle. Please ensure the" + "constructor was called inside a task." << std::endl; + } + xTaskNotifyGive(handle); +} + +BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() { + // Clear notification value on destruction. + xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr); +} + +ReturnValue_t BinarySemaphoreUsingTask::acquire(TimeoutType timeoutType, + uint32_t timeoutMs) { + TickType_t timeout = 0; + if(timeoutType == TimeoutType::POLLING) { + timeout = 0; + } + else if(timeoutType == TimeoutType::WAITING){ + timeout = pdMS_TO_TICKS(timeoutMs); + } + else { + timeout = portMAX_DELAY; + } + return acquireWithTickTimeout(timeoutType, timeout); +} + +ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout( + TimeoutType timeoutType, TickType_t timeoutTicks) { + BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + return SemaphoreIF::SEMAPHORE_TIMEOUT; + } +} + +ReturnValue_t BinarySemaphoreUsingTask::release() { + return release(this->handle); +} + +ReturnValue_t BinarySemaphoreUsingTask::release( + TaskHandle_t taskHandle) { + if(getSemaphoreCounter(taskHandle) == 1) { + return SemaphoreIF::SEMAPHORE_NOT_OWNED; + } + BaseType_t returncode = xTaskNotifyGive(taskHandle); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + // This should never happen. + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +TaskHandle_t BinarySemaphoreUsingTask::getTaskHandle() { + return handle; +} + +uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter() const { + return getSemaphoreCounter(this->handle); +} + +uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter( + TaskHandle_t taskHandle) { + uint32_t notificationValue; + xTaskNotifyAndQuery(taskHandle, 0, eNoAction, ¬ificationValue); + return notificationValue; +} + +// Be careful with the stack size here. This is called from an ISR! +ReturnValue_t BinarySemaphoreUsingTask::releaseFromISR( + TaskHandle_t taskHandle, BaseType_t * higherPriorityTaskWoken) { + if(getSemaphoreCounterFromISR(taskHandle, higherPriorityTaskWoken) == 1) { + return SemaphoreIF::SEMAPHORE_NOT_OWNED; + } + vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken); + return HasReturnvaluesIF::RETURN_OK; +} + +uint8_t BinarySemaphoreUsingTask::getSemaphoreCounterFromISR( + TaskHandle_t taskHandle, BaseType_t* higherPriorityTaskWoken) { + uint32_t notificationValue = 0; + xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, ¬ificationValue, + higherPriorityTaskWoken); + return notificationValue; +} diff --git a/fsfw/osal/FreeRTOS/BinSemaphUsingTask.h b/fsfw/osal/FreeRTOS/BinSemaphUsingTask.h new file mode 100644 index 0000000..f3c0b0e --- /dev/null +++ b/fsfw/osal/FreeRTOS/BinSemaphUsingTask.h @@ -0,0 +1,76 @@ +#ifndef FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ +#define FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" +#include "../../tasks/SemaphoreIF.h" + +#include +#include + +/** + * @brief Binary Semaphore implementation using the task notification value. + * The notification value should therefore not be used + * for other purposes. + * @details + * Additional information: https://www.freertos.org/RTOS-task-notifications.html + * and general semaphore documentation. + */ +class BinarySemaphoreUsingTask: public SemaphoreIF, + public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF; + + //! @brief Default ctor + BinarySemaphoreUsingTask(); + //! @brief Default dtor + virtual~ BinarySemaphoreUsingTask(); + + ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING, + uint32_t timeoutMs = portMAX_DELAY) override; + ReturnValue_t release() override; + uint8_t getSemaphoreCounter() const override; + static uint8_t getSemaphoreCounter(TaskHandle_t taskHandle); + static uint8_t getSemaphoreCounterFromISR(TaskHandle_t taskHandle, + BaseType_t* higherPriorityTaskWoken); + + /** + * Same as acquire() with timeout in FreeRTOS ticks. + * @param timeoutTicks + * @return - @c RETURN_OK on success + * - @c RETURN_FAILED on failure + */ + ReturnValue_t acquireWithTickTimeout( + TimeoutType timeoutType = TimeoutType::BLOCKING, + TickType_t timeoutTicks = portMAX_DELAY); + + /** + * Get handle to the task related to the semaphore. + * @return + */ + TaskHandle_t getTaskHandle(); + + /** + * Wrapper function to give back semaphore from handle + * @param semaphore + * @return - @c RETURN_OK on success + * - @c RETURN_FAILED on failure + */ + static ReturnValue_t release(TaskHandle_t taskToNotify); + + /** + * Wrapper function to give back semaphore from handle when called from an ISR + * @param semaphore + * @param higherPriorityTaskWoken This will be set to pdPASS if a task with + * a higher priority was unblocked. A context switch should be requested + * from an ISR if this is the case (see TaskManagement functions) + * @return - @c RETURN_OK on success + * - @c RETURN_FAILED on failure + */ + static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify, + BaseType_t * higherPriorityTaskWoken); + +protected: + TaskHandle_t handle; +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ */ diff --git a/fsfw/osal/FreeRTOS/BinarySemaphore.cpp b/fsfw/osal/FreeRTOS/BinarySemaphore.cpp new file mode 100644 index 0000000..8cc3c49 --- /dev/null +++ b/fsfw/osal/FreeRTOS/BinarySemaphore.cpp @@ -0,0 +1,108 @@ +#include "../../osal/FreeRTOS/BinarySemaphore.h" +#include "../../osal/FreeRTOS/TaskManagement.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +BinarySemaphore::BinarySemaphore() { + handle = xSemaphoreCreateBinary(); + if(handle == nullptr) { + sif::error << "Semaphore: Binary semaph creation failure" << std::endl; + } + // Initiated semaphore must be given before it can be taken. + xSemaphoreGive(handle); +} + +BinarySemaphore::~BinarySemaphore() { + vSemaphoreDelete(handle); +} + +BinarySemaphore::BinarySemaphore(BinarySemaphore&& s) { + handle = xSemaphoreCreateBinary(); + if(handle == nullptr) { + sif::error << "Binary semaphore creation failure" << std::endl; + } + xSemaphoreGive(handle); +} + +BinarySemaphore& BinarySemaphore::operator =( + BinarySemaphore&& s) { + if(&s != this) { + handle = xSemaphoreCreateBinary(); + if(handle == nullptr) { + sif::error << "Binary semaphore creation failure" << std::endl; + } + xSemaphoreGive(handle); + } + return *this; +} + +ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType, + uint32_t timeoutMs) { + TickType_t timeout = 0; + if(timeoutType == TimeoutType::POLLING) { + timeout = 0; + } + else if(timeoutType == TimeoutType::WAITING){ + timeout = pdMS_TO_TICKS(timeoutMs); + } + else { + timeout = portMAX_DELAY; + } + return acquireWithTickTimeout(timeoutType, timeout); +} + +ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TimeoutType timeoutType, + TickType_t timeoutTicks) { + if(handle == nullptr) { + return SemaphoreIF::SEMAPHORE_INVALID; + } + + BaseType_t returncode = xSemaphoreTake(handle, timeoutTicks); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + return SemaphoreIF::SEMAPHORE_TIMEOUT; + } +} + +ReturnValue_t BinarySemaphore::release() { + return release(handle); +} + +ReturnValue_t BinarySemaphore::release(SemaphoreHandle_t semaphore) { + if (semaphore == nullptr) { + return SemaphoreIF::SEMAPHORE_INVALID; + } + BaseType_t returncode = xSemaphoreGive(semaphore); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + return SemaphoreIF::SEMAPHORE_NOT_OWNED; + } +} + +uint8_t BinarySemaphore::getSemaphoreCounter() const { + return uxSemaphoreGetCount(handle); +} + +SemaphoreHandle_t BinarySemaphore::getSemaphore() { + return handle; +} + + +// Be careful with the stack size here. This is called from an ISR! +ReturnValue_t BinarySemaphore::releaseFromISR( + SemaphoreHandle_t semaphore, BaseType_t * higherPriorityTaskWoken) { + if (semaphore == nullptr) { + return SemaphoreIF::SEMAPHORE_INVALID; + } + BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, + higherPriorityTaskWoken); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + return SemaphoreIF::SEMAPHORE_NOT_OWNED; + } +} diff --git a/fsfw/osal/FreeRTOS/BinarySemaphore.h b/fsfw/osal/FreeRTOS/BinarySemaphore.h new file mode 100644 index 0000000..c6cedc5 --- /dev/null +++ b/fsfw/osal/FreeRTOS/BinarySemaphore.h @@ -0,0 +1,107 @@ +#ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ +#define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" +#include "../../tasks/SemaphoreIF.h" + +#include +#include + +/** + * @brief OS Tool to achieve synchronization of between tasks or between + * task and ISR. The default semaphore implementation creates a + * binary semaphore, which can only be taken once. + * @details + * Documentation: https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html + * + * Please note that if the semaphore implementation is only related to + * the synchronization of one task, the new task notifications can be used, + * also see the BinSemaphUsingTask and CountingSemaphUsingTask classes. + * These use the task notification value instead of a queue and are + * faster and more efficient. + * + * @author R. Mueller + * @ingroup osal + */ +class BinarySemaphore: public SemaphoreIF, + public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF; + + //! @brief Default ctor + BinarySemaphore(); + //! @brief Copy ctor, deleted explicitely. + BinarySemaphore(const BinarySemaphore&) = delete; + //! @brief Copy assignment, deleted explicitely. + BinarySemaphore& operator=(const BinarySemaphore&) = delete; + //! @brief Move ctor + BinarySemaphore (BinarySemaphore &&); + //! @brief Move assignment + BinarySemaphore & operator=(BinarySemaphore &&); + //! @brief Destructor + virtual ~BinarySemaphore(); + + uint8_t getSemaphoreCounter() const override; + + /** + * Take the binary semaphore. + * If the semaphore has already been taken, the task will be blocked + * for a maximum of #timeoutMs or until the semaphore is given back, + * for example by an ISR or another task. + * @param timeoutMs + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout + */ + ReturnValue_t acquire(TimeoutType timeoutType = + TimeoutType::BLOCKING, uint32_t timeoutMs = portMAX_DELAY) override; + + /** + * Same as lockBinarySemaphore() with timeout in FreeRTOS ticks. + * @param timeoutTicks + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout + */ + ReturnValue_t acquireWithTickTimeout(TimeoutType timeoutType = + TimeoutType::BLOCKING, TickType_t timeoutTicks = portMAX_DELAY); + + /** + * Release the binary semaphore. + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is + * already available. + */ + ReturnValue_t release() override; + + /** + * Get Handle to the semaphore. + * @return + */ + SemaphoreHandle_t getSemaphore(); + + /** + * Wrapper function to give back semaphore from handle + * @param semaphore + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is + * already available. + */ + static ReturnValue_t release(SemaphoreHandle_t semaphore); + + /** + * Wrapper function to give back semaphore from handle when called from an ISR + * @param semaphore + * @param higherPriorityTaskWoken This will be set to pdPASS if a task with + * a higher priority was unblocked. A context switch from an ISR should + * then be requested (see TaskManagement functions) + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is + * already available. + */ + static ReturnValue_t releaseFromISR(SemaphoreHandle_t semaphore, + BaseType_t * higherPriorityTaskWoken); + +protected: + SemaphoreHandle_t handle; +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */ diff --git a/fsfw/osal/FreeRTOS/Clock.cpp b/fsfw/osal/FreeRTOS/Clock.cpp new file mode 100644 index 0000000..acfa98d --- /dev/null +++ b/fsfw/osal/FreeRTOS/Clock.cpp @@ -0,0 +1,196 @@ +#include "../../timemanager/Clock.h" +#include "../../globalfunctions/timevalOperations.h" +#include "Timekeeper.h" + +#include +#include + +#include +#include + +//TODO sanitize input? +//TODO much of this code can be reused for tick-only systems + +uint16_t Clock::leapSeconds = 0; +MutexIF* Clock::timeMutex = nullptr; + +uint32_t Clock::getTicksPerSecond(void) { + return 1000; +} + +ReturnValue_t Clock::setClock(const TimeOfDay_t* time) { + + timeval time_timeval; + + ReturnValue_t result = convertTimeOfDayToTimeval(time, &time_timeval); + if (result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + + return setClock(&time_timeval); +} + +ReturnValue_t Clock::setClock(const timeval* time) { + timeval uptime = getUptime(); + + timeval offset = *time - uptime; + + Timekeeper::instance()->setOffset(offset); + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getClock_timeval(timeval* time) { + timeval uptime = getUptime(); + + timeval offset = Timekeeper::instance()->getOffset(); + + *time = offset + uptime; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getUptime(timeval* uptime) { + *uptime = getUptime(); + + return HasReturnvaluesIF::RETURN_OK; +} + +timeval Clock::getUptime() { + TickType_t ticksSinceStart = xTaskGetTickCount(); + return Timekeeper::ticksToTimeval(ticksSinceStart); +} + +ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) { + timeval uptime = getUptime(); + *uptimeMs = uptime.tv_sec * 1000 + uptime.tv_usec / 1000; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getClock_usecs(uint64_t* time) { + timeval time_timeval; + ReturnValue_t result = getClock_timeval(&time_timeval); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + *time = time_timeval.tv_sec * 1000000 + time_timeval.tv_usec; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) { + timeval time_timeval; + ReturnValue_t result = getClock_timeval(&time_timeval); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + struct tm time_tm; + + gmtime_r(&time_timeval.tv_sec,&time_tm); + + time->year = time_tm.tm_year + 1900; + time->month = time_tm.tm_mon + 1; + time->day = time_tm.tm_mday; + + time->hour = time_tm.tm_hour; + time->minute = time_tm.tm_min; + time->second = time_tm.tm_sec; + + time->usecond = time_timeval.tv_usec; + + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from, + timeval* to) { + struct tm time_tm; + + time_tm.tm_year = from->year - 1900; + time_tm.tm_mon = from->month - 1; + time_tm.tm_mday = from->day; + + time_tm.tm_hour = from->hour; + time_tm.tm_min = from->minute; + time_tm.tm_sec = from->second; + + time_t seconds = mktime(&time_tm); + + to->tv_sec = seconds; + to->tv_usec = from->usecond; + //Fails in 2038.. + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) { + *JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24. + / 3600.; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) { + //SHOULDDO: works not for dates in the past (might have less leap seconds) + if (timeMutex == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + + uint16_t leapSeconds; + ReturnValue_t result = getLeapSeconds(&leapSeconds); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + timeval leapSeconds_timeval = { 0, 0 }; + leapSeconds_timeval.tv_sec = leapSeconds; + + //initial offset between UTC and TAI + timeval UTCtoTAI1972 = { 10, 0 }; + + timeval TAItoTT = { 32, 184000 }; + + *tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) { + if (checkOrCreateClockMutex() != HasReturnvaluesIF::RETURN_OK) { + return HasReturnvaluesIF::RETURN_FAILED; + } + ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + leapSeconds = leapSeconds_; + + result = timeMutex->unlockMutex(); + return result; +} + +ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) { + if (timeMutex == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + *leapSeconds_ = leapSeconds; + + result = timeMutex->unlockMutex(); + return result; +} + +ReturnValue_t Clock::checkOrCreateClockMutex() { + if (timeMutex == NULL) { + MutexFactory* mutexFactory = MutexFactory::instance(); + if (mutexFactory == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + timeMutex = mutexFactory->createMutex(); + if (timeMutex == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/osal/FreeRTOS/CountingSemaphUsingTask.cpp b/fsfw/osal/FreeRTOS/CountingSemaphUsingTask.cpp new file mode 100644 index 0000000..a47341b --- /dev/null +++ b/fsfw/osal/FreeRTOS/CountingSemaphUsingTask.cpp @@ -0,0 +1,114 @@ +#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h" +#include "../../osal/FreeRTOS/TaskManagement.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(const uint8_t maxCount, + uint8_t initCount): maxCount(maxCount) { + if(initCount > maxCount) { + sif::error << "CountingSemaphoreUsingTask: Max count bigger than " + "intial cout. Setting initial count to max count." << std::endl; + initCount = maxCount; + } + + handle = TaskManagement::getCurrentTaskHandle(); + if(handle == nullptr) { + sif::error << "CountingSemaphoreUsingTask: Could not retrieve task " + "handle. Please ensure the constructor was called inside a " + "task." << std::endl; + } + + uint32_t oldNotificationValue; + xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, + &oldNotificationValue); + if(oldNotificationValue != 0) { + sif::warning << "CountinSemaphoreUsingTask: Semaphore initiated but " + "current notification value is not 0. Please ensure the " + "notification value is not used for other purposes!" << std::endl; + } + for(int i = 0; i < initCount; i++) { + xTaskNotifyGive(handle); + } +} + +CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() { + // Clear notification value on destruction. + // If this is not desired, don't call the destructor + // (or implement a boolean which disables the reset) + xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr); +} + +ReturnValue_t CountingSemaphoreUsingTask::acquire(TimeoutType timeoutType, + uint32_t timeoutMs) { + TickType_t timeout = 0; + if(timeoutType == TimeoutType::POLLING) { + timeout = 0; + } + else if(timeoutType == TimeoutType::WAITING){ + timeout = pdMS_TO_TICKS(timeoutMs); + } + else { + timeout = portMAX_DELAY; + } + return acquireWithTickTimeout(timeoutType, timeout); + +} + +ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout( + TimeoutType timeoutType, TickType_t timeoutTicks) { + // Decrement notfication value without resetting it. + BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks); + if (getSemaphoreCounter() == oldCount - 1) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + return SemaphoreIF::SEMAPHORE_TIMEOUT; + } +} + +ReturnValue_t CountingSemaphoreUsingTask::release() { + if(getSemaphoreCounter() == maxCount) { + return SemaphoreIF::SEMAPHORE_NOT_OWNED; + } + return release(handle); +} + +ReturnValue_t CountingSemaphoreUsingTask::release( + TaskHandle_t taskToNotify) { + BaseType_t returncode = xTaskNotifyGive(taskToNotify); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + // This should never happen. + return HasReturnvaluesIF::RETURN_FAILED; + } +} + + +uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const { + uint32_t notificationValue = 0; + xTaskNotifyAndQuery(handle, 0, eNoAction, ¬ificationValue); + return notificationValue; +} + +TaskHandle_t CountingSemaphoreUsingTask::getTaskHandle() { + return handle; +} + +ReturnValue_t CountingSemaphoreUsingTask::releaseFromISR( + TaskHandle_t taskToNotify, BaseType_t* higherPriorityTaskWoken) { + vTaskNotifyGiveFromISR(taskToNotify, higherPriorityTaskWoken); + return HasReturnvaluesIF::RETURN_OK; +} + +uint8_t CountingSemaphoreUsingTask::getSemaphoreCounterFromISR( + TaskHandle_t task, BaseType_t* higherPriorityTaskWoken) { + uint32_t notificationValue; + xTaskNotifyAndQueryFromISR(task, 0, eNoAction, ¬ificationValue, + higherPriorityTaskWoken); + return notificationValue; +} + +uint8_t CountingSemaphoreUsingTask::getMaxCount() const { + return maxCount; +} diff --git a/fsfw/osal/FreeRTOS/CountingSemaphUsingTask.h b/fsfw/osal/FreeRTOS/CountingSemaphUsingTask.h new file mode 100644 index 0000000..8977258 --- /dev/null +++ b/fsfw/osal/FreeRTOS/CountingSemaphUsingTask.h @@ -0,0 +1,102 @@ +#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ +#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ + +#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h" +#include "../../tasks/SemaphoreIF.h" + +extern "C" { +#include +#include +} + +/** + * @brief Couting Semaphore implementation which uses the notification value + * of the task. The notification value should therefore not be used + * for other purposes. + * @details + * Additional information: https://www.freertos.org/RTOS-task-notifications.html + * and general semaphore documentation. + */ +class CountingSemaphoreUsingTask: public SemaphoreIF { +public: + CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount); + virtual ~CountingSemaphoreUsingTask(); + + /** + * Acquire the counting semaphore. + * If no semaphores are available, the task will be blocked + * for a maximum of #timeoutMs or until one is given back, + * for example by an ISR or another task. + * @param timeoutMs + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout + */ + ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING, + uint32_t timeoutMs = portMAX_DELAY) override; + + /** + * Release a semaphore, increasing the number of available counting + * semaphores up to the #maxCount value. + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are + * already available. + */ + ReturnValue_t release() override; + + uint8_t getSemaphoreCounter() const override; + /** + * Get the semaphore counter from an ISR. + * @param task + * @param higherPriorityTaskWoken This will be set to pdPASS if a task with + * a higher priority was unblocked. A context switch should be requested + * from an ISR if this is the case (see TaskManagement functions) + * @return + */ + static uint8_t getSemaphoreCounterFromISR(TaskHandle_t task, + BaseType_t* higherPriorityTaskWoken); + + /** + * Acquire with a timeout value in ticks + * @param timeoutTicks + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout + */ + ReturnValue_t acquireWithTickTimeout( + TimeoutType timeoutType = TimeoutType::BLOCKING, + TickType_t timeoutTicks = portMAX_DELAY); + + /** + * Get handle to the task related to the semaphore. + * @return + */ + TaskHandle_t getTaskHandle(); + + /** + * Release semaphore of task by supplying task handle + * @param taskToNotify + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are + * already available. + */ + static ReturnValue_t release(TaskHandle_t taskToNotify); + /** + * Release seamphore of a task from an ISR. + * @param taskToNotify + * @param higherPriorityTaskWoken This will be set to pdPASS if a task with + * a higher priority was unblocked. A context switch should be requested + * from an ISR if this is the case (see TaskManagement functions) + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are + * already available. + */ + static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify, + BaseType_t* higherPriorityTaskWoken); + + uint8_t getMaxCount() const; + +private: + TaskHandle_t handle; + const uint8_t maxCount; +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */ diff --git a/fsfw/osal/FreeRTOS/CountingSemaphore.cpp b/fsfw/osal/FreeRTOS/CountingSemaphore.cpp new file mode 100644 index 0000000..d1310a6 --- /dev/null +++ b/fsfw/osal/FreeRTOS/CountingSemaphore.cpp @@ -0,0 +1,43 @@ +#include "../../osal/FreeRTOS/CountingSemaphore.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../osal/FreeRTOS/TaskManagement.h" + +#include + +// Make sure #define configUSE_COUNTING_SEMAPHORES 1 is set in +// free FreeRTOSConfig.h file. +CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount): + maxCount(maxCount), initCount(initCount) { + if(initCount > maxCount) { + sif::error << "CountingSemaphoreUsingTask: Max count bigger than " + "intial cout. Setting initial count to max count." << std::endl; + initCount = maxCount; + } + + handle = xSemaphoreCreateCounting(maxCount, initCount); + if(handle == nullptr) { + sif::error << "CountingSemaphore: Creation failure" << std::endl; + } +} + +CountingSemaphore::CountingSemaphore(CountingSemaphore&& other): + maxCount(other.maxCount), initCount(other.initCount) { + handle = xSemaphoreCreateCounting(other.maxCount, other.initCount); + if(handle == nullptr) { + sif::error << "CountingSemaphore: Creation failure" << std::endl; + } +} + +CountingSemaphore& CountingSemaphore::operator =( + CountingSemaphore&& other) { + handle = xSemaphoreCreateCounting(other.maxCount, other.initCount); + if(handle == nullptr) { + sif::error << "CountingSemaphore: Creation failure" << std::endl; + } + return * this; +} + + +uint8_t CountingSemaphore::getMaxCount() const { + return maxCount; +} diff --git a/fsfw/osal/FreeRTOS/CountingSemaphore.h b/fsfw/osal/FreeRTOS/CountingSemaphore.h new file mode 100644 index 0000000..ae2f62a --- /dev/null +++ b/fsfw/osal/FreeRTOS/CountingSemaphore.h @@ -0,0 +1,34 @@ +#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ +#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ +#include "../../osal/FreeRTOS/BinarySemaphore.h" + +/** + * @brief Counting semaphores, which can be acquire more than once. + * @details + * See: https://www.freertos.org/CreateCounting.html + * API of counting semaphores is almost identical to binary semaphores, + * so we just inherit from binary semaphore and provide the respective + * constructors. + */ +class CountingSemaphore: public BinarySemaphore { +public: + CountingSemaphore(const uint8_t maxCount, uint8_t initCount); + //! @brief Copy ctor, disabled + CountingSemaphore(const CountingSemaphore&) = delete; + //! @brief Copy assignment, disabled + CountingSemaphore& operator=(const CountingSemaphore&) = delete; + //! @brief Move ctor + CountingSemaphore (CountingSemaphore &&); + //! @brief Move assignment + CountingSemaphore & operator=(CountingSemaphore &&); + + /* Same API as binary semaphore otherwise. acquire() can be called + * until there are not semaphores left and release() can be called + * until maxCount is reached. */ + uint8_t getMaxCount() const; +private: + const uint8_t maxCount; + uint8_t initCount = 0; +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */ diff --git a/fsfw/osal/FreeRTOS/FixedTimeslotTask.cpp b/fsfw/osal/FreeRTOS/FixedTimeslotTask.cpp new file mode 100644 index 0000000..062686e --- /dev/null +++ b/fsfw/osal/FreeRTOS/FixedTimeslotTask.cpp @@ -0,0 +1,162 @@ +#include "FixedTimeslotTask.h" + +#include "../../serviceinterface/ServiceInterfaceStream.h" + +uint32_t FixedTimeslotTask::deadlineMissedCount = 0; +const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE = configMINIMAL_STACK_SIZE; + +FixedTimeslotTask::FixedTimeslotTask(TaskName name, TaskPriority setPriority, + TaskStackSize setStack, TaskPeriod overallPeriod, + void (*setDeadlineMissedFunc)()) : + started(false), handle(nullptr), pst(overallPeriod * 1000) { + configSTACK_DEPTH_TYPE stackSize = setStack / sizeof(configSTACK_DEPTH_TYPE); + xTaskCreate(taskEntryPoint, name, stackSize, this, setPriority, &handle); + // All additional attributes are applied to the object. + this->deadlineMissedFunc = setDeadlineMissedFunc; +} + +FixedTimeslotTask::~FixedTimeslotTask() { +} + +void FixedTimeslotTask::taskEntryPoint(void* argument) { + + // The argument is re-interpreted as FixedTimeslotTask. The Task object is + // global, so it is found from any place. + FixedTimeslotTask *originalTask(reinterpret_cast(argument)); + /* Task should not start until explicitly requested, + * but in FreeRTOS, tasks start as soon as they are created if the scheduler + * is running but not if the scheduler is not running. + * To be able to accommodate both cases we check a member which is set in + * #startTask(). If it is not set and we get here, the scheduler was started + * before #startTask() was called and we need to suspend if it is set, + * the scheduler was not running before #startTask() was called and we + * can continue */ + + if (not originalTask->started) { + vTaskSuspend(NULL); + } + + originalTask->taskFunctionality(); + sif::debug << "Polling task " << originalTask->handle + << " returned from taskFunctionality." << std::endl; +} + +void FixedTimeslotTask::missedDeadlineCounter() { + FixedTimeslotTask::deadlineMissedCount++; + if (FixedTimeslotTask::deadlineMissedCount % 10 == 0) { + sif::error << "PST missed " << FixedTimeslotTask::deadlineMissedCount + << " deadlines." << std::endl; + } +} + +ReturnValue_t FixedTimeslotTask::startTask() { + started = true; + + // We must not call resume if scheduler is not started yet + if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { + vTaskResume(handle); + } + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId, + uint32_t slotTimeMs, int8_t executionStep) { + ExecutableObjectIF* handler = + objectManager->get(componentId); + if (handler != nullptr) { + pst.addSlot(componentId, slotTimeMs, executionStep, handler, this); + return HasReturnvaluesIF::RETURN_OK; + } + + sif::error << "Component " << std::hex << componentId << + " not found, not adding it to pst" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; +} + +uint32_t FixedTimeslotTask::getPeriodMs() const { + return pst.getLengthMs(); +} + +ReturnValue_t FixedTimeslotTask::checkSequence() const { + return pst.checkSequence(); +} + +void FixedTimeslotTask::taskFunctionality() { + // A local iterator for the Polling Sequence Table is created to find the + // start time for the first entry. + auto slotListIter = pst.current; + + pst.intializeSequenceAfterTaskCreation(); + + //The start time for the first entry is read. + uint32_t intervalMs = slotListIter->pollingTimeMs; + TickType_t interval = pdMS_TO_TICKS(intervalMs); + + TickType_t xLastWakeTime; + /* The xLastWakeTime variable needs to be initialized with the current tick + count. Note that this is the only time the variable is written to + explicitly. After this assignment, xLastWakeTime is updated automatically + internally within vTaskDelayUntil(). */ + xLastWakeTime = xTaskGetTickCount(); + + // wait for first entry's start time + if(interval > 0) { + vTaskDelayUntil(&xLastWakeTime, interval); + } + + /* Enter the loop that defines the task behavior. */ + for (;;) { + //The component for this slot is executed and the next one is chosen. + this->pst.executeAndAdvance(); + if (not pst.slotFollowsImmediately()) { + // Get the interval till execution of the next slot. + intervalMs = this->pst.getIntervalToPreviousSlotMs(); + interval = pdMS_TO_TICKS(intervalMs); + + checkMissedDeadline(xLastWakeTime, interval); + + // Wait for the interval. This exits immediately if a deadline was + // missed while also updating the last wake time. + vTaskDelayUntil(&xLastWakeTime, interval); + } + } +} + +void FixedTimeslotTask::checkMissedDeadline(const TickType_t xLastWakeTime, + const TickType_t interval) { + /* Check whether deadline was missed while also taking overflows + * into account. Drawing this on paper with a timeline helps to understand + * it. */ + TickType_t currentTickCount = xTaskGetTickCount(); + TickType_t timeToWake = xLastWakeTime + interval; + // Time to wake has not overflown. + if(timeToWake > xLastWakeTime) { + /* If the current time has overflown exclusively or the current + * tick count is simply larger than the time to wake, a deadline was + * missed */ + if((currentTickCount < xLastWakeTime) or (currentTickCount > timeToWake)) { + handleMissedDeadline(); + } + } + /* Time to wake has overflown. A deadline was missed if the current time + * is larger than the time to wake */ + else if((timeToWake < xLastWakeTime) and (currentTickCount > timeToWake)) { + handleMissedDeadline(); + } +} + +void FixedTimeslotTask::handleMissedDeadline() { + if(deadlineMissedFunc != nullptr) { + this->deadlineMissedFunc(); + } +} + +ReturnValue_t FixedTimeslotTask::sleepFor(uint32_t ms) { + vTaskDelay(pdMS_TO_TICKS(ms)); + return HasReturnvaluesIF::RETURN_OK; +} + +TaskHandle_t FixedTimeslotTask::getTaskHandle() { + return handle; +} diff --git a/fsfw/osal/FreeRTOS/FixedTimeslotTask.h b/fsfw/osal/FreeRTOS/FixedTimeslotTask.h new file mode 100644 index 0000000..7d2cdb7 --- /dev/null +++ b/fsfw/osal/FreeRTOS/FixedTimeslotTask.h @@ -0,0 +1,101 @@ +#ifndef FSFW_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_ +#define FSFW_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_ + +#include "FreeRTOSTaskIF.h" +#include "../../tasks/FixedSlotSequence.h" +#include "../../tasks/FixedTimeslotTaskIF.h" +#include "../../tasks/Typedef.h" + +#include +#include + +class FixedTimeslotTask: public FixedTimeslotTaskIF, public FreeRTOSTaskIF { +public: + + /** + * Keep in mind that you need to call before vTaskStartScheduler()! + * A lot of task parameters are set in "FreeRTOSConfig.h". + * @param name Name of the task, lenght limited by configMAX_TASK_NAME_LEN + * @param setPriority Number of priorities specified by + * configMAX_PRIORITIES. High taskPriority_ number means high priority. + * @param setStack Stack size in words (not bytes!). + * Lower limit specified by configMINIMAL_STACK_SIZE + * @param overallPeriod Period in seconds. + * @param setDeadlineMissedFunc Callback if a deadline was missed. + * @return Pointer to the newly created task. + */ + FixedTimeslotTask(TaskName name, TaskPriority setPriority, + TaskStackSize setStack, TaskPeriod overallPeriod, + void (*setDeadlineMissedFunc)()); + + /** + * @brief The destructor of the class. + * @details + * The destructor frees all heap memory that was allocated on thread + * initialization for the PST and the device handlers. This is done by + * calling the PST's destructor. + */ + virtual ~FixedTimeslotTask(void); + + ReturnValue_t startTask(void); + /** + * This static function can be used as #deadlineMissedFunc. + * It counts missedDeadlines and prints the number of missed deadlines + * every 10th time. + */ + static void missedDeadlineCounter(); + /** + * A helper variable to count missed deadlines. + */ + static uint32_t deadlineMissedCount; + + ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs, + int8_t executionStep) override; + + uint32_t getPeriodMs() const override; + + ReturnValue_t checkSequence() const override; + + ReturnValue_t sleepFor(uint32_t ms) override; + + TaskHandle_t getTaskHandle() override; + +protected: + bool started; + TaskHandle_t handle; + + FixedSlotSequence pst; + + /** + * @brief This attribute holds a function pointer that is executed when + * a deadline was missed. + * @details + * Another function may be announced to determine the actions to perform + * when a deadline was missed. Currently, only one function for missing + * any deadline is allowed. If not used, it shall be declared NULL. + */ + void (*deadlineMissedFunc)(void); + /** + * @brief This is the entry point for a new task. + * @details + * This method starts the task by calling taskFunctionality(), as soon as + * all requirements (task scheduler has started and startTask() + * has been called) are met. + */ + static void taskEntryPoint(void* argument); + + /** + * @brief This function holds the main functionality of the thread. + * @details + * Core function holding the main functionality of the task + * It links the functionalities provided by FixedSlotSequence with the + * OS's System Calls to keep the timing of the periods. + */ + void taskFunctionality(void); + + void checkMissedDeadline(const TickType_t xLastWakeTime, + const TickType_t interval); + void handleMissedDeadline(); +}; + +#endif /* FSFW_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_ */ diff --git a/fsfw/osal/FreeRTOS/FreeRTOSTaskIF.h b/fsfw/osal/FreeRTOS/FreeRTOSTaskIF.h new file mode 100644 index 0000000..06fda28 --- /dev/null +++ b/fsfw/osal/FreeRTOS/FreeRTOSTaskIF.h @@ -0,0 +1,13 @@ +#ifndef FRAMEWORK_OSAL_FREERTOS_FREERTOSTASKIF_H_ +#define FRAMEWORK_OSAL_FREERTOS_FREERTOSTASKIF_H_ + +#include +#include + +class FreeRTOSTaskIF { +public: + virtual~ FreeRTOSTaskIF() {} + virtual TaskHandle_t getTaskHandle() = 0; +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_FREERTOSTASKIF_H_ */ diff --git a/fsfw/osal/FreeRTOS/MessageQueue.cpp b/fsfw/osal/FreeRTOS/MessageQueue.cpp new file mode 100644 index 0000000..2dfe5ab --- /dev/null +++ b/fsfw/osal/FreeRTOS/MessageQueue.cpp @@ -0,0 +1,156 @@ +#include "MessageQueue.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +// TODO I guess we should have a way of checking if we are in an ISR and then +// use the "fromISR" versions of all calls +// As a first step towards this, introduces system context variable which needs +// to be switched manually +// Haven't found function to find system context. +MessageQueue::MessageQueue(size_t messageDepth, size_t maxMessageSize): + maxMessageSize(maxMessageSize) { + handle = xQueueCreate(messageDepth, maxMessageSize); + if (handle == nullptr) { + sif::error << "MessageQueue::MessageQueue Creation failed" << std::endl; + } +} + +MessageQueue::~MessageQueue() { + if (handle != nullptr) { + vQueueDelete(handle); + } +} + +void MessageQueue::switchSystemContext(CallContext callContext) { + this->callContext = callContext; +} + +ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault) { + return sendMessageFrom(sendTo, message, this->getId(), ignoreFault); +} + +ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessageIF* message) { + return sendToDefaultFrom(message, this->getId()); +} + +ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessageIF* message, + MessageQueueId_t sentFrom, bool ignoreFault) { + return sendMessageFrom(defaultDestination,message,sentFrom,ignoreFault); +} + +ReturnValue_t MessageQueue::reply(MessageQueueMessageIF* message) { + if (this->lastPartner != MessageQueueIF::NO_QUEUE) { + return sendMessageFrom(this->lastPartner, message, this->getId()); + } else { + return NO_REPLY_PARTNER; + } +} + +ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + return sendMessageFromMessageQueue(sendTo, message, sentFrom, ignoreFault, + callContext); +} + + +ReturnValue_t MessageQueue::handleSendResult(BaseType_t result, bool ignoreFault) { + if (result != pdPASS) { + if (not ignoreFault) { + InternalErrorReporterIF* internalErrorReporter = objectManager-> + get( + objects::INTERNAL_ERROR_REPORTER); + if (internalErrorReporter != nullptr) { + internalErrorReporter->queueMessageNotSent(); + } + } + return MessageQueueIF::FULL; + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t* receivedFrom) { + ReturnValue_t status = this->receiveMessage(message); + if(status == HasReturnvaluesIF::RETURN_OK) { + *receivedFrom = this->lastPartner; + } + return status; +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message) { + BaseType_t result = xQueueReceive(handle,reinterpret_cast( + message->getBuffer()), 0); + if (result == pdPASS){ + this->lastPartner = message->getSender(); + return HasReturnvaluesIF::RETURN_OK; + } else { + return MessageQueueIF::EMPTY; + } +} + +MessageQueueId_t MessageQueue::getLastPartner() const { + return lastPartner; +} + +ReturnValue_t MessageQueue::flush(uint32_t* count) { + //TODO FreeRTOS does not support flushing partially + //Is always successful + xQueueReset(handle); + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueId_t MessageQueue::getId() const { + return reinterpret_cast(handle); +} + +void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) { + defaultDestinationSet = true; + this->defaultDestination = defaultDestination; +} + +MessageQueueId_t MessageQueue::getDefaultDestination() const { + return defaultDestination; +} + +bool MessageQueue::isDefaultDestinationSet() const { + return defaultDestinationSet; +} + + +// static core function to send messages. +ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault, CallContext callContext) { + BaseType_t result = pdFALSE; + QueueHandle_t destination = nullptr; + + if(sendTo == MessageQueueIF::NO_QUEUE or sendTo == 0x00) { + return MessageQueueIF::DESTINVATION_INVALID; + } + else { + destination = reinterpret_cast(sendTo); + } + + message->setSender(sentFrom); + + + if(callContext == CallContext::TASK) { + result = xQueueSendToBack(destination, + static_cast(message->getBuffer()), 0); + } + else { + /* If the call context is from an interrupt, + * request a context switch if a higher priority task + * was blocked by the interrupt. */ + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + result = xQueueSendFromISR(reinterpret_cast(sendTo), + static_cast(message->getBuffer()), + &xHigherPriorityTaskWoken); + if(xHigherPriorityTaskWoken == pdTRUE) { + TaskManagement::requestContextSwitch(callContext); + } + } + return handleSendResult(result, ignoreFault); +} diff --git a/fsfw/osal/FreeRTOS/MessageQueue.h b/fsfw/osal/FreeRTOS/MessageQueue.h new file mode 100644 index 0000000..b99bf7c --- /dev/null +++ b/fsfw/osal/FreeRTOS/MessageQueue.h @@ -0,0 +1,150 @@ +#ifndef FSFW_OSAL_FREERTOS_MESSAGEQUEUE_H_ +#define FSFW_OSAL_FREERTOS_MESSAGEQUEUE_H_ + +#include "../../internalError/InternalErrorReporterIF.h" +#include "../../ipc/MessageQueueIF.h" +#include "../../ipc/MessageQueueMessageIF.h" +#include "../../osal/FreeRTOS/TaskManagement.h" + +#include +#include +#include + +// TODO: this class assumes that MessageQueueId_t is the same size as void* +// (the FreeRTOS handle type), compiler will catch this but it might be nice +// to have something checking or even an always working solution +// https://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/ + +/** + * @brief This class manages sending and receiving of + * message queue messages. + * @details + * Message queues are used to pass asynchronous messages between processes. + * They work like post boxes, where all incoming messages are stored in FIFO + * order. This class creates a new receiving queue and provides methods to fetch + * received messages. Being a child of MessageQueueSender, this class also + * provides methods to send a message to a user-defined or a default destination. + * In addition it also provides a reply method to answer to the queue it + * received its last message from. + * + * The MessageQueue should be used as "post box" for a single owning object. + * So all message queue communication is "n-to-one". + * For creating the queue, as well as sending and receiving messages, the class + * makes use of the operating system calls provided. + * + * Please keep in mind that FreeRTOS offers different calls for message queue + * operations if called from an ISR. + * For now, the system context needs to be switched manually. + * @ingroup osal + * @ingroup message_queue + */ +class MessageQueue : public MessageQueueIF { + friend class MessageQueueSenderIF; +public: + /** + * @brief The constructor initializes and configures the message queue. + * @details + * By making use of the according operating system call, a message queue + * is created and initialized. The message depth - the maximum number of + * messages to be buffered - may be set with the help of a parameter, + * whereas the message size is automatically set to the maximum message + * queue message size. The operating system sets the message queue id, or + * in case of failure, it is set to zero. + * @param message_depth + * The number of messages to be buffered before passing an error to the + * sender. Default is three. + * @param max_message_size + * With this parameter, the maximum message size can be adjusted. + * This should be left default. + */ + MessageQueue( size_t messageDepth = 3, + size_t maxMessageSize = MessageQueueMessage::MAX_MESSAGE_SIZE ); + + /** Copying message queues forbidden */ + MessageQueue(const MessageQueue&) = delete; + MessageQueue& operator=(const MessageQueue&) = delete; + + /** + * @brief The destructor deletes the formerly created message queue. + * @details This is accomplished by using the delete call provided + * by the operating system. + */ + virtual ~MessageQueue(); + + /** + * This function is used to switch the call context. This has to be called + * if a message is sent or received from an ISR! + * @param callContext + */ + void switchSystemContext(CallContext callContext); + + /** MessageQueueIF implementation */ + ReturnValue_t sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault = false) override; + + ReturnValue_t sendToDefault(MessageQueueMessageIF* message) override; + + ReturnValue_t reply(MessageQueueMessageIF* message) override; + virtual ReturnValue_t sendMessageFrom(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, + MessageQueueId_t sentFrom = NO_QUEUE, + bool ignoreFault = false) override; + + virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message, + MessageQueueId_t sentFrom = NO_QUEUE, + bool ignoreFault = false) override; + + ReturnValue_t receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t *receivedFrom) override; + + ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override; + + ReturnValue_t flush(uint32_t* count) override; + + MessageQueueId_t getLastPartner() const override; + + MessageQueueId_t getId() const override; + + void setDefaultDestination(MessageQueueId_t defaultDestination) override; + + MessageQueueId_t getDefaultDestination() const override; + + bool isDefaultDestinationSet() const override; + +protected: + /** + * @brief Implementation to be called from any send Call within + * MessageQueue and MessageQueueSenderIF. + * @details + * This method takes the message provided, adds the sentFrom information and + * passes it on to the destination provided with an operating system call. + * The OS's return value is returned. + * @param sendTo + * This parameter specifies the message queue id to send the message to. + * @param message + * This is a pointer to a previously created message, which is sent. + * @param sentFrom + * The sentFrom information can be set to inject the sender's queue id into + * the message. This variable is set to zero by default. + * @param ignoreFault + * If set to true, the internal software fault counter is not incremented + * if queue is full. + * @param context Specify whether call is made from task or from an ISR. + */ + static ReturnValue_t sendMessageFromMessageQueue(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE, + bool ignoreFault=false, CallContext callContext = CallContext::TASK); + + static ReturnValue_t handleSendResult(BaseType_t result, bool ignoreFault); + +private: + bool defaultDestinationSet = false; + QueueHandle_t handle; + MessageQueueId_t defaultDestination = MessageQueueIF::NO_QUEUE; + MessageQueueId_t lastPartner = MessageQueueIF::NO_QUEUE; + const size_t maxMessageSize; + //! Stores the current system context + CallContext callContext = CallContext::TASK; +}; + +#endif /* FSFW_OSAL_FREERTOS_MESSAGEQUEUE_H_ */ diff --git a/fsfw/osal/FreeRTOS/Mutex.cpp b/fsfw/osal/FreeRTOS/Mutex.cpp new file mode 100644 index 0000000..9d9638b --- /dev/null +++ b/fsfw/osal/FreeRTOS/Mutex.cpp @@ -0,0 +1,51 @@ +#include "Mutex.h" + +#include "../../serviceinterface/ServiceInterfaceStream.h" + +Mutex::Mutex() { + handle = xSemaphoreCreateMutex(); + if(handle == nullptr) { + sif::error << "Mutex::Mutex(FreeRTOS): Creation failure" << std::endl; + } +} + +Mutex::~Mutex() { + if (handle != nullptr) { + vSemaphoreDelete(handle); + } + +} + +ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType, + uint32_t timeoutMs) { + if (handle == nullptr) { + return MutexIF::MUTEX_NOT_FOUND; + } + // If the timeout type is BLOCKING, this will be the correct value. + uint32_t timeout = portMAX_DELAY; + if(timeoutType == TimeoutType::POLLING) { + timeout = 0; + } + else if(timeoutType == TimeoutType::WAITING){ + timeout = pdMS_TO_TICKS(timeoutMs); + } + + BaseType_t returncode = xSemaphoreTake(handle, timeout); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } else { + return MutexIF::MUTEX_TIMEOUT; + } +} + +ReturnValue_t Mutex::unlockMutex() { + if (handle == nullptr) { + return MutexIF::MUTEX_NOT_FOUND; + } + BaseType_t returncode = xSemaphoreGive(handle); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } else { + return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX; + } +} diff --git a/fsfw/osal/FreeRTOS/Mutex.h b/fsfw/osal/FreeRTOS/Mutex.h new file mode 100644 index 0000000..156d431 --- /dev/null +++ b/fsfw/osal/FreeRTOS/Mutex.h @@ -0,0 +1,29 @@ +#ifndef FRAMEWORK_FREERTOS_MUTEX_H_ +#define FRAMEWORK_FREERTOS_MUTEX_H_ + +#include "../../ipc/MutexIF.h" + +#include +#include + +/** + * @brief OS component to implement MUTual EXclusion + * + * @details + * Mutexes are binary semaphores which include a priority inheritance mechanism. + * Documentation: https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html + * @ingroup osal + */ +class Mutex : public MutexIF { +public: + Mutex(); + ~Mutex(); + ReturnValue_t lockMutex(TimeoutType timeoutType, + uint32_t timeoutMs) override; + ReturnValue_t unlockMutex() override; + +private: + SemaphoreHandle_t handle; +}; + +#endif /* FRAMEWORK_FREERTOS_MUTEX_H_ */ diff --git a/fsfw/osal/FreeRTOS/MutexFactory.cpp b/fsfw/osal/FreeRTOS/MutexFactory.cpp new file mode 100644 index 0000000..75b63d0 --- /dev/null +++ b/fsfw/osal/FreeRTOS/MutexFactory.cpp @@ -0,0 +1,28 @@ +#include "../../ipc/MutexFactory.h" + +#include "../FreeRTOS/Mutex.h" + +//TODO: Different variant than the lazy loading in QueueFactory. What's better and why? -> one is on heap the other on bss/data +//MutexFactory* MutexFactory::factoryInstance = new MutexFactory(); +MutexFactory* MutexFactory::factoryInstance = NULL; + +MutexFactory::MutexFactory() { +} + +MutexFactory::~MutexFactory() { +} + +MutexFactory* MutexFactory::instance() { + if (factoryInstance == NULL){ + factoryInstance = new MutexFactory(); + } + return MutexFactory::factoryInstance; +} + +MutexIF* MutexFactory::createMutex() { + return new Mutex(); +} + +void MutexFactory::deleteMutex(MutexIF* mutex) { + delete mutex; +} diff --git a/fsfw/osal/FreeRTOS/PeriodicTask.cpp b/fsfw/osal/FreeRTOS/PeriodicTask.cpp new file mode 100644 index 0000000..5c0a840 --- /dev/null +++ b/fsfw/osal/FreeRTOS/PeriodicTask.cpp @@ -0,0 +1,139 @@ +#include "PeriodicTask.h" + +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tasks/ExecutableObjectIF.h" + +PeriodicTask::PeriodicTask(const char *name, TaskPriority setPriority, + TaskStackSize setStack, TaskPeriod setPeriod, + TaskDeadlineMissedFunction deadlineMissedFunc) : + started(false), handle(NULL), period(setPeriod), deadlineMissedFunc( + deadlineMissedFunc) +{ + configSTACK_DEPTH_TYPE stackSize = setStack / sizeof(configSTACK_DEPTH_TYPE); + BaseType_t status = xTaskCreate(taskEntryPoint, name, + stackSize, this, setPriority, &handle); + if(status != pdPASS){ + sif::debug << "PeriodicTask Insufficient heap memory remaining. " + "Status: " << status << std::endl; + } + +} + +PeriodicTask::~PeriodicTask(void) { + //Do not delete objects, we were responsible for ptrs only. +} + +void PeriodicTask::taskEntryPoint(void* argument) { + // The argument is re-interpreted as PeriodicTask. The Task object is + // global, so it is found from any place. + PeriodicTask *originalTask(reinterpret_cast(argument)); + /* Task should not start until explicitly requested, + * but in FreeRTOS, tasks start as soon as they are created if the scheduler + * is running but not if the scheduler is not running. + * To be able to accommodate both cases we check a member which is set in + * #startTask(). If it is not set and we get here, the scheduler was started + * before #startTask() was called and we need to suspend if it is set, + * the scheduler was not running before #startTask() was called and we + * can continue */ + + if (not originalTask->started) { + vTaskSuspend(NULL); + } + + originalTask->taskFunctionality(); + sif::debug << "Polling task " << originalTask->handle + << " returned from taskFunctionality." << std::endl; +} + +ReturnValue_t PeriodicTask::startTask() { + started = true; + + // We must not call resume if scheduler is not started yet + if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { + vTaskResume(handle); + } + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t PeriodicTask::sleepFor(uint32_t ms) { + vTaskDelay(pdMS_TO_TICKS(ms)); + return HasReturnvaluesIF::RETURN_OK; +} + +void PeriodicTask::taskFunctionality() { + TickType_t xLastWakeTime; + const TickType_t xPeriod = pdMS_TO_TICKS(this->period * 1000.); + + for (auto const &object: objectList) { + object->initializeAfterTaskCreation(); + } + + /* The xLastWakeTime variable needs to be initialized with the current tick + count. Note that this is the only time the variable is written to + explicitly. After this assignment, xLastWakeTime is updated automatically + internally within vTaskDelayUntil(). */ + xLastWakeTime = xTaskGetTickCount(); + /* Enter the loop that defines the task behavior. */ + for (;;) { + for (auto const& object: objectList) { + object->performOperation(); + } + + checkMissedDeadline(xLastWakeTime, xPeriod); + + vTaskDelayUntil(&xLastWakeTime, xPeriod); + + } +} + +ReturnValue_t PeriodicTask::addComponent(object_id_t object) { + ExecutableObjectIF* newObject = objectManager->get( + object); + if (newObject == nullptr) { + sif::error << "PeriodicTask::addComponent: Invalid object. Make sure" + "it implement ExecutableObjectIF" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + objectList.push_back(newObject); + newObject->setTaskIF(this); + + return HasReturnvaluesIF::RETURN_OK; +} + +uint32_t PeriodicTask::getPeriodMs() const { + return period * 1000; +} + +void PeriodicTask::checkMissedDeadline(const TickType_t xLastWakeTime, + const TickType_t interval) { + /* Check whether deadline was missed while also taking overflows + * into account. Drawing this on paper with a timeline helps to understand + * it. */ + TickType_t currentTickCount = xTaskGetTickCount(); + TickType_t timeToWake = xLastWakeTime + interval; + // Time to wake has not overflown. + if(timeToWake > xLastWakeTime) { + /* If the current time has overflown exclusively or the current + * tick count is simply larger than the time to wake, a deadline was + * missed */ + if((currentTickCount < xLastWakeTime) or (currentTickCount > timeToWake)) { + handleMissedDeadline(); + } + } + /* Time to wake has overflown. A deadline was missed if the current time + * is larger than the time to wake */ + else if((timeToWake < xLastWakeTime) and (currentTickCount > timeToWake)) { + handleMissedDeadline(); + } +} + +TaskHandle_t PeriodicTask::getTaskHandle() { + return handle; +} + +void PeriodicTask::handleMissedDeadline() { + if(deadlineMissedFunc != nullptr) { + this->deadlineMissedFunc(); + } +} diff --git a/fsfw/osal/FreeRTOS/PeriodicTask.h b/fsfw/osal/FreeRTOS/PeriodicTask.h new file mode 100644 index 0000000..87b8b6d --- /dev/null +++ b/fsfw/osal/FreeRTOS/PeriodicTask.h @@ -0,0 +1,126 @@ +#ifndef FSFW_OSAL_FREERTOS_PERIODICTASK_H_ +#define FSFW_OSAL_FREERTOS_PERIODICTASK_H_ + +#include "FreeRTOSTaskIF.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../tasks/PeriodicTaskIF.h" +#include "../../tasks/Typedef.h" + +#include +#include + +#include + +class ExecutableObjectIF; + +/** + * @brief This class represents a specialized task for + * periodic activities of multiple objects. + * @ingroup task_handling + */ +class PeriodicTask: public PeriodicTaskIF, public FreeRTOSTaskIF { +public: + /** + * Keep in Mind that you need to call before this vTaskStartScheduler()! + * A lot of task parameters are set in "FreeRTOSConfig.h". + * @details + * The class is initialized without allocated objects. + * These need to be added with #addComponent. + * @param priority + * Sets the priority of a task. Values depend on freeRTOS configuration, + * high number means high priority. + * @param stack_size + * The stack size reserved by the operating system for the task. + * @param setPeriod + * The length of the period with which the task's + * functionality will be executed. It is expressed in clock ticks. + * @param setDeadlineMissedFunc + * The function pointer to the deadline missed function that shall + * be assigned. + */ + PeriodicTask(TaskName name, TaskPriority setPriority, + TaskStackSize setStack, TaskPeriod setPeriod, + TaskDeadlineMissedFunction deadlineMissedFunc); + /** + * @brief Currently, the executed object's lifetime is not coupled with + * the task object's lifetime, so the destructor is empty. + */ + virtual ~PeriodicTask(void); + + /** + * @brief The method to start the task. + * @details The method starts the task with the respective system call. + * Entry point is the taskEntryPoint method described below. + * The address of the task object is passed as an argument + * to the system call. + */ + ReturnValue_t startTask() override; + /** + * Adds an object to the list of objects to be executed. + * The objects are executed in the order added. + * @param object Id of the object to add. + * @return + * -@c RETURN_OK on success + * -@c RETURN_FAILED if the object could not be added. + */ + ReturnValue_t addComponent(object_id_t object) override; + + uint32_t getPeriodMs() const override; + + ReturnValue_t sleepFor(uint32_t ms) override; + + TaskHandle_t getTaskHandle() override; +protected: + bool started; + TaskHandle_t handle; + + //! Typedef for the List of objects. + typedef std::vector ObjectList; + /** + * @brief This attribute holds a list of objects to be executed. + */ + ObjectList objectList; + /** + * @brief The period of the task. + * @details + * The period determines the frequency of the task's execution. + * It is expressed in clock ticks. + */ + TaskPeriod period; + /** + * @brief The pointer to the deadline-missed function. + * @details + * This pointer stores the function that is executed if the task's deadline + * is missed so each may react individually on a timing failure. + * The pointer may be NULL, then nothing happens on missing the deadline. + * The deadline is equal to the next execution of the periodic task. + */ + void (*deadlineMissedFunc)(void); + /** + * @brief This is the function executed in the new task's context. + * @details + * It converts the argument back to the thread object type and copies the + * class instance to the task context. The taskFunctionality method is + * called afterwards. + * @param A pointer to the task object itself is passed as argument. + */ + + static void taskEntryPoint(void* argument); + /** + * @brief The function containing the actual functionality of the task. + * @details + * The method sets and starts the task's period, then enters a loop that is + * repeated as long as the isRunning attribute is true. Within the loop, + * all performOperation methods of the added objects are called. + * Afterwards the checkAndRestartPeriod system call blocks the task until + * the next period. + * On missing the deadline, the deadlineMissedFunction is executed. + */ + void taskFunctionality(void); + + void checkMissedDeadline(const TickType_t xLastWakeTime, + const TickType_t interval); + void handleMissedDeadline(); +}; + +#endif /* FSFW_OSAL_FREERTOS_PERIODICTASK_H_ */ diff --git a/fsfw/osal/FreeRTOS/QueueFactory.cpp b/fsfw/osal/FreeRTOS/QueueFactory.cpp new file mode 100644 index 0000000..ed29e10 --- /dev/null +++ b/fsfw/osal/FreeRTOS/QueueFactory.cpp @@ -0,0 +1,37 @@ +#include "MessageQueue.h" + +#include "../../ipc/MessageQueueSenderIF.h" +#include "../../ipc/QueueFactory.h" + + +QueueFactory* QueueFactory::factoryInstance = nullptr; + + +ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + return MessageQueue::sendMessageFromMessageQueue(sendTo,message, + sentFrom,ignoreFault); +} + +QueueFactory* QueueFactory::instance() { + if (factoryInstance == nullptr) { + factoryInstance = new QueueFactory; + } + return factoryInstance; +} + +QueueFactory::QueueFactory() { +} + +QueueFactory::~QueueFactory() { +} + +MessageQueueIF* QueueFactory::createMessageQueue(uint32_t messageDepth, + size_t maxMessageSize) { + return new MessageQueue(messageDepth, maxMessageSize); +} + +void QueueFactory::deleteMessageQueue(MessageQueueIF* queue) { + delete queue; +} diff --git a/fsfw/osal/FreeRTOS/README.md b/fsfw/osal/FreeRTOS/README.md new file mode 100644 index 0000000..823642c --- /dev/null +++ b/fsfw/osal/FreeRTOS/README.md @@ -0,0 +1,14 @@ +FreeRTOS Readme += + +## Main.cpp Notices + +### Tasks + +A FreeRTOS application needs to start + +> vTaskStartScheduler() + +before creating Tasks. +Keep this in mind for the mission dependent code! +This has to be done before the Task Factory is used. \ No newline at end of file diff --git a/fsfw/osal/FreeRTOS/SemaphoreFactory.cpp b/fsfw/osal/FreeRTOS/SemaphoreFactory.cpp new file mode 100644 index 0000000..beb0d09 --- /dev/null +++ b/fsfw/osal/FreeRTOS/SemaphoreFactory.cpp @@ -0,0 +1,59 @@ +#include "../../osal/FreeRTOS/BinarySemaphore.h" +#include "../../osal/FreeRTOS/BinSemaphUsingTask.h" +#include "../../osal/FreeRTOS/CountingSemaphore.h" +#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h" +#include "../../tasks/SemaphoreFactory.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr; + +static const uint32_t USE_REGULAR_SEMAPHORES = 0; +static const uint32_t USE_TASK_NOTIFICATIONS = 1; + +SemaphoreFactory::SemaphoreFactory() { +} + +SemaphoreFactory::~SemaphoreFactory() { + delete factoryInstance; +} + +SemaphoreFactory* SemaphoreFactory::instance() { + if (factoryInstance == nullptr){ + factoryInstance = new SemaphoreFactory(); + } + return SemaphoreFactory::factoryInstance; +} + +SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t argument) { + if(argument == USE_REGULAR_SEMAPHORES) { + return new BinarySemaphore(); + } + else if(argument == USE_TASK_NOTIFICATIONS) { + return new BinarySemaphoreUsingTask(); + } + else { + sif::warning << "SemaphoreFactory: Invalid argument, return regular" + "binary semaphore" << std::endl; + return new BinarySemaphore(); + } +} + +SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t maxCount, + uint8_t initCount, uint32_t argument) { + if(argument == USE_REGULAR_SEMAPHORES) { + return new CountingSemaphore(maxCount, initCount); + } + else if(argument == USE_TASK_NOTIFICATIONS) { + return new CountingSemaphoreUsingTask(maxCount, initCount); + } + else { + sif::warning << "SemaphoreFactory: Invalid argument, return regular" + "binary semaphore" << std::endl; + return new CountingSemaphore(maxCount, initCount); + } + +} + +void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) { + delete semaphore; +} diff --git a/fsfw/osal/FreeRTOS/TaskFactory.cpp b/fsfw/osal/FreeRTOS/TaskFactory.cpp new file mode 100644 index 0000000..80df38b --- /dev/null +++ b/fsfw/osal/FreeRTOS/TaskFactory.cpp @@ -0,0 +1,55 @@ +#include "../../tasks/TaskFactory.h" +#include "../../returnvalues/HasReturnvaluesIF.h" + +#include "PeriodicTask.h" +#include "FixedTimeslotTask.h" + + +TaskFactory* TaskFactory::factoryInstance = new TaskFactory(); + +TaskFactory::~TaskFactory() { +} + +TaskFactory* TaskFactory::instance() { + return TaskFactory::factoryInstance; +} +/*** + * Keep in Mind that you need to call before this vTaskStartScheduler()! + * High taskPriority_ number means high priority. + */ +PeriodicTaskIF* TaskFactory::createPeriodicTask(TaskName name_, + TaskPriority taskPriority_, TaskStackSize stackSize_, + TaskPeriod period_, + TaskDeadlineMissedFunction deadLineMissedFunction_) { + return (PeriodicTaskIF*) (new PeriodicTask(name_, taskPriority_, stackSize_, + period_, deadLineMissedFunction_)); +} +/*** + * Keep in Mind that you need to call before this vTaskStartScheduler()! + */ +FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(TaskName name_, + TaskPriority taskPriority_, TaskStackSize stackSize_, + TaskPeriod period_, + TaskDeadlineMissedFunction deadLineMissedFunction_) { + return (FixedTimeslotTaskIF*) (new FixedTimeslotTask(name_, taskPriority_, + stackSize_, period_, deadLineMissedFunction_)); +} + +ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) { + if (task == NULL) { + //delete self + vTaskDelete(NULL); + return HasReturnvaluesIF::RETURN_OK; + } else { + //TODO not implemented + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t TaskFactory::delayTask(uint32_t delayMs) { + vTaskDelay(pdMS_TO_TICKS(delayMs)); + return HasReturnvaluesIF::RETURN_OK; +} + +TaskFactory::TaskFactory() { +} diff --git a/fsfw/osal/FreeRTOS/TaskManagement.cpp b/fsfw/osal/FreeRTOS/TaskManagement.cpp new file mode 100644 index 0000000..b77f12a --- /dev/null +++ b/fsfw/osal/FreeRTOS/TaskManagement.cpp @@ -0,0 +1,24 @@ +#include "../../osal/FreeRTOS/TaskManagement.h" + +void TaskManagement::vRequestContextSwitchFromTask() { + vTaskDelay(0); +} + +void TaskManagement::requestContextSwitch( + CallContext callContext = CallContext::TASK) { + if(callContext == CallContext::ISR) { + // This function depends on the partmacro.h definition for the specific device + vRequestContextSwitchFromISR(); + } else { + vRequestContextSwitchFromTask(); + } +} + +TaskHandle_t TaskManagement::getCurrentTaskHandle() { + return xTaskGetCurrentTaskHandle(); +} + +size_t TaskManagement::getTaskStackHighWatermark( + TaskHandle_t task) { + return uxTaskGetStackHighWaterMark(task) * sizeof(StackType_t); +} diff --git a/fsfw/osal/FreeRTOS/TaskManagement.h b/fsfw/osal/FreeRTOS/TaskManagement.h new file mode 100644 index 0000000..4b7fe3e --- /dev/null +++ b/fsfw/osal/FreeRTOS/TaskManagement.h @@ -0,0 +1,64 @@ +#ifndef FRAMEWORK_OSAL_FREERTOS_TASKMANAGEMENT_H_ +#define FRAMEWORK_OSAL_FREERTOS_TASKMANAGEMENT_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" + +extern "C" { +#include +#include +} +#include + +/** + * Architecture dependant portmacro.h function call. + * Should be implemented in bsp. + */ +extern void vRequestContextSwitchFromISR(); + +/*! + * Used by functions to tell if they are being called from + * within an ISR or from a regular task. This is required because FreeRTOS + * has different functions for handling semaphores and messages from within + * an ISR and task. + */ +enum class CallContext { + TASK = 0x00,//!< task_context + ISR = 0xFF //!< isr_context +}; + + +class TaskManagement { +public: + /** + * @brief In this function, a function dependant on the portmacro.h header + * function calls to request a context switch can be specified. + * This can be used if sending to the queue from an ISR caused a task + * to unblock and a context switch is required. + */ + static void requestContextSwitch(CallContext callContext); + + /** + * If task preemption in FreeRTOS is disabled, a context switch + * can be requested manually by calling this function. + */ + static void vRequestContextSwitchFromTask(void); + + /** + * @return The current task handle + */ + static TaskHandle_t getCurrentTaskHandle(); + + /** + * Get returns the minimum amount of remaining stack space in words + * that was a available to the task since the task started executing. + * Please note that the actual value in bytes depends + * on the stack depth type. + * E.g. on a 32 bit machine, a value of 200 means 800 bytes. + * @return Smallest value of stack remaining since the task was started in + * words. + */ + static size_t getTaskStackHighWatermark( + TaskHandle_t task = nullptr); +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_TASKMANAGEMENT_H_ */ diff --git a/fsfw/osal/FreeRTOS/Timekeeper.cpp b/fsfw/osal/FreeRTOS/Timekeeper.cpp new file mode 100644 index 0000000..1031f0c --- /dev/null +++ b/fsfw/osal/FreeRTOS/Timekeeper.cpp @@ -0,0 +1,41 @@ +#include "Timekeeper.h" + +#include "FreeRTOSConfig.h" + +Timekeeper * Timekeeper::myinstance = nullptr; + +Timekeeper::Timekeeper() : offset( { 0, 0 } ) {} + +Timekeeper::~Timekeeper() {} + +const timeval& Timekeeper::getOffset() const { + return offset; +} + +Timekeeper* Timekeeper::instance() { + if (myinstance == nullptr) { + myinstance = new Timekeeper(); + } + return myinstance; +} + +void Timekeeper::setOffset(const timeval& offset) { + this->offset = offset; +} + +timeval Timekeeper::ticksToTimeval(TickType_t ticks) { + timeval uptime; + uptime.tv_sec = ticks / configTICK_RATE_HZ; + + //TODO explain, think about overflow + uint32_t subsecondTicks = ticks % configTICK_RATE_HZ; + uint64_t usecondTicks = subsecondTicks * 1000000; + + uptime.tv_usec = usecondTicks / configTICK_RATE_HZ; + + return uptime; +} + +TickType_t Timekeeper::getTicks() { + return xTaskGetTickCount(); +} diff --git a/fsfw/osal/FreeRTOS/Timekeeper.h b/fsfw/osal/FreeRTOS/Timekeeper.h new file mode 100644 index 0000000..7d583f7 --- /dev/null +++ b/fsfw/osal/FreeRTOS/Timekeeper.h @@ -0,0 +1,40 @@ +#ifndef FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_ +#define FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_ + +#include "../../timemanager/Clock.h" + +#include +#include + + +/** + * A Class to basically store the time difference between uptime and UTC + * so the "time-agnostic" FreeRTOS can keep an UTC Time + * + * Implemented as Singleton, so the FSFW Clock Implementation (see Clock.cpp) + * can use it without having a member. + */ + +class Timekeeper { +private: + Timekeeper(); + + timeval offset; + + static Timekeeper * myinstance; +public: + static Timekeeper * instance(); + virtual ~Timekeeper(); + + static timeval ticksToTimeval(TickType_t ticks); + /** + * Get elapsed time in system ticks. + * @return + */ + static TickType_t getTicks(); + + const timeval& getOffset() const; + void setOffset(const timeval& offset); +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_ */ diff --git a/fsfw/osal/InternalErrorCodes.h b/fsfw/osal/InternalErrorCodes.h new file mode 100644 index 0000000..1da116f --- /dev/null +++ b/fsfw/osal/InternalErrorCodes.h @@ -0,0 +1,39 @@ +#ifndef INTERNALERRORCODES_H_ +#define INTERNALERRORCODES_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" + +class InternalErrorCodes { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::INTERNAL_ERROR_CODES; + +static const ReturnValue_t NO_CONFIGURATION_TABLE = MAKE_RETURN_CODE(0x01 ); +static const ReturnValue_t NO_CPU_TABLE = MAKE_RETURN_CODE(0x02 ); +static const ReturnValue_t INVALID_WORKSPACE_ADDRESS = MAKE_RETURN_CODE(0x03 ); +static const ReturnValue_t TOO_LITTLE_WORKSPACE = MAKE_RETURN_CODE(0x04 ); +static const ReturnValue_t WORKSPACE_ALLOCATION = MAKE_RETURN_CODE(0x05 ); +static const ReturnValue_t INTERRUPT_STACK_TOO_SMALL = MAKE_RETURN_CODE(0x06 ); +static const ReturnValue_t THREAD_EXITTED = MAKE_RETURN_CODE(0x07 ); +static const ReturnValue_t INCONSISTENT_MP_INFORMATION = MAKE_RETURN_CODE(0x08 ); +static const ReturnValue_t INVALID_NODE = MAKE_RETURN_CODE(0x09 ); +static const ReturnValue_t NO_MPCI = MAKE_RETURN_CODE(0x0a ); +static const ReturnValue_t BAD_PACKET = MAKE_RETURN_CODE(0x0b ); +static const ReturnValue_t OUT_OF_PACKETS = MAKE_RETURN_CODE(0x0c ); +static const ReturnValue_t OUT_OF_GLOBAL_OBJECTS = MAKE_RETURN_CODE(0x0d ); +static const ReturnValue_t OUT_OF_PROXIES = MAKE_RETURN_CODE(0x0e ); +static const ReturnValue_t INVALID_GLOBAL_ID = MAKE_RETURN_CODE(0x0f ); +static const ReturnValue_t BAD_STACK_HOOK = MAKE_RETURN_CODE(0x10 ); +static const ReturnValue_t BAD_ATTRIBUTES = MAKE_RETURN_CODE(0x11 ); +static const ReturnValue_t IMPLEMENTATION_KEY_CREATE_INCONSISTENCY = MAKE_RETURN_CODE(0x12 ); +static const ReturnValue_t IMPLEMENTATION_BLOCKING_OPERATION_CANCEL = MAKE_RETURN_CODE(0x13 ); +static const ReturnValue_t MUTEX_OBTAIN_FROM_BAD_STATE = MAKE_RETURN_CODE(0x14 ); +static const ReturnValue_t UNLIMITED_AND_MAXIMUM_IS_0 = MAKE_RETURN_CODE(0x15 ); + + virtual ~InternalErrorCodes(); + + static ReturnValue_t translate(uint8_t code); +private: + InternalErrorCodes(); +}; + +#endif /* INTERNALERRORCODES_H_ */ diff --git a/fsfw/osal/host/Clock.cpp b/fsfw/osal/host/Clock.cpp new file mode 100644 index 0000000..41321ee --- /dev/null +++ b/fsfw/osal/host/Clock.cpp @@ -0,0 +1,227 @@ +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../timemanager/Clock.h" + +#include +#if defined(WIN32) +#include +#elif defined(LINUX) +#include +#endif + +uint16_t Clock::leapSeconds = 0; +MutexIF* Clock::timeMutex = NULL; + +using SystemClock = std::chrono::system_clock; + +uint32_t Clock::getTicksPerSecond(void){ + sif::warning << "Clock::getTicksPerSecond: not implemented yet" << std::endl; + return 0; + //return CLOCKS_PER_SEC; + //uint32_t ticks = sysconf(_SC_CLK_TCK); + //return ticks; +} + +ReturnValue_t Clock::setClock(const TimeOfDay_t* time) { + // do some magic with chrono + sif::warning << "Clock::setClock: not implemented yet" << std::endl; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::setClock(const timeval* time) { + // do some magic with chrono +#if defined(WIN32) + return HasReturnvaluesIF::RETURN_OK; +#elif defined(LINUX) + return HasReturnvaluesIF::RETURN_OK; +#else + +#endif + sif::warning << "Clock::getUptime: Not implemented for found OS" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t Clock::getClock_timeval(timeval* time) { +#if defined(WIN32) + auto now = std::chrono::system_clock::now(); + auto secondsChrono = std::chrono::time_point_cast(now); + auto epoch = now.time_since_epoch(); + time->tv_sec = std::chrono::duration_cast(epoch).count(); + auto fraction = now - secondsChrono; + time->tv_usec = std::chrono::duration_cast( + fraction).count(); + return HasReturnvaluesIF::RETURN_OK; +#elif defined(LINUX) + timespec timeUnix; + int status = clock_gettime(CLOCK_REALTIME,&timeUnix); + if(status!=0){ + return HasReturnvaluesIF::RETURN_FAILED; + } + time->tv_sec = timeUnix.tv_sec; + time->tv_usec = timeUnix.tv_nsec / 1000.0; + return HasReturnvaluesIF::RETURN_OK; +#else + sif::warning << "Clock::getUptime: Not implemented for found OS" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; +#endif + +} + +ReturnValue_t Clock::getClock_usecs(uint64_t* time) { + // do some magic with chrono + sif::warning << "Clock::gerClock_usecs: not implemented yet" << std::endl; + return HasReturnvaluesIF::RETURN_OK; +} + +timeval Clock::getUptime() { + timeval timeval; +#if defined(WIN32) + auto uptime = std::chrono::milliseconds(GetTickCount64()); + auto secondsChrono = std::chrono::duration_cast(uptime); + timeval.tv_sec = secondsChrono.count(); + auto fraction = uptime - secondsChrono; + timeval.tv_usec = std::chrono::duration_cast( + fraction).count(); +#elif defined(LINUX) + double uptimeSeconds; + if (std::ifstream("/proc/uptime", std::ios::in) >> uptimeSeconds) + { + // value is rounded down automatically + timeval.tv_sec = uptimeSeconds; + timeval.tv_usec = uptimeSeconds *(double) 1e6 - (timeval.tv_sec *1e6); + } +#else + sif::warning << "Clock::getUptime: Not implemented for found OS" << std::endl; +#endif + return timeval; +} + +ReturnValue_t Clock::getUptime(timeval* uptime) { + *uptime = getUptime(); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) { + timeval uptime = getUptime(); + *uptimeMs = uptime.tv_sec * 1000 + uptime.tv_usec / 1000; + return HasReturnvaluesIF::RETURN_OK; +} + + +ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) { + // do some magic with chrono (C++20!) + // Right now, the library doesn't have the new features yet. + // so we work around that for now. + auto now = SystemClock::now(); + auto seconds = std::chrono::time_point_cast(now); + auto fraction = now - seconds; + time_t tt = SystemClock::to_time_t(now); + struct tm* timeInfo; + timeInfo = gmtime(&tt); + time->year = timeInfo->tm_year + 1900; + time->month = timeInfo->tm_mon+1; + time->day = timeInfo->tm_mday; + time->hour = timeInfo->tm_hour; + time->minute = timeInfo->tm_min; + time->second = timeInfo->tm_sec; + auto usecond = std::chrono::duration_cast(fraction); + time->usecond = usecond.count(); + + //sif::warning << "Clock::getDateAndTime: not implemented yet" << std::endl; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from, + timeval* to) { + struct tm time_tm; + + time_tm.tm_year = from->year - 1900; + time_tm.tm_mon = from->month - 1; + time_tm.tm_mday = from->day; + + time_tm.tm_hour = from->hour; + time_tm.tm_min = from->minute; + time_tm.tm_sec = from->second; + + time_t seconds = mktime(&time_tm); + + to->tv_sec = seconds; + to->tv_usec = from->usecond; + //Fails in 2038.. + return HasReturnvaluesIF::RETURN_OK; + sif::warning << "Clock::convertTimeBla: not implemented yet" << std::endl; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) { + *JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24. + / 3600.; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) { + //SHOULDDO: works not for dates in the past (might have less leap seconds) + if (timeMutex == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + + uint16_t leapSeconds; + ReturnValue_t result = getLeapSeconds(&leapSeconds); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + timeval leapSeconds_timeval = { 0, 0 }; + leapSeconds_timeval.tv_sec = leapSeconds; + + //initial offset between UTC and TAI + timeval UTCtoTAI1972 = { 10, 0 }; + + timeval TAItoTT = { 32, 184000 }; + + *tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) { + if(checkOrCreateClockMutex()!=HasReturnvaluesIF::RETURN_OK){ + return HasReturnvaluesIF::RETURN_FAILED; + } + ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + leapSeconds = leapSeconds_; + + result = timeMutex->unlockMutex(); + return result; +} + +ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) { + if(timeMutex == nullptr){ + return HasReturnvaluesIF::RETURN_FAILED; + } + ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + *leapSeconds_ = leapSeconds; + + result = timeMutex->unlockMutex(); + return result; +} + +ReturnValue_t Clock::checkOrCreateClockMutex(){ + if(timeMutex == nullptr){ + MutexFactory* mutexFactory = MutexFactory::instance(); + if (mutexFactory == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + timeMutex = mutexFactory->createMutex(); + if (timeMutex == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/osal/host/FixedTimeslotTask.cpp b/fsfw/osal/host/FixedTimeslotTask.cpp new file mode 100644 index 0000000..e78c974 --- /dev/null +++ b/fsfw/osal/host/FixedTimeslotTask.cpp @@ -0,0 +1,197 @@ +#include "../../osal/host/FixedTimeslotTask.h" + +#include "../../ipc/MutexFactory.h" +#include "../../osal/host/Mutex.h" +#include "../../osal/host/FixedTimeslotTask.h" + +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tasks/ExecutableObjectIF.h" + +#include +#include + +#if defined(WIN32) +#include +#elif defined(LINUX) +#include +#endif + +FixedTimeslotTask::FixedTimeslotTask(const char *name, TaskPriority setPriority, + TaskStackSize setStack, TaskPeriod setPeriod, + void (*setDeadlineMissedFunc)()) : + started(false), pollingSeqTable(setPeriod*1000), taskName(name), + period(setPeriod), deadlineMissedFunc(setDeadlineMissedFunc) { + // It is propably possible to set task priorities by using the native + // task handles for Windows / Linux + mainThread = std::thread(&FixedTimeslotTask::taskEntryPoint, this, this); +#if defined(WIN32) + /* List of possible priority classes: + * https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ + * nf-processthreadsapi-setpriorityclass + * And respective thread priority numbers: + * https://docs.microsoft.com/en-us/windows/ + * win32/procthread/scheduling-priorities */ + int result = SetPriorityClass( + reinterpret_cast(mainThread.native_handle()), + ABOVE_NORMAL_PRIORITY_CLASS); + if(result != 0) { + sif::error << "FixedTimeslotTask: Windows SetPriorityClass failed with code " + << GetLastError() << std::endl; + } + result = SetThreadPriority( + reinterpret_cast(mainThread.native_handle()), + THREAD_PRIORITY_NORMAL); + if(result != 0) { + sif::error << "FixedTimeslotTask: Windows SetPriorityClass failed with code " + << GetLastError() << std::endl; + } +#elif defined(LINUX) + // we can just copy and paste the code from linux here. +#endif +} + +FixedTimeslotTask::~FixedTimeslotTask(void) { + //Do not delete objects, we were responsible for ptrs only. + terminateThread = true; + if(mainThread.joinable()) { + mainThread.join(); + } + delete this; +} + +void FixedTimeslotTask::taskEntryPoint(void* argument) { + FixedTimeslotTask *originalTask(reinterpret_cast(argument)); + + if (not originalTask->started) { + // we have to suspend/block here until the task is started. + // if semaphores are implemented, use them here. + std::unique_lock lock(initMutex); + initCondition.wait(lock); + } + + this->taskFunctionality(); + sif::debug << "FixedTimeslotTask::taskEntryPoint: " + "Returned from taskFunctionality." << std::endl; +} + +ReturnValue_t FixedTimeslotTask::startTask() { + started = true; + + // Notify task to start. + std::lock_guard lock(initMutex); + initCondition.notify_one(); + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t FixedTimeslotTask::sleepFor(uint32_t ms) { + std::this_thread::sleep_for(std::chrono::milliseconds(ms)); + return HasReturnvaluesIF::RETURN_OK; +} + +void FixedTimeslotTask::taskFunctionality() { + pollingSeqTable.intializeSequenceAfterTaskCreation(); + + // A local iterator for the Polling Sequence Table is created to + // find the start time for the first entry. + auto slotListIter = pollingSeqTable.current; + + // Get start time for first entry. + chron_ms interval(slotListIter->pollingTimeMs); + auto currentStartTime { + std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + }; + if(interval.count() > 0) { + delayForInterval(¤tStartTime, interval); + } + + /* Enter the loop that defines the task behavior. */ + for (;;) { + if(terminateThread.load()) { + break; + } + //The component for this slot is executed and the next one is chosen. + this->pollingSeqTable.executeAndAdvance(); + if (not pollingSeqTable.slotFollowsImmediately()) { + // we need to wait before executing the current slot + //this gives us the time to wait: + interval = chron_ms(this->pollingSeqTable.getIntervalToPreviousSlotMs()); + delayForInterval(¤tStartTime, interval); + //TODO deadline missed check + } + } +} + +ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId, + uint32_t slotTimeMs, int8_t executionStep) { + ExecutableObjectIF* executableObject = objectManager-> + get(componentId); + if (executableObject != nullptr) { + pollingSeqTable.addSlot(componentId, slotTimeMs, executionStep, + executableObject, this); + return HasReturnvaluesIF::RETURN_OK; + } + + sif::error << "Component " << std::hex << componentId << + " not found, not adding it to pst" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t FixedTimeslotTask::checkSequence() const { + return pollingSeqTable.checkSequence(); +} + +uint32_t FixedTimeslotTask::getPeriodMs() const { + return period * 1000; +} + +bool FixedTimeslotTask::delayForInterval(chron_ms * previousWakeTimeMs, + const chron_ms interval) { + bool shouldDelay = false; + //Get current wakeup time + auto currentStartTime = + std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()); + /* Generate the tick time at which the task wants to wake. */ + auto nextTimeToWake_ms = (*previousWakeTimeMs) + interval; + + if (currentStartTime < *previousWakeTimeMs) { + /* The tick count has overflowed since this function was + lasted called. In this case the only time we should ever + actually delay is if the wake time has also overflowed, + and the wake time is greater than the tick time. When this + is the case it is as if neither time had overflowed. */ + if ((nextTimeToWake_ms < *previousWakeTimeMs) + && (nextTimeToWake_ms > currentStartTime)) { + shouldDelay = true; + } + } else { + /* The tick time has not overflowed. In this case we will + delay if either the wake time has overflowed, and/or the + tick time is less than the wake time. */ + if ((nextTimeToWake_ms < *previousWakeTimeMs) + || (nextTimeToWake_ms > currentStartTime)) { + shouldDelay = true; + } + } + + /* Update the wake time ready for the next call. */ + + (*previousWakeTimeMs) = nextTimeToWake_ms; + + if (shouldDelay) { + auto sleepTime = std::chrono::duration_cast( + nextTimeToWake_ms - currentStartTime); + std::this_thread::sleep_for(sleepTime); + return true; + } + //We are shifting the time in case the deadline was missed like rtems + (*previousWakeTimeMs) = currentStartTime; + return false; + +} + + + + diff --git a/fsfw/osal/host/FixedTimeslotTask.h b/fsfw/osal/host/FixedTimeslotTask.h new file mode 100644 index 0000000..9985e2e --- /dev/null +++ b/fsfw/osal/host/FixedTimeslotTask.h @@ -0,0 +1,130 @@ +#ifndef FRAMEWORK_OSAL_HOST_FIXEDTIMESLOTTASK_H_ +#define FRAMEWORK_OSAL_HOST_FIXEDTIMESLOTTASK_H_ + +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../tasks/FixedSlotSequence.h" +#include "../../tasks/FixedTimeslotTaskIF.h" +#include "../../tasks/Typedef.h" + +#include +#include +#include +#include + +class ExecutableObjectIF; + +/** + * @brief This class represents a task for periodic activities with multiple + * steps and strict timeslot requirements for these steps. + * @details + * @ingroup task_handling + */ +class FixedTimeslotTask: public FixedTimeslotTaskIF { +public: + /** + * @brief Standard constructor of the class. + * @details + * The class is initialized without allocated objects. These need to be + * added with #addComponent. + * @param priority + * @param stack_size + * @param setPeriod + * @param setDeadlineMissedFunc + * The function pointer to the deadline missed function that shall be + * assigned. + */ + FixedTimeslotTask(const char *name, TaskPriority setPriority, + TaskStackSize setStack, TaskPeriod setPeriod, + void (*setDeadlineMissedFunc)()); + /** + * @brief Currently, the executed object's lifetime is not coupled with + * the task object's lifetime, so the destructor is empty. + */ + virtual ~FixedTimeslotTask(void); + + /** + * @brief The method to start the task. + * @details The method starts the task with the respective system call. + * Entry point is the taskEntryPoint method described below. + * The address of the task object is passed as an argument + * to the system call. + */ + ReturnValue_t startTask(void); + + /** + * Add timeslot to the polling sequence table. + * @param componentId + * @param slotTimeMs + * @param executionStep + * @return + */ + ReturnValue_t addSlot(object_id_t componentId, + uint32_t slotTimeMs, int8_t executionStep); + + ReturnValue_t checkSequence() const override; + + uint32_t getPeriodMs() const; + + ReturnValue_t sleepFor(uint32_t ms); + +protected: + using chron_ms = std::chrono::milliseconds; + + bool started; + //!< Typedef for the List of objects. + typedef std::vector ObjectList; + std::thread mainThread; + std::atomic terminateThread = false; + + //! Polling sequence table which contains the object to execute + //! and information like the timeslots and the passed execution step. + FixedSlotSequence pollingSeqTable; + + std::condition_variable initCondition; + std::mutex initMutex; + std::string taskName; + /** + * @brief The period of the task. + * @details + * The period determines the frequency of the task's execution. + * It is expressed in clock ticks. + */ + TaskPeriod period; + + /** + * @brief The pointer to the deadline-missed function. + * @details + * This pointer stores the function that is executed if the task's deadline + * is missed. So, each may react individually on a timing failure. + * The pointer may be NULL, then nothing happens on missing the deadline. + * The deadline is equal to the next execution of the periodic task. + */ + void (*deadlineMissedFunc)(void); + /** + * @brief This is the function executed in the new task's context. + * @details + * It converts the argument back to the thread object type and copies the + * class instance to the task context. + * The taskFunctionality method is called afterwards. + * @param A pointer to the task object itself is passed as argument. + */ + + void taskEntryPoint(void* argument); + /** + * @brief The function containing the actual functionality of the task. + * @details + * The method sets and starts the task's period, then enters a loop that is + * repeated as long as the isRunning attribute is true. Within the loop, + * all performOperation methods of the added objects are called. Afterwards + * the checkAndRestartPeriod system call blocks the task until the next + * period. On missing the deadline, the deadlineMissedFunction is executed. + */ + void taskFunctionality(void); + + bool delayForInterval(chron_ms * previousWakeTimeMs, + const chron_ms interval); +}; + + + +#endif /* FRAMEWORK_OSAL_HOST_FIXEDTIMESLOTTASK_H_ */ diff --git a/fsfw/osal/host/MessageQueue.cpp b/fsfw/osal/host/MessageQueue.cpp new file mode 100644 index 0000000..bced371 --- /dev/null +++ b/fsfw/osal/host/MessageQueue.cpp @@ -0,0 +1,159 @@ +#include "MessageQueue.h" +#include "QueueMapManager.h" + +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../ipc/MutexFactory.h" +#include "../../ipc/MutexHelper.h" + +MessageQueue::MessageQueue(size_t messageDepth, size_t maxMessageSize): + messageSize(maxMessageSize), messageDepth(messageDepth) { + queueLock = MutexFactory::instance()->createMutex(); + auto result = QueueMapManager::instance()->addMessageQueue(this, &mqId); + if(result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "MessageQueue::MessageQueue:" + << " Could not be created" << std::endl; + } +} + +MessageQueue::~MessageQueue() { + MutexFactory::instance()->deleteMutex(queueLock); +} + +ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault) { + return sendMessageFrom(sendTo, message, this->getId(), ignoreFault); +} + +ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessageIF* message) { + return sendToDefaultFrom(message, this->getId()); +} + +ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessageIF* message, + MessageQueueId_t sentFrom, bool ignoreFault) { + return sendMessageFrom(defaultDestination,message,sentFrom,ignoreFault); +} + +ReturnValue_t MessageQueue::reply(MessageQueueMessageIF* message) { + if (this->lastPartner != 0) { + return sendMessageFrom(this->lastPartner, message, this->getId()); + } else { + return MessageQueueIF::NO_REPLY_PARTNER; + } +} + +ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + return sendMessageFromMessageQueue(sendTo, message, sentFrom, + ignoreFault); +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t* receivedFrom) { + ReturnValue_t status = this->receiveMessage(message); + if(status == HasReturnvaluesIF::RETURN_OK) { + *receivedFrom = this->lastPartner; + } + return status; +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message) { + if(messageQueue.empty()) { + return MessageQueueIF::EMPTY; + } + // not sure this will work.. + //*message = std::move(messageQueue.front()); + MutexHelper mutexLock(queueLock, MutexIF::TimeoutType::WAITING, 20); + MessageQueueMessage* currentMessage = &messageQueue.front(); + std::copy(currentMessage->getBuffer(), + currentMessage->getBuffer() + messageSize, message->getBuffer()); + messageQueue.pop(); + // The last partner is the first uint32_t field in the message + this->lastPartner = message->getSender(); + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueId_t MessageQueue::getLastPartner() const { + return lastPartner; +} + +ReturnValue_t MessageQueue::flush(uint32_t* count) { + *count = messageQueue.size(); + // Clears the queue. + messageQueue = std::queue(); + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueId_t MessageQueue::getId() const { + return mqId; +} + +void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) { + defaultDestinationSet = true; + this->defaultDestination = defaultDestination; +} + +MessageQueueId_t MessageQueue::getDefaultDestination() const { + return defaultDestination; +} + +bool MessageQueue::isDefaultDestinationSet() const { + return defaultDestinationSet; +} + + +// static core function to send messages. +ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + if(message->getMessageSize() > message->getMaximumMessageSize()) { + // Actually, this should never happen or an error will be emitted + // in MessageQueueMessage. + // But I will still return a failure here. + return HasReturnvaluesIF::RETURN_FAILED; + } + MessageQueue* targetQueue = dynamic_cast( + QueueMapManager::instance()->getMessageQueue(sendTo)); + if(targetQueue == nullptr) { + if(not ignoreFault) { + InternalErrorReporterIF* internalErrorReporter = + objectManager->get( + objects::INTERNAL_ERROR_REPORTER); + if (internalErrorReporter != nullptr) { + internalErrorReporter->queueMessageNotSent(); + } + } + // TODO: Better returnvalue + return HasReturnvaluesIF::RETURN_FAILED; + } + + if(targetQueue->messageQueue.size() < targetQueue->messageDepth) { + MutexHelper mutexLock(targetQueue->queueLock, + MutexIF::TimeoutType::WAITING, 20); + // not ideal, works for now though. + MessageQueueMessage* mqmMessage = + dynamic_cast(message); + if(message != nullptr) { + targetQueue->messageQueue.push(*mqmMessage); + } + else { + sif::error << "MessageQueue::sendMessageFromMessageQueue: Message" + "is not MessageQueueMessage!" << std::endl; + } + + } + else { + return MessageQueueIF::FULL; + } + message->setSender(sentFrom); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t MessageQueue::lockQueue(MutexIF::TimeoutType timeoutType, + dur_millis_t lockTimeout) { + return queueLock->lockMutex(timeoutType, lockTimeout); +} + +ReturnValue_t MessageQueue::unlockQueue() { + return queueLock->unlockMutex(); +} diff --git a/fsfw/osal/host/MessageQueue.h b/fsfw/osal/host/MessageQueue.h new file mode 100644 index 0000000..97a9e49 --- /dev/null +++ b/fsfw/osal/host/MessageQueue.h @@ -0,0 +1,231 @@ +#ifndef FRAMEWORK_OSAL_HOST_MESSAGEQUEUE_H_ +#define FRAMEWORK_OSAL_HOST_MESSAGEQUEUE_H_ + +#include "../../internalError/InternalErrorReporterIF.h" +#include "../../ipc/MessageQueueIF.h" +#include "../../ipc/MessageQueueMessage.h" +#include "../../ipc/MutexIF.h" +#include "../../timemanager/Clock.h" + +#include +#include + +/** + * @brief This class manages sending and receiving of + * message queue messages. + * @details + * Message queues are used to pass asynchronous messages between processes. + * They work like post boxes, where all incoming messages are stored in FIFO + * order. This class creates a new receiving queue and provides methods to fetch + * received messages. Being a child of MessageQueueSender, this class also + * provides methods to send a message to a user-defined or a default destination. + * In addition it also provides a reply method to answer to the queue it + * received its last message from. + * + * The MessageQueue should be used as "post box" for a single owning object. + * So all message queue communication is "n-to-one". + * For creating the queue, as well as sending and receiving messages, the class + * makes use of the operating system calls provided. + * + * Please keep in mind that FreeRTOS offers different calls for message queue + * operations if called from an ISR. + * For now, the system context needs to be switched manually. + * @ingroup osal + * @ingroup message_queue + */ +class MessageQueue : public MessageQueueIF { + friend class MessageQueueSenderIF; +public: + /** + * @brief The constructor initializes and configures the message queue. + * @details + * By making use of the according operating system call, a message queue is + * created and initialized. The message depth - the maximum number of + * messages to be buffered - may be set with the help of a parameter, + * whereas the message size is automatically set to the maximum message + * queue message size. The operating system sets the message queue id, or + * in case of failure, it is set to zero. + * @param message_depth + * The number of messages to be buffered before passing an error to the + * sender. Default is three. + * @param max_message_size + * With this parameter, the maximum message size can be adjusted. + * This should be left default. + */ + MessageQueue(size_t messageDepth = 3, + size_t maxMessageSize = MessageQueueMessage::MAX_MESSAGE_SIZE); + + /** Copying message queues forbidden */ + MessageQueue(const MessageQueue&) = delete; + MessageQueue& operator=(const MessageQueue&) = delete; + + /** + * @brief The destructor deletes the formerly created message queue. + * @details This is accomplished by using the delete call provided + * by the operating system. + */ + virtual ~MessageQueue(); + + /** + * @brief This operation sends a message to the given destination. + * @details It directly uses the sendMessage call of the MessageQueueSender + * parent, but passes its queue id as "sentFrom" parameter. + * @param sendTo This parameter specifies the message queue id of the + * destination message queue. + * @param message A pointer to a previously created message, which is sent. + * @param ignoreFault If set to true, the internal software fault counter + * is not incremented if queue is full. + */ + ReturnValue_t sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault = false) override; + /** + * @brief This operation sends a message to the default destination. + * @details As in the sendMessage method, this function uses the + * sendToDefault call of the MessageQueueSender parent class and adds its + * queue id as "sentFrom" information. + * @param message A pointer to a previously created message, which is sent. + */ + ReturnValue_t sendToDefault(MessageQueueMessageIF* message) override; + /** + * @brief This operation sends a message to the last communication partner. + * @details This operation simplifies answering an incoming message by using + * the stored lastPartner information as destination. If there was no + * message received yet (i.e. lastPartner is zero), an error code is returned. + * @param message A pointer to a previously created message, which is sent. + */ + ReturnValue_t reply(MessageQueueMessageIF* message) override; + + /** + * @brief With the sendMessage call, a queue message is sent to a + * receiving queue. + * @details + * This method takes the message provided, adds the sentFrom information and + * passes it on to the destination provided with an operating system call. + * The OS's return value is returned. + * @param sendTo This parameter specifies the message queue id to send + * the message to. + * @param message This is a pointer to a previously created message, + * which is sent. + * @param sentFrom The sentFrom information can be set to inject the + * sender's queue id into the message. This variable is set to zero by + * default. + * @param ignoreFault If set to true, the internal software fault counter + * is not incremented if queue is full. + */ + virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE, + bool ignoreFault = false) override; + + /** + * @brief The sendToDefault method sends a queue message to the default + * destination. + * @details + * In all other aspects, it works identical to the sendMessage method. + * @param message This is a pointer to a previously created message, + * which is sent. + * @param sentFrom The sentFrom information can be set to inject the + * sender's queue id into the message. This variable is set to zero by + * default. + */ + virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message, + MessageQueueId_t sentFrom = NO_QUEUE, + bool ignoreFault = false) override; + + /** + * @brief This function reads available messages from the message queue + * and returns the sender. + * @details + * It works identically to the other receiveMessage call, but in addition + * returns the sender's queue id. + * @param message A pointer to a message in which the received data is stored. + * @param receivedFrom A pointer to a queue id in which the sender's id is stored. + */ + ReturnValue_t receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t *receivedFrom) override; + + /** + * @brief This function reads available messages from the message queue. + * @details + * If data is available it is stored in the passed message pointer. + * The message's original content is overwritten and the sendFrom + * information is stored in the lastPartner attribute. Else, the lastPartner + * information remains untouched, the message's content is cleared and the + * function returns immediately. + * @param message A pointer to a message in which the received data is stored. + */ + ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override; + /** + * Deletes all pending messages in the queue. + * @param count The number of flushed messages. + * @return RETURN_OK on success. + */ + ReturnValue_t flush(uint32_t* count) override; + /** + * @brief This method returns the message queue id of the last + * communication partner. + */ + MessageQueueId_t getLastPartner() const override; + /** + * @brief This method returns the message queue id of this class's + * message queue. + */ + MessageQueueId_t getId() const override; + + /** + * @brief This method is a simple setter for the default destination. + */ + void setDefaultDestination(MessageQueueId_t defaultDestination) override; + /** + * @brief This method is a simple getter for the default destination. + */ + MessageQueueId_t getDefaultDestination() const override; + + bool isDefaultDestinationSet() const override; + + ReturnValue_t lockQueue(MutexIF::TimeoutType timeoutType, + dur_millis_t lockTimeout); + ReturnValue_t unlockQueue(); +protected: + /** + * @brief Implementation to be called from any send Call within + * MessageQueue and MessageQueueSenderIF. + * @details + * This method takes the message provided, adds the sentFrom information and + * passes it on to the destination provided with an operating system call. + * The OS's return value is returned. + * @param sendTo + * This parameter specifies the message queue id to send the message to. + * @param message + * This is a pointer to a previously created message, which is sent. + * @param sentFrom + * The sentFrom information can be set to inject the sender's queue id into + * the message. This variable is set to zero by default. + * @param ignoreFault + * If set to true, the internal software fault counter is not incremented + * if queue is full. + * @param context Specify whether call is made from task or from an ISR. + */ + static ReturnValue_t sendMessageFromMessageQueue(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE, + bool ignoreFault=false); + + //static ReturnValue_t handleSendResult(BaseType_t result, bool ignoreFault); + +private: + std::queue messageQueue; + /** + * @brief The class stores the queue id it got assigned. + * If initialization fails, the queue id is set to zero. + */ + MessageQueueId_t mqId = 0; + size_t messageSize = 0; + size_t messageDepth = 0; + + MutexIF* queueLock; + + bool defaultDestinationSet = false; + MessageQueueId_t defaultDestination = 0; + MessageQueueId_t lastPartner = 0; +}; + +#endif /* FRAMEWORK_OSAL_HOST_MESSAGEQUEUE_H_ */ diff --git a/fsfw/osal/host/Mutex.cpp b/fsfw/osal/host/Mutex.cpp new file mode 100644 index 0000000..8471cab --- /dev/null +++ b/fsfw/osal/host/Mutex.cpp @@ -0,0 +1,39 @@ +#include "Mutex.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +Mutex::Mutex() {} + +ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType, uint32_t timeoutMs) { + if(timeoutMs == MutexIF::BLOCKING) { + mutex.lock(); + locked = true; + return HasReturnvaluesIF::RETURN_OK; + } + else if(timeoutMs == MutexIF::POLLING) { + if(mutex.try_lock()) { + locked = true; + return HasReturnvaluesIF::RETURN_OK; + } + } + else if(timeoutMs > MutexIF::POLLING){ + auto chronoMs = std::chrono::milliseconds(timeoutMs); + if(mutex.try_lock_for(chronoMs)) { + locked = true; + return HasReturnvaluesIF::RETURN_OK; + } + } + return MutexIF::MUTEX_TIMEOUT; +} + +ReturnValue_t Mutex::unlockMutex() { + if(not locked) { + return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX; + } + mutex.unlock(); + locked = false; + return HasReturnvaluesIF::RETURN_OK; +} + +std::timed_mutex* Mutex::getMutexHandle() { + return &mutex; +} diff --git a/fsfw/osal/host/Mutex.h b/fsfw/osal/host/Mutex.h new file mode 100644 index 0000000..24dafbb --- /dev/null +++ b/fsfw/osal/host/Mutex.h @@ -0,0 +1,29 @@ +#ifndef FSFW_OSAL_HOSTED_MUTEX_H_ +#define FSFW_OSAL_HOSTED_MUTEX_H_ + +#include "../../ipc/MutexIF.h" + +#include + +/** + * @brief OS component to implement MUTual EXclusion + * + * @details + * Mutexes are binary semaphores which include a priority inheritance mechanism. + * Documentation: https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html + * @ingroup osal + */ +class Mutex : public MutexIF { +public: + Mutex(); + ReturnValue_t lockMutex(TimeoutType timeoutType = + TimeoutType::BLOCKING, uint32_t timeoutMs = 0) override; + ReturnValue_t unlockMutex() override; + + std::timed_mutex* getMutexHandle(); +private: + bool locked = false; + std::timed_mutex mutex; +}; + +#endif /* FSFW_OSAL_HOSTED_MUTEX_H_ */ diff --git a/fsfw/osal/host/MutexFactory.cpp b/fsfw/osal/host/MutexFactory.cpp new file mode 100644 index 0000000..bf7707d --- /dev/null +++ b/fsfw/osal/host/MutexFactory.cpp @@ -0,0 +1,28 @@ +#include "../../ipc/MutexFactory.h" +#include "../../osal/host/Mutex.h" + +//TODO: Different variant than the lazy loading in QueueFactory. +//What's better and why? -> one is on heap the other on bss/data +//MutexFactory* MutexFactory::factoryInstance = new MutexFactory(); +MutexFactory* MutexFactory::factoryInstance = nullptr; + +MutexFactory::MutexFactory() { +} + +MutexFactory::~MutexFactory() { +} + +MutexFactory* MutexFactory::instance() { + if (factoryInstance == nullptr){ + factoryInstance = new MutexFactory(); + } + return MutexFactory::factoryInstance; +} + +MutexIF* MutexFactory::createMutex() { + return new Mutex(); +} + +void MutexFactory::deleteMutex(MutexIF* mutex) { + delete mutex; +} diff --git a/fsfw/osal/host/PeriodicTask.cpp b/fsfw/osal/host/PeriodicTask.cpp new file mode 100644 index 0000000..f4ee079 --- /dev/null +++ b/fsfw/osal/host/PeriodicTask.cpp @@ -0,0 +1,176 @@ +#include "Mutex.h" +#include "PeriodicTask.h" + +#include "../../ipc/MutexFactory.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tasks/ExecutableObjectIF.h" + +#include +#include + +#if defined(WIN32) +#include +#elif defined(LINUX) +#include +#endif + +PeriodicTask::PeriodicTask(const char *name, TaskPriority setPriority, + TaskStackSize setStack, TaskPeriod setPeriod, + void (*setDeadlineMissedFunc)()) : + started(false), taskName(name), period(setPeriod), + deadlineMissedFunc(setDeadlineMissedFunc) { + // It is propably possible to set task priorities by using the native + // task handles for Windows / Linux + mainThread = std::thread(&PeriodicTask::taskEntryPoint, this, this); +#if defined(WIN32) + /* List of possible priority classes: + * https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ + * nf-processthreadsapi-setpriorityclass + * And respective thread priority numbers: + * https://docs.microsoft.com/en-us/windows/ + * win32/procthread/scheduling-priorities */ + int result = SetPriorityClass( + reinterpret_cast(mainThread.native_handle()), + ABOVE_NORMAL_PRIORITY_CLASS); + if(result != 0) { + sif::error << "PeriodicTask: Windows SetPriorityClass failed with code " + << GetLastError() << std::endl; + } + result = SetThreadPriority( + reinterpret_cast(mainThread.native_handle()), + THREAD_PRIORITY_NORMAL); + if(result != 0) { + sif::error << "PeriodicTask: Windows SetPriorityClass failed with code " + << GetLastError() << std::endl; + } +#elif defined(LINUX) + // we can just copy and paste the code from linux here. +#endif +} + +PeriodicTask::~PeriodicTask(void) { + //Do not delete objects, we were responsible for ptrs only. + terminateThread = true; + if(mainThread.joinable()) { + mainThread.join(); + } + delete this; +} + +void PeriodicTask::taskEntryPoint(void* argument) { + PeriodicTask *originalTask(reinterpret_cast(argument)); + + + if (not originalTask->started) { + // we have to suspend/block here until the task is started. + // if semaphores are implemented, use them here. + std::unique_lock lock(initMutex); + initCondition.wait(lock); + } + + this->taskFunctionality(); + sif::debug << "PeriodicTask::taskEntryPoint: " + "Returned from taskFunctionality." << std::endl; +} + +ReturnValue_t PeriodicTask::startTask() { + started = true; + + // Notify task to start. + std::lock_guard lock(initMutex); + initCondition.notify_one(); + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t PeriodicTask::sleepFor(uint32_t ms) { + std::this_thread::sleep_for(std::chrono::milliseconds(ms)); + return HasReturnvaluesIF::RETURN_OK; +} + +void PeriodicTask::taskFunctionality() { + std::chrono::milliseconds periodChrono(static_cast(period*1000)); + auto currentStartTime { + std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + }; + auto nextStartTime{ currentStartTime }; + + /* Enter the loop that defines the task behavior. */ + for (;;) { + if(terminateThread.load()) { + break; + } + for (ObjectList::iterator it = objectList.begin(); + it != objectList.end(); ++it) { + (*it)->performOperation(); + } + if(not delayForInterval(¤tStartTime, periodChrono)) { + sif::warning << "PeriodicTask: " << taskName << + " missed deadline!\n" << std::flush; + if(deadlineMissedFunc != nullptr) { + this->deadlineMissedFunc(); + } + } + } +} + +ReturnValue_t PeriodicTask::addComponent(object_id_t object) { + ExecutableObjectIF* newObject = objectManager->get( + object); + if (newObject == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + objectList.push_back(newObject); + return HasReturnvaluesIF::RETURN_OK; +} + +uint32_t PeriodicTask::getPeriodMs() const { + return period * 1000; +} + +bool PeriodicTask::delayForInterval(chron_ms* previousWakeTimeMs, + const chron_ms interval) { + bool shouldDelay = false; + //Get current wakeup time + auto currentStartTime = + std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()); + /* Generate the tick time at which the task wants to wake. */ + auto nextTimeToWake_ms = (*previousWakeTimeMs) + interval; + + if (currentStartTime < *previousWakeTimeMs) { + /* The tick count has overflowed since this function was + lasted called. In this case the only time we should ever + actually delay is if the wake time has also overflowed, + and the wake time is greater than the tick time. When this + is the case it is as if neither time had overflowed. */ + if ((nextTimeToWake_ms < *previousWakeTimeMs) + && (nextTimeToWake_ms > currentStartTime)) { + shouldDelay = true; + } + } else { + /* The tick time has not overflowed. In this case we will + delay if either the wake time has overflowed, and/or the + tick time is less than the wake time. */ + if ((nextTimeToWake_ms < *previousWakeTimeMs) + || (nextTimeToWake_ms > currentStartTime)) { + shouldDelay = true; + } + } + + /* Update the wake time ready for the next call. */ + + (*previousWakeTimeMs) = nextTimeToWake_ms; + + if (shouldDelay) { + auto sleepTime = std::chrono::duration_cast( + nextTimeToWake_ms - currentStartTime); + std::this_thread::sleep_for(sleepTime); + return true; + } + //We are shifting the time in case the deadline was missed like rtems + (*previousWakeTimeMs) = currentStartTime; + return false; + +} diff --git a/fsfw/osal/host/PeriodicTask.h b/fsfw/osal/host/PeriodicTask.h new file mode 100644 index 0000000..7689788 --- /dev/null +++ b/fsfw/osal/host/PeriodicTask.h @@ -0,0 +1,123 @@ +#ifndef FRAMEWORK_OSAL_HOST_PERIODICTASK_H_ +#define FRAMEWORK_OSAL_HOST_PERIODICTASK_H_ + +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../tasks/PeriodicTaskIF.h" +#include "../../tasks/Typedef.h" + +#include +#include +#include +#include + +class ExecutableObjectIF; + +/** + * @brief This class represents a specialized task for + * periodic activities of multiple objects. + * @details + * + * @ingroup task_handling + */ +class PeriodicTask: public PeriodicTaskIF { +public: + /** + * @brief Standard constructor of the class. + * @details + * The class is initialized without allocated objects. These need to be + * added with #addComponent. + * @param priority + * @param stack_size + * @param setPeriod + * @param setDeadlineMissedFunc + * The function pointer to the deadline missed function that shall be + * assigned. + */ + PeriodicTask(const char *name, TaskPriority setPriority, TaskStackSize setStack, + TaskPeriod setPeriod,void (*setDeadlineMissedFunc)()); + /** + * @brief Currently, the executed object's lifetime is not coupled with + * the task object's lifetime, so the destructor is empty. + */ + virtual ~PeriodicTask(void); + + /** + * @brief The method to start the task. + * @details The method starts the task with the respective system call. + * Entry point is the taskEntryPoint method described below. + * The address of the task object is passed as an argument + * to the system call. + */ + ReturnValue_t startTask(void); + /** + * Adds an object to the list of objects to be executed. + * The objects are executed in the order added. + * @param object Id of the object to add. + * @return + * -@c RETURN_OK on success + * -@c RETURN_FAILED if the object could not be added. + */ + ReturnValue_t addComponent(object_id_t object); + + uint32_t getPeriodMs() const; + + ReturnValue_t sleepFor(uint32_t ms); + +protected: + using chron_ms = std::chrono::milliseconds; + bool started; + //!< Typedef for the List of objects. + typedef std::vector ObjectList; + std::thread mainThread; + std::atomic terminateThread = false; + + /** + * @brief This attribute holds a list of objects to be executed. + */ + ObjectList objectList; + + std::condition_variable initCondition; + std::mutex initMutex; + std::string taskName; + /** + * @brief The period of the task. + * @details + * The period determines the frequency of the task's execution. + * It is expressed in clock ticks. + */ + TaskPeriod period; + /** + * @brief The pointer to the deadline-missed function. + * @details + * This pointer stores the function that is executed if the task's deadline + * is missed. So, each may react individually on a timing failure. + * The pointer may be NULL, then nothing happens on missing the deadline. + * The deadline is equal to the next execution of the periodic task. + */ + void (*deadlineMissedFunc)(void); + /** + * @brief This is the function executed in the new task's context. + * @details + * It converts the argument back to the thread object type and copies the + * class instance to the task context. + * The taskFunctionality method is called afterwards. + * @param A pointer to the task object itself is passed as argument. + */ + + void taskEntryPoint(void* argument); + /** + * @brief The function containing the actual functionality of the task. + * @details + * The method sets and starts the task's period, then enters a loop that is + * repeated as long as the isRunning attribute is true. Within the loop, + * all performOperation methods of the added objects are called. Afterwards + * the checkAndRestartPeriod system call blocks the task until the next + * period. On missing the deadline, the deadlineMissedFunction is executed. + */ + void taskFunctionality(void); + + bool delayForInterval(chron_ms * previousWakeTimeMs, + const chron_ms interval); +}; + +#endif /* PERIODICTASK_H_ */ diff --git a/fsfw/osal/host/QueueFactory.cpp b/fsfw/osal/host/QueueFactory.cpp new file mode 100644 index 0000000..1a679c9 --- /dev/null +++ b/fsfw/osal/host/QueueFactory.cpp @@ -0,0 +1,46 @@ + +#include "MessageQueue.h" + +#include "../../ipc/MessageQueueSenderIF.h" +#include "../../ipc/MessageQueueMessageIF.h" +#include "../../ipc/QueueFactory.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +#include + +QueueFactory* QueueFactory::factoryInstance = nullptr; + + +ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + return MessageQueue::sendMessageFromMessageQueue(sendTo,message, + sentFrom,ignoreFault); + return HasReturnvaluesIF::RETURN_OK; +} + +QueueFactory* QueueFactory::instance() { + if (factoryInstance == nullptr) { + factoryInstance = new QueueFactory; + } + return factoryInstance; +} + +QueueFactory::QueueFactory() { +} + +QueueFactory::~QueueFactory() { +} + +MessageQueueIF* QueueFactory::createMessageQueue(uint32_t messageDepth, + size_t maxMessageSize) { + // A thread-safe queue can be implemented by using a combination + // of std::queue and std::mutex. This uses dynamic memory allocation + // which could be alleviated by using a custom allocator, external library + // (etl::queue) or simply using std::queue, we're on a host machine anyway. + return new MessageQueue(messageDepth, maxMessageSize); +} + +void QueueFactory::deleteMessageQueue(MessageQueueIF* queue) { + delete queue; +} diff --git a/fsfw/osal/host/QueueMapManager.cpp b/fsfw/osal/host/QueueMapManager.cpp new file mode 100644 index 0000000..1b2094e --- /dev/null +++ b/fsfw/osal/host/QueueMapManager.cpp @@ -0,0 +1,52 @@ +#include "QueueMapManager.h" + +#include "../../ipc/MutexFactory.h" +#include "../../ipc/MutexHelper.h" + +QueueMapManager* QueueMapManager::mqManagerInstance = nullptr; + +QueueMapManager::QueueMapManager() { + mapLock = MutexFactory::instance()->createMutex(); +} + +QueueMapManager* QueueMapManager::instance() { + if (mqManagerInstance == nullptr){ + mqManagerInstance = new QueueMapManager(); + } + return QueueMapManager::mqManagerInstance; +} + +ReturnValue_t QueueMapManager::addMessageQueue( + MessageQueueIF* queueToInsert, MessageQueueId_t* id) { + // Not thread-safe, but it is assumed all message queues are created + // at software initialization now. If this is to be made thread-safe in + // the future, it propably would be sufficient to lock the increment + // operation here + uint32_t currentId = queueCounter++; + auto returnPair = queueMap.emplace(currentId, queueToInsert); + if(not returnPair.second) { + // this should never happen for the atomic variable. + sif::error << "QueueMapManager: This ID is already inside the map!" + << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + if (id != nullptr) { + *id = currentId; + } + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueIF* QueueMapManager::getMessageQueue( + MessageQueueId_t messageQueueId) const { + MutexHelper(mapLock, MutexIF::TimeoutType::WAITING, 50); + auto queueIter = queueMap.find(messageQueueId); + if(queueIter != queueMap.end()) { + return queueIter->second; + } + else { + sif::warning << "QueueMapManager::getQueueHandle: The ID" << + messageQueueId << " does not exists in the map" << std::endl; + return nullptr; + } +} + diff --git a/fsfw/osal/host/QueueMapManager.h b/fsfw/osal/host/QueueMapManager.h new file mode 100644 index 0000000..d7d5c91 --- /dev/null +++ b/fsfw/osal/host/QueueMapManager.h @@ -0,0 +1,47 @@ +#ifndef FSFW_OSAL_HOST_QUEUEMAPMANAGER_H_ +#define FSFW_OSAL_HOST_QUEUEMAPMANAGER_H_ + +#include "../../ipc/MessageQueueSenderIF.h" +#include "../../osal/host/MessageQueue.h" +#include +#include + +using QueueMap = std::unordered_map; + + +/** + * An internal map to map message queue IDs to message queues. + * This propably should be a singleton.. + */ +class QueueMapManager { +public: + //! Returns the single instance of SemaphoreFactory. + static QueueMapManager* instance(); + + /** + * Insert a message queue into the map and returns a message queue ID + * @param queue The message queue to insert. + * @param id The passed value will be set unless a nullptr is passed + * @return + */ + ReturnValue_t addMessageQueue(MessageQueueIF* queue, MessageQueueId_t* + id = nullptr); + /** + * Get the message queue handle by providing a message queue ID. + * @param messageQueueId + * @return + */ + MessageQueueIF* getMessageQueue(MessageQueueId_t messageQueueId) const; + +private: + //! External instantiation is forbidden. + QueueMapManager(); + uint32_t queueCounter = 1; + MutexIF* mapLock; + QueueMap queueMap; + static QueueMapManager* mqManagerInstance; +}; + + + +#endif /* FSFW_OSAL_HOST_QUEUEMAPMANAGER_H_ */ diff --git a/fsfw/osal/host/SemaphoreFactory.cpp b/fsfw/osal/host/SemaphoreFactory.cpp new file mode 100644 index 0000000..b19b2c7 --- /dev/null +++ b/fsfw/osal/host/SemaphoreFactory.cpp @@ -0,0 +1,39 @@ +#include "../../tasks/SemaphoreFactory.h" +#include "../../osal/linux/BinarySemaphore.h" +#include "../../osal/linux/CountingSemaphore.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr; + +SemaphoreFactory::SemaphoreFactory() { +} + +SemaphoreFactory::~SemaphoreFactory() { + delete factoryInstance; +} + +SemaphoreFactory* SemaphoreFactory::instance() { + if (factoryInstance == nullptr){ + factoryInstance = new SemaphoreFactory(); + } + return SemaphoreFactory::factoryInstance; +} + +SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t arguments) { + // Just gonna wait for full C++20 for now. + sif::error << "SemaphoreFactory: Binary Semaphore not implemented yet." + " Returning nullptr!\n" << std::flush; + return nullptr; +} + +SemaphoreIF* SemaphoreFactory::createCountingSemaphore(const uint8_t maxCount, + uint8_t initCount, uint32_t arguments) { + // Just gonna wait for full C++20 for now. + sif::error << "SemaphoreFactory: Counting Semaphore not implemented yet." + " Returning nullptr!\n" << std::flush; + return nullptr; +} + +void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) { + delete semaphore; +} diff --git a/fsfw/osal/host/TaskFactory.cpp b/fsfw/osal/host/TaskFactory.cpp new file mode 100644 index 0000000..bc65504 --- /dev/null +++ b/fsfw/osal/host/TaskFactory.cpp @@ -0,0 +1,51 @@ +#include "../../osal/host/FixedTimeslotTask.h" +#include "../../osal/host/PeriodicTask.h" +#include "../../tasks/TaskFactory.h" +#include "../../returnvalues/HasReturnvaluesIF.h" +#include "../../tasks/PeriodicTaskIF.h" + +#include + +TaskFactory* TaskFactory::factoryInstance = new TaskFactory(); + +// Will propably not be used for hosted implementation +const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE = 0; + +TaskFactory::TaskFactory() { +} + +TaskFactory::~TaskFactory() { +} + +TaskFactory* TaskFactory::instance() { + return TaskFactory::factoryInstance; +} + +PeriodicTaskIF* TaskFactory::createPeriodicTask(TaskName name_, + TaskPriority taskPriority_,TaskStackSize stackSize_, + TaskPeriod periodInSeconds_, + TaskDeadlineMissedFunction deadLineMissedFunction_) { + return new PeriodicTask(name_, taskPriority_, stackSize_, periodInSeconds_, + deadLineMissedFunction_); +} + +FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(TaskName name_, + TaskPriority taskPriority_,TaskStackSize stackSize_, + TaskPeriod periodInSeconds_, + TaskDeadlineMissedFunction deadLineMissedFunction_) { + return new FixedTimeslotTask(name_, taskPriority_, stackSize_, + periodInSeconds_, deadLineMissedFunction_); +} + +ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) { + // This might block for some time! + delete task; + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t TaskFactory::delayTask(uint32_t delayMs){ + std::this_thread::sleep_for(std::chrono::milliseconds(delayMs)); + return HasReturnvaluesIF::RETURN_OK; +} + + diff --git a/fsfw/osal/linux/BinarySemaphore.cpp b/fsfw/osal/linux/BinarySemaphore.cpp new file mode 100644 index 0000000..8c0eeae --- /dev/null +++ b/fsfw/osal/linux/BinarySemaphore.cpp @@ -0,0 +1,149 @@ +#include "../../osal/linux/BinarySemaphore.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +extern "C" { +#include +#include +} + +BinarySemaphore::BinarySemaphore() { + // Using unnamed semaphores for now + initSemaphore(); +} + +BinarySemaphore::~BinarySemaphore() { + sem_destroy(&handle); +} + +BinarySemaphore::BinarySemaphore(BinarySemaphore&& s) { + initSemaphore(); +} + +BinarySemaphore& BinarySemaphore::operator =( + BinarySemaphore&& s) { + initSemaphore(); + return * this; +} + +ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType, + uint32_t timeoutMs) { + int result = 0; + if(timeoutType == TimeoutType::POLLING) { + result = sem_trywait(&handle); + } + else if(timeoutType == TimeoutType::BLOCKING) { + result = sem_wait(&handle); + } + else if(timeoutType == TimeoutType::WAITING){ + timespec timeOut; + clock_gettime(CLOCK_REALTIME, &timeOut); + uint64_t nseconds = timeOut.tv_sec * 1000000000 + timeOut.tv_nsec; + nseconds += timeoutMs * 1000000; + timeOut.tv_sec = nseconds / 1000000000; + timeOut.tv_nsec = nseconds - timeOut.tv_sec * 1000000000; + result = sem_timedwait(&handle, &timeOut); + if(result != 0 and errno == EINVAL) { + sif::debug << "BinarySemaphore::acquire: Invalid time value possible" + << std::endl; + } + } + if(result == 0) { + return HasReturnvaluesIF::RETURN_OK; + } + + switch(errno) { + case(EAGAIN): + // Operation could not be performed without blocking (for sem_trywait) + case(ETIMEDOUT): + // Semaphore is 0 + return SemaphoreIF::SEMAPHORE_TIMEOUT; + case(EINVAL): + // Semaphore invalid + return SemaphoreIF::SEMAPHORE_INVALID; + case(EINTR): + // Call was interrupted by signal handler + sif::debug << "BinarySemaphore::acquire: Signal handler interrupted." + "Code " << strerror(errno) << std::endl; + /* No break */ + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t BinarySemaphore::release() { + return BinarySemaphore::release(&this->handle); +} + +ReturnValue_t BinarySemaphore::release(sem_t *handle) { + ReturnValue_t countResult = checkCount(handle, 1); + if(countResult != HasReturnvaluesIF::RETURN_OK) { + return countResult; + } + + int result = sem_post(handle); + if(result == 0) { + return HasReturnvaluesIF::RETURN_OK; + } + + switch(errno) { + case(EINVAL): + // Semaphore invalid + return SemaphoreIF::SEMAPHORE_INVALID; + case(EOVERFLOW): + // SEM_MAX_VALUE overflow. This should never happen + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +uint8_t BinarySemaphore::getSemaphoreCounter() const { + // And another ugly cast :-D + return getSemaphoreCounter(const_cast(&this->handle)); +} + +uint8_t BinarySemaphore::getSemaphoreCounter(sem_t *handle) { + int value = 0; + int result = sem_getvalue(handle, &value); + if (result == 0) { + return value; + } + else if(result != 0 and errno == EINVAL) { + // Could be called from interrupt, use lightweight printf + printf("BinarySemaphore::getSemaphoreCounter: Invalid semaphore\n"); + return 0; + } + else { + // This should never happen. + return 0; + } +} + +void BinarySemaphore::initSemaphore(uint8_t initCount) { + auto result = sem_init(&handle, true, initCount); + if(result == -1) { + switch(errno) { + case(EINVAL): + // Value exceeds SEM_VALUE_MAX + case(ENOSYS): + // System does not support process-shared semaphores + sif::error << "BinarySemaphore: Init failed with" << strerror(errno) + << std::endl; + } + } +} + +ReturnValue_t BinarySemaphore::checkCount(sem_t* handle, uint8_t maxCount) { + int value = getSemaphoreCounter(handle); + if(value >= maxCount) { + if(maxCount == 1 and value > 1) { + // Binary Semaphore special case. + // This is a config error use lightweight printf is this is called + // from an interrupt + printf("BinarySemaphore::release: Value of binary semaphore greater" + " than 1!\n"); + return HasReturnvaluesIF::RETURN_FAILED; + } + return SemaphoreIF::SEMAPHORE_NOT_OWNED; + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/osal/linux/BinarySemaphore.h b/fsfw/osal/linux/BinarySemaphore.h new file mode 100644 index 0000000..e9bb8bb --- /dev/null +++ b/fsfw/osal/linux/BinarySemaphore.h @@ -0,0 +1,81 @@ +#ifndef FRAMEWORK_OSAL_LINUX_BINARYSEMPAHORE_H_ +#define FRAMEWORK_OSAL_LINUX_BINARYSEMPAHORE_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" +#include "../../tasks/SemaphoreIF.h" + +extern "C" { +#include +} + +/** + * @brief OS Tool to achieve synchronization of between tasks or between + * task and ISR. The default semaphore implementation creates a + * binary semaphore, which can only be taken once. + * @details + * See: http://www.man7.org/linux/man-pages/man7/sem_overview.7.html + * @author R. Mueller + * @ingroup osal + */ +class BinarySemaphore: public SemaphoreIF, + public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF; + + //! @brief Default ctor + BinarySemaphore(); + //! @brief Copy ctor, deleted explicitely. + BinarySemaphore(const BinarySemaphore&) = delete; + //! @brief Copy assignment, deleted explicitely. + BinarySemaphore& operator=(const BinarySemaphore&) = delete; + //! @brief Move ctor + BinarySemaphore (BinarySemaphore &&); + //! @brief Move assignment + BinarySemaphore & operator=(BinarySemaphore &&); + //! @brief Destructor + virtual ~BinarySemaphore(); + + void initSemaphore(uint8_t initCount = 1); + + uint8_t getSemaphoreCounter() const override; + static uint8_t getSemaphoreCounter(sem_t* handle); + + /** + * Take the binary semaphore. + * If the semaphore has already been taken, the task will be blocked + * for a maximum of #timeoutMs or until the semaphore is given back, + * for example by an ISR or another task. + * @param timeoutMs + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout + */ + ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING, + uint32_t timeoutMs = 0) override; + + /** + * Release the binary semaphore. + * @return -@c RETURN_OK on success + * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is + * already available. + */ + virtual ReturnValue_t release() override; + /** + * This static function can be used to release a semaphore by providing + * its handle. + * @param handle + * @return + */ + static ReturnValue_t release(sem_t* handle); + + /** Checks the validity of the semaphore count against a specified + * known maxCount + * @param handle + * @param maxCount + * @return + */ + static ReturnValue_t checkCount(sem_t* handle, uint8_t maxCount); +protected: + sem_t handle; +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */ diff --git a/fsfw/osal/linux/Clock.cpp b/fsfw/osal/linux/Clock.cpp new file mode 100644 index 0000000..4de18f8 --- /dev/null +++ b/fsfw/osal/linux/Clock.cpp @@ -0,0 +1,221 @@ +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../timemanager/Clock.h" + +#include +#include +#include +#include +#include +#include + +uint16_t Clock::leapSeconds = 0; +MutexIF* Clock::timeMutex = NULL; + +uint32_t Clock::getTicksPerSecond(void){ + uint32_t ticks = sysconf(_SC_CLK_TCK); + return ticks; +} + +ReturnValue_t Clock::setClock(const TimeOfDay_t* time) { + timespec timeUnix; + timeval timeTimeval; + convertTimeOfDayToTimeval(time,&timeTimeval); + timeUnix.tv_sec = timeTimeval.tv_sec; + timeUnix.tv_nsec = (__syscall_slong_t) timeTimeval.tv_usec * 1000; + + int status = clock_settime(CLOCK_REALTIME,&timeUnix); + if(status!=0){ + //TODO errno + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::setClock(const timeval* time) { + timespec timeUnix; + timeUnix.tv_sec = time->tv_sec; + timeUnix.tv_nsec = (__syscall_slong_t) time->tv_usec * 1000; + int status = clock_settime(CLOCK_REALTIME,&timeUnix); + if(status!=0){ + //TODO errno + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getClock_timeval(timeval* time) { + timespec timeUnix; + int status = clock_gettime(CLOCK_REALTIME,&timeUnix); + if(status!=0){ + return HasReturnvaluesIF::RETURN_FAILED; + } + time->tv_sec = timeUnix.tv_sec; + time->tv_usec = timeUnix.tv_nsec / 1000.0; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getClock_usecs(uint64_t* time) { + timeval timeVal; + ReturnValue_t result = getClock_timeval(&timeVal); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + *time = (uint64_t)timeVal.tv_sec*1e6 + timeVal.tv_usec; + + return HasReturnvaluesIF::RETURN_OK; +} + +timeval Clock::getUptime() { + timeval uptime; + auto result = getUptime(&uptime); + if(result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "Clock::getUptime: Error getting uptime" << std::endl; + } + return uptime; +} + +ReturnValue_t Clock::getUptime(timeval* uptime) { + //TODO This is not posix compatible and delivers only seconds precision + // is the OS not called Linux? + //Linux specific file read but more precise + double uptimeSeconds; + if(std::ifstream("/proc/uptime",std::ios::in) >> uptimeSeconds){ + uptime->tv_sec = uptimeSeconds; + uptime->tv_usec = uptimeSeconds *(double) 1e6 - (uptime->tv_sec *1e6); + } + + //TODO This is not posix compatible and delivers only seconds precision + // I suggest this is moved into another clock function which will + // deliver second precision later. +// struct sysinfo sysInfo; +// int result = sysinfo(&sysInfo); +// if(result != 0){ +// return HasReturnvaluesIF::RETURN_FAILED; +// } +// return sysInfo.uptime; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) { + timeval uptime; + ReturnValue_t result = getUptime(&uptime); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + *uptimeMs = uptime.tv_sec * 1e3 + uptime.tv_usec / 1e3; + return HasReturnvaluesIF::RETURN_OK; +} + + + +ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) { + timespec timeUnix; + int status = clock_gettime(CLOCK_REALTIME,&timeUnix); + if(status != 0){ + //TODO errno + return HasReturnvaluesIF::RETURN_FAILED; + } + + struct tm* timeInfo; + timeInfo = gmtime(&timeUnix.tv_sec); + time->year = timeInfo->tm_year + 1900; + time->month = timeInfo->tm_mon+1; + time->day = timeInfo->tm_mday; + time->hour = timeInfo->tm_hour; + time->minute = timeInfo->tm_min; + time->second = timeInfo->tm_sec; + time->usecond = timeUnix.tv_nsec / 1000.0; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from, + timeval* to) { + + tm fromTm; + //Note: Fails for years before AD + fromTm.tm_year = from->year - 1900; + fromTm.tm_mon = from->month - 1; + fromTm.tm_mday = from->day; + fromTm.tm_hour = from->hour; + fromTm.tm_min = from->minute; + fromTm.tm_sec = from->second; + + to->tv_sec = mktime(&fromTm); + to->tv_usec = from->usecond; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) { + *JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24. + / 3600.; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) { + //SHOULDDO: works not for dates in the past (might have less leap seconds) + if (timeMutex == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + + uint16_t leapSeconds; + ReturnValue_t result = getLeapSeconds(&leapSeconds); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + timeval leapSeconds_timeval = { 0, 0 }; + leapSeconds_timeval.tv_sec = leapSeconds; + + //initial offset between UTC and TAI + timeval UTCtoTAI1972 = { 10, 0 }; + + timeval TAItoTT = { 32, 184000 }; + + *tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) { + if(checkOrCreateClockMutex()!=HasReturnvaluesIF::RETURN_OK){ + return HasReturnvaluesIF::RETURN_FAILED; + } + ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + leapSeconds = leapSeconds_; + + result = timeMutex->unlockMutex(); + return result; +} + +ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) { + if(timeMutex==NULL){ + return HasReturnvaluesIF::RETURN_FAILED; + } + ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + *leapSeconds_ = leapSeconds; + + result = timeMutex->unlockMutex(); + return result; +} + +ReturnValue_t Clock::checkOrCreateClockMutex(){ + if(timeMutex==NULL){ + MutexFactory* mutexFactory = MutexFactory::instance(); + if (mutexFactory == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + timeMutex = mutexFactory->createMutex(); + if (timeMutex == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/osal/linux/CountingSemaphore.cpp b/fsfw/osal/linux/CountingSemaphore.cpp new file mode 100644 index 0000000..1833939 --- /dev/null +++ b/fsfw/osal/linux/CountingSemaphore.cpp @@ -0,0 +1,54 @@ +#include "../../osal/linux/CountingSemaphore.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount): + maxCount(maxCount), initCount(initCount) { + if(initCount > maxCount) { + sif::error << "CountingSemaphoreUsingTask: Max count bigger than " + "intial cout. Setting initial count to max count." << std::endl; + initCount = maxCount; + } + + initSemaphore(initCount); +} + +CountingSemaphore::CountingSemaphore(CountingSemaphore&& other): + maxCount(other.maxCount), initCount(other.initCount) { + initSemaphore(initCount); +} + +CountingSemaphore& CountingSemaphore::operator =( + CountingSemaphore&& other) { + initSemaphore(other.initCount); + return * this; +} + +ReturnValue_t CountingSemaphore::release() { + ReturnValue_t result = checkCount(&handle, maxCount); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return CountingSemaphore::release(&this->handle); +} + +ReturnValue_t CountingSemaphore::release(sem_t* handle) { + int result = sem_post(handle); + if(result == 0) { + return HasReturnvaluesIF::RETURN_OK; + } + + switch(errno) { + case(EINVAL): + // Semaphore invalid + return SemaphoreIF::SEMAPHORE_INVALID; + case(EOVERFLOW): + // SEM_MAX_VALUE overflow. This should never happen + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +uint8_t CountingSemaphore::getMaxCount() const { + return maxCount; +} + diff --git a/fsfw/osal/linux/CountingSemaphore.h b/fsfw/osal/linux/CountingSemaphore.h new file mode 100644 index 0000000..e0fbb99 --- /dev/null +++ b/fsfw/osal/linux/CountingSemaphore.h @@ -0,0 +1,37 @@ +#ifndef FRAMEWORK_OSAL_LINUX_COUNTINGSEMAPHORE_H_ +#define FRAMEWORK_OSAL_LINUX_COUNTINGSEMAPHORE_H_ +#include "../../osal/linux/BinarySemaphore.h" + +/** + * @brief Counting semaphores, which can be acquired more than once. + * @details + * See: https://www.freertos.org/CreateCounting.html + * API of counting semaphores is almost identical to binary semaphores, + * so we just inherit from binary semaphore and provide the respective + * constructors. + */ +class CountingSemaphore: public BinarySemaphore { +public: + CountingSemaphore(const uint8_t maxCount, uint8_t initCount); + //! @brief Copy ctor, disabled + CountingSemaphore(const CountingSemaphore&) = delete; + //! @brief Copy assignment, disabled + CountingSemaphore& operator=(const CountingSemaphore&) = delete; + //! @brief Move ctor + CountingSemaphore (CountingSemaphore &&); + //! @brief Move assignment + CountingSemaphore & operator=(CountingSemaphore &&); + + ReturnValue_t release() override; + static ReturnValue_t release(sem_t* sem); + /* Same API as binary semaphore otherwise. acquire() can be called + * until there are not semaphores left and release() can be called + * until maxCount is reached. */ + + uint8_t getMaxCount() const; +private: + const uint8_t maxCount; + uint8_t initCount = 0; +}; + +#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */ diff --git a/fsfw/osal/linux/FixedTimeslotTask.cpp b/fsfw/osal/linux/FixedTimeslotTask.cpp new file mode 100644 index 0000000..247a34e --- /dev/null +++ b/fsfw/osal/linux/FixedTimeslotTask.cpp @@ -0,0 +1,97 @@ +#include "FixedTimeslotTask.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +#include + +uint32_t FixedTimeslotTask::deadlineMissedCount = 0; +const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE = PTHREAD_STACK_MIN; + +FixedTimeslotTask::FixedTimeslotTask(const char* name_, int priority_, + size_t stackSize_, uint32_t periodMs_): + PosixThread(name_,priority_,stackSize_),pst(periodMs_),started(false) { +} + +FixedTimeslotTask::~FixedTimeslotTask() { + +} + +void* FixedTimeslotTask::taskEntryPoint(void* arg) { + //The argument is re-interpreted as PollingTask. + FixedTimeslotTask *originalTask(reinterpret_cast(arg)); + //The task's functionality is called. + originalTask->taskFunctionality(); + return nullptr; +} + +ReturnValue_t FixedTimeslotTask::startTask() { + started = true; + createTask(&taskEntryPoint,this); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t FixedTimeslotTask::sleepFor(uint32_t ms) { + return PosixThread::sleep((uint64_t)ms*1000000); +} + +uint32_t FixedTimeslotTask::getPeriodMs() const { + return pst.getLengthMs(); +} + +ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId, + uint32_t slotTimeMs, int8_t executionStep) { + ExecutableObjectIF* executableObject = + objectManager->get(componentId); + if (executableObject != nullptr) { + pst.addSlot(componentId, slotTimeMs, executionStep, + executableObject,this); + return HasReturnvaluesIF::RETURN_OK; + } + + sif::error << "Component " << std::hex << componentId << + " not found, not adding it to pst" << std::dec << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t FixedTimeslotTask::checkSequence() const { + return pst.checkSequence(); +} + +void FixedTimeslotTask::taskFunctionality() { + //Like FreeRTOS pthreads are running as soon as they are created + if (!started) { + suspend(); + } + + pst.intializeSequenceAfterTaskCreation(); + + //The start time for the first entry is read. + uint64_t lastWakeTime = getCurrentMonotonicTimeMs(); + uint64_t interval = pst.getIntervalToNextSlotMs(); + + + //The task's "infinite" inner loop is entered. + while (1) { + if (pst.slotFollowsImmediately()) { + //Do nothing + } else { + //The interval for the next polling slot is selected. + interval = this->pst.getIntervalToPreviousSlotMs(); + //The period is checked and restarted with the new interval. + //If the deadline was missed, the deadlineMissedFunc is called. + if(!PosixThread::delayUntil(&lastWakeTime,interval)) { + //No time left on timer -> we missed the deadline + missedDeadlineCounter(); + } + } + //The device handler for this slot is executed and the next one is chosen. + this->pst.executeAndAdvance(); + } +} + +void FixedTimeslotTask::missedDeadlineCounter() { + FixedTimeslotTask::deadlineMissedCount++; + if (FixedTimeslotTask::deadlineMissedCount % 10 == 0) { + sif::error << "PST missed " << FixedTimeslotTask::deadlineMissedCount + << " deadlines." << std::endl; + } +} diff --git a/fsfw/osal/linux/FixedTimeslotTask.h b/fsfw/osal/linux/FixedTimeslotTask.h new file mode 100644 index 0000000..5c5c181 --- /dev/null +++ b/fsfw/osal/linux/FixedTimeslotTask.h @@ -0,0 +1,77 @@ +#ifndef FSFW_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ +#define FSFW_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ + +#include "PosixThread.h" +#include "../../tasks/FixedTimeslotTaskIF.h" +#include "../../tasks/FixedSlotSequence.h" +#include + +class FixedTimeslotTask: public FixedTimeslotTaskIF, public PosixThread { +public: + /** + * Create a generic periodic task. + * @param name_ + * Name, maximum allowed size of linux is 16 chars, everything else will + * be truncated. + * @param priority_ + * Real-time priority, ranges from 1 to 99 for Linux. + * See: https://man7.org/linux/man-pages/man7/sched.7.html + * @param stackSize_ + * @param period_ + * @param deadlineMissedFunc_ + */ + FixedTimeslotTask(const char* name_, int priority_, size_t stackSize_, + uint32_t periodMs_); + virtual ~FixedTimeslotTask(); + + virtual ReturnValue_t startTask(); + + virtual ReturnValue_t sleepFor(uint32_t ms); + + virtual uint32_t getPeriodMs() const; + + virtual ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs, + int8_t executionStep); + + virtual ReturnValue_t checkSequence() const; + + /** + * This static function can be used as #deadlineMissedFunc. + * It counts missedDeadlines and prints the number of missed deadlines every 10th time. + */ + static void missedDeadlineCounter(); + + /** + * A helper variable to count missed deadlines. + */ + static uint32_t deadlineMissedCount; + +protected: + /** + * @brief This function holds the main functionality of the thread. + * @details + * Holding the main functionality of the task, this method is most important. + * It links the functionalities provided by FixedSlotSequence with the + * OS's System Calls to keep the timing of the periods. + */ + virtual void taskFunctionality(); + +private: + /** + * @brief This is the entry point in a new thread. + * + * @details + * This method, that is the entry point in the new thread and calls + * taskFunctionality of the child class. Needs a valid pointer to the + * derived class. + * + * The void* returnvalue is not used yet but could be used to return + * arbitrary data. + */ + static void* taskEntryPoint(void* arg); + FixedSlotSequence pst; + + bool started; +}; + +#endif /* FSFW_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ */ diff --git a/fsfw/osal/linux/InternalErrorCodes.cpp b/fsfw/osal/linux/InternalErrorCodes.cpp new file mode 100644 index 0000000..a01cc72 --- /dev/null +++ b/fsfw/osal/linux/InternalErrorCodes.cpp @@ -0,0 +1,14 @@ +#include "../../osal/InternalErrorCodes.h" + +ReturnValue_t InternalErrorCodes::translate(uint8_t code) { + //TODO This class can be removed + return HasReturnvaluesIF::RETURN_FAILED; +} + +InternalErrorCodes::InternalErrorCodes() { +} + +InternalErrorCodes::~InternalErrorCodes() { + +} + diff --git a/fsfw/osal/linux/MessageQueue.cpp b/fsfw/osal/linux/MessageQueue.cpp new file mode 100644 index 0000000..bc14374 --- /dev/null +++ b/fsfw/osal/linux/MessageQueue.cpp @@ -0,0 +1,370 @@ +#include "MessageQueue.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../objectmanager/ObjectManagerIF.h" + +#include +#include /* For O_* constants */ +#include /* For mode constants */ +#include +#include + + + +MessageQueue::MessageQueue(uint32_t messageDepth, size_t maxMessageSize): + id(MessageQueueIF::NO_QUEUE),lastPartner(MessageQueueIF::NO_QUEUE), + defaultDestination(MessageQueueIF::NO_QUEUE), + maxMessageSize(maxMessageSize) { + //debug << "MessageQueue::MessageQueue: Creating a queue" << std::endl; + mq_attr attributes; + this->id = 0; + //Set attributes + attributes.mq_curmsgs = 0; + attributes.mq_maxmsg = messageDepth; + attributes.mq_msgsize = maxMessageSize; + attributes.mq_flags = 0; //Flags are ignored on Linux during mq_open + //Set the name of the queue. The slash is mandatory! + sprintf(name, "/FSFW_MQ%u\n", queueCounter++); + + // Create a nonblocking queue if the name is available (the queue is read + // and writable for the owner as well as the group) + int oflag = O_NONBLOCK | O_RDWR | O_CREAT | O_EXCL; + mode_t mode = S_IWUSR | S_IREAD | S_IWGRP | S_IRGRP | S_IROTH | S_IWOTH; + mqd_t tempId = mq_open(name, oflag, mode, &attributes); + if (tempId == -1) { + handleError(&attributes, messageDepth); + } + else { + //Successful mq_open call + this->id = tempId; + } +} + +MessageQueue::~MessageQueue() { + int status = mq_close(this->id); + if(status != 0){ + sif::error << "MessageQueue::Destructor: mq_close Failed with status: " + << strerror(errno) <> + defaultMqMaxMsg and defaultMqMaxMsg < messageDepth) { + /* + See: https://www.man7.org/linux/man-pages/man3/mq_open.3.html + This happens if the msg_max value is not large enough + It is ignored if the executable is run in privileged mode. + Run the unlockRealtime script or grant the mode manually by using: + sudo setcap 'CAP_SYS_RESOURCE=+ep' + + Persistent solution for session: + echo | sudo tee /proc/sys/fs/mqueue/msg_max + + Permanent solution: + sudo nano /etc/sysctl.conf + Append at end: fs/mqueue/msg_max = + Apply changes with: sudo sysctl -p + */ + sif::error << "MessageQueue::MessageQueue: Default MQ size " + << defaultMqMaxMsg << " is too small for requested size " + << messageDepth << std::endl; + sif::error << "This error can be fixed by setting the maximum " + "allowed message size higher!" << std::endl; + + } + break; + } + case(EEXIST): { + // An error occured during open + // We need to distinguish if it is caused by an already created queue + //There's another queue with the same name + //We unlink the other queue + int status = mq_unlink(name); + if (status != 0) { + sif::error << "mq_unlink Failed with status: " << strerror(errno) + << std::endl; + } + else { + // Successful unlinking, try to open again + mqd_t tempId = mq_open(name, + O_NONBLOCK | O_RDWR | O_CREAT | O_EXCL, + S_IWUSR | S_IREAD | S_IWGRP | S_IRGRP, attributes); + if (tempId != -1) { + //Successful mq_open + this->id = tempId; + return HasReturnvaluesIF::RETURN_OK; + } + } + break; + } + + default: + // Failed either the first time or the second time + sif::error << "MessageQueue::MessageQueue: Creating Queue " << std::hex + << name << std::dec << " failed with status: " + << strerror(errno) << std::endl; + + } + return HasReturnvaluesIF::RETURN_FAILED; + + + +} + +ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault) { + return sendMessageFrom(sendTo, message, this->getId(), false); +} + +ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessageIF* message) { + return sendToDefaultFrom(message, this->getId()); +} + +ReturnValue_t MessageQueue::reply(MessageQueueMessageIF* message) { + if (this->lastPartner != 0) { + return sendMessageFrom(this->lastPartner, message, this->getId()); + } else { + return NO_REPLY_PARTNER; + } +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t* receivedFrom) { + ReturnValue_t status = this->receiveMessage(message); + *receivedFrom = this->lastPartner; + return status; +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message) { + if(message == nullptr) { + sif::error << "MessageQueue::receiveMessage: Message is " + "nullptr!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + + if(message->getMaximumMessageSize() < maxMessageSize) { + sif::error << "MessageQueue::receiveMessage: Message size " + << message->getMaximumMessageSize() + << " too small to receive data!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + + unsigned int messagePriority = 0; + int status = mq_receive(id,reinterpret_cast(message->getBuffer()), + message->getMaximumMessageSize(),&messagePriority); + if (status > 0) { + this->lastPartner = message->getSender(); + //Check size of incoming message. + if (message->getMessageSize() < message->getMinimumMessageSize()) { + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; + }else if(status==0){ + //Success but no message received + return MessageQueueIF::EMPTY; + } else { + //No message was received. Keep lastPartner anyway, I might send + //something later. But still, delete packet content. + memset(message->getData(), 0, message->getMaximumMessageSize()); + switch(errno){ + case EAGAIN: + //O_NONBLOCK or MQ_NONBLOCK was set and there are no messages + //currently on the specified queue. + return MessageQueueIF::EMPTY; + case EBADF: + //mqdes doesn't represent a valid queue open for reading. + sif::error << "MessageQueue::receive: configuration error " + << strerror(errno) << std::endl; + /*NO BREAK*/ + case EINVAL: + /* + * This value indicates one of the following: + * - The pointer to the buffer for storing the received message, + * msg_ptr, is NULL. + * - The number of bytes requested, msg_len is less than zero. + * - msg_len is anything other than the mq_msgsize of the specified + * queue, and the QNX extended option MQ_READBUF_DYNAMIC hasn't + * been set in the queue's mq_flags. + */ + sif::error << "MessageQueue::receive: configuration error " + << strerror(errno) << std::endl; + /*NO BREAK*/ + case EMSGSIZE: + /* + * This value indicates one of the following: + * - the QNX extended option MQ_READBUF_DYNAMIC hasn't been set, + * and the given msg_len is shorter than the mq_msgsize for + * the given queue. + * - the extended option MQ_READBUF_DYNAMIC has been set, but the + * given msg_len is too short for the message that would have + * been received. + */ + sif::error << "MessageQueue::receive: configuration error " + << strerror(errno) << std::endl; + /*NO BREAK*/ + case EINTR: + //The operation was interrupted by a signal. + default: + + return HasReturnvaluesIF::RETURN_FAILED; + } + + } +} + +MessageQueueId_t MessageQueue::getLastPartner() const { + return this->lastPartner; +} + +ReturnValue_t MessageQueue::flush(uint32_t* count) { + mq_attr attrib; + int status = mq_getattr(id,&attrib); + if(status != 0){ + switch(errno){ + case EBADF: + //mqdes doesn't represent a valid message queue. + sif::error << "MessageQueue::flush configuration error, " + "called flush with an invalid queue ID" << std::endl; + /*NO BREAK*/ + case EINVAL: + //mq_attr is NULL + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + } + *count = attrib.mq_curmsgs; + attrib.mq_curmsgs = 0; + status = mq_setattr(id,&attrib,NULL); + if(status != 0){ + switch(errno){ + case EBADF: + //mqdes doesn't represent a valid message queue. + sif::error << "MessageQueue::flush configuration error, " + "called flush with an invalid queue ID" << std::endl; + /*NO BREAK*/ + case EINVAL: + /* + * This value indicates one of the following: + * - mq_attr is NULL. + * - MQ_MULT_NOTIFY had been set for this queue, and the given + * mq_flags includes a 0 in the MQ_MULT_NOTIFY bit. Once + * MQ_MULT_NOTIFY has been turned on, it may never be turned off. + */ + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + } + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueId_t MessageQueue::getId() const { + return this->id; +} + +void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) { + this->defaultDestination = defaultDestination; +} + +ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessageIF* message, + MessageQueueId_t sentFrom, bool ignoreFault) { + return sendMessageFrom(defaultDestination, message, sentFrom, ignoreFault); +} + + +ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + return sendMessageFromMessageQueue(sendTo,message, sentFrom,ignoreFault); + +} + +MessageQueueId_t MessageQueue::getDefaultDestination() const { + return this->defaultDestination; +} + +bool MessageQueue::isDefaultDestinationSet() const { + return (defaultDestination != NO_QUEUE); +} + +uint16_t MessageQueue::queueCounter = 0; + +ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo, + MessageQueueMessageIF *message, MessageQueueId_t sentFrom, + bool ignoreFault) { + if(message == nullptr) { + sif::error << "MessageQueue::sendMessageFromMessageQueue: Message is " + "nullptr!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + + message->setSender(sentFrom); + int result = mq_send(sendTo, + reinterpret_cast(message->getBuffer()), + message->getMessageSize(),0); + + //TODO: Check if we're in ISR. + if (result != 0) { + if(!ignoreFault){ + InternalErrorReporterIF* internalErrorReporter = + objectManager->get( + objects::INTERNAL_ERROR_REPORTER); + if (internalErrorReporter != NULL) { + internalErrorReporter->queueMessageNotSent(); + } + } + switch(errno){ + case EAGAIN: + //The O_NONBLOCK flag was set when opening the queue, or the + //MQ_NONBLOCK flag was set in its attributes, and the + //specified queue is full. + return MessageQueueIF::FULL; + case EBADF: { + //mq_des doesn't represent a valid message queue descriptor, + //or mq_des wasn't opened for writing. + sif::error << "MessageQueue::sendMessage: Configuration error, MQ" + << " destination invalid." << std::endl; + sif::error << strerror(errno) << " in " + <<"mq_send to: " << sendTo << " sent from " + << sentFrom << std::endl; + return DESTINVATION_INVALID; + } + case EINTR: + //The call was interrupted by a signal. + case EINVAL: + /* + * This value indicates one of the following: + * - msg_ptr is NULL. + * - msg_len is negative. + * - msg_prio is greater than MQ_PRIO_MAX. + * - msg_prio is less than 0. + * - MQ_PRIO_RESTRICT is set in the mq_attr of mq_des, and + * msg_prio is greater than the priority of the calling process. + */ + sif::error << "MessageQueue::sendMessage: Configuration error " + << strerror(errno) << " in mq_send" << std::endl; + /*NO BREAK*/ + case EMSGSIZE: + // The msg_len is greater than the msgsize associated with + //the specified queue. + sif::error << "MessageQueue::sendMessage: Size error [" << + strerror(errno) << "] in mq_send" << std::endl; + /*NO BREAK*/ + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/osal/linux/MessageQueue.h b/fsfw/osal/linux/MessageQueue.h new file mode 100644 index 0000000..239bbbd --- /dev/null +++ b/fsfw/osal/linux/MessageQueue.h @@ -0,0 +1,187 @@ +#ifndef FSFW_OSAL_LINUX_MESSAGEQUEUE_H_ +#define FSFW_OSAL_LINUX_MESSAGEQUEUE_H_ + +#include "../../internalError/InternalErrorReporterIF.h" +#include "../../ipc/MessageQueueIF.h" +#include "../../ipc/MessageQueueMessage.h" + +#include +/** + * @brief This class manages sending and receiving of message queue messages. + * + * @details + * Message queues are used to pass asynchronous messages between processes. + * They work like post boxes, where all incoming messages are stored in FIFO + * order. This class creates a new receiving queue and provides methods to fetch + * received messages. Being a child of MessageQueueSender, this class also + * provides methods to send a message to a user-defined or a default destination. + * In addition it also provides a reply method to answer to the queue it + * received its last message from. + * + * The MessageQueue should be used as "post box" for a single owning object. + * So all message queue communication is "n-to-one". + * + * The creation of message queues, as well as sending and receiving messages, + * makes use of the operating system calls provided. + * @ingroup message_queue + */ +class MessageQueue : public MessageQueueIF { + friend class MessageQueueSenderIF; +public: + /** + * @brief The constructor initializes and configures the message queue. + * @details By making use of the according operating system call, a message queue is created + * and initialized. The message depth - the maximum number of messages to be + * buffered - may be set with the help of a parameter, whereas the message size is + * automatically set to the maximum message queue message size. The operating system + * sets the message queue id, or i case of failure, it is set to zero. + * @param message_depth The number of messages to be buffered before passing an error to the + * sender. Default is three. + * @param max_message_size With this parameter, the maximum message size can be adjusted. + * This should be left default. + */ + MessageQueue(uint32_t messageDepth = 3, + size_t maxMessageSize = MessageQueueMessage::MAX_MESSAGE_SIZE ); + /** + * @brief The destructor deletes the formerly created message queue. + * @details This is accomplished by using the delete call provided by the operating system. + */ + virtual ~MessageQueue(); + /** + * @brief This operation sends a message to the given destination. + * @details It directly uses the sendMessage call of the MessageQueueSender parent, but passes its + * queue id as "sentFrom" parameter. + * @param sendTo This parameter specifies the message queue id of the destination message queue. + * @param message A pointer to a previously created message, which is sent. + * @param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full. + */ + virtual ReturnValue_t sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault = false ); + /** + * @brief This operation sends a message to the default destination. + * @details As in the sendMessage method, this function uses the sendToDefault call of the + * MessageQueueSender parent class and adds its queue id as "sentFrom" information. + * @param message A pointer to a previously created message, which is sent. + */ + virtual ReturnValue_t sendToDefault( MessageQueueMessageIF* message ); + /** + * @brief This operation sends a message to the last communication partner. + * @details This operation simplifies answering an incoming message by using the stored + * lastParnter information as destination. If there was no message received yet + * (i.e. lastPartner is zero), an error code is returned. + * @param message A pointer to a previously created message, which is sent. + */ + ReturnValue_t reply( MessageQueueMessageIF* message ); + + /** + * @brief This function reads available messages from the message queue and returns the sender. + * @details It works identically to the other receiveMessage call, but in addition returns the + * sender's queue id. + * @param message A pointer to a message in which the received data is stored. + * @param receivedFrom A pointer to a queue id in which the sender's id is stored. + */ + ReturnValue_t receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t *receivedFrom); + + /** + * @brief This function reads available messages from the message queue. + * @details If data is available it is stored in the passed message pointer. The message's + * original content is overwritten and the sendFrom information is stored in the + * lastPartner attribute. Else, the lastPartner information remains untouched, the + * message's content is cleared and the function returns immediately. + * @param message A pointer to a message in which the received data is stored. + */ + ReturnValue_t receiveMessage(MessageQueueMessageIF* message); + /** + * Deletes all pending messages in the queue. + * @param count The number of flushed messages. + * @return RETURN_OK on success. + */ + ReturnValue_t flush(uint32_t* count); + /** + * @brief This method returns the message queue id of the last communication partner. + */ + MessageQueueId_t getLastPartner() const; + /** + * @brief This method returns the message queue id of this class's message queue. + */ + MessageQueueId_t getId() const; + /** + * \brief With the sendMessage call, a queue message is sent to a receiving queue. + * \param sendTo This parameter specifies the message queue id to send the message to. + * \param message This is a pointer to a previously created message, which is sent. + * \param sentFrom The sentFrom information can be set to inject the sender's queue id into the message. + * This variable is set to zero by default. + * \param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full. + */ + virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault = false ); + /** + * \brief The sendToDefault method sends a queue message to the default destination. + * \details In all other aspects, it works identical to the sendMessage method. + * \param message This is a pointer to a previously created message, which is sent. + * \param sentFrom The sentFrom information can be set to inject the sender's queue id into the message. + * This variable is set to zero by default. + */ + virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message, + MessageQueueId_t sentFrom = NO_QUEUE, bool ignoreFault = false ); + /** + * \brief This method is a simple setter for the default destination. + */ + void setDefaultDestination(MessageQueueId_t defaultDestination); + /** + * \brief This method is a simple getter for the default destination. + */ + MessageQueueId_t getDefaultDestination() const; + + bool isDefaultDestinationSet() const; +protected: + /** + * Implementation to be called from any send Call within MessageQueue and MessageQueueSenderIF + * \details This method takes the message provided, adds the sentFrom information and passes + * it on to the destination provided with an operating system call. The OS's return + * value is returned. + * \param sendTo This parameter specifies the message queue id to send the message to. + * \param message This is a pointer to a previously created message, which is sent. + * \param sentFrom The sentFrom information can be set to inject the sender's queue id into the message. + * This variable is set to zero by default. + * \param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full. + */ + static ReturnValue_t sendMessageFromMessageQueue(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE, + bool ignoreFault=false); +private: + /** + * @brief The class stores the queue id it got assigned from the operating system in this attribute. + * If initialization fails, the queue id is set to zero. + */ + MessageQueueId_t id; + /** + * @brief In this attribute, the queue id of the last communication partner is stored + * to allow for replying. + */ + MessageQueueId_t lastPartner; + /** + * @brief The message queue's name -a user specific information for the operating system- is + * generated automatically with the help of this static counter. + */ + /** + * \brief This attribute stores a default destination to send messages to. + * \details It is stored to simplify sending to always-the-same receiver. The attribute may + * be set in the constructor or by a setter call to setDefaultDestination. + */ + MessageQueueId_t defaultDestination; + + /** + * The name of the message queue, stored for unlinking + */ + char name[16]; + + static uint16_t queueCounter; + const size_t maxMessageSize; + + ReturnValue_t handleError(mq_attr* attributes, uint32_t messageDepth); +}; + +#endif /* FSFW_OSAL_LINUX_MESSAGEQUEUE_H_ */ diff --git a/fsfw/osal/linux/Mutex.cpp b/fsfw/osal/linux/Mutex.cpp new file mode 100644 index 0000000..b27ff8c --- /dev/null +++ b/fsfw/osal/linux/Mutex.cpp @@ -0,0 +1,111 @@ +#include "Mutex.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../timemanager/Clock.h" + +uint8_t Mutex::count = 0; + + +#include +#include + +Mutex::Mutex() { + pthread_mutexattr_t mutexAttr; + int status = pthread_mutexattr_init(&mutexAttr); + if (status != 0) { + sif::error << "Mutex: Attribute init failed with: " << strerror(status) << std::endl; + } + status = pthread_mutexattr_setprotocol(&mutexAttr, PTHREAD_PRIO_INHERIT); + if (status != 0) { + sif::error << "Mutex: Attribute set PRIO_INHERIT failed with: " << strerror(status) + << std::endl; + } + status = pthread_mutex_init(&mutex, &mutexAttr); + if (status != 0) { + sif::error << "Mutex: creation with name, id " << mutex.__data.__count + << ", " << " failed with " << strerror(status) << std::endl; + } + // After a mutex attributes object has been used to initialize one or more + // mutexes, any function affecting the attributes object + // (including destruction) shall not affect any previously initialized mutexes. + status = pthread_mutexattr_destroy(&mutexAttr); + if (status != 0) { + sif::error << "Mutex: Attribute destroy failed with " << strerror(status) << std::endl; + } +} + +Mutex::~Mutex() { + //No Status check yet + pthread_mutex_destroy(&mutex); +} + +ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType, uint32_t timeoutMs) { + int status = 0; + + if(timeoutType == TimeoutType::POLLING) { + status = pthread_mutex_trylock(&mutex); + } + else if (timeoutType == TimeoutType::WAITING) { + timespec timeOut; + clock_gettime(CLOCK_REALTIME, &timeOut); + uint64_t nseconds = timeOut.tv_sec * 1000000000 + timeOut.tv_nsec; + nseconds += timeoutMs * 1000000; + timeOut.tv_sec = nseconds / 1000000000; + timeOut.tv_nsec = nseconds - timeOut.tv_sec * 1000000000; + status = pthread_mutex_timedlock(&mutex, &timeOut); + } + else if(timeoutType == TimeoutType::BLOCKING) { + status = pthread_mutex_lock(&mutex); + } + + switch (status) { + case EINVAL: + // The mutex was created with the protocol attribute having the value + // PTHREAD_PRIO_PROTECT and the calling thread's priority is higher + // than the mutex's current priority ceiling. + return WRONG_ATTRIBUTE_SETTING; + // The process or thread would have blocked, and the abs_timeout + // parameter specified a nanoseconds field value less than zero or + // greater than or equal to 1000 million. + // The value specified by mutex does not refer to an initialized mutex object. + //return MUTEX_NOT_FOUND; + case EBUSY: + // The mutex could not be acquired because it was already locked. + return MUTEX_ALREADY_LOCKED; + case ETIMEDOUT: + // The mutex could not be locked before the specified timeout expired. + return MUTEX_TIMEOUT; + case EAGAIN: + // The mutex could not be acquired because the maximum number of + // recursive locks for mutex has been exceeded. + return MUTEX_MAX_LOCKS; + case EDEADLK: + // A deadlock condition was detected or the current thread + // already owns the mutex. + return CURR_THREAD_ALREADY_OWNS_MUTEX; + case 0: + //Success + return HasReturnvaluesIF::RETURN_OK; + default: + return HasReturnvaluesIF::RETURN_FAILED; + }; +} + +ReturnValue_t Mutex::unlockMutex() { + int status = pthread_mutex_unlock(&mutex); + switch (status) { + case EINVAL: + //The value specified by mutex does not refer to an initialized mutex object. + return MUTEX_NOT_FOUND; + case EAGAIN: + //The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded. + return MUTEX_MAX_LOCKS; + case EPERM: + //The current thread does not own the mutex. + return CURR_THREAD_DOES_NOT_OWN_MUTEX; + case 0: + //Success + return HasReturnvaluesIF::RETURN_OK; + default: + return HasReturnvaluesIF::RETURN_FAILED; + }; +} diff --git a/fsfw/osal/linux/Mutex.h b/fsfw/osal/linux/Mutex.h new file mode 100644 index 0000000..ecb47a3 --- /dev/null +++ b/fsfw/osal/linux/Mutex.h @@ -0,0 +1,19 @@ +#ifndef OS_LINUX_MUTEX_H_ +#define OS_LINUX_MUTEX_H_ + +#include "../../ipc/MutexIF.h" +#include + + +class Mutex : public MutexIF { +public: + Mutex(); + virtual ~Mutex(); + virtual ReturnValue_t lockMutex(TimeoutType timeoutType, uint32_t timeoutMs); + virtual ReturnValue_t unlockMutex(); +private: + pthread_mutex_t mutex; + static uint8_t count; +}; + +#endif /* OS_RTEMS_MUTEX_H_ */ diff --git a/fsfw/osal/linux/MutexFactory.cpp b/fsfw/osal/linux/MutexFactory.cpp new file mode 100644 index 0000000..8c2faf8 --- /dev/null +++ b/fsfw/osal/linux/MutexFactory.cpp @@ -0,0 +1,23 @@ +#include "../../ipc/MutexFactory.h" +#include "Mutex.h" + +//TODO: Different variant than the lazy loading in QueueFactory. What's better and why? +MutexFactory* MutexFactory::factoryInstance = new MutexFactory(); + +MutexFactory::MutexFactory() { +} + +MutexFactory::~MutexFactory() { +} + +MutexFactory* MutexFactory::instance() { + return MutexFactory::factoryInstance; +} + +MutexIF* MutexFactory::createMutex() { + return new Mutex(); +} + +void MutexFactory::deleteMutex(MutexIF* mutex) { + delete mutex; +} diff --git a/fsfw/osal/linux/PeriodicPosixTask.cpp b/fsfw/osal/linux/PeriodicPosixTask.cpp new file mode 100644 index 0000000..3c1df6b --- /dev/null +++ b/fsfw/osal/linux/PeriodicPosixTask.cpp @@ -0,0 +1,86 @@ +#include "../../tasks/ExecutableObjectIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include +#include "PeriodicPosixTask.h" + +PeriodicPosixTask::PeriodicPosixTask(const char* name_, int priority_, + size_t stackSize_, uint32_t period_, void(deadlineMissedFunc_)()): + PosixThread(name_, priority_, stackSize_), objectList(), started(false), + periodMs(period_), deadlineMissedFunc(deadlineMissedFunc_) { +} + +PeriodicPosixTask::~PeriodicPosixTask() { + //Not Implemented +} + +void* PeriodicPosixTask::taskEntryPoint(void* arg) { + //The argument is re-interpreted as PollingTask. + PeriodicPosixTask *originalTask(reinterpret_cast(arg)); + //The task's functionality is called. + originalTask->taskFunctionality(); + return NULL; +} + +ReturnValue_t PeriodicPosixTask::addComponent(object_id_t object) { + ExecutableObjectIF* newObject = objectManager->get( + object); + if (newObject == nullptr) { + sif::error << "PeriodicTask::addComponent: Invalid object. Make sure" + << " it implements ExecutableObjectIF!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + objectList.push_back(newObject); + newObject->setTaskIF(this); + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t PeriodicPosixTask::sleepFor(uint32_t ms) { + return PosixThread::sleep((uint64_t)ms*1000000); +} + + +ReturnValue_t PeriodicPosixTask::startTask(void) { + started = true; + //sif::info << stackSize << std::endl; + PosixThread::createTask(&taskEntryPoint,this); + return HasReturnvaluesIF::RETURN_OK; +} + +void PeriodicPosixTask::taskFunctionality(void) { + if(not started) { + suspend(); + } + + for (auto const &object: objectList) { + object->initializeAfterTaskCreation(); + } + + uint64_t lastWakeTime = getCurrentMonotonicTimeMs(); + //The task's "infinite" inner loop is entered. + while (1) { + for (auto const &object: objectList) { + object->performOperation(); + } + + if(not PosixThread::delayUntil(&lastWakeTime, periodMs)){ + char name[20] = {0}; + int status = pthread_getname_np(pthread_self(), name, sizeof(name)); + if(status == 0) { + sif::error << "PeriodicPosixTask " << name << ": Deadline " + "missed." << std::endl; + } + else { + sif::error << "PeriodicPosixTask X: Deadline missed. " << + status << std::endl; + } + if (this->deadlineMissedFunc != nullptr) { + this->deadlineMissedFunc(); + } + } + } +} + +uint32_t PeriodicPosixTask::getPeriodMs() const { + return periodMs; +} diff --git a/fsfw/osal/linux/PeriodicPosixTask.h b/fsfw/osal/linux/PeriodicPosixTask.h new file mode 100644 index 0000000..ffee236 --- /dev/null +++ b/fsfw/osal/linux/PeriodicPosixTask.h @@ -0,0 +1,90 @@ +#ifndef FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ +#define FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ + +#include "../../tasks/PeriodicTaskIF.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "PosixThread.h" +#include "../../tasks/ExecutableObjectIF.h" +#include + +class PeriodicPosixTask: public PosixThread, public PeriodicTaskIF { +public: + /** + * Create a generic periodic task. + * @param name_ + * Name, maximum allowed size of linux is 16 chars, everything else will + * be truncated. + * @param priority_ + * Real-time priority, ranges from 1 to 99 for Linux. + * See: https://man7.org/linux/man-pages/man7/sched.7.html + * @param stackSize_ + * @param period_ + * @param deadlineMissedFunc_ + */ + PeriodicPosixTask(const char* name_, int priority_, size_t stackSize_, + uint32_t period_, void(*deadlineMissedFunc_)()); + virtual ~PeriodicPosixTask(); + + /** + * @brief The method to start the task. + * @details The method starts the task with the respective system call. + * Entry point is the taskEntryPoint method described below. + * The address of the task object is passed as an argument + * to the system call. + */ + ReturnValue_t startTask() override; + /** + * Adds an object to the list of objects to be executed. + * The objects are executed in the order added. + * @param object Id of the object to add. + * @return RETURN_OK on success, RETURN_FAILED if the object could not be added. + */ + ReturnValue_t addComponent(object_id_t object) override; + + uint32_t getPeriodMs() const override; + + ReturnValue_t sleepFor(uint32_t ms) override; + +private: + typedef std::vector ObjectList; //!< Typedef for the List of objects. + /** + * @brief This attribute holds a list of objects to be executed. + */ + ObjectList objectList; + + /** + * @brief Flag to indicate that the task was started and is allowed to run + */ + bool started; + + + /** + * @brief Period of the task in milliseconds + */ + uint32_t periodMs; + /** + * @brief The function containing the actual functionality of the task. + * @details The method sets and starts + * the task's period, then enters a loop that is repeated indefinitely. Within the loop, all performOperation methods of the added + * objects are called. Afterwards the task will be blocked until the next period. + * On missing the deadline, the deadlineMissedFunction is executed. + */ + virtual void taskFunctionality(void); + /** + * @brief This is the entry point in a new thread. + * + * @details This method, that is the entry point in the new thread and calls taskFunctionality of the child class. + * Needs a valid pointer to the derived class. + */ + static void* taskEntryPoint(void* arg); + /** + * @brief The pointer to the deadline-missed function. + * @details This pointer stores the function that is executed if the task's deadline is missed. + * So, each may react individually on a timing failure. The pointer may be NULL, + * then nothing happens on missing the deadline. The deadline is equal to the next execution + * of the periodic task. + */ + void (*deadlineMissedFunc)(); +}; + +#endif /* FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ */ diff --git a/fsfw/osal/linux/PosixThread.cpp b/fsfw/osal/linux/PosixThread.cpp new file mode 100644 index 0000000..ddb1f74 --- /dev/null +++ b/fsfw/osal/linux/PosixThread.cpp @@ -0,0 +1,215 @@ +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "PosixThread.h" +#include +#include + +PosixThread::PosixThread(const char* name_, int priority_, size_t stackSize_): + thread(0),priority(priority_),stackSize(stackSize_) { + name[0] = '\0'; + std::strncat(name, name_, PTHREAD_MAX_NAMELEN - 1); +} + +PosixThread::~PosixThread() { + //No deletion and no free of Stack Pointer +} + +ReturnValue_t PosixThread::sleep(uint64_t ns) { + //TODO sleep might be better with timer instead of sleep() + timespec time; + time.tv_sec = ns/1000000000; + time.tv_nsec = ns - time.tv_sec*1e9; + + //Remaining Time is not set here + int status = nanosleep(&time,NULL); + if(status != 0){ + switch(errno){ + case EINTR: + //The nanosleep() function was interrupted by a signal. + return HasReturnvaluesIF::RETURN_FAILED; + case EINVAL: + //The rqtp argument specified a nanosecond value less than zero or + // greater than or equal to 1000 million. + return HasReturnvaluesIF::RETURN_FAILED; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + + } + return HasReturnvaluesIF::RETURN_OK; +} + +void PosixThread::suspend() { + //Wait for SIGUSR1 + int caughtSig = 0; + sigset_t waitSignal; + sigemptyset(&waitSignal); + sigaddset(&waitSignal, SIGUSR1); + sigwait(&waitSignal, &caughtSig); + if (caughtSig != SIGUSR1) { + sif::error << "FixedTimeslotTask: Unknown Signal received: " << + caughtSig << std::endl; + } +} + +void PosixThread::resume(){ + /* Signal the thread to start. Makes sense to call kill to start or? ;) + * + * According to Posix raise(signal) will call pthread_kill(pthread_self(), sig), + * but as the call must be done from the thread itsself this is not possible here + */ + pthread_kill(thread,SIGUSR1); +} + +bool PosixThread::delayUntil(uint64_t* const prevoiusWakeTime_ms, + const uint64_t delayTime_ms) { + uint64_t nextTimeToWake_ms; + bool shouldDelay = false; + //Get current Time + const uint64_t currentTime_ms = getCurrentMonotonicTimeMs(); + /* Generate the tick time at which the task wants to wake. */ + nextTimeToWake_ms = (*prevoiusWakeTime_ms) + delayTime_ms; + + if (currentTime_ms < *prevoiusWakeTime_ms) { + /* The tick count has overflowed since this function was + lasted called. In this case the only time we should ever + actually delay is if the wake time has also overflowed, + and the wake time is greater than the tick time. When this + is the case it is as if neither time had overflowed. */ + if ((nextTimeToWake_ms < *prevoiusWakeTime_ms) + && (nextTimeToWake_ms > currentTime_ms)) { + shouldDelay = true; + } + } else { + /* The tick time has not overflowed. In this case we will + delay if either the wake time has overflowed, and/or the + tick time is less than the wake time. */ + if ((nextTimeToWake_ms < *prevoiusWakeTime_ms) + || (nextTimeToWake_ms > currentTime_ms)) { + shouldDelay = true; + } + } + + /* Update the wake time ready for the next call. */ + + (*prevoiusWakeTime_ms) = nextTimeToWake_ms; + + if (shouldDelay) { + uint64_t sleepTime = nextTimeToWake_ms - currentTime_ms; + PosixThread::sleep(sleepTime * 1000000ull); + return true; + } + //We are shifting the time in case the deadline was missed like rtems + (*prevoiusWakeTime_ms) = currentTime_ms; + return false; + +} + + +uint64_t PosixThread::getCurrentMonotonicTimeMs(){ + timespec timeNow; + clock_gettime(CLOCK_MONOTONIC_RAW, &timeNow); + uint64_t currentTime_ms = (uint64_t) timeNow.tv_sec * 1000 + + timeNow.tv_nsec / 1000000; + + return currentTime_ms; +} + + +void PosixThread::createTask(void* (*fnc_)(void*), void* arg_) { + //sif::debug << "PosixThread::createTask" << std::endl; + /* + * The attr argument points to a pthread_attr_t structure whose contents + are used at thread creation time to determine attributes for the new + thread; this structure is initialized using pthread_attr_init(3) and + related functions. If attr is NULL, then the thread is created with + default attributes. + */ + pthread_attr_t attributes; + int status = pthread_attr_init(&attributes); + if(status != 0){ + sif::error << "Posix Thread attribute init failed with: " << + strerror(status) << std::endl; + } + void* stackPointer; + status = posix_memalign(&stackPointer, sysconf(_SC_PAGESIZE), stackSize); + if(status != 0){ + sif::error << "PosixThread::createTask: Stack init failed with: " << + strerror(status) << std::endl; + if(errno == ENOMEM) { + uint64_t stackMb = stackSize/10e6; + sif::error << "PosixThread::createTask: Insufficient memory for" + " the requested " << stackMb << " MB" << std::endl; + } + else if(errno == EINVAL) { + sif::error << "PosixThread::createTask: Wrong alignment argument!" + << std::endl; + } + return; + } + + status = pthread_attr_setstack(&attributes, stackPointer, stackSize); + if(status != 0){ + sif::error << "Posix Thread attribute setStack failed with: " << + strerror(status) << std::endl; + } + + status = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED); + if(status != 0){ + sif::error << "Posix Thread attribute setinheritsched failed with: " << + strerror(status) << std::endl; + } + + // TODO FIFO -> This needs root privileges for the process + status = pthread_attr_setschedpolicy(&attributes,SCHED_FIFO); + if(status != 0){ + sif::error << "Posix Thread attribute schedule policy failed with: " << + strerror(status) << std::endl; + } + + sched_param scheduleParams; + scheduleParams.__sched_priority = priority; + status = pthread_attr_setschedparam(&attributes, &scheduleParams); + if(status != 0){ + sif::error << "Posix Thread attribute schedule params failed with: " << + strerror(status) << std::endl; + } + + //Set Signal Mask for suspend until startTask is called + sigset_t waitSignal; + sigemptyset(&waitSignal); + sigaddset(&waitSignal, SIGUSR1); + status = pthread_sigmask(SIG_BLOCK, &waitSignal, NULL); + if(status != 0){ + sif::error << "Posix Thread sigmask failed failed with: " << + strerror(status) << " errno: " << strerror(errno) << std::endl; + } + + + status = pthread_create(&thread,&attributes,fnc_,arg_); + if(status != 0){ + sif::error << "Posix Thread create failed with: " << + strerror(status) << std::endl; + } + + status = pthread_setname_np(thread,name); + if(status != 0){ + sif::error << "PosixThread::createTask: setname failed with: " << + strerror(status) << std::endl; + if(status == ERANGE) { + sif::error << "PosixThread::createTask: Task name length longer" + " than 16 chars. Truncating.." << std::endl; + name[15] = '\0'; + status = pthread_setname_np(thread,name); + if(status != 0){ + sif::error << "PosixThread::createTask: Setting name" + " did not work.." << std::endl; + } + } + } + + status = pthread_attr_destroy(&attributes); + if(status!=0){ + sif::error << "Posix Thread attribute destroy failed with: " << + strerror(status) << std::endl; + } +} diff --git a/fsfw/osal/linux/PosixThread.h b/fsfw/osal/linux/PosixThread.h new file mode 100644 index 0000000..7d8d349 --- /dev/null +++ b/fsfw/osal/linux/PosixThread.h @@ -0,0 +1,77 @@ +#ifndef FRAMEWORK_OSAL_LINUX_POSIXTHREAD_H_ +#define FRAMEWORK_OSAL_LINUX_POSIXTHREAD_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" +#include +#include +#include +#include + +class PosixThread { +public: + static constexpr uint8_t PTHREAD_MAX_NAMELEN = 16; + PosixThread(const char* name_, int priority_, size_t stackSize_); + virtual ~PosixThread(); + /** + * Set the Thread to sleep state + * @param ns Nanosecond sleep time + * @return Returns Failed if sleep fails + */ + static ReturnValue_t sleep(uint64_t ns); + /** + * @brief Function to suspend the task until SIGUSR1 was received + * + * @details Will be called in the beginning to suspend execution until startTask() is called explicitly. + */ + void suspend(); + + /** + * @brief Function to allow a other thread to start the thread again from suspend state + * + * @details Restarts the Thread after suspend call + */ + void resume(); + + + /** + * Delay function similar to FreeRtos delayUntil function + * + * @param prevoiusWakeTime_ms Needs the previous wake time and returns the next wakeup time + * @param delayTime_ms Time period to delay + * + * @return False If deadline was missed; True if task was delayed + */ + static bool delayUntil(uint64_t* const prevoiusWakeTime_ms, const uint64_t delayTime_ms); + + /** + * Returns the current time in milliseconds from CLOCK_MONOTONIC + * + * @return current time in milliseconds from CLOCK_MONOTONIC + */ + static uint64_t getCurrentMonotonicTimeMs(); + +protected: + pthread_t thread; + + /** + * @brief Function that has to be called by derived class because the + * derived class pointer has to be valid as argument. + * @details + * This function creates a pthread with the given parameters. As the + * function requires a pointer to the derived object it has to be called + * after the this pointer of the derived object is valid. + * Sets the taskEntryPoint as function to be called by new a thread. + * @param fnc_ Function which will be executed by the thread. + * @param arg_ + * argument of the taskEntryPoint function, needs to be this pointer + * of derived class + */ + void createTask(void* (*fnc_)(void*),void* arg_); + +private: + char name[PTHREAD_MAX_NAMELEN]; + int priority; + size_t stackSize = 0; +}; + +#endif /* FRAMEWORK_OSAL_LINUX_POSIXTHREAD_H_ */ diff --git a/fsfw/osal/linux/QueueFactory.cpp b/fsfw/osal/linux/QueueFactory.cpp new file mode 100644 index 0000000..44def48 --- /dev/null +++ b/fsfw/osal/linux/QueueFactory.cpp @@ -0,0 +1,44 @@ +#include "../../ipc/QueueFactory.h" +#include "MessageQueue.h" + +#include "../../ipc/messageQueueDefinitions.h" +#include "../../ipc/MessageQueueSenderIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +#include +#include + + +#include + +QueueFactory* QueueFactory::factoryInstance = nullptr; + + +ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + return MessageQueue::sendMessageFromMessageQueue(sendTo,message, + sentFrom,ignoreFault); +} + +QueueFactory* QueueFactory::instance() { + if (factoryInstance == nullptr) { + factoryInstance = new QueueFactory; + } + return factoryInstance; +} + +QueueFactory::QueueFactory() { +} + +QueueFactory::~QueueFactory() { +} + +MessageQueueIF* QueueFactory::createMessageQueue(uint32_t messageDepth, + size_t maxMessageSize) { + return new MessageQueue(messageDepth, maxMessageSize); +} + +void QueueFactory::deleteMessageQueue(MessageQueueIF* queue) { + delete queue; +} diff --git a/fsfw/osal/linux/SemaphoreFactory.cpp b/fsfw/osal/linux/SemaphoreFactory.cpp new file mode 100644 index 0000000..e471093 --- /dev/null +++ b/fsfw/osal/linux/SemaphoreFactory.cpp @@ -0,0 +1,33 @@ +#include "../../tasks/SemaphoreFactory.h" +#include "BinarySemaphore.h" +#include "CountingSemaphore.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr; + +SemaphoreFactory::SemaphoreFactory() { +} + +SemaphoreFactory::~SemaphoreFactory() { + delete factoryInstance; +} + +SemaphoreFactory* SemaphoreFactory::instance() { + if (factoryInstance == nullptr){ + factoryInstance = new SemaphoreFactory(); + } + return SemaphoreFactory::factoryInstance; +} + +SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t arguments) { + return new BinarySemaphore(); +} + +SemaphoreIF* SemaphoreFactory::createCountingSemaphore(const uint8_t maxCount, + uint8_t initCount, uint32_t arguments) { + return new CountingSemaphore(maxCount, initCount); +} + +void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) { + delete semaphore; +} diff --git a/fsfw/osal/linux/TaskFactory.cpp b/fsfw/osal/linux/TaskFactory.cpp new file mode 100644 index 0000000..f507c76 --- /dev/null +++ b/fsfw/osal/linux/TaskFactory.cpp @@ -0,0 +1,42 @@ +#include "FixedTimeslotTask.h" +#include "PeriodicPosixTask.h" +#include "../../tasks/TaskFactory.h" +#include "../../returnvalues/HasReturnvaluesIF.h" + +//TODO: Different variant than the lazy loading in QueueFactory. What's better and why? +TaskFactory* TaskFactory::factoryInstance = new TaskFactory(); + +TaskFactory::~TaskFactory() { +} + +TaskFactory* TaskFactory::instance() { + return TaskFactory::factoryInstance; +} + +PeriodicTaskIF* TaskFactory::createPeriodicTask(TaskName name_, + TaskPriority taskPriority_,TaskStackSize stackSize_, + TaskPeriod periodInSeconds_, + TaskDeadlineMissedFunction deadLineMissedFunction_) { + return new PeriodicPosixTask(name_, taskPriority_,stackSize_, + periodInSeconds_ * 1000, deadLineMissedFunction_); +} + +FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(TaskName name_, + TaskPriority taskPriority_,TaskStackSize stackSize_, + TaskPeriod periodInSeconds_, + TaskDeadlineMissedFunction deadLineMissedFunction_) { + return new FixedTimeslotTask(name_, taskPriority_,stackSize_, + periodInSeconds_*1000); +} + +ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) { + //TODO not implemented + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t TaskFactory::delayTask(uint32_t delayMs){ + return PosixThread::sleep(delayMs*1000000ull); +} + +TaskFactory::TaskFactory() { +} diff --git a/fsfw/osal/linux/TcUnixUdpPollingTask.cpp b/fsfw/osal/linux/TcUnixUdpPollingTask.cpp new file mode 100644 index 0000000..af99ec9 --- /dev/null +++ b/fsfw/osal/linux/TcUnixUdpPollingTask.cpp @@ -0,0 +1,138 @@ +#include "TcUnixUdpPollingTask.h" +#include "../../globalfunctions/arrayprinter.h" + +TcUnixUdpPollingTask::TcUnixUdpPollingTask(object_id_t 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); + } +} + +TcUnixUdpPollingTask::~TcUnixUdpPollingTask() {} + +ReturnValue_t TcUnixUdpPollingTask::performOperation(uint8_t opCode) { + // Poll for new UDP datagrams in permanent loop. + while(1) { + //! Sender Address is cached here. + struct sockaddr_in senderAddress; + socklen_t senderSockLen = sizeof(senderAddress); + ssize_t bytesReceived = recvfrom(serverUdpSocket, + receptionBuffer.data(), frameSize, receptionFlags, + reinterpret_cast(&senderAddress), &senderSockLen); + if(bytesReceived < 0) { + // handle error + sif::error << "TcSocketPollingTask::performOperation: Reception" + "error." << std::endl; + handleReadError(); + + continue; + } +// sif::debug << "TcSocketPollingTask::performOperation: " << bytesReceived +// << " bytes received" << std::endl; + + ReturnValue_t result = handleSuccessfullTcRead(bytesReceived); + if(result != HasReturnvaluesIF::RETURN_FAILED) { + + } + tmtcBridge->registerCommConnect(); + tmtcBridge->checkAndSetClientAddress(senderAddress); + } + return HasReturnvaluesIF::RETURN_OK; +} + + +ReturnValue_t TcUnixUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) { + store_address_t storeId; + ReturnValue_t result = tcStore->addData(&storeId, + receptionBuffer.data(), bytesRead); + // arrayprinter::print(receptionBuffer.data(), bytesRead); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "TcSerialPollingTask::transferPusToSoftwareBus: Data " + "storage failed" << std::endl; + sif::error << "Packet size: " << bytesRead << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + + TmTcMessage message(storeId); + + result = MessageQueueSenderIF::sendMessage(targetTcDestination, &message); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "Serial Polling: Sending message to queue failed" + << std::endl; + tcStore->deleteData(storeId); + } + return result; +} + +ReturnValue_t TcUnixUdpPollingTask::initialize() { + tcStore = objectManager->get(objects::TC_STORE); + if (tcStore == nullptr) { + sif::error << "TcSerialPollingTask::initialize: TC Store uninitialized!" + << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + tmtcBridge = objectManager->get(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 TcUnixUdpPollingTask::initializeAfterTaskCreation() { + // Initialize the destination after task creation. This ensures + // that the destination will be set in the TMTC bridge. + targetTcDestination = tmtcBridge->getRequestQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + +void TcUnixUdpPollingTask::setTimeout(double timeoutSeconds) { + timeval tval; + tval = timevalOperations::toTimeval(timeoutSeconds); + int result = setsockopt(serverUdpSocket, SOL_SOCKET, SO_RCVTIMEO, + &tval, sizeof(receptionTimeout)); + if(result == -1) { + sif::error << "TcSocketPollingTask::TcSocketPollingTask: Setting " + "receive timeout failed with " << strerror(errno) << std::endl; + } +} + +// TODO: sleep after error detection to prevent spam +void TcUnixUdpPollingTask::handleReadError() { + switch(errno) { + case(EAGAIN): { + // todo: When working in timeout mode, this will occur more often + // and is not an error. + sif::error << "TcUnixUdpPollingTask::handleReadError: Timeout." + << std::endl; + break; + } + default: { + sif::error << "TcUnixUdpPollingTask::handleReadError: " + << strerror(errno) << std::endl; + } + } +} diff --git a/fsfw/osal/linux/TcUnixUdpPollingTask.h b/fsfw/osal/linux/TcUnixUdpPollingTask.h new file mode 100644 index 0000000..cc03256 --- /dev/null +++ b/fsfw/osal/linux/TcUnixUdpPollingTask.h @@ -0,0 +1,67 @@ +#ifndef FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_ +#define FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_ + +#include "../../objectmanager/SystemObject.h" +#include "../../osal/linux/TmTcUnixUdpBridge.h" +#include "../../tasks/ExecutableObjectIF.h" + +#include +#include + +/** + * @brief This class can be used to implement the polling of a Unix socket, + * using UDP for now. + * @details + * The task will be blocked while the specified number of bytes has not been + * received, so TC reception is handled inside a separate task. + * This class caches the IP address of the sender. It is assumed there + * is only one sender for now. + */ +class TcUnixUdpPollingTask: public SystemObject, + public ExecutableObjectIF { + friend class TmTcUnixUdpBridge; +public: + 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}; + + TcUnixUdpPollingTask(object_id_t objectId, object_id_t tmtcUnixUdpBridge, + size_t frameSize = 0, double timeoutSeconds = -1); + virtual~ TcUnixUdpPollingTask(); + + /** + * Turn on optional timeout for UDP polling. In the default mode, + * the receive function will block until a packet is received. + * @param timeoutSeconds + */ + void setTimeout(double timeoutSeconds); + + virtual ReturnValue_t performOperation(uint8_t opCode) override; + virtual ReturnValue_t initialize() override; + virtual ReturnValue_t initializeAfterTaskCreation() override; + +protected: + StorageManagerIF* tcStore = nullptr; + +private: + //! TMTC bridge is cached. + object_id_t tmtcBridgeId = objects::NO_OBJECT; + TmTcUnixUdpBridge* tmtcBridge = nullptr; + MessageQueueId_t targetTcDestination = MessageQueueIF::NO_QUEUE; + //! 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 receptionBuffer; + + size_t frameSize = 0; + timeval receptionTimeout; + + ReturnValue_t handleSuccessfullTcRead(size_t bytesRead); + void handleReadError(); +}; + +#endif /* FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_ */ diff --git a/fsfw/osal/linux/Timer.cpp b/fsfw/osal/linux/Timer.cpp new file mode 100644 index 0000000..ee964ba --- /dev/null +++ b/fsfw/osal/linux/Timer.cpp @@ -0,0 +1,42 @@ +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include +#include "Timer.h" + +Timer::Timer() { + sigevent sigEvent; + sigEvent.sigev_notify = SIGEV_NONE; + sigEvent.sigev_signo = 0; + sigEvent.sigev_value.sival_ptr = &timerId; + int status = timer_create(CLOCK_MONOTONIC, &sigEvent, &timerId); + if(status!=0){ + sif::error << "Timer creation failed with: " << status << + " errno: " << errno << std::endl; + } +} + +Timer::~Timer() { + timer_delete(timerId); +} + +int Timer::setTimer(uint32_t intervalMs) { + itimerspec timer; + timer.it_value.tv_sec = intervalMs / 1000; + timer.it_value.tv_nsec = (intervalMs * 1000000) % (1000000000); + timer.it_interval.tv_sec = 0; + timer.it_interval.tv_nsec = 0; + return timer_settime(timerId, 0, &timer, NULL); +} + + +int Timer::getTimer(uint32_t* remainingTimeMs){ + itimerspec timer; + timer.it_value.tv_sec = 0; + timer.it_value.tv_nsec = 0; + timer.it_interval.tv_sec = 0; + timer.it_interval.tv_nsec = 0; + int status = timer_gettime(timerId, &timer); + + *remainingTimeMs = timer.it_value.tv_sec * 1000 + timer.it_value.tv_nsec / 1000000; + + return status; +} diff --git a/fsfw/osal/linux/Timer.h b/fsfw/osal/linux/Timer.h new file mode 100644 index 0000000..f94bca5 --- /dev/null +++ b/fsfw/osal/linux/Timer.h @@ -0,0 +1,45 @@ +#ifndef FRAMEWORK_OSAL_LINUX_TIMER_H_ +#define FRAMEWORK_OSAL_LINUX_TIMER_H_ + +#include +#include +#include + +/** + * This class is a helper for the creation of a Clock Monotonic timer which does not trigger a signal + */ +class Timer { +public: + /** + * Creates the Timer sets the timerId Member + */ + Timer(); + /** + * Deletes the timer + * + * Careful! According to POSIX documentation: + * The treatment of any pending signal generated by the deleted timer is unspecified. + */ + virtual ~Timer(); + + /** + * Set the timer given in timerId to the given interval + * + * @param intervalMs Interval in ms to be set + * @return 0 on Success 1 else + */ + int setTimer(uint32_t intervalMs); + + /** + * Get the remaining time of the timer + * + * @param remainingTimeMs Pointer to integer value which is used to return the remaining time + * @return 0 on Success 1 else (see timer_getime documentation of posix function) + */ + int getTimer(uint32_t* remainingTimeMs); + +private: + timer_t timerId; +}; + +#endif /* FRAMEWORK_OSAL_LINUX_TIMER_H_ */ diff --git a/fsfw/osal/linux/TmTcUnixUdpBridge.cpp b/fsfw/osal/linux/TmTcUnixUdpBridge.cpp new file mode 100644 index 0000000..ab28623 --- /dev/null +++ b/fsfw/osal/linux/TmTcUnixUdpBridge.cpp @@ -0,0 +1,178 @@ +#include "TmTcUnixUdpBridge.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../ipc/MutexHelper.h" + +#include +#include + + +TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId, + object_id_t tcDestination, object_id_t tmStoreId, object_id_t tcStoreId, + uint16_t serverPort, uint16_t clientPort): + TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) { + mutex = MutexFactory::instance()->createMutex(); + + uint16_t setServerPort = DEFAULT_UDP_SERVER_PORT; + if(serverPort != 0xFFFF) { + setServerPort = serverPort; + } + + uint16_t setClientPort = DEFAULT_UDP_CLIENT_PORT; + if(clientPort != 0xFFFF) { + setClientPort = clientPort; + } + + // Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html + //clientSocket = socket(AF_INET, SOCK_DGRAM, 0); + serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if(serverSocket < 0) { + sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not open" + " UDP socket!" << std::endl; + handleSocketError(); + return; + } + + serverAddress.sin_family = AF_INET; + + // Accept packets from any interface. + //serverAddress.sin_addr.s_addr = inet_addr("127.73.73.0"); + serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); + serverAddress.sin_port = htons(setServerPort); + serverAddressLen = sizeof(serverAddress); + setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &serverSocketOptions, + sizeof(serverSocketOptions)); + + clientAddress.sin_family = AF_INET; + clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); + clientAddress.sin_port = htons(setClientPort); + clientAddressLen = sizeof(clientAddress); + + int result = bind(serverSocket, + reinterpret_cast(&serverAddress), + serverAddressLen); + if(result == -1) { + sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not bind " + "local port " << setServerPort << " to server socket!" + << std::endl; + handleBindError(); + return; + } +} + +TmTcUnixUdpBridge::~TmTcUnixUdpBridge() { +} + +ReturnValue_t TmTcUnixUdpBridge::sendTm(const uint8_t *data, size_t dataLen) { + int flags = 0; + + MutexHelper lock(mutex, MutexIF::TimeoutType::WAITING, 10); + + if(ipAddrAnySet){ + clientAddress.sin_addr.s_addr = htons(INADDR_ANY); + //clientAddress.sin_addr.s_addr = inet_addr("127.73.73.1"); + clientAddressLen = sizeof(serverAddress); + } + +// char ipAddress [15]; +// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET, +// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; + + ssize_t bytesSent = sendto(serverSocket, data, dataLen, flags, + reinterpret_cast(&clientAddress), clientAddressLen); + if(bytesSent < 0) { + sif::error << "TmTcUnixUdpBridge::sendTm: Send operation failed." + << std::endl; + handleSendError(); + } +// sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were" +// " sent." << std::endl; + return HasReturnvaluesIF::RETURN_OK; +} + +void TmTcUnixUdpBridge::checkAndSetClientAddress(sockaddr_in& newAddress) { + MutexHelper lock(mutex, MutexIF::TimeoutType::WAITING, 10); + +// char ipAddress [15]; +// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET, +// &newAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; +// sif::debug << "IP Address Old: " << inet_ntop(AF_INET, +// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; + + // 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; + clientAddressLen = sizeof(clientAddress); + } +} + + +void TmTcUnixUdpBridge::handleSocketError() { + // See: https://man7.org/linux/man-pages/man2/socket.2.html + switch(errno) { + case(EACCES): + case(EINVAL): + case(EMFILE): + case(ENFILE): + case(EAFNOSUPPORT): + case(ENOBUFS): + case(ENOMEM): + case(EPROTONOSUPPORT): + sif::error << "TmTcUnixBridge::handleSocketError: Socket creation failed" + << " with " << strerror(errno) << std::endl; + break; + default: + sif::error << "TmTcUnixBridge::handleSocketError: Unknown error" + << std::endl; + break; + } +} + +void TmTcUnixUdpBridge::handleBindError() { + // See: https://man7.org/linux/man-pages/man2/bind.2.html + switch(errno) { + case(EACCES): { + /* + Ephermeral ports can be shown with following command: + sysctl -A | grep ip_local_port_range + */ + sif::error << "TmTcUnixBridge::handleBindError: Port access issue." + "Ports 1-1024 are reserved on UNIX systems and require root " + "rights while ephermeral ports should not be used as well." + << std::endl; + } + break; + case(EADDRINUSE): + case(EBADF): + case(EINVAL): + case(ENOTSOCK): + case(EADDRNOTAVAIL): + case(EFAULT): + case(ELOOP): + case(ENAMETOOLONG): + case(ENOENT): + case(ENOMEM): + case(ENOTDIR): + case(EROFS): { + sif::error << "TmTcUnixBridge::handleBindError: Socket creation failed" + << " with " << strerror(errno) << std::endl; + break; + } + default: + sif::error << "TmTcUnixBridge::handleBindError: Unknown error" + << std::endl; + break; + } +} + +void TmTcUnixUdpBridge::handleSendError() { + switch(errno) { + default: + sif::error << "TmTcUnixBridge::handleSendError: " + << strerror(errno) << std::endl; + } +} + +void TmTcUnixUdpBridge::setClientAddressToAny(bool ipAddrAnySet){ + this->ipAddrAnySet = ipAddrAnySet; +} + diff --git a/fsfw/osal/linux/TmTcUnixUdpBridge.h b/fsfw/osal/linux/TmTcUnixUdpBridge.h new file mode 100644 index 0000000..ae6f6ad --- /dev/null +++ b/fsfw/osal/linux/TmTcUnixUdpBridge.h @@ -0,0 +1,51 @@ +#ifndef FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_ +#define FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_ + +#include "../../tmtcservices/AcceptsTelecommandsIF.h" +#include "../../tmtcservices/TmTcBridge.h" +#include +#include +#include + +class TmTcUnixUdpBridge: public TmTcBridge { + friend class TcUnixUdpPollingTask; +public: + // The ports chosen here should not be used by any other process. + // List of used ports on Linux: /etc/services + 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 tcDestination, + object_id_t tmStoreId, object_id_t tcStoreId, + uint16_t serverPort = 0xFFFF,uint16_t clientPort = 0xFFFF); + virtual~ TmTcUnixUdpBridge(); + + void checkAndSetClientAddress(sockaddr_in& clientAddress); + + void setClientAddressToAny(bool ipAddrAnySet); +protected: + virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override; + +private: + int serverSocket = 0; + + const int serverSocketOptions = 0; + + struct sockaddr_in clientAddress; + socklen_t clientAddressLen = 0; + + struct sockaddr_in serverAddress; + socklen_t serverAddressLen = 0; + + bool ipAddrAnySet = false; + + //! Access to the client address is mutex protected as it is set + //! by another task. + MutexIF* mutex; + + void handleSocketError(); + void handleBindError(); + void handleSendError(); +}; + +#endif /* FRAMEWORK_OSAL_LINUX_TMTCUNIXUDPBRIDGE_H_ */ diff --git a/fsfw/osal/rtems/Clock.cpp b/fsfw/osal/rtems/Clock.cpp new file mode 100644 index 0000000..e5f37ec --- /dev/null +++ b/fsfw/osal/rtems/Clock.cpp @@ -0,0 +1,192 @@ +#include "../../timemanager/Clock.h" +#include "RtemsBasic.h" +#include + +uint16_t Clock::leapSeconds = 0; +MutexIF* Clock::timeMutex = nullptr; + +uint32_t Clock::getTicksPerSecond(void){ + rtems_interval ticks_per_second = rtems_clock_get_ticks_per_second(); + return static_cast(ticks_per_second); +} + +ReturnValue_t Clock::setClock(const TimeOfDay_t* time) { + rtems_time_of_day timeRtems; + timeRtems.year = time->year; + timeRtems.month = time->month; + timeRtems.day = time->day; + timeRtems.hour = time->hour; + timeRtems.minute = time->minute; + timeRtems.second = time->second; + timeRtems.ticks = time->usecond * getTicksPerSecond() / 1e6; + rtems_status_code status = rtems_clock_set(&timeRtems); + switch(status){ + case RTEMS_SUCCESSFUL: + return HasReturnvaluesIF::RETURN_OK; + case RTEMS_INVALID_ADDRESS: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_CLOCK: + return HasReturnvaluesIF::RETURN_FAILED; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t Clock::setClock(const timeval* time) { + //TODO This routine uses _TOD_Set which is not + timespec newTime; + newTime.tv_sec = time->tv_sec; + newTime.tv_nsec = time->tv_usec * TOD_NANOSECONDS_PER_MICROSECOND; + //SHOULDDO: Not sure if we need to protect this call somehow (by thread lock or something). + //Uli: rtems docu says you can call this from an ISR, not sure if this means no protetion needed + //TODO Second parameter is ISR_lock_Context + _TOD_Set(&newTime,nullptr); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getClock_timeval(timeval* time) { + //Callable from ISR + rtems_status_code status = rtems_clock_get_tod_timeval(time); + switch(status){ + case RTEMS_SUCCESSFUL: + return HasReturnvaluesIF::RETURN_OK; + case RTEMS_NOT_DEFINED: + return HasReturnvaluesIF::RETURN_FAILED; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t Clock::getUptime(timeval* uptime) { + //According to docs.rtems.org for rtems 5 this method is more accurate than rtems_clock_get_ticks_since_boot + timespec time; + rtems_status_code status = rtems_clock_get_uptime(&time); + uptime->tv_sec = time.tv_sec; + time.tv_nsec = time.tv_nsec / 1000; + uptime->tv_usec = time.tv_nsec; + switch(status){ + case RTEMS_SUCCESSFUL: + return HasReturnvaluesIF::RETURN_OK; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) { + //This counter overflows after 50 days + *uptimeMs = rtems_clock_get_ticks_since_boot(); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getClock_usecs(uint64_t* time) { + timeval temp_time; + rtems_status_code returnValue = rtems_clock_get_tod_timeval(&temp_time); + *time = ((uint64_t) temp_time.tv_sec * 1000000) + temp_time.tv_usec; + switch(returnValue){ + case RTEMS_SUCCESSFUL: + return HasReturnvaluesIF::RETURN_OK; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) { + rtems_time_of_day* timeRtems = reinterpret_cast(time); + rtems_status_code status = rtems_clock_get_tod(timeRtems); + switch (status) { + case RTEMS_SUCCESSFUL: + return HasReturnvaluesIF::RETURN_OK; + case RTEMS_NOT_DEFINED: + //system date and time is not set + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_ADDRESS: + //time_buffer is NULL + return HasReturnvaluesIF::RETURN_FAILED; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from, + timeval* to) { + //Fails in 2038.. + rtems_time_of_day timeRtems; + timeRtems.year = from->year; + timeRtems.month = from->month; + timeRtems.day = from->day; + timeRtems.hour = from->hour; + timeRtems.minute = from->minute; + timeRtems.second = from->second; + timeRtems.ticks = from->usecond * getTicksPerSecond() / 1e6; + to->tv_sec = _TOD_To_seconds(&timeRtems); + to->tv_usec = from->usecond; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) { + *JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24. + / 3600.; + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) { + //SHOULDDO: works not for dates in the past (might have less leap seconds) + if (timeMutex == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + + uint16_t leapSeconds; + ReturnValue_t result = getLeapSeconds(&leapSeconds); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + timeval leapSeconds_timeval = { 0, 0 }; + leapSeconds_timeval.tv_sec = leapSeconds; + + //initial offset between UTC and TAI + timeval UTCtoTAI1972 = { 10, 0 }; + + timeval TAItoTT = { 32, 184000 }; + + *tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) { + if(checkOrCreateClockMutex()!=HasReturnvaluesIF::RETURN_OK){ + return HasReturnvaluesIF::RETURN_FAILED; + } + MutexHelper helper(timeMutex); + + + leapSeconds = leapSeconds_; + + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) { + if(timeMutex==nullptr){ + return HasReturnvaluesIF::RETURN_FAILED; + } + MutexHelper helper(timeMutex); + + *leapSeconds_ = leapSeconds; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t Clock::checkOrCreateClockMutex(){ + if(timeMutex==nullptr){ + MutexFactory* mutexFactory = MutexFactory::instance(); + if (mutexFactory == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + timeMutex = mutexFactory->createMutex(); + if (timeMutex == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/osal/rtems/CpuUsage.cpp b/fsfw/osal/rtems/CpuUsage.cpp new file mode 100644 index 0000000..6655c69 --- /dev/null +++ b/fsfw/osal/rtems/CpuUsage.cpp @@ -0,0 +1,183 @@ +#include "CpuUsage.h" +#include "../../serialize/SerialArrayListAdapter.h" +#include "../../serialize/SerializeAdapter.h" +#include + +extern "C" { +#include +} + +int handlePrint(void * token, const char *format, ...) { + CpuUsage *cpuUsage = (CpuUsage *) token; + + if (cpuUsage->counter == 0) { + //header + cpuUsage->counter++; + return 0; + } + + if (cpuUsage->counter % 2 == 1) { + { + //we can not tell when the last call is so we assume it be every uneven time + va_list vl; + va_start(vl, format); + float timeSinceLastReset = va_arg(vl,uint32_t); + uint32_t timeSinceLastResetDecimals = va_arg(vl,uint32_t); + + timeSinceLastReset = timeSinceLastReset + + (timeSinceLastResetDecimals / 1000.); + + cpuUsage->timeSinceLastReset = timeSinceLastReset; + + va_end(vl); + } + //task name and id + va_list vl; + va_start(vl, format); + + cpuUsage->cachedValue.id = va_arg(vl,uint32_t); + const char *name = va_arg(vl,const char *); + memcpy(cpuUsage->cachedValue.name, name, + CpuUsage::ThreadData::MAX_LENGTH_OF_THREAD_NAME); + + va_end(vl); + + } else { + //statistics + va_list vl; + va_start(vl, format); + float run = va_arg(vl,uint32_t); + uint32_t runDecimals = va_arg(vl,uint32_t); + float percent = va_arg(vl,uint32_t); + uint32_t percent_decimals = va_arg(vl,uint32_t); + + run = run + (runDecimals / 1000.); + percent = percent + (percent_decimals / 1000.); + + cpuUsage->cachedValue.percentUsage = percent; + cpuUsage->cachedValue.timeRunning = run; + + cpuUsage->threadData.insert(cpuUsage->cachedValue); + + va_end(vl); + } + cpuUsage->counter++; + + return 0; +} + +CpuUsage::CpuUsage() : + counter(0), timeSinceLastReset(0) { + +} + +CpuUsage::~CpuUsage() { + +} + +void CpuUsage::resetCpuUsage() { + rtems_cpu_usage_reset(); +} + +void CpuUsage::read() { + //rtems_cpu_usage_report_with_plugin(this, &handlePrint); +} + +void CpuUsage::clear() { + counter = 0; + timeSinceLastReset = 0; + threadData.clear(); +} + +ReturnValue_t CpuUsage::serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = SerializeAdapter::serialize( + &timeSinceLastReset, buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return SerialArrayListAdapter::serialize(&threadData, buffer, + size, maxSize, streamEndianness); +} + +uint32_t CpuUsage::getSerializedSize() const { + uint32_t size = 0; + + size += sizeof(timeSinceLastReset); + size += SerialArrayListAdapter::getSerializedSize(&threadData); + + return size; +} + +ReturnValue_t CpuUsage::deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + ReturnValue_t result = SerializeAdapter::deSerialize( + &timeSinceLastReset, buffer, size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return SerialArrayListAdapter::deSerialize(&threadData, buffer, + size, streamEndianness); +} + +ReturnValue_t CpuUsage::ThreadData::serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = SerializeAdapter::serialize(&id, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (*size + MAX_LENGTH_OF_THREAD_NAME > maxSize) { + return BUFFER_TOO_SHORT; + } + memcpy(*buffer, name, MAX_LENGTH_OF_THREAD_NAME); + *size += MAX_LENGTH_OF_THREAD_NAME; + *buffer += MAX_LENGTH_OF_THREAD_NAME; + result = SerializeAdapter::serialize(&timeRunning, + buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&percentUsage, + buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return HasReturnvaluesIF::RETURN_OK; +} + +uint32_t CpuUsage::ThreadData::getSerializedSize() const { + uint32_t size = 0; + + size += sizeof(id); + size += MAX_LENGTH_OF_THREAD_NAME; + size += sizeof(timeRunning); + size += sizeof(percentUsage); + + return size; +} + +ReturnValue_t CpuUsage::ThreadData::deSerialize(const uint8_t** buffer, + size_t* size, Endianness streamEndianness) { + ReturnValue_t result = SerializeAdapter::deSerialize(&id, buffer, + size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if ((*size = *size - MAX_LENGTH_OF_THREAD_NAME) < 0) { + return STREAM_TOO_SHORT; + } + memcpy(name, *buffer, MAX_LENGTH_OF_THREAD_NAME); + *buffer -= MAX_LENGTH_OF_THREAD_NAME; + result = SerializeAdapter::deSerialize(&timeRunning, + buffer, size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&percentUsage, + buffer, size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/osal/rtems/CpuUsage.h b/fsfw/osal/rtems/CpuUsage.h new file mode 100644 index 0000000..f487e19 --- /dev/null +++ b/fsfw/osal/rtems/CpuUsage.h @@ -0,0 +1,53 @@ +#ifndef CPUUSAGE_H_ +#define CPUUSAGE_H_ + +#include "../../container/FixedArrayList.h" +#include "../../serialize/SerializeIF.h" +#include + +class CpuUsage : public SerializeIF { +public: + static const uint8_t MAXIMUM_NUMBER_OF_THREADS = 30; + + class ThreadData: public SerializeIF { + public: + static const uint8_t MAX_LENGTH_OF_THREAD_NAME = 4; + + uint32_t id; + char name[MAX_LENGTH_OF_THREAD_NAME]; + float timeRunning; + float percentUsage; + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override; + + virtual size_t getSerializedSize() const override; + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; + }; + + CpuUsage(); + virtual ~CpuUsage(); + + uint8_t counter; + float timeSinceLastReset; + FixedArrayList threadData; + ThreadData cachedValue; + + static void resetCpuUsage(); + + void read(); + + void clear(); + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override; + + virtual size_t getSerializedSize() const override; + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; +}; + +#endif /* CPUUSAGE_H_ */ diff --git a/fsfw/osal/rtems/InitTask.cpp b/fsfw/osal/rtems/InitTask.cpp new file mode 100644 index 0000000..80afed3 --- /dev/null +++ b/fsfw/osal/rtems/InitTask.cpp @@ -0,0 +1,11 @@ +#include "InitTask.h" +#include "RtemsBasic.h" + + + +InitTask::InitTask() { +} + +InitTask::~InitTask() { + rtems_task_delete(RTEMS_SELF); +} diff --git a/fsfw/osal/rtems/InitTask.h b/fsfw/osal/rtems/InitTask.h new file mode 100644 index 0000000..2c8c0c6 --- /dev/null +++ b/fsfw/osal/rtems/InitTask.h @@ -0,0 +1,19 @@ +#ifndef OS_RTEMS_INITTASK_H_ +#define OS_RTEMS_INITTASK_H_ + +//TODO move into static function in TaskIF + +/** + * The init task is created automatically by RTEMS. + * As one may need to control it (e.g. suspending it for a while), + * this dummy class provides an implementation of TaskIF to do so. + * Warning: The init task is deleted with this stub, i.e. the destructor + * calls rtems_task_delete(RTEMS_SELF) + */ +class InitTask { +public: + InitTask(); + virtual ~InitTask(); +}; + +#endif /* OS_RTEMS_INITTASK_H_ */ diff --git a/fsfw/osal/rtems/InternalErrorCodes.cpp b/fsfw/osal/rtems/InternalErrorCodes.cpp new file mode 100644 index 0000000..ddf365d --- /dev/null +++ b/fsfw/osal/rtems/InternalErrorCodes.cpp @@ -0,0 +1,60 @@ +#include "../../osal/InternalErrorCodes.h" +#include + +ReturnValue_t InternalErrorCodes::translate(uint8_t code) { + switch (code) { + //TODO It looks like RTEMS-5 does not provide the same error codes +// case INTERNAL_ERROR_NO_CONFIGURATION_TABLE: +// return NO_CONFIGURATION_TABLE; +// case INTERNAL_ERROR_NO_CPU_TABLE: +// return NO_CPU_TABLE; +// case INTERNAL_ERROR_INVALID_WORKSPACE_ADDRESS: +// return INVALID_WORKSPACE_ADDRESS; + case INTERNAL_ERROR_TOO_LITTLE_WORKSPACE: + return TOO_LITTLE_WORKSPACE; +// case INTERNAL_ERROR_WORKSPACE_ALLOCATION: +// return WORKSPACE_ALLOCATION; +// case INTERNAL_ERROR_INTERRUPT_STACK_TOO_SMALL: +// return INTERRUPT_STACK_TOO_SMALL; + case INTERNAL_ERROR_THREAD_EXITTED: + return THREAD_EXITTED; + case INTERNAL_ERROR_INCONSISTENT_MP_INFORMATION: + return INCONSISTENT_MP_INFORMATION; + case INTERNAL_ERROR_INVALID_NODE: + return INVALID_NODE; + case INTERNAL_ERROR_NO_MPCI: + return NO_MPCI; + case INTERNAL_ERROR_BAD_PACKET: + return BAD_PACKET; + case INTERNAL_ERROR_OUT_OF_PACKETS: + return OUT_OF_PACKETS; + case INTERNAL_ERROR_OUT_OF_GLOBAL_OBJECTS: + return OUT_OF_GLOBAL_OBJECTS; + case INTERNAL_ERROR_OUT_OF_PROXIES: + return OUT_OF_PROXIES; + case INTERNAL_ERROR_INVALID_GLOBAL_ID: + return INVALID_GLOBAL_ID; + case INTERNAL_ERROR_BAD_STACK_HOOK: + return BAD_STACK_HOOK; +// case INTERNAL_ERROR_BAD_ATTRIBUTES: +// return BAD_ATTRIBUTES; +// case INTERNAL_ERROR_IMPLEMENTATION_KEY_CREATE_INCONSISTENCY: +// return IMPLEMENTATION_KEY_CREATE_INCONSISTENCY; +// case INTERNAL_ERROR_IMPLEMENTATION_BLOCKING_OPERATION_CANCEL: +// return IMPLEMENTATION_BLOCKING_OPERATION_CANCEL; +// case INTERNAL_ERROR_MUTEX_OBTAIN_FROM_BAD_STATE: +// return MUTEX_OBTAIN_FROM_BAD_STATE; +// case INTERNAL_ERROR_UNLIMITED_AND_MAXIMUM_IS_0: +// return UNLIMITED_AND_MAXIMUM_IS_0; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +InternalErrorCodes::InternalErrorCodes() { +} + +InternalErrorCodes::~InternalErrorCodes() { + +} + diff --git a/fsfw/osal/rtems/MessageQueue.cpp b/fsfw/osal/rtems/MessageQueue.cpp new file mode 100644 index 0000000..839182a --- /dev/null +++ b/fsfw/osal/rtems/MessageQueue.cpp @@ -0,0 +1,150 @@ +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "MessageQueue.h" +#include "RtemsBasic.h" +#include +MessageQueue::MessageQueue(size_t message_depth, size_t max_message_size) : + id(0), lastPartner(0), defaultDestination(NO_QUEUE), internalErrorReporter(nullptr) { + rtems_name name = ('Q' << 24) + (queueCounter++ << 8); + rtems_status_code status = rtems_message_queue_create(name, message_depth, + max_message_size, 0, &(this->id)); + if (status != RTEMS_SUCCESSFUL) { + sif::error << "MessageQueue::MessageQueue: Creating Queue " << std::hex + << name << std::dec << " failed with status:" + << (uint32_t) status << std::endl; + this->id = 0; + } +} + +MessageQueue::~MessageQueue() { + rtems_message_queue_delete(id); +} + +ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault) { + return sendMessageFrom(sendTo, message, this->getId(), ignoreFault); +} + +ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessageIF* message) { + return sendToDefaultFrom(message, this->getId()); +} + +ReturnValue_t MessageQueue::reply(MessageQueueMessageIF* message) { + if (this->lastPartner != 0) { + return sendMessage(this->lastPartner, message, this->getId()); + } else { + return NO_REPLY_PARTNER; + } +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t* receivedFrom) { + ReturnValue_t status = this->receiveMessage(message); + *receivedFrom = this->lastPartner; + return status; +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message) { + size_t size = 0; + rtems_status_code status = rtems_message_queue_receive(id, + message->getBuffer(),&size, + RTEMS_NO_WAIT, 1); + if (status == RTEMS_SUCCESSFUL) { + message->setMessageSize(size); + this->lastPartner = message->getSender(); + //Check size of incoming message. + if (message->getMessageSize() < message->getMinimumMessageSize()) { + return HasReturnvaluesIF::RETURN_FAILED; + } + } else { + //No message was received. Keep lastPartner anyway, I might send something later. + //But still, delete packet content. + memset(message->getData(), 0, message->getMaximumMessageSize()); + } + return convertReturnCode(status); +} + +MessageQueueId_t MessageQueue::getLastPartner() const { + return this->lastPartner; +} + +ReturnValue_t MessageQueue::flush(uint32_t* count) { + rtems_status_code status = rtems_message_queue_flush(id, count); + return convertReturnCode(status); +} + +MessageQueueId_t MessageQueue::getId() const { + return this->id; +} + +void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) { + this->defaultDestination = defaultDestination; +} + +ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + + message->setSender(sentFrom); + rtems_status_code result = rtems_message_queue_send(sendTo, + message->getBuffer(), message->getMessageSize()); + + //TODO: Check if we're in ISR. + if (result != RTEMS_SUCCESSFUL && !ignoreFault) { + if (internalErrorReporter == nullptr) { + internalErrorReporter = objectManager->get( + objects::INTERNAL_ERROR_REPORTER); + } + if (internalErrorReporter != nullptr) { + internalErrorReporter->queueMessageNotSent(); + } + } + + ReturnValue_t returnCode = convertReturnCode(result); + if(result == MessageQueueIF::EMPTY){ + return HasReturnvaluesIF::RETURN_FAILED; + } + + return returnCode; +} + +ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessageIF* message, + MessageQueueId_t sentFrom, bool ignoreFault) { + return sendMessageFrom(defaultDestination, message, sentFrom, ignoreFault); +} + +MessageQueueId_t MessageQueue::getDefaultDestination() const { + return this->defaultDestination; +} + +bool MessageQueue::isDefaultDestinationSet() const { + return (defaultDestination != NO_QUEUE); +} + +ReturnValue_t MessageQueue::convertReturnCode(rtems_status_code inValue){ + switch(inValue){ + case RTEMS_SUCCESSFUL: + return HasReturnvaluesIF::RETURN_OK; + case RTEMS_INVALID_ID: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_TIMEOUT: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_OBJECT_WAS_DELETED: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_ADDRESS: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_SIZE: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_TOO_MANY: + return MessageQueueIF::FULL; + case RTEMS_UNSATISFIED: + return MessageQueueIF::EMPTY; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + +} + + + +uint16_t MessageQueue::queueCounter = 0; diff --git a/fsfw/osal/rtems/MessageQueue.h b/fsfw/osal/rtems/MessageQueue.h new file mode 100644 index 0000000..342f1e3 --- /dev/null +++ b/fsfw/osal/rtems/MessageQueue.h @@ -0,0 +1,172 @@ +#ifndef FSFW_OSAL_RTEMS_MESSAGEQUEUE_H_ +#define FSFW_OSAL_RTEMS_MESSAGEQUEUE_H_ + +#include "../../internalError/InternalErrorReporterIF.h" +#include "../../ipc/MessageQueueIF.h" +#include "../../ipc/MessageQueueMessage.h" +#include "RtemsBasic.h" + +/** + * @brief This class manages sending and receiving of message queue messages. + * + * @details Message queues are used to pass asynchronous messages between processes. + * They work like post boxes, where all incoming messages are stored in FIFO + * order. This class creates a new receiving queue and provides methods to fetch + * received messages. Being a child of MessageQueueSender, this class also provides + * methods to send a message to a user-defined or a default destination. In addition + * it also provides a reply method to answer to the queue it received its last message + * from. + * The MessageQueue should be used as "post box" for a single owning object. So all + * message queue communication is "n-to-one". + * For creating the queue, as well as sending and receiving messages, the class makes + * use of the operating system calls provided. + * \ingroup message_queue + */ +class MessageQueue : public MessageQueueIF { +public: + /** + * @brief The constructor initializes and configures the message queue. + * @details By making use of the according operating system call, a message queue is created + * and initialized. The message depth - the maximum number of messages to be + * buffered - may be set with the help of a parameter, whereas the message size is + * automatically set to the maximum message queue message size. The operating system + * sets the message queue id, or i case of failure, it is set to zero. + * @param message_depth The number of messages to be buffered before passing an error to the + * sender. Default is three. + * @param max_message_size With this parameter, the maximum message size can be adjusted. + * This should be left default. + */ + MessageQueue( size_t message_depth = 3, size_t max_message_size = MessageQueueMessage::MAX_MESSAGE_SIZE ); + /** + * @brief The destructor deletes the formerly created message queue. + * @details This is accomplished by using the delete call provided by the operating system. + */ + virtual ~MessageQueue(); + /** + * @brief This operation sends a message to the given destination. + * @details It directly uses the sendMessage call of the MessageQueueSender parent, but passes its + * queue id as "sentFrom" parameter. + * @param sendTo This parameter specifies the message queue id of the destination message queue. + * @param message A pointer to a previously created message, which is sent. + * @param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full. + */ + ReturnValue_t sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, bool ignoreFault = false ); + /** + * @brief This operation sends a message to the default destination. + * @details As in the sendMessage method, this function uses the sendToDefault call of the + * MessageQueueSender parent class and adds its queue id as "sentFrom" information. + * @param message A pointer to a previously created message, which is sent. + */ + ReturnValue_t sendToDefault( MessageQueueMessageIF* message ); + /** + * @brief This operation sends a message to the last communication partner. + * @details This operation simplifies answering an incoming message by using the stored + * lastParnter information as destination. If there was no message received yet + * (i.e. lastPartner is zero), an error code is returned. + * @param message A pointer to a previously created message, which is sent. + */ + ReturnValue_t reply( MessageQueueMessageIF* message ); + + /** + * @brief This function reads available messages from the message queue and returns the sender. + * @details It works identically to the other receiveMessage call, but in addition returns the + * sender's queue id. + * @param message A pointer to a message in which the received data is stored. + * @param receivedFrom A pointer to a queue id in which the sender's id is stored. + */ + ReturnValue_t receiveMessage(MessageQueueMessageIF* message, + MessageQueueId_t *receivedFrom); + + /** + * @brief This function reads available messages from the message queue. + * @details If data is available it is stored in the passed message pointer. The message's + * original content is overwritten and the sendFrom information is stored in the + * lastPartner attribute. Else, the lastPartner information remains untouched, the + * message's content is cleared and the function returns immediately. + * @param message A pointer to a message in which the received data is stored. + */ + ReturnValue_t receiveMessage(MessageQueueMessageIF* message); + /** + * Deletes all pending messages in the queue. + * @param count The number of flushed messages. + * @return RETURN_OK on success. + */ + ReturnValue_t flush(uint32_t* count); + /** + * @brief This method returns the message queue id of the last communication partner. + */ + MessageQueueId_t getLastPartner() const; + /** + * @brief This method returns the message queue id of this class's message queue. + */ + MessageQueueId_t getId() const; + /** + * \brief With the sendMessage call, a queue message is sent to a receiving queue. + * \details This method takes the message provided, adds the sentFrom information and passes + * it on to the destination provided with an operating system call. The OS's return + * value is returned. + * \param sendTo This parameter specifies the message queue id to send the message to. + * \param message This is a pointer to a previously created message, which is sent. + * \param sentFrom The sentFrom information can be set to inject the sender's queue id into the message. + * This variable is set to zero by default. + * \param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full. + */ + virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo, MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE, bool ignoreFault = false ); + /** + * \brief The sendToDefault method sends a queue message to the default destination. + * \details In all other aspects, it works identical to the sendMessage method. + * \param message This is a pointer to a previously created message, which is sent. + * \param sentFrom The sentFrom information can be set to inject the sender's queue id into the message. + * This variable is set to zero by default. + */ + virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE, bool ignoreFault = false ); + /** + * \brief This method is a simple setter for the default destination. + */ + void setDefaultDestination(MessageQueueId_t defaultDestination); + /** + * \brief This method is a simple getter for the default destination. + */ + MessageQueueId_t getDefaultDestination() const; + + bool isDefaultDestinationSet() const; +private: + /** + * @brief The class stores the queue id it got assigned from the operating system in this attribute. + * If initialization fails, the queue id is set to zero. + */ + MessageQueueId_t id; + /** + * @brief In this attribute, the queue id of the last communication partner is stored + * to allow for replying. + */ + MessageQueueId_t lastPartner; + /** + * @brief The message queue's name -a user specific information for the operating system- is + * generated automatically with the help of this static counter. + */ + /** + * \brief This attribute stores a default destination to send messages to. + * \details It is stored to simplify sending to always-the-same receiver. The attribute may + * be set in the constructor or by a setter call to setDefaultDestination. + */ + MessageQueueId_t defaultDestination; + + /** + * \brief This attribute stores a reference to the internal error reporter for reporting full queues. + * \details In the event of a full destination queue, the reporter will be notified. The reference is set + * by lazy loading + */ + InternalErrorReporterIF *internalErrorReporter; + + static uint16_t queueCounter; + /** + * A method to convert an OS-specific return code to the frameworks return value concept. + * @param inValue The return code coming from the OS. + * @return The converted return value. + */ + static ReturnValue_t convertReturnCode(rtems_status_code inValue); +}; + +#endif /* FSFW_OSAL_RTEMS_MESSAGEQUEUE_H_ */ diff --git a/fsfw/osal/rtems/MultiObjectTask.cpp b/fsfw/osal/rtems/MultiObjectTask.cpp new file mode 100644 index 0000000..970d01e --- /dev/null +++ b/fsfw/osal/rtems/MultiObjectTask.cpp @@ -0,0 +1,88 @@ +/** + * @file MultiObjectTask.cpp + * @brief This file defines the MultiObjectTask class. + * @date 30.01.2014 + * @author baetz + */ + +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tasks/ExecutableObjectIF.h" +#include "MultiObjectTask.h" + +MultiObjectTask::MultiObjectTask(const char *name, rtems_task_priority setPriority, + size_t setStack, rtems_interval setPeriod, void (*setDeadlineMissedFunc)()) : + TaskBase(setPriority, setStack, name), periodTicks( + RtemsBasic::convertMsToTicks(setPeriod)), periodId(0), deadlineMissedFunc( + setDeadlineMissedFunc) { +} + +MultiObjectTask::~MultiObjectTask(void) { + //Do not delete objects, we were responsible for ptrs only. + rtems_rate_monotonic_delete(periodId); +} +rtems_task MultiObjectTask::taskEntryPoint(rtems_task_argument argument) { + //The argument is re-interpreted as MultiObjectTask. The Task object is global, so it is found from any place. + MultiObjectTask *originalTask(reinterpret_cast(argument)); + originalTask->taskFunctionality(); +} + +ReturnValue_t MultiObjectTask::startTask() { + rtems_status_code status = rtems_task_start(id, MultiObjectTask::taskEntryPoint, + rtems_task_argument((void *) this)); + if (status != RTEMS_SUCCESSFUL) { + sif::error << "ObjectTask::startTask for " << std::hex << this->getId() + << std::dec << " failed." << std::endl; + } + switch(status){ + case RTEMS_SUCCESSFUL: + //ask started successfully + return HasReturnvaluesIF::RETURN_OK; + default: +/* RTEMS_INVALID_ADDRESS - invalid task entry point + RTEMS_INVALID_ID - invalid task id + RTEMS_INCORRECT_STATE - task not in the dormant state + RTEMS_ILLEGAL_ON_REMOTE_OBJECT - cannot start remote task */ + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t MultiObjectTask::sleepFor(uint32_t ms) { + return TaskBase::sleepFor(ms); +} + +void MultiObjectTask::taskFunctionality() { + TaskBase::setAndStartPeriod(periodTicks,&periodId); + //The task's "infinite" inner loop is entered. + while (1) { + for (ObjectList::iterator it = objectList.begin(); + it != objectList.end(); ++it) { + (*it)->performOperation(); + } + rtems_status_code status = TaskBase::restartPeriod(periodTicks,periodId); + if (status == RTEMS_TIMEOUT) { + char nameSpace[8] = { 0 }; + char* ptr = rtems_object_get_name(getId(), sizeof(nameSpace), + nameSpace); + sif::error << "ObjectTask: " << ptr << " Deadline missed." << std::endl; + if (this->deadlineMissedFunc != nullptr) { + this->deadlineMissedFunc(); + } + } + } +} + +ReturnValue_t MultiObjectTask::addComponent(object_id_t object) { + ExecutableObjectIF* newObject = objectManager->get( + object); + if (newObject == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + objectList.push_back(newObject); + newObject->setTaskIF(this); + + return HasReturnvaluesIF::RETURN_OK; +} + +uint32_t MultiObjectTask::getPeriodMs() const { + return RtemsBasic::convertTicksToMs(periodTicks); +} diff --git a/fsfw/osal/rtems/MultiObjectTask.h b/fsfw/osal/rtems/MultiObjectTask.h new file mode 100644 index 0000000..04d122a --- /dev/null +++ b/fsfw/osal/rtems/MultiObjectTask.h @@ -0,0 +1,107 @@ +#ifndef FSFW_OSAL_RTEMS_MULTIOBJECTTASK_H_ +#define FSFW_OSAL_RTEMS_MULTIOBJECTTASK_H_ + +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../tasks/PeriodicTaskIF.h" + +#include "TaskBase.h" +#include + +class ExecutableObjectIF; + +/** + * @brief This class represents a specialized task for periodic activities of multiple objects. + * + * @details MultiObjectTask is an extension to ObjectTask in the way that it is able to execute + * multiple objects that implement the ExecutableObjectIF interface. The objects must be + * added prior to starting the task. + * @author baetz + * @ingroup task_handling + */ +class MultiObjectTask: public TaskBase, public PeriodicTaskIF { +public: + /** + * @brief Standard constructor of the class. + * @details The class is initialized without allocated objects. These need to be added + * with #addObject. + * In the underlying TaskBase class, a new operating system task is created. + * In addition to the TaskBase parameters, the period, the pointer to the + * aforementioned initialization function and an optional "deadline-missed" + * function pointer is passed. + * @param priority Sets the priority of a task. Values range from a low 0 to a high 99. + * @param stack_size The stack size reserved by the operating system for the task. + * @param setPeriod The length of the period with which the task's functionality will be + * executed. It is expressed in clock ticks. + * @param setDeadlineMissedFunc The function pointer to the deadline missed function + * that shall be assigned. + */ + MultiObjectTask(const char *name, rtems_task_priority setPriority, size_t setStack, rtems_interval setPeriod, + void (*setDeadlineMissedFunc)()); + /** + * @brief Currently, the executed object's lifetime is not coupled with the task object's + * lifetime, so the destructor is empty. + */ + virtual ~MultiObjectTask(void); + + /** + * @brief The method to start the task. + * @details The method starts the task with the respective system call. + * Entry point is the taskEntryPoint method described below. + * The address of the task object is passed as an argument + * to the system call. + */ + ReturnValue_t startTask(void); + /** + * Adds an object to the list of objects to be executed. + * The objects are executed in the order added. + * @param object Id of the object to add. + * @return RETURN_OK on success, RETURN_FAILED if the object could not be added. + */ + ReturnValue_t addComponent(object_id_t object) override; + + uint32_t getPeriodMs() const override; + + ReturnValue_t sleepFor(uint32_t ms) override; +protected: + typedef std::vector ObjectList; //!< Typedef for the List of objects. + /** + * @brief This attribute holds a list of objects to be executed. + */ + ObjectList objectList; + /** + * @brief The period of the task. + * @details The period determines the frequency of the task's execution. It is expressed in clock ticks. + */ + rtems_interval periodTicks; + /** + * @brief id of the associated OS period + */ + rtems_id periodId; + /** + * @brief The pointer to the deadline-missed function. + * @details This pointer stores the function that is executed if the task's deadline is missed. + * So, each may react individually on a timing failure. The pointer may be nullptr, + * then nothing happens on missing the deadline. The deadline is equal to the next execution + * of the periodic task. + */ + void (*deadlineMissedFunc)(void); + /** + * @brief This is the function executed in the new task's context. + * @details It converts the argument back to the thread object type and copies the class instance + * to the task context. The taskFunctionality method is called afterwards. + * @param A pointer to the task object itself is passed as argument. + */ + static rtems_task taskEntryPoint(rtems_task_argument argument); + /** + * @brief The function containing the actual functionality of the task. + * @details The method sets and starts + * the task's period, then enters a loop that is repeated as long as the isRunning + * attribute is true. Within the loop, all performOperation methods of the added + * objects are called. Afterwards the checkAndRestartPeriod system call blocks the task + * until the next period. + * On missing the deadline, the deadlineMissedFunction is executed. + */ + void taskFunctionality(void); +}; + +#endif /* FSFW_OSAL_RTEMS_MULTIOBJECTTASK_H_ */ diff --git a/fsfw/osal/rtems/Mutex.cpp b/fsfw/osal/rtems/Mutex.cpp new file mode 100644 index 0000000..a5ec963 --- /dev/null +++ b/fsfw/osal/rtems/Mutex.cpp @@ -0,0 +1,79 @@ +#include "Mutex.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +uint8_t Mutex::count = 0; + +Mutex::Mutex() : + mutexId(0) { + rtems_name mutexName = ('M' << 24) + ('T' << 16) + ('X' << 8) + count++; + rtems_status_code status = rtems_semaphore_create(mutexName, 1, + RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY, 0, + &mutexId); + if (status != RTEMS_SUCCESSFUL) { + sif::error << "Mutex: creation with name, id " << mutexName << ", " << mutexId + << " failed with " << status << std::endl; + } +} + +Mutex::~Mutex() { + rtems_status_code status = rtems_semaphore_delete(mutexId); + if (status != RTEMS_SUCCESSFUL) { + sif::error << "Mutex: deletion for id " << mutexId + << " failed with " << status << std::endl; + } +} + +ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType = + TimeoutType::BLOCKING, uint32_t timeoutMs) { + rtems_status_code status = RTEMS_INVALID_ID; + if(timeoutMs == MutexIF::TimeoutType::BLOCKING) { + status = rtems_semaphore_obtain(mutexId, + RTEMS_WAIT, RTEMS_NO_TIMEOUT); + } + else if(timeoutMs == MutexIF::TimeoutType::POLLING) { + timeoutMs = RTEMS_NO_TIMEOUT; + status = rtems_semaphore_obtain(mutexId, + RTEMS_NO_WAIT, 0); + } + else { + status = rtems_semaphore_obtain(mutexId, + RTEMS_WAIT, timeoutMs); + } + + switch(status){ + case RTEMS_SUCCESSFUL: + //semaphore obtained successfully + return HasReturnvaluesIF::RETURN_OK; + case RTEMS_UNSATISFIED: + //semaphore not available + return MUTEX_NOT_FOUND; + case RTEMS_TIMEOUT: + //timed out waiting for semaphore + return MUTEX_TIMEOUT; + case RTEMS_OBJECT_WAS_DELETED: + //semaphore deleted while waiting + return MUTEX_DESTROYED_WHILE_WAITING; + case RTEMS_INVALID_ID: + //invalid semaphore id + return MUTEX_INVALID_ID; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t Mutex::unlockMutex() { + rtems_status_code status = rtems_semaphore_release(mutexId); + switch(status){ + case RTEMS_SUCCESSFUL: + //semaphore obtained successfully + return HasReturnvaluesIF::RETURN_OK; + case RTEMS_NOT_OWNER_OF_RESOURCE: + //semaphore not available + return CURR_THREAD_DOES_NOT_OWN_MUTEX; + case RTEMS_INVALID_ID: + //invalid semaphore id + return MUTEX_INVALID_ID; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} diff --git a/fsfw/osal/rtems/Mutex.h b/fsfw/osal/rtems/Mutex.h new file mode 100644 index 0000000..4c86131 --- /dev/null +++ b/fsfw/osal/rtems/Mutex.h @@ -0,0 +1,18 @@ +#ifndef FSFW_OSAL_RTEMS_MUTEX_H_ +#define FSFW_OSAL_RTEMS_MUTEX_H_ + +#include "../../ipc/MutexIF.h" +#include "RtemsBasic.h" + +class Mutex : public MutexIF { +public: + Mutex(); + ~Mutex(); + ReturnValue_t lockMutex(TimeoutType timeoutType, uint32_t timeoutMs = 0); + ReturnValue_t unlockMutex(); +private: + rtems_id mutexId; + static uint8_t count; +}; + +#endif /* FSFW_OSAL_RTEMS_MUTEX_H_ */ diff --git a/fsfw/osal/rtems/MutexFactory.cpp b/fsfw/osal/rtems/MutexFactory.cpp new file mode 100644 index 0000000..24af5fa --- /dev/null +++ b/fsfw/osal/rtems/MutexFactory.cpp @@ -0,0 +1,23 @@ +#include "../../ipc/MutexFactory.h" +#include "Mutex.h" +#include "RtemsBasic.h" + +MutexFactory* MutexFactory::factoryInstance = new MutexFactory(); + +MutexFactory::MutexFactory() { +} + +MutexFactory::~MutexFactory() { +} + +MutexFactory* MutexFactory::instance() { + return MutexFactory::factoryInstance; +} + +MutexIF* MutexFactory::createMutex() { + return new Mutex(); +} + +void MutexFactory::deleteMutex(MutexIF* mutex) { + delete mutex; +} diff --git a/fsfw/osal/rtems/PollingTask.cpp b/fsfw/osal/rtems/PollingTask.cpp new file mode 100644 index 0000000..db7864f --- /dev/null +++ b/fsfw/osal/rtems/PollingTask.cpp @@ -0,0 +1,123 @@ +#include "../../tasks/FixedSequenceSlot.h" +#include "../../objectmanager/SystemObjectIF.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "PollingTask.h" +#include "RtemsBasic.h" +#include "../../returnvalues/HasReturnvaluesIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +uint32_t PollingTask::deadlineMissedCount = 0; + +PollingTask::PollingTask(const char *name, rtems_task_priority setPriority, + size_t setStack, uint32_t setOverallPeriod, + void (*setDeadlineMissedFunc)()) : + TaskBase(setPriority, setStack, name), periodId(0), pst( + setOverallPeriod) { + // All additional attributes are applied to the object. + this->deadlineMissedFunc = setDeadlineMissedFunc; +} + +PollingTask::~PollingTask() { +} + +rtems_task PollingTask::taskEntryPoint(rtems_task_argument argument) { + + //The argument is re-interpreted as PollingTask. + PollingTask *originalTask(reinterpret_cast(argument)); + //The task's functionality is called. + originalTask->taskFunctionality(); + sif::debug << "Polling task " << originalTask->getId() + << " returned from taskFunctionality." << std::endl; +} + +void PollingTask::missedDeadlineCounter() { + PollingTask::deadlineMissedCount++; + if (PollingTask::deadlineMissedCount % 10 == 0) { + sif::error << "PST missed " << PollingTask::deadlineMissedCount + << " deadlines." << std::endl; + } +} + +ReturnValue_t PollingTask::startTask() { + rtems_status_code status = rtems_task_start(id, PollingTask::taskEntryPoint, + rtems_task_argument((void *) this)); + if (status != RTEMS_SUCCESSFUL) { + sif::error << "PollingTask::startTask for " << std::hex << this->getId() + << std::dec << " failed." << std::endl; + } + switch(status){ + case RTEMS_SUCCESSFUL: + //ask started successfully + return HasReturnvaluesIF::RETURN_OK; + default: +/* RTEMS_INVALID_ADDRESS - invalid task entry point + RTEMS_INVALID_ID - invalid task id + RTEMS_INCORRECT_STATE - task not in the dormant state + RTEMS_ILLEGAL_ON_REMOTE_OBJECT - cannot start remote task */ + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t PollingTask::addSlot(object_id_t componentId, + uint32_t slotTimeMs, int8_t executionStep) { + ExecutableObjectIF* object = objectManager->get(componentId); + if (object != nullptr) { + pst.addSlot(componentId, slotTimeMs, executionStep, object, this); + return HasReturnvaluesIF::RETURN_OK; + } + + sif::error << "Component " << std::hex << componentId << + " not found, not adding it to pst" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; +} + +uint32_t PollingTask::getPeriodMs() const { + return pst.getLengthMs(); +} + +ReturnValue_t PollingTask::checkSequence() const { + return pst.checkSequence(); +} + +#include + +void PollingTask::taskFunctionality() { + // A local iterator for the Polling Sequence Table is created to find the start time for the first entry. + FixedSlotSequence::SlotListIter it = pst.current; + + //The start time for the first entry is read. + rtems_interval interval = RtemsBasic::convertMsToTicks(it->pollingTimeMs); + TaskBase::setAndStartPeriod(interval,&periodId); + //The task's "infinite" inner loop is entered. + while (1) { + if (pst.slotFollowsImmediately()) { + //Do nothing + } else { + //The interval for the next polling slot is selected. + interval = RtemsBasic::convertMsToTicks(this->pst.getIntervalToNextSlotMs()); + //The period is checked and restarted with the new interval. + //If the deadline was missed, the deadlineMissedFunc is called. + rtems_status_code status = TaskBase::restartPeriod(interval,periodId); + if (status == RTEMS_TIMEOUT) { + if (this->deadlineMissedFunc != nullptr) { + this->deadlineMissedFunc(); + } + } + } + //The device handler for this slot is executed and the next one is chosen. + this->pst.executeAndAdvance(); + } +} + +ReturnValue_t PollingTask::sleepFor(uint32_t ms){ + return TaskBase::sleepFor(ms); +}; diff --git a/fsfw/osal/rtems/PollingTask.h b/fsfw/osal/rtems/PollingTask.h new file mode 100644 index 0000000..42deaf0 --- /dev/null +++ b/fsfw/osal/rtems/PollingTask.h @@ -0,0 +1,85 @@ +#ifndef FSFW_OSAL_RTEMS_POLLINGTASK_H_ +#define FSFW_OSAL_RTEMS_POLLINGTASK_H_ + +#include "../../tasks/FixedSlotSequence.h" +#include "../../tasks/FixedTimeslotTaskIF.h" +#include "TaskBase.h" + +class PollingTask: public TaskBase, public FixedTimeslotTaskIF { + public: + /** + * @brief The standard constructor of the class. + * + * @details This is the general constructor of the class. In addition to the TaskBase parameters, + * the following variables are passed: + * + * @param (*setDeadlineMissedFunc)() The function pointer to the deadline missed function that shall be assigned. + * + * @param getPst The object id of the completely initialized polling sequence. + */ + PollingTask( const char *name, rtems_task_priority setPriority, size_t setStackSize, uint32_t overallPeriod, void (*setDeadlineMissedFunc)()); + + /** + * @brief The destructor of the class. + * + * @details The destructor frees all heap memory that was allocated on thread initialization for the PST and + * the device handlers. This is done by calling the PST's destructor. + */ + virtual ~PollingTask( void ); + + ReturnValue_t startTask( void ); + /** + * This static function can be used as #deadlineMissedFunc. + * It counts missedDeadlines and prints the number of missed deadlines every 10th time. + */ + static void missedDeadlineCounter(); + /** + * A helper variable to count missed deadlines. + */ + static uint32_t deadlineMissedCount; + + ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs, int8_t executionStep); + + uint32_t getPeriodMs() const; + + ReturnValue_t checkSequence() const; + + ReturnValue_t sleepFor(uint32_t ms); +protected: + /** + * @brief id of the associated OS period + */ + rtems_id periodId; + + FixedSlotSequence pst; + + /** + * @brief This attribute holds a function pointer that is executed when a deadline was missed. + * + * @details Another function may be announced to determine the actions to perform when a deadline was missed. + * Currently, only one function for missing any deadline is allowed. + * If not used, it shall be declared NULL. + */ + void ( *deadlineMissedFunc )( void ); + /** + * @brief This is the entry point in a new polling thread. + * + * @details This method, that is the generalOSAL::checkAndRestartPeriod( this->periodId, interval ); entry point in the new thread, is here set to generate + * and link the Polling Sequence Table to the thread object and start taskFunctionality() + * on success. If operation of the task is ended for some reason, + * the destructor is called to free allocated memory. + */ + static rtems_task taskEntryPoint( rtems_task_argument argument ); + + /** + * @brief This function holds the main functionality of the thread. + * + * + * @details Holding the main functionality of the task, this method is most important. + * It links the functionalities provided by FixedSlotSequence with the OS's System Calls + * to keep the timing of the periods. + */ + void taskFunctionality( void ); +}; + +#endif /* FSFW_OSAL_RTEMS_POLLINGTASK_H_ */ diff --git a/fsfw/osal/rtems/QueueFactory.cpp b/fsfw/osal/rtems/QueueFactory.cpp new file mode 100644 index 0000000..ff56130 --- /dev/null +++ b/fsfw/osal/rtems/QueueFactory.cpp @@ -0,0 +1,60 @@ +#include "../../ipc/QueueFactory.h" +#include "../../ipc/MessageQueueSenderIF.h" +#include "MessageQueue.h" +#include "RtemsBasic.h" + +QueueFactory* QueueFactory::factoryInstance = nullptr; + + +ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessageIF* message, MessageQueueId_t sentFrom,bool ignoreFault) { + //TODO add ignoreFault functionality + message->setSender(sentFrom); + rtems_status_code result = rtems_message_queue_send(sendTo, message->getBuffer(), + message->getMessageSize()); + switch(result){ + case RTEMS_SUCCESSFUL: + //message sent successfully + return HasReturnvaluesIF::RETURN_OK; + case RTEMS_INVALID_ID: + //invalid queue id + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_SIZE: + // invalid message size + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_ADDRESS: + //buffer is NULL + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_UNSATISFIED: + //out of message buffers + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_TOO_MANY: + //queue's limit has been reached + return MessageQueueIF::FULL; + + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +QueueFactory* QueueFactory::instance() { + if (factoryInstance == nullptr) { + factoryInstance = new QueueFactory; + } + return factoryInstance; +} + +QueueFactory::QueueFactory() { +} + +QueueFactory::~QueueFactory() { +} + +MessageQueueIF* QueueFactory::createMessageQueue(uint32_t messageDepth, + size_t maxMessageSize) { + return new MessageQueue(messageDepth, maxMessageSize); +} + +void QueueFactory::deleteMessageQueue(MessageQueueIF* queue) { + delete queue; +} diff --git a/fsfw/osal/rtems/RtemsBasic.cpp b/fsfw/osal/rtems/RtemsBasic.cpp new file mode 100644 index 0000000..17cb345 --- /dev/null +++ b/fsfw/osal/rtems/RtemsBasic.cpp @@ -0,0 +1,70 @@ +#include "RtemsBasic.h" + + +//ReturnValue_t RtemsBasic::convertReturnCode(rtems_status_code inValue) { +// if (inValue == RTEMS_SUCCESSFUL) { +// return HasReturnvaluesIF::RETURN_OK; +// } else { +// switch(inValue){ +// case RTEMS_SUCCESSFUL: +// return OperatingSystemIF::SUCCESSFUL; +// case RTEMS_TASK_EXITTED: +// return OperatingSystemIF::TASK_EXITTED; +// case RTEMS_MP_NOT_CONFIGURED: +// return OperatingSystemIF::MP_NOT_CONFIGURED; +// case RTEMS_INVALID_NAME: +// return OperatingSystemIF::INVALID_NAME; +// case RTEMS_INVALID_ID: +// return OperatingSystemIF::INVALID_ID; +// case RTEMS_TOO_MANY: +// return OperatingSystemIF::TOO_MANY; +// case RTEMS_TIMEOUT: +// return OperatingSystemIF::TIMEOUT; +// case RTEMS_OBJECT_WAS_DELETED: +// return OperatingSystemIF::OBJECT_WAS_DELETED; +// case RTEMS_INVALID_SIZE: +// return OperatingSystemIF::INVALID_SIZE; +// case RTEMS_INVALID_ADDRESS: +// return OperatingSystemIF::INVALID_ADDRESS; +// case RTEMS_INVALID_NUMBER: +// return OperatingSystemIF::INVALID_NUMBER; +// case RTEMS_NOT_DEFINED: +// return OperatingSystemIF::NOT_DEFINED; +// case RTEMS_RESOURCE_IN_USE: +// return OperatingSystemIF::RESOURCE_IN_USE; +// //TODO RTEMS_UNSATISFIED is double mapped for FLP so it will only return Queue_empty and not unsatisfied +// case RTEMS_UNSATISFIED: +// return OperatingSystemIF::QUEUE_EMPTY; +// case RTEMS_INCORRECT_STATE: +// return OperatingSystemIF::INCORRECT_STATE; +// case RTEMS_ALREADY_SUSPENDED: +// return OperatingSystemIF::ALREADY_SUSPENDED; +// case RTEMS_ILLEGAL_ON_SELF: +// return OperatingSystemIF::ILLEGAL_ON_SELF; +// case RTEMS_ILLEGAL_ON_REMOTE_OBJECT: +// return OperatingSystemIF::ILLEGAL_ON_REMOTE_OBJECT; +// case RTEMS_CALLED_FROM_ISR: +// return OperatingSystemIF::CALLED_FROM_ISR; +// case RTEMS_INVALID_PRIORITY: +// return OperatingSystemIF::INVALID_PRIORITY; +// case RTEMS_INVALID_CLOCK: +// return OperatingSystemIF::INVALID_CLOCK; +// case RTEMS_INVALID_NODE: +// return OperatingSystemIF::INVALID_NODE; +// case RTEMS_NOT_CONFIGURED: +// return OperatingSystemIF::NOT_CONFIGURED; +// case RTEMS_NOT_OWNER_OF_RESOURCE: +// return OperatingSystemIF::NOT_OWNER_OF_RESOURCE; +// case RTEMS_NOT_IMPLEMENTED: +// return OperatingSystemIF::NOT_IMPLEMENTED; +// case RTEMS_INTERNAL_ERROR: +// return OperatingSystemIF::INTERNAL_ERROR; +// case RTEMS_NO_MEMORY: +// return OperatingSystemIF::NO_MEMORY; +// case RTEMS_IO_ERROR: +// return OperatingSystemIF::IO_ERROR; +// default: +// return HasReturnvaluesIF::RETURN_FAILED; +// } +// } +//} diff --git a/fsfw/osal/rtems/RtemsBasic.h b/fsfw/osal/rtems/RtemsBasic.h new file mode 100644 index 0000000..d0ca5ab --- /dev/null +++ b/fsfw/osal/rtems/RtemsBasic.h @@ -0,0 +1,25 @@ +#ifndef FSFW_OSAL_RTEMS_RTEMSBASIC_H_ +#define FSFW_OSAL_RTEMS_RTEMSBASIC_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" +#include +#include +#include +#include +#include + + +class RtemsBasic { +public: + static rtems_interval convertMsToTicks(uint32_t msIn) { + rtems_interval ticks_per_second = rtems_clock_get_ticks_per_second(); + return (ticks_per_second * msIn) / 1000; + } + + static rtems_interval convertTicksToMs(rtems_interval ticksIn) { + rtems_interval ticks_per_second = rtems_clock_get_ticks_per_second(); + return (ticksIn * 1000) / ticks_per_second; + } +}; + +#endif /* FSFW_OSAL_RTEMS_RTEMSBASIC_H_ */ diff --git a/fsfw/osal/rtems/TaskBase.cpp b/fsfw/osal/rtems/TaskBase.cpp new file mode 100644 index 0000000..4e0c8f0 --- /dev/null +++ b/fsfw/osal/rtems/TaskBase.cpp @@ -0,0 +1,82 @@ +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "TaskBase.h" + +const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE=RTEMS_MINIMUM_STACK_SIZE; + +TaskBase::TaskBase(rtems_task_priority set_priority, size_t stack_size, + const char *name) { + rtems_name osalName = 0; + for (uint8_t i = 0; i < 4; i++) { + if (name[i] == 0) { + break; + } + osalName += name[i] << (8 * (3 - i)); + } + //The task is created with the operating system's system call. + rtems_status_code status = RTEMS_UNSATISFIED; + if (set_priority >= 0 && set_priority <= 99) { + status = rtems_task_create(osalName, + (0xFF - 2 * set_priority), stack_size, + RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR, + RTEMS_FLOATING_POINT, &id); + } + ReturnValue_t result = convertReturnCode(status); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "TaskBase::TaskBase: createTask with name " << std::hex + << osalName << std::dec << " failed with return code " + << (uint32_t) status << std::endl; + this->id = 0; + } +} + +TaskBase::~TaskBase() { + rtems_task_delete(id); +} + +rtems_id TaskBase::getId() { + return this->id; +} + +ReturnValue_t TaskBase::sleepFor(uint32_t ms) { + rtems_status_code status = rtems_task_wake_after(RtemsBasic::convertMsToTicks(ms)); + return convertReturnCode(status); +} + + +ReturnValue_t TaskBase::convertReturnCode(rtems_status_code inValue) { + switch (inValue) { + case RTEMS_SUCCESSFUL: + return HasReturnvaluesIF::RETURN_OK; + case RTEMS_MP_NOT_CONFIGURED: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_NAME: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_TOO_MANY: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_ADDRESS: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_UNSATISFIED: + return HasReturnvaluesIF::RETURN_FAILED; + case RTEMS_INVALID_PRIORITY: + return HasReturnvaluesIF::RETURN_FAILED; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + +} + + +ReturnValue_t TaskBase::setAndStartPeriod(rtems_interval period, rtems_id *periodId) { + rtems_name periodName = (('P' << 24) + ('e' << 16) + ('r' << 8) + 'd'); + rtems_status_code status = rtems_rate_monotonic_create(periodName, periodId); + if (status == RTEMS_SUCCESSFUL) { + status = restartPeriod(period,*periodId); + } + return convertReturnCode(status); +} + +rtems_status_code TaskBase::restartPeriod(rtems_interval period, rtems_id periodId){ + //This is necessary to avoid a call with period = 0, which does not start the period. + rtems_status_code status = rtems_rate_monotonic_period(periodId, period + 1); + return status; +} diff --git a/fsfw/osal/rtems/TaskBase.h b/fsfw/osal/rtems/TaskBase.h new file mode 100644 index 0000000..0e186e6 --- /dev/null +++ b/fsfw/osal/rtems/TaskBase.h @@ -0,0 +1,47 @@ +#ifndef FSFW_OSAL_RTEMS_TASKBASE_H_ +#define FSFW_OSAL_RTEMS_TASKBASE_H_ + +#include "RtemsBasic.h" +#include "../../tasks/PeriodicTaskIF.h" + +/** + * @brief This is the basic task handling class for rtems. + * + * @details Task creation base class for rtems. + */ +class TaskBase { +protected: + /** + * @brief The class stores the task id it got assigned from the operating system in this attribute. + * If initialization fails, the id is set to zero. + */ + rtems_id id; +public: + /** + * @brief The constructor creates and initializes a task. + * @details This is accomplished by using the operating system call to create a task. The name is + * created automatically with the help od taskCounter. Priority and stack size are + * adjustable, all other attributes are set with default values. + * @param priority Sets the priority of a task. Values range from a low 0 to a high 99. + * @param stack_size The stack size reserved by the operating system for the task. + * @param nam The name of the Task, as a null-terminated String. Currently max 4 chars supported (excluding Null-terminator), rest will be truncated + */ + TaskBase( rtems_task_priority priority, size_t stack_size, const char *name); + /** + * @brief In the destructor, the created task is deleted. + */ + virtual ~TaskBase(); + /** + * @brief This method returns the task id of this class. + */ + rtems_id getId(); + + ReturnValue_t sleepFor(uint32_t ms); + static ReturnValue_t setAndStartPeriod(rtems_interval period, rtems_id *periodId); + static rtems_status_code restartPeriod(rtems_interval period, rtems_id periodId); +private: + static ReturnValue_t convertReturnCode(rtems_status_code inValue); +}; + + +#endif /* FSFW_OSAL_RTEMS_TASKBASE_H_ */ diff --git a/fsfw/osal/rtems/TaskFactory.cpp b/fsfw/osal/rtems/TaskFactory.cpp new file mode 100644 index 0000000..bab48a4 --- /dev/null +++ b/fsfw/osal/rtems/TaskFactory.cpp @@ -0,0 +1,41 @@ +#include "../../tasks/TaskFactory.h" +#include "MultiObjectTask.h" +#include "PollingTask.h" +#include "InitTask.h" +#include "RtemsBasic.h" +#include "../../returnvalues/HasReturnvaluesIF.h" + +//TODO: Different variant than the lazy loading in QueueFactory. What's better and why? +TaskFactory* TaskFactory::factoryInstance = new TaskFactory(); + +TaskFactory::~TaskFactory() { +} + +TaskFactory* TaskFactory::instance() { + return TaskFactory::factoryInstance; +} + +PeriodicTaskIF* TaskFactory::createPeriodicTask(TaskName name_,TaskPriority taskPriority_,TaskStackSize stackSize_,TaskPeriod periodInSeconds_,TaskDeadlineMissedFunction deadLineMissedFunction_) { + rtems_interval taskPeriod = periodInSeconds_ * Clock::getTicksPerSecond(); + + return static_cast(new MultiObjectTask(name_,taskPriority_,stackSize_,taskPeriod,deadLineMissedFunction_)); +} + +FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(TaskName name_,TaskPriority taskPriority_,TaskStackSize stackSize_,TaskPeriod periodInSeconds_,TaskDeadlineMissedFunction deadLineMissedFunction_) { + rtems_interval taskPeriod = periodInSeconds_ * Clock::getTicksPerSecond(); + return static_cast(new PollingTask(name_,taskPriority_,stackSize_,taskPeriod,deadLineMissedFunction_)); +} + +ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) { + //TODO not implemented + return HasReturnvaluesIF::RETURN_FAILED; +} + +ReturnValue_t TaskFactory::delayTask(uint32_t delayMs){ + rtems_task_wake_after(RtemsBasic::convertMsToTicks(delayMs)); + //Only return value is "RTEMS_SUCCESSFUL - always successful" so it has been neglected + return HasReturnvaluesIF::RETURN_OK; +} + +TaskFactory::TaskFactory() { +} diff --git a/fsfw/osal/windows/TcWinUdpPollingTask.cpp b/fsfw/osal/windows/TcWinUdpPollingTask.cpp new file mode 100644 index 0000000..7b54bb2 --- /dev/null +++ b/fsfw/osal/windows/TcWinUdpPollingTask.cpp @@ -0,0 +1,148 @@ +#include "TcWinUdpPollingTask.h" +#include "../../globalfunctions/arrayprinter.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +#include +#include + +TcWinUdpPollingTask::TcWinUdpPollingTask(object_id_t 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); + } +} + +TcWinUdpPollingTask::~TcWinUdpPollingTask() {} + +ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) { + // Poll for new UDP datagrams in permanent loop. + while(true) { + //! Sender Address is cached here. + struct sockaddr_in senderAddress; + int senderAddressSize = sizeof(senderAddress); + ssize_t bytesReceived = recvfrom(serverUdpSocket, + reinterpret_cast(receptionBuffer.data()), frameSize, + receptionFlags, reinterpret_cast(&senderAddress), + &senderAddressSize); + if(bytesReceived == SOCKET_ERROR) { + // handle error + sif::error << "TcWinUdpPollingTask::performOperation: Reception" + " error." << std::endl; + handleReadError(); + continue; + } + //sif::debug << "TcWinUdpPollingTask::performOperation: " << bytesReceived + // << " bytes received" << std::endl; + + ReturnValue_t result = handleSuccessfullTcRead(bytesReceived); + if(result != HasReturnvaluesIF::RETURN_FAILED) { + + } + tmtcBridge->registerCommConnect(); + tmtcBridge->checkAndSetClientAddress(senderAddress); + } + return HasReturnvaluesIF::RETURN_OK; +} + + +ReturnValue_t TcWinUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) { + store_address_t storeId; + ReturnValue_t result = tcStore->addData(&storeId, + receptionBuffer.data(), bytesRead); + // arrayprinter::print(receptionBuffer.data(), bytesRead); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "TcSerialPollingTask::transferPusToSoftwareBus: Data " + "storage failed" << std::endl; + sif::error << "Packet size: " << bytesRead << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + + TmTcMessage message(storeId); + + result = MessageQueueSenderIF::sendMessage(targetTcDestination, &message); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "Serial Polling: Sending message to queue failed" + << std::endl; + tcStore->deleteData(storeId); + } + return result; +} + +ReturnValue_t TcWinUdpPollingTask::initialize() { + tcStore = objectManager->get(objects::TC_STORE); + if (tcStore == nullptr) { + sif::error << "TcSerialPollingTask::initialize: TC Store uninitialized!" + << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + tmtcBridge = objectManager->get(tmtcBridgeId); + if(tmtcBridge == nullptr) { + sif::error << "TcSocketPollingTask::TcSocketPollingTask: Invalid" + " TMTC bridge object!" << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + serverUdpSocket = tmtcBridge->serverSocket; + //sif::info << "TcWinUdpPollingTask::initialize: Server UDP socket " + // << serverUdpSocket << std::endl; + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t TcWinUdpPollingTask::initializeAfterTaskCreation() { + // Initialize the destination after task creation. This ensures + // that the destination has already been set in the TMTC bridge. + targetTcDestination = tmtcBridge->getRequestQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + +void TcWinUdpPollingTask::setTimeout(double timeoutSeconds) { + DWORD timeoutMs = timeoutSeconds * 1000.0; + int result = setsockopt(serverUdpSocket, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&timeoutMs), sizeof(DWORD)); + if(result == -1) { + sif::error << "TcSocketPollingTask::TcSocketPollingTask: Setting " + "receive timeout failed with " << strerror(errno) << std::endl; + } +} + +void TcWinUdpPollingTask::handleReadError() { + int error = WSAGetLastError(); + switch(error) { + case(WSANOTINITIALISED): { + sif::info << "TmTcWinUdpBridge::handleReadError: WSANOTINITIALISED: " + << "WSAStartup(...) call " << "necessary" << std::endl; + break; + } + case(WSAEFAULT): { + sif::info << "TmTcWinUdpBridge::handleReadError: WSADEFAULT: " + << "Bad address " << std::endl; + break; + } + default: { + sif::info << "TmTcWinUdpBridge::handleReadError: Error code: " + << error << std::endl; + break; + } + } + // to prevent spam. + Sleep(1000); +} diff --git a/fsfw/osal/windows/TcWinUdpPollingTask.h b/fsfw/osal/windows/TcWinUdpPollingTask.h new file mode 100644 index 0000000..50d39d2 --- /dev/null +++ b/fsfw/osal/windows/TcWinUdpPollingTask.h @@ -0,0 +1,67 @@ +#ifndef FSFW_OSAL_WINDOWS_TCSOCKETPOLLINGTASK_H_ +#define FSFW_OSAL_WINDOWS_TCSOCKETPOLLINGTASK_H_ + +#include "TmTcWinUdpBridge.h" +#include "../../objectmanager/SystemObject.h" +#include "../../tasks/ExecutableObjectIF.h" +#include "../../storagemanager/StorageManagerIF.h" + +#include + +/** + * @brief This class can be used to implement the polling of a Unix socket, + * using UDP for now. + * @details + * The task will be blocked while the specified number of bytes has not been + * received, so TC reception is handled inside a separate task. + * This class caches the IP address of the sender. It is assumed there + * is only one sender for now. + */ +class TcWinUdpPollingTask: public SystemObject, + public ExecutableObjectIF { + friend class TmTcWinUdpBridge; +public: + 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}; + + TcWinUdpPollingTask(object_id_t objectId, object_id_t tmtcUnixUdpBridge, + size_t frameSize = 0, double timeoutSeconds = -1); + virtual~ TcWinUdpPollingTask(); + + /** + * Turn on optional timeout for UDP polling. In the default mode, + * the receive function will block until a packet is received. + * @param timeoutSeconds + */ + void setTimeout(double timeoutSeconds); + + virtual ReturnValue_t performOperation(uint8_t opCode) override; + virtual ReturnValue_t initialize() override; + virtual ReturnValue_t initializeAfterTaskCreation() override; + +protected: + StorageManagerIF* tcStore = nullptr; + +private: + //! TMTC bridge is cached. + object_id_t tmtcBridgeId = objects::NO_OBJECT; + TmTcWinUdpBridge* tmtcBridge = nullptr; + MessageQueueId_t targetTcDestination = MessageQueueIF::NO_QUEUE; + //! 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 + SOCKET serverUdpSocket = 0; + + std::vector receptionBuffer; + + size_t frameSize = 0; + timeval receptionTimeout; + + ReturnValue_t handleSuccessfullTcRead(size_t bytesRead); + void handleReadError(); +}; + +#endif /* FRAMEWORK_OSAL_LINUX_TCSOCKETPOLLINGTASK_H_ */ diff --git a/fsfw/osal/windows/TmTcWinUdpBridge.cpp b/fsfw/osal/windows/TmTcWinUdpBridge.cpp new file mode 100644 index 0000000..7e283c2 --- /dev/null +++ b/fsfw/osal/windows/TmTcWinUdpBridge.cpp @@ -0,0 +1,176 @@ +#include +#include "TmTcWinUdpBridge.h" + +TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId, + object_id_t tcDestination, object_id_t tmStoreId, object_id_t tcStoreId, + uint16_t serverPort, uint16_t clientPort): + TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) { + mutex = MutexFactory::instance()->createMutex(); + + // Initiates Winsock DLL. + WSAData wsaData; + WORD wVersionRequested = MAKEWORD(2, 2); + int err = WSAStartup(wVersionRequested, &wsaData); + if (err != 0) { + /* Tell the user that we could not find a usable */ + /* Winsock DLL. */ + sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge:" + "WSAStartup failed with error: " << err << std::endl; + return; + } + + uint16_t setServerPort = DEFAULT_UDP_SERVER_PORT; + if(serverPort != 0xFFFF) { + setServerPort = serverPort; + } + + uint16_t setClientPort = DEFAULT_UDP_CLIENT_PORT; + if(clientPort != 0xFFFF) { + setClientPort = clientPort; + } + + // Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html + //clientSocket = socket(AF_INET, SOCK_DGRAM, 0); + serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if(serverSocket == INVALID_SOCKET) { + sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open" + " UDP socket!" << std::endl; + handleSocketError(); + return; + } + + serverAddress.sin_family = AF_INET; + + // Accept packets from any interface. (potentially insecure). + serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); + serverAddress.sin_port = htons(setServerPort); + serverAddressLen = sizeof(serverAddress); + setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&serverSocketOptions), + sizeof(serverSocketOptions)); + + clientAddress.sin_family = AF_INET; + clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); + clientAddress.sin_port = htons(setClientPort); + clientAddressLen = sizeof(clientAddress); + + int result = bind(serverSocket, + reinterpret_cast(&serverAddress), + serverAddressLen); + if(result != 0) { + sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind " + "local port " << setServerPort << " to server socket!" + << std::endl; + handleBindError(); + } +} + +TmTcWinUdpBridge::~TmTcWinUdpBridge() { + WSACleanup(); +} + +ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) { + int flags = 0; + + //clientAddress.sin_addr.s_addr = htons(INADDR_ANY); + //clientAddressLen = sizeof(serverAddress); + +// char ipAddress [15]; +// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET, +// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; + + ssize_t bytesSent = sendto(serverSocket, + reinterpret_cast(data), dataLen, flags, + reinterpret_cast(&clientAddress), clientAddressLen); + if(bytesSent == SOCKET_ERROR) { + sif::error << "TmTcWinUdpBridge::sendTm: Send operation failed." + << std::endl; + handleSendError(); + } +// sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were" +// " sent." << std::endl; + return HasReturnvaluesIF::RETURN_OK; + return HasReturnvaluesIF::RETURN_OK; +} + +void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) { + MutexHelper lock(mutex, MutexIF::TimeoutType::WAITING, 10); + +// char ipAddress [15]; +// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET, +// &newAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; +// sif::debug << "IP Address Old: " << inet_ntop(AF_INET, +// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; + + // 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; + clientAddressLen = sizeof(clientAddress); + } +} + +void TmTcWinUdpBridge::handleSocketError() { + int errCode = WSAGetLastError(); + switch(errCode) { + case(WSANOTINITIALISED): { + sif::info << "TmTcWinUdpBridge::handleSocketError: WSANOTINITIALISED: " + << "WSAStartup(...) call " << "necessary" << std::endl; + break; + } + default: { + /* + https://docs.microsoft.com/en-us/windows/win32/winsock/ + windows-sockets-error-codes-2 + */ + sif::info << "TmTcWinUdpBridge::handleSocketError: Error code: " + << errCode << std::endl; + break; + } + } +} + +void TmTcWinUdpBridge::handleBindError() { + int errCode = WSAGetLastError(); + switch(errCode) { + case(WSANOTINITIALISED): { + sif::info << "TmTcWinUdpBridge::handleBindError: WSANOTINITIALISED: " + << "WSAStartup(...) call " << "necessary" << std::endl; + break; + } + default: { + /* + https://docs.microsoft.com/en-us/windows/win32/winsock/ + windows-sockets-error-codes-2 + */ + sif::info << "TmTcWinUdpBridge::handleBindError: Error code: " + << errCode << std::endl; + break; + } + } +} + +void TmTcWinUdpBridge::handleSendError() { + int errCode = WSAGetLastError(); + switch(errCode) { + case(WSANOTINITIALISED): { + sif::info << "TmTcWinUdpBridge::handleSendError: WSANOTINITIALISED: " + << "WSAStartup(...) call " << "necessary" << std::endl; + break; + } + case(WSAEADDRNOTAVAIL): { + sif::info << "TmTcWinUdpBridge::handleReadError: WSAEADDRNOTAVAIL: " + << "Check target address. " << std::endl; + break; + } + default: { + /* + https://docs.microsoft.com/en-us/windows/win32/winsock/ + windows-sockets-error-codes-2 + */ + sif::info << "TmTcWinUdpBridge::handleSendError: Error code: " + << errCode << std::endl; + break; + } + } +} + diff --git a/fsfw/osal/windows/TmTcWinUdpBridge.h b/fsfw/osal/windows/TmTcWinUdpBridge.h new file mode 100644 index 0000000..8188039 --- /dev/null +++ b/fsfw/osal/windows/TmTcWinUdpBridge.h @@ -0,0 +1,49 @@ +#ifndef FSFW_OSAL_WINDOWS_TMTCWINUDPBRIDGE_H_ +#define FSFW_OSAL_WINDOWS_TMTCWINUDPBRIDGE_H_ + +#include "../../tmtcservices/TmTcBridge.h" + +#include +#include + +class TmTcWinUdpBridge: public TmTcBridge { + friend class TcWinUdpPollingTask; +public: + // The ports chosen here should not be used by any other process. + static constexpr uint16_t DEFAULT_UDP_SERVER_PORT = 7301; + static constexpr uint16_t DEFAULT_UDP_CLIENT_PORT = 7302; + + TmTcWinUdpBridge(object_id_t objectId, object_id_t tcDestination, + object_id_t tmStoreId, object_id_t tcStoreId, + uint16_t serverPort = 0xFFFF,uint16_t clientPort = 0xFFFF); + virtual~ TmTcWinUdpBridge(); + + void checkAndSetClientAddress(sockaddr_in clientAddress); + +protected: + virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) override; + +private: + SOCKET serverSocket = 0; + + const int serverSocketOptions = 0; + + struct sockaddr_in clientAddress; + int clientAddressLen = 0; + + struct sockaddr_in serverAddress; + int serverAddressLen = 0; + + //! Access to the client address is mutex protected as it is set + //! by another task. + MutexIF* mutex; + + void handleSocketError(); + void handleBindError(); + void handleSendError(); +}; + + + +#endif /* FSFW_OSAL_HOST_TMTCWINUDPBRIDGE_H_ */ + diff --git a/fsfw/parameters/HasParametersIF.h b/fsfw/parameters/HasParametersIF.h new file mode 100644 index 0000000..005403f --- /dev/null +++ b/fsfw/parameters/HasParametersIF.h @@ -0,0 +1,67 @@ +#ifndef FSFW_PARAMETERS_HASPARAMETERSIF_H_ +#define FSFW_PARAMETERS_HASPARAMETERSIF_H_ + +#include "../parameters/ParameterWrapper.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +/** Each parameter is identified with a unique parameter ID */ +typedef uint32_t ParameterId_t; + +/** + * @brief This interface is used by components which have modifiable + * parameters, e.g. atittude controllers + * @details + * Each parameter has a unique parameter ID. The first byte of the parameter + * ID is the domain ID which can be used to identify unqiue spacecraft domains + * (e.g. control and sensor domain in the AOCS controller). + * + * The second and third byte represent the matrix ID, which can represent + * a 8-bit row and column number and the last byte... + * + * Yeah, it it matrix ID oder parameter ID now and is index a 16 bit number + * of a 8 bit number now? + */ +class HasParametersIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::HAS_PARAMETERS_IF; + static const ReturnValue_t INVALID_MATRIX_ID = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t INVALID_DOMAIN_ID = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t INVALID_VALUE = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t READ_ONLY = MAKE_RETURN_CODE(0x05); + + static uint8_t getDomain(ParameterId_t id) { + return id >> 24; + } + + static uint16_t getMatrixId(ParameterId_t id) { + return id >> 8; + } + + static uint8_t getIndex(ParameterId_t id) { + return id; + } + + static uint32_t getFullParameterId(uint8_t domainId, uint16_t parameterId, + uint8_t index) { + return (domainId << 24) + (parameterId << 8) + index; + } + + virtual ~HasParametersIF() {} + + /** + * Always set parameter before checking newValues! + * + * @param domainId + * @param parameterId + * @param parameterWrapper + * @param newValues + * @param startAtIndex + * @return + */ + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex) = 0; +}; + +#endif /* FSFW_PARAMETERS_HASPARAMETERSIF_H_ */ diff --git a/fsfw/parameters/ParameterHelper.cpp b/fsfw/parameters/ParameterHelper.cpp new file mode 100644 index 0000000..659b00d --- /dev/null +++ b/fsfw/parameters/ParameterHelper.cpp @@ -0,0 +1,132 @@ +#include "ParameterHelper.h" +#include "ParameterMessage.h" +#include "../objectmanager/ObjectManagerIF.h" + +ParameterHelper::ParameterHelper(ReceivesParameterMessagesIF* owner) : + owner(owner) {} + +ParameterHelper::~ParameterHelper() { +} + +ReturnValue_t ParameterHelper::handleParameterMessage(CommandMessage *message) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED; + switch (message->getCommand()) { + case ParameterMessage::CMD_PARAMETER_DUMP: { + ParameterWrapper description; + uint8_t domain = HasParametersIF::getDomain( + ParameterMessage::getParameterId(message)); + uint16_t parameterId = HasParametersIF::getMatrixId( + ParameterMessage::getParameterId(message)); + result = owner->getParameter(domain, parameterId, + &description, &description, 0); + if (result == HasReturnvaluesIF::RETURN_OK) { + result = sendParameter(message->getSender(), + ParameterMessage::getParameterId(message), &description); + } + } + break; + case ParameterMessage::CMD_PARAMETER_LOAD: { + uint8_t domain = HasParametersIF::getDomain( + ParameterMessage::getParameterId(message)); + uint16_t parameterId = HasParametersIF::getMatrixId( + ParameterMessage::getParameterId(message)); + uint8_t index = HasParametersIF::getIndex( + ParameterMessage::getParameterId(message)); + + const uint8_t *storedStream = nullptr; + size_t storedStreamSize = 0; + result = storage->getData( + ParameterMessage::getStoreId(message), &storedStream, + &storedStreamSize); + if (result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "ParameterHelper::handleParameterMessage: Getting" + " store data failed for load command." << std::endl; + break; + } + + ParameterWrapper streamWrapper; + result = streamWrapper.set(storedStream, storedStreamSize); + if (result != HasReturnvaluesIF::RETURN_OK) { + storage->deleteData(ParameterMessage::getStoreId(message)); + break; + } + + ParameterWrapper ownerWrapper; + result = owner->getParameter(domain, parameterId, &ownerWrapper, + &streamWrapper, index); + if (result != HasReturnvaluesIF::RETURN_OK) { + storage->deleteData(ParameterMessage::getStoreId(message)); + break; + } + + result = ownerWrapper.copyFrom(&streamWrapper, index); + + storage->deleteData(ParameterMessage::getStoreId(message)); + + if (result == HasReturnvaluesIF::RETURN_OK) { + result = sendParameter(message->getSender(), + ParameterMessage::getParameterId(message), &ownerWrapper); + } + } + break; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } + + if (result != HasReturnvaluesIF::RETURN_OK) { + rejectCommand(message->getSender(), result, message->getCommand()); + } + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t ParameterHelper::sendParameter(MessageQueueId_t to, uint32_t id, + const ParameterWrapper* description) { + size_t serializedSize = description->getSerializedSize(); + + uint8_t *storeElement; + store_address_t address; + + ReturnValue_t result = storage->getFreeElement(&address, serializedSize, + &storeElement); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + size_t storeElementSize = 0; + + result = description->serialize(&storeElement, &storeElementSize, + serializedSize, SerializeIF::Endianness::BIG); + + if (result != HasReturnvaluesIF::RETURN_OK) { + storage->deleteData(address); + return result; + } + + CommandMessage reply; + + ParameterMessage::setParameterDumpReply(&reply, id, address); + + MessageQueueSenderIF::sendMessage(to, &reply, ownerQueueId); + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t ParameterHelper::initialize() { + ownerQueueId = owner->getCommandQueue(); + + + storage = objectManager->get(objects::IPC_STORE); + if (storage == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } else { + return HasReturnvaluesIF::RETURN_OK; + } +} + +void ParameterHelper::rejectCommand(MessageQueueId_t to, ReturnValue_t reason, + Command_t initialCommand) { + CommandMessage reply; + reply.setReplyRejected(reason, initialCommand); + MessageQueueSenderIF::sendMessage(to, &reply, ownerQueueId); +} diff --git a/fsfw/parameters/ParameterHelper.h b/fsfw/parameters/ParameterHelper.h new file mode 100644 index 0000000..595b4bc --- /dev/null +++ b/fsfw/parameters/ParameterHelper.h @@ -0,0 +1,36 @@ +#ifndef FSFW_PARAMETERS_PARAMETERHELPER_H_ +#define FSFW_PARAMETERS_PARAMETERHELPER_H_ + +#include "ParameterMessage.h" +#include "ReceivesParameterMessagesIF.h" +#include "../ipc/MessageQueueIF.h" + +/** + * @brief Helper class to handle parameter messages. + * @details + * This class simplfies handling of parameter messages, which are sent + * to a class which implements ReceivesParameterMessagesIF. + */ +class ParameterHelper { +public: + ParameterHelper(ReceivesParameterMessagesIF *owner); + virtual ~ParameterHelper(); + + ReturnValue_t handleParameterMessage(CommandMessage *message); + + ReturnValue_t initialize(); +private: + ReceivesParameterMessagesIF *owner; + + MessageQueueId_t ownerQueueId = MessageQueueIF::NO_QUEUE; + + StorageManagerIF *storage = nullptr; + + ReturnValue_t sendParameter(MessageQueueId_t to, uint32_t id, + const ParameterWrapper *description); + + void rejectCommand(MessageQueueId_t to, ReturnValue_t reason, + Command_t initialCommand); +}; + +#endif /* FSFW_PARAMETERS_PARAMETERHELPER_H_ */ diff --git a/fsfw/parameters/ParameterMessage.cpp b/fsfw/parameters/ParameterMessage.cpp new file mode 100644 index 0000000..e9f3191 --- /dev/null +++ b/fsfw/parameters/ParameterMessage.cpp @@ -0,0 +1,48 @@ +#include "../parameters/ParameterMessage.h" +#include "../objectmanager/ObjectManagerIF.h" + +ParameterId_t ParameterMessage::getParameterId(const CommandMessage* message) { + return message->getParameter(); +} + +store_address_t ParameterMessage::getStoreId(const CommandMessage* message) { + store_address_t address; + address.raw = message->getParameter2(); + return address; +} + +void ParameterMessage::setParameterDumpCommand(CommandMessage* message, + ParameterId_t id) { + message->setCommand(CMD_PARAMETER_DUMP); + message->setParameter(id); +} + +void ParameterMessage::setParameterDumpReply(CommandMessage* message, + ParameterId_t id, store_address_t storageID) { + message->setCommand(REPLY_PARAMETER_DUMP); + message->setParameter(id); + message->setParameter2(storageID.raw); +} + +void ParameterMessage::setParameterLoadCommand(CommandMessage* message, + ParameterId_t id, store_address_t storageID) { + message->setCommand(CMD_PARAMETER_LOAD); + message->setParameter(id); + message->setParameter2(storageID.raw); +} + +void ParameterMessage::clear(CommandMessage* message) { + switch (message->getCommand()) { + case CMD_PARAMETER_LOAD: + case REPLY_PARAMETER_DUMP: { + StorageManagerIF *ipcStore = objectManager->get( + objects::IPC_STORE); + if (ipcStore != NULL) { + ipcStore->deleteData(getStoreId(message)); + } + break; + } + default: + break; + } +} diff --git a/fsfw/parameters/ParameterMessage.h b/fsfw/parameters/ParameterMessage.h new file mode 100644 index 0000000..31d7fe0 --- /dev/null +++ b/fsfw/parameters/ParameterMessage.h @@ -0,0 +1,29 @@ +#ifndef FSFW_PARAMETERS_PARAMETERMESSAGE_H_ +#define FSFW_PARAMETERS_PARAMETERMESSAGE_H_ + +#include "HasParametersIF.h" +#include "../ipc/CommandMessage.h" +#include "../storagemanager/StorageManagerIF.h" + +class ParameterMessage { +private: + ParameterMessage(); +public: + static const uint8_t MESSAGE_ID = messagetypes::PARAMETER; + static const Command_t CMD_PARAMETER_LOAD = MAKE_COMMAND_ID( 0x01 ); + static const Command_t CMD_PARAMETER_DUMP = MAKE_COMMAND_ID( 0x02 ); + static const Command_t REPLY_PARAMETER_DUMP = MAKE_COMMAND_ID( 0x03 ); + + static ParameterId_t getParameterId(const CommandMessage* message); + static store_address_t getStoreId(const CommandMessage* message); + static void setParameterDumpCommand(CommandMessage* message, + ParameterId_t id); + static void setParameterDumpReply(CommandMessage* message, + ParameterId_t id, store_address_t storageID); + static void setParameterLoadCommand(CommandMessage* message, + ParameterId_t id, store_address_t storageID); + static void clear(CommandMessage* message); + +}; + +#endif /* FSFW_PARAMETERS_PARAMETERMESSAGE_H_ */ diff --git a/fsfw/parameters/ParameterWrapper.cpp b/fsfw/parameters/ParameterWrapper.cpp new file mode 100644 index 0000000..6adc685 --- /dev/null +++ b/fsfw/parameters/ParameterWrapper.cpp @@ -0,0 +1,281 @@ +#include "ParameterWrapper.h" + +ParameterWrapper::ParameterWrapper() : + pointsToStream(false), type(Type::UNKNOWN_TYPE) { +} + +ParameterWrapper::ParameterWrapper(Type type, uint8_t rows, uint8_t columns, + void *data) : + pointsToStream(false), type(type), rows(rows), columns(columns), + data(data), readonlyData(data) { +} + +ParameterWrapper::ParameterWrapper(Type type, uint8_t rows, uint8_t columns, + const void *data) : + pointsToStream(false), type(type), rows(rows), columns(columns), + data(nullptr), readonlyData(data) { +} + +ParameterWrapper::~ParameterWrapper() { +} + +ReturnValue_t ParameterWrapper::serialize(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result; + + result = SerializeAdapter::serialize(&type, buffer, size, maxSize, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = SerializeAdapter::serialize(&columns, buffer, size, maxSize, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&rows, buffer, size, maxSize, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + //serialize uses readonlyData, as it is always valid + if (readonlyData == NULL) { + return NOT_SET; + } + switch (type) { + case Type::UINT8_T: + result = serializeData(buffer, size, maxSize, + streamEndianness); + break; + case Type::INT8_T: + result = serializeData(buffer, size, maxSize, streamEndianness); + break; + case Type::UINT16_T: + result = serializeData(buffer, size, maxSize, + streamEndianness); + break; + case Type::INT16_T: + result = serializeData(buffer, size, maxSize, + streamEndianness); + break; + case Type::UINT32_T: + result = serializeData(buffer, size, maxSize, + streamEndianness); + break; + case Type::INT32_T: + result = serializeData(buffer, size, maxSize, + streamEndianness); + break; + case Type::FLOAT: + result = serializeData(buffer, size, maxSize, streamEndianness); + break; + case Type::DOUBLE: + result = serializeData(buffer, size, maxSize, streamEndianness); + break; + default: + result = UNKNOW_DATATYPE; + break; + } + return result; +} + +size_t ParameterWrapper::getSerializedSize() const { + uint32_t serializedSize = 0; + serializedSize += type.getSerializedSize(); + serializedSize += sizeof(rows); + serializedSize += sizeof(columns); + serializedSize += rows * columns * type.getSize(); + + return serializedSize; +} + +template +ReturnValue_t ParameterWrapper::serializeData(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const { + const T *element = (const T*) readonlyData; + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + uint16_t dataSize = columns * rows; + while (dataSize != 0) { + result = SerializeAdapter::serialize(element, buffer, size, maxSize, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + element++; + dataSize--; + } + return result; +} + +template +ReturnValue_t ParameterWrapper::deSerializeData(uint8_t startingRow, + uint8_t startingColumn, const void *from, uint8_t fromRows, + uint8_t fromColumns) { + + //treat from as a continuous Stream as we copy all of it + const uint8_t *fromAsStream = (const uint8_t*) from; + size_t streamSize = fromRows * fromColumns * sizeof(T); + + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + + for (uint8_t fromRow = 0; fromRow < fromRows; fromRow++) { + + //get the start element of this row in data + T *dataWithDataType = ((T*) data) + + (((startingRow + fromRow) * columns) + startingColumn); + + for (uint8_t fromColumn = 0; fromColumn < fromColumns; fromColumn++) { + result = SerializeAdapter::deSerialize( + dataWithDataType + fromColumn, &fromAsStream, &streamSize, + SerializeIF::Endianness::BIG); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + } + + return result; + +} + + +ReturnValue_t ParameterWrapper::deSerialize(const uint8_t **buffer, + size_t *size, Endianness streamEndianness) { + return deSerialize(buffer, size, streamEndianness, 0); +} + +ReturnValue_t ParameterWrapper::deSerialize(const uint8_t **buffer, + size_t *size, Endianness streamEndianness, + uint16_t startWritingAtIndex) { + ParameterWrapper streamDescription; + + ReturnValue_t result = streamDescription.set(*buffer, *size, buffer, size); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + return copyFrom(&streamDescription, startWritingAtIndex); +} + +ReturnValue_t ParameterWrapper::set(const uint8_t *stream, size_t streamSize, + const uint8_t **remainingStream, size_t *remainingSize) { + ReturnValue_t result = SerializeAdapter::deSerialize(&type, &stream, + &streamSize, SerializeIF::Endianness::BIG); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = SerializeAdapter::deSerialize(&columns, &stream, &streamSize, + SerializeIF::Endianness::BIG); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&rows, &stream, &streamSize, + SerializeIF::Endianness::BIG); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + size_t dataSize = type.getSize() * rows * columns; + + if (streamSize < dataSize) { + return SerializeIF::STREAM_TOO_SHORT; + } + + data = nullptr; + readonlyData = stream; + pointsToStream = true; + + stream += dataSize; + if (remainingStream != nullptr) { + *remainingStream = stream; + } + streamSize -= dataSize; + if (remainingSize != nullptr) { + *remainingSize = streamSize; + } + + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t ParameterWrapper::copyFrom(const ParameterWrapper *from, + uint16_t startWritingAtIndex) { + if (data == NULL) { + return READONLY; + } + + if (from->readonlyData == NULL) { + return SOURCE_NOT_SET; + } + + if (type != from->type) { + return DATATYPE_MISSMATCH; + } + + //check if from fits into this + uint8_t startingRow = startWritingAtIndex / columns; + uint8_t startingColumn = startWritingAtIndex % columns; + + if ((from->rows > (rows - startingRow)) + || (from->columns > (columns - startingColumn))) { + return TOO_BIG; + } + + uint8_t typeSize = type.getSize(); + + ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED; + //copy data + if (from->pointsToStream) { + switch (type) { + case Type::UINT8_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows, from->columns); + break; + case Type::INT8_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows, from->columns); + break; + case Type::UINT16_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows, from->columns); + break; + case Type::INT16_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows, from->columns); + break; + case Type::UINT32_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows, from->columns); + break; + case Type::INT32_T: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows, from->columns); + break; + case Type::FLOAT: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows, from->columns); + break; + case Type::DOUBLE: + result = deSerializeData(startingRow, startingColumn, + from->readonlyData, from->rows, from->columns); + break; + default: + result = UNKNOW_DATATYPE; + break; + } + } + else { + //need a type to do arithmetic + uint8_t* typedData = static_cast(data); + for (uint8_t fromRow = 0; fromRow < from->rows; fromRow++) { + size_t offset = (((startingRow + fromRow) * columns) + + startingColumn) * typeSize; + std::memcpy(typedData + offset, from->readonlyData, + typeSize * from->columns); + } + } + + return result; +} diff --git a/fsfw/parameters/ParameterWrapper.h b/fsfw/parameters/ParameterWrapper.h new file mode 100644 index 0000000..f07205d --- /dev/null +++ b/fsfw/parameters/ParameterWrapper.h @@ -0,0 +1,162 @@ +#ifndef FSFW_PARAMETERS_PARAMETERWRAPPER_H_ +#define FSFW_PARAMETERS_PARAMETERWRAPPER_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerializeAdapter.h" +#include "../serialize/SerializeIF.h" +#include "../globalfunctions/Type.h" +#include + +/** + * @brief + * @details + */ +class ParameterWrapper: public SerializeIF { + friend class DataPoolParameterWrapper; +public: + static const uint8_t INTERFACE_ID = CLASS_ID::PARAMETER_WRAPPER; + static const ReturnValue_t UNKNOW_DATATYPE = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t DATATYPE_MISSMATCH = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t READONLY = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t TOO_BIG = MAKE_RETURN_CODE(0x04); + static const ReturnValue_t SOURCE_NOT_SET = MAKE_RETURN_CODE(0x05); + static const ReturnValue_t OUT_OF_BOUNDS = MAKE_RETURN_CODE(0x06); + static const ReturnValue_t NOT_SET = MAKE_RETURN_CODE(0x07); + + ParameterWrapper(); + ParameterWrapper(Type type, uint8_t rows, uint8_t columns, void *data); + ParameterWrapper(Type type, uint8_t rows, uint8_t columns, + const void *data); + virtual ~ParameterWrapper(); + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override; + + virtual size_t getSerializedSize() const override; + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness, uint16_t startWritingAtIndex = 0); + + /** + * Get a specific parameter value by supplying the row and the column. + * @tparam T Type of target data + * @param value [out] Pointer to storage location + * @param row + * @param column + * @return + * -@c RETURN_OK if element was retrieved successfully + * -@c NOT_SET data has not been set yet + * -@c DATATYPE_MISSMATCH Invalid supplied type + * -@c OUT_OF_BOUNDS Invalid row and/or column. + */ + template + ReturnValue_t getElement(T *value, uint8_t row = 0, + uint8_t column = 0) const; + + template + void set(T *data, uint8_t rows, uint8_t columns) { + this->data = data; + this->readonlyData = data; + this->type = PodTypeConversion::type; + this->rows = rows; + this->columns = columns; + this->pointsToStream = false; + } + + template + void set(const T *readonlyData, uint8_t rows, uint8_t columns) { + this->data = NULL; + this->readonlyData = readonlyData; + this->type = PodTypeConversion::type; + this->rows = rows; + this->columns = columns; + this->pointsToStream = false; + } + + template + void set(T& member) { + this->set(&member, 1, 1); + } + + template + void set(const T& readonlyMember) { + this->set(&readonlyMember, 1, 1); + } + + template + void setVector(T& member) { + this->set(member, sizeof(member)/sizeof(member[0]), 1); + } + + template + void setVector(const T& member) { + this->set(member, 1, sizeof(member)/sizeof(member[0])); + } + template + void setMatrix(T& member) { + this->set(member[0], sizeof(member)/sizeof(member[0]), sizeof(member[0])/sizeof(member[0][0])); + } + + template + void setMatrix(const T& member) { + this->set(member[0], sizeof(member)/sizeof(member[0]), sizeof(member[0])/sizeof(member[0][0])); + } + + ReturnValue_t set(const uint8_t *stream, size_t streamSize, + const uint8_t **remainingStream = nullptr, + size_t *remainingSize = nullptr); + + ReturnValue_t copyFrom(const ParameterWrapper *from, + uint16_t startWritingAtIndex); + +private: + bool pointsToStream = false; + + Type type; + uint8_t rows = 0; + uint8_t columns = 0; + void *data = nullptr; + const void *readonlyData = nullptr; + + template + ReturnValue_t serializeData(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const; + + template + ReturnValue_t deSerializeData(uint8_t startingRow, uint8_t startingColumn, + const void *from, uint8_t fromRows, uint8_t fromColumns); +}; + +template +inline ReturnValue_t ParameterWrapper::getElement(T *value, uint8_t row, + uint8_t column) const { + if (readonlyData == nullptr){ + return NOT_SET; + } + + if (PodTypeConversion::type != type) { + return DATATYPE_MISSMATCH; + } + + if ((row >= rows) or (column >= columns)) { + return OUT_OF_BOUNDS; + } + + if (pointsToStream) { + const uint8_t *streamWithType = static_cast(readonlyData); + streamWithType += (row * columns + column) * type.getSize(); + int32_t size = type.getSize(); + return SerializeAdapter::deSerialize(value, &streamWithType, + &size, true); + } + else { + const T *dataWithType = static_cast(readonlyData); + *value = dataWithType[row * columns + column]; + return HasReturnvaluesIF::RETURN_OK; + } +} + +#endif /* FSFW_PARAMETERS_PARAMETERWRAPPER_H_ */ diff --git a/fsfw/parameters/ReceivesParameterMessagesIF.h b/fsfw/parameters/ReceivesParameterMessagesIF.h new file mode 100644 index 0000000..e8c7fa6 --- /dev/null +++ b/fsfw/parameters/ReceivesParameterMessagesIF.h @@ -0,0 +1,19 @@ +#ifndef FSFW_PARAMETERS_RECEIVESPARAMETERMESSAGESIF_H_ +#define FSFW_PARAMETERS_RECEIVESPARAMETERMESSAGESIF_H_ + + +#include "HasParametersIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +class ReceivesParameterMessagesIF : public HasParametersIF { +public: + + static const uint8_t DOMAIN_ID_BASE = 0; + virtual ~ReceivesParameterMessagesIF() { + } + + virtual MessageQueueId_t getCommandQueue() const = 0; +}; + + +#endif /* FSFW_PARAMETERS_RECEIVESPARAMETERMESSAGESIF_H_ */ diff --git a/fsfw/power/Fuse.cpp b/fsfw/power/Fuse.cpp new file mode 100644 index 0000000..9986ab6 --- /dev/null +++ b/fsfw/power/Fuse.cpp @@ -0,0 +1,260 @@ +#include "../monitoring/LimitViolationReporter.h" +#include "../monitoring/MonitoringMessageContent.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "Fuse.h" +#include "../serialize/SerialFixedArrayListAdapter.h" +#include "../ipc/QueueFactory.h" + +object_id_t Fuse::powerSwitchId = 0; + +Fuse::Fuse(object_id_t fuseObjectId, uint8_t fuseId, VariableIds ids, + float maxCurrent, uint16_t confirmationCount) : + SystemObject(fuseObjectId), oldFuseState(0), fuseId(fuseId), powerIF( + NULL), currentLimit(fuseObjectId, 1, ids.pidCurrent, confirmationCount, + maxCurrent, FUSE_CURRENT_HIGH), powerMonitor(fuseObjectId, 2, + DataPool::poolIdAndPositionToPid(ids.poolIdPower, 0), + confirmationCount), set(), voltage(ids.pidVoltage, &set), current( + ids.pidCurrent, &set), state(ids.pidState, &set), power( + ids.poolIdPower, &set, PoolVariableIF::VAR_READ_WRITE), commandQueue( + NULL), parameterHelper(this), healthHelper(this, fuseObjectId) { + commandQueue = QueueFactory::instance()->createMessageQueue(); +} + +Fuse::~Fuse() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); +} + +void Fuse::addDevice(PowerComponentIF* switchSet) { + devices.push_back(switchSet); +} + +ReturnValue_t Fuse::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = parameterHelper.initialize(); + if (result != RETURN_OK) { + return result; + } + result = healthHelper.initialize(); + if (result != RETURN_OK) { + return result; + } + powerIF = objectManager->get(powerSwitchId); + if (powerIF == NULL) { + return RETURN_FAILED; + } + return RETURN_OK; +} + +void Fuse::calculatePowerLimits(float* low, float* high) { + for (DeviceList::iterator iter = devices.begin(); iter != devices.end(); + iter++) { + if (areSwitchesOfComponentOn(iter)) { + *low += (*iter)->getMin(); + *high += (*iter)->getMax(); + } + } +} + +ReturnValue_t Fuse::check() { + set.read(); + if (!healthHelper.healthTable->isHealthy(getObjectId())) { + setAllMonitorsToUnchecked(); + set.commit(PoolVariableIF::INVALID); + return RETURN_OK; + } + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + checkFuseState(); + calculateFusePower(); + //Check if power is valid and if fuse state is off or invalid. + if (!power.isValid() || (state == 0) || !state.isValid()) { + result = powerMonitor.setToInvalid(); + } else { + float lowLimit = 0.0; + float highLimit = RESIDUAL_POWER; + calculatePowerLimits(&lowLimit, &highLimit); + result = powerMonitor.checkPower(power, lowLimit, highLimit); + if (result == MonitoringIF::BELOW_LOW_LIMIT) { + reportEvents(POWER_BELOW_LOW_LIMIT); + } else if (result == MonitoringIF::ABOVE_HIGH_LIMIT) { + reportEvents(POWER_ABOVE_HIGH_LIMIT); + } + } + set.commit(); + return result; +} + +ReturnValue_t Fuse::serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = RETURN_FAILED; + for (DeviceList::const_iterator iter = devices.begin(); + iter != devices.end(); iter++) { + result = (*iter)->serialize(buffer, size, maxSize, streamEndianness); + if (result != RETURN_OK) { + return result; + } + } + return RETURN_OK; +} + +size_t Fuse::getSerializedSize() const { + uint32_t size = 0; + for (DeviceList::const_iterator iter = devices.begin(); + iter != devices.end(); iter++) { + size += (*iter)->getSerializedSize(); + } + return size; +} + +ReturnValue_t Fuse::deSerialize(const uint8_t** buffer, size_t* size, +Endianness streamEndianness) { + ReturnValue_t result = RETURN_FAILED; + for (DeviceList::iterator iter = devices.begin(); iter != devices.end(); + iter++) { + result = (*iter)->deSerialize(buffer, size, streamEndianness); + if (result != RETURN_OK) { + return result; + } + } + return RETURN_OK; +} + +uint8_t Fuse::getFuseId() const { + return fuseId; +} + +void Fuse::calculateFusePower() { + ReturnValue_t result1 = currentLimit.check(); + if (result1 != HasReturnvaluesIF::RETURN_OK || !(voltage.isValid())) { + power.setValid(PoolVariableIF::INVALID); + return; + } + //Calculate fuse power. + power = current * voltage; + power.setValid(PoolVariableIF::VALID); +} + +ReturnValue_t Fuse::performOperation(uint8_t opCode) { + checkCommandQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + +void Fuse::reportEvents(Event event) { + if (!powerMonitor.isEventEnabled()) { + return; + } + for (DeviceList::iterator iter = devices.begin(); iter != devices.end(); + iter++) { + if (areSwitchesOfComponentOn(iter)) { + EventManagerIF::triggerEvent((*iter)->getDeviceObjectId(), event); + } + } +} + +MessageQueueId_t Fuse::getCommandQueue() const { + return commandQueue->getId(); +} + +void Fuse::setAllMonitorsToUnchecked() { + currentLimit.setToUnchecked(); + powerMonitor.setToUnchecked(); +} + +void Fuse::checkCommandQueue() { + CommandMessage command; + ReturnValue_t result = commandQueue->receiveMessage(&command); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + result = healthHelper.handleHealthCommand(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + result = parameterHelper.handleParameterMessage(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + command.setToUnknownCommand(); + commandQueue->reply(&command); +} + +void Fuse::checkFuseState() { + if (!state.isValid()) { + oldFuseState = 0; + return; + } + if (state == 0) { + if (oldFuseState != 0) { + reportEvents(FUSE_WENT_OFF); + } + } + oldFuseState = state; +} + +float Fuse::getPower() { + if (power.isValid()) { + return power; + } else { + return 0.0; + } +} + +void Fuse::setDataPoolEntriesInvalid() { + set.read(); + set.commit(PoolVariableIF::INVALID); +} + +ReturnValue_t Fuse::getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper* parameterWrapper, const ParameterWrapper* newValues, + uint16_t startAtIndex) { + ReturnValue_t result = currentLimit.getParameter(domainId, parameterId, + parameterWrapper, newValues, startAtIndex); + if (result != INVALID_DOMAIN_ID) { + return result; + } + result = powerMonitor.getParameter(domainId, parameterId, parameterWrapper, + newValues, startAtIndex); + return result; +} + +bool Fuse::areSwitchesOfComponentOn(DeviceList::iterator iter) { + if (powerIF->getSwitchState((*iter)->getSwitchId1()) + != PowerSwitchIF::SWITCH_ON) { + return false; + } + if ((*iter)->hasTwoSwitches()) { + if ((powerIF->getSwitchState((*iter)->getSwitchId2()) + != PowerSwitchIF::SWITCH_ON)) { + return false; + } + } + return true; +} + +bool Fuse::isPowerValid() { + return power.isValid(); +} + +ReturnValue_t Fuse::setHealth(HealthState health) { + healthHelper.setHealth(health); + return RETURN_OK; +} + +HasHealthIF::HealthState Fuse::getHealth() { + return healthHelper.getHealth(); +} + +ReturnValue_t Fuse::PowerMonitor::checkPower(float sample, float lowerLimit, + float upperLimit) { + if (sample > upperLimit) { + return this->monitorStateIs(MonitoringIF::ABOVE_HIGH_LIMIT, sample, + upperLimit); + } else if (sample < lowerLimit) { + return this->monitorStateIs(MonitoringIF::BELOW_LOW_LIMIT, sample, + lowerLimit); + } else { + return this->monitorStateIs(RETURN_OK, sample, 0.0); //Within limits. + } +} diff --git a/fsfw/power/Fuse.h b/fsfw/power/Fuse.h new file mode 100644 index 0000000..279642b --- /dev/null +++ b/fsfw/power/Fuse.h @@ -0,0 +1,105 @@ +#ifndef FUSE_H_ +#define FUSE_H_ + +#include "../datapool/DataSet.h" +#include "../datapool/PIDReader.h" +#include "../devicehandlers/HealthDevice.h" +#include "../monitoring/AbsLimitMonitor.h" +#include "PowerComponentIF.h" +#include "PowerSwitchIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../parameters/ParameterHelper.h" +#include + +namespace Factory { +void setStaticFrameworkObjectIds(); +} + +class Fuse: public SystemObject, + public HasHealthIF, + public HasReturnvaluesIF, + public ReceivesParameterMessagesIF, + public SerializeIF { + friend void (Factory::setStaticFrameworkObjectIds)(); +private: + static constexpr float RESIDUAL_POWER = 0.005 * 28.5; //!< This is the upper limit of residual power lost by fuses and switches. Worst case is Fuse and one of two switches on. See PCDU ICD 1.9 p29 bottom +public: + struct VariableIds { + uint32_t pidVoltage; + uint32_t pidCurrent; + uint32_t pidState; + uint32_t poolIdPower; + }; + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::PCDU_1; + static const Event FUSE_CURRENT_HIGH = MAKE_EVENT(1, SEVERITY::LOW); //!< PSS detected that current on a fuse is totally out of bounds. + static const Event FUSE_WENT_OFF = MAKE_EVENT(2, SEVERITY::LOW); //!< PSS detected a fuse that went off. + static const Event POWER_ABOVE_HIGH_LIMIT = MAKE_EVENT(4, SEVERITY::LOW); //!< PSS detected a fuse that violates its limits. + static const Event POWER_BELOW_LOW_LIMIT = MAKE_EVENT(5, SEVERITY::LOW); //!< PSS detected a fuse that violates its limits. + + typedef std::list DeviceList; + Fuse(object_id_t fuseObjectId, uint8_t fuseId, VariableIds ids, + float maxCurrent, uint16_t confirmationCount = 2); + virtual ~Fuse(); + void addDevice(PowerComponentIF *set); + float getPower(); + + bool isPowerValid(); + + ReturnValue_t check(); + uint8_t getFuseId() const; + ReturnValue_t initialize(); + DeviceList devices; + ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, + SerializeIF::Endianness streamEndianness) const override; + size_t getSerializedSize() const override; + ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + SerializeIF::Endianness streamEndianness) override; + void setAllMonitorsToUnchecked(); + ReturnValue_t performOperation(uint8_t opCode); + MessageQueueId_t getCommandQueue() const; + void setDataPoolEntriesInvalid(); + ReturnValue_t setHealth(HealthState health); + HasHealthIF::HealthState getHealth(); + + ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); + +private: + uint8_t oldFuseState; + uint8_t fuseId; + PowerSwitchIF *powerIF; //could be static in our case. + AbsLimitMonitor currentLimit; + class PowerMonitor: public MonitorReporter { + public: + template + PowerMonitor(Args ... args) : + MonitorReporter(std::forward(args)...) { + } + ReturnValue_t checkPower(float sample, float lowerLimit, + float upperLimit); + void sendTransitionEvent(float currentValue, ReturnValue_t state) { + } + + }; + PowerMonitor powerMonitor; + DataSet set; + PIDReader voltage; + PIDReader current; + PIDReader state; + db_float_t power; + MessageQueueIF *commandQueue; + ParameterHelper parameterHelper; + HealthHelper healthHelper; + static object_id_t powerSwitchId; + void calculatePowerLimits(float *low, float *high); + void calculateFusePower(); + void checkFuseState(); + void reportEvents(Event event); + void checkCommandQueue(); + + bool areSwitchesOfComponentOn(DeviceList::iterator iter); +}; + +#endif /* FUSE_H_ */ diff --git a/fsfw/power/PowerComponent.cpp b/fsfw/power/PowerComponent.cpp new file mode 100644 index 0000000..6012ad4 --- /dev/null +++ b/fsfw/power/PowerComponent.cpp @@ -0,0 +1,86 @@ +/** + * @file PowerComponent.cpp + * @brief This file defines the PowerComponent class. + * @date 28.08.2014 + * @author baetz + */ + +#include "PowerComponent.h" + +PowerComponent::PowerComponent() : + deviceObjectId(0), switchId1(0xFF), switchId2(0xFF), doIHaveTwoSwitches( + false), min(0.0), max(0.0), moduleId(0) { +} +PowerComponent::PowerComponent(object_id_t setId, uint8_t moduleId, float min, float max, + uint8_t switchId1, bool twoSwitches, uint8_t switchId2) : + deviceObjectId(setId), switchId1(switchId1), switchId2(switchId2), doIHaveTwoSwitches( + twoSwitches), min(min), max(max), moduleId(moduleId) { +} + +ReturnValue_t PowerComponent::serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = SerializeAdapter::serialize(&min, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return SerializeAdapter::serialize(&max, buffer, size, maxSize, + streamEndianness); +} + +size_t PowerComponent::getSerializedSize() const { + return sizeof(min) + sizeof(max); +} + +object_id_t PowerComponent::getDeviceObjectId() { + return deviceObjectId; +} + +uint8_t PowerComponent::getSwitchId1() { + return switchId1; +} + +uint8_t PowerComponent::getSwitchId2() { + return switchId2; +} + +bool PowerComponent::hasTwoSwitches() { + return doIHaveTwoSwitches; +} + +float PowerComponent::getMin() { + return min; +} + +float PowerComponent::getMax() { + return max; +} + +ReturnValue_t PowerComponent::deSerialize(const uint8_t** buffer, size_t* size, +Endianness streamEndianness) { + ReturnValue_t result = SerializeAdapter::deSerialize(&min, buffer, + size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return SerializeAdapter::deSerialize(&max, buffer, size, streamEndianness); +} + +ReturnValue_t PowerComponent::getParameter(uint8_t domainId, + uint16_t parameterId, ParameterWrapper* parameterWrapper, + const ParameterWrapper* newValues, uint16_t startAtIndex) { + if (domainId != moduleId) { + return INVALID_DOMAIN_ID; + } + switch (parameterId) { + case 0: + parameterWrapper->set<>(min); + break; + case 1: + parameterWrapper->set<>(max); + break; + default: + return INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/power/PowerComponent.h b/fsfw/power/PowerComponent.h new file mode 100644 index 0000000..6d1c9c0 --- /dev/null +++ b/fsfw/power/PowerComponent.h @@ -0,0 +1,48 @@ +#ifndef POWERCOMPONENT_H_ +#define POWERCOMPONENT_H_ + +#include "../objectmanager/SystemObjectIF.h" +#include "PowerComponentIF.h" + +class PowerComponent: public PowerComponentIF { +public: + PowerComponent(object_id_t setId, uint8_t moduleId, float min, float max, uint8_t switchId1, + bool twoSwitches = false, uint8_t switchId2 = 0xFF); + + virtual object_id_t getDeviceObjectId(); + + virtual uint8_t getSwitchId1(); + virtual uint8_t getSwitchId2(); + + bool hasTwoSwitches(); + + float getMin(); + float getMax(); + + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override; + + size_t getSerializedSize() const override; + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; + + ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); +private: + const object_id_t deviceObjectId; + const uint8_t switchId1; + const uint8_t switchId2; + + const bool doIHaveTwoSwitches; + + float min; + float max; + + uint8_t moduleId; + + PowerComponent(); +}; + +#endif /* POWERCOMPONENT_H_ */ diff --git a/fsfw/power/PowerComponentIF.h b/fsfw/power/PowerComponentIF.h new file mode 100644 index 0000000..c2e3a6a --- /dev/null +++ b/fsfw/power/PowerComponentIF.h @@ -0,0 +1,24 @@ +#ifndef POWERCOMPONENTIF_H_ +#define POWERCOMPONENTIF_H_ + +#include "../serialize/SerializeIF.h" +#include "../parameters/HasParametersIF.h" + +class PowerComponentIF : public SerializeIF, public HasParametersIF { +public: + virtual ~PowerComponentIF() { + + } + + virtual object_id_t getDeviceObjectId()=0; + + virtual uint8_t getSwitchId1()=0; + virtual uint8_t getSwitchId2()=0; + virtual bool hasTwoSwitches()=0; + + virtual float getMin() = 0; + virtual float getMax() = 0; + +}; + +#endif /* POWERCOMPONENTIF_H_ */ diff --git a/fsfw/power/PowerSensor.cpp b/fsfw/power/PowerSensor.cpp new file mode 100644 index 0000000..50cb6ac --- /dev/null +++ b/fsfw/power/PowerSensor.cpp @@ -0,0 +1,128 @@ +#include "PowerSensor.h" +#include "../ipc/QueueFactory.h" + +PowerSensor::PowerSensor(object_id_t setId, VariableIds ids, + DefaultLimits limits, SensorEvents events, uint16_t confirmationCount) : + SystemObject(setId), commandQueue(NULL), parameterHelper(this), healthHelper(this, setId), set(), current( + ids.pidCurrent, &set), voltage(ids.pidVoltage, &set), power( + ids.poolIdPower, &set, PoolVariableIF::VAR_WRITE), currentLimit( + setId, MODULE_ID_CURRENT, ids.pidCurrent, confirmationCount, + limits.currentMin, limits.currentMax, events.currentLow, + events.currentHigh), voltageLimit(setId, MODULE_ID_VOLTAGE, + ids.pidVoltage, confirmationCount, limits.voltageMin, + limits.voltageMax, events.voltageLow, events.voltageHigh) { + commandQueue = QueueFactory::instance()->createMessageQueue(); +} + +PowerSensor::~PowerSensor() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); +} + +ReturnValue_t PowerSensor::calculatePower() { + set.read(); + ReturnValue_t result1 = HasReturnvaluesIF::RETURN_FAILED; + ReturnValue_t result2 = HasReturnvaluesIF::RETURN_FAILED; + if (healthHelper.healthTable->isHealthy(getObjectId()) && voltage.isValid() + && current.isValid()) { + result1 = voltageLimit.doCheck(voltage); + result2 = currentLimit.doCheck(current); + } else { + voltageLimit.setToInvalid(); + currentLimit.setToInvalid(); + result1 = OBJECT_NOT_HEALTHY; + } + if (result1 != HasReturnvaluesIF::RETURN_OK + || result2 != HasReturnvaluesIF::RETURN_OK) { + result1 = MonitoringIF::INVALID; + power.setValid(PoolVariableIF::INVALID); + } else { + power.setValid(PoolVariableIF::VALID); + power = current * voltage; + } + set.commit(); + return result1; +} + +ReturnValue_t PowerSensor::performOperation(uint8_t opCode) { + checkCommandQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueId_t PowerSensor::getCommandQueue() const { + return commandQueue->getId(); +} + +ReturnValue_t PowerSensor::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = healthHelper.initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = parameterHelper.initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return result; +} + +void PowerSensor::setAllMonitorsToUnchecked() { + currentLimit.setToUnchecked(); + voltageLimit.setToUnchecked(); +} + +void PowerSensor::checkCommandQueue() { + CommandMessage command; + ReturnValue_t result = commandQueue->receiveMessage(&command); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + result = healthHelper.handleHealthCommand(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + result = parameterHelper.handleParameterMessage(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + command.setToUnknownCommand(); + commandQueue->reply(&command); +} + +void PowerSensor::setDataPoolEntriesInvalid() { + set.read(); + set.commit(PoolVariableIF::INVALID); +} + +float PowerSensor::getPower() { + if (power.isValid()) { + return power.value; + } else { + return 0.0; + } + +} + +ReturnValue_t PowerSensor::setHealth(HealthState health) { + healthHelper.setHealth(health); + return HasReturnvaluesIF::RETURN_OK; +} + +HasHealthIF::HealthState PowerSensor::getHealth() { + return healthHelper.getHealth(); +} + +ReturnValue_t PowerSensor::getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper* parameterWrapper, const ParameterWrapper* newValues, + uint16_t startAtIndex) { + ReturnValue_t result = currentLimit.getParameter(domainId, parameterId, + parameterWrapper, newValues, startAtIndex); + if (result != INVALID_DOMAIN_ID) { + return result; + } + result = voltageLimit.getParameter(domainId, parameterId, parameterWrapper, + newValues, startAtIndex); + return result; +} diff --git a/fsfw/power/PowerSensor.h b/fsfw/power/PowerSensor.h new file mode 100644 index 0000000..0f973e4 --- /dev/null +++ b/fsfw/power/PowerSensor.h @@ -0,0 +1,71 @@ +#ifndef POWERSENSOR_H_ +#define POWERSENSOR_H_ + +#include "../datapool/DataSet.h" +#include "../datapool/PIDReader.h" +#include "../datapool/PoolVariable.h" +#include "../devicehandlers/HealthDevice.h" +#include "../monitoring/LimitMonitor.h" +#include "../parameters/ParameterHelper.h" +#include "../objectmanager/SystemObject.h" +#include "../ipc/MessageQueueIF.h" + +class PowerController; + +class PowerSensor: public SystemObject, + public ReceivesParameterMessagesIF, + public HasHealthIF { + friend class PowerController; +public: + struct VariableIds { + uint32_t pidCurrent; + uint32_t pidVoltage; + uint32_t poolIdPower; + }; + struct DefaultLimits { + float currentMin; + float currentMax; + float voltageMin; + float voltageMax; + }; + struct SensorEvents { + Event currentLow; + Event currentHigh; + Event voltageLow; + Event voltageHigh; + }; + PowerSensor(object_id_t setId, VariableIds setIds, DefaultLimits limits, + SensorEvents events, uint16_t confirmationCount = 0); + virtual ~PowerSensor(); + ReturnValue_t calculatePower(); + ReturnValue_t performOperation(uint8_t opCode); + void setAllMonitorsToUnchecked(); + MessageQueueId_t getCommandQueue() const; + ReturnValue_t initialize(); + void setDataPoolEntriesInvalid(); + float getPower(); + ReturnValue_t setHealth(HealthState health); + HasHealthIF::HealthState getHealth(); + ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); +private: + MessageQueueIF* commandQueue; + ParameterHelper parameterHelper; + HealthHelper healthHelper; + DataSet set; + //Variables in + PIDReader current; + PIDReader voltage; + //Variables out + db_float_t power; + + static const uint8_t MODULE_ID_CURRENT = 1; + static const uint8_t MODULE_ID_VOLTAGE = 2; + void checkCommandQueue(); +protected: + LimitMonitor currentLimit; + LimitMonitor voltageLimit; +}; + +#endif /* POWERSENSOR_H_ */ diff --git a/fsfw/power/PowerSwitchIF.h b/fsfw/power/PowerSwitchIF.h new file mode 100644 index 0000000..40220fb --- /dev/null +++ b/fsfw/power/PowerSwitchIF.h @@ -0,0 +1,75 @@ +/** + * @file PowerSwitchIF.h + * @brief This file defines the PowerSwitchIF class. + * @date 20.03.2013 + * @author baetz + */ + +#ifndef POWERSWITCHIF_H_ +#define POWERSWITCHIF_H_ + +#include "../events/Event.h" +#include "../returnvalues/HasReturnvaluesIF.h" +/** + * This interface defines a connection to a device that is capable of turning on and off + * switches of devices identified by a switch ID. + */ +class PowerSwitchIF : public HasReturnvaluesIF { +public: + /** + * Empty dtor. + */ + virtual ~PowerSwitchIF() { + + } + /** + * The Returnvalues id of this class, required by HasReturnvaluesIF + */ + static const uint8_t INTERFACE_ID = CLASS_ID::POWER_SWITCH_IF; + static const ReturnValue_t SWITCH_ON = MAKE_RETURN_CODE(1); + static const ReturnValue_t SWITCH_OFF = MAKE_RETURN_CODE(0); + static const ReturnValue_t SWITCH_TIMEOUT = MAKE_RETURN_CODE(2); + static const ReturnValue_t FUSE_ON = MAKE_RETURN_CODE(3); + static const ReturnValue_t FUSE_OFF = MAKE_RETURN_CODE(4); + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::PCDU_2; + static const Event SWITCH_WENT_OFF = MAKE_EVENT(0, SEVERITY::LOW); //!< Someone detected that a switch went off which shouldn't. Severity: Low, Parameter1: switchId1, Parameter2: switchId2 + /** + * send a direct command to the Power Unit to enable/disable the specified switch. + * + * @param switchNr + * @param onOff on == @c SWITCH_ON; off != @c SWITCH_ON + */ + virtual void sendSwitchCommand(uint8_t switchNr, ReturnValue_t onOff) const = 0; + /** + * Sends a command to the Power Unit to enable a certain fuse. + */ + virtual void sendFuseOnCommand(uint8_t fuseNr) const = 0; + + /** + * get the state of the Switches. + * @param switchNr + * @return + * - @c SWITCH_ON if the specified switch is on. + * - @c SWITCH_OFF if the specified switch is off. + * - @c RETURN_FAILED if an error occured + */ + virtual ReturnValue_t getSwitchState( uint8_t switchNr ) const = 0; + /** + * get state of a fuse. + * @param fuseNr + * @return + * - @c FUSE_ON if the specified fuse is on. + * - @c FUSE_OFF if the specified fuse is off. + * - @c RETURN_FAILED if an error occured + */ + virtual ReturnValue_t getFuseState( uint8_t fuseNr ) const = 0; + /** + * The maximum delay that it will take to change a switch + * + * This may take into account the time to send a command, wait for it to be executed and see the switch changed. + */ + virtual uint32_t getSwitchDelayMs(void) const = 0; +}; + + +#endif /* POWERSWITCHIF_H_ */ diff --git a/fsfw/power/PowerSwitcher.cpp b/fsfw/power/PowerSwitcher.cpp new file mode 100644 index 0000000..6296a04 --- /dev/null +++ b/fsfw/power/PowerSwitcher.cpp @@ -0,0 +1,127 @@ +#include "../objectmanager/ObjectManagerIF.h" +#include "PowerSwitcher.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +PowerSwitcher::PowerSwitcher(uint8_t setSwitch1, uint8_t setSwitch2, + PowerSwitcher::State_t setStartState) : + state(setStartState), firstSwitch(setSwitch1), secondSwitch(setSwitch2), power(NULL) { +} + +ReturnValue_t PowerSwitcher::initialize(object_id_t powerSwitchId) { + power = objectManager->get(powerSwitchId); + if (power == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t PowerSwitcher::getStateOfSwitches() { + SwitchReturn_t result = howManySwitches(); + switch (result) { + case ONE_SWITCH: + return power->getSwitchState(firstSwitch); + case TWO_SWITCHES: + if ((power->getSwitchState(firstSwitch) == PowerSwitchIF::SWITCH_ON) + && (power->getSwitchState(secondSwitch) == PowerSwitchIF::SWITCH_ON)) { + return PowerSwitchIF::SWITCH_ON; + } else if ((power->getSwitchState(firstSwitch) == PowerSwitchIF::SWITCH_OFF) + && (power->getSwitchState(secondSwitch) == PowerSwitchIF::SWITCH_OFF)) { + return PowerSwitchIF::SWITCH_OFF; + } else { + return HasReturnvaluesIF::RETURN_FAILED; + } + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +void PowerSwitcher::commandSwitches(ReturnValue_t onOff) { + SwitchReturn_t result = howManySwitches(); + switch (result) { + case TWO_SWITCHES: + power->sendSwitchCommand(secondSwitch, onOff); + /* NO BREAK falls through*/ + case ONE_SWITCH: + power->sendSwitchCommand(firstSwitch, onOff); + break; + } + return; + +} + +void PowerSwitcher::turnOn() { + commandSwitches(PowerSwitchIF::SWITCH_ON); + state = WAIT_ON; +} + +void PowerSwitcher::turnOff() { + commandSwitches(PowerSwitchIF::SWITCH_OFF); + state = WAIT_OFF; +} + +PowerSwitcher::SwitchReturn_t PowerSwitcher::howManySwitches() { + if (secondSwitch == NO_SWITCH) { + return ONE_SWITCH; + } else { + return TWO_SWITCHES; + } +} + +void PowerSwitcher::doStateMachine() { + switch (state) { + case SWITCH_IS_OFF: + case SWITCH_IS_ON: + //Do nothing. + break; + case WAIT_OFF: + if (getStateOfSwitches() == PowerSwitchIF::SWITCH_OFF) { + state = SWITCH_IS_OFF; + } + break; + case WAIT_ON: + if (getStateOfSwitches() == PowerSwitchIF::SWITCH_ON) { + state = SWITCH_IS_ON; + } + break; + default: + //Should never happen. + break; + } +} + +ReturnValue_t PowerSwitcher::checkSwitchState() { + switch (state) { + case WAIT_OFF: + case WAIT_ON: + return IN_POWER_TRANSITION; + case SWITCH_IS_OFF: + if (getStateOfSwitches() == PowerSwitchIF::SWITCH_OFF) { + return RETURN_OK; + } else { + return SWITCH_STATE_MISMATCH; + } + case SWITCH_IS_ON: + if (getStateOfSwitches() == PowerSwitchIF::SWITCH_ON) { + return RETURN_OK; + } else { + return SWITCH_STATE_MISMATCH; + } + } + return RETURN_FAILED; +} + +PowerSwitcher::State_t PowerSwitcher::getState() { + return state; +} + +uint32_t PowerSwitcher::getSwitchDelay() { + return power->getSwitchDelayMs(); +} + +uint8_t PowerSwitcher::getFirstSwitch() const { + return firstSwitch; +} + +uint8_t PowerSwitcher::getSecondSwitch() const { + return secondSwitch; +} diff --git a/fsfw/power/PowerSwitcher.h b/fsfw/power/PowerSwitcher.h new file mode 100644 index 0000000..0f1c02d --- /dev/null +++ b/fsfw/power/PowerSwitcher.h @@ -0,0 +1,45 @@ +#ifndef POWERSWITCHER_H_ +#define POWERSWITCHER_H_ +#include "PowerSwitchIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../timemanager/Countdown.h" + +class PowerSwitcher : public HasReturnvaluesIF { +public: + enum State_t { + WAIT_OFF, + WAIT_ON, + SWITCH_IS_OFF, + SWITCH_IS_ON, + }; + State_t state; + static const uint8_t INTERFACE_ID = CLASS_ID::POWER_SWITCHER; + static const ReturnValue_t IN_POWER_TRANSITION = MAKE_RETURN_CODE(1); + static const ReturnValue_t SWITCH_STATE_MISMATCH = MAKE_RETURN_CODE(2); + PowerSwitcher( uint8_t setSwitch1, uint8_t setSwitch2 = NO_SWITCH, State_t setStartState = SWITCH_IS_OFF ); + ReturnValue_t initialize(object_id_t powerSwitchId); + void turnOn(); + void turnOff(); + void doStateMachine(); + State_t getState(); + ReturnValue_t checkSwitchState(); + uint32_t getSwitchDelay(); + uint8_t getFirstSwitch() const; + uint8_t getSecondSwitch() const; +private: + uint8_t firstSwitch; + uint8_t secondSwitch; + PowerSwitchIF* power; + static const uint8_t NO_SWITCH = 0xFF; + enum SwitchReturn_t { + ONE_SWITCH = 1, + TWO_SWITCHES = 2 + }; + ReturnValue_t getStateOfSwitches(); + void commandSwitches( ReturnValue_t onOff ); + SwitchReturn_t howManySwitches(); +}; + + + +#endif /* POWERSWITCHER_H_ */ diff --git a/fsfw/pus/CService200ModeCommanding.cpp b/fsfw/pus/CService200ModeCommanding.cpp new file mode 100644 index 0000000..c63b47a --- /dev/null +++ b/fsfw/pus/CService200ModeCommanding.cpp @@ -0,0 +1,129 @@ +#include "CService200ModeCommanding.h" +#include "servicepackets/Service200Packets.h" + +#include "../modes/HasModesIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../serialize/SerialLinkedListAdapter.h" +#include "../modes/ModeMessage.h" + +CService200ModeCommanding::CService200ModeCommanding(object_id_t objectId, + uint16_t apid, uint8_t serviceId): + CommandingServiceBase(objectId, apid, serviceId, + NUMBER_OF_PARALLEL_COMMANDS,COMMAND_TIMEOUT_SECONDS) {} + +CService200ModeCommanding::~CService200ModeCommanding() {} + +ReturnValue_t CService200ModeCommanding::isValidSubservice(uint8_t subservice) { + switch(subservice) { + case(Subservice::COMMAND_MODE_COMMAND): + case(Subservice::COMMAND_MODE_READ): + case(Subservice::COMMAND_MODE_ANNCOUNCE): + return RETURN_OK; + default: + return AcceptsTelecommandsIF::INVALID_SUBSERVICE; + } +} + + +ReturnValue_t CService200ModeCommanding::getMessageQueueAndObject( + uint8_t subservice, const uint8_t *tcData, size_t tcDataLen, + MessageQueueId_t *id, object_id_t *objectId) { + if(tcDataLen < sizeof(object_id_t)) { + return CommandingServiceBase::INVALID_TC; + } + SerializeAdapter::deSerialize(objectId, &tcData, &tcDataLen, + SerializeIF::Endianness::BIG); + + return checkInterfaceAndAcquireMessageQueue(id,objectId); +} + +ReturnValue_t CService200ModeCommanding::checkInterfaceAndAcquireMessageQueue( + MessageQueueId_t* messageQueueToSet, object_id_t* objectId) { + HasModesIF * destination = objectManager->get(*objectId); + if(destination == nullptr) { + return CommandingServiceBase::INVALID_OBJECT; + + } + + *messageQueueToSet = destination->getCommandQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + + +ReturnValue_t CService200ModeCommanding::prepareCommand( + CommandMessage* message,uint8_t subservice, const uint8_t *tcData, + size_t tcDataLen, uint32_t *state, object_id_t objectId) { + ModePacket modeCommandPacket; + ReturnValue_t result = modeCommandPacket.deSerialize(&tcData, + &tcDataLen, SerializeIF::Endianness::BIG); + if (result != RETURN_OK) { + return result; + } + + ModeMessage::setModeMessage(dynamic_cast(message), + ModeMessage::CMD_MODE_COMMAND, modeCommandPacket.getMode(), + modeCommandPacket.getSubmode()); + return result; +} + + +ReturnValue_t CService200ModeCommanding::handleReply( + const CommandMessage* reply, Command_t previousCommand, + uint32_t *state, CommandMessage* optionalNextCommand, + object_id_t objectId, bool *isStep) { + Command_t replyId = reply->getCommand(); + ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED; + switch(replyId) { + case(ModeMessage::REPLY_MODE_REPLY): { + result = prepareModeReply(reply, objectId); + break; + } + case(ModeMessage::REPLY_WRONG_MODE_REPLY): { + result = prepareWrongModeReply(reply, objectId); + break; + } + case(ModeMessage::REPLY_CANT_REACH_MODE): { + result = prepareCantReachModeReply(reply, objectId); + break; + } + case(ModeMessage::REPLY_MODE_INFO): + result = INVALID_REPLY; + break; + default: + result = RETURN_FAILED; + } + return result; +} + +ReturnValue_t CService200ModeCommanding::prepareModeReply( + const CommandMessage *reply, object_id_t objectId) { + ModePacket modeReplyPacket(objectId, + ModeMessage::getMode(reply), + ModeMessage::getSubmode(reply)); + return sendTmPacket(Subservice::REPLY_MODE_REPLY, &modeReplyPacket); +} + +ReturnValue_t CService200ModeCommanding::prepareWrongModeReply( + const CommandMessage *reply, object_id_t objectId) { + ModePacket wrongModeReply(objectId, ModeMessage::getMode(reply), + ModeMessage::getSubmode(reply)); + ReturnValue_t result = sendTmPacket(Subservice::REPLY_WRONG_MODE_REPLY, &wrongModeReply); + if(result == RETURN_OK){ + // We want to produce an error here in any case because the mode was not correct + return RETURN_FAILED; + } + return result; +} + +ReturnValue_t CService200ModeCommanding::prepareCantReachModeReply( + const CommandMessage *reply, object_id_t objectId) { + CantReachModePacket cantReachModePacket(objectId, + ModeMessage::getCantReachModeReason(reply)); + ReturnValue_t result = sendTmPacket(Subservice::REPLY_CANT_REACH_MODE, + &cantReachModePacket); + if(result == RETURN_OK){ + // We want to produce an error here in any case because the mode was not reached + return RETURN_FAILED; + } + return result; +} diff --git a/fsfw/pus/CService200ModeCommanding.h b/fsfw/pus/CService200ModeCommanding.h new file mode 100644 index 0000000..89347db --- /dev/null +++ b/fsfw/pus/CService200ModeCommanding.h @@ -0,0 +1,85 @@ +#ifndef FRAMEWORK_PUS_CSERVICE200MODECOMMANDING_H_ +#define FRAMEWORK_PUS_CSERVICE200MODECOMMANDING_H_ + +#include "../tmtcservices/CommandingServiceBase.h" + +/** + * @brief Custom PUS service to set mode of all objects implementing HasModesIF + * + * Examples: Device Handlers, Assemblies or Subsystems. + * Full Documentation: ECSS-E-ST-70-41C or ECSS-E-70-41A + * Dissertation Baetz p. 115, 116, 165-167. + * + * This is a gateway service. It relays device commands using the software bus. + * @ingroup pus_services + */ +class CService200ModeCommanding: public CommandingServiceBase { +public: + static constexpr uint8_t NUMBER_OF_PARALLEL_COMMANDS = 4; + static constexpr uint16_t COMMAND_TIMEOUT_SECONDS = 60; + + CService200ModeCommanding(object_id_t objectId, + uint16_t apid, uint8_t serviceId); + virtual~ CService200ModeCommanding(); + +protected: + //! CommandingServiceBase (CSB) abstract functions. See CSB documentation. + ReturnValue_t isValidSubservice(uint8_t subservice) override; + ReturnValue_t getMessageQueueAndObject(uint8_t subservice, + const uint8_t *tcData, size_t tcDataLen, MessageQueueId_t *id, + object_id_t *objectId) override; + ReturnValue_t prepareCommand(CommandMessage* message, + uint8_t subservice, const uint8_t *tcData, size_t tcDataLen, + uint32_t *state, object_id_t objectId) override; + ReturnValue_t handleReply(const CommandMessage* reply, + Command_t previousCommand, uint32_t *state, + CommandMessage* optionalNextCommand, object_id_t objectId, + bool *isStep) override; + +private: + ReturnValue_t checkAndAcquireTargetID(object_id_t* objectIdToSet, + const uint8_t* tcData, uint32_t tcDataLen); + ReturnValue_t checkInterfaceAndAcquireMessageQueue( + MessageQueueId_t* MessageQueueToSet, object_id_t* objectId); + + ReturnValue_t prepareModeReply(const CommandMessage *reply, + object_id_t objectId); + ReturnValue_t prepareWrongModeReply(const CommandMessage *reply, + object_id_t objectId); + ReturnValue_t prepareCantReachModeReply(const CommandMessage *reply, + object_id_t objectId); + + enum Subservice { //!< [EXPORT] : [COMMENT] Mode Commanding Subservices + //!< [EXPORT] : [COMMAND] Command assembly, subsystem or device mode + COMMAND_MODE_COMMAND = 1, + //!< [EXPORT] : [COMMAND] Command to set the specified Mode, + //! regardless of external control flag + COMMAND_MODE_COMMAND_FORCED = 2, + //!< [EXPORT] : [COMMAND] Read the current mode and + //! reply with a REPLY_MODE_REPLY + COMMAND_MODE_READ = 3, + //!< [EXPORT] : [COMMAND] Trigger an ModeInfo Event. + //! This command does NOT have a reply + COMMAND_MODE_ANNCOUNCE = 4, + //!< [EXPORT] : [COMMAND] Trigger a ModeInfo Event and to send this + //! command to every child. This command does NOT have a reply. + COMMAND_MODE_ANNOUNCE_RECURSIVELY = 5, + //!< [EXPORT] : [REPLY] Reply to a CMD_MODE_COMMAND or CMD_MODE_READ + REPLY_MODE_REPLY = 6, + //!< [EXPORT] : [REPLY] Reply in case a mode command can't be executed. + REPLY_CANT_REACH_MODE = 7, + //!< [EXPORT] : [REPLY] Reply to a CMD_MODE_COMMAND, indicating that a + //! mode was commanded and a transition started but was aborted, + //! the parameters contain the mode that was reached + REPLY_WRONG_MODE_REPLY = 8 + }; + + enum modeParameters { + MODE_OFF = 0, + MODE_ON = 1, + MODE_NORMAL = 2, + MODE_RAW = 3 + }; +}; + +#endif /* FRAMEWORK_PUS_CSERVICE200MODECOMMANDING_H_ */ diff --git a/fsfw/pus/Service17Test.cpp b/fsfw/pus/Service17Test.cpp new file mode 100644 index 0000000..2ef4449 --- /dev/null +++ b/fsfw/pus/Service17Test.cpp @@ -0,0 +1,41 @@ +#include "Service17Test.h" + +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../objectmanager/SystemObject.h" +#include "../tmtcpacket/pus/TmPacketStored.h" + + +Service17Test::Service17Test(object_id_t objectId, + uint16_t apid, uint8_t serviceId): + PusServiceBase(objectId, apid, serviceId), + packetSubCounter(0) { +} + +Service17Test::~Service17Test() { +} + +ReturnValue_t Service17Test::handleRequest(uint8_t subservice) { + switch(subservice){ + case Subservice::CONNECTION_TEST: { + TmPacketStored connectionPacket(apid, serviceId, + Subservice::CONNECTION_TEST_REPORT, packetSubCounter++); + connectionPacket.sendPacket(requestQueue->getDefaultDestination(), + requestQueue->getId()); + return HasReturnvaluesIF::RETURN_OK; + } + case Subservice::EVENT_TRIGGER_TEST: { + TmPacketStored connectionPacket(apid, serviceId, + Subservice::CONNECTION_TEST_REPORT, packetSubCounter++); + connectionPacket.sendPacket(requestQueue->getDefaultDestination(), + requestQueue->getId()); + triggerEvent(TEST, 1234, 5678); + return RETURN_OK; + } + default: + return AcceptsTelecommandsIF::INVALID_SUBSERVICE; + } +} + +ReturnValue_t Service17Test::performService() { + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/pus/Service17Test.h b/fsfw/pus/Service17Test.h new file mode 100644 index 0000000..e268186 --- /dev/null +++ b/fsfw/pus/Service17Test.h @@ -0,0 +1,44 @@ +#ifndef FSFW_PUS_SERVICE17TEST_H_ +#define FSFW_PUS_SERVICE17TEST_H_ + +#include "../tmtcservices/PusServiceBase.h" +#include "../objectmanager/SystemObject.h" + +/** + * @brief Test Service + * Full Documentation: ECSS-E70-41A p.167 + * + * The test service provides the capability to activate test functions + * implemented on-board and to report the results of such tests. + * Service capability: + * - TC[17,1]: Perform connection test + * - TM[17,2]: Send Connection Test Report + * - TC[17,128]: Perform connection test and trigger event + * + * @ingroup pus_services + */ +class Service17Test: public PusServiceBase { +public: + // Custom events which can be triggered + static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::PUS_SERVICE_17; + static constexpr Event TEST = MAKE_EVENT(0, SEVERITY::INFO); + + enum Subservice: uint8_t { + //! [EXPORT] : [COMMAND] Perform connection test + CONNECTION_TEST = 1, + //! [EXPORT] : [REPLY] Connection test reply + CONNECTION_TEST_REPORT = 2, + //! [EXPORT] : [COMMAND] Trigger test reply and test event + EVENT_TRIGGER_TEST = 128, + }; + + Service17Test(object_id_t objectId, uint16_t apid, uint8_t serviceId); + virtual ~Service17Test(); + virtual ReturnValue_t handleRequest(uint8_t subservice) override; + virtual ReturnValue_t performService() override; + +protected: + uint16_t packetSubCounter = 0; +}; + +#endif /* FSFW_PUS_SERVICE17TEST_H_ */ diff --git a/fsfw/pus/Service1TelecommandVerification.cpp b/fsfw/pus/Service1TelecommandVerification.cpp new file mode 100644 index 0000000..578eb02 --- /dev/null +++ b/fsfw/pus/Service1TelecommandVerification.cpp @@ -0,0 +1,100 @@ +#include "Service1TelecommandVerification.h" +#include "servicepackets/Service1Packets.h" + +#include "../ipc/QueueFactory.h" +#include "../tmtcservices/PusVerificationReport.h" +#include "../tmtcpacket/pus/TmPacketStored.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tmtcservices/AcceptsTelemetryIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + + +Service1TelecommandVerification::Service1TelecommandVerification( + object_id_t objectId, uint16_t apid, uint8_t serviceId, + object_id_t targetDestination): + SystemObject(objectId), apid(apid), serviceId(serviceId), + targetDestination(targetDestination) { + tmQueue = QueueFactory::instance()->createMessageQueue(); +} + +Service1TelecommandVerification::~Service1TelecommandVerification() {} + +MessageQueueId_t Service1TelecommandVerification::getVerificationQueue(){ + return tmQueue->getId(); +} + + +ReturnValue_t Service1TelecommandVerification::performOperation( + uint8_t operationCode){ + PusVerificationMessage message; + ReturnValue_t status = tmQueue->receiveMessage(&message); + while(status == HasReturnvaluesIF::RETURN_OK) { + status = sendVerificationReport(&message); + if(status != HasReturnvaluesIF::RETURN_OK) { + return status; + } + status = tmQueue->receiveMessage(&message); + } + if (status == MessageQueueIF::EMPTY) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + return status; + } +} + + +ReturnValue_t Service1TelecommandVerification::sendVerificationReport( + PusVerificationMessage* message) { + ReturnValue_t result; + if(message->getReportId() % 2 == 0) { + result = generateFailureReport(message); + } else { + result = generateSuccessReport(message); + } + if(result != HasReturnvaluesIF::RETURN_OK){ + sif::error << "Service1TelecommandVerification::initialize: " + "Sending verification packet failed !" << std::endl; + } + return result; +} + +ReturnValue_t Service1TelecommandVerification::generateFailureReport( + PusVerificationMessage *message) { + FailureReport report( + message->getReportId(), message->getTcPacketId(), + message->getTcSequenceControl(), message->getStep(), + message->getErrorCode(), message->getParameter1(), + message->getParameter2()); + TmPacketStored tmPacket(apid, serviceId, message->getReportId(), + packetSubCounter++, &report); + ReturnValue_t result = tmPacket.sendPacket(tmQueue->getDefaultDestination(), + tmQueue->getId()); + return result; +} + +ReturnValue_t Service1TelecommandVerification::generateSuccessReport( + PusVerificationMessage *message) { + SuccessReport report(message->getReportId(),message->getTcPacketId(), + message->getTcSequenceControl(),message->getStep()); + TmPacketStored tmPacket(apid, serviceId, message->getReportId(), + packetSubCounter++, &report); + ReturnValue_t result = tmPacket.sendPacket(tmQueue->getDefaultDestination(), + tmQueue->getId()); + return result; +} + + +ReturnValue_t Service1TelecommandVerification::initialize() { + // Get target object for TC verification messages + AcceptsTelemetryIF* funnel = objectManager-> + get(targetDestination); + if(funnel == nullptr){ + sif::error << "Service1TelecommandVerification::initialize: Specified" + " TM funnel invalid. Make sure it is set up and implements" + " AcceptsTelemetryIF." << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + tmQueue->setDefaultDestination(funnel->getReportReceptionQueue()); + return SystemObject::initialize(); +} diff --git a/fsfw/pus/Service1TelecommandVerification.h b/fsfw/pus/Service1TelecommandVerification.h new file mode 100644 index 0000000..37562d1 --- /dev/null +++ b/fsfw/pus/Service1TelecommandVerification.h @@ -0,0 +1,94 @@ +#ifndef MISSION_PUS_SERVICE1TELECOMMANDVERIFICATION_H_ +#define MISSION_PUS_SERVICE1TELECOMMANDVERIFICATION_H_ + +#include "../objectmanager/SystemObject.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../tmtcservices/AcceptsVerifyMessageIF.h" +#include "../tmtcservices/PusVerificationReport.h" +#include "../ipc/MessageQueueIF.h" + +/** + * @brief Verify TC acceptance, start, progress and execution. + * + * Full Documentation: ECSS-E70-41A p.51 + * + * The telecommand verification service provides the capability for + * explicit verification of each distinct stage of execution of a telecommand + * packet, from on-board acceptance through to completion of execution. + * + * Minimum capabilities of this service: + * + * - TM[1,1]: Telecommand Acceptance Report - Success. + * - TM[1,2]: Telecommand Acceptance Report - Failure. + * + * Additional capabilities of this service: + * + * - TM[1,3]: Telecommand Execution Started Report - Success (Req. 4). + * - TM[1,4]: Telecommand Execution Started Report - Failure (Req. 3). + * - TM[1,5]: Telecommand Execution Progress Report - Success (Req. 6). + * - TM[1,6]: Telecommand Execution Progress Report - Failure (Req. 5). + * - TM[1,7]: Telecommand Execution Completed Report - Success (Req. 8). + * - TM[1,8]: Telecommand Execution Completed Report - Failure (Req. 7). + * + * This Service is not inherited from PUSServiceBase unlike other PUS Services + * because all services implementing PUSServiceBase use this service to + * generate verification reports. + * @ingroup pus_services + */ +class Service1TelecommandVerification: public AcceptsVerifyMessageIF, + public SystemObject, + public ExecutableObjectIF, + public HasReturnvaluesIF { +public: + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::PUS_SERVICE_1; + + Service1TelecommandVerification(object_id_t objectId, + uint16_t apid, uint8_t serviceId, object_id_t targetDestination); + virtual ~Service1TelecommandVerification(); + + /** + * + * @return ID of Verification Queue + */ + virtual MessageQueueId_t getVerificationQueue(); + + /** + * Performs the service periodically as specified in init_mission(). + * Triggers the handlePacket function to send TC verification messages + * @param operationCode + * @return + */ + ReturnValue_t performOperation(uint8_t operationCode = 0) override; + + /** + * Initializes the destination for TC verification messages and initializes + * Service 1 as a system object + * @return + */ + ReturnValue_t initialize() override; +private: + uint16_t apid = 0; + uint8_t serviceId = 0; + + object_id_t targetDestination = objects::NO_OBJECT; + + ReturnValue_t sendVerificationReport(PusVerificationMessage* message); + ReturnValue_t generateFailureReport(PusVerificationMessage* message); + ReturnValue_t generateSuccessReport(PusVerificationMessage* message); + + uint16_t packetSubCounter = 0; + + MessageQueueIF* tmQueue = nullptr; + + enum class Subservice: uint8_t { + VERIFY_ACCEPTANCE_SUCCESS = 1, //!< [EXPORT] : [TM] + VERIFY_ACCEPTANCE_FAILED = 2, //!< [EXPORT] : [TM] + VERIFY_START_SUCCESS = 3, //!< [EXPORT] : [TM] + VERIFY_START_FAILED = 4, //!< [EXPORT] : [TM] + VERIFY_STEP_SUCCESS = 5, //!< [EXPORT] : [TM] + VERIFY_STEP_FAILED = 6 //!< [EXPORT] : [TM] + }; +}; + +#endif /* MISSION_PUS_SERVICE1TELECOMMANDVERIFICATION_H_ */ diff --git a/fsfw/pus/Service2DeviceAccess.cpp b/fsfw/pus/Service2DeviceAccess.cpp new file mode 100644 index 0000000..2093a61 --- /dev/null +++ b/fsfw/pus/Service2DeviceAccess.cpp @@ -0,0 +1,167 @@ +#include "Service2DeviceAccess.h" +#include "servicepackets/Service2Packets.h" + +#include "../devicehandlers/DeviceHandlerIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../devicehandlers/DeviceHandlerMessage.h" +#include "../serialize/EndianConverter.h" +#include "../action/ActionMessage.h" +#include "../serialize/SerializeAdapter.h" +#include "../serialize/SerialLinkedListAdapter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +Service2DeviceAccess::Service2DeviceAccess(object_id_t objectId, + uint16_t apid, uint8_t serviceId, uint8_t numberOfParallelCommands, + uint16_t commandTimeoutSeconds): + CommandingServiceBase(objectId, apid, serviceId, + numberOfParallelCommands, commandTimeoutSeconds) {} + +Service2DeviceAccess::~Service2DeviceAccess() {} + + +ReturnValue_t Service2DeviceAccess::isValidSubservice(uint8_t subservice) { + switch(static_cast(subservice)){ + case Subservice::RAW_COMMANDING: + case Subservice::TOGGLE_WIRETAPPING: + return HasReturnvaluesIF::RETURN_OK; + default: + sif::error << "Invalid Subservice" << std::endl; + return AcceptsTelecommandsIF::INVALID_SUBSERVICE; + } +} + +ReturnValue_t Service2DeviceAccess::getMessageQueueAndObject( + uint8_t subservice, const uint8_t* tcData, size_t tcDataLen, + MessageQueueId_t* id, object_id_t* objectId) { + if(tcDataLen < sizeof(object_id_t)) { + return CommandingServiceBase::INVALID_TC; + } + SerializeAdapter::deSerialize(objectId, &tcData, + &tcDataLen, SerializeIF::Endianness::BIG); + + ReturnValue_t result = checkInterfaceAndAcquireMessageQueue(id,objectId); + return result; +} + +ReturnValue_t Service2DeviceAccess::checkInterfaceAndAcquireMessageQueue( + MessageQueueId_t * messageQueueToSet, object_id_t *objectId) { + DeviceHandlerIF* possibleTarget = + objectManager->get(*objectId); + if(possibleTarget == nullptr) { + return CommandingServiceBase::INVALID_OBJECT; + } + *messageQueueToSet = possibleTarget->getCommandQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + + +ReturnValue_t Service2DeviceAccess::prepareCommand(CommandMessage* message, + uint8_t subservice, const uint8_t* tcData, size_t tcDataLen, + uint32_t* state, object_id_t objectId) { + switch(static_cast(subservice)){ + case Subservice::RAW_COMMANDING: { + return prepareRawCommand(dynamic_cast(message), + tcData, tcDataLen); + } + break; + case Subservice::TOGGLE_WIRETAPPING: { + return prepareWiretappingCommand(dynamic_cast(message), + tcData, tcDataLen); + } + break; + default: + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +ReturnValue_t Service2DeviceAccess::prepareRawCommand( + CommandMessage* messageToSet, const uint8_t *tcData,size_t tcDataLen) { + RawCommand RawCommand(tcData,tcDataLen); + // store command into the Inter Process Communication Store + store_address_t storeAddress; + ReturnValue_t result = IPCStore->addData(&storeAddress, + RawCommand.getCommand(), RawCommand.getCommandSize()); + DeviceHandlerMessage::setDeviceHandlerRawCommandMessage(messageToSet, + storeAddress); + return result; +} + +ReturnValue_t Service2DeviceAccess::prepareWiretappingCommand( + CommandMessage *messageToSet, const uint8_t *tcData, + size_t tcDataLen) { + if(tcDataLen != WiretappingToggle::WIRETAPPING_COMMAND_SIZE) { + return CommandingServiceBase::INVALID_TC; + } + WiretappingToggle command; + ReturnValue_t result = command.deSerialize(&tcData, &tcDataLen, + SerializeIF::Endianness::BIG); + DeviceHandlerMessage::setDeviceHandlerWiretappingMessage(messageToSet, + command.getWiretappingMode()); + return result; +} + + +ReturnValue_t Service2DeviceAccess::handleReply(const CommandMessage* reply, + Command_t previousCommand, uint32_t* state, + CommandMessage* optionalNextCommand, object_id_t objectId, + bool* isStep) { + switch(reply->getCommand()) { + case CommandMessage::REPLY_COMMAND_OK: + return HasReturnvaluesIF::RETURN_OK; + case CommandMessage::REPLY_REJECTED: + return reply->getReplyRejectedReason(); + default: + return CommandingServiceBase::INVALID_REPLY; + } +} + +// All device handlers set service 2 as default raw receiver for wiretapping +// so we have to handle those unrequested messages. +void Service2DeviceAccess::handleUnrequestedReply(CommandMessage* reply) { + switch(reply->getCommand()) { + case DeviceHandlerMessage::REPLY_RAW_COMMAND: + sendWiretappingTm(reply, + static_cast(Subservice::WIRETAPPING_RAW_TC)); + break; + case DeviceHandlerMessage::REPLY_RAW_REPLY: + sendWiretappingTm(reply, + static_cast(Subservice::RAW_REPLY)); + break; + default: + sif::error << "Unknown message in Service2DeviceAccess::" + "handleUnrequestedReply with command ID " << + reply->getCommand() << std::endl; + break; + } + //Must be reached by all cases to clear message + reply->clear(); +} + +void Service2DeviceAccess::sendWiretappingTm(CommandMessage *reply, + uint8_t subservice) { + // Raw Wiretapping + // Get Address of Data from Message + store_address_t storeAddress = DeviceHandlerMessage::getStoreAddress(reply); + const uint8_t* data = nullptr; + size_t size = 0; + ReturnValue_t result = IPCStore->getData(storeAddress, &data, &size); + if(result != HasReturnvaluesIF::RETURN_OK){ + sif::error << "Service2DeviceAccess::sendWiretappingTm: Data Lost in " + "handleUnrequestedReply with failure ID "<< result + << std::endl; + return; + } + + // Init our dummy packet and correct endianness of object ID before + // sending it back. + WiretappingPacket TmPacket(DeviceHandlerMessage::getDeviceObjectId(reply), + data); + TmPacket.objectId = EndianConverter::convertBigEndian(TmPacket.objectId); + sendTmPacket(subservice, TmPacket.data,size, reinterpret_cast( + &TmPacket.objectId), sizeof(TmPacket.objectId)); +} + +MessageQueueId_t Service2DeviceAccess::getDeviceQueue() { + return commandQueue->getId(); +} + diff --git a/fsfw/pus/Service2DeviceAccess.h b/fsfw/pus/Service2DeviceAccess.h new file mode 100644 index 0000000..f6aa8b5 --- /dev/null +++ b/fsfw/pus/Service2DeviceAccess.h @@ -0,0 +1,92 @@ +#ifndef FRAMEWORK_PUS_SERVICE2DEVICEACCESS_H_ +#define FRAMEWORK_PUS_SERVICE2DEVICEACCESS_H_ + +#include "../objectmanager/SystemObjectIF.h" +#include "../devicehandlers/AcceptsDeviceResponsesIF.h" +#include "../tmtcservices/CommandingServiceBase.h" + +/** + * @brief Raw Commanding and Wiretapping of devices. + * @details + * Full Documentation: ECSS-E-ST-70-41C or ECSS-E-70-41A + * Dissertation Baetz p. 115, 116, 165-167. + * + * This service provides the capability to communicate with devices in their + * native protocols with raw commands through the DeviceHandlerIF. + * + * This is a gateway service. It relays device commands to the software bus. + * This service is very closely tied to the CommandingServiceBase + * template class. + * + * There are 4 adaption points for component implementation through the + * CommandingServiceBase. + * + * This service employs custom subservices exclusively. This includes a + * wiretapping subservice to monitor all traffic between target devices and + * this service. + * + * - TC[2,128]: Raw Commanding + * - TC[2,129]: Toggle Wiretapping + * - TM[2,130]: Wiretapping Packet TM + * - TM[2,131]: Wiretapping Packet TC + * @ingroup pus_services + */ +class Service2DeviceAccess : public CommandingServiceBase, + public AcceptsDeviceResponsesIF +{ +public: + Service2DeviceAccess(object_id_t objectId, uint16_t apid, + uint8_t serviceId, uint8_t numberOfParallelCommands = 4, + uint16_t commandTimeoutSeconds = 60); + virtual ~Service2DeviceAccess(); + +protected: + //! CommandingServiceBase (CSB) abstract functions. See CSB documentation. + ReturnValue_t isValidSubservice(uint8_t subservice) override; + ReturnValue_t getMessageQueueAndObject(uint8_t subservice, + const uint8_t *tcData, size_t tcDataLen, MessageQueueId_t *id, + object_id_t *objectId) override; + ReturnValue_t prepareCommand(CommandMessage* message, uint8_t subservice, + const uint8_t *tcData, size_t tcDataLen, uint32_t *state, + object_id_t objectId) override; + ReturnValue_t handleReply(const CommandMessage* reply, + Command_t previousCommand, uint32_t *state, + CommandMessage* optionalNextCommand, object_id_t objectId, + bool *isStep) override; + + /** + * @brief Generates TM packets containing either the TC wiretapping + * packets or the TM wiretapping packets. + * Note that for service 2, all telemetry will be treated as an + * unrequested reply regardless of wiretapping mode. + * @param reply + */ + void handleUnrequestedReply(CommandMessage* reply) override; + + MessageQueueId_t getDeviceQueue() override; +private: + /** + * Generates TM packets for Wiretapping Service + * @param reply + * @param subservice + */ + void sendWiretappingTm(CommandMessage* reply,uint8_t subservice); + + ReturnValue_t checkInterfaceAndAcquireMessageQueue( + MessageQueueId_t* messageQueueToSet, object_id_t* objectId); + + ReturnValue_t prepareRawCommand(CommandMessage* messageToSet, + const uint8_t* tcData, size_t tcDataLen); + ReturnValue_t prepareWiretappingCommand(CommandMessage* messageToSet, + const uint8_t* tcData, size_t tcDataLen); + + enum class Subservice { + RAW_COMMANDING = 128, //!< [EXPORT] : [COMMAND] Command in device native protocol + TOGGLE_WIRETAPPING = 129, //!< [EXPORT] : [COMMAND] Toggle wiretapping of raw communication + RAW_REPLY = 130, //!< [EXPORT] : [REPLY] Includes wiretapping TM and normal TM raw replies from device + WIRETAPPING_RAW_TC = 131 //!< [EXPORT] : [REPLY] Wiretapping packets of commands built by device handler + }; +}; + + +#endif /* MISSION_PUS_DEVICE2DEVICECOMMANDING_H_ */ diff --git a/fsfw/pus/Service5EventReporting.cpp b/fsfw/pus/Service5EventReporting.cpp new file mode 100644 index 0000000..829d04b --- /dev/null +++ b/fsfw/pus/Service5EventReporting.cpp @@ -0,0 +1,91 @@ +#include "Service5EventReporting.h" +#include "servicepackets/Service5Packets.h" + +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../events/EventManagerIF.h" +#include "../ipc/QueueFactory.h" +#include "../tmtcpacket/pus/TmPacketStored.h" + + +Service5EventReporting::Service5EventReporting(object_id_t objectId, + uint16_t apid, uint8_t serviceId, size_t maxNumberReportsPerCycle): + PusServiceBase(objectId, apid, serviceId), + maxNumberReportsPerCycle(maxNumberReportsPerCycle) { + eventQueue = QueueFactory::instance()->createMessageQueue(); +} + +Service5EventReporting::~Service5EventReporting(){} + +ReturnValue_t Service5EventReporting::performService() { + EventMessage message; + ReturnValue_t status = RETURN_OK; + for(uint8_t counter = 0; + counter < maxNumberReportsPerCycle; + counter++) + { + // Receive messages even if reporting is disabled for now. + status = eventQueue->receiveMessage(&message); + if(status == MessageQueueIF::EMPTY) { + return HasReturnvaluesIF::RETURN_OK; + } + + if(enableEventReport) { + status = generateEventReport(message); + if(status != HasReturnvaluesIF::RETURN_OK) { + return status; + } + } + } + sif::debug << "Service5EventReporting::generateEventReport:" + " Too many events" << std::endl; + return HasReturnvaluesIF::RETURN_OK; +} + + +ReturnValue_t Service5EventReporting::generateEventReport( + EventMessage message) +{ + EventReport report(message.getEventId(),message.getReporter(), + message.getParameter1(),message.getParameter2()); + TmPacketStored tmPacket(PusServiceBase::apid, PusServiceBase::serviceId, + message.getSeverity(), packetSubCounter++, &report); + ReturnValue_t result = tmPacket.sendPacket( + requestQueue->getDefaultDestination(),requestQueue->getId()); + if(result != HasReturnvaluesIF::RETURN_OK) { + sif::debug << "Service5EventReporting::generateEventReport:" + " Could not send TM packet" << std::endl; + } + return result; +} + +ReturnValue_t Service5EventReporting::handleRequest(uint8_t subservice) { + switch(subservice) { + case Subservice::ENABLE: { + enableEventReport = true; + return HasReturnvaluesIF::RETURN_OK; + } + case Subservice::DISABLE: { + enableEventReport = false; + return HasReturnvaluesIF::RETURN_OK; + } + default: + return AcceptsTelecommandsIF::INVALID_SUBSERVICE; + } +} + + +// In addition to the default PUSServiceBase initialization, this service needs +// to be registered to the event manager to listen for events. +ReturnValue_t Service5EventReporting::initialize() { + EventManagerIF* manager = objectManager->get( + objects::EVENT_MANAGER); + if (manager == NULL) { + return RETURN_FAILED; + } + // register Service 5 as listener for events + ReturnValue_t result = manager->registerListener(eventQueue->getId(),true); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return PusServiceBase::initialize(); +} diff --git a/fsfw/pus/Service5EventReporting.h b/fsfw/pus/Service5EventReporting.h new file mode 100644 index 0000000..0b6ee9a --- /dev/null +++ b/fsfw/pus/Service5EventReporting.h @@ -0,0 +1,86 @@ +#ifndef FRAMEWORK_PUS_SERVICE5EVENTREPORTING_H_ +#define FRAMEWORK_PUS_SERVICE5EVENTREPORTING_H_ + +#include "../tmtcservices/PusServiceBase.h" +#include "../events/EventMessage.h" + +/** + * @brief Report on-board events like information or errors + * @details + * Full Documentation: ECSS-E70-41A p.79 + * Implements the PusServiceBase template class. + * Documentation: Dissertation Baetz p.135,136 + * + * This service provides for the reporting to the service user of information of + * operational significance. + * 1. reporting of failures or anomalies detected on-board; + * 2. reporting of autonomous on-board actions; + * 3. reporting of normal progress of operations and activities, e.g. + * detection of events which are not anomalous (such as payload events), + * reaching of predefined steps in an operation. Some reports can combine + * more than one of these events. + * + * Minimum capabilities of this service: + * + * - TM[5,1]: Normal/Progress Report + * - TM[5,2]: Error/Anomaly Report - Low Severity + * - TM[5,3]: Error/Anomaly Report - Medium Severity + * - TM[5,4]: Error/Anomaly Report - High Severity + * + * Events can be translated by using translator files located in + * /config/objects/ and /config/events/. Description to events can be added by + * adding a comment behind the event definition with [//!<] as leading string + * + * Additional capabilities of this service: + * + * - TC[5,5]: Enable Event Report Generation (Req. 6) + * - TC[5,6]: Disable Event Report Generation (Req. 5) + * @author R. Mueller + * @ingroup pus_services + */ +class Service5EventReporting: public PusServiceBase { +public: + + Service5EventReporting(object_id_t objectId, uint16_t apid, + uint8_t serviceId, size_t maxNumberReportsPerCycle = 10); + virtual ~Service5EventReporting(); + + /*** + * Check for events and generate event reports if required. + * @return + */ + ReturnValue_t performService() override; + + /*** + * Turn event generation on or off. + * @return + */ + ReturnValue_t handleRequest(uint8_t subservice) override; + + /** + * The default PusServiceBase initialize has been overridden but is still + * executed. Registers this service as a listener for events at the + * EventManager. + * @return + */ + ReturnValue_t initialize() override; + + enum Subservice: uint8_t { + NORMAL_REPORT = 1, //!< [EXPORT] : [REPLY] Generate normal report + ERROR_LOW_SEVERITY = 2, //!< [EXPORT] : [REPLY] Generate error report with low severity + ERROR_MED_SEVERITY = 3, //!< [EXPORT] : [REPLY] Generate error report with medium severity + ERROR_HIGH_SEVERITY = 4, //!< [EXPORT] : [REPLY] Generate error report with high severity + ENABLE = 5, //!< [EXPORT] : [COMMAND] Enable report generation + DISABLE = 6 //!< [EXPORT] : [COMMAND] Disable report generation + }; + +private: + uint16_t packetSubCounter = 0; + MessageQueueIF* eventQueue = nullptr; + bool enableEventReport = true; + const uint8_t maxNumberReportsPerCycle; + + ReturnValue_t generateEventReport(EventMessage message); +}; + +#endif /* MISSION_PUS_SERVICE5EVENTREPORTING_H_ */ diff --git a/fsfw/pus/Service8FunctionManagement.cpp b/fsfw/pus/Service8FunctionManagement.cpp new file mode 100644 index 0000000..2c2e590 --- /dev/null +++ b/fsfw/pus/Service8FunctionManagement.cpp @@ -0,0 +1,142 @@ +#include "Service8FunctionManagement.h" +#include "servicepackets/Service8Packets.h" + +#include "../objectmanager/SystemObjectIF.h" +#include "../action/HasActionsIF.h" +#include "../devicehandlers/DeviceHandlerIF.h" +#include "../serialize/SerializeAdapter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +Service8FunctionManagement::Service8FunctionManagement(object_id_t object_id, + uint16_t apid, uint8_t serviceId, uint8_t numParallelCommands, + uint16_t commandTimeoutSeconds): + CommandingServiceBase(object_id, apid, serviceId, numParallelCommands, + commandTimeoutSeconds) {} + +Service8FunctionManagement::~Service8FunctionManagement() {} + + +ReturnValue_t Service8FunctionManagement::isValidSubservice( + uint8_t subservice) { + switch(static_cast(subservice)) { + case Subservice::DIRECT_COMMANDING: + return HasReturnvaluesIF::RETURN_OK; + default: + return AcceptsTelecommandsIF::INVALID_SUBSERVICE; + } +} + +ReturnValue_t Service8FunctionManagement::getMessageQueueAndObject( + uint8_t subservice, const uint8_t* tcData, size_t tcDataLen, + MessageQueueId_t* id, object_id_t* objectId) { + if(tcDataLen < sizeof(object_id_t)) { + return CommandingServiceBase::INVALID_TC; + } + SerializeAdapter::deSerialize(objectId, &tcData, + &tcDataLen, SerializeIF::Endianness::BIG); + + return checkInterfaceAndAcquireMessageQueue(id,objectId); +} + +ReturnValue_t Service8FunctionManagement::checkInterfaceAndAcquireMessageQueue( + MessageQueueId_t* messageQueueToSet, object_id_t* objectId) { + // check HasActionIF property of target + HasActionsIF* possibleTarget = objectManager->get(*objectId); + if(possibleTarget == nullptr){ + return CommandingServiceBase::INVALID_OBJECT; + } + *messageQueueToSet = possibleTarget->getCommandQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + + +ReturnValue_t Service8FunctionManagement::prepareCommand( + CommandMessage* message, uint8_t subservice, const uint8_t* tcData, + size_t tcDataLen, uint32_t* state, object_id_t objectId) { + return prepareDirectCommand(dynamic_cast(message), + tcData, tcDataLen); +} + +ReturnValue_t Service8FunctionManagement::prepareDirectCommand( + CommandMessage *message, const uint8_t *tcData, size_t tcDataLen) { + if(tcDataLen < sizeof(object_id_t) + sizeof(ActionId_t)) { + sif::debug << "Service8FunctionManagement::prepareDirectCommand:" + << " TC size smaller thant minimum size of direct command." + << std::endl; + return CommandingServiceBase::INVALID_TC; + } + + // Create direct command instance by extracting data from Telecommand + DirectCommand command(tcData, tcDataLen); + + // store additional parameters into the IPC Store + store_address_t parameterAddress; + ReturnValue_t result = IPCStore->addData(¶meterAddress, + command.getParameters(),command.getParametersSize()); + + // setCommand expects a Command Message, an Action ID and a store adress + // pointing to additional parameters + ActionMessage::setCommand(message,command.getActionId(),parameterAddress); + return result; +} + + +ReturnValue_t Service8FunctionManagement::handleReply( + const CommandMessage* reply, Command_t previousCommand, + uint32_t* state, CommandMessage* optionalNextCommand, + object_id_t objectId, bool* isStep) { + Command_t replyId = reply->getCommand(); + ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED; + ActionId_t actionId = ActionMessage::getActionId(reply); + ReturnValue_t returnCode = ActionMessage::getReturnCode(reply); + + switch(replyId) { + case ActionMessage::COMPLETION_SUCCESS: { + DirectReply completionReply(objectId, actionId,returnCode); + result = CommandingServiceBase::EXECUTION_COMPLETE; + break; + } + case ActionMessage::STEP_SUCCESS: { + *isStep = true; + result = HasReturnvaluesIF::RETURN_OK; + break; + } + case ActionMessage::DATA_REPLY: { + result = handleDataReply(reply, objectId, actionId); + break; + } + case ActionMessage::STEP_FAILED: + *isStep = true; + /*No break, falls through*/ + case ActionMessage::COMPLETION_FAILED: + result = ActionMessage::getReturnCode(reply); + break; + default: + result = INVALID_REPLY; + } + return result; +} + +ReturnValue_t Service8FunctionManagement::handleDataReply( + const CommandMessage* reply, object_id_t objectId, + ActionId_t actionId) { + store_address_t storeId = ActionMessage::getStoreId(reply); + size_t size = 0; + const uint8_t * buffer = nullptr; + ReturnValue_t result = IPCStore->getData(storeId, &buffer, &size); + if(result != RETURN_OK) { + sif::error << "Service 8: Could not retrieve data for data reply" + << std::endl; + return result; + } + DataReply dataReply(objectId, actionId, buffer, size); + result = sendTmPacket(static_cast( + Subservice::DIRECT_COMMANDING_DATA_REPLY), &dataReply); + + auto deletionResult = IPCStore->deleteData(storeId); + if(deletionResult != HasReturnvaluesIF::RETURN_OK) { + sif::warning << "Service8FunctionManagement::handleReply: Deletion" + << " of data in pool failed." << std::endl; + } + return result; +} diff --git a/fsfw/pus/Service8FunctionManagement.h b/fsfw/pus/Service8FunctionManagement.h new file mode 100644 index 0000000..b5ebcda --- /dev/null +++ b/fsfw/pus/Service8FunctionManagement.h @@ -0,0 +1,67 @@ +#ifndef FRAMEWORK_PUS_SERVICE8FUNCTIONMANAGEMENT_H_ +#define FRAMEWORK_PUS_SERVICE8FUNCTIONMANAGEMENT_H_ + +#include "../action/ActionMessage.h" +#include "../tmtcservices/CommandingServiceBase.h" + +/** + * @brief Functional commanding. + * Full Documentation: ECSS-E-ST-70-41C p.64, p. 451 + * Dissertation Baetz p. 115, 116, 165-167 + * + * This service provides the capability to perform functions of an + * application process and provides high-level commanding as opposed to the + * Raw Access provided by Service 2. Examples for these functions can include + * control and operation of payload or the AOCS subsystem. + * This service will be the primary means to control the spacecraft as it is + * considered safer than the Raw Access provided + * by Service 2 and is generally sufficient for most tasks. + * + * This is a gateway service. It relays device commands using the software bus. + * This service is very closely tied to the Commanding Service Base template + * class. There is constant interaction between this Service Base und a + * subclass like this service. + * + * Service Capability: + * - TC[8,128]: Direct Commanding + * - TM[8,130]: Direct Commanding Data Reply + * + * @ingroup pus_services + */ +class Service8FunctionManagement : public CommandingServiceBase +{ +public: + Service8FunctionManagement(object_id_t objectId, uint16_t apid, + uint8_t serviceId, uint8_t numParallelCommands = 4, + uint16_t commandTimeoutSeconds = 60); + virtual ~Service8FunctionManagement(); + +protected: + /* CSB abstract functions implementation . See CSB documentation. */ + ReturnValue_t isValidSubservice(uint8_t subservice) override; + ReturnValue_t getMessageQueueAndObject(uint8_t subservice, + const uint8_t *tcData, size_t tcDataLen, MessageQueueId_t *id, + object_id_t *objectId) override; + ReturnValue_t prepareCommand(CommandMessage* message, + uint8_t subservice, const uint8_t *tcData, size_t tcDataLen, + uint32_t *state, object_id_t objectId) override; + ReturnValue_t handleReply(const CommandMessage* reply, + Command_t previousCommand, uint32_t *state, + CommandMessage* optionalNextCommand, object_id_t objectId, + bool *isStep) override; + +private: + enum class Subservice { + DIRECT_COMMANDING = 128, //!< [EXPORT] : [COMMAND] Functional commanding + DIRECT_COMMANDING_DATA_REPLY = 130, //!< [EXPORT] : [REPLY] Data reply + }; + + ReturnValue_t checkInterfaceAndAcquireMessageQueue( + MessageQueueId_t* messageQueueToSet, object_id_t* objectId); + ReturnValue_t prepareDirectCommand(CommandMessage* message, + const uint8_t* tcData, size_t tcDataLen); + ReturnValue_t handleDataReply(const CommandMessage* reply, + object_id_t objectId, ActionId_t actionId); +}; + +#endif /* FRAMEWORK_PUS_SERVICE8FUNCTIONMANAGEMENT_H_ */ diff --git a/fsfw/pus/Service9TimeManagement.cpp b/fsfw/pus/Service9TimeManagement.cpp new file mode 100644 index 0000000..5625bdd --- /dev/null +++ b/fsfw/pus/Service9TimeManagement.cpp @@ -0,0 +1,58 @@ +#include "Service9TimeManagement.h" +#include "servicepackets/Service9Packets.h" + +#include "../timemanager/CCSDSTime.h" +#include "../events/EventManagerIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + + +Service9TimeManagement::Service9TimeManagement(object_id_t objectId, + uint16_t apid, uint8_t serviceId) : + PusServiceBase(objectId, apid , serviceId) { +} + +Service9TimeManagement::~Service9TimeManagement() {} + +ReturnValue_t Service9TimeManagement::performService() { + return RETURN_OK; +} + +ReturnValue_t Service9TimeManagement::handleRequest(uint8_t subservice) { + switch(subservice){ + case SUBSERVICE::SET_TIME:{ + return setTime(); + } + default: + return AcceptsTelecommandsIF::INVALID_SUBSERVICE; + } +} + +ReturnValue_t Service9TimeManagement::setTime() { + Clock::TimeOfDay_t timeToSet; + TimePacket timePacket(currentPacket.getApplicationData(), + currentPacket.getApplicationDataSize()); + ReturnValue_t result = CCSDSTime::convertFromCcsds(&timeToSet, + timePacket.getTime(), timePacket.getTimeSize()); + if(result != RETURN_OK) { + triggerEvent(CLOCK_SET_FAILURE, result, 0); + return result; + } + + uint32_t formerUptime; + Clock::getUptime(&formerUptime); + result = Clock::setClock(&timeToSet); + + if(result == RETURN_OK) { + uint32_t newUptime; + Clock::getUptime(&newUptime); + triggerEvent(CLOCK_SET,newUptime,formerUptime); + return RETURN_OK; + } + else { + triggerEvent(CLOCK_SET_FAILURE, result, 0); + return RETURN_FAILED; + } +} + + + diff --git a/fsfw/pus/Service9TimeManagement.h b/fsfw/pus/Service9TimeManagement.h new file mode 100644 index 0000000..4802cde --- /dev/null +++ b/fsfw/pus/Service9TimeManagement.h @@ -0,0 +1,41 @@ +#ifndef FSFW_PUS_SERVICE9TIMEMANAGEMENT_H_ +#define FSFW_PUS_SERVICE9TIMEMANAGEMENT_H_ + +#include "../tmtcservices/PusServiceBase.h" + +class Service9TimeManagement: public PusServiceBase { +public: + + static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::PUS_SERVICE_9; + static constexpr Event CLOCK_SET = MAKE_EVENT(0, SEVERITY::INFO); //!< Clock has been set. P1: New Uptime. P2: Old Uptime + static constexpr Event CLOCK_SET_FAILURE = MAKE_EVENT(1, SEVERITY::LOW); //!< Clock could not be set. P1: Returncode. + + static constexpr uint8_t CLASS_ID = CLASS_ID::PUS_SERVICE_9; + + /** + * @brief This service provides the capability to set the on-board time. + */ + Service9TimeManagement(object_id_t objectId, uint16_t apid, + uint8_t serviceId); + + virtual ~Service9TimeManagement(); + + virtual ReturnValue_t performService() override; + + /** + * @brief Sets the onboard-time by retrieving the time to set from TC[9,128]. + */ + virtual ReturnValue_t handleRequest(uint8_t subservice) override; + + virtual ReturnValue_t setTime(); +private: + + enum SUBSERVICE { + SET_TIME = 128 //!< [EXPORT] : [COMMAND] Time command in ASCII, CUC or CDS format + }; + +}; + + + +#endif /* FSFW_PUS_SERVICE9TIMEMANAGEMENT_H_ */ diff --git a/fsfw/pus/servicepackets/Service1Packets.h b/fsfw/pus/servicepackets/Service1Packets.h new file mode 100644 index 0000000..dbd3102 --- /dev/null +++ b/fsfw/pus/servicepackets/Service1Packets.h @@ -0,0 +1,166 @@ +/** + * @defgroup spacepackets PUS Packet Definitions + * This group contains all implemented TM or TM packages that are sent to + * or sent by the OBC.They are exported later to display + * packet structures in Mission Information Base (MIB). + */ + +#ifndef MISSION_PUS_SERVICEPACKETS_SERVICE1PACKETS_H_ +#define MISSION_PUS_SERVICEPACKETS_SERVICE1PACKETS_H_ + +#include "../../serialize/SerializeAdapter.h" +#include "../../tmtcservices/VerificationCodes.h" + +/** + * @brief FailureReport class to serialize a failure report + * @brief Subservice 1, 3, 5, 7 + * @ingroup spacepackets + */ +class FailureReport: public SerializeIF { //!< [EXPORT] : [SUBSERVICE] 1, 3, 5, 7 +public: + FailureReport(uint8_t failureSubtype_, uint16_t packetId_, + uint16_t packetSequenceControl_, uint8_t stepNumber_, + ReturnValue_t errorCode_, uint32_t errorParameter1_, + uint32_t errorParameter2_) : + packetId(packetId_), packetSequenceControl(packetSequenceControl_), + stepNumber(stepNumber_), errorCode(errorCode_), + errorParameter1(errorParameter1_), errorParameter2(errorParameter2_), + failureSubtype(failureSubtype_) {} + + /** + * This function is called by the FSFW when calling the tm packet send + * function and supplying the SerializeIF* as parameter + * @param buffer Object content is serialized into the buffer + * @param size + * @param max_size + * @param bigEndian + * @return + */ + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, SerializeIF::Endianness streamEndianness + ) const override { + ReturnValue_t result = SerializeAdapter::serialize(&packetId, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&packetSequenceControl, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (failureSubtype == TC_VERIFY::PROGRESS_FAILURE) { + result = SerializeAdapter::serialize(&stepNumber, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + result = SerializeAdapter::serialize(&errorCode, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&errorParameter1, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = SerializeAdapter::serialize(&errorParameter2, buffer, size, + maxSize, streamEndianness); + return result; + } + + + virtual size_t getSerializedSize() const { + size_t size = 0; + size += SerializeAdapter::getSerializedSize(&packetId); + size += sizeof(packetSequenceControl); + if(failureSubtype==TC_VERIFY::PROGRESS_FAILURE){ + size += SerializeAdapter::getSerializedSize(&stepNumber); + } + size += SerializeAdapter::getSerializedSize(&errorCode); + size += SerializeAdapter::getSerializedSize(&errorParameter1); + size += SerializeAdapter::getSerializedSize(&errorParameter2); + return size; + } + + /** + * Deserialization is not allowed for a report. + * @param buffer + * @param size + * @param bigEndian + * @return + */ + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + SerializeIF::Endianness streamEndianness) override { + return HasReturnvaluesIF::RETURN_FAILED; + } +private: + uint16_t packetId; //!< [EXPORT] : [COMMENT] Packet ID of respective Telecommand + uint16_t packetSequenceControl; //!< [EXPORT] : [COMMENT] Packet SSC of respective Telecommand + uint8_t stepNumber; //!< [EXPORT] : [OPTIONAL][SUBSERVICE] 6 + ReturnValue_t errorCode; //!< [EXPORT] : [COMMENT] Error code which can be looked up in generated error code file + uint32_t errorParameter1; + uint32_t errorParameter2; + const uint8_t failureSubtype; //!< [EXPORT] : [IGNORE] +}; + +/** + * @brief Subservices 2, 4, 6, 8 + * @ingroup spacepackets + */ +class SuccessReport: public SerializeIF { //!< [EXPORT] : [SUBSERVICE] 2, 4, 6, 8 +public: + SuccessReport(uint8_t subtype_, uint16_t packetId_, + uint16_t packetSequenceControl_,uint8_t stepNumber_) : + packetId(packetId_), packetSequenceControl(packetSequenceControl_), + stepNumber(stepNumber_), subtype(subtype_) {} + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, SerializeIF::Endianness streamEndianness + ) const override { + ReturnValue_t result = SerializeAdapter::serialize(&packetId, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&packetSequenceControl, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (subtype == TC_VERIFY::PROGRESS_SUCCESS) { + result = SerializeAdapter::serialize(&stepNumber, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + return result; + } + + virtual size_t getSerializedSize() const override { + size_t size = 0; + size += SerializeAdapter::getSerializedSize(&packetId); + size += sizeof(packetSequenceControl); + if(subtype == TC_VERIFY::PROGRESS_SUCCESS){ + size += SerializeAdapter::getSerializedSize(&stepNumber); + } + return size; + + } + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + SerializeIF::Endianness streamEndianness) override { + return HasReturnvaluesIF::RETURN_FAILED; + } +private: + uint16_t packetId; //!< [EXPORT] : [COMMENT] Packet ID of respective Telecommand + uint16_t packetSequenceControl; //!< [EXPORT] : [COMMENT] Packet SSC of respective Telecommand + uint8_t stepNumber; //!< [EXPORT] : [OPTIONAL][SUBSERVICE] 6 + const uint8_t subtype; //!< [EXPORT] : [IGNORE] +}; + +#endif /* MISSION_PUS_SERVICEPACKETS_SERVICE1PACKETS_H_ */ diff --git a/fsfw/pus/servicepackets/Service200Packets.h b/fsfw/pus/servicepackets/Service200Packets.h new file mode 100644 index 0000000..efcf65f --- /dev/null +++ b/fsfw/pus/servicepackets/Service200Packets.h @@ -0,0 +1,63 @@ +#ifndef FRAMEWORK_PUS_SERVICEPACKETS_SERVICE200PACKETS_H_ +#define FRAMEWORK_PUS_SERVICEPACKETS_SERVICE200PACKETS_H_ + +#include "../../serialize/SerialLinkedListAdapter.h" +#include "../../modes/ModeMessage.h" +#include "../../serialize/SerializeIF.h" + +/** + * @brief Subservice 1, 2, 3, 4, 5 + * @ingroup spacepackets + */ +class ModePacket : public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 1, 2, 6 +public: + + ModePacket() { + setLinks(); + } + + ModePacket(object_id_t objectId, Mode_t mode, Submode_t submode) : + objectId(objectId), mode(mode), submode(submode) { + setLinks(); + } + + Mode_t getMode() { + return mode.entry; + } + + Submode_t getSubmode() { + return submode.entry; + } + + // Forbid copying, pointers are used. + ModePacket(const ModePacket&) = delete; + ModePacket& operator=(const ModePacket&) = delete; +private: + + void setLinks() { + setStart(&objectId); + objectId.setNext(&mode); + mode.setNext(&submode); + } + SerializeElement objectId; //!< [EXPORT] : [COMMENT] Target or source object + SerializeElement mode; //!< [EXPORT] : [COMMENT] 0: MODE_OFF, 1: MODE_ON, 2: MODE_NORMAL, 3: MODE_RAW + SerializeElement submode; //!< [EXPORT] : [COMMENT] Usually 0, device specific submode possible +}; + +/** + * @brief Subservice 7 + * @ingroup spacepackets + */ +class CantReachModePacket: public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 7 +public: + CantReachModePacket(object_id_t objectId, ReturnValue_t reason): + objectId(objectId), reason(reason) { + setStart(&this->objectId); + this->objectId.setNext(&this->reason); + } + + SerializeElement objectId; //!< [EXPORT] : [COMMENT] Reply source object + SerializeElement reason; //!< [EXPORT] : [COMMENT] Reason the mode could not be reached +}; + +#endif /* FRAMEWORK_PUS_SERVICEPACKETS_SERVICE200PACKETS_H_ */ diff --git a/fsfw/pus/servicepackets/Service2Packets.h b/fsfw/pus/servicepackets/Service2Packets.h new file mode 100644 index 0000000..d4f3fb1 --- /dev/null +++ b/fsfw/pus/servicepackets/Service2Packets.h @@ -0,0 +1,76 @@ +#ifndef FRAMEWORK_PUS_SERVICEPACKETS_SERVICE2PACKETS_H_ +#define FRAMEWORK_PUS_SERVICEPACKETS_SERVICE2PACKETS_H_ + +#include "../../action/ActionMessage.h" +#include "../../objectmanager/SystemObjectIF.h" +#include "../../serialize/SerialLinkedListAdapter.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +/** + * @brief Subservice 128 + * @ingroup spacepackets + */ +class RawCommand { //!< [EXPORT] : [SUBSERVICE] 128 +public: + RawCommand(const uint8_t* buffer, size_t size) { + // Deserialize Adapter to get correct endianness + SerializeAdapter::deSerialize(&objectId, &buffer, &size, + SerializeIF::Endianness::BIG); + commandBuffer = buffer; + // size is decremented by AutoSerializeAdapter, + // remaining size is data size + dataSize = size; + } + object_id_t getObjectId() const { + return objectId; + } + + const uint8_t* getCommand() { + return commandBuffer; + } + + size_t getCommandSize() const { + return dataSize; + } +private: + object_id_t objectId = 0; + const uint8_t* commandBuffer = nullptr; //!< [EXPORT] : [MAXSIZE] 256 Bytes + size_t dataSize = 0; //!< [EXPORT] : [IGNORE] +}; + + +/** + * @brief Subservice 129: Command packet to set wiretapping mode + * @ingroup spacepackets + */ +class WiretappingToggle: public SerialLinkedListAdapter{ //!< [EXPORT] : [SUBSERVICE] 129 +public: + static const size_t WIRETAPPING_COMMAND_SIZE = 5; + WiretappingToggle(){ + setStart(&objectId); + objectId.setNext(&wiretappingMode); + } + + uint8_t getWiretappingMode() const { + return wiretappingMode.entry; + } +private: + SerializeElement objectId; + SerializeElement wiretappingMode; //!< [EXPORT] : [INPUT] Mode 0: OFF, Mode 1: RAW +}; + + +/** + * @brief Subservices 130 and 131: TM packets + * @ingroup spacepackets + */ +class WiretappingPacket { //!< [EXPORT] : [SUBSERVICE] 130, 131 +public: + object_id_t objectId; //!< [EXPORT] : [COMMENT] Object ID of source object + const uint8_t* data; //!< [EXPORT] : [MAXSIZE] Raw Command Max. Size + WiretappingPacket(object_id_t objectId, const uint8_t* buffer): + objectId(objectId), data(buffer) { + } +}; + +#endif /* FRAMEWORK_PUS_SERVICEPACKETS_SERVICE2PACKETS_H_ */ diff --git a/fsfw/pus/servicepackets/Service5Packets.h b/fsfw/pus/servicepackets/Service5Packets.h new file mode 100644 index 0000000..9655608 --- /dev/null +++ b/fsfw/pus/servicepackets/Service5Packets.h @@ -0,0 +1,76 @@ +#ifndef MISSION_PUS_SERVICEPACKETS_SERVICE5PACKETS_H_ +#define MISSION_PUS_SERVICEPACKETS_SERVICE5PACKETS_H_ + +#include "../../serialize/SerializeAdapter.h" +#include "../../tmtcservices/VerificationCodes.h" + + +/** + * @brief Subservice 1, 2, 3, 4 + * Structure of Event Report. + * It consists of: + * 1. Report ID(RID). This is the Event ID in the FSFW + * 2. Object ID of the reporter (e.g. subsystem) + * 2. Parameter 1 + * 3. Parameter 2 + * + * @ingroup spacepackets + */ +class EventReport: public SerializeIF { //!< [EXPORT] : [SUBSERVICE] 1, 2, 3, 4 +public: + + EventReport(EventId_t reportId_, object_id_t objectId_, uint32_t parameter1_, + uint32_t parameter2_): + reportId(reportId_),objectId(objectId_), parameter1(parameter1_), + parameter2(parameter2_) {} + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, + SerializeIF::Endianness streamEndianness) const override + { + ReturnValue_t result = SerializeAdapter::serialize(&reportId, buffer, + size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&objectId, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(¶meter1, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(¶meter2, buffer, size, + maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return result; + } + + virtual size_t getSerializedSize() const override { + uint32_t size = 0; + size += SerializeAdapter::getSerializedSize(&reportId); + size += SerializeAdapter::getSerializedSize(&objectId); + size += SerializeAdapter::getSerializedSize(¶meter1); + size += SerializeAdapter::getSerializedSize(¶meter2); + return size; + + } + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + SerializeIF::Endianness streamEndianness) override { + return HasReturnvaluesIF::RETURN_FAILED; + } +private: + EventId_t reportId; + object_id_t objectId; + uint32_t parameter1; + uint32_t parameter2; +}; + + +#endif /* MISSION_PUS_SERVICEPACKETS_SERVICE5PACKETS_H_ */ diff --git a/fsfw/pus/servicepackets/Service8Packets.h b/fsfw/pus/servicepackets/Service8Packets.h new file mode 100644 index 0000000..14f8b6e --- /dev/null +++ b/fsfw/pus/servicepackets/Service8Packets.h @@ -0,0 +1,121 @@ +#ifndef FRAMEWORK_PUS_SERVICEPACKETS_SERVICE8PACKETS_H_ +#define FRAMEWORK_PUS_SERVICEPACKETS_SERVICE8PACKETS_H_ + +#include "../../action/ActionMessage.h" +#include "../../objectmanager/SystemObjectIF.h" +#include "../../serialize/SerialBufferAdapter.h" +#include "../../serialize/SerializeElement.h" +#include "../../serialize/SerialLinkedListAdapter.h" +#include "../../serialize/SerialFixedArrayListAdapter.h" + + +/** + * @brief Subservice 128 + * @ingroup spacepackets + */ +class DirectCommand: public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 128 +public: + + DirectCommand(const uint8_t* tcData, size_t size) { + SerializeAdapter::deSerialize(&objectId, &tcData, &size, + SerializeIF::Endianness::BIG); + SerializeAdapter::deSerialize(&actionId, &tcData, &size, + SerializeIF::Endianness::BIG); + parameterBuffer = tcData; + parametersSize = size; + } + + ActionId_t getActionId() const { + return actionId; + } + + object_id_t getObjectId() const { + return objectId; + } + + const uint8_t* getParameters() { + return parameterBuffer; + } + + uint32_t getParametersSize() const { + return parametersSize; + } + +private: + DirectCommand(const DirectCommand &command); + object_id_t objectId; + ActionId_t actionId; + uint32_t parametersSize; //!< [EXPORT] : [IGNORE] + const uint8_t * parameterBuffer; //!< [EXPORT] : [MAXSIZE] 65535 Bytes + +}; + + +/** + * @brief Subservice 130 + * Data reply (subservice 130) consists of + * 1. Target Object ID + * 2. Action ID + * 3. Data + * @ingroup spacepackets + */ +class DataReply: public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 130 +public: + typedef uint16_t typeOfMaxDataSize; + static const uint16_t MAX_DATA_LENGTH = sizeof(typeOfMaxDataSize); + DataReply(object_id_t objectId_, ActionId_t actionId_, + const uint8_t * replyDataBuffer_ = NULL, uint16_t replyDataSize_ = 0): + objectId(objectId_), actionId(actionId_), replyData(replyDataBuffer_,replyDataSize_){ + setLinks(); + } + +private: + DataReply(const DataReply &reply); + void setLinks() { + setStart(&objectId); + objectId.setNext(&actionId); + actionId.setNext(&replyData); + } + SerializeElement objectId; + SerializeElement actionId; + SerializeElement> replyData; +}; + + +/** + * @brief Subservice 132 + * @details + * Not used yet. Telecommand Verification takes care of this. + * @ingroup spacepackets + */ +class DirectReply: public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 132 +public: + typedef uint16_t typeOfMaxDataSize; + static const uint16_t MAX_DATA_LENGTH = sizeof(typeOfMaxDataSize); + + DirectReply(object_id_t objectId_, ActionId_t actionId_, ReturnValue_t returnCode_, + bool isStep_ = false, uint8_t step_ = 0): + isStep(isStep_), objectId(objectId_), actionId(actionId_), + returnCode(returnCode_),step(step_) { + setLinks(); + } +private: + + void setLinks() { + setStart(&objectId); + objectId.setNext(&actionId); + actionId.setNext(&returnCode); + if(isStep) { + returnCode.setNext(&step); + } + } + + bool isStep; //!< [EXPORT] : [IGNORE] + SerializeElement objectId; //!< [EXPORT] : [IGNORE] + SerializeElement actionId; //!< [EXPORT] : [IGNORE] + SerializeElement returnCode; //!< [EXPORT] : [IGNORE] + SerializeElement step; //!< [EXPORT] : [OPTIONAL] [IGNORE] + +}; + +#endif /* FRAMEWORK_PUS_SERVICEPACKETS_SERVICE8PACKETS_H_ */ diff --git a/fsfw/pus/servicepackets/Service9Packets.h b/fsfw/pus/servicepackets/Service9Packets.h new file mode 100644 index 0000000..11bd260 --- /dev/null +++ b/fsfw/pus/servicepackets/Service9Packets.h @@ -0,0 +1,32 @@ +#ifndef FSFW_PUS_SERVICEPACKETS_SERVICE9PACKETS_H_ +#define FSFW_PUS_SERVICEPACKETS_SERVICE9PACKETS_H_ + +#include "../../serialize/SerialLinkedListAdapter.h" + +/** + * @brief Subservice 128 + * @details + * It only contains the time encoded as ASCII, CRC, CUC or CDS + * @ingroup spacepackets + */ +class TimePacket : SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 128 +public: + TimePacket(const uint8_t * timeBuffer_, uint32_t timeSize_) { + timeBuffer = timeBuffer_; + timeSize = timeSize_; + } + const uint8_t* getTime() { + return timeBuffer; + } + + uint32_t getTimeSize() const { + return timeSize; + } + +private: + TimePacket(const TimePacket &command); + const uint8_t * timeBuffer; + uint32_t timeSize; //!< [EXPORT] : [IGNORE] +}; + +#endif /* FSFW_PUS_SERVICEPACKETS_SERVICE9PACKETS_H_ */ diff --git a/fsfw/returnvalues/FwClassIds.h b/fsfw/returnvalues/FwClassIds.h new file mode 100644 index 0000000..53add6a --- /dev/null +++ b/fsfw/returnvalues/FwClassIds.h @@ -0,0 +1,73 @@ +#ifndef FSFW_RETURNVALUES_FWCLASSIDS_H_ +#define FSFW_RETURNVALUES_FWCLASSIDS_H_ + +namespace CLASS_ID { +enum { + OPERATING_SYSTEM_ABSTRACTION = 1, //OS + OBJECT_MANAGER_IF, //OM + DEVICE_HANDLER_BASE, //DHB + RMAP_CHANNEL, //RMP + POWER_SWITCH_IF, //PS + HAS_MEMORY_IF, //PP + DEVICE_STATE_MACHINE_BASE, //DSMB + DATA_SET_CLASS, //DPS + POOL_RAW_ACCESS_CLASS, //DPR + CONTROLLER_BASE, //CTR + SUBSYSTEM_BASE, //SB + MODE_STORE_IF, //MS + SUBSYSTEM, //SS + HAS_MODES_IF, //HM + COMMAND_MESSAGE, //CM + CCSDS_TIME_HELPER_CLASS, //TIM + ARRAY_LIST, //AL + ASSEMBLY_BASE, //AB + MEMORY_HELPER, //MH + SERIALIZE_IF, //SE + FIXED_MAP, //FM + FIXED_MULTIMAP, //FMM + HAS_HEALTH_IF, //HHI + FIFO_CLASS, //FF + MESSAGE_PROXY, //MQP + TRIPLE_REDUNDACY_CHECK, //TRC + TC_PACKET_CHECK, //TCC + PACKET_DISTRIBUTION, //TCD + ACCEPTS_TELECOMMANDS_IF, //PUS + DEVICE_SERVICE_BASE, //DSB + COMMAND_SERVICE_BASE, //CSB + TM_STORE_BACKEND_IF, //TMB + TM_STORE_FRONTEND_IF, //TMF + STORAGE_AND_RETRIEVAL_SERVICE, //SR + MATCH_TREE_CLASS, //MT + EVENT_MANAGER_IF, //EV + HANDLES_FAILURES_IF, //FDI + DEVICE_HANDLER_IF, //DHI + STORAGE_MANAGER_IF, //SM + THERMAL_COMPONENT_IF, //TC + INTERNAL_ERROR_CODES, //IEC + TRAP, //TRP + CCSDS_HANDLER_IF, //CCS + PARAMETER_WRAPPER, //PAW + HAS_PARAMETERS_IF, //HPA + ASCII_CONVERTER, //ASC + POWER_SWITCHER, //POS + LIMITS_IF, //LIM + COMMANDS_ACTIONS_IF, //CF + HAS_ACTIONS_IF, //HF + DEVICE_COMMUNICATION_IF, //DC + BSP, //BSP + TIME_STAMPER_IF, //TSI 53 + SGP4PROPAGATOR_CLASS, //SGP4 54 + MUTEX_IF, //MUX 55 + MESSAGE_QUEUE_IF,//MQI 56 + SEMAPHORE_IF, //SPH 57 + LOCAL_POOL_OWNER_IF, //LPIF 58 + POOL_VARIABLE_IF, //PVA 59 + HOUSEKEEPING_MANAGER, //HKM 60 + DLE_ENCODER, //DLEE 61 + PUS_SERVICE_9, //PUS9 62 + FW_CLASS_ID_COUNT //is actually count + 1 ! + +}; +} + +#endif /* FSFW_RETURNVALUES_FWCLASSIDS_H_ */ diff --git a/fsfw/returnvalues/HasReturnvaluesIF.h b/fsfw/returnvalues/HasReturnvaluesIF.h new file mode 100644 index 0000000..4a3835b --- /dev/null +++ b/fsfw/returnvalues/HasReturnvaluesIF.h @@ -0,0 +1,31 @@ +#ifndef FSFW_RETURNVALUES_HASRETURNVALUESIF_H_ +#define FSFW_RETURNVALUES_HASRETURNVALUESIF_H_ + +#include "FwClassIds.h" +#include +#include + +#define MAKE_RETURN_CODE( number ) ((INTERFACE_ID << 8) + (number)) +typedef uint16_t ReturnValue_t; + + +class HasReturnvaluesIF { +public: + static const ReturnValue_t RETURN_OK = 0; + static const ReturnValue_t RETURN_FAILED = 1; + virtual ~HasReturnvaluesIF() {} + + /** + * It is discouraged to use the input parameters 0,0 and 0,1 as this + * will generate the RETURN_OK and RETURN_FAILED returnvalues. + * @param interfaceId + * @param number + * @return + */ + static constexpr ReturnValue_t makeReturnCode(uint8_t interfaceId, + uint8_t number) { + return (static_cast(interfaceId) << 8) + number; + } +}; + +#endif /* FSFW_RETURNVALUES_HASRETURNVALUESIF_H_ */ diff --git a/fsfw/rmap/RMAP.cpp b/fsfw/rmap/RMAP.cpp new file mode 100644 index 0000000..4ab8b17 --- /dev/null +++ b/fsfw/rmap/RMAP.cpp @@ -0,0 +1,89 @@ +#include "../devicehandlers/DeviceCommunicationIF.h" +#include "rmapStructs.h" +#include "RMAP.h" +#include "RMAPChannelIF.h" +#include + +ReturnValue_t RMAP::reset(RMAPCookie* cookie) { + return cookie->getChannel()->reset(); +} + +RMAP::RMAP(){ + +} + +ReturnValue_t RMAP::sendWriteCommand(RMAPCookie *cookie, uint8_t* buffer, + uint32_t length) { + uint8_t instruction; + + if ((buffer == NULL) && (length != 0)) { + return DeviceCommunicationIF::NULLPOINTER; + } + if (cookie->getChannel() == NULL) { + return COMMAND_NO_CHANNEL; + } + instruction = RMAPIds::RMAP_COMMAND_WRITE | cookie->getCommandMask(); + return cookie->getChannel()->sendCommand(cookie, instruction, buffer, + length); + +} + +ReturnValue_t RMAP::getWriteReply(RMAPCookie *cookie) { + if (cookie->getChannel() == NULL) { + return COMMAND_NO_CHANNEL; + } + if (cookie->getHeader()->instruction & (1 << RMAPIds::RMAP_COMMAND_BIT_WRITE)) { + return cookie->getChannel()->getReply(cookie, NULL, NULL); + } else { + return REPLY_MISSMATCH; + } +} + +ReturnValue_t RMAP::writeBlocking(RMAPCookie *cookie, uint8_t* buffer, + uint32_t length, uint32_t timeout_us) { + if (cookie->getChannel() == NULL) { + return COMMAND_NO_CHANNEL; + } + return cookie->getChannel()->sendCommandBlocking(cookie, buffer, length, + NULL, NULL, timeout_us); + +} +ReturnValue_t RMAP::sendReadCommand(RMAPCookie *cookie, uint32_t expLength) { + uint8_t command; + if (cookie->getChannel() == NULL) { + return COMMAND_NO_CHANNEL; + } + command = RMAPIds::RMAP_COMMAND_READ + | (cookie->getCommandMask() & ~(1 << RMAPIds::RMAP_COMMAND_BIT_VERIFY)); + + return cookie->getChannel()->sendCommand(cookie, command, NULL, expLength); + +} + +ReturnValue_t RMAP::getReadReply(RMAPCookie *cookie, uint8_t **buffer, + uint32_t *size) { + if (cookie->getChannel() == NULL) { + return COMMAND_NO_CHANNEL; + } + if (buffer == NULL || size == NULL) { + return DeviceCommunicationIF::NULLPOINTER; + } + if (cookie->getHeader()->instruction & (1 << RMAPIds::RMAP_COMMAND_BIT_WRITE)) { + return REPLY_MISSMATCH; + } else { + return cookie->getChannel()->getReply(cookie, buffer, size); + } + +} + +ReturnValue_t RMAP::readBlocking(RMAPCookie *cookie, uint32_t expLength, + uint8_t** buffer, uint32_t *size, uint32_t timeout_us) { + if (cookie->getChannel() == NULL) { + return COMMAND_NO_CHANNEL; + } + if (buffer == NULL || size == NULL) { + return DeviceCommunicationIF::NULLPOINTER; + } + return cookie->getChannel()->sendCommandBlocking(cookie, NULL, expLength, + buffer, size, timeout_us); +} diff --git a/fsfw/rmap/RMAP.h b/fsfw/rmap/RMAP.h new file mode 100644 index 0000000..d14320c --- /dev/null +++ b/fsfw/rmap/RMAP.h @@ -0,0 +1,227 @@ +#ifndef RMAPpp_H_ +#define RMAPpp_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "RMAPCookie.h" + +//SHOULDTODO: clean up includes for RMAP, should be enough to include RMAP.h but right now it's quite chaotic... + +/** + * API for a Cookie/Channel based RMAP implementation. + * + * The API offers the four basic RMAP actions: sending a Read or Write command and getting the reply to each. + * As RMAP is an asynchronous protocol, these are implemented as four seperate calls. There are blocking + * calls which combine a send and get call using a timeout, but these are "real" blocking, looping in between + * the calls. + * + * Cookies are used to contain Information between a send[Read,Write]Command and a get[Read,Write]Reply call, + * one can think of them like *nix file descriptors. A cookie is valid from a sendX call until the related getX + * call. That means if a cookie is used in a getX call, the reply to the sendX call the cookie was used with + * previously is returned. + * Depending on the underlying RMAPChannel implementation, a cookie can also be valid for more than one getX + * call, but this should not be assumed generally. + * A cookie encapsulates information like the RMAP Channel to use, as well as the RMAP Address + * and a Command Mask used for specifying which RMAP capabilities are used. + * Cookies are created without interaction with this API, there is no open() call. The RMAP implementation + * will initialize all fields which are not set by the cookie's constructor. + * + * The RMAP implementation relies on Channels. A channel is used to construct the RMAP Headers and handle the + * protocol. It is access via the RMAPChannelIF. Thus it is possible to use different Implementations which only + * need to implement the RMAPChannelIF. A channel is also responsible for accessing the lower layers, for example + * a SPaceWire transport layer. + * + * There is one RMAP Channel per physical device. The cookie-channel as well as the device-channel assignment + * can be changed at runtime to allow for example redundancy switching. This API is static as the information which + * channel to use is contained within the cookie. + */ +class RMAP: public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::RMAP_CHANNEL; + + //static const ReturnValue_t COMMAND_OK = MAKE_RETURN_CODE(0x00); + static const ReturnValue_t COMMAND_NO_DESCRIPTORS_AVAILABLE = + MAKE_RETURN_CODE(0xE1); //no descriptors available for sending command; command was not sent + static const ReturnValue_t COMMAND_BUFFER_FULL = MAKE_RETURN_CODE(0xE2); //no receiver buffer available for expected len; command was not sent + static const ReturnValue_t COMMAND_CHANNEL_OUT_OF_RANGE = MAKE_RETURN_CODE( + 0xE3); //The cookie points to an invalid channel; command was not sent +//Replaced by DeviceCommunicationIF::TOO_MUCH_DATA static const ReturnValue_t COMMAND_TOO_BIG = MAKE_RETURN_CODE(0xE4); //the data that was to be sent was too long for the hw to handle (write command) or the expected len was bigger than maximal expected len (read command) + //command was not sent +//replaced by DeviceCommunicationIF::NULLPOINTER static const ReturnValue_t COMMAND_NULLPOINTER = MAKE_RETURN_CODE(0xE5); //datalen was != 0 but data was == NULL in write command, or nullpointer in read command + static const ReturnValue_t COMMAND_CHANNEL_DEACTIVATED = MAKE_RETURN_CODE( + 0xE6); //the channel has no port set + static const ReturnValue_t COMMAND_PORT_OUT_OF_RANGE = MAKE_RETURN_CODE( + 0xE7); //The specified port is not valid + static const ReturnValue_t COMMAND_PORT_IN_USE = MAKE_RETURN_CODE(0xE8);//The specified port is already in use + static const ReturnValue_t COMMAND_NO_CHANNEL = MAKE_RETURN_CODE(0xE9);//The cookie to work with has no channel assigned. + static const ReturnValue_t NO_HW_CRC = MAKE_RETURN_CODE(0xEA);//The SpW port does not support HW CRC generation, which is unsupported + //return values for both get_write_reply and get_read_reply + static const ReturnValue_t REPLY_NO_REPLY = MAKE_RETURN_CODE(0xD0); //no reply was received + static const ReturnValue_t REPLY_NOT_SENT = MAKE_RETURN_CODE(0xD1); //command was not sent, implies no reply + static const ReturnValue_t REPLY_NOT_YET_SENT = MAKE_RETURN_CODE(0xD2); //command is still waiting to be sent + static const ReturnValue_t REPLY_MISSMATCH = MAKE_RETURN_CODE(0xD3);//a read command was issued, but get_write_rply called, or other way round + static const ReturnValue_t REPLY_TIMEOUT = MAKE_RETURN_CODE(0xD4);//timeout +//replaced by DeviceCommunicationIF::NULLPOINTER static const ReturnValue_t REPLY_NULLPOINTER = MAKE_RETURN_CODE(0xD5);//one of the arguments in a read reply was NULL + //return values for get_reply + static const ReturnValue_t REPLY_INTERFACE_BUSY = MAKE_RETURN_CODE( + 0xC0);//Interface is busy (transmission buffer still being processed) + static const ReturnValue_t REPLY_TRANSMISSION_ERROR = + MAKE_RETURN_CODE(0xC1); //Interface encountered errors during last operation, data could not be processed. (transmission error) + static const ReturnValue_t REPLY_INVALID_DATA = MAKE_RETURN_CODE( + 0xC2); //Invalid data (amount / value) + static const ReturnValue_t REPLY_NOT_SUPPORTED = MAKE_RETURN_CODE( + 0xC3); + + //return values for reset + static const ReturnValue_t LINK_DOWN = MAKE_RETURN_CODE(0xF0);//The spw link is down + //Other SpW codes: + static const ReturnValue_t SPW_CREDIT = MAKE_RETURN_CODE(0xF1); + static const ReturnValue_t SPW_ESCAPE = MAKE_RETURN_CODE(0xF2); + static const ReturnValue_t SPW_DISCONNECT = MAKE_RETURN_CODE(0xF3); + static const ReturnValue_t SPW_PARITY = MAKE_RETURN_CODE(0xF4); + static const ReturnValue_t SPW_WRITE_SYNC = MAKE_RETURN_CODE(0xF5); + static const ReturnValue_t SPW_INVALID_ADDRESS = MAKE_RETURN_CODE(0xF6); + static const ReturnValue_t SPW_EARLY_EOP = MAKE_RETURN_CODE(0xF7); + static const ReturnValue_t SPW_DMA = MAKE_RETURN_CODE(0xF8); + static const ReturnValue_t SPW_LINK_ERROR = MAKE_RETURN_CODE(0xF9); + + + //RMAP standard replies + static const ReturnValue_t REPLY_OK = MAKE_RETURN_CODE(0); + static const ReturnValue_t REPLY_GENERAL_ERROR_CODE = MAKE_RETURN_CODE(1);// The detected error does not fit into the other + // error cases or the node does not support + // further distinction between the errors + static const ReturnValue_t REPLY_UNUSED_PACKET_TYPE_OR_COMMAND_CODE = + MAKE_RETURN_CODE(2); // The Header CRC was decoded correctly but + // the packet type is reserved or the command + // is not used by the RMAP protocol. + static const ReturnValue_t REPLY_INVALID_KEY = MAKE_RETURN_CODE(3); // The Header CRC was decoded correctly but + // the device key did not match that expected + // by the target user application + static const ReturnValue_t REPLY_INVALID_DATA_CRC = MAKE_RETURN_CODE(4);// Error in the CRC of the data field + static const ReturnValue_t REPLY_EARLY_EOP = MAKE_RETURN_CODE(5);// EOP marker detected before the end of the data + static const ReturnValue_t REPLY_TOO_MUCH_DATA = MAKE_RETURN_CODE(6);// More than the expected amount of data in a + // command has been received + static const ReturnValue_t REPLY_EEP = MAKE_RETURN_CODE(7); // EEP marker detected immediately after the + // header CRC or during the transfer of data + // and Data CRC or immediately thereafter. + // Indicates that there was a communication + // failure of some sort on the network + static const ReturnValue_t REPLY_RESERVED = MAKE_RETURN_CODE(8);// Reserved + static const ReturnValue_t REPLY_VERIFY_BUFFER_OVERRUN = MAKE_RETURN_CODE( + 9); // The verify before write bit of the command + // was set so that the data field was buffered in + // order to verify the Data CRC before + // transferring the data to target memory. The + // data field was longer than able to fit inside + // the verify buffer resulting in a buffer overrun + // Note that the command is not executed in + // this case + static const ReturnValue_t REPLY_COMMAND_NOT_IMPLEMENTED_OR_NOT_AUTHORISED = + MAKE_RETURN_CODE(10);// The target user application did not authorise + // the requested operation. This may be because + // the command requested has not been + // implemented + static const ReturnValue_t REPLY_RMW_DATA_LENGTH_ERROR = MAKE_RETURN_CODE( + 11); // The amount of data in a RMW command is + // invalid (0x01 0x03 0x05 0x07 or greater + // than 0x08) + static const ReturnValue_t REPLY_INVALID_TARGET_LOGICAL_ADDRESS = + MAKE_RETURN_CODE(12); // The Header CRC was decoded correctly but + // the Target Logical Address was not the value + // expected by the target + + /** + * Resets the underlying channel. + * + * @param cookie The cookie which points to the channel to reset + * @return + */ + static ReturnValue_t reset(RMAPCookie *cookie); + + /** + * send an RMAP write command + * + * datalen is only 24bit wide, rest will be ignored + * IMPORTANT: the data buffer must be datalen+1 large, as the driver might + * write a CRC sum at data[datalen] + * if you want to send an empty packet, just do datalen = 0 and data = NULL + * + * @param cookie The cookie to write to + * @param buffer the data to write + * @param length length of data + * @return + * - @c COMMAND_NULLPOINTER datalen was != 0 but data was == NULL in write command + * - return codes of RMAPChannelIF::sendCommand() + */ + static ReturnValue_t sendWriteCommand(RMAPCookie *cookie, uint8_t* buffer, + uint32_t length); + + /** + * get the reply to a write command + * + * @param cookie the cookie the command was sent with + * @return + * - @c REPLY_MISSMATCH a read command was issued, but getWriteReply called + * - return codes of RMAPChannelIF::getReply() + */ + static ReturnValue_t getWriteReply(RMAPCookie *cookie); + + /** + * @see sendWriteCommand() + * @see getWriteReply() + * + * @param timeout_us the time after the function returns, if no reply was received + * + * @return + * - All of sendWriteCommand() + * - All of getWriteReply() + * - @c REPLY_TIMEOUT timeout + */ + static ReturnValue_t writeBlocking(RMAPCookie *cookie, uint8_t* buffer, + uint32_t length, uint32_t timeout_us); + + /** + * send an RMAP read command + * + * @param cookie to cookie to read from + * @param expLength the expected maximum length of the reply + * @return + * - @c COMMAND_NULLPOINTER datalen was != 0 but data was == NULL in write command, or nullpointer in read command + * - return codes of RMAPChannelIF::sendCommand() + */ + static ReturnValue_t sendReadCommand(RMAPCookie *cookie, + uint32_t expLength); + + /** + * get a reply to an RMAP read command + * + * @param cookie the cookie that was read from + * @param[out] buffer the location of the data + * @param[out] size size of the data + * @return + * - @c COMMAND_NULLPOINTER buffer or size was NULL + * - @c REPLY_MISSMATCH a write command was issued, but getReadReply called + * - return codes of RMAPChannelIF::getReply() + */ + static ReturnValue_t getReadReply(RMAPCookie *cookie, uint8_t **buffer, + uint32_t *size); + + /** + * @see sendReadCommand() + * @see getReadReply() + * + * @param timeout_us the time after the function returns, if no reply was received + * + * @return + * - All of sendReadCommand() + * - All of getReadReply() + * - @c REPLY_TIMEOUT timeout + */ + static ReturnValue_t readBlocking(RMAPCookie *cookie, uint32_t expLength, + uint8_t** buffer, uint32_t *size, uint32_t timeout_us); + +protected: + RMAP(); +}; + +#endif /* RMAPpp_H_ */ diff --git a/fsfw/rmap/RMAPChannelIF.h b/fsfw/rmap/RMAPChannelIF.h new file mode 100644 index 0000000..dac4a72 --- /dev/null +++ b/fsfw/rmap/RMAPChannelIF.h @@ -0,0 +1,115 @@ +#ifndef RMAPCHANNELIF_H_ +#define RMAPCHANNELIF_H_ + +#include "RMAPCookie.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +class RMAPChannelIF { +public: + virtual ~RMAPChannelIF(){}; + /** + * Reset an RMAP channel + * + * Clears the receive buffer (all received messages are deleted) and resets the descriptor table. + * Also checks for errors in the descriptors and submits them to FDIR (aka stdout) + * + * @param channel to reset + * + * @return + * - @c LINK_DOWN when the link is down and all replies were missed + * - @c COMMAND_CHANNEL_DEACTIVATED if the channel's port is NULL + * - @c RETURN_OK else + */ + virtual ReturnValue_t reset()=0; + + /** + * Check if a channel is active (ie has a port) + * + * @param channel_nr + * @return + * - @c COMMAND_OK if channel is active + * - @c COMMAND_CHANNEL_DEACTIVATED if channel is deactivated + */ + virtual ReturnValue_t isActive()=0; + + /** + * Assign a SpaceWire port to the Channel + * + * @param port Number of the port. SpaceWire devices are mapped to port numbers to allow checking of the validity + * @param dest_addr the destination address used by all packets sent from this channel + * @param src_addr the source address used by all packets sent from this channel and used when checking incoming packets + * @return + * - @c COMMAND_OK if port was changed + * - @c COMMAND_PORT_OUT_OF_RANGE if the port is invalid + */ + virtual ReturnValue_t setPort(int8_t port, uint8_t dest_addr, + uint8_t src_addr)=0; + + /** + * Assign a SpaceWire port to the Channel + * + * same as setPort(int8_t port, uint8_t dest_addr, uint8_t src_addr), only the addresses are left unchanged + * + * @param port Number of the port. SpaceWire devices are mapped to port numbers to allow checking of the validity + * @return + * - @c COMMAND_OK if port was changed + * - @c COMMAND_PORT_OUT_OF_RANGE if the port is invalid + */ + virtual ReturnValue_t setPort(int8_t port)=0; + + /** + * Send an RMAP command + * + * @param cookie the cookie used with this call + * @param instruction the instruction byte that will be sent (this defines if it is a read or write command) + * @param data data to be sent + * @param datalen length of data + * @return + * - @c RETURN_OK + * - @c COMMAND_NO_DESCRIPTORS_AVAILABLE no descriptors available for sending command; command was not sent + * - @c COMMAND_BUFFER_FULL no receiver buffer available for expected len; command was not sent + * - @c COMMAND_TOO_BIG the data that was to be sent was too long for the hw to handle (write command) or the expected len was bigger than maximal expected len (read command) command was not sent + * - @c COMMAND_CHANNEL_DEACTIVATED the channel has no port set + * - @c NOT_SUPPORTED if you dont feel like implementing something... + */ + virtual ReturnValue_t sendCommand(RMAPCookie *cookie, uint8_t instruction, + uint8_t *data, uint32_t datalen)=0; + + /** + * get the reply to an rmap command + * + * @param cookie the cookie the command was sent with + * @param databuffer a pointer to a pointer the location of the reply will be written to + * @param len a pointer to the variable the length of the reply will be written to + * @return + * - @c REPLY_NO_REPLY no reply was received + * - @c REPLY_NOT_SENT command was not sent, implies no reply + * - @c REPLY_NOT_YET_SENT command is still waiting to be sent + * - @c WRITE_REPLY_INTERFACE_BUSY Interface is busy (transmission buffer still being processed) + * - @c WRITE_REPLY_TRANSMISSION_ERROR Interface encountered errors during last operation, data could not be processed. (transmission error) + * - @c WRITE_REPLY_INVALID_DATA Invalid data (amount / value) + * - @c WRITE_REPLY_NOT_SUPPORTED + * - all RMAP standard replies + */ + virtual ReturnValue_t getReply(RMAPCookie *cookie, uint8_t **databuffer, + uint32_t *len)=0; + + /** + * + * @param cookie + * @param data + * @param datalen + * @param databuffer + * @param len + * @param timeout_us + * @return + * - all replies of sendCommand() and getReply() + * - @c REPLY_TIMEOUT timeout + */ + virtual ReturnValue_t sendCommandBlocking(RMAPCookie *cookie, uint8_t *data, + uint32_t datalen, uint8_t **databuffer, uint32_t *len, + uint32_t timeout_us)=0; + +}; + +#endif /* RMAPCHANNELIF_H_ */ diff --git a/fsfw/rmap/RMAPCookie.cpp b/fsfw/rmap/RMAPCookie.cpp new file mode 100644 index 0000000..597f066 --- /dev/null +++ b/fsfw/rmap/RMAPCookie.cpp @@ -0,0 +1,124 @@ +#include "RMAPChannelIF.h" +#include "RMAPCookie.h" +#include + + +RMAPCookie::RMAPCookie() { + this->header.dest_address = 0; + this->header.protocol = 0x01; + this->header.instruction = 0; + this->header.dest_key = 0; + this->header.source_address = 0; + this->header.tid_h = 0; + this->header.tid_l = 0; + this->header.extended_address = 0; + this->header.address_hh = 0; + this->header.address_h = 0; + this->header.address_l = 0; + this->header.address_ll = 0; + this->header.datalen_h = 0; + this->header.datalen_m = 0; + this->header.datalen_l = 0; + this->header.header_crc = 0; + this->channel = NULL; + this->command_mask = 0; + + this->dataCRC = 0; + + this->maxReplyLen = 0; +} + + + +RMAPCookie::RMAPCookie(uint32_t set_address, uint8_t set_extended_address, + RMAPChannelIF *set_channel, uint8_t set_command_mask, uint32_t maxReplyLen) { + this->header.dest_address = 0; + this->header.protocol = 0x01; + this->header.instruction = 0; + this->header.dest_key = 0; + this->header.source_address = 0; + this->header.tid_h = 0; + this->header.tid_l = 0; + this->header.extended_address = set_extended_address; + setAddress(set_address); + this->header.datalen_h = 0; + this->header.datalen_m = 0; + this->header.datalen_l = 0; + this->header.header_crc = 0; + this->channel = set_channel; + this->command_mask = set_command_mask; + this->dataCRC = 0; + + this->maxReplyLen = maxReplyLen; +} + + +void RMAPCookie::setAddress(uint32_t address) { + this->header.address_hh = (address & 0xFF000000) >> 24; + this->header.address_h = (address & 0x00FF0000) >> 16; + this->header.address_l = (address & 0x0000FF00) >> 8; + this->header.address_ll = address & 0x000000FF; +} + +void RMAPCookie::setExtendedAddress(uint8_t extendedAddress) { + this->header.extended_address = extendedAddress; +} + +void RMAPCookie::setChannel(RMAPChannelIF *channel) { + this->channel = channel; +} + +void RMAPCookie::setCommandMask(uint8_t commandMask) { + this->command_mask = commandMask; +} + +uint32_t RMAPCookie::getAddress() { + return (header.address_hh << 24) + (header.address_h << 16) + + (header.address_l << 8) + (header.address_ll); +} + +uint8_t RMAPCookie::getExtendedAddress() { + return header.extended_address; +} + +RMAPChannelIF *RMAPCookie::getChannel() { + return channel; +} + +uint8_t RMAPCookie::getCommandMask() { + return command_mask; +} + +RMAPCookie::~RMAPCookie() { + +} + +uint32_t RMAPCookie::getMaxReplyLen() const { + return maxReplyLen; +} + +void RMAPCookie::setMaxReplyLen(uint32_t maxReplyLen) { + this->maxReplyLen = maxReplyLen; +} + +RMAPStructs::rmap_cmd_header* RMAPCookie::getHeader(){ + return &this->header; +} + +uint16_t RMAPCookie::getTransactionIdentifier() const { + return static_cast((header.tid_h << 8) | (header.tid_l)); +} + +void RMAPCookie::setTransactionIdentifier(uint16_t id_) { + header.tid_l = id_ & 0xFF; + header.tid_h = (id_ >> 8 ) & 0xFF; +} + +uint32_t RMAPCookie::getDataLength() const { + return static_cast(header.datalen_h << 16 | header.datalen_m << 8 | header.datalen_l); +} +void RMAPCookie::setDataLength(uint32_t length_) { + header.datalen_l = length_ & 0xff; + header.datalen_m = (length_ >> 8) & 0xff; + header.datalen_h = (length_ >> 16) & 0xff; +} diff --git a/fsfw/rmap/RMAPCookie.h b/fsfw/rmap/RMAPCookie.h new file mode 100644 index 0000000..4c6081b --- /dev/null +++ b/fsfw/rmap/RMAPCookie.h @@ -0,0 +1,58 @@ +#ifndef RMAPCOOKIE_H_ +#define RMAPCOOKIE_H_ + +#include "../devicehandlers/CookieIF.h" +#include "rmapStructs.h" + +class RMAPChannelIF; + +class RMAPCookie : public CookieIF { +public: + //To Uli: Sorry, I need an empty ctor to initialize an array of cookies. + RMAPCookie(); + + RMAPCookie(uint32_t set_address, uint8_t set_extended_address, + RMAPChannelIF *set_channel, uint8_t set_command_mask, uint32_t maxReplyLen = 0); + virtual ~RMAPCookie(); + + + void setAddress(uint32_t address); + uint32_t getAddress(); + + void setExtendedAddress(uint8_t); + uint8_t getExtendedAddress(); + + void setChannel(RMAPChannelIF *channel); + RMAPChannelIF *getChannel(); + + void setCommandMask(uint8_t commandMask); + uint8_t getCommandMask(); + + uint32_t getMaxReplyLen() const; + void setMaxReplyLen(uint32_t maxReplyLen); + + uint16_t getTransactionIdentifier() const; + void setTransactionIdentifier(uint16_t id_); + + RMAPStructs::rmap_cmd_header* getHeader(); + + uint32_t getDataLength() const; + void setDataLength(uint32_t lenght_); + + uint8_t getDataCrc() const { + return dataCRC; + } + + void setDataCrc(uint8_t dataCrc) { + dataCRC = dataCrc; + } + +protected: + RMAPStructs::rmap_cmd_header header; + RMAPChannelIF *channel; + uint8_t command_mask; + uint32_t maxReplyLen; + uint8_t dataCRC; +}; + +#endif /* RMAPCOOKIE_H_ */ diff --git a/fsfw/rmap/RmapDeviceCommunicationIF.cpp b/fsfw/rmap/RmapDeviceCommunicationIF.cpp new file mode 100644 index 0000000..db4a75b --- /dev/null +++ b/fsfw/rmap/RmapDeviceCommunicationIF.cpp @@ -0,0 +1,47 @@ +#include "RmapDeviceCommunicationIF.h" +#include "RMAP.h" + +//TODO Cast here are all potential bugs +RmapDeviceCommunicationIF::~RmapDeviceCommunicationIF() { +} + +ReturnValue_t RmapDeviceCommunicationIF::sendMessage(CookieIF* cookie, + uint8_t* data, uint32_t len) { + return RMAP::sendWriteCommand((RMAPCookie *) cookie, data, len); +} + +ReturnValue_t RmapDeviceCommunicationIF::getSendSuccess(CookieIF* cookie) { + return RMAP::getWriteReply((RMAPCookie *) cookie); +} + +ReturnValue_t RmapDeviceCommunicationIF::requestReceiveMessage( + CookieIF* cookie) { + return RMAP::sendReadCommand((RMAPCookie *) cookie, + ((RMAPCookie *) cookie)->getMaxReplyLen()); +} + +ReturnValue_t RmapDeviceCommunicationIF::readReceivedMessage(CookieIF* cookie, + uint8_t** buffer, uint32_t* size) { + return RMAP::getReadReply((RMAPCookie *) cookie, buffer, size); +} + +ReturnValue_t RmapDeviceCommunicationIF::setAddress(CookieIF* cookie, + uint32_t address) { + + ((RMAPCookie *) cookie)->setAddress(address); + return HasReturnvaluesIF::RETURN_OK; +} + +uint32_t RmapDeviceCommunicationIF::getAddress(CookieIF* cookie) { + return ((RMAPCookie *) cookie)->getAddress(); +} + +ReturnValue_t RmapDeviceCommunicationIF::setParameter(CookieIF* cookie, + uint32_t parameter) { + //TODO Empty? + return HasReturnvaluesIF::RETURN_FAILED; +} + +uint32_t RmapDeviceCommunicationIF::getParameter(CookieIF* cookie) { + return 0; +} diff --git a/fsfw/rmap/RmapDeviceCommunicationIF.h b/fsfw/rmap/RmapDeviceCommunicationIF.h new file mode 100644 index 0000000..dacc720 --- /dev/null +++ b/fsfw/rmap/RmapDeviceCommunicationIF.h @@ -0,0 +1,80 @@ +#ifndef MISSION_RMAP_RMAPDEVICECOMMUNICATIONINTERFACE_H_ +#define MISSION_RMAP_RMAPDEVICECOMMUNICATIONINTERFACE_H_ + +#include "../devicehandlers/DeviceCommunicationIF.h" + +/** + * @brief This class is a implementation of a DeviceCommunicationIF for RMAP calls. It expects RMAPCookies or a derived class of RMAPCookies + * + * @details The open, close and reOpen calls are mission specific + * The open call might return any child of RMAPCookies + * + * \ingroup rmap + */ +class RmapDeviceCommunicationIF: public DeviceCommunicationIF { + +public: + virtual ~RmapDeviceCommunicationIF(); + + + /** + * This method is mission specific as the open call will return a mission specific cookie + * + * @param cookie A cookie, can be mission specific subclass of RMAP Cookie + * @param address The address of the RMAP Cookie + * @param maxReplyLen Maximum length of expected reply + * @return + */ + virtual ReturnValue_t open(CookieIF **cookie, uint32_t address, + uint32_t maxReplyLen) = 0; + + /** + * Use an existing cookie to open a connection to a new DeviceCommunication. + * The previous connection must not be closed. + * If the returnvalue is not RETURN_OK, the cookie is unchanged and + * can be used with the previous connection. + * + * @param cookie + * @param address + * @param maxReplyLen + * @return + */ + virtual ReturnValue_t reOpen(CookieIF *cookie, uint32_t address, + uint32_t maxReplyLen) = 0; + + + /** + * Closing call of connection and memory free of cookie. Mission dependent call + * @param cookie + */ + virtual void close(CookieIF *cookie) = 0; + + //SHOULDDO can data be const? + /** + * + * + * @param cookie Expects an RMAPCookie or derived from RMAPCookie Class + * @param data Data to be send + * @param len Length of the data to be send + * @return - Return codes of RMAP::sendWriteCommand() + */ + virtual ReturnValue_t sendMessage(CookieIF *cookie, uint8_t *data, + uint32_t len); + + virtual ReturnValue_t getSendSuccess(CookieIF *cookie); + + virtual ReturnValue_t requestReceiveMessage(CookieIF *cookie); + + virtual ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, + uint32_t *size); + + virtual ReturnValue_t setAddress(CookieIF *cookie, uint32_t address); + + virtual uint32_t getAddress(CookieIF *cookie); + + virtual ReturnValue_t setParameter(CookieIF *cookie, uint32_t parameter); + + virtual uint32_t getParameter(CookieIF *cookie); +}; + +#endif /* MISSION_RMAP_RMAPDEVICECOMMUNICATIONINTERFACE_H_ */ diff --git a/fsfw/rmap/rmapStructs.h b/fsfw/rmap/rmapStructs.h new file mode 100644 index 0000000..bf0fb26 --- /dev/null +++ b/fsfw/rmap/rmapStructs.h @@ -0,0 +1,98 @@ +#ifndef RMAPSTRUCTS_H_ +#define RMAPSTRUCTS_H_ + +#include + +//SHOULDDO: having the defines within a namespace would be nice. Problem are the defines referencing the previous define, eg RMAP_COMMAND_WRITE + +////////////////////////////////////////////////////////////////////////////////// +// RMAP command bits +//#define RMAP_COMMAND_BIT_INCREMENT 2 +//#define RMAP_COMMAND_BIT_REPLY 3 +//#define RMAP_COMMAND_BIT_WRITE 5 +//#define RMAP_COMMAND_BIT_VERIFY 4 +//#define RMAP_COMMAND_BIT 6 + +namespace RMAPIds{ + +static const uint8_t RMAP_COMMAND_BIT_INCREMENT = 2; +static const uint8_t RMAP_COMMAND_BIT_REPLY = 3; +static const uint8_t RMAP_COMMAND_BIT_WRITE = 5; +static const uint8_t RMAP_COMMAND_BIT_VERIFY = 4; +static const uint8_t RMAP_COMMAND_BIT = 6; + +////////////////////////////////////////////////////////////////////////////////// +// RMAP commands +static const uint8_t RMAP_COMMAND_WRITE = ((1< +#include + +/** + * Helper class to convert variables or bitstreams between machine + * endian and either big or little endian. + * Machine endian is the endianness used by the machine running the + * program and is one of big or little endian. As this is portable + * code, it is not known at coding time which it is. At compile time + * it is however, which is why this is implemented using compiler + * macros and translates to a copy operation at runtime. + * + * This changes the layout of multi-byte variables in the machine's + * memory. In most cases, you should not need to use this class. + * Probably what you are looking for is the SerializeAdapter. + * If you still decide you need this class, please read and understand + * the code first. + * + * The order of the individual bytes of the multi-byte variable is + * reversed, the byte at the highest address is moved to the lowest + * address and vice versa, same for the bytes in between. + * + * Note that the conversion is also its inversion, that is converting + * from machine to a specified endianness is the same operation as + * converting from specified to machine (I looked it up, mathematicians + * would call it an involution): + * + * X == convertBigEndian(convertBigEndian(X)) + * + * Thus, there is only one function supplied to do the conversion. + */ +class EndianConverter { +private: + EndianConverter() {}; +public: + /** + * Convert a typed variable between big endian and machine endian. + * Intended for plain old datatypes. + */ + template + static T convertBigEndian(T in) { +#ifndef BYTE_ORDER_SYSTEM +#error BYTE_ORDER_SYSTEM not defined +#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN + T tmp; + uint8_t *pointerOut = (uint8_t*) &tmp; + uint8_t *pointerIn = (uint8_t*) ∈ + for (size_t count = 0; count < sizeof(T); count++) { + pointerOut[sizeof(T) - count - 1] = pointerIn[count]; + } + return tmp; +#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN + return in; +#else +#error Unknown Byte Order +#endif + } + + /** + * convert a bytestream representing a single variable between big endian + * and machine endian. + */ + static void convertBigEndian(uint8_t *out, const uint8_t *in, + size_t size) { +#ifndef BYTE_ORDER_SYSTEM +#error BYTE_ORDER_SYSTEM not defined +#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN + for (size_t count = 0; count < size; count++) { + out[size - count - 1] = in[count]; + } + return; +#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN + memcpy(out, in, size); + return; +#endif + } + + /** + * Convert a typed variable between little endian and machine endian. + * Intended for plain old datatypes. + */ + template + static T convertLittleEndian(T in) { +#ifndef BYTE_ORDER_SYSTEM + #error BYTE_ORDER_SYSTEM not defined + #elif BYTE_ORDER_SYSTEM == BIG_ENDIAN + T tmp; + uint8_t *pointerOut = (uint8_t *) &tmp; + uint8_t *pointerIn = (uint8_t *) ∈ + for (size_t count = 0; count < sizeof(T); count++) { + pointerOut[sizeof(T) - count - 1] = pointerIn[count]; + } + return tmp; + #elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN + return in; +#else + #error Unknown Byte Order + #endif + } + /** + * convert a bytestream representing a single variable between little endian + * and machine endian. + */ + static void convertLittleEndian(uint8_t *out, const uint8_t *in, + size_t size) { +#ifndef BYTE_ORDER_SYSTEM + #error BYTE_ORDER_SYSTEM not defined + #elif BYTE_ORDER_SYSTEM == BIG_ENDIAN + for (size_t count = 0; count < size; count++) { + out[size - count - 1] = in[count]; + } + return; + #elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN + memcpy(out, in, size); + return; +#endif + } +}; + +#endif /* FSFW_SERIALIZE_ENDIANCONVERTER_H_ */ diff --git a/fsfw/serialize/SerialArrayListAdapter.h b/fsfw/serialize/SerialArrayListAdapter.h new file mode 100644 index 0000000..daa3fe7 --- /dev/null +++ b/fsfw/serialize/SerialArrayListAdapter.h @@ -0,0 +1,86 @@ +#ifndef FSFW_SERIALIZE_SERIALARRAYLISTADAPTER_H_ +#define FSFW_SERIALIZE_SERIALARRAYLISTADAPTER_H_ + +#include "SerializeIF.h" +#include "../container/ArrayList.h" +#include + +/** + * Also serializes length field ! + * @author baetz + * @ingroup serialize + */ +template +class SerialArrayListAdapter : public SerializeIF { +public: + SerialArrayListAdapter(ArrayList *adaptee) : adaptee(adaptee) { + } + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + return serialize(adaptee, buffer, size, maxSize, streamEndianness); + } + + static ReturnValue_t serialize(const ArrayList* list, + uint8_t** buffer, size_t* size, size_t maxSize, + Endianness streamEndianness) { + ReturnValue_t result = SerializeAdapter::serialize(&list->size, + buffer, size, maxSize, streamEndianness); + count_t i = 0; + while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) { + result = SerializeAdapter::serialize(&list->entries[i], buffer, size, + maxSize, streamEndianness); + ++i; + } + return result; + } + + virtual size_t getSerializedSize() const { + return getSerializedSize(adaptee); + } + + static uint32_t getSerializedSize(const ArrayList* list) { + uint32_t printSize = sizeof(count_t); + count_t i = 0; + + for (i = 0; i < list->size; ++i) { + printSize += SerializeAdapter::getSerializedSize(&list->entries[i]); + } + + return printSize; + } + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + return deSerialize(adaptee, buffer, size, streamEndianness); + } + + static ReturnValue_t deSerialize(ArrayList* list, + const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + count_t tempSize = 0; + ReturnValue_t result = SerializeAdapter::deSerialize(&tempSize, + buffer, size, streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (tempSize > list->maxSize()) { + return SerializeIF::TOO_MANY_ELEMENTS; + } + + list->size = tempSize; + count_t i = 0; + while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) { + result = SerializeAdapter::deSerialize( + &list->front()[i], buffer, size, + streamEndianness); + ++i; + } + return result; + } + +private: + ArrayList *adaptee; +}; + +#endif /* FSFW_SERIALIZE_SERIALARRAYLISTADAPTER_H_ */ diff --git a/fsfw/serialize/SerialBufferAdapter.cpp b/fsfw/serialize/SerialBufferAdapter.cpp new file mode 100644 index 0000000..1c11afd --- /dev/null +++ b/fsfw/serialize/SerialBufferAdapter.cpp @@ -0,0 +1,129 @@ +#include "../serialize/SerialBufferAdapter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include + +template +SerialBufferAdapter::SerialBufferAdapter(const uint8_t* buffer, + count_t bufferLength, bool serializeLength) : + serializeLength(serializeLength), + constBuffer(buffer), buffer(nullptr), + bufferLength(bufferLength) {} + +template +SerialBufferAdapter::SerialBufferAdapter(uint8_t* buffer, + count_t bufferLength, bool serializeLength) : + serializeLength(serializeLength), constBuffer(buffer), buffer(buffer), + bufferLength(bufferLength) {} + + +template +SerialBufferAdapter::~SerialBufferAdapter() { +} + +template +ReturnValue_t SerialBufferAdapter::serialize(uint8_t** buffer, + size_t* size, size_t maxSize, Endianness streamEndianness) const { + if (serializeLength) { + ReturnValue_t result = SerializeAdapter::serialize(&bufferLength, + buffer, size, maxSize, streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + + if (*size + bufferLength > maxSize) { + return BUFFER_TOO_SHORT; + } + + if (this->constBuffer != nullptr) { + std::memcpy(*buffer, this->constBuffer, bufferLength); + } + else if (this->buffer != nullptr) { + // This will propably be never reached, constBuffer should always be + // set if non-const buffer is set. + std::memcpy(*buffer, this->buffer, bufferLength); + } + else { + return HasReturnvaluesIF::RETURN_FAILED; + } + *size += bufferLength; + (*buffer) += bufferLength; + return HasReturnvaluesIF::RETURN_OK; + +} + +template +size_t SerialBufferAdapter::getSerializedSize() const { + if (serializeLength) { + return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength); + } else { + return bufferLength; + } +} + +template +ReturnValue_t SerialBufferAdapter::deSerialize(const uint8_t** buffer, + size_t* size, Endianness streamEndianness) { + if (this->buffer == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + + if(serializeLength){ + count_t lengthField = 0; + ReturnValue_t result = SerializeAdapter::deSerialize(&lengthField, + buffer, size, streamEndianness); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if(lengthField > bufferLength) { + return TOO_MANY_ELEMENTS; + } + bufferLength = lengthField; + } + + if (bufferLength <= *size) { + *size -= bufferLength; + std::memcpy(this->buffer, *buffer, bufferLength); + (*buffer) += bufferLength; + return HasReturnvaluesIF::RETURN_OK; + } + else { + return STREAM_TOO_SHORT; + } +} + +template +uint8_t * SerialBufferAdapter::getBuffer() { + if(buffer == nullptr) { + sif::error << "Wrong access function for stored type !" + " Use getConstBuffer()." << std::endl; + return nullptr; + } + return buffer; +} + +template +const uint8_t * SerialBufferAdapter::getConstBuffer() { + if(constBuffer == nullptr) { + sif::error << "SerialBufferAdapter::getConstBuffer:" + " Buffers are unitialized!" << std::endl; + return nullptr; + } + return constBuffer; +} + +template +void SerialBufferAdapter::setBuffer(uint8_t* buffer, + count_t bufferLength) { + this->buffer = buffer; + this->constBuffer = buffer; + this->bufferLength = bufferLength; +} + + +//forward Template declaration for linker +template class SerialBufferAdapter; +template class SerialBufferAdapter; +template class SerialBufferAdapter; +template class SerialBufferAdapter; + diff --git a/fsfw/serialize/SerialBufferAdapter.h b/fsfw/serialize/SerialBufferAdapter.h new file mode 100644 index 0000000..9a89e18 --- /dev/null +++ b/fsfw/serialize/SerialBufferAdapter.h @@ -0,0 +1,78 @@ +#ifndef SERIALBUFFERADAPTER_H_ +#define SERIALBUFFERADAPTER_H_ + +#include "../serialize/SerializeIF.h" +#include "../serialize/SerializeAdapter.h" + +/** + * This adapter provides an interface for SerializeIF to serialize or deserialize + * buffers with no length header but a known size. + * + * Additionally, the buffer length can be serialized too and will be put in + * front of the serialized buffer. + * + * Can be used with SerialLinkedListAdapter by declaring a SerializeElement with + * SerialElement>. + * Right now, the SerialBufferAdapter must always + * be initialized with the buffer and size ! + * + * \ingroup serialize + */ +template +class SerialBufferAdapter: public SerializeIF { +public: + + /** + * Constructor for constant uint8_t buffer. Length field can be serialized optionally. + * Type of length can be supplied as template type. + * @param buffer + * @param bufferLength + * @param serializeLength + */ + SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength, + bool serializeLength = false); + + /** + * Constructor for non-constant uint8_t buffer. + * Length field can be serialized optionally. + * Type of length can be supplied as template type. + * @param buffer + * @param bufferLength + * @param serializeLength Length field will be serialized with size count_t + */ + SerialBufferAdapter(uint8_t* buffer, count_t bufferLength, + bool serializeLength = false); + + virtual ~SerialBufferAdapter(); + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override; + + virtual size_t getSerializedSize() const override; + + /** + * @brief This function deserializes a buffer into the member buffer. + * @details + * If a length field is present, it is ignored, as the size should have + * been set in the constructor. If the size is not known beforehand, + * consider using SerialFixedArrayListAdapter instead. + * @param buffer [out] Resulting buffer + * @param size remaining size to deserialize, should be larger than buffer + * + size field size + * @param bigEndian + * @return + */ + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; + + uint8_t * getBuffer(); + const uint8_t * getConstBuffer(); + void setBuffer(uint8_t* buffer, count_t bufferLength); +private: + bool serializeLength = false; + const uint8_t *constBuffer = nullptr; + uint8_t *buffer = nullptr; + count_t bufferLength = 0; +}; + +#endif /* SERIALBUFFERADAPTER_H_ */ diff --git a/fsfw/serialize/SerialFixedArrayListAdapter.h b/fsfw/serialize/SerialFixedArrayListAdapter.h new file mode 100644 index 0000000..70ca987 --- /dev/null +++ b/fsfw/serialize/SerialFixedArrayListAdapter.h @@ -0,0 +1,57 @@ +#ifndef FSFW_SERIALIZE_SERIALFIXEDARRAYLISTADAPTER_H_ +#define FSFW_SERIALIZE_SERIALFIXEDARRAYLISTADAPTER_H_ + +#include "SerialArrayListAdapter.h" +#include "../container/FixedArrayList.h" + +/** + * @brief This adapter provides an interface for SerializeIF to serialize and + * deserialize buffers with a header containing the buffer length. + * @details + * Can be used by SerialLinkedListAdapter by declaring + * as a linked element with SerializeElement>. + * The sequence of objects is defined in the constructor by + * using the setStart and setNext functions. + * + * @tparam BUFFER_TYPE: Specifies the data type of the buffer + * @tparam MAX_SIZE: Specifies the maximum allowed number of elements + * (not bytes!) + * @tparam count_t: specifies the type/size of the length field which defaults + * to one byte. + * @ingroup serialize + */ +template +class SerialFixedArrayListAdapter : + public FixedArrayList, + public SerializeIF { +public: + /** + * Constructor arguments are forwarded to FixedArrayList constructor. + * Refer to the fixed array list constructors for different options. + * @param args + */ + template + SerialFixedArrayListAdapter(Args... args) : + FixedArrayList( + std::forward(args)...){} + + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + return SerialArrayListAdapter::serialize(this, + buffer, size, maxSize, streamEndianness); + } + + size_t getSerializedSize() const { + return SerialArrayListAdapter:: + getSerializedSize(this); + } + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + return SerialArrayListAdapter::deSerialize(this, + buffer, size, streamEndianness); + } + +}; + +#endif /* FSFW_SERIALIZE_SERIALFIXEDARRAYLISTADAPTER_H_ */ diff --git a/fsfw/serialize/SerialLinkedListAdapter.h b/fsfw/serialize/SerialLinkedListAdapter.h new file mode 100644 index 0000000..430a21a --- /dev/null +++ b/fsfw/serialize/SerialLinkedListAdapter.h @@ -0,0 +1,128 @@ +#ifndef FSFW_SERIALIZE_SERIALLINKEDLISTADAPTER_H_ +#define FSFW_SERIALIZE_SERIALLINKEDLISTADAPTER_H_ + +#include "../container/SinglyLinkedList.h" +#include "SerializeAdapter.h" +#include "SerializeElement.h" +#include "SerializeIF.h" + + /** + * @brief Implement the conversion of object data to data streams + * or vice-versa, using linked lists. + * @details + * An alternative to the AutoSerializeAdapter functions + * - All object members with a datatype are declared as + * SerializeElement members inside the class + * implementing this adapter. + * - The element type can also be a SerialBufferAdapter to + * de-/serialize buffers. + * - The element type can also be a SerialFixedArrayListAdapter to + * de-/serialize buffers with a size header, which is scanned automatically. + * + * The sequence of objects is defined in the constructor by using + * the setStart and setNext functions. + * + * 1. The serialization process is done by instantiating the class and + * calling serialize after all SerializeElement entries have been set by + * using the constructor or setter functions. An additional size variable + * can be supplied which is calculated/incremented automatically. + * 2. The deserialization process is done by instantiating the class and + * supplying a buffer with the data which is converted into an object. + * The size of data to serialize can be supplied and is + * decremented in the function. Range checking is done internally. + * @author baetz + * @ingroup serialize + */ +template +class SerialLinkedListAdapter: public SinglyLinkedList, public SerializeIF { +public: + + SerialLinkedListAdapter(typename LinkedElement::Iterator start, + bool printCount = false) : + SinglyLinkedList(start), printCount(printCount) { + } + + SerialLinkedListAdapter(LinkedElement* first, bool printCount = false) : + SinglyLinkedList(first), printCount(printCount) { + + } + + SerialLinkedListAdapter(bool printCount = false) : + SinglyLinkedList(), printCount(printCount) { + } + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const override { + if (printCount) { + count_t mySize = SinglyLinkedList::getSize(); + ReturnValue_t result = SerializeAdapter::serialize(&mySize, + buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + return serialize(SinglyLinkedList::start, buffer, size, maxSize, + streamEndianness); + } + + static ReturnValue_t serialize(const LinkedElement* element, + uint8_t** buffer, size_t* size, size_t maxSize, + Endianness streamEndianness) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + while ((result == HasReturnvaluesIF::RETURN_OK) and (element != nullptr)) { + result = element->value->serialize(buffer, size, maxSize, + streamEndianness); + element = element->getNext(); + } + return result; + } + + virtual size_t getSerializedSize() const override { + if (printCount) { + return SerialLinkedListAdapter::getSerializedSize() + + sizeof(count_t); + } else { + return getSerializedSize(SinglyLinkedList::start); + } + } + + static size_t getSerializedSize(const LinkedElement *element) { + size_t size = 0; + while (element != nullptr) { + size += element->value->getSerializedSize(); + element = element->getNext(); + } + return size; + } + + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override { + return deSerialize(SinglyLinkedList::start, buffer, size, + streamEndianness); + } + + static ReturnValue_t deSerialize(LinkedElement* element, + const uint8_t** buffer, size_t* size, Endianness streamEndianness) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + while ((result == HasReturnvaluesIF::RETURN_OK) and (element != nullptr)) { + result = element->value->deSerialize(buffer, size, streamEndianness); + element = element->getNext(); + } + return result; + } + + /** + * Copying is forbidden by deleting the copy constructor and the copy + * assignment operator because of the pointers to the linked list members. + * Unless the child class implements an own copy constructor or + * copy assignment operator, these operation will throw a compiler error. + * @param + */ + SerialLinkedListAdapter(const SerialLinkedListAdapter &) = delete; + SerialLinkedListAdapter& operator=(const SerialLinkedListAdapter&) = delete; + + bool printCount; +}; + +#endif /* FSFW_SERIALIZE_SERIALLINKEDLISTADAPTER_H_ */ diff --git a/fsfw/serialize/SerializeAdapter.h b/fsfw/serialize/SerializeAdapter.h new file mode 100644 index 0000000..e6cd247 --- /dev/null +++ b/fsfw/serialize/SerializeAdapter.h @@ -0,0 +1,193 @@ +#ifndef _FSFW_SERIALIZE_SERIALIZEADAPTER_H_ +#define _FSFW_SERIALIZE_SERIALIZEADAPTER_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "EndianConverter.h" +#include "SerializeIF.h" +#include +#include + + /** + * @brief These adapters provides an interface to use the SerializeIF functions + * with arbitrary template objects to facilitate and simplify the + * serialization of classes with different multiple different data types + * into buffers and vice-versa. + * @details + * The correct serialization or deserialization function is chosen at + * compile time with template type deduction. + * + * @ingroup serialize + */ +class SerializeAdapter { +public: + /*** + * This function can be used to serialize a trivial copy-able type or a + * child of SerializeIF. + * The right template to be called is determined in the function itself. + * For objects of non trivial copy-able type this function is almost never + * called by the user directly. Instead helpers for specific types like + * SerialArrayListAdapter or SerialLinkedListAdapter is the right choice here. + * + * @param[in] object Object to serialize, the used type is deduced from this pointer + * @param[in/out] buffer Buffer to serialize into. Will be moved by the function. + * @param[in/out] size Size of current written buffer. Will be incremented by the function. + * @param[in] maxSize Max size of Buffer + * @param[in] streamEndianness Endianness of serialized element as in according to SerializeIF::Endianness + * @return + * - @c BUFFER_TOO_SHORT The given buffer in is too short + * - @c RETURN_FAILED Generic Error + * - @c RETURN_OK Successful serialization + */ + template + static ReturnValue_t serialize(const T *object, uint8_t **buffer, + size_t *size, size_t maxSize, + SerializeIF::Endianness streamEndianness) { + InternalSerializeAdapter::value> adapter; + return adapter.serialize(object, buffer, size, maxSize, + streamEndianness); + } + /** + * Function to return the serialized size of the object in the pointer. + * May be a trivially copy-able object or a Child of SerializeIF + * + * @param object Pointer to Object + * @return Serialized size of object + */ + template + static size_t getSerializedSize(const T *object){ + InternalSerializeAdapter::value> adapter; + return adapter.getSerializedSize(object); + } + /** + * @brief + * Deserializes a object from a given buffer of given size. + * Object Must be trivially copy-able or a child of SerializeIF. + * + * @details + * Buffer will be moved to the current read location. Size will be decreased by the function. + * + * @param[in/out] buffer Buffer to deSerialize from. Will be moved by the function. + * @param[in/out] size Remaining size of the buffer to read from. Will be decreased by function. + * @param[in] streamEndianness Endianness as in according to SerializeIF::Endianness + * @return + * - @c STREAM_TOO_SHORT The input stream is too short to deSerialize the object + * - @c TOO_MANY_ELEMENTS The buffer has more inputs than expected + * - @c RETURN_FAILED Generic Error + * - @c RETURN_OK Successful deserialization + */ + template + static ReturnValue_t deSerialize(T *object, const uint8_t **buffer, + size_t *size, SerializeIF::Endianness streamEndianness) { + InternalSerializeAdapter::value> adapter; + return adapter.deSerialize(object, buffer, size, streamEndianness); + } +private: + /** + * Internal template to deduce the right function calls at compile time + */ + template class InternalSerializeAdapter; + + /** + * Template to be used if T is not a child of SerializeIF + * + * @tparam T T must be trivially_copyable + */ + template + class InternalSerializeAdapter { + static_assert (std::is_trivially_copyable::value, + "If a type needs to be serialized it must be a child of " + "SerializeIF or trivially copy-able"); + public: + static ReturnValue_t serialize(const T *object, uint8_t **buffer, + size_t *size, size_t max_size, + SerializeIF::Endianness streamEndianness) { + size_t ignoredSize = 0; + if (size == nullptr) { + size = &ignoredSize; + } + // Check remaining size is large enough and check integer + // overflow of *size + size_t newSize = sizeof(T) + *size; + if ((newSize <= max_size) and (newSize > *size)) { + T tmp; + switch (streamEndianness) { + case SerializeIF::Endianness::BIG: + tmp = EndianConverter::convertBigEndian(*object); + break; + case SerializeIF::Endianness::LITTLE: + tmp = EndianConverter::convertLittleEndian(*object); + break; + default: + case SerializeIF::Endianness::MACHINE: + tmp = *object; + break; + } + std::memcpy(*buffer, &tmp, sizeof(T)); + *size += sizeof(T); + (*buffer) += sizeof(T); + return HasReturnvaluesIF::RETURN_OK; + } else { + return SerializeIF::BUFFER_TOO_SHORT; + } + } + + ReturnValue_t deSerialize(T *object, const uint8_t **buffer, + size_t *size, SerializeIF::Endianness streamEndianness) { + T tmp; + if (*size >= sizeof(T)) { + *size -= sizeof(T); + std::memcpy(&tmp, *buffer, sizeof(T)); + switch (streamEndianness) { + case SerializeIF::Endianness::BIG: + *object = EndianConverter::convertBigEndian(tmp); + break; + case SerializeIF::Endianness::LITTLE: + *object = EndianConverter::convertLittleEndian(tmp); + break; + default: + case SerializeIF::Endianness::MACHINE: + *object = tmp; + break; + } + + *buffer += sizeof(T); + return HasReturnvaluesIF::RETURN_OK; + } else { + return SerializeIF::STREAM_TOO_SHORT; + } + } + + uint32_t getSerializedSize(const T *object) { + return sizeof(T); + } + }; + + /** + * Template for objects that inherit from SerializeIF + * + * @tparam T A child of SerializeIF + */ + template + class InternalSerializeAdapter { + public: + ReturnValue_t serialize(const T *object, uint8_t **buffer, size_t *size, + size_t max_size, + SerializeIF::Endianness streamEndianness) const { + size_t ignoredSize = 0; + if (size == nullptr) { + size = &ignoredSize; + } + return object->serialize(buffer, size, max_size, streamEndianness); + } + size_t getSerializedSize(const T *object) const { + return object->getSerializedSize(); + } + + ReturnValue_t deSerialize(T *object, const uint8_t **buffer, + size_t *size, SerializeIF::Endianness streamEndianness) { + return object->deSerialize(buffer, size, streamEndianness); + } + }; +}; + +#endif /* _FSFW_SERIALIZE_SERIALIZEADAPTER_H_ */ diff --git a/fsfw/serialize/SerializeElement.h b/fsfw/serialize/SerializeElement.h new file mode 100644 index 0000000..4708029 --- /dev/null +++ b/fsfw/serialize/SerializeElement.h @@ -0,0 +1,63 @@ +#ifndef FSFW_SERIALIZE_SERIALIZEELEMENT_H_ +#define FSFW_SERIALIZE_SERIALIZEELEMENT_H_ + +#include "SerializeAdapter.h" +#include "../container/SinglyLinkedList.h" +#include + +/** + * @brief This class is used to mark datatypes for serialization with the + * SerialLinkedListAdapter + * @details + * Used by declaring any arbitrary datatype with SerializeElement myVariable, + * inside a SerialLinkedListAdapter implementation and setting the sequence + * of objects with setNext() and setStart(). + * Serialization and Deserialization is then performed automatically in + * specified sequence order. + * @ingroup serialize + */ +template +class SerializeElement: public SerializeIF, public LinkedElement { +public: + template + SerializeElement(Args ... args) : + LinkedElement(this), entry(std::forward(args)...) { + + } + SerializeElement() : + LinkedElement(this) { + } + + ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, + Endianness streamEndianness) const override { + return SerializeAdapter::serialize(&entry, buffer, size, maxSize, + streamEndianness); + } + + size_t getSerializedSize() const override { + return SerializeAdapter::getSerializedSize(&entry); + } + + virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) override { + return SerializeAdapter::deSerialize(&entry, buffer, size, + streamEndianness); + } + + operator T() { + return entry; + } + + SerializeElement& operator=(T newValue) { + entry = newValue; + return *this; + } + + T* operator->() { + return &entry; + } + + T entry; +}; + +#endif /* FSFW_SERIALIZE_SERIALIZEELEMENT_H_ */ diff --git a/fsfw/serialize/SerializeIF.h b/fsfw/serialize/SerializeIF.h new file mode 100644 index 0000000..d72218f --- /dev/null +++ b/fsfw/serialize/SerializeIF.h @@ -0,0 +1,87 @@ +#ifndef FSFW_SERIALIZE_SERIALIZEIF_H_ +#define FSFW_SERIALIZE_SERIALIZEIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +/** + * @defgroup serialize Serialization + * Contains serialization services. + */ + +/** + * @brief Translation of objects into data streams and from data streams. + * @details + * Also provides options to convert from/to data with different endianness. + * variables. + * @ingroup serialize + */ +class SerializeIF { +public: + enum class Endianness : uint8_t { + BIG, LITTLE, MACHINE + }; + + static const uint8_t INTERFACE_ID = CLASS_ID::SERIALIZE_IF; + static const ReturnValue_t BUFFER_TOO_SHORT = MAKE_RETURN_CODE(1); // !< The given buffer in serialize is too short + static const ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(2); // !< The input stream in deserialize is too short + static const ReturnValue_t TOO_MANY_ELEMENTS = MAKE_RETURN_CODE(3);// !< There are too many elements to be deserialized + + virtual ~SerializeIF() { + } + /** + * @brief + * Function to serialize the object into a buffer with maxSize. Size represents the written amount. + * If a part of the buffer has been used already, size must be set to the used amount of bytes. + * + * @details + * Implementations of this function must increase the size variable and move the buffer pointer. + * MaxSize must be checked by implementations of this function + * and BUFFER_TOO_SHORT has to be returned if size would be larger than maxSize. + * + * Custom implementations might use additional return values. + * + * @param[in/out] buffer Buffer to serialize into, will be set to the current write location + * @param[in/out] size Size that has been used in the buffer already, will be increased by the function + * @param[in] maxSize The size of the buffer that is allowed to be used for serialize. + * @param[in] streamEndianness Endianness of the serialized data according to SerializeIF::Endianness + * @return + * - @c BUFFER_TOO_SHORT The given buffer in is too short + * - @c RETURN_FAILED Generic error + * - @c RETURN_OK Successful serialization + */ + virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const = 0; + + /** + * Gets the size of a object if it would be serialized in a buffer + * @return Size of serialized object + */ + virtual size_t getSerializedSize() const = 0; + + /** + * @brief + * Deserializes a object from a given buffer of given size. + * + * @details + * Buffer must be moved to the current read location by the implementation + * of this function. Size must be decreased by the implementation. + * Implementations are not allowed to alter the buffer as indicated by const pointer. + * + * Custom implementations might use additional return values. + * + * @param[in/out] buffer Buffer to deSerialize from. Will be moved by the function. + * @param[in/out] size Remaining size of the buffer to read from. Will be decreased by function. + * @param[in] streamEndianness Endianness as in according to SerializeIF::Endianness + * @return + * - @c STREAM_TOO_SHORT The input stream is too short to deSerialize the object + * - @c TOO_MANY_ELEMENTS The buffer has more inputs than expected + * - @c RETURN_FAILED Generic Error + * - @c RETURN_OK Successful deserialization + */ + virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) = 0; + +}; + +#endif /* FSFW_SERIALIZE_SERIALIZEIF_H_ */ diff --git a/fsfw/serviceinterface/ServiceInterfaceBuffer.cpp b/fsfw/serviceinterface/ServiceInterfaceBuffer.cpp new file mode 100644 index 0000000..68fc4de --- /dev/null +++ b/fsfw/serviceinterface/ServiceInterfaceBuffer.cpp @@ -0,0 +1,217 @@ +#include "../timemanager/Clock.h" +#include "ServiceInterfaceBuffer.h" +#include +#include + +// to be implemented by bsp +extern "C" void printChar(const char*, bool errStream); + +#ifndef UT699 + +ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage, + bool addCrToPreamble, bool buffered , bool errStream, uint16_t port): + isActive(true), logMessage(setMessage), + addCrToPreamble(addCrToPreamble), buffered(buffered), + errStream(errStream) { + if(buffered) { + // Set pointers if the stream is buffered. + setp( buf, buf + BUF_SIZE ); + } + preamble.reserve(MAX_PREAMBLE_SIZE); + preamble.resize(MAX_PREAMBLE_SIZE); +} + +void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) { + char array[BUF_SIZE]; + uint32_t length = end - begin; + if (length > sizeof(array)) { + length = sizeof(array); + } + memcpy(array, begin, length); + + for(; begin != end; begin++){ + if(errStream) { + printChar(begin, true); + } + else { + printChar(begin, false); + } + } +} + +#endif + +int ServiceInterfaceBuffer::overflow(int c) { + if(not buffered and this->isActive) { + if (c != Traits::eof()) { + if(errStream) { + printChar(reinterpret_cast(&c), true); + } + else { + printChar(reinterpret_cast(&c), false); + } + } + return 0; + } + // Handle output + putChars(pbase(), pptr()); + if (c != Traits::eof()) { + char c2 = c; + // Handle the one character that didn't fit to buffer + putChars(&c2, &c2 + 1); + } + // This tells that buffer is empty again + setp(buf, buf + BUF_SIZE - 1); + // I'm not sure about this return value! + return 0; +} + +int ServiceInterfaceBuffer::sync(void) { + if(not this->isActive and not buffered) { + if(not buffered) { + setp(buf, buf + BUF_SIZE - 1); + } + return 0; + } + if(not buffered) { + return 0; + } + + size_t preambleSize = 0; + std::string* preamble = getPreamble(&preambleSize); + // Write logMessage and time + this->putChars(preamble->data(), preamble->data() + preambleSize); + // Handle output + this->putChars(pbase(), pptr()); + // This tells that buffer is empty again + setp(buf, buf + BUF_SIZE - 1); + return 0; +} + +bool ServiceInterfaceBuffer::isBuffered() const { + return buffered; +} + +std::string* ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) { + Clock::TimeOfDay_t loggerTime; + Clock::getDateAndTime(&loggerTime); + size_t currentSize = 0; + char* parsePosition = &preamble[0]; + if(addCrToPreamble) { + preamble[0] = '\r'; + currentSize += 1; + parsePosition += 1; + } + int32_t charCount = sprintf(parsePosition, + "%s: | %02" SCNu32 ":%02" SCNu32 ":%02" SCNu32 ".%03" SCNu32 " | ", + this->logMessage.c_str(), loggerTime.hour, + loggerTime.minute, + loggerTime.second, + loggerTime.usecond /1000); + if(charCount < 0) { + printf("ServiceInterfaceBuffer: Failure parsing preamble\r\n"); + return &preamble; + } + if(charCount > MAX_PREAMBLE_SIZE) { + printf("ServiceInterfaceBuffer: Char count too large for maximum " + "preamble size"); + return &preamble; + } + currentSize += charCount; + if(preambleSize != nullptr) { + *preambleSize = currentSize; + } + return &preamble; +} + + + +#ifdef UT699 +#include "../osal/rtems/Interrupt.h" + +ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, + uint16_t port) { + this->log_message = set_message; + this->isActive = true; + setp( buf, buf + BUF_SIZE ); +} + +void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) { + char array[BUF_SIZE]; + uint32_t length = end - begin; + if (length > sizeof(array)) { + length = sizeof(array); + } + memcpy(array, begin, length); + + if (!Interrupt::isInterruptInProgress()) { + std::cout << array; + } else { + //Uncomment the following line if you need ISR debug output. +// printk(array); + } +} +#endif //UT699 + +#ifdef ML505 +#include +ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, + uint16_t port) : + isActive(true), log_message(set_message), udpSocket(0), remoteAddressLength( + sizeof(remoteAddress)) { + setp(buf, buf + BUF_SIZE); + memset((uint8_t*) &remoteAddress, 0, sizeof(remoteAddress)); + remoteAddress.sin_family = AF_INET; + remoteAddress.sin_port = htons(port); + remoteAddress.sin_addr.s_addr = htonl(inet_addr("192.168.250.100")); +} + +void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) { + + char array[BUF_SIZE]; + uint32_t length = end - begin; + if (length > sizeof(array)) { + length = sizeof(array); + } + memcpy(array, begin, length); + + if (udpSocket <= 0) { + initSocket(); + } + + if (udpSocket > 0) { + sendto(udpSocket, array, length, 0, (sockaddr*) &remoteAddress, + sizeof(remoteAddress)); + } + +} + +void ServiceInterfaceBuffer::initSocket() { + sockaddr_in address; + memset((uint8_t*) &address, 0, sizeof(address)); + address.sin_family = AF_INET; + address.sin_port = htons(0); + address.sin_addr.s_addr = htonl(INADDR_ANY); + + udpSocket = socket(PF_INET, SOCK_DGRAM, 0); + if (socket < 0) { + printf("Error opening socket!\n"); + return; + } + timeval timeout = { 0, 20 }; + if (setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, + sizeof(timeout)) < 0) { + printf("Error setting SO_RCVTIMEO socket options!\n"); + return; + } + if (setsockopt(udpSocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, + sizeof(timeout)) < 0) { + printf("Error setting SO_SNDTIMEO socket options!\n"); + return; + } + if (bind(udpSocket, (sockaddr *) &address, sizeof(address)) < 0) { + printf("Error binding socket!\n"); + } +} + +#endif //ML505 diff --git a/fsfw/serviceinterface/ServiceInterfaceBuffer.h b/fsfw/serviceinterface/ServiceInterfaceBuffer.h new file mode 100644 index 0000000..c5d5b25 --- /dev/null +++ b/fsfw/serviceinterface/ServiceInterfaceBuffer.h @@ -0,0 +1,145 @@ +#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ +#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include +#include +#include + +#ifndef UT699 + +/** + * @brief This is the underlying stream buffer which implements the + * streambuf class and overloads the overflow() and sync() methods + * @details + * This class is used to modify the output of the stream, for example by adding. + * It also calls the char printing function which is implemented in the + * board supply package (BSP). + */ +class ServiceInterfaceBuffer: + public std::streambuf { + friend class ServiceInterfaceStream; +public: + static constexpr uint8_t MAX_PREAMBLE_SIZE = 40; + + ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble, + bool buffered, bool errStream, uint16_t port); + +protected: + bool isActive; + //! This is called when buffer becomes full. If + //! buffer is not used, then this is called every + //! time when characters are put to stream. + int overflow(int c = Traits::eof()) override; + + //! This function is called when stream is flushed, + //! for example when std::endl is put to stream. + int sync(void) override; + + bool isBuffered() const; +private: + //! For additional message information + std::string logMessage; + std::string preamble; + // For EOF detection + typedef std::char_traits Traits; + + //! This is useful for some terminal programs which do not have + //! implicit carriage return with newline characters. + bool addCrToPreamble; + + //! Specifies whether the stream operates in buffered or unbuffered mode. + bool buffered; + //! This specifies to print to stderr and work in unbuffered mode. + bool errStream; + + //! Needed for buffered mode. + static size_t const BUF_SIZE = 128; + char buf[BUF_SIZE]; + + //! In this function, the characters are parsed. + void putChars(char const* begin, char const* end); + + std::string* getPreamble(size_t * preambleSize = nullptr); +}; + +#endif + + +#ifdef UT699 +class ServiceInterfaceBuffer: public std::basic_streambuf > { + friend class ServiceInterfaceStream; +public: + ServiceInterfaceBuffer(std::string set_message, uint16_t port); +protected: + bool isActive; + // This is called when buffer becomes full. If + // buffer is not used, then this is called every + // time when characters are put to stream. + virtual int overflow(int c = Traits::eof()); + + // This function is called when stream is flushed, + // for example when std::endl is put to stream. + virtual int sync(void); + +private: + // For additional message information + std::string log_message; + // For EOF detection + typedef std::char_traits Traits; + + // Work in buffer mode. It is also possible to work without buffer. + static size_t const BUF_SIZE = 128; + char buf[BUF_SIZE]; + + // In this function, the characters are parsed. + void putChars(char const* begin, char const* end); +}; +#endif //UT699 + +#ifdef ML505 +#include +#include +#include +#include +#include + +class ServiceInterfaceBuffer: public std::basic_streambuf > { + friend class ServiceInterfaceStream; +public: + ServiceInterfaceBuffer(std::string set_message, uint16_t port); +protected: + bool isActive; + // This is called when buffer becomes full. If + // buffer is not used, then this is called every + // time when characters are put to stream. + virtual int overflow(int c = Traits::eof()); + + // This function is called when stream is flushed, + // for example when std::endl is put to stream. + virtual int sync(void); + +private: + // For additional message information + std::string log_message; + // For EOF detection + typedef std::char_traits Traits; + + // Work in buffer mode. It is also possible to work without buffer. + static size_t const BUF_SIZE = 128; + char buf[BUF_SIZE]; + + // In this function, the characters are parsed. + void putChars(char const* begin, char const* end); + + int udpSocket; + sockaddr_in remoteAddress; + socklen_t remoteAddressLength; + void initSocket(); +}; +#endif //ML505 + + +#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ */ diff --git a/fsfw/serviceinterface/ServiceInterfaceStream.cpp b/fsfw/serviceinterface/ServiceInterfaceStream.cpp new file mode 100644 index 0000000..5b7b9f0 --- /dev/null +++ b/fsfw/serviceinterface/ServiceInterfaceStream.cpp @@ -0,0 +1,32 @@ +#include "ServiceInterfaceStream.h" + +ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage, + bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) : + std::ostream(&streambuf), + streambuf(setMessage, addCrToPreamble, buffered, errStream, port) {} + +void ServiceInterfaceStream::setActive( bool myActive) { + this->streambuf.isActive = myActive; +} + +std::string* ServiceInterfaceStream::getPreamble() { + return streambuf.getPreamble(); +} + +void ServiceInterfaceStream::print(std::string error, + bool withPreamble, bool withNewline, bool flush) { + if(not streambuf.isBuffered() and withPreamble) { + *this << getPreamble() << error; + } + else { + *this << error; + } + + if(withNewline) { + *this << "\n"; + } + // if mode is non-buffered, no need to flush. + if(flush and streambuf.isBuffered()) { + this->flush(); + } +} diff --git a/fsfw/serviceinterface/ServiceInterfaceStream.h b/fsfw/serviceinterface/ServiceInterfaceStream.h new file mode 100644 index 0000000..76fa1bf --- /dev/null +++ b/fsfw/serviceinterface/ServiceInterfaceStream.h @@ -0,0 +1,58 @@ +#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ +#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ + +#include "ServiceInterfaceBuffer.h" +#include +#include + +/** + * Generic service interface stream which can be used like std::cout or + * std::cerr but has additional capability. Add preamble and timestamp + * to output. Can be run in buffered or unbuffered mode. + */ +class ServiceInterfaceStream : public std::ostream { +public: + /** + * This constructor is used by specifying the preamble message. + * Optionally, the output can be directed to stderr and a CR character + * can be prepended to the preamble. + * @param setMessage message of preamble. + * @param addCrToPreamble Useful for applications like Puttty. + * @param buffered specify whether to use buffered mode. + * @param errStream specify which output stream to use (stderr or stdout). + */ + ServiceInterfaceStream(std::string setMessage, + bool addCrToPreamble = false, bool buffered = true, + bool errStream = false, uint16_t port = 1234); + + //! An inactive stream will not print anything. + void setActive( bool ); + + /** + * This can be used to retrieve the preamble in case it should be printed in + * the unbuffered mode. + * @return Preamle consisting of log message and timestamp. + */ + std::string* getPreamble(); + + /** + * This prints an error with a preamble. Useful if using the unbuffered + * mode. Flushes in default mode (prints immediately). + */ + void print(std::string error, bool withPreamble = true, + bool withNewline = true, bool flush = true); + +protected: + ServiceInterfaceBuffer streambuf; +}; + +// Forward declaration of interface streams. These should be instantiated in +// main. They can then be used like std::cout or std::cerr. +namespace sif { +extern ServiceInterfaceStream debug; +extern ServiceInterfaceStream info; +extern ServiceInterfaceStream warning; +extern ServiceInterfaceStream error; +} + +#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */ diff --git a/fsfw/storagemanager/ConstStorageAccessor.cpp b/fsfw/storagemanager/ConstStorageAccessor.cpp new file mode 100644 index 0000000..842f1ce --- /dev/null +++ b/fsfw/storagemanager/ConstStorageAccessor.cpp @@ -0,0 +1,88 @@ +#include "ConstStorageAccessor.h" +#include "StorageManagerIF.h" + +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../globalfunctions/arrayprinter.h" + +ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId): + storeId(storeId) {} + +ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId, + StorageManagerIF* store): + storeId(storeId), store(store) { + internalState = AccessState::ASSIGNED; +} + +ConstStorageAccessor::~ConstStorageAccessor() { + if(deleteData and store != nullptr) { + store->deleteData(storeId); + } +} + +ConstStorageAccessor::ConstStorageAccessor(ConstStorageAccessor&& other): + constDataPointer(other.constDataPointer), storeId(other.storeId), + size_(other.size_), store(other.store), deleteData(other.deleteData), + internalState(other.internalState) { + // This prevent premature deletion + other.store = nullptr; +} + +ConstStorageAccessor& ConstStorageAccessor::operator=( + ConstStorageAccessor&& other) { + constDataPointer = other.constDataPointer; + storeId = other.storeId; + store = other.store; + size_ = other.size_; + deleteData = other.deleteData; + this->store = other.store; + // This prevents premature deletion + other.store = nullptr; + return *this; +} + +const uint8_t* ConstStorageAccessor::data() const { + return constDataPointer; +} + +size_t ConstStorageAccessor::size() const { + if(internalState == AccessState::UNINIT) { + sif::warning << "StorageAccessor: Not initialized!" << std::endl; + } + return size_; +} + +ReturnValue_t ConstStorageAccessor::getDataCopy(uint8_t *pointer, + size_t maxSize) { + if(internalState == AccessState::UNINIT) { + sif::warning << "StorageAccessor: Not initialized!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + if(size_ > maxSize) { + sif::error << "StorageAccessor: Supplied buffer not large enough" + << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + std::copy(constDataPointer, constDataPointer + size_, pointer); + return HasReturnvaluesIF::RETURN_OK; +} + +void ConstStorageAccessor::release() { + deleteData = false; +} + +store_address_t ConstStorageAccessor::getId() const { + return storeId; +} + +void ConstStorageAccessor::print() const { + if(internalState == AccessState::UNINIT or constDataPointer == nullptr) { + sif::warning << "StorageAccessor: Not initialized!" << std::endl; + return; + } + arrayprinter::print(constDataPointer, size_); +} + +void ConstStorageAccessor::assignStore(StorageManagerIF* store) { + internalState = AccessState::ASSIGNED; + this->store = store; +} diff --git a/fsfw/storagemanager/ConstStorageAccessor.h b/fsfw/storagemanager/ConstStorageAccessor.h new file mode 100644 index 0000000..96d2dca --- /dev/null +++ b/fsfw/storagemanager/ConstStorageAccessor.h @@ -0,0 +1,116 @@ +#ifndef FSFW_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ +#define FSFW_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ + +#include "storeAddress.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +class StorageManagerIF; + +/** + * @brief Helper classes to facilitate safe access to storages which is also + * conforming to RAII principles + * @details + * Accessor class which can be returned by pool manager or passed and set by + * pool managers to have safe access to the pool resources. + * + * These helper can be used together with the StorageManager classes to manage + * access to a storage. It can take care of thread-safety while also providing + * mechanisms to automatically clear storage data. + */ +class ConstStorageAccessor { + //! StorageManager classes have exclusive access to private variables. + template + friend class PoolManager; + template + friend class LocalPool; +public: + /** + * @brief Simple constructor which takes the store ID of the storage + * entry to access. + * @param storeId + */ + ConstStorageAccessor(store_address_t storeId); + ConstStorageAccessor(store_address_t storeId, StorageManagerIF* store); + + /** + * @brief The destructor in default configuration takes care of + * deleting the accessed pool entry and unlocking the mutex + */ + virtual ~ConstStorageAccessor(); + + /** + * @brief Returns a pointer to the read-only data + * @return + */ + const uint8_t* data() const; + + /** + * @brief Copies the read-only data to the supplied pointer + * @param pointer + */ + virtual ReturnValue_t getDataCopy(uint8_t *pointer, size_t maxSize); + + /** + * @brief Calling this will prevent the Accessor from deleting the data + * when the destructor is called. + */ + void release(); + + /** + * Get the size of the data + * @return + */ + size_t size() const; + + /** + * Get the storage ID. + * @return + */ + store_address_t getId() const; + + void print() const; + + /** + * @brief Move ctor and move assignment allow returning accessors as + * a returnvalue. They prevent resource being free prematurely. + * Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/ + * move-constructors-and-move-assignment-operators-cpp.md + * @param + * @return + */ + ConstStorageAccessor& operator= (ConstStorageAccessor&&); + ConstStorageAccessor(ConstStorageAccessor&&); + + //! The copy ctor and copy assignemnt should be deleted implicitely + //! according to https://foonathan.net/2019/02/special-member-functions/ + //! but I still deleted them to make it more explicit. (remember rule of 5). + ConstStorageAccessor& operator=(const ConstStorageAccessor&) = delete; + ConstStorageAccessor(const ConstStorageAccessor&) = delete; +protected: + const uint8_t* constDataPointer = nullptr; + store_address_t storeId; + size_t size_ = 0; + //! Managing pool, has to assign itself. + StorageManagerIF* store = nullptr; + bool deleteData = true; + + enum class AccessState { + UNINIT, + ASSIGNED + }; + //! Internal state for safety reasons. + AccessState internalState = AccessState::UNINIT; + /** + * Used by the pool manager instances to assign themselves to the + * accessor. This is necessary to delete the data when the acessor + * exits the scope ! The internal state will be considered read + * when this function is called, so take care all data is set properly as + * well. + * @param + */ + void assignStore(StorageManagerIF*); +}; + + +#endif /* FSFW_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ */ diff --git a/fsfw/storagemanager/LocalPool.h b/fsfw/storagemanager/LocalPool.h new file mode 100644 index 0000000..3a94c03 --- /dev/null +++ b/fsfw/storagemanager/LocalPool.h @@ -0,0 +1,190 @@ +#ifndef FSFW_STORAGEMANAGER_LOCALPOOL_H_ +#define FSFW_STORAGEMANAGER_LOCALPOOL_H_ + +#include "StorageManagerIF.h" +#include "../objectmanager/SystemObject.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../internalError/InternalErrorReporterIF.h" +#include "../storagemanager/StorageAccessor.h" +#include + + +/** + * @brief The LocalPool class provides an intermediate data storage with + * a fixed pool size policy. + * @details The class implements the StorageManagerIF interface. While the + * total number of pools is fixed, the element sizes in one pool and + * the number of pool elements per pool are set on construction. + * The full amount of memory is allocated on construction. + * The overhead is 4 byte per pool element to store the size + * information of each stored element. + * To maintain an "empty" information, the pool size is limited to + * 0xFFFF-1 bytes. + * It is possible to store empty packets in the pool. + * The local pool is NOT thread-safe. + * @author Bastian Baetz + */ +template +class LocalPool: public SystemObject, public StorageManagerIF { +public: + /** + * @brief This definition generally sets the number of different sized pools. + * @details This must be less than the maximum number of pools (currently 0xff). + */ + // static const uint32_t NUMBER_OF_POOLS; + /** + * @brief This is the default constructor for a pool manager instance. + * @details By passing two arrays of size NUMBER_OF_POOLS, the constructor + * allocates memory (with @c new) for store and size_list. These + * regions are all set to zero on start up. + * @param setObjectId The object identifier to be set. This allows for + * multiple instances of LocalPool in the system. + * @param element_sizes An array of size NUMBER_OF_POOLS in which the size + * of a single element in each pool is determined. + * The sizes must be provided in ascending order. + * + * @param n_elements An array of size NUMBER_OF_POOLS in which the + * number of elements for each pool is determined. + * The position of these values correspond to those in + * element_sizes. + * @param registered Register the pool in object manager or not. + * Default is false (local pool). + * @param spillsToHigherPools A variable to determine whether + * higher n pools are used if the store is full. + */ + LocalPool(object_id_t setObjectId, + const uint16_t element_sizes[NUMBER_OF_POOLS], + const uint16_t n_elements[NUMBER_OF_POOLS], + bool registered = false, + bool spillsToHigherPools = false); + /** + * @brief In the LocalPool's destructor all allocated memory is freed. + */ + virtual ~LocalPool(void); + + /** + * Documentation: See StorageManagerIF.h + */ + ReturnValue_t addData(store_address_t* storageId, const uint8_t * data, + size_t size, bool ignoreFault = false) override; + ReturnValue_t getFreeElement(store_address_t* storageId,const size_t size, + uint8_t** p_data, bool ignoreFault = false) override; + + ConstAccessorPair getData(store_address_t packet_id) override; + ReturnValue_t getData(store_address_t packet_id, ConstStorageAccessor&) override; + ReturnValue_t getData(store_address_t packet_id, const uint8_t** packet_ptr, + size_t * size) override; + + AccessorPair modifyData(store_address_t packet_id) override; + ReturnValue_t modifyData(store_address_t packet_id, StorageAccessor&) override; + ReturnValue_t modifyData(store_address_t packet_id, uint8_t** packet_ptr, + size_t * size) override; + + virtual ReturnValue_t deleteData(store_address_t) override; + virtual ReturnValue_t deleteData(uint8_t* ptr, size_t size, + store_address_t* storeId = NULL) override; + void clearStore() override; + ReturnValue_t initialize() override; +protected: + /** + * With this helper method, a free element of @c size is reserved. + * @param size The minimum packet size that shall be reserved. + * @param[out] address Storage ID of the reserved data. + * @return - #RETURN_OK on success, + * - the return codes of #getPoolIndex or #findEmpty otherwise. + */ + virtual ReturnValue_t reserveSpace(const uint32_t size, + store_address_t* address, bool ignoreFault); + + InternalErrorReporterIF *internalErrorReporter; +private: + /** + * Indicates that this element is free. + * This value limits the maximum size of a pool. Change to larger data type + * if increase is required. + */ + static const uint32_t STORAGE_FREE = 0xFFFFFFFF; + /** + * @brief In this array, the element sizes of each pool is stored. + * @details The sizes are maintained for internal pool management. The sizes + * must be set in ascending order on construction. + */ + uint32_t element_sizes[NUMBER_OF_POOLS]; + /** + * @brief n_elements stores the number of elements per pool. + * @details These numbers are maintained for internal pool management. + */ + uint16_t n_elements[NUMBER_OF_POOLS]; + /** + * @brief store represents the actual memory pool. + * @details It is an array of pointers to memory, which was allocated with + * a @c new call on construction. + */ + uint8_t* store[NUMBER_OF_POOLS]; + /** + * @brief The size_list attribute stores the size values of every pool element. + * @details As the number of elements is determined on construction, the size list + * is also dynamically allocated there. + */ + uint32_t* size_list[NUMBER_OF_POOLS]; + //! A variable to determine whether higher n pools are used if + //! the store is full. + bool spillsToHigherPools; + /** + * @brief This method safely stores the given data in the given packet_id. + * @details It also sets the size in size_list. The method does not perform + * any range checks, these are done in advance. + * @param packet_id The storage identifier in which the data shall be stored. + * @param data The data to be stored. + * @param size The size of the data to be stored. + */ + void write(store_address_t packet_id, const uint8_t* data, size_t size); + /** + * @brief A helper method to read the element size of a certain pool. + * @param pool_index The pool in which to look. + * @return Returns the size of an element or 0. + */ + uint32_t getPageSize(uint16_t pool_index); + /** + * @brief This helper method looks up a fitting pool for a given size. + * @details The pools are looked up in ascending order, so the first that + * fits is used. + * @param packet_size The size of the data to be stored. + * @return Returns the pool that fits or StorageManagerIF::INVALID_ADDRESS. + */ + /** + * @brief This helper method looks up a fitting pool for a given size. + * @details The pools are looked up in ascending order, so the first that + * fits is used. + * @param packet_size The size of the data to be stored. + * @param[out] poolIndex The fitting pool index found. + * @return - #RETURN_OK on success, + * - #DATA_TOO_LARGE otherwise. + */ + ReturnValue_t getPoolIndex(size_t packet_size, uint16_t* poolIndex, + uint16_t startAtIndex = 0); + /** + * @brief This helper method calculates the true array position in store + * of a given packet id. + * @details The method does not perform any range checks, these are done in + * advance. + * @param packet_id The packet id to look up. + * @return Returns the position of the data in store. + */ + uint32_t getRawPosition(store_address_t packet_id); + /** + * @brief This is a helper method to find an empty element in a given pool. + * @details The method searches size_list for the first empty element, so + * duration grows with the fill level of the pool. + * @param pool_index The pool in which the search is performed. + * @param[out] element The first found element in the pool. + * @return - #RETURN_OK on success, + * - #DATA_STORAGE_FULL if the store is full + */ + ReturnValue_t findEmpty(uint16_t pool_index, uint16_t* element); +}; + +#include "LocalPool.tpp" + +#endif /* FSFW_STORAGEMANAGER_LOCALPOOL_H_ */ diff --git a/fsfw/storagemanager/LocalPool.tpp b/fsfw/storagemanager/LocalPool.tpp new file mode 100644 index 0000000..5e61efe --- /dev/null +++ b/fsfw/storagemanager/LocalPool.tpp @@ -0,0 +1,305 @@ +#ifndef FSFW_STORAGEMANAGER_LOCALPOOL_TPP_ +#define FSFW_STORAGEMANAGER_LOCALPOOL_TPP_ + +#ifndef FSFW_STORAGEMANAGER_LOCALPOOL_H_ +#error Include LocalPool.h before LocalPool.tpp! +#endif + +template +inline LocalPool::LocalPool(object_id_t setObjectId, + const uint16_t element_sizes[NUMBER_OF_POOLS], + const uint16_t n_elements[NUMBER_OF_POOLS], bool registered, + bool spillsToHigherPools) : + SystemObject(setObjectId, registered), internalErrorReporter(nullptr), + spillsToHigherPools(spillsToHigherPools) +{ + for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) { + this->element_sizes[n] = element_sizes[n]; + this->n_elements[n] = n_elements[n]; + store[n] = new uint8_t[n_elements[n] * element_sizes[n]]; + size_list[n] = new uint32_t[n_elements[n]]; + memset(store[n], 0x00, (n_elements[n] * element_sizes[n])); + //TODO checkme + memset(size_list[n], STORAGE_FREE, (n_elements[n] * sizeof(**size_list))); + } +} + + +template +inline ReturnValue_t LocalPool::findEmpty(uint16_t pool_index, + uint16_t* element) { + ReturnValue_t status = DATA_STORAGE_FULL; + for (uint16_t foundElement = 0; foundElement < n_elements[pool_index]; + foundElement++) { + if (size_list[pool_index][foundElement] == STORAGE_FREE) { + *element = foundElement; + status = RETURN_OK; + break; + } + } + return status; +} + +template +inline void LocalPool::write(store_address_t packet_id, + const uint8_t* data, size_t size) { + uint8_t* ptr; + uint32_t packet_position = getRawPosition(packet_id); + + //check size? -> Not necessary, because size is checked before calling this function. + ptr = &store[packet_id.pool_index][packet_position]; + memcpy(ptr, data, size); + size_list[packet_id.pool_index][packet_id.packet_index] = size; +} + +//Returns page size of 0 in case store_index is illegal +template +inline uint32_t LocalPool::getPageSize(uint16_t pool_index) { + if (pool_index < NUMBER_OF_POOLS) { + return element_sizes[pool_index]; + } else { + return 0; + } +} + +template +inline ReturnValue_t LocalPool::getPoolIndex( + size_t packet_size, uint16_t* poolIndex, uint16_t startAtIndex) { + for (uint16_t n = startAtIndex; n < NUMBER_OF_POOLS; n++) { + //debug << "LocalPool " << getObjectId() << "::getPoolIndex: Pool: " << + // n << ", Element Size: " << element_sizes[n] << std::endl; + if (element_sizes[n] >= packet_size) { + *poolIndex = n; + return RETURN_OK; + } + } + return DATA_TOO_LARGE; +} + +template +inline uint32_t LocalPool::getRawPosition( + store_address_t packet_id) { + return packet_id.packet_index * element_sizes[packet_id.pool_index]; +} + +template +inline ReturnValue_t LocalPool::reserveSpace( + const uint32_t size, store_address_t* address, bool ignoreFault) { + ReturnValue_t status = getPoolIndex(size, &address->pool_index); + if (status != RETURN_OK) { + sif::error << "LocalPool( " << std::hex << getObjectId() << std::dec + << " )::reserveSpace: Packet too large." << std::endl; + return status; + } + status = findEmpty(address->pool_index, &address->packet_index); + while (status != RETURN_OK && spillsToHigherPools) { + status = getPoolIndex(size, &address->pool_index, address->pool_index + 1); + if (status != RETURN_OK) { + //We don't find any fitting pool anymore. + break; + } + status = findEmpty(address->pool_index, &address->packet_index); + } + if (status == RETURN_OK) { + // if (getObjectId() == objects::IPC_STORE && address->pool_index >= 3) { + // debug << "Reserve: Pool: " << std::dec << address->pool_index << + // " Index: " << address->packet_index << std::endl; + // } + + size_list[address->pool_index][address->packet_index] = size; + } else { + if (!ignoreFault and internalErrorReporter != nullptr) { + internalErrorReporter->storeFull(); + } + // error << "LocalPool( " << std::hex << getObjectId() << std::dec + // << " )::reserveSpace: Packet store is full." << std::endl; + } + return status; +} + +template +inline LocalPool::~LocalPool(void) { + for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) { + delete[] store[n]; + delete[] size_list[n]; + } +} + +template +inline ReturnValue_t LocalPool::addData( + store_address_t* storageId, const uint8_t* data, size_t size, + bool ignoreFault) { + ReturnValue_t status = reserveSpace(size, storageId, ignoreFault); + if (status == RETURN_OK) { + write(*storageId, data, size); + } + return status; +} + +template +inline ReturnValue_t LocalPool::getFreeElement( + store_address_t* storageId, const size_t size, + uint8_t** p_data, bool ignoreFault) { + ReturnValue_t status = reserveSpace(size, storageId, ignoreFault); + if (status == RETURN_OK) { + *p_data = &store[storageId->pool_index][getRawPosition(*storageId)]; + } else { + *p_data = NULL; + } + return status; +} + +template +inline ConstAccessorPair LocalPool::getData( + store_address_t storeId) { + uint8_t* tempData = nullptr; + ConstStorageAccessor constAccessor(storeId, this); + ReturnValue_t status = modifyData(storeId, &tempData, &constAccessor.size_); + constAccessor.constDataPointer = tempData; + return ConstAccessorPair(status, std::move(constAccessor)); +} + +template +inline ReturnValue_t LocalPool::getData(store_address_t storeId, + ConstStorageAccessor& storeAccessor) { + uint8_t* tempData = nullptr; + ReturnValue_t status = modifyData(storeId, &tempData, &storeAccessor.size_); + storeAccessor.assignStore(this); + storeAccessor.constDataPointer = tempData; + return status; +} + +template +inline ReturnValue_t LocalPool::getData( + store_address_t packet_id, const uint8_t** packet_ptr, size_t* size) { + uint8_t* tempData = nullptr; + ReturnValue_t status = modifyData(packet_id, &tempData, size); + *packet_ptr = tempData; + return status; +} + +template +inline AccessorPair LocalPool::modifyData( + store_address_t storeId) { + StorageAccessor accessor(storeId, this); + ReturnValue_t status = modifyData(storeId, &accessor.dataPointer, + &accessor.size_); + accessor.assignConstPointer(); + return AccessorPair(status, std::move(accessor)); +} + +template +inline ReturnValue_t LocalPool::modifyData( + store_address_t storeId, StorageAccessor& storeAccessor) { + storeAccessor.assignStore(this); + ReturnValue_t status = modifyData(storeId, &storeAccessor.dataPointer, + &storeAccessor.size_); + storeAccessor.assignConstPointer(); + return status; +} + +template +inline ReturnValue_t LocalPool::modifyData( + store_address_t packet_id, uint8_t** packet_ptr, size_t* size) { + ReturnValue_t status = RETURN_FAILED; + if (packet_id.pool_index >= NUMBER_OF_POOLS) { + return ILLEGAL_STORAGE_ID; + } + if ((packet_id.packet_index >= n_elements[packet_id.pool_index])) { + return ILLEGAL_STORAGE_ID; + } + if (size_list[packet_id.pool_index][packet_id.packet_index] + != STORAGE_FREE) { + uint32_t packet_position = getRawPosition(packet_id); + *packet_ptr = &store[packet_id.pool_index][packet_position]; + *size = size_list[packet_id.pool_index][packet_id.packet_index]; + status = RETURN_OK; + } else { + status = DATA_DOES_NOT_EXIST; + } + return status; +} + +template +inline ReturnValue_t LocalPool::deleteData( + store_address_t packet_id) { + //if (getObjectId() == objects::IPC_STORE && packet_id.pool_index >= 3) { + // debug << "Delete: Pool: " << std::dec << packet_id.pool_index << " Index: " + // << packet_id.packet_index << std::endl; + //} + ReturnValue_t status = RETURN_OK; + uint32_t page_size = getPageSize(packet_id.pool_index); + if ((page_size != 0) + && (packet_id.packet_index < n_elements[packet_id.pool_index])) { + uint16_t packet_position = getRawPosition(packet_id); + uint8_t* ptr = &store[packet_id.pool_index][packet_position]; + memset(ptr, 0, page_size); + //Set free list + size_list[packet_id.pool_index][packet_id.packet_index] = STORAGE_FREE; + } else { + //pool_index or packet_index is too large + sif::error << "LocalPool:deleteData failed." << std::endl; + status = ILLEGAL_STORAGE_ID; + } + return status; +} + +template +inline void LocalPool::clearStore() { + for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) { + //TODO checkme + memset(size_list[n], STORAGE_FREE, (n_elements[n] * sizeof(**size_list))); + } +} + +template +inline ReturnValue_t LocalPool::deleteData(uint8_t* ptr, + size_t size, store_address_t* storeId) { + store_address_t localId; + ReturnValue_t result = ILLEGAL_ADDRESS; + for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) { + //Not sure if new allocates all stores in order. so better be careful. + if ((store[n] <= ptr) && (&store[n][n_elements[n]*element_sizes[n]]) > ptr) { + localId.pool_index = n; + uint32_t deltaAddress = ptr - store[n]; + // Getting any data from the right "block" is ok. + // This is necessary, as IF's sometimes don't point to the first + // element of an object. + localId.packet_index = deltaAddress / element_sizes[n]; + result = deleteData(localId); + //if (deltaAddress % element_sizes[n] != 0) { + // error << "Pool::deleteData: address not aligned!" << std::endl; + //} + break; + } + } + if (storeId != NULL) { + *storeId = localId; + } + return result; +} + +template +inline ReturnValue_t LocalPool::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != RETURN_OK) { + return result; + } + internalErrorReporter = objectManager->get( + objects::INTERNAL_ERROR_REPORTER); + if (internalErrorReporter == nullptr){ + return ObjectManagerIF::INTERNAL_ERR_REPORTER_UNINIT; + } + + //Check if any pool size is large than the maximum allowed. + for (uint8_t count = 0; count < NUMBER_OF_POOLS; count++) { + if (element_sizes[count] >= STORAGE_FREE) { + sif::error << "LocalPool::initialize: Pool is too large! " + "Max. allowed size is: " << (STORAGE_FREE - 1) << std::endl; + return StorageManagerIF::POOL_TOO_LARGE; + } + } + return RETURN_OK; +} + +#endif /* FSFW_STORAGEMANAGER_LOCALPOOL_TPP_ */ diff --git a/fsfw/storagemanager/PoolManager.h b/fsfw/storagemanager/PoolManager.h new file mode 100644 index 0000000..8cc6c06 --- /dev/null +++ b/fsfw/storagemanager/PoolManager.h @@ -0,0 +1,56 @@ +#ifndef FSFW_STORAGEMANAGER_POOLMANAGER_H_ +#define FSFW_STORAGEMANAGER_POOLMANAGER_H_ + +#include "LocalPool.h" +#include "StorageAccessor.h" +#include "../ipc/MutexHelper.h" + + +/** + * @brief The PoolManager class provides an intermediate data storage with + * a fixed pool size policy for inter-process communication. + * @details Uses local pool calls but is thread safe by protecting the call + * with a lock. + * @author Bastian Baetz + */ +template +class PoolManager : public LocalPool { +public: + PoolManager(object_id_t setObjectId, + const uint16_t element_sizes[NUMBER_OF_POOLS], + const uint16_t n_elements[NUMBER_OF_POOLS]); + + /** + * @brief In the PoolManager's destructor all allocated memory + * is freed. + */ + virtual ~PoolManager(); + + /** + * @brief LocalPool overrides for thread-safety. Decorator function + * which wraps LocalPool calls with a mutex protection. + */ + ReturnValue_t deleteData(store_address_t) override; + ReturnValue_t deleteData(uint8_t* buffer, size_t size, + store_address_t* storeId = nullptr) override; + + void setMutexTimeout(uint32_t mutexTimeoutMs); +protected: + //! Default mutex timeout value to prevent permanent blocking. + uint32_t mutexTimeoutMs = 20; + + ReturnValue_t reserveSpace(const uint32_t size, store_address_t* address, + bool ignoreFault) override; + + /** + * @brief The mutex is created in the constructor and makes + * access mutual exclusive. + * @details Locking and unlocking is done during searching for free slots + * and deleting existing slots. + */ + MutexIF* mutex; +}; + +#include "PoolManager.tpp" + +#endif /* FSFW_STORAGEMANAGER_POOLMANAGER_H_ */ diff --git a/fsfw/storagemanager/PoolManager.tpp b/fsfw/storagemanager/PoolManager.tpp new file mode 100644 index 0000000..2be44ec --- /dev/null +++ b/fsfw/storagemanager/PoolManager.tpp @@ -0,0 +1,56 @@ +#ifndef FRAMEWORK_STORAGEMANAGER_POOLMANAGER_TPP_ +#define FRAMEWORK_STORAGEMANAGER_POOLMANAGER_TPP_ + +#ifndef FSFW_STORAGEMANAGER_POOLMANAGER_H_ +#error Include PoolManager.h before PoolManager.tpp! +#endif + +template +inline PoolManager::PoolManager(object_id_t setObjectId, + const uint16_t element_sizes[NUMBER_OF_POOLS], + const uint16_t n_elements[NUMBER_OF_POOLS]) : + LocalPool(setObjectId, element_sizes, n_elements, true) { + mutex = MutexFactory::instance()->createMutex(); +} + +template +inline PoolManager::~PoolManager(void) { + MutexFactory::instance()->deleteMutex(mutex); +} + +template +inline ReturnValue_t PoolManager::reserveSpace( + const uint32_t size, store_address_t* address, bool ignoreFault) { + MutexHelper mutexHelper(mutex,MutexIF::WAITING, mutexTimeoutMs); + ReturnValue_t status = LocalPool::reserveSpace(size, + address,ignoreFault); + return status; +} + +template +inline ReturnValue_t PoolManager::deleteData( + store_address_t packet_id) { + // debug << "PoolManager( " << translateObject(getObjectId()) << + // " )::deleteData from store " << packet_id.pool_index << + // ". id is "<< packet_id.packet_index << std::endl; + MutexHelper mutexHelper(mutex,MutexIF::WAITING, mutexTimeoutMs); + ReturnValue_t status = LocalPool::deleteData(packet_id); + return status; +} + +template +inline ReturnValue_t PoolManager::deleteData(uint8_t* buffer, + size_t size, store_address_t* storeId) { + MutexHelper mutexHelper(mutex,MutexIF::WAITING, mutexTimeoutMs); + ReturnValue_t status = LocalPool::deleteData(buffer, + size, storeId); + return status; +} + +template +inline void PoolManager::setMutexTimeout( + uint32_t mutexTimeoutMs) { + this->mutexTimeout = mutexTimeoutMs; +} + +#endif /* FRAMEWORK_STORAGEMANAGER_POOLMANAGER_TPP_ */ diff --git a/fsfw/storagemanager/StorageAccessor.cpp b/fsfw/storagemanager/StorageAccessor.cpp new file mode 100644 index 0000000..9c2f936 --- /dev/null +++ b/fsfw/storagemanager/StorageAccessor.cpp @@ -0,0 +1,67 @@ +#include "StorageAccessor.h" +#include "StorageManagerIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +StorageAccessor::StorageAccessor(store_address_t storeId): + ConstStorageAccessor(storeId) { +} + +StorageAccessor::StorageAccessor(store_address_t storeId, + StorageManagerIF* store): + ConstStorageAccessor(storeId, store) { +} + +StorageAccessor& StorageAccessor::operator =( + StorageAccessor&& other) { + // Call the parent move assignment and also assign own member. + dataPointer = other.dataPointer; + StorageAccessor::operator=(std::move(other)); + return * this; +} + +// Call the parent move ctor and also transfer own member. +StorageAccessor::StorageAccessor(StorageAccessor&& other): + ConstStorageAccessor(std::move(other)), dataPointer(other.dataPointer) { +} + +ReturnValue_t StorageAccessor::getDataCopy(uint8_t *pointer, size_t maxSize) { + if(internalState == AccessState::UNINIT) { + sif::warning << "StorageAccessor: Not initialized!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + if(size_ > maxSize) { + sif::error << "StorageAccessor: Supplied buffer not large " + "enough" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + std::copy(dataPointer, dataPointer + size_, pointer); + return HasReturnvaluesIF::RETURN_OK; +} + +uint8_t* StorageAccessor::data() { + if(internalState == AccessState::UNINIT) { + sif::warning << "StorageAccessor: Not initialized!" << std::endl; + } + return dataPointer; +} + +ReturnValue_t StorageAccessor::write(uint8_t *data, size_t size, + uint16_t offset) { + if(internalState == AccessState::UNINIT) { + sif::warning << "StorageAccessor: Not initialized!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + if(offset + size > size_) { + sif::error << "StorageAccessor: Data too large for pool " + "entry!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + std::copy(data, data + size, dataPointer + offset); + return HasReturnvaluesIF::RETURN_OK; +} + +void StorageAccessor::assignConstPointer() { + constDataPointer = dataPointer; +} + + diff --git a/fsfw/storagemanager/StorageAccessor.h b/fsfw/storagemanager/StorageAccessor.h new file mode 100644 index 0000000..5cf15d5 --- /dev/null +++ b/fsfw/storagemanager/StorageAccessor.h @@ -0,0 +1,45 @@ +#ifndef FSFW_STORAGEMANAGER_STORAGEACCESSOR_H_ +#define FSFW_STORAGEMANAGER_STORAGEACCESSOR_H_ + +#include "ConstStorageAccessor.h" + +class StorageManagerIF; + +/** + * @brief Child class for modifyable data. Also has a normal pointer member. + */ +class StorageAccessor: public ConstStorageAccessor { + //! StorageManager classes have exclusive access to private variables. + template + friend class PoolManager; + template + friend class LocalPool; +public: + StorageAccessor(store_address_t storeId); + StorageAccessor(store_address_t storeId, StorageManagerIF* store); + + /** + * @brief Move ctor and move assignment allow returning accessors as + * a returnvalue. They prevent resource being freed prematurely. + * See: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/ + * move-constructors-and-move-assignment-operators-cpp.md + * @param + * @return + */ + StorageAccessor& operator=(StorageAccessor&&); + StorageAccessor(StorageAccessor&&); + + ReturnValue_t write(uint8_t *data, size_t size, + uint16_t offset = 0); + uint8_t* data(); + ReturnValue_t getDataCopy(uint8_t *pointer, size_t maxSize) override; + +private: + //! Non-const pointer for modifyable data. + uint8_t* dataPointer = nullptr; + //! For modifyable data, the const pointer is assigned to the normal + //! pointer by the pool manager so both access functions can be used safely + void assignConstPointer(); +}; + +#endif /* FSFW_STORAGEMANAGER_STORAGEACCESSOR_H_ */ diff --git a/fsfw/storagemanager/StorageManagerIF.h b/fsfw/storagemanager/StorageManagerIF.h new file mode 100644 index 0000000..834e756 --- /dev/null +++ b/fsfw/storagemanager/StorageManagerIF.h @@ -0,0 +1,169 @@ +#ifndef FSFW_STORAGEMANAGER_STORAGEMANAGERIF_H_ +#define FSFW_STORAGEMANAGER_STORAGEMANAGERIF_H_ + +#include "StorageAccessor.h" +#include "storeAddress.h" + +#include "../events/Event.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +#include +#include + +using AccessorPair = std::pair; +using ConstAccessorPair = std::pair; + +/** + * @brief This class provides an interface for intermediate data storage. + * @details The Storage manager classes shall be used to store larger chunks of + * data in RAM for exchange between tasks. This interface expects the + * data to be stored in one consecutive block of memory, so tasks can + * write directly to the destination pointer. + * For interprocess communication, the stores must be locked during + * insertion and deletion. If the receiving storage identifier is + * passed token-like between tasks, a lock during read and write + * operations is not necessary. + * @author Bastian Baetz + * @date 18.09.2012 + */ +class StorageManagerIF : public HasReturnvaluesIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::STORAGE_MANAGER_IF; //!< The unique ID for return codes for this interface. + static const ReturnValue_t DATA_TOO_LARGE = MAKE_RETURN_CODE(1); //!< This return code indicates that the data to be stored is too large for the store. + static const ReturnValue_t DATA_STORAGE_FULL = MAKE_RETURN_CODE(2); //!< This return code indicates that a data storage is full. + static const ReturnValue_t ILLEGAL_STORAGE_ID = MAKE_RETURN_CODE(3); //!< This return code indicates that data was requested with an illegal storage ID. + static const ReturnValue_t DATA_DOES_NOT_EXIST = MAKE_RETURN_CODE(4); //!< This return code indicates that the requested ID was valid, but no data is stored there. + static const ReturnValue_t ILLEGAL_ADDRESS = MAKE_RETURN_CODE(5); + static const ReturnValue_t POOL_TOO_LARGE = MAKE_RETURN_CODE(6); //!< Pool size too large on initialization. + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::OBSW; + static const Event GET_DATA_FAILED = MAKE_EVENT(0, SEVERITY::LOW); + static const Event STORE_DATA_FAILED = MAKE_EVENT(1, SEVERITY::LOW); + + static const uint32_t INVALID_ADDRESS = 0xFFFFFFFF; //!< Indicates an invalid (i.e unused) storage address. + /** + * @brief This is the empty virtual destructor as required for C++ interfaces. + */ + virtual ~StorageManagerIF() { + } + ; + /** + * @brief With addData, a free storage position is allocated and data + * stored there. + * @details During the allocation, the StorageManager is blocked. + * @param storageId A pointer to the storageId to retrieve. + * @param data The data to be stored in the StorageManager. + * @param size The amount of data to be stored. + * @return Returns @li RETURN_OK if data was added. + * @li RETURN_FAILED if data could not be added. + * storageId is unchanged then. + */ + virtual ReturnValue_t addData(store_address_t* storageId, + const uint8_t * data, size_t size, bool ignoreFault = false) = 0; + /** + * @brief With deleteData, the storageManager frees the memory region + * identified by packet_id. + * @param packet_id The identifier of the memory region to be freed. + * @return @li RETURN_OK on success. + * @li RETURN_FAILED if deletion did not work + * (e.g. an illegal packet_id was passed). + */ + virtual ReturnValue_t deleteData(store_address_t packet_id) = 0; + /** + * @brief Another deleteData which uses the pointer and size of the + * stored data to delete the content. + * @param buffer Pointer to the data. + * @param size Size of data to be stored. + * @param storeId Store id of the deleted element (optional) + * @return @li RETURN_OK on success. + * @li failure code if deletion did not work + */ + virtual ReturnValue_t deleteData(uint8_t* buffer, size_t size, + store_address_t* storeId = nullptr) = 0; + + + /** + * @brief Access the data by supplying a store ID. + * @details + * A pair consisting of the retrieval result and an instance of a + * ConstStorageAccessor class is returned + * @param storeId + * @return Pair of return value and a ConstStorageAccessor instance + */ + virtual ConstAccessorPair getData(store_address_t storeId) = 0; + + /** + * @brief Access the data by supplying a store ID and a helper + * instance + * @param storeId + * @param constAccessor Wrapper function to access store data. + * @return + */ + virtual ReturnValue_t getData(store_address_t storeId, + ConstStorageAccessor& constAccessor) = 0; + + + /** + * @brief getData returns an address to data and the size of the data + * for a given packet_id. + * @param packet_id The id of the data to be returned + * @param packet_ptr The passed pointer address is set to the the memory + * position + * @param size The exact size of the stored data is returned here. + * @return @li RETURN_OK on success. + * @li RETURN_FAILED if fetching data did not work + * (e.g. an illegal packet_id was passed). + */ + virtual ReturnValue_t getData(store_address_t packet_id, + const uint8_t** packet_ptr, size_t* size) = 0; + + + /** + * Modify data by supplying a store ID + * @param storeId + * @return Pair of return value and StorageAccessor helper + */ + virtual AccessorPair modifyData(store_address_t storeId) = 0; + + /** + * Modify data by supplying a store ID and a StorageAccessor helper instance. + * @param storeId + * @param accessor Helper class to access the modifiable data. + * @return + */ + virtual ReturnValue_t modifyData(store_address_t storeId, + StorageAccessor& accessor) = 0; + + /** + * Get pointer and size of modifiable data by supplying the storeId + * @param packet_id + * @param packet_ptr [out] Pointer to pointer of data to set + * @param size [out] Pointer to size to set + * @return + */ + virtual ReturnValue_t modifyData(store_address_t packet_id, + uint8_t** packet_ptr, size_t* size) = 0; + /** + * This method reserves an element of \c size. + * + * It returns the packet id of this element as well as a direct pointer to the + * data of the element. It must be assured that exactly \c size data is + * written to p_data! + * @param storageId A pointer to the storageId to retrieve. + * @param size The size of the space to be reserved. + * @param p_data A pointer to the element data is returned here. + * @return Returns @li RETURN_OK if data was added. + * @li RETURN_FAILED if data could not be added. + * storageId is unchanged then. + */ + virtual ReturnValue_t getFreeElement(store_address_t* storageId, + const size_t size, uint8_t** p_data, bool ignoreFault = false ) = 0; + + /** + * Clears the whole store. + * Use with care! + */ + virtual void clearStore() = 0; +}; + +#endif /* FSFW_STORAGEMANAGER_STORAGEMANAGERIF_H_ */ diff --git a/fsfw/storagemanager/storeAddress.h b/fsfw/storagemanager/storeAddress.h new file mode 100644 index 0000000..044c079 --- /dev/null +++ b/fsfw/storagemanager/storeAddress.h @@ -0,0 +1,55 @@ +#ifndef FSFW_STORAGEMANAGER_STOREADDRESS_H_ +#define FSFW_STORAGEMANAGER_STOREADDRESS_H_ + +#include + +/** + * This union defines the type that identifies where a data packet is + * stored in the store. It comprises of a raw part to read it as raw value and + * a structured part to use it in pool-like stores. + */ +union store_address_t { + /** + * Default Constructor, initializing to INVALID_ADDRESS + */ + store_address_t():raw(0xFFFFFFFF){} + /** + * Constructor to create an address object using the raw address + * + * @param rawAddress + */ + store_address_t(uint32_t rawAddress):raw(rawAddress){} + + /** + * Constructor to create an address object using pool + * and packet indices + * + * @param poolIndex + * @param packetIndex + */ + store_address_t(uint16_t poolIndex, uint16_t packetIndex): + pool_index(poolIndex),packet_index(packetIndex){} + /** + * A structure with two elements to access the store address pool-like. + */ + struct { + /** + * The index in which pool the packet lies. + */ + uint16_t pool_index; + /** + * The position in the chosen pool. + */ + uint16_t packet_index; + }; + /** + * Alternative access to the raw value. + */ + uint32_t raw; + + bool operator==(const store_address_t& other) const { + return raw == other.raw; + } +}; + +#endif /* FSFW_STORAGEMANAGER_STOREADDRESS_H_ */ diff --git a/fsfw/subsystem/Subsystem.cpp b/fsfw/subsystem/Subsystem.cpp new file mode 100644 index 0000000..56d47da --- /dev/null +++ b/fsfw/subsystem/Subsystem.cpp @@ -0,0 +1,663 @@ +#include "../health/HealthMessage.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../serialize/SerialArrayListAdapter.h" +#include "../serialize/SerialFixedArrayListAdapter.h" +#include "../serialize/SerializeElement.h" +#include "../serialize/SerialLinkedListAdapter.h" +#include "Subsystem.h" +#include + +Subsystem::Subsystem(object_id_t setObjectId, object_id_t parent, + uint32_t maxNumberOfSequences, uint32_t maxNumberOfTables) : + SubsystemBase(setObjectId, parent, 0), isInTransition(false), childrenChangedHealth( + false), uptimeStartTable(0), currentTargetTable(), targetMode( + 0), targetSubmode(SUBMODE_NONE), initialMode(0), currentSequenceIterator(), modeTables( + maxNumberOfTables), modeSequences(maxNumberOfSequences), IPCStore( + NULL) +#ifdef USE_MODESTORE +,modeStore(NULL) +#endif +{ + +} + +Subsystem::~Subsystem() { + //Auto-generated destructor stub +} + +ReturnValue_t Subsystem::checkSequence(HybridIterator iter, + Mode_t fallbackSequence) { + + //only check for existence, checking the fallback would lead to a (possibly infinite) recursion. + //the fallback sequence will be checked when it is needed. + if (!existsModeSequence(fallbackSequence)) { + return FALLBACK_SEQUENCE_DOES_NOT_EXIST; + } + + if (iter.value == NULL) { + return NO_TARGET_TABLE; + } + + for (; iter.value != NULL; ++iter) { + if (!existsModeTable(iter->getTableId())) { + return TABLE_DOES_NOT_EXIST; + } else { + ReturnValue_t result = checkTable(getTable(iter->getTableId())); + if (result != RETURN_OK) { + return result; + } + } + } + return RETURN_OK; +} + +ReturnValue_t Subsystem::checkSequence(Mode_t sequence) { + if (!existsModeSequence(sequence)) { + return SEQUENCE_DOES_NOT_EXIST; + } + HybridIterator iter = getSequence(sequence); + return checkSequence(iter, getFallbackSequence(sequence)); +} + +bool Subsystem::existsModeSequence(Mode_t id) { + return modeSequences.exists(id) == RETURN_OK; +} + +bool Subsystem::existsModeTable(Mode_t id) { + return modeTables.exists(id) == RETURN_OK; +} + +HybridIterator Subsystem::getCurrentTable() { + return getTable(currentSequenceIterator->getTableId()); +} + +void Subsystem::performChildOperation() { + if (isInTransition) { + if (commandsOutstanding <= 0) { //all children of the current table were commanded and replied + if (currentSequenceIterator.value == NULL) { //we're through with this sequence + if (checkStateAgainstTable(currentTargetTable, targetSubmode) + == RETURN_OK) { + setMode(targetMode, targetSubmode); + isInTransition = false; + return; + } else { + transitionFailed(TARGET_TABLE_NOT_REACHED, + getSequence(targetMode)->getTableId()); + return; + } + } + if (currentSequenceIterator->checkSuccess()) { + if (checkStateAgainstTable(getCurrentTable(), targetSubmode) + != RETURN_OK) { + transitionFailed(TABLE_CHECK_FAILED, + currentSequenceIterator->getTableId()); + return; + } + } + if (currentSequenceIterator->getWaitSeconds() != 0) { + if (uptimeStartTable == 0) { + Clock::getUptime(&uptimeStartTable); + return; + } else { + uint32_t uptimeNow; + Clock::getUptime(&uptimeNow); + if ((uptimeNow - uptimeStartTable) + < (currentSequenceIterator->getWaitSeconds() * 1000)) { + return; + } + } + } + uptimeStartTable = 0; + //next Table, but only if there is one + if ((++currentSequenceIterator).value != NULL) { //we're through with this sequence + executeTable(getCurrentTable(), targetSubmode); + } + } + } else { + if (childrenChangedHealth) { + triggerEvent(CHILD_CHANGED_HEALTH, 0, 0); + childrenChangedHealth = false; + startTransition(mode, submode); + } else if (childrenChangedMode) { + if (checkStateAgainstTable(currentTargetTable, submode) + != RETURN_OK) { + triggerEvent(CANT_KEEP_MODE, mode, submode); + cantKeepMode(); + } + } + } +} + +HybridIterator Subsystem::getSequence(Mode_t id) { + SequenceInfo *sequenceInfo = modeSequences.findValue(id); + if (sequenceInfo->entries.islinked) { + return HybridIterator( + sequenceInfo->entries.firstLinkedElement); + } else { + return HybridIterator( + sequenceInfo->entries.array->front(), + sequenceInfo->entries.array->back()); + } +} + +HybridIterator Subsystem::getTable(Mode_t id) { + EntryPointer *entry = modeTables.findValue(id); + if (entry->islinked) { + return HybridIterator(entry->firstLinkedElement); + } else { + return HybridIterator(entry->array->front(), + entry->array->back()); + } +} + +ReturnValue_t Subsystem::handleCommandMessage(CommandMessage *message) { + switch (message->getCommand()) { + case HealthMessage::HEALTH_INFO: { + HealthState health = HealthMessage::getHealth(message); + if (health != EXTERNAL_CONTROL) { + //Ignore external control, as it has an effect only if the mode changes, + //which is communicated with an additional mode info event. + childrenChangedHealth = true; + } + } + break; + case ModeSequenceMessage::ADD_SEQUENCE: { + FixedArrayList sequence; + const uint8_t *pointer; + size_t sizeRead; + ReturnValue_t result = IPCStore->getData( + ModeSequenceMessage::getStoreAddress(message), &pointer, + &sizeRead); + if (result == RETURN_OK) { + Mode_t fallbackId; + size_t size = sizeRead; + result = SerializeAdapter::deSerialize(&fallbackId, &pointer, &size, + SerializeIF::Endianness::BIG); + if (result == RETURN_OK) { + result = SerialArrayListAdapter::deSerialize( + &sequence, &pointer, &size, + SerializeIF::Endianness::BIG); + if (result == RETURN_OK) { + result = addSequence(&sequence, + ModeSequenceMessage::getSequenceId(message), + fallbackId); + } + } + IPCStore->deleteData(ModeSequenceMessage::getStoreAddress(message)); + } + replyToCommand(result, 0); + } + break; + case ModeSequenceMessage::ADD_TABLE: { + FixedArrayList table; + const uint8_t *pointer; + size_t sizeRead; + ReturnValue_t result = IPCStore->getData( + ModeSequenceMessage::getStoreAddress(message), &pointer, + &sizeRead); + if (result == RETURN_OK) { + size_t size = sizeRead; + result = SerialArrayListAdapter::deSerialize(&table, + &pointer, &size, SerializeIF::Endianness::BIG); + if (result == RETURN_OK) { + result = addTable(&table, + ModeSequenceMessage::getSequenceId(message)); + } + IPCStore->deleteData(ModeSequenceMessage::getStoreAddress(message)); + } + replyToCommand(result, 0); + + } + break; + case ModeSequenceMessage::DELETE_SEQUENCE:{ + if (isInTransition) { + replyToCommand(IN_TRANSITION, 0); + break; + } + ReturnValue_t result = deleteSequence(ModeSequenceMessage::getSequenceId(message)); + replyToCommand(result, 0); + } + break; + case ModeSequenceMessage::DELETE_TABLE:{ + if (isInTransition) { + replyToCommand(IN_TRANSITION, 0); + break; + } + ReturnValue_t result = deleteTable(ModeSequenceMessage::getTableId(message)); + replyToCommand(result, 0); + } + break; + case ModeSequenceMessage::LIST_SEQUENCES: { + SerialFixedArrayListAdapter sequences; + FixedMap::Iterator iter; + for (iter = modeSequences.begin(); iter != modeSequences.end(); + ++iter) { + sequences.insert(iter.value->first); + } + SerializeIF *pointer = &sequences; + sendSerializablesAsCommandMessage(ModeSequenceMessage::SEQUENCE_LIST, + &pointer, 1); + } + break; + case ModeSequenceMessage::LIST_TABLES: { + SerialFixedArrayListAdapter tables; + FixedMap::Iterator iter; + for (iter = modeTables.begin(); iter != modeTables.end(); ++iter) { + tables.insert(iter.value->first); + } + SerializeIF *pointer = &tables; + sendSerializablesAsCommandMessage(ModeSequenceMessage::TABLE_LIST, + &pointer, 1); + } + break; + case ModeSequenceMessage::READ_SEQUENCE: { + ReturnValue_t result; + Mode_t sequence = ModeSequenceMessage::getSequenceId(message); + SequenceInfo *sequenceInfo = NULL; + result = modeSequences.find(sequence, &sequenceInfo); + if (result != RETURN_OK) { + replyToCommand(result, 0); + } + + SerializeIF *elements[3]; + SerializeElement sequenceId(sequence); + SerializeElement fallbackSequenceId( + getFallbackSequence(sequence)); + + elements[0] = &sequenceId; + elements[1] = &fallbackSequenceId; + + if (sequenceInfo->entries.islinked) { + SerialLinkedListAdapter list( + sequenceInfo->entries.firstLinkedElement, true); + elements[2] = &list; + sendSerializablesAsCommandMessage(ModeSequenceMessage::SEQUENCE, + elements, 3); + } else { + SerialArrayListAdapter serializableArray( + sequenceInfo->entries.array); + + elements[2] = &serializableArray; + sendSerializablesAsCommandMessage(ModeSequenceMessage::SEQUENCE, + elements, 3); + } + } + break; + case ModeSequenceMessage::READ_TABLE: { + ReturnValue_t result; + Mode_t table = ModeSequenceMessage::getSequenceId(message); + EntryPointer *entry = NULL; + result = modeTables.find(table, &entry); + if (result != RETURN_OK) { + replyToCommand(result, 0); + } + + SerializeIF *elements[2]; + SerializeElement tableId(table); + + elements[0] = &tableId; + + if (entry->islinked) { + SerialLinkedListAdapter list( + entry->firstLinkedElement, true); + elements[1] = &list; + sendSerializablesAsCommandMessage(ModeSequenceMessage::TABLE, + elements, 2); + } else { + SerialArrayListAdapter serializableArray( + entry->array); + elements[1] = &serializableArray; + sendSerializablesAsCommandMessage(ModeSequenceMessage::TABLE, + elements, 2); + } + } + break; + case ModeSequenceMessage::READ_FREE_SEQUENCE_SLOTS: { + uint32_t freeSlots = modeSequences.maxSize() - modeSequences.size(); + CommandMessage reply; + ModeSequenceMessage::setModeSequenceMessage(&reply, + ModeSequenceMessage::FREE_SEQUENCE_SLOTS, freeSlots); + commandQueue->reply(&reply); + } + break; + case ModeSequenceMessage::READ_FREE_TABLE_SLOTS: { + uint32_t free = modeTables.maxSize() - modeTables.size(); + CommandMessage reply; + ModeSequenceMessage::setModeSequenceMessage(&reply, + ModeSequenceMessage::FREE_TABLE_SLOTS, free); + commandQueue->reply(&reply); + } + break; + default: + return RETURN_FAILED; + } + return RETURN_OK; +} + +void Subsystem::replyToCommand(ReturnValue_t status, uint32_t parameter) { + if (status == RETURN_OK) { + CommandMessage reply(CommandMessage::REPLY_COMMAND_OK, 0, 0); + commandQueue->reply(&reply); + } else { + CommandMessage reply(CommandMessage::REPLY_REJECTED, status, 0); + commandQueue->reply(&reply); + } +} + +ReturnValue_t Subsystem::addSequence(ArrayList *sequence, + Mode_t id, Mode_t fallbackSequence, bool inStore, bool preInit) { + + ReturnValue_t result; + + //Before initialize() is called, tables must not be checked as the children are not added yet. + //Sequences added before are checked by initialize() + if (!preInit) { + result = checkSequence( + HybridIterator(sequence->front(), + sequence->back()), fallbackSequence); + if (result != RETURN_OK) { + return result; + } + } + + SequenceInfo info; + + info.fallbackSequence = fallbackSequence; + + info.entries.islinked = inStore; + info.entries.array = sequence; + + result = modeSequences.insert(id, info); + + if (result != RETURN_OK) { + return result; + } + + if (inStore) { +#ifdef USE_MODESTORE + result = modeStore->storeArray(sequence, + &(modeSequences.find(id)->entries.firstLinkedElement)); + if (result != RETURN_OK) { + modeSequences.erase(id); + } +#else + modeSequences.erase(id); + return RETURN_FAILED; +#endif + } + + return result; + +} + +ReturnValue_t Subsystem::addTable(ArrayList *table, Mode_t id, + bool inStore, bool preInit) { + + ReturnValue_t result; + + //Before initialize() is called, tables must not be checked as the children are not added yet. + //Tables added before are checked by initialize() + if (!preInit) { + result = checkTable( + HybridIterator(table->front(), table->back())); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + + EntryPointer pointer; + + pointer.islinked = inStore; + pointer.array = table; + + result = modeTables.insert(id, pointer); + + if (result != RETURN_OK) { + return result; + } + + if (inStore) { +#ifdef USE_MODESTORE + result = modeStore->storeArray(table, + &(modeTables.find(id)->firstLinkedElement)); + if (result != RETURN_OK) { + modeTables.erase(id); + } +#else + modeTables.erase(id); + return RETURN_FAILED; +#endif + } + return result; +} + +ReturnValue_t Subsystem::deleteSequence(Mode_t id) { + + if (isFallbackSequence(id)) { + return IS_FALLBACK_SEQUENCE; + } + + SequenceInfo *sequenceInfo; + ReturnValue_t result; + result = modeSequences.find(id, &sequenceInfo); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (!sequenceInfo->entries.islinked) { + return ACCESS_DENIED; + } + +#ifdef USE_MODESTORE + modeStore->deleteList(sequenceInfo->entries.firstLinkedElement); +#endif + modeSequences.erase(id); + return RETURN_OK; +} + +ReturnValue_t Subsystem::deleteTable(Mode_t id) { + + if (isTableUsed(id)) { + return TABLE_IN_USE; + } + + EntryPointer *pointer; + ReturnValue_t result; + result = modeTables.find(id, &pointer); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + if (!pointer->islinked) { + return ACCESS_DENIED; + } + +#ifdef USE_MODESTORE + modeStore->deleteList(pointer->firstLinkedElement); +#endif + modeSequences.erase(id); + return RETURN_OK; +} + +ReturnValue_t Subsystem::initialize() { + ReturnValue_t result = SubsystemBase::initialize(); + + if (result != RETURN_OK) { + return result; + } + + IPCStore = objectManager->get(objects::IPC_STORE); + if (IPCStore == NULL) { + return RETURN_FAILED; + } + +#ifdef USE_MODESTORE + modeStore = objectManager->get(objects::MODE_STORE); + + if (modeStore == NULL) { + return RETURN_FAILED; + } +#endif + + if ((modeSequences.maxSize() > MAX_NUMBER_OF_TABLES_OR_SEQUENCES) + || (modeTables.maxSize() > MAX_NUMBER_OF_TABLES_OR_SEQUENCES)) { + return TABLE_OR_SEQUENCE_LENGTH_INVALID; + } + + mode = initialMode; + + return RETURN_OK; +} + +MessageQueueId_t Subsystem::getSequenceCommandQueue() const { + return SubsystemBase::getCommandQueue(); +} + +ReturnValue_t Subsystem::checkModeCommand(Mode_t mode, Submode_t submode, + uint32_t *msToReachTheMode) { + //Need to accept all submodes to be able to inherit submodes +// if (submode != SUBMODE_NONE) { +// return INVALID_SUBMODE; +// } + + if (isInTransition && (mode != getFallbackSequence(targetMode))) { + return HasModesIF::IN_TRANSITION; + } else { + return checkSequence(mode); + } +} + +void Subsystem::startTransition(Mode_t sequence, Submode_t submode) { + if (modeHelper.isForced()) { + triggerEvent(FORCING_MODE, sequence, submode); + } else { + triggerEvent(CHANGING_MODE, sequence, submode); + } + targetMode = sequence; + targetSubmode = submode; + isInTransition = true; + commandsOutstanding = 0; + currentSequenceIterator = getSequence(sequence); + + currentTargetTable = getTable(currentSequenceIterator->getTableId()); + + ++currentSequenceIterator; + + if (currentSequenceIterator.value != NULL) { + executeTable(getCurrentTable(), targetSubmode); + } +} + +Mode_t Subsystem::getFallbackSequence(Mode_t sequence) { + for (FixedMap::Iterator iter = modeSequences.begin(); + iter != modeSequences.end(); ++iter) { + if (iter.value->first == sequence) { + return iter->second.fallbackSequence; + } + } + return -1; +} + +bool Subsystem::isFallbackSequence(Mode_t SequenceId) { + for (FixedMap::Iterator iter = modeSequences.begin(); + iter != modeSequences.end(); iter++) { + if (iter->second.fallbackSequence == SequenceId) { + return true; + } + } + return false; +} + +bool Subsystem::isTableUsed(Mode_t tableId) { + for (FixedMap::Iterator sequence = + modeSequences.begin(); sequence != modeSequences.end(); + sequence++) { + HybridIterator sequenceIterator = getSequence( + sequence.value->first); + while (sequenceIterator.value != NULL) { + if (sequenceIterator->getTableId() == tableId) { + return true; + } + ++sequenceIterator; + } + } + return false; +} + +void Subsystem::transitionFailed(ReturnValue_t failureCode, + uint32_t parameter) { + triggerEvent(MODE_TRANSITION_FAILED, failureCode, parameter); + if (mode == targetMode) { + //already tried going back to the current mode + //go into fallback mode, also set current mode to fallback mode, so we come here at the next fail + modeHelper.setForced(true); + ReturnValue_t result; + if ((result = checkSequence(getFallbackSequence(mode))) != RETURN_OK) { + triggerEvent(FALLBACK_FAILED, result, getFallbackSequence(mode)); + isInTransition = false; //keep still and allow arbitrary mode commands to recover + return; + } + mode = getFallbackSequence(mode); + startTransition(mode, submode); + } else { + //try to go back to the current mode + startTransition(mode, submode); + } +} + +void Subsystem::sendSerializablesAsCommandMessage(Command_t command, + SerializeIF **elements, uint8_t count) { + ReturnValue_t result; + size_t maxSize = 0; + for (uint8_t i = 0; i < count; i++) { + maxSize += elements[i]->getSerializedSize(); + } + uint8_t *storeBuffer; + store_address_t address; + size_t size = 0; + + result = IPCStore->getFreeElement(&address, maxSize, &storeBuffer); + if (result != HasReturnvaluesIF::RETURN_OK) { + replyToCommand(result, 0); + return; + } + for (uint8_t i = 0; i < count; i++) { + elements[i]->serialize(&storeBuffer, &size, maxSize, + SerializeIF::Endianness::BIG); + } + CommandMessage reply; + ModeSequenceMessage::setModeSequenceMessage(&reply, command, address); + if (commandQueue->reply(&reply) != RETURN_OK) { + IPCStore->deleteData(address); + } +} + +ReturnValue_t Subsystem::checkObjectConnections() { + ReturnValue_t result = RETURN_OK; + for (FixedMap::Iterator iter = modeSequences.begin(); + iter != modeSequences.end(); iter++) { + result = checkSequence(iter.value->first); + if (result != RETURN_OK) { + return result; + } + + } + return RETURN_OK; +} + +void Subsystem::setInitialMode(Mode_t mode) { + initialMode = mode; +} + +void Subsystem::cantKeepMode() { + ReturnValue_t result; + if ((result = checkSequence(getFallbackSequence(mode))) != RETURN_OK) { + triggerEvent(FALLBACK_FAILED, result, getFallbackSequence(mode)); + return; + } + + modeHelper.setForced(true); + + //already set the mode, so that we do not try to go back in our old mode when the transition fails + mode = getFallbackSequence(mode); + //SHOULDDO: We should store submodes for fallback sequence as well, otherwise we should get rid of submodes completely. + startTransition(mode, SUBMODE_NONE); +} diff --git a/fsfw/subsystem/Subsystem.h b/fsfw/subsystem/Subsystem.h new file mode 100644 index 0000000..a40b802 --- /dev/null +++ b/fsfw/subsystem/Subsystem.h @@ -0,0 +1,164 @@ +#ifndef SUBSYSTEM_H_ +#define SUBSYSTEM_H_ + +#include "../container/FixedArrayList.h" +#include "../container/FixedMap.h" +#include "../container/HybridIterator.h" +#include "../container/SinglyLinkedList.h" +#include "../serialize/SerialArrayListAdapter.h" +#include "modes/ModeDefinitions.h" +#include "SubsystemBase.h" + +class Subsystem: public SubsystemBase, public HasModeSequenceIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::SUBSYSTEM; + static const ReturnValue_t SEQUENCE_ALREADY_EXISTS = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t TABLE_ALREADY_EXISTS = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t TABLE_DOES_NOT_EXIST = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t TABLE_OR_SEQUENCE_LENGTH_INVALID = MAKE_RETURN_CODE(0x04); + static const ReturnValue_t SEQUENCE_DOES_NOT_EXIST = MAKE_RETURN_CODE(0x05); + static const ReturnValue_t TABLE_CONTAINS_INVALID_OBJECT_ID = + MAKE_RETURN_CODE(0x06); + static const ReturnValue_t FALLBACK_SEQUENCE_DOES_NOT_EXIST = + MAKE_RETURN_CODE(0x07); + static const ReturnValue_t NO_TARGET_TABLE = MAKE_RETURN_CODE(0x08); + static const ReturnValue_t SEQUENCE_OR_TABLE_TOO_LONG = MAKE_RETURN_CODE(0x09); + static const ReturnValue_t IS_FALLBACK_SEQUENCE = MAKE_RETURN_CODE(0x0B); + static const ReturnValue_t ACCESS_DENIED = MAKE_RETURN_CODE(0x0C); + static const ReturnValue_t TABLE_IN_USE = MAKE_RETURN_CODE(0x0E); + + static const ReturnValue_t TARGET_TABLE_NOT_REACHED = MAKE_RETURN_CODE(0xA1); + static const ReturnValue_t TABLE_CHECK_FAILED = MAKE_RETURN_CODE(0xA2); + + + + Subsystem(object_id_t setObjectId, object_id_t parent, + uint32_t maxNumberOfSequences, uint32_t maxNumberOfTables); + virtual ~Subsystem(); + + ReturnValue_t addSequence(ArrayList* sequence, Mode_t id, + Mode_t fallbackSequence, bool inStore = true, bool preInit = true); + + ReturnValue_t addTable(ArrayList *table, Mode_t id, + bool inStore = true, bool preInit = true); + + void setInitialMode(Mode_t mode); + + virtual ReturnValue_t initialize(); + + virtual ReturnValue_t checkObjectConnections(); + + virtual MessageQueueId_t getSequenceCommandQueue() const; + + /** + * + * + * IMPORTANT: Do not call on non existing sequence! Use existsSequence() first + * + * @param sequence + * @return + */ + ReturnValue_t checkSequence(Mode_t sequence); + + /** + * + * + * IMPORTANT: Do not call on non existing sequence! Use existsSequence() first + * + * @param iter + * @return + */ + ReturnValue_t checkSequence(HybridIterator iter, Mode_t fallbackSequence); +protected: + + struct EntryPointer { + bool islinked; + union { + ModeListEntry *firstLinkedElement; + ArrayList *array; + }; + }; + + struct SequenceInfo { + Mode_t fallbackSequence; + EntryPointer entries; + }; + + static const uint8_t MAX_NUMBER_OF_TABLES_OR_SEQUENCES = 70; + + static const uint8_t MAX_LENGTH_OF_TABLE_OR_SEQUENCE = 20; + + bool isInTransition; + + bool childrenChangedHealth; + + uint32_t uptimeStartTable; + + HybridIterator currentTargetTable; + + Mode_t targetMode; + + Submode_t targetSubmode; + + Mode_t initialMode; + + HybridIterator currentSequenceIterator; + + FixedMap modeTables; + + FixedMap modeSequences; + + StorageManagerIF *IPCStore; + +#ifdef USE_MODESTORE + ModeStoreIF *modeStore; +#endif + + bool existsModeSequence(Mode_t id); + + HybridIterator getSequence(Mode_t id); + + bool existsModeTable(Mode_t id); + + HybridIterator getTable(Mode_t id); + + HybridIterator getCurrentTable(); + +// void startSequence(Mode_t sequence); + + /** + * DO NOT USE ON NON EXISTING SEQUENCE + * + * @param a sequence + * @return the fallback sequence's Id + */ + Mode_t getFallbackSequence(Mode_t sequence); + + void replyToCommand(ReturnValue_t status, uint32_t parameter); + + ReturnValue_t deleteSequence(Mode_t id); + + ReturnValue_t deleteTable(Mode_t id); + + virtual void performChildOperation(); + + virtual ReturnValue_t handleCommandMessage(CommandMessage *message); + + bool isFallbackSequence(Mode_t SequenceId); + + bool isTableUsed(Mode_t tableId); + + virtual ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, + uint32_t *msToReachTheMode); + + virtual void startTransition(Mode_t mode, Submode_t submode); + + void sendSerializablesAsCommandMessage(Command_t command, SerializeIF **elements, uint8_t count); + + void transitionFailed(ReturnValue_t failureCode, uint32_t parameter); + + void cantKeepMode(); + +}; + +#endif /* SUBSYSTEM_H_ */ diff --git a/fsfw/subsystem/SubsystemBase.cpp b/fsfw/subsystem/SubsystemBase.cpp new file mode 100644 index 0000000..56ae106 --- /dev/null +++ b/fsfw/subsystem/SubsystemBase.cpp @@ -0,0 +1,356 @@ +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "SubsystemBase.h" +#include "../ipc/QueueFactory.h" + +SubsystemBase::SubsystemBase(object_id_t setObjectId, object_id_t parent, + Mode_t initialMode, uint16_t commandQueueDepth) : + SystemObject(setObjectId), mode(initialMode), submode(SUBMODE_NONE), childrenChangedMode( + false), commandsOutstanding(0), commandQueue(NULL), healthHelper(this, + setObjectId), modeHelper(this), parentId(parent) { + commandQueue = QueueFactory::instance()->createMessageQueue(commandQueueDepth, + CommandMessage::MAX_MESSAGE_SIZE); +} + +SubsystemBase::~SubsystemBase() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); + +} + +ReturnValue_t SubsystemBase::registerChild(object_id_t objectId) { + ChildInfo info; + + HasModesIF *child = objectManager->get(objectId); + //This is a rather ugly hack to have the changedHealth info for all children available. (needed for FOGs). + HasHealthIF* healthChild = objectManager->get(objectId); + if (child == NULL) { + if (healthChild == NULL) { + return CHILD_DOESNT_HAVE_MODES; + } else { + info.commandQueue = healthChild->getCommandQueue(); + info.mode = MODE_OFF; + } + } else { + info.commandQueue = child->getCommandQueue(); + info.mode = -1; //intentional to force an initial command during system startup + } + + info.submode = SUBMODE_NONE; + info.healthChanged = false; + + std::pair::iterator, bool> returnValue = + childrenMap.insert( + std::pair(objectId, info)); + if (!(returnValue.second)) { + return COULD_NOT_INSERT_CHILD; + } else { + return RETURN_OK; + } +} + +ReturnValue_t SubsystemBase::checkStateAgainstTable( + HybridIterator tableIter, Submode_t targetSubmode) { + + std::map::iterator childIter; + + for (; tableIter.value != NULL; ++tableIter) { + object_id_t object = tableIter.value->getObject(); + + if ((childIter = childrenMap.find(object)) == childrenMap.end()) { + return RETURN_FAILED; + } + + if (childIter->second.mode != tableIter.value->getMode()) { + return RETURN_FAILED; + } + + Submode_t submodeToCheckAgainst = tableIter.value->getSubmode(); + if (tableIter.value->inheritSubmode()) { + submodeToCheckAgainst = targetSubmode; + } + + if (childIter->second.submode != submodeToCheckAgainst) { + return RETURN_FAILED; + } + } + return RETURN_OK; +} + +void SubsystemBase::executeTable(HybridIterator tableIter, Submode_t targetSubmode) { + + CommandMessage message; + + std::map::iterator iter; + + commandsOutstanding = 0; + + for (; tableIter.value != NULL; ++tableIter) { + object_id_t object = tableIter.value->getObject(); + if ((iter = childrenMap.find(object)) == childrenMap.end()) { + //illegal table entry, should only happen due to misconfigured mode table + sif::debug << std::hex << getObjectId() << ": invalid mode table entry" + << std::endl; + continue; + } + + Submode_t submodeToCommand = tableIter.value->getSubmode(); + if (tableIter.value->inheritSubmode()) { + submodeToCommand = targetSubmode; + } + + if (healthHelper.healthTable->hasHealth(object)) { + if (healthHelper.healthTable->isFaulty(object)) { + ModeMessage::setModeMessage(&message, + ModeMessage::CMD_MODE_COMMAND, HasModesIF::MODE_OFF, + SUBMODE_NONE); + } else { + if (modeHelper.isForced()) { + ModeMessage::setModeMessage(&message, + ModeMessage::CMD_MODE_COMMAND_FORCED, + tableIter.value->getMode(), submodeToCommand); + } else { + if (healthHelper.healthTable->isCommandable(object)) { + ModeMessage::setModeMessage(&message, + ModeMessage::CMD_MODE_COMMAND, + tableIter.value->getMode(), submodeToCommand); + } else { + continue; + } + } + } + } else { + ModeMessage::setModeMessage(&message, ModeMessage::CMD_MODE_COMMAND, + tableIter.value->getMode(), submodeToCommand); + } + + if ((iter->second.mode == ModeMessage::getMode(&message)) + && (iter->second.submode == ModeMessage::getSubmode(&message)) + && !modeHelper.isForced()) { + continue; //don't send redundant mode commands (produces event spam), but still command if mode is forced to reach lower levels + } + ReturnValue_t result = commandQueue->sendMessage( + iter->second.commandQueue, &message); + if (result == RETURN_OK) { + ++commandsOutstanding; + } + } + +} + +ReturnValue_t SubsystemBase::updateChildMode(MessageQueueId_t queue, + Mode_t mode, Submode_t submode) { + std::map::iterator iter; + + for (iter = childrenMap.begin(); iter != childrenMap.end(); iter++) { + if (iter->second.commandQueue == queue) { + iter->second.mode = mode; + iter->second.submode = submode; + return RETURN_OK; + } + } + return CHILD_NOT_FOUND; +} + +ReturnValue_t SubsystemBase::updateChildChangedHealth(MessageQueueId_t queue, +bool changedHealth) { + for (auto iter = childrenMap.begin(); iter != childrenMap.end(); iter++) { + if (iter->second.commandQueue == queue) { + iter->second.healthChanged = changedHealth; + return RETURN_OK; + } + } + return CHILD_NOT_FOUND; +} + +MessageQueueId_t SubsystemBase::getCommandQueue() const { + return commandQueue->getId(); +} + +ReturnValue_t SubsystemBase::initialize() { + MessageQueueId_t parentQueue = 0; + ReturnValue_t result = SystemObject::initialize(); + + if (result != RETURN_OK) { + return result; + } + + if (parentId != 0) { + SubsystemBase *parent = objectManager->get(parentId); + if (parent == NULL) { + return RETURN_FAILED; + } + parentQueue = parent->getCommandQueue(); + + parent->registerChild(getObjectId()); + } + + result = healthHelper.initialize(parentQueue); + + if (result != RETURN_OK) { + return result; + } + + result = modeHelper.initialize(parentQueue); + + if (result != RETURN_OK) { + return result; + } + + return RETURN_OK; +} + +ReturnValue_t SubsystemBase::performOperation(uint8_t opCode) { + + childrenChangedMode = false; + + checkCommandQueue(); + + performChildOperation(); + + return RETURN_OK; +} + +ReturnValue_t SubsystemBase::handleModeReply(CommandMessage* message) { + switch (message->getCommand()) { + case ModeMessage::REPLY_MODE_INFO: + updateChildMode(message->getSender(), ModeMessage::getMode(message), + ModeMessage::getSubmode(message)); + childrenChangedMode = true; + return RETURN_OK; + case ModeMessage::REPLY_MODE_REPLY: + case ModeMessage::REPLY_WRONG_MODE_REPLY: + updateChildMode(message->getSender(), ModeMessage::getMode(message), + ModeMessage::getSubmode(message)); + childrenChangedMode = true; + commandsOutstanding--; + return RETURN_OK; + case ModeMessage::REPLY_CANT_REACH_MODE: + commandsOutstanding--; + { + for (auto iter = childrenMap.begin(); iter != childrenMap.end(); + iter++) { + if (iter->second.commandQueue == message->getSender()) { + triggerEvent(MODE_CMD_REJECTED, iter->first, + message->getParameter()); + } + } + } + return RETURN_OK; +// case ModeMessage::CMD_MODE_COMMAND: +// handleCommandedMode(message); +// return RETURN_OK; +// case ModeMessage::CMD_MODE_ANNOUNCE: +// triggerEvent(MODE_INFO, mode, submode); +// return RETURN_OK; +// case ModeMessage::CMD_MODE_ANNOUNCE_RECURSIVELY: +// triggerEvent(MODE_INFO, mode, submode); +// commandAllChildren(message); +// return RETURN_OK; + default: + return RETURN_FAILED; + } +} + +ReturnValue_t SubsystemBase::checkTable( + HybridIterator tableIter) { + for (; tableIter.value != NULL; ++tableIter) { + if (childrenMap.find(tableIter.value->getObject()) + == childrenMap.end()) { + return TABLE_CONTAINS_INVALID_OBJECT_ID; + } + } + return RETURN_OK; +} + +void SubsystemBase::replyToCommand(CommandMessage* message) { + commandQueue->reply(message); +} + +void SubsystemBase::setMode(Mode_t newMode, Submode_t newSubmode) { + modeHelper.modeChanged(newMode, newSubmode); + mode = newMode; + submode = newSubmode; + modeChanged(); + announceMode(false); +} + +void SubsystemBase::setMode(Mode_t newMode) { + setMode(newMode, submode); +} + +void SubsystemBase::commandAllChildren(CommandMessage* message) { + std::map::iterator iter; + for (iter = childrenMap.begin(); iter != childrenMap.end(); ++iter) { + commandQueue->sendMessage(iter->second.commandQueue, message); + } +} + +void SubsystemBase::getMode(Mode_t* mode, Submode_t* submode) { + *mode = this->mode; + *submode = this->submode; +} + +void SubsystemBase::setToExternalControl() { + healthHelper.setHealth(EXTERNAL_CONTROL); +} + +void SubsystemBase::announceMode(bool recursive) { + triggerEvent(MODE_INFO, mode, submode); + if (recursive) { + CommandMessage command; + ModeMessage::setModeMessage(&command, + ModeMessage::CMD_MODE_ANNOUNCE_RECURSIVELY, 0, 0); + commandAllChildren(&command); + } +} + +void SubsystemBase::checkCommandQueue() { + ReturnValue_t result; + CommandMessage message; + + for (result = commandQueue->receiveMessage(&message); result == RETURN_OK; + result = commandQueue->receiveMessage(&message)) { + + result = healthHelper.handleHealthCommand(&message); + if (result == RETURN_OK) { + continue; + } + + result = modeHelper.handleModeCommand(&message); + if (result == RETURN_OK) { + continue; + } + + result = handleModeReply(&message); + if (result == RETURN_OK) { + continue; + } + + result = handleCommandMessage(&message); + if (result != RETURN_OK) { + CommandMessage reply; + reply.setReplyRejected(CommandMessage::UNKNOWN_COMMAND, + message.getCommand()); + replyToCommand(&reply); + } + } +} + +ReturnValue_t SubsystemBase::setHealth(HealthState health) { + switch (health) { + case HEALTHY: + case EXTERNAL_CONTROL: + healthHelper.setHealth(health); + return RETURN_OK; + default: + return INVALID_HEALTH_STATE; + } +} + +HasHealthIF::HealthState SubsystemBase::getHealth() { + return healthHelper.getHealth(); +} + +void SubsystemBase::modeChanged() { +} + diff --git a/fsfw/subsystem/SubsystemBase.h b/fsfw/subsystem/SubsystemBase.h new file mode 100644 index 0000000..61a7eae --- /dev/null +++ b/fsfw/subsystem/SubsystemBase.h @@ -0,0 +1,125 @@ +#ifndef SUBSYSTEMBASE_H_ +#define SUBSYSTEMBASE_H_ + +#include "../container/HybridIterator.h" +#include "../health/HasHealthIF.h" +#include "../health/HealthHelper.h" +#include "../modes/HasModesIF.h" +#include "../objectmanager/SystemObject.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "modes/HasModeSequenceIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../ipc/MessageQueueIF.h" +#include + +class SubsystemBase: public SystemObject, + public HasModesIF, + public HasHealthIF, + public HasReturnvaluesIF, + public ExecutableObjectIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::SUBSYSTEM_BASE; + static const ReturnValue_t CHILD_NOT_FOUND = MAKE_RETURN_CODE(0x01); + static const ReturnValue_t CHILD_INFO_UPDATED = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t CHILD_DOESNT_HAVE_MODES = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t COULD_NOT_INSERT_CHILD = MAKE_RETURN_CODE(0x04); + static const ReturnValue_t TABLE_CONTAINS_INVALID_OBJECT_ID = + MAKE_RETURN_CODE(0x05); + + SubsystemBase(object_id_t setObjectId, object_id_t parent, + Mode_t initialMode = 0, uint16_t commandQueueDepth = 8); + virtual ~SubsystemBase(); + + virtual MessageQueueId_t getCommandQueue() const; + + ReturnValue_t registerChild(object_id_t objectId); + + virtual ReturnValue_t initialize(); + + virtual ReturnValue_t performOperation(uint8_t opCode); + + virtual ReturnValue_t setHealth(HealthState health); + + virtual HasHealthIF::HealthState getHealth(); + +protected: + struct ChildInfo { + MessageQueueId_t commandQueue; + Mode_t mode; + Submode_t submode;bool healthChanged; + }; + + Mode_t mode; + + Submode_t submode; + + bool childrenChangedMode; + + /** + * Always check this against <=0, so you are robust against too many replies + */ + int32_t commandsOutstanding; + + MessageQueueIF* commandQueue; + + HealthHelper healthHelper; + + ModeHelper modeHelper; + + const object_id_t parentId; + + typedef std::map ChildrenMap; + ChildrenMap childrenMap; + + void checkCommandQueue(); + + /** + * We need to know the target Submode, as children are able to inherit the submode + */ + ReturnValue_t checkStateAgainstTable( + HybridIterator tableIter, Submode_t targetSubmode); + + /** + * We need to know the target Submode, as children are able to inherit the submode + * Still, we have a default for all child implementations which do not use submode inheritance + */ + void executeTable(HybridIterator tableIter, + Submode_t targetSubmode = SUBMODE_NONE); + + ReturnValue_t updateChildMode(MessageQueueId_t queue, Mode_t mode, + Submode_t submode); + + ReturnValue_t updateChildChangedHealth(MessageQueueId_t queue, + bool changedHealth = true); + + virtual ReturnValue_t handleModeReply(CommandMessage *message); + + void commandAllChildren(CommandMessage *message); + + ReturnValue_t checkTable(HybridIterator tableIter); + + void replyToCommand(CommandMessage *message); + + void setMode(Mode_t newMode, Submode_t newSubmode); + + void setMode(Mode_t newMode); + + virtual ReturnValue_t handleCommandMessage(CommandMessage *message) = 0; + + virtual void performChildOperation() = 0; + + virtual ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, + uint32_t *msToReachTheMode) = 0; + + virtual void startTransition(Mode_t mode, Submode_t submode) = 0; + + virtual void getMode(Mode_t *mode, Submode_t *submode); + + virtual void setToExternalControl(); + + virtual void announceMode(bool recursive); + + virtual void modeChanged(); +}; + +#endif /* SUBSYSTEMBASE_H_ */ diff --git a/fsfw/subsystem/modes/HasModeSequenceIF.h b/fsfw/subsystem/modes/HasModeSequenceIF.h new file mode 100644 index 0000000..70b1667 --- /dev/null +++ b/fsfw/subsystem/modes/HasModeSequenceIF.h @@ -0,0 +1,20 @@ +#ifndef HASMODESEQUENCEIF_H_ +#define HASMODESEQUENCEIF_H_ + +#include "ModeDefinitions.h" +#include "ModeSequenceMessage.h" +#include "ModeStoreIF.h" + + +class HasModeSequenceIF { +public: + virtual ~HasModeSequenceIF() { + + } + + virtual MessageQueueId_t getSequenceCommandQueue() const = 0; + +}; + + +#endif /* HASMODESEQUENCEIF_H_ */ diff --git a/fsfw/subsystem/modes/ModeDefinitions.h b/fsfw/subsystem/modes/ModeDefinitions.h new file mode 100644 index 0000000..a865ab0 --- /dev/null +++ b/fsfw/subsystem/modes/ModeDefinitions.h @@ -0,0 +1,152 @@ +#ifndef MODEDEFINITIONS_H_ +#define MODEDEFINITIONS_H_ + +#include "../../modes/HasModesIF.h" +#include "../../objectmanager/SystemObjectIF.h" +#include "../../serialize/SerializeIF.h" +#include "../../serialize/SerialLinkedListAdapter.h" +class ModeListEntry: public SerializeIF, public LinkedElement { +public: + ModeListEntry() : + LinkedElement(this), value1(0), value2(0), value3(0), value4( + 0) { + + } + + uint32_t value1; + uint32_t value2; + uint8_t value3; + uint8_t value4; + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + + ReturnValue_t result; + + result = SerializeAdapter::serialize(&value1, buffer, size, + maxSize, streamEndianness); + + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&value2, buffer, size, + maxSize, streamEndianness); + + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::serialize(&value3, buffer, size, + maxSize, streamEndianness); + + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = SerializeAdapter::serialize(&value4, buffer, size, + maxSize, streamEndianness); + + return result; + + } + + virtual size_t getSerializedSize() const { + return sizeof(value1) + sizeof(value2) + sizeof(value3) + sizeof(value4); + } + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + ReturnValue_t result; + + result = SerializeAdapter::deSerialize(&value1, buffer, size, + streamEndianness); + + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&value2, buffer, size, + streamEndianness); + + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&value3, buffer, size, + streamEndianness); + + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&value4, buffer, size, + streamEndianness); + + return result; + } + + //for Sequences + Mode_t getTableId() const { + return value1; + } + + void setTableId(Mode_t tableId) { + this->value1 = tableId; + } + + uint8_t getWaitSeconds() const { + return value2; + } + + void setWaitSeconds(uint8_t waitSeconds) { + this->value2 = waitSeconds; + } + + bool checkSuccess() const { + return value3 == 1; + } + + void setCheckSuccess(bool checkSuccess) { + this->value3 = checkSuccess; + } + + //for Tables + object_id_t getObject() const { + return value1; + } + + void setObject(object_id_t object) { + this->value1 = object; + } + + Mode_t getMode() const { + return value2; + } + + void setMode(Mode_t mode) { + this->value2 = mode; + } + + Submode_t getSubmode() const { + return value3; + } + + void setSubmode(Submode_t submode) { + this->value3 = submode; + } + + bool inheritSubmode() const { + return value4 == 1; + } + + void setInheritSubmode(bool inherit){ + if (inherit){ + value4 = 1; + } else { + value4 = 0; + } + } + + bool operator==(ModeListEntry other) { + return ((value1 == other.value1) && (value2 == other.value2) + && (value3 == other.value3)); + } +}; + +#endif //MODEDEFINITIONS_H_ diff --git a/fsfw/subsystem/modes/ModeSequenceMessage.cpp b/fsfw/subsystem/modes/ModeSequenceMessage.cpp new file mode 100644 index 0000000..33019f5 --- /dev/null +++ b/fsfw/subsystem/modes/ModeSequenceMessage.cpp @@ -0,0 +1,89 @@ +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../storagemanager/StorageManagerIF.h" +#include "ModeSequenceMessage.h" + +void ModeSequenceMessage::setModeSequenceMessage(CommandMessage* message, + Command_t command, Mode_t sequence, store_address_t storeAddress) { + message->setCommand(command); + message->setParameter(storeAddress.raw); + message->setParameter2(sequence); +} + +//void ModeSequenceMessage::setModeSequenceMessage(CommandMessage* message, +// Command_t command, ModeTableId_t table, store_address_t storeAddress) { +// message->setCommand(command); +// message->setParameter(storeAddress.raw); +// message->setParameter2(table); +//} + +void ModeSequenceMessage::setModeSequenceMessage(CommandMessage* message, + Command_t command, Mode_t sequence) { + message->setCommand(command); + message->setParameter2(sequence); +} + +//void ModeSequenceMessage::setModeSequenceMessage(CommandMessage* message, +// Command_t command, ModeTableId_t table) { +// message->setCommand(command); +// message->setParameter2(table); +//} + +void ModeSequenceMessage::setModeSequenceMessage(CommandMessage* message, + Command_t command, store_address_t storeAddress) { + message->setCommand(command); + message->setParameter(storeAddress.raw); +} + +store_address_t ModeSequenceMessage::getStoreAddress( + const CommandMessage* message) { + store_address_t address; + address.raw = message->getParameter(); + return address; +} + +Mode_t ModeSequenceMessage::getSequenceId(const CommandMessage* message) { + return message->getParameter2(); +} + +Mode_t ModeSequenceMessage::getTableId(const CommandMessage* message) { + return message->getParameter2(); +} + + +uint32_t ModeSequenceMessage::getNumber(const CommandMessage* message) { + return message->getParameter2(); +} + +void ModeSequenceMessage::clear(CommandMessage *message) { + switch (message->getCommand()) { + case ADD_SEQUENCE: + case ADD_TABLE: + case SEQUENCE_LIST: + case TABLE_LIST: + case TABLE: + case SEQUENCE:{ + StorageManagerIF *ipcStore = objectManager->get(objects::IPC_STORE); + if (ipcStore != NULL){ + ipcStore->deleteData(ModeSequenceMessage::getStoreAddress(message)); + } + } + /* NO BREAK falls through*/ + case DELETE_SEQUENCE: + case DELETE_TABLE: + case READ_SEQUENCE: + case READ_TABLE: + case LIST_SEQUENCES: + case LIST_TABLES: + case READ_FREE_SEQUENCE_SLOTS: + case FREE_SEQUENCE_SLOTS: + case READ_FREE_TABLE_SLOTS: + case FREE_TABLE_SLOTS: + default: + message->setCommand(CommandMessage::CMD_NONE); + message->setParameter(0); + message->setParameter2(0); + break; + } +} diff --git a/fsfw/subsystem/modes/ModeSequenceMessage.h b/fsfw/subsystem/modes/ModeSequenceMessage.h new file mode 100644 index 0000000..bbf747f --- /dev/null +++ b/fsfw/subsystem/modes/ModeSequenceMessage.h @@ -0,0 +1,48 @@ +#ifndef MODESEQUENCEMESSAGE_H_ +#define MODESEQUENCEMESSAGE_H_ + +#include "../../ipc/CommandMessage.h" +#include "../../storagemanager/StorageManagerIF.h" +#include "ModeDefinitions.h" + +class ModeSequenceMessage { +public: + static const uint8_t MESSAGE_ID = messagetypes::MODE_SEQUENCE; + + static const Command_t ADD_SEQUENCE = MAKE_COMMAND_ID(0x01); + static const Command_t ADD_TABLE = MAKE_COMMAND_ID(0x02); + static const Command_t DELETE_SEQUENCE = MAKE_COMMAND_ID(0x03); + static const Command_t DELETE_TABLE = MAKE_COMMAND_ID(0x04); + static const Command_t READ_SEQUENCE = MAKE_COMMAND_ID(0x05); + static const Command_t READ_TABLE = MAKE_COMMAND_ID(0x06); + static const Command_t LIST_SEQUENCES = MAKE_COMMAND_ID(0x07); + static const Command_t LIST_TABLES = MAKE_COMMAND_ID(0x08); + static const Command_t SEQUENCE_LIST = MAKE_COMMAND_ID(0x09); + static const Command_t TABLE_LIST = MAKE_COMMAND_ID(0x0A); + static const Command_t TABLE = MAKE_COMMAND_ID(0x0B); + static const Command_t SEQUENCE = MAKE_COMMAND_ID(0x0C); + static const Command_t READ_FREE_SEQUENCE_SLOTS = MAKE_COMMAND_ID(0x0D); + static const Command_t FREE_SEQUENCE_SLOTS = MAKE_COMMAND_ID(0x0E); + static const Command_t READ_FREE_TABLE_SLOTS = MAKE_COMMAND_ID(0x0F); + static const Command_t FREE_TABLE_SLOTS = MAKE_COMMAND_ID(0x10); + + static void setModeSequenceMessage(CommandMessage *message, + Command_t command, Mode_t sequenceOrTable, + store_address_t storeAddress); + static void setModeSequenceMessage(CommandMessage *message, + Command_t command, Mode_t sequenceOrTable); + static void setModeSequenceMessage(CommandMessage *message, + Command_t command, store_address_t storeAddress); + + static store_address_t getStoreAddress(const CommandMessage *message); + static Mode_t getSequenceId(const CommandMessage *message); + static Mode_t getTableId(const CommandMessage *message); + static uint32_t getNumber(const CommandMessage *message); + + static void clear(CommandMessage *message); + +private: + ModeSequenceMessage(); +}; + +#endif /* MODESEQUENCEMESSAGE_H_ */ diff --git a/fsfw/subsystem/modes/ModeStore.cpp b/fsfw/subsystem/modes/ModeStore.cpp new file mode 100644 index 0000000..217e177 --- /dev/null +++ b/fsfw/subsystem/modes/ModeStore.cpp @@ -0,0 +1,126 @@ +#include "ModeStore.h" + +#ifdef USE_MODESTORE + +ModeStore::ModeStore(object_id_t objectId, uint32_t slots) : + SystemObject(objectId), store(slots), emptySlot(store.front()) { + mutex = MutexFactory::instance()->createMutex();; + OSAL::createMutex(objectId + 1, mutex); + clear(); +} + +ModeStore::~ModeStore() { + delete mutex; +} + +uint32_t ModeStore::getFreeSlots() { + OSAL::lockMutex(mutex, OSAL::NO_TIMEOUT); + uint32_t count = 0; + ArrayList::Iterator iter; + for (iter = store.begin(); iter != store.end(); ++iter) { + if (iter->getNext() == emptySlot) { + ++count; + } + } + OSAL::unlockMutex(mutex); + return count; +} + +ReturnValue_t ModeStore::storeArray(ArrayList* sequence, + ModeListEntry** storedFirstEntry) { + if (sequence->size == 0) { + return CANT_STORE_EMPTY; + } + OSAL::lockMutex(mutex, OSAL::NO_TIMEOUT); + *storedFirstEntry = findEmptySlotNoLock(store.front()); + + ModeListEntry* pointer = + *storedFirstEntry; + pointer->setNext(pointer); + + ArrayList::Iterator iter; + for (iter = sequence->begin(); iter != sequence->end(); ++iter) { + //SHOULDDO: I need to check this in detail. What is the idea? Why does it not work? + pointer = pointer->getNext()->value; + if (pointer == NULL) { + deleteListNoLock(*storedFirstEntry); + OSAL::unlockMutex(mutex); + return TOO_MANY_ELEMENTS; + } + pointer->value->value1 = iter->value1; + pointer->value->value2 = iter->value2; + pointer->value->value3 = iter->value3; + pointer->setNext(findEmptySlotNoLock(pointer + 1)); + } + pointer->setNext(NULL); + OSAL::unlockMutex(mutex); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t ModeStore::deleteList(ModeListEntry* sequence) { + ReturnValue_t result = isValidEntry(sequence); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + OSAL::lockMutex(mutex, OSAL::NO_TIMEOUT); + deleteListNoLock(sequence); + OSAL::unlockMutex(mutex); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t ModeStore::readList(ModeListEntry* sequence, + ArrayList* into) { + ReturnValue_t result = isValidEntry(sequence); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + OSAL::lockMutex(mutex, OSAL::NO_TIMEOUT); + result = into->insert(*sequence->value); + while ((result == HasReturnvaluesIF::RETURN_OK) && (sequence->getNext() != NULL)) { + result = into->insert(*sequence->value); + sequence = sequence->getNext()->value; + } + OSAL::unlockMutex(mutex); + return result; +} + +void ModeStore::clear() { + OSAL::lockMutex(mutex, OSAL::NO_TIMEOUT); + store.size = store.maxSize(); + ArrayList::Iterator iter; + for (iter = store.begin(); iter != store.end(); ++iter) { + iter->setNext(emptySlot); + } + OSAL::unlockMutex(mutex); +} + +ModeListEntry* ModeStore::findEmptySlotNoLock(ModeListEntry* startFrom) { + ArrayList::Iterator iter( + startFrom); + for (; iter != store.end(); ++iter) { + if (iter.value->getNext() == emptySlot) { + OSAL::unlockMutex(mutex); + return iter.value; + } + } + return NULL; +} + +void ModeStore::deleteListNoLock(ModeListEntry* sequence) { + ModeListEntry* next = sequence; + while (next != NULL) { + next = sequence->getNext()->value; + sequence->setNext(emptySlot); + sequence = next; + } +} + +ReturnValue_t ModeStore::isValidEntry(ModeListEntry* sequence) { + if ((sequence < store.front()) || (sequence > store.back()) + || sequence->getNext() == emptySlot) { + return INVALID_ENTRY; + } + return HasReturnvaluesIF::RETURN_OK; +} + +#endif diff --git a/fsfw/subsystem/modes/ModeStore.h b/fsfw/subsystem/modes/ModeStore.h new file mode 100644 index 0000000..0bf856a --- /dev/null +++ b/fsfw/subsystem/modes/ModeStore.h @@ -0,0 +1,45 @@ +#ifndef MODESTORE_H_ +#define MODESTORE_H_ + +#ifdef USE_MODESTORE + +#include "../../container/ArrayList.h" +#include "../../container/SinglyLinkedList.h" +#include "../../objectmanager/SystemObject.h" +#include "ModeStoreIF.h" + +class ModeStore: public ModeStoreIF, public SystemObject { +public: + ModeStore(object_id_t objectId, uint32_t slots); + virtual ~ModeStore(); + + virtual ReturnValue_t storeArray(ArrayList *sequence, + ModeListEntry **storedFirstEntry); + + virtual ReturnValue_t deleteList( + ModeListEntry *sequence); + + virtual ReturnValue_t readList( + ModeListEntry *sequence, + ArrayList *into); + + virtual uint32_t getFreeSlots(); + +private: + MutexId_t* mutex; + ArrayList store; + ModeListEntry *emptySlot; + + void clear(); + ModeListEntry* findEmptySlotNoLock( + ModeListEntry* startFrom); + void deleteListNoLock( + ModeListEntry *sequence); + + ReturnValue_t isValidEntry(ModeListEntry *sequence); +}; + +#endif + +#endif /* MODESTORE_H_ */ + diff --git a/fsfw/subsystem/modes/ModeStoreIF.h b/fsfw/subsystem/modes/ModeStoreIF.h new file mode 100644 index 0000000..e5cac3b --- /dev/null +++ b/fsfw/subsystem/modes/ModeStoreIF.h @@ -0,0 +1,37 @@ +#ifndef MODESTOREIF_H_ +#define MODESTOREIF_H_ + +#ifdef USE_MODESTORE + +#include "../../container/ArrayList.h" +#include "../../container/SinglyLinkedList.h" +#include "../../returnvalues/HasReturnvaluesIF.h" +#include "ModeDefinitions.h" + +class ModeStoreIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::MODE_STORE_IF; + static const ReturnValue_t INVALID_ENTRY = MAKE_RETURN_CODE(0x02); + static const ReturnValue_t TOO_MANY_ELEMENTS = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t CANT_STORE_EMPTY = MAKE_RETURN_CODE(0x04); + + virtual ~ModeStoreIF() { + + } + + virtual ReturnValue_t storeArray(ArrayList *sequence, + ModeListEntry **storedFirstEntry) = 0; + + virtual ReturnValue_t deleteList( + ModeListEntry *sequence) = 0; + + virtual ReturnValue_t readList( + ModeListEntry *sequence, + ArrayList *into) = 0; + + virtual uint32_t getFreeSlots() = 0; +}; + +#endif + +#endif /* MODESTOREIF_H_ */ diff --git a/fsfw/tasks/ExecutableObjectIF.h b/fsfw/tasks/ExecutableObjectIF.h new file mode 100644 index 0000000..06b837d --- /dev/null +++ b/fsfw/tasks/ExecutableObjectIF.h @@ -0,0 +1,53 @@ +#ifndef FRAMEWORK_TASKS_EXECUTABLEOBJECTIF_H_ +#define FRAMEWORK_TASKS_EXECUTABLEOBJECTIF_H_ + +class PeriodicTaskIF; + +#include "../returnvalues/HasReturnvaluesIF.h" + +#include +/** + * @brief The interface provides a method to execute objects within a task. + * @details The performOperation method, that is required by the interface is + * executed cyclically within a task context. + * @author Bastian Baetz + */ +class ExecutableObjectIF { +public: + /** + * @brief This is the empty virtual destructor as required for C++ interfaces. + */ + virtual ~ExecutableObjectIF() { } + /** + * @brief The performOperation method is executed in a task. + * @details There are no restrictions for calls within this method, so any + * other member of the class can be used. + * @return Currently, the return value is ignored. + */ + virtual ReturnValue_t performOperation(uint8_t operationCode = 0) = 0; + + /** + * @brief Function called during setup assignment of object to task + * @details + * Has to be called from the function that assigns the object to a task and + * enables the object implementation to overwrite this function and get + * a reference to the executing task + * @param task_ Pointer to the taskIF of this task + */ + virtual void setTaskIF(PeriodicTaskIF* task_) {}; + + /** + * This function should be called after the object was assigned to a + * specific task. + * + * Example: Can be used to get task execution frequency. + * The task is created after initialize() and the object ctors have been + * called so the execution frequency can't be cached in initialize() + * @return + */ + virtual ReturnValue_t initializeAfterTaskCreation() { + return HasReturnvaluesIF::RETURN_OK; + } +}; + +#endif /* FRAMEWORK_TASKS_EXECUTABLEOBJECTIF_H_ */ diff --git a/fsfw/tasks/FixedSequenceSlot.cpp b/fsfw/tasks/FixedSequenceSlot.cpp new file mode 100644 index 0000000..f5d8217 --- /dev/null +++ b/fsfw/tasks/FixedSequenceSlot.cpp @@ -0,0 +1,17 @@ +#include "FixedSequenceSlot.h" +#include "PeriodicTaskIF.h" +#include + +FixedSequenceSlot::FixedSequenceSlot(object_id_t handlerId, uint32_t setTime, + int8_t setSequenceId, ExecutableObjectIF* executableObject, + PeriodicTaskIF* executingTask) : handlerId(handlerId), + pollingTimeMs(setTime), opcode(setSequenceId) { + if(executableObject == nullptr) { + return; + } + this->executableObject = executableObject; + this->executableObject->setTaskIF(executingTask); +} + +FixedSequenceSlot::~FixedSequenceSlot() {} + diff --git a/fsfw/tasks/FixedSequenceSlot.h b/fsfw/tasks/FixedSequenceSlot.h new file mode 100644 index 0000000..1744ec1 --- /dev/null +++ b/fsfw/tasks/FixedSequenceSlot.h @@ -0,0 +1,60 @@ +#ifndef FSFW_TASKS_FIXEDSEQUENCESLOT_H_ +#define FSFW_TASKS_FIXEDSEQUENCESLOT_H_ + +#include "ExecutableObjectIF.h" +#include "../objectmanager/ObjectManagerIF.h" + +class PeriodicTaskIF; + +/** + * @brief This class is the representation of a single polling sequence + * table entry. + * @details + * The PollingSlot class is the representation of a single polling + * sequence table entry. + * @author baetz + */ +class FixedSequenceSlot { +public: + FixedSequenceSlot( object_id_t handlerId, uint32_t setTimeMs, + int8_t setSequenceId, ExecutableObjectIF* executableObject, + PeriodicTaskIF* executingTask); + virtual ~FixedSequenceSlot(); + + object_id_t handlerId; + + /** + * @brief Handler identifies which object is executed in this slot. + */ + ExecutableObjectIF* executableObject = nullptr; + + /** + * @brief This attribute defines when a device handler object is executed. + * @details + * The pollingTime attribute identifies the time the handler is + * executed in ms. It must be smaller than the period length of the + * polling sequence. + */ + uint32_t pollingTimeMs; + + /** + * @brief This value defines the type of device communication. + * + * @details The state of this value decides what communication routine is + * called in the PST executable or the device handler object. + */ + uint8_t opcode; + + /** + * @brief Operator overload for the comparison operator to + * allow sorting by polling time. + * @param fixedSequenceSlot + * @return + */ + bool operator <(const FixedSequenceSlot & fixedSequenceSlot) const { + return pollingTimeMs < fixedSequenceSlot.pollingTimeMs; + } +}; + + +#endif /* FSFW_TASKS_FIXEDSEQUENCESLOT_H_ */ diff --git a/fsfw/tasks/FixedSlotSequence.cpp b/fsfw/tasks/FixedSlotSequence.cpp new file mode 100644 index 0000000..e5db430 --- /dev/null +++ b/fsfw/tasks/FixedSlotSequence.cpp @@ -0,0 +1,162 @@ +#include "FixedSlotSequence.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include + +FixedSlotSequence::FixedSlotSequence(uint32_t setLengthMs) : + lengthMs(setLengthMs) { + current = slotList.begin(); +} + +FixedSlotSequence::~FixedSlotSequence() { + // Call the destructor on each list entry. + slotList.clear(); +} + +void FixedSlotSequence::executeAndAdvance() { + current->executableObject->performOperation(current->opcode); +// if (returnValue != RETURN_OK) { +// this->sendErrorMessage( returnValue ); +// } + //Increment the polling Sequence iterator + this->current++; + //Set it to the beginning, if the list's end is reached. + if (this->current == this->slotList.end()) { + this->current = this->slotList.begin(); + } +} + +uint32_t FixedSlotSequence::getIntervalToNextSlotMs() { + uint32_t oldTime; + SlotListIter slotListIter = current; + // Get the pollingTimeMs of the current slot object. + oldTime = slotListIter->pollingTimeMs; + // Advance to the next object. + slotListIter++; + // Find the next interval which is not 0. + while (slotListIter != slotList.end()) { + if (oldTime != slotListIter->pollingTimeMs) { + return slotListIter->pollingTimeMs - oldTime; + } else { + slotListIter++; + } + } + // If the list end is reached (this is definitely an interval != 0), + // the interval is calculated by subtracting the remaining time of the PST + // and adding the start time of the first handler in the list. + slotListIter = slotList.begin(); + return lengthMs - oldTime + slotListIter->pollingTimeMs; +} + +uint32_t FixedSlotSequence::getIntervalToPreviousSlotMs() { + uint32_t currentTime; + SlotListIter slotListIter = current; + // Get the pollingTimeMs of the current slot object. + currentTime = slotListIter->pollingTimeMs; + + //if it is the first slot, calculate difference to last slot + if (slotListIter == slotList.begin()){ + return lengthMs - (--slotList.end())->pollingTimeMs + currentTime; + } + // get previous slot + slotListIter--; + + return currentTime - slotListIter->pollingTimeMs; +} + +bool FixedSlotSequence::slotFollowsImmediately() { + uint32_t currentTime = current->pollingTimeMs; + SlotListIter fixedSequenceIter = this->current; + // Get the pollingTimeMs of the current slot object. + if (fixedSequenceIter == slotList.begin()) + return false; + fixedSequenceIter--; + if (fixedSequenceIter->pollingTimeMs == currentTime) { + return true; + } else { + return false; + } +} + +uint32_t FixedSlotSequence::getLengthMs() const { + return this->lengthMs; +} + +void FixedSlotSequence::addSlot(object_id_t componentId, uint32_t slotTimeMs, + int8_t executionStep, ExecutableObjectIF* executableObject, + PeriodicTaskIF* executingTask) { + this->slotList.insert(FixedSequenceSlot(componentId, slotTimeMs, + executionStep, executableObject, executingTask)); + this->current = slotList.begin(); +} + +ReturnValue_t FixedSlotSequence::checkSequence() const { + if(slotList.empty()) { + sif::error << "FixedSlotSequence::checkSequence:" + << " Slot list is empty!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + + if(customCheckFunction != nullptr) { + ReturnValue_t result = customCheckFunction(slotList); + if(result != HasReturnvaluesIF::RETURN_OK) { + // Continue for now but print error output. + sif::error << "FixedSlotSequence::checkSequence:" + << " Custom check failed!" << std::endl; + } + } + + uint32_t errorCount = 0; + uint32_t time = 0; + for(const auto& slot: slotList) { + if (slot.executableObject == nullptr) { + errorCount++; + } + else if (slot.pollingTimeMs < time) { + sif::error << "FixedSlotSequence::checkSequence: Time: " + << slot.pollingTimeMs << " is smaller than previous with " + << time << std::endl; + errorCount++; + } + else { + // All ok, print slot. + //sif::info << "Current slot polling time: " << std::endl; + //sif::info << std::dec << slotIt->pollingTimeMs << std::endl; + } + time = slot.pollingTimeMs; + + } + //sif::info << "Number of elements in slot list: " + // << slotList.size() << std::endl; + if (errorCount > 0) { + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + + +ReturnValue_t FixedSlotSequence::intializeSequenceAfterTaskCreation() const { + std::set uniqueObjects; + uint32_t count = 0; + for(const auto& slot: slotList) { + // Ensure that each unique object is initialized once. + if(uniqueObjects.find(slot.executableObject) == uniqueObjects.end()) { + ReturnValue_t result = + slot.executableObject->initializeAfterTaskCreation(); + if(result != HasReturnvaluesIF::RETURN_OK) { + count++; + } + uniqueObjects.emplace(slot.executableObject); + } + } + if (count > 0) { + sif::error << "FixedSlotSequence::intializeSequenceAfterTaskCreation:" + "Counted " << count << " failed initializations!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + return HasReturnvaluesIF::RETURN_OK; +} + +void FixedSlotSequence::addCustomCheck(ReturnValue_t + (*customCheckFunction)(const SlotList&)) { + this->customCheckFunction = customCheckFunction; +} diff --git a/fsfw/tasks/FixedSlotSequence.h b/fsfw/tasks/FixedSlotSequence.h new file mode 100644 index 0000000..077dd10 --- /dev/null +++ b/fsfw/tasks/FixedSlotSequence.h @@ -0,0 +1,181 @@ +#ifndef FSFW_TASKS_FIXEDSLOTSEQUENCE_H_ +#define FSFW_TASKS_FIXEDSLOTSEQUENCE_H_ + +#include "FixedSequenceSlot.h" +#include "../objectmanager/SystemObject.h" + +#include + +/** + * @brief This class is the representation of a + * Polling Sequence Table in software. + * @details + * The FixedSlotSequence object maintains the dynamic execution of + * objects with stricter timing requirements for the FixedTimeslotTask. + * + * The main idea is to create a list of executable objects (for example + * device handlers), to announce all handlers to the polling sequence and to + * maintain a list of polling slot objects. + * This slot list represents the Polling Sequence Table in software. + * + * Each polling slot contains information to indicate when and + * which executable object shall be executed within a given polling period. + * When adding a slot, a pointer to the executing task, a pointer to the + * executable object and a step number can be passed. The step number will be + * passed to the periodic handler. + * The sequence is executed by iterating through the slot sequence and + * executing the executable object in the correct timeslot. + */ +class FixedSlotSequence { +public: + using SlotList = std::multiset; + using SlotListIter = std::multiset::iterator; + + /** + * @brief The constructor of the FixedSlotSequence object. + * @param setLength The period length, expressed in ms. + */ + FixedSlotSequence(uint32_t setLengthMs); + + /** + * @brief The destructor of the FixedSlotSequence object. + * @details + * The destructor frees all allocated memory by iterating through the + * slotList and deleting all allocated resources. + */ + virtual ~FixedSlotSequence(); + + /** + * @brief This is a method to add an PollingSlot object to slotList. + * + * @details + * Here, a polling slot object is added to the slot list. It is appended + * to the end of the list. The list is currently NOT reordered. + * Afterwards, the iterator current is set to the beginning of the list. + * @param handlerId ID of the object to add + * @param setTime + * Value between (0 to 1) * slotLengthMs, when a FixedTimeslotTask + * will be called inside the slot period. + * @param setSequenceId + * ID which can be used to distinguish different task operations. This + * value will be passed to the executable function. + * @param + * @param + */ + void addSlot(object_id_t handlerId, uint32_t setTime, int8_t setSequenceId, + ExecutableObjectIF* executableObject, + PeriodicTaskIF* executingTask); + + /** + * @brief Checks if the current slot shall be executed immediately + * after the one before. + * @details + * This allows to distinguish between grouped and separated handlers. + * @return - @c true if the slot has the same polling time as the previous + * - @c false else + */ + bool slotFollowsImmediately(); + + /** + * @brief This method returns the time until the next software + * component is invoked. + * + * @details + * This method is vitally important for the operation of the PST. + * By fetching the polling time of the current slot and that of the + * next one (or the first one, if the list end is reached) + * it calculates and returns the interval in milliseconds within + * which the handler execution shall take place. + * If the next slot has the same time as the current one, it is ignored + * until a slot with different time or the end of the PST is found. + */ + uint32_t getIntervalToNextSlotMs(); + + /** + * @brief This method returns the time difference between the current + * slot and the previous slot + * + * @details + * This method is vitally important for the operation of the PST. + * By fetching the polling time of the current slot and that of the previous + * one (or the last one, if the slot is the first one) it calculates and + * returns the interval in milliseconds that the handler execution shall + * be delayed. + */ + uint32_t getIntervalToPreviousSlotMs(); + + /** + * @brief This method returns the length of this FixedSlotSequence instance. + */ + uint32_t getLengthMs() const; + + /** + * @brief The method to execute the device handler entered in the current + * PollingSlot object. + * + * @details + * Within this method the device handler object to be executed is chosen by + * looking up the handler address of the current slot in the handlerMap. + * Either the device handler's talkToInterface or its listenToInterface + * method is invoked, depending on the isTalking flag of the polling slot. + * After execution the iterator current is increased or, by reaching the + * end of slotList, reset to the beginning. + */ + void executeAndAdvance(); + + /** + * @brief An iterator that indicates the current polling slot to execute. + * + * @details This is an iterator for slotList and always points to the + * polling slot which is executed next. + */ + SlotListIter current; + + /** + * @brief Check and initialize slot list. + * @details + * Checks if timing is ok (must be ascending) and if all handlers were found. + * @return + */ + ReturnValue_t checkSequence() const; + + /** + * @brief A custom check can be injected for the respective slot list. + * @details + * This can be used by the developer to check the validity of a certain + * sequence. The function will be run in the #checkSequence function. + * The general check will be continued for now if the custom check function + * fails but a diagnostic debug output will be given. + * @param customCheckFunction + */ + void addCustomCheck(ReturnValue_t (*customCheckFunction)(const SlotList &)); + + /** + * @brief Perform any initialization steps required after the executing + * task has been created. This function should be called from the + * executing task! + * @return + */ + ReturnValue_t intializeSequenceAfterTaskCreation() const; + +protected: + + /** + * @brief This list contains all PollingSlot objects, defining order and + * execution time of the device handler objects. + * + * @details + * The slot list is a std:list object that contains all created + * PollingSlot instances. They are NOT ordered automatically, so by + * adding entries, the correct order needs to be ensured. By iterating + * through this list the polling sequence is executed. Two entries with + * identical polling times are executed immediately one after another. + */ + SlotList slotList; + + ReturnValue_t (*customCheckFunction)(const SlotList&) = nullptr; + + uint32_t lengthMs; +}; + +#endif /* FSFW_TASKS_FIXEDSLOTSEQUENCE_H_ */ diff --git a/fsfw/tasks/FixedTimeslotTaskIF.h b/fsfw/tasks/FixedTimeslotTaskIF.h new file mode 100644 index 0000000..421978f --- /dev/null +++ b/fsfw/tasks/FixedTimeslotTaskIF.h @@ -0,0 +1,19 @@ +#ifndef FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ +#define FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ + +#include "../objectmanager/ObjectManagerIF.h" +#include "PeriodicTaskIF.h" + +/** + * Following the same principle as the base class IF. This is the interface for a Fixed timeslot task + */ +class FixedTimeslotTaskIF : public PeriodicTaskIF { +public: + virtual ~FixedTimeslotTaskIF() {} + virtual ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs, int8_t executionStep) = 0; + virtual ReturnValue_t checkSequence() const = 0; +}; + + + +#endif /* FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ */ diff --git a/fsfw/tasks/PeriodicTaskIF.h b/fsfw/tasks/PeriodicTaskIF.h new file mode 100644 index 0000000..4df3764 --- /dev/null +++ b/fsfw/tasks/PeriodicTaskIF.h @@ -0,0 +1,49 @@ +#ifndef FRAMEWORK_TASK_PERIODICTASKIF_H_ +#define FRAMEWORK_TASK_PERIODICTASKIF_H_ + +#include "../objectmanager/SystemObjectIF.h" +#include "../timemanager/Clock.h" +#include +class ExecutableObjectIF; + +/** + * New version of TaskIF + * Follows RAII principles, i.e. there's no create or delete method. + * Minimalistic. +*/ +class PeriodicTaskIF { +public: + static const size_t MINIMUM_STACK_SIZE; + /** + * @brief A virtual destructor as it is mandatory for interfaces. + */ + virtual ~PeriodicTaskIF() { } + /** + * @brief With the startTask method, a created task can be started + * for the first time. + */ + virtual ReturnValue_t startTask() = 0; + + /** + * Add a component (object) to a periodic task. The pointer to the + * task can be set optionally + * @param object + * Add an object to the task. The most important case is to add an + * executable object with a function which will be called regularly + * (see ExecutableObjectIF) + * @param setTaskIF + * Can be used to specify whether the task object pointer is passed + * to the component. + * @return + */ + virtual ReturnValue_t addComponent(object_id_t object) { + return HasReturnvaluesIF::RETURN_FAILED; + }; + + virtual ReturnValue_t sleepFor(uint32_t ms) = 0; + + virtual uint32_t getPeriodMs() const = 0; +}; + + +#endif /* PERIODICTASKIF_H_ */ diff --git a/fsfw/tasks/SemaphoreFactory.h b/fsfw/tasks/SemaphoreFactory.h new file mode 100644 index 0000000..01c09d1 --- /dev/null +++ b/fsfw/tasks/SemaphoreFactory.h @@ -0,0 +1,50 @@ +#ifndef FSFW_TASKS_SEMAPHOREFACTORY_H_ +#define FSFW_TASKS_SEMAPHOREFACTORY_H_ + +#include "../tasks/SemaphoreIF.h" + +/** + * Creates Semaphore. + * This class is a "singleton" interface, i.e. it provides an + * interface, but also is the base class for a singleton. + */ +class SemaphoreFactory { +public: + virtual ~SemaphoreFactory(); + /** + * Returns the single instance of SemaphoreFactory. + * The implementation of #instance is found in its subclasses. + * Thus, we choose link-time variability of the instance. + */ + static SemaphoreFactory* instance(); + + /** + * Create a binary semaphore. + * Creator function for a binary semaphore which may only be acquired once + * @param argument Can be used to pass implementation specific information. + * @return Pointer to newly created semaphore class instance. + */ + SemaphoreIF* createBinarySemaphore(uint32_t arguments = 0); + /** + * Create a counting semaphore. + * Creator functons for a counting semaphore which may be acquired multiple + * times. + * @param count Semaphore can be taken count times. + * @param initCount Initial count value. + * @param argument Can be used to pass implementation specific information. + * @return + */ + SemaphoreIF* createCountingSemaphore(const uint8_t maxCount, + uint8_t initCount, uint32_t arguments = 0); + + void deleteSemaphore(SemaphoreIF* semaphore); + +private: + /** + * External instantiation is not allowed. + */ + SemaphoreFactory(); + static SemaphoreFactory* factoryInstance; +}; + +#endif /* FSFW_TASKS_SEMAPHOREFACTORY_H_ */ diff --git a/fsfw/tasks/SemaphoreIF.h b/fsfw/tasks/SemaphoreIF.h new file mode 100644 index 0000000..dd327e2 --- /dev/null +++ b/fsfw/tasks/SemaphoreIF.h @@ -0,0 +1,68 @@ +#ifndef FRAMEWORK_TASKS_SEMAPHOREIF_H_ +#define FRAMEWORK_TASKS_SEMAPHOREIF_H_ +#include "../returnvalues/FwClassIds.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +/** + * @brief Generic interface for semaphores, which can be used to achieve + * task synchronization. This is a generic interface which can be + * used for both binary semaphores and counting semaphores. + * @details + * A semaphore is a synchronization primitive. + * See: https://en.wikipedia.org/wiki/Semaphore_(programming) + * A semaphore can be used to achieve task synchonization and track the + * availability of resources by using either the binary or the counting + * semaphore types. + * + * If mutual exlcusion of a resource is desired, a mutex should be used, + * which is a special form of a semaphore and has an own interface. + */ +class SemaphoreIF { +public: + /** + * Different types of timeout for the mutex lock. + */ + enum TimeoutType { + POLLING, //!< If mutex is not available, return immediately + WAITING, //!< Wait a specified time for the mutex to become available + BLOCKING //!< Block indefinitely until the mutex becomes available. + }; + + virtual~ SemaphoreIF() {}; + + static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF; + //! Semaphore timeout + static constexpr ReturnValue_t SEMAPHORE_TIMEOUT = MAKE_RETURN_CODE(1); + //! The current semaphore can not be given, because it is not owned + static constexpr ReturnValue_t SEMAPHORE_NOT_OWNED = MAKE_RETURN_CODE(2); + static constexpr ReturnValue_t SEMAPHORE_INVALID = MAKE_RETURN_CODE(3); + + /** + * Generic call to acquire a semaphore. + * If there are no more semaphores to be taken (for a counting semaphore, + * a semaphore may be taken more than once), the taks will block + * for a maximum of timeoutMs while trying to acquire the semaphore. + * This can be used to achieve task synchrnization. + * @param timeoutMs + * @return - c RETURN_OK for successfull acquisition + */ + virtual ReturnValue_t acquire(TimeoutType timeoutType = + TimeoutType::BLOCKING, uint32_t timeoutMs = 0) = 0; + + /** + * Corrensponding call to release a semaphore. + * @return -@c RETURN_OK for successfull release + */ + virtual ReturnValue_t release() = 0; + + /** + * If the semaphore is a counting semaphore then the semaphores current + * count value is returned. If the semaphore is a binary semaphore then 1 + * is returned if the semaphore is available, and 0 is returned if the + * semaphore is not available. + */ + virtual uint8_t getSemaphoreCounter() const = 0; +}; + +#endif /* FRAMEWORK_TASKS_SEMAPHOREIF_H_ */ diff --git a/fsfw/tasks/TaskFactory.h b/fsfw/tasks/TaskFactory.h new file mode 100644 index 0000000..cbf2272 --- /dev/null +++ b/fsfw/tasks/TaskFactory.h @@ -0,0 +1,72 @@ +#ifndef FRAMEWORK_TASKS_TASKFACTORY_H_ +#define FRAMEWORK_TASKS_TASKFACTORY_H_ + +#include +#include "FixedTimeslotTaskIF.h" +#include "Typedef.h" + +/** + * Singleton Class that produces Tasks. + */ +class TaskFactory { +public: + virtual ~TaskFactory(); + /** + * Returns the single instance of TaskFactory. + * The implementation of #instance is found in its subclasses. + * Thus, we choose link-time variability of the instance. + */ + static TaskFactory* instance(); + + /** + * Creates a new periodic task and returns the interface pointer. + * @param name_ Name of the task + * @param taskPriority_ Priority of the task + * @param stackSize_ Stack Size of the task + * @param period_ Period of the task + * @param deadLineMissedFunction_ Function to be called if a deadline was missed + * @return PeriodicTaskIF* Pointer to the newly created Task + */ + PeriodicTaskIF* createPeriodicTask(TaskName name_, + TaskPriority taskPriority_, TaskStackSize stackSize_, + TaskPeriod periodInSeconds_, + TaskDeadlineMissedFunction deadLineMissedFunction_); + + /** + * + * @param name_ Name of the task + * @param taskPriority_ Priority of the task + * @param stackSize_ Stack Size of the task + * @param period_ Period of the task + * @param deadLineMissedFunction_ Function to be called if a deadline was missed + * @return FixedTimeslotTaskIF* Pointer to the newly created Task + */ + FixedTimeslotTaskIF* createFixedTimeslotTask(TaskName name_, + TaskPriority taskPriority_, TaskStackSize stackSize_, + TaskPeriod periodInSeconds_, + TaskDeadlineMissedFunction deadLineMissedFunction_); + + /** + * Function to be called to delete a task + * @param task The pointer to the task that shall be deleted, NULL specifies current Task + * @return Success of deletion + */ + static ReturnValue_t deleteTask(PeriodicTaskIF* task = NULL); + + /** + * Function to be called to delay current task + * @param delay The delay in milliseconds + * @return Success of deletion + */ + static ReturnValue_t delayTask(uint32_t delayMs); + +private: + /** + * External instantiation is not allowed. + */ + TaskFactory(); + static TaskFactory* factoryInstance; + +}; + +#endif /* FRAMEWORK_TASKS_TASKFACTORY_H_ */ diff --git a/fsfw/tasks/Typedef.h b/fsfw/tasks/Typedef.h new file mode 100644 index 0000000..07d96b0 --- /dev/null +++ b/fsfw/tasks/Typedef.h @@ -0,0 +1,10 @@ +#ifndef FRAMEWORK_TASKS_TYPEDEF_H_ +#define FRAMEWORK_TASKS_TYPEDEF_H_ + +typedef const char* TaskName; +typedef uint8_t TaskPriority; +typedef size_t TaskStackSize; +typedef double TaskPeriod; +typedef void (*TaskDeadlineMissedFunction)(); + +#endif /* FRAMEWORK_TASKS_TYPEDEF_H_ */ diff --git a/fsfw/tcdistribution/CCSDSDistributor.cpp b/fsfw/tcdistribution/CCSDSDistributor.cpp new file mode 100644 index 0000000..ddd6330 --- /dev/null +++ b/fsfw/tcdistribution/CCSDSDistributor.cpp @@ -0,0 +1,86 @@ +#include "CCSDSDistributor.h" + +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tmtcpacket/SpacePacketBase.h" + +CCSDSDistributor::CCSDSDistributor(uint16_t setDefaultApid, + object_id_t setObjectId): + TcDistributor(setObjectId), defaultApid( setDefaultApid ) { +} + +CCSDSDistributor::~CCSDSDistributor() {} + +TcDistributor::TcMqMapIter CCSDSDistributor::selectDestination() { +// sif::debug << "CCSDSDistributor::selectDestination received: " << +// this->currentMessage.getStorageId().pool_index << ", " << +// this->currentMessage.getStorageId().packet_index << std::endl; + const uint8_t* packet = nullptr; + size_t size = 0; + ReturnValue_t result = this->tcStore->getData(currentMessage.getStorageId(), + &packet, &size ); + if(result != HasReturnvaluesIF::RETURN_OK) { + sif::error << "CCSDSDistributor::selectDestination: Getting data from" + " store failed!" << std::endl; + } + SpacePacketBase currentPacket(packet); + +// sif:: info << "CCSDSDistributor::selectDestination has packet with APID " +// << std::hex << currentPacket.getAPID() << std::dec << std::endl; + TcMqMapIter position = this->queueMap.find(currentPacket.getAPID()); + if ( position != this->queueMap.end() ) { + return position; + } else { + //The APID was not found. Forward packet to main SW-APID anyway to + // create acceptance failure report. + return this->queueMap.find( this->defaultApid ); + } +} + +MessageQueueId_t CCSDSDistributor::getRequestQueue() { + return tcQueue->getId(); +} + +ReturnValue_t CCSDSDistributor::registerApplication( + AcceptsTelecommandsIF* application) { + ReturnValue_t returnValue = RETURN_OK; + auto insertPair = this->queueMap.emplace(application->getIdentifier(), + application->getRequestQueue()); + if(not insertPair.second) { + returnValue = RETURN_FAILED; + } + return returnValue; +} + +ReturnValue_t CCSDSDistributor::registerApplication(uint16_t apid, + MessageQueueId_t id) { + ReturnValue_t returnValue = RETURN_OK; + auto insertPair = this->queueMap.emplace(apid, id); + if(not insertPair.second) { + returnValue = RETURN_FAILED; + } + return returnValue; + +} + +uint16_t CCSDSDistributor::getIdentifier() { + return 0; +} + +ReturnValue_t CCSDSDistributor::initialize() { + ReturnValue_t status = this->TcDistributor::initialize(); + this->tcStore = objectManager->get( objects::TC_STORE ); + if (this->tcStore == nullptr) { + sif::error << "CCSDSDistributor::initialize: Could not initialize" + " TC store!" << std::endl; + status = RETURN_FAILED; + } + return status; +} + +ReturnValue_t CCSDSDistributor::callbackAfterSending( + ReturnValue_t queueStatus) { + if (queueStatus != RETURN_OK) { + tcStore->deleteData(currentMessage.getStorageId()); + } + return RETURN_OK; +} diff --git a/fsfw/tcdistribution/CCSDSDistributor.h b/fsfw/tcdistribution/CCSDSDistributor.h new file mode 100644 index 0000000..e8d54c9 --- /dev/null +++ b/fsfw/tcdistribution/CCSDSDistributor.h @@ -0,0 +1,71 @@ +#ifndef FRAMEWORK_TCDISTRIBUTION_CCSDSDISTRIBUTOR_H_ +#define FRAMEWORK_TCDISTRIBUTION_CCSDSDISTRIBUTOR_H_ + +#include "../objectmanager/ObjectManagerIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tcdistribution/CCSDSDistributorIF.h" +#include "../tcdistribution/TcDistributor.h" +#include "../tmtcservices/AcceptsTelecommandsIF.h" + +/** + * @brief An instantiation of the CCSDSDistributorIF. + * @details + * It receives Space Packets, and selects a destination depending on the + * APID of the telecommands. + * The Secondary Header (with Service/Subservice) is ignored. + * @ingroup tc_distribution + */ +class CCSDSDistributor : public TcDistributor, + public CCSDSDistributorIF, + public AcceptsTelecommandsIF { +public: + /** + * @brief The constructor sets the default APID and calls the + * TcDistributor ctor with a certain object id. + * @details + * @c tcStore is set in the @c initialize method. + * @param setDefaultApid The default APID, where packets with unknown + * destination are sent to. + */ + CCSDSDistributor(uint16_t setDefaultApid, object_id_t setObjectId); + /** + * The destructor is empty. + */ + virtual ~CCSDSDistributor(); + + MessageQueueId_t getRequestQueue() override; + ReturnValue_t registerApplication( uint16_t apid, + MessageQueueId_t id) override; + ReturnValue_t registerApplication( + AcceptsTelecommandsIF* application) override; + uint16_t getIdentifier() override; + ReturnValue_t initialize() override; + +protected: + /** + * This implementation checks if an application with fitting APID has + * registered and forwards the packet to the according message queue. + * If the packet is not found, it returns the queue to @c defaultApid, + * where a Acceptance Failure message should be generated. + * @return Iterator to map entry of found APID or iterator to default APID. + */ + TcMqMapIter selectDestination() override; + /** + * The callback here handles the generation of acceptance + * success/failure messages. + */ + ReturnValue_t callbackAfterSending( ReturnValue_t queueStatus ) override; + + /** + * The default APID, where packets with unknown APID are sent to. + */ + uint16_t defaultApid; + /** + * A reference to the TC storage must be maintained, as this class handles + * pure Space Packets and there exists no SpacePacketStored class. + */ + StorageManagerIF* tcStore = nullptr; + +}; + +#endif /* FRAMEWORK_TCDISTRIBUTION_CCSDSDISTRIBUTOR_H_ */ diff --git a/fsfw/tcdistribution/CCSDSDistributorIF.h b/fsfw/tcdistribution/CCSDSDistributorIF.h new file mode 100644 index 0000000..6334a35 --- /dev/null +++ b/fsfw/tcdistribution/CCSDSDistributorIF.h @@ -0,0 +1,44 @@ +#ifndef FSFW_TCDISTRIBUTION_CCSDSDISTRIBUTORIF_H_ +#define FSFW_TCDISTRIBUTION_CCSDSDISTRIBUTORIF_H_ + +#include "../tmtcservices/AcceptsTelecommandsIF.h" +#include "../ipc/MessageQueueSenderIF.h" +/** + * This is the Interface to a CCSDS Distributor. + * On a CCSDS Distributor, Applications (in terms of CCSDS) may register + * themselves, either by passing a pointer to themselves (and implementing the + * CCSDSApplicationIF), or by explicitly passing an APID and a MessageQueueId + * to route the TC's to. + * @ingroup tc_distribution + */ +class CCSDSDistributorIF { +public: + /** + * With this call, a class implementing the CCSDSApplicationIF can register + * at the distributor. + * @param application A pointer to the Application to register. + * @return - @c RETURN_OK on success, + * - @c RETURN_FAILED on failure. + */ + virtual ReturnValue_t registerApplication( + AcceptsTelecommandsIF* application) = 0; + /** + * With this call, other Applications can register to the CCSDS distributor. + * This is done by passing an APID and a MessageQueueId to the method. + * @param apid The APID to register. + * @param id The MessageQueueId of the message queue to send the + * TC Packets to. + * @return - @c RETURN_OK on success, + * - @c RETURN_FAILED on failure. + */ + virtual ReturnValue_t registerApplication( uint16_t apid, + MessageQueueId_t id) = 0; + /** + * The empty virtual destructor. + */ + virtual ~CCSDSDistributorIF() { + } +}; + + +#endif /* FSFW_TCDISTRIBUTION_CCSDSDISTRIBUTORIF_H_ */ diff --git a/fsfw/tcdistribution/PUSDistributor.cpp b/fsfw/tcdistribution/PUSDistributor.cpp new file mode 100644 index 0000000..d964202 --- /dev/null +++ b/fsfw/tcdistribution/PUSDistributor.cpp @@ -0,0 +1,113 @@ +#include "CCSDSDistributorIF.h" +#include "PUSDistributor.h" + +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tmtcpacket/pus/TcPacketStored.h" +#include "../tmtcservices/PusVerificationReport.h" + +PUSDistributor::PUSDistributor(uint16_t setApid, object_id_t setObjectId, + object_id_t setPacketSource) : + TcDistributor(setObjectId), checker(setApid), verifyChannel(), + tcStatus(RETURN_FAILED), packetSource(setPacketSource) {} + +PUSDistributor::~PUSDistributor() {} + +PUSDistributor::TcMqMapIter PUSDistributor::selectDestination() { + // sif:: debug << "PUSDistributor::handlePacket received: " + // << this->current_packet_id.store_index << ", " + // << this->current_packet_id.packet_index << std::endl; + TcMqMapIter queueMapIt = this->queueMap.end(); + if(this->currentPacket == nullptr) { + return queueMapIt; + } + this->currentPacket->setStoreAddress(this->currentMessage.getStorageId()); + if (currentPacket->getWholeData() != nullptr) { + tcStatus = checker.checkPacket(currentPacket); +#ifdef DEBUG + if(tcStatus != HasReturnvaluesIF::RETURN_OK) { + sif::debug << "PUSDistributor::handlePacket: Packet format " + << "invalid, code "<< static_cast(tcStatus) + << std::endl; + } +#endif + uint32_t queue_id = currentPacket->getService(); + queueMapIt = this->queueMap.find(queue_id); + } + else { + tcStatus = PACKET_LOST; + } + + if (queueMapIt == this->queueMap.end()) { + tcStatus = DESTINATION_NOT_FOUND; +#ifdef DEBUG + sif::debug << "PUSDistributor::handlePacket: Destination not found, " + << "code "<< static_cast(tcStatus) << std::endl; +#endif + } + + if (tcStatus != RETURN_OK) { + return this->queueMap.end(); + } + else { + return queueMapIt; + } + +} + + +ReturnValue_t PUSDistributor::registerService(AcceptsTelecommandsIF* service) { + uint16_t serviceId = service->getIdentifier(); + // sif::info << "Service ID: " << (int)serviceId << std::endl; + MessageQueueId_t queue = service->getRequestQueue(); + auto returnPair = queueMap.emplace(serviceId, queue); + if (not returnPair.second) { + sif::error << "PUSDistributor::registerService: Service ID already" + " exists in map." << std::endl; + return SERVICE_ID_ALREADY_EXISTS; + } + return HasReturnvaluesIF::RETURN_OK; +} + +MessageQueueId_t PUSDistributor::getRequestQueue() { + return tcQueue->getId(); +} + +ReturnValue_t PUSDistributor::callbackAfterSending(ReturnValue_t queueStatus) { + if (queueStatus != RETURN_OK) { + tcStatus = queueStatus; + } + if (tcStatus != RETURN_OK) { + this->verifyChannel.sendFailureReport(TC_VERIFY::ACCEPTANCE_FAILURE, + currentPacket, tcStatus); + // A failed packet is deleted immediately after reporting, + // otherwise it will block memory. + currentPacket->deletePacket(); + return RETURN_FAILED; + } else { + this->verifyChannel.sendSuccessReport(TC_VERIFY::ACCEPTANCE_SUCCESS, + currentPacket); + return RETURN_OK; + } +} + +uint16_t PUSDistributor::getIdentifier() { + return checker.getApid(); +} + +ReturnValue_t PUSDistributor::initialize() { + currentPacket = new TcPacketStored(); + if(currentPacket == nullptr) { + // Should not happen, memory allocation failed! + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + CCSDSDistributorIF* ccsdsDistributor = + objectManager->get(packetSource); + if (ccsdsDistributor == nullptr) { + sif::error << "PUSDistributor::initialize: Packet source invalid." + << " Make sure it exists and implements CCSDSDistributorIF!" + << std::endl; + return RETURN_FAILED; + } + return ccsdsDistributor->registerApplication(this); +} diff --git a/fsfw/tcdistribution/PUSDistributor.h b/fsfw/tcdistribution/PUSDistributor.h new file mode 100644 index 0000000..be3804e --- /dev/null +++ b/fsfw/tcdistribution/PUSDistributor.h @@ -0,0 +1,79 @@ +#ifndef FSFW_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ +#define FSFW_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ + +#include "PUSDistributorIF.h" +#include "TcDistributor.h" +#include "TcPacketCheck.h" + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../tmtcservices/AcceptsTelecommandsIF.h" +#include "../tmtcservices/VerificationReporter.h" + +/** + * This class accepts PUS Telecommands and forwards them to Application + * services. In addition, the class performs a formal packet check and + * sends acceptance success or failure messages. + * @ingroup tc_distribution + */ +class PUSDistributor: public TcDistributor, + public PUSDistributorIF, + public AcceptsTelecommandsIF { +public: + /** + * The ctor passes @c set_apid to the checker class and calls the + * TcDistribution ctor with a certain object id. + * @param setApid The APID of this receiving Application. + * @param setObjectId Object ID of the distributor itself + * @param setPacketSource Object ID of the source of TC packets. + * Must implement CCSDSDistributorIF. + */ + PUSDistributor(uint16_t setApid, object_id_t setObjectId, + object_id_t setPacketSource); + /** + * The destructor is empty. + */ + virtual ~PUSDistributor(); + ReturnValue_t registerService(AcceptsTelecommandsIF* service) override; + MessageQueueId_t getRequestQueue() override; + ReturnValue_t initialize() override; + uint16_t getIdentifier() override; + +protected: + /** + * This attribute contains the class, that performs a formal packet check. + */ + TcPacketCheck checker; + /** + * With this class, verification messages are sent to the + * TC Verification service. + */ + VerificationReporter verifyChannel; + /** + * The currently handled packet is stored here. + */ + TcPacketStored* currentPacket = nullptr; + /** + * With this variable, the current check status is stored to generate + * acceptance messages later. + */ + ReturnValue_t tcStatus; + + const object_id_t packetSource; + + /** + * This method reads the packet service, checks if such a service is + * registered and forwards the packet to the destination. + * It also initiates the formal packet check and sending of verification + * messages. + * @return Iterator to map entry of found service id + * or iterator to @c map.end(). + */ + TcMqMapIter selectDestination() override; + /** + * The callback here handles the generation of acceptance + * success/failure messages. + */ + ReturnValue_t callbackAfterSending(ReturnValue_t queueStatus) override; +}; + +#endif /* FSFW_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ */ diff --git a/fsfw/tcdistribution/PUSDistributorIF.h b/fsfw/tcdistribution/PUSDistributorIF.h new file mode 100644 index 0000000..0125c08 --- /dev/null +++ b/fsfw/tcdistribution/PUSDistributorIF.h @@ -0,0 +1,27 @@ +#ifndef FSFW_TCDISTRIBUTION_PUSDISTRIBUTORIF_H_ +#define FSFW_TCDISTRIBUTION_PUSDISTRIBUTORIF_H_ + +#include "../tmtcservices/AcceptsTelecommandsIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +/** + * This interface allows PUS Services to register themselves at a PUS Distributor. + * @ingroup tc_distribution + */ +class PUSDistributorIF { +public: + /** + * The empty virtual destructor. + */ + virtual ~PUSDistributorIF() { + } +/** + * With this method, Services can register themselves at the PUS Distributor. + * @param service A pointer to the registering Service. + * @return - @c RETURN_OK on success, + * - @c RETURN_FAILED on failure. + */ + virtual ReturnValue_t registerService( AcceptsTelecommandsIF* service ) = 0; +}; + +#endif /* FSFW_TCDISTRIBUTION_PUSDISTRIBUTORIF_H_ */ diff --git a/fsfw/tcdistribution/TcDistributor.cpp b/fsfw/tcdistribution/TcDistributor.cpp new file mode 100644 index 0000000..06e1817 --- /dev/null +++ b/fsfw/tcdistribution/TcDistributor.cpp @@ -0,0 +1,55 @@ +#include "TcDistributor.h" + +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tmtcservices/TmTcMessage.h" +#include "../ipc/QueueFactory.h" + +TcDistributor::TcDistributor(object_id_t objectId) : + SystemObject(objectId) { + tcQueue = QueueFactory::instance()-> + createMessageQueue(DISTRIBUTER_MAX_PACKETS); +} + +TcDistributor::~TcDistributor() { + QueueFactory::instance()->deleteMessageQueue(tcQueue); +} + +ReturnValue_t TcDistributor::performOperation(uint8_t opCode) { + ReturnValue_t status = RETURN_OK; + for (status = tcQueue->receiveMessage(¤tMessage); status == RETURN_OK; + status = tcQueue->receiveMessage(¤tMessage)) { + status = handlePacket(); + } + if (status == MessageQueueIF::EMPTY) { + return RETURN_OK; + } else { + return status; + } +} + +ReturnValue_t TcDistributor::handlePacket() { + + TcMqMapIter queueMapIt = this->selectDestination(); + ReturnValue_t returnValue = RETURN_FAILED; + if (queueMapIt != this->queueMap.end()) { + returnValue = this->tcQueue->sendMessage(queueMapIt->second, + &this->currentMessage); + } + return this->callbackAfterSending(returnValue); +} + +void TcDistributor::print() { + sif::debug << "Distributor content is: " << std::endl + << "ID\t| Message Queue ID" << std::endl; + sif::debug << std::setfill('0') << std::setw(8) << std::hex; + for (const auto& queueMapIter: queueMap) { + sif::debug << queueMapIter.first << "\t| 0x" << queueMapIter.second + << std::endl; + } + sif::debug << std::setfill(' ') << std::dec; + +} + +ReturnValue_t TcDistributor::callbackAfterSending(ReturnValue_t queueStatus) { + return RETURN_OK; +} diff --git a/fsfw/tcdistribution/TcDistributor.h b/fsfw/tcdistribution/TcDistributor.h new file mode 100644 index 0000000..5d0ca45 --- /dev/null +++ b/fsfw/tcdistribution/TcDistributor.h @@ -0,0 +1,122 @@ +#ifndef FSFW_TMTCSERVICES_TCDISTRIBUTOR_H_ +#define FSFW_TMTCSERVICES_TCDISTRIBUTOR_H_ + +#include "../objectmanager/ObjectManagerIF.h" +#include "../objectmanager/SystemObject.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../tmtcservices/TmTcMessage.h" +#include "../ipc/MessageQueueIF.h" +#include + +/** + * @defgroup tc_distribution Telecommand Distribution + * All classes associated with Routing and Distribution of Telecommands + * belong to this group. + */ + + +/** + * This is the base class to implement distributors for Space Packets. + * Typically, the distribution is required to forward Telecommand packets + * over the satellite applications and services. The class receives + * Space Packets over a message queue and holds a map that links other + * message queue ids to some identifier. The process of unpacking the + * destination information from the packet is handled by the child class + * implementations. + * @ingroup tc_distribution + */ +class TcDistributor : public SystemObject, + public ExecutableObjectIF, + public HasReturnvaluesIF { +public: + using TcMessageQueueMap = std::map; + using TcMqMapIter = std::map::iterator; + + static constexpr uint8_t INTERFACE_ID = CLASS_ID::PACKET_DISTRIBUTION; + static constexpr ReturnValue_t PACKET_LOST = MAKE_RETURN_CODE( 1 ); + static constexpr ReturnValue_t DESTINATION_NOT_FOUND = MAKE_RETURN_CODE( 2 ); + static constexpr ReturnValue_t SERVICE_ID_ALREADY_EXISTS = MAKE_RETURN_CODE(3); + /** + * Within the default constructor, the SystemObject id is set and the + * message queue is initialized. + * Filling the map is under control of the child classes. + * @param set_object_id This id is assigned to the distributor + * implementation. + */ + TcDistributor(object_id_t objectId); + /** + * The destructor is empty, the message queues are not in the vicinity of + * this class. + */ + virtual ~TcDistributor(); + /** + * The method is called cyclically and fetches new incoming packets from + * the message queue. + * In case a new packet is found, it calls the handlePacket method to deal + * with distribution. + * @return The error code of the message queue call. + */ + ReturnValue_t performOperation(uint8_t opCode); + /** + * A simple debug print, that prints all distribution information stored in + * queueMap. + */ + void print(); + +protected: + /** + * This is the receiving queue for incoming Telecommands. + * The child classes must make its queue id public. + */ + MessageQueueIF* tcQueue = nullptr; + /** + * The last received incoming packet information is stored in this + * member. + * As different child classes unpack the incoming packet differently + * (i.e. as a CCSDS Space Packet or as a PUS Telecommand Packet), it + * is not tried to unpack the packet information within this class. + */ + TmTcMessage currentMessage; + /** + * The map that links certain packet information to a destination. + * The packet information may be the APID of the packet or the service + * identifier. Filling of the map is under control of the different child + * classes. + */ + TcMessageQueueMap queueMap; + /** + * This method shall unpack the routing information from the incoming + * packet and select the map entry which represents the packet's target. + * @return An iterator to the map element to forward to or queuMap.end(). + */ + virtual TcMqMapIter selectDestination() = 0; + /** + * The handlePacket method calls the child class's selectDestination method + * and forwards the packet to its destination, if found. + * @return The message queue return value or @c RETURN_FAILED, in case no + * destination was found. + */ + ReturnValue_t handlePacket(); + /** + * This method gives the child class a chance to perform some kind of + * operation after the parent tried to forward the message. + * A typically application would be sending success/failure messages. + * The default implementation just returns @c RETURN_OK. + * @param queueStatus The status of the message queue after an attempt + * to send the TC. + * @return - @c RETURN_OK on success + * - @c RETURN_FAILED on failure + */ + virtual ReturnValue_t callbackAfterSending( ReturnValue_t queueStatus ); + +private: + /** + * This constant sets the maximum number of packets distributed per call. + */ + static constexpr uint8_t DISTRIBUTER_MAX_PACKETS = 128; +}; + + +#endif /* FSFW_TMTCSERVICES_TCDISTRIBUTOR_H_ */ diff --git a/fsfw/tcdistribution/TcPacketCheck.cpp b/fsfw/tcdistribution/TcPacketCheck.cpp new file mode 100644 index 0000000..38ed04a --- /dev/null +++ b/fsfw/tcdistribution/TcPacketCheck.cpp @@ -0,0 +1,39 @@ +#include "TcPacketCheck.h" + +#include "../globalfunctions/CRC.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tmtcservices/VerificationCodes.h" + +TcPacketCheck::TcPacketCheck( uint16_t setApid ) : apid(setApid) { +} + +ReturnValue_t TcPacketCheck::checkPacket( TcPacketStored* currentPacket ) { + uint16_t calculated_crc = CRC::crc16ccitt( currentPacket->getWholeData(), + currentPacket->getFullSize() ); + if ( calculated_crc != 0 ) { + return INCORRECT_CHECKSUM; + } + bool condition = (not currentPacket->hasSecondaryHeader()) or + (currentPacket->getPacketVersionNumber() != CCSDS_VERSION_NUMBER) or + (not currentPacket->isTelecommand()); + if ( condition ) { + return INCORRECT_PRIMARY_HEADER; + } + if ( currentPacket->getAPID() != this->apid ) + return ILLEGAL_APID; + + if ( not currentPacket->isSizeCorrect() ) { + return INCOMPLETE_PACKET; + } + condition = (currentPacket->getSecondaryHeaderFlag() != CCSDS_SECONDARY_HEADER_FLAG) || + (currentPacket->getPusVersionNumber() != PUS_VERSION_NUMBER); + if ( condition ) { + return INCORRECT_SECONDARY_HEADER; + } + return RETURN_OK; +} + +uint16_t TcPacketCheck::getApid() const { + return apid; +} diff --git a/fsfw/tcdistribution/TcPacketCheck.h b/fsfw/tcdistribution/TcPacketCheck.h new file mode 100644 index 0000000..703bb1b --- /dev/null +++ b/fsfw/tcdistribution/TcPacketCheck.h @@ -0,0 +1,60 @@ +#ifndef FSFW_TCDISTRIBUTION_TCPACKETCHECK_H_ +#define FSFW_TCDISTRIBUTION_TCPACKETCHECK_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../tmtcpacket/pus/TcPacketStored.h" +#include "../tmtcservices/PusVerificationReport.h" + +/** + * This class performs a formal packet check for incoming PUS Telecommand Packets. + * Currently, it only checks if the APID and CRC are correct. + * @ingroup tc_distribution + */ +class TcPacketCheck : public HasReturnvaluesIF { +protected: + /** + * Describes the version number a packet must have to pass. + */ + static constexpr uint8_t CCSDS_VERSION_NUMBER = 0; + /** + * Describes the secondary header a packet must have to pass. + */ + static constexpr uint8_t CCSDS_SECONDARY_HEADER_FLAG = 0; + /** + * Describes the TC Packet PUS Version Number a packet must have to pass. + */ + static constexpr uint8_t PUS_VERSION_NUMBER = 1; + /** + * The packet id each correct packet should have. + * It is composed of the APID and some static fields. + */ + uint16_t apid; +public: + static const uint8_t INTERFACE_ID = CLASS_ID::TC_PACKET_CHECK; + static const ReturnValue_t ILLEGAL_APID = MAKE_RETURN_CODE( 0 ); + static const ReturnValue_t INCOMPLETE_PACKET = MAKE_RETURN_CODE( 1 ); + static const ReturnValue_t INCORRECT_CHECKSUM = MAKE_RETURN_CODE( 2 ); + static const ReturnValue_t ILLEGAL_PACKET_TYPE = MAKE_RETURN_CODE( 3 ); + static const ReturnValue_t ILLEGAL_PACKET_SUBTYPE = MAKE_RETURN_CODE( 4 ); + static const ReturnValue_t INCORRECT_PRIMARY_HEADER = MAKE_RETURN_CODE( 5 ); + static const ReturnValue_t INCORRECT_SECONDARY_HEADER = MAKE_RETURN_CODE( 6 ); + /** + * The constructor only sets the APID attribute. + * @param set_apid The APID to set. + */ + TcPacketCheck( uint16_t setApid ); + /** + * This is the actual method to formally check a certain Telecommand Packet. + * The packet's Application Data can not be checked here. + * @param current_packet The packt to check + * @return - @c RETURN_OK on success. + * - @c INCORRECT_CHECKSUM if checksum is invalid. + * - @c ILLEGAL_APID if APID does not match. + */ + ReturnValue_t checkPacket( TcPacketStored* currentPacket ); + + uint16_t getApid() const; +}; + + +#endif /* FSFW_TCDISTRIBUTION_TCPACKETCHECK_H_ */ diff --git a/fsfw/thermal/AbstractTemperatureSensor.cpp b/fsfw/thermal/AbstractTemperatureSensor.cpp new file mode 100644 index 0000000..45ebe4a --- /dev/null +++ b/fsfw/thermal/AbstractTemperatureSensor.cpp @@ -0,0 +1,70 @@ +#include "AbstractTemperatureSensor.h" +#include "../ipc/QueueFactory.h" + +AbstractTemperatureSensor::AbstractTemperatureSensor(object_id_t setObjectid, + ThermalModuleIF *thermalModule) : + SystemObject(setObjectid), commandQueue(NULL), healthHelper(this, + setObjectid), parameterHelper(this) { + if (thermalModule != NULL) { + thermalModule->registerSensor(this); + } + commandQueue = QueueFactory::instance()->createMessageQueue(); +} + +AbstractTemperatureSensor::~AbstractTemperatureSensor() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); +} + +MessageQueueId_t AbstractTemperatureSensor::getCommandQueue() const { + return commandQueue->getId(); +} + +ReturnValue_t AbstractTemperatureSensor::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = healthHelper.initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = parameterHelper.initialize(); + return result; +} + +ReturnValue_t AbstractTemperatureSensor::performOperation(uint8_t opCode) { + handleCommandQueue(); + doChildOperation(); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t AbstractTemperatureSensor::performHealthOp() { + handleCommandQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + +void AbstractTemperatureSensor::handleCommandQueue() { + CommandMessage message; + ReturnValue_t result = commandQueue->receiveMessage(&message); + if (result == HasReturnvaluesIF::RETURN_OK) { + result = healthHelper.handleHealthCommand(&message); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + result = parameterHelper.handleParameterMessage(&message); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + message.setToUnknownCommand(); + commandQueue->reply(&message); + } +} + +ReturnValue_t AbstractTemperatureSensor::setHealth(HealthState health) { + healthHelper.setHealth(health); + return HasReturnvaluesIF::RETURN_OK; +} + +HasHealthIF::HealthState AbstractTemperatureSensor::getHealth() { + return healthHelper.getHealth(); +} diff --git a/fsfw/thermal/AbstractTemperatureSensor.h b/fsfw/thermal/AbstractTemperatureSensor.h new file mode 100644 index 0000000..726ab9f --- /dev/null +++ b/fsfw/thermal/AbstractTemperatureSensor.h @@ -0,0 +1,54 @@ +#ifndef ABSTRACTSENSOR_H_ +#define ABSTRACTSENSOR_H_ + +#include "../health/HasHealthIF.h" +#include "../health/HealthHelper.h" +#include "../objectmanager/SystemObject.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../parameters/ParameterHelper.h" +#include "../ipc/MessageQueueIF.h" +#include "ThermalModuleIF.h" +#include "tcsDefinitions.h" + +class AbstractTemperatureSensor: public HasHealthIF, + public SystemObject, + public ExecutableObjectIF, + public ReceivesParameterMessagesIF { +public: + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::T_SENSORS; + static const Event TEMP_SENSOR_HIGH = MAKE_EVENT(0, SEVERITY::LOW); + static const Event TEMP_SENSOR_LOW = MAKE_EVENT(1, SEVERITY::LOW); + static const Event TEMP_SENSOR_GRADIENT = MAKE_EVENT(2, SEVERITY::LOW); + + static constexpr float ZERO_KELVIN_C = -273.15; + AbstractTemperatureSensor(object_id_t setObjectid, + ThermalModuleIF *thermalModule); + virtual ~AbstractTemperatureSensor(); + + virtual MessageQueueId_t getCommandQueue() const; + + ReturnValue_t initialize(); + + ReturnValue_t performHealthOp(); + + ReturnValue_t performOperation(uint8_t opCode); + + virtual float getTemperature() = 0; + virtual bool isValid() = 0; + + virtual void resetOldState() = 0; + + ReturnValue_t setHealth(HealthState health); + HasHealthIF::HealthState getHealth(); +protected: + MessageQueueIF* commandQueue; + HealthHelper healthHelper; + ParameterHelper parameterHelper; + + virtual void doChildOperation() = 0; + + void handleCommandQueue(); +}; + +#endif /* ABSTRACTSENSOR_H_ */ diff --git a/fsfw/thermal/CoreComponent.cpp b/fsfw/thermal/CoreComponent.cpp new file mode 100644 index 0000000..304712e --- /dev/null +++ b/fsfw/thermal/CoreComponent.cpp @@ -0,0 +1,257 @@ +#include "CoreComponent.h" + +CoreComponent::CoreComponent(object_id_t reportingObjectId, uint8_t domainId, + uint32_t temperaturePoolId, uint32_t targetStatePoolId, + uint32_t currentStatePoolId, uint32_t requestPoolId, DataSet* dataSet, + AbstractTemperatureSensor* sensor, + AbstractTemperatureSensor* firstRedundantSensor, + AbstractTemperatureSensor* secondRedundantSensor, + ThermalModuleIF* thermalModule, Parameters parameters, + Priority priority, StateRequest initialTargetState) : + sensor(sensor), firstRedundantSensor(firstRedundantSensor), secondRedundantSensor( + secondRedundantSensor), thermalModule(thermalModule), temperature( + temperaturePoolId, dataSet, PoolVariableIF::VAR_WRITE), targetState( + targetStatePoolId, dataSet, PoolVariableIF::VAR_READ), currentState( + currentStatePoolId, dataSet, PoolVariableIF::VAR_WRITE), heaterRequest( + requestPoolId, dataSet, PoolVariableIF::VAR_WRITE), isHeating( + false), isSafeComponent(priority == SAFE), minTemp(999), maxTemp( + AbstractTemperatureSensor::ZERO_KELVIN_C), parameters( + parameters), temperatureMonitor(reportingObjectId, + domainId + 1, + DataPool::poolIdAndPositionToPid(temperaturePoolId, 0), + COMPONENT_TEMP_CONFIRMATION), domainId(domainId) { + if (thermalModule != NULL) { + thermalModule->registerComponent(this, priority); + } + //Set thermal state once, then leave to operator. + DataSet mySet; + PoolVariable writableTargetState(targetStatePoolId, &mySet, + PoolVariableIF::VAR_WRITE); + writableTargetState = initialTargetState; + mySet.commit(PoolVariableIF::VALID); +} + +CoreComponent::~CoreComponent() { +} + +ThermalComponentIF::HeaterRequest CoreComponent::performOperation(uint8_t opCode) { + HeaterRequest request = HEATER_DONT_CARE; + //SHOULDDO: Better pass db_float_t* to getTemperature and set it invalid if invalid. + temperature = getTemperature(); + updateMinMaxTemp(); + if ((temperature != INVALID_TEMPERATURE)) { + temperature.setValid(PoolVariableIF::VALID); + State state = getState(temperature, getParameters(), targetState); + currentState = state; + checkLimits(state); + request = getHeaterRequest(targetState, temperature, getParameters()); + } else { + temperatureMonitor.setToInvalid(); + temperature.setValid(PoolVariableIF::INVALID); + currentState = UNKNOWN; + request = HEATER_DONT_CARE; + } + currentState.setValid(PoolVariableIF::VALID); + heaterRequest = request; + heaterRequest.setValid(PoolVariableIF::VALID); + return request; +} + +void CoreComponent::markStateIgnored() { + currentState = getIgnoredState(currentState); +} + +object_id_t CoreComponent::getObjectId() { + return temperatureMonitor.getReporterId(); +} + +float CoreComponent::getLowerOpLimit() { + return parameters.lowerOpLimit; +} + +ReturnValue_t CoreComponent::setTargetState(int8_t newState) { + DataSet mySet; + PoolVariable writableTargetState(targetState.getDataPoolId(), + &mySet, PoolVariableIF::VAR_READ_WRITE); + mySet.read(); + if ((writableTargetState == STATE_REQUEST_OPERATIONAL) + && (newState != STATE_REQUEST_IGNORE)) { + return HasReturnvaluesIF::RETURN_FAILED; + } + switch (newState) { + case STATE_REQUEST_HEATING: + case STATE_REQUEST_IGNORE: + case STATE_REQUEST_OPERATIONAL: + writableTargetState = newState; + break; + case STATE_REQUEST_NON_OPERATIONAL: + default: + return INVALID_TARGET_STATE; + } + mySet.commit(PoolVariableIF::VALID); + return HasReturnvaluesIF::RETURN_OK; +} + +void CoreComponent::setOutputInvalid() { + temperature = INVALID_TEMPERATURE; + temperature.setValid(PoolVariableIF::INVALID); + currentState.setValid(PoolVariableIF::INVALID); + heaterRequest = HEATER_DONT_CARE; + heaterRequest.setValid(PoolVariableIF::INVALID); + temperatureMonitor.setToUnchecked(); +} + +float CoreComponent::getTemperature() { + if ((sensor != NULL) && (sensor->isValid())) { + return sensor->getTemperature(); + } + + if ((firstRedundantSensor != NULL) && (firstRedundantSensor->isValid())) { + return firstRedundantSensor->getTemperature(); + } + + if ((secondRedundantSensor != NULL) && (secondRedundantSensor->isValid())) { + return secondRedundantSensor->getTemperature(); + } + + if (thermalModule != NULL) { + float temperature = thermalModule->getTemperature(); + if (temperature != ThermalModuleIF::INVALID_TEMPERATURE) { + return temperature; + } else { + return INVALID_TEMPERATURE; + } + } else { + return INVALID_TEMPERATURE; + } +} + +ThermalComponentIF::State CoreComponent::getState(float temperature, + Parameters parameters, int8_t targetState) { + ThermalComponentIF::State state; + + if (temperature < parameters.lowerOpLimit) { + state = NON_OPERATIONAL_LOW; + } else if (temperature < parameters.upperOpLimit) { + state = OPERATIONAL; + } else { + state = NON_OPERATIONAL_HIGH; + } + if (targetState == STATE_REQUEST_IGNORE) { + state = getIgnoredState(state); + } + + return state; +} + +void CoreComponent::checkLimits(ThermalComponentIF::State state) { + //Checks operational limits only. + temperatureMonitor.translateState(state, temperature.value, + getParameters().lowerOpLimit, getParameters().upperOpLimit); + +} + +ThermalComponentIF::HeaterRequest CoreComponent::getHeaterRequest( + int8_t targetState, float temperature, Parameters parameters) { + if (targetState == STATE_REQUEST_IGNORE) { + isHeating = false; + return HEATER_DONT_CARE; + } + + if (temperature > parameters.upperOpLimit - parameters.heaterSwitchoff) { + isHeating = false; + return HEATER_REQUEST_EMERGENCY_OFF; + } + + float opHeaterLimit = parameters.lowerOpLimit + parameters.heaterOn; + + if (isHeating) { + opHeaterLimit += parameters.hysteresis; + } + + if (temperature < opHeaterLimit) { + isHeating = true; + return HEATER_REQUEST_EMERGENCY_ON; + } + isHeating = false; + return HEATER_DONT_CARE; +} + +ThermalComponentIF::State CoreComponent::getIgnoredState(int8_t state) { + switch (state) { + case NON_OPERATIONAL_LOW: + return NON_OPERATIONAL_LOW_IGNORED; + case OPERATIONAL: + return OPERATIONAL_IGNORED; + case NON_OPERATIONAL_HIGH: + return NON_OPERATIONAL_HIGH_IGNORED; + case NON_OPERATIONAL_LOW_IGNORED: + return NON_OPERATIONAL_LOW_IGNORED; + case OPERATIONAL_IGNORED: + return OPERATIONAL_IGNORED; + case NON_OPERATIONAL_HIGH_IGNORED: + return NON_OPERATIONAL_HIGH_IGNORED; + default: + case UNKNOWN: + return UNKNOWN; + } +} + +void CoreComponent::updateMinMaxTemp() { + if (temperature == INVALID_TEMPERATURE) { + return; + } + if (temperature < minTemp) { + minTemp = temperature; + } + if (temperature > maxTemp) { + maxTemp = temperature; + } +} + +uint8_t CoreComponent::getDomainId() const { + return domainId; +} + +CoreComponent::Parameters CoreComponent::getParameters() { + return parameters; +} + +ReturnValue_t CoreComponent::getParameter(uint8_t domainId, + uint16_t parameterId, ParameterWrapper* parameterWrapper, + const ParameterWrapper* newValues, uint16_t startAtIndex) { + ReturnValue_t result = temperatureMonitor.getParameter(domainId, + parameterId, parameterWrapper, newValues, startAtIndex); + if (result != INVALID_DOMAIN_ID) { + return result; + } + if (domainId != this->domainId) { + return INVALID_DOMAIN_ID; + } + switch (parameterId) { + case 0: + parameterWrapper->set(parameters.heaterOn); + break; + case 1: + parameterWrapper->set(parameters.hysteresis); + break; + case 2: + parameterWrapper->set(parameters.heaterSwitchoff); + break; + case 3: + parameterWrapper->set(minTemp); + break; + case 4: + parameterWrapper->set(maxTemp); + break; + case 10: + parameterWrapper->set(parameters.lowerOpLimit); + break; + case 11: + parameterWrapper->set(parameters.upperOpLimit); + break; + default: + return INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/thermal/CoreComponent.h b/fsfw/thermal/CoreComponent.h new file mode 100644 index 0000000..48a49f7 --- /dev/null +++ b/fsfw/thermal/CoreComponent.h @@ -0,0 +1,95 @@ +#ifndef MISSION_CONTROLLERS_TCS_CORECOMPONENT_H_ +#define MISSION_CONTROLLERS_TCS_CORECOMPONENT_H_ + +#include "../datapool/DataSet.h" +#include "../datapool/PoolVariable.h" +#include "ThermalComponentIF.h" +#include "AbstractTemperatureSensor.h" +#include "ThermalModule.h" +#include "ThermalMonitor.h" + +class CoreComponent: public ThermalComponentIF { +public: + struct Parameters { + float lowerOpLimit; + float upperOpLimit; + float heaterOn; + float hysteresis; + float heaterSwitchoff; + }; + + static const uint16_t COMPONENT_TEMP_CONFIRMATION = 5; + + CoreComponent(object_id_t reportingObjectId, uint8_t domainId, uint32_t temperaturePoolId, + uint32_t targetStatePoolId, uint32_t currentStatePoolId, + uint32_t requestPoolId, DataSet *dataSet, + AbstractTemperatureSensor *sensor, + AbstractTemperatureSensor *firstRedundantSensor, + AbstractTemperatureSensor *secondRedundantSensor, + ThermalModuleIF *thermalModule, Parameters parameters, + Priority priority, StateRequest initialTargetState = + ThermalComponentIF::STATE_REQUEST_OPERATIONAL); + + virtual ~CoreComponent(); + + virtual HeaterRequest performOperation(uint8_t opCode); + + void markStateIgnored(); + + object_id_t getObjectId(); + + uint8_t getDomainId() const; + + virtual float getLowerOpLimit(); + + ReturnValue_t setTargetState(int8_t newState); + + virtual void setOutputInvalid(); + + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); + +protected: + + AbstractTemperatureSensor *sensor; + AbstractTemperatureSensor *firstRedundantSensor; + AbstractTemperatureSensor *secondRedundantSensor; + ThermalModuleIF *thermalModule; + + db_float_t temperature; + db_int8_t targetState; + db_int8_t currentState; + db_uint8_t heaterRequest; + + bool isHeating; + + bool isSafeComponent; + + float minTemp; + + float maxTemp; + + Parameters parameters; + + ThermalMonitor temperatureMonitor; + + const uint8_t domainId; + + virtual float getTemperature(); + virtual State getState(float temperature, Parameters parameters, + int8_t targetState); + + virtual void checkLimits(State state); + + virtual HeaterRequest getHeaterRequest(int8_t targetState, + float temperature, Parameters parameters); + + virtual State getIgnoredState(int8_t state); + + void updateMinMaxTemp(); + + virtual Parameters getParameters(); +}; + +#endif /* MISSION_CONTROLLERS_TCS_CORECOMPONENT_H_ */ diff --git a/fsfw/thermal/Heater.cpp b/fsfw/thermal/Heater.cpp new file mode 100644 index 0000000..1301e2e --- /dev/null +++ b/fsfw/thermal/Heater.cpp @@ -0,0 +1,350 @@ +#include "../devicehandlers/DeviceHandlerFailureIsolation.h" +#include "Heater.h" + +#include "../power/Fuse.h" +#include "../ipc/QueueFactory.h" + +Heater::Heater(uint32_t objectId, uint8_t switch0, uint8_t switch1) : + HealthDevice(objectId, 0), internalState(STATE_OFF), powerSwitcher( + NULL), pcduQueueId(0), switch0(switch0), switch1(switch1), wasOn( + false), timedOut(false), reactedToBeingFaulty(false), passive( + false), eventQueue(NULL), heaterOnCountdown(10800000)/*about two orbits*/, parameterHelper( + this), lastAction(CLEAR) { + eventQueue = QueueFactory::instance()->createMessageQueue(); +} + +Heater::~Heater() { + QueueFactory::instance()->deleteMessageQueue(eventQueue); +} + +ReturnValue_t Heater::set() { + passive = false; + //wait for clear before doing anything + if (internalState == STATE_WAIT) { + return HasReturnvaluesIF::RETURN_OK; + } + if (healthHelper.healthTable->isHealthy(getObjectId())) { + doAction(SET); + if ((internalState == STATE_OFF) || (internalState == STATE_PASSIVE)){ + return HasReturnvaluesIF::RETURN_FAILED; + } else { + return HasReturnvaluesIF::RETURN_OK; + } + } else { + if (healthHelper.healthTable->isFaulty(getObjectId())) { + if (!reactedToBeingFaulty) { + reactedToBeingFaulty = true; + doAction(CLEAR); + } + } + return HasReturnvaluesIF::RETURN_FAILED; + } +} + +void Heater::clear(bool passive) { + this->passive = passive; + //Force switching off + if (internalState == STATE_WAIT) { + internalState = STATE_ON; + } + if (healthHelper.healthTable->isHealthy(getObjectId())) { + doAction(CLEAR); + } else if (healthHelper.healthTable->isFaulty(getObjectId())) { + if (!reactedToBeingFaulty) { + reactedToBeingFaulty = true; + doAction(CLEAR); + } + } +} + +void Heater::doAction(Action action) { + //only act if we are not in the right state or in a transition + if (action == SET) { + if ((internalState == STATE_OFF) || (internalState == STATE_PASSIVE) + || (internalState == STATE_EXTERNAL_CONTROL)) { + switchCountdown.setTimeout(powerSwitcher->getSwitchDelayMs()); + internalState = STATE_WAIT_FOR_SWITCHES_ON; + powerSwitcher->sendSwitchCommand(switch0, PowerSwitchIF::SWITCH_ON); + powerSwitcher->sendSwitchCommand(switch1, PowerSwitchIF::SWITCH_ON); + } + } else { //clear + if ((internalState == STATE_ON) || (internalState == STATE_FAULTY) + || (internalState == STATE_EXTERNAL_CONTROL)) { + internalState = STATE_WAIT_FOR_SWITCHES_OFF; + switchCountdown.setTimeout(powerSwitcher->getSwitchDelayMs()); + powerSwitcher->sendSwitchCommand(switch0, + PowerSwitchIF::SWITCH_OFF); + powerSwitcher->sendSwitchCommand(switch1, + PowerSwitchIF::SWITCH_OFF); + } + } +} + +void Heater::setPowerSwitcher(PowerSwitchIF* powerSwitch) { + this->powerSwitcher = powerSwitch; +} + +ReturnValue_t Heater::performOperation(uint8_t opCode) { + handleQueue(); + handleEventQueue(); + + if (!healthHelper.healthTable->isFaulty(getObjectId())) { + reactedToBeingFaulty = false; + } + + switch (internalState) { + case STATE_ON: + if ((powerSwitcher->getSwitchState(switch0) == PowerSwitchIF::SWITCH_OFF) + || (powerSwitcher->getSwitchState(switch1) + == PowerSwitchIF::SWITCH_OFF)) { + //switch went off on its own + //trigger event. FDIR can confirm if it is caused by MniOps and decide on the action + //do not trigger FD events when under external control + if (healthHelper.getHealth() != EXTERNAL_CONTROL) { + triggerEvent(PowerSwitchIF::SWITCH_WENT_OFF); + } else { + internalState = STATE_EXTERNAL_CONTROL; + } + } + break; + case STATE_OFF: + //check if heater is on, ie both switches are on + //if so, just command it to off, to resolve the situation or force a switch stayed on event + //But, only do anything if not already faulty (state off is the stable point for being faulty) + if ((!healthHelper.healthTable->isFaulty(getObjectId())) + && (powerSwitcher->getSwitchState(switch0) + == PowerSwitchIF::SWITCH_ON) + && (powerSwitcher->getSwitchState(switch1) + == PowerSwitchIF::SWITCH_ON)) { + //do not trigger FD events when under external control + if (healthHelper.getHealth() != EXTERNAL_CONTROL) { + internalState = STATE_WAIT_FOR_SWITCHES_OFF; + switchCountdown.setTimeout(powerSwitcher->getSwitchDelayMs()); + powerSwitcher->sendSwitchCommand(switch0, + PowerSwitchIF::SWITCH_OFF); + powerSwitcher->sendSwitchCommand(switch1, + PowerSwitchIF::SWITCH_OFF); + } else { + internalState = STATE_EXTERNAL_CONTROL; + } + } + break; + case STATE_PASSIVE: + break; + case STATE_WAIT_FOR_SWITCHES_ON: + if (switchCountdown.hasTimedOut()) { + if ((powerSwitcher->getSwitchState(switch0) + == PowerSwitchIF::SWITCH_OFF) + || (powerSwitcher->getSwitchState(switch1) + == PowerSwitchIF::SWITCH_OFF)) { + triggerEvent(HEATER_STAYED_OFF); + internalState = STATE_WAIT_FOR_FDIR; //wait before retrying or anything + } else { + triggerEvent(HEATER_ON); + internalState = STATE_ON; + } + } + break; + case STATE_WAIT_FOR_SWITCHES_OFF: + if (switchCountdown.hasTimedOut()) { + //only check for both being on (ie heater still on) + if ((powerSwitcher->getSwitchState(switch0) + == PowerSwitchIF::SWITCH_ON) + && (powerSwitcher->getSwitchState(switch1) + == PowerSwitchIF::SWITCH_ON)) { + if (healthHelper.healthTable->isFaulty(getObjectId())) { + if (passive) { + internalState = STATE_PASSIVE; + } else { + internalState = STATE_OFF; //just accept it + } + triggerEvent(HEATER_ON); //but throw an event to make it more visible + break; + } + triggerEvent(HEATER_STAYED_ON); + internalState = STATE_WAIT_FOR_FDIR; //wait before retrying or anything + } else { + triggerEvent(HEATER_OFF); + if (passive) { + internalState = STATE_PASSIVE; + } else { + internalState = STATE_OFF; + } + } + } + break; + default: + break; + } + + if ((powerSwitcher->getSwitchState(switch0) == PowerSwitchIF::SWITCH_ON) + && (powerSwitcher->getSwitchState(switch1) + == PowerSwitchIF::SWITCH_ON)) { + if (wasOn) { + if (heaterOnCountdown.hasTimedOut()) { + //SHOULDDO this means if a heater fails in single mode, the timeout will start again + //I am not sure if this is a bug, but atm I have no idea how to fix this and think + //it will be ok. whatcouldpossiblygowrongâ„¢ + if (!timedOut) { + triggerEvent(HEATER_TIMEOUT); + timedOut = true; + } + } + } else { + wasOn = true; + heaterOnCountdown.resetTimer(); + timedOut = false; + } + } else { + wasOn = false; + } + + return HasReturnvaluesIF::RETURN_OK; +} + +void Heater::setSwitch(uint8_t number, ReturnValue_t state, + uint32_t* uptimeOfSwitching) { + if (powerSwitcher == NULL) { + return; + } + if (powerSwitcher->getSwitchState(number) == state) { + *uptimeOfSwitching = INVALID_UPTIME; + } else { + if ((*uptimeOfSwitching == INVALID_UPTIME)) { + powerSwitcher->sendSwitchCommand(number, state); + Clock::getUptime(uptimeOfSwitching); + } else { + uint32_t currentUptime; + Clock::getUptime(¤tUptime); + if (currentUptime - *uptimeOfSwitching + > powerSwitcher->getSwitchDelayMs()) { + *uptimeOfSwitching = INVALID_UPTIME; + if (healthHelper.healthTable->isHealthy(getObjectId())) { + if (state == PowerSwitchIF::SWITCH_ON) { + triggerEvent(HEATER_STAYED_OFF); + } else { + triggerEvent(HEATER_STAYED_ON); + } + } + //SHOULDDO MiniOps during switch timeout leads to a faulty switch + } + } + } +} + +MessageQueueId_t Heater::getCommandQueue() const { + return commandQueue->getId(); +} + +ReturnValue_t Heater::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + EventManagerIF* manager = objectManager->get( + objects::EVENT_MANAGER); + if (manager == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + result = manager->registerListener(eventQueue->getId()); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + ConfirmsFailuresIF* pcdu = objectManager->get( + DeviceHandlerFailureIsolation::powerConfirmationId); + if (pcdu == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + pcduQueueId = pcdu->getEventReceptionQueue(); + + result = manager->subscribeToAllEventsFrom(eventQueue->getId(), + getObjectId()); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = parameterHelper.initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + result = healthHelper.initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + return HasReturnvaluesIF::RETURN_OK; +} + +void Heater::handleQueue() { + CommandMessage message; + ReturnValue_t result = commandQueue->receiveMessage(&message); + if (result == HasReturnvaluesIF::RETURN_OK) { + result = healthHelper.handleHealthCommand(&message); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + parameterHelper.handleParameterMessage(&message); + } +} + +ReturnValue_t Heater::getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper* parameterWrapper, const ParameterWrapper* newValues, + uint16_t startAtIndex) { + if (domainId != DOMAIN_ID_BASE) { + return INVALID_DOMAIN_ID; + } + switch (parameterId) { + case 0: + parameterWrapper->set(heaterOnCountdown.timeout); + break; + default: + return INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; +} + +void Heater::handleEventQueue() { + EventMessage event; + for (ReturnValue_t result = eventQueue->receiveMessage(&event); + result == HasReturnvaluesIF::RETURN_OK; + result = eventQueue->receiveMessage(&event)) { + switch (event.getMessageId()) { + case EventMessage::EVENT_MESSAGE: + switch (event.getEvent()) { + case Fuse::FUSE_WENT_OFF: + case HEATER_STAYED_OFF: + case HEATER_STAYED_ON://Setting it faulty does not help, but we need to reach a stable state and can check for being faulty before throwing this event again. + if (healthHelper.healthTable->isCommandable(getObjectId())) { + healthHelper.setHealth(HasHealthIF::FAULTY); + internalState = STATE_FAULTY; + } + break; + case PowerSwitchIF::SWITCH_WENT_OFF: + internalState = STATE_WAIT; + event.setMessageId(EventMessage::CONFIRMATION_REQUEST); + if (pcduQueueId != 0) { + eventQueue->sendMessage(pcduQueueId, &event); + } else { + healthHelper.setHealth(HasHealthIF::FAULTY); + internalState = STATE_FAULTY; + } + break; + default: + return; + } + break; + case EventMessage::YOUR_FAULT: + healthHelper.setHealth(HasHealthIF::FAULTY); + internalState = STATE_FAULTY; + break; + case EventMessage::MY_FAULT: + //do nothing, we are already in STATE_WAIT and wait for a clear() + break; + default: + return; + } + } +} diff --git a/fsfw/thermal/Heater.h b/fsfw/thermal/Heater.h new file mode 100644 index 0000000..63fe206 --- /dev/null +++ b/fsfw/thermal/Heater.h @@ -0,0 +1,90 @@ +#ifndef FRAMEWORK_THERMAL_HEATER_H_ +#define FRAMEWORK_THERMAL_HEATER_H_ + +#include "../devicehandlers/HealthDevice.h" +#include "../parameters/ParameterHelper.h" +#include "../power/PowerSwitchIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../timemanager/Countdown.h" +#include +//class RedundantHeater; + +class Heater: public HealthDevice, public ReceivesParameterMessagesIF { + friend class RedundantHeater; +public: + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::HEATER; + static const Event HEATER_ON = MAKE_EVENT(0, SEVERITY::INFO); + static const Event HEATER_OFF = MAKE_EVENT(1, SEVERITY::INFO); + static const Event HEATER_TIMEOUT = MAKE_EVENT(2, SEVERITY::LOW); + static const Event HEATER_STAYED_ON = MAKE_EVENT(3, SEVERITY::LOW); + static const Event HEATER_STAYED_OFF = MAKE_EVENT(4, SEVERITY::LOW); + + Heater(uint32_t objectId, uint8_t switch0, uint8_t switch1); + virtual ~Heater(); + + ReturnValue_t performOperation(uint8_t opCode); + + ReturnValue_t initialize(); + + ReturnValue_t set(); + void clear(bool passive); + + void setPowerSwitcher(PowerSwitchIF *powerSwitch); + + MessageQueueId_t getCommandQueue() const; + + ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); + +protected: + static const uint32_t INVALID_UPTIME = 0; + + enum InternalState { + STATE_ON, + STATE_OFF, + STATE_PASSIVE, + STATE_WAIT_FOR_SWITCHES_ON, + STATE_WAIT_FOR_SWITCHES_OFF, + STATE_WAIT_FOR_FDIR, //used to avoid doing anything until fdir decided what to do + STATE_FAULTY, + STATE_WAIT, //used when waiting for system to recover from miniops + STATE_EXTERNAL_CONTROL //entered when under external control and a fdir reaction would be triggered. This is useful when leaving external control into an unknown state + //if no fdir reaction is triggered under external control the state is still ok and no need for any special treatment is needed + } internalState; + + PowerSwitchIF *powerSwitcher; + MessageQueueId_t pcduQueueId; + + uint8_t switch0; + uint8_t switch1; + + bool wasOn; + + bool timedOut; + + bool reactedToBeingFaulty; + + bool passive; + + MessageQueueIF* eventQueue; + Countdown heaterOnCountdown; + Countdown switchCountdown; + ParameterHelper parameterHelper; + + enum Action { + SET, CLEAR + } lastAction; + + void doAction(Action action); + + void setSwitch(uint8_t number, ReturnValue_t state, + uint32_t *upTimeOfSwitching); + + void handleQueue(); + + void handleEventQueue(); +}; + +#endif /* FRAMEWORK_THERMAL_HEATER_H_ */ diff --git a/fsfw/thermal/RedundantHeater.cpp b/fsfw/thermal/RedundantHeater.cpp new file mode 100644 index 0000000..6463fcc --- /dev/null +++ b/fsfw/thermal/RedundantHeater.cpp @@ -0,0 +1,40 @@ +#include "RedundantHeater.h" + +RedundantHeater::~RedundantHeater() { +} + +RedundantHeater::RedundantHeater(Parameters parameters) : + heater0(parameters.objectIdHeater0, parameters.switch0Heater0, + parameters.switch1Heater0), heater1(parameters.objectIdHeater1, + parameters.switch0Heater1, parameters.switch1Heater1) { +} + +void RedundantHeater::performOperation(uint8_t opCode) { + heater0.performOperation(0); + heater1.performOperation(0); +} + +void RedundantHeater::set(bool on, bool both, bool passive) { + if (on) { + ReturnValue_t result = heater0.set(); + if (result != HasReturnvaluesIF::RETURN_OK || both) { + heater1.set(); + } else { + heater1.clear(passive); + } + } else { + heater0.clear(passive); + heater1.clear(passive); + } + +} + +void RedundantHeater::triggerHeaterEvent(Event event) { + heater0.triggerEvent(event); + heater1.triggerEvent(event); +} + +void RedundantHeater::setPowerSwitcher(PowerSwitchIF* powerSwitch) { + heater0.setPowerSwitcher(powerSwitch); + heater1.setPowerSwitcher(powerSwitch); +} diff --git a/fsfw/thermal/RedundantHeater.h b/fsfw/thermal/RedundantHeater.h new file mode 100644 index 0000000..ab745a6 --- /dev/null +++ b/fsfw/thermal/RedundantHeater.h @@ -0,0 +1,51 @@ +#ifndef REDUNDANTHEATER_H_ +#define REDUNDANTHEATER_H_ + +#include "Heater.h" + +class RedundantHeater { +public: + + struct Parameters { + Parameters(uint32_t objectIdHeater0, uint32_t objectIdHeater1, + uint8_t switch0Heater0, uint8_t switch1Heater0, + uint8_t switch0Heater1, uint8_t switch1Heater1) : + objectIdHeater0(objectIdHeater0), objectIdHeater1( + objectIdHeater1), switch0Heater0(switch0Heater0), switch1Heater0( + switch1Heater0), switch0Heater1(switch0Heater1), switch1Heater1( + switch1Heater1) { + } + + Parameters() : + objectIdHeater0(0), objectIdHeater1(0), switch0Heater0(0), switch1Heater0( + 0), switch0Heater1(0), switch1Heater1(0) { + } + + uint32_t objectIdHeater0; + uint32_t objectIdHeater1; + uint8_t switch0Heater0; + uint8_t switch1Heater0; + uint8_t switch0Heater1; + uint8_t switch1Heater1; + }; + + RedundantHeater(Parameters parameters); + virtual ~RedundantHeater(); + + void performOperation(uint8_t opCode); + + void triggerHeaterEvent(Event event); + + void set(bool on, bool both, bool passive = false); + + void setPowerSwitcher(PowerSwitchIF *powerSwitch); + +protected: + + Heater heater0; + Heater heater1; + +}; + +#endif /* REDUNDANTHEATER_H_ */ + diff --git a/fsfw/thermal/TemperatureSensor.h b/fsfw/thermal/TemperatureSensor.h new file mode 100644 index 0000000..356ca72 --- /dev/null +++ b/fsfw/thermal/TemperatureSensor.h @@ -0,0 +1,174 @@ +#ifndef TEMPERATURESENSOR_H_ +#define TEMPERATURESENSOR_H_ + +#include "../datapool/DataSet.h" +#include "AbstractTemperatureSensor.h" +#include "../monitoring/LimitMonitor.h" + +template +class TemperatureSensor: public AbstractTemperatureSensor { +public: + struct Parameters { + float a; + float b; + float c; + T lowerLimit; + T upperLimit; + float gradient; + }; + struct UsedParameters { + UsedParameters(Parameters parameters) : + a(parameters.a), b(parameters.b), c(parameters.c), gradient( + parameters.gradient) { + } + float a; + float b; + float c; + float gradient; + }; + + static const uint16_t ADDRESS_A = 0; + static const uint16_t ADDRESS_B = 1; + static const uint16_t ADDRESS_C = 2; + static const uint16_t ADDRESS_GRADIENT = 3; + + static const uint16_t DEFAULT_CONFIRMATION_COUNT = 1; //!< Changed due to issue with later temperature checking even tough the sensor monitor was confirming already (Was 10 before with comment = Correlates to a 10s confirmation time. Chosen rather large, should not be so bad for components and helps survive glitches.) + + static const uint8_t DOMAIN_ID_SENSOR = 1; +private: + void setInvalid() { + outputTemperature = INVALID_TEMPERATURE; + outputTemperature.setValid(false); + uptimeOfOldTemperature.tv_sec = INVALID_UPTIME; + sensorMonitor.setToInvalid(); + } +protected: + static const int32_t INVALID_UPTIME = 0; + + UsedParameters parameters; + + T *inputTemperature; + + PoolVariableIF *poolVariable; + + PoolVariable outputTemperature; + + LimitMonitor sensorMonitor; + + float oldTemperature; + timeval uptimeOfOldTemperature; + + virtual float calculateOutputTemperature(T inputTemperature) { + return parameters.a * inputTemperature * inputTemperature + + parameters.b * inputTemperature + parameters.c; + } + + void doChildOperation() { + if (!poolVariable->isValid() + || !healthHelper.healthTable->isHealthy(getObjectId())) { + setInvalid(); + return; + } + + outputTemperature = calculateOutputTemperature(*inputTemperature); + outputTemperature.setValid(PoolVariableIF::VALID); + + timeval uptime; + Clock::getUptime(&uptime); + + if (uptimeOfOldTemperature.tv_sec != INVALID_UPTIME) { + //In theory, we could use an AbsValueMonitor to monitor the gradient. + //But this would require storing the gradient in DP and quite some overhead. + //The concept of delta limits is a bit strange anyway. + float deltaTime; + float deltaTemp; + + deltaTime = (uptime.tv_sec + uptime.tv_usec / 1000000.) + - (uptimeOfOldTemperature.tv_sec + + uptimeOfOldTemperature.tv_usec / 1000000.); + deltaTemp = oldTemperature - outputTemperature; + if (deltaTemp < 0) { + deltaTemp = -deltaTemp; + } + if (parameters.gradient < deltaTemp / deltaTime) { + triggerEvent(TEMP_SENSOR_GRADIENT); + //Don't set invalid, as we did not recognize it as invalid with full authority, let FDIR handle it + } + } + + //Check is done against raw limits. SHOULDDO: Why? Using °C would be more easy to handle. + sensorMonitor.doCheck(*inputTemperature); + + if (sensorMonitor.isOutOfLimits()) { + uptimeOfOldTemperature.tv_sec = INVALID_UPTIME; + outputTemperature.setValid(PoolVariableIF::INVALID); + outputTemperature = INVALID_TEMPERATURE; + } else { + oldTemperature = outputTemperature; + uptimeOfOldTemperature = uptime; + } + } + +public: + TemperatureSensor(object_id_t setObjectid, + T *inputTemperature, PoolVariableIF *poolVariable, + uint8_t vectorIndex, Parameters parameters, uint32_t datapoolId, + DataSet *outputSet, ThermalModuleIF *thermalModule) : + AbstractTemperatureSensor(setObjectid, thermalModule), parameters( + parameters), inputTemperature(inputTemperature), poolVariable( + poolVariable), outputTemperature(datapoolId, outputSet, + PoolVariableIF::VAR_WRITE), sensorMonitor(setObjectid, + DOMAIN_ID_SENSOR, + DataPool::poolIdAndPositionToPid( + poolVariable->getDataPoolId(), vectorIndex), + DEFAULT_CONFIRMATION_COUNT, parameters.lowerLimit, + parameters.upperLimit, TEMP_SENSOR_LOW, TEMP_SENSOR_HIGH), oldTemperature( + 20), uptimeOfOldTemperature( { INVALID_TEMPERATURE, 0 }) { + + } + + float getTemperature() { + return outputTemperature; + } + + bool isValid() { + return outputTemperature.isValid(); + } + + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex) { + ReturnValue_t result = sensorMonitor.getParameter(domainId, parameterId, + parameterWrapper, newValues, startAtIndex); + if (result != INVALID_DOMAIN_ID) { + return result; + } + if (domainId != this->DOMAIN_ID_BASE) { + return INVALID_DOMAIN_ID; + } + switch (parameterId) { + case ADDRESS_A: + parameterWrapper->set(parameters.a); + break; + case ADDRESS_B: + parameterWrapper->set(parameters.b); + break; + case ADDRESS_C: + parameterWrapper->set(parameters.c); + break; + case ADDRESS_GRADIENT: + parameterWrapper->set(parameters.gradient); + break; + default: + return INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; + } + + virtual void resetOldState() { + sensorMonitor.setToUnchecked(); + } + +}; + +#endif /* TEMPERATURESENSOR_H_ */ diff --git a/fsfw/thermal/ThermalComponent.cpp b/fsfw/thermal/ThermalComponent.cpp new file mode 100644 index 0000000..5dcd0bc --- /dev/null +++ b/fsfw/thermal/ThermalComponent.cpp @@ -0,0 +1,170 @@ +#include "ThermalComponent.h" + +ThermalComponent::ThermalComponent(object_id_t reportingObjectId, + uint8_t domainId, uint32_t temperaturePoolId, + uint32_t targetStatePoolId, uint32_t currentStatePoolId, + uint32_t requestPoolId, DataSet* dataSet, + AbstractTemperatureSensor* sensor, + AbstractTemperatureSensor* firstRedundantSensor, + AbstractTemperatureSensor* secondRedundantSensor, + ThermalModuleIF* thermalModule, Parameters parameters, + Priority priority) : + CoreComponent(reportingObjectId, domainId, temperaturePoolId, + targetStatePoolId, currentStatePoolId, requestPoolId, dataSet, + sensor, firstRedundantSensor, secondRedundantSensor, + thermalModule, + { parameters.lowerOpLimit, parameters.upperOpLimit, + parameters.heaterOn, parameters.hysteresis, + parameters.heaterSwitchoff }, priority, + ThermalComponentIF::STATE_REQUEST_NON_OPERATIONAL), nopParameters( + { parameters.lowerNopLimit, parameters.upperNopLimit }) { +} + +ThermalComponent::~ThermalComponent() { +} + +ReturnValue_t ThermalComponent::setTargetState(int8_t newState) { + DataSet mySet; + PoolVariable writableTargetState(targetState.getDataPoolId(), + &mySet, PoolVariableIF::VAR_READ_WRITE); + mySet.read(); + if ((writableTargetState == STATE_REQUEST_OPERATIONAL) + && (newState != STATE_REQUEST_IGNORE)) { + return HasReturnvaluesIF::RETURN_FAILED; + } + switch (newState) { + case STATE_REQUEST_NON_OPERATIONAL: + writableTargetState = newState; + mySet.commit(PoolVariableIF::VALID); + return HasReturnvaluesIF::RETURN_OK; + default: + return CoreComponent::setTargetState(newState); + } +} + +ReturnValue_t ThermalComponent::setLimits(const uint8_t* data, uint32_t size) { + if (size != 4 * sizeof(parameters.lowerOpLimit)) { + return MonitoringIF::INVALID_SIZE; + } + size_t readSize = size; + SerializeAdapter::deSerialize(&nopParameters.lowerNopLimit, &data, + &readSize, SerializeIF::Endianness::BIG); + SerializeAdapter::deSerialize(¶meters.lowerOpLimit, &data, + &readSize, SerializeIF::Endianness::BIG); + SerializeAdapter::deSerialize(¶meters.upperOpLimit, &data, + &readSize, SerializeIF::Endianness::BIG); + SerializeAdapter::deSerialize(&nopParameters.upperNopLimit, &data, + &readSize, SerializeIF::Endianness::BIG); + return HasReturnvaluesIF::RETURN_OK; +} + +ThermalComponentIF::State ThermalComponent::getState(float temperature, + CoreComponent::Parameters parameters, int8_t targetState) { + if (temperature < nopParameters.lowerNopLimit) { + return OUT_OF_RANGE_LOW; + } else { + State state = CoreComponent::getState(temperature, parameters, + targetState); + if (state != NON_OPERATIONAL_HIGH + && state != NON_OPERATIONAL_HIGH_IGNORED) { + return state; + } + if (temperature > nopParameters.upperNopLimit) { + state = OUT_OF_RANGE_HIGH; + } + if (targetState == STATE_REQUEST_IGNORE) { + state = getIgnoredState(state); + } + return state; + } +} + +void ThermalComponent::checkLimits(ThermalComponentIF::State state) { + if (targetState == STATE_REQUEST_OPERATIONAL || targetState == STATE_REQUEST_IGNORE) { + CoreComponent::checkLimits(state); + return; + } + //If component is not operational, it checks the NOP limits. + temperatureMonitor.translateState(state, temperature.value, + nopParameters.lowerNopLimit, nopParameters.upperNopLimit, false); +} + +ThermalComponentIF::HeaterRequest ThermalComponent::getHeaterRequest( + int8_t targetState, float temperature, + CoreComponent::Parameters parameters) { + if (targetState == STATE_REQUEST_IGNORE) { + isHeating = false; + return HEATER_DONT_CARE; + } + + if (temperature + > nopParameters.upperNopLimit - parameters.heaterSwitchoff) { + isHeating = false; + return HEATER_REQUEST_EMERGENCY_OFF; + } + + float nopHeaterLimit = nopParameters.lowerNopLimit + parameters.heaterOn; + float opHeaterLimit = parameters.lowerOpLimit + parameters.heaterOn; + + if (isHeating) { + nopHeaterLimit += parameters.hysteresis; + opHeaterLimit += parameters.hysteresis; + } + + if (temperature < nopHeaterLimit) { + isHeating = true; + return HEATER_REQUEST_EMERGENCY_ON; + } + + if ((targetState == STATE_REQUEST_OPERATIONAL) + || (targetState == STATE_REQUEST_HEATING)) { + if (temperature < opHeaterLimit) { + isHeating = true; + return HEATER_REQUEST_ON; + } + if (temperature + > parameters.upperOpLimit - parameters.heaterSwitchoff) { + isHeating = false; + return HEATER_REQUEST_OFF; + } + } + + isHeating = false; + return HEATER_DONT_CARE; +} + +ThermalComponentIF::State ThermalComponent::getIgnoredState(int8_t state) { + switch (state) { + case OUT_OF_RANGE_LOW: + return OUT_OF_RANGE_LOW_IGNORED; + case OUT_OF_RANGE_HIGH: + return OUT_OF_RANGE_HIGH_IGNORED; + case OUT_OF_RANGE_LOW_IGNORED: + return OUT_OF_RANGE_LOW_IGNORED; + case OUT_OF_RANGE_HIGH_IGNORED: + return OUT_OF_RANGE_HIGH_IGNORED; + default: + return CoreComponent::getIgnoredState(state); + } +} + +ReturnValue_t ThermalComponent::getParameter(uint8_t domainId, + uint16_t parameterId, ParameterWrapper* parameterWrapper, + const ParameterWrapper* newValues, uint16_t startAtIndex) { + ReturnValue_t result = CoreComponent::getParameter(domainId, parameterId, + parameterWrapper, newValues, startAtIndex); + if (result != INVALID_MATRIX_ID) { + return result; + } + switch (parameterId) { + case 12: + parameterWrapper->set(nopParameters.lowerNopLimit); + break; + case 13: + parameterWrapper->set(nopParameters.upperNopLimit); + break; + default: + return INVALID_MATRIX_ID; + } + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/thermal/ThermalComponent.h b/fsfw/thermal/ThermalComponent.h new file mode 100644 index 0000000..9324386 --- /dev/null +++ b/fsfw/thermal/ThermalComponent.h @@ -0,0 +1,53 @@ +#ifndef THERMALCOMPONENT_H_ +#define THERMALCOMPONENT_H_ + +#include "CoreComponent.h" + +class ThermalComponent: public CoreComponent { +public: + struct Parameters { + float lowerNopLimit; + float lowerOpLimit; + float upperOpLimit; + float upperNopLimit; + float heaterOn; + float hysteresis; + float heaterSwitchoff; + }; + struct NopParameters { + float lowerNopLimit; + float upperNopLimit; + }; + ThermalComponent(object_id_t reportingObjectId, uint8_t domainId, uint32_t temperaturePoolId, + uint32_t targetStatePoolId, uint32_t currentStatePoolId, uint32_t requestPoolId, + DataSet *dataSet, AbstractTemperatureSensor *sensor, + AbstractTemperatureSensor *firstRedundantSensor, + AbstractTemperatureSensor *secondRedundantSensor, + ThermalModuleIF *thermalModule, Parameters parameters, + Priority priority); + virtual ~ThermalComponent(); + + ReturnValue_t setTargetState(int8_t newState); + + virtual ReturnValue_t setLimits( const uint8_t* data, uint32_t size); + + virtual ReturnValue_t getParameter(uint8_t domainId, uint16_t parameterId, + ParameterWrapper *parameterWrapper, + const ParameterWrapper *newValues, uint16_t startAtIndex); + +protected: + + NopParameters nopParameters; + + State getState(float temperature, CoreComponent::Parameters parameters, + int8_t targetState); + + virtual void checkLimits(State state); + + virtual HeaterRequest getHeaterRequest(int8_t targetState, float temperature, + CoreComponent::Parameters parameters); + + State getIgnoredState(int8_t state); +}; + +#endif /* THERMALCOMPONENT_H_ */ diff --git a/fsfw/thermal/ThermalComponentIF.h b/fsfw/thermal/ThermalComponentIF.h new file mode 100644 index 0000000..522d4e4 --- /dev/null +++ b/fsfw/thermal/ThermalComponentIF.h @@ -0,0 +1,114 @@ +#ifndef THERMALCOMPONENTIF_H_ +#define THERMALCOMPONENTIF_H_ + +#include "../events/Event.h" +#include "../parameters/HasParametersIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../objectmanager/SystemObjectIF.h" + +class ThermalComponentIF : public HasParametersIF { +public: + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::TCS_1; + static const Event COMPONENT_TEMP_LOW = MAKE_EVENT(1, SEVERITY::LOW); + static const Event COMPONENT_TEMP_HIGH = MAKE_EVENT(2, SEVERITY::LOW); + static const Event COMPONENT_TEMP_OOL_LOW = MAKE_EVENT(3, SEVERITY::LOW); + static const Event COMPONENT_TEMP_OOL_HIGH = MAKE_EVENT(4, SEVERITY::LOW); + static const Event TEMP_NOT_IN_OP_RANGE = MAKE_EVENT(5, SEVERITY::LOW); //!< Is thrown when a device should start-up, but the temperature is out of OP range. P1: thermalState of the component, P2: 0 + + static const uint8_t INTERFACE_ID = CLASS_ID::THERMAL_COMPONENT_IF; + static const ReturnValue_t INVALID_TARGET_STATE = MAKE_RETURN_CODE(1); + static const ReturnValue_t ABOVE_OPERATIONAL_LIMIT = MAKE_RETURN_CODE(0xF1); + static const ReturnValue_t BELOW_OPERATIONAL_LIMIT = MAKE_RETURN_CODE(0xF2); + + enum State { + OUT_OF_RANGE_LOW = -2, + NON_OPERATIONAL_LOW = -1, + OPERATIONAL = 0, + NON_OPERATIONAL_HIGH = 1, + OUT_OF_RANGE_HIGH = 2, + OUT_OF_RANGE_LOW_IGNORED = OUT_OF_RANGE_LOW - 10, + NON_OPERATIONAL_LOW_IGNORED = NON_OPERATIONAL_LOW - 10, + OPERATIONAL_IGNORED = OPERATIONAL + 10, + NON_OPERATIONAL_HIGH_IGNORED = NON_OPERATIONAL_HIGH + 10, + OUT_OF_RANGE_HIGH_IGNORED = OUT_OF_RANGE_HIGH + 10, + UNKNOWN = 20 + }; + + enum StateRequest { + STATE_REQUEST_HEATING = 4, + STATE_REQUEST_IGNORE = 3, + STATE_REQUEST_OPERATIONAL = 1, + STATE_REQUEST_NON_OPERATIONAL = 0 + }; + + /** + * The elements are ordered by priority, lowest have highest priority + */ + enum Priority { + SAFE = 0, //!< SAFE + IDLE, //!< IDLE + PAYLOAD, //!< PAYLOAD + NUMBER_OF_PRIORITIES //!< MAX_PRIORITY + }; + + /** + * The elements are ordered by priority, lowest have highest priority + */ + enum HeaterRequest { + HEATER_REQUEST_EMERGENCY_OFF = 0, //!< REQUEST_EMERGENCY_OFF + HEATER_REQUEST_EMERGENCY_ON, //!< REQUEST_EMERGENCY_ON + HEATER_REQUEST_OFF, //!< REQUEST_OFF + HEATER_REQUEST_ON, //!< REQUEST_ON + HEATER_DONT_CARE //!< DONT_CARE + }; + + virtual ~ThermalComponentIF() { + + } + + virtual HeaterRequest performOperation(uint8_t opCode) = 0; + + virtual object_id_t getObjectId() = 0; + + virtual uint8_t getDomainId() const = 0; + + virtual void markStateIgnored() = 0; + + virtual float getLowerOpLimit() = 0; + + virtual ReturnValue_t setTargetState(int8_t state) = 0; + + virtual void setOutputInvalid() = 0; + + static bool isOperational(int8_t state) { + return ((state == OPERATIONAL) || (state == OPERATIONAL_IGNORED)); + } + + static bool isOutOfRange(State state) { + return ((state == OUT_OF_RANGE_HIGH) + || (state == OUT_OF_RANGE_HIGH_IGNORED) + || (state == OUT_OF_RANGE_LOW) + || (state == OUT_OF_RANGE_LOW_IGNORED) || (state == UNKNOWN)); + } + + static bool isNonOperational(State state) { + return !isOutOfRange(state); + } + + static bool isIgnoredState(State state) { + switch (state) { + case OUT_OF_RANGE_LOW_IGNORED: + case OUT_OF_RANGE_HIGH_IGNORED: + case NON_OPERATIONAL_LOW_IGNORED: + case NON_OPERATIONAL_HIGH_IGNORED: + case OPERATIONAL_IGNORED: + case UNKNOWN: + return true; + default: + return false; + } + } +}; + +#endif /* THERMALCOMPONENTIF_H_ */ diff --git a/fsfw/thermal/ThermalModule.cpp b/fsfw/thermal/ThermalModule.cpp new file mode 100644 index 0000000..c573008 --- /dev/null +++ b/fsfw/thermal/ThermalModule.cpp @@ -0,0 +1,288 @@ +#include "../monitoring/LimitViolationReporter.h" +#include "../monitoring/MonitoringMessageContent.h" +#include "ThermalModule.h" + +#include "AbstractTemperatureSensor.h" + +ThermalModule::ThermalModule(uint32_t moduleTemperaturePoolId, + uint32_t currentStatePoolId, uint32_t targetStatePoolId, + DataSet *dataSet, Parameters parameters, + RedundantHeater::Parameters heaterParameters) : + oldStrategy(ACTIVE_SINGLE), survivalTargetTemp(0), targetTemp(0), heating( + false), parameters(parameters), moduleTemperature( + moduleTemperaturePoolId, dataSet, PoolVariableIF::VAR_WRITE), currentState( + currentStatePoolId, dataSet, PoolVariableIF::VAR_WRITE), targetState( + targetStatePoolId, dataSet, PoolVariableIF::VAR_READ) { + heater = new RedundantHeater(heaterParameters); +} + +ThermalModule::ThermalModule(uint32_t moduleTemperaturePoolId, DataSet* dataSet) : + oldStrategy(ACTIVE_SINGLE), survivalTargetTemp(0), targetTemp(0), heating( + false), parameters( { 0, 0 }), moduleTemperature( + moduleTemperaturePoolId, dataSet, PoolVariableIF::VAR_WRITE), heater( + NULL), currentState(PoolVariableIF::INVALID, dataSet, + PoolVariableIF::VAR_WRITE), targetState(PoolVariableIF::INVALID, + dataSet, PoolVariableIF::VAR_READ) { +} + +ThermalModule::~ThermalModule() { + delete heater; +} + +void ThermalModule::performOperation(uint8_t opCode) { + if (heater != NULL) { + heater->performOperation(0); + } +} + +void ThermalModule::performMode(Strategy strategy) { + calculateTemperature(); + + bool safeOnly = (strategy == ACTIVE_SURVIVAL); + ThermalComponentIF::HeaterRequest componentHeaterRequest = + letComponentsPerformAndDeciceIfWeNeedToHeat(safeOnly); + + if (heater == NULL) { + informComponentsAboutHeaterState(false, NONE); + return; + } + + bool heating = calculateModuleHeaterRequestAndSetModuleStatus(strategy); + + if (componentHeaterRequest != ThermalComponentIF::HEATER_DONT_CARE) { + //Components overwrite the module request. + heating = ((componentHeaterRequest + == ThermalComponentIF::HEATER_REQUEST_ON) + || (componentHeaterRequest + == ThermalComponentIF::HEATER_REQUEST_EMERGENCY_ON)); + } + + bool dual = (strategy == ACTIVE_DUAL); + + if (strategy == PASSIVE) { + informComponentsAboutHeaterState(false, NONE); + if (oldStrategy != PASSIVE) { + heater->set(false, false, true); + } + } else { + if (safeOnly) { + informComponentsAboutHeaterState(heating, SAFE); + } else { + informComponentsAboutHeaterState(heating, ALL); + } + heater->set(heating, dual); + } + oldStrategy = strategy; +} + +float ThermalModule::getTemperature() { + return moduleTemperature; +} + +void ThermalModule::registerSensor(AbstractTemperatureSensor * sensor) { + sensors.push_back(sensor); +} + +void ThermalModule::registerComponent(ThermalComponentIF* component, + ThermalComponentIF::Priority priority) { + components.push_back(ComponentData( { component, priority, ThermalComponentIF::HEATER_DONT_CARE })); +} + +void ThermalModule::calculateTemperature() { + uint32_t numberOfValidSensors = 0; + moduleTemperature = 0; + std::list::iterator iter = sensors.begin(); + for (; iter != sensors.end(); iter++) { + if ((*iter)->isValid()) { + moduleTemperature = moduleTemperature + (*iter)->getTemperature(); + numberOfValidSensors++; + } + } + if (numberOfValidSensors != 0) { + moduleTemperature = moduleTemperature / numberOfValidSensors; + moduleTemperature.setValid(PoolVariableIF::VALID); + } else { + moduleTemperature = INVALID_TEMPERATURE; + moduleTemperature.setValid(PoolVariableIF::INVALID); + } +} + +ThermalComponentIF* ThermalModule::findComponent(object_id_t objectId) { + std::list::iterator iter = components.begin(); + for (; iter != components.end(); iter++) { + if (iter->component->getObjectId() == objectId) { + return iter->component; + } + } + return NULL; +} + +ThermalComponentIF::HeaterRequest ThermalModule::letComponentsPerformAndDeciceIfWeNeedToHeat( + bool safeOnly) { + ThermalComponentIF::HeaterRequest heaterRequests[ThermalComponentIF::NUMBER_OF_PRIORITIES]; + + survivalTargetTemp = -999; + targetTemp = -999; + + for (uint8_t i = 0; i < ThermalComponentIF::NUMBER_OF_PRIORITIES; i++) { + heaterRequests[i] = ThermalComponentIF::HEATER_DONT_CARE; + } + + std::list::iterator iter = components.begin(); + for (; iter != components.end(); iter++) { + updateTargetTemperatures(iter->component, + iter->priority == ThermalComponentIF::SAFE); + ThermalComponentIF::HeaterRequest request = + iter->component->performOperation(0); + iter->request = request; + if (request != ThermalComponentIF::HEATER_DONT_CARE) { + if (request < heaterRequests[iter->priority]) { + heaterRequests[iter->priority] = request; + } + } + } + + if (!safeOnly) { + for (uint8_t i = ThermalComponentIF::NUMBER_OF_PRIORITIES - 1; i > 0; + i--) { + if (heaterRequests[i - 1] == ThermalComponentIF::HEATER_DONT_CARE) { + heaterRequests[i - 1] = heaterRequests[i]; + } + } + } + return heaterRequests[0]; +} + +void ThermalModule::informComponentsAboutHeaterState(bool heaterIsOn, + Informee whomToInform) { + std::list::iterator iter = components.begin(); + for (; iter != components.end(); iter++) { + switch (whomToInform) { + case ALL: + break; + case SAFE: + if (!(iter->priority == ThermalComponentIF::SAFE)) { + iter->component->markStateIgnored(); + continue; + } + break; + case NONE: + iter->component->markStateIgnored(); + continue; + } + + if (heaterIsOn) { + if ((iter->request + == ThermalComponentIF::HEATER_REQUEST_EMERGENCY_OFF) + || (iter->request == ThermalComponentIF::HEATER_REQUEST_OFF)) { + iter->component->markStateIgnored(); + } + } else { + if ((iter->request + == ThermalComponentIF::HEATER_REQUEST_EMERGENCY_ON) + || (iter->request == ThermalComponentIF::HEATER_REQUEST_ON)) { + iter->component->markStateIgnored(); + } + } + } +} + +void ThermalModule::initialize(PowerSwitchIF* powerSwitch) { + if (heater != NULL) { + heater->setPowerSwitcher(powerSwitch); + } + + std::list::iterator iter = components.begin(); + for (; iter != components.end(); iter++) { + float componentLowerOpLimit = iter->component->getLowerOpLimit(); + if (iter->priority == ThermalComponentIF::SAFE) { + if (componentLowerOpLimit > survivalTargetTemp) { + survivalTargetTemp = componentLowerOpLimit; + } + } else { + if (componentLowerOpLimit > targetTemp) { + targetTemp = componentLowerOpLimit; + } + } + } + if (survivalTargetTemp > targetTemp) { + targetTemp = survivalTargetTemp; + } +} + +bool ThermalModule::calculateModuleHeaterRequestAndSetModuleStatus( + Strategy strategy) { + currentState.setValid(PoolVariableIF::VALID); + if (moduleTemperature == INVALID_TEMPERATURE) { + currentState = UNKNOWN; + return false; + } + + float limit = targetTemp; + bool heaterRequest = false; + if (strategy == ACTIVE_SURVIVAL) { + limit = survivalTargetTemp; + } + + if (moduleTemperature >= limit) { + currentState = OPERATIONAL; + } else { + currentState = NON_OPERATIONAL; + } + + limit += parameters.heaterOn; + + if (heating) { + limit += parameters.hysteresis; + } + + if (targetState == STATE_REQUEST_HEATING) { + if (moduleTemperature < limit) { + heaterRequest = true; + } else { + heaterRequest = false; + } + } + + heating = heaterRequest; + + return heaterRequest; +} + +void ThermalModule::setHeating(bool on) { + DataSet mySet; + PoolVariable writableTargetState(targetState.getDataPoolId(), + &mySet, PoolVariableIF::VAR_WRITE); + if (on) { + writableTargetState = STATE_REQUEST_HEATING; + } else { + writableTargetState = STATE_REQUEST_PASSIVE; + } + mySet.commit(PoolVariableIF::VALID); +} + +void ThermalModule::updateTargetTemperatures(ThermalComponentIF* component, + bool isSafe) { + if (isSafe) { + if (component->getLowerOpLimit() > survivalTargetTemp) { + survivalTargetTemp = component->getLowerOpLimit(); + } + } else { + if (component->getLowerOpLimit() > targetTemp) { + targetTemp = component->getLowerOpLimit(); + } + } +} + +void ThermalModule::setOutputInvalid() { + moduleTemperature = INVALID_TEMPERATURE; + moduleTemperature.setValid(PoolVariableIF::INVALID); + currentState.setValid(PoolVariableIF::INVALID); + std::list::iterator iter = components.begin(); + for (; iter != components.end(); iter++) { + iter->component->setOutputInvalid(); + } + if (heater != NULL) { + heater->set(false,true); + } +} diff --git a/fsfw/thermal/ThermalModule.h b/fsfw/thermal/ThermalModule.h new file mode 100644 index 0000000..19ab9a5 --- /dev/null +++ b/fsfw/thermal/ThermalModule.h @@ -0,0 +1,92 @@ +#ifndef THERMALMODULE_H_ +#define THERMALMODULE_H_ + +#include "../datapool/DataSet.h" +#include "../datapool/PoolVariable.h" +#include "../devicehandlers/HealthDevice.h" +#include "../events/EventReportingProxyIF.h" +#include "ThermalModuleIF.h" +#include +#include "tcsDefinitions.h" +#include "RedundantHeater.h" +class PowerSwitchIF; + +class ThermalModule: public ThermalModuleIF { + friend class ThermalController; +public: + struct Parameters { + float heaterOn; + float hysteresis; + }; + + ThermalModule(uint32_t moduleTemperaturePoolId, uint32_t currentStatePoolId, + uint32_t targetStatePoolId, DataSet *dataSet, Parameters parameters, + RedundantHeater::Parameters heaterParameters); + + ThermalModule(uint32_t moduleTemperaturePoolId, DataSet *dataSet); + + virtual ~ThermalModule(); + + void performOperation(uint8_t opCode); + + void performMode(Strategy strategy); + + float getTemperature(); + + void registerSensor(AbstractTemperatureSensor *sensor); + + void registerComponent(ThermalComponentIF *component, + ThermalComponentIF::Priority priority); + + ThermalComponentIF *findComponent(object_id_t objectId); + + void initialize(PowerSwitchIF* powerSwitch); + + void setHeating(bool on); + + virtual void setOutputInvalid(); + +protected: + enum Informee { + ALL, SAFE, NONE + }; + + struct ComponentData { + ThermalComponentIF *component; + ThermalComponentIF::Priority priority; + ThermalComponentIF::HeaterRequest request; + }; + + Strategy oldStrategy; + + float survivalTargetTemp; + + float targetTemp; + + bool heating; + + Parameters parameters; + + db_float_t moduleTemperature; + + RedundantHeater *heater; + + db_int8_t currentState; + db_int8_t targetState; + + std::list sensors; + std::list components; + + void calculateTemperature(); + + ThermalComponentIF::HeaterRequest letComponentsPerformAndDeciceIfWeNeedToHeat(bool safeOnly); + + void informComponentsAboutHeaterState(bool heaterIsOn, + Informee whomToInform); + + bool calculateModuleHeaterRequestAndSetModuleStatus(Strategy strategy); + + void updateTargetTemperatures(ThermalComponentIF *component, bool isSafe); +}; + +#endif /* THERMALMODULE_H_ */ diff --git a/fsfw/thermal/ThermalModuleIF.h b/fsfw/thermal/ThermalModuleIF.h new file mode 100644 index 0000000..2d11a6f --- /dev/null +++ b/fsfw/thermal/ThermalModuleIF.h @@ -0,0 +1,45 @@ +#ifndef THERMALMODULEIF_H_ +#define THERMALMODULEIF_H_ + +#include "ThermalComponentIF.h" +class AbstractTemperatureSensor; + +class ThermalModuleIF{ +public: + enum Strategy { + PASSIVE = 0, ACTIVE_SURVIVAL = 1, ACTIVE_SINGLE = 2, ACTIVE_DUAL = 3, + }; + + enum StateRequest { + STATE_REQUEST_HEATING = 1, STATE_REQUEST_PASSIVE = 0 + }; + + enum State { + NON_OPERATIONAL = 0, OPERATIONAL = 1, UNKNOWN = 2 + }; + + static constexpr float INVALID_TEMPERATURE = 999; + + virtual ~ThermalModuleIF() { + + } + + virtual void performOperation(uint8_t opCode) = 0; + + virtual void performMode(Strategy strategy) = 0; + + virtual float getTemperature() = 0; + + virtual void registerSensor(AbstractTemperatureSensor *sensor) = 0; + + virtual void registerComponent(ThermalComponentIF *component, + ThermalComponentIF::Priority priority) = 0; + + virtual ThermalComponentIF *findComponent(object_id_t objectId) = 0; + + virtual void setHeating(bool on) = 0; + + virtual void setOutputInvalid() = 0; +}; + +#endif /* THERMALMODULEIF_H_ */ diff --git a/fsfw/thermal/ThermalMonitor.cpp b/fsfw/thermal/ThermalMonitor.cpp new file mode 100644 index 0000000..11abfbe --- /dev/null +++ b/fsfw/thermal/ThermalMonitor.cpp @@ -0,0 +1,68 @@ +#include "ThermalMonitor.h" +#include "ThermalComponentIF.h" +#include "../monitoring/MonitoringIF.h" +ThermalMonitor::~ThermalMonitor() { +} + +void ThermalMonitor::sendTransitionEvent(float currentValue, + ReturnValue_t state) { + switch (state) { + case MonitoringIF::BELOW_LOW_LIMIT: + EventManagerIF::triggerEvent(reportingId, + ThermalComponentIF::COMPONENT_TEMP_OOL_LOW, state); + break; + case MonitoringIF::ABOVE_HIGH_LIMIT: + EventManagerIF::triggerEvent(reportingId, + ThermalComponentIF::COMPONENT_TEMP_OOL_HIGH, state); + break; + case ThermalComponentIF::BELOW_OPERATIONAL_LIMIT: + EventManagerIF::triggerEvent(reportingId, + ThermalComponentIF::COMPONENT_TEMP_LOW, state); + break; + case ThermalComponentIF::ABOVE_OPERATIONAL_LIMIT: + EventManagerIF::triggerEvent(reportingId, + ThermalComponentIF::COMPONENT_TEMP_HIGH, state); + break; + default: + break; + } +} + +bool ThermalMonitor::isAboveHighLimit() { + if (oldState == ThermalComponentIF::ABOVE_OPERATIONAL_LIMIT) { + return true; + } else { + return false; + } +} + +ReturnValue_t ThermalMonitor::translateState(ThermalComponentIF::State state, float sample, float lowerLimit, + float upperLimit, bool componentIsOperational) { + if (ThermalComponentIF::isIgnoredState(state)) { + setToUnchecked(); + return MonitoringIF::UNCHECKED; + } + switch (state) { + case ThermalComponentIF::OUT_OF_RANGE_LOW: + return monitorStateIs(MonitoringIF::BELOW_LOW_LIMIT, sample, lowerLimit); + case ThermalComponentIF::NON_OPERATIONAL_LOW: + if (componentIsOperational) { + return monitorStateIs(ThermalComponentIF::BELOW_OPERATIONAL_LIMIT, sample, lowerLimit); + } else { + return monitorStateIs(HasReturnvaluesIF::RETURN_OK, sample, 0.0); + } + case ThermalComponentIF::OPERATIONAL: + return monitorStateIs(HasReturnvaluesIF::RETURN_OK, sample, 0.0); + case ThermalComponentIF::NON_OPERATIONAL_HIGH: + if (componentIsOperational) { + return monitorStateIs(ThermalComponentIF::ABOVE_OPERATIONAL_LIMIT, sample, upperLimit); + } else { + return monitorStateIs(HasReturnvaluesIF::RETURN_OK, sample, 0.0); + } + case ThermalComponentIF::OUT_OF_RANGE_HIGH: + return monitorStateIs(MonitoringIF::ABOVE_HIGH_LIMIT, sample, upperLimit); + default: + //Never reached, all states covered. + return HasReturnvaluesIF::RETURN_FAILED; + } +} diff --git a/fsfw/thermal/ThermalMonitor.h b/fsfw/thermal/ThermalMonitor.h new file mode 100644 index 0000000..5c6806c --- /dev/null +++ b/fsfw/thermal/ThermalMonitor.h @@ -0,0 +1,23 @@ +#ifndef FRAMEWORK_THERMAL_THERMALMONITOR_H_ +#define FRAMEWORK_THERMAL_THERMALMONITOR_H_ + +#include "../monitoring/MonitorReporter.h" +#include "ThermalComponentIF.h" + +class ThermalMonitor: public MonitorReporter { +public: + template + ThermalMonitor(Args ... args) : + MonitorReporter(std::forward(args)...) { + } + ~ThermalMonitor(); + ReturnValue_t translateState(ThermalComponentIF::State state, float sample, + float lowerLimit, float upperLimit, bool componentIsOperational = true); + + bool isAboveHighLimit(); +protected: + virtual void sendTransitionEvent(float currentValue, ReturnValue_t state); + +}; + +#endif /* FRAMEWORK_THERMAL_THERMALMONITOR_H_ */ diff --git a/fsfw/thermal/tcsDefinitions.h b/fsfw/thermal/tcsDefinitions.h new file mode 100644 index 0000000..ad258ce --- /dev/null +++ b/fsfw/thermal/tcsDefinitions.h @@ -0,0 +1,8 @@ +#ifndef TCSDEFINITIONS_H_ +#define TCSDEFINITIONS_H_ + + +static const uint32_t INVALID_TEMPERATURE = 999; + + +#endif /* TCSDEFINITIONS_H_ */ diff --git a/fsfw/timemanager/CCSDSTime.cpp b/fsfw/timemanager/CCSDSTime.cpp new file mode 100644 index 0000000..f137e03 --- /dev/null +++ b/fsfw/timemanager/CCSDSTime.cpp @@ -0,0 +1,606 @@ +#include "CCSDSTime.h" + +#include +#include +#include +#include + +CCSDSTime::CCSDSTime() { +} + +CCSDSTime::~CCSDSTime() { +} + +ReturnValue_t CCSDSTime::convertToCcsds(Ccs_seconds* to, + const Clock::TimeOfDay_t* from) { + ReturnValue_t result = checkTimeOfDay(from); + if (result != RETURN_OK) { + return result; + } + + to->pField = (CCS << 4); + + to->yearMSB = (from->year >> 8); + to->yearLSB = from->year & 0xff; + to->month = from->month; + to->day = from->day; + to->hour = from->hour; + to->minute = from->minute; + to->second = from->second; + + return RETURN_OK; +} + +ReturnValue_t CCSDSTime::convertToCcsds(Ccs_mseconds* to, + const Clock::TimeOfDay_t* from) { + ReturnValue_t result = checkTimeOfDay(from); + if (result != RETURN_OK) { + return result; + } + + to->pField = (CCS << 4) + 2; + + to->yearMSB = (from->year >> 8); + to->yearLSB = from->year & 0xff; + to->month = from->month; + to->day = from->day; + to->hour = from->hour; + to->minute = from->minute; + to->second = from->second; + to->secondEminus2 = from->usecond / 10000; + to->secondEminus4 = (from->usecond % 10000) / 100; + + return RETURN_OK; +} + +ReturnValue_t CCSDSTime::convertFromCcsds(Clock::TimeOfDay_t* to, const uint8_t* from, + uint32_t length) { + ReturnValue_t result; + if (length > 0xFF) { + return LENGTH_MISMATCH; + } + result = convertFromASCII(to, from, length); //Try to parse it as ASCII + if (result == RETURN_OK) { + return RETURN_OK; + } + + //Seems to be no ascii, try the other formats + uint8_t codeIdentification = (*from >> 4); + switch (codeIdentification) { + case CUC_LEVEL1: //CUC_LEVEL2 can not be converted to TimeOfDay (ToD is Level 1) <- Well, if we know the epoch, we can... <- see bug 1133 + return convertFromCUC(to, from, length); + case CDS: + return convertFromCDS(to, from, length); + case CCS: { + uint32_t temp = 0; + return convertFromCCS(to, from, &temp, length); + } + + default: + return UNSUPPORTED_TIME_FORMAT; + } +} + +ReturnValue_t CCSDSTime::convertFromCUC(Clock::TimeOfDay_t* to, const uint8_t* from, + uint8_t length) { + return UNSUPPORTED_TIME_FORMAT; +} + +ReturnValue_t CCSDSTime::convertFromCDS(Clock::TimeOfDay_t* to, const uint8_t* from, + uint8_t length) { + timeval time; + ReturnValue_t result = convertFromCDS(&time, from, NULL, length); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return convertTimevalToTimeOfDay(to, &time); +} + +ReturnValue_t CCSDSTime::convertFromCCS(Clock::TimeOfDay_t* to, const uint8_t* from, + uint32_t* foundLength, uint32_t maxLength) { + uint8_t subsecondsLength = *from & 0b111; + uint32_t totalLength = subsecondsLength + 8; + if (maxLength < totalLength) { + return LENGTH_MISMATCH; + } + + *foundLength = totalLength; + + ReturnValue_t result = checkCcs(from, maxLength); + + if (result != RETURN_OK) { + return result; + } + + Ccs_mseconds *temp = (Ccs_mseconds *) from; + + to->year = (temp->yearMSB << 8) + temp->yearLSB; + to->hour = temp->hour; + to->minute = temp->minute; + to->second = temp->second; + + if (temp->pField & (1 << 3)) { //day of year variation + uint16_t tempDay = (temp->month << 8) + temp->day; + result = convertDaysOfYear(tempDay, to->year, + &(temp->month), &(temp->day)); + if (result != RETURN_OK) { + return result; + } + } + + to->month = temp->month; + to->day = temp->day; + + to->usecond = 0; + if (subsecondsLength > 0) { + *foundLength += 1; + if (temp->secondEminus2 >= 100) { + return INVALID_TIME_FORMAT; + } + to->usecond = temp->secondEminus2 * 10000; + } + + if (subsecondsLength > 1) { + *foundLength += 1; + if (temp->secondEminus4 >= 100) { + return INVALID_TIME_FORMAT; + } + to->usecond += temp->secondEminus4 * 100; + } + + return RETURN_OK; + +} + +ReturnValue_t CCSDSTime::convertFromASCII(Clock::TimeOfDay_t* to, const uint8_t* from, + uint8_t length) { + if (length < 19) { + return RETURN_FAILED; + } +// Newlib nano can't parse uint8, see SCNu8 documentation and https://sourceware.org/newlib/README +// Suggestion: use uint16 all the time. This should work on all systems. +#if FSFW_NO_C99_IO == 1 + uint16_t year; + uint16_t month; + uint16_t day; + uint16_t hour; + uint16_t minute; + float second; + int count = sscanf((char *) from, "%4" SCNu16 "-%2" SCNu16 "-%2" SCNu16 "T%" + "2" SCNu16 ":%2" SCNu16 ":%fZ", &year, &month, &day, &hour, + &minute, &second); + if (count == 6) { + to->year = year; + to->month = month; + to->day = day; + to->hour = hour; + to->minute = minute; + to->second = second; + to->usecond = (second - floor(second)) * 1000000; + return RETURN_OK; + } + + // try Code B (yyyy-ddd) + count = sscanf((char *) from, "%4" SCNu16 "-%3" SCNu16 "T%2" SCNu16 ":%" + "2" SCNu16 ":%fZ", &year, &day, &hour, &minute, &second); + if (count == 5) { + uint8_t tempDay; + ReturnValue_t result = CCSDSTime::convertDaysOfYear(day, year, + reinterpret_cast(&month), + reinterpret_cast(&tempDay)); + if (result != RETURN_OK) { + return RETURN_FAILED; + } + to->year = year; + to->month = month; + to->day = tempDay; + to->hour = hour; + to->minute = minute; + to->second = second; + to->usecond = (second - floor(second)) * 1000000; + return RETURN_OK; + } +// Warning: Compiler/Linker fails ambiguously if library does not implement +// C99 I/O +#else + uint16_t year; + uint8_t month; + uint16_t day; + uint8_t hour; + uint8_t minute; + float second; + //try Code A (yyyy-mm-dd) + int count = sscanf((char *) from, "%4" SCNu16 "-%2" SCNu8 "-%2" SCNu16 + "T%2" SCNu8 ":%2" SCNu8 ":%fZ", &year, &month, &day, + &hour, &minute, &second); + if (count == 6) { + to->year = year; + to->month = month; + to->day = day; + to->hour = hour; + to->minute = minute; + to->second = second; + to->usecond = (second - floor(second)) * 1000000; + return RETURN_OK; + } + + //try Code B (yyyy-ddd) + count = sscanf((char *) from, "%4" SCNu16 "-%3" SCNu16 "T%2" SCNu8 + ":%2" SCNu8 ":%fZ", &year, &day, &hour, &minute, &second); + if (count == 5) { + uint8_t tempDay; + ReturnValue_t result = CCSDSTime::convertDaysOfYear(day, year, &month, + &tempDay); + if (result != RETURN_OK) { + return RETURN_FAILED; + } + to->year = year; + to->month = month; + to->day = tempDay; + to->hour = hour; + to->minute = minute; + to->second = second; + to->usecond = (second - floor(second)) * 1000000; + return RETURN_OK; + } +#endif + + return UNSUPPORTED_TIME_FORMAT; +} + +ReturnValue_t CCSDSTime::checkCcs(const uint8_t* time, uint8_t length) { + Ccs_mseconds *time_struct = (Ccs_mseconds *) time; + + uint8_t additionalBytes = time_struct->pField & 0b111; + if ((additionalBytes == 0b111) || (length < (additionalBytes + 8))) { + return INVALID_TIME_FORMAT; + } + + if (time_struct->pField & (1 << 3)) { //day of year variation + uint16_t day = (time_struct->month << 8) + time_struct->day; + if (day > 366) { + return INVALID_TIME_FORMAT; + } + } else { + if (time_struct->month > 12) { + return INVALID_TIME_FORMAT; + } + if (time_struct->day > 31) { + return INVALID_TIME_FORMAT; + } + } + if (time_struct->hour > 23) { + return INVALID_TIME_FORMAT; + } + if (time_struct->minute > 59) { + return INVALID_TIME_FORMAT; + } + if (time_struct->second > 59) { + return INVALID_TIME_FORMAT; + } + + uint8_t *additionalByte = &time_struct->secondEminus2; + + for (; additionalBytes != 0; additionalBytes--) { + if (*additionalByte++ > 99) { + return INVALID_TIME_FORMAT; + } + } + return RETURN_OK; +} + +ReturnValue_t CCSDSTime::convertDaysOfYear(uint16_t dayofYear, uint16_t year, + uint8_t* month, uint8_t* day) { + if (isLeapYear(year)) { + if (dayofYear > 366) { + return INVALID_DAY_OF_YEAR; + } + } else { + if (dayofYear > 365) { + return INVALID_DAY_OF_YEAR; + } + } + *month = 1; + if (dayofYear <= 31) { + *day = dayofYear; + return RETURN_OK; + } + *month += 1; + dayofYear -= 31; + if (isLeapYear(year)) { + if (dayofYear <= 29) { + *day = dayofYear; + return RETURN_OK; + } + *month += 1; + dayofYear -= 29; + } else { + if (dayofYear <= 28) { + *day = dayofYear; + return RETURN_OK; + } + *month += 1; + dayofYear -= 28; + } + while (*month <= 12) { + if (dayofYear <= 31) { + *day = dayofYear; + return RETURN_OK; + } + *month += 1; + dayofYear -= 31; + + if (*month == 8) { + continue; + } + + if (dayofYear <= 30) { + *day = dayofYear; + return RETURN_OK; + } + *month += 1; + dayofYear -= 30; + } + return INVALID_DAY_OF_YEAR; +} + +bool CCSDSTime::isLeapYear(uint32_t year) { + if ((year % 400) == 0) { + return true; + } + if ((year % 100) == 0) { + return false; + } + if ((year % 4) == 0) { + return true; + } + return false; +} + +ReturnValue_t CCSDSTime::convertToCcsds(CDS_short* to, const timeval* from) { + to->pField = (CDS << 4) + 0; + uint32_t days = ((from->tv_sec) / SECONDS_PER_DAY) + + DAYS_CCSDS_TO_UNIX_EPOCH; + if (days > (1 << 16)) { + //Date is beyond year 2137 + return TIME_DOES_NOT_FIT_FORMAT; + } + to->dayMSB = (days & 0xFF00) >> 8; + to->dayLSB = (days & 0xFF); + uint32_t msDay = ((from->tv_sec % SECONDS_PER_DAY) * 1000) + + (from->tv_usec / 1000); + to->msDay_hh = (msDay & 0xFF000000) >> 24; + to->msDay_h = (msDay & 0xFF0000) >> 16; + to->msDay_l = (msDay & 0xFF00) >> 8; + to->msDay_ll = (msDay & 0xFF); + return RETURN_OK; +} + +ReturnValue_t CCSDSTime::convertToCcsds(OBT_FLP* to, const timeval* from) { + to->pFiled = (AGENCY_DEFINED << 4) + 5; + to->seconds_hh = (from->tv_sec >> 24) & 0xff; + to->seconds_h = (from->tv_sec >> 16) & 0xff; + to->seconds_l = (from->tv_sec >> 8) & 0xff; + to->seconds_ll = (from->tv_sec >> 0) & 0xff; + + //convert the µs to 2E-16 seconds + uint64_t temp = from->tv_usec; + temp = temp << 16; + temp = temp / 1E6; + + to->subsecondsMSB = (temp >> 8) & 0xff; + to->subsecondsLSB = temp & 0xff; + + return RETURN_OK; +} + +ReturnValue_t CCSDSTime::convertFromCcsds(timeval* to, const uint8_t* from, + uint32_t* foundLength, uint32_t maxLength) { + //We don't expect ascii here. SHOULDDO + uint8_t codeIdentification = (*from >> 4); + switch (codeIdentification) { + //unsupported, as Leap second correction would have to be applied +// case CUC_LEVEL1: +// return convertFromCUC(to, from, foundLength, maxLength); + case CDS: + return convertFromCDS(to, from, foundLength, maxLength); + case CCS: + return convertFromCCS(to, from, foundLength, maxLength); + default: + return UNSUPPORTED_TIME_FORMAT; + } + +} + +ReturnValue_t CCSDSTime::convertFromCUC(timeval* to, const uint8_t* from, + uint32_t* foundLength, uint32_t maxLength) { + if (maxLength < 1) { + return INVALID_TIME_FORMAT; + } + uint8_t pField = *from; + from++; + ReturnValue_t result = convertFromCUC(to, pField, from, foundLength, + maxLength - 1); + if (result == HasReturnvaluesIF::RETURN_OK) { + if (foundLength != NULL) { + *foundLength += 1; + } + } + return result; +} + +ReturnValue_t CCSDSTime::checkTimeOfDay(const Clock::TimeOfDay_t* time) { + if ((time->month > 12) || (time->month == 0)) { + return INVALID_TIME_FORMAT; + } + + if (time->day == 0) { + return INVALID_TIME_FORMAT; + } + switch (time->month) { + case 2: + if (isLeapYear(time->year)) { + if (time->day > 29) { + return INVALID_TIME_FORMAT; + } + } else { + if (time->day > 28) { + return INVALID_TIME_FORMAT; + } + } + break; + case 4: + case 6: + case 9: + case 11: + if (time->day > 30) { + return INVALID_TIME_FORMAT; + } + break; + default: + if (time->day > 31) { + return INVALID_TIME_FORMAT; + } + break; + } + + if (time->hour > 23) { + return INVALID_TIME_FORMAT; + } + + if (time->minute > 59) { + return INVALID_TIME_FORMAT; + } + + if (time->second > 59) { + return INVALID_TIME_FORMAT; + } + + if (time->usecond > 999999) { + return INVALID_TIME_FORMAT; + } + + return RETURN_OK; + +} + +ReturnValue_t CCSDSTime::convertTimevalToTimeOfDay(Clock::TimeOfDay_t* to, + timeval* from) { +//This is rather tricky. Implement only if needed. Also, if so, move to OSAL. + return UNSUPPORTED_TIME_FORMAT; +} + +ReturnValue_t CCSDSTime::convertFromCDS(timeval* to, const uint8_t* from, + uint32_t* foundLength, uint32_t maxLength) { + uint8_t pField = *from; + from++; +//Check epoch + if (pField & 0b1000) { + return NOT_ENOUGH_INFORMATION_FOR_TARGET_FORMAT; + } +//Check length + uint8_t expectedLength = 7; //Including p-Field. + bool extendedDays = pField & 0b100; + if (extendedDays) { + expectedLength += 1; + } + if ((pField & 0b11) == 0b01) { + expectedLength += 2; + } else if ((pField & 0b11) == 0b10) { + expectedLength += 4; + } + if (foundLength != NULL) { + *foundLength = expectedLength; + } + if (expectedLength > maxLength) { + return LENGTH_MISMATCH; + } +//Check and count days + uint32_t days = 0; + if (extendedDays) { + days = (from[0] << 16) + (from[1] << 8) + from[2]; + from += 3; + } else { + days = (from[0] << 8) + from[1]; + from += 2; + } +//Move to POSIX epoch. + if (days <= DAYS_CCSDS_TO_UNIX_EPOCH) { + return INVALID_TIME_FORMAT; + } + days -= DAYS_CCSDS_TO_UNIX_EPOCH; + to->tv_sec = days * SECONDS_PER_DAY; + uint32_t msDay = (from[0] << 24) + (from[1] << 16) + (from[2] << 8) + + from[3]; + from += 4; + to->tv_sec += (msDay / 1000); + to->tv_usec = (msDay % 1000) * 1000; + if ((pField & 0b11) == 0b01) { + uint16_t usecs = (from[0] << 16) + from[1]; + from += 2; + if (usecs > 999) { + return INVALID_TIME_FORMAT; + } + to->tv_usec += usecs; + } else if ((pField & 0b11) == 0b10) { + uint32_t picosecs = (from[0] << 24) + (from[1] << 16) + (from[2] << 8) + + from[3]; + from += 4; + if (picosecs > 999999) { + return INVALID_TIME_FORMAT; + } + //Not very useful. + to->tv_usec += (picosecs / 1000); + } + return RETURN_OK; +} + +ReturnValue_t CCSDSTime::convertFromCUC(timeval* to, uint8_t pField, + const uint8_t* from, uint32_t* foundLength, uint32_t maxLength) { + uint32_t secs = 0; + uint32_t subSeconds = 0; + uint8_t nCoarse = ((pField & 0b1100) >> 2) + 1; + uint8_t nFine = (pField & 0b11); + uint32_t totalLength = nCoarse + nFine; + if (foundLength != NULL) { + *foundLength = totalLength; + } + if (totalLength > maxLength) { + return LENGTH_MISMATCH; + } + for (int count = 0; count < nCoarse; count++) { + secs += *from << ((nCoarse * 8 - 8) * (1 + count)); + from++; + } + for (int count = 0; count < nFine; count++) { + subSeconds += *from << ((nFine * 8 - 8) * (1 + count)); + from++; + } + //Move to POSIX epoch. + to->tv_sec = secs; + if (pField & 0b10000) { + //CCSDS-Epoch + to->tv_sec -= (DAYS_CCSDS_TO_UNIX_EPOCH * SECONDS_PER_DAY); + } + to->tv_usec = subsecondsToMicroseconds(subSeconds); + return RETURN_OK; +} + +uint32_t CCSDSTime::subsecondsToMicroseconds(uint16_t subseconds) { + uint64_t temp = (uint64_t) subseconds * 1000000 + / (1 << (sizeof(subseconds) * 8)); + return temp; +} + +ReturnValue_t CCSDSTime::convertFromCCS(timeval* to, const uint8_t* from, + uint32_t* foundLength, uint32_t maxLength) { + Clock::TimeOfDay_t tempTime; + ReturnValue_t result = convertFromCCS(&tempTime, from, foundLength, + maxLength); + if (result != RETURN_OK) { + return result; + } + + return Clock::convertTimeOfDayToTimeval(&tempTime, to); + +} diff --git a/fsfw/timemanager/CCSDSTime.h b/fsfw/timemanager/CCSDSTime.h new file mode 100644 index 0000000..89fcff9 --- /dev/null +++ b/fsfw/timemanager/CCSDSTime.h @@ -0,0 +1,233 @@ +#ifndef CCSDSTIME_H_ +#define CCSDSTIME_H_ + +// COULDDO: have calls in Clock.h which return time quality and use timespec accordingly + +#include "Clock.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +bool operator<(const timeval& lhs, const timeval& rhs); +bool operator<=(const timeval& lhs, const timeval& rhs); +bool operator==(const timeval& lhs, const timeval& rhs); +/** + * static helper class for CCSDS Time Code Formats + * + * as described in CCSDS 301.0-B-3 + * + * Still work in progress + */ +class CCSDSTime: public HasReturnvaluesIF { +public: + /** + * The Time code identifications, bits 4-6 in the P-Field + */ + enum TimeCodeIdentification { + CCS = 0b101, + CUC_LEVEL1 = 0b001, + CUC_LEVEL2 = 0b010, + CDS = 0b100, + AGENCY_DEFINED = 0b110 + }; + static const uint8_t P_FIELD_CUC_6B_CCSDS = (CUC_LEVEL1 << 4) + (3 << 2) + + 2; + static const uint8_t P_FIELD_CUC_6B_AGENCY = (CUC_LEVEL2 << 4) + (3 << 2) + + 2; + static const uint8_t P_FIELD_CDS_SHORT = (CDS << 4); + /** + * Struct for CDS day-segmented format. + */ + struct CDS_short { + uint8_t pField; + uint8_t dayMSB; + uint8_t dayLSB; + uint8_t msDay_hh; + uint8_t msDay_h; + uint8_t msDay_l; + uint8_t msDay_ll; + }; + /** + * Struct for the CCS fromat in day of month variation with max resolution + */ + struct Ccs_seconds { + uint8_t pField; + uint8_t yearMSB; + uint8_t yearLSB; + uint8_t month; + uint8_t day; + uint8_t hour; + uint8_t minute; + uint8_t second; + }; + + /** + * Struct for the CCS fromat in day of month variation with 10E-4 seconds resolution + */ + struct Ccs_mseconds { + uint8_t pField; + uint8_t yearMSB; + uint8_t yearLSB; + uint8_t month; + uint8_t day; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t secondEminus2; + uint8_t secondEminus4; + }; + + struct OBT_FLP { + uint8_t pFiled; + uint8_t seconds_hh; + uint8_t seconds_h; + uint8_t seconds_l; + uint8_t seconds_ll; + uint8_t subsecondsMSB; + uint8_t subsecondsLSB; + }; + + struct TimevalLess { + bool operator()(const timeval& lhs, const timeval& rhs) const { + return (lhs < rhs); + } + }; + + static const uint8_t INTERFACE_ID = CLASS_ID::CCSDS_TIME_HELPER_CLASS; + static const ReturnValue_t UNSUPPORTED_TIME_FORMAT = MAKE_RETURN_CODE(0); + static const ReturnValue_t NOT_ENOUGH_INFORMATION_FOR_TARGET_FORMAT = + MAKE_RETURN_CODE(1); + static const ReturnValue_t LENGTH_MISMATCH = MAKE_RETURN_CODE(2); + static const ReturnValue_t INVALID_TIME_FORMAT = MAKE_RETURN_CODE(3); + static const ReturnValue_t INVALID_DAY_OF_YEAR = MAKE_RETURN_CODE(4); + static const ReturnValue_t TIME_DOES_NOT_FIT_FORMAT = MAKE_RETURN_CODE(5); + + /** + * convert a TimeofDay struct to ccs with seconds resolution + * + * @param to pointer to a CCS struct + * @param from pointer to a TimeOfDay Struct + * @return + * - @c RETURN_OK if OK + * - @c INVALID_TIMECODE if not OK + */ + static ReturnValue_t convertToCcsds(Ccs_seconds *to, + Clock::TimeOfDay_t const *from); + + /** + * Converts to CDS format from timeval. + * @param to pointer to the CDS struct to generate + * @param from pointer to a timeval struct which comprises a time of day since UNIX epoch. + * @return + * - @c RETURN_OK as it assumes a valid timeval. + */ + static ReturnValue_t convertToCcsds(CDS_short* to, timeval const *from); + + static ReturnValue_t convertToCcsds(OBT_FLP* to, timeval const *from); + + /** + * convert a TimeofDay struct to ccs with 10E-3 seconds resolution + * + * The 10E-4 seconds in the CCS Struct are 0 as the TimeOfDay only has ms resolution + * + * @param to pointer to a CCS struct + * @param from pointer to a TimeOfDay Struct + * @return + * - @c RETURN_OK if OK + * - @c INVALID_TIMECODE if not OK + */ + static ReturnValue_t convertToCcsds(Ccs_mseconds *to, + Clock::TimeOfDay_t const *from); + + /** + * SHOULDDO: can this be modified to recognize padding? + * Tries to interpret a Level 1 CCSDS time code + * + * It assumes binary formats contain a valid P Field and recognizes the ASCII format + * by the lack of one. + * + * @param to an empty TimeOfDay struct + * @param from pointer to an CCSDS Time code + * @param length length of the Time code + * @return + * - @c RETURN_OK if successful + * - @c UNSUPPORTED_TIME_FORMAT if a (possibly valid) time code is not supported + * - @c LENGTH_MISMATCH if the length does not match the P Field + * - @c INVALID_TIME_FORMAT if the format or a value is invalid + */ + static ReturnValue_t convertFromCcsds(Clock::TimeOfDay_t *to, uint8_t const *from, + uint32_t length); + + /** + * not implemented yet + * + * @param to + * @param from + * @return + */ + static ReturnValue_t convertFromCcsds(timeval *to, uint8_t const *from, + uint32_t* foundLength, uint32_t maxLength); + + static ReturnValue_t convertFromCUC(Clock::TimeOfDay_t *to, uint8_t const *from, + uint8_t length); + + static ReturnValue_t convertFromCUC(timeval *to, uint8_t const *from, + uint32_t* foundLength, uint32_t maxLength); + + static ReturnValue_t convertFromCUC(timeval *to, uint8_t pField, + uint8_t const *from, uint32_t* foundLength, uint32_t maxLength); + + static ReturnValue_t convertFromCCS(timeval *to, uint8_t const *from, + uint32_t* foundLength, uint32_t maxLength); + + static ReturnValue_t convertFromCCS(timeval *to, uint8_t pField, + uint8_t const *from, uint32_t* foundLength, uint32_t maxLength); + + static ReturnValue_t convertFromCDS(Clock::TimeOfDay_t *to, uint8_t const *from, + uint8_t length); + + static ReturnValue_t convertFromCDS(timeval *to, uint8_t const *from, + uint32_t* foundLength, uint32_t maxLength); + + static ReturnValue_t convertFromCCS(Clock::TimeOfDay_t *to, uint8_t const *from, + uint32_t* foundLength, uint32_t maxLength); + + static ReturnValue_t convertFromASCII(Clock::TimeOfDay_t *to, uint8_t const *from, + uint8_t length); + + static uint32_t subsecondsToMicroseconds(uint16_t subseconds); +private: + CCSDSTime(); + virtual ~CCSDSTime(); + /** + * checks a ccs time stream for validity + * + * Stream may be longer than the actual timecode + * + * @param time pointer to an Ccs stream + * @param length length of stream + * @return + */ + static ReturnValue_t checkCcs(const uint8_t* time, uint8_t length); + + static ReturnValue_t checkTimeOfDay(const Clock::TimeOfDay_t *time); + + static const uint32_t SECONDS_PER_DAY = 24 * 60 * 60; + static const uint32_t SECONDS_PER_NON_LEAP_YEAR = SECONDS_PER_DAY * 365; + static const uint32_t DAYS_CCSDS_TO_UNIX_EPOCH = 4383; //!< Time difference between CCSDS and POSIX epoch. This is exact, because leap-seconds where not introduced before 1972. + static const uint32_t SECONDS_CCSDS_TO_UNIX_EPOCH = DAYS_CCSDS_TO_UNIX_EPOCH + * SECONDS_PER_DAY; + /** + * @param dayofYear + * @param year + * @param month + * @param day + */ + static ReturnValue_t convertDaysOfYear(uint16_t dayofYear, uint16_t year, + uint8_t *month, uint8_t *day); + + static bool isLeapYear(uint32_t year); + static ReturnValue_t convertTimevalToTimeOfDay(Clock::TimeOfDay_t* to, + timeval* from); +}; + +#endif /* CCSDSTIME_H_ */ diff --git a/fsfw/timemanager/Clock.h b/fsfw/timemanager/Clock.h new file mode 100644 index 0000000..bc11238 --- /dev/null +++ b/fsfw/timemanager/Clock.h @@ -0,0 +1,154 @@ +#ifndef FRAMEWORK_TIMEMANAGER_CLOCK_H_ +#define FRAMEWORK_TIMEMANAGER_CLOCK_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MutexHelper.h" +#include "../globalfunctions/timevalOperations.h" + +#include +#include + +//! Don't use these for time points, type is not large enough for UNIX epoch. +using dur_millis_t = uint32_t; + +class Clock { +public: + typedef struct { + uint32_t year; //!< Year, A.D. + uint32_t month; //!< Month, 1 .. 12. + uint32_t day; //!< Day, 1 .. 31. + uint32_t hour; //!< Hour, 0 .. 23. + uint32_t minute; //!< Minute, 0 .. 59. + uint32_t second; //!< Second, 0 .. 59. + uint32_t usecond; //!< Microseconds, 0 .. 999999 + } TimeOfDay_t; + + /** + * This method returns the number of clock ticks per second. + * In RTEMS, this is typically 1000. + * @return The number of ticks. + * + * @deprecated, we should not worry about ticks, but only time + */ + static uint32_t getTicksPerSecond(void); + /** + * This system call sets the system time. + * To set the time, it uses a TimeOfDay_t struct. + * @param time The struct with the time settings to set. + * @return -@c RETURN_OK on success. Otherwise, the OS failure code + * is returned. + */ + static ReturnValue_t setClock(const TimeOfDay_t* time); + /** + * This system call sets the system time. + * To set the time, it uses a timeval struct. + * @param time The struct with the time settings to set. + * @return -@c RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t setClock(const timeval* time); + /** + * This system call returns the current system clock in timeval format. + * The timval format has the fields @c tv_sec with seconds and @c tv_usec with + * microseconds since an OS-defined epoch. + * @param time A pointer to a timeval struct where the current time is stored. + * @return @c RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t getClock_timeval(timeval* time); + + /** + * Get the time since boot in a timeval struct + * + * @param[out] time A pointer to a timeval struct where the uptime is stored. + * @return @c RETURN_OK on success. Otherwise, the OS failure code is returned. + * + * @deprecated, I do not think this should be able to fail, use timeval getUptime() + */ + static ReturnValue_t getUptime(timeval* uptime); + + static timeval getUptime(); + + /** + * Get the time since boot in milliseconds + * + * This value can overflow! Still, it can be used to calculate time intervalls + * between two calls up to 49 days by always using uint32_t in the calculation + * + * @param ms uptime in ms + * @return RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t getUptime(uint32_t* uptimeMs); + + /** + * Returns the time in microseconds since an OS-defined epoch. + * The time is returned in a 64 bit unsigned integer. + * @param time A pointer to a 64 bit unisigned integer where the data is stored. + * @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t getClock_usecs(uint64_t* time); + /** + * Returns the time in a TimeOfDay_t struct. + * @param time A pointer to a TimeOfDay_t struct. + * @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t getDateAndTime(TimeOfDay_t* time); + + /** + * Converts a time of day struct to POSIX seconds. + * @param time The time of day as input + * @param timeval The corresponding seconds since the epoch. + * @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t convertTimeOfDayToTimeval(const TimeOfDay_t* from, + timeval* to); + + /** + * Converts a time represented as seconds and subseconds since unix epoch to days since J2000 + * + * @param time seconds since unix epoch + * @param[out] JD2000 days since J2000 + * @return \c RETURN_OK + */ + static ReturnValue_t convertTimevalToJD2000(timeval time, double* JD2000); + + /** + * Calculates and adds the offset between UTC and TT + * + * Depends on the leap seconds to be set correctly. + * + * @param utc timeval, corresponding to UTC time + * @param[out] tt timeval, corresponding to Terrestial Time + * @return \c RETURN_OK on success, \c RETURN_FAILED if leapSeconds are not set + */ + static ReturnValue_t convertUTCToTT(timeval utc, timeval* tt); + + /** + * Set the Leap Seconds since 1972 + * + * @param leapSeconds_ + * @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t setLeapSeconds(const uint16_t leapSeconds_); + + /** + * Get the Leap Seconds since 1972 + * + * Must be set before! + * + * @param[out] leapSeconds_ + * @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t getLeapSeconds(uint16_t *leapSeconds_); + + /** + * Function to check and create the Mutex for the clock + * @return \c RETURN_OK on success. Otherwise \c RETURN_FAILED if not able to create one + */ + static ReturnValue_t checkOrCreateClockMutex(); + +private: + static MutexIF* timeMutex; + static uint16_t leapSeconds; +}; + + +#endif /* FRAMEWORK_TIMEMANAGER_CLOCK_H_ */ diff --git a/fsfw/timemanager/Countdown.cpp b/fsfw/timemanager/Countdown.cpp new file mode 100644 index 0000000..d569573 --- /dev/null +++ b/fsfw/timemanager/Countdown.cpp @@ -0,0 +1,45 @@ +/** + * @file Countdown.cpp + * @brief This file defines the Countdown class. + * @date 21.03.2013 + * @author baetz + */ + + +#include "Countdown.h" + +Countdown::Countdown(uint32_t initialTimeout) : startTime(0), timeout(initialTimeout) { +} + +Countdown::~Countdown() { +} + +ReturnValue_t Countdown::setTimeout(uint32_t miliseconds) { + ReturnValue_t return_value = Clock::getUptime( &startTime ); + timeout = miliseconds; + return return_value; +} + +bool Countdown::hasTimedOut() const { + uint32_t current_time; + Clock::getUptime( ¤t_time ); + if ( uint32_t(current_time - startTime) >= timeout) { + return true; + } else { + return false; + } +} + +bool Countdown::isBusy() const { + return !hasTimedOut(); +} + +ReturnValue_t Countdown::resetTimer() { + return setTimeout(timeout); +} + +void Countdown::timeOut() { + uint32_t current_time; + Clock::getUptime( ¤t_time ); + startTime= current_time - timeout; +} diff --git a/fsfw/timemanager/Countdown.h b/fsfw/timemanager/Countdown.h new file mode 100644 index 0000000..b86d9fe --- /dev/null +++ b/fsfw/timemanager/Countdown.h @@ -0,0 +1,31 @@ +/** + * @file Countdown.h + * @brief This file defines the Countdown class. + * @date 21.03.2013 + * @author baetz + */ + +#ifndef COUNTDOWN_H_ +#define COUNTDOWN_H_ + +#include "Clock.h" + +class Countdown { +private: + uint32_t startTime; +public: + uint32_t timeout; + Countdown(uint32_t initialTimeout = 0); + ~Countdown(); + ReturnValue_t setTimeout(uint32_t miliseconds); + + bool hasTimedOut() const; + + bool isBusy() const; + + ReturnValue_t resetTimer(); //!< Use last set timeout value and restart timer. + + void timeOut(); //!< Make hasTimedOut() return true +}; + +#endif /* COUNTDOWN_H_ */ diff --git a/fsfw/timemanager/ReceivesTimeInfoIF.h b/fsfw/timemanager/ReceivesTimeInfoIF.h new file mode 100644 index 0000000..14a750c --- /dev/null +++ b/fsfw/timemanager/ReceivesTimeInfoIF.h @@ -0,0 +1,31 @@ +/** + * @file ReceivesTimeInfoIF.h + * @brief This file defines the ReceivesTimeInfoIF class. + * @date 26.02.2013 + * @author baetz + */ + +#ifndef RECEIVESTIMEINFOIF_H_ +#define RECEIVESTIMEINFOIF_H_ + +/** + * This is a Interface for classes that receive timing information + * with the help of a dedicated message queue. + */ +class ReceivesTimeInfoIF { +public: + /** + * Returns the id of the queue which receives the timing information. + * @return Queue id of the timing queue. + */ + virtual MessageQueueId_t getTimeReceptionQueue() const = 0; + /** + * Empty virtual destructor. + */ + virtual ~ReceivesTimeInfoIF() { + } + +}; + + +#endif /* RECEIVESTIMEINFOIF_H_ */ diff --git a/fsfw/timemanager/Stopwatch.cpp b/fsfw/timemanager/Stopwatch.cpp new file mode 100644 index 0000000..302e2ac --- /dev/null +++ b/fsfw/timemanager/Stopwatch.cpp @@ -0,0 +1,60 @@ +#include "Stopwatch.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include + +Stopwatch::Stopwatch(bool displayOnDestruction, + StopwatchDisplayMode displayMode): displayOnDestruction( + displayOnDestruction), displayMode(displayMode) { + // Measures start time on initialization. + Clock::getUptime(&startTime); +} + +void Stopwatch::start() { + Clock::getUptime(&startTime); +} + +dur_millis_t Stopwatch::stop(bool display) { + stopInternal(); + if(display) { + this->display(); + } + return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000; +} + +double Stopwatch::stopSeconds() { + stopInternal(); + return timevalOperations::toDouble(elapsedTime); +} + +void Stopwatch::display() { + if(displayMode == StopwatchDisplayMode::MILLIS) { + sif::info << "Stopwatch: Operation took " << (elapsedTime.tv_sec * 1000 + + elapsedTime.tv_usec / 1000) << " milliseconds" << std::endl; + } + else if(displayMode == StopwatchDisplayMode::SECONDS) { + sif::info <<"Stopwatch: Operation took " << std::setprecision(3) + << std::fixed << timevalOperations::toDouble(elapsedTime) + << " seconds" << std::endl; + } +} + +Stopwatch::~Stopwatch() { + if(displayOnDestruction) { + stopInternal(); + display(); + } +} + +void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) { + this->displayMode = displayMode; +} + +StopwatchDisplayMode Stopwatch::getDisplayMode() const { + return displayMode; +} + +void Stopwatch::stopInternal() { + timeval endTime; + Clock::getUptime(&endTime); + elapsedTime = endTime - startTime; +} diff --git a/fsfw/timemanager/Stopwatch.h b/fsfw/timemanager/Stopwatch.h new file mode 100644 index 0000000..ea72c66 --- /dev/null +++ b/fsfw/timemanager/Stopwatch.h @@ -0,0 +1,70 @@ +#ifndef FSFW_TIMEMANAGER_STOPWATCH_H_ +#define FSFW_TIMEMANAGER_STOPWATCH_H_ + +#include "Clock.h" + +enum class StopwatchDisplayMode { + MILLIS, + SECONDS +}; + +/** + * @brief Simple Stopwatch implementation to measure elapsed time + * @details + * This class can be used to measure elapsed times. It also displays elapsed + * times automatically on destruction if not explicitely deactivated in the + * constructor. The default time format is the elapsed time in miliseconds + * in seconds as a double. + * @author R. Mueller + */ +class Stopwatch { +public: + /** + * Default constructor. Call "Stopwatch stopwatch" without brackets if + * no parameters are required! + * @param displayOnDestruction If set to true, displays measured time on + * object destruction + * @param displayMode Display format is either MS rounded or MS as double + * format + * @param outputPrecision If using double format, specify precision here. + */ + Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode + = StopwatchDisplayMode::MILLIS); + virtual~ Stopwatch(); + + /** + * Caches the start time + */ + void start(); + + /** + * Calculates the elapsed time since start and returns it + * @return elapsed time in milliseconds (rounded) + */ + dur_millis_t stop(bool display = false); + /** + * Calculates the elapsed time since start and returns it + * @return elapsed time in seconds (double precision) + */ + double stopSeconds(); + + /** + * Displays the elapsed times on the osstream, depending on internal display + * mode. + */ + void display(); + + StopwatchDisplayMode getDisplayMode() const; + void setDisplayMode(StopwatchDisplayMode displayMode); + bool displayOnDestruction = true; +private: + timeval startTime {0, 0}; + timeval elapsedTime {0, 0}; + + StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS; + + void stopInternal(); +}; + + +#endif /* FSFW_TIMEMANAGER_STOPWATCH_H_ */ diff --git a/fsfw/timemanager/TimeMessage.cpp b/fsfw/timemanager/TimeMessage.cpp new file mode 100644 index 0000000..5a9a416 --- /dev/null +++ b/fsfw/timemanager/TimeMessage.cpp @@ -0,0 +1,37 @@ +/** + * @file TimeMessage.cpp + * @brief This file defines the TimeMessage class. + * @date 26.02.2013 + * @author baetz + */ + +#include "TimeMessage.h" + +TimeMessage::TimeMessage() { + this->messageSize += sizeof(timeval) + sizeof(uint32_t); +} + +TimeMessage::TimeMessage(timeval setTime, uint32_t CounterValue) { + memcpy (this->getData(), &setTime, sizeof(timeval)); + this->messageSize += sizeof(timeval) + sizeof(uint32_t); + memcpy (this->getData() + sizeof(timeval), &CounterValue, sizeof(uint32_t)); +} + +TimeMessage::~TimeMessage() { +} + +timeval TimeMessage::getTime() { + timeval temp; + memcpy( &temp, this->getData(), sizeof(timeval)); + return temp; +} + +uint32_t TimeMessage::getCounterValue() { + uint32_t temp; + memcpy ( &temp, this->getData() + sizeof(timeval), sizeof(uint32_t)); + return temp; +} + +size_t TimeMessage::getMinimumMessageSize() { + return this->MAX_SIZE; +} diff --git a/fsfw/timemanager/TimeMessage.h b/fsfw/timemanager/TimeMessage.h new file mode 100644 index 0000000..116002e --- /dev/null +++ b/fsfw/timemanager/TimeMessage.h @@ -0,0 +1,56 @@ +/** + * @file TimeMessage.h + * @brief This file defines the TimeMessage class. + * @date 26.02.2013 + * @author baetz + */ + +#ifndef TIMEMESSAGE_H_ +#define TIMEMESSAGE_H_ + +#include "../ipc/MessageQueueMessage.h" +#include "Clock.h" +#include + +class TimeMessage : public MessageQueueMessage { +protected: + /** + * @brief This call always returns the same fixed size of the message. + * @return Returns HEADER_SIZE + \c sizeof(timeval) + sizeof(uint32_t). + */ + size_t getMinimumMessageSize(); +public: + + /** + * @ brief the size of a TimeMessage + */ + static const uint32_t MAX_SIZE = HEADER_SIZE + sizeof(timeval) + sizeof(uint32_t); + + /** + * @brief In the default constructor, only the message_size is set. + */ + TimeMessage(); + /** + * @brief With this constructor, the passed time information is directly put + * into the message. + * @param setTime The time information to put into the message. + * @param counterValue The counterValue to put into the message (GPS PPS). + */ + TimeMessage( timeval setTime, uint32_t counterValue = 0 ); + /** + * @brief The class's destructor is empty. + */ + ~TimeMessage(); + /** + * @brief This getter returns the time information in timeval format. + * @return Returns the time stored in this packet. + */ + timeval getTime(); + /** + * @brief This getter returns the CounterValue in uint32_t format. + * @return Returns the CounterValue stored in this packet. + */ + uint32_t getCounterValue(); +}; + +#endif /* TIMEMESSAGE_H_ */ diff --git a/fsfw/timemanager/TimeStamper.cpp b/fsfw/timemanager/TimeStamper.cpp new file mode 100644 index 0000000..d9f0f2f --- /dev/null +++ b/fsfw/timemanager/TimeStamper.cpp @@ -0,0 +1,23 @@ +#include "TimeStamper.h" +#include "Clock.h" +#include + +TimeStamper::TimeStamper(object_id_t objectId): SystemObject(objectId) {} + + +ReturnValue_t TimeStamper::addTimeStamp(uint8_t* buffer, + const uint8_t maxSize) { + if(maxSize < TimeStamperIF::MISSION_TIMESTAMP_SIZE){ + return HasReturnvaluesIF::RETURN_FAILED; + } + + timeval now; + Clock::getClock_timeval(&now); + CCSDSTime::CDS_short cds; + ReturnValue_t result = CCSDSTime::convertToCcsds(&cds,&now); + if(result != HasReturnvaluesIF::RETURN_OK){ + return result; + } + std::memcpy(buffer,&cds,sizeof(cds)); + return result; +} diff --git a/fsfw/timemanager/TimeStamper.h b/fsfw/timemanager/TimeStamper.h new file mode 100644 index 0000000..6895c14 --- /dev/null +++ b/fsfw/timemanager/TimeStamper.h @@ -0,0 +1,36 @@ +#ifndef FSFW_TIMEMANAGER_TIMESTAMPER_H_ +#define FSFW_TIMEMANAGER_TIMESTAMPER_H_ + +#include "TimeStamperIF.h" +#include "CCSDSTime.h" +#include "../objectmanager/SystemObject.h" + +/** + * @brief Time stamper which can be used to add any timestamp to a + * given buffer. + * @details + * This time stamper uses the CCSDS CDC short timestamp as a fault timestamp. + * This timestamp has a size of 8 bytes. A custom timestamp can be used by + * overriding the #addTimeStamp function. + * @ingroup utility + */ +class TimeStamper: public TimeStamperIF, public SystemObject { +public: + /** + * @brief Default constructor which also registers the time stamper as a + * system object so it can be found with the #objectManager. + * @param objectId + */ + TimeStamper(object_id_t objectId); + + /** + * Adds a CCSDS CDC short 8 byte timestamp to the given buffer. + * This function can be overriden to use a custom timestamp. + * @param buffer + * @param maxSize + * @return + */ + virtual ReturnValue_t addTimeStamp(uint8_t* buffer, const uint8_t maxSize); +}; + +#endif /* FSFW_TIMEMANAGER_TIMESTAMPER_H_ */ diff --git a/fsfw/timemanager/TimeStamperIF.h b/fsfw/timemanager/TimeStamperIF.h new file mode 100644 index 0000000..9601108 --- /dev/null +++ b/fsfw/timemanager/TimeStamperIF.h @@ -0,0 +1,28 @@ +#ifndef FRAMEWORK_TIMEMANAGER_TIMESTAMPERIF_H_ +#define FRAMEWORK_TIMEMANAGER_TIMESTAMPERIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" + +/** + * A class implementing this IF provides facilities to add a time stamp to the + * buffer provided. + * Implementors need to ensure that calling the method is thread-safe, i.e. + * addTimeStamp may be called in parallel from a different context. + */ +class TimeStamperIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::TIME_STAMPER_IF; + static const ReturnValue_t BAD_TIMESTAMP = MAKE_RETURN_CODE(1); + + //! This is a mission-specific constant and determines the total + //! size reserved for timestamps. + //! TODO: Default define in FSFWConfig ? + static const uint8_t MISSION_TIMESTAMP_SIZE = 8; + virtual ReturnValue_t addTimeStamp(uint8_t* buffer, + const uint8_t maxSize) = 0; + virtual ~TimeStamperIF() {} +}; + + + +#endif /* FRAMEWORK_TIMEMANAGER_TIMESTAMPERIF_H_ */ diff --git a/fsfw/tmstorage/TmStoreBackendIF.h b/fsfw/tmstorage/TmStoreBackendIF.h new file mode 100644 index 0000000..a441808 --- /dev/null +++ b/fsfw/tmstorage/TmStoreBackendIF.h @@ -0,0 +1,76 @@ +#ifndef PLATFORM_TMTCSERVICES_TMSTOREBACKENDIF_H_ +#define PLATFORM_TMTCSERVICES_TMSTOREBACKENDIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../objectmanager/SystemObjectIF.h" +#include "../parameters/HasParametersIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../timemanager/Clock.h" +class TmPacketInformation; +class TmPacketMinimal; +class SpacePacketBase; +class ApidSsc; + +class TmStoreBackendIF : public HasParametersIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::TM_STORE_BACKEND_IF; + static const ReturnValue_t BUSY = MAKE_RETURN_CODE(1); + static const ReturnValue_t FULL = MAKE_RETURN_CODE(2); + static const ReturnValue_t EMPTY = MAKE_RETURN_CODE(3); + static const ReturnValue_t NULL_REQUESTED = MAKE_RETURN_CODE(4); + static const ReturnValue_t TOO_LARGE = MAKE_RETURN_CODE(5); + static const ReturnValue_t NOT_READY = MAKE_RETURN_CODE(6); + static const ReturnValue_t DUMP_ERROR = MAKE_RETURN_CODE(7); + static const ReturnValue_t CRC_ERROR = MAKE_RETURN_CODE(8); + static const ReturnValue_t TIMEOUT = MAKE_RETURN_CODE(9); + static const ReturnValue_t IDLE_PACKET_FOUND = MAKE_RETURN_CODE(10); + static const ReturnValue_t TELECOMMAND_FOUND = MAKE_RETURN_CODE(11); + static const ReturnValue_t NO_PUS_A_TM = MAKE_RETURN_CODE(12); + static const ReturnValue_t TOO_SMALL = MAKE_RETURN_CODE(13); + static const ReturnValue_t BLOCK_NOT_FOUND = MAKE_RETURN_CODE(14); + static const ReturnValue_t INVALID_REQUEST = MAKE_RETURN_CODE(15); + + static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::MEMORY; + static const Event STORE_SEND_WRITE_FAILED = MAKE_EVENT(0, SEVERITY::LOW); //!< Initiating sending data to store failed. Low, par1: returnCode, par2: integer (debug info) + static const Event STORE_WRITE_FAILED = MAKE_EVENT(1, SEVERITY::LOW); //!< Data was sent, but writing failed. Low, par1: returnCode, par2: 0 + static const Event STORE_SEND_READ_FAILED = MAKE_EVENT(2, SEVERITY::LOW); //!< Initiating reading data from store failed. Low, par1: returnCode, par2: 0 + static const Event STORE_READ_FAILED = MAKE_EVENT(3, SEVERITY::LOW); //!< Data was requested, but access failed. Low, par1: returnCode, par2: 0 + static const Event UNEXPECTED_MSG = MAKE_EVENT(4, SEVERITY::LOW); //!< An unexpected TM packet or data message occurred. Low, par1: 0, par2: integer (debug info) + static const Event STORING_FAILED = MAKE_EVENT(5, SEVERITY::LOW); //!< Storing data failed. May simply be a full store. Low, par1: returnCode, par2: integer (sequence count of failed packet). + static const Event TM_DUMP_FAILED = MAKE_EVENT(6, SEVERITY::LOW); //!< Dumping retrieved data failed. Low, par1: returnCode, par2: integer (sequence count of failed packet). + static const Event STORE_INIT_FAILED = MAKE_EVENT(7, SEVERITY::LOW); //!< Corrupted init data or read error. Low, par1: returnCode, par2: integer (debug info) + static const Event STORE_INIT_EMPTY = MAKE_EVENT(8, SEVERITY::INFO); //!< Store was not initialized. Starts empty. Info, parameters both zero. + static const Event STORE_CONTENT_CORRUPTED = MAKE_EVENT(9, SEVERITY::LOW); //!< Data was read out, but it is inconsistent. Low par1: Memory address of corruption, par2: integer (debug info) + static const Event STORE_INITIALIZE = MAKE_EVENT(10, SEVERITY::INFO); //!< Info event indicating the store will be initialized, either at boot or after IOB switch. Info. pars: 0 + static const Event INIT_DONE = MAKE_EVENT(11, SEVERITY::INFO); //!< Info event indicating the store was successfully initialized, either at boot or after IOB switch. Info. pars: 0 + static const Event DUMP_FINISHED = MAKE_EVENT(12, SEVERITY::INFO); //!< Info event indicating that dumping finished successfully. par1: Number of dumped packets. par2: APID/SSC (16bits each) + static const Event DELETION_FINISHED = MAKE_EVENT(13, SEVERITY::INFO); //!< Info event indicating that deletion finished successfully. par1: Number of deleted packets. par2: APID/SSC (16bits each) + static const Event DELETION_FAILED = MAKE_EVENT(14, SEVERITY::LOW); //!< Info event indicating that something went wrong during deletion. pars: 0 + static const Event AUTO_CATALOGS_SENDING_FAILED = MAKE_EVENT(15, SEVERITY::INFO);//!< Info that the a auto catalog report failed + + virtual ~TmStoreBackendIF() {} + virtual ReturnValue_t performOperation(uint8_t opCode) = 0; + virtual ReturnValue_t initialize() = 0; + virtual ReturnValue_t storePacket(TmPacketMinimal* tmPacket) = 0; + virtual ReturnValue_t setFetchLimitTime(const timeval* loverLimit, const timeval* upperLimit) = 0; + virtual ReturnValue_t setFetchLimitBlocks(uint32_t startAddress, uint32_t endAddress) = 0; + virtual ReturnValue_t fetchPackets(bool fromBegin = false) = 0; + virtual ReturnValue_t initializeStore(object_id_t dumpTarget) = 0; + virtual ReturnValue_t dumpIndex(store_address_t* storeId) = 0; + virtual ReturnValue_t deleteBlocks(uint32_t startAddress, uint32_t endAddress) = 0; + virtual ReturnValue_t deleteTime(const timeval* timeUntil, + uint32_t* deletedPackets) = 0; + virtual void resetStore(bool clearStore, bool resetWrite, bool resetRead) = 0; + virtual bool isReady() = 0; + virtual uint32_t availableData() = 0; + virtual float getPercentageFilled() const = 0; + virtual uint32_t getStoredPacketsCount() const = 0; + virtual TmPacketInformation* getOldestPacket() = 0; + virtual TmPacketInformation* getYoungestPacket() = 0; + virtual float getDataRate() = 0; + +}; + + + +#endif /* PLATFORM_TMTCSERVICES_TMSTOREBACKENDIF_H_ */ diff --git a/fsfw/tmstorage/TmStoreFrontendIF.h b/fsfw/tmstorage/TmStoreFrontendIF.h new file mode 100644 index 0000000..e1bc3a1 --- /dev/null +++ b/fsfw/tmstorage/TmStoreFrontendIF.h @@ -0,0 +1,55 @@ +#ifndef PLATFORM_TMTCSERVICES_TMSTOREFRONTENDIF_H_ +#define PLATFORM_TMTCSERVICES_TMSTOREFRONTENDIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "TmStorePackets.h" +#include "../ipc/MessageQueueSenderIF.h" +class TmPacketMinimal; +class SpacePacketBase; +class TmStoreBackendIF; + +class TmStoreFrontendIF { +public: + virtual TmStoreBackendIF* getBackend() const = 0; + virtual ReturnValue_t performOperation(uint8_t opCode) = 0; + /** + * Callback from the back-end to indicate a certain packet was received. + * front-end takes care of discarding/downloading the packet. + * @param packet Pointer to the newly received Space Packet. + * @param address Start address of the packet found + * @param isLastPacket Indicates if no more packets can be fetched. + * @return If more packets shall be fetched, RETURN_OK must be returned. + * Any other code stops fetching packets. + */ + virtual ReturnValue_t packetRetrieved(TmPacketMinimal* packet, uint32_t address) = 0; + virtual void noMorePacketsInStore() = 0; + virtual void handleRetrievalFailed(ReturnValue_t errorCode, uint32_t parameter1 = 0, uint32_t parameter2 = 0) = 0; + /** + * To get the queue where commands shall be sent. + * @return Id of command queue. + */ + virtual MessageQueueId_t getCommandQueue() const = 0; + virtual ReturnValue_t fetchPackets(ApidSsc start, ApidSsc end) = 0; + virtual ReturnValue_t deletePackets(ApidSsc upTo) = 0; + virtual ReturnValue_t checkPacket(SpacePacketBase* tmPacket) = 0; + virtual bool isEnabled() const = 0; + virtual void setEnabled(bool enabled) = 0; + virtual void resetDownlinkedPacketCount() = 0; + virtual ReturnValue_t setDumpTarget(object_id_t dumpTarget) = 0; + static const uint8_t INTERFACE_ID = CLASS_ID::TM_STORE_FRONTEND_IF; + static const ReturnValue_t BUSY = MAKE_RETURN_CODE(1); + static const ReturnValue_t LAST_PACKET_FOUND = MAKE_RETURN_CODE(2); + static const ReturnValue_t STOP_FETCH = MAKE_RETURN_CODE(3); + static const ReturnValue_t TIMEOUT = MAKE_RETURN_CODE(4); + static const ReturnValue_t TM_CHANNEL_FULL = MAKE_RETURN_CODE(5); + static const ReturnValue_t NOT_STORED = MAKE_RETURN_CODE(6); + static const ReturnValue_t ALL_DELETED = MAKE_RETURN_CODE(7); + static const ReturnValue_t INVALID_DATA = MAKE_RETURN_CODE(8); + static const ReturnValue_t NOT_READY = MAKE_RETURN_CODE(9); + virtual ~TmStoreFrontendIF() { + } +}; + + + +#endif /* PLATFORM_TMTCSERVICES_TMSTOREFRONTENDIF_H_ */ diff --git a/fsfw/tmstorage/TmStoreMessage.cpp b/fsfw/tmstorage/TmStoreMessage.cpp new file mode 100644 index 0000000..d2efd40 --- /dev/null +++ b/fsfw/tmstorage/TmStoreMessage.cpp @@ -0,0 +1,165 @@ +#include "../objectmanager/ObjectManagerIF.h" +#include "TmStoreMessage.h" + +TmStoreMessage::~TmStoreMessage() { + +} + +TmStoreMessage::TmStoreMessage() { +} + +ReturnValue_t TmStoreMessage::setEnableStoringMessage(CommandMessage* cmd, + bool setEnabled) { + cmd->setCommand(ENABLE_STORING); + cmd->setParameter(setEnabled); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t TmStoreMessage::setDeleteContentMessage(CommandMessage* cmd, + ApidSsc upTo) { + cmd->setCommand(DELETE_STORE_CONTENT); + cmd->setParameter((upTo.apid<<16) + upTo.ssc); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t TmStoreMessage::setDownlinkContentMessage(CommandMessage* cmd, + ApidSsc fromPacket, + ApidSsc toPacket) { + cmd->setCommand(DOWNLINK_STORE_CONTENT); + cmd->setParameter((fromPacket.apid<<16) + fromPacket.ssc); + cmd->setParameter2((toPacket.apid<<16) + toPacket.ssc); + return HasReturnvaluesIF::RETURN_OK; +} + +ApidSsc TmStoreMessage::getPacketId1(CommandMessage* cmd) { + ApidSsc temp; + temp.apid = (cmd->getParameter() >> 16) & 0xFFFF; + temp.ssc = cmd->getParameter() & 0xFFFF; + return temp; +} + +ApidSsc TmStoreMessage::getPacketId2(CommandMessage* cmd) { + ApidSsc temp; + temp.apid = (cmd->getParameter2() >> 16) & 0xFFFF; + temp.ssc = cmd->getParameter2() & 0xFFFF; + return temp; +} + +bool TmStoreMessage::getEnableStoring(CommandMessage* cmd) { + return (bool)cmd->getParameter(); +} + +void TmStoreMessage::setChangeSelectionDefinitionMessage( + CommandMessage* cmd, bool addDefinition, store_address_t store_id) { + cmd->setCommand(CHANGE_SELECTION_DEFINITION); + cmd->setParameter(addDefinition); + cmd->setParameter2(store_id.raw); +} + +void TmStoreMessage::clear(CommandMessage* cmd) { + switch(cmd->getCommand()) { + case SELECTION_DEFINITION_REPORT: + case STORE_CATALOGUE_REPORT: + case CHANGE_SELECTION_DEFINITION: + case INDEX_REPORT: + case DELETE_STORE_CONTENT_TIME: + case DOWNLINK_STORE_CONTENT_TIME: { + StorageManagerIF *ipcStore = objectManager->get( + objects::IPC_STORE); + if (ipcStore != NULL) { + ipcStore->deleteData(getStoreId(cmd)); + } + } + /* NO BREAK falls through*/ + case DELETE_STORE_CONTENT_BLOCKS: + case DOWNLINK_STORE_CONTENT_BLOCKS: + case REPORT_INDEX_REQUEST: + cmd->setCommand(UNKNOWN_COMMAND); + cmd->setParameter(0); + cmd->setParameter2(0); + break; + default: + break; + } +} + +store_address_t TmStoreMessage::getStoreId(const CommandMessage* cmd) { + store_address_t temp; + temp.raw = cmd->getParameter2(); + return temp; +} + +bool TmStoreMessage::getAddToSelection(CommandMessage* cmd) { + return (bool)cmd->getParameter(); +} + +ReturnValue_t TmStoreMessage::setReportSelectionDefinitionMessage( + CommandMessage* cmd) { + cmd->setCommand(REPORT_SELECTION_DEFINITION); + return HasReturnvaluesIF::RETURN_OK; +} + +void TmStoreMessage::setSelectionDefinitionReportMessage( + CommandMessage* cmd, store_address_t storeId) { + cmd->setCommand(SELECTION_DEFINITION_REPORT); + cmd->setParameter2(storeId.raw); +} + +ReturnValue_t TmStoreMessage::setReportStoreCatalogueMessage( + CommandMessage* cmd) { + cmd->setCommand(REPORT_STORE_CATALOGUE); + return HasReturnvaluesIF::RETURN_OK; +} + +void TmStoreMessage::setStoreCatalogueReportMessage(CommandMessage* cmd, object_id_t objectId, + store_address_t storeId) { + cmd->setCommand(STORE_CATALOGUE_REPORT); + cmd->setParameter(objectId); + cmd->setParameter2(storeId.raw); +} + +object_id_t TmStoreMessage::getObjectId(CommandMessage* cmd) { + return cmd->getParameter(); +} + +void TmStoreMessage::setDownlinkContentTimeMessage(CommandMessage* cmd, + store_address_t storeId) { + cmd->setCommand(DOWNLINK_STORE_CONTENT_TIME); + cmd->setParameter2(storeId.raw); +} + +uint32_t TmStoreMessage::getAddressLow(CommandMessage* cmd){ + return cmd->getParameter(); +} +uint32_t TmStoreMessage::getAddressHigh(CommandMessage* cmd){ + return cmd->getParameter2(); +} + +void TmStoreMessage::setDeleteContentTimeMessage(CommandMessage* cmd, + store_address_t storeId) { + cmd->setCommand(DELETE_STORE_CONTENT_TIME); + cmd->setParameter2(storeId.raw); +} + +ReturnValue_t TmStoreMessage::setDeleteBlocksMessage(CommandMessage* cmd, uint32_t addressLow, uint32_t addressHigh){ + cmd->setCommand(DELETE_STORE_CONTENT_BLOCKS); + cmd->setParameter(addressLow); + cmd->setParameter2(addressHigh); + return HasReturnvaluesIF::RETURN_OK; +} +ReturnValue_t TmStoreMessage::setDownlinkBlocksMessage(CommandMessage* cmd, uint32_t addressLow, uint32_t addressHigh){ + cmd->setCommand(DOWNLINK_STORE_CONTENT_BLOCKS); + cmd->setParameter(addressLow); + cmd->setParameter2(addressHigh); + return HasReturnvaluesIF::RETURN_OK; +} +ReturnValue_t TmStoreMessage::setIndexRequestMessage(CommandMessage* cmd){ + cmd->setCommand(REPORT_INDEX_REQUEST); + return HasReturnvaluesIF::RETURN_OK; +} + + +void TmStoreMessage::setIndexReportMessage(CommandMessage* cmd, store_address_t storeId){ + cmd->setCommand(INDEX_REPORT); + cmd->setParameter2(storeId.raw); +} diff --git a/fsfw/tmstorage/TmStoreMessage.h b/fsfw/tmstorage/TmStoreMessage.h new file mode 100644 index 0000000..5b92856 --- /dev/null +++ b/fsfw/tmstorage/TmStoreMessage.h @@ -0,0 +1,63 @@ +#ifndef FRAMEWORK_TMSTORAGE_TMSTOREMESSAGE_H_ +#define FRAMEWORK_TMSTORAGE_TMSTOREMESSAGE_H_ + +#include "../ipc/CommandMessage.h" +#include "../storagemanager/StorageManagerIF.h" +#include "TmStorePackets.h" +#include "../objectmanager/SystemObjectIF.h" +class TmStoreMessage: public CommandMessage { +public: + static ReturnValue_t setEnableStoringMessage(CommandMessage* cmd, + bool setEnabled); + static ReturnValue_t setDeleteContentMessage(CommandMessage* cmd, + ApidSsc upTo); + static ReturnValue_t setDownlinkContentMessage(CommandMessage* cmd, + ApidSsc fromPacket, ApidSsc toPacket); + static void setChangeSelectionDefinitionMessage(CommandMessage* cmd, + bool addDefinition, store_address_t store_id); + static ReturnValue_t setReportSelectionDefinitionMessage( + CommandMessage* cmd); + static void setSelectionDefinitionReportMessage(CommandMessage* cmd, + store_address_t storeId); + static ReturnValue_t setReportStoreCatalogueMessage(CommandMessage* cmd); + static void setStoreCatalogueReportMessage(CommandMessage* cmd, object_id_t objectId, + store_address_t storeId); + static void setDownlinkContentTimeMessage(CommandMessage* cmd, + store_address_t storeId); + static void setIndexReportMessage(CommandMessage* cmd, store_address_t storeId); + static ReturnValue_t setDeleteBlocksMessage(CommandMessage* cmd, uint32_t addressLow, uint32_t addressHigh); + static ReturnValue_t setDownlinkBlocksMessage(CommandMessage* cmd, uint32_t addressLow, uint32_t addressHigh); + static ReturnValue_t setIndexRequestMessage(CommandMessage* cmd); + static void setDeleteContentTimeMessage(CommandMessage* cmd, + store_address_t storeId); + static void clear(CommandMessage* cmd); + static object_id_t getObjectId(CommandMessage* cmd); + static ApidSsc getPacketId1(CommandMessage* cmd); + static ApidSsc getPacketId2(CommandMessage* cmd); + static bool getEnableStoring(CommandMessage* cmd); + static bool getAddToSelection(CommandMessage* cmd); + static uint32_t getAddressLow(CommandMessage* cmd); + static uint32_t getAddressHigh(CommandMessage* cmd); + + static store_address_t getStoreId(const CommandMessage* cmd); + virtual ~TmStoreMessage(); + static const uint8_t MESSAGE_ID = messagetypes::TM_STORE; + static const Command_t ENABLE_STORING = MAKE_COMMAND_ID(1); + static const Command_t DELETE_STORE_CONTENT = MAKE_COMMAND_ID(2); + static const Command_t DOWNLINK_STORE_CONTENT = MAKE_COMMAND_ID(3); + static const Command_t CHANGE_SELECTION_DEFINITION = MAKE_COMMAND_ID(4); + static const Command_t REPORT_SELECTION_DEFINITION = MAKE_COMMAND_ID(5); + static const Command_t SELECTION_DEFINITION_REPORT = MAKE_COMMAND_ID(6); + static const Command_t REPORT_STORE_CATALOGUE = MAKE_COMMAND_ID(7); + static const Command_t STORE_CATALOGUE_REPORT = MAKE_COMMAND_ID(8); + static const Command_t DOWNLINK_STORE_CONTENT_TIME = MAKE_COMMAND_ID(9); + static const Command_t DELETE_STORE_CONTENT_TIME = MAKE_COMMAND_ID(10); + static const Command_t DELETE_STORE_CONTENT_BLOCKS = MAKE_COMMAND_ID(11); + static const Command_t DOWNLINK_STORE_CONTENT_BLOCKS = MAKE_COMMAND_ID(12); + static const Command_t REPORT_INDEX_REQUEST = MAKE_COMMAND_ID(13); + static const Command_t INDEX_REPORT = MAKE_COMMAND_ID(14); +private: + TmStoreMessage(); +}; + +#endif /* FRAMEWORK_TMSTORAGE_TMSTOREMESSAGE_H_ */ diff --git a/fsfw/tmstorage/TmStorePackets.h b/fsfw/tmstorage/TmStorePackets.h new file mode 100644 index 0000000..e88e741 --- /dev/null +++ b/fsfw/tmstorage/TmStorePackets.h @@ -0,0 +1,300 @@ +#ifndef FRAMEWORK_TMSTORAGE_TMSTOREPACKETS_H_ +#define FRAMEWORK_TMSTORAGE_TMSTOREPACKETS_H_ + +#include "../serialize/SerialFixedArrayListAdapter.h" +#include "../serialize/SerializeElement.h" +#include "../serialize/SerialLinkedListAdapter.h" +#include "../serialize/SerialBufferAdapter.h" +#include "../tmtcpacket/pus/TmPacketMinimal.h" +#include "../timemanager/TimeStamperIF.h" +#include "../timemanager/CCSDSTime.h" +#include "../globalfunctions/timevalOperations.h" + +class ServiceSubservice: public SerialLinkedListAdapter { +public: + SerializeElement service; + SerialFixedArrayListAdapter subservices; + LinkedElement linkedSubservices; + ServiceSubservice() : + SerialLinkedListAdapter(&service), linkedSubservices( + &subservices) { + service.setNext(&linkedSubservices); + } +}; + +class ApidSsc: public SerializeIF { +public: + ApidSsc() : + apid(SpacePacketBase::LIMIT_APID), ssc(0) { + } + ApidSsc(uint16_t apid, uint16_t ssc) : + apid(apid), ssc(ssc) { + } + uint16_t apid; + uint16_t ssc; + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = SerializeAdapter::serialize(&apid, + buffer, size, maxSize, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return SerializeAdapter::serialize(&ssc, buffer, size, + maxSize, streamEndianness); + + } + + size_t getSerializedSize() const { + return sizeof(apid) + sizeof(ssc); + } + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + ReturnValue_t result = SerializeAdapter::deSerialize(&apid, + buffer, size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return SerializeAdapter::deSerialize(&ssc, buffer, size, + streamEndianness); + } +}; + +class ChangeSelectionDefinition: public SerialLinkedListAdapter { +public: + SerializeElement apid; + SerialFixedArrayListAdapter serviceList; + LinkedElement linkedServiceList; + ChangeSelectionDefinition() : + SerialLinkedListAdapter(&apid), linkedServiceList( + &serviceList) { + apid.setNext(&linkedServiceList); + } +}; + +class TmPacketInformation: public SerializeIF { +public: + TmPacketInformation(TmPacketMinimal* packet){ + setContent(packet); + } + TmPacketInformation() :apid( + SpacePacketBase::LIMIT_APID), sourceSequenceCount(0), serviceType( + 0), serviceSubtype(0), subCounter(0) { + } + void reset() { + apid = SpacePacketBase::LIMIT_APID; + sourceSequenceCount = 0; + serviceType = 0; + serviceSubtype = 0; + subCounter = 0; + memset(rawTimestamp, 0, sizeof(rawTimestamp)); + } + void setContent(TmPacketMinimal* packet) { + apid = packet->getAPID(); + sourceSequenceCount = packet->getPacketSequenceCount(); + serviceType = packet->getService(); + serviceSubtype = packet->getSubService(); + subCounter = packet->getPacketSubcounter(); + memset(rawTimestamp, 0, sizeof(rawTimestamp)); + const uint8_t* pField = NULL; + uint32_t size = 0; + ReturnValue_t result = packet->getPacketTimeRaw(&pField, &size); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + if (*pField == CCSDSTime::P_FIELD_CDS_SHORT + && size <= TimeStamperIF::MISSION_TIMESTAMP_SIZE) { + //Shortcut to avoid converting CDS back and forth. + memcpy(rawTimestamp, pField, size); + return; + } + timeval time = { 0, 0 }; + result = packet->getPacketTime(&time); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + + CCSDSTime::CDS_short cdsFormat; + result = CCSDSTime::convertToCcsds(&cdsFormat, &time); + if (result != HasReturnvaluesIF::RETURN_OK) { + return; + } + memcpy(rawTimestamp, &cdsFormat, sizeof(cdsFormat)); + } + void setContent(TmPacketInformation* content) { + apid = content->apid; + sourceSequenceCount = content->sourceSequenceCount; + serviceType = content->serviceType; + serviceSubtype = content->serviceSubtype; + subCounter = content->subCounter; + memcpy(rawTimestamp, content->rawTimestamp, sizeof(rawTimestamp)); + } + bool isValid() const { + return (apid < SpacePacketBase::LIMIT_APID) ? true : false; + } + static void reset(TmPacketInformation* packet){ + packet->reset(); + } + + static bool isOlderThan(const TmPacketInformation* packet, const timeval* cmpTime){ + if(packet->isValid()){ + timeval packetTime = {0,0}; + uint32_t foundlen = 0; + CCSDSTime::convertFromCcsds(&packetTime,&packet->rawTimestamp[0],&foundlen,sizeof(rawTimestamp)); + if(packetTime <= *cmpTime){ + return true; + } + } + return false; + } + + static bool isNewerThan(const TmPacketInformation* packet, const timeval* cmpTime){ + if(packet->isValid()){ + timeval packetTime = {0,0}; + uint32_t foundlen = 0; + CCSDSTime::convertFromCcsds(&packetTime,&packet->rawTimestamp[0],&foundlen,sizeof(rawTimestamp)); + if(packetTime >= *cmpTime){ + return true; + } + } + return false; + } + + static bool isSmallerSSC(const TmPacketInformation* packet,const ApidSsc* compareSSC){ + if(packet->isValid()){ + if(packet->apid == compareSSC->apid){ + if(packet->sourceSequenceCount <= compareSSC->ssc){ + return true; + } + } + } + return false; + } + + static bool isLargerSSC(const TmPacketInformation* packet,const ApidSsc* compareSSC){ + if(packet->isValid()){ + if(packet->apid == compareSSC->apid){ + if(packet->sourceSequenceCount >= compareSSC->ssc){ + return true; + } + } + } + return false; + } + + uint16_t getApid() const{ + return apid; + } + + uint16_t getSsc() const{ + return sourceSequenceCount; + } + + uint8_t getServiceType() const{ + return serviceType; + } + + uint8_t getServiceSubtype() const{ + return serviceSubtype; + } + + uint8_t getSubCounter() const{ + return subCounter; + } + + timeval getTime() const { + timeval packetTime = {0,0}; + uint32_t foundlen = 0; + CCSDSTime::convertFromCcsds(&packetTime,&this->rawTimestamp[0],&foundlen,sizeof(rawTimestamp)); + return packetTime; + } + + bool operator==(const TmPacketInformation& other) { + //TODO Does not compare Raw Timestamp + return ((apid == other.getApid()) + && (sourceSequenceCount == other.getSsc()) + && (serviceType == other.getServiceType()) && (serviceSubtype = + other.getServiceSubtype())); + } + + + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + ReturnValue_t result = SerializeAdapter::serialize(&apid,buffer,size,maxSize,streamEndianness); + if(result!=HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::serialize(&sourceSequenceCount,buffer,size,maxSize,streamEndianness); + if(result!=HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::serialize(&serviceType,buffer,size,maxSize,streamEndianness); + if(result!=HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::serialize(&serviceSubtype,buffer,size,maxSize,streamEndianness); + if(result!=HasReturnvaluesIF::RETURN_OK){ + return result; + } + result = SerializeAdapter::serialize(&subCounter,buffer,size,maxSize,streamEndianness); + if(result!=HasReturnvaluesIF::RETURN_OK){ + return result; + } + SerialBufferAdapter adapter(rawTimestamp,sizeof(rawTimestamp)); + return adapter.serialize(buffer,size,maxSize,streamEndianness); + } + + size_t getSerializedSize() const { + uint32_t size = 0; + size += SerializeAdapter::getSerializedSize(&apid); + size += SerializeAdapter::getSerializedSize(&sourceSequenceCount); + size += SerializeAdapter::getSerializedSize(&serviceType); + size += SerializeAdapter::getSerializedSize(&serviceSubtype); + size += SerializeAdapter::getSerializedSize(&subCounter); + SerialBufferAdapter adapter(rawTimestamp,sizeof(rawTimestamp)); + size += adapter.getSerializedSize(); + return size; + + }; + + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + ReturnValue_t result = SerializeAdapter::deSerialize(&apid, buffer, + size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&sourceSequenceCount, buffer, + size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&serviceType, buffer, size, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&serviceSubtype, buffer, + size, streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = SerializeAdapter::deSerialize(&subCounter, buffer, size, + streamEndianness); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + SerialBufferAdapter adapter(rawTimestamp,sizeof(rawTimestamp)); + return adapter.deSerialize(buffer,size,streamEndianness); + } + +private: + uint16_t apid; + uint16_t sourceSequenceCount; + uint8_t serviceType; + uint8_t serviceSubtype; + uint8_t subCounter; + uint8_t rawTimestamp[TimeStamperIF::MISSION_TIMESTAMP_SIZE]; + +}; +#endif /* FRAMEWORK_TMSTORAGE_TMSTOREPACKETS_H_ */ diff --git a/fsfw/tmtcpacket/SpacePacket.cpp b/fsfw/tmtcpacket/SpacePacket.cpp new file mode 100644 index 0000000..b8ba27e --- /dev/null +++ b/fsfw/tmtcpacket/SpacePacket.cpp @@ -0,0 +1,27 @@ +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "ccsds_header.h" +#include "SpacePacket.h" +#include + +SpacePacket::SpacePacket( uint16_t packetDataLength, bool isTelecommand, uint16_t apid, uint16_t sequenceCount ): +SpacePacketBase( (uint8_t*)&this->localData ) { + initSpacePacketHeader(isTelecommand, false, apid, sequenceCount); + this->setPacketSequenceCount(sequenceCount); + if ( packetDataLength <= sizeof(this->localData.fields.buffer) ) { + this->setPacketDataLength(packetDataLength); + } else { + this->setPacketDataLength( sizeof(this->localData.fields.buffer) ); + } +} + +SpacePacket::~SpacePacket( void ) { +} + +bool SpacePacket::addWholeData( const uint8_t* p_Data, uint32_t packet_size ) { + if ( packet_size <= sizeof(this->data) ) { + memcpy( &this->localData.byteStream, p_Data, packet_size ); + return true; + } else { + return false; + } +} diff --git a/fsfw/tmtcpacket/SpacePacket.h b/fsfw/tmtcpacket/SpacePacket.h new file mode 100644 index 0000000..49dd5ae --- /dev/null +++ b/fsfw/tmtcpacket/SpacePacket.h @@ -0,0 +1,70 @@ +#ifndef SPACEPACKET_H_ +#define SPACEPACKET_H_ + +#include "SpacePacketBase.h" + +/** + * The SpacePacket class is a representation of a simple CCSDS Space Packet + * without (control over) a secondary header. + * It can be instantiated with a size smaller than \c PACKET_MAX_SIZE. Its + * main use is to serve as an idle packet in case no other packets are sent. + * For the ECSS PUS part the TcPacket and TmPacket classes are used. + * A pointer to \c local_data is passed to the \c SpacePacketBase parent class, + * so the parent's methods are reachable. + * @ingroup tmtcpackets + */ +class SpacePacket: public SpacePacketBase { +public: + static const uint16_t PACKET_MAX_SIZE = 1024; + /** + * The constructor initializes the packet and sets all header information + * according to the passed parameters. + * @param packetDataLength Sets the packet data length field and therefore specifies the size of the packet. + * @param isTelecommand Sets the packet type field to either TC (true) or TM (false). + * @param apid Sets the packet's APID field. The default value describes an idle packet. + * @param sequenceCount ets the packet's Source Sequence Count field. + */ + SpacePacket(uint16_t packetDataLength, bool isTelecommand = false, + uint16_t apid = APID_IDLE_PACKET, uint16_t sequenceCount = 0); + /** + * The class's default destructor. + */ + virtual ~SpacePacket(); + /** + * With this call, the complete data content (including the CCSDS Primary + * Header) is overwritten with the byte stream given. + * @param p_data Pointer to data to overwrite the content with + * @param packet_size Size of the data + * @return @li \c true if packet_size is smaller than \c MAX_PACKET_SIZE. + * @li \c false else. + */ + bool addWholeData(const uint8_t* p_data, uint32_t packet_size); +protected: + /** + * This structure defines the data structure of a Space Packet as local data. + * There's a buffer which corresponds to the Space Packet Data Field with a + * maximum size of \c PACKET_MAX_SIZE. + */ + struct PacketStructured { + CCSDSPrimaryHeader header; + uint8_t buffer[PACKET_MAX_SIZE]; + }; + /** + * This union simplifies accessing the full data content of the Space Packet. + * This is achieved by putting the \c PacketStructured struct in a union with + * a plain buffer. + */ + union SpacePacketData { + PacketStructured fields; + uint8_t byteStream[PACKET_MAX_SIZE + sizeof(CCSDSPrimaryHeader)]; + }; + /** + * This is the data representation of the class. + * It is a struct of CCSDS Primary Header and a data field, which again is + * packed in an union, so the data can be accessed as a byte stream without + * a cast. + */ + SpacePacketData localData; +}; + +#endif /* SPACEPACKET_H_ */ diff --git a/fsfw/tmtcpacket/SpacePacketBase.cpp b/fsfw/tmtcpacket/SpacePacketBase.cpp new file mode 100644 index 0000000..e13af8d --- /dev/null +++ b/fsfw/tmtcpacket/SpacePacketBase.cpp @@ -0,0 +1,104 @@ +#include "SpacePacketBase.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include + +SpacePacketBase::SpacePacketBase( const uint8_t* set_address ) { + this->data = (SpacePacketPointer*) set_address; +} + +SpacePacketBase::~SpacePacketBase() { +}; + +//CCSDS Methods: +uint8_t SpacePacketBase::getPacketVersionNumber( void ) { + return (this->data->header.packet_id_h & 0b11100000) >> 5; +} + +void SpacePacketBase::initSpacePacketHeader(bool isTelecommand, + bool hasSecondaryHeader, uint16_t apid, uint16_t sequenceCount) { + //reset header to zero: + memset(data,0, sizeof(this->data->header) ); + //Set TC/TM bit. + data->header.packet_id_h = ((isTelecommand? 1 : 0)) << 4; + //Set secondaryHeader bit + data->header.packet_id_h |= ((hasSecondaryHeader? 1 : 0)) << 3; + this->setAPID( apid ); + //Always initialize as standalone packets. + data->header.sequence_control_h = 0b11000000; + setPacketSequenceCount(sequenceCount); + +} + +bool SpacePacketBase::isTelecommand( void ) { + return (this->data->header.packet_id_h & 0b00010000) >> 4; +} + +bool SpacePacketBase::hasSecondaryHeader( void ) { + return (this->data->header.packet_id_h & 0b00001000) >> 3; +} + +uint16_t SpacePacketBase::getPacketId() { + return ( (this->data->header.packet_id_h) << 8 ) + + this->data->header.packet_id_l; +} + +uint16_t SpacePacketBase::getAPID( void ) const { + return ( (this->data->header.packet_id_h & 0b00000111) << 8 ) + + this->data->header.packet_id_l; +} + +void SpacePacketBase::setAPID( uint16_t new_apid ) { + //Use first three bits of new APID, but keep rest of packet id as it was (see specification). + this->data->header.packet_id_h = (this->data->header.packet_id_h & 0b11111000) | ( ( new_apid & 0x0700 ) >> 8 ); + this->data->header.packet_id_l = ( new_apid & 0x00FF ); +} + +uint16_t SpacePacketBase::getPacketSequenceControl( void ) { + return ( (this->data->header.sequence_control_h) << 8 ) + + this->data->header.sequence_control_l; +} + +uint8_t SpacePacketBase::getSequenceFlags( void ) { + return (this->data->header.sequence_control_h & 0b11000000) >> 6 ; +} + +uint16_t SpacePacketBase::getPacketSequenceCount( void ) const { + return ( (this->data->header.sequence_control_h & 0b00111111) << 8 ) + + this->data->header.sequence_control_l; +} + +void SpacePacketBase::setPacketSequenceCount( uint16_t new_count) { + this->data->header.sequence_control_h = ( this->data->header.sequence_control_h & 0b11000000 ) | ( ( (new_count%LIMIT_SEQUENCE_COUNT) & 0x3F00 ) >> 8 ); + this->data->header.sequence_control_l = ( (new_count%LIMIT_SEQUENCE_COUNT) & 0x00FF ); +} + +uint16_t SpacePacketBase::getPacketDataLength( void ) { + return ( (this->data->header.packet_length_h) << 8 ) + + this->data->header.packet_length_l; +} + +void SpacePacketBase::setPacketDataLength( uint16_t new_length) { + this->data->header.packet_length_h = ( ( new_length & 0xFF00 ) >> 8 ); + this->data->header.packet_length_l = ( new_length & 0x00FF ); +} + +size_t SpacePacketBase::getFullSize() { + //+1 is done because size in packet data length field is: size of data field -1 + return this->getPacketDataLength() + sizeof(this->data->header) + 1; +} + +uint8_t* SpacePacketBase::getWholeData() { + return (uint8_t*)this->data; +} + +void SpacePacketBase::setData( const uint8_t* p_Data ) { + this->data = (SpacePacketPointer*)p_Data; +} + +uint32_t SpacePacketBase::getApidAndSequenceCount() const { + return (getAPID() << 16) + getPacketSequenceCount(); +} + +uint8_t* SpacePacketBase::getPacketData() { + return &(data->packet_data); +} diff --git a/fsfw/tmtcpacket/SpacePacketBase.h b/fsfw/tmtcpacket/SpacePacketBase.h new file mode 100644 index 0000000..19cbd07 --- /dev/null +++ b/fsfw/tmtcpacket/SpacePacketBase.h @@ -0,0 +1,178 @@ +#ifndef FSFW_TMTCPACKET_SPACEPACKETBASE_H_ +#define FSFW_TMTCPACKET_SPACEPACKETBASE_H_ + +#include "ccsds_header.h" +#include + +/** + * @defgroup tmtcpackets Space Packets + * This is the group, where all classes associated with Telecommand and + * Telemetry packets belong to. + * The class hierarchy resembles the dependency between the different standards + * applied, namely the CCSDS Space Packet standard and the ECCSS Packet + * Utilization Standard. Most field and structure names are taken from these + * standards. + */ + +/** + * This struct defines the data structure of a Space Packet when accessed + * via a pointer. + * @ingroup tmtcpackets + */ +struct SpacePacketPointer { + CCSDSPrimaryHeader header; + uint8_t packet_data; +}; + +/** + * This class is the basic data handler for any CCSDS Space Packet + * compatible Telecommand and Telemetry packet. + * It does not contain the packet data itself but a pointer to the + * data must be set on instantiation. An invalid pointer may cause + * damage, as no getter method checks data validity. Anyway, a NULL + * check can be performed by making use of the getWholeData method. + * Remark: All bit numbers in this documentation are counted from + * the most significant bit (from left). + * @ingroup tmtcpackets + */ +class SpacePacketBase { +protected: + /** + * A pointer to a structure which defines the data structure of + * the packet header. + * To be hardware-safe, all elements are of byte size. + */ + SpacePacketPointer* data; +public: + static const uint16_t LIMIT_APID = 2048; //2^1 + static const uint16_t LIMIT_SEQUENCE_COUNT = 16384; // 2^14 + static const uint16_t APID_IDLE_PACKET = 0x7FF; + static const uint8_t TELECOMMAND_PACKET = 1; + static const uint8_t TELEMETRY_PACKET = 0; + /** + * This definition defines the CRC size in byte. + */ + static const uint8_t CRC_SIZE = 2; + /** + * This is the minimum size of a SpacePacket. + */ + static const uint16_t MINIMUM_SIZE = sizeof(CCSDSPrimaryHeader) + CRC_SIZE; + /** + * This is the default constructor. + * It sets its internal data pointer to the address passed. + * @param set_address The position where the packet data lies. + */ + SpacePacketBase( const uint8_t* set_address ); + /** + * No data is allocated, so the destructor is empty. + */ + virtual ~SpacePacketBase(); + + //CCSDS Methods: + /** + * Getter for the packet version number field. + * @return Returns the highest three bit of the packet in one byte. + */ + uint8_t getPacketVersionNumber( void ); + /** + * This method checks the type field in the header. + * This bit specifies, if the command is interpreted as Telecommand of + * as Telemetry. For a Telecommand, the bit is set. + * @return Returns true if the bit is set and false if not. + */ + bool isTelecommand( void ); + + void initSpacePacketHeader(bool isTelecommand, bool hasSecondaryHeader, + uint16_t apid, uint16_t sequenceCount = 0); + /** + * The CCSDS header provides a secondary header flag (the fifth-highest bit), + * which is checked with this method. + * @return Returns true if the bit is set and false if not. + */ + bool hasSecondaryHeader( void ); + /** + * Returns the complete first two bytes of the packet, which together form + * the CCSDS packet id. + * @return The CCSDS packet id. + */ + uint16_t getPacketId( void ); + /** + * Returns the APID of a packet, which are the lowest 11 bit of the packet + * id. + * @return The CCSDS APID. + */ + uint16_t getAPID( void ) const; + /** + * Sets the APID of a packet, which are the lowest 11 bit of the packet + * id. + * @param The APID to set. The highest five bits of the parameter are + * ignored. + */ + void setAPID( uint16_t setAPID ); + /** + * Returns the CCSDS packet sequence control field, which are the third and + * the fourth byte of the CCSDS primary header. + * @return The CCSDS packet sequence control field. + */ + uint16_t getPacketSequenceControl( void ); + /** + * Returns the SequenceFlags, which are the highest two bit of the packet + * sequence control field. + * @return The CCSDS sequence flags. + */ + uint8_t getSequenceFlags( void ); + /** + * Returns the packet sequence count, which are the lowest 14 bit of the + * packet sequence control field. + * @return The CCSDS sequence count. + */ + uint16_t getPacketSequenceCount( void ) const; + /** + * Sets the packet sequence count, which are the lowest 14 bit of the + * packet sequence control field. + * setCount is modulo-divided by \c LIMIT_SEQUENCE_COUNT to avoid overflows. + * @param setCount The value to set the count to. + */ + void setPacketSequenceCount( uint16_t setCount ); + /** + * Returns the packet data length, which is the fifth and sixth byte of the + * CCSDS Primary Header. The packet data length is the size of every kind + * of data \b after the CCSDS Primary Header \b -1. + * @return The CCSDS packet data length. + */ + uint16_t getPacketDataLength( void ); //uint16_t is sufficient, because this is limit in CCSDS standard + /** + * Sets the packet data length, which is the fifth and sixth byte of the + * CCSDS Primary Header. + * @param setLength The value of the length to set. It must fit the true + * CCSDS packet data length . The packet data length is + * the size of every kind of data \b after the CCSDS + * Primary Header \b -1. + */ + void setPacketDataLength( uint16_t setLength ); + + //Helper methods: + /** + * This method returns a raw uint8_t pointer to the packet. + * @return A \c uint8_t pointer to the first byte of the CCSDS primary header. + */ + virtual uint8_t* getWholeData( void ); + + uint8_t* getPacketData(); + /** + * With this method, the packet data pointer can be redirected to another + * location. + * @param p_Data A pointer to another raw Space Packet. + */ + virtual void setData( const uint8_t* p_Data ); + /** + * This method returns the full raw packet size. + * @return The full size of the packet in bytes. + */ + size_t getFullSize(); + + uint32_t getApidAndSequenceCount() const; + +}; + +#endif /* FSFW_TMTCPACKET_SPACEPACKETBASE_H_ */ diff --git a/fsfw/tmtcpacket/ccsds_header.h b/fsfw/tmtcpacket/ccsds_header.h new file mode 100644 index 0000000..40d6829 --- /dev/null +++ b/fsfw/tmtcpacket/ccsds_header.h @@ -0,0 +1,15 @@ +#ifndef CCSDS_HEADER_H_ +#define CCSDS_HEADER_H_ + +#include + +struct CCSDSPrimaryHeader { + uint8_t packet_id_h; + uint8_t packet_id_l; + uint8_t sequence_control_h; + uint8_t sequence_control_l; + uint8_t packet_length_h; + uint8_t packet_length_l; +}; + +#endif /* CCSDS_HEADER_H_ */ diff --git a/fsfw/tmtcpacket/packetmatcher/ApidMatcher.h b/fsfw/tmtcpacket/packetmatcher/ApidMatcher.h new file mode 100644 index 0000000..4f196ac --- /dev/null +++ b/fsfw/tmtcpacket/packetmatcher/ApidMatcher.h @@ -0,0 +1,40 @@ +#ifndef FRAMEWORK_TMTCPACKET_PACKETMATCHER_APIDMATCHER_H_ +#define FRAMEWORK_TMTCPACKET_PACKETMATCHER_APIDMATCHER_H_ + +#include "../../globalfunctions/matching/SerializeableMatcherIF.h" +#include "../../serialize/SerializeAdapter.h" +#include "../../tmtcpacket/pus/TmPacketMinimal.h" + +class ApidMatcher: public SerializeableMatcherIF { +private: + uint16_t apid; +public: + ApidMatcher(uint16_t setApid) : + apid(setApid) { + } + ApidMatcher(TmPacketMinimal* test) : + apid(test->getAPID()) { + } + bool match(TmPacketMinimal* packet) { + if (packet->getAPID() == apid) { + return true; + } else { + return false; + } + } + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + return SerializeAdapter::serialize(&apid, buffer, size, maxSize, streamEndianness); + } + size_t getSerializedSize() const { + return SerializeAdapter::getSerializedSize(&apid); + } + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + return SerializeAdapter::deSerialize(&apid, buffer, size, streamEndianness); + } +}; + + + +#endif /* FRAMEWORK_TMTCPACKET_PACKETMATCHER_APIDMATCHER_H_ */ diff --git a/fsfw/tmtcpacket/packetmatcher/PacketMatchTree.cpp b/fsfw/tmtcpacket/packetmatcher/PacketMatchTree.cpp new file mode 100644 index 0000000..a457965 --- /dev/null +++ b/fsfw/tmtcpacket/packetmatcher/PacketMatchTree.cpp @@ -0,0 +1,195 @@ +#include "ApidMatcher.h" +#include "PacketMatchTree.h" +#include "ServiceMatcher.h" +#include "SubserviceMatcher.h" + +PacketMatchTree::PacketMatchTree(Node* root) : + MatchTree(root, 2), factoryBackend(0, POOL_SIZES, + N_ELEMENTS, false, true), factory(&factoryBackend) { +} + +PacketMatchTree::PacketMatchTree(iterator root) : + MatchTree(root.element, 2), factoryBackend(0, + POOL_SIZES, N_ELEMENTS, false, true), factory(&factoryBackend) { +} + +PacketMatchTree::PacketMatchTree() : + MatchTree((Node*) NULL, 2), factoryBackend(0, + POOL_SIZES, N_ELEMENTS, false, true), factory(&factoryBackend) { +} + +PacketMatchTree::~PacketMatchTree() { +} + +ReturnValue_t PacketMatchTree::addMatch(uint16_t apid, uint8_t type, + uint8_t subtype) { + //We assume adding APID is always requested. + TmPacketMinimal::TmPacketMinimalPointer data; + data.data_field.service_type = type; + data.data_field.service_subtype = subtype; + TmPacketMinimal testPacket((uint8_t*) &data); + testPacket.setAPID(apid); + iterator lastTest; + iterator rollback; + ReturnValue_t result = findOrInsertMatch( + this->begin(), &testPacket, &lastTest); + if (result == NEW_NODE_CREATED) { + rollback = lastTest; + } else if (result != RETURN_OK) { + return result; + } + if (type == 0) { + //Check if lastTest has no children, otherwise, delete them, + //as a more general check is requested. + if (lastTest.left() != this->end()) { + removeElementAndAllChildren(lastTest.left()); + } + return RETURN_OK; + } + //Type insertion required. + result = findOrInsertMatch( + lastTest.left(), &testPacket, &lastTest); + if (result == NEW_NODE_CREATED) { + if (rollback == this->end()) { + rollback = lastTest; + } + } else if (result != RETURN_OK) { + if (rollback != this->end()) { + removeElementAndAllChildren(rollback); + } + return result; + } + if (subtype == 0) { + if (lastTest.left() != this->end()) { + //See above + removeElementAndAllChildren(lastTest.left()); + } + return RETURN_OK; + } + //Subtype insertion required. + result = findOrInsertMatch( + lastTest.left(), &testPacket, &lastTest); + if (result == NEW_NODE_CREATED) { + return RETURN_OK; + } else if (result != RETURN_OK) { + if (rollback != this->end()) { + removeElementAndAllChildren(rollback); + } + return result; + } + return RETURN_OK; +} + +template +ReturnValue_t PacketMatchTree::findOrInsertMatch(iterator startAt, VALUE_T test, + iterator* lastTest) { + bool attachToBranch = AND; + iterator iter = startAt; + while (iter != this->end()) { + bool isMatch = iter->match(test); + attachToBranch = OR; + *lastTest = iter; + if (isMatch) { + return RETURN_OK; + } else { + //Go down OR branch. + iter = iter.right(); + } + } + //Only reached if nothing was found. + SerializeableMatcherIF* newContent = factory.generate( + test); + if (newContent == NULL) { + return FULL; + } + Node* newNode = factory.generate(newContent); + if (newNode == NULL) { + //Need to make sure partially generated content is deleted, otherwise, that's a leak. + factory.destroy(static_cast(newContent)); + return FULL; + } + *lastTest = insert(attachToBranch, *lastTest, newNode); + if (*lastTest == end()) { + //This actaully never fails, so creating a dedicated returncode seems an overshoot. + return RETURN_FAILED; + } + return NEW_NODE_CREATED; +} + +ReturnValue_t PacketMatchTree::removeMatch(uint16_t apid, uint8_t type, + uint8_t subtype) { + TmPacketMinimal::TmPacketMinimalPointer data; + data.data_field.service_type = type; + data.data_field.service_subtype = subtype; + TmPacketMinimal testPacket((uint8_t*) &data); + testPacket.setAPID(apid); + iterator foundElement = findMatch(begin(), &testPacket); + if (foundElement == this->end()) { + return NO_MATCH; + } + if (type == 0) { + if (foundElement.left() == end()) { + return removeElementAndReconnectChildren(foundElement); + } else { + return TOO_GENERAL_REQUEST; + } + } + //Go down AND branch. Will abort if empty. + foundElement = findMatch(foundElement.left(), &testPacket); + if (foundElement == this->end()) { + return NO_MATCH; + } + if (subtype == 0) { + if (foundElement.left() == end()) { + return removeElementAndReconnectChildren(foundElement); + } else { + return TOO_GENERAL_REQUEST; + } + } + //Again, go down AND branch. + foundElement = findMatch(foundElement.left(), &testPacket); + if (foundElement == end()) { + return NO_MATCH; + } + return removeElementAndReconnectChildren(foundElement); +} + +PacketMatchTree::iterator PacketMatchTree::findMatch(iterator startAt, + TmPacketMinimal* test) { + iterator iter = startAt; + while (iter != end()) { + bool isMatch = iter->match(test); + if (isMatch) { + break; + } else { + iter = iter.right(); //next OR element + } + } + return iter; +} + +ReturnValue_t PacketMatchTree::initialize() { + return factoryBackend.initialize(); +} + +const uint16_t PacketMatchTree::POOL_SIZES[N_POOLS] = { sizeof(ServiceMatcher), + sizeof(SubServiceMatcher), sizeof(ApidMatcher), + sizeof(PacketMatchTree::Node) }; +//Maximum number of types and subtypes to filter should be more than sufficient. +const uint16_t PacketMatchTree::N_ELEMENTS[N_POOLS] = { 10, 20, 2, 40 }; + +ReturnValue_t PacketMatchTree::changeMatch(bool addToMatch, uint16_t apid, + uint8_t type, uint8_t subtype) { + if (addToMatch) { + return addMatch(apid, type, subtype); + } else { + return removeMatch(apid, type, subtype); + } +} + +ReturnValue_t PacketMatchTree::cleanUpElement(iterator position) { + factory.destroy(position.element->value); + //Go on anyway, there's nothing we can do. + //SHOULDDO: Throw event, or write debug message? + return factory.destroy(position.element); +} diff --git a/fsfw/tmtcpacket/packetmatcher/PacketMatchTree.h b/fsfw/tmtcpacket/packetmatcher/PacketMatchTree.h new file mode 100644 index 0000000..86fb087 --- /dev/null +++ b/fsfw/tmtcpacket/packetmatcher/PacketMatchTree.h @@ -0,0 +1,36 @@ +#ifndef FRAMEWORK_TMTCPACKET_PACKETMATCHER_PACKETMATCHTREE_H_ +#define FRAMEWORK_TMTCPACKET_PACKETMATCHER_PACKETMATCHTREE_H_ + +#include "../../container/PlacementFactory.h" +#include "../../globalfunctions/matching/MatchTree.h" +#include "../../storagemanager/LocalPool.h" +#include "../../tmtcpacket/pus/TmPacketMinimal.h" + +class PacketMatchTree: public MatchTree, public HasReturnvaluesIF { +public: + PacketMatchTree(Node* root); + PacketMatchTree(iterator root); + PacketMatchTree(); + virtual ~PacketMatchTree(); + ReturnValue_t changeMatch(bool addToMatch, uint16_t apid, uint8_t type = 0, + uint8_t subtype = 0); + ReturnValue_t addMatch(uint16_t apid, uint8_t type = 0, + uint8_t subtype = 0); + ReturnValue_t removeMatch(uint16_t apid, uint8_t type = 0, + uint8_t subtype = 0); + ReturnValue_t initialize(); +protected: + ReturnValue_t cleanUpElement(iterator position); +private: + static const uint8_t N_POOLS = 4; + LocalPool factoryBackend; + PlacementFactory factory; + static const uint16_t POOL_SIZES[N_POOLS]; + static const uint16_t N_ELEMENTS[N_POOLS]; + template + ReturnValue_t findOrInsertMatch(iterator startAt, VALUE_T test, iterator* lastTest); + iterator findMatch(iterator startAt, TmPacketMinimal* test); +}; + +#endif /* FRAMEWORK_TMTCPACKET_PACKETMATCHER_PACKETMATCHTREE_H_ */ + diff --git a/fsfw/tmtcpacket/packetmatcher/ServiceMatcher.h b/fsfw/tmtcpacket/packetmatcher/ServiceMatcher.h new file mode 100644 index 0000000..eba23d7 --- /dev/null +++ b/fsfw/tmtcpacket/packetmatcher/ServiceMatcher.h @@ -0,0 +1,39 @@ +#ifndef FRAMEWORK_TMTCPACKET_PACKETMATCHER_SERVICEMATCHER_H_ +#define FRAMEWORK_TMTCPACKET_PACKETMATCHER_SERVICEMATCHER_H_ + +#include "../../globalfunctions/matching/SerializeableMatcherIF.h" +#include "../../serialize/SerializeAdapter.h" +#include "../../tmtcpacket/pus/TmPacketMinimal.h" + +class ServiceMatcher: public SerializeableMatcherIF { +private: + uint8_t service; +public: + ServiceMatcher(uint8_t setService) : + service(setService) { + } + ServiceMatcher(TmPacketMinimal* test) : + service(test->getService()) { + } + bool match(TmPacketMinimal* packet) { + if (packet->getService() == service) { + return true; + } else { + return false; + } + } + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + return SerializeAdapter::serialize(&service, buffer, size, maxSize, streamEndianness); + } + size_t getSerializedSize() const { + return SerializeAdapter::getSerializedSize(&service); + } + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + return SerializeAdapter::deSerialize(&service, buffer, size, streamEndianness); + } +}; + + +#endif /* FRAMEWORK_TMTCPACKET_PACKETMATCHER_SERVICEMATCHER_H_ */ diff --git a/fsfw/tmtcpacket/packetmatcher/SubserviceMatcher.h b/fsfw/tmtcpacket/packetmatcher/SubserviceMatcher.h new file mode 100644 index 0000000..a9b6def --- /dev/null +++ b/fsfw/tmtcpacket/packetmatcher/SubserviceMatcher.h @@ -0,0 +1,40 @@ +#ifndef FRAMEWORK_TMTCPACKET_PACKETMATCHER_SUBSERVICEMATCHER_H_ +#define FRAMEWORK_TMTCPACKET_PACKETMATCHER_SUBSERVICEMATCHER_H_ + +#include "../../globalfunctions/matching/SerializeableMatcherIF.h" +#include "../../serialize/SerializeAdapter.h" +#include "../../tmtcpacket/pus/TmPacketMinimal.h" + +class SubServiceMatcher: public SerializeableMatcherIF { +public: + SubServiceMatcher(uint8_t subService) : + subService(subService) { + } + SubServiceMatcher(TmPacketMinimal* test) : + subService(test->getSubService()) { + } + bool match(TmPacketMinimal* packet) { + if (packet->getSubService() == subService) { + return true; + } else { + return false; + } + } + ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t maxSize, Endianness streamEndianness) const { + return SerializeAdapter::serialize(&subService, buffer, size, maxSize, streamEndianness); + } + size_t getSerializedSize() const { + return SerializeAdapter::getSerializedSize(&subService); + } + ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + return SerializeAdapter::deSerialize(&subService, buffer, size, streamEndianness); + } +private: + uint8_t subService; +}; + + + +#endif /* FRAMEWORK_TMTCPACKET_PACKETMATCHER_SUBSERVICEMATCHER_H_ */ diff --git a/fsfw/tmtcpacket/pus/PacketTimestampInterpreterIF.h b/fsfw/tmtcpacket/pus/PacketTimestampInterpreterIF.h new file mode 100644 index 0000000..40e7a2e --- /dev/null +++ b/fsfw/tmtcpacket/pus/PacketTimestampInterpreterIF.h @@ -0,0 +1,17 @@ +#ifndef FRAMEWORK_TMTCPACKET_PUS_PACKETTIMESTAMPINTERPRETERIF_H_ +#define FRAMEWORK_TMTCPACKET_PUS_PACKETTIMESTAMPINTERPRETERIF_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" +class TmPacketMinimal; + +class PacketTimestampInterpreterIF { +public: + virtual ~PacketTimestampInterpreterIF() {} + virtual ReturnValue_t getPacketTime(TmPacketMinimal* packet, + timeval* timestamp) const = 0; + virtual ReturnValue_t getPacketTimeRaw(TmPacketMinimal* packet, const uint8_t** timePtr, uint32_t* size) const = 0; +}; + + + +#endif /* FRAMEWORK_TMTCPACKET_PUS_PACKETTIMESTAMPINTERPRETERIF_H_ */ diff --git a/fsfw/tmtcpacket/pus/TcPacketBase.cpp b/fsfw/tmtcpacket/pus/TcPacketBase.cpp new file mode 100644 index 0000000..eaa8416 --- /dev/null +++ b/fsfw/tmtcpacket/pus/TcPacketBase.cpp @@ -0,0 +1,86 @@ +#include "TcPacketBase.h" + +#include "../../globalfunctions/CRC.h" +#include "../../globalfunctions/arrayprinter.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +#include + +TcPacketBase::TcPacketBase(const uint8_t* setData) : + SpacePacketBase(setData) { + tcData = reinterpret_cast(const_cast(setData)); +} + +TcPacketBase::~TcPacketBase() { + //Nothing to do. +} + +uint8_t TcPacketBase::getService() { + return tcData->dataField.service_type; +} + +uint8_t TcPacketBase::getSubService() { + return tcData->dataField.service_subtype; +} + +uint8_t TcPacketBase::getAcknowledgeFlags() { + return tcData->dataField.version_type_ack & 0b00001111; +} + +const uint8_t* TcPacketBase::getApplicationData() const { + return &tcData->appData; +} + +uint16_t TcPacketBase::getApplicationDataSize() { + return getPacketDataLength() - sizeof(tcData->dataField) - CRC_SIZE + 1; +} + +uint16_t TcPacketBase::getErrorControl() { + uint16_t size = getApplicationDataSize() + CRC_SIZE; + uint8_t* p_to_buffer = &tcData->appData; + return (p_to_buffer[size - 2] << 8) + p_to_buffer[size - 1]; +} + +void TcPacketBase::setErrorControl() { + uint32_t full_size = getFullSize(); + uint16_t crc = CRC::crc16ccitt(getWholeData(), full_size - CRC_SIZE); + uint32_t size = getApplicationDataSize(); + (&tcData->appData)[size] = (crc & 0XFF00) >> 8; // CRCH + (&tcData->appData)[size + 1] = (crc) & 0X00FF; // CRCL +} + +void TcPacketBase::setData(const uint8_t* pData) { + SpacePacketBase::setData(pData); + tcData = (TcPacketPointer*) pData; +} + +uint8_t TcPacketBase::getSecondaryHeaderFlag() { + return (tcData->dataField.version_type_ack & 0b10000000) >> 7; +} + +uint8_t TcPacketBase::getPusVersionNumber() { + return (tcData->dataField.version_type_ack & 0b01110000) >> 4; +} + +void TcPacketBase::print() { + sif::debug << "TcPacketBase::print: " << std::endl; + arrayprinter::print(getWholeData(), getFullSize()); +} + +void TcPacketBase::initializeTcPacket(uint16_t apid, uint16_t sequenceCount, + uint8_t ack, uint8_t service, uint8_t subservice) { + initSpacePacketHeader(true, true, apid, sequenceCount); + std::memset(&tcData->dataField, 0, sizeof(tcData->dataField)); + setPacketDataLength(sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1); + //Data Field Header: + //Set CCSDS_secondary_header_flag to 0 and version number to 001 + tcData->dataField.version_type_ack = 0b00010000; + tcData->dataField.version_type_ack |= (ack & 0x0F); + tcData->dataField.service_type = service; + tcData->dataField.service_subtype = subservice; +} + +size_t TcPacketBase::calculateFullPacketLength(size_t appDataLen) { + return sizeof(CCSDSPrimaryHeader) + sizeof(PUSTcDataFieldHeader) + + appDataLen + TcPacketBase::CRC_SIZE; +} diff --git a/fsfw/tmtcpacket/pus/TcPacketBase.h b/fsfw/tmtcpacket/pus/TcPacketBase.h new file mode 100644 index 0000000..582a221 --- /dev/null +++ b/fsfw/tmtcpacket/pus/TcPacketBase.h @@ -0,0 +1,187 @@ +#ifndef TMTCPACKET_PUS_TCPACKETBASE_H_ +#define TMTCPACKET_PUS_TCPACKETBASE_H_ + +#include "../../tmtcpacket/SpacePacketBase.h" +#include + + +/** + * This struct defines a byte-wise structured PUS TC Data Field Header. + * Any optional fields in the header must be added or removed here. + * Currently, the Source Id field is present with one byte. + * @ingroup tmtcpackets + */ +struct PUSTcDataFieldHeader { + uint8_t version_type_ack; + uint8_t service_type; + uint8_t service_subtype; + uint8_t source_id; +}; + +/** + * This struct defines the data structure of a PUS Telecommand Packet when + * accessed via a pointer. + * @ingroup tmtcpackets + */ +struct TcPacketPointer { + CCSDSPrimaryHeader primary; + PUSTcDataFieldHeader dataField; + uint8_t appData; +}; + +/** + * This class is the basic data handler for any ECSS PUS Telecommand packet. + * + * In addition to #SpacePacketBase, the class provides methods to handle + * the standardized entries of the PUS TC Packet Data Field Header. + * It does not contain the packet data itself but a pointer to the + * data must be set on instantiation. An invalid pointer may cause + * damage, as no getter method checks data validity. Anyway, a NULL + * check can be performed by making use of the getWholeData method. + * @ingroup tmtcpackets + */ +class TcPacketBase : public SpacePacketBase { +public: + static const uint16_t TC_PACKET_MIN_SIZE = (sizeof(CCSDSPrimaryHeader) + + sizeof(PUSTcDataFieldHeader) + 2); + + enum AckField { + //! No acknowledgements are expected. + ACK_NONE = 0b0000, + //! Acknowledgements on acceptance are expected. + ACK_ACCEPTANCE = 0b0001, + //! Acknowledgements on start are expected. + ACK_START = 0b0010, + //! Acknowledgements on step are expected. + ACK_STEP = 0b0100, + //! Acknowledfgement on completion are expected. + ACK_COMPLETION = 0b1000 + }; + + static constexpr uint8_t ACK_ALL = ACK_ACCEPTANCE | ACK_START | ACK_STEP | + ACK_COMPLETION; + + /** + * This is the default constructor. + * It sets its internal data pointer to the address passed and also + * forwards the data pointer to the parent SpacePacketBase class. + * @param setData The position where the packet data lies. + */ + TcPacketBase( const uint8_t* setData ); + /** + * This is the empty default destructor. + */ + virtual ~TcPacketBase(); + + /** + * This command returns the CCSDS Secondary Header Flag. + * It shall always be zero for PUS Packets. This is the + * highest bit of the first byte of the Data Field Header. + * @return the CCSDS Secondary Header Flag + */ + uint8_t getSecondaryHeaderFlag(); + /** + * This command returns the TC Packet PUS Version Number. + * The version number of ECSS PUS 2003 is 1. + * It consists of the second to fourth highest bits of the + * first byte. + * @return + */ + uint8_t getPusVersionNumber(); + /** + * This is a getter for the packet's Ack field, which are the lowest four + * bits of the first byte of the Data Field Header. + * + * It is packed in a uint8_t variable. + * @return The packet's PUS Ack field. + */ + uint8_t getAcknowledgeFlags(); + /** + * This is a getter for the packet's PUS Service ID, which is the second + * byte of the Data Field Header. + * @return The packet's PUS Service ID. + */ + uint8_t getService(); + /** + * This is a getter for the packet's PUS Service Subtype, which is the + * third byte of the Data Field Header. + * @return The packet's PUS Service Subtype. + */ + uint8_t getSubService(); + /** + * This is a getter for a pointer to the packet's Application data. + * + * These are the bytes that follow after the Data Field Header. They form + * the packet's application data. + * @return A pointer to the PUS Application Data. + */ + const uint8_t* getApplicationData() const; + /** + * This method calculates the size of the PUS Application data field. + * + * It takes the information stored in the CCSDS Packet Data Length field + * and subtracts the Data Field Header size and the CRC size. + * @return The size of the PUS Application Data (without Error Control + * field) + */ + uint16_t getApplicationDataSize(); + /** + * This getter returns the Error Control Field of the packet. + * + * The field is placed after any possible Application Data. If no + * Application Data is present there's still an Error Control field. It is + * supposed to be a 16bit-CRC. + * @return The PUS Error Control + */ + uint16_t getErrorControl(); + /** + * With this method, the Error Control Field is updated to match the + * current content of the packet. + */ + void setErrorControl(); + + /** + * This is a debugging helper method that prints the whole packet content + * to the screen. + */ + void print(); + /** + * Calculate full packet length from application data length. + * @param appDataLen + * @return + */ + static size_t calculateFullPacketLength(size_t appDataLen); + +protected: + /** + * A pointer to a structure which defines the data structure of + * the packet's data. + * + * To be hardware-safe, all elements are of byte size. + */ + TcPacketPointer* tcData; + + /** + * Initializes the Tc Packet header. + * @param apid APID used. + * @param sequenceCount Sequence Count in the primary header. + * @param ack Which acknowledeges are expected from the receiver. + * @param service PUS Service + * @param subservice PUS Subservice + */ + void initializeTcPacket(uint16_t apid, uint16_t sequenceCount, uint8_t ack, + uint8_t service, uint8_t subservice); + + /** + * With this method, the packet data pointer can be redirected to another + * location. + * This call overwrites the parent's setData method to set both its + * @c tc_data pointer and the parent's @c data pointer. + * + * @param p_data A pointer to another PUS Telecommand Packet. + */ + void setData( const uint8_t* pData ); +}; + + +#endif /* TMTCPACKET_PUS_TCPACKETBASE_H_ */ diff --git a/fsfw/tmtcpacket/pus/TcPacketStored.cpp b/fsfw/tmtcpacket/pus/TcPacketStored.cpp new file mode 100644 index 0000000..fffc86a --- /dev/null +++ b/fsfw/tmtcpacket/pus/TcPacketStored.cpp @@ -0,0 +1,117 @@ +#include "TcPacketStored.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +#include + +StorageManagerIF* TcPacketStored::store = nullptr; + +TcPacketStored::TcPacketStored(store_address_t setAddress) : + TcPacketBase(nullptr), storeAddress(setAddress) { + setStoreAddress(storeAddress); +} + +TcPacketStored::TcPacketStored(uint16_t apid, uint8_t service, + uint8_t subservice, uint8_t sequenceCount, const uint8_t* data, + size_t size, uint8_t ack) : + TcPacketBase(nullptr) { + this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + if (not this->checkAndSetStore()) { + return; + } + uint8_t* pData = nullptr; + ReturnValue_t returnValue = this->store->getFreeElement(&this->storeAddress, + (TC_PACKET_MIN_SIZE + size), &pData); + if (returnValue != this->store->RETURN_OK) { + sif::warning << "TcPacketStored: Could not get free element from store!" + << std::endl; + return; + } + this->setData(pData); + initializeTcPacket(apid, sequenceCount, ack, service, subservice); + memcpy(&tcData->appData, data, size); + this->setPacketDataLength( + size + sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1); + this->setErrorControl(); +} + +ReturnValue_t TcPacketStored::getData(const uint8_t ** dataPtr, + size_t* dataSize) { + auto result = this->store->getData(storeAddress, dataPtr, dataSize); + if(result != HasReturnvaluesIF::RETURN_OK) { + sif::warning << "TcPacketStored: Could not get data!" << std::endl; + } + return result; +} + +TcPacketStored::TcPacketStored(): TcPacketBase(nullptr) { + this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + this->checkAndSetStore(); + +} + +ReturnValue_t TcPacketStored::deletePacket() { + ReturnValue_t result = this->store->deleteData(this->storeAddress); + this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + this->setData(nullptr); + return result; +} + +bool TcPacketStored::checkAndSetStore() { + if (this->store == nullptr) { + this->store = objectManager->get(objects::TC_STORE); + if (this->store == nullptr) { + sif::error << "TcPacketStored::TcPacketStored: TC Store not found!" + << std::endl; + return false; + } + } + return true; +} + +void TcPacketStored::setStoreAddress(store_address_t setAddress) { + this->storeAddress = setAddress; + const uint8_t* tempData = nullptr; + size_t temp_size; + ReturnValue_t status = StorageManagerIF::RETURN_FAILED; + if (this->checkAndSetStore()) { + status = this->store->getData(this->storeAddress, &tempData, + &temp_size); + } + if (status == StorageManagerIF::RETURN_OK) { + this->setData(tempData); + } else { + this->setData(nullptr); + this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + } +} + +store_address_t TcPacketStored::getStoreAddress() { + return this->storeAddress; +} + +bool TcPacketStored::isSizeCorrect() { + const uint8_t* temp_data = nullptr; + size_t temp_size; + ReturnValue_t status = this->store->getData(this->storeAddress, &temp_data, + &temp_size); + if (status == StorageManagerIF::RETURN_OK) { + if (this->getFullSize() == temp_size) { + return true; + } + } + return false; +} + +TcPacketStored::TcPacketStored(const uint8_t* data, uint32_t size) : + TcPacketBase(data) { + if (getFullSize() != size) { + return; + } + if (this->checkAndSetStore()) { + ReturnValue_t status = store->addData(&storeAddress, data, size); + if (status != HasReturnvaluesIF::RETURN_OK) { + this->setData(nullptr); + } + } +} diff --git a/fsfw/tmtcpacket/pus/TcPacketStored.h b/fsfw/tmtcpacket/pus/TcPacketStored.h new file mode 100644 index 0000000..1666107 --- /dev/null +++ b/fsfw/tmtcpacket/pus/TcPacketStored.h @@ -0,0 +1,117 @@ +#ifndef TMTCPACKET_PUS_TCPACKETSTORED_H_ +#define TMTCPACKET_PUS_TCPACKETSTORED_H_ + +#include "TcPacketBase.h" +#include "../../storagemanager/StorageManagerIF.h" + +/** + * This class generates a ECSS PUS Telecommand packet within a given + * intermediate storage. + * As most packets are passed between tasks with the help of a storage + * anyway, it seems logical to create a Packet-In-Storage access class + * which saves the user almost all storage handling operation. + * Packets can both be newly created with the class and be "linked" to + * packets in a store with the help of a storeAddress. + * @ingroup tmtcpackets + */ +class TcPacketStored : public TcPacketBase { +public: + /** + * This is a default constructor which does not set the data pointer. + * However, it does try to set the packet store. + */ + TcPacketStored(); + /** + * With this constructor, the class instance is linked to an existing + * packet in the packet store. + * The packet content is neither checked nor changed with this call. If + * the packet could not be found, the data pointer is set to NULL. + */ + TcPacketStored( store_address_t setAddress ); + /** + * With this constructor, new space is allocated in the packet store and + * a new PUS Telecommand Packet is created there. + * Packet Application Data passed in data is copied into the packet. + * @param apid Sets the packet's APID field. + * @param service Sets the packet's Service ID field. + * This specifies the destination service. + * @param subservice Sets the packet's Service Subtype field. + * This specifies the destination sub-service. + * @param sequence_count Sets the packet's Source Sequence Count field. + * @param data The data to be copied to the Application Data Field. + * @param size The amount of data to be copied. + * @param ack Set's the packet's Ack field, which specifies + * number of verification packets returned + * for this command. + */ + TcPacketStored(uint16_t apid, uint8_t service, uint8_t subservice, + uint8_t sequence_count = 0, const uint8_t* data = nullptr, + size_t size = 0, uint8_t ack = TcPacketBase::ACK_ALL); + /** + * Another constructor to create a TcPacket from a raw packet stream. + * Takes the data and adds it unchecked to the TcStore. + * @param data Pointer to the complete TC Space Packet. + * @param Size size of the packet. + */ + TcPacketStored( const uint8_t* data, uint32_t size); + + /** + * Getter function for the raw data. + * @param dataPtr [out] Pointer to the data pointer to set + * @param dataSize [out] Address of size to set. + * @return -@c RETURN_OK if data was retrieved successfully. + */ + ReturnValue_t getData(const uint8_t ** dataPtr, + size_t* dataSize); + /** + * This is a getter for the current store address of the packet. + * @return The current store address. The (raw) value is + * @c StorageManagerIF::INVALID_ADDRESS if the packet is not linked. + */ + store_address_t getStoreAddress(); + /** + * With this call, the packet is deleted. + * It removes itself from the store and sets its data pointer to NULL. + * @return returncode from deleting the data. + */ + ReturnValue_t deletePacket(); + /** + * With this call, a packet can be linked to another store. This is useful + * if the packet is a class member and used for more than one packet. + * @param setAddress The new packet id to link to. + */ + void setStoreAddress( store_address_t setAddress ); + /** + * This method performs a size check. + * It reads the stored size and compares it with the size entered in the + * packet header. This class is the optimal place for such a check as it + * has access to both the header data and the store. + * @return true if size is correct, false if packet is not registered in + * store or size is incorrect. + */ + bool isSizeCorrect(); + +private: + /** + * This is a pointer to the store all instances of the class use. + * If the store is not yet set (i.e. @c store is NULL), every constructor + * call tries to set it and throws an error message in case of failures. + * The default store is objects::TC_STORE. + */ + static StorageManagerIF* store; + /** + * The address where the packet data of the object instance is stored. + */ + store_address_t storeAddress; + /** + * A helper method to check if a store is assigned to the class. + * If not, the method tries to retrieve the store from the global + * ObjectManager. + * @return @li @c true if the store is linked or could be created. + * @li @c false otherwise. + */ + bool checkAndSetStore(); +}; + + +#endif /* TMTCPACKET_PUS_TCPACKETSTORED_H_ */ diff --git a/fsfw/tmtcpacket/pus/TmPacketBase.cpp b/fsfw/tmtcpacket/pus/TmPacketBase.cpp new file mode 100644 index 0000000..f3bc3f0 --- /dev/null +++ b/fsfw/tmtcpacket/pus/TmPacketBase.cpp @@ -0,0 +1,117 @@ +#include "TmPacketBase.h" + +#include "../../globalfunctions/CRC.h" +#include "../../globalfunctions/arrayprinter.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../timemanager/CCSDSTime.h" + +#include + +TimeStamperIF* TmPacketBase::timeStamper = nullptr; +object_id_t TmPacketBase::timeStamperId = 0; + +TmPacketBase::TmPacketBase(uint8_t* setData) : + SpacePacketBase(setData) { + tmData = reinterpret_cast(setData); +} + +TmPacketBase::~TmPacketBase() { + //Nothing to do. +} + +uint8_t TmPacketBase::getService() { + return tmData->data_field.service_type; +} + +uint8_t TmPacketBase::getSubService() { + return tmData->data_field.service_subtype; +} + +uint8_t* TmPacketBase::getSourceData() { + return &tmData->data; +} + +uint16_t TmPacketBase::getSourceDataSize() { + return getPacketDataLength() - sizeof(tmData->data_field) + - CRC_SIZE + 1; +} + +uint16_t TmPacketBase::getErrorControl() { + uint32_t size = getSourceDataSize() + CRC_SIZE; + uint8_t* p_to_buffer = &tmData->data; + return (p_to_buffer[size - 2] << 8) + p_to_buffer[size - 1]; +} + +void TmPacketBase::setErrorControl() { + uint32_t full_size = getFullSize(); + uint16_t crc = CRC::crc16ccitt(getWholeData(), full_size - CRC_SIZE); + uint32_t size = getSourceDataSize(); + getSourceData()[size] = (crc & 0XFF00) >> 8; // CRCH + getSourceData()[size + 1] = (crc) & 0X00FF; // CRCL +} + +void TmPacketBase::setData(const uint8_t* p_Data) { + SpacePacketBase::setData(p_Data); + tmData = (TmPacketPointer*) p_Data; +} + +void TmPacketBase::print() { + sif::debug << "TmPacketBase::print: " << std::endl; + arrayprinter::print(getWholeData(), getFullSize()); +} + +bool TmPacketBase::checkAndSetStamper() { + if (timeStamper == NULL) { + timeStamper = objectManager->get(timeStamperId); + if (timeStamper == NULL) { + sif::error << "TmPacketBase::checkAndSetStamper: Stamper not found!" + << std::endl; + return false; + } + } + return true; +} + +ReturnValue_t TmPacketBase::getPacketTime(timeval* timestamp) const { + uint32_t tempSize = 0; + return CCSDSTime::convertFromCcsds(timestamp, tmData->data_field.time, + &tempSize, sizeof(tmData->data_field.time)); +} + +uint8_t* TmPacketBase::getPacketTimeRaw() const{ + return tmData->data_field.time; + +} + +void TmPacketBase::initializeTmPacket(uint16_t apid, uint8_t service, + uint8_t subservice, uint8_t packetSubcounter) { + //Set primary header: + initSpacePacketHeader(false, true, apid); + //Set data Field Header: + //First, set to zero. + memset(&tmData->data_field, 0, sizeof(tmData->data_field)); + + // NOTE: In PUS-C, the PUS Version is 2 and specified for the first 4 bits. + // The other 4 bits of the first byte are the spacecraft time reference + // status. To change to PUS-C, set 0b00100000. + // Set CCSDS_secondary header flag to 0, version number to 001 and ack + // to 0000 + tmData->data_field.version_type_ack = 0b00010000; + tmData->data_field.service_type = service; + tmData->data_field.service_subtype = subservice; + tmData->data_field.subcounter = packetSubcounter; + //Timestamp packet + if (checkAndSetStamper()) { + timeStamper->addTimeStamp(tmData->data_field.time, + sizeof(tmData->data_field.time)); + } +} + +void TmPacketBase::setSourceDataSize(uint16_t size) { + setPacketDataLength(size + sizeof(PUSTmDataFieldHeader) + CRC_SIZE - 1); +} + +size_t TmPacketBase::getTimestampSize() const { + return sizeof(tmData->data_field.time); +} diff --git a/fsfw/tmtcpacket/pus/TmPacketBase.h b/fsfw/tmtcpacket/pus/TmPacketBase.h new file mode 100644 index 0000000..6efc016 --- /dev/null +++ b/fsfw/tmtcpacket/pus/TmPacketBase.h @@ -0,0 +1,196 @@ +#ifndef TMTCPACKET_PUS_TMPACKETBASE_H_ +#define TMTCPACKET_PUS_TMPACKETBASE_H_ + +#include "../SpacePacketBase.h" +#include "../../timemanager/TimeStamperIF.h" +#include "../../timemanager/Clock.h" +#include "../../objectmanager/SystemObjectIF.h" + +namespace Factory{ +void setStaticFrameworkObjectIds(); +} + +/** + * This struct defines a byte-wise structured PUS TM Data Field Header. + * Any optional fields in the header must be added or removed here. + * Currently, no Destination field is present, but an eigth-byte representation + * for a time tag. + * @ingroup tmtcpackets + */ +struct PUSTmDataFieldHeader { + uint8_t version_type_ack; + uint8_t service_type; + uint8_t service_subtype; + uint8_t subcounter; +// uint8_t destination; + uint8_t time[TimeStamperIF::MISSION_TIMESTAMP_SIZE]; +}; + +/** + * This struct defines the data structure of a PUS Telecommand Packet when + * accessed via a pointer. + * @ingroup tmtcpackets + */ +struct TmPacketPointer { + CCSDSPrimaryHeader primary; + PUSTmDataFieldHeader data_field; + uint8_t data; +}; + +/** + * This class is the basic data handler for any ECSS PUS Telemetry packet. + * + * In addition to #SpacePacketBase, the class provides methods to handle + * the standardized entries of the PUS TM Packet Data Field Header. + * It does not contain the packet data itself but a pointer to the + * data must be set on instantiation. An invalid pointer may cause + * damage, as no getter method checks data validity. Anyway, a NULL + * check can be performed by making use of the getWholeData method. + * @ingroup tmtcpackets + */ +class TmPacketBase : public SpacePacketBase { + friend void (Factory::setStaticFrameworkObjectIds)(); +public: + /** + * This constant defines the minimum size of a valid PUS Telemetry Packet. + */ + static const uint32_t TM_PACKET_MIN_SIZE = (sizeof(CCSDSPrimaryHeader) + + sizeof(PUSTmDataFieldHeader) + 2); + //! Maximum size of a TM Packet in this mission. + //! TODO: Make this dependant on a config variable. + static const uint32_t MISSION_TM_PACKET_MAX_SIZE = 2048; + //! First byte of secondary header for PUS-A packets. + //! TODO: Maybe also support PUS-C via config? + static const uint8_t VERSION_NUMBER_BYTE_PUS_A = 0b00010000; + + /** + * This is the default constructor. + * It sets its internal data pointer to the address passed and also + * forwards the data pointer to the parent SpacePacketBase class. + * @param set_address The position where the packet data lies. + */ + TmPacketBase( uint8_t* setData ); + /** + * This is the empty default destructor. + */ + virtual ~TmPacketBase(); + + /** + * This is a getter for the packet's PUS Service ID, which is the second + * byte of the Data Field Header. + * @return The packet's PUS Service ID. + */ + uint8_t getService(); + /** + * This is a getter for the packet's PUS Service Subtype, which is the + * third byte of the Data Field Header. + * @return The packet's PUS Service Subtype. + */ + uint8_t getSubService(); + /** + * This is a getter for a pointer to the packet's Source data. + * + * These are the bytes that follow after the Data Field Header. They form + * the packet's source data. + * @return A pointer to the PUS Source Data. + */ + uint8_t* getSourceData(); + /** + * This method calculates the size of the PUS Source data field. + * + * It takes the information stored in the CCSDS Packet Data Length field + * and subtracts the Data Field Header size and the CRC size. + * @return The size of the PUS Source Data (without Error Control field) + */ + uint16_t getSourceDataSize(); + + /** + * With this method, the Error Control Field is updated to match the + * current content of the packet. This method is not protected because + * a recalculation by the user might be necessary when manipulating fields + * like the sequence count. + */ + void setErrorControl(); + + /** + * This getter returns the Error Control Field of the packet. + * + * The field is placed after any possible Source Data. If no + * Source Data is present there's still an Error Control field. It is + * supposed to be a 16bit-CRC. + * @return The PUS Error Control + */ + uint16_t getErrorControl(); + + /** + * This is a debugging helper method that prints the whole packet content + * to the screen. + */ + void print(); + /** + * Interprets the "time"-field in the secondary header and returns it in + * timeval format. + * @return Converted timestamp of packet. + */ + ReturnValue_t getPacketTime(timeval* timestamp) const; + /** + * Returns a raw pointer to the beginning of the time field. + * @return Raw pointer to time field. + */ + uint8_t* getPacketTimeRaw() const; + + size_t getTimestampSize() const; + +protected: + /** + * A pointer to a structure which defines the data structure of + * the packet's data. + * + * To be hardware-safe, all elements are of byte size. + */ + TmPacketPointer* tmData; + /** + * The timeStamper is responsible for adding a timestamp to the packet. + * It is initialized lazy. + */ + static TimeStamperIF* timeStamper; + //! The ID to use when looking for a time stamper. + static object_id_t timeStamperId; + + /** + * Initializes the Tm Packet header. + * Does set the timestamp (to now), but not the error control field. + * @param apid APID used. + * @param service PUS Service + * @param subservice PUS Subservice + * @param packetSubcounter Additional subcounter used. + */ + void initializeTmPacket(uint16_t apid, uint8_t service, uint8_t subservice, + uint8_t packetSubcounter); + + /** + * With this method, the packet data pointer can be redirected to another + * location. + * + * This call overwrites the parent's setData method to set both its + * @c tc_data pointer and the parent's @c data pointer. + * + * @param p_data A pointer to another PUS Telemetry Packet. + */ + void setData( const uint8_t* pData ); + + /** + * In case data was filled manually (almost never the case). + * @param size Size of source data (without CRC and data filed header!). + */ + void setSourceDataSize(uint16_t size); + + /** + * Checks if a time stamper is available and tries to set it if not. + * @return Returns false if setting failed. + */ + bool checkAndSetStamper(); +}; + + +#endif /* TMTCPACKET_PUS_TMPACKETBASE_H_ */ diff --git a/fsfw/tmtcpacket/pus/TmPacketMinimal.cpp b/fsfw/tmtcpacket/pus/TmPacketMinimal.cpp new file mode 100644 index 0000000..18e9dda --- /dev/null +++ b/fsfw/tmtcpacket/pus/TmPacketMinimal.cpp @@ -0,0 +1,45 @@ +#include "TmPacketMinimal.h" +#include +#include +#include "PacketTimestampInterpreterIF.h" + +TmPacketMinimal::TmPacketMinimal(const uint8_t* set_data) : SpacePacketBase( set_data ) { + this->tm_data = (TmPacketMinimalPointer*)set_data; +} + +TmPacketMinimal::~TmPacketMinimal() { +} + +uint8_t TmPacketMinimal::getService() { + return tm_data->data_field.service_type; +} + +uint8_t TmPacketMinimal::getSubService() { + return tm_data->data_field.service_subtype; +} + +uint8_t TmPacketMinimal::getPacketSubcounter() { + return tm_data->data_field.subcounter; +} + +ReturnValue_t TmPacketMinimal::getPacketTime(timeval* timestamp) { + if (timestampInterpreter == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + return timestampInterpreter->getPacketTime(this, timestamp); +} + +ReturnValue_t TmPacketMinimal::getPacketTimeRaw(const uint8_t** timePtr, uint32_t* size) { + if (timestampInterpreter == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + return timestampInterpreter->getPacketTimeRaw(this, timePtr, size); +} + +void TmPacketMinimal::setInterpretTimestampObject(PacketTimestampInterpreterIF* interpreter) { + if (TmPacketMinimal::timestampInterpreter == NULL) { + TmPacketMinimal::timestampInterpreter = interpreter; + } +} + +PacketTimestampInterpreterIF* TmPacketMinimal::timestampInterpreter = NULL; diff --git a/fsfw/tmtcpacket/pus/TmPacketMinimal.h b/fsfw/tmtcpacket/pus/TmPacketMinimal.h new file mode 100644 index 0000000..728acb1 --- /dev/null +++ b/fsfw/tmtcpacket/pus/TmPacketMinimal.h @@ -0,0 +1,84 @@ +#ifndef FRAMEWORK_TMTCPACKET_PUS_TMPACKETMINIMAL_H_ +#define FRAMEWORK_TMTCPACKET_PUS_TMPACKETMINIMAL_H_ + + +#include "../../tmtcpacket/SpacePacketBase.h" +#include "../../returnvalues/HasReturnvaluesIF.h" + +struct timeval; +class PacketTimestampInterpreterIF; +/** + * This is a minimal version of a PUS TmPacket without any variable field, or, + * in other words with Service Type, Subtype and subcounter only. + * This is required for handling TM packets with different APIDs with different + * secondary headers. + */ +class TmPacketMinimal : public SpacePacketBase { +public: + /** + * This is the default constructor. + * It sets its internal data pointer to the address passed and also + * forwards the data pointer to the parent SpacePacketBase class. + * @param set_address The position where the packet data lies. + */ + TmPacketMinimal( const uint8_t* set_data ); + /** + * This is the empty default destructor. + */ + virtual ~TmPacketMinimal(); + /** + * This is a getter for the packet's PUS Service ID, which is the second + * byte of the Data Field Header. + * @return The packet's PUS Service ID. + */ + uint8_t getService(); + /** + * This is a getter for the packet's PUS Service Subtype, which is the + * third byte of the Data Field Header. + * @return The packet's PUS Service Subtype. + */ + uint8_t getSubService(); + /** + * Returns the subcounter. + * @return the subcounter of the Data Field Header. + */ + uint8_t getPacketSubcounter(); + struct PUSTmMinimalHeader { + uint8_t version_type_ack; + uint8_t service_type; + uint8_t service_subtype; + uint8_t subcounter; + }; + + ReturnValue_t getPacketTime(timeval* timestamp); + + ReturnValue_t getPacketTimeRaw(const uint8_t** timePtr, uint32_t* size); + + static void setInterpretTimestampObject(PacketTimestampInterpreterIF* interpreter); + /** + * This struct defines the data structure of a PUS Telecommand Packet when + * accessed via a pointer. + * @ingroup tmtcpackets + */ + struct TmPacketMinimalPointer { + CCSDSPrimaryHeader primary; + PUSTmMinimalHeader data_field; + uint8_t rest; + }; + //Must include a checksum and is therefore at least one larger than the above struct. + static const uint16_t MINIMUM_SIZE = sizeof(TmPacketMinimalPointer) +1; +protected: + /** + * A pointer to a structure which defines the data structure of + * the packet's data. + * + * To be hardware-safe, all elements are of byte size. + */ + TmPacketMinimalPointer* tm_data; + + static PacketTimestampInterpreterIF* timestampInterpreter; +}; + + + +#endif /* FRAMEWORK_TMTCPACKET_PUS_TMPACKETMINIMAL_H_ */ diff --git a/fsfw/tmtcpacket/pus/TmPacketStored.cpp b/fsfw/tmtcpacket/pus/TmPacketStored.cpp new file mode 100644 index 0000000..0fb789a --- /dev/null +++ b/fsfw/tmtcpacket/pus/TmPacketStored.cpp @@ -0,0 +1,145 @@ +#include "TmPacketStored.h" + +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tmtcservices/TmTcMessage.h" + +#include + +StorageManagerIF *TmPacketStored::store = nullptr; +InternalErrorReporterIF *TmPacketStored::internalErrorReporter = nullptr; + +TmPacketStored::TmPacketStored(store_address_t setAddress) : + TmPacketBase(nullptr), storeAddress(setAddress) { + setStoreAddress(storeAddress); +} + +TmPacketStored::TmPacketStored(uint16_t apid, uint8_t service, + uint8_t subservice, uint8_t packetSubcounter, const uint8_t *data, + uint32_t size, const uint8_t *headerData, uint32_t headerSize) : + TmPacketBase(NULL) { + storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + if (not checkAndSetStore()) { + return; + } + uint8_t *pData = nullptr; + ReturnValue_t returnValue = store->getFreeElement(&storeAddress, + (TmPacketBase::TM_PACKET_MIN_SIZE + size + headerSize), &pData); + + if (returnValue != store->RETURN_OK) { + checkAndReportLostTm(); + return; + } + setData(pData); + initializeTmPacket(apid, service, subservice, packetSubcounter); + memcpy(getSourceData(), headerData, headerSize); + memcpy(getSourceData() + headerSize, data, size); + setPacketDataLength( + size + headerSize + sizeof(PUSTmDataFieldHeader) + CRC_SIZE - 1); +} + +TmPacketStored::TmPacketStored(uint16_t apid, uint8_t service, + uint8_t subservice, uint8_t packetSubcounter, SerializeIF *content, + SerializeIF *header) : + TmPacketBase(NULL) { + storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + if (not checkAndSetStore()) { + return; + } + size_t sourceDataSize = 0; + if (content != NULL) { + sourceDataSize += content->getSerializedSize(); + } + if (header != NULL) { + sourceDataSize += header->getSerializedSize(); + } + uint8_t *p_data = NULL; + ReturnValue_t returnValue = store->getFreeElement(&storeAddress, + (TmPacketBase::TM_PACKET_MIN_SIZE + sourceDataSize), &p_data); + if (returnValue != store->RETURN_OK) { + checkAndReportLostTm(); + } + setData(p_data); + initializeTmPacket(apid, service, subservice, packetSubcounter); + uint8_t *putDataHere = getSourceData(); + size_t size = 0; + if (header != NULL) { + header->serialize(&putDataHere, &size, sourceDataSize, + SerializeIF::Endianness::BIG); + } + if (content != NULL) { + content->serialize(&putDataHere, &size, sourceDataSize, + SerializeIF::Endianness::BIG); + } + setPacketDataLength( + sourceDataSize + sizeof(PUSTmDataFieldHeader) + CRC_SIZE - 1); +} + +store_address_t TmPacketStored::getStoreAddress() { + return storeAddress; +} + +void TmPacketStored::deletePacket() { + store->deleteData(storeAddress); + storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + setData(nullptr); +} + +void TmPacketStored::setStoreAddress(store_address_t setAddress) { + storeAddress = setAddress; + const uint8_t* tempData = nullptr; + size_t tempSize; + if (not checkAndSetStore()) { + return; + } + ReturnValue_t status = store->getData(storeAddress, &tempData, &tempSize); + if (status == StorageManagerIF::RETURN_OK) { + setData(tempData); + } else { + setData(nullptr); + storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + } +} + +bool TmPacketStored::checkAndSetStore() { + if (store == nullptr) { + store = objectManager->get(objects::TM_STORE); + if (store == nullptr) { + sif::error << "TmPacketStored::TmPacketStored: TM Store not found!" + << std::endl; + return false; + } + } + return true; +} + +ReturnValue_t TmPacketStored::sendPacket(MessageQueueId_t destination, + MessageQueueId_t sentFrom, bool doErrorReporting) { + if (getWholeData() == nullptr) { + //SHOULDDO: More decent code. + return HasReturnvaluesIF::RETURN_FAILED; + } + TmTcMessage tmMessage(getStoreAddress()); + ReturnValue_t result = MessageQueueSenderIF::sendMessage(destination, + &tmMessage, sentFrom); + if (result != HasReturnvaluesIF::RETURN_OK) { + deletePacket(); + if (doErrorReporting) { + checkAndReportLostTm(); + } + return result; + } + //SHOULDDO: In many cases, some counter is incremented for successfully sent packets. The check is often not done, but just incremented. + return HasReturnvaluesIF::RETURN_OK; + +} + +void TmPacketStored::checkAndReportLostTm() { + if (internalErrorReporter == nullptr) { + internalErrorReporter = objectManager->get( + objects::INTERNAL_ERROR_REPORTER); + } + if (internalErrorReporter != nullptr) { + internalErrorReporter->lostTm(); + } +} diff --git a/fsfw/tmtcpacket/pus/TmPacketStored.h b/fsfw/tmtcpacket/pus/TmPacketStored.h new file mode 100644 index 0000000..6ddbf13 --- /dev/null +++ b/fsfw/tmtcpacket/pus/TmPacketStored.h @@ -0,0 +1,108 @@ +#ifndef TMTCPACKET_PUS_TMPACKETSTORED_H_ +#define TMTCPACKET_PUS_TMPACKETSTORED_H_ + +#include "TmPacketBase.h" + +#include "../../serialize/SerializeIF.h" +#include "../../storagemanager/StorageManagerIF.h" +#include "../../internalError/InternalErrorReporterIF.h" +#include "../../ipc/MessageQueueSenderIF.h" + +/** + * This class generates a ECSS PUS Telemetry packet within a given + * intermediate storage. + * As most packets are passed between tasks with the help of a storage + * anyway, it seems logical to create a Packet-In-Storage access class + * which saves the user almost all storage handling operation. + * Packets can both be newly created with the class and be "linked" to + * packets in a store with the help of a storeAddress. + * @ingroup tmtcpackets + */ +class TmPacketStored : public TmPacketBase { +public: + /** + * This is a default constructor which does not set the data pointer. + * However, it does try to set the packet store. + */ + TmPacketStored( store_address_t setAddress ); + /** + * With this constructor, new space is allocated in the packet store and + * a new PUS Telemetry Packet is created there. + * Packet Application Data passed in data is copied into the packet. + * The Application data is passed in two parts, first a header, then a + * data field. This allows building a Telemetry Packet from two separate + * data sources. + * @param apid Sets the packet's APID field. + * @param service Sets the packet's Service ID field. + * This specifies the source service. + * @param subservice Sets the packet's Service Subtype field. + * This specifies the source sub-service. + * @param packet_counter Sets the Packet counter field of this packet + * @param data The payload data to be copied to the + * Application Data Field + * @param size The amount of data to be copied. + * @param headerData The header Data of the Application field, + * will be copied in front of data + * @param headerSize The size of the headerDataF + */ + TmPacketStored( uint16_t apid, uint8_t service, uint8_t subservice, + uint8_t packet_counter = 0, const uint8_t* data = nullptr, + uint32_t size = 0, const uint8_t* headerData = nullptr, + uint32_t headerSize = 0); + /** + * Another ctor to directly pass structured content and header data to the + * packet to avoid additional buffers. + */ + TmPacketStored( uint16_t apid, uint8_t service, uint8_t subservice, + uint8_t packet_counter, SerializeIF* content, + SerializeIF* header = nullptr); + /** + * This is a getter for the current store address of the packet. + * @return The current store address. The (raw) value is + * @c StorageManagerIF::INVALID_ADDRESS if + * the packet is not linked. + */ + store_address_t getStoreAddress(); + /** + * With this call, the packet is deleted. + * It removes itself from the store and sets its data pointer to NULL. + */ + void deletePacket(); + /** + * With this call, a packet can be linked to another store. This is useful + * if the packet is a class member and used for more than one packet. + * @param setAddress The new packet id to link to. + */ + void setStoreAddress( store_address_t setAddress ); + + ReturnValue_t sendPacket( MessageQueueId_t destination, + MessageQueueId_t sentFrom, bool doErrorReporting = true ); +private: + /** + * This is a pointer to the store all instances of the class use. + * If the store is not yet set (i.e. @c store is NULL), every constructor + * call tries to set it and throws an error message in case of failures. + * The default store is objects::TM_STORE. + */ + static StorageManagerIF* store; + + static InternalErrorReporterIF *internalErrorReporter; + + /** + * The address where the packet data of the object instance is stored. + */ + store_address_t storeAddress; + /** + * A helper method to check if a store is assigned to the class. + * If not, the method tries to retrieve the store from the global + * ObjectManager. + * @return @li @c true if the store is linked or could be created. + * @li @c false otherwise. + */ + bool checkAndSetStore(); + + void checkAndReportLostTm(); +}; + + +#endif /* TMTCPACKET_PUS_TMPACKETSTORED_H_ */ diff --git a/fsfw/tmtcservices/AcceptsTelecommandsIF.h b/fsfw/tmtcservices/AcceptsTelecommandsIF.h new file mode 100644 index 0000000..76d10a5 --- /dev/null +++ b/fsfw/tmtcservices/AcceptsTelecommandsIF.h @@ -0,0 +1,43 @@ +#ifndef ACCEPTSTELECOMMANDSIF_H_ +#define ACCEPTSTELECOMMANDSIF_H_ + +#include "../ipc/MessageQueueSenderIF.h" + +/** + * @brief This interface is implemented by classes that are sinks for + * Telecommands. + * @details Any service receiving telecommands shall implement this interface + * and thus make the service id and the receiving message queue public. + */ +class AcceptsTelecommandsIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::ACCEPTS_TELECOMMANDS_IF; + static const ReturnValue_t ACTIVITY_STARTED = MAKE_RETURN_CODE(1); + static const ReturnValue_t INVALID_SUBSERVICE = MAKE_RETURN_CODE(2); + static const ReturnValue_t ILLEGAL_APPLICATION_DATA = MAKE_RETURN_CODE(3); + static const ReturnValue_t SEND_TM_FAILED = MAKE_RETURN_CODE(4); + static const ReturnValue_t TIMEOUT = MAKE_RETURN_CODE(5); + /** + * @brief The virtual destructor as it is mandatory for C++ interfaces. + */ + virtual ~AcceptsTelecommandsIF() { + + } + /** + * @brief Getter for the service id. + * @details Any receiving service (at least any PUS service) shall have a + * service id. If the receiver can handle Telecommands, but for + * some reason has no service id, it shall return 0. + * @return The service id or 0. + */ + virtual uint16_t getIdentifier() = 0; + /** + * @brief This method returns the message queue id of the telecommand + * receiving message queue. + * @return The telecommand reception message queue id. + */ + virtual MessageQueueId_t getRequestQueue() = 0; +}; + + +#endif /* ACCEPTSTELECOMMANDSIF_H_ */ diff --git a/fsfw/tmtcservices/AcceptsTelemetryIF.h b/fsfw/tmtcservices/AcceptsTelemetryIF.h new file mode 100644 index 0000000..2325dbe --- /dev/null +++ b/fsfw/tmtcservices/AcceptsTelemetryIF.h @@ -0,0 +1,26 @@ +#ifndef ACCEPTSTELEMETRYIF_H_ +#define ACCEPTSTELEMETRYIF_H_ + +#include "../ipc/MessageQueueSenderIF.h" +/** + * @brief This interface is implemented by classes that are sinks for + * Telemetry. + * @details Any object receiving telemetry shall implement this interface + * and thus make the service id and the receiving message queue public. + */ +class AcceptsTelemetryIF { +public: + /** + * @brief The virtual destructor as it is mandatory for C++ interfaces. + */ + virtual ~AcceptsTelemetryIF() { + } + /** + * @brief This method returns the message queue id of the telemetry + * receiving message queue. + * @return The telemetry reception message queue id. + */ + virtual MessageQueueId_t getReportReceptionQueue(uint8_t virtualChannel = 0) = 0; +}; + +#endif /* ACCEPTSTELEMETRYIF_H_ */ diff --git a/fsfw/tmtcservices/AcceptsVerifyMessageIF.h b/fsfw/tmtcservices/AcceptsVerifyMessageIF.h new file mode 100644 index 0000000..c9318ab --- /dev/null +++ b/fsfw/tmtcservices/AcceptsVerifyMessageIF.h @@ -0,0 +1,15 @@ +#ifndef ACCEPTSVERIFICATIONMESSAGEIF_H_ +#define ACCEPTSVERIFICATIONMESSAGEIF_H_ + +#include "../ipc/MessageQueueSenderIF.h" + +class AcceptsVerifyMessageIF { +public: + virtual ~AcceptsVerifyMessageIF() { + + } + virtual MessageQueueId_t getVerificationQueue() = 0; +}; + + +#endif /* ACCEPTSVERIFICATIONMESSAGEIF_H_ */ diff --git a/fsfw/tmtcservices/CommandingServiceBase.cpp b/fsfw/tmtcservices/CommandingServiceBase.cpp new file mode 100644 index 0000000..0ebc394 --- /dev/null +++ b/fsfw/tmtcservices/CommandingServiceBase.cpp @@ -0,0 +1,427 @@ +#include "../tcdistribution/PUSDistributorIF.h" +#include "AcceptsTelemetryIF.h" +#include "../objectmanager/ObjectManagerIF.h" + +#include "CommandingServiceBase.h" +#include "TmTcMessage.h" +#include "../ipc/QueueFactory.h" +#include "../tmtcpacket/pus/TcPacketStored.h" +#include "../tmtcpacket/pus/TmPacketStored.h" + +object_id_t CommandingServiceBase::defaultPacketSource = objects::NO_OBJECT; +object_id_t CommandingServiceBase::defaultPacketDestination = objects::NO_OBJECT; + +CommandingServiceBase::CommandingServiceBase(object_id_t setObjectId, + uint16_t apid, uint8_t service, uint8_t numberOfParallelCommands, + uint16_t commandTimeoutSeconds, size_t queueDepth) : + SystemObject(setObjectId), apid(apid), service(service), + timeoutSeconds(commandTimeoutSeconds), + commandMap(numberOfParallelCommands) { + commandQueue = QueueFactory::instance()->createMessageQueue(queueDepth); + requestQueue = QueueFactory::instance()->createMessageQueue(queueDepth); +} + +void CommandingServiceBase::setPacketSource(object_id_t packetSource) { + this->packetSource = packetSource; +} + +void CommandingServiceBase::setPacketDestination( + object_id_t packetDestination) { + this->packetDestination = packetDestination; +} + + +CommandingServiceBase::~CommandingServiceBase() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); + QueueFactory::instance()->deleteMessageQueue(requestQueue); +} + + +ReturnValue_t CommandingServiceBase::performOperation(uint8_t opCode) { + handleCommandQueue(); + handleRequestQueue(); + checkTimeout(); + doPeriodicOperation(); + return RETURN_OK; +} + + +uint16_t CommandingServiceBase::getIdentifier() { + return service; +} + + +MessageQueueId_t CommandingServiceBase::getRequestQueue() { + return requestQueue->getId(); +} + + +ReturnValue_t CommandingServiceBase::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + + if(packetDestination == objects::NO_OBJECT) { + packetDestination = defaultPacketDestination; + } + AcceptsTelemetryIF* packetForwarding = + objectManager->get(packetDestination); + + if(packetSource == objects::NO_OBJECT) { + packetSource = defaultPacketSource; + } + PUSDistributorIF* distributor = objectManager->get( + packetSource); + + if (packetForwarding == nullptr or distributor == nullptr) { + sif::error << "CommandingServiceBase::intialize: Packet source or " + "packet destination invalid!" << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + distributor->registerService(this); + requestQueue->setDefaultDestination( + packetForwarding->getReportReceptionQueue()); + + IPCStore = objectManager->get(objects::IPC_STORE); + TCStore = objectManager->get(objects::TC_STORE); + + if (IPCStore == nullptr or TCStore == nullptr) { + sif::error << "CommandingServiceBase::intialize: IPC store or TC store " + "not initialized yet!" << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + return RETURN_OK; + +} + +void CommandingServiceBase::handleCommandQueue() { + CommandMessage reply; + ReturnValue_t result = RETURN_FAILED; + for (result = commandQueue->receiveMessage(&reply); result == RETURN_OK; + result = commandQueue->receiveMessage(&reply)) { + handleCommandMessage(&reply); + } +} + + +void CommandingServiceBase::handleCommandMessage(CommandMessage* reply) { + bool isStep = false; + CommandMessage nextCommand; + CommandMapIter iter = commandMap.find(reply->getSender()); + + // handle unrequested reply first + if (reply->getSender() == MessageQueueIF::NO_QUEUE or + iter == commandMap.end()) { + handleUnrequestedReply(reply); + return; + } + nextCommand.setCommand(CommandMessage::CMD_NONE); + + + // Implemented by child class, specifies what to do with reply. + ReturnValue_t result = handleReply(reply, iter->second.command, &iter->second.state, + &nextCommand, iter->second.objectId, &isStep); + + /* If the child implementation does not implement special handling for + * rejected replies (RETURN_FAILED or INVALID_REPLY is returned), a + * failure verification will be generated with the reason as the + * return code and the initial command as failure parameter 1 */ + if((reply->getCommand() == CommandMessage::REPLY_REJECTED) and + (result == RETURN_FAILED or result == INVALID_REPLY)) { + result = reply->getReplyRejectedReason(); + failureParameter1 = iter->second.command; + } + + switch (result) { + case EXECUTION_COMPLETE: + case RETURN_OK: + case NO_STEP_MESSAGE: + // handle result of reply handler implemented by developer. + handleReplyHandlerResult(result, iter, &nextCommand, reply, isStep); + break; + case INVALID_REPLY: + //might be just an unrequested reply at a bad moment + handleUnrequestedReply(reply); + break; + default: + if (isStep) { + verificationReporter.sendFailureReport( + TC_VERIFY::PROGRESS_FAILURE, iter->second.tcInfo.ackFlags, + iter->second.tcInfo.tcPacketId, iter->second.tcInfo.tcSequenceControl, + result, ++iter->second.step, failureParameter1, + failureParameter2); + } else { + verificationReporter.sendFailureReport( + TC_VERIFY::COMPLETION_FAILURE, iter->second.tcInfo.ackFlags, + iter->second.tcInfo.tcPacketId, iter->second.tcInfo.tcSequenceControl, + result, 0, failureParameter1, failureParameter2); + } + failureParameter1 = 0; + failureParameter2 = 0; + checkAndExecuteFifo(iter); + break; + } + +} + +void CommandingServiceBase::handleReplyHandlerResult(ReturnValue_t result, + CommandMapIter iter, CommandMessage* nextCommand, + CommandMessage* reply, bool& isStep) { + iter->second.command = nextCommand->getCommand(); + + // In case a new command is to be sent immediately, this is performed here. + // If no new command is sent, only analyse reply result by initializing + // sendResult as RETURN_OK + ReturnValue_t sendResult = RETURN_OK; + if (nextCommand->getCommand() != CommandMessage::CMD_NONE) { + sendResult = commandQueue->sendMessage(reply->getSender(), + nextCommand); + } + + if (sendResult == RETURN_OK) { + if (isStep and result != NO_STEP_MESSAGE) { + verificationReporter.sendSuccessReport( + TC_VERIFY::PROGRESS_SUCCESS, + iter->second.tcInfo.ackFlags, iter->second.tcInfo.tcPacketId, + iter->second.tcInfo.tcSequenceControl, ++iter->second.step); + } + else { + verificationReporter.sendSuccessReport( + TC_VERIFY::COMPLETION_SUCCESS, + iter->second.tcInfo.ackFlags, iter->second.tcInfo.tcPacketId, + iter->second.tcInfo.tcSequenceControl, 0); + checkAndExecuteFifo(iter); + } + } + else { + if (isStep) { + nextCommand->clearCommandMessage(); + verificationReporter.sendFailureReport( + TC_VERIFY::PROGRESS_FAILURE, iter->second.tcInfo.ackFlags, + iter->second.tcInfo.tcPacketId, + iter->second.tcInfo.tcSequenceControl, sendResult, + ++iter->second.step, failureParameter1, failureParameter2); + } else { + nextCommand->clearCommandMessage(); + verificationReporter.sendFailureReport( + TC_VERIFY::COMPLETION_FAILURE, + iter->second.tcInfo.ackFlags, iter->second.tcInfo.tcPacketId, + iter->second.tcInfo.tcSequenceControl, sendResult, 0, + failureParameter1, failureParameter2); + } + failureParameter1 = 0; + failureParameter2 = 0; + checkAndExecuteFifo(iter); + } +} + +void CommandingServiceBase::handleRequestQueue() { + TmTcMessage message; + ReturnValue_t result; + store_address_t address; + TcPacketStored packet; + MessageQueueId_t queue; + object_id_t objectId; + for (result = requestQueue->receiveMessage(&message); result == RETURN_OK; + result = requestQueue->receiveMessage(&message)) { + address = message.getStorageId(); + packet.setStoreAddress(address); + + if ((packet.getSubService() == 0) + or (isValidSubservice(packet.getSubService()) != RETURN_OK)) { + rejectPacket(TC_VERIFY::START_FAILURE, &packet, INVALID_SUBSERVICE); + continue; + } + result = getMessageQueueAndObject(packet.getSubService(), + packet.getApplicationData(), packet.getApplicationDataSize(), + &queue, &objectId); + if (result != HasReturnvaluesIF::RETURN_OK) { + rejectPacket(TC_VERIFY::START_FAILURE, &packet, result); + continue; + } + + //Is a command already active for the target object? + CommandMapIter iter; + iter = commandMap.find(queue); + + if (iter != commandMap.end()) { + result = iter->second.fifo.insert(address); + if (result != RETURN_OK) { + rejectPacket(TC_VERIFY::START_FAILURE, &packet, OBJECT_BUSY); + } + } else { + CommandInfo newInfo; //Info will be set by startExecution if neccessary + newInfo.objectId = objectId; + result = commandMap.insert(queue, newInfo, &iter); + if (result != RETURN_OK) { + rejectPacket(TC_VERIFY::START_FAILURE, &packet, BUSY); + } else { + startExecution(&packet, iter); + } + } + + } +} + + +ReturnValue_t CommandingServiceBase::sendTmPacket(uint8_t subservice, + const uint8_t* data, size_t dataLen, const uint8_t* headerData, + size_t headerSize) { + TmPacketStored tmPacketStored(this->apid, this->service, subservice, + this->tmPacketCounter, data, dataLen, headerData, headerSize); + ReturnValue_t result = tmPacketStored.sendPacket( + requestQueue->getDefaultDestination(), requestQueue->getId()); + if (result == HasReturnvaluesIF::RETURN_OK) { + this->tmPacketCounter++; + } + return result; +} + + +ReturnValue_t CommandingServiceBase::sendTmPacket(uint8_t subservice, + object_id_t objectId, const uint8_t *data, size_t dataLen) { + uint8_t buffer[sizeof(object_id_t)]; + uint8_t* pBuffer = buffer; + size_t size = 0; + SerializeAdapter::serialize(&objectId, &pBuffer, &size, + sizeof(object_id_t), SerializeIF::Endianness::BIG); + TmPacketStored tmPacketStored(this->apid, this->service, subservice, + this->tmPacketCounter, data, dataLen, buffer, size); + ReturnValue_t result = tmPacketStored.sendPacket( + requestQueue->getDefaultDestination(), requestQueue->getId()); + if (result == HasReturnvaluesIF::RETURN_OK) { + this->tmPacketCounter++; + } + return result; +} + + +ReturnValue_t CommandingServiceBase::sendTmPacket(uint8_t subservice, + SerializeIF* content, SerializeIF* header) { + TmPacketStored tmPacketStored(this->apid, this->service, subservice, + this->tmPacketCounter, content, header); + ReturnValue_t result = tmPacketStored.sendPacket( + requestQueue->getDefaultDestination(), requestQueue->getId()); + if (result == HasReturnvaluesIF::RETURN_OK) { + this->tmPacketCounter++; + } + return result; +} + + +void CommandingServiceBase::startExecution(TcPacketStored *storedPacket, + CommandMapIter iter) { + ReturnValue_t result = RETURN_OK; + CommandMessage command; + iter->second.subservice = storedPacket->getSubService(); + result = prepareCommand(&command, iter->second.subservice, + storedPacket->getApplicationData(), + storedPacket->getApplicationDataSize(), &iter->second.state, + iter->second.objectId); + + ReturnValue_t sendResult = RETURN_OK; + switch (result) { + case RETURN_OK: + if (command.getCommand() != CommandMessage::CMD_NONE) { + sendResult = commandQueue->sendMessage(iter.value->first, + &command); + } + if (sendResult == RETURN_OK) { + Clock::getUptime(&iter->second.uptimeOfStart); + iter->second.step = 0; + iter->second.subservice = storedPacket->getSubService(); + iter->second.command = command.getCommand(); + iter->second.tcInfo.ackFlags = storedPacket->getAcknowledgeFlags(); + iter->second.tcInfo.tcPacketId = storedPacket->getPacketId(); + iter->second.tcInfo.tcSequenceControl = + storedPacket->getPacketSequenceControl(); + acceptPacket(TC_VERIFY::START_SUCCESS, storedPacket); + } else { + command.clearCommandMessage(); + rejectPacket(TC_VERIFY::START_FAILURE, storedPacket, sendResult); + checkAndExecuteFifo(iter); + } + break; + case EXECUTION_COMPLETE: + if (command.getCommand() != CommandMessage::CMD_NONE) { + //Fire-and-forget command. + sendResult = commandQueue->sendMessage(iter.value->first, + &command); + } + if (sendResult == RETURN_OK) { + verificationReporter.sendSuccessReport(TC_VERIFY::START_SUCCESS, + storedPacket); + acceptPacket(TC_VERIFY::COMPLETION_SUCCESS, storedPacket); + checkAndExecuteFifo(iter); + } else { + command.clearCommandMessage(); + rejectPacket(TC_VERIFY::START_FAILURE, storedPacket, sendResult); + checkAndExecuteFifo(iter); + } + break; + default: + rejectPacket(TC_VERIFY::START_FAILURE, storedPacket, result); + checkAndExecuteFifo(iter); + break; + } +} + + +void CommandingServiceBase::rejectPacket(uint8_t report_id, + TcPacketStored* packet, ReturnValue_t error_code) { + verificationReporter.sendFailureReport(report_id, packet, error_code); + packet->deletePacket(); +} + + +void CommandingServiceBase::acceptPacket(uint8_t reportId, + TcPacketStored* packet) { + verificationReporter.sendSuccessReport(reportId, packet); + packet->deletePacket(); +} + + +void CommandingServiceBase::checkAndExecuteFifo(CommandMapIter& iter) { + store_address_t address; + if (iter->second.fifo.retrieve(&address) != RETURN_OK) { + commandMap.erase(&iter); + } else { + TcPacketStored newPacket(address); + startExecution(&newPacket, iter); + } +} + + +void CommandingServiceBase::handleUnrequestedReply(CommandMessage* reply) { + reply->clearCommandMessage(); +} + + +inline void CommandingServiceBase::doPeriodicOperation() { +} + +MessageQueueId_t CommandingServiceBase::getCommandQueue() { + return commandQueue->getId(); +} + +void CommandingServiceBase::checkTimeout() { + uint32_t uptime; + Clock::getUptime(&uptime); + CommandMapIter iter; + for (iter = commandMap.begin(); iter != commandMap.end(); ++iter) { + if ((iter->second.uptimeOfStart + (timeoutSeconds * 1000)) < uptime) { + verificationReporter.sendFailureReport( + TC_VERIFY::COMPLETION_FAILURE, iter->second.tcInfo.ackFlags, + iter->second.tcInfo.tcPacketId, iter->second.tcInfo.tcSequenceControl, + TIMEOUT); + checkAndExecuteFifo(iter); + } + } +} + +void CommandingServiceBase::setTaskIF(PeriodicTaskIF* task_) { + executingTask = task_; +} diff --git a/fsfw/tmtcservices/CommandingServiceBase.h b/fsfw/tmtcservices/CommandingServiceBase.h new file mode 100644 index 0000000..252b694 --- /dev/null +++ b/fsfw/tmtcservices/CommandingServiceBase.h @@ -0,0 +1,365 @@ +#ifndef FSFW_TMTCSERVICES_COMMANDINGSERVICEBASE_H_ +#define FSFW_TMTCSERVICES_COMMANDINGSERVICEBASE_H_ + +#include "../objectmanager/SystemObject.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../ipc/MessageQueueIF.h" +#include "AcceptsTelecommandsIF.h" + +#include "VerificationReporter.h" +#include "../ipc/CommandMessage.h" +#include "../container/FixedMap.h" +#include "../container/FIFO.h" +#include "../serialize/SerializeIF.h" + +class TcPacketStored; + +namespace Factory{ +void setStaticFrameworkObjectIds(); +} + +/** + * @brief This class is the basis for all PUS Services, which have to + * relay Telecommands to software bus. + * + * It manages Telecommand reception and the generation of Verification Reports + * similar to PusServiceBase. This class is used if a telecommand can't be + * handled immediately and must be relayed to the internal software bus. + * - isValidSubservice + * - getMessageQueueAndObject + * - prepareCommand + * - handleReply + * @author gaisser + * @ingroup pus_services + */ +class CommandingServiceBase: public SystemObject, + public AcceptsTelecommandsIF, + public ExecutableObjectIF, + public HasReturnvaluesIF { + friend void (Factory::setStaticFrameworkObjectIds)(); +public: + // We could make this configurable via preprocessor and the FSFWConfig file. + static constexpr uint8_t COMMAND_INFO_FIFO_DEPTH = 3; + + static const uint8_t INTERFACE_ID = CLASS_ID::COMMAND_SERVICE_BASE; + + static const ReturnValue_t EXECUTION_COMPLETE = MAKE_RETURN_CODE(1); + static const ReturnValue_t NO_STEP_MESSAGE = MAKE_RETURN_CODE(2); + static const ReturnValue_t OBJECT_BUSY = MAKE_RETURN_CODE(3); + static const ReturnValue_t BUSY = MAKE_RETURN_CODE(4); + static const ReturnValue_t INVALID_TC = MAKE_RETURN_CODE(5); + static const ReturnValue_t INVALID_OBJECT = MAKE_RETURN_CODE(6); + static const ReturnValue_t INVALID_REPLY = MAKE_RETURN_CODE(7); + + /** + * Class constructor. Initializes two important MessageQueues: + * commandQueue for command reception and requestQueue for device reception + * @param setObjectId + * @param apid + * @param service + * @param numberOfParallelCommands + * @param commandTimeout_seconds + * @param setPacketSource + * @param setPacketDestination + * @param queueDepth + */ + CommandingServiceBase(object_id_t setObjectId, uint16_t apid, + uint8_t service, uint8_t numberOfParallelCommands, + uint16_t commandTimeoutSeconds, size_t queueDepth = 20); + virtual ~CommandingServiceBase(); + + /** + * This setter can be used to set the packet source individually instead + * of using the default static framework ID set in the factory. + * This should be called at object initialization and not during run-time! + * @param packetSource + */ + void setPacketSource(object_id_t packetSource); + /** + * This setter can be used to set the packet destination individually + * instead of using the default static framework ID set in the factory. + * This should be called at object initialization and not during run-time! + * @param packetDestination + */ + void setPacketDestination(object_id_t packetDestination); + + /*** + * This is the periodically called function. + * Handle request queue for external commands. + * Handle command Queue for internal commands. + * @param opCode is unused here at the moment + * @return RETURN_OK + */ + virtual ReturnValue_t performOperation(uint8_t opCode) override; + + virtual uint16_t getIdentifier(); + + /** + * Returns the requestQueue MessageQueueId_t + * + * The requestQueue is the queue for external commands (TC) + * + * @return requestQueue messageQueueId_t + */ + virtual MessageQueueId_t getRequestQueue(); + + /** + * Returns the commandQueue MessageQueueId_t + * + * Remember the CommandQueue is the queue for internal communication + * @return commandQueue messageQueueId_t + */ + virtual MessageQueueId_t getCommandQueue(); + + virtual ReturnValue_t initialize() override; + + /** + * Implementation of ExecutableObjectIF function + * + * Used to setup the reference of the task, that executes this component + * @param task Pointer to the taskIF of this task + */ + virtual void setTaskIF(PeriodicTaskIF* task) override; + +protected: + /** + * Check the target subservice + * @param subservice[in] + * @return + * -@c RETURN_OK Subservice valid, continue message handling + * -@c INVALID_SUBSERVICE if service is not known, rejects packet. + */ + virtual ReturnValue_t isValidSubservice(uint8_t subservice) = 0; + + /** + * Once a TC Request is valid, the existence of the destination and its + * target interface is checked and retrieved. The target message queue ID + * can then be acquired by using the target interface. + * @param subservice + * @param tcData Application Data of TC Packet + * @param tcDataLen + * @param id MessageQueue ID is stored here + * @param objectId Object ID is extracted and stored here + * @return + * - @c RETURN_OK Cotinue message handling + * - @c RETURN_FAILED Reject the packet and generates a start failure + * verification + */ + virtual ReturnValue_t getMessageQueueAndObject(uint8_t subservice, + const uint8_t *tcData, size_t tcDataLen, MessageQueueId_t *id, + object_id_t *objectId) = 0; + + /** + * After the Message Queue and Object ID are determined, the command is + * prepared by using an implementation specific CommandMessage type + * which is sent to the target object. It contains all necessary information + * for the device to execute telecommands. + * @param message [out] message which can be set and is sent to the object + * @param subservice Subservice of the current communication + * @param tcData Application data of command + * @param tcDataLen Application data length + * @param state [out/in] Setable state of the communication. + * communication + * @param objectId Target object ID + * @return + * - @c RETURN_OK to generate a verification start message + * - @c EXECUTION_COMPELTE Fire-and-forget command. Generate a completion + * verification message. + * - @c Anything else rejects the packets and generates a start failure + * verification. + */ + virtual ReturnValue_t prepareCommand(CommandMessage* message, + uint8_t subservice, const uint8_t *tcData, size_t tcDataLen, + uint32_t *state, object_id_t objectId) = 0; + + /** + * This function is implemented by child services to specify how replies + * to a command from another software component are handled. + * @param reply + * This is the reply in form of a generic read-only command message. + * @param previousCommand + * Command_t of related command + * @param state [out/in] + * Additional parameter which can be used to pass state information. + * State of the communication + * @param optionalNextCommand [out] + * An optional next command which can be set in this function + * @param objectId Source object ID + * @param isStep Flag value to mark steps of command execution + * @return + * - @c RETURN_OK, @c EXECUTION_COMPLETE or @c NO_STEP_MESSAGE to + * generate TC verification success + * - @c INVALID_REPLY Calls handleUnrequestedReply + * - Anything else triggers a TC verification failure. If RETURN_FAILED or + * INVALID_REPLY is returned and the command ID is + * CommandMessage::REPLY_REJECTED, a failure verification message with + * the reason as the error parameter and the initial command as + * failure parameter 1 is generated. + */ + virtual ReturnValue_t handleReply(const CommandMessage* reply, + Command_t previousCommand, uint32_t *state, + CommandMessage* optionalNextCommand, object_id_t objectId, + bool *isStep) = 0; + + /** + * This function can be overidden to handle unrequested reply, + * when the reply sender ID is unknown or is not found is the command map. + * The default implementation will clear the command message and all + * its contents. + * @param reply + * Reply which is non-const so the default implementation can clear the + * message. + */ + virtual void handleUnrequestedReply(CommandMessage* reply); + + virtual void doPeriodicOperation(); + + struct CommandInfo: public SerializeIF{ + struct tcInfo { + uint8_t ackFlags; + uint16_t tcPacketId; + uint16_t tcSequenceControl; + } tcInfo; + uint32_t uptimeOfStart; + uint8_t step; + uint8_t subservice; + uint32_t state; + Command_t command; + object_id_t objectId; + FIFO fifo; + + virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const override{ + return HasReturnvaluesIF::RETURN_FAILED; + }; + + virtual size_t getSerializedSize() const override { + return 0; + }; + + virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) override { + return HasReturnvaluesIF::RETURN_FAILED; + }; + }; + + using CommandMapIter = FixedMap::Iterator; + + const uint16_t apid; + + const uint8_t service; + + const uint16_t timeoutSeconds; + + uint8_t tmPacketCounter = 0; + + StorageManagerIF *IPCStore = nullptr; + + StorageManagerIF *TCStore = nullptr; + + MessageQueueIF* commandQueue = nullptr; + + MessageQueueIF* requestQueue = nullptr; + + VerificationReporter verificationReporter; + + FixedMap commandMap; + + /* May be set be children to return a more precise failure condition. */ + uint32_t failureParameter1 = 0; + uint32_t failureParameter2 = 0; + + static object_id_t defaultPacketSource; + object_id_t packetSource = objects::NO_OBJECT; + static object_id_t defaultPacketDestination; + object_id_t packetDestination = objects::NO_OBJECT; + + /** + * Pointer to the task which executes this component, + * is invalid before setTaskIF was called. + */ + PeriodicTaskIF* executingTask = nullptr; + + /** + * @brief Send TM data from pointer to data. + * If a header is supplied it is added before data + * @param subservice Number of subservice + * @param data Pointer to the data in the Packet + * @param dataLen Lenght of data in the Packet + * @param headerData HeaderData will be placed before data + * @param headerSize Size of HeaderData + */ + ReturnValue_t sendTmPacket(uint8_t subservice, const uint8_t *data, + size_t dataLen, const uint8_t* headerData = nullptr, + size_t headerSize = 0); + + /** + * @brief To send TM packets of objects that still need to be serialized + * and consist of an object ID with appended data. + * @param subservice Number of subservice + * @param objectId ObjectId is placed before data + * @param data Data to append to the packet + * @param dataLen Length of Data + */ + ReturnValue_t sendTmPacket(uint8_t subservice, object_id_t objectId, + const uint8_t *data, size_t dataLen); + + /** + * @brief To send packets which are contained inside a class implementing + * SerializeIF. + * @param subservice Number of subservice + * @param content This is a pointer to the serialized packet + * @param header Serialize IF header which will be placed before content + */ + ReturnValue_t sendTmPacket(uint8_t subservice, SerializeIF* content, + SerializeIF* header = nullptr); + + void checkAndExecuteFifo(CommandMapIter& iter); + +private: + /** + * This method handles internal execution of a command, + * once it has been started by @sa{startExecution()} in the request + * queue handler. + * It handles replies generated by the devices and relayed by the specific + * service implementation. This means that it determines further course of + * action depending on the return values specified in the service + * implementation. + * This includes the generation of TC verification messages. Note that + * the static framework object ID @c VerificationReporter::messageReceiver + * needs to be set. + * - TM[1,5] Step Successs + * - TM[1,6] Step Failure + * - TM[1,7] Completion Success + * - TM[1,8] Completion Failure + */ + void handleCommandQueue(); + + /** + * @brief Handler function for request queue + * @details + * Sequence of request queue handling: + * isValidSubservice -> getMessageQueueAndObject -> startExecution + * Generates a Start Success Reports TM[1,3] in subfunction + * @sa{startExecution()} or a Start Failure Report TM[1,4] by using the + * TC Verification Service. + */ + void handleRequestQueue(); + + void rejectPacket(uint8_t reportId, TcPacketStored* packet, + ReturnValue_t errorCode); + + void acceptPacket(uint8_t reportId, TcPacketStored* packet); + + void startExecution(TcPacketStored *storedPacket, CommandMapIter iter); + + void handleCommandMessage(CommandMessage* reply); + void handleReplyHandlerResult(ReturnValue_t result, CommandMapIter iter, + CommandMessage* nextCommand, CommandMessage* reply, bool& isStep); + + void checkTimeout(); +}; + +#endif /* FSFW_TMTCSERVICES_COMMANDINGSERVICEBASE_H_ */ diff --git a/fsfw/tmtcservices/PusServiceBase.cpp b/fsfw/tmtcservices/PusServiceBase.cpp new file mode 100644 index 0000000..d35944a --- /dev/null +++ b/fsfw/tmtcservices/PusServiceBase.cpp @@ -0,0 +1,121 @@ +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tcdistribution/PUSDistributorIF.h" +#include "AcceptsTelemetryIF.h" +#include "PusServiceBase.h" +#include "PusVerificationReport.h" +#include "TmTcMessage.h" +#include "../ipc/QueueFactory.h" + +object_id_t PusServiceBase::packetSource = 0; +object_id_t PusServiceBase::packetDestination = 0; + +PusServiceBase::PusServiceBase(object_id_t setObjectId, uint16_t setApid, + uint8_t setServiceId) : + SystemObject(setObjectId), apid(setApid), serviceId(setServiceId) { + requestQueue = QueueFactory::instance()-> + createMessageQueue(PUS_SERVICE_MAX_RECEPTION); +} + +PusServiceBase::~PusServiceBase() { + QueueFactory::instance()->deleteMessageQueue(requestQueue); +} + +ReturnValue_t PusServiceBase::performOperation(uint8_t opCode) { + handleRequestQueue(); + ReturnValue_t result = this->performService(); + if (result != RETURN_OK) { + sif::error << "PusService " << (uint16_t) this->serviceId + << ": performService returned with " << (int16_t) result + << std::endl; + return RETURN_FAILED; + } + return RETURN_OK; +} + +void PusServiceBase::setTaskIF(PeriodicTaskIF* taskHandle) { + this->taskHandle = taskHandle; +} + +void PusServiceBase::handleRequestQueue() { + TmTcMessage message; + ReturnValue_t result = RETURN_FAILED; + for (uint8_t count = 0; count < PUS_SERVICE_MAX_RECEPTION; count++) { + ReturnValue_t status = this->requestQueue->receiveMessage(&message); + // debug << "PusServiceBase::performOperation: Receiving from MQ ID: " + // << std::hex << this->requestQueue.getId() + // << std::dec << " returned: " << status << std::endl; + if (status == RETURN_OK) { + this->currentPacket.setStoreAddress(message.getStorageId()); + //info << "Service " << (uint16_t) this->serviceId << + // ": new packet!" << std::endl; + + result = this->handleRequest(currentPacket.getSubService()); + + // debug << "Service " << (uint16_t)this->serviceId << + // ": handleRequest returned: " << (int)return_code << std::endl; + if (result == RETURN_OK) { + this->verifyReporter.sendSuccessReport( + TC_VERIFY::COMPLETION_SUCCESS, &this->currentPacket); + } + else { + this->verifyReporter.sendFailureReport( + TC_VERIFY::COMPLETION_FAILURE, &this->currentPacket, + result, 0, errorParameter1, errorParameter2); + } + this->currentPacket.deletePacket(); + errorParameter1 = 0; + errorParameter2 = 0; + } + else if (status == MessageQueueIF::EMPTY) { + status = RETURN_OK; + // debug << "PusService " << (uint16_t)this->serviceId << + // ": no new packet." << std::endl; + break; + } + else { + sif::error << "PusServiceBase::performOperation: Service " + << (uint16_t) this->serviceId + << ": Error receiving packet. Code: " << std::hex << status + << std::dec << std::endl; + } + } +} + +uint16_t PusServiceBase::getIdentifier() { + return this->serviceId; +} + +MessageQueueId_t PusServiceBase::getRequestQueue() { + return this->requestQueue->getId(); +} + +ReturnValue_t PusServiceBase::initialize() { + ReturnValue_t result = SystemObject::initialize(); + if (result != RETURN_OK) { + return result; + } + AcceptsTelemetryIF* destService = objectManager->get( + packetDestination); + PUSDistributorIF* distributor = objectManager->get( + packetSource); + if ((destService != nullptr) && (distributor != nullptr)) { + this->requestQueue->setDefaultDestination( + destService->getReportReceptionQueue()); + distributor->registerService(this); + return RETURN_OK; + } + else { + sif::error << "PusServiceBase::PusServiceBase: Service " + << (uint32_t) this->serviceId << ": Configuration error." + << " Make sure packetSource and packetDestination are defined " + "correctly" << std::endl; + return RETURN_FAILED; + } +} + +ReturnValue_t PusServiceBase::initializeAfterTaskCreation() { + // If task parameters, for example task frequency are required, this + // function should be overriden and the system object task IF can + // be used to get those parameters. + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/fsfw/tmtcservices/PusServiceBase.h b/fsfw/tmtcservices/PusServiceBase.h new file mode 100644 index 0000000..98d65ef --- /dev/null +++ b/fsfw/tmtcservices/PusServiceBase.h @@ -0,0 +1,159 @@ +#ifndef FRAMEWORK_TMTCSERVICES_PUSSERVICEBASE_H_ +#define FRAMEWORK_TMTCSERVICES_PUSSERVICEBASE_H_ + +#include "../objectmanager/ObjectManagerIF.h" +#include "../objectmanager/SystemObject.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../tmtcpacket/pus/TcPacketStored.h" +#include "AcceptsTelecommandsIF.h" +#include "VerificationCodes.h" +#include "VerificationReporter.h" +#include "../ipc/MessageQueueIF.h" + +namespace Factory{ +void setStaticFrameworkObjectIds(); +} + +/** + * @defgroup pus_services PUS Service Framework + * These group contains all implementations of PUS Services in the OBSW. + * Most of the Services are directly taken from the ECSS PUS Standard. + */ + +/** + * @brief This class is the basis for all PUS Services, + * which can immediately process Telecommand Packets. + * @details + * It manages Telecommand reception and the generation of Verification Reports. + * Every class that inherits from this abstract class has to implement + * handleRequest and performService. Services that are created with this + * Base class have to handle any kind of request immediately on reception. + * All PUS Services are System Objects, so an Object ID needs to be specified + * on construction. + * @ingroup pus_services + */ +class PusServiceBase : public ExecutableObjectIF, + public AcceptsTelecommandsIF, + public SystemObject, + public HasReturnvaluesIF { + friend void (Factory::setStaticFrameworkObjectIds)(); +public: + /** + * @brief The passed values are set, but inter-object initialization is + * done in the initialize method. + * @param setObjectId + * The system object identifier of this Service instance. + * @param setApid + * The APID the Service is instantiated for. + * @param setServiceId + * The Service Identifier as specified in ECSS PUS. + */ + PusServiceBase( object_id_t setObjectId, uint16_t setApid, + uint8_t setServiceId); + /** + * The destructor is empty. + */ + virtual ~PusServiceBase(); + /** + * @brief The handleRequest method shall handle any kind of Telecommand + * Request immediately. + * @details + * Implemetations can take the Telecommand in currentPacket and perform + * any kind of operation. + * They may send additional "Start Success (1,3)" messages with the + * verifyReporter, but Completion Success or Failure Reports are generated + * automatically after execution of this method. + * + * If a Telecommand can not be executed within one call cycle, + * this Base class is not the right parent. + * + * The child class may add additional error information by setting + * #errorParameters which aren attached to the generated verification + * message. + * + * Subservice checking should be implemented in this method. + * + * @return The returned status_code is directly taken as main error code + * in the Verification Report. + * On success, RETURN_OK shall be returned. + */ + virtual ReturnValue_t handleRequest(uint8_t subservice) = 0; + /** + * In performService, implementations can handle periodic, + * non-TC-triggered activities. + * The performService method is always called. + * @return Currently, everything other that RETURN_OK only triggers + * diagnostic output. + */ + virtual ReturnValue_t performService() = 0; + /** + * This method implements the typical activity of a simple PUS Service. + * It checks for new requests, and, if found, calls handleRequest, sends + * completion verification messages and deletes + * the TC requests afterwards. + * performService is always executed afterwards. + * @return @c RETURN_OK if the periodic performService was successful. + * @c RETURN_FAILED else. + */ + ReturnValue_t performOperation(uint8_t opCode) override; + virtual uint16_t getIdentifier() override; + MessageQueueId_t getRequestQueue() override; + virtual ReturnValue_t initialize() override; + + virtual void setTaskIF(PeriodicTaskIF* taskHandle) override; + virtual ReturnValue_t initializeAfterTaskCreation() override; +protected: + /** + * @brief Handle to the underlying task + * @details + * Will be set by setTaskIF(), which is called on task creation. + */ + PeriodicTaskIF* taskHandle = nullptr; + /** + * The APID of this instance of the Service. + */ + uint16_t apid; + /** + * The Service Identifier. + */ + uint8_t serviceId; + /** + * One of two error parameters for additional error information. + */ + uint32_t errorParameter1 = 0; + /** + * One of two error parameters for additional error information. + */ + uint32_t errorParameter2 = 0; + /** + * This is a complete instance of the telecommand reception queue + * of the class. It is initialized on construction of the class. + */ + MessageQueueIF* requestQueue = nullptr; + /** + * An instance of the VerificationReporter class, that simplifies + * sending any kind of verification message to the TC Verification Service. + */ + VerificationReporter verifyReporter; + /** + * The current Telecommand to be processed. + * It is deleted after handleRequest was executed. + */ + TcPacketStored currentPacket; + + static object_id_t packetSource; + + static object_id_t packetDestination; +private: + /** + * This constant sets the maximum number of packets accepted per call. + * Remember that one packet must be completely handled in one + * #handleRequest call. + */ + static const uint8_t PUS_SERVICE_MAX_RECEPTION = 10; + + void handleRequestQueue(); +}; + +#endif /* PUSSERVICEBASE_H_ */ diff --git a/fsfw/tmtcservices/PusVerificationReport.cpp b/fsfw/tmtcservices/PusVerificationReport.cpp new file mode 100644 index 0000000..bfad9ec --- /dev/null +++ b/fsfw/tmtcservices/PusVerificationReport.cpp @@ -0,0 +1,138 @@ +#include "../serialize/SerializeAdapter.h" +#include "PusVerificationReport.h" + +PusVerificationMessage::PusVerificationMessage() { +} + +PusVerificationMessage::PusVerificationMessage(uint8_t set_report_id, + uint8_t ackFlags, uint16_t tcPacketId, uint16_t tcSequenceControl, + ReturnValue_t set_error_code, uint8_t set_step, uint32_t parameter1, + uint32_t parameter2) { + uint8_t *data = this->getBuffer(); + data[messageSize] = set_report_id; + messageSize += sizeof(set_report_id); + data[messageSize] = ackFlags; + messageSize += sizeof(ackFlags); + memcpy(&data[messageSize], &tcPacketId, sizeof(tcPacketId)); + messageSize += sizeof(tcPacketId); + memcpy(&data[messageSize], &tcSequenceControl, sizeof(tcSequenceControl)); + messageSize += sizeof(tcSequenceControl); + data[messageSize] = set_step; + messageSize += sizeof(set_step); + memcpy(&data[messageSize], &set_error_code, sizeof(set_error_code)); + messageSize += sizeof(set_error_code); + memcpy(&data[messageSize], ¶meter1, sizeof(parameter1)); + messageSize += sizeof(parameter1); + memcpy(&data[messageSize], ¶meter2, sizeof(parameter2)); + messageSize += sizeof(parameter2); +} + +uint8_t PusVerificationMessage::getReportId() { + + return getContent()->reportId; +} + +uint8_t PusVerificationMessage::getAckFlags() { + + return getContent()->ackFlags; +} + +uint16_t PusVerificationMessage::getTcPacketId() { + + uint16_t tcPacketId; + memcpy(&tcPacketId, &getContent()->packetId_0, sizeof(tcPacketId)); + return tcPacketId; +} + +uint16_t PusVerificationMessage::getTcSequenceControl() { + + uint16_t tcSequenceControl; + memcpy(&tcSequenceControl, &getContent()->tcSequenceControl_0, + sizeof(tcSequenceControl)); + return tcSequenceControl; +} + +uint8_t PusVerificationMessage::getStep() { + + return getContent()->step; +} + +ReturnValue_t PusVerificationMessage::getErrorCode() { + ReturnValue_t errorCode; + memcpy(&errorCode, &getContent()->error_code_0, sizeof(errorCode)); + return errorCode; +} + +PusVerificationMessage::verifciationMessageContent* PusVerificationMessage::getContent() { + return (verifciationMessageContent*) this->getData(); +} + +uint32_t PusVerificationMessage::getParameter1() { + uint32_t parameter; + memcpy(¶meter, &getContent()->parameter1_0, sizeof(parameter)); + return parameter; +} + +uint32_t PusVerificationMessage::getParameter2() { + uint32_t parameter; + memcpy(¶meter, &getContent()->parameter2_0, sizeof(parameter)); + return parameter; +} + +PusSuccessReport::PusSuccessReport(uint16_t setPacketId, + uint16_t setSequenceControl, uint8_t setStep) : + reportSize(0), pBuffer(reportBuffer) { + //Serialization won't fail, because we know the necessary max-size of the buffer. + SerializeAdapter::serialize(&setPacketId, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); + SerializeAdapter::serialize(&setSequenceControl, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); + if (setStep != 0) { + SerializeAdapter::serialize(&setStep, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); + } +} + +PusSuccessReport::~PusSuccessReport() { + +} + +uint32_t PusSuccessReport::getSize() { + return reportSize; +} + +uint8_t* PusSuccessReport::getReport() { + return reportBuffer; +} + +PusFailureReport::PusFailureReport(uint16_t setPacketId, + uint16_t setSequenceControl, ReturnValue_t setErrorCode, + uint8_t setStep, uint32_t parameter1, uint32_t parameter2) : + reportSize(0), pBuffer(reportBuffer) { + //Serialization won't fail, because we know the necessary max-size of the buffer. + SerializeAdapter::serialize(&setPacketId, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); + SerializeAdapter::serialize(&setSequenceControl, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); + if (setStep != 0) { + SerializeAdapter::serialize(&setStep, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); + } + SerializeAdapter::serialize(&setErrorCode, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); + SerializeAdapter::serialize(¶meter1, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); + SerializeAdapter::serialize(¶meter2, &pBuffer, &reportSize, + sizeof(reportBuffer), SerializeIF::Endianness::BIG); +} + +PusFailureReport::~PusFailureReport() { +} + +size_t PusFailureReport::getSize() { + return reportSize; +} + +uint8_t* PusFailureReport::getReport() { + return reportBuffer; +} diff --git a/fsfw/tmtcservices/PusVerificationReport.h b/fsfw/tmtcservices/PusVerificationReport.h new file mode 100644 index 0000000..88954cf --- /dev/null +++ b/fsfw/tmtcservices/PusVerificationReport.h @@ -0,0 +1,81 @@ +#ifndef FSFW_TMTCSERVICES_PUSVERIFICATIONREPORT_H_ +#define FSFW_TMTCSERVICES_PUSVERIFICATIONREPORT_H_ + +#include "VerificationCodes.h" + +#include "../ipc/MessageQueueMessage.h" +#include "../tmtcpacket/pus/TcPacketBase.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +#include + +class PusVerificationMessage: public MessageQueueMessage { +private: + struct verifciationMessageContent { + uint8_t reportId; + uint8_t ackFlags; + uint8_t packetId_0; + uint8_t packetId_1; + uint8_t tcSequenceControl_0; + uint8_t tcSequenceControl_1; + uint8_t step; + uint8_t error_code_0; + uint8_t error_code_1; + uint8_t parameter1_0; + uint8_t parameter1_1; + uint8_t parameter1_2; + uint8_t parameter1_3; + uint8_t parameter2_0; + uint8_t parameter2_1; + uint8_t parameter2_2; + uint8_t parameter2_3; + }; + verifciationMessageContent* getContent(); +public: + static const uint8_t VERIFICATION_MIN_SIZE = 6; + PusVerificationMessage(); + + PusVerificationMessage(uint8_t set_report_id, uint8_t ackFlags, + uint16_t tcPacketId, uint16_t tcSequenceControl, + ReturnValue_t set_error_code = 0, uint8_t set_step = 0, + uint32_t parameter1 = 0, uint32_t parameter2 = 0); + uint8_t getReportId(); + uint8_t getAckFlags(); + uint16_t getTcPacketId(); + uint16_t getTcSequenceControl(); + ReturnValue_t getErrorCode(); + uint8_t getStep(); + uint32_t getParameter1(); + uint32_t getParameter2(); +}; + +class PusSuccessReport { +private: + static const uint16_t MAX_SIZE = 7; + uint8_t reportBuffer[MAX_SIZE]; + size_t reportSize; + uint8_t * pBuffer; +public: + PusSuccessReport(uint16_t setPacketId, uint16_t setSequenceControl, + uint8_t set_step = 0); + ~PusSuccessReport(); + uint32_t getSize(); + uint8_t* getReport(); +}; + +class PusFailureReport { +private: + static const uint16_t MAX_SIZE = 16; + uint8_t reportBuffer[MAX_SIZE]; + size_t reportSize; + uint8_t * pBuffer; +public: + PusFailureReport(uint16_t setPacketId, uint16_t setSequenceControl, + ReturnValue_t setErrorCode, uint8_t setStep = 0, + uint32_t parameter1 = 0, uint32_t parameter2 = 0); + ~PusFailureReport(); + size_t getSize(); + uint8_t* getReport(); +}; + +#endif /* FSFW_TMTCSERVICES_PUSVERIFICATIONREPORT_H_ */ diff --git a/fsfw/tmtcservices/ServiceTypes.h b/fsfw/tmtcservices/ServiceTypes.h new file mode 100644 index 0000000..1f5bff4 --- /dev/null +++ b/fsfw/tmtcservices/ServiceTypes.h @@ -0,0 +1,32 @@ +/** + * @file ServiceTypes.h + * @brief This file defines the ServiceTypes class. + * @date 11.04.2013 + * @author baetz + */ + +#ifndef SERVICETYPES_H_ +#define SERVICETYPES_H_ + +namespace SERVICE { +enum ServiceTypes { + TELECOMMAND_VERIFICATION = 1, + DEVICE_COMMAND_DISTRIBUTION = 2, + HOUSEKEEPING_AND_DIAGNOSTIC_DATA_REPORTING = 3, + PARAMETER_STATISTICS_REPORTING = 4, + EVENT_REPORTING = 5, + MEMORY_MANAGEMENT = 6, + FUNCTION_MANAGEMENT = 8, + TIME_MANAGEMENT = 9, + ON_BOARD_OPERATIONS_SCHEDULING = 11, + ON_BOARD_MONITORING = 12, + LARGE_DATA_TRANSFER = 13, + PACKET_FORWARDING_CONTROL = 14, + ON_BOARD_STORAGE_AND_RETRIEVAL = 15, + TEST = 17, + ON_BOARD_OPERATIONS_PROCEDURE = 18, + EVENT_ACTION = 19 +}; +} + +#endif /* SERVICETYPES_H_ */ diff --git a/fsfw/tmtcservices/SourceSequenceCounter.h b/fsfw/tmtcservices/SourceSequenceCounter.h new file mode 100644 index 0000000..1cc5875 --- /dev/null +++ b/fsfw/tmtcservices/SourceSequenceCounter.h @@ -0,0 +1,30 @@ +/** + * @file SourceSequenceCounter.h + * @brief This file defines the SourceSequenceCounter class. + * @date 04.02.2013 + * @author baetz + */ + +#ifndef SOURCESEQUENCECOUNTER_H_ +#define SOURCESEQUENCECOUNTER_H_ +#include "../tmtcpacket/SpacePacketBase.h" + +class SourceSequenceCounter { +private: + uint16_t sequenceCount; +public: + SourceSequenceCounter() : sequenceCount(0) {} + void increment() { + sequenceCount = (sequenceCount+1) % (SpacePacketBase::LIMIT_SEQUENCE_COUNT); + } + void decrement() { + sequenceCount = (sequenceCount-1) % (SpacePacketBase::LIMIT_SEQUENCE_COUNT); + } + uint16_t get() { return this->sequenceCount; } + void reset(uint16_t toValue = 0) { + sequenceCount = toValue % (SpacePacketBase::LIMIT_SEQUENCE_COUNT); + } +}; + + +#endif /* SOURCESEQUENCECOUNTER_H_ */ diff --git a/fsfw/tmtcservices/TmTcBridge.cpp b/fsfw/tmtcservices/TmTcBridge.cpp new file mode 100644 index 0000000..8c2f15e --- /dev/null +++ b/fsfw/tmtcservices/TmTcBridge.cpp @@ -0,0 +1,237 @@ +#include "../tmtcservices/TmTcBridge.h" + +#include "../ipc/QueueFactory.h" +#include "../tmtcservices/AcceptsTelecommandsIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../globalfunctions/arrayprinter.h" + +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); +} + +TmTcBridge::~TmTcBridge() {} + +ReturnValue_t TmTcBridge::setNumberOfSentPacketsPerCycle( + uint8_t sentPacketsPerCycle) { + if(sentPacketsPerCycle <= LIMIT_STORED_DATA_SENT_PER_CYCLE) { + this->sentPacketsPerCycle = sentPacketsPerCycle; + return RETURN_OK; + } + else { + sif::warning << "TmTcBridge::setNumberOfSentPacketsPerCycle: Number of " + << "packets sent per cycle exceeds limits. " + << "Keeping default value." << std::endl; + return RETURN_FAILED; + } +} + +ReturnValue_t TmTcBridge::setMaxNumberOfPacketsStored( + uint8_t maxNumberOfPacketsStored) { + if(maxNumberOfPacketsStored <= LIMIT_DOWNLINK_PACKETS_STORED) { + this->maxNumberOfPacketsStored = maxNumberOfPacketsStored; + return RETURN_OK; + } + else { + sif::warning << "TmTcBridge::setMaxNumberOfPacketsStored: Number of " + << "packets stored exceeds limits. " + << "Keeping default value." << std::endl; + return RETURN_FAILED; + } +} + +ReturnValue_t TmTcBridge::initialize() { + tcStore = objectManager->get(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; + } + tmStore = objectManager->get(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; + } + AcceptsTelecommandsIF* tcDistributor = + objectManager->get(tcDestination); + if (tcDistributor == nullptr) { + sif::error << "TmTcBridge::initialize: TC Distributor invalid" + << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + + tmFifo = new DynamicFIFO(maxNumberOfPacketsStored); + + tmTcReceptionQueue->setDefaultDestination(tcDistributor->getRequestQueue()); + return RETURN_OK; +} + +ReturnValue_t TmTcBridge::performOperation(uint8_t operationCode) { + ReturnValue_t result; + result = handleTc(); + if(result != RETURN_OK) { + sif::debug << "TmTcBridge::performOperation: " + << "Error handling TCs" << std::endl; + } + result = handleTm(); + if (result != RETURN_OK) { + sif::debug << "TmTcBridge::performOperation: " + << "Error handling TMs" << std::endl; + } + return result; +} + +ReturnValue_t TmTcBridge::handleTc() { + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t TmTcBridge::handleTm() { + ReturnValue_t status = HasReturnvaluesIF::RETURN_OK; + ReturnValue_t result = handleTmQueue(); + if(result != RETURN_OK) { + sif::error << "TmTcBridge::handleTm: Error handling TM queue!" + << std::endl; + status = result; + } + + 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; + } + } + packetSentCounter = 0; + return status; +} + +ReturnValue_t TmTcBridge::handleTmQueue() { + TmTcMessage message; + const uint8_t* data = nullptr; + size_t size = 0; + ReturnValue_t status = HasReturnvaluesIF::RETURN_OK; + for (ReturnValue_t result = tmTcReceptionQueue->receiveMessage(&message); + result == HasReturnvaluesIF::RETURN_OK; + result = tmTcReceptionQueue->receiveMessage(&message)) + { + //sif::info << (int) packetSentCounter << std::endl; + if(communicationLinkUp == false or + packetSentCounter >= sentPacketsPerCycle) { + storeDownlinkData(&message); + continue; + } + + result = tmStore->getData(message.getStorageId(), &data, &size); + if (result != HasReturnvaluesIF::RETURN_OK) { + status = result; + continue; + } + + result = sendTm(data, size); + if (result != HasReturnvaluesIF::RETURN_OK) { + status = result; + } + else { + tmStore->deleteData(message.getStorageId()); + packetSentCounter++; + } + } + return status; +} + +ReturnValue_t TmTcBridge::storeDownlinkData(TmTcMessage *message) { + store_address_t storeId = 0; + + 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; + } + } + + storeId = message->getStorageId(); + tmFifo->insert(storeId); + tmStored = true; + return RETURN_OK; +} + +ReturnValue_t TmTcBridge::handleStoredTm() { + ReturnValue_t status = RETURN_OK; + while(not tmFifo->empty() and packetSentCounter < sentPacketsPerCycle) { + //sif::info << "TMTC Bridge: Sending stored TM data. There are " + // << (int) tmFifo->size() << " left to send\r\n" << std::flush; + + store_address_t storeId; + 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; + } + + result = sendTm(data,size); + if(result != RETURN_OK) { + sif::error << "TMTC Bridge: Could not send stored downlink data" + << std::endl; + status = result; + } + packetSentCounter ++; + + if(tmFifo->empty()) { + tmStored = false; + } + tmStore->deleteData(storeId); + } + return status; +} + +void TmTcBridge::registerCommConnect() { + if(not communicationLinkUp) { + //sif::info << "TMTC Bridge: Registered Comm Link Connect" << std::endl; + communicationLinkUp = true; + } +} + +void TmTcBridge::registerCommDisconnect() { + //sif::info << "TMTC Bridge: Registered Comm Link Disconnect" << std::endl; + 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; +} diff --git a/fsfw/tmtcservices/TmTcBridge.h b/fsfw/tmtcservices/TmTcBridge.h new file mode 100644 index 0000000..62cfdaa --- /dev/null +++ b/fsfw/tmtcservices/TmTcBridge.h @@ -0,0 +1,162 @@ +#ifndef FRAMEWORK_TMTCSERVICES_TMTCBRIDGE_H_ +#define FRAMEWORK_TMTCSERVICES_TMTCBRIDGE_H_ + + +#include "../objectmanager/SystemObject.h" +#include "../tmtcservices/AcceptsTelemetryIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../ipc/MessageQueueIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tmtcservices/AcceptsTelecommandsIF.h" +#include "../container/DynamicFIFO.h" +#include "../tmtcservices/TmTcMessage.h" + +class TmTcBridge : public AcceptsTelemetryIF, + public AcceptsTelecommandsIF, + public ExecutableObjectIF, + public HasReturnvaluesIF, + public SystemObject { +public: + static constexpr uint8_t TMTC_RECEPTION_QUEUE_DEPTH = 20; + static constexpr uint8_t LIMIT_STORED_DATA_SENT_PER_CYCLE = 15; + static constexpr uint8_t LIMIT_DOWNLINK_PACKETS_STORED = 20; + + static constexpr uint8_t DEFAULT_STORED_DATA_SENT_PER_CYCLE = 5; + static constexpr uint8_t DEFAULT_DOWNLINK_PACKETS_STORED = 10; + + TmTcBridge(object_id_t objectId, object_id_t tcDestination, + object_id_t tmStoreId, object_id_t tcStoreId); + virtual ~TmTcBridge(); + + /** + * Set number of packets sent per performOperation().Please note that this + * value must be smaller than MAX_STORED_DATA_SENT_PER_CYCLE + * @param sentPacketsPerCycle + * @return -@c RETURN_OK if value was set successfully + * -@c RETURN_FAILED otherwise, stored value stays the same + */ + ReturnValue_t setNumberOfSentPacketsPerCycle(uint8_t sentPacketsPerCycle); + + /** + * Set number of packets sent per performOperation().Please note that this + * value must be smaller than MAX_DOWNLINK_PACKETS_STORED + * @param sentPacketsPerCycle + * @return -@c RETURN_OK if value was set successfully + * -@c RETURN_FAILED otherwise, stored value stays the same + */ + ReturnValue_t setMaxNumberOfPacketsStored(uint8_t maxNumberOfPacketsStored); + + /** + * This will set up the bridge to overwrite old data in the FIFO. + * @param overwriteOld + */ + void setFifoToOverwriteOldData(bool overwriteOld); + + virtual void registerCommConnect(); + virtual void registerCommDisconnect(); + + /** + * Initializes necessary FSFW components for the TMTC Bridge + * @return + */ + virtual ReturnValue_t initialize() override; + + /** + * @brief Handles TMTC reception + */ + virtual ReturnValue_t performOperation(uint8_t operationCode = 0) override; + + + /** AcceptsTelemetryIF override */ + virtual MessageQueueId_t getReportReceptionQueue( + uint8_t virtualChannel = 0) override; + + /** AcceptsTelecommandsIF override */ + virtual uint16_t getIdentifier() override; + virtual MessageQueueId_t getRequestQueue() override; + +protected: + //! Cached for initialize function. + object_id_t tmStoreId = objects::NO_OBJECT; + object_id_t tcStoreId = objects::NO_OBJECT; + object_id_t tcDestination = objects::NO_OBJECT; + + //! Used to send and receive TMTC messages. + //! The TmTcMessage class is used to transport messages between tasks. + MessageQueueIF* tmTcReceptionQueue = nullptr; + + StorageManagerIF* tmStore = nullptr; + StorageManagerIF* tcStore = nullptr; + + //! Used to specify whether communication link is up. Will be true + //! by default, so telemetry will be handled immediately. + bool communicationLinkUp = true; + bool tmStored = false; + bool overwriteOld = true; + uint8_t packetSentCounter = 0; + + /** + * @brief Handle TC reception + * @details + * Default implementation provided, but is empty. + * In most cases, TC reception will be handled in a separate task anyway. + * @return + */ + virtual ReturnValue_t handleTc(); + + /** + * Handle Telemetry. Default implementation provided. + * Calls sendTm() + * @return + */ + virtual ReturnValue_t handleTm(); + + /** + * Read the TM Queue and send TM if necessary. + * Default implementation provided + * @return + */ + virtual ReturnValue_t handleTmQueue(); + + /** + * Send stored data if communication link is active + * @return + */ + virtual ReturnValue_t handleStoredTm(); + + /** + * Implemented by child class. Perform sending of Telemetry by implementing + * communication drivers or wrappers, e.g. serial communication or a socket + * call. + * @param data + * @param dataLen + * @return + */ + virtual ReturnValue_t sendTm(const uint8_t * data, size_t dataLen) = 0; + + /** + * Store data to be sent later if communication link is not up. + * @param message + * @return + */ + virtual ReturnValue_t storeDownlinkData(TmTcMessage * message); + + + /** + * Print data as hexidecimal array + * @param data + * @param dataLen + */ + void printData(uint8_t * data, size_t dataLen); + + /** + * This fifo can be used to store downlink data + * which can not be sent at the moment. + */ + DynamicFIFO* tmFifo = nullptr; + uint8_t sentPacketsPerCycle = DEFAULT_STORED_DATA_SENT_PER_CYCLE; + uint8_t maxNumberOfPacketsStored = DEFAULT_DOWNLINK_PACKETS_STORED; +}; + + +#endif /* FRAMEWORK_TMTCSERVICES_TMTCBRIDGE_H_ */ diff --git a/fsfw/tmtcservices/TmTcMessage.cpp b/fsfw/tmtcservices/TmTcMessage.cpp new file mode 100644 index 0000000..987bd23 --- /dev/null +++ b/fsfw/tmtcservices/TmTcMessage.cpp @@ -0,0 +1,29 @@ +#include "TmTcMessage.h" +#include + + +TmTcMessage::TmTcMessage() { + this->messageSize += sizeof(store_address_t); +} + +TmTcMessage::~TmTcMessage() { +} + +store_address_t TmTcMessage::getStorageId() { + store_address_t temp_id; + memcpy(&temp_id, this->getData(), sizeof(store_address_t) ); + return temp_id; +} + +TmTcMessage::TmTcMessage(store_address_t store_id) { + this->messageSize += sizeof(store_address_t); + this->setStorageId(store_id); +} + +size_t TmTcMessage::getMinimumMessageSize() { + return this->HEADER_SIZE + sizeof(store_address_t); +} + +void TmTcMessage::setStorageId(store_address_t store_id) { + memcpy(this->getData(), &store_id, sizeof(store_address_t) ); +} diff --git a/fsfw/tmtcservices/TmTcMessage.h b/fsfw/tmtcservices/TmTcMessage.h new file mode 100644 index 0000000..b5e1ff8 --- /dev/null +++ b/fsfw/tmtcservices/TmTcMessage.h @@ -0,0 +1,50 @@ +#ifndef TMTCMESSAGE_H_ +#define TMTCMESSAGE_H_ + +#include "../ipc/MessageQueueMessage.h" +#include "../storagemanager/StorageManagerIF.h" +/** + * @brief This message class is used to pass Telecommand and Telemetry + * packets between tasks. + * @details Within such a packet, nothing is transported but the identifier of + * a packet stored in one of the IPC stores (typically a special TM and + * a special TC store). This makes passing commands very simple and + * efficient. + * \ingroup message_queue + */ +class TmTcMessage : public MessageQueueMessage { +protected: + /** + * @brief This call always returns the same fixed size of the message. + * @return Returns HEADER_SIZE + \c sizeof(store_address_t). + */ + size_t getMinimumMessageSize(); +public: + /** + * @brief In the default constructor, only the message_size is set. + */ + TmTcMessage(); + /** + * @brief With this constructor, the passed packet id is directly put + * into the message. + * @param packet_id The packet id to put into the message. + */ + TmTcMessage( store_address_t packet_id ); + /** + * @brief The class's destructor is empty. + */ + ~TmTcMessage(); + /** + * @brief This getter returns the packet id in the correct format. + * @return Returns the packet id. + */ + store_address_t getStorageId(); + /** + * @brief In some cases it might be useful to have a setter for packet id + * as well. + * @param packet_id The packet id to put into the message. + */ + void setStorageId( store_address_t packet_id ); +}; + +#endif /* TMTCMESSAGE_H_ */ diff --git a/fsfw/tmtcservices/VerificationCodes.h b/fsfw/tmtcservices/VerificationCodes.h new file mode 100644 index 0000000..e3e89b5 --- /dev/null +++ b/fsfw/tmtcservices/VerificationCodes.h @@ -0,0 +1,28 @@ +#ifndef VERIFICATIONCODES_H_ +#define VERIFICATIONCODES_H_ + +namespace TC_VERIFY { + +enum verification_flags { + NONE = 0b0000, + ACCEPTANCE = 0b0001, + START = 0b0010, + PROGRESS = 0b0100, + COMPLETION = 0b1000 +}; + +enum subservice_ids { + NOTHING_TO_REPORT = 0, + ACCEPTANCE_SUCCESS = 1, + ACCEPTANCE_FAILURE = 2, + START_SUCCESS = 3, + START_FAILURE = 4, + PROGRESS_SUCCESS = 5, + PROGRESS_FAILURE = 6, + COMPLETION_SUCCESS = 7, + COMPLETION_FAILURE = 8, +}; + +} + +#endif /* VERIFICATIONCODES_H_ */ diff --git a/fsfw/tmtcservices/VerificationReporter.cpp b/fsfw/tmtcservices/VerificationReporter.cpp new file mode 100644 index 0000000..7e40bd2 --- /dev/null +++ b/fsfw/tmtcservices/VerificationReporter.cpp @@ -0,0 +1,106 @@ +#include "VerificationReporter.h" +#include "AcceptsVerifyMessageIF.h" +#include "PusVerificationReport.h" + +#include "../ipc/MessageQueueIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../objectmanager/frameworkObjects.h" + +object_id_t VerificationReporter::messageReceiver = + objects::PUS_SERVICE_1_VERIFICATION; + +VerificationReporter::VerificationReporter() : + acknowledgeQueue(MessageQueueIF::NO_QUEUE) { +} + +VerificationReporter::~VerificationReporter() {} + +void VerificationReporter::sendSuccessReport(uint8_t set_report_id, + TcPacketBase* current_packet, uint8_t set_step) { + if (acknowledgeQueue == MessageQueueIF::NO_QUEUE) { + this->initialize(); + } + PusVerificationMessage message(set_report_id, + current_packet->getAcknowledgeFlags(), + current_packet->getPacketId(), + current_packet->getPacketSequenceControl(), 0, set_step); + ReturnValue_t status = MessageQueueSenderIF::sendMessage(acknowledgeQueue, + &message); + if (status != HasReturnvaluesIF::RETURN_OK) { + sif::error << "VerificationReporter::sendSuccessReport: Error writing " + << "to queue. Code: " << std::hex << status << std::dec + << std::endl; + } +} + +void VerificationReporter::sendSuccessReport(uint8_t set_report_id, + uint8_t ackFlags, uint16_t tcPacketId, uint16_t tcSequenceControl, + uint8_t set_step) { + if (acknowledgeQueue == MessageQueueIF::NO_QUEUE) { + this->initialize(); + } + PusVerificationMessage message(set_report_id, ackFlags, tcPacketId, + tcSequenceControl, 0, set_step); + ReturnValue_t status = MessageQueueSenderIF::sendMessage(acknowledgeQueue, + &message); + if (status != HasReturnvaluesIF::RETURN_OK) { + sif::error << "VerificationReporter::sendSuccessReport: Error writing " + << "to queue. Code: " << std::hex << status << std::dec + << std::endl; + } +} + +void VerificationReporter::sendFailureReport(uint8_t report_id, + TcPacketBase* current_packet, ReturnValue_t error_code, uint8_t step, + uint32_t parameter1, uint32_t parameter2) { + if (acknowledgeQueue == MessageQueueIF::NO_QUEUE) { + this->initialize(); + } + PusVerificationMessage message(report_id, + current_packet->getAcknowledgeFlags(), + current_packet->getPacketId(), + current_packet->getPacketSequenceControl(), error_code, step, + parameter1, parameter2); + ReturnValue_t status = MessageQueueSenderIF::sendMessage(acknowledgeQueue, + &message); + if (status != HasReturnvaluesIF::RETURN_OK) { + sif::error << "VerificationReporter::sendFailureReport: Error writing " + << "to queue. Code: " << std::hex << "0x" << status << std::dec + << std::endl; + } +} + +void VerificationReporter::sendFailureReport(uint8_t report_id, + uint8_t ackFlags, uint16_t tcPacketId, uint16_t tcSequenceControl, + ReturnValue_t error_code, uint8_t step, uint32_t parameter1, + uint32_t parameter2) { + if (acknowledgeQueue == MessageQueueIF::NO_QUEUE) { + this->initialize(); + } + PusVerificationMessage message(report_id, ackFlags, tcPacketId, + tcSequenceControl, error_code, step, parameter1, parameter2); + ReturnValue_t status = MessageQueueSenderIF::sendMessage(acknowledgeQueue, + &message); + if (status != HasReturnvaluesIF::RETURN_OK) { + sif::error << "VerificationReporter::sendFailureReport: Error writing " + << "to queue. Code: " << std::hex << "0x" << status << std::dec + << std::endl; + } +} + +void VerificationReporter::initialize() { + if(messageReceiver == objects::NO_OBJECT) { + sif::warning << "VerificationReporter::initialize: Verification message" + " receiver object ID not set yet in Factory!" << std::endl; + return; + } + AcceptsVerifyMessageIF* temp = objectManager->get( + messageReceiver); + if (temp == nullptr) { + sif::error << "VerificationReporter::initialize: Message " + << "receiver invalid. Make sure it is set up properly and " + << "implementsAcceptsVerifyMessageIF" << std::endl; + return; + } + this->acknowledgeQueue = temp->getVerificationQueue(); +} diff --git a/fsfw/tmtcservices/VerificationReporter.h b/fsfw/tmtcservices/VerificationReporter.h new file mode 100644 index 0000000..f26fa54 --- /dev/null +++ b/fsfw/tmtcservices/VerificationReporter.h @@ -0,0 +1,50 @@ +#ifndef FSFW_TMTCSERVICES_VERIFICATIONREPORTER_H_ +#define FSFW_TMTCSERVICES_VERIFICATIONREPORTER_H_ + +#include "PusVerificationReport.h" +#include "../objectmanager/ObjectManagerIF.h" + +namespace Factory{ +void setStaticFrameworkObjectIds(); +} + +/** + * @brief This helper object is used to forward verification messages + * which are generated by the Flight Software Framework. + * @details + * The messages can be relayed to an arbitrary object, for example a dedicated + * Verification Reporter. The destination is set by setting the static framework + * Id VerificationReporter::messageReceiver. The default verification reporter + * will be the PUS service 1, which sends verification messages according + * to the PUS standard. + * + */ +class VerificationReporter { + friend void (Factory::setStaticFrameworkObjectIds)(); +public: + VerificationReporter(); + virtual ~VerificationReporter(); + + void sendSuccessReport( uint8_t set_report_id, TcPacketBase* current_packet, + uint8_t set_step = 0 ); + void sendSuccessReport(uint8_t set_report_id, uint8_t ackFlags, + uint16_t tcPacketId, uint16_t tcSequenceControl, + uint8_t set_step = 0); + + void sendFailureReport( uint8_t report_id, TcPacketBase* current_packet, + ReturnValue_t error_code = 0, + uint8_t step = 0, uint32_t parameter1 = 0, + uint32_t parameter2 = 0 ); + void sendFailureReport(uint8_t report_id, + uint8_t ackFlags, uint16_t tcPacketId, uint16_t tcSequenceControl, + ReturnValue_t error_code = 0, uint8_t step = 0, + uint32_t parameter1 = 0, uint32_t parameter2 = 0); + + void initialize(); + +private: + static object_id_t messageReceiver; + MessageQueueId_t acknowledgeQueue; +}; + +#endif /* FSFW_TMTCSERVICES_VERIFICATIONREPORTER_H_ */ diff --git a/fsfw/unittest/README.md b/fsfw/unittest/README.md new file mode 100644 index 0000000..8a787c0 --- /dev/null +++ b/fsfw/unittest/README.md @@ -0,0 +1,59 @@ +## FSFW Testing +This repository contains testing and unit testing components. + +[Catch2](https://github.com/catchorg/Catch2) has been used as a framework, +and these unit tests can only be run on a linux host machine. +The makefile with default settings creates the unit test binary which can be +run in the terminal or in eclipse. + +### Instructions + +To run the fsfw unittests in the project, perform following steps: + +1. Copy the testcfg folder the project root (folder containing the FSFW). +2. There is a makefile inside the testcfg folder which can be used to have + a starting point to compile the unit tests. Copy that Makefile to the project + root +3. Create a folder named catch2 (can have other name which requires Makefile + adaption) and copy the Catch2 header files there (NOTE: CMake support + not enabled yet!) + +### Eclipse CDT settings + +The default eclipse terminal has issues displaying the colors used +when running the unit test binary by catch2. To fix this issue, +install the ANSI Escape In Console package from the eclipse marketplace. + +### GCOV integration + +GCOV has been integrated as a code coverage tool. +It can be enabled by adding `GCOV=1` to the build process as an additional argument. +Coverage data will be provided in form of .gcno and .gcda files. +These can be displayed in eclipse by looking +for a .gcno or .gcda file in the \_obj folder, double-clicking it +and picking the right source-binary. This will generate +information about which lines of a file have run, provided it is open in +eclipse. + +### LCOV integration + +The files generated by GCOV can also be processed by the tool LCOV. +On ubuntu, the tool can be installed with the following command: + +```sh +sudo apt-get install lcov +```` + +After that, the tool can be run by building the unit tests with `GCOV=1`, +running them at least one time and then executing the `lcov.sh` script. + +### Adding unit tests + +The catch unit tests are located in unittest/testfw. To add new unit tests, +add them to the UnitTestCatch.cpp file or add a new source file which +includes catch.hpp. + +For writing basics tests, the [assertion documentation](https://github.com/catchorg/Catch2/blob/master/docs/assertions.md#top) +or the existing examples are a good guideliens. +For more advanced tests, refer to the [catch2 documentation](https://github.com/catchorg/Catch2/blob/master/docs/Readme.md#top). + diff --git a/fsfw/unittest/core/CatchDefinitions.cpp b/fsfw/unittest/core/CatchDefinitions.cpp new file mode 100644 index 0000000..0b66558 --- /dev/null +++ b/fsfw/unittest/core/CatchDefinitions.cpp @@ -0,0 +1,11 @@ +#include "CatchDefinitions.h" +#include + +StorageManagerIF* tglob::getIpcStoreHandle() { + if(objectManager != nullptr) { + return objectManager->get(objects::IPC_STORE); + } else { + sif::error << "Global object manager uninitialized" << std::endl; + return nullptr; + } +} diff --git a/fsfw/unittest/core/CatchDefinitions.h b/fsfw/unittest/core/CatchDefinitions.h new file mode 100644 index 0000000..366cda7 --- /dev/null +++ b/fsfw/unittest/core/CatchDefinitions.h @@ -0,0 +1,21 @@ +#ifndef FSFW_UNITTEST_CORE_CATCHDEFINITIONS_H_ +#define FSFW_UNITTEST_CORE_CATCHDEFINITIONS_H_ + +#include +#include +#include + +namespace retval { +static constexpr int CATCH_OK = static_cast(HasReturnvaluesIF::RETURN_OK); +static constexpr int CATCH_FAILED = static_cast(HasReturnvaluesIF::RETURN_FAILED); +} + +namespace tconst { + static constexpr MessageQueueId_t testQueueId = 42; +} + +namespace tglob { + StorageManagerIF* getIpcStoreHandle(); +} + +#endif /* FSFW_UNITTEST_CORE_CATCHDEFINITIONS_H_ */ diff --git a/fsfw/unittest/core/CatchRunner.cpp b/fsfw/unittest/core/CatchRunner.cpp new file mode 100644 index 0000000..35c53cd --- /dev/null +++ b/fsfw/unittest/core/CatchRunner.cpp @@ -0,0 +1,31 @@ +/** + * @file CatchSource.cpp + * @brief Source file to compile catch framework. + * @details All tests should be written in other files. + * For eclipse console output, install ANSI Escape in Console + * from the eclipse market place to get colored characters. + */ + +#ifndef NO_UNIT_TEST_FRAMEWORK + +#define CATCH_CONFIG_RUNNER +#include + +#if CUSTOM_UNITTEST_RUNNER == 0 + +extern int customSetup(); + +int main( int argc, char* argv[] ) { + customSetup(); + + // Catch internal function call + int result = Catch::Session().run( argc, argv ); + + // global clean-up + return result; +} + +#endif + + +#endif diff --git a/fsfw/unittest/core/CatchSetup.cpp b/fsfw/unittest/core/CatchSetup.cpp new file mode 100644 index 0000000..f8543fd --- /dev/null +++ b/fsfw/unittest/core/CatchSetup.cpp @@ -0,0 +1,42 @@ +#include "CatchDefinitions.h" + +#include +#include + + +#ifdef GCOV +#include +#endif + +#include "../../objectmanager/ObjectManager.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../storagemanager/StorageManagerIF.h" +#include "../../datapool/DataPool.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + + +/* Global instantiations normally done in main.cpp */ +/* Initialize Data Pool */ +//namespace glob { +DataPool dataPool(datapool::dataPoolInit); +//} + + +namespace sif { +/* Set up output streams */ +ServiceInterfaceStream debug("DEBUG"); +ServiceInterfaceStream info("INFO"); +ServiceInterfaceStream error("ERROR"); +ServiceInterfaceStream warning("WARNING"); +} + +/* Global object manager */ +ObjectManagerIF *objectManager; + +int customSetup() { + // global setup + objectManager = new ObjectManager(Factory::produce); + objectManager -> initialize(); + return 0; +} + diff --git a/fsfw/unittest/core/core.mk b/fsfw/unittest/core/core.mk new file mode 100644 index 0000000..3e5626d --- /dev/null +++ b/fsfw/unittest/core/core.mk @@ -0,0 +1,3 @@ +CXXSRC += $(wildcard $(CURRENTPATH)/*.cpp) + +INCLUDES += $(CURRENTPATH) \ No newline at end of file diff --git a/fsfw/unittest/core/printChar.cpp b/fsfw/unittest/core/printChar.cpp new file mode 100644 index 0000000..755a13b --- /dev/null +++ b/fsfw/unittest/core/printChar.cpp @@ -0,0 +1,10 @@ +#include +#include + +void printChar(const char* character, bool errStream) { + if(errStream) { + std::putc(*character, stderr); + return; + } + std::putc(*character, stdout); +} diff --git a/fsfw/unittest/core/printChar.h b/fsfw/unittest/core/printChar.h new file mode 100644 index 0000000..80e867f --- /dev/null +++ b/fsfw/unittest/core/printChar.h @@ -0,0 +1,8 @@ +#ifndef FSFW_UNITTEST_CORE_PRINTCHAR_H_ +#define FSFW_UNITTEST_CORE_PRINTCHAR_H_ + + +extern "C" void printChar(const char*, bool errStream); + + +#endif /* FSFW_UNITTEST_CORE_PRINTCHAR_H_ */ diff --git a/fsfw/unittest/internal/InternalUnitTester.cpp b/fsfw/unittest/internal/InternalUnitTester.cpp new file mode 100644 index 0000000..503a71e --- /dev/null +++ b/fsfw/unittest/internal/InternalUnitTester.cpp @@ -0,0 +1,27 @@ +#include "InternalUnitTester.h" +#include "UnittDefinitions.h" + +#include "osal/IntTestMq.h" +#include "osal/IntTestSemaphore.h" +#include "osal/IntTestMutex.h" +#include "serialize/IntTestSerialization.h" + +#include + +InternalUnitTester::InternalUnitTester() {} + +InternalUnitTester::~InternalUnitTester() {} + +ReturnValue_t InternalUnitTester::performTests() { + sif::info << "Running internal unit tests.." << std::endl; + testserialize::test_serialization(); + testmq::testMq(); + testsemaph::testBinSemaph(); + testsemaph::testCountingSemaph(); + testmutex::testMutex(); + sif::info << "Internal unit tests finished." << std::endl; + return RETURN_OK; +} + + + diff --git a/fsfw/unittest/internal/InternalUnitTester.h b/fsfw/unittest/internal/InternalUnitTester.h new file mode 100644 index 0000000..b301b92 --- /dev/null +++ b/fsfw/unittest/internal/InternalUnitTester.h @@ -0,0 +1,29 @@ +#ifndef FRAMEWORK_TEST_UNITTESTCLASS_H_ +#define FRAMEWORK_TEST_UNITTESTCLASS_H_ + +#include "UnittDefinitions.h" +#include + +/** + * @brief Can be used for internal testing, for example for hardware specific + * tests which can not be run on a host-machine. + * + * TODO: A lot of ways to improve this class. A way for tests to subscribe + * in this central class would be nice. Right now, this is the class + * which simply calls all other tests from other files manually. + * Maybe there is a better way.. + */ +class InternalUnitTester: public HasReturnvaluesIF { +public: + InternalUnitTester(); + virtual~ InternalUnitTester(); + + /** + * Some function which calls all other tests + * @return + */ + virtual ReturnValue_t performTests(); +}; + + +#endif /* FRAMEWORK_TEST_UNITTESTCLASS_H_ */ diff --git a/fsfw/unittest/internal/UnittDefinitions.cpp b/fsfw/unittest/internal/UnittDefinitions.cpp new file mode 100644 index 0000000..0bdbfcc --- /dev/null +++ b/fsfw/unittest/internal/UnittDefinitions.cpp @@ -0,0 +1,7 @@ +#include + + ReturnValue_t unitt::put_error(std::string errorId) { + sif::error << "Unit Tester error: Failed at test ID " + << errorId << "\n" << std::flush; + return HasReturnvaluesIF::RETURN_FAILED; +} diff --git a/fsfw/unittest/internal/UnittDefinitions.h b/fsfw/unittest/internal/UnittDefinitions.h new file mode 100644 index 0000000..ea36fea --- /dev/null +++ b/fsfw/unittest/internal/UnittDefinitions.h @@ -0,0 +1,33 @@ +#ifndef UNITTEST_INTERNAL_UNITTDEFINITIONS_H_ +#define UNITTEST_INTERNAL_UNITTDEFINITIONS_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include +#include + +namespace tv { +// POD test values +static const bool tv_bool = true; +static const uint8_t tv_uint8 {5}; +static const uint16_t tv_uint16 {283}; +static const uint32_t tv_uint32 {929221}; +static const uint64_t tv_uint64 {2929329429}; + +static const int8_t tv_int8 {-16}; +static const int16_t tv_int16 {-829}; +static const int32_t tv_int32 {-2312}; + +static const float tv_float {8.2149214}; +static const float tv_sfloat = {-922.2321321}; +static const double tv_double {9.2132142141e8}; +static const double tv_sdouble {-2.2421e19}; +} + +namespace unitt { +ReturnValue_t put_error(std::string errorId); +} + + + +#endif /* UNITTEST_INTERNAL_UNITTDEFINITIONS_H_ */ diff --git a/fsfw/unittest/internal/internal.mk b/fsfw/unittest/internal/internal.mk new file mode 100644 index 0000000..799fd79 --- /dev/null +++ b/fsfw/unittest/internal/internal.mk @@ -0,0 +1,3 @@ +CXXSRC += $(wildcard $(CURRENTPATH)/osal/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/serialize/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/*.cpp) \ No newline at end of file diff --git a/fsfw/unittest/internal/osal/IntTestMq.cpp b/fsfw/unittest/internal/osal/IntTestMq.cpp new file mode 100644 index 0000000..6301637 --- /dev/null +++ b/fsfw/unittest/internal/osal/IntTestMq.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +#include + +using retval = HasReturnvaluesIF; + +void testmq::testMq() { + std::string id = "[testMq]"; + MessageQueueIF* testSenderMq = + QueueFactory::instance()->createMessageQueue(1); + MessageQueueId_t testSenderMqId = testSenderMq->getId(); + + MessageQueueIF* testReceiverMq = + QueueFactory::instance()->createMessageQueue(1); + MessageQueueId_t testReceiverMqId = testReceiverMq->getId(); + std::array testData { 0 }; + testData[0] = 42; + MessageQueueMessage testMessage(testData.data(), 1); + testSenderMq->setDefaultDestination(testReceiverMqId); + + + auto result = testSenderMq->sendMessage(testReceiverMqId, &testMessage); + if(result != retval::RETURN_OK) { + unitt::put_error(id); + } + MessageQueueMessage recvMessage; + result = testReceiverMq->receiveMessage(&recvMessage); + if(result != retval::RETURN_OK or recvMessage.getData()[0] != 42) { + unitt::put_error(id); + } + + result = testSenderMq->sendMessage(testReceiverMqId, &testMessage); + if(result != retval::RETURN_OK) { + unitt::put_error(id); + } + MessageQueueId_t senderId = 0; + result = testReceiverMq->receiveMessage(&recvMessage,&senderId); + if(result != retval::RETURN_OK or recvMessage.getData()[0] != 42) { + unitt::put_error(id); + } + if(senderId != testSenderMqId) { + unitt::put_error(id); + } + senderId = testReceiverMq->getLastPartner(); + if(senderId != testSenderMqId) { + unitt::put_error(id); + } + +} diff --git a/fsfw/unittest/internal/osal/IntTestMq.h b/fsfw/unittest/internal/osal/IntTestMq.h new file mode 100644 index 0000000..bc12a9b --- /dev/null +++ b/fsfw/unittest/internal/osal/IntTestMq.h @@ -0,0 +1,9 @@ +#ifndef UNITTEST_INTERNAL_INTESTMQ_H_ +#define UNITTEST_INTERNAL_INTESTMQ_H_ + +namespace testmq { +void testMq(); +} + + +#endif /* UNITTEST_INTERNAL_INTESTMQ_H_ */ diff --git a/fsfw/unittest/internal/osal/IntTestMutex.cpp b/fsfw/unittest/internal/osal/IntTestMutex.cpp new file mode 100644 index 0000000..3fd668d --- /dev/null +++ b/fsfw/unittest/internal/osal/IntTestMutex.cpp @@ -0,0 +1,42 @@ +#include "IntTestMutex.h" + +#include +#include + +#if defined(hosted) +#include +#include +#include +#endif + + +void testmutex::testMutex() { + std::string id = "[testMutex]"; + MutexIF* mutex = MutexFactory::instance()->createMutex(); + auto result = mutex->lockMutex(MutexIF::POLLING); + if(result != HasReturnvaluesIF::RETURN_OK) { + unitt::put_error(id); + } + // timed_mutex from the C++ library specifies undefined behaviour if + // the timed mutex is locked twice from the same thread. +#if defined(hosted) + // hold on, this actually worked ? :-D This calls the function from + // another thread and stores the returnvalue in a future. + auto future = std::async(&MutexIF::lockMutex, mutex, 1); + result = future.get(); +#else + result = mutex->lockMutex(MutexIF::TimeoutType::WAITING, 1); +#endif + if(result != MutexIF::MUTEX_TIMEOUT) { + unitt::put_error(id); + } + + result = mutex->unlockMutex(); + if(result != HasReturnvaluesIF::RETURN_OK) { + unitt::put_error(id); + } + result = mutex->unlockMutex(); + if(result != MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX) { + unitt::put_error(id); + } +} diff --git a/fsfw/unittest/internal/osal/IntTestMutex.h b/fsfw/unittest/internal/osal/IntTestMutex.h new file mode 100644 index 0000000..b467ea6 --- /dev/null +++ b/fsfw/unittest/internal/osal/IntTestMutex.h @@ -0,0 +1,10 @@ +#ifndef UNITTEST_INTERNAL_INTTESTMUTEX_H_ +#define UNITTEST_INTERNAL_INTTESTMUTEX_H_ + +namespace testmutex { +void testMutex(); +} + + + +#endif /* UNITTEST_INTERNAL_INTTESTMUTEX_H_ */ diff --git a/fsfw/unittest/internal/osal/IntTestSemaphore.cpp b/fsfw/unittest/internal/osal/IntTestSemaphore.cpp new file mode 100644 index 0000000..534a6a6 --- /dev/null +++ b/fsfw/unittest/internal/osal/IntTestSemaphore.cpp @@ -0,0 +1,160 @@ +#include "IntTestSemaphore.h" +#include +#include +#include +#include + + +void testsemaph::testBinSemaph() { + std::string id = "[BinSemaphore]"; + SemaphoreIF* binSemaph = + SemaphoreFactory::instance()->createBinarySemaphore(); + if(binSemaph == nullptr) { + return; + } + testBinSemaphoreImplementation(binSemaph, id); + SemaphoreFactory::instance()->deleteSemaphore(binSemaph); +#if defined(freeRTOS) + SemaphoreIF* binSemaphUsingTask = + SemaphoreFactory::instance()->createBinarySemaphore(1); + testBinSemaphoreImplementation(binSemaphUsingTask, id); + SemaphoreFactory::instance()->deleteSemaphore(binSemaphUsingTask); +#endif +} + + +void testsemaph::testCountingSemaph() { + std::string id = "[CountingSemaph]"; + { + // First test: create a binary semaphore by using a counting semaphore. + SemaphoreIF* countingSemaph = SemaphoreFactory::instance()-> + createCountingSemaphore(1,1); + if(countingSemaph == nullptr) { + return; + } + testBinSemaphoreImplementation(countingSemaph, id); + SemaphoreFactory::instance()->deleteSemaphore(countingSemaph); +#if defined(freeRTOS) + countingSemaph = SemaphoreFactory::instance()-> + createCountingSemaphore(1, 1, 1); + testBinSemaphoreImplementation(countingSemaph, id); + SemaphoreFactory::instance()->deleteSemaphore(countingSemaph); +#endif + } + + { + // Second test: counting semaphore with count 3 and init count of 3. + SemaphoreIF* countingSemaph = SemaphoreFactory::instance()-> + createCountingSemaphore(3,3); + testCountingSemaphImplementation(countingSemaph, id); + SemaphoreFactory::instance()->deleteSemaphore(countingSemaph); +#if defined(freeRTOS) + countingSemaph = SemaphoreFactory::instance()-> + createCountingSemaphore(3, 0, 1); + uint8_t semaphCount = countingSemaph->getSemaphoreCounter(); + if(semaphCount != 0) { + unitt::put_error(id); + } + // release 3 times in a row + for(int i = 0; i < 3; i++) { + auto result = countingSemaph->release(); + if(result != HasReturnvaluesIF::RETURN_OK) { + unitt::put_error(id); + } + } + testCountingSemaphImplementation(countingSemaph, id); + SemaphoreFactory::instance()->deleteSemaphore(countingSemaph); +#endif + } +} + + +void testsemaph::testBinSemaphoreImplementation(SemaphoreIF* binSemaph, + std::string id) { + uint8_t semaphCount = binSemaph->getSemaphoreCounter(); + if(semaphCount != 1) { + unitt::put_error(id); + } + + ReturnValue_t result = binSemaph->release(); + if(result != SemaphoreIF::SEMAPHORE_NOT_OWNED) { + unitt::put_error(id); + } + result = binSemaph->acquire(SemaphoreIF::BLOCKING); + if(result != HasReturnvaluesIF::RETURN_OK) { + unitt::put_error(id); + } + + // There is not really a point in testing time related, the task + // might get interrupted.. + { + //Stopwatch stopwatch(false); + result = binSemaph->acquire(SemaphoreIF::TimeoutType::WAITING, 10); + //dur_millis_t time = stopwatch.stop(); +// if(abs(time - 10) > 2) { +// sif::error << "UnitTester: Semaphore timeout measured incorrect." +// << std::endl; +// unitt::put_error(id); +// } + } + + if(result != SemaphoreIF::SEMAPHORE_TIMEOUT) { + unitt::put_error(id); + } + + semaphCount = binSemaph->getSemaphoreCounter(); + if(semaphCount != 0) { + unitt::put_error(id); + } + + result = binSemaph->release(); + if(result != HasReturnvaluesIF::RETURN_OK) { + unitt::put_error(id); + } +} + +void testsemaph::testCountingSemaphImplementation(SemaphoreIF* countingSemaph, + std::string id) { + // check count getter function + uint8_t semaphCount = countingSemaph->getSemaphoreCounter(); + if(semaphCount != 3) { + unitt::put_error(id); + } + ReturnValue_t result = countingSemaph->release(); + if(result != SemaphoreIF::SEMAPHORE_NOT_OWNED) { + unitt::put_error(id); + } + // acquire 3 times in a row + for(int i = 0; i < 3; i++) { + result = countingSemaph->acquire(SemaphoreIF::BLOCKING); + if(result != HasReturnvaluesIF::RETURN_OK) { + unitt::put_error(id); + } + } + + { + Stopwatch stopwatch(false); + // attempt to take when count is 0, measure time + result = countingSemaph->acquire(SemaphoreIF::TimeoutType::WAITING, 10); + dur_millis_t time = stopwatch.stop(); + if(abs(time - 10) > 1) { + unitt::put_error(id); + } + } + + if(result != SemaphoreIF::SEMAPHORE_TIMEOUT) { + unitt::put_error(id); + } + + // release 3 times in a row + for(int i = 0; i < 3; i++) { + result = countingSemaph->release(); + if(result != HasReturnvaluesIF::RETURN_OK) { + unitt::put_error(id); + } + } + // assert correct full count + if(countingSemaph->getSemaphoreCounter() != 3) { + unitt::put_error(id); + } +} diff --git a/fsfw/unittest/internal/osal/IntTestSemaphore.h b/fsfw/unittest/internal/osal/IntTestSemaphore.h new file mode 100644 index 0000000..af26c13 --- /dev/null +++ b/fsfw/unittest/internal/osal/IntTestSemaphore.h @@ -0,0 +1,15 @@ +#ifndef UNITTEST_INTERNAL_INTTESTSEMAPHORE_H_ +#define UNITTEST_INTERNAL_INTTESTSEMAPHORE_H_ +class SemaphoreIF; +#include + +namespace testsemaph { +void testBinSemaph(); +void testBinSemaphoreImplementation(SemaphoreIF* binSemaph, std::string id); +void testCountingSemaph(); +void testCountingSemaphImplementation(SemaphoreIF* countingSemaph, + std::string id); +} + + +#endif /* UNITTEST_INTERNAL_INTTESTSEMAPHORE_H_ */ diff --git a/fsfw/unittest/internal/serialize/IntTestSerialization.cpp b/fsfw/unittest/internal/serialize/IntTestSerialization.cpp new file mode 100644 index 0000000..3f231a4 --- /dev/null +++ b/fsfw/unittest/internal/serialize/IntTestSerialization.cpp @@ -0,0 +1,230 @@ +#include "IntTestSerialization.h" +#include +#include +#include +#include +#include + +using retval = HasReturnvaluesIF; +std::array testserialize::test_array = { 0 }; + +ReturnValue_t testserialize::test_serialization() { + // Here, we test all serialization tools. First test basic cases. + ReturnValue_t result = test_endianness_tools(); + if(result != retval::RETURN_OK) { + return result; + } + result = test_autoserialization(); + if(result != retval::RETURN_OK) { + return result; + } + result = test_serial_buffer_adapter(); + if(result != retval::RETURN_OK) { + return result; + } + return retval::RETURN_OK; +} + +ReturnValue_t testserialize::test_endianness_tools() { + std::string id = "[test_endianness_tools]"; + test_array[0] = 0; + test_array[1] = 0; + uint16_t two_byte_value = 1; + size_t size = 0; + uint8_t* p_array = test_array.data(); + SerializeAdapter::serialize(&two_byte_value, &p_array, &size, 2, + SerializeIF::Endianness::MACHINE); + // Little endian: Value one on first byte + if(test_array[0] != 1 and test_array[1] != 0) { + return unitt::put_error(id); + + } + + p_array = test_array.data(); + size = 0; + SerializeAdapter::serialize(&two_byte_value, &p_array, &size, 2, + SerializeIF::Endianness::BIG); + // Big endian: Value one on second byte + if(test_array[0] != 0 and test_array[1] != 1) { + return unitt::put_error(id); + } + return retval::RETURN_OK; +} + +ReturnValue_t testserialize::test_autoserialization() { + std::string id = "[test_autoserialization]"; + // Unit Test getSerializedSize + if(SerializeAdapter:: + getSerializedSize(&tv::tv_bool) != sizeof(tv::tv_bool) or + SerializeAdapter:: + getSerializedSize(&tv::tv_uint8) != sizeof(tv::tv_uint8) or + SerializeAdapter:: + getSerializedSize(&tv::tv_uint16) != sizeof(tv::tv_uint16) or + SerializeAdapter:: + getSerializedSize(&tv::tv_uint32) != sizeof(tv::tv_uint32) or + SerializeAdapter:: + getSerializedSize(&tv::tv_uint64) != sizeof(tv::tv_uint64) or + SerializeAdapter:: + getSerializedSize(&tv::tv_int8) != sizeof(tv::tv_int8) or + SerializeAdapter:: + getSerializedSize(&tv::tv_double) != sizeof(tv::tv_double) or + SerializeAdapter:: + getSerializedSize(&tv::tv_int16) != sizeof(tv::tv_int16) or + SerializeAdapter:: + getSerializedSize(&tv::tv_int32) != sizeof(tv::tv_int32) or + SerializeAdapter:: + getSerializedSize(&tv::tv_float) != sizeof(tv::tv_float)) + { + return unitt::put_error(id); + } + + size_t serialized_size = 0; + uint8_t * p_array = test_array.data(); + + SerializeAdapter::serialize(&tv::tv_bool, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_uint8, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_uint16, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_uint32, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_int8, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_int16, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_int32, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_uint64, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_float, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_double, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_sfloat, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv::tv_sdouble, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + // expected size is 1 + 1 + 2 + 4 + 1 + 2 + 4 + 8 + 4 + 8 + 4 + 8 + if(serialized_size != 47) { + return unitt::put_error(id); + } + + p_array = test_array.data(); + size_t remaining_size = serialized_size; + bool tv_bool; + uint8_t tv_uint8; + uint16_t tv_uint16; + uint32_t tv_uint32; + int8_t tv_int8; + int16_t tv_int16; + int32_t tv_int32; + uint64_t tv_uint64; + float tv_float; + double tv_double; + float tv_sfloat; + double tv_sdouble; + + SerializeAdapter::deSerialize(&tv_bool, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_uint8, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_uint16, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_uint32, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_int8, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_int16, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_int32, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_uint64, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_float, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_double, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_sfloat, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_sdouble, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + + if(tv_bool != tv::tv_bool or tv_uint8 != tv::tv_uint8 or + tv_uint16 != tv::tv_uint16 or tv_uint32 != tv::tv_uint32 or + tv_uint64 != tv::tv_uint64 or tv_int8 != tv::tv_int8 or + tv_int16 != tv::tv_int16 or tv_int32 != tv::tv_int32) + { + return unitt::put_error(id); + } + + // These epsilon values were just guessed.. It appears to work though. + if(abs(tv_float - tv::tv_float) > 0.0001 or + abs(tv_double - tv::tv_double) > 0.01 or + abs(tv_sfloat - tv::tv_sfloat) > 0.0001 or + abs(tv_sdouble - tv::tv_sdouble) > 0.01) { + return unitt::put_error(id); + } + + // Check overflow + return retval::RETURN_OK; +} + +// TODO: Also test for constant buffers. +ReturnValue_t testserialize::test_serial_buffer_adapter() { + std::string id = "[test_serial_buffer_adapter]"; + + // I will skip endian swapper testing, its going to be changed anyway.. + // uint8_t tv::tv_uint8_swapped = EndianSwapper::swap(tv::tv_uint8); + + size_t serialized_size = 0; + uint8_t * p_array = test_array.data(); + std::array test_serial_buffer {5, 4, 3, 2, 1}; + SerialBufferAdapter tv_serial_buffer_adapter = + SerialBufferAdapter(test_serial_buffer.data(), + test_serial_buffer.size(), false); + uint16_t testUint16 = 16; + + SerializeAdapter::serialize(&tv::tv_bool, &p_array,&serialized_size, + test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_serial_buffer_adapter, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&testUint16, &p_array, &serialized_size, + test_array.size(), SerializeIF::Endianness::MACHINE); + + if(serialized_size != 8 or test_array[0] != true or test_array[1] != 5 + or test_array[2] != 4 or test_array[3] != 3 or test_array[4] != 2 + or test_array[5] != 1) + { + return unitt::put_error(id); + } + memcpy(&testUint16, test_array.data() + 6, sizeof(testUint16)); + if(testUint16 != 16) { + return unitt::put_error(id); + } + + // Serialize with size field + SerialBufferAdapter tv_serial_buffer_adapter2 = + SerialBufferAdapter(test_serial_buffer.data(), + test_serial_buffer.size(), true); + serialized_size = 0; + p_array = test_array.data(); + SerializeAdapter::serialize(&tv::tv_bool, &p_array,&serialized_size, + test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_serial_buffer_adapter2, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&testUint16, &p_array, &serialized_size, + test_array.size(), SerializeIF::Endianness::MACHINE); + + if(serialized_size != 9 or test_array[0] != true or test_array[1] != 5 + or test_array[2] != 5 or test_array[3] != 4 or test_array[4] != 3 + or test_array[5] != 2 or test_array[6] != 1) + { + return unitt::put_error(id); + } + memcpy(&testUint16, test_array.data() + 7, sizeof(testUint16)); + if(testUint16 != 16) { + return unitt::put_error(id); + } + return retval::RETURN_OK; +} diff --git a/fsfw/unittest/internal/serialize/IntTestSerialization.h b/fsfw/unittest/internal/serialize/IntTestSerialization.h new file mode 100644 index 0000000..f8841b8 --- /dev/null +++ b/fsfw/unittest/internal/serialize/IntTestSerialization.h @@ -0,0 +1,15 @@ +#ifndef UNITTEST_INTERNAL_INTTESTSERIALIZATION_H_ +#define UNITTEST_INTERNAL_INTTESTSERIALIZATION_H_ +#include +#include + +namespace testserialize { +ReturnValue_t test_serialization(); +ReturnValue_t test_endianness_tools(); +ReturnValue_t test_autoserialization(); +ReturnValue_t test_serial_buffer_adapter(); + +extern std::array test_array; +} + +#endif /* UNITTEST_INTERNAL_INTTESTSERIALIZATION_H_ */ diff --git a/fsfw/unittest/lcov.sh b/fsfw/unittest/lcov.sh new file mode 100644 index 0000000..4db16e5 --- /dev/null +++ b/fsfw/unittest/lcov.sh @@ -0,0 +1,3 @@ +#!/bin/bash +lcov --capture --directory . --output-file coverage.info +genhtml coverage.info --output-directory _coverage diff --git a/fsfw/unittest/testcfg/FSFWConfig.h b/fsfw/unittest/testcfg/FSFWConfig.h new file mode 100644 index 0000000..4fb224c --- /dev/null +++ b/fsfw/unittest/testcfg/FSFWConfig.h @@ -0,0 +1,46 @@ +#ifndef CONFIG_FSFWCONFIG_H_ +#define CONFIG_FSFWCONFIG_H_ + +#include + +//! Used to determine whether C++ ostreams are used +//! Those can lead to code bloat. +#define FSFW_CPP_OSTREAM_ENABLED 1 + +//! Reduced printout to further decrese code size +//! Be careful, this also turns off most diagnostic prinouts! +#define FSFW_REDUCED_PRINTOUT 0 + +//! Can be used to enable debugging printouts for developing the FSFW +#define FSFW_DEBUGGING 0 + +//! Defines the FIFO depth of each commanding service base which +//! also determines how many commands a CSB service can handle in one cycle +//! simulataneously. This will increase the required RAM for +//! each CSB service ! +#define FSFW_CSB_FIFO_DEPTH 6 + +//! If FSFW_OBJ_EVENT_TRANSLATION is set to one, +//! additional output which requires the translation files translateObjects +//! and translateEvents (and their compiled source files) +#define FSFW_OBJ_EVENT_TRANSLATION 0 + +//! If -DDEBUG is supplied in the build defines, there will be +//! additional output which requires the translation files translateObjects +//! and translateEvents (and their compiles source files) +#if FSFW_OBJ_EVENT_TRANSLATION == 1 +#define FSFW_DEBUG_OUTPUT 1 +//! Specify whether info events are printed too. +#define FSFW_DEBUG_INFO 1 +#include +#include +#else +#define FSFW_DEBUG_OUTPUT 0 +#endif + +//! When using the newlib nano library, C99 support for stdio facilities +//! will not be provided. This define should be set to 1 if this is the case. +#define FSFW_NO_C99_IO 1 + + +#endif /* CONFIG_FSFWCONFIG_H_ */ diff --git a/fsfw/unittest/testcfg/Makefile-FSFW-Tests b/fsfw/unittest/testcfg/Makefile-FSFW-Tests new file mode 100644 index 0000000..2017d2b --- /dev/null +++ b/fsfw/unittest/testcfg/Makefile-FSFW-Tests @@ -0,0 +1,415 @@ +#------------------------------------------------------------------------------- +# Makefile for FSFW Test +#------------------------------------------------------------------------------- +# User-modifiable options +#------------------------------------------------------------------------------- +# Fundamentals on the build process of C/C++ Software: +# https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html + +# Make documentation: https://www.gnu.org/software/make/manual/make.pdf +# Online: https://www.gnu.org/software/make/manual/make.html +# General rules: http://make.mad-scientist.net/papers/rules-of-makefiles/#rule3 +SHELL = /bin/sh + +# Chip & board used for compilation +# (can be overriden by adding CHIP=chip and BOARD=board to the command-line) +# Unit Test can only be run on host machine for now (Linux) +FRAMEWORK_PATH = fsfw +FILE_ROOT = $(FRAMEWORK_PATH)/unittest +BOARD = unittest +LINUX = 1 +OS_FSFW = linux +CUSTOM_DEFINES += -D$(OS_FSFW) + +# Copied from stackoverflow, can be used to differentiate between Windows +# and Linux +ifeq ($(OS),Windows_NT) + CUSTOM_DEFINES += -DWIN32 + ifeq ($(PROCESSOR_ARCHITEW6432),AMD64) + CUSTOM_DEFINES += -DAMD64 + else + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + CUSTOM_DEFINES += -DAMD64 + endif + ifeq ($(PROCESSOR_ARCHITECTURE),x86) + CUSTOM_DEFINES += -DIA32 + endif + endif +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S),Linux) + DETECTED_OS = LINUX + CUSTOM_DEFINES += -DLINUX + endif + ifeq ($(UNAME_S),Darwin) + CUSTOM_DEFINES += -DOSX + endif + UNAME_P := $(shell uname -p) + ifeq ($(UNAME_P),x86_64) + CUSTOM_DEFINES += -DAMD64 + endif + ifneq ($(filter %86,$(UNAME_P)),) + CUSTOM_DEFINES += -DIA32 + endif + ifneq ($(filter arm%,$(UNAME_P)),) + CUSTOM_DEFINES += -DARM + endif +endif + +UNIT_TEST = 1 +# General folder paths +CONFIG_PATH = $(FILE_ROOT)/config +UNIT_TEST_PATH = $(FILE_ROOT)/tests +CORE_PATH = $(FILE_ROOT)/core + +# Output file basename +BASENAME = fsfw +BINARY_NAME := $(BASENAME)-$(BOARD) +# Output files will be put in this directory inside +OUTPUT_FOLDER = $(OS) + +# Optimization level. Optimized for debugging. +OPTIMIZATION = -O0 + +# Default debug output. Optimized for debugging. +DEBUG_LEVEL = -g3 + +ifdef GCOV +CUSTOM_DEFINES += -DGCOV +endif + + +# Output directories +BUILDPATH = _bin +DEPENDPATH = _dep +OBJECTPATH = _obj + +ifeq ($(MAKECMDGOALS),mission) +BUILD_FOLDER = mission +else +BUILD_FOLDER = devel +endif + +DEPENDDIR = $(DEPENDPATH)/$(OUTPUT_FOLDER)/$(BUILD_FOLDER) +OBJDIR = $(OBJECTPATH)/$(OUTPUT_FOLDER)/$(BUILD_FOLDER) +BINDIR = $(BUILDPATH) + +CLEANDEP = $(DEPENDPATH)/$(OUTPUT_FOLDER) +CLEANOBJ = $(OBJECTPATH)/$(OUTPUT_FOLDER) +CLEANBIN = $(BUILDPATH) +#------------------------------------------------------------------------------- +# Tools and Includes +#------------------------------------------------------------------------------- + +# Tool suffix when cross-compiling +CROSS_COMPILE = + +# C Compiler +CC = $(CROSS_COMPILE)gcc + +# C++ compiler +CXX = $(CROSS_COMPILE)g++ + +# Additional Tools +SIZE = $(CROSS_COMPILE)size +STRIP = $(CROSS_COMPILE)strip +CP = $(CROSS_COMPILE)objcopy + +HEXCOPY = $(CP) -O ihex +BINCOPY = $(CP) -O binary +# files to be compiled, will be filled in by include makefiles +# := assignment is neccessary so we get all paths right +# https://www.gnu.org/software/make/manual/html_node/Flavors.html +CSRC := +CXXSRC := +ASRC := +INCLUDES := + +# Directories where $(directoryname).mk files should be included from +SUBDIRS := $(FRAMEWORK_PATH) $(TEST_PATH) $(UNIT_TEST_PATH) $(CONFIG_PATH) \ + $(CORE_PATH) + + +I_INCLUDES += $(addprefix -I, $(INCLUDES)) + +# This is a hack from http://make.mad-scientist.net/the-eval-function/ +# +# The problem is, that included makefiles should be aware of their relative path +# but not need to guess or hardcode it. So we set $(CURRENTPATH) for them. If +# we do this globally and the included makefiles want to include other makefiles as +# well, they would overwrite $(CURRENTPATH), screwing the include after them. +# +# By using a for-loop with an eval'd macro, we can generate the code to include all +# sub-makefiles (with the correct $(CURRENTPATH) set) before actually evaluating +# (and by this possibly changing $(CURRENTPATH)) them. +# +# This works recursively, if an included makefile wants to include, it can safely set +# $(SUBDIRS) (which has already been evaluated here) and do +# "$(foreach S,$(SUBDIRS),$(eval $(INCLUDE_FILE)))" +# $(SUBDIRS) must be relative to the project root, so to include subdir foo, set +# $(SUBDIRS) = $(CURRENTPATH)/foo. +define INCLUDE_FILE +CURRENTPATH := $S +include $(S)/$(notdir $S).mk +endef +$(foreach S,$(SUBDIRS),$(eval $(INCLUDE_FILE))) + +INCLUDES += $(FILE_ROOT) +INCLUDES += $(FILE_ROOT)/catch2/ + +#------------------------------------------------------------------------------- +# Source Files +#------------------------------------------------------------------------------- + +# All source files which are not includes by the .mk files are added here +# Please ensure that no files are included by both .mk file and here ! + +# if a target is not listed in the current directory, +# make searches in the directories specified with VPATH + +# All C Sources included by .mk files are assigned here +# Add the objects to sources so dependency handling works +C_OBJECTS += $(CSRC:.c=.o) + +# Objects built from Assembly source files +ASM_OBJECTS = $(ASRC:.S=.o) + +# Objects built from C++ source files +CXX_OBJECTS += $(CXXSRC:.cpp=.o) + +#------------------------------------------------------------------------------- +# Build Configuration + Output +#------------------------------------------------------------------------------- + +TARGET = Debug build. +DEBUG_MESSAGE = Off +OPTIMIZATION_MESSAGE = Off + +# Define Messages +MSG_INFO = Software: Hosted unittest \(Catch2\) for the FSFW. +MSG_OPTIMIZATION = Optimization: $(OPTIMIZATION), $(OPTIMIZATION_MESSAGE) +MSG_TARGET = Target Build: $(TARGET) +MSG_DEBUG = Debug level: $(DEBUG_LEVEL), FSFW Debugging: $(DEBUG_MESSAGE) + +MSG_LINKING = Linking: +MSG_COMPILING = Compiling: +MSG_ASSEMBLING = Assembling: +MSG_DEPENDENCY = Collecting dependencies for: +MSG_BINARY = Generate binary: + +# See https://stackoverflow.com/questions/6687630/how-to-remove-unused-c-c-symbols-with-gcc-and-ld +# Used to throw away unused code. Reduces code size significantly ! +# -Wl,--gc-sections: needs to be passed to the linker to throw aways unused code +ifdef KEEP_UNUSED_CODE +PROTOTYPE_OPTIMIZATION = +UNUSED_CODE_REMOVAL = +else +PROTOTYPE_OPTIMIZATION = -ffunction-sections -fdata-sections +UNUSED_CODE_REMOVAL = -Wl,--gc-sections +# Link time optimization +# See https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html for reference +# Link time is larger and size of object files can not be retrieved +# but resulting binary is smaller. Could be used in mission/deployment build +# Requires -ffunction-section in linker call +LINK_TIME_OPTIMIZATION = -flto +OPTIMIZATION += $(PROTOTYPE_OPTIMIZATION) +endif + +# Dependency Flags +# These flags tell the compiler to build dependencies +# See: https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html +# Using following guide: http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#combine +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPENDDIR)/$*.d + +# Flags for the compiler call +# - std: Which C++ version to use. Common versions: c++11, c++14 and c++17 +# - Wall: enable all warnings +# - Wextra: enable extra warnings +# - g: defines debug level +# - fmessage-length: to control the formatting algorithm for diagnostic messages; +# =0 means no line-wrapping is done; each error message appears on a single line +# - fno-exceptions: stops generating extra code needed to propagate exceptions, +# which can produce significant data size overhead +CUSTOM_DEFINES += -DUNIT_TEST +WARNING_FLAGS = -Wall -Wshadow=local -Wextra -Wimplicit-fallthrough=1 \ + -Wno-unused-parameter + +CXXDEFINES := $(CUSTOM_DEFINES) +CFLAGS += +CXXFLAGS += -I. $(DEBUG_LEVEL) $(WARNING_FLAGS) $(DEPFLAGS) -fmessage-length=0 $(OPTIMIZATION)\ + $(I_INCLUDES) $(CXXDEFINES) +CPPFLAGS += -std=c++11 + +# Flags for the linker call +# LINK_INCLUDES specify the path to used libraries and the linker script +# LINK_LIBRARIES: Link real time support +LDFLAGS := $(DEBUG_LEVEL) $(UNUSED_CODE_REMOVAL) $(OPTIMIZATION) -pthread +LINK_INCLUDES := +LINK_LIBRARIES := + +ifdef LINUX +LINK_LIBRARIES += -lrt +endif + +ifeq ($(OS),Windows_NT) +LINK_LIBRARIES += -lwsock32 -lws2_32 +LDFLASGS += -fuse-ld=lld +endif + +# Gnu Coverage Tools Flags +ifdef GCOV +GCOV_CXXFLAGS = -fprofile-arcs -ftest-coverage --coverage -fno-inline \ + -fno-inline-small-functions -fno-default-inline +CXXFLAGS += $(GCOV_CXXFLAGS) +GCOV_LINKER_LIBS = -lgcov -fprofile-arcs -ftest-coverage +LINK_LIBRARIES += $(GCOV_LINKER_LIBS) +endif + +# $(info $${CXXFLAGS} is [${CXXFLAGS}]) + +#------------------------------------------------------------------------------- +# Rules +#------------------------------------------------------------------------------- +# the call function assigns parameters to temporary variables +# https://www.gnu.org/software/make/manual/make.html#Call-Function +# $(1) = Memory names +# Rules are called for each memory type +# Two Expansion Symbols $$ are to escape the dollar sign for eval. +# See: http://make.mad-scientist.net/the-eval-function/ + +default: all + +# Cleans all files +hardclean: + -rm -rf $(BUILDPATH) + -rm -rf $(OBJECTPATH) + -rm -rf $(DEPENDPATH) + +# Only clean files for current build +clean: + -rm -rf $(CLEANOBJ) + -rm -rf $(CLEANBIN) + -rm -rf $(CLEANDEP) + +# Only clean binaries. Useful for changing the binary type when object files +# are already compiled so complete rebuild is not necessary +cleanbin: + -rm -rf $(CLEANBIN) + +# In this section, the binaries are built for all selected memories +# notestfw: all +all: executable + +# Build target configuration +release: OPTIMIZATION = -Os $(PROTOTYPE_OPTIMIZATION) $(LINK_TIME_OPTIMIZATION) +release: LINK_TIME_OPTIMIZATION = -flto +release: TARGET = Mission build. +release: OPTIMIZATION_MESSAGE = On with Link Time Optimization + +debug: CXXDEFINES += -DDEBUG +debug: TARGET = Debug +debug: DEBUG_MESSAGE = On + +ifndef KEEP_UNUSED_CODE +debug release: OPTIMIZATION_MESSAGE += , no unused code removal +endif + +debug release notestfw: executable + +executable: $(BINDIR)/$(BINARY_NAME).elf + @echo + @echo $(MSG_INFO) + @echo $(MSG_TARGET) + @echo $(MSG_OPTIMIZATION) + @echo $(MSG_DEBUG) + +C_OBJECTS_PREFIXED = $(addprefix $(OBJDIR)/, $(C_OBJECTS)) +CXX_OBJECTS_PREFIXED = $(addprefix $(OBJDIR)/, $(CXX_OBJECTS)) +ASM_OBJECTS_PREFIXED = $(addprefix $(OBJDIR)/, $(ASM_OBJECTS)) +ALL_OBJECTS = $(ASM_OBJECTS_PREFIXED) $(C_OBJECTS_PREFIXED) \ + $(CXX_OBJECTS_PREFIXED) + +# Useful for debugging the Makefile +# Also see: https://www.oreilly.com/openbook/make3/book/ch12.pdf +# $(info $${ALL_OBJECTS} is [${ALL_OBJECTS}]) +# $(info $${CXXSRC} is [${CXXSRC}]) + +# Automatic variables are used here extensively. Some of them +# are escaped($$) to suppress immediate evaluation. The most important ones are: +# $@: Name of Target (left side of rule) +# $<: Name of the first prerequisite (right side of rule) +# @^: List of all prerequisite, omitting duplicates +# @D: Directory and file-within-directory part of $@ + +# Generates binary and displays all build properties +# -p with mkdir ignores error and creates directory when needed. + +# SHOW_DETAILS = 1 + + +# Link with required libraries: HAL (Hardware Abstraction Layer) and +# HCC (File System Library) +$(BINDIR)/$(BINARY_NAME).elf: $(ALL_OBJECTS) + @echo + @echo $(MSG_LINKING) Target $@ + @mkdir -p $(@D) +ifdef SHOW_DETAILS + $(CXX) $(LDFLAGS) $(LINK_INCLUDES) -o $@ $^ $(LINK_LIBRARIES) +else + @$(CXX) $(LDFLAGS) $(LINK_INCLUDES) -o $@ $^ $(LINK_LIBRARIES) +endif +ifeq ($(BUILD_FOLDER), mission) +# With Link Time Optimization, section size is not available + $(SIZE) $@ +else + $(SIZE) $^ $@ +endif + +$(BINDIR)/$(BINARY_NAME).hex: $(BINDIR)/$(BINARY_NAME).elf + @echo + @echo $(MSG_BINARY) + @mkdir -p $(@D) + $(HEXCOPY) $< $@ + +# Build new objects for changed dependencies. +$(OBJDIR)/%.o: %.cpp +$(OBJDIR)/%.o: %.cpp $(DEPENDDIR)/%.d | $(DEPENDDIR) + @echo + @echo $(MSG_COMPILING) $< + @mkdir -p $(@D) +ifdef SHOW_DETAILS + $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $< +else + @$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $< +endif + +$(OBJDIR)/%.o: %.c +$(OBJDIR)/%.o: %.c $(DEPENDDIR)/%.d | $(DEPENDDIR) + @echo + @echo $(MSG_COMPILING) $< + @mkdir -p $(@D) +ifdef SHOW_DETAILS + $(CC) $(CXXFLAGS) $(CFLAGS) -c -o $@ $< +else + @$(CC) $(CXXFLAGS) $(CFLAGS) -c -o $@ $< +endif + +#------------------------------------------------------------------------------- +# Dependency Handling +#------------------------------------------------------------------------------- + +# Dependency Handling according to following guide: +# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/ +$(DEPENDDIR): + @mkdir -p $(@D) +DEPENDENCY_RELATIVE = $(CSRC:.c=.d) $(CXXSRC:.cpp=.d) +# This is the list of all dependencies +DEPFILES = $(addprefix $(DEPENDDIR)/, $(DEPENDENCY_RELATIVE)) +# Create subdirectories for dependencies +$(DEPFILES): + @mkdir -p $(@D) +# Include all dependencies +include $(wildcard $(DEPFILES)) + +# .PHONY tells make that these targets aren't files +.PHONY: clean release debug all hardclean cleanbin diff --git a/fsfw/unittest/testcfg/TestsConfig.h b/fsfw/unittest/testcfg/TestsConfig.h new file mode 100644 index 0000000..cd967fa --- /dev/null +++ b/fsfw/unittest/testcfg/TestsConfig.h @@ -0,0 +1,8 @@ +#ifndef FSFW_UNITTEST_CONFIG_TESTSCONFIG_H_ +#define FSFW_UNITTEST_CONFIG_TESTSCONFIG_H_ + + +#define CUSTOM_UNITTEST_RUNNER 0 + + +#endif /* FSFW_UNITTEST_CONFIG_TESTSCONFIG_H_ */ diff --git a/fsfw/unittest/testcfg/cdatapool/dataPoolInit.cpp b/fsfw/unittest/testcfg/cdatapool/dataPoolInit.cpp new file mode 100644 index 0000000..ad2dc4e --- /dev/null +++ b/fsfw/unittest/testcfg/cdatapool/dataPoolInit.cpp @@ -0,0 +1,5 @@ +#include "dataPoolInit.h" + +void datapool::dataPoolInit(std::map * poolMap) { + +} diff --git a/fsfw/unittest/testcfg/cdatapool/dataPoolInit.h b/fsfw/unittest/testcfg/cdatapool/dataPoolInit.h new file mode 100644 index 0000000..9425d76 --- /dev/null +++ b/fsfw/unittest/testcfg/cdatapool/dataPoolInit.h @@ -0,0 +1,17 @@ +#ifndef HOSTED_CONFIG_CDATAPOOL_DATAPOOLINIT_H_ +#define HOSTED_CONFIG_CDATAPOOL_DATAPOOLINIT_H_ + +#include +#include +#include +#include + + +namespace datapool { + void dataPoolInit(std::map * poolMap); + + enum datapoolvariables { + NO_PARAMETER = 0, + }; +} +#endif /* CONFIG_CDATAPOOL_DATAPOOLINIT_H_ */ diff --git a/fsfw/unittest/testcfg/devices/logicalAddresses.cpp b/fsfw/unittest/testcfg/devices/logicalAddresses.cpp new file mode 100644 index 0000000..c7ce314 --- /dev/null +++ b/fsfw/unittest/testcfg/devices/logicalAddresses.cpp @@ -0,0 +1,5 @@ +#include "logicalAddresses.h" + + + + diff --git a/fsfw/unittest/testcfg/devices/logicalAddresses.h b/fsfw/unittest/testcfg/devices/logicalAddresses.h new file mode 100644 index 0000000..cdf8702 --- /dev/null +++ b/fsfw/unittest/testcfg/devices/logicalAddresses.h @@ -0,0 +1,15 @@ +#ifndef CONFIG_DEVICES_LOGICALADDRESSES_H_ +#define CONFIG_DEVICES_LOGICALADDRESSES_H_ + +#include +#include +#include + +namespace addresses { + /* Logical addresses have uint32_t datatype */ + enum logicalAddresses: address_t { + }; +} + + +#endif /* CONFIG_DEVICES_LOGICALADDRESSES_H_ */ diff --git a/fsfw/unittest/testcfg/devices/powerSwitcherList.cpp b/fsfw/unittest/testcfg/devices/powerSwitcherList.cpp new file mode 100644 index 0000000..343f78d --- /dev/null +++ b/fsfw/unittest/testcfg/devices/powerSwitcherList.cpp @@ -0,0 +1,4 @@ +#include "powerSwitcherList.h" + + + diff --git a/fsfw/unittest/testcfg/devices/powerSwitcherList.h b/fsfw/unittest/testcfg/devices/powerSwitcherList.h new file mode 100644 index 0000000..86ddea5 --- /dev/null +++ b/fsfw/unittest/testcfg/devices/powerSwitcherList.h @@ -0,0 +1,12 @@ +#ifndef CONFIG_DEVICES_POWERSWITCHERLIST_H_ +#define CONFIG_DEVICES_POWERSWITCHERLIST_H_ + +namespace switches { + /* Switches are uint8_t datatype and go from 0 to 255 */ + enum switcherList { + }; + +} + + +#endif /* CONFIG_DEVICES_POWERSWITCHERLIST_H_ */ diff --git a/fsfw/unittest/testcfg/events/subsystemIdRanges.h b/fsfw/unittest/testcfg/events/subsystemIdRanges.h new file mode 100644 index 0000000..24eee81 --- /dev/null +++ b/fsfw/unittest/testcfg/events/subsystemIdRanges.h @@ -0,0 +1,18 @@ +#ifndef CONFIG_EVENTS_SUBSYSTEMIDRANGES_H_ +#define CONFIG_EVENTS_SUBSYSTEMIDRANGES_H_ + +#include +#include + +/** + * @brief Custom subsystem IDs can be added here + * @details + * Subsystem IDs are used to create unique events. + */ +namespace SUBSYSTEM_ID { +enum: uint8_t { + SUBSYSTEM_ID_START = FW_SUBSYSTEM_ID_RANGE, +}; +} + +#endif /* CONFIG_EVENTS_SUBSYSTEMIDRANGES_H_ */ diff --git a/fsfw/unittest/testcfg/ipc/MissionMessageTypes.cpp b/fsfw/unittest/testcfg/ipc/MissionMessageTypes.cpp new file mode 100644 index 0000000..d68cb58 --- /dev/null +++ b/fsfw/unittest/testcfg/ipc/MissionMessageTypes.cpp @@ -0,0 +1,12 @@ +#include "MissionMessageTypes.h" + +#include + +void messagetypes::clearMissionMessage(CommandMessage* message) { + switch(message->getMessageType()) { + default: + break; + } +} + + diff --git a/fsfw/unittest/testcfg/ipc/MissionMessageTypes.h b/fsfw/unittest/testcfg/ipc/MissionMessageTypes.h new file mode 100644 index 0000000..832d8e5 --- /dev/null +++ b/fsfw/unittest/testcfg/ipc/MissionMessageTypes.h @@ -0,0 +1,22 @@ +#ifndef CONFIG_IPC_MISSIONMESSAGETYPES_H_ +#define CONFIG_IPC_MISSIONMESSAGETYPES_H_ + +#include + +class CommandMessage; + +/** + * Custom command messages are specified here. + * Most messages needed to use FSFW are already located in + * + * @param message Generic Command Message + */ +namespace messagetypes{ +enum MESSAGE_TYPE { + MISSION_MESSAGE_TYPE_START = FW_MESSAGES_COUNT, +}; + +void clearMissionMessage(CommandMessage* message); +} + +#endif /* CONFIG_IPC_MISSIONMESSAGETYPES_H_ */ diff --git a/fsfw/unittest/testcfg/objects/Factory.cpp b/fsfw/unittest/testcfg/objects/Factory.cpp new file mode 100644 index 0000000..e05b794 --- /dev/null +++ b/fsfw/unittest/testcfg/objects/Factory.cpp @@ -0,0 +1,34 @@ +#include "Factory.h" + +#include +#include + +#include +#include + +/** + * @brief Produces system objects. + * @details + * Build tasks by using SystemObject Interface (Interface). + * Header files of all tasks must be included + * Please note that an object has to implement the system object interface + * if the interface validity is checked or retrieved later by using the + * get(object_id) function from the ObjectManagerIF. + * + * Framework objects are created first. + * + * @ingroup init + */ +void Factory::produce(void) { + setStaticFrameworkObjectIds(); + new EventManager(objects::EVENT_MANAGER); + new HealthTable(objects::HEALTH_TABLE); + new InternalErrorReporter(objects::INTERNAL_ERROR_REPORTER); + +} + +void Factory::setStaticFrameworkObjectIds() { + +} + + diff --git a/fsfw/unittest/testcfg/objects/Factory.h b/fsfw/unittest/testcfg/objects/Factory.h new file mode 100644 index 0000000..84f9207 --- /dev/null +++ b/fsfw/unittest/testcfg/objects/Factory.h @@ -0,0 +1,16 @@ +#ifndef FACTORY_H_ +#define FACTORY_H_ + +#include + +namespace Factory { + /** + * @brief Creates all SystemObject elements which are persistent + * during execution. + */ + void produce(); + void setStaticFrameworkObjectIds(); + +} + +#endif /* FACTORY_H_ */ diff --git a/fsfw/unittest/testcfg/objects/systemObjectList.h b/fsfw/unittest/testcfg/objects/systemObjectList.h new file mode 100644 index 0000000..0e034af --- /dev/null +++ b/fsfw/unittest/testcfg/objects/systemObjectList.h @@ -0,0 +1,16 @@ +#ifndef HOSTED_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ +#define HOSTED_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ + +#include +#include + +// The objects will be instantiated in the ID order +namespace objects { + enum sourceObjects: uint32_t { + /* All addresses between start and end are reserved for the FSFW */ + FSFW_CONFIG_RESERVED_START = PUS_SERVICE_1_VERIFICATION, + FSFW_CONFIG_RESERVED_END = TM_STORE + }; +} + +#endif /* BSP_CONFIG_OBJECTS_SYSTEMOBJECTLIST_H_ */ diff --git a/fsfw/unittest/testcfg/pollingsequence/PollingSequenceFactory.cpp b/fsfw/unittest/testcfg/pollingsequence/PollingSequenceFactory.cpp new file mode 100644 index 0000000..f836a74 --- /dev/null +++ b/fsfw/unittest/testcfg/pollingsequence/PollingSequenceFactory.cpp @@ -0,0 +1,23 @@ +#include "PollingSequenceFactory.h" + +#include +#include +#include + +ReturnValue_t pst::pollingSequenceInitDefault( + FixedTimeslotTaskIF *thisSequence) { + /* Length of a communication cycle */ + uint32_t length = thisSequence->getPeriodMs(); + + /* Add polling sequence table here */ + + if (thisSequence->checkSequence() == HasReturnvaluesIF::RETURN_OK) { + return HasReturnvaluesIF::RETURN_OK; + } + else { + sif::error << "pst::pollingSequenceInitDefault: Sequence invalid!" + << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } +} + diff --git a/fsfw/unittest/testcfg/pollingsequence/PollingSequenceFactory.h b/fsfw/unittest/testcfg/pollingsequence/PollingSequenceFactory.h new file mode 100644 index 0000000..c5d41b7 --- /dev/null +++ b/fsfw/unittest/testcfg/pollingsequence/PollingSequenceFactory.h @@ -0,0 +1,32 @@ +#ifndef POLLINGSEQUENCEFACTORY_H_ +#define POLLINGSEQUENCEFACTORY_H_ + +#include + +class FixedTimeslotTaskIF; + +/** + * All device handlers are scheduled by adding them into Polling Sequence Tables (PST) + * to satisfy stricter timing requirements of device communication, + * A device handler has four different communication steps: + * 1. DeviceHandlerIF::SEND_WRITE -> Send write via interface + * 2. DeviceHandlerIF::GET_WRITE -> Get confirmation for write + * 3. DeviceHandlerIF::SEND_READ -> Send read request + * 4. DeviceHandlerIF::GET_READ -> Read from interface + * The PST specifies precisely when the respective ComIF functions are called + * during the communication cycle time. + * The task is created using the FixedTimeslotTaskIF, + * which utilises the underlying Operating System Abstraction Layer (OSAL) + * + * @param thisSequence FixedTimeslotTaskIF * object is passed inside the Factory class when creating the PST + * @return + */ +namespace pst { + +/* Default PST */ +ReturnValue_t pollingSequenceInitDefault(FixedTimeslotTaskIF *thisSequence); + + +} + +#endif /* POLLINGSEQUENCEINIT_H_ */ diff --git a/fsfw/unittest/testcfg/returnvalues/classIds.h b/fsfw/unittest/testcfg/returnvalues/classIds.h new file mode 100644 index 0000000..606cc60 --- /dev/null +++ b/fsfw/unittest/testcfg/returnvalues/classIds.h @@ -0,0 +1,16 @@ +#ifndef CONFIG_RETURNVALUES_CLASSIDS_H_ +#define CONFIG_RETURNVALUES_CLASSIDS_H_ + +#include + +/** + * @brief CLASS_ID defintions which are required for custom returnvalues. + */ +namespace CLASS_ID { +enum { + MISSION_CLASS_ID_START = FW_CLASS_ID_COUNT, +}; +} + + +#endif /* CONFIG_RETURNVALUES_CLASSIDS_H_ */ diff --git a/fsfw/unittest/testcfg/testcfg.mk b/fsfw/unittest/testcfg/testcfg.mk new file mode 100644 index 0000000..64fa87f --- /dev/null +++ b/fsfw/unittest/testcfg/testcfg.mk @@ -0,0 +1,15 @@ +CXXSRC += $(wildcard $(CURRENTPATH)/cdatapool/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/ipc/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/objects/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/pollingsequence/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/events/*.cpp) + +INCLUDES += $(CURRENTPATH) +INCLUDES += $(CURRENTPATH)/objects +INCLUDES += $(CURRENTPATH)/ipc +INCLUDES += $(CURRENTPATH)/pollingsequence +INCLUDES += $(CURRENTPATH)/returnvalues +INCLUDES += $(CURRENTPATH)/tmtc +INCLUDES += $(CURRENTPATH)/events +INCLUDES += $(CURRENTPATH)/devices +INCLUDES += $(CURRENTPATH)/cdatapool diff --git a/fsfw/unittest/testcfg/tmtc/apid.h b/fsfw/unittest/testcfg/tmtc/apid.h new file mode 100644 index 0000000..c0231bc --- /dev/null +++ b/fsfw/unittest/testcfg/tmtc/apid.h @@ -0,0 +1,18 @@ +#ifndef CONFIG_TMTC_APID_H_ +#define CONFIG_TMTC_APID_H_ + +#include + +/** + * Application Process Definition: entity, uniquely identified by an + * application process ID (APID), capable of generating telemetry source + * packets and receiving telecommand packets. + * + * Chose APID(s) for mission and define it here. + */ +namespace apid { + static const uint16_t DEFAULT_APID = 0x00; +} + + +#endif /* CONFIG_TMTC_APID_H_ */ diff --git a/fsfw/unittest/testcfg/tmtc/pusIds.h b/fsfw/unittest/testcfg/tmtc/pusIds.h new file mode 100644 index 0000000..821a998 --- /dev/null +++ b/fsfw/unittest/testcfg/tmtc/pusIds.h @@ -0,0 +1,25 @@ +#ifndef CONFIG_TMTC_PUSIDS_HPP_ +#define CONFIG_TMTC_PUSIDS_HPP_ + +#include + +namespace pus { +enum Ids: uint8_t { + PUS_SERVICE_1 = 1, + PUS_SERVICE_2 = 2, + PUS_SERVICE_3 = 3, + PUS_SERVICE_5 = 5, + PUS_SERVICE_6 = 6, + PUS_SERVICE_8 = 8, + PUS_SERVICE_9 = 9, + PUS_SERVICE_11 = 11, + PUS_SERVICE_17 = 17, + PUS_SERVICE_19 = 19, + PUS_SERVICE_20 = 20, + PUS_SERVICE_23 = 23, + PUS_SERVICE_200 = 200, + PUS_SERVICE_201 = 201, +}; +}; + +#endif /* CONFIG_TMTC_PUSIDS_HPP_ */ diff --git a/fsfw/unittest/tests/action/TestActionHelper.cpp b/fsfw/unittest/tests/action/TestActionHelper.cpp new file mode 100644 index 0000000..d5b2e46 --- /dev/null +++ b/fsfw/unittest/tests/action/TestActionHelper.cpp @@ -0,0 +1,106 @@ +//#include "TestActionHelper.h" +//#include +//#include +//#include +//#include "../../core/CatchDefinitions.h" +// +// +//TEST_CASE( "Action Helper" , "[ActionHelper]") { +// ActionHelperOwnerMockBase testDhMock; +// MessageQueueMockBase testMqMock; +// ActionHelper actionHelper = ActionHelper( +// &testDhMock, dynamic_cast(&testMqMock)); +// CommandMessage actionMessage; +// ActionId_t testActionId = 777; +// std::array testParams {1, 2, 3}; +// store_address_t paramAddress; +// StorageManagerIF *ipcStore = tglob::getIpcStoreHandle(); +// ipcStore->addData(¶mAddress, testParams.data(), 3); +// REQUIRE(actionHelper.initialize() == retval::CATCH_OK); +// +// SECTION ("Simple tests") { +// ActionMessage::setCommand(&actionMessage, testActionId, paramAddress); +// CHECK(not testDhMock.executeActionCalled); +// REQUIRE(actionHelper.handleActionMessage(&actionMessage) == retval::CATCH_OK); +// CHECK(testDhMock.executeActionCalled); +// // No message is sent if everything is alright. +// CHECK(not testMqMock.wasMessageSent()); +// store_address_t invalidAddress; +// ActionMessage::setCommand(&actionMessage, testActionId, invalidAddress); +// actionHelper.handleActionMessage(&actionMessage); +// CHECK(testMqMock.wasMessageSent()); +// const uint8_t* ptr = nullptr; +// size_t size = 0; +// REQUIRE(ipcStore->getData(paramAddress, &ptr, &size) == static_cast(StorageManagerIF::DATA_DOES_NOT_EXIST)); +// REQUIRE(ptr == nullptr); +// REQUIRE(size == 0); +// testDhMock.getBuffer(&ptr, &size); +// REQUIRE(size == 3); +// for(uint8_t i = 0; i<3;i++){ +// REQUIRE(ptr[i] == (i+1)); +// } +// testDhMock.clearBuffer(); +// } +// +// SECTION("Handle failures"){ +// actionMessage.setCommand(1234); +// REQUIRE(actionHelper.handleActionMessage(&actionMessage) == static_cast(CommandMessage::UNKNOWN_COMMAND)); +// CHECK(not testMqMock.wasMessageSent()); +// uint16_t step = 5; +// ReturnValue_t status = 0x1234; +// actionHelper.step(step, testMqMock.getId(), testActionId, status); +// step += 1; +// CHECK(testMqMock.wasMessageSent()); +// CommandMessage testMessage; +// REQUIRE(testMqMock.receiveMessage(&testMessage) == static_cast(HasReturnvaluesIF::RETURN_OK)); +// REQUIRE(testMessage.getCommand() == static_cast(ActionMessage::STEP_FAILED)); +// REQUIRE(testMessage.getParameter() == static_cast(testActionId)); +// uint32_t parameter2 = ((uint32_t)step << 16) | (uint32_t)status; +// REQUIRE(testMessage.getParameter2() == parameter2); +// REQUIRE(ActionMessage::getStep(&testMessage) == step); +// } +// +// SECTION("Handle finish"){ +// CHECK(not testMqMock.wasMessageSent()); +// ReturnValue_t status = 0x9876; +// actionHelper.finish(testMqMock.getId(), testActionId, status); +// CHECK(testMqMock.wasMessageSent()); +// CommandMessage testMessage; +// REQUIRE(testMqMock.receiveMessage(&testMessage) == static_cast(HasReturnvaluesIF::RETURN_OK)); +// REQUIRE(testMessage.getCommand() == static_cast(ActionMessage::COMPLETION_FAILED)); +// REQUIRE(ActionMessage::getActionId(&testMessage) == testActionId); +// REQUIRE(ActionMessage::getReturnCode(&testMessage) == static_cast(status)); +// } +// +// SECTION("Handle failed"){ +// store_address_t toLongParamAddress = StorageManagerIF::INVALID_ADDRESS; +// std::array toLongData = {5, 4, 3, 2, 1}; +// REQUIRE(ipcStore->addData(&toLongParamAddress, toLongData.data(), 5) == retval::CATCH_OK); +// ActionMessage::setCommand(&actionMessage, testActionId, toLongParamAddress); +// CHECK(not testDhMock.executeActionCalled); +// REQUIRE(actionHelper.handleActionMessage(&actionMessage) == retval::CATCH_OK); +// REQUIRE(ipcStore->getData(toLongParamAddress).first == static_cast(StorageManagerIF::DATA_DOES_NOT_EXIST)); +// CommandMessage testMessage; +// REQUIRE(testMqMock.receiveMessage(&testMessage) == static_cast(HasReturnvaluesIF::RETURN_OK)); +// REQUIRE(testMessage.getCommand() == static_cast(ActionMessage::STEP_FAILED)); +// REQUIRE(ActionMessage::getReturnCode(&testMessage) == 0xAFFE); +// REQUIRE(ActionMessage::getStep(&testMessage) == 0); +// REQUIRE(ActionMessage::getActionId(&testMessage) == testActionId); +// } +// +// SECTION("Missing IPC Data"){ +// ActionMessage::setCommand(&actionMessage, testActionId, StorageManagerIF::INVALID_ADDRESS); +// CHECK(not testDhMock.executeActionCalled); +// REQUIRE(actionHelper.handleActionMessage(&actionMessage) == retval::CATCH_OK); +// CommandMessage testMessage; +// REQUIRE(testMqMock.receiveMessage(&testMessage) == static_cast(HasReturnvaluesIF::RETURN_OK)); +// REQUIRE(testMessage.getCommand() == static_cast(ActionMessage::STEP_FAILED)); +// REQUIRE(ActionMessage::getReturnCode(&testMessage) == static_cast(StorageManagerIF::ILLEGAL_STORAGE_ID)); +// REQUIRE(ActionMessage::getStep(&testMessage) == 0); +// } +// +// +// SECTION("Data Reply"){ +// +// } +//} diff --git a/fsfw/unittest/tests/action/TestActionHelper.h b/fsfw/unittest/tests/action/TestActionHelper.h new file mode 100644 index 0000000..9bc93d3 --- /dev/null +++ b/fsfw/unittest/tests/action/TestActionHelper.h @@ -0,0 +1,131 @@ +//#ifndef UNITTEST_HOSTED_TESTACTIONHELPER_H_ +//#define UNITTEST_HOSTED_TESTACTIONHELPER_H_ +// +//#include +//#include +//#include +//#include +// +// +//class ActionHelperOwnerMockBase: public HasActionsIF { +//public: +// bool getCommandQueueCalled = false; +// bool executeActionCalled = false; +// static const size_t MAX_SIZE = 3; +// uint8_t buffer[MAX_SIZE] = {0, 0, 0}; +// size_t size = 0; +// +// MessageQueueId_t getCommandQueue() const override { +// return tconst::testQueueId; +// } +// +// ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy, +// const uint8_t* data, size_t size) override { +// executeActionCalled = true; +// if(size > MAX_SIZE){ +// return 0xAFFE; +// } +// this->size = size; +// memcpy(buffer, data, size); +// return HasReturnvaluesIF::RETURN_OK; +// } +// +// void clearBuffer(){ +// this->size = 0; +// for(size_t i = 0; isize; +// } +// if(ptr != nullptr){ +// *ptr = buffer; +// } +// } +//}; +// +// +//class MessageQueueMockBase: public MessageQueueIF { +//public: +// MessageQueueId_t myQueueId = 0; +// bool defaultDestSet = false; +// bool messageSent = false; +// +// +// +// bool wasMessageSent() { +// bool tempMessageSent = messageSent; +// messageSent = false; +// return tempMessageSent; +// } +// +// virtual ReturnValue_t reply( MessageQueueMessage* message ) { +// messageSent = true; +// lastMessage = (*message); +// return HasReturnvaluesIF::RETURN_OK; +// }; +// virtual ReturnValue_t receiveMessage(MessageQueueMessage* message, +// MessageQueueId_t *receivedFrom) { +// (*message) = lastMessage; +// lastMessage.clear(); +// return HasReturnvaluesIF::RETURN_OK; +// } +// virtual ReturnValue_t receiveMessage(MessageQueueMessage* message) { +// (*message) = lastMessage; +// lastMessage.clear(); +// return HasReturnvaluesIF::RETURN_OK; +// } +// virtual ReturnValue_t flush(uint32_t* count) { +// return HasReturnvaluesIF::RETURN_OK; +// } +// virtual MessageQueueId_t getLastPartner() const { +// return tconst::testQueueId; +// } +// virtual MessageQueueId_t getId() const { +// return tconst::testQueueId; +// } +// virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo, +// MessageQueueMessage* message, MessageQueueId_t sentFrom, +// bool ignoreFault = false ) { +// messageSent = true; +// lastMessage = (*message); +// return HasReturnvaluesIF::RETURN_OK; +// } +// virtual ReturnValue_t sendMessage( MessageQueueId_t sendTo, +// MessageQueueMessage* message, bool ignoreFault = false ) override { +// messageSent = true; +// lastMessage = (*message); +// return HasReturnvaluesIF::RETURN_OK; +// } +// virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessage* message, +// MessageQueueId_t sentFrom, bool ignoreFault = false ) { +// messageSent = true; +// lastMessage = (*message); +// return HasReturnvaluesIF::RETURN_OK; +// } +// virtual ReturnValue_t sendToDefault( MessageQueueMessage* message ) { +// messageSent = true; +// lastMessage = (*message); +// return HasReturnvaluesIF::RETURN_OK; +// } +// virtual void setDefaultDestination(MessageQueueId_t defaultDestination) { +// myQueueId = defaultDestination; +// defaultDestSet = true; +// } +// +// virtual MessageQueueId_t getDefaultDestination() const { +// return myQueueId; +// } +// virtual bool isDefaultDestinationSet() const { +// return defaultDestSet; +// } +//private: +// MessageQueueMessage lastMessage; +// +//}; +// +// +//#endif /* UNITTEST_TESTFW_NEWTESTS_TESTACTIONHELPER_H_ */ diff --git a/fsfw/unittest/tests/container/RingBufferTest.cpp b/fsfw/unittest/tests/container/RingBufferTest.cpp new file mode 100644 index 0000000..8b82d40 --- /dev/null +++ b/fsfw/unittest/tests/container/RingBufferTest.cpp @@ -0,0 +1,327 @@ +#include +#include +#include "../../core/CatchDefinitions.h" + +#include + +TEST_CASE("Ring Buffer Test" , "[RingBufferTest]") { + uint8_t testData[13]= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + uint8_t readBuffer[10] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13}; + SimpleRingBuffer ringBuffer(10, false, 5); + + SECTION("Simple Test") { + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 9) == retval::CATCH_OK); + REQUIRE(ringBuffer.writeData(testData, 3) == retval::CATCH_FAILED); + REQUIRE(ringBuffer.readData(readBuffer, 5, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 5; i++) { + CHECK(readBuffer[i] == i); + } + REQUIRE(ringBuffer.availableWriteSpace() == 5); + ringBuffer.clear(); + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 4) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 4, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 4; i++) { + CHECK(readBuffer[i] == i); + } + REQUIRE(ringBuffer.writeData(testData, 9) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 9, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 9; i++) { + CHECK(readBuffer[i] == i); + } + + } + + SECTION("Get Free Element Test") { + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 8) == retval::CATCH_OK); + REQUIRE(ringBuffer.availableWriteSpace() == 1); + REQUIRE(ringBuffer.readData(readBuffer, 8, true) == retval::CATCH_OK); + REQUIRE(ringBuffer.availableWriteSpace() == 9); + uint8_t *testPtr = nullptr; + REQUIRE(ringBuffer.getFreeElement(&testPtr, 10) == retval::CATCH_FAILED); + + + REQUIRE(ringBuffer.writeTillWrap() == 2); + // too many excess bytes. + REQUIRE(ringBuffer.getFreeElement(&testPtr, 8) == retval::CATCH_FAILED); + REQUIRE(ringBuffer.getFreeElement(&testPtr, 5) == retval::CATCH_OK); + REQUIRE(ringBuffer.getExcessBytes() == 3); + std::memcpy(testPtr, testData, 5); + ringBuffer.confirmBytesWritten(5); + REQUIRE(ringBuffer.getAvailableReadData() == 5); + ringBuffer.readData(readBuffer, 5, true); + for(uint8_t i = 0; i< 5; i++) { + CHECK(readBuffer[i] == i); + } + } + + SECTION("Read Remaining Test") { + REQUIRE(ringBuffer.writeData(testData, 3) == retval::CATCH_OK); + REQUIRE(ringBuffer.getAvailableReadData() == 3); + REQUIRE(ringBuffer.readData(readBuffer, 5, false, false, nullptr) == retval::CATCH_FAILED); + size_t trueSize = 0; + REQUIRE(ringBuffer.readData(readBuffer, 5, false, true, &trueSize) == retval::CATCH_OK); + REQUIRE(trueSize == 3); + for(uint8_t i = 0; i< 3; i++) { + CHECK(readBuffer[i] == i); + } + trueSize = 0; + REQUIRE(ringBuffer.deleteData(5, false, &trueSize) == retval::CATCH_FAILED); + REQUIRE(trueSize == 0); + REQUIRE(ringBuffer.deleteData(5, true, &trueSize) == retval::CATCH_OK); + REQUIRE(trueSize == 3); + } +} + +TEST_CASE("Ring Buffer Test2" , "[RingBufferTest2]") { + uint8_t testData[13]= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + uint8_t readBuffer[10] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13}; + uint8_t* newBuffer = new uint8_t[10]; + SimpleRingBuffer ringBuffer(newBuffer, 10, true, 5); + + SECTION("Simple Test") { + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 9) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 5, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 5; i++) { + CHECK(readBuffer[i] == i); + } + REQUIRE(ringBuffer.availableWriteSpace() == 5); + ringBuffer.clear(); + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 4) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 4, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 4; i++) { + CHECK(readBuffer[i] == i); + } + REQUIRE(ringBuffer.writeData(testData, 9) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 9, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 9; i++) { + CHECK(readBuffer[i] == i); + } + + } + + SECTION("Get Free Element Test") { + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 8) == retval::CATCH_OK); + REQUIRE(ringBuffer.availableWriteSpace() == 1); + REQUIRE(ringBuffer.readData(readBuffer, 8, true) == retval::CATCH_OK); + REQUIRE(ringBuffer.availableWriteSpace() == 9); + uint8_t *testPtr = nullptr; + REQUIRE(ringBuffer.getFreeElement(&testPtr, 10) == retval::CATCH_FAILED); + + + REQUIRE(ringBuffer.writeTillWrap() == 2); + // too many excess bytes. + REQUIRE(ringBuffer.getFreeElement(&testPtr, 8) == retval::CATCH_FAILED); + REQUIRE(ringBuffer.getFreeElement(&testPtr, 5) == retval::CATCH_OK); + REQUIRE(ringBuffer.getExcessBytes() == 3); + std::memcpy(testPtr, testData, 5); + ringBuffer.confirmBytesWritten(5); + REQUIRE(ringBuffer.getAvailableReadData() == 5); + ringBuffer.readData(readBuffer, 5, true); + for(uint8_t i = 0; i< 5; i++) { + CHECK(readBuffer[i] == i); + } + } + + SECTION("Read Remaining Test") { + REQUIRE(ringBuffer.writeData(testData, 3) == retval::CATCH_OK); + REQUIRE(ringBuffer.getAvailableReadData() == 3); + REQUIRE(ringBuffer.readData(readBuffer, 5, false, false, nullptr) == retval::CATCH_FAILED); + size_t trueSize = 0; + REQUIRE(ringBuffer.readData(readBuffer, 5, false, true, &trueSize) == retval::CATCH_OK); + REQUIRE(trueSize == 3); + for(uint8_t i = 0; i< 3; i++) { + CHECK(readBuffer[i] == i); + } + trueSize = 0; + REQUIRE(ringBuffer.deleteData(5, false, &trueSize) == retval::CATCH_FAILED); + REQUIRE(trueSize == 0); + REQUIRE(ringBuffer.deleteData(5, true, &trueSize) == retval::CATCH_OK); + REQUIRE(trueSize == 3); + } + + SECTION("Overflow"){ + REQUIRE(ringBuffer.availableWriteSpace()==9); + //Writing more than the buffer is large, technically thats allowed + //But it is senseless and has undesired impact on read call + REQUIRE(ringBuffer.writeData(testData, 13) == retval::CATCH_OK); + REQUIRE(ringBuffer.getAvailableReadData()==3); + ringBuffer.clear(); + uint8_t * ptr = nullptr; + REQUIRE(ringBuffer.getFreeElement(&ptr, 13) == retval::CATCH_OK); + REQUIRE(ptr != nullptr); + memcpy(ptr, testData, 13); + ringBuffer.confirmBytesWritten(13); + REQUIRE(ringBuffer.getAvailableReadData()==3); + REQUIRE(ringBuffer.readData(readBuffer, 3, true)== retval::CATCH_OK); + for(auto i =0;i<3;i++){ + REQUIRE(readBuffer[i] == testData[i+10]); + } + } +} + +TEST_CASE("Ring Buffer Test3" , "[RingBufferTest3]") { + uint8_t testData[13]= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + uint8_t readBuffer[10] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13}; + uint8_t* newBuffer = new uint8_t[10]; + SimpleRingBuffer ringBuffer(newBuffer, 10, true, 15); + + SECTION("Simple Test") { + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 9) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 5, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 5; i++) { + CHECK(readBuffer[i] == i); + } + REQUIRE(ringBuffer.availableWriteSpace() == 5); + ringBuffer.clear(); + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 4) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 4, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 4; i++) { + CHECK(readBuffer[i] == i); + } + REQUIRE(ringBuffer.writeData(testData, 9) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 9, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 9; i++) { + CHECK(readBuffer[i] == i); + } + + } + + SECTION("Get Free Element Test") { + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 8) == retval::CATCH_OK); + REQUIRE(ringBuffer.availableWriteSpace() == 1); + REQUIRE(ringBuffer.readData(readBuffer, 8, true) == retval::CATCH_OK); + REQUIRE(ringBuffer.availableWriteSpace() == 9); + uint8_t *testPtr = nullptr; + REQUIRE(ringBuffer.getFreeElement(&testPtr, 10) == retval::CATCH_OK); + REQUIRE(ringBuffer.getExcessBytes() == 8); + + REQUIRE(ringBuffer.writeTillWrap() == 2); + // too many excess bytes. + REQUIRE(ringBuffer.getFreeElement(&testPtr, 8) == retval::CATCH_FAILED); + // Less Execss bytes overwrites before + REQUIRE(ringBuffer.getFreeElement(&testPtr, 3) == retval::CATCH_OK); + REQUIRE(ringBuffer.getExcessBytes() == 1); + std::memcpy(testPtr, testData, 3); + ringBuffer.confirmBytesWritten(3); + REQUIRE(ringBuffer.getAvailableReadData() == 3); + ringBuffer.readData(readBuffer, 3, true); + for(uint8_t i = 0; i< 3; i++) { + CHECK(readBuffer[i] == i); + } + } + + SECTION("Read Remaining Test") { + REQUIRE(ringBuffer.writeData(testData, 3) == retval::CATCH_OK); + REQUIRE(ringBuffer.getAvailableReadData() == 3); + REQUIRE(ringBuffer.readData(readBuffer, 5, false, false, nullptr) == retval::CATCH_FAILED); + size_t trueSize = 0; + REQUIRE(ringBuffer.readData(readBuffer, 5, false, true, &trueSize) == retval::CATCH_OK); + REQUIRE(trueSize == 3); + for(uint8_t i = 0; i< 3; i++) { + CHECK(readBuffer[i] == i); + } + trueSize = 0; + REQUIRE(ringBuffer.deleteData(5, false, &trueSize) == retval::CATCH_FAILED); + REQUIRE(trueSize == 0); + REQUIRE(ringBuffer.deleteData(5, true, &trueSize) == retval::CATCH_OK); + REQUIRE(trueSize == 3); + } + + SECTION("Overflow"){ + REQUIRE(ringBuffer.availableWriteSpace()==9); + //Writing more than the buffer is large, technically thats allowed + //But it is senseless and has undesired impact on read call + REQUIRE(ringBuffer.writeData(testData, 13) == retval::CATCH_OK); + REQUIRE(ringBuffer.getAvailableReadData()==3); + ringBuffer.clear(); + uint8_t * ptr = nullptr; + REQUIRE(ringBuffer.getFreeElement(&ptr, 13) == retval::CATCH_OK); + REQUIRE(ptr != nullptr); + memcpy(ptr, testData, 13); + ringBuffer.confirmBytesWritten(13); + REQUIRE(ringBuffer.getAvailableReadData()==3); + REQUIRE(ringBuffer.readData(readBuffer, 3, true)== retval::CATCH_OK); + for(auto i =0;i<3;i++){ + REQUIRE(readBuffer[i] == testData[i+10]); + } + } +} + +TEST_CASE("Ring Buffer Test4" , "[RingBufferTest4]") { + uint8_t testData[13]= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + uint8_t readBuffer[10] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13}; + SimpleRingBuffer ringBuffer(10, false, 15); + + SECTION("Simple Test") { + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 9) == retval::CATCH_OK); + REQUIRE(ringBuffer.writeData(testData, 3) == retval::CATCH_FAILED); + REQUIRE(ringBuffer.readData(readBuffer, 5, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 5; i++) { + CHECK(readBuffer[i] == i); + } + REQUIRE(ringBuffer.availableWriteSpace() == 5); + ringBuffer.clear(); + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 4) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 4, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 4; i++) { + CHECK(readBuffer[i] == i); + } + REQUIRE(ringBuffer.writeData(testData, 9) == retval::CATCH_OK); + REQUIRE(ringBuffer.readData(readBuffer, 9, true) == retval::CATCH_OK); + for(uint8_t i = 0; i< 9; i++) { + CHECK(readBuffer[i] == i); + } + + } + + SECTION("Get Free Element Test") { + REQUIRE(ringBuffer.availableWriteSpace() == 9); + REQUIRE(ringBuffer.writeData(testData, 8) == retval::CATCH_OK); + REQUIRE(ringBuffer.availableWriteSpace() == 1); + REQUIRE(ringBuffer.readData(readBuffer, 8, true) == retval::CATCH_OK); + REQUIRE(ringBuffer.availableWriteSpace() == 9); + uint8_t *testPtr = nullptr; + REQUIRE(ringBuffer.getFreeElement(&testPtr, 10) == retval::CATCH_FAILED); + + + REQUIRE(ringBuffer.writeTillWrap() == 2); + REQUIRE(ringBuffer.getFreeElement(&testPtr, 8) == retval::CATCH_OK); + REQUIRE(ringBuffer.getFreeElement(&testPtr, 5) == retval::CATCH_OK); + REQUIRE(ringBuffer.getExcessBytes() == 3); + std::memcpy(testPtr, testData, 5); + ringBuffer.confirmBytesWritten(5); + REQUIRE(ringBuffer.getAvailableReadData() == 5); + ringBuffer.readData(readBuffer, 5, true); + for(uint8_t i = 0; i< 5; i++) { + CHECK(readBuffer[i] == i); + } + } + + SECTION("Read Remaining Test") { + REQUIRE(ringBuffer.writeData(testData, 3) == retval::CATCH_OK); + REQUIRE(ringBuffer.getAvailableReadData() == 3); + REQUIRE(ringBuffer.readData(readBuffer, 5, false, false, nullptr) == retval::CATCH_FAILED); + size_t trueSize = 0; + REQUIRE(ringBuffer.readData(readBuffer, 5, false, true, &trueSize) == retval::CATCH_OK); + REQUIRE(trueSize == 3); + for(uint8_t i = 0; i< 3; i++) { + CHECK(readBuffer[i] == i); + } + trueSize = 0; + REQUIRE(ringBuffer.deleteData(5, false, &trueSize) == retval::CATCH_FAILED); + REQUIRE(trueSize == 0); + REQUIRE(ringBuffer.deleteData(5, true, &trueSize) == retval::CATCH_OK); + REQUIRE(trueSize == 3); + } +} diff --git a/fsfw/unittest/tests/container/TestArrayList.cpp b/fsfw/unittest/tests/container/TestArrayList.cpp new file mode 100644 index 0000000..914188c --- /dev/null +++ b/fsfw/unittest/tests/container/TestArrayList.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include "../../core/CatchDefinitions.h" + +/** + * @brief Array List test + */ +TEST_CASE("Array List" , "[ArrayListTest]") { + //perform set-up here + ArrayList list(20); + struct TestClass{ + public: + TestClass(){}; + TestClass(uint32_t number1, uint64_t number2): + number1(number1), number2(number2){}; + uint32_t number1 = -1; + uint64_t number2 = -1; + bool operator==(const TestClass& other){ + return ((this->number1 == other.number1) and (this->number2 == other.number2)); + }; + }; + ArrayList complexList(20); + SECTION("SimpleTest") { + REQUIRE(list.maxSize()==20); + REQUIRE(list.size == 0); + REQUIRE(list.insert(10) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(list[0] == 10); + REQUIRE(list.front() != nullptr); + REQUIRE((*list.front()) == 10); + REQUIRE(list.back() != nullptr); + REQUIRE((*list.back()) == 10); + // Need to test the const version of back as well + const uint16_t* number = const_cast*>(&list)->back(); + REQUIRE(*number == 10); + list.clear(); + REQUIRE(list.size == 0); + } + SECTION("Fill and check"){ + //This is an invalid element but its not a nullptr + REQUIRE(list.back() != nullptr); + for (auto i =0; i < 20; i++){ + REQUIRE(list.insert(i) == static_cast(HasReturnvaluesIF::RETURN_OK)); + } + REQUIRE(list.insert(20) == static_cast(ArrayList::FULL)); + ArrayList::Iterator it = list.begin(); + REQUIRE((*it) == 0); + it++; + REQUIRE((*it) == 1); + it--; + REQUIRE((*it) == 0); + it++; + for(auto it2 = list.begin(); it2!=list.end(); it2++){ + if (it == it2){ + REQUIRE((*it) == (*it2)); + break; + }else{ + REQUIRE((*it2) == 0); + REQUIRE(it2 != it); + } + } + } + SECTION("Const Iterator"){ + ArrayList::Iterator it = list.begin(); + for (auto i =0; i < 10; i++){ + REQUIRE(list.insert(i) == static_cast(HasReturnvaluesIF::RETURN_OK)); + } + it++; + const uint16_t* number = it.value; + REQUIRE(*number == 1); + } + + SECTION("Const Iterator"){ + ArrayList::Iterator it = complexList.begin(); + for (auto i =0; i < 10; i++){ + REQUIRE(complexList.insert(TestClass(i, i+1)) == static_cast(HasReturnvaluesIF::RETURN_OK)); + } + it++; + const TestClass* secondTest = it.value; + bool compare = TestClass(1, 2) == *secondTest; + REQUIRE(compare); + it++; + REQUIRE(it->number1 == 2); + REQUIRE(it->number2 == 3); + const ArrayList::Iterator it4(&(complexList[2])); + REQUIRE(it4->number1 == 2); + REQUIRE((*it4).number2 == 3); + REQUIRE(complexList.remaining()==10); + } +} diff --git a/fsfw/unittest/tests/container/TestDynamicFifo.cpp b/fsfw/unittest/tests/container/TestDynamicFifo.cpp new file mode 100644 index 0000000..6c9b741 --- /dev/null +++ b/fsfw/unittest/tests/container/TestDynamicFifo.cpp @@ -0,0 +1,149 @@ + +#include +#include +#include + +#include +#include + +TEST_CASE( "Dynamic Fifo Tests", "[TestDynamicFifo]") { + INFO("Dynamic Fifo Tests"); + struct Test{ + uint64_t number1; + uint32_t number2; + uint8_t number3; + bool operator==(struct Test& other){ + if ((other.number1 == this->number1) and + (other.number1 == this->number1) and + (other.number1 == this->number1)){ + return true; + } + return false; + } + }; + DynamicFIFO fifo(3); + std::vector list; + + struct Test structOne({UINT64_MAX, UINT32_MAX, UINT8_MAX}); + struct Test structTwo({0, 1, 2}); + struct Test structThree({42, 43, 44}); + list.push_back(structThree); + list.push_back(structTwo); + list.push_back(structOne); + SECTION("Insert, retrieval test"){ + REQUIRE(fifo.getMaxCapacity()==3); + REQUIRE(fifo.size()==0); + REQUIRE(fifo.empty()); + REQUIRE(not fifo.full()); + + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structThree)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(FIFOBase::FULL)); + + struct Test testptr; + REQUIRE(fifo.peek(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + bool equal = testptr == structOne; + REQUIRE(equal); + REQUIRE(fifo.size()==3); + REQUIRE(fifo.full()); + REQUIRE(not fifo.empty()); + + for(size_t i=2;i<3;i--){ + testptr.number1 = 0; + testptr.number2 = 0; + testptr.number3 = 0; + REQUIRE(fifo.retrieve(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + equal = testptr == list[i]; + REQUIRE(equal); + REQUIRE(fifo.size()==i); + } + testptr.number1 = 0; + testptr.number2 = 0; + testptr.number3 = 0; + REQUIRE(fifo.retrieve(&testptr)==static_cast(FIFOBase::EMPTY)); + REQUIRE(fifo.peek(&testptr)==static_cast(FIFOBase::EMPTY)); + REQUIRE(not fifo.full()); + REQUIRE(fifo.empty()); + REQUIRE(fifo.pop()==static_cast(FIFOBase::EMPTY)); + + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==1); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==2); + REQUIRE(fifo.pop()==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==1); + testptr.number1 = 0; + testptr.number2 = 0; + testptr.number3 = 0; + REQUIRE(fifo.peek(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + equal = testptr == structTwo; + REQUIRE(equal); + REQUIRE(fifo.pop()==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==0); + REQUIRE(fifo.empty()); + //struct Test* ptr = nullptr; + //REQUIRE(fifo.retrieve(ptr) == static_cast(HasReturnvaluesIF::RETURN_FAILED)); + //REQUIRE(fifo.peek(ptr) == static_cast(HasReturnvaluesIF::RETURN_FAILED)); + }; + SECTION("Copy Test"){ + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structThree)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==3); + REQUIRE(fifo.full()); + REQUIRE(not fifo.empty()); + + DynamicFIFO fifo2(fifo); + REQUIRE(fifo2.size()==3); + REQUIRE(fifo2.full()); + REQUIRE(not fifo2.empty()); + + }; + + SECTION("Assignment Test"){ + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structThree)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==3); + REQUIRE(fifo.full()); + REQUIRE(not fifo.empty()); + + DynamicFIFO fifo2(6); + fifo2 = fifo; + REQUIRE(fifo2.size()==3); + REQUIRE(fifo2.full()); + REQUIRE(not fifo2.empty()); + for(size_t i=2;i<3;i--){ + struct Test testptr = {0, 0, 0}; + REQUIRE(fifo2.retrieve(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + bool equal = testptr == list[i]; + REQUIRE(equal); + REQUIRE(fifo2.size()==i); + } + + }; + + SECTION("Assignment Test Smaller"){ + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structThree)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==3); + REQUIRE(fifo.full()); + REQUIRE(not fifo.empty()); + + DynamicFIFO fifo2(2); + fifo2 = fifo; + REQUIRE(fifo2.size()==3); + REQUIRE(fifo2.full()); + REQUIRE(not fifo2.empty()); + for(size_t i=2;i<3;i--){ + struct Test testptr = {0, 0, 0}; + REQUIRE(fifo2.retrieve(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + bool equal = testptr == list[i]; + REQUIRE(equal); + REQUIRE(fifo2.size()==i); + } + }; + +}; diff --git a/fsfw/unittest/tests/container/TestFifo.cpp b/fsfw/unittest/tests/container/TestFifo.cpp new file mode 100644 index 0000000..3775f42 --- /dev/null +++ b/fsfw/unittest/tests/container/TestFifo.cpp @@ -0,0 +1,138 @@ + +#include +#include +#include + +#include +#include "../../core/CatchDefinitions.h" + +TEST_CASE( "Static Fifo Tests", "[TestFifo]") { + INFO("Fifo Tests"); + struct Test{ + uint64_t number1; + uint32_t number2; + uint8_t number3; + bool operator==(struct Test& other){ + if ((other.number1 == this->number1) and + (other.number1 == this->number1) and + (other.number1 == this->number1)){ + return true; + } + return false; + } + }; + FIFO fifo; + std::vector list; + + struct Test structOne({UINT64_MAX, UINT32_MAX, UINT8_MAX}); + struct Test structTwo({0, 1, 2}); + struct Test structThree({42, 43, 44}); + list.push_back(structThree); + list.push_back(structTwo); + list.push_back(structOne); + SECTION("Insert, retrieval test"){ + REQUIRE(fifo.getMaxCapacity()==3); + REQUIRE(fifo.size()==0); + REQUIRE(fifo.empty()); + REQUIRE(not fifo.full()); + + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structThree)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(FIFOBase::FULL)); + + struct Test testptr; + REQUIRE(fifo.peek(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + bool equal = testptr == structOne; + REQUIRE(equal); + REQUIRE(fifo.size()==3); + REQUIRE(fifo.full()); + REQUIRE(not fifo.empty()); + + for(size_t i=2;i<3;i--){ + testptr.number1 = 0; + testptr.number2 = 0; + testptr.number3 = 0; + REQUIRE(fifo.retrieve(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + equal = testptr == list[i]; + REQUIRE(equal); + REQUIRE(fifo.size()==i); + } + testptr.number1 = 0; + testptr.number2 = 0; + testptr.number3 = 0; + REQUIRE(fifo.retrieve(&testptr)==static_cast(FIFOBase::EMPTY)); + REQUIRE(fifo.peek(&testptr)==static_cast(FIFOBase::EMPTY)); + REQUIRE(not fifo.full()); + REQUIRE(fifo.empty()); + REQUIRE(fifo.pop()==static_cast(FIFOBase::EMPTY)); + + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==1); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==2); + REQUIRE(fifo.pop()==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==1); + testptr.number1 = 0; + testptr.number2 = 0; + testptr.number3 = 0; + + // Test that retrieve and peek will not cause a nullptr dereference + struct Test* ptr = nullptr; + REQUIRE(fifo.retrieve(ptr) == static_cast(HasReturnvaluesIF::RETURN_FAILED)); + REQUIRE(fifo.peek(ptr) == static_cast(HasReturnvaluesIF::RETURN_FAILED)); + + + REQUIRE(fifo.peek(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + equal = testptr == structTwo; + REQUIRE(equal); + REQUIRE(fifo.pop()==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==0); + REQUIRE(fifo.empty()); + + + }; + SECTION("Copy Test"){ + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structThree)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==3); + REQUIRE(fifo.full()); + REQUIRE(not fifo.empty()); + + FIFO fifo2(fifo); + REQUIRE(fifo2.size()==3); + REQUIRE(fifo2.full()); + REQUIRE(not fifo2.empty()); + for(size_t i=2;i<3;i--){ + struct Test testptr = {0, 0, 0}; + REQUIRE(fifo2.retrieve(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + bool equal = testptr == list[i]; + REQUIRE(equal); + REQUIRE(fifo2.size()==i); + } + + }; + + SECTION("Assignment Test"){ + REQUIRE(fifo.insert(structOne)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structTwo)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.insert(structThree)==static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(fifo.size()==3); + REQUIRE(fifo.full()); + REQUIRE(not fifo.empty()); + + FIFO fifo2; + fifo2 = fifo; + REQUIRE(fifo2.size()==3); + REQUIRE(fifo2.full()); + REQUIRE(not fifo2.empty()); + for(size_t i=2;i<3;i--){ + struct Test testptr = {0, 0, 0}; + REQUIRE(fifo2.retrieve(&testptr)==static_cast(HasReturnvaluesIF::RETURN_OK)); + bool equal = testptr == list[i]; + REQUIRE(equal); + REQUIRE(fifo2.size()==i); + } + }; +}; diff --git a/fsfw/unittest/tests/container/TestFixedArrayList.cpp b/fsfw/unittest/tests/container/TestFixedArrayList.cpp new file mode 100644 index 0000000..737932e --- /dev/null +++ b/fsfw/unittest/tests/container/TestFixedArrayList.cpp @@ -0,0 +1,42 @@ +#include "../../core/CatchDefinitions.h" + +#include +#include + +#include + + +TEST_CASE( "FixedArrayList Tests", "[TestFixedArrayList]") { + INFO("FixedArrayList Tests"); + using testList = FixedArrayList; + testList list; + REQUIRE(list.size==0); + REQUIRE(list.insert(10) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(list.size==1); + REQUIRE(list.maxSize()==260); + SECTION("Copy Constructor"){ + testList list2(list); + REQUIRE(list2.size==1); + REQUIRE(list2[0] == 10); + REQUIRE(list.maxSize()==260); + }; + SECTION("Assignment copy"){ + testList list2; + REQUIRE(list2.size==0); + list2 = list; + REQUIRE(list2.size==1); + REQUIRE(list2[0] == 10); + REQUIRE(list.maxSize()==260); + }; + SECTION("Fill"){ + for(auto i=1;i<260;i++){ + REQUIRE(list.insert(i) == static_cast(HasReturnvaluesIF::RETURN_OK)); + } + REQUIRE(list.insert(260) == static_cast(ArrayList::FULL)); + list.clear(); + REQUIRE(list.size == 0); + } +} + + + diff --git a/fsfw/unittest/tests/container/TestFixedMap.cpp b/fsfw/unittest/tests/container/TestFixedMap.cpp new file mode 100644 index 0000000..079062f --- /dev/null +++ b/fsfw/unittest/tests/container/TestFixedMap.cpp @@ -0,0 +1,172 @@ +#include +#include + +#include +#include "../../core/CatchDefinitions.h" + +template class FixedMap; + +TEST_CASE( "FixedMap Tests", "[TestFixedMap]") { + INFO("FixedMap Tests"); + + FixedMap map(30); + REQUIRE(map.size() == 0); + REQUIRE(map.maxSize() == 30); + REQUIRE(map.getSerializedSize() == sizeof(uint32_t)); + REQUIRE(map.empty()); + REQUIRE(not map.full()); + + SECTION("Fill and erase"){ + for (uint16_t i=0;i<30;i++){ + REQUIRE(map.insert(std::make_pair(i, i+1))== static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.exists(i) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.find(i)->second==i+1); + REQUIRE(not map.empty()); + } + REQUIRE(map.insert(0, 0) == static_cast(FixedMap::KEY_ALREADY_EXISTS)); + REQUIRE(map.insert(31, 0) == static_cast(FixedMap::MAP_FULL)); + REQUIRE(map.exists(31) == static_cast(FixedMap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.size() == 30); + REQUIRE(map.full()); + { + uint16_t* ptr; + REQUIRE(map.find(5,&ptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(*ptr == 6); + REQUIRE(*(map.findValue(6)) == 7); + REQUIRE(map.find(31,&ptr) == static_cast(FixedMap::KEY_DOES_NOT_EXIST)); + } + + REQUIRE(map.getSerializedSize() == (sizeof(uint32_t)+ 30*(sizeof(uint32_t) + sizeof(uint16_t)))); + REQUIRE(map.erase(2) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.erase(31) == static_cast(FixedMap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.exists(2) == static_cast(FixedMap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.size() == 29); + + for (auto element: map){ + if (element.first == 5){ + REQUIRE(element.second == 6); + } + } + + for (FixedMap::Iterator it = map.begin(); it != map.end(); it++){ + REQUIRE(it->second == it->first + 1); + REQUIRE((*it).second == (*it).first + 1); + it->second = it->second + 1; + REQUIRE(it->second == it->first + 2); + } + + for (FixedMap::Iterator it = map.begin(); it != map.end(); it++){ + REQUIRE(map.erase(&it) == static_cast(HasReturnvaluesIF::RETURN_OK)); + } + + REQUIRE(map.size() == 0); + + for (FixedMap::Iterator it = map.begin(); it != map.end(); it++){ + // This line should never executed if begin and end is correct + FAIL("Should never be reached, Iterators invalid"); + } + }; + + SECTION("Insert variants"){ + FixedMap::Iterator it = map.end(); + REQUIRE(map.insert(36, 37, &it) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(it->first == 36); + REQUIRE(it->second == 37); + REQUIRE(map.size() == 1); + REQUIRE(map.insert(37, 38, nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.find(37)->second == 38); + REQUIRE(map.size() == 2); + REQUIRE(map.insert(37, 24, nullptr) == static_cast(FixedMap::KEY_ALREADY_EXISTS)); + REQUIRE(map.find(37)->second != 24); + REQUIRE(map.size() == 2); + }; + SECTION("Serialize and DeSerialize") { + REQUIRE(map.insert(36, 37, nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.insert(37, 38, nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + uint8_t buffer[sizeof(uint32_t) + + 2 * (sizeof(uint32_t) + sizeof(uint16_t))]; + REQUIRE( + map.getSerializedSize() + == (sizeof(uint32_t) + + 2 * (sizeof(uint32_t) + sizeof(uint16_t)))); + uint8_t *loc_ptr = buffer; + size_t size = 0; + REQUIRE( + map.serialize(&loc_ptr, &size, 10, SerializeIF::Endianness::BIG) + == static_cast(SerializeIF::BUFFER_TOO_SHORT)); + loc_ptr = buffer; + size = 0; + REQUIRE( + map.serialize(&loc_ptr, &size, + sizeof(uint32_t) + + 2 * (sizeof(uint32_t) + sizeof(uint16_t)), + SerializeIF::Endianness::BIG) + == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(size == 16); + + uint32_t internal_size = 0; + const uint8_t *ptr2 = buffer; + REQUIRE( + SerializeAdapter::deSerialize(&internal_size, &ptr2, &size, + SerializeIF::Endianness::BIG) + == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(internal_size == 2); + for (uint8_t i = 36; i < 38; i++) { + uint32_t first_element = 0; + REQUIRE( + SerializeAdapter::deSerialize(&first_element, &ptr2, &size, + SerializeIF::Endianness::BIG) + == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(first_element == i); + uint16_t second_element = 0; + REQUIRE( + SerializeAdapter::deSerialize(&second_element, &ptr2, &size, + SerializeIF::Endianness::BIG) + == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(second_element == i + 1); + } + REQUIRE(size == 0); + map.clear(); + const uint8_t* constPtr = buffer; + size = 16; + REQUIRE(map.size() == 0); + REQUIRE(map.deSerialize(&constPtr, &size, + SerializeIF::Endianness::BIG) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.size() == 2); + REQUIRE(map.find(36)->second == 37); + for(auto& element: map){ + REQUIRE((element.first+1) == element.second); + } + }; + + + SECTION("Failed erase and deSerialize"){ + FixedMap::Iterator it; + std::pair pair = std::make_pair(44, 43); + it = FixedMap::Iterator(&pair); + REQUIRE(map.erase(&it) == static_cast(FixedMap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.find(45) == map.end()); + size_t toLargeMap = 100; + const uint8_t* ptr = reinterpret_cast(&toLargeMap); + size_t size = sizeof(size_t); + REQUIRE(map.deSerialize(&ptr, &size, SerializeIF::Endianness::BIG) == + static_cast(SerializeIF::TOO_MANY_ELEMENTS)); + }; + SECTION("Little Endianess"){ + map.clear(); + map.insert(10,20, nullptr); + uint8_t newBuffer[sizeof(uint32_t)+ 1*(sizeof(uint32_t) + sizeof(uint16_t))]; + uint8_t* ptr = newBuffer; + size_t size = 0; + size_t max_size = sizeof(uint32_t)+ 1*(sizeof(uint32_t) + sizeof(uint16_t)); + REQUIRE(map.serialize(&ptr, &size, max_size, + SerializeIF::Endianness::LITTLE) == static_cast(HasReturnvaluesIF::RETURN_OK)); + map.clear(); + REQUIRE(map.size()==0); + const uint8_t* ptr2 = newBuffer; + REQUIRE(map.deSerialize(&ptr2, &size, + SerializeIF::Endianness::LITTLE) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.size()==1); + REQUIRE(map.find(10)->second == 20); + }; +} diff --git a/fsfw/unittest/tests/container/TestFixedOrderedMultimap.cpp b/fsfw/unittest/tests/container/TestFixedOrderedMultimap.cpp new file mode 100644 index 0000000..95194cb --- /dev/null +++ b/fsfw/unittest/tests/container/TestFixedOrderedMultimap.cpp @@ -0,0 +1,203 @@ +#include +#include + +#include +#include "../../core/CatchDefinitions.h" + +TEST_CASE( "FixedOrderedMultimap Tests", "[TestFixedOrderedMultimap]") { + INFO("FixedOrderedMultimap Tests"); + + FixedOrderedMultimap map(30); + REQUIRE(map.size() == 0); + REQUIRE(map.maxSize() == 30); + + SECTION("Test insert, find, exists"){ + for (uint16_t i=0;i<30;i++){ + REQUIRE(map.insert(std::make_pair(i, i+1))== static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.exists(i) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.find(i)->second==i+1); + } + REQUIRE(map.insert(0, 0) == static_cast(FixedOrderedMultimap::MAP_FULL)); + REQUIRE(map.exists(31) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.size() == 30); + { + uint16_t* ptr; + REQUIRE(map.find(5,&ptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(*ptr == 6); + REQUIRE(map.find(31,&ptr) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + } + REQUIRE(map.erase(2) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.erase(31) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.exists(2) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.size() == 29); + + for (auto element: map){ + if (element.first == 5){ + REQUIRE(element.second == 6); + } + } + + for (FixedOrderedMultimap::Iterator it = map.begin(); it != map.end(); it++){ + REQUIRE(it->second == it->first + 1); + REQUIRE((*it).second == (*it).first + 1); + it->second = it->second + 1; + REQUIRE(it->second == it->first + 2); + } + + { + FixedOrderedMultimap::Iterator it = map.begin(); + while(it != map.end()){ + REQUIRE(map.erase(&it) == static_cast(HasReturnvaluesIF::RETURN_OK)); + } + REQUIRE(map.size() == 0); + } + + for (FixedOrderedMultimap::Iterator it = map.begin(); it != map.end(); it++){ + // This line should never executed if begin and end is correct + FAIL("Should never be reached, Iterators invalid"); + } + }; + + SECTION("Test different insert variants") + { + FixedOrderedMultimap::Iterator it = map.end(); + REQUIRE(map.insert(36, 37, &it) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(it->first == 36); + REQUIRE(it->second == 37); + REQUIRE(map.size() == 1); + REQUIRE(map.insert(37, 38, nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.find(37)->second == 38); + REQUIRE(map.size() == 2); + REQUIRE(map.insert(37, 24, nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.find(37)->second == 38); + REQUIRE(map.insert(0, 1, nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.find(0)->second == 1); + REQUIRE(map.size() == 4); + map.clear(); + REQUIRE(map.size() == 0); + } + SECTION("Test different erase and find with no entries"){ + FixedOrderedMultimap::Iterator it; + it = map.end(); + REQUIRE(map.erase(&it) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.find(1)== map.end()); + } +} + +TEST_CASE( "FixedOrderedMultimap Non Trivial Type", "[TestFixedOrderedMultimapNonTrivial]") { + INFO("FixedOrderedMultimap Non Trivial Type"); + + class TestClass{ + public: + TestClass(){}; + TestClass(uint32_t number1, uint64_t number2): + number1(number1),number2(number2){}; + ~TestClass(){}; + + bool operator==(const TestClass& lhs){ + return ((this->number1 == lhs.number1) and (this->number2 == lhs.number2)); + } + bool operator!=(const TestClass& lhs){ + return not(this->operator ==(lhs)); + } + + TestClass(const TestClass& other){ + this->number1 = other.number1; + this->number2 = other.number2; + }; + TestClass& operator=(const TestClass& other){ + this->number1 = other.number1; + this->number2 = other.number2; + return *this; + }; + + private: + uint32_t number1 = 0; + uint64_t number2 = 5; + }; + FixedOrderedMultimap map(30); + REQUIRE(map.size() == 0); + REQUIRE(map.maxSize() == 30); + + SECTION("Test insert, find, exists"){ + for (uint16_t i=0;i<30;i++){ + REQUIRE(map.insert(std::make_pair(i, TestClass(i+1,i)))== static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.exists(i) == static_cast(HasReturnvaluesIF::RETURN_OK)); + bool compare = map.find(i)->second == TestClass(i+1,i); + REQUIRE(compare); + } + REQUIRE(map.insert(0, TestClass()) == static_cast(FixedOrderedMultimap::MAP_FULL)); + REQUIRE(map.exists(31) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.size() == 30); + { + TestClass* ptr = nullptr; + REQUIRE(map.find(5,&ptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + bool compare = *ptr == TestClass(6, 5); + REQUIRE(compare); + REQUIRE(map.find(31,&ptr) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + } + REQUIRE(map.erase(2) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(map.erase(31) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.exists(2) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.size() == 29); + + for (auto element: map){ + if (element.first == 5){ + bool compare = element.second == TestClass(6, 5); + REQUIRE(compare); + } + } + + for (FixedOrderedMultimap::Iterator it = map.begin(); it != map.end(); it++){ + bool compare = it->second == TestClass(it->first + 1, it->first); + REQUIRE(compare); + compare = (*it).second == TestClass((*it).first + 1, (*it).first); + REQUIRE(compare); + it->second = TestClass(it->first + 2, it->first); + compare = it->second == TestClass(it->first + 2, it->first); + REQUIRE(compare); + } + + { + FixedOrderedMultimap::Iterator it = map.begin(); + while(it != map.end()){ + REQUIRE(map.erase(&it) == static_cast(HasReturnvaluesIF::RETURN_OK)); + } + REQUIRE(map.size() == 0); + } + + for (FixedOrderedMultimap::Iterator it = map.begin(); it != map.end(); it++){ + // This line should never executed if begin and end is correct + FAIL("Should never be reached, Iterators invalid"); + } + }; + + SECTION("Test different insert variants") + { + FixedOrderedMultimap::Iterator it = map.end(); + REQUIRE(map.insert(36, TestClass(37, 36), &it) == static_cast(HasReturnvaluesIF::RETURN_OK)); + REQUIRE(it->first == 36); + bool compare = it->second == TestClass(37, 36); + REQUIRE(compare); + REQUIRE(map.size() == 1); + REQUIRE(map.insert(37, TestClass(38, 37), nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + compare = map.find(37)->second == TestClass(38, 37); + REQUIRE(compare); + REQUIRE(map.size() == 2); + REQUIRE(map.insert(37, TestClass(24, 37), nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + compare = map.find(37)->second == TestClass(38, 37); + REQUIRE(compare); + REQUIRE(map.insert(0, TestClass(1, 0), nullptr) == static_cast(HasReturnvaluesIF::RETURN_OK)); + compare = map.find(0)->second == TestClass(1, 0); + REQUIRE(compare); + REQUIRE(map.size() == 4); + map.clear(); + REQUIRE(map.size() == 0); + } + SECTION("Test different erase and find with no entries"){ + FixedOrderedMultimap::Iterator it; + it = map.end(); + REQUIRE(map.erase(&it) == static_cast(FixedOrderedMultimap::KEY_DOES_NOT_EXIST)); + REQUIRE(map.find(1)== map.end()); + } +} diff --git a/fsfw/unittest/tests/container/TestPlacementFactory.cpp b/fsfw/unittest/tests/container/TestPlacementFactory.cpp new file mode 100644 index 0000000..5edbb9d --- /dev/null +++ b/fsfw/unittest/tests/container/TestPlacementFactory.cpp @@ -0,0 +1,45 @@ +//#include +//#include +//#include +//#include +// +//#include +//#include "../../core/CatchDefinitions.h" +// +//TEST_CASE( "PlacementFactory Tests", "[TestPlacementFactory]") { +// INFO("PlacementFactory Tests"); +// +// const uint16_t element_sizes[3] = {sizeof(uint16_t), sizeof(uint32_t), sizeof(uint64_t)}; +// const uint16_t n_elements[3] = {1, 1, 1}; +// LocalPool<3> storagePool(0x1, element_sizes, n_elements, false, true); +// PlacementFactory factory(&storagePool); +// +// SECTION("Pool overload"){ +// store_address_t address; +// uint8_t* ptr = nullptr; +// REQUIRE(storagePool.getFreeElement(&address, sizeof(ArrayList), &ptr) +// == static_cast(StorageManagerIF::DATA_TOO_LARGE)); +// ArrayList* list2 = factory.generate >(80); +// REQUIRE(list2 == nullptr); +// } +// +// SECTION("Test generate and destroy"){ +// uint64_t* number = factory.generate(32000); +// REQUIRE(number != nullptr); +// REQUIRE(*number == 32000); +// store_address_t address; +// uint8_t* ptr = nullptr; +// REQUIRE(storagePool.getFreeElement(&address, sizeof(uint64_t), &ptr) +// == static_cast(StorageManagerIF::DATA_TOO_LARGE)); +// uint64_t* number2 = factory.generate(12345); +// REQUIRE(number2 == nullptr); +// REQUIRE(factory.destroy(number) == static_cast(HasReturnvaluesIF::RETURN_OK)); +// REQUIRE(storagePool.getFreeElement(&address, sizeof(uint64_t), &ptr) +// == static_cast(HasReturnvaluesIF::RETURN_OK)); +// REQUIRE(storagePool.deleteData(address) == static_cast(HasReturnvaluesIF::RETURN_OK)); +// +// //Check that PlacementFactory checks for nullptr +// ptr = nullptr; +// REQUIRE(factory.destroy(ptr) == static_cast(HasReturnvaluesIF::RETURN_FAILED)); +// } +//} diff --git a/fsfw/unittest/tests/osal/TestMessageQueue.cpp b/fsfw/unittest/tests/osal/TestMessageQueue.cpp new file mode 100644 index 0000000..8e59fa0 --- /dev/null +++ b/fsfw/unittest/tests/osal/TestMessageQueue.cpp @@ -0,0 +1,40 @@ +#include +#include +#include "catch.hpp" +#include +#include "core/CatchDefinitions.h" + +TEST_CASE("MessageQueue Basic Test","[TestMq]") { + MessageQueueIF* testSenderMq = + QueueFactory::instance()->createMessageQueue(1); + MessageQueueId_t testSenderMqId = testSenderMq->getId(); + + MessageQueueIF* testReceiverMq = + QueueFactory::instance()->createMessageQueue(1); + MessageQueueId_t testReceiverMqId = testReceiverMq->getId(); + std::array testData { 0 }; + testData[0] = 42; + MessageQueueMessage testMessage(testData.data(), 1); + testSenderMq->setDefaultDestination(testReceiverMqId); + + SECTION("Simple Tests") { + auto result = testSenderMq->sendMessage(testReceiverMqId, &testMessage); + REQUIRE(result == retval::CATCH_OK); + MessageQueueMessage recvMessage; + result = testReceiverMq->receiveMessage(&recvMessage); + REQUIRE(result == retval::CATCH_OK); + CHECK(recvMessage.getData()[0] == 42); + + result = testSenderMq->sendMessage(testReceiverMqId, &testMessage); + REQUIRE(result == retval::CATCH_OK); + MessageQueueId_t senderId = 0; + result = testReceiverMq->receiveMessage(&recvMessage,&senderId); + REQUIRE(result == retval::CATCH_OK); + CHECK(recvMessage.getData()[0] == 42); + CHECK(senderId == testSenderMqId); + senderId = testReceiverMq->getLastPartner(); + CHECK(senderId == testSenderMqId); + } + + +} diff --git a/fsfw/unittest/tests/osal/TestSemaphore.cpp b/fsfw/unittest/tests/osal/TestSemaphore.cpp new file mode 100644 index 0000000..f7b2553 --- /dev/null +++ b/fsfw/unittest/tests/osal/TestSemaphore.cpp @@ -0,0 +1,46 @@ + +#ifdef LINUX + +/* +#include "core/CatchDefinitions.h" +#include "catch.hpp" + +#include +#include + +TEST_CASE("Binary Semaphore Test" , "[BinSemaphore]") { + //perform set-up here + SemaphoreIF* binSemaph = SemaphoreFactory::instance()-> + createBinarySemaphore(); + REQUIRE(binSemaph != nullptr); + SECTION("Simple Test") { + // set-up is run for each section + REQUIRE(binSemaph->getSemaphoreCounter() == 1); + REQUIRE(binSemaph->release() == + static_cast(SemaphoreIF::SEMAPHORE_NOT_OWNED)); + REQUIRE(binSemaph->acquire(SemaphoreIF::POLLING) == + retval::CATCH_OK); + { + // not precise enough on linux.. should use clock instead.. + //Stopwatch stopwatch(false); + //REQUIRE(binSemaph->acquire(SemaphoreIF::TimeoutType::WAITING, 5) == + // SemaphoreIF::SEMAPHORE_TIMEOUT); + //dur_millis_t time = stopwatch.stop(); + //CHECK(time == 5); + } + REQUIRE(binSemaph->getSemaphoreCounter() == 0); + REQUIRE(binSemaph->release() == retval::CATCH_OK); + } + SemaphoreFactory::instance()->deleteSemaphore(binSemaph); + // perform tear-down here +} + + +TEST_CASE("Counting Semaphore Test" , "[CountingSemaph]") { + SECTION("Simple Test") { + + } +} +*/ + +#endif diff --git a/fsfw/unittest/tests/serialize/TestSerialBufferAdapter.cpp b/fsfw/unittest/tests/serialize/TestSerialBufferAdapter.cpp new file mode 100644 index 0000000..9919ed8 --- /dev/null +++ b/fsfw/unittest/tests/serialize/TestSerialBufferAdapter.cpp @@ -0,0 +1,143 @@ +#include + +#include +#include "../../core/CatchDefinitions.h" + + +static bool test_value_bool = true; +static uint16_t tv_uint16 {283}; +static std::array testArray; + +TEST_CASE("Serial Buffer Adapter", "[single-file]") { + size_t serialized_size = 0; + test_value_bool = true; + uint8_t * arrayPtr = testArray.data(); + std::array test_serial_buffer {5, 4, 3, 2, 1}; + SerialBufferAdapter tv_serial_buffer_adapter = + SerialBufferAdapter(test_serial_buffer.data(), + test_serial_buffer.size(), false); + tv_uint16 = 16; + + SECTION("Serialize without size field") { + SerializeAdapter::serialize(&test_value_bool, &arrayPtr, + &serialized_size, testArray.size(), + SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_serial_buffer_adapter, &arrayPtr, + &serialized_size, testArray.size(), + SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_uint16, &arrayPtr, &serialized_size, + testArray.size(), SerializeIF::Endianness::MACHINE); + + REQUIRE(serialized_size == 8); + REQUIRE(testArray[0] == true); + REQUIRE(testArray[1] == 5); + REQUIRE(testArray[2] == 4); + REQUIRE(testArray[3] == 3); + REQUIRE(testArray[4] == 2); + REQUIRE(testArray[5] == 1); + memcpy(&tv_uint16, testArray.data() + 6, sizeof(tv_uint16)); + REQUIRE(tv_uint16 == 16); + } + + SECTION("Serialize with size field") { + SerialBufferAdapter tv_serial_buffer_adapter_loc = + SerialBufferAdapter(test_serial_buffer.data(), + test_serial_buffer.size(), true); + serialized_size = 0; + arrayPtr = testArray.data(); + SerializeAdapter::serialize(&test_value_bool, &arrayPtr,&serialized_size, + testArray.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_serial_buffer_adapter_loc, &arrayPtr, + &serialized_size, testArray.size(), + SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_uint16, &arrayPtr, &serialized_size, + testArray.size(), SerializeIF::Endianness::MACHINE); + + REQUIRE(serialized_size == 9); + REQUIRE(testArray[0] == true); + REQUIRE(testArray[1] == 5); + REQUIRE(testArray[2] == 5); + REQUIRE(testArray[3] == 4); + REQUIRE(testArray[4] == 3); + REQUIRE(testArray[5] == 2); + REQUIRE(testArray[6] == 1); + memcpy(&tv_uint16, testArray.data() + 7, sizeof(tv_uint16)); + REQUIRE(tv_uint16 == 16); + } + + SECTION("Test set buffer function") { + SerialBufferAdapter tv_serial_buffer_adapter_loc = + SerialBufferAdapter((uint8_t*)nullptr, + 0, true); + tv_serial_buffer_adapter_loc.setBuffer(test_serial_buffer.data(), + test_serial_buffer.size()); + serialized_size = 0; + arrayPtr = testArray.data(); + SerializeAdapter::serialize(&test_value_bool, &arrayPtr,&serialized_size, + testArray.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_serial_buffer_adapter_loc, &arrayPtr, + &serialized_size, testArray.size(), + SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_uint16, &arrayPtr, &serialized_size, + testArray.size(), SerializeIF::Endianness::MACHINE); + REQUIRE(serialized_size == 9); + REQUIRE(testArray[0] == true); + REQUIRE(testArray[1] == 5); + REQUIRE(testArray[2] == 5); + REQUIRE(testArray[3] == 4); + REQUIRE(testArray[4] == 3); + REQUIRE(testArray[5] == 2); + REQUIRE(testArray[6] == 1); + memcpy(&tv_uint16, testArray.data() + 7, sizeof(tv_uint16)); + REQUIRE(tv_uint16 == 16); + } + + SECTION("Deserialization with size field") { + size_t buffer_size = 4; + memcpy(testArray.data(), &buffer_size, sizeof(uint16_t)); + testArray[2] = 1; + testArray[3] = 1; + testArray[4] = 1; + testArray[5] = 0; + std::array test_recv_array; + arrayPtr = testArray.data(); + // copy testArray[1] to testArray[4] into receive buffer, skip + // size field (testArray[0]) for deSerialization. + SerialBufferAdapter tv_serial_buffer_adapter3 = + SerialBufferAdapter(test_recv_array.data(), 4, true); + // Deserialization + size_t size = 6; + auto result = tv_serial_buffer_adapter3.deSerialize( + const_cast(&arrayPtr), &size, + SerializeIF::Endianness::MACHINE); + REQUIRE(result == retval::CATCH_OK); + CHECK(test_recv_array[0] == 1); + CHECK(test_recv_array[1] == 1); + CHECK(test_recv_array[2] == 1); + CHECK(test_recv_array[3] == 0); + } + + SECTION("Deserialization without size field") { + size_t buffer_size = 4; + memcpy(testArray.data(), &buffer_size, sizeof(uint16_t)); + testArray[2] = 1; + testArray[3] = 1; + testArray[4] = 1; + testArray[5] = 0; + std::array test_recv_array; + arrayPtr = testArray.data() + 2; + // copy testArray[1] to testArray[4] into receive buffer, skip + // size field (testArray[0]) + SerialBufferAdapter tv_serial_buffer_adapter3 = + SerialBufferAdapter(test_recv_array.data(), 4, false); + // Deserialization + size_t size = 4; + tv_serial_buffer_adapter3.deSerialize( + const_cast(&arrayPtr), &size, + SerializeIF::Endianness::MACHINE); + CHECK(test_recv_array[0] == 1); + CHECK(test_recv_array[1] == 1); + CHECK(test_recv_array[2] == 1); + CHECK(test_recv_array[3] == 0); + } +} diff --git a/fsfw/unittest/tests/serialize/TestSerialLinkedPacket.cpp b/fsfw/unittest/tests/serialize/TestSerialLinkedPacket.cpp new file mode 100644 index 0000000..0a09e43 --- /dev/null +++ b/fsfw/unittest/tests/serialize/TestSerialLinkedPacket.cpp @@ -0,0 +1,73 @@ +#include + +#include +#include "../../core/CatchDefinitions.h" +#include "TestSerialLinkedPacket.h" + + +TEST_CASE("Serial Linked Packet" , "[SerLinkPacket]") { + // perform set-up here + uint32_t header = 42; + std::array testArray {1,2,3}; + uint32_t tail = 96; + size_t packetMaxSize = 256; + uint8_t packet [packetMaxSize] = {}; + size_t packetLen = 0; + + SECTION("Test Deserialization with Serial Buffer Adapter.") { + // This is a serialization of a packet, made "manually". + // We generate a packet which store data big-endian by swapping some + // values. (like coming from ground). + header = EndianConverter::convertBigEndian(header); + std::memcpy(packet, &header, sizeof(header)); + packetLen += sizeof(header); + + std::copy(testArray.data(), testArray.data() + testArray.size(), + packet + packetLen); + packetLen += testArray.size(); + + tail = EndianConverter::convertBigEndian(tail); + std::memcpy(packet + packetLen, &tail, sizeof(tail)); + packetLen += sizeof(tail); + + //arrayprinter::print(packet, packetLen, OutputType::DEC); + + // This is the buffer which will be filled when testClass.deSerialize + // is called. + std::array bufferAdaptee = {}; + TestPacket testClass(packet, packetLen, bufferAdaptee.data(), + bufferAdaptee.size()); + const uint8_t* readOnlyPointer = packet; + // Deserialize big endian packet by setting bigEndian to true. + ReturnValue_t result = testClass.deSerialize(&readOnlyPointer, + &packetLen, SerializeIF::Endianness::BIG); + REQUIRE(result == retval::CATCH_OK); + CHECK(testClass.getHeader() == 42); + // Equivalent check. + // CHECK(testClass.getBuffer()[0] == 1); + CHECK(bufferAdaptee[0] == 1); + CHECK(bufferAdaptee[1] == 2); + CHECK(bufferAdaptee[2] == 3); + CHECK(testClass.getTail() == 96); + } + + SECTION("Test Serialization") { + // Same process as performed in setup, this time using the class + // instead of doing it manually. + TestPacket testClass(header, tail, testArray.data(), testArray.size()); + size_t serializedSize = 0; + uint8_t* packetPointer = packet; + // serialize for ground: bigEndian = true. + ReturnValue_t result = testClass.serialize(&packetPointer, + &serializedSize, packetMaxSize, SerializeIF::Endianness::BIG); + REQUIRE(result == retval::CATCH_OK); + // Result should be big endian now. + CHECK(packet[3] == 42); + CHECK(packet[4] == 1); + CHECK(packet[5] == 2); + CHECK(packet[6] == 3); + CHECK(packet[10] == 96); + } + + // perform tear-down here +} diff --git a/fsfw/unittest/tests/serialize/TestSerialLinkedPacket.h b/fsfw/unittest/tests/serialize/TestSerialLinkedPacket.h new file mode 100644 index 0000000..6c72057 --- /dev/null +++ b/fsfw/unittest/tests/serialize/TestSerialLinkedPacket.h @@ -0,0 +1,61 @@ +#ifndef UNITTEST_HOSTED_TESTSERIALLINKEDPACKET_H_ +#define UNITTEST_HOSTED_TESTSERIALLINKEDPACKET_H_ + +#include +#include +#include +#include +#include + +class TestPacket: public SerialLinkedListAdapter { +public: + /** + * For Deserialization + */ + TestPacket(const uint8_t *somePacket, size_t size, uint8_t * storePointer, + size_t storeSize): + buffer(storePointer, storeSize) + { + setLinks(); + } + + /** + * For Serialization + */ + TestPacket(uint32_t header, uint32_t tail, + const uint8_t* parameters, size_t paramSize): + header(header), buffer(parameters, paramSize), + tail(tail) { + setLinks(); + } + + uint32_t getHeader() const { + return header.entry; + } + + const uint8_t * getBuffer() { + return buffer.entry.getConstBuffer(); + } + + const size_t getBufferLength() { + return buffer.getSerializedSize(); + } + + + uint16_t getTail() const { + return tail.entry; + } +private: + void setLinks() { + setStart(&header); + header.setNext(&buffer); + buffer.setNext(&tail); + tail.setEnd(); + } + + SerializeElement header = 0; + SerializeElement> buffer; + SerializeElement tail = 0; +}; + +#endif /* UNITTEST_TESTFW_NEWTESTS_TESTTEMPLATE_H_ */ diff --git a/fsfw/unittest/tests/serialize/TestSerialization.cpp b/fsfw/unittest/tests/serialize/TestSerialization.cpp new file mode 100644 index 0000000..4c9ba18 --- /dev/null +++ b/fsfw/unittest/tests/serialize/TestSerialization.cpp @@ -0,0 +1,129 @@ +#include + +#include "catch.hpp" +#include +#include "../../core/CatchDefinitions.h" + +static bool test_value_bool = true; +static uint8_t tv_uint8 {5}; +static uint16_t tv_uint16 {283}; +static uint32_t tv_uint32 {929221}; +static uint64_t tv_uint64 {2929329429}; + +static int8_t tv_int8 {-16}; +static int16_t tv_int16 {-829}; +static int32_t tv_int32 {-2312}; + +static float tv_float {8.2149214}; +static float tv_sfloat = {-922.2321321}; +static double tv_double {9.2132142141e8}; +static double tv_sdouble {-2.2421e19}; + +static std::array test_array; + +TEST_CASE( "Serialization size tests", "[TestSerialization]") { + //REQUIRE(unitTestClass.test_autoserialization() == 0); + REQUIRE(SerializeAdapter::getSerializedSize(&test_value_bool) == + sizeof(test_value_bool)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_uint8) == + sizeof(tv_uint8)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_uint16) == + sizeof(tv_uint16)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_uint32 ) == + sizeof(tv_uint32)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_uint64) == + sizeof(tv_uint64)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_int8) == + sizeof(tv_int8)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_int16) == + sizeof(tv_int16)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_int32) == + sizeof(tv_int32)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_float) == + sizeof(tv_float)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_sfloat) == + sizeof(tv_sfloat )); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_double) == + sizeof(tv_double)); + REQUIRE(SerializeAdapter::getSerializedSize(&tv_sdouble) == + sizeof(tv_sdouble)); +} + + +TEST_CASE("Auto Serialize Adapter testing", "[single-file]") { + size_t serialized_size = 0; + uint8_t * p_array = test_array.data(); + + SECTION("Serializing...") { + SerializeAdapter::serialize(&test_value_bool, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_uint8, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_uint16, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_uint32, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_int8, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_int16, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_int32, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_uint64, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_float, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_double, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_sfloat, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + SerializeAdapter::serialize(&tv_sdouble, &p_array, + &serialized_size, test_array.size(), SerializeIF::Endianness::MACHINE); + REQUIRE (serialized_size == 47); + } + + SECTION("Deserializing") { + p_array = test_array.data(); + size_t remaining_size = serialized_size; + SerializeAdapter::deSerialize(&test_value_bool, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_uint8, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_uint16, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_uint32, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_int8, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_int16, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_int32, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_uint64, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_float, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_double, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_sfloat, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + SerializeAdapter::deSerialize(&tv_sdouble, + const_cast(&p_array), &remaining_size, SerializeIF::Endianness::MACHINE); + + REQUIRE(test_value_bool == true); + REQUIRE(tv_uint8 == 5); + REQUIRE(tv_uint16 == 283); + REQUIRE(tv_uint32 == 929221); + REQUIRE(tv_uint64 == 2929329429); + REQUIRE(tv_int8 == -16); + REQUIRE(tv_int16 == -829); + REQUIRE(tv_int32 == -2312); + + REQUIRE(tv_float == Approx(8.214921)); + REQUIRE(tv_double == Approx(9.2132142141e8)); + REQUIRE(tv_sfloat == Approx(-922.2321321)); + REQUIRE(tv_sdouble == Approx(-2.2421e19)); + } +} + + diff --git a/fsfw/unittest/tests/storagemanager/TestNewAccessor.cpp b/fsfw/unittest/tests/storagemanager/TestNewAccessor.cpp new file mode 100644 index 0000000..7bd0dee --- /dev/null +++ b/fsfw/unittest/tests/storagemanager/TestNewAccessor.cpp @@ -0,0 +1,161 @@ +//#include +//#include +//#include "../../core/CatchDefinitions.h" +//#include +// +//TEST_CASE( "New Accessor" , "[NewAccessor]") { +// uint16_t numberOfElements[1] = {1}; +// uint16_t sizeofElements[1] = {10}; +// LocalPool<1> SimplePool = LocalPool<1>(0, sizeofElements, numberOfElements); +// std::array testDataArray; +// std::array receptionArray; +// store_address_t testStoreId; +// ReturnValue_t result = retval::CATCH_FAILED; +// +// for(size_t i = 0; i < testDataArray.size(); i++) { +// testDataArray[i] = i; +// } +// size_t size = 10; +// +// SECTION ("Simple tests getter functions") { +// result = SimplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// auto resultPair = SimplePool.getData(testStoreId); +// REQUIRE(resultPair.first == retval::CATCH_OK); +// resultPair.second.getDataCopy(receptionArray.data(), 20); +// CHECK(resultPair.second.getId() == testStoreId); +// CHECK(resultPair.second.size() == 10); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// +// std::copy(resultPair.second.data(), resultPair.second.data() + +// resultPair.second.size(), receptionArray.data()); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// +// { +// auto resultPairLoc = SimplePool.getData(testStoreId); +// REQUIRE(resultPairLoc.first == retval::CATCH_OK); +// // data should be deleted when accessor goes out of scope. +// } +// resultPair = SimplePool.getData(testStoreId); +// REQUIRE(resultPair.first == (int) StorageManagerIF::DATA_DOES_NOT_EXIST); +// +// result = SimplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// { +// ConstStorageAccessor constAccessor(testStoreId); +// result = SimplePool.getData(testStoreId, constAccessor); +// REQUIRE(result == retval::CATCH_OK); +// constAccessor.getDataCopy(receptionArray.data(), 20); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// // likewise, data should be deleted when accessor gets out of scope. +// } +// resultPair = SimplePool.getData(testStoreId); +// REQUIRE(resultPair.first == (int) StorageManagerIF::DATA_DOES_NOT_EXIST); +// +// result = SimplePool.addData(&testStoreId, testDataArray.data(), size); +// { +// resultPair = SimplePool.getData(testStoreId); +// REQUIRE(resultPair.first == retval::CATCH_OK); +// resultPair.second.release(); +// // now data should not be deleted anymore +// } +// resultPair = SimplePool.getData(testStoreId); +// REQUIRE(resultPair.first == retval::CATCH_OK); +// resultPair.second.getDataCopy(receptionArray.data(), 20); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// } +// +// +// SECTION("Simple tests modify functions") { +// result = SimplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// { +// StorageAccessor accessor(testStoreId); +// result = SimplePool.modifyData(testStoreId, accessor); +// REQUIRE(result == retval::CATCH_OK); +// CHECK(accessor.getId() == testStoreId); +// CHECK(accessor.size() == 10); +// accessor.getDataCopy(receptionArray.data(), 20); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// std::copy(accessor.data(), accessor.data() + +// accessor.size(), receptionArray.data()); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// // data should be deleted when accessor goes out of scope +// } +// auto resultPair = SimplePool.getData(testStoreId); +// REQUIRE(resultPair.first == (int) StorageManagerIF::DATA_DOES_NOT_EXIST); +// +// result = SimplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// { +// auto resultPairLoc = SimplePool.modifyData(testStoreId); +// REQUIRE(resultPairLoc.first == retval::CATCH_OK); +// CHECK(resultPairLoc.second.getId() == testStoreId); +// CHECK(resultPairLoc.second.size() == 10); +// resultPairLoc.second.getDataCopy(receptionArray.data(), 20); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// std::copy(resultPairLoc.second.data(), resultPairLoc.second.data() + +// resultPairLoc.second.size(), receptionArray.data()); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// resultPairLoc.second.release(); +// // data should not be deleted when accessor goes out of scope +// } +// resultPair = SimplePool.getData(testStoreId); +// REQUIRE(resultPair.first == retval::CATCH_OK); +// } +// +// +// SECTION("Write tests") { +// result = SimplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// { +// auto resultPair = SimplePool.modifyData(testStoreId); +// REQUIRE(resultPair.first == retval::CATCH_OK); +// testDataArray[9] = 42; +// resultPair.second.write(testDataArray.data(), 10, 0); +// // now data should not be deleted +// resultPair.second.release(); +// } +// auto resultConstPair = SimplePool.getData(testStoreId); +// REQUIRE(resultConstPair.first == retval::CATCH_OK); +// +// resultConstPair.second.getDataCopy(receptionArray.data(), 10); +// for(size_t i = 0; i < size-1; i++) { +// CHECK(receptionArray[i] == i ); +// } +// CHECK(receptionArray[9] == 42 ); +// +// auto resultPair = SimplePool.modifyData(testStoreId); +// REQUIRE(resultPair.first == retval::CATCH_OK); +// result = resultPair.second.write(testDataArray.data(), 20, 0); +// REQUIRE(result == retval::CATCH_FAILED); +// result = resultPair.second.write(testDataArray.data(), 10, 5); +// REQUIRE(result == retval::CATCH_FAILED); +// +// memset(testDataArray.data(), 42, 5); +// result = resultPair.second.write(testDataArray.data(), 5, 5); +// REQUIRE(result == retval::CATCH_OK); +// resultConstPair = SimplePool.getData(testStoreId); +// resultPair.second.getDataCopy(receptionArray.data(), 20); +// for(size_t i = 5; i < 10; i++) { +// CHECK(receptionArray[i] == 42 ); +// } +// +// } +//} diff --git a/fsfw/unittest/tests/storagemanager/TestPool.cpp b/fsfw/unittest/tests/storagemanager/TestPool.cpp new file mode 100644 index 0000000..f278c40 --- /dev/null +++ b/fsfw/unittest/tests/storagemanager/TestPool.cpp @@ -0,0 +1,296 @@ +//#include "CatchDefinitions.h" +// +//#include +//#include +//#include +// +//#include +//#include +// +//#include +// +// +//TEST_CASE( "Local Pool Simple Tests [1 Pool]" , "[TestPool]") { +//// uint16_t numberOfElements[1] = {1}; +//// uint16_t sizeofElements[1] = {10}; +// LocalPool::LocalPoolConfig config = {{1, 10}}; +// LocalPool simplePool(0, config); +// std::array testDataArray; +// std::array receptionArray; +// store_address_t testStoreId; +// ReturnValue_t result = retval::CATCH_FAILED; +// uint8_t *pointer = nullptr; +// const uint8_t * constPointer = nullptr; +// +// for(size_t i = 0; i < testDataArray.size(); i++) { +// testDataArray[i] = i; +// } +// size_t size = 10; +// +// SECTION ( "Basic tests") { +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// result = simplePool.getData(testStoreId, &constPointer, &size); +// REQUIRE(result == retval::CATCH_OK); +// memcpy(receptionArray.data(), constPointer, size); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// memset(receptionArray.data(), 0, size); +// result = simplePool.modifyData(testStoreId, &pointer, &size); +// memcpy(receptionArray.data(), pointer, size); +// REQUIRE(result == retval::CATCH_OK); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// result = simplePool.deleteData(testStoreId); +// REQUIRE(result == retval::CATCH_OK); +// result = simplePool.addData(&testStoreId, testDataArray.data(), 15); +// CHECK (result == (int) StorageManagerIF::DATA_TOO_LARGE); +// } +// +// SECTION ( "Reservation Tests ") { +// pointer = nullptr; +// result = simplePool.getFreeElement(&testStoreId, size, &pointer); +// REQUIRE (result == retval::CATCH_OK); +// memcpy(pointer, testDataArray.data(), size); +// constPointer = nullptr; +// result = simplePool.getData(testStoreId, &constPointer, &size); +// +// REQUIRE (result == retval::CATCH_OK); +// memcpy(receptionArray.data(), constPointer, size); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// } +// +// SECTION ( "Add, delete, add, add when full") { +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// result = simplePool.getData(testStoreId, &constPointer, &size); +// REQUIRE( result == retval::CATCH_OK); +// memcpy(receptionArray.data(), constPointer, size); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// +// result = simplePool.deleteData(testStoreId); +// REQUIRE(result == retval::CATCH_OK); +// +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// result = simplePool.getData(testStoreId, &constPointer, &size); +// REQUIRE( result == retval::CATCH_OK); +// memcpy(receptionArray.data(), constPointer, size); +// for(size_t i = 0; i < size; i++) { +// CHECK(receptionArray[i] == i ); +// } +// +// store_address_t newAddress; +// result = simplePool.addData(&newAddress, testDataArray.data(), size); +// REQUIRE(result == (int) StorageManagerIF::DATA_STORAGE_FULL); +// +// // Packet Index to high intentionally +// newAddress.packetIndex = 2; +// pointer = testDataArray.data(); +// result = simplePool.modifyData(newAddress, &pointer, &size); +// REQUIRE(result == (int) StorageManagerIF::ILLEGAL_STORAGE_ID); +// +// result = simplePool.deleteData(newAddress); +// REQUIRE(result == (int) StorageManagerIF::ILLEGAL_STORAGE_ID); +// +// newAddress.packetIndex = 0; +// newAddress.poolIndex = 2; +// result = simplePool.deleteData(newAddress); +// REQUIRE(result == (int) StorageManagerIF::ILLEGAL_STORAGE_ID); +// } +// +// SECTION ( "Initialize and clear store, delete with pointer") { +// result = simplePool.initialize(); +// REQUIRE(result == retval::CATCH_OK); +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// simplePool.clearStore(); +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// result = simplePool.modifyData(testStoreId, &pointer, &size); +// REQUIRE(result == retval::CATCH_OK); +// store_address_t newId; +// result = simplePool.deleteData(pointer, size, &testStoreId); +// REQUIRE(result == retval::CATCH_OK); +// REQUIRE(testStoreId.raw != (uint32_t) StorageManagerIF::INVALID_ADDRESS); +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// } +//} +// +//int runIdx = 0; +// +//TEST_CASE( "Local Pool Extended Tests [3 Pools]" , "[TestPool2]") { +// LocalPool::LocalPoolConfig* config; +// if(runIdx == 0) { +// config = new LocalPool::LocalPoolConfig{{10, 5}, {5, 10}, {2, 20}}; +// } +// else { +// // shufle the order, they should be sort implictely so that the +// // order is ascending for the page sizes. +// config = new LocalPool::LocalPoolConfig{{5, 10}, {2, 20}, {10, 5}}; +// size_t lastSize = 0; +// for(const auto& pair: *config) { +// CHECK(pair.second > lastSize); +// lastSize = pair.second; +// } +// } +// runIdx++; +// +// LocalPool simplePool(0, *config); +// std::array testDataArray; +// std::array receptionArray; +// store_address_t testStoreId; +// ReturnValue_t result = retval::CATCH_FAILED; +// for(size_t i = 0; i < testDataArray.size(); i++) { +// testDataArray[i] = i; +// } +// size_t size = 0; +// +// SECTION ("Basic tests") { +// size = 8; +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// // Should be on second page of the pool now for 8 bytes +// CHECK(testStoreId.poolIndex == 1); +// CHECK(testStoreId.packetIndex == 0); +// +// size = 15; +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// // Should be on third page of the pool now for 15 bytes +// CHECK(testStoreId.poolIndex == 2); +// CHECK(testStoreId.packetIndex == 0); +// +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// // Should be on third page of the pool now for 15 bytes +// CHECK(testStoreId.poolIndex == 2); +// CHECK(testStoreId.packetIndex == 1); +// +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// // Should be on third page of the pool now for 15 bytes +// REQUIRE(result == (int) LocalPool::DATA_STORAGE_FULL); +// +// size = 8; +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// // Should still work +// CHECK(testStoreId.poolIndex == 1); +// CHECK(testStoreId.packetIndex == 1); +// +// // fill the rest of the pool +// for(uint8_t idx = 2; idx < 5; idx++) { +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// CHECK(testStoreId.poolIndex == 1); +// CHECK(testStoreId.packetIndex == idx); +// } +// } +// +// SECTION ("Fill Count and Clearing") { +// //SECTION("Basic tests"); +// uint8_t bytesWritten = 0; +// simplePool.getFillCount(receptionArray.data(), &bytesWritten); +// // fill count should be all zeros now. +// CHECK(bytesWritten == 4); +// CHECK(receptionArray[0] == 0); +// CHECK(receptionArray[1] == 0); +// CHECK(receptionArray[2] == 0); +// CHECK(receptionArray[3] == 0); +// +// // now fill the store completely. +// size = 5; +// for(uint8_t idx = 0; idx < 10; idx++) { +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// CHECK(testStoreId.poolIndex == 0); +// CHECK(testStoreId.packetIndex == idx); +// } +// size = 10; +// for(uint8_t idx = 0; idx < 5; idx++) { +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// CHECK(testStoreId.poolIndex == 1); +// CHECK(testStoreId.packetIndex == idx); +// } +// size = 20; +// for(uint8_t idx = 0; idx < 2; idx++) { +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// CHECK(testStoreId.poolIndex == 2); +// CHECK(testStoreId.packetIndex == idx); +// } +// bytesWritten = 0; +// simplePool.getFillCount(receptionArray.data(), &bytesWritten); +// // fill count should be all 100 now. +// CHECK(bytesWritten == 4); +// CHECK(receptionArray[0] == 100); +// CHECK(receptionArray[1] == 100); +// CHECK(receptionArray[2] == 100); +// CHECK(receptionArray[3] == 100); +// +// // now clear the store +// simplePool.clearStore(); +// bytesWritten = 0; +// simplePool.getFillCount(receptionArray.data(), &bytesWritten); +// CHECK(bytesWritten == 4); +// CHECK(receptionArray[0] == 0); +// CHECK(receptionArray[1] == 0); +// CHECK(receptionArray[2] == 0); +// CHECK(receptionArray[3] == 0); +// +// // now fill one page +// size = 5; +// for(uint8_t idx = 0; idx < 10; idx++) { +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// CHECK(testStoreId.poolIndex == 0); +// CHECK(testStoreId.packetIndex == idx); +// } +// bytesWritten = 0; +// simplePool.getFillCount(receptionArray.data(), &bytesWritten); +// // First page full, median fill count is 33 % +// CHECK(bytesWritten == 4); +// CHECK(receptionArray[0] == 100); +// CHECK(receptionArray[1] == 0); +// CHECK(receptionArray[2] == 0); +// CHECK(receptionArray[3] == 33); +// +// // now fill second page +// size = 10; +// for(uint8_t idx = 0; idx < 5; idx++) { +// result = simplePool.addData(&testStoreId, testDataArray.data(), size); +// REQUIRE(result == retval::CATCH_OK); +// CHECK(testStoreId.poolIndex == 1); +// CHECK(testStoreId.packetIndex == idx); +// } +// bytesWritten = 0; +// simplePool.getFillCount(receptionArray.data(), &bytesWritten); +// // First and second page full, median fill count is 66 % +// CHECK(bytesWritten == 4); +// CHECK(receptionArray[0] == 100); +// CHECK(receptionArray[1] == 100); +// CHECK(receptionArray[2] == 0); +// CHECK(receptionArray[3] == 66); +// +// // now clear first page +// simplePool.clearPage(0); +// bytesWritten = 0; +// simplePool.getFillCount(receptionArray.data(), &bytesWritten); +// // Second page full, median fill count is 33 % +// CHECK(bytesWritten == 4); +// CHECK(receptionArray[0] == 0); +// CHECK(receptionArray[1] == 100); +// CHECK(receptionArray[2] == 0); +// CHECK(receptionArray[3] == 33); +// } +// +// delete(config); +//} diff --git a/fsfw/unittest/tests/tests.mk b/fsfw/unittest/tests/tests.mk new file mode 100644 index 0000000..47e634a --- /dev/null +++ b/fsfw/unittest/tests/tests.mk @@ -0,0 +1,8 @@ +CXXSRC += $(wildcard $(CURRENTPATH)/container/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/action/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/serialize/*.cpp) +CXXSRC += $(wildcard $(CURRENTPATH)/storagemanager/*.cpp) + +# OSAL not included for now. + +INCLUDES += $(CURRENTPATH) \ No newline at end of file diff --git a/fsfw/unittest/testtemplate/TestTemplate.cpp b/fsfw/unittest/testtemplate/TestTemplate.cpp new file mode 100644 index 0000000..6b5fc3d --- /dev/null +++ b/fsfw/unittest/testtemplate/TestTemplate.cpp @@ -0,0 +1,31 @@ +#include +#include + + +/** + * @brief Template test file + * @details + * In each test case, the code outside the sections is executed + * for EACH section. + * The most common macros to perform tests are: + * - CHECK(...): assert expression and continues even if it fails + * - REQUIRE(...): test case fails if assertion fails + * + * Tests are generally sturctured in test cases and sections, see example + * below. + * + * More Documentation: + * - https://github.com/catchorg/Catch2 + * - https://github.com/catchorg/Catch2/blob/master/docs/assertions.md + * - https://github.com/catchorg/Catch2/blob/master/docs/test-cases-and-sections.md + */ +TEST_CASE("Dummy Test" , "[DummyTest]") { + uint8_t testVariable = 1; + //perform set-up here + CHECK(testVariable == 1); + SECTION("TestSection") { + // set-up is run for each section + REQUIRE(testVariable == 1); + } + // perform tear-down here +} diff --git a/fsfw/unittest/unlockRealtime.sh b/fsfw/unittest/unlockRealtime.sh new file mode 100644 index 0000000..b28d549 --- /dev/null +++ b/fsfw/unittest/unlockRealtime.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# Run this script to unlock all permissions to run the linux binaries +# and create threads + +binaries=$(find $directory -type f -name "*.elf") + +echo Unlocking real time permissions for binaries and bash console... + +# Set up the soft realtime limit to maximum (99) +# Please note that the hard limit needs to be set to 99 too +# for this to work (check with ulimit -Hr). +# If that has not been done yet, add +# hard rtprio 99 +# to /etc/security/limits.conf +# It is also necessary and recommended to add +# soft rtprio 99 +# as well. This can also be done in the command line +# but would need to be done for each session. +ulimit -Sr 99 + +for binary in ${binaries}; do + sudo setcap 'cap_sys_nice=eip' ${binary} + result=$? + if [ ${result} = 0 ];then + echo ${binary} was unlocked + fi +done + +# sudo setcap 'cap_sys_nice=eip' /bin/bash +# result=$? +# if [ ${result} = 0 ];then +# echo /bin/bash was unlocked +# fi +