From 1b9c8446b7bb7fa005eaf7ce390c12800a3a2177 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Fri, 28 Aug 2020 18:33:29 +0200 Subject: [PATCH] renormalized line endings --- container/ArrayList.h | 494 +- container/DynamicFIFO.h | 84 +- container/FIFO.h | 68 +- container/FIFOBase.h | 130 +- container/FixedArrayList.h | 114 +- container/FixedMap.h | 450 +- container/HybridIterator.h | 180 +- container/IndexedRingMemoryArray.h | 1442 +++--- container/PlacementFactory.h | 70 +- container/RingBufferBase.h | 226 +- container/SharedRingBuffer.cpp | 60 +- container/SharedRingBuffer.h | 136 +- container/SimpleRingBuffer.cpp | 270 +- container/SimpleRingBuffer.h | 256 +- contrib/sgp4/sgp4unit.cpp | 4180 ++++++++--------- contrib/sgp4/sgp4unit.h | 234 +- controller/ControllerBase.cpp | 280 +- controller/ControllerBase.h | 188 +- datapool/ControllerSet.cpp | 28 +- datapool/ControllerSet.h | 30 +- datapool/HkSwitchHelper.cpp | 150 +- datapool/HkSwitchHelper.h | 92 +- datapool/PoolEntry.cpp | 174 +- datapool/PoolEntry.h | 260 +- datapool/PoolEntryIF.h | 126 +- datapool/PoolRawAccessHelper.cpp | 376 +- datapool/PoolRawAccessHelper.h | 222 +- datapool/PoolVarList.h | 56 +- datapoolglob/DataPoolAdmin.cpp | 600 +-- datapoolglob/DataPoolAdmin.h | 118 +- datapoolglob/DataPoolParameterWrapper.cpp | 358 +- datapoolglob/DataPoolParameterWrapper.h | 76 +- datapoolglob/GlobalDataPool.cpp | 266 +- datapoolglob/GlobalDataPool.h | 298 +- datapoolglob/GlobalPoolVariable.h | 426 +- datapoolglob/GlobalPoolVector.h | 370 +- datapoolglob/PIDReader.h | 328 +- datapoolglob/PIDReaderList.h | 54 +- datapoolglob/PoolRawAccess.cpp | 478 +- datapoolglob/PoolRawAccess.h | 440 +- datapoollocal/LocalPoolVector.h | 400 +- datapoollocal/SharedLocalDataSet.cpp | 32 +- devicehandlers/AcceptsDeviceResponsesIF.h | 38 +- devicehandlers/AssemblyBase.cpp | 546 +-- devicehandlers/AssemblyBase.h | 326 +- devicehandlers/ChildHandlerBase.cpp | 94 +- devicehandlers/ChildHandlerBase.h | 48 +- devicehandlers/ChildHandlerFDIR.cpp | 20 +- devicehandlers/ChildHandlerFDIR.h | 40 +- devicehandlers/CommunicationMessage.cpp | 402 +- devicehandlers/CommunicationMessage.h | 346 +- devicehandlers/DeviceCommunicationIF.h | 254 +- devicehandlers/DeviceHandlerIF.h | 310 +- devicehandlers/DeviceHandlerMessage.cpp | 202 +- devicehandlers/DeviceHandlerMessage.h | 182 +- devicehandlers/DeviceTmReportingWrapper.cpp | 92 +- devicehandlers/DeviceTmReportingWrapper.h | 54 +- devicehandlers/HealthDevice.cpp | 118 +- devicehandlers/HealthDevice.h | 80 +- fdir/ConfirmsFailuresIF.h | 40 +- fdir/EventCorrelation.cpp | 88 +- fdir/EventCorrelation.h | 46 +- fdir/FailureIsolationBase.cpp | 328 +- fdir/FailureIsolationBase.h | 112 +- fdir/FaultCounter.cpp | 172 +- fdir/FaultCounter.h | 76 +- globalfunctions/AsciiConverter.cpp | 460 +- globalfunctions/AsciiConverter.h | 78 +- globalfunctions/CRC.cpp | 278 +- globalfunctions/DleEncoder.cpp | 248 +- globalfunctions/DleEncoder.h | 158 +- globalfunctions/Type.cpp | 362 +- globalfunctions/Type.h | 188 +- globalfunctions/arrayprinter.cpp | 122 +- globalfunctions/matching/BinaryMatcher.h | 82 +- globalfunctions/matching/DecimalMatcher.h | 100 +- globalfunctions/matching/MatchTree.h | 432 +- globalfunctions/matching/RangeMatcher.h | 140 +- .../matching/SerializeableMatcherIF.h | 26 +- globalfunctions/math/QuaternionOperations.cpp | 312 +- globalfunctions/timevalOperations.cpp | 198 +- health/HasHealthIF.h | 100 +- health/HealthHelper.cpp | 210 +- health/HealthHelper.h | 242 +- health/HealthMessage.cpp | 56 +- health/HealthMessage.h | 60 +- health/HealthTable.cpp | 200 +- health/HealthTable.h | 70 +- health/HealthTableIF.h | 56 +- health/ManagesHealthIF.h | 104 +- housekeeping/AcceptsHkPacketsIF.h | 22 +- internalError/InternalErrorReporter.cpp | 250 +- internalError/InternalErrorReporter.h | 100 +- ipc/CommandMessage.cpp | 222 +- ipc/CommandMessage.h | 258 +- ipc/CommandMessageCleaner.cpp | 90 +- ipc/CommandMessageCleaner.h | 32 +- ipc/CommandMessageIF.h | 146 +- ipc/MessageQueueIF.h | 342 +- ipc/MessageQueueMessage.cpp | 168 +- ipc/MessageQueueMessage.h | 300 +- ipc/MessageQueueSenderIF.h | 50 +- ipc/MutexFactory.h | 68 +- ipc/MutexHelper.h | 60 +- ipc/MutexIF.h | 178 +- ipc/QueueFactory.h | 66 +- memory/AcceptsMemoryMessagesIF.h | 42 +- memory/FileSystemMessage.cpp | 56 +- memory/FileSystemMessage.h | 60 +- memory/HasFileSystemIF.h | 72 +- memory/HasMemoryIF.h | 94 +- memory/MemoryHelper.cpp | 386 +- memory/MemoryHelper.h | 74 +- memory/MemoryMessage.cpp | 224 +- memory/MemoryMessage.h | 78 +- memory/MemoryProxyIF.h | 44 +- modes/HasModesIF.h | 114 +- modes/ModeHelper.cpp | 270 +- modes/ModeHelper.h | 106 +- modes/ModeMessage.cpp | 62 +- modes/ModeMessage.h | 80 +- monitoring/AbsLimitMonitor.h | 146 +- monitoring/HasMonitorsIF.h | 60 +- monitoring/LimitMonitor.h | 190 +- monitoring/LimitViolationReporter.cpp | 120 +- monitoring/LimitViolationReporter.h | 62 +- monitoring/MonitorBase.h | 124 +- monitoring/MonitorReporter.h | 356 +- monitoring/MonitoringIF.h | 134 +- monitoring/MonitoringMessage.cpp | 76 +- monitoring/MonitoringMessage.h | 42 +- monitoring/MonitoringMessageContent.h | 154 +- monitoring/ReceivesMonitoringReportsIF.h | 30 +- monitoring/TriplexMonitor.h | 310 +- monitoring/TwoValueLimitMonitor.h | 88 +- objectmanager/ObjectManager.cpp | 214 +- objectmanager/ObjectManager.h | 134 +- objectmanager/ObjectManagerIF.h | 190 +- objectmanager/SystemObject.cpp | 74 +- objectmanager/SystemObjectIF.h | 124 +- osal/FreeRTOS/BinSemaphUsingTask.cpp | 190 +- osal/FreeRTOS/BinSemaphUsingTask.h | 158 +- osal/FreeRTOS/BinarySemaphore.cpp | 216 +- osal/FreeRTOS/BinarySemaphore.h | 214 +- osal/FreeRTOS/Clock.cpp | 392 +- osal/FreeRTOS/CountingSemaphUsingTask.cpp | 228 +- osal/FreeRTOS/CountingSemaphUsingTask.h | 210 +- osal/FreeRTOS/CountingSemaphore.cpp | 86 +- osal/FreeRTOS/CountingSemaphore.h | 68 +- osal/FreeRTOS/FixedTimeslotTask.cpp | 332 +- osal/FreeRTOS/FixedTimeslotTask.h | 202 +- osal/FreeRTOS/MessageQueue.h | 298 +- osal/FreeRTOS/Mutex.cpp | 102 +- osal/FreeRTOS/Mutex.h | 58 +- osal/FreeRTOS/MutexFactory.cpp | 56 +- osal/FreeRTOS/QueueFactory.cpp | 72 +- osal/FreeRTOS/SemaphoreFactory.cpp | 118 +- osal/FreeRTOS/TaskFactory.cpp | 106 +- osal/FreeRTOS/TaskManagement.cpp | 48 +- osal/FreeRTOS/TaskManagement.h | 128 +- osal/FreeRTOS/Timekeeper.cpp | 82 +- osal/FreeRTOS/Timekeeper.h | 80 +- osal/InternalErrorCodes.h | 78 +- osal/host/Clock.cpp | 454 +- osal/host/FixedTimeslotTask.cpp | 382 +- osal/host/FixedTimeslotTask.h | 260 +- osal/host/MessageQueue.cpp | 310 +- osal/host/MessageQueue.h | 460 +- osal/host/Mutex.cpp | 80 +- osal/host/Mutex.h | 56 +- osal/host/MutexFactory.cpp | 56 +- osal/host/PeriodicTask.cpp | 352 +- osal/host/PeriodicTask.h | 246 +- osal/host/QueueFactory.cpp | 82 +- osal/host/QueueMapManager.cpp | 102 +- osal/host/QueueMapManager.h | 94 +- osal/host/SemaphoreFactory.cpp | 84 +- osal/host/TaskFactory.cpp | 110 +- osal/linux/BinarySemaphore.cpp | 298 +- osal/linux/BinarySemaphore.h | 162 +- osal/linux/Clock.cpp | 440 +- osal/linux/CountingSemaphore.cpp | 108 +- osal/linux/CountingSemaphore.h | 74 +- osal/linux/FixedTimeslotTask.cpp | 192 +- osal/linux/FixedTimeslotTask.h | 154 +- osal/linux/InternalErrorCodes.cpp | 28 +- osal/linux/MessageQueue.cpp | 738 +-- osal/linux/MessageQueue.h | 374 +- osal/linux/Mutex.cpp | 222 +- osal/linux/Mutex.h | 44 +- osal/linux/MutexFactory.cpp | 46 +- osal/linux/PeriodicPosixTask.cpp | 166 +- osal/linux/PeriodicPosixTask.h | 180 +- osal/linux/PosixThread.cpp | 434 +- osal/linux/PosixThread.h | 154 +- osal/linux/QueueFactory.cpp | 76 +- osal/linux/SemaphoreFactory.cpp | 66 +- osal/linux/TaskFactory.cpp | 84 +- osal/linux/TcUnixUdpPollingTask.cpp | 274 +- osal/linux/TcUnixUdpPollingTask.h | 134 +- osal/linux/Timer.cpp | 84 +- osal/linux/TmTcUnixUdpBridge.cpp | 336 +- osal/linux/TmTcUnixUdpBridge.h | 96 +- osal/rtems/Clock.cpp | 396 +- osal/rtems/CpuUsage.cpp | 366 +- osal/rtems/CpuUsage.h | 106 +- osal/rtems/InternalErrorCodes.cpp | 120 +- osal/rtems/Interrupt.h | 100 +- osal/rtems/MessageQueue.cpp | 294 +- osal/rtems/MessageQueue.h | 362 +- osal/rtems/MultiObjectTask.cpp | 174 +- osal/rtems/MultiObjectTask.h | 226 +- osal/rtems/Mutex.cpp | 130 +- osal/rtems/Mutex.h | 36 +- osal/rtems/MutexFactory.cpp | 48 +- osal/rtems/PollingTask.cpp | 244 +- osal/rtems/PollingTask.h | 170 +- osal/rtems/QueueFactory.cpp | 118 +- osal/rtems/RtemsBasic.h | 50 +- osal/rtems/TaskBase.cpp | 164 +- osal/rtems/TaskBase.h | 94 +- osal/rtems/TaskFactory.cpp | 82 +- parameters/HasParametersIF.h | 134 +- parameters/ParameterHelper.cpp | 264 +- parameters/ParameterHelper.h | 72 +- parameters/ParameterMessage.cpp | 96 +- parameters/ParameterMessage.h | 58 +- parameters/ParameterWrapper.cpp | 560 +-- parameters/ParameterWrapper.h | 324 +- parameters/ReceivesParameterMessagesIF.h | 38 +- power/Fuse.cpp | 520 +- power/Fuse.h | 212 +- power/PowerComponent.cpp | 172 +- power/PowerComponent.h | 96 +- power/PowerComponentIF.h | 48 +- power/PowerSensor.cpp | 256 +- power/PowerSensor.h | 142 +- power/PowerSwitchIF.h | 160 +- power/PowerSwitcher.cpp | 254 +- power/PowerSwitcher.h | 90 +- pus/CService200ModeCommanding.cpp | 238 +- pus/CService200ModeCommanding.h | 170 +- pus/CService201HealthCommanding.cpp | 168 +- pus/CService201HealthCommanding.h | 116 +- pus/Service1TelecommandVerification.cpp | 200 +- pus/Service1TelecommandVerification.h | 188 +- pus/Service2DeviceAccess.cpp | 330 +- pus/Service2DeviceAccess.h | 184 +- pus/Service5EventReporting.cpp | 182 +- pus/Service5EventReporting.h | 172 +- pus/Service8FunctionManagement.cpp | 284 +- pus/Service8FunctionManagement.h | 134 +- pus/servicepackets/Service1Packets.h | 332 +- pus/servicepackets/Service200Packets.h | 126 +- pus/servicepackets/Service201Packets.h | 96 +- pus/servicepackets/Service2Packets.h | 152 +- pus/servicepackets/Service5Packets.h | 152 +- pus/servicepackets/Service8Packets.h | 242 +- returnvalues/HasReturnvaluesIF.h | 62 +- rmap/RMAP.cpp | 178 +- rmap/RMAP.h | 454 +- rmap/RMAPChannelIF.h | 232 +- rmap/RMAPCookie.cpp | 248 +- rmap/RMAPCookie.h | 118 +- rmap/RmapDeviceCommunicationIF.cpp | 94 +- rmap/RmapDeviceCommunicationIF.h | 178 +- serialize/EndianConverter.h | 248 +- serialize/SerialArrayListAdapter.h | 182 +- serialize/SerialBufferAdapter.cpp | 258 +- serialize/SerialBufferAdapter.h | 156 +- serialize/SerialFixedArrayListAdapter.h | 126 +- serialize/SerialLinkedListAdapter.h | 266 +- serialize/SerializeAdapter.h | 350 +- serialize/SerializeElement.h | 120 +- serialize/SerializeIF.h | 116 +- serviceinterface/ServiceInterfaceBuffer.cpp | 434 +- serviceinterface/ServiceInterfaceBuffer.h | 290 +- serviceinterface/ServiceInterfaceStream.cpp | 64 +- serviceinterface/ServiceInterfaceStream.h | 116 +- storagemanager/ConstStorageAccessor.cpp | 174 +- storagemanager/ConstStorageAccessor.h | 232 +- storagemanager/LocalPool.h | 378 +- storagemanager/PoolManager.h | 98 +- storagemanager/StorageAccessor.cpp | 134 +- storagemanager/StorageAccessor.h | 90 +- storagemanager/StorageManagerIF.h | 334 +- subsystem/Subsystem.h | 328 +- subsystem/SubsystemBase.cpp | 712 +-- subsystem/SubsystemBase.h | 258 +- subsystem/modes/HasModeSequenceIF.h | 40 +- subsystem/modes/ModeDefinitions.h | 304 +- subsystem/modes/ModeSequenceMessage.cpp | 178 +- subsystem/modes/ModeSequenceMessage.h | 96 +- subsystem/modes/ModeStore.cpp | 252 +- subsystem/modes/ModeStore.h | 90 +- subsystem/modes/ModeStoreIF.h | 74 +- tasks/ExecutableObjectIF.h | 106 +- tasks/FixedSequenceSlot.cpp | 34 +- tasks/FixedSequenceSlot.h | 114 +- tasks/FixedSlotSequence.cpp | 294 +- tasks/FixedSlotSequence.h | 318 +- tasks/FixedTimeslotTaskIF.h | 66 +- tasks/PeriodicTaskIF.h | 98 +- tasks/SemaphoreFactory.h | 100 +- tasks/SemaphoreIF.h | 136 +- tasks/TaskFactory.h | 146 +- tcdistribution/CCSDSDistributor.cpp | 164 +- tcdistribution/CCSDSDistributor.h | 138 +- tcdistribution/CCSDSDistributorIF.h | 80 +- tcdistribution/PUSDistributor.cpp | 184 +- tcdistribution/PUSDistributor.h | 154 +- tcdistribution/PUSDistributorIF.h | 52 +- tcdistribution/TcDistributor.cpp | 108 +- tcdistribution/TcDistributor.h | 240 +- tcdistribution/TcPacketCheck.cpp | 74 +- tcdistribution/TcPacketCheck.h | 118 +- thermal/AbstractTemperatureSensor.cpp | 140 +- thermal/AbstractTemperatureSensor.h | 128 +- thermal/AcceptsThermalMessagesIF.h | 44 +- thermal/CoreComponent.h | 192 +- thermal/Heater.cpp | 700 +-- thermal/Heater.h | 180 +- thermal/RedundantHeater.h | 100 +- thermal/TemperatureSensor.h | 444 +- thermal/ThermalComponentIF.h | 228 +- thermal/ThermalModule.cpp | 576 +-- thermal/ThermalModule.h | 190 +- thermal/ThermalMonitor.cpp | 136 +- thermal/ThermalMonitor.h | 52 +- timemanager/CCSDSTime.cpp | 1204 ++--- timemanager/CCSDSTime.h | 466 +- timemanager/Clock.h | 310 +- timemanager/Countdown.cpp | 90 +- timemanager/Countdown.h | 62 +- timemanager/ReceivesTimeInfoIF.h | 64 +- timemanager/Stopwatch.cpp | 114 +- timemanager/Stopwatch.h | 142 +- timemanager/TimeMessage.cpp | 74 +- timemanager/TimeMessage.h | 112 +- timemanager/TimeStamperIF.h | 48 +- tmstorage/TmStoreBackendIF.h | 190 +- tmstorage/TmStoreFrontendIF.h | 126 +- tmstorage/TmStoreMessage.cpp | 330 +- tmstorage/TmStoreMessage.h | 126 +- tmstorage/TmStorePackets.h | 600 +-- tmtcpacket/SpacePacket.cpp | 54 +- tmtcpacket/SpacePacket.h | 140 +- tmtcpacket/SpacePacketBase.cpp | 208 +- tmtcpacket/SpacePacketBase.h | 354 +- tmtcpacket/packetmatcher/ApidMatcher.h | 80 +- tmtcpacket/packetmatcher/PacketMatchTree.cpp | 390 +- tmtcpacket/packetmatcher/PacketMatchTree.h | 72 +- tmtcpacket/packetmatcher/ServiceMatcher.h | 78 +- tmtcpacket/packetmatcher/SubserviceMatcher.h | 80 +- tmtcpacket/pus/PacketTimestampInterpreterIF.h | 32 +- tmtcpacket/pus/TcPacketBase.cpp | 186 +- tmtcpacket/pus/TcPacketBase.h | 408 +- tmtcpacket/pus/TcPacketStored.cpp | 236 +- tmtcpacket/pus/TcPacketStored.h | 220 +- tmtcpacket/pus/TmPacketBase.cpp | 238 +- tmtcpacket/pus/TmPacketBase.h | 372 +- tmtcpacket/pus/TmPacketMinimal.cpp | 90 +- tmtcpacket/pus/TmPacketMinimal.h | 168 +- tmtcpacket/pus/TmPacketStored.cpp | 292 +- tmtcpacket/pus/TmPacketStored.h | 192 +- tmtcservices/AcceptsTelecommandsIF.h | 86 +- tmtcservices/AcceptsTelemetryIF.h | 52 +- tmtcservices/AcceptsVerifyMessageIF.h | 30 +- tmtcservices/CommandingServiceBase.cpp | 854 ++-- tmtcservices/CommandingServiceBase.h | 696 +-- tmtcservices/PusServiceBase.cpp | 246 +- tmtcservices/PusServiceBase.h | 318 +- tmtcservices/PusVerificationReport.cpp | 326 +- tmtcservices/PusVerificationReport.h | 154 +- tmtcservices/SourceSequenceCounter.h | 60 +- tmtcservices/TmTcBridge.cpp | 474 +- tmtcservices/TmTcBridge.h | 324 +- tmtcservices/TmTcMessage.cpp | 58 +- tmtcservices/TmTcMessage.h | 100 +- tmtcservices/VerificationReporter.cpp | 210 +- tmtcservices/VerificationReporter.h | 100 +- 381 files changed, 38723 insertions(+), 38723 deletions(-) diff --git a/container/ArrayList.h b/container/ArrayList.h index 436dc716..9f5e76de 100644 --- a/container/ArrayList.h +++ b/container/ArrayList.h @@ -1,247 +1,247 @@ -#ifndef FRAMEWORK_CONTAINER_ARRAYLIST_H_ -#define FRAMEWORK_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); - - /** - * 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; - - /** - * 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) { - } - - /** - * 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; - } - - T *operator->() { - return value; - } - - const T *operator->() const{ - return value; - } - - //SHOULDDO this should be implemented as non-member - bool operator==(const typename ArrayList::Iterator& other) const{ - return (value == other.value); - } - - //SHOULDDO this should be implemented as non-member - bool operator!=(const typename ArrayList::Iterator& other) const { - return !(*this == other); - } - }; - - /** - * 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 - */ - uint32_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 - */ - uint32_t maxSize_; - - /** - * true if the array was allocated and needs to be deleted in the destructor. - */ - bool allocated; -}; - -#endif /* ARRAYLIST_H_ */ +#ifndef FRAMEWORK_CONTAINER_ARRAYLIST_H_ +#define FRAMEWORK_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); + + /** + * 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; + + /** + * 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) { + } + + /** + * 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; + } + + T *operator->() { + return value; + } + + const T *operator->() const{ + return value; + } + + //SHOULDDO this should be implemented as non-member + bool operator==(const typename ArrayList::Iterator& other) const{ + return (value == other.value); + } + + //SHOULDDO this should be implemented as non-member + bool operator!=(const typename ArrayList::Iterator& other) const { + return !(*this == other); + } + }; + + /** + * 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 + */ + uint32_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 + */ + uint32_t maxSize_; + + /** + * true if the array was allocated and needs to be deleted in the destructor. + */ + bool allocated; +}; + +#endif /* ARRAYLIST_H_ */ diff --git a/container/DynamicFIFO.h b/container/DynamicFIFO.h index b63cee65..9af98f96 100644 --- a/container/DynamicFIFO.h +++ b/container/DynamicFIFO.h @@ -1,42 +1,42 @@ -#ifndef FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ -#define FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ - -#include "../container/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->setData(fifoVector.data()); - }; - - /** - * @brief Custom copy constructor which prevents setting the - * underlying pointer wrong. - */ - DynamicFIFO(const DynamicFIFO& other): FIFOBase(other), - fifoVector(other.maxCapacity) { - this->setData(fifoVector.data()); - } - - -private: - std::vector fifoVector; -}; - -#endif /* FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ */ +#ifndef FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ +#define FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ + +#include "../container/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->setData(fifoVector.data()); + }; + + /** + * @brief Custom copy constructor which prevents setting the + * underlying pointer wrong. + */ + DynamicFIFO(const DynamicFIFO& other): FIFOBase(other), + fifoVector(other.maxCapacity) { + this->setData(fifoVector.data()); + } + + +private: + std::vector fifoVector; +}; + +#endif /* FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ */ diff --git a/container/FIFO.h b/container/FIFO.h index a870e773..4688a7b0 100644 --- a/container/FIFO.h +++ b/container/FIFO.h @@ -1,34 +1,34 @@ -#ifndef FRAMEWORK_CONTAINER_FIFO_H_ -#define FRAMEWORK_CONTAINER_FIFO_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../container/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(fifoArray.data(), capacity) {}; - - /** - * @brief Custom copy constructor to set pointer correctly. - * @param other - */ - FIFO(const FIFO& other): FIFOBase(other) { - this->setData(fifoArray.data()); - } - -private: - std::array fifoArray; -}; - -#endif /* FRAMEWORK_CONTAINERS_STATICFIFO_H_ */ +#ifndef FRAMEWORK_CONTAINER_FIFO_H_ +#define FRAMEWORK_CONTAINER_FIFO_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../container/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(fifoArray.data(), capacity) {}; + + /** + * @brief Custom copy constructor to set pointer correctly. + * @param other + */ + FIFO(const FIFO& other): FIFOBase(other) { + this->setData(fifoArray.data()); + } + +private: + std::array fifoArray; +}; + +#endif /* FRAMEWORK_CONTAINERS_STATICFIFO_H_ */ diff --git a/container/FIFOBase.h b/container/FIFOBase.h index ddccbdd4..410e52a5 100644 --- a/container/FIFOBase.h +++ b/container/FIFOBase.h @@ -1,65 +1,65 @@ -#ifndef FRAMEWORK_CONTAINER_FIFOBASE_H_ -#define FRAMEWORK_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 - */ - ReturnValue_t insert(T value); - /** - * Retrieve item from FIFO. This removes the item from the FIFO. - * @param value - * @return - */ - ReturnValue_t retrieve(T *value); - /** - * Retrieve item from FIFO without removing it from FIFO. - * @param value - * @return - */ - ReturnValue_t peek(T * value); - /** - * Remove item from FIFO. - * @return - */ - ReturnValue_t pop(); - - bool empty(); - bool full(); - size_t size(); - - - size_t getMaxCapacity() const; - -protected: - void setData(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 "../container/FIFOBase.tpp" - -#endif /* FRAMEWORK_CONTAINER_FIFOBASE_H_ */ +#ifndef FRAMEWORK_CONTAINER_FIFOBASE_H_ +#define FRAMEWORK_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 + */ + ReturnValue_t insert(T value); + /** + * Retrieve item from FIFO. This removes the item from the FIFO. + * @param value + * @return + */ + ReturnValue_t retrieve(T *value); + /** + * Retrieve item from FIFO without removing it from FIFO. + * @param value + * @return + */ + ReturnValue_t peek(T * value); + /** + * Remove item from FIFO. + * @return + */ + ReturnValue_t pop(); + + bool empty(); + bool full(); + size_t size(); + + + size_t getMaxCapacity() const; + +protected: + void setData(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 "../container/FIFOBase.tpp" + +#endif /* FRAMEWORK_CONTAINER_FIFOBASE_H_ */ diff --git a/container/FixedArrayList.h b/container/FixedArrayList.h index 70a1d5e2..da85c623 100644 --- a/container/FixedArrayList.h +++ b/container/FixedArrayList.h @@ -1,57 +1,57 @@ -#ifndef FIXEDARRAYLIST_H_ -#define FIXEDARRAYLIST_H_ - -#include "../container/ArrayList.h" - -/** - * @brief Array List with a fixed maximum size - * @ingroup container - */ -template -class FixedArrayList: public ArrayList { -private: - T data[MAX_SIZE]; -public: - /** - * (Robin) Maybe we should also implement move assignment and move ctor. - * Or at least delete them. - */ - FixedArrayList() : - ArrayList(data, MAX_SIZE) { - } - - // (Robin): We could create a constructor to initialize the fixed array list - // with data and the known size field - // so it can be used for serialization too (with SerialFixedArrrayListAdapter) - // is this feasible? - /** - * Initialize a fixed array list with data and number of data fields. - * Endianness of entries can be swapped optionally. - * @param data_ - * @param count - * @param swapArrayListEndianess - */ - FixedArrayList(T * data_, count_t count): - ArrayList(data, MAX_SIZE) { - memcpy(this->data, data_, count * sizeof(T)); - this->size = count; - } - - FixedArrayList(const FixedArrayList& other) : - ArrayList(data, MAX_SIZE) { - memcpy(this->data, other.data, sizeof(this->data)); - this->entries = data; - } - - FixedArrayList& operator=(FixedArrayList other) { - memcpy(this->data, other.data, sizeof(this->data)); - this->entries = data; - return *this; - } - - virtual ~FixedArrayList() { - } - -}; - -#endif /* FIXEDARRAYLIST_H_ */ +#ifndef FIXEDARRAYLIST_H_ +#define FIXEDARRAYLIST_H_ + +#include "../container/ArrayList.h" + +/** + * @brief Array List with a fixed maximum size + * @ingroup container + */ +template +class FixedArrayList: public ArrayList { +private: + T data[MAX_SIZE]; +public: + /** + * (Robin) Maybe we should also implement move assignment and move ctor. + * Or at least delete them. + */ + FixedArrayList() : + ArrayList(data, MAX_SIZE) { + } + + // (Robin): We could create a constructor to initialize the fixed array list + // with data and the known size field + // so it can be used for serialization too (with SerialFixedArrrayListAdapter) + // is this feasible? + /** + * Initialize a fixed array list with data and number of data fields. + * Endianness of entries can be swapped optionally. + * @param data_ + * @param count + * @param swapArrayListEndianess + */ + FixedArrayList(T * data_, count_t count): + ArrayList(data, MAX_SIZE) { + memcpy(this->data, data_, count * sizeof(T)); + this->size = count; + } + + FixedArrayList(const FixedArrayList& other) : + ArrayList(data, MAX_SIZE) { + memcpy(this->data, other.data, sizeof(this->data)); + this->entries = data; + } + + FixedArrayList& operator=(FixedArrayList other) { + memcpy(this->data, other.data, sizeof(this->data)); + this->entries = data; + return *this; + } + + virtual ~FixedArrayList() { + } + +}; + +#endif /* FIXEDARRAYLIST_H_ */ diff --git a/container/FixedMap.h b/container/FixedMap.h index a3afc023..3da65784 100644 --- a/container/FixedMap.h +++ b/container/FixedMap.h @@ -1,225 +1,225 @@ -#ifndef FIXEDMAP_H_ -#define FIXEDMAP_H_ - -#include "../container/ArrayList.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#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). - * @ingroup container - */ -template -class FixedMap: public SerializeIF { -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) { - } - - T operator*() { - return ArrayList, uint32_t>::Iterator::value->second; - } - - // -> operator overloaded, can be used to access value - T *operator->() { - return &ArrayList, uint32_t>::Iterator::value->second; - } - - // Can be used to access the key of the iterator - key_t first() { - return ArrayList, uint32_t>::Iterator::value->first; - } - - // Alternative to access value, similar to std::map implementation - T second() { - return ArrayList, uint32_t>::Iterator::value->second; - } - }; - - - - 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 = NULL) { - if (exists(key) == HasReturnvaluesIF::RETURN_OK) { - return FixedMap::KEY_ALREADY_EXISTS; - } - if (_size == theMap.maxSize()) { - return FixedMap::MAP_FULL; - } - theMap[_size].first = key; - theMap[_size].second = value; - if (storedValue != NULL) { - *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; - } - - void clear() { - _size = 0; - } - - uint32_t maxSize() const { - return theMap.maxSize(); - } - - - bool full() { - if(_size == theMap.maxSize()) { - return true; - } - else { - return false; - } - } - - 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 /* FIXEDMAP_H_ */ +#ifndef FIXEDMAP_H_ +#define FIXEDMAP_H_ + +#include "../container/ArrayList.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#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). + * @ingroup container + */ +template +class FixedMap: public SerializeIF { +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) { + } + + T operator*() { + return ArrayList, uint32_t>::Iterator::value->second; + } + + // -> operator overloaded, can be used to access value + T *operator->() { + return &ArrayList, uint32_t>::Iterator::value->second; + } + + // Can be used to access the key of the iterator + key_t first() { + return ArrayList, uint32_t>::Iterator::value->first; + } + + // Alternative to access value, similar to std::map implementation + T second() { + return ArrayList, uint32_t>::Iterator::value->second; + } + }; + + + + 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 = NULL) { + if (exists(key) == HasReturnvaluesIF::RETURN_OK) { + return FixedMap::KEY_ALREADY_EXISTS; + } + if (_size == theMap.maxSize()) { + return FixedMap::MAP_FULL; + } + theMap[_size].first = key; + theMap[_size].second = value; + if (storedValue != NULL) { + *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; + } + + void clear() { + _size = 0; + } + + uint32_t maxSize() const { + return theMap.maxSize(); + } + + + bool full() { + if(_size == theMap.maxSize()) { + return true; + } + else { + return false; + } + } + + 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 /* FIXEDMAP_H_ */ diff --git a/container/HybridIterator.h b/container/HybridIterator.h index 11881326..630bae28 100644 --- a/container/HybridIterator.h +++ b/container/HybridIterator.h @@ -1,90 +1,90 @@ -#ifndef FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_ -#define FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_ - -#include "../container/ArrayList.h" -#include "../container/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_ */ +#ifndef FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_ +#define FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_ + +#include "../container/ArrayList.h" +#include "../container/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/container/IndexedRingMemoryArray.h b/container/IndexedRingMemoryArray.h index 282916e1..0968abfa 100644 --- a/container/IndexedRingMemoryArray.h +++ b/container/IndexedRingMemoryArray.h @@ -1,721 +1,721 @@ -#ifndef FRAMEWORK_CONTAINER_INDEXEDRINGMEMORY_H_ -#define FRAMEWORK_CONTAINER_INDEXEDRINGMEMORY_H_ - -#include "../container/ArrayList.h" -#include "../globalfunctions/CRC.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../serialize/SerialArrayListAdapter.h" -#include - -/** - * Index is the Type used for the list of indices. - * - * @tparam T Type which destribes the index. Needs to be a child of SerializeIF - * to be able to make it persistent - */ -template -class Index: public SerializeIF{ - /** - * - */ - 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; -}; - - -/** - * @brief Indexed Ring Memory Array is a class for a ring memory with indices. - * @details - * 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 - * @tparam T - */ -template -class IndexedRingMemoryArray: public SerializeIF, public ArrayList, uint32_t>{ - /** - * - */ -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 - size_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 - * @param it - */ - 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 { - - size_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_ */ +#ifndef FRAMEWORK_CONTAINER_INDEXEDRINGMEMORY_H_ +#define FRAMEWORK_CONTAINER_INDEXEDRINGMEMORY_H_ + +#include "../container/ArrayList.h" +#include "../globalfunctions/CRC.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerialArrayListAdapter.h" +#include + +/** + * Index is the Type used for the list of indices. + * + * @tparam T Type which destribes the index. Needs to be a child of SerializeIF + * to be able to make it persistent + */ +template +class Index: public SerializeIF{ + /** + * + */ + 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; +}; + + +/** + * @brief Indexed Ring Memory Array is a class for a ring memory with indices. + * @details + * 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 + * @tparam T + */ +template +class IndexedRingMemoryArray: public SerializeIF, public ArrayList, uint32_t>{ + /** + * + */ +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 + size_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 + * @param it + */ + 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 { + + size_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/container/PlacementFactory.h b/container/PlacementFactory.h index 4a1b7df2..05538478 100644 --- a/container/PlacementFactory.h +++ b/container/PlacementFactory.h @@ -1,35 +1,35 @@ -#ifndef FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_ -#define FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_ - -#include "../storagemanager/StorageManagerIF.h" -#include - -class PlacementFactory { -public: - PlacementFactory(StorageManagerIF* backend) : - dataBackend(backend) { - } - template - T* generate(Args&&... args) { - store_address_t tempId; - uint8_t* pData = NULL; - ReturnValue_t result = dataBackend->getFreeElement(&tempId, sizeof(T), - &pData); - if (result != HasReturnvaluesIF::RETURN_OK) { - return NULL; - } - T* temp = new (pData) T(std::forward(args)...); - return temp; - } - template - ReturnValue_t destroy(T* thisElement) { - //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_ */ +#ifndef FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_ +#define FRAMEWORK_CONTAINER_PLACEMENTFACTORY_H_ + +#include "../storagemanager/StorageManagerIF.h" +#include + +class PlacementFactory { +public: + PlacementFactory(StorageManagerIF* backend) : + dataBackend(backend) { + } + template + T* generate(Args&&... args) { + store_address_t tempId; + uint8_t* pData = NULL; + ReturnValue_t result = dataBackend->getFreeElement(&tempId, sizeof(T), + &pData); + if (result != HasReturnvaluesIF::RETURN_OK) { + return NULL; + } + T* temp = new (pData) T(std::forward(args)...); + return temp; + } + template + ReturnValue_t destroy(T* thisElement) { + //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/container/RingBufferBase.h b/container/RingBufferBase.h index 0e784631..243a2915 100644 --- a/container/RingBufferBase.h +++ b/container/RingBufferBase.h @@ -1,113 +1,113 @@ -#ifndef FRAMEWORK_CONTAINER_RINGBUFFERBASE_H_ -#define FRAMEWORK_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 /* FRAMEWORK_CONTAINER_RINGBUFFERBASE_H_ */ +#ifndef FRAMEWORK_CONTAINER_RINGBUFFERBASE_H_ +#define FRAMEWORK_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 /* FRAMEWORK_CONTAINER_RINGBUFFERBASE_H_ */ diff --git a/container/SharedRingBuffer.cpp b/container/SharedRingBuffer.cpp index c41e8d30..490988e5 100644 --- a/container/SharedRingBuffer.cpp +++ b/container/SharedRingBuffer.cpp @@ -1,30 +1,30 @@ -#include "../container/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(); -} - -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; -} +#include "../container/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(); +} + +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; +} diff --git a/container/SharedRingBuffer.h b/container/SharedRingBuffer.h index c6d958cc..394dcaaa 100644 --- a/container/SharedRingBuffer.h +++ b/container/SharedRingBuffer.h @@ -1,68 +1,68 @@ -#ifndef FRAMEWORK_CONTAINER_SHAREDRINGBUFFER_H_ -#define FRAMEWORK_CONTAINER_SHAREDRINGBUFFER_H_ - -#include "../container/SimpleRingBuffer.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); - - /** - * 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; -private: - MutexIF* mutex = nullptr; -}; - - - -#endif /* FRAMEWORK_CONTAINER_SHAREDRINGBUFFER_H_ */ +#ifndef FRAMEWORK_CONTAINER_SHAREDRINGBUFFER_H_ +#define FRAMEWORK_CONTAINER_SHAREDRINGBUFFER_H_ + +#include "../container/SimpleRingBuffer.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); + + /** + * 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; +private: + MutexIF* mutex = nullptr; +}; + + + +#endif /* FRAMEWORK_CONTAINER_SHAREDRINGBUFFER_H_ */ diff --git a/container/SimpleRingBuffer.cpp b/container/SimpleRingBuffer.cpp index 7ed019b2..d6b6f5af 100644 --- a/container/SimpleRingBuffer.cpp +++ b/container/SimpleRingBuffer.cpp @@ -1,135 +1,135 @@ -#include "../container/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; -} - - +#include "../container/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/container/SimpleRingBuffer.h b/container/SimpleRingBuffer.h index b13e9f2a..dcf2684b 100644 --- a/container/SimpleRingBuffer.h +++ b/container/SimpleRingBuffer.h @@ -1,128 +1,128 @@ -#ifndef FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ -#define FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ - -#include "../container/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 /* FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ */ - +#ifndef FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ +#define FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ + +#include "../container/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 /* FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ */ + diff --git a/contrib/sgp4/sgp4unit.cpp b/contrib/sgp4/sgp4unit.cpp index 24d63aa9..b707a7a2 100644 --- a/contrib/sgp4/sgp4unit.cpp +++ b/contrib/sgp4/sgp4unit.cpp @@ -1,2090 +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 - - - - - +/* ---------------------------------------------------------------- +* +* 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/contrib/sgp4/sgp4unit.h b/contrib/sgp4/sgp4unit.h index 48c113d7..f16acbd9 100644 --- a/contrib/sgp4/sgp4unit.h +++ b/contrib/sgp4/sgp4unit.h @@ -1,117 +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 - +#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/controller/ControllerBase.cpp b/controller/ControllerBase.cpp index 2f43b13e..da84ed27 100644 --- a/controller/ControllerBase.cpp +++ b/controller/ControllerBase.cpp @@ -1,140 +1,140 @@ -#include "../subsystem/SubsystemBase.h" -#include "../controller/ControllerBase.h" -#include "../subsystem/SubsystemBase.h" -#include "../ipc/QueueFactory.h" -#include "../action/HasActionsIF.h" - -ControllerBase::ControllerBase(object_id_t setObjectId, object_id_t parentId, - size_t commandQueueDepth) : - SystemObject(setObjectId), parentId(parentId), mode(MODE_OFF), - submode(SUBMODE_NONE), modeHelper(this), - healthHelper(this, setObjectId), hkSwitcher(this) { - 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 != objects::NO_OBJECT) { - 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 command; - ReturnValue_t result; - for (result = commandQueue->receiveMessage(&command); result == RETURN_OK; - result = commandQueue->receiveMessage(&command)) { - - result = modeHelper.handleModeCommand(&command); - if (result == RETURN_OK) { - continue; - } - - result = healthHelper.handleHealthCommand(&command); - if (result == RETURN_OK) { - continue; - } - result = handleCommandMessage(&command); - if (result == RETURN_OK) { - continue; - } - command.setToUnknownCommand(); - commandQueue->reply(&command); - } - -} - -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) { -} - -ReturnValue_t ControllerBase::initializeAfterTaskCreation() { - return HasReturnvaluesIF::RETURN_OK; -} +#include "../subsystem/SubsystemBase.h" +#include "../controller/ControllerBase.h" +#include "../subsystem/SubsystemBase.h" +#include "../ipc/QueueFactory.h" +#include "../action/HasActionsIF.h" + +ControllerBase::ControllerBase(object_id_t setObjectId, object_id_t parentId, + size_t commandQueueDepth) : + SystemObject(setObjectId), parentId(parentId), mode(MODE_OFF), + submode(SUBMODE_NONE), modeHelper(this), + healthHelper(this, setObjectId), hkSwitcher(this) { + 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 != objects::NO_OBJECT) { + 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 command; + ReturnValue_t result; + for (result = commandQueue->receiveMessage(&command); result == RETURN_OK; + result = commandQueue->receiveMessage(&command)) { + + result = modeHelper.handleModeCommand(&command); + if (result == RETURN_OK) { + continue; + } + + result = healthHelper.handleHealthCommand(&command); + if (result == RETURN_OK) { + continue; + } + result = handleCommandMessage(&command); + if (result == RETURN_OK) { + continue; + } + command.setToUnknownCommand(); + commandQueue->reply(&command); + } + +} + +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) { +} + +ReturnValue_t ControllerBase::initializeAfterTaskCreation() { + return HasReturnvaluesIF::RETURN_OK; +} diff --git a/controller/ControllerBase.h b/controller/ControllerBase.h index 5aacd9d8..400c3bb6 100644 --- a/controller/ControllerBase.h +++ b/controller/ControllerBase.h @@ -1,94 +1,94 @@ -#ifndef FSFW_CONTROLLER_CONTROLLERBASE_H_ -#define FSFW_CONTROLLER_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" - -/** - * @brief Generic base class for controller classes - * @details - * Implements common interfaces for controllers. - */ -class ControllerBase: public HasModesIF, - public HasHealthIF, - public ExecutableObjectIF, - public SystemObject, - public HasReturnvaluesIF { -public: - static const Mode_t MODE_NORMAL = 2; - - ControllerBase(object_id_t setObjectId, object_id_t parentId, - size_t commandQueueDepth = 3); - virtual ~ControllerBase(); - - virtual ReturnValue_t initialize() override; - - virtual MessageQueueId_t getCommandQueue() const override; - - virtual ReturnValue_t performOperation(uint8_t opCode) override; - - virtual ReturnValue_t setHealth(HealthState health) override; - - virtual HasHealthIF::HealthState getHealth() 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; - - virtual ReturnValue_t initializeAfterTaskCreation() override; - -protected: - - /** - * Implemented by child class. Handle command messages which are not - * mode or health messages. - * @param message - * @return - */ - 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; - - const object_id_t parentId; - - Mode_t mode; - - Submode_t submode; - - MessageQueueIF* commandQueue = nullptr; - - ModeHelper modeHelper; - - HealthHelper healthHelper; - - HkSwitchHelper hkSwitcher; - - /** - * Pointer to the task which executes this component, - * is invalid before setTaskIF was called. - */ - PeriodicTaskIF* executingTask = nullptr; - - void handleQueue(); - - 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 /* FSFW_CONTROLLER_CONTROLLERBASE_H_ */ +#ifndef FSFW_CONTROLLER_CONTROLLERBASE_H_ +#define FSFW_CONTROLLER_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" + +/** + * @brief Generic base class for controller classes + * @details + * Implements common interfaces for controllers. + */ +class ControllerBase: public HasModesIF, + public HasHealthIF, + public ExecutableObjectIF, + public SystemObject, + public HasReturnvaluesIF { +public: + static const Mode_t MODE_NORMAL = 2; + + ControllerBase(object_id_t setObjectId, object_id_t parentId, + size_t commandQueueDepth = 3); + virtual ~ControllerBase(); + + virtual ReturnValue_t initialize() override; + + virtual MessageQueueId_t getCommandQueue() const override; + + virtual ReturnValue_t performOperation(uint8_t opCode) override; + + virtual ReturnValue_t setHealth(HealthState health) override; + + virtual HasHealthIF::HealthState getHealth() 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; + + virtual ReturnValue_t initializeAfterTaskCreation() override; + +protected: + + /** + * Implemented by child class. Handle command messages which are not + * mode or health messages. + * @param message + * @return + */ + 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; + + const object_id_t parentId; + + Mode_t mode; + + Submode_t submode; + + MessageQueueIF* commandQueue = nullptr; + + ModeHelper modeHelper; + + HealthHelper healthHelper; + + HkSwitchHelper hkSwitcher; + + /** + * Pointer to the task which executes this component, + * is invalid before setTaskIF was called. + */ + PeriodicTaskIF* executingTask = nullptr; + + void handleQueue(); + + 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 /* FSFW_CONTROLLER_CONTROLLERBASE_H_ */ diff --git a/datapool/ControllerSet.cpp b/datapool/ControllerSet.cpp index f83b38d5..4055158b 100644 --- a/datapool/ControllerSet.cpp +++ b/datapool/ControllerSet.cpp @@ -1,14 +1,14 @@ -#include "../datapool/ControllerSet.h" - -ControllerSet::ControllerSet() { - -} - -ControllerSet::~ControllerSet() { -} - -void ControllerSet::setInvalid() { - read(); - setToDefault(); - commit(PoolVariableIF::INVALID); -} +#include "../datapool/ControllerSet.h" + +ControllerSet::ControllerSet() { + +} + +ControllerSet::~ControllerSet() { +} + +void ControllerSet::setInvalid() { + read(); + setToDefault(); + commit(PoolVariableIF::INVALID); +} diff --git a/datapool/ControllerSet.h b/datapool/ControllerSet.h index da6a9bf4..43e1cbf2 100644 --- a/datapool/ControllerSet.h +++ b/datapool/ControllerSet.h @@ -1,15 +1,15 @@ -#ifndef CONTROLLERSET_H_ -#define CONTROLLERSET_H_ - -#include "../datapoolglob/GlobalDataSet.h" - -class ControllerSet :public GlobDataSet { -public: - ControllerSet(); - virtual ~ControllerSet(); - - virtual void setToDefault() = 0; - void setInvalid(); -}; - -#endif /* CONTROLLERSET_H_ */ +#ifndef CONTROLLERSET_H_ +#define CONTROLLERSET_H_ + +#include "../datapoolglob/GlobalDataSet.h" + +class ControllerSet :public GlobDataSet { +public: + ControllerSet(); + virtual ~ControllerSet(); + + virtual void setToDefault() = 0; + void setInvalid(); +}; + +#endif /* CONTROLLERSET_H_ */ diff --git a/datapool/HkSwitchHelper.cpp b/datapool/HkSwitchHelper.cpp index 5a59f94c..1a2a25eb 100644 --- a/datapool/HkSwitchHelper.cpp +++ b/datapool/HkSwitchHelper.cpp @@ -1,75 +1,75 @@ -#include "../datapool/HkSwitchHelper.h" -#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 command; - while (actionQueue->receiveMessage(&command) == HasReturnvaluesIF::RETURN_OK) { - ReturnValue_t result = commandActionHelper.handleReply(&command); - if (result == HasReturnvaluesIF::RETURN_OK) { - continue; - } - command.setToUnknownCommand(); - actionQueue->reply(&command); - } - - 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; -} +#include "../datapool/HkSwitchHelper.h" +#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 command; + while (actionQueue->receiveMessage(&command) == HasReturnvaluesIF::RETURN_OK) { + ReturnValue_t result = commandActionHelper.handleReply(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + continue; + } + command.setToUnknownCommand(); + actionQueue->reply(&command); + } + + 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/datapool/HkSwitchHelper.h b/datapool/HkSwitchHelper.h index de34ab23..385b2047 100644 --- a/datapool/HkSwitchHelper.h +++ b/datapool/HkSwitchHelper.h @@ -1,46 +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_ */ +#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/datapool/PoolEntry.cpp b/datapool/PoolEntry.cpp index a21d521f..b0506b82 100644 --- a/datapool/PoolEntry.cpp +++ b/datapool/PoolEntry.cpp @@ -1,87 +1,87 @@ -#include "../datapool/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; +#include "../datapool/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/datapool/PoolEntry.h b/datapool/PoolEntry.h index 67891af8..6a3daacf 100644 --- a/datapool/PoolEntry.h +++ b/datapool/PoolEntry.h @@ -1,130 +1,130 @@ -#ifndef FRAMEWORK_DATAPOOL_POOLENTRY_H_ -#define FRAMEWORK_DATAPOOL_POOLENTRY_H_ - -#include "../datapool/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_ */ +#ifndef FRAMEWORK_DATAPOOL_POOLENTRY_H_ +#define FRAMEWORK_DATAPOOL_POOLENTRY_H_ + +#include "../datapool/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/datapool/PoolEntryIF.h b/datapool/PoolEntryIF.h index a9fb44ef..462de18b 100644 --- a/datapool/PoolEntryIF.h +++ b/datapool/PoolEntryIF.h @@ -1,63 +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_ */ +#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/datapool/PoolRawAccessHelper.cpp b/datapool/PoolRawAccessHelper.cpp index 272bb502..3696a21d 100644 --- a/datapool/PoolRawAccessHelper.cpp +++ b/datapool/PoolRawAccessHelper.cpp @@ -1,188 +1,188 @@ -/** - * @file PoolRawAccessHelper.cpp - * - * @date 22.12.2019 - * @author R. Mueller - */ - -#include "../datapool/PoolRawAccessHelper.h" -#include "../datapoolglob/GlobalDataSet.h" -#include "../serialize/SerializeAdapter.h" -#include "../serviceinterface/ServiceInterfaceStream.h" - -#include -#include - -PoolRawAccessHelper::PoolRawAccessHelper(uint32_t * poolIdBuffer_, - uint8_t numberOfParameters_): - poolIdBuffer(reinterpret_cast(poolIdBuffer_)), - numberOfParameters(numberOfParameters_), validBufferIndex(0), - validBufferIndexBit(1) { -} - -PoolRawAccessHelper::~PoolRawAccessHelper() { -} - -ReturnValue_t PoolRawAccessHelper::serialize(uint8_t **buffer, size_t *size, - const size_t max_size, SerializeIF::Endianness streamEndianness) { - SerializationArgs serializationArgs = {buffer, size, max_size, - streamEndianness}; - ReturnValue_t result = RETURN_OK; - size_t remainingParametersSize = numberOfParameters * 4; - for(uint8_t count=0; count < numberOfParameters; count++) { - result = serializeCurrentPoolEntryIntoBuffer(serializationArgs, - &remainingParametersSize, false); - if(result != RETURN_OK) { - return result; - } - } - if(remainingParametersSize != 0) { - sif::debug << "PoolRawAccessHelper: " - "Remaining parameters size not 0 !" << std::endl; - result = RETURN_FAILED; - } - return result; -} - -ReturnValue_t PoolRawAccessHelper::serializeWithValidityMask(uint8_t ** buffer, - size_t * size, const size_t max_size, - SerializeIF::Endianness streamEndianness) { - ReturnValue_t result = RETURN_OK; - SerializationArgs argStruct = {buffer, size, max_size, streamEndianness}; - size_t remainingParametersSize = numberOfParameters * 4; - uint8_t validityMaskSize = ceil((float)numberOfParameters/8.0); - uint8_t validityMask[validityMaskSize]; - memset(validityMask,0, validityMaskSize); - for(uint8_t count = 0; count < numberOfParameters; count++) { - result = serializeCurrentPoolEntryIntoBuffer(argStruct, - &remainingParametersSize,true,validityMask); - if (result != RETURN_OK) { - return result; - } - } - if(remainingParametersSize != 0) { - sif::debug << "PoolRawAccessHelper: Remaining " - "parameters size not 0 !" << std::endl; - result = RETURN_FAILED; - } - - memcpy(*argStruct.buffer, validityMask, validityMaskSize); - *size += validityMaskSize; - validBufferIndex = 1; - validBufferIndexBit = 0; - return result; -} - -ReturnValue_t PoolRawAccessHelper::serializeCurrentPoolEntryIntoBuffer( - SerializationArgs argStruct, size_t * remainingParameters, - bool withValidMask, uint8_t * validityMask) { - uint32_t currentPoolId; - // Deserialize current pool ID from pool ID buffer - ReturnValue_t result = SerializeAdapter::deSerialize(¤tPoolId, - &poolIdBuffer,remainingParameters, SerializeIF::Endianness::MACHINE); - if(result != RETURN_OK) { - sif::debug << std::hex << "PoolRawAccessHelper: Error deSeralizing " - "pool IDs" << std::dec << std::endl; - return result; - } - result = handlePoolEntrySerialization(currentPoolId, argStruct, - withValidMask, validityMask); - return result; -} - -ReturnValue_t PoolRawAccessHelper::handlePoolEntrySerialization( - uint32_t currentPoolId,SerializationArgs argStruct, bool withValidMask, - uint8_t * validityMask) { - ReturnValue_t result = RETURN_FAILED; - uint8_t arrayPosition = 0; - uint8_t counter = 0; - bool poolEntrySerialized = false; - //debug << "Pool Raw Access Helper: Handling Pool ID: " - // << std::hex << currentPoolId << std::endl; - while(not poolEntrySerialized) { - - if(counter > GlobDataSet::DATA_SET_MAX_SIZE) { - sif::error << "PoolRawAccessHelper: Config error, " - "max. number of possible data set variables exceeded" - << std::endl; - return result; - } - counter ++; - - GlobDataSet currentDataSet; - //debug << "Current array position: " << (int)arrayPosition << std::endl; - PoolRawAccess currentPoolRawAccess(currentPoolId, arrayPosition, - ¤tDataSet, PoolVariableIF::VAR_READ); - - result = currentDataSet.read(); - if (result != RETURN_OK) { - sif::debug << std::hex << "PoolRawAccessHelper: Error reading raw " - "dataset with returncode 0x" << result << std::dec << std::endl; - return result; - } - - result = checkRemainingSize(¤tPoolRawAccess, &poolEntrySerialized, - &arrayPosition); - if(result != RETURN_OK) { - sif::error << "Pool Raw Access Helper: Configuration Error at pool ID " - << std::hex << currentPoolId - << ". Size till end smaller than 0" << std::dec << std::endl; - return result; - } - - // set valid mask bit if necessary - if(withValidMask) { - if(currentPoolRawAccess.isValid()) { - handleMaskModification(validityMask); - } - validBufferIndexBit ++; - } - - result = currentDataSet.serialize(argStruct.buffer, argStruct.size, - argStruct.max_size, argStruct.streamEndianness); - if (result != RETURN_OK) { - sif::debug << "Pool Raw Access Helper: Error serializing pool data with " - "ID 0x" << std::hex << currentPoolId << " into send buffer " - "with return code " << result << std::dec << std::endl; - return result; - } - - } - return result; -} - -ReturnValue_t PoolRawAccessHelper::checkRemainingSize(PoolRawAccess* - currentPoolRawAccess, bool * isSerialized, uint8_t * arrayPosition) { - int8_t remainingSize = currentPoolRawAccess->getSizeTillEnd() - - currentPoolRawAccess->getSizeOfType(); - if(remainingSize == 0) { - *isSerialized = true; - } - else if(remainingSize > 0) { - *arrayPosition += 1; - } - else { - return RETURN_FAILED; - } - return RETURN_OK; -} - -void PoolRawAccessHelper::handleMaskModification(uint8_t * validityMask) { - validityMask[validBufferIndex] = - bitSetter(validityMask[validBufferIndex], validBufferIndexBit, true); - if(validBufferIndexBit == 8) { - validBufferIndex ++; - validBufferIndexBit = 1; - } -} - -uint8_t PoolRawAccessHelper::bitSetter(uint8_t byte, uint8_t position, - bool value) { - if(position < 1 or position > 8) { - sif::debug << "Pool Raw Access: Bit setting invalid position" << std::endl; - return byte; - } - uint8_t shiftNumber = position + (6 - 2 * (position - 1)); - byte |= 1UL << shiftNumber; - return byte; -} +/** + * @file PoolRawAccessHelper.cpp + * + * @date 22.12.2019 + * @author R. Mueller + */ + +#include "../datapool/PoolRawAccessHelper.h" +#include "../datapoolglob/GlobalDataSet.h" +#include "../serialize/SerializeAdapter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +#include +#include + +PoolRawAccessHelper::PoolRawAccessHelper(uint32_t * poolIdBuffer_, + uint8_t numberOfParameters_): + poolIdBuffer(reinterpret_cast(poolIdBuffer_)), + numberOfParameters(numberOfParameters_), validBufferIndex(0), + validBufferIndexBit(1) { +} + +PoolRawAccessHelper::~PoolRawAccessHelper() { +} + +ReturnValue_t PoolRawAccessHelper::serialize(uint8_t **buffer, size_t *size, + const size_t max_size, SerializeIF::Endianness streamEndianness) { + SerializationArgs serializationArgs = {buffer, size, max_size, + streamEndianness}; + ReturnValue_t result = RETURN_OK; + size_t remainingParametersSize = numberOfParameters * 4; + for(uint8_t count=0; count < numberOfParameters; count++) { + result = serializeCurrentPoolEntryIntoBuffer(serializationArgs, + &remainingParametersSize, false); + if(result != RETURN_OK) { + return result; + } + } + if(remainingParametersSize != 0) { + sif::debug << "PoolRawAccessHelper: " + "Remaining parameters size not 0 !" << std::endl; + result = RETURN_FAILED; + } + return result; +} + +ReturnValue_t PoolRawAccessHelper::serializeWithValidityMask(uint8_t ** buffer, + size_t * size, const size_t max_size, + SerializeIF::Endianness streamEndianness) { + ReturnValue_t result = RETURN_OK; + SerializationArgs argStruct = {buffer, size, max_size, streamEndianness}; + size_t remainingParametersSize = numberOfParameters * 4; + uint8_t validityMaskSize = ceil((float)numberOfParameters/8.0); + uint8_t validityMask[validityMaskSize]; + memset(validityMask,0, validityMaskSize); + for(uint8_t count = 0; count < numberOfParameters; count++) { + result = serializeCurrentPoolEntryIntoBuffer(argStruct, + &remainingParametersSize,true,validityMask); + if (result != RETURN_OK) { + return result; + } + } + if(remainingParametersSize != 0) { + sif::debug << "PoolRawAccessHelper: Remaining " + "parameters size not 0 !" << std::endl; + result = RETURN_FAILED; + } + + memcpy(*argStruct.buffer, validityMask, validityMaskSize); + *size += validityMaskSize; + validBufferIndex = 1; + validBufferIndexBit = 0; + return result; +} + +ReturnValue_t PoolRawAccessHelper::serializeCurrentPoolEntryIntoBuffer( + SerializationArgs argStruct, size_t * remainingParameters, + bool withValidMask, uint8_t * validityMask) { + uint32_t currentPoolId; + // Deserialize current pool ID from pool ID buffer + ReturnValue_t result = SerializeAdapter::deSerialize(¤tPoolId, + &poolIdBuffer,remainingParameters, SerializeIF::Endianness::MACHINE); + if(result != RETURN_OK) { + sif::debug << std::hex << "PoolRawAccessHelper: Error deSeralizing " + "pool IDs" << std::dec << std::endl; + return result; + } + result = handlePoolEntrySerialization(currentPoolId, argStruct, + withValidMask, validityMask); + return result; +} + +ReturnValue_t PoolRawAccessHelper::handlePoolEntrySerialization( + uint32_t currentPoolId,SerializationArgs argStruct, bool withValidMask, + uint8_t * validityMask) { + ReturnValue_t result = RETURN_FAILED; + uint8_t arrayPosition = 0; + uint8_t counter = 0; + bool poolEntrySerialized = false; + //debug << "Pool Raw Access Helper: Handling Pool ID: " + // << std::hex << currentPoolId << std::endl; + while(not poolEntrySerialized) { + + if(counter > GlobDataSet::DATA_SET_MAX_SIZE) { + sif::error << "PoolRawAccessHelper: Config error, " + "max. number of possible data set variables exceeded" + << std::endl; + return result; + } + counter ++; + + GlobDataSet currentDataSet; + //debug << "Current array position: " << (int)arrayPosition << std::endl; + PoolRawAccess currentPoolRawAccess(currentPoolId, arrayPosition, + ¤tDataSet, PoolVariableIF::VAR_READ); + + result = currentDataSet.read(); + if (result != RETURN_OK) { + sif::debug << std::hex << "PoolRawAccessHelper: Error reading raw " + "dataset with returncode 0x" << result << std::dec << std::endl; + return result; + } + + result = checkRemainingSize(¤tPoolRawAccess, &poolEntrySerialized, + &arrayPosition); + if(result != RETURN_OK) { + sif::error << "Pool Raw Access Helper: Configuration Error at pool ID " + << std::hex << currentPoolId + << ". Size till end smaller than 0" << std::dec << std::endl; + return result; + } + + // set valid mask bit if necessary + if(withValidMask) { + if(currentPoolRawAccess.isValid()) { + handleMaskModification(validityMask); + } + validBufferIndexBit ++; + } + + result = currentDataSet.serialize(argStruct.buffer, argStruct.size, + argStruct.max_size, argStruct.streamEndianness); + if (result != RETURN_OK) { + sif::debug << "Pool Raw Access Helper: Error serializing pool data with " + "ID 0x" << std::hex << currentPoolId << " into send buffer " + "with return code " << result << std::dec << std::endl; + return result; + } + + } + return result; +} + +ReturnValue_t PoolRawAccessHelper::checkRemainingSize(PoolRawAccess* + currentPoolRawAccess, bool * isSerialized, uint8_t * arrayPosition) { + int8_t remainingSize = currentPoolRawAccess->getSizeTillEnd() - + currentPoolRawAccess->getSizeOfType(); + if(remainingSize == 0) { + *isSerialized = true; + } + else if(remainingSize > 0) { + *arrayPosition += 1; + } + else { + return RETURN_FAILED; + } + return RETURN_OK; +} + +void PoolRawAccessHelper::handleMaskModification(uint8_t * validityMask) { + validityMask[validBufferIndex] = + bitSetter(validityMask[validBufferIndex], validBufferIndexBit, true); + if(validBufferIndexBit == 8) { + validBufferIndex ++; + validBufferIndexBit = 1; + } +} + +uint8_t PoolRawAccessHelper::bitSetter(uint8_t byte, uint8_t position, + bool value) { + if(position < 1 or position > 8) { + sif::debug << "Pool Raw Access: Bit setting invalid position" << std::endl; + return byte; + } + uint8_t shiftNumber = position + (6 - 2 * (position - 1)); + byte |= 1UL << shiftNumber; + return byte; +} diff --git a/datapool/PoolRawAccessHelper.h b/datapool/PoolRawAccessHelper.h index a09f7a2f..db25acdf 100644 --- a/datapool/PoolRawAccessHelper.h +++ b/datapool/PoolRawAccessHelper.h @@ -1,111 +1,111 @@ -/** - * @file PoolRawAccessHelper.h - * - * @date 22.12.2019 - */ - -#ifndef FRAMEWORK_DATAPOOL_POOLRAWACCESSHELPER_H_ -#define FRAMEWORK_DATAPOOL_POOLRAWACCESSHELPER_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/PoolRawAccess.h" - -/** - * @brief This helper function simplifies accessing data pool entries - * via PoolRawAccess - * @details Can be used for a Housekeeping Service - * like ECSS PUS Service 3 if the type of the datapool entries is unknown. - * The provided dataset can be serialized into a provided buffer automatically by - * providing a buffer of pool IDs - * @ingroup data_pool - */ -class PoolRawAccessHelper: public HasReturnvaluesIF { -public: - /** - * Call this constructor if a dataset needs to be serialized via - * Pool Raw Access - * @param dataSet_ This dataset will be used to perform thread-safe reading - * @param poolIdBuffer_ A buffer of uint32_t pool IDs - * @param numberOfParameters_ The number of parameters / pool IDs - */ - PoolRawAccessHelper(uint32_t * poolIdBuffer_, uint8_t numberOfParameters_); - virtual ~PoolRawAccessHelper(); - - /** - * Serialize the datapool entries derived from the pool ID buffer - * directly into a provided buffer - * @param [out] buffer - * @param [out] size Size of the serialized buffer - * @param max_size - * @param bigEndian - * @return @c RETURN_OK On success - * @c RETURN_FAILED on failure - */ - ReturnValue_t serialize(uint8_t ** buffer, size_t * size, - const size_t max_size, SerializeIF::Endianness streamEndianness); - - /** - * Serializes data pool entries into provided buffer with the validity mask buffer - * at the end of the buffer. Every bit of the validity mask denotes - * the validity of a corresponding data pool entry from left to right. - * @param [out] buffer - * @param [out] size Size of the serialized buffer plus size - * of the validity mask - * @return @c RETURN_OK On success - * @c RETURN_FAILED on failure - */ - ReturnValue_t serializeWithValidityMask(uint8_t ** buffer, size_t * size, - const size_t max_size, SerializeIF::Endianness streamEndianness); - - -private: - // DataSet * dataSet; - const uint8_t * poolIdBuffer; - uint8_t numberOfParameters; - - uint8_t validBufferIndex; - uint8_t validBufferIndexBit; - - struct SerializationArgs { - uint8_t ** buffer; - size_t * size; - const size_t max_size; - SerializeIF::Endianness streamEndianness; - }; - /** - * Helper function to serialize single pool entries - * @param pPoolIdBuffer - * @param buffer - * @param remainingParameters - * @param hkDataSize - * @param max_size - * @param bigEndian - * @param withValidMask Can be set optionally to set a - * provided validity mask - * @param validityMask Can be supplied and will be set if - * @c withValidMask is set to true - * @return - */ - ReturnValue_t serializeCurrentPoolEntryIntoBuffer( - SerializationArgs argStruct, size_t * remainingParameters, - bool withValidMask = false, uint8_t * validityMask = nullptr); - - ReturnValue_t handlePoolEntrySerialization(uint32_t currentPoolId, - SerializationArgs argStruct, bool withValidMask = false, - uint8_t * validityMask = nullptr); - - ReturnValue_t checkRemainingSize(PoolRawAccess * currentPoolRawAccess, - bool * isSerialized, uint8_t * arrayPosition); - void handleMaskModification(uint8_t * validityMask); - /** - * Sets specific bit of a byte - * @param byte - * @param position Position of byte to set from 1 to 8 - * @param value Binary value to set - * @return - */ - uint8_t bitSetter(uint8_t byte, uint8_t position, bool value); -}; - -#endif /* FRAMEWORK_DATAPOOL_POOLRAWACCESSHELPER_H_ */ +/** + * @file PoolRawAccessHelper.h + * + * @date 22.12.2019 + */ + +#ifndef FRAMEWORK_DATAPOOL_POOLRAWACCESSHELPER_H_ +#define FRAMEWORK_DATAPOOL_POOLRAWACCESSHELPER_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/PoolRawAccess.h" + +/** + * @brief This helper function simplifies accessing data pool entries + * via PoolRawAccess + * @details Can be used for a Housekeeping Service + * like ECSS PUS Service 3 if the type of the datapool entries is unknown. + * The provided dataset can be serialized into a provided buffer automatically by + * providing a buffer of pool IDs + * @ingroup data_pool + */ +class PoolRawAccessHelper: public HasReturnvaluesIF { +public: + /** + * Call this constructor if a dataset needs to be serialized via + * Pool Raw Access + * @param dataSet_ This dataset will be used to perform thread-safe reading + * @param poolIdBuffer_ A buffer of uint32_t pool IDs + * @param numberOfParameters_ The number of parameters / pool IDs + */ + PoolRawAccessHelper(uint32_t * poolIdBuffer_, uint8_t numberOfParameters_); + virtual ~PoolRawAccessHelper(); + + /** + * Serialize the datapool entries derived from the pool ID buffer + * directly into a provided buffer + * @param [out] buffer + * @param [out] size Size of the serialized buffer + * @param max_size + * @param bigEndian + * @return @c RETURN_OK On success + * @c RETURN_FAILED on failure + */ + ReturnValue_t serialize(uint8_t ** buffer, size_t * size, + const size_t max_size, SerializeIF::Endianness streamEndianness); + + /** + * Serializes data pool entries into provided buffer with the validity mask buffer + * at the end of the buffer. Every bit of the validity mask denotes + * the validity of a corresponding data pool entry from left to right. + * @param [out] buffer + * @param [out] size Size of the serialized buffer plus size + * of the validity mask + * @return @c RETURN_OK On success + * @c RETURN_FAILED on failure + */ + ReturnValue_t serializeWithValidityMask(uint8_t ** buffer, size_t * size, + const size_t max_size, SerializeIF::Endianness streamEndianness); + + +private: + // DataSet * dataSet; + const uint8_t * poolIdBuffer; + uint8_t numberOfParameters; + + uint8_t validBufferIndex; + uint8_t validBufferIndexBit; + + struct SerializationArgs { + uint8_t ** buffer; + size_t * size; + const size_t max_size; + SerializeIF::Endianness streamEndianness; + }; + /** + * Helper function to serialize single pool entries + * @param pPoolIdBuffer + * @param buffer + * @param remainingParameters + * @param hkDataSize + * @param max_size + * @param bigEndian + * @param withValidMask Can be set optionally to set a + * provided validity mask + * @param validityMask Can be supplied and will be set if + * @c withValidMask is set to true + * @return + */ + ReturnValue_t serializeCurrentPoolEntryIntoBuffer( + SerializationArgs argStruct, size_t * remainingParameters, + bool withValidMask = false, uint8_t * validityMask = nullptr); + + ReturnValue_t handlePoolEntrySerialization(uint32_t currentPoolId, + SerializationArgs argStruct, bool withValidMask = false, + uint8_t * validityMask = nullptr); + + ReturnValue_t checkRemainingSize(PoolRawAccess * currentPoolRawAccess, + bool * isSerialized, uint8_t * arrayPosition); + void handleMaskModification(uint8_t * validityMask); + /** + * Sets specific bit of a byte + * @param byte + * @param position Position of byte to set from 1 to 8 + * @param value Binary value to set + * @return + */ + uint8_t bitSetter(uint8_t byte, uint8_t position, bool value); +}; + +#endif /* FRAMEWORK_DATAPOOL_POOLRAWACCESSHELPER_H_ */ diff --git a/datapool/PoolVarList.h b/datapool/PoolVarList.h index 10c0e074..1c8a7be4 100644 --- a/datapool/PoolVarList.h +++ b/datapool/PoolVarList.h @@ -1,28 +1,28 @@ -#ifndef POOLVARLIST_H_ -#define POOLVARLIST_H_ - -#include "../datapool/PoolVariableIF.h" -#include "../datapoolglob/GlobalPoolVariable.h" -template -class PoolVarList { -private: - GlobPoolVar 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]); - } - } - - GlobPoolVar &operator [](int i) { return variables[i]; } -}; - - - -#endif /* POOLVARLIST_H_ */ +#ifndef POOLVARLIST_H_ +#define POOLVARLIST_H_ + +#include "../datapool/PoolVariableIF.h" +#include "../datapoolglob/GlobalPoolVariable.h" +template +class PoolVarList { +private: + GlobPoolVar 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]); + } + } + + GlobPoolVar &operator [](int i) { return variables[i]; } +}; + + + +#endif /* POOLVARLIST_H_ */ diff --git a/datapoolglob/DataPoolAdmin.cpp b/datapoolglob/DataPoolAdmin.cpp index 669bcc00..029a336d 100644 --- a/datapoolglob/DataPoolAdmin.cpp +++ b/datapoolglob/DataPoolAdmin.cpp @@ -1,300 +1,300 @@ -#include "../datapoolglob/DataPoolAdmin.h" -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/GlobalDataPool.h" -#include "../datapoolglob/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 = glob::dataPool.PIDToDataPoolId(address); - - GlobDataSet 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 = glob::dataPool.PIDToDataPoolId(address); - uint8_t arrayIndex = glob::dataPool.PIDToArrayIndex(address); - GlobDataSet 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) { - GlobDataSet 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 = glob::dataPool.PIDToDataPoolId(address); - uint8_t arrayIndex = glob::dataPool.PIDToArrayIndex(address); - GlobDataSet 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) { - GlobDataSet 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); -} +#include "../datapoolglob/DataPoolAdmin.h" +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/GlobalDataPool.h" +#include "../datapoolglob/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 = glob::dataPool.PIDToDataPoolId(address); + + GlobDataSet 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 = glob::dataPool.PIDToDataPoolId(address); + uint8_t arrayIndex = glob::dataPool.PIDToArrayIndex(address); + GlobDataSet 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) { + GlobDataSet 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 = glob::dataPool.PIDToDataPoolId(address); + uint8_t arrayIndex = glob::dataPool.PIDToArrayIndex(address); + GlobDataSet 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) { + GlobDataSet 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/datapoolglob/DataPoolAdmin.h b/datapoolglob/DataPoolAdmin.h index 3d5d4d0c..a08f322d 100644 --- a/datapoolglob/DataPoolAdmin.h +++ b/datapoolglob/DataPoolAdmin.h @@ -1,59 +1,59 @@ -#ifndef DATAPOOLADMIN_H_ -#define DATAPOOLADMIN_H_ - -#include "../objectmanager/SystemObject.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../tasks/ExecutableObjectIF.h" -#include "../action/HasActionsIF.h" -#include "../ipc/MessageQueueIF.h" -#include "../parameters/ReceivesParameterMessagesIF.h" - -#include "../memory/MemoryHelper.h" -#include "../action/SimpleActionHelper.h" -#include "../datapoolglob/DataPoolParameterWrapper.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_ */ +#ifndef DATAPOOLADMIN_H_ +#define DATAPOOLADMIN_H_ + +#include "../objectmanager/SystemObject.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../action/HasActionsIF.h" +#include "../ipc/MessageQueueIF.h" +#include "../parameters/ReceivesParameterMessagesIF.h" + +#include "../memory/MemoryHelper.h" +#include "../action/SimpleActionHelper.h" +#include "../datapoolglob/DataPoolParameterWrapper.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/datapoolglob/DataPoolParameterWrapper.cpp b/datapoolglob/DataPoolParameterWrapper.cpp index 597cc86b..3a57d90d 100644 --- a/datapoolglob/DataPoolParameterWrapper.cpp +++ b/datapoolglob/DataPoolParameterWrapper.cpp @@ -1,179 +1,179 @@ -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/DataPoolParameterWrapper.h" -#include "../datapoolglob/PoolRawAccess.h" -#include "../parameters/HasParametersIF.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; - - GlobDataSet 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++){ - GlobDataSet 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++) { - - GlobDataSet 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; -} +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/DataPoolParameterWrapper.h" +#include "../datapoolglob/PoolRawAccess.h" +#include "../parameters/HasParametersIF.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; + + GlobDataSet 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++){ + GlobDataSet 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++) { + + GlobDataSet 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/datapoolglob/DataPoolParameterWrapper.h b/datapoolglob/DataPoolParameterWrapper.h index 0c5c65ea..b1e505a7 100644 --- a/datapoolglob/DataPoolParameterWrapper.h +++ b/datapoolglob/DataPoolParameterWrapper.h @@ -1,38 +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_ */ +#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/datapoolglob/GlobalDataPool.cpp b/datapoolglob/GlobalDataPool.cpp index aa6831f8..afb27b77 100644 --- a/datapoolglob/GlobalDataPool.cpp +++ b/datapoolglob/GlobalDataPool.cpp @@ -1,133 +1,133 @@ -#include "../datapoolglob/GlobalDataPool.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../ipc/MutexFactory.h" - -GlobalDataPool::GlobalDataPool( - void(*initFunction)(GlobPoolMap* pool_map)) { - mutex = MutexFactory::instance()->createMutex(); - if (initFunction != NULL ) { - initFunction( &this->globDataPool ); - } -} - -GlobalDataPool::~GlobalDataPool() { - MutexFactory::instance()->deleteMutex(mutex); - for(GlobPoolMapIter it = this->globDataPool.begin(); - it != this->globDataPool.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* GlobalDataPool::getData( uint32_t data_pool_id, - uint8_t sizeOrPosition ) { - GlobPoolMapIter it = this->globDataPool.find( data_pool_id ); - if ( it != this->globDataPool.end() ) { - PoolEntry* entry = dynamic_cast< PoolEntry* >( it->second ); - if (entry != nullptr ) { - if ( sizeOrPosition <= entry->length ) { - return entry; - } - } - } - return nullptr; -} - -PoolEntryIF* GlobalDataPool::getRawData( uint32_t data_pool_id ) { - GlobPoolMapIter it = this->globDataPool.find( data_pool_id ); - if ( it != this->globDataPool.end() ) { - return it->second; - } else { - return nullptr; - } -} - -ReturnValue_t GlobalDataPool::unlockDataPool() { - 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 GlobalDataPool::lockDataPool(uint32_t timeoutMs) { - ReturnValue_t status = mutex->lockMutex(MutexIF::TimeoutType::WAITING, - timeoutMs); - if(status != RETURN_OK) { - sif::error << "DataPool::DataPool: lock of mutex failed " - "with error code: " << status << std::endl; - } - return status; -} - -void GlobalDataPool::print() { - sif::debug << "DataPool contains: " << std::endl; - std::map::iterator dataPoolIt; - dataPoolIt = this->globDataPool.begin(); - while( dataPoolIt != this->globDataPool.end() ) { - sif::debug << std::hex << dataPoolIt->first << std::dec << " |"; - dataPoolIt->second->print(); - dataPoolIt++; - } -} - -uint32_t GlobalDataPool::PIDToDataPoolId(uint32_t parameter_id) { - return (parameter_id >> 8) & 0x00FFFFFF; -} - -uint8_t GlobalDataPool::PIDToArrayIndex(uint32_t parameter_id) { - return (parameter_id & 0x000000FF); -} - -uint32_t GlobalDataPool::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 GlobalDataPool::getType(uint32_t parameter_id, Type* type) { - GlobPoolMapIter it = this->globDataPool.find( PIDToDataPoolId(parameter_id)); - if ( it != this->globDataPool.end() ) { - *type = it->second->getType(); - return RETURN_OK; - } else { - *type = Type::UNKNOWN_TYPE; - return RETURN_FAILED; - } -} - -bool GlobalDataPool::exists(uint32_t parameterId) { - uint32_t poolId = PIDToDataPoolId(parameterId); - uint32_t index = PIDToArrayIndex(parameterId); - GlobPoolMapIter it = this->globDataPool.find( poolId ); - if (it != globDataPool.end()) { - if (it->second->getSize() >= index) { - return true; - } - } - return false; -} - -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size ); -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size ); -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size ); -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size); -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size ); -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size ); -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size ); -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size ); -template PoolEntry* GlobalDataPool::getData( - uint32_t data_pool_id, uint8_t size); +#include "../datapoolglob/GlobalDataPool.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../ipc/MutexFactory.h" + +GlobalDataPool::GlobalDataPool( + void(*initFunction)(GlobPoolMap* pool_map)) { + mutex = MutexFactory::instance()->createMutex(); + if (initFunction != NULL ) { + initFunction( &this->globDataPool ); + } +} + +GlobalDataPool::~GlobalDataPool() { + MutexFactory::instance()->deleteMutex(mutex); + for(GlobPoolMapIter it = this->globDataPool.begin(); + it != this->globDataPool.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* GlobalDataPool::getData( uint32_t data_pool_id, + uint8_t sizeOrPosition ) { + GlobPoolMapIter it = this->globDataPool.find( data_pool_id ); + if ( it != this->globDataPool.end() ) { + PoolEntry* entry = dynamic_cast< PoolEntry* >( it->second ); + if (entry != nullptr ) { + if ( sizeOrPosition <= entry->length ) { + return entry; + } + } + } + return nullptr; +} + +PoolEntryIF* GlobalDataPool::getRawData( uint32_t data_pool_id ) { + GlobPoolMapIter it = this->globDataPool.find( data_pool_id ); + if ( it != this->globDataPool.end() ) { + return it->second; + } else { + return nullptr; + } +} + +ReturnValue_t GlobalDataPool::unlockDataPool() { + 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 GlobalDataPool::lockDataPool(uint32_t timeoutMs) { + ReturnValue_t status = mutex->lockMutex(MutexIF::TimeoutType::WAITING, + timeoutMs); + if(status != RETURN_OK) { + sif::error << "DataPool::DataPool: lock of mutex failed " + "with error code: " << status << std::endl; + } + return status; +} + +void GlobalDataPool::print() { + sif::debug << "DataPool contains: " << std::endl; + std::map::iterator dataPoolIt; + dataPoolIt = this->globDataPool.begin(); + while( dataPoolIt != this->globDataPool.end() ) { + sif::debug << std::hex << dataPoolIt->first << std::dec << " |"; + dataPoolIt->second->print(); + dataPoolIt++; + } +} + +uint32_t GlobalDataPool::PIDToDataPoolId(uint32_t parameter_id) { + return (parameter_id >> 8) & 0x00FFFFFF; +} + +uint8_t GlobalDataPool::PIDToArrayIndex(uint32_t parameter_id) { + return (parameter_id & 0x000000FF); +} + +uint32_t GlobalDataPool::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 GlobalDataPool::getType(uint32_t parameter_id, Type* type) { + GlobPoolMapIter it = this->globDataPool.find( PIDToDataPoolId(parameter_id)); + if ( it != this->globDataPool.end() ) { + *type = it->second->getType(); + return RETURN_OK; + } else { + *type = Type::UNKNOWN_TYPE; + return RETURN_FAILED; + } +} + +bool GlobalDataPool::exists(uint32_t parameterId) { + uint32_t poolId = PIDToDataPoolId(parameterId); + uint32_t index = PIDToArrayIndex(parameterId); + GlobPoolMapIter it = this->globDataPool.find( poolId ); + if (it != globDataPool.end()) { + if (it->second->getSize() >= index) { + return true; + } + } + return false; +} + +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size ); +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size ); +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size ); +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size); +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size ); +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size ); +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size ); +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size ); +template PoolEntry* GlobalDataPool::getData( + uint32_t data_pool_id, uint8_t size); diff --git a/datapoolglob/GlobalDataPool.h b/datapoolglob/GlobalDataPool.h index 37a4057b..ce5b132c 100644 --- a/datapoolglob/GlobalDataPool.h +++ b/datapoolglob/GlobalDataPool.h @@ -1,149 +1,149 @@ -#ifndef GLOBALDATAPOOL_H_ -#define GLOBALDATAPOOL_H_ - -#include "../datapool/PoolEntry.h" -#include "../globalfunctions/Type.h" -#include "../ipc/MutexIF.h" -#include - -/** - * @defgroup data_pool Global data pool - * This is the group, where all classes associated with global - * data pool handling belong to. - * This includes classes to access Data Pool variables. - */ - -/** - * Typedefs for the global pool representations - */ -using GlobPoolMap = std::map; -using GlobPoolMapIter = GlobPoolMap::iterator; - -/** - * @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. - * @author Bastian Baetz - * @ingroup data_pool - */ -class GlobalDataPool : 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. - */ - GlobPoolMap globDataPool; - - /** - * @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; -public: - /** - * @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. - */ - GlobalDataPool( void ( *initFunction )( GlobPoolMap* pool_map ) ); - - /** - * @brief The destructor iterates through the data_pool map and - * calls all entries destructors to clean up the heap. - */ - ~GlobalDataPool(); - - /** - * @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(uint32_t timeoutMs = MutexIF::BLOCKING); - /** - * @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 unlockDataPool(); - /** - * @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. -namespace glob { -extern GlobalDataPool dataPool; -} - -#endif /* DATAPOOL_H_ */ +#ifndef GLOBALDATAPOOL_H_ +#define GLOBALDATAPOOL_H_ + +#include "../datapool/PoolEntry.h" +#include "../globalfunctions/Type.h" +#include "../ipc/MutexIF.h" +#include + +/** + * @defgroup data_pool Global data pool + * This is the group, where all classes associated with global + * data pool handling belong to. + * This includes classes to access Data Pool variables. + */ + +/** + * Typedefs for the global pool representations + */ +using GlobPoolMap = std::map; +using GlobPoolMapIter = GlobPoolMap::iterator; + +/** + * @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. + * @author Bastian Baetz + * @ingroup data_pool + */ +class GlobalDataPool : 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. + */ + GlobPoolMap globDataPool; + + /** + * @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; +public: + /** + * @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. + */ + GlobalDataPool( void ( *initFunction )( GlobPoolMap* pool_map ) ); + + /** + * @brief The destructor iterates through the data_pool map and + * calls all entries destructors to clean up the heap. + */ + ~GlobalDataPool(); + + /** + * @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(uint32_t timeoutMs = MutexIF::BLOCKING); + /** + * @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 unlockDataPool(); + /** + * @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. +namespace glob { +extern GlobalDataPool dataPool; +} + +#endif /* DATAPOOL_H_ */ diff --git a/datapoolglob/GlobalPoolVariable.h b/datapoolglob/GlobalPoolVariable.h index f5fc49f8..a995bfaf 100644 --- a/datapoolglob/GlobalPoolVariable.h +++ b/datapoolglob/GlobalPoolVariable.h @@ -1,213 +1,213 @@ -#ifndef GLOBALPOOLVARIABLE_H_ -#define GLOBALPOOLVARIABLE_H_ - -#include "../datapool/DataSetIF.h" -#include "../datapoolglob/GlobalDataPool.h" -#include "../datapool/PoolVariableIF.h" -#include "../datapool/PoolEntry.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 GlobPoolVar: public PoolVariableIF { - template friend class PoolVarList; - static_assert(not std::is_same::value, - "Do not use boolean for the PoolEntry type, use uint8_t instead!" - "There is no boolean type in CCSDS."); -public: - /** - * @brief In the constructor, the variable can register itself in a - * DataSet (if nullptr is not 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. - */ - GlobPoolVar(uint32_t set_id, DataSetIF* dataSet, - ReadWriteMode_t setReadWriteMode); - - /** - * @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 = 0; - - /** - * @brief Copy ctor to copy classes containing Pool Variables. - * (Robin): This only copies member variables, which is done - * by the default copy ctor. maybe we can ommit this ctor? - */ - GlobPoolVar(const GlobPoolVar& rhs); - - /** - * @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. - */ - ~GlobPoolVar() {} - - /** - * @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 read call is protected with a lock. - * It is recommended to use DataSets to read and commit multiple variables - * at once to avoid the overhead of unnecessary lock und unlock operations. - */ - ReturnValue_t read(uint32_t lockTimeout) override; - /** - * @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. - * The commit call is protected with a lock. - * It is recommended to use DataSets to read and commit multiple variables - * at once to avoid the overhead of unnecessary lock und unlock operations. - */ - ReturnValue_t commit(uint32_t lockTimeout) override; - -protected: - /** - * @brief Like #read, but without a lock protection of the global pool. - * @details - * The operation does NOT provide any mutual exclusive protection by itself. - * This can be used if the lock is handled externally to avoid the overhead - * of consecutive lock und unlock operations. - * Declared protected to discourage free public usage. - */ - ReturnValue_t readWithoutLock() override; - /** - * @brief Like #commit, but without a lock protection of the global pool. - * @details - * The operation does NOT provide any mutual exclusive protection by itself. - * This can be used if the lock is handled externally to avoid the overhead - * of consecutive lock und unlock operations. - * Declared protected to discourage free public usage. - */ - ReturnValue_t commitWithoutLock() override; - /** - * @brief To access the correct data pool entry on read and commit calls, - * the data pool 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. - */ - pool_rwm_t readWriteMode; - - /** - * Empty ctor for List initialization - */ - GlobPoolVar(); -public: - /** - * \brief This operation returns the data pool id of the variable. - */ - uint32_t getDataPoolId() const override; - - /** - * This method returns if the variable is write-only, read-write or read-only. - */ - ReadWriteMode_t getReadWriteMode() const override; - /** - * 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); - - /** - * \brief With this call, the valid information of the variable is returned. - */ - bool isValid() const override; - - uint8_t getValid(); - - void setValid(bool valid) override; - - operator T() { - return value; - } - - operator T() const { - return value; - } - - GlobPoolVar &operator=(T newValue) { - value = newValue; - return *this; - } - - GlobPoolVar &operator=(GlobPoolVar newPoolVariable) { - value = newPoolVariable.value; - return *this; - } - - virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, - const size_t max_size, - SerializeIF::Endianness streamEndianness) const override { - return SerializeAdapter::serialize(&value, buffer, size, max_size, - streamEndianness); - } - - virtual size_t getSerializedSize() const { - return SerializeAdapter::getSerializedSize(&value); - } - - virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, - SerializeIF::Endianness streamEndianness) { - return SerializeAdapter::deSerialize(&value, buffer, size, - streamEndianness); - } -}; - -#include "../datapoolglob/GlobalPoolVariable.tpp" - -typedef GlobPoolVar gp_bool_t; -typedef GlobPoolVar gp_uint8_t; -typedef GlobPoolVar gp_uint16_t; -typedef GlobPoolVar gp_uint32_t; -typedef GlobPoolVar gp_int8_t; -typedef GlobPoolVar gp_int16_t; -typedef GlobPoolVar gp_int32_t; -typedef GlobPoolVar gp_float_t; -typedef GlobPoolVar gp_double_t; - -#endif /* POOLVARIABLE_H_ */ +#ifndef GLOBALPOOLVARIABLE_H_ +#define GLOBALPOOLVARIABLE_H_ + +#include "../datapool/DataSetIF.h" +#include "../datapoolglob/GlobalDataPool.h" +#include "../datapool/PoolVariableIF.h" +#include "../datapool/PoolEntry.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 GlobPoolVar: public PoolVariableIF { + template friend class PoolVarList; + static_assert(not std::is_same::value, + "Do not use boolean for the PoolEntry type, use uint8_t instead!" + "There is no boolean type in CCSDS."); +public: + /** + * @brief In the constructor, the variable can register itself in a + * DataSet (if nullptr is not 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. + */ + GlobPoolVar(uint32_t set_id, DataSetIF* dataSet, + ReadWriteMode_t setReadWriteMode); + + /** + * @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 = 0; + + /** + * @brief Copy ctor to copy classes containing Pool Variables. + * (Robin): This only copies member variables, which is done + * by the default copy ctor. maybe we can ommit this ctor? + */ + GlobPoolVar(const GlobPoolVar& rhs); + + /** + * @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. + */ + ~GlobPoolVar() {} + + /** + * @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 read call is protected with a lock. + * It is recommended to use DataSets to read and commit multiple variables + * at once to avoid the overhead of unnecessary lock und unlock operations. + */ + ReturnValue_t read(uint32_t lockTimeout) override; + /** + * @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. + * The commit call is protected with a lock. + * It is recommended to use DataSets to read and commit multiple variables + * at once to avoid the overhead of unnecessary lock und unlock operations. + */ + ReturnValue_t commit(uint32_t lockTimeout) override; + +protected: + /** + * @brief Like #read, but without a lock protection of the global pool. + * @details + * The operation does NOT provide any mutual exclusive protection by itself. + * This can be used if the lock is handled externally to avoid the overhead + * of consecutive lock und unlock operations. + * Declared protected to discourage free public usage. + */ + ReturnValue_t readWithoutLock() override; + /** + * @brief Like #commit, but without a lock protection of the global pool. + * @details + * The operation does NOT provide any mutual exclusive protection by itself. + * This can be used if the lock is handled externally to avoid the overhead + * of consecutive lock und unlock operations. + * Declared protected to discourage free public usage. + */ + ReturnValue_t commitWithoutLock() override; + /** + * @brief To access the correct data pool entry on read and commit calls, + * the data pool 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. + */ + pool_rwm_t readWriteMode; + + /** + * Empty ctor for List initialization + */ + GlobPoolVar(); +public: + /** + * \brief This operation returns the data pool id of the variable. + */ + uint32_t getDataPoolId() const override; + + /** + * This method returns if the variable is write-only, read-write or read-only. + */ + ReadWriteMode_t getReadWriteMode() const override; + /** + * 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); + + /** + * \brief With this call, the valid information of the variable is returned. + */ + bool isValid() const override; + + uint8_t getValid(); + + void setValid(bool valid) override; + + operator T() { + return value; + } + + operator T() const { + return value; + } + + GlobPoolVar &operator=(T newValue) { + value = newValue; + return *this; + } + + GlobPoolVar &operator=(GlobPoolVar newPoolVariable) { + value = newPoolVariable.value; + return *this; + } + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + const size_t max_size, + SerializeIF::Endianness streamEndianness) const override { + return SerializeAdapter::serialize(&value, buffer, size, max_size, + streamEndianness); + } + + virtual size_t getSerializedSize() const { + return SerializeAdapter::getSerializedSize(&value); + } + + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + SerializeIF::Endianness streamEndianness) { + return SerializeAdapter::deSerialize(&value, buffer, size, + streamEndianness); + } +}; + +#include "../datapoolglob/GlobalPoolVariable.tpp" + +typedef GlobPoolVar gp_bool_t; +typedef GlobPoolVar gp_uint8_t; +typedef GlobPoolVar gp_uint16_t; +typedef GlobPoolVar gp_uint32_t; +typedef GlobPoolVar gp_int8_t; +typedef GlobPoolVar gp_int16_t; +typedef GlobPoolVar gp_int32_t; +typedef GlobPoolVar gp_float_t; +typedef GlobPoolVar gp_double_t; + +#endif /* POOLVARIABLE_H_ */ diff --git a/datapoolglob/GlobalPoolVector.h b/datapoolglob/GlobalPoolVector.h index eb40d1f1..6b592fc6 100644 --- a/datapoolglob/GlobalPoolVector.h +++ b/datapoolglob/GlobalPoolVector.h @@ -1,185 +1,185 @@ -#ifndef GLOBALPOOLVECTOR_H_ -#define GLOBALPOOLVECTOR_H_ - -#include "../datapool/DataSetIF.h" -#include "../datapool/PoolEntry.h" -#include "../datapool/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 GlobPoolVector: public PoolVariableIF { -public: - /** - * @brief In the constructor, the variable can register itself in a - * DataSet (if no nullptr 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 nullptr, - * 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. - */ - GlobPoolVector(uint32_t set_id, DataSetIF* set, - ReadWriteMode_t setReadWriteMode); - - /** - * @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 local array of this type. - */ - T value[vectorSize]; - /** - * @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. - */ - ~GlobPoolVector() {}; - /** - * @brief The operation returns the number of array entries - * in this variable. - */ - uint8_t getSize() { - return vectorSize; - } - /** - * @brief This operation returns the data pool id of the variable. - */ - uint32_t getDataPoolId() const { - return dataPoolId; - } - /** - * @brief This operation sets the data pool id of the variable. - * @details - * 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(bool 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];} - - virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, - size_t max_size, Endianness streamEndianness) const override; - virtual size_t getSerializedSize() const override; - virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, - Endianness streamEndianness) override; - - /** - * @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 read call is protected by a lock of the global data pool. - * It is recommended to use DataSets to read and commit multiple variables - * at once to avoid the overhead of unnecessary lock und unlock operations. - */ - ReturnValue_t read(uint32_t lockTimeout = MutexIF::BLOCKING) override; - /** - * @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 commit call is protected by a lock of the global data pool. - * It is recommended to use DataSets to read and commit multiple variables - * at once to avoid the overhead of unnecessary lock und unlock operations. - */ - ReturnValue_t commit(uint32_t lockTimeout = MutexIF::BLOCKING) override; - -protected: - /** - * @brief Like #read, but without a lock protection of the global pool. - * @details - * The operation does NOT provide any mutual exclusive protection by itself. - * This can be used if the lock is handled externally to avoid the overhead - * of consecutive lock und unlock operations. - * Declared protected to discourage free public usage. - */ - ReturnValue_t readWithoutLock() override; - /** - * @brief Like #commit, but without a lock protection of the global pool. - * @details - * The operation does NOT provide any mutual exclusive protection by itself. - * This can be used if the lock is handled externally to avoid the overhead - * of consecutive lock und unlock operations. - * Declared protected to discourage free public usage. - */ - ReturnValue_t commitWithoutLock() override; - -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; -}; - -#include "../datapoolglob/GlobalPoolVector.tpp" - -template -using gp_vec_t = GlobPoolVector; - -#endif /* POOLVECTOR_H_ */ +#ifndef GLOBALPOOLVECTOR_H_ +#define GLOBALPOOLVECTOR_H_ + +#include "../datapool/DataSetIF.h" +#include "../datapool/PoolEntry.h" +#include "../datapool/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 GlobPoolVector: public PoolVariableIF { +public: + /** + * @brief In the constructor, the variable can register itself in a + * DataSet (if no nullptr 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 nullptr, + * 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. + */ + GlobPoolVector(uint32_t set_id, DataSetIF* set, + ReadWriteMode_t setReadWriteMode); + + /** + * @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 local array of this type. + */ + T value[vectorSize]; + /** + * @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. + */ + ~GlobPoolVector() {}; + /** + * @brief The operation returns the number of array entries + * in this variable. + */ + uint8_t getSize() { + return vectorSize; + } + /** + * @brief This operation returns the data pool id of the variable. + */ + uint32_t getDataPoolId() const { + return dataPoolId; + } + /** + * @brief This operation sets the data pool id of the variable. + * @details + * 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(bool 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];} + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + size_t max_size, Endianness streamEndianness) const override; + virtual size_t getSerializedSize() const override; + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) override; + + /** + * @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 read call is protected by a lock of the global data pool. + * It is recommended to use DataSets to read and commit multiple variables + * at once to avoid the overhead of unnecessary lock und unlock operations. + */ + ReturnValue_t read(uint32_t lockTimeout = MutexIF::BLOCKING) override; + /** + * @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 commit call is protected by a lock of the global data pool. + * It is recommended to use DataSets to read and commit multiple variables + * at once to avoid the overhead of unnecessary lock und unlock operations. + */ + ReturnValue_t commit(uint32_t lockTimeout = MutexIF::BLOCKING) override; + +protected: + /** + * @brief Like #read, but without a lock protection of the global pool. + * @details + * The operation does NOT provide any mutual exclusive protection by itself. + * This can be used if the lock is handled externally to avoid the overhead + * of consecutive lock und unlock operations. + * Declared protected to discourage free public usage. + */ + ReturnValue_t readWithoutLock() override; + /** + * @brief Like #commit, but without a lock protection of the global pool. + * @details + * The operation does NOT provide any mutual exclusive protection by itself. + * This can be used if the lock is handled externally to avoid the overhead + * of consecutive lock und unlock operations. + * Declared protected to discourage free public usage. + */ + ReturnValue_t commitWithoutLock() override; + +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; +}; + +#include "../datapoolglob/GlobalPoolVector.tpp" + +template +using gp_vec_t = GlobPoolVector; + +#endif /* POOLVECTOR_H_ */ diff --git a/datapoolglob/PIDReader.h b/datapoolglob/PIDReader.h index 7d12a0fd..9431e1d4 100644 --- a/datapoolglob/PIDReader.h +++ b/datapoolglob/PIDReader.h @@ -1,164 +1,164 @@ -#ifndef PIDREADER_H_ -#define PIDREADER_H_ -#include "../datapool/DataSetIF.h" -#include "../datapoolglob/GlobalDataPool.h" -#include "../datapool/PoolEntry.h" -#include "../datapool/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 readWithoutLock() { - uint8_t arrayIndex = GlobalDataPool::PIDToArrayIndex(parameterId); - PoolEntry *read_out = glob::dataPool.getData( - GlobalDataPool::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(uint32_t lockTimeout) override { - return HasReturnvaluesIF::RETURN_FAILED; - } - ReturnValue_t commitWithoutLock() override { - 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); - } - } - - ReturnValue_t read(uint32_t lockTimeout) override { - ReturnValue_t result = glob::dataPool.lockDataPool(); - if(result != HasReturnvaluesIF::RETURN_OK) { - return result; - } - result = readWithoutLock(); - ReturnValue_t unlockResult = glob::dataPool.unlockDataPool(); - if(unlockResult != HasReturnvaluesIF::RETURN_OK) { - sif::error << "PIDReader::read: Could not unlock data pool!" - << std::endl; - } - return result; - } - /** - * 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 GlobalDataPool::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(bool 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_ */ +#ifndef PIDREADER_H_ +#define PIDREADER_H_ +#include "../datapool/DataSetIF.h" +#include "../datapoolglob/GlobalDataPool.h" +#include "../datapool/PoolEntry.h" +#include "../datapool/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 readWithoutLock() { + uint8_t arrayIndex = GlobalDataPool::PIDToArrayIndex(parameterId); + PoolEntry *read_out = glob::dataPool.getData( + GlobalDataPool::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(uint32_t lockTimeout) override { + return HasReturnvaluesIF::RETURN_FAILED; + } + ReturnValue_t commitWithoutLock() override { + 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); + } + } + + ReturnValue_t read(uint32_t lockTimeout) override { + ReturnValue_t result = glob::dataPool.lockDataPool(); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = readWithoutLock(); + ReturnValue_t unlockResult = glob::dataPool.unlockDataPool(); + if(unlockResult != HasReturnvaluesIF::RETURN_OK) { + sif::error << "PIDReader::read: Could not unlock data pool!" + << std::endl; + } + return result; + } + /** + * 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 GlobalDataPool::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(bool 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/datapoolglob/PIDReaderList.h b/datapoolglob/PIDReaderList.h index b167feaa..ae99f3aa 100644 --- a/datapoolglob/PIDReaderList.h +++ b/datapoolglob/PIDReaderList.h @@ -1,27 +1,27 @@ -#ifndef FRAMEWORK_DATAPOOLGLOB_PIDREADERLIST_H_ -#define FRAMEWORK_DATAPOOLGLOB_PIDREADERLIST_H_ - -#include "../datapool/PoolVariableIF.h" -#include "../datapoolglob/PIDReader.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_DATAPOOLGLOB_PIDREADERLIST_H_ */ +#ifndef FRAMEWORK_DATAPOOLGLOB_PIDREADERLIST_H_ +#define FRAMEWORK_DATAPOOLGLOB_PIDREADERLIST_H_ + +#include "../datapool/PoolVariableIF.h" +#include "../datapoolglob/PIDReader.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_DATAPOOLGLOB_PIDREADERLIST_H_ */ diff --git a/datapoolglob/PoolRawAccess.cpp b/datapoolglob/PoolRawAccess.cpp index 09093cd6..53706c6d 100644 --- a/datapoolglob/PoolRawAccess.cpp +++ b/datapoolglob/PoolRawAccess.cpp @@ -1,239 +1,239 @@ -#include "../datapoolglob/GlobalDataPool.h" -#include "../datapoolglob/PoolRawAccess.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../serialize/EndianConverter.h" - -#include - -PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry, - DataSetIF* dataSet, 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 (dataSet != nullptr) { - dataSet->registerVariable(this); - } -} - -PoolRawAccess::~PoolRawAccess() {} - -ReturnValue_t PoolRawAccess::read(uint32_t lockTimeout) { - ReturnValue_t result = glob::dataPool.lockDataPool(lockTimeout); - if(result != HasReturnvaluesIF::RETURN_OK) { - return result; - } - result = readWithoutLock(); - ReturnValue_t unlockResult = glob::dataPool.unlockDataPool(); - if(unlockResult != HasReturnvaluesIF::RETURN_OK) { - sif::error << "GlobPoolVar::read: Could not unlock global data pool" - << std::endl; - } - return result; -} - -ReturnValue_t PoolRawAccess::readWithoutLock() { - ReturnValue_t result = RETURN_FAILED; - PoolEntryIF* readOut = glob::dataPool.getRawData(dataPoolId); - if (readOut != nullptr) { - result = handleReadOut(readOut); - if(result == RETURN_OK) { - return result; - } - } else { - result = READ_ENTRY_NON_EXISTENT; - } - handleReadError(result); - return result; -} - -ReturnValue_t PoolRawAccess::handleReadOut(PoolEntryIF* readOut) { - ReturnValue_t result = RETURN_FAILED; - valid = readOut->getValid(); - if (readOut->getSize() > arrayEntry) { - arraySize = readOut->getSize(); - typeSize = readOut->getByteSize() / readOut->getSize(); - type = readOut->getType(); - if (typeSize <= sizeof(value)) { - uint16_t arrayPosition = arrayEntry * typeSize; - sizeTillEnd = readOut->getByteSize() - arrayPosition; - uint8_t* ptr = &((uint8_t*) readOut->getRawData())[arrayPosition]; - memcpy(value, ptr, typeSize); - return RETURN_OK; - } else { - result = READ_TYPE_TOO_LARGE; - } - } else { - //debug << "PoolRawAccess: Size: " << (int)read_out->getSize() << std::endl; - result = READ_INDEX_TOO_LARGE; - } - return result; -} - -void PoolRawAccess::handleReadError(ReturnValue_t result) { - sif::error << "PoolRawAccess: read of DP Variable 0x" << std::hex << dataPoolId - << std::dec << " failed, "; - if(result == READ_TYPE_TOO_LARGE) { - sif::error << "type too large." << std::endl; - } - else if(result == READ_INDEX_TOO_LARGE) { - sif::error << "index too large." << std::endl; - } - else if(result == READ_ENTRY_NON_EXISTENT) { - sif::error << "entry does not exist." << std::endl; - } - - valid = INVALID; - typeSize = 0; - sizeTillEnd = 0; - memset(value, 0, sizeof(value)); -} - -ReturnValue_t PoolRawAccess::commit(uint32_t lockTimeout) { - ReturnValue_t result = glob::dataPool.lockDataPool(lockTimeout); - if(result != HasReturnvaluesIF::RETURN_OK) { - return result; - } - result = commitWithoutLock(); - ReturnValue_t unlockResult = glob::dataPool.unlockDataPool(); - if(unlockResult != HasReturnvaluesIF::RETURN_OK) { - sif::error << "GlobPoolVar::read: Could not unlock global data pool" - << std::endl; - } - return result; -} - -ReturnValue_t PoolRawAccess::commitWithoutLock() { - PoolEntryIF* write_back = glob::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 max_size) { - 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 > max_size) - return INCORRECT_SIZE; - EndianConverter::convertBigEndian(buffer, data_ptr, typeSize); - *writtenBytes = typeSize; - return HasReturnvaluesIF::RETURN_OK; -} - - -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; - case(Endianness::MACHINE): - default: - memcpy(*buffer, value, typeSize); - break; - } - *size += typeSize; - (*buffer) += typeSize; - return HasReturnvaluesIF::RETURN_OK; - } else { - return SerializeIF::BUFFER_TOO_SHORT; - } -} - - -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(bool valid) { - this->valid = valid; -} - -size_t PoolRawAccess::getSizeTillEnd() const { - return sizeTillEnd; -} - - -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; - case(Endianness::MACHINE): - default: - memcpy(value, *buffer, typeSize); - break; - } - *size -= typeSize; - *buffer += typeSize; - return HasReturnvaluesIF::RETURN_OK; - } - else { - return SerializeIF::STREAM_TOO_SHORT; - } -} +#include "../datapoolglob/GlobalDataPool.h" +#include "../datapoolglob/PoolRawAccess.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../serialize/EndianConverter.h" + +#include + +PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry, + DataSetIF* dataSet, 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 (dataSet != nullptr) { + dataSet->registerVariable(this); + } +} + +PoolRawAccess::~PoolRawAccess() {} + +ReturnValue_t PoolRawAccess::read(uint32_t lockTimeout) { + ReturnValue_t result = glob::dataPool.lockDataPool(lockTimeout); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = readWithoutLock(); + ReturnValue_t unlockResult = glob::dataPool.unlockDataPool(); + if(unlockResult != HasReturnvaluesIF::RETURN_OK) { + sif::error << "GlobPoolVar::read: Could not unlock global data pool" + << std::endl; + } + return result; +} + +ReturnValue_t PoolRawAccess::readWithoutLock() { + ReturnValue_t result = RETURN_FAILED; + PoolEntryIF* readOut = glob::dataPool.getRawData(dataPoolId); + if (readOut != nullptr) { + result = handleReadOut(readOut); + if(result == RETURN_OK) { + return result; + } + } else { + result = READ_ENTRY_NON_EXISTENT; + } + handleReadError(result); + return result; +} + +ReturnValue_t PoolRawAccess::handleReadOut(PoolEntryIF* readOut) { + ReturnValue_t result = RETURN_FAILED; + valid = readOut->getValid(); + if (readOut->getSize() > arrayEntry) { + arraySize = readOut->getSize(); + typeSize = readOut->getByteSize() / readOut->getSize(); + type = readOut->getType(); + if (typeSize <= sizeof(value)) { + uint16_t arrayPosition = arrayEntry * typeSize; + sizeTillEnd = readOut->getByteSize() - arrayPosition; + uint8_t* ptr = &((uint8_t*) readOut->getRawData())[arrayPosition]; + memcpy(value, ptr, typeSize); + return RETURN_OK; + } else { + result = READ_TYPE_TOO_LARGE; + } + } else { + //debug << "PoolRawAccess: Size: " << (int)read_out->getSize() << std::endl; + result = READ_INDEX_TOO_LARGE; + } + return result; +} + +void PoolRawAccess::handleReadError(ReturnValue_t result) { + sif::error << "PoolRawAccess: read of DP Variable 0x" << std::hex << dataPoolId + << std::dec << " failed, "; + if(result == READ_TYPE_TOO_LARGE) { + sif::error << "type too large." << std::endl; + } + else if(result == READ_INDEX_TOO_LARGE) { + sif::error << "index too large." << std::endl; + } + else if(result == READ_ENTRY_NON_EXISTENT) { + sif::error << "entry does not exist." << std::endl; + } + + valid = INVALID; + typeSize = 0; + sizeTillEnd = 0; + memset(value, 0, sizeof(value)); +} + +ReturnValue_t PoolRawAccess::commit(uint32_t lockTimeout) { + ReturnValue_t result = glob::dataPool.lockDataPool(lockTimeout); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = commitWithoutLock(); + ReturnValue_t unlockResult = glob::dataPool.unlockDataPool(); + if(unlockResult != HasReturnvaluesIF::RETURN_OK) { + sif::error << "GlobPoolVar::read: Could not unlock global data pool" + << std::endl; + } + return result; +} + +ReturnValue_t PoolRawAccess::commitWithoutLock() { + PoolEntryIF* write_back = glob::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 max_size) { + 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 > max_size) + return INCORRECT_SIZE; + EndianConverter::convertBigEndian(buffer, data_ptr, typeSize); + *writtenBytes = typeSize; + return HasReturnvaluesIF::RETURN_OK; +} + + +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; + case(Endianness::MACHINE): + default: + memcpy(*buffer, value, typeSize); + break; + } + *size += typeSize; + (*buffer) += typeSize; + return HasReturnvaluesIF::RETURN_OK; + } else { + return SerializeIF::BUFFER_TOO_SHORT; + } +} + + +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(bool valid) { + this->valid = valid; +} + +size_t PoolRawAccess::getSizeTillEnd() const { + return sizeTillEnd; +} + + +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; + case(Endianness::MACHINE): + default: + memcpy(value, *buffer, typeSize); + break; + } + *size -= typeSize; + *buffer += typeSize; + return HasReturnvaluesIF::RETURN_OK; + } + else { + return SerializeIF::STREAM_TOO_SHORT; + } +} diff --git a/datapoolglob/PoolRawAccess.h b/datapoolglob/PoolRawAccess.h index a1bed466..15643a41 100644 --- a/datapoolglob/PoolRawAccess.h +++ b/datapoolglob/PoolRawAccess.h @@ -1,220 +1,220 @@ -#ifndef POOLRAWACCESS_H_ -#define POOLRAWACCESS_H_ - -#include "../datapool/DataSetIF.h" -#include "../datapool/PoolEntryIF.h" -#include "../datapool/PoolVariableIF.h" -#include "../globalfunctions/Type.h" - -/** - * @brief This class allows accessing Data Pool variables as raw bytes. - * @details - * This is necessary to have an access method for HK data, as the PID's alone - * do not provide type information. Please note that the the raw pool access - * read() and commit() calls are not thread-safe. - * - * Please supply a data set and use the data set read(), commit() calls for - * thread-safe data pool access. - * @ingroup data_pool - */ -class PoolRawAccess: public PoolVariableIF, HasReturnvaluesIF { -public: - /** - * This constructor is used to access a data pool entry with a - * given ID if the target type is not known. A DataSet object is supplied - * and the data pool entry with the given ID is registered to that data set. - * Please note that a pool raw access buffer only has a buffer - * with a size of double. As such, for vector entries which have - * @param data_pool_id Target data pool entry ID - * @param arrayEntry - * @param data_set Dataset to register data pool entry to - * @param setReadWriteMode - * @param registerVectors If set to true, the constructor checks if - * there are multiple vector entries to registers - * and registers all of them recursively into the data_set - * - */ - PoolRawAccess(uint32_t data_pool_id, uint8_t arrayEntry, - DataSetIF* data_set, ReadWriteMode_t setReadWriteMode = - PoolVariableIF::VAR_READ); - - /** - * @brief This operation returns a pointer to the entry fetched. - * @details Return pointer to the buffer containing the raw data - * Size and number of data can be retrieved by other means. - */ - 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 max_size. - * @param buffer A pointer to a buffer to write to - * @param writtenBytes The number of bytes written is returned with this value. - * @param max_size 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); - - /** - * @brief Serialize raw pool entry into provided buffer directly - * @param buffer Provided buffer. Raw pool data will be copied here - * @param size [out] Increment provided size value by serialized size - * @param max_size Maximum allowed serialization size - * @param bigEndian Specify endianess - * @return - @c RETURN_OK if serialization was successfull - * - @c SerializeIF::BUFFER_TOO_SHORT if range check failed - */ - 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; - - /** - * 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; - - 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); - static const ReturnValue_t READ_TYPE_TOO_LARGE = MAKE_RETURN_CODE(0x03); - static const ReturnValue_t READ_INDEX_TOO_LARGE = MAKE_RETURN_CODE(0x04); - static const ReturnValue_t READ_ENTRY_NON_EXISTENT = MAKE_RETURN_CODE(0x05); - static const uint8_t RAW_MAX_SIZE = sizeof(double); - uint8_t value[RAW_MAX_SIZE]; - - - /** - * @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(); - - /** - * 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(bool valid); - /** - * Getter for the remaining size. - */ - size_t getSizeTillEnd() const; - - /** - * @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 call is protected by a lock of the global data pool. - * @return -@c RETURN_OK Read successfull - * -@c READ_TYPE_TOO_LARGE - * -@c READ_INDEX_TOO_LARGE - * -@c READ_ENTRY_NON_EXISTENT - */ - ReturnValue_t read(uint32_t lockTimeout = MutexIF::BLOCKING) override; - /** - * @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 call is protected by a lock of the global data pool. - * - */ - ReturnValue_t commit(uint32_t lockTimeout = MutexIF::BLOCKING) override; - -protected: - /** - * @brief Like #read, but without a lock protection of the global pool. - * @details - * The operation does NOT provide any mutual exclusive protection by itself. - * This can be used if the lock is handled externally to avoid the overhead - * of consecutive lock und unlock operations. - * Declared protected to discourage free public usage. - */ - ReturnValue_t readWithoutLock() override; - /** - * @brief Like #commit, but without a lock protection of the global pool. - * @details - * The operation does NOT provide any mutual exclusive protection by itself. - * This can be used if the lock is handled externally to avoid the overhead - * of consecutive lock und unlock operations. - * Declared protected to discourage free public usage. - */ - ReturnValue_t commitWithoutLock() override; - - ReturnValue_t handleReadOut(PoolEntryIF* read_out); - void handleReadError(ReturnValue_t result); -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 type 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; -}; - -#endif /* POOLRAWACCESS_H_ */ +#ifndef POOLRAWACCESS_H_ +#define POOLRAWACCESS_H_ + +#include "../datapool/DataSetIF.h" +#include "../datapool/PoolEntryIF.h" +#include "../datapool/PoolVariableIF.h" +#include "../globalfunctions/Type.h" + +/** + * @brief This class allows accessing Data Pool variables as raw bytes. + * @details + * This is necessary to have an access method for HK data, as the PID's alone + * do not provide type information. Please note that the the raw pool access + * read() and commit() calls are not thread-safe. + * + * Please supply a data set and use the data set read(), commit() calls for + * thread-safe data pool access. + * @ingroup data_pool + */ +class PoolRawAccess: public PoolVariableIF, HasReturnvaluesIF { +public: + /** + * This constructor is used to access a data pool entry with a + * given ID if the target type is not known. A DataSet object is supplied + * and the data pool entry with the given ID is registered to that data set. + * Please note that a pool raw access buffer only has a buffer + * with a size of double. As such, for vector entries which have + * @param data_pool_id Target data pool entry ID + * @param arrayEntry + * @param data_set Dataset to register data pool entry to + * @param setReadWriteMode + * @param registerVectors If set to true, the constructor checks if + * there are multiple vector entries to registers + * and registers all of them recursively into the data_set + * + */ + PoolRawAccess(uint32_t data_pool_id, uint8_t arrayEntry, + DataSetIF* data_set, ReadWriteMode_t setReadWriteMode = + PoolVariableIF::VAR_READ); + + /** + * @brief This operation returns a pointer to the entry fetched. + * @details Return pointer to the buffer containing the raw data + * Size and number of data can be retrieved by other means. + */ + 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 max_size. + * @param buffer A pointer to a buffer to write to + * @param writtenBytes The number of bytes written is returned with this value. + * @param max_size 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); + + /** + * @brief Serialize raw pool entry into provided buffer directly + * @param buffer Provided buffer. Raw pool data will be copied here + * @param size [out] Increment provided size value by serialized size + * @param max_size Maximum allowed serialization size + * @param bigEndian Specify endianess + * @return - @c RETURN_OK if serialization was successfull + * - @c SerializeIF::BUFFER_TOO_SHORT if range check failed + */ + 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; + + /** + * 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; + + 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); + static const ReturnValue_t READ_TYPE_TOO_LARGE = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t READ_INDEX_TOO_LARGE = MAKE_RETURN_CODE(0x04); + static const ReturnValue_t READ_ENTRY_NON_EXISTENT = MAKE_RETURN_CODE(0x05); + static const uint8_t RAW_MAX_SIZE = sizeof(double); + uint8_t value[RAW_MAX_SIZE]; + + + /** + * @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(); + + /** + * 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(bool valid); + /** + * Getter for the remaining size. + */ + size_t getSizeTillEnd() const; + + /** + * @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 call is protected by a lock of the global data pool. + * @return -@c RETURN_OK Read successfull + * -@c READ_TYPE_TOO_LARGE + * -@c READ_INDEX_TOO_LARGE + * -@c READ_ENTRY_NON_EXISTENT + */ + ReturnValue_t read(uint32_t lockTimeout = MutexIF::BLOCKING) override; + /** + * @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 call is protected by a lock of the global data pool. + * + */ + ReturnValue_t commit(uint32_t lockTimeout = MutexIF::BLOCKING) override; + +protected: + /** + * @brief Like #read, but without a lock protection of the global pool. + * @details + * The operation does NOT provide any mutual exclusive protection by itself. + * This can be used if the lock is handled externally to avoid the overhead + * of consecutive lock und unlock operations. + * Declared protected to discourage free public usage. + */ + ReturnValue_t readWithoutLock() override; + /** + * @brief Like #commit, but without a lock protection of the global pool. + * @details + * The operation does NOT provide any mutual exclusive protection by itself. + * This can be used if the lock is handled externally to avoid the overhead + * of consecutive lock und unlock operations. + * Declared protected to discourage free public usage. + */ + ReturnValue_t commitWithoutLock() override; + + ReturnValue_t handleReadOut(PoolEntryIF* read_out); + void handleReadError(ReturnValue_t result); +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 type 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; +}; + +#endif /* POOLRAWACCESS_H_ */ diff --git a/datapoollocal/LocalPoolVector.h b/datapoollocal/LocalPoolVector.h index 8c3c50db..86705bb0 100644 --- a/datapoollocal/LocalPoolVector.h +++ b/datapoollocal/LocalPoolVector.h @@ -1,200 +1,200 @@ -#ifndef FRAMEWORK_DATAPOOLLOCAL_LOCALPOOLVECTOR_H_ -#define FRAMEWORK_DATAPOOLLOCAL_LOCALPOOLVECTOR_H_ - -#include "../datapool/DataSetIF.h" -#include "../datapool/PoolEntry.h" -#include "../datapool/PoolVariableIF.h" -#include "../datapoollocal/LocalDataPoolManager.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 LocalPoolVector: public PoolVariableIF, public HasReturnvaluesIF { -public: - LocalPoolVector() = delete; - /** - * This constructor is used by the data creators to have pool variable - * instances which can also be stored in datasets. - * It does not fetch the current value from the data pool. This is performed - * by the read() operation (which is not thread-safe). - * Datasets can be used to access local pool entires in a thread-safe way. - * @param poolId ID of the local pool entry. - * @param hkOwner Pointer of the owner. This will generally be the calling - * class itself which passes "this". - * @param setReadWriteMode Specify the read-write mode of the pool variable. - * @param dataSet The data set in which the variable shall register itself. - * If nullptr, the variable is not registered. - */ - LocalPoolVector(lp_id_t poolId, HasLocalDataPoolIF* hkOwner, - pool_rwm_t setReadWriteMode = pool_rwm_t::VAR_READ_WRITE, - DataSetIF* dataSet = nullptr); - - /** - * This constructor is used by data users like controllers to have - * access to the local pool variables of data creators by supplying - * the respective creator object ID. - * It does not fetch the current value from the data pool. This is performed - * by the read() operation (which is not thread-safe). - * Datasets can be used to access local pool entires in a thread-safe way. - * @param poolId ID of the local pool entry. - * @param hkOwner Pointer of the owner. This will generally be the calling - * class itself which passes "this". - * @param setReadWriteMode Specify the read-write mode of the pool variable. - * @param dataSet The data set in which the variable shall register itself. - * If nullptr, the variable is not registered. - */ - LocalPoolVector(lp_id_t poolId, object_id_t poolOwner, - pool_rwm_t setReadWriteMode = pool_rwm_t::VAR_READ_WRITE, - DataSetIF* dataSet = nullptr); - - /** - * @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 local - * array of this type. - */ - T value[vectorSize]; - /** - * @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. - */ - ~LocalPoolVector() {}; - /** - * @brief The operation returns the number of array entries - * in this variable. - */ - uint8_t getSize() { - return vectorSize; - } - - uint32_t getDataPoolId() const override; - /** - * @brief This operation sets the data pool ID of the variable. - * @details - * The method is necessary to set id's of data pool member variables - * with bad initialization. - */ - void setDataPoolId(uint32_t poolId); - - /** - * This method returns if the variable is write-only, read-write or read-only. - */ - pool_rwm_t getReadWriteMode() const; - - /** - * @brief With this call, the valid information of the variable is returned. - */ - bool isValid() const override; - void setValid(bool valid) override; - uint8_t getValid() const; - - T& operator [](int i); - const T &operator [](int i) const; - - virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, - const size_t maxSize, - SerializeIF::Endianness streamEndiannes) const override; - virtual size_t getSerializedSize() const override; - virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, - SerializeIF::Endianness streamEndianness) override; - - /** - * @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 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 read call is protected with a lock. - * It is recommended to use DataSets to read and commit multiple variables - * at once to avoid the overhead of unnecessary lock und unlock operations. - */ - ReturnValue_t read(uint32_t lockTimeout = MutexIF::BLOCKING) override; - /** - * @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 local valid flag is written back as well. - * The read call is protected with a lock. - * It is recommended to use DataSets to read and commit multiple variables - * at once to avoid the overhead of unnecessary lock und unlock operations. - */ - ReturnValue_t commit(uint32_t lockTimeout = MutexIF::BLOCKING) override; - -protected: - /** - * @brief Like #read, but without a lock protection of the global pool. - * @details - * The operation does NOT provide any mutual exclusive protection by itself. - * This can be used if the lock is handled externally to avoid the overhead - * of consecutive lock und unlock operations. - * Declared protected to discourage free public usage. - */ - ReturnValue_t readWithoutLock() override; - /** - * @brief Like #commit, but without a lock protection of the global pool. - * @details - * The operation does NOT provide any mutual exclusive protection by itself. - * This can be used if the lock is handled externally to avoid the overhead - * of consecutive lock und unlock operations. - * Declared protected to discourage free public usage. - */ - ReturnValue_t commitWithoutLock() override; - -private: - /** - * @brief To access the correct data pool entry on read and commit calls, - * the data pool id is stored. - */ - uint32_t localPoolId; - /** - * @brief The valid information as it was stored in the data pool - * is copied to this attribute. - */ - bool valid; - /** - * @brief The information whether the class is read-write or - * read-only is stored here. - */ - ReadWriteMode_t readWriteMode; - //! @brief Pointer to the class which manages the HK pool. - LocalDataPoolManager* hkManager; - - // std::ostream is the type for object std::cout - template - friend std::ostream& operator<< (std::ostream &out, - const LocalPoolVector &var); - - -}; - -#include "../datapoollocal/LocalPoolVector.tpp" - -template -using lp_vec_t = LocalPoolVector; - -#endif /* FRAMEWORK_DATAPOOLLOCAL_LOCALPOOLVECTOR_H_ */ +#ifndef FRAMEWORK_DATAPOOLLOCAL_LOCALPOOLVECTOR_H_ +#define FRAMEWORK_DATAPOOLLOCAL_LOCALPOOLVECTOR_H_ + +#include "../datapool/DataSetIF.h" +#include "../datapool/PoolEntry.h" +#include "../datapool/PoolVariableIF.h" +#include "../datapoollocal/LocalDataPoolManager.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 LocalPoolVector: public PoolVariableIF, public HasReturnvaluesIF { +public: + LocalPoolVector() = delete; + /** + * This constructor is used by the data creators to have pool variable + * instances which can also be stored in datasets. + * It does not fetch the current value from the data pool. This is performed + * by the read() operation (which is not thread-safe). + * Datasets can be used to access local pool entires in a thread-safe way. + * @param poolId ID of the local pool entry. + * @param hkOwner Pointer of the owner. This will generally be the calling + * class itself which passes "this". + * @param setReadWriteMode Specify the read-write mode of the pool variable. + * @param dataSet The data set in which the variable shall register itself. + * If nullptr, the variable is not registered. + */ + LocalPoolVector(lp_id_t poolId, HasLocalDataPoolIF* hkOwner, + pool_rwm_t setReadWriteMode = pool_rwm_t::VAR_READ_WRITE, + DataSetIF* dataSet = nullptr); + + /** + * This constructor is used by data users like controllers to have + * access to the local pool variables of data creators by supplying + * the respective creator object ID. + * It does not fetch the current value from the data pool. This is performed + * by the read() operation (which is not thread-safe). + * Datasets can be used to access local pool entires in a thread-safe way. + * @param poolId ID of the local pool entry. + * @param hkOwner Pointer of the owner. This will generally be the calling + * class itself which passes "this". + * @param setReadWriteMode Specify the read-write mode of the pool variable. + * @param dataSet The data set in which the variable shall register itself. + * If nullptr, the variable is not registered. + */ + LocalPoolVector(lp_id_t poolId, object_id_t poolOwner, + pool_rwm_t setReadWriteMode = pool_rwm_t::VAR_READ_WRITE, + DataSetIF* dataSet = nullptr); + + /** + * @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 local + * array of this type. + */ + T value[vectorSize]; + /** + * @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. + */ + ~LocalPoolVector() {}; + /** + * @brief The operation returns the number of array entries + * in this variable. + */ + uint8_t getSize() { + return vectorSize; + } + + uint32_t getDataPoolId() const override; + /** + * @brief This operation sets the data pool ID of the variable. + * @details + * The method is necessary to set id's of data pool member variables + * with bad initialization. + */ + void setDataPoolId(uint32_t poolId); + + /** + * This method returns if the variable is write-only, read-write or read-only. + */ + pool_rwm_t getReadWriteMode() const; + + /** + * @brief With this call, the valid information of the variable is returned. + */ + bool isValid() const override; + void setValid(bool valid) override; + uint8_t getValid() const; + + T& operator [](int i); + const T &operator [](int i) const; + + virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, + const size_t maxSize, + SerializeIF::Endianness streamEndiannes) const override; + virtual size_t getSerializedSize() const override; + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, + SerializeIF::Endianness streamEndianness) override; + + /** + * @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 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 read call is protected with a lock. + * It is recommended to use DataSets to read and commit multiple variables + * at once to avoid the overhead of unnecessary lock und unlock operations. + */ + ReturnValue_t read(uint32_t lockTimeout = MutexIF::BLOCKING) override; + /** + * @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 local valid flag is written back as well. + * The read call is protected with a lock. + * It is recommended to use DataSets to read and commit multiple variables + * at once to avoid the overhead of unnecessary lock und unlock operations. + */ + ReturnValue_t commit(uint32_t lockTimeout = MutexIF::BLOCKING) override; + +protected: + /** + * @brief Like #read, but without a lock protection of the global pool. + * @details + * The operation does NOT provide any mutual exclusive protection by itself. + * This can be used if the lock is handled externally to avoid the overhead + * of consecutive lock und unlock operations. + * Declared protected to discourage free public usage. + */ + ReturnValue_t readWithoutLock() override; + /** + * @brief Like #commit, but without a lock protection of the global pool. + * @details + * The operation does NOT provide any mutual exclusive protection by itself. + * This can be used if the lock is handled externally to avoid the overhead + * of consecutive lock und unlock operations. + * Declared protected to discourage free public usage. + */ + ReturnValue_t commitWithoutLock() override; + +private: + /** + * @brief To access the correct data pool entry on read and commit calls, + * the data pool id is stored. + */ + uint32_t localPoolId; + /** + * @brief The valid information as it was stored in the data pool + * is copied to this attribute. + */ + bool valid; + /** + * @brief The information whether the class is read-write or + * read-only is stored here. + */ + ReadWriteMode_t readWriteMode; + //! @brief Pointer to the class which manages the HK pool. + LocalDataPoolManager* hkManager; + + // std::ostream is the type for object std::cout + template + friend std::ostream& operator<< (std::ostream &out, + const LocalPoolVector &var); + + +}; + +#include "../datapoollocal/LocalPoolVector.tpp" + +template +using lp_vec_t = LocalPoolVector; + +#endif /* FRAMEWORK_DATAPOOLLOCAL_LOCALPOOLVECTOR_H_ */ diff --git a/datapoollocal/SharedLocalDataSet.cpp b/datapoollocal/SharedLocalDataSet.cpp index 73e0e1fb..dd1bdcc4 100644 --- a/datapoollocal/SharedLocalDataSet.cpp +++ b/datapoollocal/SharedLocalDataSet.cpp @@ -1,16 +1,16 @@ -#include "SharedLocalDataSet.h" - -SharedLocalDataSet::SharedLocalDataSet(object_id_t objectId, sid_t sid, - const size_t maxSize): SystemObject(objectId), - LocalPoolDataSetBase(sid, nullptr, maxSize) { - this->setContainer(poolVarVector.data()); - datasetLock = MutexFactory::instance()->createMutex(); -} - -ReturnValue_t SharedLocalDataSet::lockDataset(dur_millis_t mutexTimeout) { - return datasetLock->lockMutex(MutexIF::TimeoutType::WAITING, mutexTimeout); -} - -ReturnValue_t SharedLocalDataSet::unlockDataset() { - return datasetLock->unlockMutex(); -} +#include "SharedLocalDataSet.h" + +SharedLocalDataSet::SharedLocalDataSet(object_id_t objectId, sid_t sid, + const size_t maxSize): SystemObject(objectId), + LocalPoolDataSetBase(sid, nullptr, maxSize) { + this->setContainer(poolVarVector.data()); + datasetLock = MutexFactory::instance()->createMutex(); +} + +ReturnValue_t SharedLocalDataSet::lockDataset(dur_millis_t mutexTimeout) { + return datasetLock->lockMutex(MutexIF::TimeoutType::WAITING, mutexTimeout); +} + +ReturnValue_t SharedLocalDataSet::unlockDataset() { + return datasetLock->unlockMutex(); +} diff --git a/devicehandlers/AcceptsDeviceResponsesIF.h b/devicehandlers/AcceptsDeviceResponsesIF.h index d7e11714..eb7780db 100644 --- a/devicehandlers/AcceptsDeviceResponsesIF.h +++ b/devicehandlers/AcceptsDeviceResponsesIF.h @@ -1,19 +1,19 @@ -#ifndef FRAMEWORK_DEVICEHANDLERS_ACCEPTSDEVICERESPONSESIF_H_ -#define FRAMEWORK_DEVICEHANDLERS_ACCEPTSDEVICERESPONSESIF_H_ - -#include "../ipc/MessageQueueSenderIF.h" - -/** - * This interface is used by the device handler to send a device response - * to the queue ID, which is returned in the implemented abstract method. - */ -class AcceptsDeviceResponsesIF { -public: - /** - * Default empty virtual destructor. - */ - virtual ~AcceptsDeviceResponsesIF() {} - virtual MessageQueueId_t getDeviceQueue() = 0; -}; - -#endif /* FRAMEWORK_DEVICEHANDLERS_ACCEPTSDEVICERESPONSESIF_H_ */ +#ifndef FRAMEWORK_DEVICEHANDLERS_ACCEPTSDEVICERESPONSESIF_H_ +#define FRAMEWORK_DEVICEHANDLERS_ACCEPTSDEVICERESPONSESIF_H_ + +#include "../ipc/MessageQueueSenderIF.h" + +/** + * This interface is used by the device handler to send a device response + * to the queue ID, which is returned in the implemented abstract method. + */ +class AcceptsDeviceResponsesIF { +public: + /** + * Default empty virtual destructor. + */ + virtual ~AcceptsDeviceResponsesIF() {} + virtual MessageQueueId_t getDeviceQueue() = 0; +}; + +#endif /* FRAMEWORK_DEVICEHANDLERS_ACCEPTSDEVICERESPONSESIF_H_ */ diff --git a/devicehandlers/AssemblyBase.cpp b/devicehandlers/AssemblyBase.cpp index c98a9f67..3bcb8a11 100644 --- a/devicehandlers/AssemblyBase.cpp +++ b/devicehandlers/AssemblyBase.cpp @@ -1,273 +1,273 @@ -#include "../devicehandlers/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 (const auto& childIter: childrenMap) { - if (checkChildOff(childIter.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); -} +#include "../devicehandlers/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 (const auto& childIter: childrenMap) { + if (checkChildOff(childIter.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/devicehandlers/AssemblyBase.h b/devicehandlers/AssemblyBase.h index 7c66b21d..b67c551e 100644 --- a/devicehandlers/AssemblyBase.h +++ b/devicehandlers/AssemblyBase.h @@ -1,163 +1,163 @@ -#ifndef FRAMEWORK_DEVICEHANDLERS_ASSEMBLYBASE_H_ -#define FRAMEWORK_DEVICEHANDLERS_ASSEMBLYBASE_H_ - -#include "../container/FixedArrayList.h" -#include "../devicehandlers/DeviceHandlerBase.h" -#include "../subsystem/SubsystemBase.h" - -/** - * @brief Base class to implement reconfiguration and failure handling for - * redundant devices by monitoring their modes health states. - * @details - * Documentation: Dissertation Baetz p.156, 157. - * - * This class reduces the complexity of controller components which would - * otherwise be needed for the handling of redundant devices. - * - * The template class monitors mode and health state of its children - * and checks availability of devices on every detected change. - * AssemblyBase does not implement any redundancy logic by itself, but provides - * adaptation points for implementations to do so. Since most monitoring - * activities rely on mode and health state only and are therefore - * generic, it is sufficient for subclasses to provide: - * - * 1. check logic when active-> checkChildrenStateOn - * 2. transition logic to change the mode -> commandChildren - * - */ -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: - - // SHOULDDO: Change that OVERWRITE_HEALTH may be returned - // (or return internalState directly?) - /** - * Command children to reach [mode,submode] combination - * Can be done by setting #commandsOutstanding correctly, - * or using 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; - - /** - * Check whether desired assembly mode was achieved by checking the modes - * or/and health states of child device handlers. - * The assembly template class will also call this function if a health - * or mode change of a child device handler was detected. - * @param wantedMode - * @param wantedSubmode - * @return - */ - virtual ReturnValue_t checkChildrenStateOn(Mode_t wantedMode, - Submode_t wantedSubmode) = 0; - - virtual ReturnValue_t isModeCombinationValid(Mode_t mode, - Submode_t submode) = 0; - - 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 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); - - 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 /* FRAMEWORK_DEVICEHANDLERS_ASSEMBLYBASE_H_ */ +#ifndef FRAMEWORK_DEVICEHANDLERS_ASSEMBLYBASE_H_ +#define FRAMEWORK_DEVICEHANDLERS_ASSEMBLYBASE_H_ + +#include "../container/FixedArrayList.h" +#include "../devicehandlers/DeviceHandlerBase.h" +#include "../subsystem/SubsystemBase.h" + +/** + * @brief Base class to implement reconfiguration and failure handling for + * redundant devices by monitoring their modes health states. + * @details + * Documentation: Dissertation Baetz p.156, 157. + * + * This class reduces the complexity of controller components which would + * otherwise be needed for the handling of redundant devices. + * + * The template class monitors mode and health state of its children + * and checks availability of devices on every detected change. + * AssemblyBase does not implement any redundancy logic by itself, but provides + * adaptation points for implementations to do so. Since most monitoring + * activities rely on mode and health state only and are therefore + * generic, it is sufficient for subclasses to provide: + * + * 1. check logic when active-> checkChildrenStateOn + * 2. transition logic to change the mode -> commandChildren + * + */ +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: + + // SHOULDDO: Change that OVERWRITE_HEALTH may be returned + // (or return internalState directly?) + /** + * Command children to reach [mode,submode] combination + * Can be done by setting #commandsOutstanding correctly, + * or using 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; + + /** + * Check whether desired assembly mode was achieved by checking the modes + * or/and health states of child device handlers. + * The assembly template class will also call this function if a health + * or mode change of a child device handler was detected. + * @param wantedMode + * @param wantedSubmode + * @return + */ + virtual ReturnValue_t checkChildrenStateOn(Mode_t wantedMode, + Submode_t wantedSubmode) = 0; + + virtual ReturnValue_t isModeCombinationValid(Mode_t mode, + Submode_t submode) = 0; + + 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 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); + + 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 /* FRAMEWORK_DEVICEHANDLERS_ASSEMBLYBASE_H_ */ diff --git a/devicehandlers/ChildHandlerBase.cpp b/devicehandlers/ChildHandlerBase.cpp index acc1b5a0..976c252a 100644 --- a/devicehandlers/ChildHandlerBase.cpp +++ b/devicehandlers/ChildHandlerBase.cpp @@ -1,47 +1,47 @@ -#include "../subsystem/SubsystemBase.h" -#include "../devicehandlers/ChildHandlerBase.h" -#include "../subsystem/SubsystemBase.h" - -ChildHandlerBase::ChildHandlerBase(object_id_t setObjectId, - object_id_t deviceCommunication, CookieIF * cookie, - object_id_t hkDestination, 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->setHkDestination(hkDestination); - 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; -} +#include "../subsystem/SubsystemBase.h" +#include "../devicehandlers/ChildHandlerBase.h" +#include "../subsystem/SubsystemBase.h" + +ChildHandlerBase::ChildHandlerBase(object_id_t setObjectId, + object_id_t deviceCommunication, CookieIF * cookie, + object_id_t hkDestination, 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->setHkDestination(hkDestination); + 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/devicehandlers/ChildHandlerBase.h b/devicehandlers/ChildHandlerBase.h index 625955f0..19e0fc80 100644 --- a/devicehandlers/ChildHandlerBase.h +++ b/devicehandlers/ChildHandlerBase.h @@ -1,24 +1,24 @@ -#ifndef PAYLOADHANDLERBASE_H_ -#define PAYLOADHANDLERBASE_H_ - -#include "../devicehandlers/ChildHandlerFDIR.h" -#include "../devicehandlers/DeviceHandlerBase.h" - -class ChildHandlerBase: public DeviceHandlerBase { -public: - ChildHandlerBase(object_id_t setObjectId, object_id_t deviceCommunication, - CookieIF * cookie, object_id_t hkDestination, - 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 /* PAYLOADHANDLERBASE_H_ */ +#ifndef PAYLOADHANDLERBASE_H_ +#define PAYLOADHANDLERBASE_H_ + +#include "../devicehandlers/ChildHandlerFDIR.h" +#include "../devicehandlers/DeviceHandlerBase.h" + +class ChildHandlerBase: public DeviceHandlerBase { +public: + ChildHandlerBase(object_id_t setObjectId, object_id_t deviceCommunication, + CookieIF * cookie, object_id_t hkDestination, + 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 /* PAYLOADHANDLERBASE_H_ */ diff --git a/devicehandlers/ChildHandlerFDIR.cpp b/devicehandlers/ChildHandlerFDIR.cpp index bab5a7e5..9589b44f 100644 --- a/devicehandlers/ChildHandlerFDIR.cpp +++ b/devicehandlers/ChildHandlerFDIR.cpp @@ -1,10 +1,10 @@ -#include "../devicehandlers/ChildHandlerFDIR.h" - -ChildHandlerFDIR::ChildHandlerFDIR(object_id_t owner, object_id_t faultTreeParent, uint32_t recoveryCount) : - DeviceHandlerFailureIsolation(owner, faultTreeParent) { - recoveryCounter.setFailureThreshold(recoveryCount); -} - -ChildHandlerFDIR::~ChildHandlerFDIR() { -} - +#include "../devicehandlers/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/devicehandlers/ChildHandlerFDIR.h b/devicehandlers/ChildHandlerFDIR.h index 83f79a63..203fc944 100644 --- a/devicehandlers/ChildHandlerFDIR.h +++ b/devicehandlers/ChildHandlerFDIR.h @@ -1,20 +1,20 @@ -#ifndef FRAMEWORK_DEVICEHANDLERS_CHILDHANDLERFDIR_H_ -#define FRAMEWORK_DEVICEHANDLERS_CHILDHANDLERFDIR_H_ - -#include "../devicehandlers/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_ */ +#ifndef FRAMEWORK_DEVICEHANDLERS_CHILDHANDLERFDIR_H_ +#define FRAMEWORK_DEVICEHANDLERS_CHILDHANDLERFDIR_H_ + +#include "../devicehandlers/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/devicehandlers/CommunicationMessage.cpp b/devicehandlers/CommunicationMessage.cpp index aacf5580..abdfd3a8 100644 --- a/devicehandlers/CommunicationMessage.cpp +++ b/devicehandlers/CommunicationMessage.cpp @@ -1,201 +1,201 @@ -/** - * @file CommunicationMessage.cpp - * - * @date 28.02.2020 - */ - -#include "../devicehandlers/CommunicationMessage.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include - -CommunicationMessage::CommunicationMessage(): uninitialized(true) { -} - -CommunicationMessage::~CommunicationMessage() {} - -void CommunicationMessage::setSendRequestFromPointer(uint32_t address, - uint32_t dataLen, const uint8_t * data) { - setMessageType(SEND_DATA_FROM_POINTER); - setAddress(address); - setDataLen(dataLen); - setDataPointer(data); -} - -void CommunicationMessage::setSendRequestFromIpcStore(uint32_t address, store_address_t storeId) { - setMessageType(SEND_DATA_FROM_IPC_STORE); - setAddress(address); - setStoreId(storeId.raw); -} - -void CommunicationMessage::setSendRequestRaw(uint32_t address, uint32_t length, - uint16_t sendBufferPosition) { - setMessageType(SEND_DATA_RAW); - setAddress(address); - setDataLen(length); - if(sendBufferPosition != 0) { - setBufferPosition(sendBufferPosition); - } -} - -void CommunicationMessage::setDataReplyFromIpcStore(uint32_t address, store_address_t storeId) { - setMessageType(REPLY_DATA_IPC_STORE); - setAddress(address); - setStoreId(storeId.raw); -} -void CommunicationMessage::setDataReplyFromPointer(uint32_t address, - uint32_t dataLen, uint8_t *data) { - setMessageType(REPLY_DATA_FROM_POINTER); - setAddress(address); - setDataLen(dataLen); - setDataPointer(data); -} - -void CommunicationMessage::setDataReplyRaw(uint32_t address, - uint32_t length, uint16_t receiveBufferPosition) { - setMessageType(REPLY_DATA_RAW); - setAddress(address); - setDataLen(length); - if(receiveBufferPosition != 0) { - setBufferPosition(receiveBufferPosition); - } -} - -void CommunicationMessage::setMessageType(messageType status) { - uint8_t status_uint8 = status; - memcpy(getData() + sizeof(uint32_t), &status_uint8, sizeof(status_uint8)); -} - -void CommunicationMessage::setAddress(address_t address) { - memcpy(getData(),&address,sizeof(address)); -} - -address_t CommunicationMessage::getAddress() const { - address_t address; - memcpy(&address,getData(),sizeof(address)); - return address; -} - -void CommunicationMessage::setBufferPosition(uint16_t bufferPosition) { - memcpy(getData() + sizeof(uint32_t) + sizeof(uint16_t), - &bufferPosition, sizeof(bufferPosition)); -} - -uint16_t CommunicationMessage::getBufferPosition() const { - uint16_t bufferPosition; - memcpy(&bufferPosition, - getData() + sizeof(uint32_t) + sizeof(uint16_t), sizeof(bufferPosition)); - return bufferPosition; -} - -void CommunicationMessage::setDataPointer(const void * data) { - memcpy(getData() + 3 * sizeof(uint32_t), &data, sizeof(uint32_t)); -} - -void CommunicationMessage::setStoreId(store_address_t storeId) { - memcpy(getData() + 2 * sizeof(uint32_t), &storeId.raw, sizeof(uint32_t)); -} - -store_address_t CommunicationMessage::getStoreId() const{ - store_address_t temp; - memcpy(&temp.raw,getData() + 2 * sizeof(uint32_t), sizeof(uint32_t)); - return temp; -} - -void CommunicationMessage::setDataLen(uint32_t length) { - memcpy(getData() + 2 * sizeof(uint32_t), &length, sizeof(length)); -} - -uint32_t CommunicationMessage::getDataLen() const { - uint32_t len; - memcpy(&len, getData() + 2 * sizeof(uint32_t), sizeof(len)); - return len; -} - -void CommunicationMessage::setUint32Data(uint32_t data) { - memcpy(getData() + 3 * sizeof(uint32_t), &data, sizeof(data)); -} - -uint32_t CommunicationMessage::getUint32Data() const{ - uint32_t data; - memcpy(&data,getData() + 3 * sizeof(uint32_t), sizeof(data)); - return data; -} - -void CommunicationMessage::setDataByte(uint8_t byte, uint8_t position) { - if(0 <= position && position <= 3) { - memcpy(getData() + 3 * sizeof(uint32_t) + position * sizeof(uint8_t), &byte, sizeof(byte)); - } - else { - sif::error << "Comm Message: Invalid byte position" << std::endl; - } -} - -uint8_t CommunicationMessage::getDataByte(uint8_t position) const { - if(0 <= position && position <= 3) { - uint8_t byte; - memcpy(&byte, getData() + 3 * sizeof(uint32_t) + position * sizeof(uint8_t), sizeof(byte)); - return byte; - } - else { - return 0; - sif::error << "Comm Message: Invalid byte position" << std::endl; - } -} - -void CommunicationMessage::setDataUint16(uint16_t data, uint8_t position) { - if(position == 0 || position == 1) { - memcpy(getData() + 3 * sizeof(uint32_t) + position * sizeof(uint16_t), &data, sizeof(data)); - } - else { - sif::error << "Comm Message: Invalid byte position" << std::endl; - } - -} - -uint16_t CommunicationMessage::getDataUint16(uint8_t position) const{ - if(position == 0 || position == 1) { - uint16_t data; - memcpy(&data, getData() + 3 * sizeof(uint32_t) + position * sizeof(uint16_t), sizeof(data)); - return data; - } - else { - return 0; - sif::error << "Comm Message: Invalid byte position" << std::endl; - } -} - -CommunicationMessage::messageType CommunicationMessage::getMessageType() const{ - messageType messageType; - memcpy(&messageType, getData() + sizeof(uint32_t),sizeof(uint8_t)); - return messageType; -} - -void CommunicationMessage::setMessageId(uint8_t messageId) { - memcpy(getData() + sizeof(uint32_t) + sizeof(uint8_t), &messageId, sizeof(messageId)); -} - -uint8_t CommunicationMessage::getMessageId() const { - uint8_t messageId; - memcpy(&messageId, getData() + sizeof(uint32_t) + sizeof(uint8_t), sizeof(messageId)); - return messageId; -} - -void CommunicationMessage::clearCommunicationMessage() { - messageType messageType = getMessageType(); - switch(messageType) { - case(messageType::REPLY_DATA_IPC_STORE): - case(messageType::SEND_DATA_FROM_IPC_STORE): { - store_address_t storeId = getStoreId(); - StorageManagerIF *ipcStore = objectManager-> - get(objects::IPC_STORE); - if (ipcStore != NULL) { - ipcStore->deleteData(storeId); - } - } - /* NO BREAK falls through*/ - default: - memset(getData(),0,4*sizeof(uint32_t)); - break; - } -} - +/** + * @file CommunicationMessage.cpp + * + * @date 28.02.2020 + */ + +#include "../devicehandlers/CommunicationMessage.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include + +CommunicationMessage::CommunicationMessage(): uninitialized(true) { +} + +CommunicationMessage::~CommunicationMessage() {} + +void CommunicationMessage::setSendRequestFromPointer(uint32_t address, + uint32_t dataLen, const uint8_t * data) { + setMessageType(SEND_DATA_FROM_POINTER); + setAddress(address); + setDataLen(dataLen); + setDataPointer(data); +} + +void CommunicationMessage::setSendRequestFromIpcStore(uint32_t address, store_address_t storeId) { + setMessageType(SEND_DATA_FROM_IPC_STORE); + setAddress(address); + setStoreId(storeId.raw); +} + +void CommunicationMessage::setSendRequestRaw(uint32_t address, uint32_t length, + uint16_t sendBufferPosition) { + setMessageType(SEND_DATA_RAW); + setAddress(address); + setDataLen(length); + if(sendBufferPosition != 0) { + setBufferPosition(sendBufferPosition); + } +} + +void CommunicationMessage::setDataReplyFromIpcStore(uint32_t address, store_address_t storeId) { + setMessageType(REPLY_DATA_IPC_STORE); + setAddress(address); + setStoreId(storeId.raw); +} +void CommunicationMessage::setDataReplyFromPointer(uint32_t address, + uint32_t dataLen, uint8_t *data) { + setMessageType(REPLY_DATA_FROM_POINTER); + setAddress(address); + setDataLen(dataLen); + setDataPointer(data); +} + +void CommunicationMessage::setDataReplyRaw(uint32_t address, + uint32_t length, uint16_t receiveBufferPosition) { + setMessageType(REPLY_DATA_RAW); + setAddress(address); + setDataLen(length); + if(receiveBufferPosition != 0) { + setBufferPosition(receiveBufferPosition); + } +} + +void CommunicationMessage::setMessageType(messageType status) { + uint8_t status_uint8 = status; + memcpy(getData() + sizeof(uint32_t), &status_uint8, sizeof(status_uint8)); +} + +void CommunicationMessage::setAddress(address_t address) { + memcpy(getData(),&address,sizeof(address)); +} + +address_t CommunicationMessage::getAddress() const { + address_t address; + memcpy(&address,getData(),sizeof(address)); + return address; +} + +void CommunicationMessage::setBufferPosition(uint16_t bufferPosition) { + memcpy(getData() + sizeof(uint32_t) + sizeof(uint16_t), + &bufferPosition, sizeof(bufferPosition)); +} + +uint16_t CommunicationMessage::getBufferPosition() const { + uint16_t bufferPosition; + memcpy(&bufferPosition, + getData() + sizeof(uint32_t) + sizeof(uint16_t), sizeof(bufferPosition)); + return bufferPosition; +} + +void CommunicationMessage::setDataPointer(const void * data) { + memcpy(getData() + 3 * sizeof(uint32_t), &data, sizeof(uint32_t)); +} + +void CommunicationMessage::setStoreId(store_address_t storeId) { + memcpy(getData() + 2 * sizeof(uint32_t), &storeId.raw, sizeof(uint32_t)); +} + +store_address_t CommunicationMessage::getStoreId() const{ + store_address_t temp; + memcpy(&temp.raw,getData() + 2 * sizeof(uint32_t), sizeof(uint32_t)); + return temp; +} + +void CommunicationMessage::setDataLen(uint32_t length) { + memcpy(getData() + 2 * sizeof(uint32_t), &length, sizeof(length)); +} + +uint32_t CommunicationMessage::getDataLen() const { + uint32_t len; + memcpy(&len, getData() + 2 * sizeof(uint32_t), sizeof(len)); + return len; +} + +void CommunicationMessage::setUint32Data(uint32_t data) { + memcpy(getData() + 3 * sizeof(uint32_t), &data, sizeof(data)); +} + +uint32_t CommunicationMessage::getUint32Data() const{ + uint32_t data; + memcpy(&data,getData() + 3 * sizeof(uint32_t), sizeof(data)); + return data; +} + +void CommunicationMessage::setDataByte(uint8_t byte, uint8_t position) { + if(0 <= position && position <= 3) { + memcpy(getData() + 3 * sizeof(uint32_t) + position * sizeof(uint8_t), &byte, sizeof(byte)); + } + else { + sif::error << "Comm Message: Invalid byte position" << std::endl; + } +} + +uint8_t CommunicationMessage::getDataByte(uint8_t position) const { + if(0 <= position && position <= 3) { + uint8_t byte; + memcpy(&byte, getData() + 3 * sizeof(uint32_t) + position * sizeof(uint8_t), sizeof(byte)); + return byte; + } + else { + return 0; + sif::error << "Comm Message: Invalid byte position" << std::endl; + } +} + +void CommunicationMessage::setDataUint16(uint16_t data, uint8_t position) { + if(position == 0 || position == 1) { + memcpy(getData() + 3 * sizeof(uint32_t) + position * sizeof(uint16_t), &data, sizeof(data)); + } + else { + sif::error << "Comm Message: Invalid byte position" << std::endl; + } + +} + +uint16_t CommunicationMessage::getDataUint16(uint8_t position) const{ + if(position == 0 || position == 1) { + uint16_t data; + memcpy(&data, getData() + 3 * sizeof(uint32_t) + position * sizeof(uint16_t), sizeof(data)); + return data; + } + else { + return 0; + sif::error << "Comm Message: Invalid byte position" << std::endl; + } +} + +CommunicationMessage::messageType CommunicationMessage::getMessageType() const{ + messageType messageType; + memcpy(&messageType, getData() + sizeof(uint32_t),sizeof(uint8_t)); + return messageType; +} + +void CommunicationMessage::setMessageId(uint8_t messageId) { + memcpy(getData() + sizeof(uint32_t) + sizeof(uint8_t), &messageId, sizeof(messageId)); +} + +uint8_t CommunicationMessage::getMessageId() const { + uint8_t messageId; + memcpy(&messageId, getData() + sizeof(uint32_t) + sizeof(uint8_t), sizeof(messageId)); + return messageId; +} + +void CommunicationMessage::clearCommunicationMessage() { + messageType messageType = getMessageType(); + switch(messageType) { + case(messageType::REPLY_DATA_IPC_STORE): + case(messageType::SEND_DATA_FROM_IPC_STORE): { + store_address_t storeId = getStoreId(); + StorageManagerIF *ipcStore = objectManager-> + get(objects::IPC_STORE); + if (ipcStore != NULL) { + ipcStore->deleteData(storeId); + } + } + /* NO BREAK falls through*/ + default: + memset(getData(),0,4*sizeof(uint32_t)); + break; + } +} + diff --git a/devicehandlers/CommunicationMessage.h b/devicehandlers/CommunicationMessage.h index a31e270c..5b429d9a 100644 --- a/devicehandlers/CommunicationMessage.h +++ b/devicehandlers/CommunicationMessage.h @@ -1,173 +1,173 @@ -/** - * @file CommunicationMessage.h - * - * @date 28.02.2020 - */ - -#ifndef FRAMEWORK_DEVICEHANDLERS_COMMUNICATIONMESSAGE_H_ -#define FRAMEWORK_DEVICEHANDLERS_COMMUNICATIONMESSAGE_H_ -#include "../devicehandlers/CommunicationMessage.h" - -#include "../ipc/MessageQueueMessage.h" -#include "../storagemanager/StorageManagerIF.h" -#include "../devicehandlers/DeviceHandlerBase.h" - -/** - * @brief Message type to send larger messages - * - * @details - * Can be used to pass information like data pointers and - * data sizes between communication tasks. - * - */ -class CommunicationMessage: public MessageQueueMessage { -public: - enum messageType { - NONE, - SEND_DATA_FROM_POINTER, - SEND_DATA_FROM_IPC_STORE, - SEND_DATA_RAW, - REPLY_DATA_FROM_POINTER, - REPLY_DATA_IPC_STORE, - REPLY_DATA_RAW, - FAULTY, - }; - - //Add other messageIDs here if necessary. - static const uint8_t COMMUNICATION_MESSAGE_SIZE = HEADER_SIZE + 4 * sizeof(uint32_t); - - CommunicationMessage(); - virtual ~CommunicationMessage(); - - /** - * Message Type is stored as the fifth byte of the message data - * @param status - */ - void setMessageType(messageType status); - messageType getMessageType() const; - - /** - * This is a unique ID which can be used to handle different kinds of messages. - * For example, the same interface (e.g. SPI) could be used to exchange raw data - * (e.g. sensor values) and data stored in the IPC store. - * The ID can be used to distinguish the messages in child implementations. - * The message ID is stored as the sixth byte of the message data. - * @param messageId - */ - void setMessageId(uint8_t messageId); - uint8_t getMessageId() const; - - /** - * Send requests with pointer to the data to be sent and send data length - * @param address Target Address, first four bytes - * @param dataLen Length of data to send, next four bytes - * @param data Pointer to data to send - * - */ - void setSendRequestFromPointer(uint32_t address, uint32_t dataLen, const uint8_t * data); - - /** - * Send requests with a store ID, using the IPC store - * @param address Target Address, first four bytes - * @param storeId Store ID in the IPC store - * - */ - void setSendRequestFromIpcStore(uint32_t address, store_address_t storeId); - - /** - * Send requests with data length and data in message (max. 4 bytes) - * @param address Target Address, first four bytes - * @param dataLen Length of data to send, next four bytes - * @param data Pointer to data to send - * - */ - void setSendRequestRaw(uint32_t address, uint32_t length, - uint16_t sendBufferPosition = 0); - - /** - * Data message with data stored in IPC store - * @param address Target Address, first four bytes - * @param length - * @param storeId - */ - void setDataReplyFromIpcStore(uint32_t address, store_address_t storeId); - - /** - * Data reply with data stored in buffer, passing the pointer to - * the buffer and the data size - * @param address Target Address, first four bytes - * @param dataLen Length of data to send, next four bytes - * @param data Pointer to the data - */ - void setDataReplyFromPointer(uint32_t address, uint32_t dataLen, uint8_t * data); - - /** - * Data message with data stored in actual message. - * 4 byte datafield is intialized with 0. - * Set data with specific setter functions below. - * Can also be used to supply information at which position the raw data should be stored - * in a receive buffer. - */ - void setDataReplyRaw(uint32_t address, uint32_t length, uint16_t receiveBufferPosition = 0); - - /** - * First four bytes of message data - * @param address - */ - void setAddress(address_t address); - - address_t getAddress() const; - /** - * Set byte as position of 4 byte data field - * @param byte - * @param position Position, 0 to 3 possible - */ - void setDataByte(uint8_t byte, uint8_t position); - uint8_t getDataByte(uint8_t position) const; - - /** - * Set 2 byte value at position 1 or 2 of data field - * @param data - * @param position 0 or 1 possible - */ - void setDataUint16(uint16_t data, uint8_t position); - uint16_t getDataUint16(uint8_t position) const; - - void setUint32Data(uint32_t data); - uint32_t getUint32Data() const; - - /** - * Stored in Bytes 13-16 of message data - * @param length - */ - void setDataLen(uint32_t length); - - uint32_t getDataLen() const; - - /** - * Stored in last four bytes (Bytes 17-20) of message data - * @param sendData - */ - void setDataPointer(const void * data); - - /** - * In case the send request data or reply data is to be stored in a buffer, - * a buffer Position can be stored here as the seventh and eigth byte of - * the message, so the receive buffer can't be larger than sizeof(uint16_t) for now. - * @param bufferPosition In case the data is stored in a buffer, the position can be supplied here - */ - void setBufferPosition(uint16_t bufferPosition); - uint16_t getBufferPosition() const; - void setStoreId(store_address_t storeId); - store_address_t getStoreId() const; - - /** - * Clear the message. Deletes IPC Store data - * and sets all data to 0. Also sets message type to NONE - */ - void clearCommunicationMessage(); -private: - bool uninitialized; //!< Could be used to warn if data has not been set. -}; - -#endif /* FRAMEWORK_DEVICEHANDLERS_COMMUNICATIONMESSAGE_H_ */ +/** + * @file CommunicationMessage.h + * + * @date 28.02.2020 + */ + +#ifndef FRAMEWORK_DEVICEHANDLERS_COMMUNICATIONMESSAGE_H_ +#define FRAMEWORK_DEVICEHANDLERS_COMMUNICATIONMESSAGE_H_ +#include "../devicehandlers/CommunicationMessage.h" + +#include "../ipc/MessageQueueMessage.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../devicehandlers/DeviceHandlerBase.h" + +/** + * @brief Message type to send larger messages + * + * @details + * Can be used to pass information like data pointers and + * data sizes between communication tasks. + * + */ +class CommunicationMessage: public MessageQueueMessage { +public: + enum messageType { + NONE, + SEND_DATA_FROM_POINTER, + SEND_DATA_FROM_IPC_STORE, + SEND_DATA_RAW, + REPLY_DATA_FROM_POINTER, + REPLY_DATA_IPC_STORE, + REPLY_DATA_RAW, + FAULTY, + }; + + //Add other messageIDs here if necessary. + static const uint8_t COMMUNICATION_MESSAGE_SIZE = HEADER_SIZE + 4 * sizeof(uint32_t); + + CommunicationMessage(); + virtual ~CommunicationMessage(); + + /** + * Message Type is stored as the fifth byte of the message data + * @param status + */ + void setMessageType(messageType status); + messageType getMessageType() const; + + /** + * This is a unique ID which can be used to handle different kinds of messages. + * For example, the same interface (e.g. SPI) could be used to exchange raw data + * (e.g. sensor values) and data stored in the IPC store. + * The ID can be used to distinguish the messages in child implementations. + * The message ID is stored as the sixth byte of the message data. + * @param messageId + */ + void setMessageId(uint8_t messageId); + uint8_t getMessageId() const; + + /** + * Send requests with pointer to the data to be sent and send data length + * @param address Target Address, first four bytes + * @param dataLen Length of data to send, next four bytes + * @param data Pointer to data to send + * + */ + void setSendRequestFromPointer(uint32_t address, uint32_t dataLen, const uint8_t * data); + + /** + * Send requests with a store ID, using the IPC store + * @param address Target Address, first four bytes + * @param storeId Store ID in the IPC store + * + */ + void setSendRequestFromIpcStore(uint32_t address, store_address_t storeId); + + /** + * Send requests with data length and data in message (max. 4 bytes) + * @param address Target Address, first four bytes + * @param dataLen Length of data to send, next four bytes + * @param data Pointer to data to send + * + */ + void setSendRequestRaw(uint32_t address, uint32_t length, + uint16_t sendBufferPosition = 0); + + /** + * Data message with data stored in IPC store + * @param address Target Address, first four bytes + * @param length + * @param storeId + */ + void setDataReplyFromIpcStore(uint32_t address, store_address_t storeId); + + /** + * Data reply with data stored in buffer, passing the pointer to + * the buffer and the data size + * @param address Target Address, first four bytes + * @param dataLen Length of data to send, next four bytes + * @param data Pointer to the data + */ + void setDataReplyFromPointer(uint32_t address, uint32_t dataLen, uint8_t * data); + + /** + * Data message with data stored in actual message. + * 4 byte datafield is intialized with 0. + * Set data with specific setter functions below. + * Can also be used to supply information at which position the raw data should be stored + * in a receive buffer. + */ + void setDataReplyRaw(uint32_t address, uint32_t length, uint16_t receiveBufferPosition = 0); + + /** + * First four bytes of message data + * @param address + */ + void setAddress(address_t address); + + address_t getAddress() const; + /** + * Set byte as position of 4 byte data field + * @param byte + * @param position Position, 0 to 3 possible + */ + void setDataByte(uint8_t byte, uint8_t position); + uint8_t getDataByte(uint8_t position) const; + + /** + * Set 2 byte value at position 1 or 2 of data field + * @param data + * @param position 0 or 1 possible + */ + void setDataUint16(uint16_t data, uint8_t position); + uint16_t getDataUint16(uint8_t position) const; + + void setUint32Data(uint32_t data); + uint32_t getUint32Data() const; + + /** + * Stored in Bytes 13-16 of message data + * @param length + */ + void setDataLen(uint32_t length); + + uint32_t getDataLen() const; + + /** + * Stored in last four bytes (Bytes 17-20) of message data + * @param sendData + */ + void setDataPointer(const void * data); + + /** + * In case the send request data or reply data is to be stored in a buffer, + * a buffer Position can be stored here as the seventh and eigth byte of + * the message, so the receive buffer can't be larger than sizeof(uint16_t) for now. + * @param bufferPosition In case the data is stored in a buffer, the position can be supplied here + */ + void setBufferPosition(uint16_t bufferPosition); + uint16_t getBufferPosition() const; + void setStoreId(store_address_t storeId); + store_address_t getStoreId() const; + + /** + * Clear the message. Deletes IPC Store data + * and sets all data to 0. Also sets message type to NONE + */ + void clearCommunicationMessage(); +private: + bool uninitialized; //!< Could be used to warn if data has not been set. +}; + +#endif /* FRAMEWORK_DEVICEHANDLERS_COMMUNICATIONMESSAGE_H_ */ diff --git a/devicehandlers/DeviceCommunicationIF.h b/devicehandlers/DeviceCommunicationIF.h index cbff5411..db37eade 100644 --- a/devicehandlers/DeviceCommunicationIF.h +++ b/devicehandlers/DeviceCommunicationIF.h @@ -1,127 +1,127 @@ -#ifndef FRAMEWORK_DEVICES_DEVICECOMMUNICATIONIF_H_ -#define FRAMEWORK_DEVICES_DEVICECOMMUNICATIONIF_H_ - -#include "../devicehandlers/CookieIF.h" -#include "../devicehandlers/DeviceHandlerIF.h" -#include "../returnvalues/HasReturnvaluesIF.h" -/** - * @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; - - //! This is returned in readReceivedMessage() if no reply was reived. - static const ReturnValue_t NO_REPLY_RECEIVED = MAKE_RETURN_CODE(0x01); - - //! General protocol error. Define more concrete errors in child handler - static const ReturnValue_t PROTOCOL_ERROR = MAKE_RETURN_CODE(0x02); - //! If cookie is a null pointer - static const ReturnValue_t NULLPOINTER = MAKE_RETURN_CODE(0x03); - static const ReturnValue_t INVALID_COOKIE_TYPE = MAKE_RETURN_CODE(0x04); - // is this needed if there is no open/close call? - static const ReturnValue_t NOT_ACTIVE = MAKE_RETURN_CODE(0x05); - static const ReturnValue_t TOO_MUCH_DATA = MAKE_RETURN_CODE(0x06); - - 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 If this is 0, nothing shall be sent. - * @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_ */ +#ifndef FRAMEWORK_DEVICES_DEVICECOMMUNICATIONIF_H_ +#define FRAMEWORK_DEVICES_DEVICECOMMUNICATIONIF_H_ + +#include "../devicehandlers/CookieIF.h" +#include "../devicehandlers/DeviceHandlerIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +/** + * @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; + + //! This is returned in readReceivedMessage() if no reply was reived. + static const ReturnValue_t NO_REPLY_RECEIVED = MAKE_RETURN_CODE(0x01); + + //! General protocol error. Define more concrete errors in child handler + static const ReturnValue_t PROTOCOL_ERROR = MAKE_RETURN_CODE(0x02); + //! If cookie is a null pointer + static const ReturnValue_t NULLPOINTER = MAKE_RETURN_CODE(0x03); + static const ReturnValue_t INVALID_COOKIE_TYPE = MAKE_RETURN_CODE(0x04); + // is this needed if there is no open/close call? + static const ReturnValue_t NOT_ACTIVE = MAKE_RETURN_CODE(0x05); + static const ReturnValue_t TOO_MUCH_DATA = MAKE_RETURN_CODE(0x06); + + 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 If this is 0, nothing shall be sent. + * @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/devicehandlers/DeviceHandlerIF.h b/devicehandlers/DeviceHandlerIF.h index 67ab37f7..e3fa3b88 100644 --- a/devicehandlers/DeviceHandlerIF.h +++ b/devicehandlers/DeviceHandlerIF.h @@ -1,155 +1,155 @@ -#ifndef DEVICEHANDLERIF_H_ -#define DEVICEHANDLERIF_H_ - -#include "../action/HasActionsIF.h" -#include "../devicehandlers/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. - //! It is possible to set the mode to _MODE_SHUT_DOWN to use the to off - //! transition if available. - 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 no command data was given when expected. - 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_ */ +#ifndef DEVICEHANDLERIF_H_ +#define DEVICEHANDLERIF_H_ + +#include "../action/HasActionsIF.h" +#include "../devicehandlers/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. + //! It is possible to set the mode to _MODE_SHUT_DOWN to use the to off + //! transition if available. + 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 no command data was given when expected. + 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/devicehandlers/DeviceHandlerMessage.cpp b/devicehandlers/DeviceHandlerMessage.cpp index 95e5ae0c..b4963dbf 100644 --- a/devicehandlers/DeviceHandlerMessage.cpp +++ b/devicehandlers/DeviceHandlerMessage.cpp @@ -1,101 +1,101 @@ -#include "../objectmanager/ObjectManagerIF.h" -#include "../devicehandlers/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_ADDRESS); - 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_ADDRESS: - case CMD_WIRETAPPING: - message->setCommand(CommandMessage::CMD_NONE); - message->setParameter(0); - message->setParameter2(0); - break; - } - -} +#include "../objectmanager/ObjectManagerIF.h" +#include "../devicehandlers/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_ADDRESS); + 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_ADDRESS: + case CMD_WIRETAPPING: + message->setCommand(CommandMessage::CMD_NONE); + message->setParameter(0); + message->setParameter2(0); + break; + } + +} diff --git a/devicehandlers/DeviceHandlerMessage.h b/devicehandlers/DeviceHandlerMessage.h index 6b54392b..5f4583d2 100644 --- a/devicehandlers/DeviceHandlerMessage.h +++ b/devicehandlers/DeviceHandlerMessage.h @@ -1,91 +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_ADDRESS = 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_ */ +#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_ADDRESS = 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/devicehandlers/DeviceTmReportingWrapper.cpp b/devicehandlers/DeviceTmReportingWrapper.cpp index f19b02e9..25d8932f 100644 --- a/devicehandlers/DeviceTmReportingWrapper.cpp +++ b/devicehandlers/DeviceTmReportingWrapper.cpp @@ -1,46 +1,46 @@ -#include "../serialize/SerializeAdapter.h" -#include "../devicehandlers/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); -} +#include "../serialize/SerializeAdapter.h" +#include "../devicehandlers/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/devicehandlers/DeviceTmReportingWrapper.h b/devicehandlers/DeviceTmReportingWrapper.h index ecf7089a..a14c4261 100644 --- a/devicehandlers/DeviceTmReportingWrapper.h +++ b/devicehandlers/DeviceTmReportingWrapper.h @@ -1,27 +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_ */ +#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/devicehandlers/HealthDevice.cpp b/devicehandlers/HealthDevice.cpp index de7e4d8c..666f86bb 100644 --- a/devicehandlers/HealthDevice.cpp +++ b/devicehandlers/HealthDevice.cpp @@ -1,59 +1,59 @@ -#include "../devicehandlers/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, CommandMessage::MINIMUM_COMMAND_MESSAGE_SIZE); -} - -HealthDevice::~HealthDevice() { - QueueFactory::instance()->deleteMessageQueue(commandQueue); -} - -ReturnValue_t HealthDevice::performOperation(uint8_t opCode) { - CommandMessage command; - ReturnValue_t result = commandQueue->receiveMessage(&command); - if (result == HasReturnvaluesIF::RETURN_OK) { - healthHelper.handleHealthCommand(&command); - } - 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(); -} +#include "../devicehandlers/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, CommandMessage::MINIMUM_COMMAND_MESSAGE_SIZE); +} + +HealthDevice::~HealthDevice() { + QueueFactory::instance()->deleteMessageQueue(commandQueue); +} + +ReturnValue_t HealthDevice::performOperation(uint8_t opCode) { + CommandMessage command; + ReturnValue_t result = commandQueue->receiveMessage(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + healthHelper.handleHealthCommand(&command); + } + 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/devicehandlers/HealthDevice.h b/devicehandlers/HealthDevice.h index fb38584e..53b0ab09 100644 --- a/devicehandlers/HealthDevice.h +++ b/devicehandlers/HealthDevice.h @@ -1,40 +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_ */ +#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/fdir/ConfirmsFailuresIF.h b/fdir/ConfirmsFailuresIF.h index 1896d24a..6cb2c47d 100644 --- a/fdir/ConfirmsFailuresIF.h +++ b/fdir/ConfirmsFailuresIF.h @@ -1,20 +1,20 @@ -#ifndef FRAMEWORK_FDIR_CONFIRMSFAILURESIF_H_ -#define FRAMEWORK_FDIR_CONFIRMSFAILURESIF_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../ipc/MessageQueueSenderIF.h" - -// TODO: Documentation. -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_ */ +#ifndef FRAMEWORK_FDIR_CONFIRMSFAILURESIF_H_ +#define FRAMEWORK_FDIR_CONFIRMSFAILURESIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +// TODO: Documentation. +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/fdir/EventCorrelation.cpp b/fdir/EventCorrelation.cpp index dea6a569..f98d2223 100644 --- a/fdir/EventCorrelation.cpp +++ b/fdir/EventCorrelation.cpp @@ -1,44 +1,44 @@ -#include "../fdir/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; - } -} +#include "../fdir/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/fdir/EventCorrelation.h b/fdir/EventCorrelation.h index bbfe92c1..b8793edf 100644 --- a/fdir/EventCorrelation.h +++ b/fdir/EventCorrelation.h @@ -1,23 +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_ */ +#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/fdir/FailureIsolationBase.cpp b/fdir/FailureIsolationBase.cpp index b9994dc3..ab462a97 100644 --- a/fdir/FailureIsolationBase.cpp +++ b/fdir/FailureIsolationBase.cpp @@ -1,164 +1,164 @@ -#include "../events/EventManagerIF.h" -#include "../fdir/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()); -} +#include "../events/EventManagerIF.h" +#include "../fdir/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/fdir/FailureIsolationBase.h b/fdir/FailureIsolationBase.h index b2735e6d..f8a307b3 100644 --- a/fdir/FailureIsolationBase.h +++ b/fdir/FailureIsolationBase.h @@ -1,56 +1,56 @@ -#ifndef FRAMEWORK_FDIR_FAILUREISOLATIONBASE_H_ -#define FRAMEWORK_FDIR_FAILUREISOLATIONBASE_H_ - -#include "../events/EventMessage.h" -#include "../fdir/ConfirmsFailuresIF.h" -#include "../fdir/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_ */ +#ifndef FRAMEWORK_FDIR_FAILUREISOLATIONBASE_H_ +#define FRAMEWORK_FDIR_FAILUREISOLATIONBASE_H_ + +#include "../events/EventMessage.h" +#include "../fdir/ConfirmsFailuresIF.h" +#include "../fdir/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/fdir/FaultCounter.cpp b/fdir/FaultCounter.cpp index 674d278a..b75bd48f 100644 --- a/fdir/FaultCounter.cpp +++ b/fdir/FaultCounter.cpp @@ -1,86 +1,86 @@ -#include "../fdir/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; -} +#include "../fdir/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/fdir/FaultCounter.h b/fdir/FaultCounter.h index 17e2a3df..dc929deb 100644 --- a/fdir/FaultCounter.h +++ b/fdir/FaultCounter.h @@ -1,38 +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_ */ +#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/globalfunctions/AsciiConverter.cpp b/globalfunctions/AsciiConverter.cpp index f54af6a8..c57125e2 100644 --- a/globalfunctions/AsciiConverter.cpp +++ b/globalfunctions/AsciiConverter.cpp @@ -1,230 +1,230 @@ -#include "../globalfunctions/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; -} +#include "../globalfunctions/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/globalfunctions/AsciiConverter.h b/globalfunctions/AsciiConverter.h index 27bed4e7..7af3cb3d 100644 --- a/globalfunctions/AsciiConverter.h +++ b/globalfunctions/AsciiConverter.h @@ -1,39 +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_ */ +#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/globalfunctions/CRC.cpp b/globalfunctions/CRC.cpp index 6156b558..1258b698 100644 --- a/globalfunctions/CRC.cpp +++ b/globalfunctions/CRC.cpp @@ -1,139 +1,139 @@ -#include "../globalfunctions/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 + +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= 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 preven 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; - } -} - +#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 preven 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/globalfunctions/DleEncoder.h b/globalfunctions/DleEncoder.h index 3c327a55..3c535d1f 100644 --- a/globalfunctions/DleEncoder.h +++ b/globalfunctions/DleEncoder.h @@ -1,79 +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 occurences 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 enoded 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 occurences - //! 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 decoing 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_ */ +#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 occurences 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 enoded 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 occurences + //! 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 decoing 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/globalfunctions/Type.cpp b/globalfunctions/Type.cpp index 9589bed3..97f38473 100644 --- a/globalfunctions/Type.cpp +++ b/globalfunctions/Type.cpp @@ -1,181 +1,181 @@ -#include "../globalfunctions/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; -} +#include "../globalfunctions/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/globalfunctions/Type.h b/globalfunctions/Type.h index c10511f8..6910f909 100644 --- a/globalfunctions/Type.h +++ b/globalfunctions/Type.h @@ -1,94 +1,94 @@ -#ifndef TYPE_H_ -#define TYPE_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../serialize/SerializeIF.h" - -/** - * @brief Type definition for CCSDS or ECSS. - */ -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_ */ +#ifndef TYPE_H_ +#define TYPE_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerializeIF.h" + +/** + * @brief Type definition for CCSDS or ECSS. + */ +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/globalfunctions/arrayprinter.cpp b/globalfunctions/arrayprinter.cpp index 1e58ca75..fa89c0dc 100644 --- a/globalfunctions/arrayprinter.cpp +++ b/globalfunctions/arrayprinter.cpp @@ -1,61 +1,61 @@ -#include "../globalfunctions/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 << "\r\n" << std::flush; - } - } - - } - 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; -} +#include "../globalfunctions/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 << "\r\n" << std::flush; + } + } + + } + 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/globalfunctions/matching/BinaryMatcher.h b/globalfunctions/matching/BinaryMatcher.h index 60e055a1..550439ab 100644 --- a/globalfunctions/matching/BinaryMatcher.h +++ b/globalfunctions/matching/BinaryMatcher.h @@ -1,41 +1,41 @@ -#ifndef BINARYMATCHER_H_ -#define BINARYMATCHER_H_ - -#include "../../globalfunctions/matching/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_ */ +#ifndef BINARYMATCHER_H_ +#define BINARYMATCHER_H_ + +#include "../../globalfunctions/matching/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/globalfunctions/matching/DecimalMatcher.h b/globalfunctions/matching/DecimalMatcher.h index 24f399ef..3c765cd7 100644 --- a/globalfunctions/matching/DecimalMatcher.h +++ b/globalfunctions/matching/DecimalMatcher.h @@ -1,50 +1,50 @@ -#ifndef DECIMALMATCHER_H_ -#define DECIMALMATCHER_H_ - -#include "../../globalfunctions/matching/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_ */ +#ifndef DECIMALMATCHER_H_ +#define DECIMALMATCHER_H_ + +#include "../../globalfunctions/matching/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/globalfunctions/matching/MatchTree.h b/globalfunctions/matching/MatchTree.h index 2d380bbb..46245c25 100644 --- a/globalfunctions/matching/MatchTree.h +++ b/globalfunctions/matching/MatchTree.h @@ -1,216 +1,216 @@ -#ifndef FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_ -#define FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_ - -#include "../../container/BinaryTree.h" -#include "../../globalfunctions/matching/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_ */ +#ifndef FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_ +#define FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_ + +#include "../../container/BinaryTree.h" +#include "../../globalfunctions/matching/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/globalfunctions/matching/RangeMatcher.h b/globalfunctions/matching/RangeMatcher.h index ac67e8b8..04c9c3fa 100644 --- a/globalfunctions/matching/RangeMatcher.h +++ b/globalfunctions/matching/RangeMatcher.h @@ -1,70 +1,70 @@ -#ifndef RANGEMATCHER_H_ -#define RANGEMATCHER_H_ - -#include "../../globalfunctions/matching/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_ */ +#ifndef RANGEMATCHER_H_ +#define RANGEMATCHER_H_ + +#include "../../globalfunctions/matching/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/globalfunctions/matching/SerializeableMatcherIF.h b/globalfunctions/matching/SerializeableMatcherIF.h index 0d2873ef..f99c6964 100644 --- a/globalfunctions/matching/SerializeableMatcherIF.h +++ b/globalfunctions/matching/SerializeableMatcherIF.h @@ -1,13 +1,13 @@ -#ifndef FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ -#define FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ - -#include "../../globalfunctions/matching/MatcherIF.h" -#include "../../serialize/SerializeIF.h" - -template -class SerializeableMatcherIF : public MatcherIF, public SerializeIF { -public: - virtual ~SerializeableMatcherIF() {} -}; - -#endif /* FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ */ +#ifndef FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ +#define FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ + +#include "../../globalfunctions/matching/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/globalfunctions/math/QuaternionOperations.cpp b/globalfunctions/math/QuaternionOperations.cpp index 448d30f1..c6778de4 100644 --- a/globalfunctions/math/QuaternionOperations.cpp +++ b/globalfunctions/math/QuaternionOperations.cpp @@ -1,156 +1,156 @@ -#include "QuaternionOperations.h" -#include "../../globalfunctions/math/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]); - } - } -} +#include "QuaternionOperations.h" +#include "../../globalfunctions/math/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/globalfunctions/timevalOperations.cpp b/globalfunctions/timevalOperations.cpp index ae49ef21..1228da04 100644 --- a/globalfunctions/timevalOperations.cpp +++ b/globalfunctions/timevalOperations.cpp @@ -1,99 +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; -} +#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/health/HasHealthIF.h b/health/HasHealthIF.h index 9ef8c1e9..ac404300 100644 --- a/health/HasHealthIF.h +++ b/health/HasHealthIF.h @@ -1,50 +1,50 @@ -#ifndef HASHEALTHIF_H_ -#define HASHEALTHIF_H_ - -#include "../events/Event.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../ipc/MessageQueueSenderIF.h" - -class HasHealthIF { -public: - - typedef enum { - HEALTHY = 1, FAULTY = 0, EXTERNAL_CONTROL = 2, NEEDS_RECOVERY = 3, PERMANENT_FAULTY = 4 - } HealthState; - - 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; - - /** - * set the Health State - * - * The parent will be informed, if the Health changes - * - * @param health - */ - virtual ReturnValue_t setHealth(HealthState health) = 0; - - /** - * get Health State - * - * @return Health State of the object - */ - virtual HasHealthIF::HealthState getHealth() = 0; -}; - -#endif /* HASHEALTHIF_H_ */ +#ifndef HASHEALTHIF_H_ +#define HASHEALTHIF_H_ + +#include "../events/Event.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MessageQueueSenderIF.h" + +class HasHealthIF { +public: + + typedef enum { + HEALTHY = 1, FAULTY = 0, EXTERNAL_CONTROL = 2, NEEDS_RECOVERY = 3, PERMANENT_FAULTY = 4 + } HealthState; + + 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; + + /** + * set the Health State + * + * The parent will be informed, if the Health changes + * + * @param health + */ + virtual ReturnValue_t setHealth(HealthState health) = 0; + + /** + * get Health State + * + * @return Health State of the object + */ + virtual HasHealthIF::HealthState getHealth() = 0; +}; + +#endif /* HASHEALTHIF_H_ */ diff --git a/health/HealthHelper.cpp b/health/HealthHelper.cpp index 800d6e36..61718f99 100644 --- a/health/HealthHelper.cpp +++ b/health/HealthHelper.cpp @@ -1,105 +1,105 @@ -#include "../health/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 == MessageQueueMessageIF::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() == MessageQueueMessageIF::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; - - } -} +#include "../health/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 == MessageQueueMessageIF::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() == MessageQueueMessageIF::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/health/HealthHelper.h b/health/HealthHelper.h index 99bb501f..d8b81ca9 100644 --- a/health/HealthHelper.h +++ b/health/HealthHelper.h @@ -1,121 +1,121 @@ -#ifndef FRAMEWORK_HEALTH_HEALTHHELPER_H_ -#define FRAMEWORK_HEALTH_HEALTHHELPER_H_ - -#include "../events/EventManagerIF.h" -#include "../events/EventReportingProxyIF.h" -#include "../health/HasHealthIF.h" -#include "../health/HealthMessage.h" -#include "../health/HealthTableIF.h" -#include "../ipc/MessageQueueIF.h" -#include "../objectmanager/ObjectManagerIF.h" -#include "../returnvalues/HasReturnvaluesIF.h" - -/** - * Helper class for Objects that implement HasHealthIF - * - * 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: - - /** - * ctor - * - * @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 /* HEALTHHELPER_H_ */ +#ifndef FRAMEWORK_HEALTH_HEALTHHELPER_H_ +#define FRAMEWORK_HEALTH_HEALTHHELPER_H_ + +#include "../events/EventManagerIF.h" +#include "../events/EventReportingProxyIF.h" +#include "../health/HasHealthIF.h" +#include "../health/HealthMessage.h" +#include "../health/HealthTableIF.h" +#include "../ipc/MessageQueueIF.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" + +/** + * Helper class for Objects that implement HasHealthIF + * + * 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: + + /** + * ctor + * + * @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 /* HEALTHHELPER_H_ */ diff --git a/health/HealthMessage.cpp b/health/HealthMessage.cpp index c5f5b733..39c53fb1 100644 --- a/health/HealthMessage.cpp +++ b/health/HealthMessage.cpp @@ -1,28 +1,28 @@ -#include "../health/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(); -} +#include "../health/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/health/HealthMessage.h b/health/HealthMessage.h index d16c65db..fa773d42 100644 --- a/health/HealthMessage.h +++ b/health/HealthMessage.h @@ -1,30 +1,30 @@ -#ifndef HEALTHMESSAGE_H_ -#define HEALTHMESSAGE_H_ - -#include "../health/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);//REPLY_COMMAND_OK/REPLY_REJECTED - static const Command_t HEALTH_ANNOUNCE = MAKE_COMMAND_ID(3); //NO REPLY! - 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 /* HEALTHMESSAGE_H_ */ +#ifndef HEALTHMESSAGE_H_ +#define HEALTHMESSAGE_H_ + +#include "../health/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);//REPLY_COMMAND_OK/REPLY_REJECTED + static const Command_t HEALTH_ANNOUNCE = MAKE_COMMAND_ID(3); //NO REPLY! + 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 /* HEALTHMESSAGE_H_ */ diff --git a/health/HealthTable.cpp b/health/HealthTable.cpp index 88db89b1..1aa5a3ae 100644 --- a/health/HealthTable.cpp +++ b/health/HealthTable.cpp @@ -1,100 +1,100 @@ -#include "../health/HealthTable.h" -#include "../serialize/SerializeAdapter.h" -#include "../ipc/MutexFactory.h" - -HealthTable::HealthTable(object_id_t objectid) : - SystemObject(objectid) { - mutex = MutexFactory::instance()->createMutex();; - - mapIterator = healthMap.begin(); -} - -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.insert( - std::pair(object, - initilialState)); - return HasReturnvaluesIF::RETURN_OK; -} - -void HealthTable::setHealth(object_id_t object, - HasHealthIF::HealthState newState) { - mutex->lockMutex(MutexIF::BLOCKING); - HealthMap::iterator iter = healthMap.find(object); - if (iter != healthMap.end()) { - iter->second = newState; - } - mutex->unlockMutex(); -} - -HasHealthIF::HealthState HealthTable::getHealth(object_id_t object) { - HasHealthIF::HealthState state = HasHealthIF::HEALTHY; - mutex->lockMutex(MutexIF::BLOCKING); - HealthMap::iterator iter = healthMap.find(object); - if (iter != healthMap.end()) { - state = iter->second; - } - mutex->unlockMutex(); - return state; -} - -uint32_t HealthTable::getPrintSize() { - mutex->lockMutex(MutexIF::BLOCKING); - uint32_t size = healthMap.size() * 5 + 2; - mutex->unlockMutex(); - return size; -} - -bool HealthTable::hasHealth(object_id_t object) { - bool exits = false; - mutex->lockMutex(MutexIF::BLOCKING); - HealthMap::iterator iter = healthMap.find(object); - if (iter != healthMap.end()) { - exits = true; - } - mutex->unlockMutex(); - return exits; -} - -void HealthTable::printAll(uint8_t* pointer, size_t maxSize) { - mutex->lockMutex(MutexIF::BLOCKING); - size_t size = 0; - uint16_t count = healthMap.size(); - ReturnValue_t result = SerializeAdapter::serialize(&count, - &pointer, &size, maxSize, SerializeIF::Endianness::BIG); - HealthMap::iterator iter; - for (iter = healthMap.begin(); - iter != healthMap.end() && result == HasReturnvaluesIF::RETURN_OK; - ++iter) { - result = SerializeAdapter::serialize(&iter->first, - &pointer, &size, maxSize, SerializeIF::Endianness::BIG); - uint8_t health = iter->second; - result = SerializeAdapter::serialize(&health, &pointer, &size, - maxSize, SerializeIF::Endianness::BIG); - } - mutex->unlockMutex(); -} - -ReturnValue_t HealthTable::iterate( - std::pair *value, bool reset) { - ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; - mutex->lockMutex(MutexIF::BLOCKING); - if (reset) { - mapIterator = healthMap.begin(); - } - if (mapIterator == healthMap.end()) { - result = HasReturnvaluesIF::RETURN_FAILED; - } - *value = *mapIterator; - mapIterator++; - mutex->unlockMutex(); - - return result; -} +#include "../health/HealthTable.h" +#include "../serialize/SerializeAdapter.h" +#include "../ipc/MutexFactory.h" + +HealthTable::HealthTable(object_id_t objectid) : + SystemObject(objectid) { + mutex = MutexFactory::instance()->createMutex();; + + mapIterator = healthMap.begin(); +} + +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.insert( + std::pair(object, + initilialState)); + return HasReturnvaluesIF::RETURN_OK; +} + +void HealthTable::setHealth(object_id_t object, + HasHealthIF::HealthState newState) { + mutex->lockMutex(MutexIF::BLOCKING); + HealthMap::iterator iter = healthMap.find(object); + if (iter != healthMap.end()) { + iter->second = newState; + } + mutex->unlockMutex(); +} + +HasHealthIF::HealthState HealthTable::getHealth(object_id_t object) { + HasHealthIF::HealthState state = HasHealthIF::HEALTHY; + mutex->lockMutex(MutexIF::BLOCKING); + HealthMap::iterator iter = healthMap.find(object); + if (iter != healthMap.end()) { + state = iter->second; + } + mutex->unlockMutex(); + return state; +} + +uint32_t HealthTable::getPrintSize() { + mutex->lockMutex(MutexIF::BLOCKING); + uint32_t size = healthMap.size() * 5 + 2; + mutex->unlockMutex(); + return size; +} + +bool HealthTable::hasHealth(object_id_t object) { + bool exits = false; + mutex->lockMutex(MutexIF::BLOCKING); + HealthMap::iterator iter = healthMap.find(object); + if (iter != healthMap.end()) { + exits = true; + } + mutex->unlockMutex(); + return exits; +} + +void HealthTable::printAll(uint8_t* pointer, size_t maxSize) { + mutex->lockMutex(MutexIF::BLOCKING); + size_t size = 0; + uint16_t count = healthMap.size(); + ReturnValue_t result = SerializeAdapter::serialize(&count, + &pointer, &size, maxSize, SerializeIF::Endianness::BIG); + HealthMap::iterator iter; + for (iter = healthMap.begin(); + iter != healthMap.end() && result == HasReturnvaluesIF::RETURN_OK; + ++iter) { + result = SerializeAdapter::serialize(&iter->first, + &pointer, &size, maxSize, SerializeIF::Endianness::BIG); + uint8_t health = iter->second; + result = SerializeAdapter::serialize(&health, &pointer, &size, + maxSize, SerializeIF::Endianness::BIG); + } + mutex->unlockMutex(); +} + +ReturnValue_t HealthTable::iterate( + std::pair *value, bool reset) { + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + mutex->lockMutex(MutexIF::BLOCKING); + if (reset) { + mapIterator = healthMap.begin(); + } + if (mapIterator == healthMap.end()) { + result = HasReturnvaluesIF::RETURN_FAILED; + } + *value = *mapIterator; + mapIterator++; + mutex->unlockMutex(); + + return result; +} diff --git a/health/HealthTable.h b/health/HealthTable.h index 5ed45041..0f54a264 100644 --- a/health/HealthTable.h +++ b/health/HealthTable.h @@ -1,35 +1,35 @@ -#ifndef FRAMEWORK_HEALTH_HEALTHTABLE_H_ -#define FRAMEWORK_HEALTH_HEALTHTABLE_H_ - -#include "../health/HealthTableIF.h" -#include "../objectmanager/SystemObject.h" -#include "../ipc/MutexIF.h" -#include - -typedef std::map HealthMap; - -class HealthTable: public HealthTableIF, public SystemObject { -public: - HealthTable(object_id_t objectid); - virtual ~HealthTable(); - - virtual ReturnValue_t registerObject(object_id_t object, - HasHealthIF::HealthState initilialState = HasHealthIF::HEALTHY); - - virtual bool hasHealth(object_id_t object); - virtual void setHealth(object_id_t object, HasHealthIF::HealthState newState); - virtual HasHealthIF::HealthState getHealth(object_id_t); - - virtual uint32_t getPrintSize(); - virtual void printAll(uint8_t *pointer, size_t maxSize); - -protected: - MutexIF* mutex; - HealthMap healthMap; - - HealthMap::iterator mapIterator; - - virtual ReturnValue_t iterate(std::pair *value, bool reset = false); -}; - -#endif /* HEALTHTABLE_H_ */ +#ifndef FRAMEWORK_HEALTH_HEALTHTABLE_H_ +#define FRAMEWORK_HEALTH_HEALTHTABLE_H_ + +#include "../health/HealthTableIF.h" +#include "../objectmanager/SystemObject.h" +#include "../ipc/MutexIF.h" +#include + +typedef std::map HealthMap; + +class HealthTable: public HealthTableIF, public SystemObject { +public: + HealthTable(object_id_t objectid); + virtual ~HealthTable(); + + virtual ReturnValue_t registerObject(object_id_t object, + HasHealthIF::HealthState initilialState = HasHealthIF::HEALTHY); + + virtual bool hasHealth(object_id_t object); + virtual void setHealth(object_id_t object, HasHealthIF::HealthState newState); + virtual HasHealthIF::HealthState getHealth(object_id_t); + + virtual uint32_t getPrintSize(); + virtual void printAll(uint8_t *pointer, size_t maxSize); + +protected: + MutexIF* mutex; + HealthMap healthMap; + + HealthMap::iterator mapIterator; + + virtual ReturnValue_t iterate(std::pair *value, bool reset = false); +}; + +#endif /* HEALTHTABLE_H_ */ diff --git a/health/HealthTableIF.h b/health/HealthTableIF.h index bc4a8cfb..f18f0315 100644 --- a/health/HealthTableIF.h +++ b/health/HealthTableIF.h @@ -1,28 +1,28 @@ -#ifndef FRAMEWORK_HEALTH_HEALTHTABLEIF_H_ -#define FRAMEWORK_HEALTH_HEALTHTABLEIF_H_ - -#include "../health/ManagesHealthIF.h" -#include "../objectmanager/ObjectManagerIF.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#include - - -class HealthTableIF: public ManagesHealthIF { - // TODO: This is in the mission folder and not in the framework folder. - // delete it? - friend class HealthCommandingService; -public: - virtual ~HealthTableIF() { - } - - virtual ReturnValue_t registerObject(object_id_t object, - HasHealthIF::HealthState initilialState = HasHealthIF::HEALTHY) = 0; - - virtual uint32_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_ */ +#ifndef FRAMEWORK_HEALTH_HEALTHTABLEIF_H_ +#define FRAMEWORK_HEALTH_HEALTHTABLEIF_H_ + +#include "../health/ManagesHealthIF.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include + + +class HealthTableIF: public ManagesHealthIF { + // TODO: This is in the mission folder and not in the framework folder. + // delete it? + friend class HealthCommandingService; +public: + virtual ~HealthTableIF() { + } + + virtual ReturnValue_t registerObject(object_id_t object, + HasHealthIF::HealthState initilialState = HasHealthIF::HEALTHY) = 0; + + virtual uint32_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/health/ManagesHealthIF.h b/health/ManagesHealthIF.h index 57996dda..8ca17012 100644 --- a/health/ManagesHealthIF.h +++ b/health/ManagesHealthIF.h @@ -1,52 +1,52 @@ -#ifndef FRAMEWORK_HEALTH_MANAGESHEALTHIF_H_ -#define FRAMEWORK_HEALTH_MANAGESHEALTHIF_H_ - -#include "../health/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 /* FRAMEWORK_HEALTH_MANAGESHEALTHIF_H_ */ +#ifndef FRAMEWORK_HEALTH_MANAGESHEALTHIF_H_ +#define FRAMEWORK_HEALTH_MANAGESHEALTHIF_H_ + +#include "../health/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 /* FRAMEWORK_HEALTH_MANAGESHEALTHIF_H_ */ diff --git a/housekeeping/AcceptsHkPacketsIF.h b/housekeeping/AcceptsHkPacketsIF.h index 879aaf8e..b3ba6e7c 100644 --- a/housekeeping/AcceptsHkPacketsIF.h +++ b/housekeeping/AcceptsHkPacketsIF.h @@ -1,11 +1,11 @@ -#ifndef FRAMEWORK_HOUSEKEEPING_ACCEPTSHKPACKETSIF_H_ -#define FRAMEWORK_HOUSEKEEPING_ACCEPTSHKPACKETSIF_H_ -#include "../ipc/MessageQueueMessageIF.h" - -class AcceptsHkPacketsIF { -public: - virtual~ AcceptsHkPacketsIF() {}; - virtual MessageQueueId_t getHkQueue() const = 0; -}; - -#endif /* FRAMEWORK_HOUSEKEEPING_ACCEPTSHKPACKETSIF_H_ */ +#ifndef FRAMEWORK_HOUSEKEEPING_ACCEPTSHKPACKETSIF_H_ +#define FRAMEWORK_HOUSEKEEPING_ACCEPTSHKPACKETSIF_H_ +#include "../ipc/MessageQueueMessageIF.h" + +class AcceptsHkPacketsIF { +public: + virtual~ AcceptsHkPacketsIF() {}; + virtual MessageQueueId_t getHkQueue() const = 0; +}; + +#endif /* FRAMEWORK_HOUSEKEEPING_ACCEPTSHKPACKETSIF_H_ */ diff --git a/internalError/InternalErrorReporter.cpp b/internalError/InternalErrorReporter.cpp index ed064da6..3424266b 100644 --- a/internalError/InternalErrorReporter.cpp +++ b/internalError/InternalErrorReporter.cpp @@ -1,125 +1,125 @@ -#include "../datapoolglob/GlobalDataSet.h" -#include "InternalErrorReporter.h" - -#include "../datapoolglob/GlobalPoolVariable.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) { - - GlobDataSet mySet; - gp_uint32_t queueHitsInPool(queuePoolId, &mySet, - PoolVariableIF::VAR_READ_WRITE); - gp_uint32_t tmHitsInPool(tmPoolId, &mySet, - PoolVariableIF::VAR_READ_WRITE); - - gp_uint32_t 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(); -} +#include "../datapoolglob/GlobalDataSet.h" +#include "InternalErrorReporter.h" + +#include "../datapoolglob/GlobalPoolVariable.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) { + + GlobDataSet mySet; + gp_uint32_t queueHitsInPool(queuePoolId, &mySet, + PoolVariableIF::VAR_READ_WRITE); + gp_uint32_t tmHitsInPool(tmPoolId, &mySet, + PoolVariableIF::VAR_READ_WRITE); + + gp_uint32_t 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/internalError/InternalErrorReporter.h b/internalError/InternalErrorReporter.h index 1fe7426d..9aa24a0f 100644 --- a/internalError/InternalErrorReporter.h +++ b/internalError/InternalErrorReporter.h @@ -1,50 +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_ */ +#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/ipc/CommandMessage.cpp b/ipc/CommandMessage.cpp index 16293608..513debd3 100644 --- a/ipc/CommandMessage.cpp +++ b/ipc/CommandMessage.cpp @@ -1,111 +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); -} +#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/ipc/CommandMessage.h b/ipc/CommandMessage.h index 3be9a120..ca7b817f 100644 --- a/ipc/CommandMessage.h +++ b/ipc/CommandMessage.h @@ -1,129 +1,129 @@ -#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 2 4-byte parameters. - * @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 2 4-byte parameters. - */ - static constexpr size_t DEFAULT_COMMAND_MESSAGE_SIZE = - CommandMessageIF::MINIMUM_COMMAND_MESSAGE_SIZE + 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_ */ +#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 2 4-byte parameters. + * @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 2 4-byte parameters. + */ + static constexpr size_t DEFAULT_COMMAND_MESSAGE_SIZE = + CommandMessageIF::MINIMUM_COMMAND_MESSAGE_SIZE + 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/ipc/CommandMessageCleaner.cpp b/ipc/CommandMessageCleaner.cpp index a4dec78d..6a99b4d2 100644 --- a/ipc/CommandMessageCleaner.cpp +++ b/ipc/CommandMessageCleaner.cpp @@ -1,45 +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; - } -} +#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/ipc/CommandMessageCleaner.h b/ipc/CommandMessageCleaner.h index f17bf282..2bf4a193 100644 --- a/ipc/CommandMessageCleaner.h +++ b/ipc/CommandMessageCleaner.h @@ -1,16 +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_ */ +#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/ipc/CommandMessageIF.h b/ipc/CommandMessageIF.h index 68a8d956..aafa40ef 100644 --- a/ipc/CommandMessageIF.h +++ b/ipc/CommandMessageIF.h @@ -1,73 +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_ */ +#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/ipc/MessageQueueIF.h b/ipc/MessageQueueIF.h index 1e80f7bd..96bd379f 100644 --- a/ipc/MessageQueueIF.h +++ b/ipc/MessageQueueIF.h @@ -1,171 +1,171 @@ -#ifndef FSFW_IPC_MESSAGEQUEUEIF_H_ -#define FSFW_IPC_MESSAGEQUEUEIF_H_ - -// 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 - */ - -#include "../ipc/MessageQueueMessage.h" -#include "../ipc/MessageQueueSenderIF.h" -#include "../returnvalues/HasReturnvaluesIF.h" -class MessageQueueIF { -public: - static const MessageQueueId_t NO_QUEUE = MessageQueueMessageIF::NO_QUEUE; //!< Ugly hack. - - 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_ */ +#ifndef FSFW_IPC_MESSAGEQUEUEIF_H_ +#define FSFW_IPC_MESSAGEQUEUEIF_H_ + +// 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 + */ + +#include "../ipc/MessageQueueMessage.h" +#include "../ipc/MessageQueueSenderIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +class MessageQueueIF { +public: + static const MessageQueueId_t NO_QUEUE = MessageQueueMessageIF::NO_QUEUE; //!< Ugly hack. + + 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/ipc/MessageQueueMessage.cpp b/ipc/MessageQueueMessage.cpp index 2f79cb3b..dcd4def3 100644 --- a/ipc/MessageQueueMessage.cpp +++ b/ipc/MessageQueueMessage.cpp @@ -1,84 +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; -} - +#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/ipc/MessageQueueMessage.h b/ipc/MessageQueueMessage.h index da5c78cb..f68e9b9f 100644 --- a/ipc/MessageQueueMessage.h +++ b/ipc/MessageQueueMessage.h @@ -1,150 +1,150 @@ -#ifndef FSFW_IPC_MESSAGEQUEUEMESSAGE_H_ -#define FSFW_IPC_MESSAGEQUEUEMESSAGE_H_ - -#include "../ipc/MessageQueueMessageIF.h" -#include "../ipc/MessageQueueSenderIF.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_ */ +#ifndef FSFW_IPC_MESSAGEQUEUEMESSAGE_H_ +#define FSFW_IPC_MESSAGEQUEUEMESSAGE_H_ + +#include "../ipc/MessageQueueMessageIF.h" +#include "../ipc/MessageQueueSenderIF.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/ipc/MessageQueueSenderIF.h b/ipc/MessageQueueSenderIF.h index 80623157..b967bbdf 100644 --- a/ipc/MessageQueueSenderIF.h +++ b/ipc/MessageQueueSenderIF.h @@ -1,25 +1,25 @@ -#ifndef FRAMEWORK_IPC_MESSAGEQUEUESENDERIF_H_ -#define FRAMEWORK_IPC_MESSAGEQUEUESENDERIF_H_ - -#include "../ipc/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 = MessageQueueMessageIF::NO_QUEUE, - bool ignoreFault = false); -private: - MessageQueueSenderIF() {} -}; - - -#endif /* FRAMEWORK_IPC_MESSAGEQUEUESENDERIF_H_ */ +#ifndef FRAMEWORK_IPC_MESSAGEQUEUESENDERIF_H_ +#define FRAMEWORK_IPC_MESSAGEQUEUESENDERIF_H_ + +#include "../ipc/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 = MessageQueueMessageIF::NO_QUEUE, + bool ignoreFault = false); +private: + MessageQueueSenderIF() {} +}; + + +#endif /* FRAMEWORK_IPC_MESSAGEQUEUESENDERIF_H_ */ diff --git a/ipc/MutexFactory.h b/ipc/MutexFactory.h index 91e90907..f34d3b04 100644 --- a/ipc/MutexFactory.h +++ b/ipc/MutexFactory.h @@ -1,34 +1,34 @@ -#ifndef FRAMEWORK_IPC_MUTEXFACTORY_H_ -#define FRAMEWORK_IPC_MUTEXFACTORY_H_ - -#include "../ipc/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_ */ +#ifndef FRAMEWORK_IPC_MUTEXFACTORY_H_ +#define FRAMEWORK_IPC_MUTEXFACTORY_H_ + +#include "../ipc/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/ipc/MutexHelper.h b/ipc/MutexHelper.h index 97e28007..97001ade 100644 --- a/ipc/MutexHelper.h +++ b/ipc/MutexHelper.h @@ -1,30 +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_ */ +#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/ipc/MutexIF.h b/ipc/MutexIF.h index 7bca4c9d..5673e4d7 100644 --- a/ipc/MutexIF.h +++ b/ipc/MutexIF.h @@ -1,89 +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_ */ +#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/ipc/QueueFactory.h b/ipc/QueueFactory.h index e9891987..be554406 100644 --- a/ipc/QueueFactory.h +++ b/ipc/QueueFactory.h @@ -1,33 +1,33 @@ -#ifndef FRAMEWORK_IPC_QUEUEFACTORY_H_ -#define FRAMEWORK_IPC_QUEUEFACTORY_H_ - -#include "MessageQueueIF.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 /* FRAMEWORK_IPC_QUEUEFACTORY_H_ */ +#ifndef FRAMEWORK_IPC_QUEUEFACTORY_H_ +#define FRAMEWORK_IPC_QUEUEFACTORY_H_ + +#include "MessageQueueIF.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 /* FRAMEWORK_IPC_QUEUEFACTORY_H_ */ diff --git a/memory/AcceptsMemoryMessagesIF.h b/memory/AcceptsMemoryMessagesIF.h index 044f2236..4c222dec 100644 --- a/memory/AcceptsMemoryMessagesIF.h +++ b/memory/AcceptsMemoryMessagesIF.h @@ -1,21 +1,21 @@ -/** - * @file AcceptsMemoryMessagesIF.h - * @brief This file defines the AcceptsMemoryMessagesIF class. - * @date 11.07.2013 - * @author baetz - */ - -#ifndef ACCEPTSMEMORYMESSAGESIF_H_ -#define ACCEPTSMEMORYMESSAGESIF_H_ - -#include "../memory/HasMemoryIF.h" -#include "../memory/MemoryMessage.h" -#include "../ipc/MessageQueueSenderIF.h" - -class AcceptsMemoryMessagesIF : public HasMemoryIF { -public: - virtual MessageQueueId_t getCommandQueue() const = 0; -}; - - -#endif /* ACCEPTSMEMORYMESSAGESIF_H_ */ +/** + * @file AcceptsMemoryMessagesIF.h + * @brief This file defines the AcceptsMemoryMessagesIF class. + * @date 11.07.2013 + * @author baetz + */ + +#ifndef ACCEPTSMEMORYMESSAGESIF_H_ +#define ACCEPTSMEMORYMESSAGESIF_H_ + +#include "../memory/HasMemoryIF.h" +#include "../memory/MemoryMessage.h" +#include "../ipc/MessageQueueSenderIF.h" + +class AcceptsMemoryMessagesIF : public HasMemoryIF { +public: + virtual MessageQueueId_t getCommandQueue() const = 0; +}; + + +#endif /* ACCEPTSMEMORYMESSAGESIF_H_ */ diff --git a/memory/FileSystemMessage.cpp b/memory/FileSystemMessage.cpp index a1c3955f..30437a62 100644 --- a/memory/FileSystemMessage.cpp +++ b/memory/FileSystemMessage.cpp @@ -1,28 +1,28 @@ -/* - * FileSystemMessage.cpp - * - * Created on: 19.01.2020 - * Author: Jakob Meier - */ - -#include "FileSystemMessage.h" -#include "../objectmanager/ObjectManagerIF.h" - -ReturnValue_t FileSystemMessage::setWriteToFileCommand(CommandMessage* message, - MessageQueueId_t replyQueueId, store_address_t storageID) { - message->setCommand(WRITE_TO_FILE); - message->setParameter(replyQueueId); - message->setParameter2(storageID.raw); - return HasReturnvaluesIF::RETURN_OK; -} - -store_address_t FileSystemMessage::getStoreID(const CommandMessage* message) { - store_address_t temp; - temp.raw = message->getParameter2(); - return temp; -} - -MessageQueueId_t FileSystemMessage::getReplyQueueId(const CommandMessage* message){ - return message->getParameter(); -} - +/* + * FileSystemMessage.cpp + * + * Created on: 19.01.2020 + * Author: Jakob Meier + */ + +#include "FileSystemMessage.h" +#include "../objectmanager/ObjectManagerIF.h" + +ReturnValue_t FileSystemMessage::setWriteToFileCommand(CommandMessage* message, + MessageQueueId_t replyQueueId, store_address_t storageID) { + message->setCommand(WRITE_TO_FILE); + message->setParameter(replyQueueId); + message->setParameter2(storageID.raw); + return HasReturnvaluesIF::RETURN_OK; +} + +store_address_t FileSystemMessage::getStoreID(const CommandMessage* message) { + store_address_t temp; + temp.raw = message->getParameter2(); + return temp; +} + +MessageQueueId_t FileSystemMessage::getReplyQueueId(const CommandMessage* message){ + return message->getParameter(); +} + diff --git a/memory/FileSystemMessage.h b/memory/FileSystemMessage.h index 9ef9a2f4..da5b3181 100644 --- a/memory/FileSystemMessage.h +++ b/memory/FileSystemMessage.h @@ -1,30 +1,30 @@ -/* - * FileSystemMessage.h - * - * Created on: 19.01.2020 - * Author: Jakob Meier - */ - -#ifndef FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_ -#define FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_ - -#include "../ipc/CommandMessage.h" -#include "../storagemanager/StorageManagerIF.h" -#include "../objectmanager/SystemObject.h" - -class FileSystemMessage { -private: - FileSystemMessage(); //A private ctor inhibits instantiation -public: - static const uint8_t MESSAGE_ID = messagetypes::FILE_SYSTEM_MESSAGE; - static const Command_t CREATE_FILE = MAKE_COMMAND_ID( 0x01 ); - static const Command_t DELETE_FILE = MAKE_COMMAND_ID( 0x02 ); - static const Command_t WRITE_TO_FILE = MAKE_COMMAND_ID( 0x80 ); - - static ReturnValue_t setWriteToFileCommand(CommandMessage* message, MessageQueueId_t replyToQueue, store_address_t storageID ); - static store_address_t getStoreID( const CommandMessage* message ); - static MessageQueueId_t getReplyQueueId(const CommandMessage* message); - -}; - -#endif /* FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_ */ +/* + * FileSystemMessage.h + * + * Created on: 19.01.2020 + * Author: Jakob Meier + */ + +#ifndef FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_ +#define FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_ + +#include "../ipc/CommandMessage.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../objectmanager/SystemObject.h" + +class FileSystemMessage { +private: + FileSystemMessage(); //A private ctor inhibits instantiation +public: + static const uint8_t MESSAGE_ID = messagetypes::FILE_SYSTEM_MESSAGE; + static const Command_t CREATE_FILE = MAKE_COMMAND_ID( 0x01 ); + static const Command_t DELETE_FILE = MAKE_COMMAND_ID( 0x02 ); + static const Command_t WRITE_TO_FILE = MAKE_COMMAND_ID( 0x80 ); + + static ReturnValue_t setWriteToFileCommand(CommandMessage* message, MessageQueueId_t replyToQueue, store_address_t storageID ); + static store_address_t getStoreID( const CommandMessage* message ); + static MessageQueueId_t getReplyQueueId(const CommandMessage* message); + +}; + +#endif /* FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_ */ diff --git a/memory/HasFileSystemIF.h b/memory/HasFileSystemIF.h index 49a2ae54..b6d43981 100644 --- a/memory/HasFileSystemIF.h +++ b/memory/HasFileSystemIF.h @@ -1,36 +1,36 @@ -/* - * HasFileSystemIF.h - * - * Created on: 19.01.2020 - * Author: Jakob Meier - */ - -#ifndef FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_ -#define FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" - -class HasFileSystemIF { -public: - - virtual ~HasFileSystemIF() {} - /** - * Function to get the MessageQueueId_t of the implementing object - * @return MessageQueueId_t of the object - */ - virtual MessageQueueId_t getCommandQueue() const = 0; - /** - * Function to write to a file - * @param dirname Directory of the file - * @param filename The filename of the file - * @param data The data to write to the file - * @param size The size of the data to write - * @param packetNumber Counts the number of packets. For large files the write procedure must be split in multiple calls to writeToFile - */ - virtual ReturnValue_t writeToFile(const char* dirname, char* filename, const uint8_t* data, uint32_t size, uint16_t packetNumber) = 0; - virtual ReturnValue_t createFile(const char* dirname, const char* filename, const uint8_t* data, uint32_t size) = 0; - virtual ReturnValue_t deleteFile(const char* dirname, const char* filename) = 0; -}; - - -#endif /* FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_ */ +/* + * HasFileSystemIF.h + * + * Created on: 19.01.2020 + * Author: Jakob Meier + */ + +#ifndef FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_ +#define FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" + +class HasFileSystemIF { +public: + + virtual ~HasFileSystemIF() {} + /** + * Function to get the MessageQueueId_t of the implementing object + * @return MessageQueueId_t of the object + */ + virtual MessageQueueId_t getCommandQueue() const = 0; + /** + * Function to write to a file + * @param dirname Directory of the file + * @param filename The filename of the file + * @param data The data to write to the file + * @param size The size of the data to write + * @param packetNumber Counts the number of packets. For large files the write procedure must be split in multiple calls to writeToFile + */ + virtual ReturnValue_t writeToFile(const char* dirname, char* filename, const uint8_t* data, uint32_t size, uint16_t packetNumber) = 0; + virtual ReturnValue_t createFile(const char* dirname, const char* filename, const uint8_t* data, uint32_t size) = 0; + virtual ReturnValue_t deleteFile(const char* dirname, const char* filename) = 0; +}; + + +#endif /* FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_ */ diff --git a/memory/HasMemoryIF.h b/memory/HasMemoryIF.h index 788a86b6..4096d1b7 100644 --- a/memory/HasMemoryIF.h +++ b/memory/HasMemoryIF.h @@ -1,47 +1,47 @@ -#ifndef HASMEMORYIF_H_ -#define HASMEMORYIF_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" - -class HasMemoryIF { -public: - static const uint8_t INTERFACE_ID = CLASS_ID::HAS_MEMORY_IF; - static const ReturnValue_t DO_IT_MYSELF = MAKE_RETURN_CODE(1); - static const ReturnValue_t POINTS_TO_VARIABLE = MAKE_RETURN_CODE(2); - static const ReturnValue_t POINTS_TO_MEMORY = MAKE_RETURN_CODE(3); - static const ReturnValue_t ACTIVITY_COMPLETED = MAKE_RETURN_CODE(4); - static const ReturnValue_t POINTS_TO_VECTOR_UINT8 = MAKE_RETURN_CODE(5); - static const ReturnValue_t POINTS_TO_VECTOR_UINT16 = MAKE_RETURN_CODE(6); - static const ReturnValue_t POINTS_TO_VECTOR_UINT32 = MAKE_RETURN_CODE(7); - static const ReturnValue_t POINTS_TO_VECTOR_FLOAT = MAKE_RETURN_CODE(8); - static const ReturnValue_t DUMP_NOT_SUPPORTED = MAKE_RETURN_CODE(0xA0); - static const ReturnValue_t INVALID_SIZE = MAKE_RETURN_CODE(0xE0); - static const ReturnValue_t INVALID_ADDRESS = MAKE_RETURN_CODE(0xE1); - static const ReturnValue_t INVALID_CONTENT = MAKE_RETURN_CODE(0xE2); - static const ReturnValue_t UNALIGNED_ACCESS = MAKE_RETURN_CODE(0xE3); - static const ReturnValue_t WRITE_PROTECTED = MAKE_RETURN_CODE(0xE4); -// static const ReturnValue_t TARGET_BUSY = MAKE_RETURN_CODE(0xE5); - virtual ~HasMemoryIF() {} - virtual ReturnValue_t handleMemoryLoad(uint32_t address, const uint8_t* data, uint32_t size, uint8_t** dataPointer) = 0; - virtual ReturnValue_t handleMemoryDump(uint32_t address, uint32_t size, uint8_t** dataPointer, uint8_t* dumpTarget ) = 0; - /** - * Sets the address of the memory, if possible. - * startAddress is a proposal for an address, or the base address if multiple addresses are set. - */ - virtual ReturnValue_t setAddress( uint32_t* startAddress ) { return HasReturnvaluesIF::RETURN_FAILED; } - static bool memAccessWasSuccessful(ReturnValue_t result) { - switch (result) { - case DO_IT_MYSELF: - case POINTS_TO_MEMORY: - case POINTS_TO_VARIABLE: - case HasReturnvaluesIF::RETURN_OK: - case ACTIVITY_COMPLETED: - return true; - default: - return false; - } - } -}; - - -#endif /* HASMEMORYIF_H_ */ +#ifndef HASMEMORYIF_H_ +#define HASMEMORYIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" + +class HasMemoryIF { +public: + static const uint8_t INTERFACE_ID = CLASS_ID::HAS_MEMORY_IF; + static const ReturnValue_t DO_IT_MYSELF = MAKE_RETURN_CODE(1); + static const ReturnValue_t POINTS_TO_VARIABLE = MAKE_RETURN_CODE(2); + static const ReturnValue_t POINTS_TO_MEMORY = MAKE_RETURN_CODE(3); + static const ReturnValue_t ACTIVITY_COMPLETED = MAKE_RETURN_CODE(4); + static const ReturnValue_t POINTS_TO_VECTOR_UINT8 = MAKE_RETURN_CODE(5); + static const ReturnValue_t POINTS_TO_VECTOR_UINT16 = MAKE_RETURN_CODE(6); + static const ReturnValue_t POINTS_TO_VECTOR_UINT32 = MAKE_RETURN_CODE(7); + static const ReturnValue_t POINTS_TO_VECTOR_FLOAT = MAKE_RETURN_CODE(8); + static const ReturnValue_t DUMP_NOT_SUPPORTED = MAKE_RETURN_CODE(0xA0); + static const ReturnValue_t INVALID_SIZE = MAKE_RETURN_CODE(0xE0); + static const ReturnValue_t INVALID_ADDRESS = MAKE_RETURN_CODE(0xE1); + static const ReturnValue_t INVALID_CONTENT = MAKE_RETURN_CODE(0xE2); + static const ReturnValue_t UNALIGNED_ACCESS = MAKE_RETURN_CODE(0xE3); + static const ReturnValue_t WRITE_PROTECTED = MAKE_RETURN_CODE(0xE4); +// static const ReturnValue_t TARGET_BUSY = MAKE_RETURN_CODE(0xE5); + virtual ~HasMemoryIF() {} + virtual ReturnValue_t handleMemoryLoad(uint32_t address, const uint8_t* data, uint32_t size, uint8_t** dataPointer) = 0; + virtual ReturnValue_t handleMemoryDump(uint32_t address, uint32_t size, uint8_t** dataPointer, uint8_t* dumpTarget ) = 0; + /** + * Sets the address of the memory, if possible. + * startAddress is a proposal for an address, or the base address if multiple addresses are set. + */ + virtual ReturnValue_t setAddress( uint32_t* startAddress ) { return HasReturnvaluesIF::RETURN_FAILED; } + static bool memAccessWasSuccessful(ReturnValue_t result) { + switch (result) { + case DO_IT_MYSELF: + case POINTS_TO_MEMORY: + case POINTS_TO_VARIABLE: + case HasReturnvaluesIF::RETURN_OK: + case ACTIVITY_COMPLETED: + return true; + default: + return false; + } + } +}; + + +#endif /* HASMEMORYIF_H_ */ diff --git a/memory/MemoryHelper.cpp b/memory/MemoryHelper.cpp index c74b7d9f..2daddf63 100644 --- a/memory/MemoryHelper.cpp +++ b/memory/MemoryHelper.cpp @@ -1,193 +1,193 @@ -#include "../globalfunctions/CRC.h" -#include "../memory/MemoryHelper.h" -#include "../memory/MemoryMessage.h" -#include "../objectmanager/ObjectManagerIF.h" -#include "../serialize/EndianConverter.h" -#include "../serviceinterface/ServiceInterfaceStream.h" - -MemoryHelper::MemoryHelper(HasMemoryIF* workOnThis, - MessageQueueIF* useThisQueue): - workOnThis(workOnThis), queueToUse(useThisQueue), ipcStore(nullptr), - ipcAddress(), lastCommand(CommandMessage::CMD_NONE), busy(false) { -} - -ReturnValue_t MemoryHelper::handleMemoryCommand(CommandMessage* message) { - lastSender = message->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 uint32_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 uint32_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, - uint32_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; - } -} +#include "../globalfunctions/CRC.h" +#include "../memory/MemoryHelper.h" +#include "../memory/MemoryMessage.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../serialize/EndianConverter.h" +#include "../serviceinterface/ServiceInterfaceStream.h" + +MemoryHelper::MemoryHelper(HasMemoryIF* workOnThis, + MessageQueueIF* useThisQueue): + workOnThis(workOnThis), queueToUse(useThisQueue), ipcStore(nullptr), + ipcAddress(), lastCommand(CommandMessage::CMD_NONE), busy(false) { +} + +ReturnValue_t MemoryHelper::handleMemoryCommand(CommandMessage* message) { + lastSender = message->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 uint32_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 uint32_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, + uint32_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/memory/MemoryHelper.h b/memory/MemoryHelper.h index 90dbafb4..418cf50d 100644 --- a/memory/MemoryHelper.h +++ b/memory/MemoryHelper.h @@ -1,37 +1,37 @@ -#ifndef FRAMEWORK_MEMORY_MEMORYHELPER_H_ -#define FRAMEWORK_MEMORY_MEMORYHELPER_H_ -#include "../ipc/CommandMessage.h" -#include "../memory/AcceptsMemoryMessagesIF.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../storagemanager/StorageManagerIF.h" -#include "../ipc/MessageQueueIF.h" - -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); -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(); -public: - ReturnValue_t handleMemoryCommand(CommandMessage* message); - void completeLoad( ReturnValue_t errorCode, const uint8_t* dataToCopy = NULL, const uint32_t size = 0, uint8_t* copyHere = NULL ); - void completeDump( ReturnValue_t errorCode, const uint8_t* dataToCopy = NULL, const uint32_t size = 0); - void swapMatrixCopy( uint8_t *out, const uint8_t *in, uint32_t totalSize, uint8_t datatypeSize); - ReturnValue_t initialize(MessageQueueIF* queueToUse_); - MemoryHelper( HasMemoryIF* workOnThis, MessageQueueIF* useThisQueue ); - ~MemoryHelper(); -}; -#endif /* MEMORYHELPER_H_ */ +#ifndef FRAMEWORK_MEMORY_MEMORYHELPER_H_ +#define FRAMEWORK_MEMORY_MEMORYHELPER_H_ +#include "../ipc/CommandMessage.h" +#include "../memory/AcceptsMemoryMessagesIF.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../ipc/MessageQueueIF.h" + +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); +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(); +public: + ReturnValue_t handleMemoryCommand(CommandMessage* message); + void completeLoad( ReturnValue_t errorCode, const uint8_t* dataToCopy = NULL, const uint32_t size = 0, uint8_t* copyHere = NULL ); + void completeDump( ReturnValue_t errorCode, const uint8_t* dataToCopy = NULL, const uint32_t size = 0); + void swapMatrixCopy( uint8_t *out, const uint8_t *in, uint32_t totalSize, uint8_t datatypeSize); + ReturnValue_t initialize(MessageQueueIF* queueToUse_); + MemoryHelper( HasMemoryIF* workOnThis, MessageQueueIF* useThisQueue ); + ~MemoryHelper(); +}; +#endif /* MEMORYHELPER_H_ */ diff --git a/memory/MemoryMessage.cpp b/memory/MemoryMessage.cpp index 16a60502..4e077dee 100644 --- a/memory/MemoryMessage.cpp +++ b/memory/MemoryMessage.cpp @@ -1,112 +1,112 @@ -#include "../memory/MemoryMessage.h" -#include "../objectmanager/ObjectManagerIF.h" -MemoryMessage::MemoryMessage() { -} - -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(); -} - -ReturnValue_t MemoryMessage::setMemoryDumpCommand(CommandMessage* message, - uint32_t address, uint32_t length) { - message->setCommand(CMD_MEMORY_DUMP); - message->setParameter( address ); - message->setParameter2( length ); - return HasReturnvaluesIF::RETURN_OK; -} - -ReturnValue_t MemoryMessage::setMemoryDumpReply(CommandMessage* message, store_address_t storageID) { - message->setCommand(REPLY_MEMORY_DUMP); - message->setParameter2( storageID.raw ); - return HasReturnvaluesIF::RETURN_OK; -} - -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; - } -} - -ReturnValue_t MemoryMessage::setMemoryCheckCommand(CommandMessage* message, - uint32_t address, uint32_t length) { - message->setCommand(CMD_MEMORY_CHECK); - message->setParameter( address ); - message->setParameter2( length ); - return HasReturnvaluesIF::RETURN_OK; -} - -ReturnValue_t MemoryMessage::setMemoryCheckReply(CommandMessage* message, - uint16_t crc) { - message->setCommand(REPLY_MEMORY_CHECK); - message->setParameter( crc ); - return HasReturnvaluesIF::RETURN_OK; -} - -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(); -} - -ReturnValue_t MemoryMessage::setMemoryReplyFailed(CommandMessage* message, - ReturnValue_t errorCode, Command_t initialCommand) { - message->setCommand(REPLY_MEMORY_FAILED); - message->setParameter(errorCode); - message->setParameter2(initialCommand); - return HasReturnvaluesIF::RETURN_OK; -} - -ReturnValue_t MemoryMessage::setMemoryCopyEnd(CommandMessage* message) { - message->setCommand(END_OF_MEMORY_COPY); - message->setParameter(0); - message->setParameter2(0); - return HasReturnvaluesIF::RETURN_OK; -} - +#include "../memory/MemoryMessage.h" +#include "../objectmanager/ObjectManagerIF.h" +MemoryMessage::MemoryMessage() { +} + +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(); +} + +ReturnValue_t MemoryMessage::setMemoryDumpCommand(CommandMessage* message, + uint32_t address, uint32_t length) { + message->setCommand(CMD_MEMORY_DUMP); + message->setParameter( address ); + message->setParameter2( length ); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t MemoryMessage::setMemoryDumpReply(CommandMessage* message, store_address_t storageID) { + message->setCommand(REPLY_MEMORY_DUMP); + message->setParameter2( storageID.raw ); + return HasReturnvaluesIF::RETURN_OK; +} + +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; + } +} + +ReturnValue_t MemoryMessage::setMemoryCheckCommand(CommandMessage* message, + uint32_t address, uint32_t length) { + message->setCommand(CMD_MEMORY_CHECK); + message->setParameter( address ); + message->setParameter2( length ); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t MemoryMessage::setMemoryCheckReply(CommandMessage* message, + uint16_t crc) { + message->setCommand(REPLY_MEMORY_CHECK); + message->setParameter( crc ); + return HasReturnvaluesIF::RETURN_OK; +} + +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(); +} + +ReturnValue_t MemoryMessage::setMemoryReplyFailed(CommandMessage* message, + ReturnValue_t errorCode, Command_t initialCommand) { + message->setCommand(REPLY_MEMORY_FAILED); + message->setParameter(errorCode); + message->setParameter2(initialCommand); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t MemoryMessage::setMemoryCopyEnd(CommandMessage* message) { + message->setCommand(END_OF_MEMORY_COPY); + message->setParameter(0); + message->setParameter2(0); + return HasReturnvaluesIF::RETURN_OK; +} + diff --git a/memory/MemoryMessage.h b/memory/MemoryMessage.h index af936c1f..a366eb9a 100644 --- a/memory/MemoryMessage.h +++ b/memory/MemoryMessage.h @@ -1,39 +1,39 @@ -#ifndef MEMORYMESSAGE_H_ -#define MEMORYMESSAGE_H_ - -#include "../ipc/CommandMessage.h" -#include "../storagemanager/StorageManagerIF.h" - - -class MemoryMessage { -private: - MemoryMessage(); //A private ctor inhibits instantiation -public: - 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 ReturnValue_t setMemoryDumpCommand( CommandMessage* message, uint32_t address, uint32_t length ); - static ReturnValue_t setMemoryDumpReply( CommandMessage* message, store_address_t storageID ); - static void setMemoryLoadCommand( CommandMessage* message, uint32_t address, store_address_t storageID ); - static ReturnValue_t setMemoryCheckCommand( CommandMessage* message, uint32_t address, uint32_t length ); - static ReturnValue_t setMemoryCheckReply( CommandMessage* message, uint16_t crc ); - static ReturnValue_t setMemoryReplyFailed( CommandMessage* message, ReturnValue_t errorCode, Command_t initialCommand ); - static ReturnValue_t setMemoryCopyEnd( CommandMessage* message); - static void setCrcReturnValue(CommandMessage*, ReturnValue_t returnValue); - static uint16_t getCrc( const CommandMessage* message ); - static ReturnValue_t getCrcReturnValue(const CommandMessage* message); - static Command_t getInitialCommand( const CommandMessage* message ); - static void clear(CommandMessage* message); -}; - -#endif /* MEMORYMESSAGE_H_ */ +#ifndef MEMORYMESSAGE_H_ +#define MEMORYMESSAGE_H_ + +#include "../ipc/CommandMessage.h" +#include "../storagemanager/StorageManagerIF.h" + + +class MemoryMessage { +private: + MemoryMessage(); //A private ctor inhibits instantiation +public: + 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 ReturnValue_t setMemoryDumpCommand( CommandMessage* message, uint32_t address, uint32_t length ); + static ReturnValue_t setMemoryDumpReply( CommandMessage* message, store_address_t storageID ); + static void setMemoryLoadCommand( CommandMessage* message, uint32_t address, store_address_t storageID ); + static ReturnValue_t setMemoryCheckCommand( CommandMessage* message, uint32_t address, uint32_t length ); + static ReturnValue_t setMemoryCheckReply( CommandMessage* message, uint16_t crc ); + static ReturnValue_t setMemoryReplyFailed( CommandMessage* message, ReturnValue_t errorCode, Command_t initialCommand ); + static ReturnValue_t setMemoryCopyEnd( CommandMessage* message); + static void setCrcReturnValue(CommandMessage*, ReturnValue_t returnValue); + static uint16_t getCrc( const CommandMessage* message ); + static ReturnValue_t getCrcReturnValue(const CommandMessage* message); + static Command_t getInitialCommand( const CommandMessage* message ); + static void clear(CommandMessage* message); +}; + +#endif /* MEMORYMESSAGE_H_ */ diff --git a/memory/MemoryProxyIF.h b/memory/MemoryProxyIF.h index a30165c3..31045d3f 100644 --- a/memory/MemoryProxyIF.h +++ b/memory/MemoryProxyIF.h @@ -1,22 +1,22 @@ -#ifndef FRAMEWORK_MEMORY_MEMORYPROXYIF_H_ -#define FRAMEWORK_MEMORY_MEMORYPROXYIF_H_ - -#include "../memory/AcceptsMemoryMessagesIF.h" - -/** - * This was a nice idea to transparently forward incoming messages to another object. - * But it doesn't work like that. - */ -class MemoryProxyIF : public AcceptsMemoryMessagesIF { -public: - virtual MessageQueueId_t getProxyQueue() const = 0; - MessageQueueId_t getCommandQueue() const { - return getProxyQueue(); - } - virtual ~MemoryProxyIF() {} - -}; - - - -#endif /* FRAMEWORK_MEMORY_MEMORYPROXYIF_H_ */ +#ifndef FRAMEWORK_MEMORY_MEMORYPROXYIF_H_ +#define FRAMEWORK_MEMORY_MEMORYPROXYIF_H_ + +#include "../memory/AcceptsMemoryMessagesIF.h" + +/** + * This was a nice idea to transparently forward incoming messages to another object. + * But it doesn't work like that. + */ +class MemoryProxyIF : public AcceptsMemoryMessagesIF { +public: + virtual MessageQueueId_t getProxyQueue() const = 0; + MessageQueueId_t getCommandQueue() const { + return getProxyQueue(); + } + virtual ~MemoryProxyIF() {} + +}; + + + +#endif /* FRAMEWORK_MEMORY_MEMORYPROXYIF_H_ */ diff --git a/modes/HasModesIF.h b/modes/HasModesIF.h index eade3613..c3abaaaf 100644 --- a/modes/HasModesIF.h +++ b/modes/HasModesIF.h @@ -1,57 +1,57 @@ -/** - * @file HasModesIF.h - * @brief This file defines the HasModesIF class. - * @date 20.06.2013 - * @author baetz - */ - -#ifndef HASMODESIF_H_ -#define HASMODESIF_H_ - -#include "../events/Event.h" -#include "../modes/ModeHelper.h" -#include "../modes/ModeMessage.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 /* HASMODESIF_H_ */ +/** + * @file HasModesIF.h + * @brief This file defines the HasModesIF class. + * @date 20.06.2013 + * @author baetz + */ + +#ifndef HASMODESIF_H_ +#define HASMODESIF_H_ + +#include "../events/Event.h" +#include "../modes/ModeHelper.h" +#include "../modes/ModeMessage.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 /* HASMODESIF_H_ */ diff --git a/modes/ModeHelper.cpp b/modes/ModeHelper.cpp index d6cbf2c9..e7fc0bcb 100644 --- a/modes/ModeHelper.cpp +++ b/modes/ModeHelper.cpp @@ -1,135 +1,135 @@ -#include "../modes/HasModesIF.h" -#include "../modes/ModeHelper.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::cantReachMode(&reply, result); - MessageQueueSenderIF::sendMessage(command->getSender(), &reply, - owner->getCommandQueue()); - break; - } - //Free to start transition - theOneWhoCommandedAMode = command->getSender(); - commandedMode = mode; - commandedSubmode = submode; - - if ((parentQueueId != MessageQueueMessageIF::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 = MessageQueueMessageIF::NO_QUEUE; -} - -void ModeHelper::sendModeReplyMessage(Mode_t ownerMode, - Submode_t ownerSubmode) { - CommandMessage reply; - if (theOneWhoCommandedAMode != MessageQueueMessageIF::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 != MessageQueueMessageIF::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; -} +#include "../modes/HasModesIF.h" +#include "../modes/ModeHelper.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::cantReachMode(&reply, result); + MessageQueueSenderIF::sendMessage(command->getSender(), &reply, + owner->getCommandQueue()); + break; + } + //Free to start transition + theOneWhoCommandedAMode = command->getSender(); + commandedMode = mode; + commandedSubmode = submode; + + if ((parentQueueId != MessageQueueMessageIF::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 = MessageQueueMessageIF::NO_QUEUE; +} + +void ModeHelper::sendModeReplyMessage(Mode_t ownerMode, + Submode_t ownerSubmode) { + CommandMessage reply; + if (theOneWhoCommandedAMode != MessageQueueMessageIF::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 != MessageQueueMessageIF::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/modes/ModeHelper.h b/modes/ModeHelper.h index 1e51bce5..02cf5799 100644 --- a/modes/ModeHelper.h +++ b/modes/ModeHelper.h @@ -1,53 +1,53 @@ -#ifndef MODEHELPER_H_ -#define MODEHELPER_H_ - -#include "../ipc/MessageQueueIF.h" -#include "../modes/ModeMessage.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 is there to stop eclipse CODAN from falsely reporting an error - - 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 /* MODEHELPER_H_ */ +#ifndef MODEHELPER_H_ +#define MODEHELPER_H_ + +#include "../ipc/MessageQueueIF.h" +#include "../modes/ModeMessage.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 is there to stop eclipse CODAN from falsely reporting an error + + 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 /* MODEHELPER_H_ */ diff --git a/modes/ModeMessage.cpp b/modes/ModeMessage.cpp index de3fc69c..7ad7d06f 100644 --- a/modes/ModeMessage.cpp +++ b/modes/ModeMessage.cpp @@ -1,31 +1,31 @@ -#include "../modes/ModeMessage.h" - -Mode_t ModeMessage::getMode(const CommandMessage* message) { - return message->getParameter(); -} - -Submode_t ModeMessage::getSubmode(const CommandMessage* message) { - return message->getParameter2(); -} - -ReturnValue_t ModeMessage::setModeMessage(CommandMessage* message, Command_t command, - Mode_t mode, Submode_t submode) { - message->setCommand( command ); - message->setParameter( mode ); - message->setParameter2( submode ); - return HasReturnvaluesIF::RETURN_OK; -} - -ReturnValue_t ModeMessage::getCantReachModeReason(const CommandMessage* message) { - return message->getParameter(); -} - -void ModeMessage::clear(CommandMessage* message) { - message->setCommand(CommandMessage::CMD_NONE); -} - -void ModeMessage::cantReachMode(CommandMessage* message, ReturnValue_t reason) { - message->setCommand(REPLY_CANT_REACH_MODE); - message->setParameter(reason); - message->setParameter2(0); -} +#include "../modes/ModeMessage.h" + +Mode_t ModeMessage::getMode(const CommandMessage* message) { + return message->getParameter(); +} + +Submode_t ModeMessage::getSubmode(const CommandMessage* message) { + return message->getParameter2(); +} + +ReturnValue_t ModeMessage::setModeMessage(CommandMessage* message, Command_t command, + Mode_t mode, Submode_t submode) { + message->setCommand( command ); + message->setParameter( mode ); + message->setParameter2( submode ); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t ModeMessage::getCantReachModeReason(const CommandMessage* message) { + return message->getParameter(); +} + +void ModeMessage::clear(CommandMessage* message) { + message->setCommand(CommandMessage::CMD_NONE); +} + +void ModeMessage::cantReachMode(CommandMessage* message, ReturnValue_t reason) { + message->setCommand(REPLY_CANT_REACH_MODE); + message->setParameter(reason); + message->setParameter2(0); +} diff --git a/modes/ModeMessage.h b/modes/ModeMessage.h index 63624b1d..925f3fc1 100644 --- a/modes/ModeMessage.h +++ b/modes/ModeMessage.h @@ -1,40 +1,40 @@ -/** - * @file ModeMessage.h - * @brief This file defines the ModeMessage class. - * @date 17.07.2013 - * @author baetz - */ - -#ifndef MODEMESSAGE_H_ -#define 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 setModeMessage(CommandMessage* message, - Command_t command, Mode_t mode, Submode_t submode); - static void cantReachMode(CommandMessage* message, ReturnValue_t reason); - static ReturnValue_t getCantReachModeReason(const CommandMessage* message); - static void clear(CommandMessage* message); -}; - -#endif /* MODEMESSAGE_H_ */ +/** + * @file ModeMessage.h + * @brief This file defines the ModeMessage class. + * @date 17.07.2013 + * @author baetz + */ + +#ifndef MODEMESSAGE_H_ +#define 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 setModeMessage(CommandMessage* message, + Command_t command, Mode_t mode, Submode_t submode); + static void cantReachMode(CommandMessage* message, ReturnValue_t reason); + static ReturnValue_t getCantReachModeReason(const CommandMessage* message); + static void clear(CommandMessage* message); +}; + +#endif /* MODEMESSAGE_H_ */ diff --git a/monitoring/AbsLimitMonitor.h b/monitoring/AbsLimitMonitor.h index 6b546bd0..c32c63ef 100644 --- a/monitoring/AbsLimitMonitor.h +++ b/monitoring/AbsLimitMonitor.h @@ -1,73 +1,73 @@ -#ifndef FRAMEWORK_MONITORING_ABSLIMITMONITOR_H_ -#define FRAMEWORK_MONITORING_ABSLIMITMONITOR_H_ - -#include "../monitoring/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_ */ +#ifndef FRAMEWORK_MONITORING_ABSLIMITMONITOR_H_ +#define FRAMEWORK_MONITORING_ABSLIMITMONITOR_H_ + +#include "../monitoring/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/monitoring/HasMonitorsIF.h b/monitoring/HasMonitorsIF.h index be561efd..85d92b6b 100644 --- a/monitoring/HasMonitorsIF.h +++ b/monitoring/HasMonitorsIF.h @@ -1,30 +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_ */ +/** + * @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/monitoring/LimitMonitor.h b/monitoring/LimitMonitor.h index 465d72e8..4aec8669 100644 --- a/monitoring/LimitMonitor.h +++ b/monitoring/LimitMonitor.h @@ -1,95 +1,95 @@ -#ifndef FRAMEWORK_MONITORING_LIMITMONITOR_H_ -#define FRAMEWORK_MONITORING_LIMITMONITOR_H_ - -#include "../monitoring/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_ */ +#ifndef FRAMEWORK_MONITORING_LIMITMONITOR_H_ +#define FRAMEWORK_MONITORING_LIMITMONITOR_H_ + +#include "../monitoring/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/monitoring/LimitViolationReporter.cpp b/monitoring/LimitViolationReporter.cpp index 7869dc2b..87278f4e 100644 --- a/monitoring/LimitViolationReporter.cpp +++ b/monitoring/LimitViolationReporter.cpp @@ -1,60 +1,60 @@ -/** - * @file LimitViolationReporter.cpp - * @brief This file defines the LimitViolationReporter class. - * @date 17.07.2014 - * @author baetz - */ -#include "../monitoring/LimitViolationReporter.h" -#include "../monitoring/MonitoringIF.h" -#include "../monitoring/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; +/** + * @file LimitViolationReporter.cpp + * @brief This file defines the LimitViolationReporter class. + * @date 17.07.2014 + * @author baetz + */ +#include "../monitoring/LimitViolationReporter.h" +#include "../monitoring/MonitoringIF.h" +#include "../monitoring/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/monitoring/LimitViolationReporter.h b/monitoring/LimitViolationReporter.h index 53d67b9d..a71b972f 100644 --- a/monitoring/LimitViolationReporter.h +++ b/monitoring/LimitViolationReporter.h @@ -1,31 +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_ */ +/** + * @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/monitoring/MonitorBase.h b/monitoring/MonitorBase.h index 45fe22ec..5173c479 100644 --- a/monitoring/MonitorBase.h +++ b/monitoring/MonitorBase.h @@ -1,62 +1,62 @@ -#ifndef MONITORBASE_H_ -#define MONITORBASE_H_ - -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/PIDReader.h" -#include "../monitoring/LimitViolationReporter.h" -#include "../monitoring/MonitoringIF.h" -#include "../monitoring/MonitoringMessageContent.h" -#include "../monitoring/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) { - GlobDataSet mySet; - PIDReader parameter(this->parameterId, &mySet); - mySet.read(); - if (!parameter.isValid()) { - return MonitoringIF::INVALID; - } - *sample = parameter.value; - return HasReturnvaluesIF::RETURN_OK; - } -}; - -#endif /* MONITORBASE_H_ */ +#ifndef MONITORBASE_H_ +#define MONITORBASE_H_ + +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/PIDReader.h" +#include "../monitoring/LimitViolationReporter.h" +#include "../monitoring/MonitoringIF.h" +#include "../monitoring/MonitoringMessageContent.h" +#include "../monitoring/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) { + GlobDataSet 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/monitoring/MonitorReporter.h b/monitoring/MonitorReporter.h index 69bb00ea..9823cde0 100644 --- a/monitoring/MonitorReporter.h +++ b/monitoring/MonitorReporter.h @@ -1,178 +1,178 @@ -#ifndef FRAMEWORK_MONITORING_MONITORREPORTER_H_ -#define FRAMEWORK_MONITORING_MONITORREPORTER_H_ - -#include "../events/EventManagerIF.h" -#include "../monitoring/LimitViolationReporter.h" -#include "../monitoring/MonitoringIF.h" -#include "../monitoring/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_ */ +#ifndef FRAMEWORK_MONITORING_MONITORREPORTER_H_ +#define FRAMEWORK_MONITORING_MONITORREPORTER_H_ + +#include "../events/EventManagerIF.h" +#include "../monitoring/LimitViolationReporter.h" +#include "../monitoring/MonitoringIF.h" +#include "../monitoring/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/monitoring/MonitoringIF.h b/monitoring/MonitoringIF.h index a4c0666c..d9ea339f 100644 --- a/monitoring/MonitoringIF.h +++ b/monitoring/MonitoringIF.h @@ -1,67 +1,67 @@ -#ifndef MONITORINGIF_H_ -#define MONITORINGIF_H_ - -#include "../memory/HasMemoryIF.h" -#include "../monitoring/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_ */ +#ifndef MONITORINGIF_H_ +#define MONITORINGIF_H_ + +#include "../memory/HasMemoryIF.h" +#include "../monitoring/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/monitoring/MonitoringMessage.cpp b/monitoring/MonitoringMessage.cpp index 9edf16d5..2cfe1ece 100644 --- a/monitoring/MonitoringMessage.cpp +++ b/monitoring/MonitoringMessage.cpp @@ -1,38 +1,38 @@ -#include "../monitoring/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; - } -} +#include "../monitoring/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/monitoring/MonitoringMessage.h b/monitoring/MonitoringMessage.h index 55d3ea5c..84b02750 100644 --- a/monitoring/MonitoringMessage.h +++ b/monitoring/MonitoringMessage.h @@ -1,21 +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_ */ +#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/monitoring/MonitoringMessageContent.h b/monitoring/MonitoringMessageContent.h index a0934c14..ea52962b 100644 --- a/monitoring/MonitoringMessageContent.h +++ b/monitoring/MonitoringMessageContent.h @@ -1,77 +1,77 @@ -#ifndef MONITORINGMESSAGECONTENT_H_ -#define MONITORINGMESSAGECONTENT_H_ - -#include "../monitoring/HasMonitorsIF.h" -#include "../monitoring/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_ */ +#ifndef MONITORINGMESSAGECONTENT_H_ +#define MONITORINGMESSAGECONTENT_H_ + +#include "../monitoring/HasMonitorsIF.h" +#include "../monitoring/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/monitoring/ReceivesMonitoringReportsIF.h b/monitoring/ReceivesMonitoringReportsIF.h index 16595f33..fb37c16c 100644 --- a/monitoring/ReceivesMonitoringReportsIF.h +++ b/monitoring/ReceivesMonitoringReportsIF.h @@ -1,15 +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_ */ +#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/monitoring/TriplexMonitor.h b/monitoring/TriplexMonitor.h index 6f190361..9b60aeb6 100644 --- a/monitoring/TriplexMonitor.h +++ b/monitoring/TriplexMonitor.h @@ -1,155 +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_ */ +#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/monitoring/TwoValueLimitMonitor.h b/monitoring/TwoValueLimitMonitor.h index ac0ef1ee..74934828 100644 --- a/monitoring/TwoValueLimitMonitor.h +++ b/monitoring/TwoValueLimitMonitor.h @@ -1,44 +1,44 @@ -#ifndef FRAMEWORK_MONITORING_TWOVALUELIMITMONITOR_H_ -#define FRAMEWORK_MONITORING_TWOVALUELIMITMONITOR_H_ - -#include "../monitoring/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_ */ +#ifndef FRAMEWORK_MONITORING_TWOVALUELIMITMONITOR_H_ +#define FRAMEWORK_MONITORING_TWOVALUELIMITMONITOR_H_ + +#include "../monitoring/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/objectmanager/ObjectManager.cpp b/objectmanager/ObjectManager.cpp index 467eed4b..a8c84415 100644 --- a/objectmanager/ObjectManager.cpp +++ b/objectmanager/ObjectManager.cpp @@ -1,107 +1,107 @@ -#include "../objectmanager/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; - } -} +#include "../objectmanager/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/objectmanager/ObjectManager.h b/objectmanager/ObjectManager.h index 0bc525be..e582584c 100644 --- a/objectmanager/ObjectManager.h +++ b/objectmanager/ObjectManager.h @@ -1,67 +1,67 @@ -/** - * @file ObjectManager.h - * @brief This file contains the implementation of the ObjectManager class - * @date 18.09.2012 - * @author Bastian Baetz - */ - -#ifndef OBJECTMANAGER_H_ -#define OBJECTMANAGER_H_ - -#include "../objectmanager/ObjectManagerIF.h" -#include "../objectmanager/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 - */ -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 /* OBJECTMANAGER_H_ */ +/** + * @file ObjectManager.h + * @brief This file contains the implementation of the ObjectManager class + * @date 18.09.2012 + * @author Bastian Baetz + */ + +#ifndef OBJECTMANAGER_H_ +#define OBJECTMANAGER_H_ + +#include "../objectmanager/ObjectManagerIF.h" +#include "../objectmanager/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 + */ +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 /* OBJECTMANAGER_H_ */ diff --git a/objectmanager/ObjectManagerIF.h b/objectmanager/ObjectManagerIF.h index 5dfe8e4e..16619e23 100644 --- a/objectmanager/ObjectManagerIF.h +++ b/objectmanager/ObjectManagerIF.h @@ -1,95 +1,95 @@ -#ifndef FRAMEWORK_OBJECTMANAGER_OBJECTMANAGERIF_H_ -#define FRAMEWORK_OBJECTMANAGER_OBJECTMANAGERIF_H_ - -#include "../objectmanager/frameworkObjects.h" -#include "../objectmanager/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 ); - 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. - */ -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_ */ +#ifndef FRAMEWORK_OBJECTMANAGER_OBJECTMANAGERIF_H_ +#define FRAMEWORK_OBJECTMANAGER_OBJECTMANAGERIF_H_ + +#include "../objectmanager/frameworkObjects.h" +#include "../objectmanager/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 ); + 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. + */ +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/objectmanager/SystemObject.cpp b/objectmanager/SystemObject.cpp index e3eb22ee..61eaf90a 100644 --- a/objectmanager/SystemObject.cpp +++ b/objectmanager/SystemObject.cpp @@ -1,37 +1,37 @@ -#include "../events/EventManagerIF.h" -#include "../objectmanager/ObjectManager.h" -#include "../objectmanager/SystemObject.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); -} +#include "../events/EventManagerIF.h" +#include "../objectmanager/ObjectManager.h" +#include "../objectmanager/SystemObject.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/objectmanager/SystemObjectIF.h b/objectmanager/SystemObjectIF.h index 54a2e866..108717af 100644 --- a/objectmanager/SystemObjectIF.h +++ b/objectmanager/SystemObjectIF.h @@ -1,62 +1,62 @@ -#ifndef FRAMEWORK_OBJECTMANAGER_SYSTEMOBJECTIF_H_ -#define FRAMEWORK_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 /* FRAMEWORK_OBJECTMANAGER_SYSTEMOBJECTIF_H_ */ +#ifndef FRAMEWORK_OBJECTMANAGER_SYSTEMOBJECTIF_H_ +#define FRAMEWORK_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 /* FRAMEWORK_OBJECTMANAGER_SYSTEMOBJECTIF_H_ */ diff --git a/osal/FreeRTOS/BinSemaphUsingTask.cpp b/osal/FreeRTOS/BinSemaphUsingTask.cpp index 8c831bbe..dd1e48ca 100644 --- a/osal/FreeRTOS/BinSemaphUsingTask.cpp +++ b/osal/FreeRTOS/BinSemaphUsingTask.cpp @@ -1,95 +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; -} +#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/osal/FreeRTOS/BinSemaphUsingTask.h b/osal/FreeRTOS/BinSemaphUsingTask.h index 0b30c094..4317e0b2 100644 --- a/osal/FreeRTOS/BinSemaphUsingTask.h +++ b/osal/FreeRTOS/BinSemaphUsingTask.h @@ -1,79 +1,79 @@ -#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. - * This semaphore is bound to the task it is created in! - * Take care of calling this function with the correct executing task, - * (for example in the initializeAfterTaskCreation() function). - */ -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_ */ +#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. + * This semaphore is bound to the task it is created in! + * Take care of calling this function with the correct executing task, + * (for example in the initializeAfterTaskCreation() function). + */ +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/osal/FreeRTOS/BinarySemaphore.cpp b/osal/FreeRTOS/BinarySemaphore.cpp index f1c78473..8cc3c495 100644 --- a/osal/FreeRTOS/BinarySemaphore.cpp +++ b/osal/FreeRTOS/BinarySemaphore.cpp @@ -1,108 +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; - } -} +#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/osal/FreeRTOS/BinarySemaphore.h b/osal/FreeRTOS/BinarySemaphore.h index 5a32088a..c6cedc53 100644 --- a/osal/FreeRTOS/BinarySemaphore.h +++ b/osal/FreeRTOS/BinarySemaphore.h @@ -1,107 +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_ */ +#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/osal/FreeRTOS/Clock.cpp b/osal/FreeRTOS/Clock.cpp index 48d7bc07..d556444c 100644 --- a/osal/FreeRTOS/Clock.cpp +++ b/osal/FreeRTOS/Clock.cpp @@ -1,196 +1,196 @@ -#include "../../timemanager/Clock.h" -#include "../../globalfunctions/timevalOperations.h" -#include "../../osal/FreeRTOS/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; -} +#include "../../timemanager/Clock.h" +#include "../../globalfunctions/timevalOperations.h" +#include "../../osal/FreeRTOS/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/osal/FreeRTOS/CountingSemaphUsingTask.cpp b/osal/FreeRTOS/CountingSemaphUsingTask.cpp index e66671ac..a47341bc 100644 --- a/osal/FreeRTOS/CountingSemaphUsingTask.cpp +++ b/osal/FreeRTOS/CountingSemaphUsingTask.cpp @@ -1,114 +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; -} +#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/osal/FreeRTOS/CountingSemaphUsingTask.h b/osal/FreeRTOS/CountingSemaphUsingTask.h index d3b23ebe..49ff3ee5 100644 --- a/osal/FreeRTOS/CountingSemaphUsingTask.h +++ b/osal/FreeRTOS/CountingSemaphUsingTask.h @@ -1,105 +1,105 @@ -#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. - * This semaphore is bound to the task it is created in! - * Take care of calling this function with the correct executing task, - * (for example in the initializeAfterTaskCreation() function). - */ -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_ */ +#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. + * This semaphore is bound to the task it is created in! + * Take care of calling this function with the correct executing task, + * (for example in the initializeAfterTaskCreation() function). + */ +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/osal/FreeRTOS/CountingSemaphore.cpp b/osal/FreeRTOS/CountingSemaphore.cpp index 0b218e54..d1310a6a 100644 --- a/osal/FreeRTOS/CountingSemaphore.cpp +++ b/osal/FreeRTOS/CountingSemaphore.cpp @@ -1,43 +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; -} +#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/osal/FreeRTOS/CountingSemaphore.h b/osal/FreeRTOS/CountingSemaphore.h index 9432ed81..ae2f62ae 100644 --- a/osal/FreeRTOS/CountingSemaphore.h +++ b/osal/FreeRTOS/CountingSemaphore.h @@ -1,34 +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_ */ +#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/osal/FreeRTOS/FixedTimeslotTask.cpp b/osal/FreeRTOS/FixedTimeslotTask.cpp index 772c4bca..404c3c05 100644 --- a/osal/FreeRTOS/FixedTimeslotTask.cpp +++ b/osal/FreeRTOS/FixedTimeslotTask.cpp @@ -1,166 +1,166 @@ -#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() { -#ifdef DEBUG - sif::warning << "FixedTimeslotTask: " << pcTaskGetName(NULL) << - " missed deadline!\n" << std::flush; -#endif - 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; -} +#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() { +#ifdef DEBUG + sif::warning << "FixedTimeslotTask: " << pcTaskGetName(NULL) << + " missed deadline!\n" << std::flush; +#endif + 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/osal/FreeRTOS/FixedTimeslotTask.h b/osal/FreeRTOS/FixedTimeslotTask.h index 42af14b8..c46de5b7 100644 --- a/osal/FreeRTOS/FixedTimeslotTask.h +++ b/osal/FreeRTOS/FixedTimeslotTask.h @@ -1,101 +1,101 @@ -#ifndef FRAMEWORK_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_ -#define FRAMEWORK_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_ - -#include "../../osal/FreeRTOS/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 /* POLLINGTASK_H_ */ +#ifndef FRAMEWORK_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_ +#define FRAMEWORK_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_ + +#include "../../osal/FreeRTOS/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 /* POLLINGTASK_H_ */ diff --git a/osal/FreeRTOS/MessageQueue.h b/osal/FreeRTOS/MessageQueue.h index 11986751..e953ec34 100644 --- a/osal/FreeRTOS/MessageQueue.h +++ b/osal/FreeRTOS/MessageQueue.h @@ -1,149 +1,149 @@ -#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 - -// 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 = 0; - MessageQueueId_t lastPartner = 0; - const size_t maxMessageSize; - //! Stores the current system context - CallContext callContext = CallContext::TASK; -}; - -#endif /* FSFW_OSAL_FREERTOS_MESSAGEQUEUE_H_ */ +#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 + +// 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 = 0; + MessageQueueId_t lastPartner = 0; + const size_t maxMessageSize; + //! Stores the current system context + CallContext callContext = CallContext::TASK; +}; + +#endif /* FSFW_OSAL_FREERTOS_MESSAGEQUEUE_H_ */ diff --git a/osal/FreeRTOS/Mutex.cpp b/osal/FreeRTOS/Mutex.cpp index 8d2e43b8..82f23ff9 100644 --- a/osal/FreeRTOS/Mutex.cpp +++ b/osal/FreeRTOS/Mutex.cpp @@ -1,51 +1,51 @@ -#include "../../osal/FreeRTOS/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; - } -} +#include "../../osal/FreeRTOS/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/osal/FreeRTOS/Mutex.h b/osal/FreeRTOS/Mutex.h index 5cbc6a72..156d431c 100644 --- a/osal/FreeRTOS/Mutex.h +++ b/osal/FreeRTOS/Mutex.h @@ -1,29 +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_ */ +#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/osal/FreeRTOS/MutexFactory.cpp b/osal/FreeRTOS/MutexFactory.cpp index 9dcd8536..1f34cc18 100644 --- a/osal/FreeRTOS/MutexFactory.cpp +++ b/osal/FreeRTOS/MutexFactory.cpp @@ -1,28 +1,28 @@ -#include "../../ipc/MutexFactory.h" -#include "../../osal/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 = 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; -} +#include "../../ipc/MutexFactory.h" +#include "../../osal/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 = 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/osal/FreeRTOS/QueueFactory.cpp b/osal/FreeRTOS/QueueFactory.cpp index 05091733..cd99afd9 100644 --- a/osal/FreeRTOS/QueueFactory.cpp +++ b/osal/FreeRTOS/QueueFactory.cpp @@ -1,36 +1,36 @@ -#include "../../ipc/QueueFactory.h" - -#include "../../osal/FreeRTOS/MessageQueue.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; -} +#include "../../ipc/QueueFactory.h" + +#include "../../osal/FreeRTOS/MessageQueue.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/osal/FreeRTOS/SemaphoreFactory.cpp b/osal/FreeRTOS/SemaphoreFactory.cpp index 8575cf4a..beb0d096 100644 --- a/osal/FreeRTOS/SemaphoreFactory.cpp +++ b/osal/FreeRTOS/SemaphoreFactory.cpp @@ -1,59 +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; -} +#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/osal/FreeRTOS/TaskFactory.cpp b/osal/FreeRTOS/TaskFactory.cpp index b68b05b3..3ac5cb49 100644 --- a/osal/FreeRTOS/TaskFactory.cpp +++ b/osal/FreeRTOS/TaskFactory.cpp @@ -1,53 +1,53 @@ -#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; -} - -PeriodicTaskIF* TaskFactory::createPeriodicTask(TaskName name_, - TaskPriority taskPriority_, TaskStackSize stackSize_, - TaskPeriod period_, - TaskDeadlineMissedFunction deadLineMissedFunction_) { - return dynamic_cast(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 dynamic_cast(new FixedTimeslotTask(name_, - taskPriority_,stackSize_, period_, deadLineMissedFunction_)); -} - -ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) { - if (task == nullptr) { - //delete self - vTaskDelete(nullptr); - 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() { -} +#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; +} + +PeriodicTaskIF* TaskFactory::createPeriodicTask(TaskName name_, + TaskPriority taskPriority_, TaskStackSize stackSize_, + TaskPeriod period_, + TaskDeadlineMissedFunction deadLineMissedFunction_) { + return dynamic_cast(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 dynamic_cast(new FixedTimeslotTask(name_, + taskPriority_,stackSize_, period_, deadLineMissedFunction_)); +} + +ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) { + if (task == nullptr) { + //delete self + vTaskDelete(nullptr); + 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/osal/FreeRTOS/TaskManagement.cpp b/osal/FreeRTOS/TaskManagement.cpp index 16682d36..b77f12a9 100644 --- a/osal/FreeRTOS/TaskManagement.cpp +++ b/osal/FreeRTOS/TaskManagement.cpp @@ -1,24 +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); -} +#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/osal/FreeRTOS/TaskManagement.h b/osal/FreeRTOS/TaskManagement.h index 43003d76..4b7fe3eb 100644 --- a/osal/FreeRTOS/TaskManagement.h +++ b/osal/FreeRTOS/TaskManagement.h @@ -1,64 +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_ */ +#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/osal/FreeRTOS/Timekeeper.cpp b/osal/FreeRTOS/Timekeeper.cpp index f7a3c8f8..836f0b21 100644 --- a/osal/FreeRTOS/Timekeeper.cpp +++ b/osal/FreeRTOS/Timekeeper.cpp @@ -1,41 +1,41 @@ -#include "../../osal/FreeRTOS/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(); -} +#include "../../osal/FreeRTOS/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/osal/FreeRTOS/Timekeeper.h b/osal/FreeRTOS/Timekeeper.h index fc1664e2..7d583f7d 100644 --- a/osal/FreeRTOS/Timekeeper.h +++ b/osal/FreeRTOS/Timekeeper.h @@ -1,40 +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_ */ +#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/osal/InternalErrorCodes.h b/osal/InternalErrorCodes.h index d89eb230..1da116fa 100644 --- a/osal/InternalErrorCodes.h +++ b/osal/InternalErrorCodes.h @@ -1,39 +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_ */ +#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/osal/host/Clock.cpp b/osal/host/Clock.cpp index 52d0d5f0..41321eeb 100644 --- a/osal/host/Clock.cpp +++ b/osal/host/Clock.cpp @@ -1,227 +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; -} +#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/osal/host/FixedTimeslotTask.cpp b/osal/host/FixedTimeslotTask.cpp index 2bb0f84d..1139badb 100644 --- a/osal/host/FixedTimeslotTask.cpp +++ b/osal/host/FixedTimeslotTask.cpp @@ -1,191 +1,191 @@ -#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() { - // A local iterator for the Polling Sequence Table is created to - // find the start time for the first entry. - FixedSlotSequence::SlotListIter 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) { - if (objectManager->get(componentId) != nullptr) { - pollingSeqTable.addSlot(componentId, slotTimeMs, executionStep, 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::checkAndInitializeSequence() 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; - -} - - - - +#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() { + // A local iterator for the Polling Sequence Table is created to + // find the start time for the first entry. + FixedSlotSequence::SlotListIter 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) { + if (objectManager->get(componentId) != nullptr) { + pollingSeqTable.addSlot(componentId, slotTimeMs, executionStep, 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::checkAndInitializeSequence() 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/osal/host/FixedTimeslotTask.h b/osal/host/FixedTimeslotTask.h index 861a072e..3ea97b97 100644 --- a/osal/host/FixedTimeslotTask.h +++ b/osal/host/FixedTimeslotTask.h @@ -1,130 +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 checkAndInitializeSequence() const; - - 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_ */ +#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 checkAndInitializeSequence() const; + + 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/osal/host/MessageQueue.cpp b/osal/host/MessageQueue.cpp index 6fd42849..8a34282c 100644 --- a/osal/host/MessageQueue.cpp +++ b/osal/host/MessageQueue.cpp @@ -1,155 +1,155 @@ -#include "../../osal/host/MessageQueue.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../osal/host/QueueMapManager.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: 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, 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, 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(dur_millis_t lockTimeout) { - return queueLock->lockMutex(lockTimeout); -} - -ReturnValue_t MessageQueue::unlockQueue() { - return queueLock->unlockMutex(); -} +#include "../../osal/host/MessageQueue.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../osal/host/QueueMapManager.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: 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, 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, 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(dur_millis_t lockTimeout) { + return queueLock->lockMutex(lockTimeout); +} + +ReturnValue_t MessageQueue::unlockQueue() { + return queueLock->unlockMutex(); +} diff --git a/osal/host/MessageQueue.h b/osal/host/MessageQueue.h index d2da2402..21a01663 100644 --- a/osal/host/MessageQueue.h +++ b/osal/host/MessageQueue.h @@ -1,230 +1,230 @@ -#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(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_ */ +#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(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/osal/host/Mutex.cpp b/osal/host/Mutex.cpp index 28768507..2718f2b9 100644 --- a/osal/host/Mutex.cpp +++ b/osal/host/Mutex.cpp @@ -1,40 +1,40 @@ -#include "../../osal/host/Mutex.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" - -const uint32_t MutexIF::POLLING = 0; -const uint32_t MutexIF::BLOCKING = 0xffffffff; - -ReturnValue_t Mutex::lockMutex(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; -} +#include "../../osal/host/Mutex.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +const uint32_t MutexIF::POLLING = 0; +const uint32_t MutexIF::BLOCKING = 0xffffffff; + +ReturnValue_t Mutex::lockMutex(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/osal/host/Mutex.h b/osal/host/Mutex.h index 4d49bac7..4461e5f2 100644 --- a/osal/host/Mutex.h +++ b/osal/host/Mutex.h @@ -1,28 +1,28 @@ -#ifndef FRAMEWORK_OSAL_FREERTOS_MUTEX_H_ -#define FRAMEWORK_OSAL_FREERTOS_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() = default; - ReturnValue_t lockMutex(uint32_t timeoutMs = MutexIF::BLOCKING) override; - ReturnValue_t unlockMutex() override; - - std::timed_mutex* getMutexHandle(); -private: - bool locked = false; - std::timed_mutex mutex; -}; - -#endif /* FRAMEWORK_FREERTOS_MUTEX_H_ */ +#ifndef FRAMEWORK_OSAL_FREERTOS_MUTEX_H_ +#define FRAMEWORK_OSAL_FREERTOS_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() = default; + ReturnValue_t lockMutex(uint32_t timeoutMs = MutexIF::BLOCKING) override; + ReturnValue_t unlockMutex() override; + + std::timed_mutex* getMutexHandle(); +private: + bool locked = false; + std::timed_mutex mutex; +}; + +#endif /* FRAMEWORK_FREERTOS_MUTEX_H_ */ diff --git a/osal/host/MutexFactory.cpp b/osal/host/MutexFactory.cpp index 284ce59f..bf7707d1 100644 --- a/osal/host/MutexFactory.cpp +++ b/osal/host/MutexFactory.cpp @@ -1,28 +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; -} +#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/osal/host/PeriodicTask.cpp b/osal/host/PeriodicTask.cpp index c4cf9f56..0b5f8bbe 100644 --- a/osal/host/PeriodicTask.cpp +++ b/osal/host/PeriodicTask.cpp @@ -1,176 +1,176 @@ -#include "../../ipc/MutexFactory.h" -#include "../../osal/host/Mutex.h" -#include "../../osal/host/PeriodicTask.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; - -} +#include "../../ipc/MutexFactory.h" +#include "../../osal/host/Mutex.h" +#include "../../osal/host/PeriodicTask.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/osal/host/PeriodicTask.h b/osal/host/PeriodicTask.h index 7eb768cb..7689788a 100644 --- a/osal/host/PeriodicTask.h +++ b/osal/host/PeriodicTask.h @@ -1,123 +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_ */ +#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/osal/host/QueueFactory.cpp b/osal/host/QueueFactory.cpp index 052671ca..da3fea55 100644 --- a/osal/host/QueueFactory.cpp +++ b/osal/host/QueueFactory.cpp @@ -1,41 +1,41 @@ -#include "../../ipc/QueueFactory.h" -#include "../../osal/host/MessageQueue.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; -} +#include "../../ipc/QueueFactory.h" +#include "../../osal/host/MessageQueue.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/osal/host/QueueMapManager.cpp b/osal/host/QueueMapManager.cpp index 35db204e..47687bc1 100644 --- a/osal/host/QueueMapManager.cpp +++ b/osal/host/QueueMapManager.cpp @@ -1,51 +1,51 @@ -#include "../../ipc/MutexFactory.h" -#include "../../ipc/MutexHelper.h" -#include "../../osal/host/QueueMapManager.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, 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; - } -} - +#include "../../ipc/MutexFactory.h" +#include "../../ipc/MutexHelper.h" +#include "../../osal/host/QueueMapManager.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, 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/osal/host/QueueMapManager.h b/osal/host/QueueMapManager.h index acf20389..823fd4c2 100644 --- a/osal/host/QueueMapManager.h +++ b/osal/host/QueueMapManager.h @@ -1,47 +1,47 @@ -#ifndef FRAMEWORK_OSAL_HOST_QUEUEMAP_H_ -#define FRAMEWORK_OSAL_HOST_QUEUEMAP_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 /* FRAMEWORK_OSAL_HOST_QUEUEMAP_H_ */ +#ifndef FRAMEWORK_OSAL_HOST_QUEUEMAP_H_ +#define FRAMEWORK_OSAL_HOST_QUEUEMAP_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 /* FRAMEWORK_OSAL_HOST_QUEUEMAP_H_ */ diff --git a/osal/host/SemaphoreFactory.cpp b/osal/host/SemaphoreFactory.cpp index 58040428..c354c47c 100644 --- a/osal/host/SemaphoreFactory.cpp +++ b/osal/host/SemaphoreFactory.cpp @@ -1,42 +1,42 @@ -#include "../../tasks/SemaphoreFactory.h" -#include "../../osal/linux/BinarySemaphore.h" -#include "../../osal/linux/CountingSemaphore.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" - -const uint32_t SemaphoreIF::POLLING = 0; -const uint32_t SemaphoreIF::BLOCKING = 0xFFFFFFFF; - -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; -} +#include "../../tasks/SemaphoreFactory.h" +#include "../../osal/linux/BinarySemaphore.h" +#include "../../osal/linux/CountingSemaphore.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +const uint32_t SemaphoreIF::POLLING = 0; +const uint32_t SemaphoreIF::BLOCKING = 0xFFFFFFFF; + +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/osal/host/TaskFactory.cpp b/osal/host/TaskFactory.cpp index 3f527130..37df981a 100644 --- a/osal/host/TaskFactory.cpp +++ b/osal/host/TaskFactory.cpp @@ -1,55 +1,55 @@ -#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_) { - // This is going to be interesting. Time now learn the C++ threading library - // :-) - return new PeriodicTask(name_, taskPriority_, stackSize_, periodInSeconds_, - deadLineMissedFunction_); -} - -FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(TaskName name_, - TaskPriority taskPriority_,TaskStackSize stackSize_, - TaskPeriod periodInSeconds_, - TaskDeadlineMissedFunction deadLineMissedFunction_) { - // This is going to be interesting. Time now learn the C++ threading library - // :-) - 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; -} - - +#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_) { + // This is going to be interesting. Time now learn the C++ threading library + // :-) + return new PeriodicTask(name_, taskPriority_, stackSize_, periodInSeconds_, + deadLineMissedFunction_); +} + +FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(TaskName name_, + TaskPriority taskPriority_,TaskStackSize stackSize_, + TaskPeriod periodInSeconds_, + TaskDeadlineMissedFunction deadLineMissedFunction_) { + // This is going to be interesting. Time now learn the C++ threading library + // :-) + 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/osal/linux/BinarySemaphore.cpp b/osal/linux/BinarySemaphore.cpp index 5216ff37..8c0eeae7 100644 --- a/osal/linux/BinarySemaphore.cpp +++ b/osal/linux/BinarySemaphore.cpp @@ -1,149 +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; -} +#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/osal/linux/BinarySemaphore.h b/osal/linux/BinarySemaphore.h index 4181de85..e9bb8bb6 100644 --- a/osal/linux/BinarySemaphore.h +++ b/osal/linux/BinarySemaphore.h @@ -1,81 +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_ */ +#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/osal/linux/Clock.cpp b/osal/linux/Clock.cpp index 370c77f7..5f764a6f 100644 --- a/osal/linux/Clock.cpp +++ b/osal/linux/Clock.cpp @@ -1,220 +1,220 @@ -#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 - struct sysinfo sysInfo; - int result = sysinfo(&sysInfo); - if(result != 0){ - return HasReturnvaluesIF::RETURN_FAILED; - } - uptime->tv_sec = sysInfo.uptime; - uptime->tv_usec = 0; - - - //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); -// } - - 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; -} +#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 + struct sysinfo sysInfo; + int result = sysinfo(&sysInfo); + if(result != 0){ + return HasReturnvaluesIF::RETURN_FAILED; + } + uptime->tv_sec = sysInfo.uptime; + uptime->tv_usec = 0; + + + //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); +// } + + 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/osal/linux/CountingSemaphore.cpp b/osal/linux/CountingSemaphore.cpp index ef32539b..18339399 100644 --- a/osal/linux/CountingSemaphore.cpp +++ b/osal/linux/CountingSemaphore.cpp @@ -1,54 +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; -} - +#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/osal/linux/CountingSemaphore.h b/osal/linux/CountingSemaphore.h index afe21a61..e0fbb992 100644 --- a/osal/linux/CountingSemaphore.h +++ b/osal/linux/CountingSemaphore.h @@ -1,37 +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_ */ +#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/osal/linux/FixedTimeslotTask.cpp b/osal/linux/FixedTimeslotTask.cpp index 12db83b1..b634b4cf 100644 --- a/osal/linux/FixedTimeslotTask.cpp +++ b/osal/linux/FixedTimeslotTask.cpp @@ -1,96 +1,96 @@ -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../osal/linux/FixedTimeslotTask.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; - } -} +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../osal/linux/FixedTimeslotTask.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/osal/linux/FixedTimeslotTask.h b/osal/linux/FixedTimeslotTask.h index 83bf203e..0148922d 100644 --- a/osal/linux/FixedTimeslotTask.h +++ b/osal/linux/FixedTimeslotTask.h @@ -1,77 +1,77 @@ -#ifndef FRAMEWORK_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ -#define FRAMEWORK_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ - -#include "../../tasks/FixedTimeslotTaskIF.h" -#include "../../tasks/FixedSlotSequence.h" -#include "../../osal/linux/PosixThread.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 /* FRAMEWORK_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ */ +#ifndef FRAMEWORK_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ +#define FRAMEWORK_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ + +#include "../../tasks/FixedTimeslotTaskIF.h" +#include "../../tasks/FixedSlotSequence.h" +#include "../../osal/linux/PosixThread.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 /* FRAMEWORK_OSAL_LINUX_FIXEDTIMESLOTTASK_H_ */ diff --git a/osal/linux/InternalErrorCodes.cpp b/osal/linux/InternalErrorCodes.cpp index 724cb4bc..a01cc72a 100644 --- a/osal/linux/InternalErrorCodes.cpp +++ b/osal/linux/InternalErrorCodes.cpp @@ -1,14 +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() { - -} - +#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/osal/linux/MessageQueue.cpp b/osal/linux/MessageQueue.cpp index 97c89552..87251724 100644 --- a/osal/linux/MessageQueue.cpp +++ b/osal/linux/MessageQueue.cpp @@ -1,369 +1,369 @@ -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../osal/linux/MessageQueue.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; -} +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../osal/linux/MessageQueue.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/osal/linux/MessageQueue.h b/osal/linux/MessageQueue.h index 35d16e25..c0b77019 100644 --- a/osal/linux/MessageQueue.h +++ b/osal/linux/MessageQueue.h @@ -1,187 +1,187 @@ -#ifndef MESSAGEQUEUE_H_ -#define 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 /* MESSAGEQUEUE_H_ */ +#ifndef MESSAGEQUEUE_H_ +#define 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 /* MESSAGEQUEUE_H_ */ diff --git a/osal/linux/Mutex.cpp b/osal/linux/Mutex.cpp index 20be8aee..2ecae0cb 100644 --- a/osal/linux/Mutex.cpp +++ b/osal/linux/Mutex.cpp @@ -1,111 +1,111 @@ -#include "../../osal/linux/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; - }; -} +#include "../../osal/linux/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/osal/linux/Mutex.h b/osal/linux/Mutex.h index 77a11fe4..3acb6d90 100644 --- a/osal/linux/Mutex.h +++ b/osal/linux/Mutex.h @@ -1,22 +1,22 @@ -#ifndef OS_LINUX_MUTEX_H_ -#define OS_LINUX_MUTEX_H_ - -#include "../../ipc/MutexIF.h" - -extern "C" { -#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_ */ +#ifndef OS_LINUX_MUTEX_H_ +#define OS_LINUX_MUTEX_H_ + +#include "../../ipc/MutexIF.h" + +extern "C" { +#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/osal/linux/MutexFactory.cpp b/osal/linux/MutexFactory.cpp index 70dc3c71..73ab10a2 100644 --- a/osal/linux/MutexFactory.cpp +++ b/osal/linux/MutexFactory.cpp @@ -1,23 +1,23 @@ -#include "../../ipc/MutexFactory.h" -#include "../../osal/linux/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; -} +#include "../../ipc/MutexFactory.h" +#include "../../osal/linux/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/osal/linux/PeriodicPosixTask.cpp b/osal/linux/PeriodicPosixTask.cpp index c0a29f47..bbf33b01 100644 --- a/osal/linux/PeriodicPosixTask.cpp +++ b/osal/linux/PeriodicPosixTask.cpp @@ -1,83 +1,83 @@ -#include "../../tasks/ExecutableObjectIF.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../osal/linux/PeriodicPosixTask.h" - -#include - -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 newObject->initializeAfterTaskCreation(); -} - -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(!started){ - suspend(); - } - - uint64_t lastWakeTime = getCurrentMonotonicTimeMs(); - //The task's "infinite" inner loop is entered. - while (1) { - for (ObjectList::iterator it = objectList.begin(); - it != objectList.end(); ++it) { - (*it)->performOperation(); - } - if(!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; -} +#include "../../tasks/ExecutableObjectIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../osal/linux/PeriodicPosixTask.h" + +#include + +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 newObject->initializeAfterTaskCreation(); +} + +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(!started){ + suspend(); + } + + uint64_t lastWakeTime = getCurrentMonotonicTimeMs(); + //The task's "infinite" inner loop is entered. + while (1) { + for (ObjectList::iterator it = objectList.begin(); + it != objectList.end(); ++it) { + (*it)->performOperation(); + } + if(!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/osal/linux/PeriodicPosixTask.h b/osal/linux/PeriodicPosixTask.h index 59302e43..38825bbf 100644 --- a/osal/linux/PeriodicPosixTask.h +++ b/osal/linux/PeriodicPosixTask.h @@ -1,90 +1,90 @@ -#ifndef FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ -#define FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ - -#include "../../tasks/PeriodicTaskIF.h" -#include "../../objectmanager/ObjectManagerIF.h" -#include "../../osal/linux/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(void) 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_ */ +#ifndef FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ +#define FRAMEWORK_OSAL_LINUX_PERIODICPOSIXTASK_H_ + +#include "../../tasks/PeriodicTaskIF.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../osal/linux/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(void) 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/osal/linux/PosixThread.cpp b/osal/linux/PosixThread.cpp index 70c63c4a..f6ace380 100644 --- a/osal/linux/PosixThread.cpp +++ b/osal/linux/PosixThread.cpp @@ -1,217 +1,217 @@ -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../osal/linux/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 << "PosixThread::createTask: pthread_attr_setstack " - " failed with: " << strerror(status) << std::endl; - sif::error << "Make sure the specified stack size is valid and is " - "larger than the minimum allowed stack size." << 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; - } -} +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../osal/linux/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 << "PosixThread::createTask: pthread_attr_setstack " + " failed with: " << strerror(status) << std::endl; + sif::error << "Make sure the specified stack size is valid and is " + "larger than the minimum allowed stack size." << 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/osal/linux/PosixThread.h b/osal/linux/PosixThread.h index 9d6097f7..7d8d349a 100644 --- a/osal/linux/PosixThread.h +++ b/osal/linux/PosixThread.h @@ -1,77 +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_ */ +#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/osal/linux/QueueFactory.cpp b/osal/linux/QueueFactory.cpp index d1332504..fb8f6df7 100644 --- a/osal/linux/QueueFactory.cpp +++ b/osal/linux/QueueFactory.cpp @@ -1,38 +1,38 @@ -#include "../../ipc/QueueFactory.h" -#include -#include -#include "../../osal/linux/MessageQueue.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); -} - -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; -} +#include "../../ipc/QueueFactory.h" +#include +#include +#include "../../osal/linux/MessageQueue.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); +} + +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/osal/linux/SemaphoreFactory.cpp b/osal/linux/SemaphoreFactory.cpp index bcb5d16f..e4710933 100644 --- a/osal/linux/SemaphoreFactory.cpp +++ b/osal/linux/SemaphoreFactory.cpp @@ -1,33 +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; -} +#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/osal/linux/TaskFactory.cpp b/osal/linux/TaskFactory.cpp index 1cd0b2b1..a25b817d 100644 --- a/osal/linux/TaskFactory.cpp +++ b/osal/linux/TaskFactory.cpp @@ -1,42 +1,42 @@ -#include "../../osal/linux/FixedTimeslotTask.h" -#include "../../osal/linux/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() { -} +#include "../../osal/linux/FixedTimeslotTask.h" +#include "../../osal/linux/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/osal/linux/TcUnixUdpPollingTask.cpp b/osal/linux/TcUnixUdpPollingTask.cpp index 04c1d255..d8a39869 100644 --- a/osal/linux/TcUnixUdpPollingTask.cpp +++ b/osal/linux/TcUnixUdpPollingTask.cpp @@ -1,137 +1,137 @@ -#include "../../osal/linux/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 = 0; - 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; - } -} - -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; - } - } -} +#include "../../osal/linux/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 = 0; + 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; + } +} + +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/osal/linux/TcUnixUdpPollingTask.h b/osal/linux/TcUnixUdpPollingTask.h index 9d678a81..cc032561 100644 --- a/osal/linux/TcUnixUdpPollingTask.h +++ b/osal/linux/TcUnixUdpPollingTask.h @@ -1,67 +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_ */ +#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/osal/linux/Timer.cpp b/osal/linux/Timer.cpp index e4539521..b458fd90 100644 --- a/osal/linux/Timer.cpp +++ b/osal/linux/Timer.cpp @@ -1,42 +1,42 @@ -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include -#include "../../osal/linux/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; -} +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include +#include "../../osal/linux/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/osal/linux/TmTcUnixUdpBridge.cpp b/osal/linux/TmTcUnixUdpBridge.cpp index 57722b3f..3e9674f3 100644 --- a/osal/linux/TmTcUnixUdpBridge.cpp +++ b/osal/linux/TmTcUnixUdpBridge.cpp @@ -1,168 +1,168 @@ -#include "../../osal/linux/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(socket < 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; - - 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 << "Error: " << strerror(errno) << std::endl; - } -} - +#include "../../osal/linux/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(socket < 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; + + 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 << "Error: " << strerror(errno) << std::endl; + } +} + diff --git a/osal/linux/TmTcUnixUdpBridge.h b/osal/linux/TmTcUnixUdpBridge.h index e4aa8b1a..5245c44c 100644 --- a/osal/linux/TmTcUnixUdpBridge.h +++ b/osal/linux/TmTcUnixUdpBridge.h @@ -1,48 +1,48 @@ -#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); - -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; - - //! 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_ */ +#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); + +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; + + //! 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/osal/rtems/Clock.cpp b/osal/rtems/Clock.cpp index 826d0b14..eeffd7f3 100644 --- a/osal/rtems/Clock.cpp +++ b/osal/rtems/Clock.cpp @@ -1,198 +1,198 @@ -#include "../../timemanager/Clock.h" -#include "RtemsBasic.h" -#include - -uint16_t Clock::leapSeconds = 0; -MutexIF* Clock::timeMutex = NULL; - -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,NULL); - 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 == 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::NO_TIMEOUT); - 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::NO_TIMEOUT); - 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; -} +#include "../../timemanager/Clock.h" +#include "RtemsBasic.h" +#include + +uint16_t Clock::leapSeconds = 0; +MutexIF* Clock::timeMutex = NULL; + +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,NULL); + 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 == 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::NO_TIMEOUT); + 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::NO_TIMEOUT); + 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/osal/rtems/CpuUsage.cpp b/osal/rtems/CpuUsage.cpp index c5fac653..d49de4ad 100644 --- a/osal/rtems/CpuUsage.cpp +++ b/osal/rtems/CpuUsage.cpp @@ -1,183 +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, - int32_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; -} +#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, + int32_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/osal/rtems/CpuUsage.h b/osal/rtems/CpuUsage.h index 24cbb919..f487e191 100644 --- a/osal/rtems/CpuUsage.h +++ b/osal/rtems/CpuUsage.h @@ -1,53 +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_ */ +#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/osal/rtems/InternalErrorCodes.cpp b/osal/rtems/InternalErrorCodes.cpp index 3542ecce..9346cf15 100644 --- a/osal/rtems/InternalErrorCodes.cpp +++ b/osal/rtems/InternalErrorCodes.cpp @@ -1,60 +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() { - -} - +#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/osal/rtems/Interrupt.h b/osal/rtems/Interrupt.h index 076a6704..2152e2f0 100644 --- a/osal/rtems/Interrupt.h +++ b/osal/rtems/Interrupt.h @@ -1,50 +1,50 @@ -#ifndef OS_RTEMS_INTERRUPT_H_ -#define OS_RTEMS_INTERRUPT_H_ - -#include "../../returnvalues/HasReturnvaluesIF.h" -#include -#include - -typedef rtems_isr_entry IsrHandler_t; -typedef rtems_isr IsrReturn_t; -typedef rtems_vector_number InterruptNumber_t; - -class Interrupt { -public: - virtual ~Interrupt(){}; - - /** - * Establishes a new interrupt service routine. - * @param handler The service routine to establish - * @param interrupt The interrupt (NOT trap type) the routine shall react to. - * @return RETURN_OK on success. Otherwise, the OS failure code is returned. - */ - static ReturnValue_t setInterruptServiceRoutine(IsrHandler_t handler, - InterruptNumber_t interrupt, IsrHandler_t *oldHandler = NULL); - static ReturnValue_t enableInterrupt(InterruptNumber_t interruptNumber); - static ReturnValue_t disableInterrupt(InterruptNumber_t interruptNumber); - /** - * Enables the interrupt given. - * The function tests, if the InterruptMask register was written successfully. - * @param interrupt The interrupt to enable. - * @return RETURN_OK if the interrupt was set successfully. RETURN_FAILED else. - */ - static ReturnValue_t enableGpioInterrupt(InterruptNumber_t interrupt); - /** - * Disables the interrupt given. - * @param interrupt The interrupt to disable. - * @return RETURN_OK if the interrupt was set successfully. RETURN_FAILED else. - */ - static ReturnValue_t disableGpioInterrupt(InterruptNumber_t interrupt); - - - /** - * Checks if the current executing context is an ISR. - * @return true if handling an interrupt, false else. - */ - static bool isInterruptInProgress(); - -}; - - -#endif /* OS_RTEMS_INTERRUPT_H_ */ +#ifndef OS_RTEMS_INTERRUPT_H_ +#define OS_RTEMS_INTERRUPT_H_ + +#include "../../returnvalues/HasReturnvaluesIF.h" +#include +#include + +typedef rtems_isr_entry IsrHandler_t; +typedef rtems_isr IsrReturn_t; +typedef rtems_vector_number InterruptNumber_t; + +class Interrupt { +public: + virtual ~Interrupt(){}; + + /** + * Establishes a new interrupt service routine. + * @param handler The service routine to establish + * @param interrupt The interrupt (NOT trap type) the routine shall react to. + * @return RETURN_OK on success. Otherwise, the OS failure code is returned. + */ + static ReturnValue_t setInterruptServiceRoutine(IsrHandler_t handler, + InterruptNumber_t interrupt, IsrHandler_t *oldHandler = NULL); + static ReturnValue_t enableInterrupt(InterruptNumber_t interruptNumber); + static ReturnValue_t disableInterrupt(InterruptNumber_t interruptNumber); + /** + * Enables the interrupt given. + * The function tests, if the InterruptMask register was written successfully. + * @param interrupt The interrupt to enable. + * @return RETURN_OK if the interrupt was set successfully. RETURN_FAILED else. + */ + static ReturnValue_t enableGpioInterrupt(InterruptNumber_t interrupt); + /** + * Disables the interrupt given. + * @param interrupt The interrupt to disable. + * @return RETURN_OK if the interrupt was set successfully. RETURN_FAILED else. + */ + static ReturnValue_t disableGpioInterrupt(InterruptNumber_t interrupt); + + + /** + * Checks if the current executing context is an ISR. + * @return true if handling an interrupt, false else. + */ + static bool isInterruptInProgress(); + +}; + + +#endif /* OS_RTEMS_INTERRUPT_H_ */ diff --git a/osal/rtems/MessageQueue.cpp b/osal/rtems/MessageQueue.cpp index e0752409..700db444 100644 --- a/osal/rtems/MessageQueue.cpp +++ b/osal/rtems/MessageQueue.cpp @@ -1,147 +1,147 @@ -#include "../../serviceinterface/ServiceInterfaceStream.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(NULL) { - 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) { - 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, - MessageQueueMessage* message, bool ignoreFault) { - return sendMessageFrom(sendTo, message, this->getId(), ignoreFault); -} - -ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessage* message) { - return sendToDefaultFrom(message, this->getId()); -} - -ReturnValue_t MessageQueue::reply(MessageQueueMessage* message) { - if (this->lastPartner != 0) { - return sendMessage(this->lastPartner, message, this->getId()); - } else { - return NO_REPLY_PARTNER; - } -} - -ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message, - MessageQueueId_t* receivedFrom) { - ReturnValue_t status = this->receiveMessage(message); - *receivedFrom = this->lastPartner; - return status; -} - -ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) { - rtems_status_code status = rtems_message_queue_receive(id, - message->getBuffer(), &(message->messageSize), - RTEMS_NO_WAIT, 1); - if (status == RTEMS_SUCCESSFUL) { - this->lastPartner = message->getSender(); - //Check size of incoming message. - if (message->messageSize < 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->MAX_DATA_SIZE); - } - 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, - MessageQueueMessage* message, MessageQueueId_t sentFrom, - bool ignoreFault) { - - message->setSender(sentFrom); - rtems_status_code result = rtems_message_queue_send(sendTo, - message->getBuffer(), message->messageSize); - - //TODO: Check if we're in ISR. - if (result != RTEMS_SUCCESSFUL && !ignoreFault) { - if (internalErrorReporter == NULL) { - internalErrorReporter = objectManager->get( - objects::INTERNAL_ERROR_REPORTER); - } - if (internalErrorReporter != NULL) { - internalErrorReporter->queueMessageNotSent(); - } - } - - ReturnValue_t returnCode = convertReturnCode(result); - if(result == MessageQueueIF::EMPTY){ - return HasReturnvaluesIF::RETURN_FAILED; - } - - return returnCode; -} - -ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessage* 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; +#include "../../serviceinterface/ServiceInterfaceStream.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(NULL) { + 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) { + 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, + MessageQueueMessage* message, bool ignoreFault) { + return sendMessageFrom(sendTo, message, this->getId(), ignoreFault); +} + +ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessage* message) { + return sendToDefaultFrom(message, this->getId()); +} + +ReturnValue_t MessageQueue::reply(MessageQueueMessage* message) { + if (this->lastPartner != 0) { + return sendMessage(this->lastPartner, message, this->getId()); + } else { + return NO_REPLY_PARTNER; + } +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message, + MessageQueueId_t* receivedFrom) { + ReturnValue_t status = this->receiveMessage(message); + *receivedFrom = this->lastPartner; + return status; +} + +ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) { + rtems_status_code status = rtems_message_queue_receive(id, + message->getBuffer(), &(message->messageSize), + RTEMS_NO_WAIT, 1); + if (status == RTEMS_SUCCESSFUL) { + this->lastPartner = message->getSender(); + //Check size of incoming message. + if (message->messageSize < 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->MAX_DATA_SIZE); + } + 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, + MessageQueueMessage* message, MessageQueueId_t sentFrom, + bool ignoreFault) { + + message->setSender(sentFrom); + rtems_status_code result = rtems_message_queue_send(sendTo, + message->getBuffer(), message->messageSize); + + //TODO: Check if we're in ISR. + if (result != RTEMS_SUCCESSFUL && !ignoreFault) { + if (internalErrorReporter == NULL) { + internalErrorReporter = objectManager->get( + objects::INTERNAL_ERROR_REPORTER); + } + if (internalErrorReporter != NULL) { + internalErrorReporter->queueMessageNotSent(); + } + } + + ReturnValue_t returnCode = convertReturnCode(result); + if(result == MessageQueueIF::EMPTY){ + return HasReturnvaluesIF::RETURN_FAILED; + } + + return returnCode; +} + +ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessage* 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/osal/rtems/MessageQueue.h b/osal/rtems/MessageQueue.h index 2d1bd8db..c6fc62d5 100644 --- a/osal/rtems/MessageQueue.h +++ b/osal/rtems/MessageQueue.h @@ -1,181 +1,181 @@ -/** - * @file MessageQueue.h - * - * @date 10/02/2012 - * @author Bastian Baetz - * - * @brief This file contains the definition of the MessageQueue class. - */ - -#ifndef MESSAGEQUEUE_H_ -#define 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, - MessageQueueMessage* 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( MessageQueueMessage* 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( MessageQueueMessage* 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(MessageQueueMessage* 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(MessageQueueMessage* 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, MessageQueueMessage* 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( MessageQueueMessage* 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 /* MESSAGEQUEUE_H_ */ +/** + * @file MessageQueue.h + * + * @date 10/02/2012 + * @author Bastian Baetz + * + * @brief This file contains the definition of the MessageQueue class. + */ + +#ifndef MESSAGEQUEUE_H_ +#define 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, + MessageQueueMessage* 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( MessageQueueMessage* 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( MessageQueueMessage* 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(MessageQueueMessage* 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(MessageQueueMessage* 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, MessageQueueMessage* 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( MessageQueueMessage* 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 /* MESSAGEQUEUE_H_ */ diff --git a/osal/rtems/MultiObjectTask.cpp b/osal/rtems/MultiObjectTask.cpp index 8cec10f7..8f969a1d 100644 --- a/osal/rtems/MultiObjectTask.cpp +++ b/osal/rtems/MultiObjectTask.cpp @@ -1,87 +1,87 @@ -/** - * @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) { - 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); - error << "ObjectTask: " << ptr << " Deadline missed." << std::endl; - if (this->deadlineMissedFunc != NULL) { - this->deadlineMissedFunc(); - } - } - } -} - -ReturnValue_t MultiObjectTask::addComponent(object_id_t object) { - ExecutableObjectIF* newObject = objectManager->get( - object); - if (newObject == NULL) { - return HasReturnvaluesIF::RETURN_FAILED; - } - objectList.push_back(newObject); - ReturnValue_t result = newObject->initializeAfterTaskCreation(); - return result; -} - -uint32_t MultiObjectTask::getPeriodMs() const { - return RtemsBasic::convertTicksToMs(periodTicks); -} +/** + * @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) { + 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); + error << "ObjectTask: " << ptr << " Deadline missed." << std::endl; + if (this->deadlineMissedFunc != NULL) { + this->deadlineMissedFunc(); + } + } + } +} + +ReturnValue_t MultiObjectTask::addComponent(object_id_t object) { + ExecutableObjectIF* newObject = objectManager->get( + object); + if (newObject == NULL) { + return HasReturnvaluesIF::RETURN_FAILED; + } + objectList.push_back(newObject); + ReturnValue_t result = newObject->initializeAfterTaskCreation(); + return result; +} + +uint32_t MultiObjectTask::getPeriodMs() const { + return RtemsBasic::convertTicksToMs(periodTicks); +} diff --git a/osal/rtems/MultiObjectTask.h b/osal/rtems/MultiObjectTask.h index b3ff66c2..28d05fb1 100644 --- a/osal/rtems/MultiObjectTask.h +++ b/osal/rtems/MultiObjectTask.h @@ -1,113 +1,113 @@ -/** - * @file MultiObjectTask.h - * @brief This file defines the MultiObjectTask class. - * @date 30.01.2014 - * @author baetz - */ -#ifndef MULTIOBJECTTASK_H_ -#define 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. - * - * @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); - - uint32_t getPeriodMs() const; - - ReturnValue_t sleepFor(uint32_t ms); -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 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 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 /* MULTIOBJECTTASK_H_ */ +/** + * @file MultiObjectTask.h + * @brief This file defines the MultiObjectTask class. + * @date 30.01.2014 + * @author baetz + */ +#ifndef MULTIOBJECTTASK_H_ +#define 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. + * + * @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); + + uint32_t getPeriodMs() const; + + ReturnValue_t sleepFor(uint32_t ms); +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 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 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 /* MULTIOBJECTTASK_H_ */ diff --git a/osal/rtems/Mutex.cpp b/osal/rtems/Mutex.cpp index 7ead8851..d8babcde 100644 --- a/osal/rtems/Mutex.cpp +++ b/osal/rtems/Mutex.cpp @@ -1,65 +1,65 @@ -#include "Mutex.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" - -const uint32_t MutexIF::NO_TIMEOUT = RTEMS_NO_TIMEOUT; -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) { - 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) { - error << "Mutex: deletion for id " << mutexId - << " failed with " << status << std::endl; - } -} - -ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) { - rtems_status_code 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; - } -} +#include "Mutex.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" + +const uint32_t MutexIF::NO_TIMEOUT = RTEMS_NO_TIMEOUT; +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) { + 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) { + error << "Mutex: deletion for id " << mutexId + << " failed with " << status << std::endl; + } +} + +ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) { + rtems_status_code 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/osal/rtems/Mutex.h b/osal/rtems/Mutex.h index fd76b86a..340bb16b 100644 --- a/osal/rtems/Mutex.h +++ b/osal/rtems/Mutex.h @@ -1,18 +1,18 @@ -#ifndef OS_RTEMS_MUTEX_H_ -#define OS_RTEMS_MUTEX_H_ - -#include "../../ipc/MutexIF.h" -#include "RtemsBasic.h" - -class Mutex : public MutexIF { -public: - Mutex(); - ~Mutex(); - ReturnValue_t lockMutex(uint32_t timeoutMs); - ReturnValue_t unlockMutex(); -private: - rtems_id mutexId; - static uint8_t count; -}; - -#endif /* OS_RTEMS_MUTEX_H_ */ +#ifndef OS_RTEMS_MUTEX_H_ +#define OS_RTEMS_MUTEX_H_ + +#include "../../ipc/MutexIF.h" +#include "RtemsBasic.h" + +class Mutex : public MutexIF { +public: + Mutex(); + ~Mutex(); + ReturnValue_t lockMutex(uint32_t timeoutMs); + ReturnValue_t unlockMutex(); +private: + rtems_id mutexId; + static uint8_t count; +}; + +#endif /* OS_RTEMS_MUTEX_H_ */ diff --git a/osal/rtems/MutexFactory.cpp b/osal/rtems/MutexFactory.cpp index 3ffab927..ea594789 100644 --- a/osal/rtems/MutexFactory.cpp +++ b/osal/rtems/MutexFactory.cpp @@ -1,24 +1,24 @@ -#include "../../ipc/MutexFactory.h" -#include "Mutex.h" -#include "RtemsBasic.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; -} +#include "../../ipc/MutexFactory.h" +#include "Mutex.h" +#include "RtemsBasic.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/osal/rtems/PollingTask.cpp b/osal/rtems/PollingTask.cpp index b53d6a5a..efb425d9 100644 --- a/osal/rtems/PollingTask.cpp +++ b/osal/rtems/PollingTask.cpp @@ -1,122 +1,122 @@ -#include "../../tasks/FixedSequenceSlot.h" -#include "../../objectmanager/SystemObjectIF.h" -#include "../../osal/rtems/PollingTask.h" -#include "../../osal/rtems/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(); - debug << "Polling task " << originalTask->getId() - << " returned from taskFunctionality." << std::endl; -} - -void PollingTask::missedDeadlineCounter() { - PollingTask::deadlineMissedCount++; - if (PollingTask::deadlineMissedCount % 10 == 0) { - 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) { - 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) { - if (objectManager->get(componentId) != nullptr) { - pst.addSlot(componentId, slotTimeMs, executionStep, 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::checkAndInitializeSequence() 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. - std::list::iterator 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 != NULL) { - 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); -}; +#include "../../tasks/FixedSequenceSlot.h" +#include "../../objectmanager/SystemObjectIF.h" +#include "../../osal/rtems/PollingTask.h" +#include "../../osal/rtems/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(); + debug << "Polling task " << originalTask->getId() + << " returned from taskFunctionality." << std::endl; +} + +void PollingTask::missedDeadlineCounter() { + PollingTask::deadlineMissedCount++; + if (PollingTask::deadlineMissedCount % 10 == 0) { + 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) { + 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) { + if (objectManager->get(componentId) != nullptr) { + pst.addSlot(componentId, slotTimeMs, executionStep, 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::checkAndInitializeSequence() 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. + std::list::iterator 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 != NULL) { + 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/osal/rtems/PollingTask.h b/osal/rtems/PollingTask.h index 60ce0816..1502f0c6 100644 --- a/osal/rtems/PollingTask.h +++ b/osal/rtems/PollingTask.h @@ -1,85 +1,85 @@ -#ifndef POLLINGTASK_H_ -#define 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 checkAndInitializeSequence() 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 /* POLLINGTASK_H_ */ +#ifndef POLLINGTASK_H_ +#define 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 checkAndInitializeSequence() 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 /* POLLINGTASK_H_ */ diff --git a/osal/rtems/QueueFactory.cpp b/osal/rtems/QueueFactory.cpp index a492d400..fce55a0e 100644 --- a/osal/rtems/QueueFactory.cpp +++ b/osal/rtems/QueueFactory.cpp @@ -1,59 +1,59 @@ -#include "../../ipc/QueueFactory.h" -#include "MessageQueue.h" -#include "RtemsBasic.h" - -QueueFactory* QueueFactory::factoryInstance = NULL; - - -ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo, - MessageQueueMessage* 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->messageSize); - 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 == NULL) { - 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; -} +#include "../../ipc/QueueFactory.h" +#include "MessageQueue.h" +#include "RtemsBasic.h" + +QueueFactory* QueueFactory::factoryInstance = NULL; + + +ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo, + MessageQueueMessage* 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->messageSize); + 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 == NULL) { + 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/osal/rtems/RtemsBasic.h b/osal/rtems/RtemsBasic.h index b9aa33e8..78e0d46e 100644 --- a/osal/rtems/RtemsBasic.h +++ b/osal/rtems/RtemsBasic.h @@ -1,25 +1,25 @@ -#ifndef OS_RTEMS_RTEMSBASIC_H_ -#define OS_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 /* OS_RTEMS_RTEMSBASIC_H_ */ +#ifndef OS_RTEMS_RTEMSBASIC_H_ +#define OS_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 /* OS_RTEMS_RTEMSBASIC_H_ */ diff --git a/osal/rtems/TaskBase.cpp b/osal/rtems/TaskBase.cpp index b95ab51f..adf6ee70 100644 --- a/osal/rtems/TaskBase.cpp +++ b/osal/rtems/TaskBase.cpp @@ -1,82 +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) { - 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; -} +#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) { + 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/osal/rtems/TaskBase.h b/osal/rtems/TaskBase.h index 3010a972..410d9110 100644 --- a/osal/rtems/TaskBase.h +++ b/osal/rtems/TaskBase.h @@ -1,47 +1,47 @@ -#ifndef TASKBASE_H_ -#define 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 /* TASKBASE_H_ */ +#ifndef TASKBASE_H_ +#define 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 /* TASKBASE_H_ */ diff --git a/osal/rtems/TaskFactory.cpp b/osal/rtems/TaskFactory.cpp index f79571f0..bab48a44 100644 --- a/osal/rtems/TaskFactory.cpp +++ b/osal/rtems/TaskFactory.cpp @@ -1,41 +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() { -} +#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/parameters/HasParametersIF.h b/parameters/HasParametersIF.h index 14446e3c..0c6afc12 100644 --- a/parameters/HasParametersIF.h +++ b/parameters/HasParametersIF.h @@ -1,67 +1,67 @@ -#ifndef HASPARAMETERSIF_H_ -#define 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 /* HASPARAMETERSIF_H_ */ +#ifndef HASPARAMETERSIF_H_ +#define 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 /* HASPARAMETERSIF_H_ */ diff --git a/parameters/ParameterHelper.cpp b/parameters/ParameterHelper.cpp index 55633566..ec7ded3c 100644 --- a/parameters/ParameterHelper.cpp +++ b/parameters/ParameterHelper.cpp @@ -1,132 +1,132 @@ -#include "../objectmanager/ObjectManagerIF.h" -#include "../parameters/ParameterHelper.h" -#include "../parameters/ParameterMessage.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); -} +#include "../objectmanager/ObjectManagerIF.h" +#include "../parameters/ParameterHelper.h" +#include "../parameters/ParameterMessage.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/parameters/ParameterHelper.h b/parameters/ParameterHelper.h index 668bb081..5dd78b64 100644 --- a/parameters/ParameterHelper.h +++ b/parameters/ParameterHelper.h @@ -1,36 +1,36 @@ -#ifndef FRAMEWORK_PARAMETERS_PARAMETERHELPER_H_ -#define FRAMEWORK_PARAMETERS_PARAMETERHELPER_H_ - -#include "../ipc/MessageQueueIF.h" -#include "../parameters/ParameterMessage.h" -#include "../parameters/ReceivesParameterMessagesIF.h" - -/** - * @brief Helper class to handle parameter messages - * @details - * This class simplfiies 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 /* PARAMETERHELPER_H_ */ +#ifndef FRAMEWORK_PARAMETERS_PARAMETERHELPER_H_ +#define FRAMEWORK_PARAMETERS_PARAMETERHELPER_H_ + +#include "../ipc/MessageQueueIF.h" +#include "../parameters/ParameterMessage.h" +#include "../parameters/ReceivesParameterMessagesIF.h" + +/** + * @brief Helper class to handle parameter messages + * @details + * This class simplfiies 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 /* PARAMETERHELPER_H_ */ diff --git a/parameters/ParameterMessage.cpp b/parameters/ParameterMessage.cpp index 0cc4dd90..e9f3191f 100644 --- a/parameters/ParameterMessage.cpp +++ b/parameters/ParameterMessage.cpp @@ -1,48 +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; - } -} +#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/parameters/ParameterMessage.h b/parameters/ParameterMessage.h index 51321f2b..de68dcfc 100644 --- a/parameters/ParameterMessage.h +++ b/parameters/ParameterMessage.h @@ -1,29 +1,29 @@ -#ifndef PARAMETERMESSAGE_H_ -#define PARAMETERMESSAGE_H_ - -#include "../ipc/CommandMessage.h" -#include "../parameters/HasParametersIF.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 /* PARAMETERMESSAGE_H_ */ +#ifndef PARAMETERMESSAGE_H_ +#define PARAMETERMESSAGE_H_ + +#include "../ipc/CommandMessage.h" +#include "../parameters/HasParametersIF.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 /* PARAMETERMESSAGE_H_ */ diff --git a/parameters/ParameterWrapper.cpp b/parameters/ParameterWrapper.cpp index 8e6dd86c..fd36a0a9 100644 --- a/parameters/ParameterWrapper.cpp +++ b/parameters/ParameterWrapper.cpp @@ -1,280 +1,280 @@ -#include "../parameters/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++) { - uint8_t offset = (((startingRow + fromRow) * columns) + startingColumn) * typeSize; - std::memcpy(typedData + offset, from->readonlyData, - typeSize * from->columns); - } - } - - return result; -} +#include "../parameters/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++) { + uint8_t offset = (((startingRow + fromRow) * columns) + startingColumn) * typeSize; + std::memcpy(typedData + offset, from->readonlyData, + typeSize * from->columns); + } + } + + return result; +} diff --git a/parameters/ParameterWrapper.h b/parameters/ParameterWrapper.h index 1f38f691..35045959 100644 --- a/parameters/ParameterWrapper.h +++ b/parameters/ParameterWrapper.h @@ -1,162 +1,162 @@ -#ifndef FRAMEWORK_PARAMETERS_PARAMETERWRAPPER_H_ -#define FRAMEWORK_PARAMETERS_PARAMETERWRAPPER_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../serialize/SerializeAdapter.h" -#include "../serialize/SerializeIF.h" -#include -#include "../globalfunctions/Type.h" - -/** - * @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 /* PARAMETERWRAPPER_H_ */ +#ifndef FRAMEWORK_PARAMETERS_PARAMETERWRAPPER_H_ +#define FRAMEWORK_PARAMETERS_PARAMETERWRAPPER_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/SerializeAdapter.h" +#include "../serialize/SerializeIF.h" +#include +#include "../globalfunctions/Type.h" + +/** + * @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 /* PARAMETERWRAPPER_H_ */ diff --git a/parameters/ReceivesParameterMessagesIF.h b/parameters/ReceivesParameterMessagesIF.h index 9a280b4f..f8c3746e 100644 --- a/parameters/ReceivesParameterMessagesIF.h +++ b/parameters/ReceivesParameterMessagesIF.h @@ -1,19 +1,19 @@ -#ifndef RECEIVESPARAMETERMESSAGESIF_H_ -#define RECEIVESPARAMETERMESSAGESIF_H_ - - -#include "../parameters/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 /* RECEIVESPARAMETERMESSAGESIF_H_ */ +#ifndef RECEIVESPARAMETERMESSAGESIF_H_ +#define RECEIVESPARAMETERMESSAGESIF_H_ + + +#include "../parameters/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 /* RECEIVESPARAMETERMESSAGESIF_H_ */ diff --git a/power/Fuse.cpp b/power/Fuse.cpp index 59d60491..3ea0b18c 100644 --- a/power/Fuse.cpp +++ b/power/Fuse.cpp @@ -1,260 +1,260 @@ -#include "../monitoring/LimitViolationReporter.h" -#include "../monitoring/MonitoringMessageContent.h" -#include "../objectmanager/ObjectManagerIF.h" -#include "../power/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, - GlobalDataPool::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. - } -} +#include "../monitoring/LimitViolationReporter.h" +#include "../monitoring/MonitoringMessageContent.h" +#include "../objectmanager/ObjectManagerIF.h" +#include "../power/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, + GlobalDataPool::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/power/Fuse.h b/power/Fuse.h index e6d6929c..dd8759f4 100644 --- a/power/Fuse.h +++ b/power/Fuse.h @@ -1,106 +1,106 @@ -#ifndef FUSE_H_ -#define FUSE_H_ - -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/GlobalPoolVariable.h" -#include "../datapoolglob/PIDReader.h" -#include "../devicehandlers/HealthDevice.h" -#include "../monitoring/AbsLimitMonitor.h" -#include "../power/PowerComponentIF.h" -#include "../power/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; - GlobDataSet set; - PIDReader voltage; - PIDReader current; - PIDReader state; - gp_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_ */ +#ifndef FUSE_H_ +#define FUSE_H_ + +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/GlobalPoolVariable.h" +#include "../datapoolglob/PIDReader.h" +#include "../devicehandlers/HealthDevice.h" +#include "../monitoring/AbsLimitMonitor.h" +#include "../power/PowerComponentIF.h" +#include "../power/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; + GlobDataSet set; + PIDReader voltage; + PIDReader current; + PIDReader state; + gp_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/power/PowerComponent.cpp b/power/PowerComponent.cpp index ef852e64..a3797055 100644 --- a/power/PowerComponent.cpp +++ b/power/PowerComponent.cpp @@ -1,86 +1,86 @@ -/** - * @file PowerComponent.cpp - * @brief This file defines the PowerComponent class. - * @date 28.08.2014 - * @author baetz - */ - -#include "../power/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; -} +/** + * @file PowerComponent.cpp + * @brief This file defines the PowerComponent class. + * @date 28.08.2014 + * @author baetz + */ + +#include "../power/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/power/PowerComponent.h b/power/PowerComponent.h index 2e11fa82..dc5217be 100644 --- a/power/PowerComponent.h +++ b/power/PowerComponent.h @@ -1,48 +1,48 @@ -#ifndef POWERCOMPONENT_H_ -#define POWERCOMPONENT_H_ - -#include "../objectmanager/SystemObjectIF.h" -#include "../power/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_ */ +#ifndef POWERCOMPONENT_H_ +#define POWERCOMPONENT_H_ + +#include "../objectmanager/SystemObjectIF.h" +#include "../power/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/power/PowerComponentIF.h b/power/PowerComponentIF.h index eeaa39fb..c2e3a6a5 100644 --- a/power/PowerComponentIF.h +++ b/power/PowerComponentIF.h @@ -1,24 +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_ */ +#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/power/PowerSensor.cpp b/power/PowerSensor.cpp index 51300db7..df26fb8e 100644 --- a/power/PowerSensor.cpp +++ b/power/PowerSensor.cpp @@ -1,128 +1,128 @@ -#include "../power/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; -} +#include "../power/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/power/PowerSensor.h b/power/PowerSensor.h index dc859ab3..da0bd8c7 100644 --- a/power/PowerSensor.h +++ b/power/PowerSensor.h @@ -1,71 +1,71 @@ -#ifndef POWERSENSOR_H_ -#define POWERSENSOR_H_ - -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/GlobalPoolVariable.h" -#include "../datapoolglob/PIDReader.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; - GlobDataSet set; - //Variables in - PIDReader current; - PIDReader voltage; - //Variables out - gp_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_ */ +#ifndef POWERSENSOR_H_ +#define POWERSENSOR_H_ + +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/GlobalPoolVariable.h" +#include "../datapoolglob/PIDReader.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; + GlobDataSet set; + //Variables in + PIDReader current; + PIDReader voltage; + //Variables out + gp_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/power/PowerSwitchIF.h b/power/PowerSwitchIF.h index 817c4ce1..876a82c5 100644 --- a/power/PowerSwitchIF.h +++ b/power/PowerSwitchIF.h @@ -1,80 +1,80 @@ -/** - * @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" -/** - * - * @brief This interface defines a connection to a device that is capable of turning on and off - * switches of devices identified by a switch ID. - * @details The virtual functions of this interface do not allow to make any assignments - * because they can be called asynchronosuly (const ending). - * - * @ingroup interfaces - */ -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_ */ +/** + * @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" +/** + * + * @brief This interface defines a connection to a device that is capable of turning on and off + * switches of devices identified by a switch ID. + * @details The virtual functions of this interface do not allow to make any assignments + * because they can be called asynchronosuly (const ending). + * + * @ingroup interfaces + */ +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/power/PowerSwitcher.cpp b/power/PowerSwitcher.cpp index 73d9f89b..1844a11a 100644 --- a/power/PowerSwitcher.cpp +++ b/power/PowerSwitcher.cpp @@ -1,127 +1,127 @@ -#include "../objectmanager/ObjectManagerIF.h" -#include "../power/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; -} +#include "../objectmanager/ObjectManagerIF.h" +#include "../power/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/power/PowerSwitcher.h b/power/PowerSwitcher.h index 39f44f50..515ce042 100644 --- a/power/PowerSwitcher.h +++ b/power/PowerSwitcher.h @@ -1,45 +1,45 @@ -#ifndef POWERSWITCHER_H_ -#define POWERSWITCHER_H_ -#include "../power/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_ */ +#ifndef POWERSWITCHER_H_ +#define POWERSWITCHER_H_ +#include "../power/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/pus/CService200ModeCommanding.cpp b/pus/CService200ModeCommanding.cpp index 8a51469f..dda58e03 100644 --- a/pus/CService200ModeCommanding.cpp +++ b/pus/CService200ModeCommanding.cpp @@ -1,119 +1,119 @@ -#include "../pus/CService200ModeCommanding.h" -#include "../pus/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)); - return sendTmPacket(Subservice::REPLY_WRONG_MODE_REPLY, &wrongModeReply); -} - -ReturnValue_t CService200ModeCommanding::prepareCantReachModeReply( - const CommandMessage *reply, object_id_t objectId) { - CantReachModePacket cantReachModePacket(objectId, - ModeMessage::getCantReachModeReason(reply)); - return sendTmPacket(Subservice::REPLY_CANT_REACH_MODE, - &cantReachModePacket); -} +#include "../pus/CService200ModeCommanding.h" +#include "../pus/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)); + return sendTmPacket(Subservice::REPLY_WRONG_MODE_REPLY, &wrongModeReply); +} + +ReturnValue_t CService200ModeCommanding::prepareCantReachModeReply( + const CommandMessage *reply, object_id_t objectId) { + CantReachModePacket cantReachModePacket(objectId, + ModeMessage::getCantReachModeReason(reply)); + return sendTmPacket(Subservice::REPLY_CANT_REACH_MODE, + &cantReachModePacket); +} diff --git a/pus/CService200ModeCommanding.h b/pus/CService200ModeCommanding.h index dfb4dfa6..89347dbd 100644 --- a/pus/CService200ModeCommanding.h +++ b/pus/CService200ModeCommanding.h @@ -1,85 +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_ */ +#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/pus/CService201HealthCommanding.cpp b/pus/CService201HealthCommanding.cpp index 057ff707..cc5dd640 100644 --- a/pus/CService201HealthCommanding.cpp +++ b/pus/CService201HealthCommanding.cpp @@ -1,84 +1,84 @@ -#include "CService201HealthCommanding.h" - -#include "../health/HasHealthIF.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../health/HealthMessage.h" -#include "servicepackets/Service201Packets.h" - -CService201HealthCommanding::CService201HealthCommanding(object_id_t objectId, - uint16_t apid, uint8_t serviceId): - CommandingServiceBase(objectId, apid, serviceId, - NUMBER_OF_PARALLEL_COMMANDS,COMMAND_TIMEOUT_SECONDS) { -} - -CService201HealthCommanding::~CService201HealthCommanding() { -} - -ReturnValue_t CService201HealthCommanding::isValidSubservice(uint8_t subservice) { - switch(subservice) { - case(Subservice::COMMAND_SET_HEALTH): - return RETURN_OK; - default: - sif::error << "Invalid Subservice" << std::endl; - return AcceptsTelecommandsIF::INVALID_SUBSERVICE; - } -} - -ReturnValue_t CService201HealthCommanding::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 CService201HealthCommanding::checkInterfaceAndAcquireMessageQueue( - MessageQueueId_t* messageQueueToSet, object_id_t* objectId) { - HasHealthIF * destination = objectManager->get(*objectId); - if(destination == nullptr) { - return CommandingServiceBase::INVALID_OBJECT; - } - - *messageQueueToSet = destination->getCommandQueue(); - return HasReturnvaluesIF::RETURN_OK; -} - -ReturnValue_t CService201HealthCommanding::prepareCommand - (CommandMessage* message, uint8_t subservice, const uint8_t *tcData, - size_t tcDataLen, uint32_t *state, object_id_t objectId) { - HealthCommand healthCommand; - ReturnValue_t result = healthCommand.deSerialize(&tcData, &tcDataLen, - SerializeIF::Endianness::BIG); - if (result != RETURN_OK) { - return result; - } else { - HealthMessage::setHealthMessage(dynamic_cast(message), - HealthMessage::HEALTH_SET, healthCommand.getHealth()); - return result; - } -} - -ReturnValue_t CService201HealthCommanding::handleReply - (const CommandMessage* reply, Command_t previousCommand, - uint32_t *state, CommandMessage* optionalNextCommand, - object_id_t objectId, bool *isStep) { - Command_t replyId = reply->getCommand(); - if (replyId == HealthMessage::REPLY_HEALTH_SET) { - prepareHealthSetReply(reply); - } - return RETURN_OK; -} - -void CService201HealthCommanding::prepareHealthSetReply( - const CommandMessage* reply) { - prepareHealthSetReply(reply); - uint8_t health = static_cast(HealthMessage::getHealth(reply)); - uint8_t oldHealth = static_cast(HealthMessage::getOldHealth(reply)); - HealthSetReply healthSetReply(health, oldHealth); - sendTmPacket(Subservice::REPLY_HEALTH_SET,&healthSetReply); -} - +#include "CService201HealthCommanding.h" + +#include "../health/HasHealthIF.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../health/HealthMessage.h" +#include "servicepackets/Service201Packets.h" + +CService201HealthCommanding::CService201HealthCommanding(object_id_t objectId, + uint16_t apid, uint8_t serviceId): + CommandingServiceBase(objectId, apid, serviceId, + NUMBER_OF_PARALLEL_COMMANDS,COMMAND_TIMEOUT_SECONDS) { +} + +CService201HealthCommanding::~CService201HealthCommanding() { +} + +ReturnValue_t CService201HealthCommanding::isValidSubservice(uint8_t subservice) { + switch(subservice) { + case(Subservice::COMMAND_SET_HEALTH): + return RETURN_OK; + default: + sif::error << "Invalid Subservice" << std::endl; + return AcceptsTelecommandsIF::INVALID_SUBSERVICE; + } +} + +ReturnValue_t CService201HealthCommanding::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 CService201HealthCommanding::checkInterfaceAndAcquireMessageQueue( + MessageQueueId_t* messageQueueToSet, object_id_t* objectId) { + HasHealthIF * destination = objectManager->get(*objectId); + if(destination == nullptr) { + return CommandingServiceBase::INVALID_OBJECT; + } + + *messageQueueToSet = destination->getCommandQueue(); + return HasReturnvaluesIF::RETURN_OK; +} + +ReturnValue_t CService201HealthCommanding::prepareCommand + (CommandMessage* message, uint8_t subservice, const uint8_t *tcData, + size_t tcDataLen, uint32_t *state, object_id_t objectId) { + HealthCommand healthCommand; + ReturnValue_t result = healthCommand.deSerialize(&tcData, &tcDataLen, + SerializeIF::Endianness::BIG); + if (result != RETURN_OK) { + return result; + } else { + HealthMessage::setHealthMessage(dynamic_cast(message), + HealthMessage::HEALTH_SET, healthCommand.getHealth()); + return result; + } +} + +ReturnValue_t CService201HealthCommanding::handleReply + (const CommandMessage* reply, Command_t previousCommand, + uint32_t *state, CommandMessage* optionalNextCommand, + object_id_t objectId, bool *isStep) { + Command_t replyId = reply->getCommand(); + if (replyId == HealthMessage::REPLY_HEALTH_SET) { + prepareHealthSetReply(reply); + } + return RETURN_OK; +} + +void CService201HealthCommanding::prepareHealthSetReply( + const CommandMessage* reply) { + prepareHealthSetReply(reply); + uint8_t health = static_cast(HealthMessage::getHealth(reply)); + uint8_t oldHealth = static_cast(HealthMessage::getOldHealth(reply)); + HealthSetReply healthSetReply(health, oldHealth); + sendTmPacket(Subservice::REPLY_HEALTH_SET,&healthSetReply); +} + diff --git a/pus/CService201HealthCommanding.h b/pus/CService201HealthCommanding.h index 85d2e24c..100f675e 100644 --- a/pus/CService201HealthCommanding.h +++ b/pus/CService201HealthCommanding.h @@ -1,58 +1,58 @@ -#ifndef FSFW_PUS_CSERVICE201HEALTHCOMMANDING_H_ -#define FSFW_PUS_CSERVICE201HEALTHCOMMANDING_H_ - -#include "../tmtcservices/CommandingServiceBase.h" - -/** - * @brief Custom PUS service to set health of all objects - * implementing hasHealthIF. - * - * 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. - * This service is very closely tied to the Commanding Service Base template - * class. There is constant interaction between this Service Base und a - * child class like this service - * - */ -class CService201HealthCommanding: public CommandingServiceBase { -public: - static const uint8_t NUMBER_OF_PARALLEL_COMMANDS = 4; - static const uint16_t COMMAND_TIMEOUT_SECONDS = 60; - - CService201HealthCommanding(object_id_t objectId, uint16_t apid, - uint8_t serviceId); - virtual~ CService201HealthCommanding(); -protected: - /* CSB abstract function implementations */ - 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; - /** Prepare health command */ - ReturnValue_t prepareCommand(CommandMessage* message, - uint8_t subservice, const uint8_t *tcData, size_t tcDataLen, - uint32_t *state, object_id_t objectId) override; - /** Handle health reply */ - 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, size_t tcDataLen); - ReturnValue_t checkInterfaceAndAcquireMessageQueue( - MessageQueueId_t* MessageQueueToSet, object_id_t* objectId); - - void prepareHealthSetReply(const CommandMessage *reply); - - enum Subservice { - COMMAND_SET_HEALTH = 1, //!< [EXPORT] : [TC] Set health of target object - REPLY_HEALTH_SET = 2 //!< [EXPORT] : [TM] Reply to health set command which also provides old health - }; -}; - -#endif /* FSFW_PUS_CSERVICE201HEALTHCOMMANDING_H_ */ +#ifndef FSFW_PUS_CSERVICE201HEALTHCOMMANDING_H_ +#define FSFW_PUS_CSERVICE201HEALTHCOMMANDING_H_ + +#include "../tmtcservices/CommandingServiceBase.h" + +/** + * @brief Custom PUS service to set health of all objects + * implementing hasHealthIF. + * + * 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. + * This service is very closely tied to the Commanding Service Base template + * class. There is constant interaction between this Service Base und a + * child class like this service + * + */ +class CService201HealthCommanding: public CommandingServiceBase { +public: + static const uint8_t NUMBER_OF_PARALLEL_COMMANDS = 4; + static const uint16_t COMMAND_TIMEOUT_SECONDS = 60; + + CService201HealthCommanding(object_id_t objectId, uint16_t apid, + uint8_t serviceId); + virtual~ CService201HealthCommanding(); +protected: + /* CSB abstract function implementations */ + 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; + /** Prepare health command */ + ReturnValue_t prepareCommand(CommandMessage* message, + uint8_t subservice, const uint8_t *tcData, size_t tcDataLen, + uint32_t *state, object_id_t objectId) override; + /** Handle health reply */ + 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, size_t tcDataLen); + ReturnValue_t checkInterfaceAndAcquireMessageQueue( + MessageQueueId_t* MessageQueueToSet, object_id_t* objectId); + + void prepareHealthSetReply(const CommandMessage *reply); + + enum Subservice { + COMMAND_SET_HEALTH = 1, //!< [EXPORT] : [TC] Set health of target object + REPLY_HEALTH_SET = 2 //!< [EXPORT] : [TM] Reply to health set command which also provides old health + }; +}; + +#endif /* FSFW_PUS_CSERVICE201HEALTHCOMMANDING_H_ */ diff --git a/pus/Service1TelecommandVerification.cpp b/pus/Service1TelecommandVerification.cpp index fb66c160..9b17eba5 100644 --- a/pus/Service1TelecommandVerification.cpp +++ b/pus/Service1TelecommandVerification.cpp @@ -1,100 +1,100 @@ -#include "../pus/Service1TelecommandVerification.h" -#include "../pus/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::sendVerificationReport: " - "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(); -} +#include "../pus/Service1TelecommandVerification.h" +#include "../pus/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::sendVerificationReport: " + "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/pus/Service1TelecommandVerification.h b/pus/Service1TelecommandVerification.h index 91275cfb..37562d1c 100644 --- a/pus/Service1TelecommandVerification.h +++ b/pus/Service1TelecommandVerification.h @@ -1,94 +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_ */ +#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/pus/Service2DeviceAccess.cpp b/pus/Service2DeviceAccess.cpp index 26639de6..9529c675 100644 --- a/pus/Service2DeviceAccess.cpp +++ b/pus/Service2DeviceAccess.cpp @@ -1,165 +1,165 @@ -#include "../pus/Service2DeviceAccess.h" -#include "../pus/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(message, tcData, tcDataLen); - } - break; - case Subservice::TOGGLE_WIRETAPPING: { - return prepareWiretappingCommand(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(); -} - +#include "../pus/Service2DeviceAccess.h" +#include "../pus/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(message, tcData, tcDataLen); + } + break; + case Subservice::TOGGLE_WIRETAPPING: { + return prepareWiretappingCommand(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/pus/Service2DeviceAccess.h b/pus/Service2DeviceAccess.h index 3fb9e830..f6aa8b52 100644 --- a/pus/Service2DeviceAccess.h +++ b/pus/Service2DeviceAccess.h @@ -1,92 +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_ */ +#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/pus/Service5EventReporting.cpp b/pus/Service5EventReporting.cpp index 5343b318..02031df4 100644 --- a/pus/Service5EventReporting.cpp +++ b/pus/Service5EventReporting.cpp @@ -1,91 +1,91 @@ -#include "../pus/Service5EventReporting.h" -#include "../pus/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(); -} +#include "../pus/Service5EventReporting.h" +#include "../pus/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/pus/Service5EventReporting.h b/pus/Service5EventReporting.h index 3f0c8cdf..0b6ee9a8 100644 --- a/pus/Service5EventReporting.h +++ b/pus/Service5EventReporting.h @@ -1,86 +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_ */ +#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/pus/Service8FunctionManagement.cpp b/pus/Service8FunctionManagement.cpp index 7174566a..3c94071d 100644 --- a/pus/Service8FunctionManagement.cpp +++ b/pus/Service8FunctionManagement.cpp @@ -1,142 +1,142 @@ -#include "../pus/Service8FunctionManagement.h" -#include "../pus/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; -} +#include "../pus/Service8FunctionManagement.h" +#include "../pus/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/pus/Service8FunctionManagement.h b/pus/Service8FunctionManagement.h index 16523533..b5ebcda8 100644 --- a/pus/Service8FunctionManagement.h +++ b/pus/Service8FunctionManagement.h @@ -1,67 +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_ */ +#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/pus/servicepackets/Service1Packets.h b/pus/servicepackets/Service1Packets.h index f4acbb61..8c5cb005 100644 --- a/pus/servicepackets/Service1Packets.h +++ b/pus/servicepackets/Service1Packets.h @@ -1,166 +1,166 @@ -#ifndef MISSION_PUS_SERVICEPACKETS_SERVICE1PACKETS_H_ -#define MISSION_PUS_SERVICEPACKETS_SERVICE1PACKETS_H_ - -#include "../../serialize/SerializeAdapter.h" -#include "../../tmtcservices/VerificationCodes.h" - -/** - * @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). - */ - -/** - * @brief FailureReport class to serialize a failure report - * @brief Subservice 1, 3, 5, 7 - * @ingroup spacepackets - */ -class FailureReport: public SerializeIF { //!< [EXPORT] : [SUBSERVICE] 2, 4, 6, 8 -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] 1, 3, 5, 7 -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_ */ +#ifndef MISSION_PUS_SERVICEPACKETS_SERVICE1PACKETS_H_ +#define MISSION_PUS_SERVICEPACKETS_SERVICE1PACKETS_H_ + +#include "../../serialize/SerializeAdapter.h" +#include "../../tmtcservices/VerificationCodes.h" + +/** + * @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). + */ + +/** + * @brief FailureReport class to serialize a failure report + * @brief Subservice 1, 3, 5, 7 + * @ingroup spacepackets + */ +class FailureReport: public SerializeIF { //!< [EXPORT] : [SUBSERVICE] 2, 4, 6, 8 +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] 1, 3, 5, 7 +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/pus/servicepackets/Service200Packets.h b/pus/servicepackets/Service200Packets.h index 6a700b50..efcf65fc 100644 --- a/pus/servicepackets/Service200Packets.h +++ b/pus/servicepackets/Service200Packets.h @@ -1,63 +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_ */ +#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/pus/servicepackets/Service201Packets.h b/pus/servicepackets/Service201Packets.h index 5ff5dcd5..8dce88e5 100644 --- a/pus/servicepackets/Service201Packets.h +++ b/pus/servicepackets/Service201Packets.h @@ -1,48 +1,48 @@ -#ifndef FSFW_PUS_SERVICEPACKETS_SERVICE201PACKETS_H_ -#define FSFW_PUS_SERVICEPACKETS_SERVICE201PACKETS_H_ - -#include "../../serialize/SerialLinkedListAdapter.h" -#include "../../serialize/SerializeIF.h" -#include "../../health/HasHealthIF.h" - -class HealthCommand: public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 1 -public: - - HealthCommand() { - setLinks(); - } - - HasHealthIF::HealthState getHealth() { - return static_cast(health.entry); - } -private: - void setLinks() { - setStart(&objectId); - objectId.setNext(&health); - } - SerializeElement objectId; //!< [EXPORT] : [COMMENT] Target object Id - SerializeElement health; //!< [EXPORT] : [COMMENT] Health to set -}; - - -class HealthSetReply: public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 2 -public: - HealthSetReply(uint8_t health_, uint8_t oldHealth_): - health(health_), oldHealth(oldHealth_) - { - setLinks(); - } - -private: - HealthSetReply(const HealthSetReply &reply); - void setLinks() { - setStart(&objectId); - objectId.setNext(&health); - health.setNext(&oldHealth); - } - SerializeElement objectId; //!< [EXPORT] : [COMMENT] Source object ID - SerializeElement health; //!< [EXPORT] : [COMMENT] New Health - SerializeElement oldHealth; //!< [EXPORT] : [COMMENT] Old Health -}; - -#endif /* FSFW_PUS_SERVICEPACKETS_SERVICE201PACKETS_H_ */ +#ifndef FSFW_PUS_SERVICEPACKETS_SERVICE201PACKETS_H_ +#define FSFW_PUS_SERVICEPACKETS_SERVICE201PACKETS_H_ + +#include "../../serialize/SerialLinkedListAdapter.h" +#include "../../serialize/SerializeIF.h" +#include "../../health/HasHealthIF.h" + +class HealthCommand: public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 1 +public: + + HealthCommand() { + setLinks(); + } + + HasHealthIF::HealthState getHealth() { + return static_cast(health.entry); + } +private: + void setLinks() { + setStart(&objectId); + objectId.setNext(&health); + } + SerializeElement objectId; //!< [EXPORT] : [COMMENT] Target object Id + SerializeElement health; //!< [EXPORT] : [COMMENT] Health to set +}; + + +class HealthSetReply: public SerialLinkedListAdapter { //!< [EXPORT] : [SUBSERVICE] 2 +public: + HealthSetReply(uint8_t health_, uint8_t oldHealth_): + health(health_), oldHealth(oldHealth_) + { + setLinks(); + } + +private: + HealthSetReply(const HealthSetReply &reply); + void setLinks() { + setStart(&objectId); + objectId.setNext(&health); + health.setNext(&oldHealth); + } + SerializeElement objectId; //!< [EXPORT] : [COMMENT] Source object ID + SerializeElement health; //!< [EXPORT] : [COMMENT] New Health + SerializeElement oldHealth; //!< [EXPORT] : [COMMENT] Old Health +}; + +#endif /* FSFW_PUS_SERVICEPACKETS_SERVICE201PACKETS_H_ */ diff --git a/pus/servicepackets/Service2Packets.h b/pus/servicepackets/Service2Packets.h index a0b1305d..d4f3fb17 100644 --- a/pus/servicepackets/Service2Packets.h +++ b/pus/servicepackets/Service2Packets.h @@ -1,76 +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_ */ +#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/pus/servicepackets/Service5Packets.h b/pus/servicepackets/Service5Packets.h index 0b52be3c..9655608a 100644 --- a/pus/servicepackets/Service5Packets.h +++ b/pus/servicepackets/Service5Packets.h @@ -1,76 +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_ */ +#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/pus/servicepackets/Service8Packets.h b/pus/servicepackets/Service8Packets.h index d16309ec..14f8b6e6 100644 --- a/pus/servicepackets/Service8Packets.h +++ b/pus/servicepackets/Service8Packets.h @@ -1,121 +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_ */ +#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/returnvalues/HasReturnvaluesIF.h b/returnvalues/HasReturnvaluesIF.h index 893ad1db..9000b23c 100644 --- a/returnvalues/HasReturnvaluesIF.h +++ b/returnvalues/HasReturnvaluesIF.h @@ -1,31 +1,31 @@ -#ifndef FRAMEWORK_RETURNVALUES_HASRETURNVALUESIF_H_ -#define FRAMEWORK_RETURNVALUES_HASRETURNVALUESIF_H_ - -#include "../returnvalues/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 (interfaceId << 8) + number; - } -}; - -#endif /* FRAMEWORK_RETURNVALUES_HASRETURNVALUESIF_H_ */ +#ifndef FRAMEWORK_RETURNVALUES_HASRETURNVALUESIF_H_ +#define FRAMEWORK_RETURNVALUES_HASRETURNVALUESIF_H_ + +#include "../returnvalues/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 (interfaceId << 8) + number; + } +}; + +#endif /* FRAMEWORK_RETURNVALUES_HASRETURNVALUESIF_H_ */ diff --git a/rmap/RMAP.cpp b/rmap/RMAP.cpp index 8bc91bb5..d0872c8e 100644 --- a/rmap/RMAP.cpp +++ b/rmap/RMAP.cpp @@ -1,89 +1,89 @@ -#include "../devicehandlers/DeviceCommunicationIF.h" -#include "../rmap/rmapStructs.h" -#include "../rmap/RMAP.h" -#include "../rmap/RMAPChannelIF.h" -#include - -ReturnValue_t RMAP::reset(RMAPCookie* cookie) { - return cookie->getChannel()->reset(); -} - -RMAP::RMAP(){ - -} - -ReturnValue_t RMAP::sendWriteCommand(RMAPCookie *cookie, const uint8_t* buffer, - size_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, - size_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); -} +#include "../devicehandlers/DeviceCommunicationIF.h" +#include "../rmap/rmapStructs.h" +#include "../rmap/RMAP.h" +#include "../rmap/RMAPChannelIF.h" +#include + +ReturnValue_t RMAP::reset(RMAPCookie* cookie) { + return cookie->getChannel()->reset(); +} + +RMAP::RMAP(){ + +} + +ReturnValue_t RMAP::sendWriteCommand(RMAPCookie *cookie, const uint8_t* buffer, + size_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, + size_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/rmap/RMAP.h b/rmap/RMAP.h index 9ab6b87d..e083d407 100644 --- a/rmap/RMAP.h +++ b/rmap/RMAP.h @@ -1,227 +1,227 @@ -#ifndef RMAPpp_H_ -#define RMAPpp_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../rmap/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, const uint8_t* buffer, - size_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, - size_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_ */ +#ifndef RMAPpp_H_ +#define RMAPpp_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../rmap/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, const uint8_t* buffer, + size_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, + size_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/rmap/RMAPChannelIF.h b/rmap/RMAPChannelIF.h index 037d6d27..d851885b 100644 --- a/rmap/RMAPChannelIF.h +++ b/rmap/RMAPChannelIF.h @@ -1,116 +1,116 @@ -#ifndef RMAPCHANNELIF_H_ -#define RMAPCHANNELIF_H_ - -#include "../rmap/RMAPCookie.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#include - -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, - const uint8_t *data, size_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, - size_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_ */ +#ifndef RMAPCHANNELIF_H_ +#define RMAPCHANNELIF_H_ + +#include "../rmap/RMAPCookie.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +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, + const uint8_t *data, size_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, + size_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/rmap/RMAPCookie.cpp b/rmap/RMAPCookie.cpp index 74101ffa..c92ee433 100644 --- a/rmap/RMAPCookie.cpp +++ b/rmap/RMAPCookie.cpp @@ -1,124 +1,124 @@ -#include "../rmap/RMAPChannelIF.h" -#include "../rmap/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() { - -} - -size_t RMAPCookie::getMaxReplyLen() const { - return maxReplyLen; -} - -void RMAPCookie::setMaxReplyLen(size_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; -} +#include "../rmap/RMAPChannelIF.h" +#include "../rmap/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() { + +} + +size_t RMAPCookie::getMaxReplyLen() const { + return maxReplyLen; +} + +void RMAPCookie::setMaxReplyLen(size_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/rmap/RMAPCookie.h b/rmap/RMAPCookie.h index 0da03e30..8832f857 100644 --- a/rmap/RMAPCookie.h +++ b/rmap/RMAPCookie.h @@ -1,59 +1,59 @@ -#ifndef RMAPCOOKIE_H_ -#define RMAPCOOKIE_H_ - -#include "../devicehandlers/CookieIF.h" -#include "../rmap/rmapStructs.h" -#include - -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(); - - size_t getMaxReplyLen() const; - void setMaxReplyLen(size_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_ */ +#ifndef RMAPCOOKIE_H_ +#define RMAPCOOKIE_H_ + +#include "../devicehandlers/CookieIF.h" +#include "../rmap/rmapStructs.h" +#include + +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(); + + size_t getMaxReplyLen() const; + void setMaxReplyLen(size_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/rmap/RmapDeviceCommunicationIF.cpp b/rmap/RmapDeviceCommunicationIF.cpp index 36247ce6..62a3f3ef 100644 --- a/rmap/RmapDeviceCommunicationIF.cpp +++ b/rmap/RmapDeviceCommunicationIF.cpp @@ -1,47 +1,47 @@ -#include "../rmap/RmapDeviceCommunicationIF.h" -#include "../rmap/RMAP.h" - -//TODO Cast here are all potential bugs -RmapDeviceCommunicationIF::~RmapDeviceCommunicationIF() { -} - -ReturnValue_t RmapDeviceCommunicationIF::sendMessage(CookieIF *cookie, - const uint8_t * sendData, size_t sendLen) { - return RMAP::sendWriteCommand((RMAPCookie *) cookie, sendData, sendLen); -} - -ReturnValue_t RmapDeviceCommunicationIF::getSendSuccess(CookieIF* cookie) { - return RMAP::getWriteReply((RMAPCookie *) cookie); -} - -ReturnValue_t RmapDeviceCommunicationIF::requestReceiveMessage( - CookieIF *cookie, size_t requestLen) { - return RMAP::sendReadCommand((RMAPCookie *) cookie, - ((RMAPCookie *) cookie)->getMaxReplyLen()); -} - -ReturnValue_t RmapDeviceCommunicationIF::readReceivedMessage(CookieIF* cookie, - uint8_t** buffer, size_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; -} +#include "../rmap/RmapDeviceCommunicationIF.h" +#include "../rmap/RMAP.h" + +//TODO Cast here are all potential bugs +RmapDeviceCommunicationIF::~RmapDeviceCommunicationIF() { +} + +ReturnValue_t RmapDeviceCommunicationIF::sendMessage(CookieIF *cookie, + const uint8_t * sendData, size_t sendLen) { + return RMAP::sendWriteCommand((RMAPCookie *) cookie, sendData, sendLen); +} + +ReturnValue_t RmapDeviceCommunicationIF::getSendSuccess(CookieIF* cookie) { + return RMAP::getWriteReply((RMAPCookie *) cookie); +} + +ReturnValue_t RmapDeviceCommunicationIF::requestReceiveMessage( + CookieIF *cookie, size_t requestLen) { + return RMAP::sendReadCommand((RMAPCookie *) cookie, + ((RMAPCookie *) cookie)->getMaxReplyLen()); +} + +ReturnValue_t RmapDeviceCommunicationIF::readReceivedMessage(CookieIF* cookie, + uint8_t** buffer, size_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/rmap/RmapDeviceCommunicationIF.h b/rmap/RmapDeviceCommunicationIF.h index b0564193..52efa3de 100644 --- a/rmap/RmapDeviceCommunicationIF.h +++ b/rmap/RmapDeviceCommunicationIF.h @@ -1,89 +1,89 @@ -#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(); - - /** - * @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); - - /** - * 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); - - /** - * Called by DHB in the SEND_WRITE doSendRead(). - * It is assumed that it is always possible to request a reply - * from a device. - * - * @param cookie - * @return -@c RETURN_OK to confirm the request for data has been sent. - * -@c NO_READ_REQUEST if no request shall be made. readReceivedMessage() - * will not be called in the respective communication cycle. - * - Everything else triggers failure event with returnvalue as parameter 1 - */ - virtual ReturnValue_t requestReceiveMessage(CookieIF *cookie, size_t requestLen); - - /** - * 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 data - * @param len - * @return @c RETURN_OK for successfull receive - * - Everything else triggers failure event with returnvalue as parameter 1 - */ - virtual ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, - size_t *size); - - ReturnValue_t setAddress(CookieIF* cookie, - uint32_t address); - uint32_t getAddress(CookieIF* cookie); - ReturnValue_t setParameter(CookieIF* cookie, - uint32_t parameter); - uint32_t getParameter(CookieIF* cookie); -}; - -#endif /* MISSION_RMAP_RMAPDEVICECOMMUNICATIONINTERFACE_H_ */ +#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(); + + /** + * @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); + + /** + * 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); + + /** + * Called by DHB in the SEND_WRITE doSendRead(). + * It is assumed that it is always possible to request a reply + * from a device. + * + * @param cookie + * @return -@c RETURN_OK to confirm the request for data has been sent. + * -@c NO_READ_REQUEST if no request shall be made. readReceivedMessage() + * will not be called in the respective communication cycle. + * - Everything else triggers failure event with returnvalue as parameter 1 + */ + virtual ReturnValue_t requestReceiveMessage(CookieIF *cookie, size_t requestLen); + + /** + * 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 data + * @param len + * @return @c RETURN_OK for successfull receive + * - Everything else triggers failure event with returnvalue as parameter 1 + */ + virtual ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, + size_t *size); + + ReturnValue_t setAddress(CookieIF* cookie, + uint32_t address); + uint32_t getAddress(CookieIF* cookie); + ReturnValue_t setParameter(CookieIF* cookie, + uint32_t parameter); + uint32_t getParameter(CookieIF* cookie); +}; + +#endif /* MISSION_RMAP_RMAPDEVICECOMMUNICATIONINTERFACE_H_ */ diff --git a/serialize/EndianConverter.h b/serialize/EndianConverter.h index fae10bca..f981751d 100644 --- a/serialize/EndianConverter.h +++ b/serialize/EndianConverter.h @@ -1,124 +1,124 @@ -#ifndef ENDIANSWAPPER_H_ -#define ENDIANSWAPPER_H_ - -#include "../osal/Endiness.h" -#include -#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 /* ENDIANSWAPPER_H_ */ +#ifndef ENDIANSWAPPER_H_ +#define ENDIANSWAPPER_H_ + +#include "../osal/Endiness.h" +#include +#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 /* ENDIANSWAPPER_H_ */ diff --git a/serialize/SerialArrayListAdapter.h b/serialize/SerialArrayListAdapter.h index 19190abd..69afdd4a 100644 --- a/serialize/SerialArrayListAdapter.h +++ b/serialize/SerialArrayListAdapter.h @@ -1,91 +1,91 @@ -/** - * @file SerialArrayListAdapter.h - * @brief This file defines the SerialArrayListAdapter class. - * @date 22.07.2014 - * @author baetz - */ -#ifndef SERIALARRAYLISTADAPTER_H_ -#define SERIALARRAYLISTADAPTER_H_ - -#include "../container/ArrayList.h" -#include "../serialize/SerializeIF.h" -#include - -/** - * Also serializes length field ! - * @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 (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; - } - - - static void swapArrayListEndianness(ArrayList* list) { - list->swapArrayListEndianness(); - } -private: - ArrayList *adaptee; -}; - - - -#endif /* SERIALARRAYLISTADAPTER_H_ */ +/** + * @file SerialArrayListAdapter.h + * @brief This file defines the SerialArrayListAdapter class. + * @date 22.07.2014 + * @author baetz + */ +#ifndef SERIALARRAYLISTADAPTER_H_ +#define SERIALARRAYLISTADAPTER_H_ + +#include "../container/ArrayList.h" +#include "../serialize/SerializeIF.h" +#include + +/** + * Also serializes length field ! + * @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 (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; + } + + + static void swapArrayListEndianness(ArrayList* list) { + list->swapArrayListEndianness(); + } +private: + ArrayList *adaptee; +}; + + + +#endif /* SERIALARRAYLISTADAPTER_H_ */ diff --git a/serialize/SerialBufferAdapter.cpp b/serialize/SerialBufferAdapter.cpp index 812cd34b..1c11afd4 100644 --- a/serialize/SerialBufferAdapter.cpp +++ b/serialize/SerialBufferAdapter.cpp @@ -1,129 +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; - +#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/serialize/SerialBufferAdapter.h b/serialize/SerialBufferAdapter.h index c3dfcd8f..9a89e18b 100644 --- a/serialize/SerialBufferAdapter.h +++ b/serialize/SerialBufferAdapter.h @@ -1,78 +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_ */ +#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/serialize/SerialFixedArrayListAdapter.h b/serialize/SerialFixedArrayListAdapter.h index 7968b420..c866df86 100644 --- a/serialize/SerialFixedArrayListAdapter.h +++ b/serialize/SerialFixedArrayListAdapter.h @@ -1,63 +1,63 @@ -#ifndef SERIALFIXEDARRAYLISTADAPTER_H_ -#define SERIALFIXEDARRAYLISTADAPTER_H_ - -#include "../container/FixedArrayList.h" -#include "../serialize/SerialArrayListAdapter.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. - * - * - Buffers with a size header inside that class can be declared with - * @code - * SerialFixedArrayListAdapter mySerialFixedArrayList(...). - * @endcode - * - * - MAX_SIZE: specifies the maximum allowed number of elements - * in FixedArrayList. - * - BUFFER_TYPE: specifies the data type of the buffer - * - 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 /* SERIALFIXEDARRAYLISTADAPTER_H_ */ +#ifndef SERIALFIXEDARRAYLISTADAPTER_H_ +#define SERIALFIXEDARRAYLISTADAPTER_H_ + +#include "../container/FixedArrayList.h" +#include "../serialize/SerialArrayListAdapter.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. + * + * - Buffers with a size header inside that class can be declared with + * @code + * SerialFixedArrayListAdapter mySerialFixedArrayList(...). + * @endcode + * + * - MAX_SIZE: specifies the maximum allowed number of elements + * in FixedArrayList. + * - BUFFER_TYPE: specifies the data type of the buffer + * - 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 /* SERIALFIXEDARRAYLISTADAPTER_H_ */ diff --git a/serialize/SerialLinkedListAdapter.h b/serialize/SerialLinkedListAdapter.h index a4652028..987fa70e 100644 --- a/serialize/SerialLinkedListAdapter.h +++ b/serialize/SerialLinkedListAdapter.h @@ -1,133 +1,133 @@ -/** - * @file SerialLinkedListAdapter.h - * @brief This file defines the SerialLinkedListAdapter class. - * @date 22.07.2014 - * @author baetz - */ -#ifndef SERIALLINKEDLISTADAPTER_H_ -#define SERIALLINKEDLISTADAPTER_H_ - -#include "../container/SinglyLinkedList.h" -#include "../serialize/SerializeAdapter.h" -#include "../serialize/SerializeElement.h" -#include "../serialize/SerializeIF.h" -//This is where we need the SerializeAdapter! - - /** - * @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 with a known size - * - 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. - * - * @ingroup serialize - */ -template -class SerialLinkedListAdapter: public SinglyLinkedList, public SerializeIF { -public: - /** - * 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; - - 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) && (element != NULL)) { - 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 != NULL) { - 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) && (element != NULL)) { - result = element->value->deSerialize(buffer, size, streamEndianness); - element = element->getNext(); - } - return result; - } - - bool printCount; -}; - -#endif /* SERIALLINKEDLISTADAPTER_H_ */ +/** + * @file SerialLinkedListAdapter.h + * @brief This file defines the SerialLinkedListAdapter class. + * @date 22.07.2014 + * @author baetz + */ +#ifndef SERIALLINKEDLISTADAPTER_H_ +#define SERIALLINKEDLISTADAPTER_H_ + +#include "../container/SinglyLinkedList.h" +#include "../serialize/SerializeAdapter.h" +#include "../serialize/SerializeElement.h" +#include "../serialize/SerializeIF.h" +//This is where we need the SerializeAdapter! + + /** + * @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 with a known size + * - 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. + * + * @ingroup serialize + */ +template +class SerialLinkedListAdapter: public SinglyLinkedList, public SerializeIF { +public: + /** + * 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; + + 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) && (element != NULL)) { + 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 != NULL) { + 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) && (element != NULL)) { + result = element->value->deSerialize(buffer, size, streamEndianness); + element = element->getNext(); + } + return result; + } + + bool printCount; +}; + +#endif /* SERIALLINKEDLISTADAPTER_H_ */ diff --git a/serialize/SerializeAdapter.h b/serialize/SerializeAdapter.h index bcddb71a..3e84bdf9 100644 --- a/serialize/SerializeAdapter.h +++ b/serialize/SerializeAdapter.h @@ -1,175 +1,175 @@ -#ifndef SERIALIZEADAPTER_H_ -#define SERIALIZEADAPTER_H_ - -#include "../container/IsDerivedFrom.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../serialize/EndianConverter.h" -#include "../serialize/SerializeIF.h" -#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 - * - * A report class is converted into a TM buffer. The report class implements a - * serialize functions and calls the AutoSerializeAdapter::serialize function - * repeatedly on all object data fields. The getSerializedSize function is - * implemented by calling the AutoSerializeAdapter::getSerializedSize function - * repeatedly on all data fields. - * - * The AutoSerializeAdapter functions can also be used as an alternative to - * memcpy to retrieve data out of a buffer directly into a class variable - * with data type T while being able to specify endianness. The boolean - * bigEndian specifies whether an endian swap is performed on the data before - * serialization or deserialization. - * - * There are three ways to retrieve data out of a buffer to be used in the FSFW - * to use regular aligned (big endian) data. Examples: - * - * 1. Use the AutoSerializeAdapter::deSerialize function - * The pointer *buffer will be incremented automatically by the typeSize - * of the object, so this function can be called on &buffer repeatedly - * without adjusting pointer position. Set bigEndian parameter to true - * to perform endian swapping, if necessary - * @code - * uint16_t data; - * int32_t dataLen = sizeof(data); - * ReturnValue_t result = - * AutoSerializeAdapter::deSerialize(&data,&buffer,&dataLen,true); - * @endcode - * - * 2. Perform a bitshift operation. Watch for for endianness: - * @code - * uint16_t data; - * data = buffer[targetByte1] << 8 | buffer[targetByte2]; - * data = EndianSwapper::swap(data); //optional, or swap order above - * @endcode - * - * 3. memcpy or std::copy can also be used, but watch out if system - * endianness is different from required data endianness. - * Perform endian-swapping if necessary. - * @code - * uint16_t data; - * memcpy(&data,buffer + positionOfTargetByte1,sizeof(data)); - * data = EndianSwapper::swap(data); //optional - * @endcode - * - * When serializing for downlink, the packets are generally serialized assuming - * big endian data format like seen in TmPacketStored.cpp for example. - * - * @ingroup serialize - */ - -class SerializeAdapter { -public: - template - static ReturnValue_t serialize(const T *object, uint8_t **buffer, - size_t *size, size_t maxSize, SerializeIF::Endianness streamEndianness) { - InternalSerializeAdapter::Is> adapter; - return adapter.serialize(object, buffer, size, maxSize, - streamEndianness); - } - template - static uint32_t getSerializedSize(const T *object) { - InternalSerializeAdapter::Is> adapter; - return adapter.getSerializedSize(object); - } - template - static ReturnValue_t deSerialize(T *object, const uint8_t **buffer, - size_t *size, SerializeIF::Endianness streamEndianness) { - InternalSerializeAdapter::Is> adapter; - return adapter.deSerialize(object, buffer, size, streamEndianness); - } -private: - template - class InternalSerializeAdapter { - 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 == NULL) { - size = &ignoredSize; - } - //TODO check integer overflow of *size - if (sizeof(T) + *size <= max_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; - } - 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); - 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 - 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 == NULL) { - size = &ignoredSize; - } - return object->serialize(buffer, size, max_size, streamEndianness); - } - uint32_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 /* SERIALIZEADAPTER_H_ */ +#ifndef SERIALIZEADAPTER_H_ +#define SERIALIZEADAPTER_H_ + +#include "../container/IsDerivedFrom.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../serialize/EndianConverter.h" +#include "../serialize/SerializeIF.h" +#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 + * + * A report class is converted into a TM buffer. The report class implements a + * serialize functions and calls the AutoSerializeAdapter::serialize function + * repeatedly on all object data fields. The getSerializedSize function is + * implemented by calling the AutoSerializeAdapter::getSerializedSize function + * repeatedly on all data fields. + * + * The AutoSerializeAdapter functions can also be used as an alternative to + * memcpy to retrieve data out of a buffer directly into a class variable + * with data type T while being able to specify endianness. The boolean + * bigEndian specifies whether an endian swap is performed on the data before + * serialization or deserialization. + * + * There are three ways to retrieve data out of a buffer to be used in the FSFW + * to use regular aligned (big endian) data. Examples: + * + * 1. Use the AutoSerializeAdapter::deSerialize function + * The pointer *buffer will be incremented automatically by the typeSize + * of the object, so this function can be called on &buffer repeatedly + * without adjusting pointer position. Set bigEndian parameter to true + * to perform endian swapping, if necessary + * @code + * uint16_t data; + * int32_t dataLen = sizeof(data); + * ReturnValue_t result = + * AutoSerializeAdapter::deSerialize(&data,&buffer,&dataLen,true); + * @endcode + * + * 2. Perform a bitshift operation. Watch for for endianness: + * @code + * uint16_t data; + * data = buffer[targetByte1] << 8 | buffer[targetByte2]; + * data = EndianSwapper::swap(data); //optional, or swap order above + * @endcode + * + * 3. memcpy or std::copy can also be used, but watch out if system + * endianness is different from required data endianness. + * Perform endian-swapping if necessary. + * @code + * uint16_t data; + * memcpy(&data,buffer + positionOfTargetByte1,sizeof(data)); + * data = EndianSwapper::swap(data); //optional + * @endcode + * + * When serializing for downlink, the packets are generally serialized assuming + * big endian data format like seen in TmPacketStored.cpp for example. + * + * @ingroup serialize + */ + +class SerializeAdapter { +public: + template + static ReturnValue_t serialize(const T *object, uint8_t **buffer, + size_t *size, size_t maxSize, SerializeIF::Endianness streamEndianness) { + InternalSerializeAdapter::Is> adapter; + return adapter.serialize(object, buffer, size, maxSize, + streamEndianness); + } + template + static uint32_t getSerializedSize(const T *object) { + InternalSerializeAdapter::Is> adapter; + return adapter.getSerializedSize(object); + } + template + static ReturnValue_t deSerialize(T *object, const uint8_t **buffer, + size_t *size, SerializeIF::Endianness streamEndianness) { + InternalSerializeAdapter::Is> adapter; + return adapter.deSerialize(object, buffer, size, streamEndianness); + } +private: + template + class InternalSerializeAdapter { + 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 == NULL) { + size = &ignoredSize; + } + //TODO check integer overflow of *size + if (sizeof(T) + *size <= max_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; + } + 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); + 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 + 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 == NULL) { + size = &ignoredSize; + } + return object->serialize(buffer, size, max_size, streamEndianness); + } + uint32_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 /* SERIALIZEADAPTER_H_ */ diff --git a/serialize/SerializeElement.h b/serialize/SerializeElement.h index e33192ca..8c3472b2 100644 --- a/serialize/SerializeElement.h +++ b/serialize/SerializeElement.h @@ -1,60 +1,60 @@ -#ifndef SERIALIZEELEMENT_H_ -#define SERIALIZEELEMENT_H_ - -#include "../container/SinglyLinkedList.h" -#include "../serialize/SerializeAdapter.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(). - * Serilization 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) { - } - T entry; - 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; - } -}; - -#endif /* SERIALIZEELEMENT_H_ */ +#ifndef SERIALIZEELEMENT_H_ +#define SERIALIZEELEMENT_H_ + +#include "../container/SinglyLinkedList.h" +#include "../serialize/SerializeAdapter.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(). + * Serilization 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) { + } + T entry; + 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; + } +}; + +#endif /* SERIALIZEELEMENT_H_ */ diff --git a/serialize/SerializeIF.h b/serialize/SerializeIF.h index 8dc4894a..6d5a0dda 100644 --- a/serialize/SerializeIF.h +++ b/serialize/SerializeIF.h @@ -1,58 +1,58 @@ -#ifndef SERIALIZEIF_H_ -#define SERIALIZEIF_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include - -/** - * @defgroup serialize Serialization - * Contains serialisation services. - */ - -/** - * @brief An interface for alle classes which require - * translation of objects data into data streams and vice-versa. - * @details - * If the target architecture is little endian (e.g. ARM), any data types - * created might have the wrong endianess if they are to be used for the FSFW. - * Depending on the system architecture, endian correctness must be assured, - * This is important for incoming and outgoing data. The internal handling - * of data should be performed in the native system endianness. - * There are three ways to copy data (with different options to ensure - * endian correctness): - * - * 1. Use the @c AutoSerializeAdapter::deSerialize function (with - * the endian flag) - * 2. Perform a bitshift operation (with correct order) - * 3. @c memcpy (with @c EndianSwapper if necessary) - * - * When serializing for downlink, the packets are generally serialized - * assuming big endian data format like seen in TmPacketStored.cpp for example. - * - * @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); - static const ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(2); - static const ReturnValue_t TOO_MANY_ELEMENTS = MAKE_RETURN_CODE(3); - - virtual ~SerializeIF() { - } - - virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, - size_t maxSize, Endianness streamEndianness) const = 0; - - virtual size_t getSerializedSize() const = 0; - - virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, - Endianness streamEndianness) = 0; - -}; - -#endif /* SERIALIZEIF_H_ */ +#ifndef SERIALIZEIF_H_ +#define SERIALIZEIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include + +/** + * @defgroup serialize Serialization + * Contains serialisation services. + */ + +/** + * @brief An interface for alle classes which require + * translation of objects data into data streams and vice-versa. + * @details + * If the target architecture is little endian (e.g. ARM), any data types + * created might have the wrong endianess if they are to be used for the FSFW. + * Depending on the system architecture, endian correctness must be assured, + * This is important for incoming and outgoing data. The internal handling + * of data should be performed in the native system endianness. + * There are three ways to copy data (with different options to ensure + * endian correctness): + * + * 1. Use the @c AutoSerializeAdapter::deSerialize function (with + * the endian flag) + * 2. Perform a bitshift operation (with correct order) + * 3. @c memcpy (with @c EndianSwapper if necessary) + * + * When serializing for downlink, the packets are generally serialized + * assuming big endian data format like seen in TmPacketStored.cpp for example. + * + * @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); + static const ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(2); + static const ReturnValue_t TOO_MANY_ELEMENTS = MAKE_RETURN_CODE(3); + + virtual ~SerializeIF() { + } + + virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, + size_t maxSize, Endianness streamEndianness) const = 0; + + virtual size_t getSerializedSize() const = 0; + + virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness) = 0; + +}; + +#endif /* SERIALIZEIF_H_ */ diff --git a/serviceinterface/ServiceInterfaceBuffer.cpp b/serviceinterface/ServiceInterfaceBuffer.cpp index e7b2a43e..880f51e1 100644 --- a/serviceinterface/ServiceInterfaceBuffer.cpp +++ b/serviceinterface/ServiceInterfaceBuffer.cpp @@ -1,217 +1,217 @@ -#include "../timemanager/Clock.h" -#include "../serviceinterface/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 +#include "../timemanager/Clock.h" +#include "../serviceinterface/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/serviceinterface/ServiceInterfaceBuffer.h b/serviceinterface/ServiceInterfaceBuffer.h index a5c135b6..c5d5b258 100644 --- a/serviceinterface/ServiceInterfaceBuffer.h +++ b/serviceinterface/ServiceInterfaceBuffer.h @@ -1,145 +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_ */ +#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/serviceinterface/ServiceInterfaceStream.cpp b/serviceinterface/ServiceInterfaceStream.cpp index 7c6676dd..8a358999 100644 --- a/serviceinterface/ServiceInterfaceStream.cpp +++ b/serviceinterface/ServiceInterfaceStream.cpp @@ -1,32 +1,32 @@ -#include "../serviceinterface/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(); - } -} +#include "../serviceinterface/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/serviceinterface/ServiceInterfaceStream.h b/serviceinterface/ServiceInterfaceStream.h index b1786a38..051500f5 100644 --- a/serviceinterface/ServiceInterfaceStream.h +++ b/serviceinterface/ServiceInterfaceStream.h @@ -1,58 +1,58 @@ -#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ -#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ - -#include "../serviceinterface/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_ */ +#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ +#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ + +#include "../serviceinterface/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/storagemanager/ConstStorageAccessor.cpp b/storagemanager/ConstStorageAccessor.cpp index ea9f2e44..4ada7f80 100644 --- a/storagemanager/ConstStorageAccessor.cpp +++ b/storagemanager/ConstStorageAccessor.cpp @@ -1,87 +1,87 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../storagemanager/ConstStorageAccessor.h" -#include "../storagemanager/StorageManagerIF.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) { - sif::debug << "deleting store data" << std::endl; - 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; -} +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../storagemanager/ConstStorageAccessor.h" +#include "../storagemanager/StorageManagerIF.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) { + sif::debug << "deleting store data" << std::endl; + 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/storagemanager/ConstStorageAccessor.h b/storagemanager/ConstStorageAccessor.h index ec2da8ce..573892da 100644 --- a/storagemanager/ConstStorageAccessor.h +++ b/storagemanager/ConstStorageAccessor.h @@ -1,116 +1,116 @@ -#ifndef FRAMEWORK_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ -#define FRAMEWORK_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ - -#include "../storagemanager/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 /* FRAMEWORK_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ */ +#ifndef FRAMEWORK_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ +#define FRAMEWORK_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ + +#include "../storagemanager/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 /* FRAMEWORK_STORAGEMANAGER_CONSTSTORAGEACCESSOR_H_ */ diff --git a/storagemanager/LocalPool.h b/storagemanager/LocalPool.h index 5ead7981..2a526b6a 100644 --- a/storagemanager/LocalPool.h +++ b/storagemanager/LocalPool.h @@ -1,189 +1,189 @@ -#ifndef FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_ -#define FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_ - -#include "../objectmanager/SystemObject.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../storagemanager/StorageManagerIF.h" -#include "../objectmanager/ObjectManagerIF.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 "../storagemanager/LocalPool.tpp" - -#endif /* FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_ */ +#ifndef FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_ +#define FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_ + +#include "../objectmanager/SystemObject.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../objectmanager/ObjectManagerIF.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 "../storagemanager/LocalPool.tpp" + +#endif /* FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_ */ diff --git a/storagemanager/PoolManager.h b/storagemanager/PoolManager.h index 2dc3266a..76bddb8f 100644 --- a/storagemanager/PoolManager.h +++ b/storagemanager/PoolManager.h @@ -1,49 +1,49 @@ -#ifndef FRAMEWORK_STORAGEMANAGER_POOLMANAGER_H_ -#define FRAMEWORK_STORAGEMANAGER_POOLMANAGER_H_ - -#include "../storagemanager/LocalPool.h" -#include "../ipc/MutexHelper.h" -#include "../storagemanager/StorageAccessor.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; - -protected: - //! Default mutex timeout value to prevent permanent blocking. - static constexpr uint32_t mutexTimeout = 50; - - 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 /* POOLMANAGER_H_ */ +#ifndef FRAMEWORK_STORAGEMANAGER_POOLMANAGER_H_ +#define FRAMEWORK_STORAGEMANAGER_POOLMANAGER_H_ + +#include "../storagemanager/LocalPool.h" +#include "../ipc/MutexHelper.h" +#include "../storagemanager/StorageAccessor.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; + +protected: + //! Default mutex timeout value to prevent permanent blocking. + static constexpr uint32_t mutexTimeout = 50; + + 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 /* POOLMANAGER_H_ */ diff --git a/storagemanager/StorageAccessor.cpp b/storagemanager/StorageAccessor.cpp index d4d2a54d..60bd4130 100644 --- a/storagemanager/StorageAccessor.cpp +++ b/storagemanager/StorageAccessor.cpp @@ -1,67 +1,67 @@ -#include "../storagemanager/StorageAccessor.h" -#include "../storagemanager/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; -} - - +#include "../storagemanager/StorageAccessor.h" +#include "../storagemanager/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/storagemanager/StorageAccessor.h b/storagemanager/StorageAccessor.h index 1ca5f7dd..a65427b8 100644 --- a/storagemanager/StorageAccessor.h +++ b/storagemanager/StorageAccessor.h @@ -1,45 +1,45 @@ -#ifndef FRAMEWORK_STORAGEMANAGER_STORAGEACCESSOR_H_ -#define FRAMEWORK_STORAGEMANAGER_STORAGEACCESSOR_H_ - -#include "../storagemanager/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 /* TEST_PROTOTYPES_STORAGEACCESSOR_H_ */ +#ifndef FRAMEWORK_STORAGEMANAGER_STORAGEACCESSOR_H_ +#define FRAMEWORK_STORAGEMANAGER_STORAGEACCESSOR_H_ + +#include "../storagemanager/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 /* TEST_PROTOTYPES_STORAGEACCESSOR_H_ */ diff --git a/storagemanager/StorageManagerIF.h b/storagemanager/StorageManagerIF.h index 85341f2e..2229cdf8 100644 --- a/storagemanager/StorageManagerIF.h +++ b/storagemanager/StorageManagerIF.h @@ -1,167 +1,167 @@ -#ifndef STORAGEMANAGERIF_H_H -#define STORAGEMANAGERIF_H_H - -#include "../events/Event.h" -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../storagemanager/StorageAccessor.h" -#include "../storagemanager/storeAddress.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 /* STORAGEMANAGERIF_H_ */ +#ifndef STORAGEMANAGERIF_H_H +#define STORAGEMANAGERIF_H_H + +#include "../events/Event.h" +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../storagemanager/StorageAccessor.h" +#include "../storagemanager/storeAddress.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 /* STORAGEMANAGERIF_H_ */ diff --git a/subsystem/Subsystem.h b/subsystem/Subsystem.h index de4761fa..2b501574 100644 --- a/subsystem/Subsystem.h +++ b/subsystem/Subsystem.h @@ -1,164 +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 "../subsystem/modes/ModeDefinitions.h" -#include "../subsystem/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 = 0; - - HybridIterator currentTargetTable; - - Mode_t targetMode; - - Submode_t targetSubmode; - - Mode_t initialMode; - - HybridIterator currentSequenceIterator; - - FixedMap modeTables; - - FixedMap modeSequences; - - StorageManagerIF *IPCStore = nullptr; - -#ifdef USE_MODESTORE - ModeStoreIF *modeStore = nullptr; -#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_ */ +#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 "../subsystem/modes/ModeDefinitions.h" +#include "../subsystem/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 = 0; + + HybridIterator currentTargetTable; + + Mode_t targetMode; + + Submode_t targetSubmode; + + Mode_t initialMode; + + HybridIterator currentSequenceIterator; + + FixedMap modeTables; + + FixedMap modeSequences; + + StorageManagerIF *IPCStore = nullptr; + +#ifdef USE_MODESTORE + ModeStoreIF *modeStore = nullptr; +#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/subsystem/SubsystemBase.cpp b/subsystem/SubsystemBase.cpp index 2bd6c1ad..17599808 100644 --- a/subsystem/SubsystemBase.cpp +++ b/subsystem/SubsystemBase.cpp @@ -1,356 +1,356 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../subsystem/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, - MessageQueueMessage::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 == nullptr) { - if (healthChild == nullptr) { - 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 command; - - std::map::iterator iter; - - commandsOutstanding = 0; - - for (; tableIter.value != nullptr; ++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(&command, - ModeMessage::CMD_MODE_COMMAND, HasModesIF::MODE_OFF, - SUBMODE_NONE); - } else { - if (modeHelper.isForced()) { - ModeMessage::setModeMessage(&command, - ModeMessage::CMD_MODE_COMMAND_FORCED, - tableIter.value->getMode(), submodeToCommand); - } else { - if (healthHelper.healthTable->isCommandable(object)) { - ModeMessage::setModeMessage(&command, - ModeMessage::CMD_MODE_COMMAND, - tableIter.value->getMode(), submodeToCommand); - } else { - continue; - } - } - } - } else { - ModeMessage::setModeMessage(&command, ModeMessage::CMD_MODE_COMMAND, - tableIter.value->getMode(), submodeToCommand); - } - - if ((iter->second.mode == ModeMessage::getMode(&command)) - && (iter->second.submode == ModeMessage::getSubmode(&command)) - && !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, &command); - 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 command; - - for (result = commandQueue->receiveMessage(&command); result == RETURN_OK; - result = commandQueue->receiveMessage(&command)) { - - result = healthHelper.handleHealthCommand(&command); - if (result == RETURN_OK) { - continue; - } - - result = modeHelper.handleModeCommand(&command); - if (result == RETURN_OK) { - continue; - } - - result = handleModeReply(&command); - if (result == RETURN_OK) { - continue; - } - - result = handleCommandMessage(&command); - if (result != RETURN_OK) { - CommandMessage reply; - reply.setReplyRejected(CommandMessage::UNKNOWN_COMMAND, - command.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() { -} - +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../subsystem/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, + MessageQueueMessage::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 == nullptr) { + if (healthChild == nullptr) { + 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 command; + + std::map::iterator iter; + + commandsOutstanding = 0; + + for (; tableIter.value != nullptr; ++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(&command, + ModeMessage::CMD_MODE_COMMAND, HasModesIF::MODE_OFF, + SUBMODE_NONE); + } else { + if (modeHelper.isForced()) { + ModeMessage::setModeMessage(&command, + ModeMessage::CMD_MODE_COMMAND_FORCED, + tableIter.value->getMode(), submodeToCommand); + } else { + if (healthHelper.healthTable->isCommandable(object)) { + ModeMessage::setModeMessage(&command, + ModeMessage::CMD_MODE_COMMAND, + tableIter.value->getMode(), submodeToCommand); + } else { + continue; + } + } + } + } else { + ModeMessage::setModeMessage(&command, ModeMessage::CMD_MODE_COMMAND, + tableIter.value->getMode(), submodeToCommand); + } + + if ((iter->second.mode == ModeMessage::getMode(&command)) + && (iter->second.submode == ModeMessage::getSubmode(&command)) + && !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, &command); + 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 command; + + for (result = commandQueue->receiveMessage(&command); result == RETURN_OK; + result = commandQueue->receiveMessage(&command)) { + + result = healthHelper.handleHealthCommand(&command); + if (result == RETURN_OK) { + continue; + } + + result = modeHelper.handleModeCommand(&command); + if (result == RETURN_OK) { + continue; + } + + result = handleModeReply(&command); + if (result == RETURN_OK) { + continue; + } + + result = handleCommandMessage(&command); + if (result != RETURN_OK) { + CommandMessage reply; + reply.setReplyRejected(CommandMessage::UNKNOWN_COMMAND, + command.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/subsystem/SubsystemBase.h b/subsystem/SubsystemBase.h index c0abbe40..2d949b56 100644 --- a/subsystem/SubsystemBase.h +++ b/subsystem/SubsystemBase.h @@ -1,129 +1,129 @@ -#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 "../subsystem/modes/HasModeSequenceIF.h" -#include "../tasks/ExecutableObjectIF.h" -#include "../ipc/MessageQueueIF.h" -#include - -/** - * @defgroup subsystems Subsystem Objects - * Contains all Subsystem and Assemblies - */ -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_ */ +#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 "../subsystem/modes/HasModeSequenceIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../ipc/MessageQueueIF.h" +#include + +/** + * @defgroup subsystems Subsystem Objects + * Contains all Subsystem and Assemblies + */ +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/subsystem/modes/HasModeSequenceIF.h b/subsystem/modes/HasModeSequenceIF.h index 90007d80..06bde2aa 100644 --- a/subsystem/modes/HasModeSequenceIF.h +++ b/subsystem/modes/HasModeSequenceIF.h @@ -1,20 +1,20 @@ -#ifndef HASMODESEQUENCEIF_H_ -#define HASMODESEQUENCEIF_H_ - -#include "../../subsystem/modes/ModeDefinitions.h" -#include "../../subsystem/modes/ModeSequenceMessage.h" -#include "../../subsystem/modes/ModeStoreIF.h" - - -class HasModeSequenceIF { -public: - virtual ~HasModeSequenceIF() { - - } - - virtual MessageQueueId_t getSequenceCommandQueue() const = 0; - -}; - - -#endif /* HASMODESEQUENCEIF_H_ */ +#ifndef HASMODESEQUENCEIF_H_ +#define HASMODESEQUENCEIF_H_ + +#include "../../subsystem/modes/ModeDefinitions.h" +#include "../../subsystem/modes/ModeSequenceMessage.h" +#include "../../subsystem/modes/ModeStoreIF.h" + + +class HasModeSequenceIF { +public: + virtual ~HasModeSequenceIF() { + + } + + virtual MessageQueueId_t getSequenceCommandQueue() const = 0; + +}; + + +#endif /* HASMODESEQUENCEIF_H_ */ diff --git a/subsystem/modes/ModeDefinitions.h b/subsystem/modes/ModeDefinitions.h index 0514b357..a865ab0c 100644 --- a/subsystem/modes/ModeDefinitions.h +++ b/subsystem/modes/ModeDefinitions.h @@ -1,152 +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_ +#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/subsystem/modes/ModeSequenceMessage.cpp b/subsystem/modes/ModeSequenceMessage.cpp index 3eb4ebe8..e092380e 100644 --- a/subsystem/modes/ModeSequenceMessage.cpp +++ b/subsystem/modes/ModeSequenceMessage.cpp @@ -1,89 +1,89 @@ -#include "../../objectmanager/ObjectManagerIF.h" -#include "../../objectmanager/ObjectManagerIF.h" -#include "../../objectmanager/ObjectManagerIF.h" -#include "../../storagemanager/StorageManagerIF.h" -#include "../../subsystem/modes/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; - } -} +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../storagemanager/StorageManagerIF.h" +#include "../../subsystem/modes/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/subsystem/modes/ModeSequenceMessage.h b/subsystem/modes/ModeSequenceMessage.h index fcb5b2e7..61ba30f3 100644 --- a/subsystem/modes/ModeSequenceMessage.h +++ b/subsystem/modes/ModeSequenceMessage.h @@ -1,48 +1,48 @@ -#ifndef MODESEQUENCEMESSAGE_H_ -#define MODESEQUENCEMESSAGE_H_ - -#include "../../ipc/CommandMessage.h" -#include "../../storagemanager/StorageManagerIF.h" -#include "../../subsystem/modes/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_ */ +#ifndef MODESEQUENCEMESSAGE_H_ +#define MODESEQUENCEMESSAGE_H_ + +#include "../../ipc/CommandMessage.h" +#include "../../storagemanager/StorageManagerIF.h" +#include "../../subsystem/modes/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/subsystem/modes/ModeStore.cpp b/subsystem/modes/ModeStore.cpp index 409a5f0f..dce2931c 100644 --- a/subsystem/modes/ModeStore.cpp +++ b/subsystem/modes/ModeStore.cpp @@ -1,126 +1,126 @@ -#include "../../subsystem/modes/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 +#include "../../subsystem/modes/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/subsystem/modes/ModeStore.h b/subsystem/modes/ModeStore.h index cb340443..3d031cc8 100644 --- a/subsystem/modes/ModeStore.h +++ b/subsystem/modes/ModeStore.h @@ -1,45 +1,45 @@ -#ifndef MODESTORE_H_ -#define MODESTORE_H_ - -#ifdef USE_MODESTORE - -#include "../../container/ArrayList.h" -#include "../../container/SinglyLinkedList.h" -#include "../../objectmanager/SystemObject.h" -#include "../../subsystem/modes/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_ */ - +#ifndef MODESTORE_H_ +#define MODESTORE_H_ + +#ifdef USE_MODESTORE + +#include "../../container/ArrayList.h" +#include "../../container/SinglyLinkedList.h" +#include "../../objectmanager/SystemObject.h" +#include "../../subsystem/modes/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/subsystem/modes/ModeStoreIF.h b/subsystem/modes/ModeStoreIF.h index b9f2df70..30cca1f7 100644 --- a/subsystem/modes/ModeStoreIF.h +++ b/subsystem/modes/ModeStoreIF.h @@ -1,37 +1,37 @@ -#ifndef MODESTOREIF_H_ -#define MODESTOREIF_H_ - -#ifdef USE_MODESTORE - -#include "../../container/ArrayList.h" -#include "../../container/SinglyLinkedList.h" -#include "../../returnvalues/HasReturnvaluesIF.h" -#include "../../subsystem/modes/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_ */ +#ifndef MODESTOREIF_H_ +#define MODESTOREIF_H_ + +#ifdef USE_MODESTORE + +#include "../../container/ArrayList.h" +#include "../../container/SinglyLinkedList.h" +#include "../../returnvalues/HasReturnvaluesIF.h" +#include "../../subsystem/modes/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/tasks/ExecutableObjectIF.h b/tasks/ExecutableObjectIF.h index 902b2e3c..06b837db 100644 --- a/tasks/ExecutableObjectIF.h +++ b/tasks/ExecutableObjectIF.h @@ -1,53 +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_ */ +#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/tasks/FixedSequenceSlot.cpp b/tasks/FixedSequenceSlot.cpp index 9d290d19..a38734a2 100644 --- a/tasks/FixedSequenceSlot.cpp +++ b/tasks/FixedSequenceSlot.cpp @@ -1,17 +1,17 @@ -#include "../objectmanager/SystemObjectIF.h" -#include "../tasks/FixedSequenceSlot.h" -#include - -FixedSequenceSlot::FixedSequenceSlot(object_id_t handlerId, uint32_t setTime, - int8_t setSequenceId, ExecutableObjectIF* executableObject, - PeriodicTaskIF* executingTask) : - pollingTimeMs(setTime), opcode(setSequenceId) { - if(executableObject == nullptr) { - return; - } - this->executableObject = executableObject; - this->executableObject->setTaskIF(executingTask); -} - -FixedSequenceSlot::~FixedSequenceSlot() {} - +#include "../objectmanager/SystemObjectIF.h" +#include "../tasks/FixedSequenceSlot.h" +#include + +FixedSequenceSlot::FixedSequenceSlot(object_id_t handlerId, uint32_t setTime, + int8_t setSequenceId, ExecutableObjectIF* executableObject, + PeriodicTaskIF* executingTask) : + pollingTimeMs(setTime), opcode(setSequenceId) { + if(executableObject == nullptr) { + return; + } + this->executableObject = executableObject; + this->executableObject->setTaskIF(executingTask); +} + +FixedSequenceSlot::~FixedSequenceSlot() {} + diff --git a/tasks/FixedSequenceSlot.h b/tasks/FixedSequenceSlot.h index 9cf5894c..6ec9ae13 100644 --- a/tasks/FixedSequenceSlot.h +++ b/tasks/FixedSequenceSlot.h @@ -1,57 +1,57 @@ -#ifndef FRAMEWORK_TASKS_FIXEDSEQUENCESLOT_H_ -#define FRAMEWORK_TASKS_FIXEDSEQUENCESLOT_H_ - -#include "../objectmanager/ObjectManagerIF.h" -#include "../tasks/ExecutableObjectIF.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(); - - /** - * @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 /* FIXEDSEQUENCESLOT_H_ */ +#ifndef FRAMEWORK_TASKS_FIXEDSEQUENCESLOT_H_ +#define FRAMEWORK_TASKS_FIXEDSEQUENCESLOT_H_ + +#include "../objectmanager/ObjectManagerIF.h" +#include "../tasks/ExecutableObjectIF.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(); + + /** + * @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 /* FIXEDSEQUENCESLOT_H_ */ diff --git a/tasks/FixedSlotSequence.cpp b/tasks/FixedSlotSequence.cpp index d803a419..59859ce5 100644 --- a/tasks/FixedSlotSequence.cpp +++ b/tasks/FixedSlotSequence.cpp @@ -1,147 +1,147 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../tasks/FixedSlotSequence.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 << "Fixed Slot Sequence: Slot list is empty!" << std::endl; - return HasReturnvaluesIF::RETURN_FAILED; - } - - uint32_t count = 0; - uint32_t time = 0; - for(const auto& slot: slotList) { - if (slot.executableObject == nullptr) { - count++; - } - else if (slot.pollingTimeMs < time) { - sif::error << "FixedSlotSequence::initialize: Time: " - << slot.pollingTimeMs << " is smaller than previous with " - << time << std::endl; - count++; - } - 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 (count > 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; -} +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tasks/FixedSlotSequence.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 << "Fixed Slot Sequence: Slot list is empty!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + + uint32_t count = 0; + uint32_t time = 0; + for(const auto& slot: slotList) { + if (slot.executableObject == nullptr) { + count++; + } + else if (slot.pollingTimeMs < time) { + sif::error << "FixedSlotSequence::initialize: Time: " + << slot.pollingTimeMs << " is smaller than previous with " + << time << std::endl; + count++; + } + 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 (count > 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; +} diff --git a/tasks/FixedSlotSequence.h b/tasks/FixedSlotSequence.h index 9ab84552..1140dd25 100644 --- a/tasks/FixedSlotSequence.h +++ b/tasks/FixedSlotSequence.h @@ -1,159 +1,159 @@ -#ifndef FRAMEWORK_TASKS_FIXEDSLOTSEQUENCE_H_ -#define FRAMEWORK_TASKS_FIXEDSLOTSEQUENCE_H_ - -#include "../objectmanager/SystemObject.h" -#include "../tasks/FixedSequenceSlot.h" - -#include - -/** - * @brief This class is the representation of a Polling Sequence Table in software. - * @details - * The FixedSlotSequence object maintains the dynamic execution of - * device handler objects. - * - * The main idea is to create a list of device handlers, to announce all - * handlers to thepolling 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 device handler shall be executed within a given polling period. - * The sequence is then executed by iterating through this slot list. - * Handlers are invoking by calling a certain function stored in the handler list. - */ -class FixedSlotSequence { -public: - using SlotList = std::multiset; - using SlotListIter = std::multiset::iterator; - - /** - * @brief The constructor of the FixedSlotSequence object. - * - * @details The constructor takes two arguments, the period length and the init function. - * - * @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 Object 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 - * @param - * @param - */ - void addSlot(object_id_t handlerId, uint32_t setTime, int8_t setSequenceId, - ExecutableObjectIF* executableObject, PeriodicTaskIF* executingTask); - - /** - * Checks if the current slot shall be executed immediately after the one before. - * This allows to distinguish between grouped and not grouped 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. - * Also calls any initialization steps which are required after task - * creation. - * @return - */ - ReturnValue_t checkSequence() const; - - 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; - - uint32_t lengthMs; - - bool isEmpty = false; -}; - -#endif /* FIXEDSLOTSEQUENCE_H_ */ +#ifndef FRAMEWORK_TASKS_FIXEDSLOTSEQUENCE_H_ +#define FRAMEWORK_TASKS_FIXEDSLOTSEQUENCE_H_ + +#include "../objectmanager/SystemObject.h" +#include "../tasks/FixedSequenceSlot.h" + +#include + +/** + * @brief This class is the representation of a Polling Sequence Table in software. + * @details + * The FixedSlotSequence object maintains the dynamic execution of + * device handler objects. + * + * The main idea is to create a list of device handlers, to announce all + * handlers to thepolling 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 device handler shall be executed within a given polling period. + * The sequence is then executed by iterating through this slot list. + * Handlers are invoking by calling a certain function stored in the handler list. + */ +class FixedSlotSequence { +public: + using SlotList = std::multiset; + using SlotListIter = std::multiset::iterator; + + /** + * @brief The constructor of the FixedSlotSequence object. + * + * @details The constructor takes two arguments, the period length and the init function. + * + * @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 Object 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 + * @param + * @param + */ + void addSlot(object_id_t handlerId, uint32_t setTime, int8_t setSequenceId, + ExecutableObjectIF* executableObject, PeriodicTaskIF* executingTask); + + /** + * Checks if the current slot shall be executed immediately after the one before. + * This allows to distinguish between grouped and not grouped 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. + * Also calls any initialization steps which are required after task + * creation. + * @return + */ + ReturnValue_t checkSequence() const; + + 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; + + uint32_t lengthMs; + + bool isEmpty = false; +}; + +#endif /* FIXEDSLOTSEQUENCE_H_ */ diff --git a/tasks/FixedTimeslotTaskIF.h b/tasks/FixedTimeslotTaskIF.h index daced785..6344efa5 100644 --- a/tasks/FixedTimeslotTaskIF.h +++ b/tasks/FixedTimeslotTaskIF.h @@ -1,33 +1,33 @@ -#ifndef FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ -#define FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ - -#include "../objectmanager/ObjectManagerIF.h" -#include "../tasks/PeriodicTaskIF.h" - -/** - * @brief 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() {} - - /** - * Add an object with a slot time and the execution step to the task. - * The execution step shall be passed to the object. - * @param componentId - * @param slotTimeMs - * @param executionStep - * @return - */ - virtual ReturnValue_t addSlot(object_id_t componentId, - uint32_t slotTimeMs, int8_t executionStep) = 0; - - /** - * Check whether the sequence is valid and perform all other required - * initialization steps which are needed after task creation - */ - virtual ReturnValue_t checkSequence() const = 0; -}; - -#endif /* FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ */ +#ifndef FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ +#define FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ + +#include "../objectmanager/ObjectManagerIF.h" +#include "../tasks/PeriodicTaskIF.h" + +/** + * @brief 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() {} + + /** + * Add an object with a slot time and the execution step to the task. + * The execution step shall be passed to the object. + * @param componentId + * @param slotTimeMs + * @param executionStep + * @return + */ + virtual ReturnValue_t addSlot(object_id_t componentId, + uint32_t slotTimeMs, int8_t executionStep) = 0; + + /** + * Check whether the sequence is valid and perform all other required + * initialization steps which are needed after task creation + */ + virtual ReturnValue_t checkSequence() const = 0; +}; + +#endif /* FRAMEWORK_TASKS_FIXEDTIMESLOTTASKIF_H_ */ diff --git a/tasks/PeriodicTaskIF.h b/tasks/PeriodicTaskIF.h index 38cf201b..4df37645 100644 --- a/tasks/PeriodicTaskIF.h +++ b/tasks/PeriodicTaskIF.h @@ -1,49 +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_ */ +#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/tasks/SemaphoreFactory.h b/tasks/SemaphoreFactory.h index 2c41a135..01c09d1b 100644 --- a/tasks/SemaphoreFactory.h +++ b/tasks/SemaphoreFactory.h @@ -1,50 +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_ */ +#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/tasks/SemaphoreIF.h b/tasks/SemaphoreIF.h index 30d4ed88..dd327e23 100644 --- a/tasks/SemaphoreIF.h +++ b/tasks/SemaphoreIF.h @@ -1,68 +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_ */ +#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/tasks/TaskFactory.h b/tasks/TaskFactory.h index 56b3083e..fd2f4460 100644 --- a/tasks/TaskFactory.h +++ b/tasks/TaskFactory.h @@ -1,73 +1,73 @@ -#ifndef FRAMEWORK_TASKS_TASKFACTORY_H_ -#define FRAMEWORK_TASKS_TASKFACTORY_H_ - -#include -#include "../tasks/FixedTimeslotTaskIF.h" -#include "../tasks/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(); - - /** - * Generic interface to create a periodic task - * @param name_ Name of the task - * @param taskPriority_ Priority of the task - * @param stackSize_ Stack size if the task - * @param periodInSeconds_ Period in seconds - * @param deadLineMissedFunction_ This function is called if a deadline was - * missed - * @return Pointer to the created periodic task class - */ - PeriodicTaskIF* createPeriodicTask(TaskName name_, - TaskPriority taskPriority_, TaskStackSize stackSize_, - TaskPeriod periodInSeconds_, - TaskDeadlineMissedFunction deadLineMissedFunction_); - - /** - * Generic interface to create a fixed timeslot task - * @param name_ Name of the task - * @param taskPriority_ Priority of the task - * @param stackSize_ Stack size if the task - * @param periodInSeconds_ Period in seconds - * @param deadLineMissedFunction_ This function is called if a deadline was - * missed - * @return Pointer to the created periodic task class - */ - 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_ */ +#ifndef FRAMEWORK_TASKS_TASKFACTORY_H_ +#define FRAMEWORK_TASKS_TASKFACTORY_H_ + +#include +#include "../tasks/FixedTimeslotTaskIF.h" +#include "../tasks/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(); + + /** + * Generic interface to create a periodic task + * @param name_ Name of the task + * @param taskPriority_ Priority of the task + * @param stackSize_ Stack size if the task + * @param periodInSeconds_ Period in seconds + * @param deadLineMissedFunction_ This function is called if a deadline was + * missed + * @return Pointer to the created periodic task class + */ + PeriodicTaskIF* createPeriodicTask(TaskName name_, + TaskPriority taskPriority_, TaskStackSize stackSize_, + TaskPeriod periodInSeconds_, + TaskDeadlineMissedFunction deadLineMissedFunction_); + + /** + * Generic interface to create a fixed timeslot task + * @param name_ Name of the task + * @param taskPriority_ Priority of the task + * @param stackSize_ Stack size if the task + * @param periodInSeconds_ Period in seconds + * @param deadLineMissedFunction_ This function is called if a deadline was + * missed + * @return Pointer to the created periodic task class + */ + 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/tcdistribution/CCSDSDistributor.cpp b/tcdistribution/CCSDSDistributor.cpp index 68f26d2c..dda26d7f 100644 --- a/tcdistribution/CCSDSDistributor.cpp +++ b/tcdistribution/CCSDSDistributor.cpp @@ -1,82 +1,82 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../tcdistribution/CCSDSDistributor.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; - bool errorCode = true; - errorCode = this->queueMap.insert( std::pair( application->getIdentifier(), application->getRequestQueue() ) ).second; - if( errorCode == false ) { - returnValue = RETURN_FAILED; - } - return returnValue; -} - -ReturnValue_t CCSDSDistributor::registerApplication(uint16_t apid, - MessageQueueId_t id) { - ReturnValue_t returnValue = RETURN_OK; - bool errorCode = true; - errorCode = this->queueMap.insert( std::pair( apid, id ) ).second; - if( errorCode == false ) { - 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 == NULL) status = RETURN_FAILED; - return status; -} - -ReturnValue_t CCSDSDistributor::callbackAfterSending( - ReturnValue_t queueStatus) { - if (queueStatus != RETURN_OK) { - tcStore->deleteData(currentMessage.getStorageId()); - } - return RETURN_OK; -} +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tcdistribution/CCSDSDistributor.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; + bool errorCode = true; + errorCode = this->queueMap.insert( std::pair( application->getIdentifier(), application->getRequestQueue() ) ).second; + if( errorCode == false ) { + returnValue = RETURN_FAILED; + } + return returnValue; +} + +ReturnValue_t CCSDSDistributor::registerApplication(uint16_t apid, + MessageQueueId_t id) { + ReturnValue_t returnValue = RETURN_OK; + bool errorCode = true; + errorCode = this->queueMap.insert( std::pair( apid, id ) ).second; + if( errorCode == false ) { + 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 == NULL) 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/tcdistribution/CCSDSDistributor.h b/tcdistribution/CCSDSDistributor.h index e54b9fe4..6abd952b 100644 --- a/tcdistribution/CCSDSDistributor.h +++ b/tcdistribution/CCSDSDistributor.h @@ -1,69 +1,69 @@ -#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(); - 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 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; - /** - * The callback here handles the generation of acceptance - * success/failure messages. - */ - ReturnValue_t callbackAfterSending( ReturnValue_t queueStatus ); -}; - -#endif /* FRAMEWORK_TCDISTRIBUTION_CCSDSDISTRIBUTOR_H_ */ +#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(); + 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 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; + /** + * The callback here handles the generation of acceptance + * success/failure messages. + */ + ReturnValue_t callbackAfterSending( ReturnValue_t queueStatus ); +}; + +#endif /* FRAMEWORK_TCDISTRIBUTION_CCSDSDISTRIBUTOR_H_ */ diff --git a/tcdistribution/CCSDSDistributorIF.h b/tcdistribution/CCSDSDistributorIF.h index 32198120..be3c2811 100644 --- a/tcdistribution/CCSDSDistributorIF.h +++ b/tcdistribution/CCSDSDistributorIF.h @@ -1,40 +1,40 @@ -#ifndef CCSDSDISTRIBUTORIF_H_ -#define 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 /* CCSDSDISTRIBUTORIF_H_ */ +#ifndef CCSDSDISTRIBUTORIF_H_ +#define 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 /* CCSDSDISTRIBUTORIF_H_ */ diff --git a/tcdistribution/PUSDistributor.cpp b/tcdistribution/PUSDistributor.cpp index fee16f11..0c710ef2 100644 --- a/tcdistribution/PUSDistributor.cpp +++ b/tcdistribution/PUSDistributor.cpp @@ -1,92 +1,92 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../tcdistribution/CCSDSDistributorIF.h" -#include "../tcdistribution/PUSDistributor.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(), - currentPacket(), tcStatus(RETURN_FAILED), - packetSource(setPacketSource) {} - -PUSDistributor::~PUSDistributor() {} - -TcDistributor::TcMqMapIter PUSDistributor::selectDestination() { - TcMqMapIter queueMapIt = this->queueMap.end(); - this->currentPacket.setStoreAddress(this->currentMessage.getStorageId()); - if (currentPacket.getWholeData() != NULL) { - tcStatus = checker.checkPacket(¤tPacket); -// sif::debug << "PUSDistributor::handlePacket: packetCheck returned with " -// << (int)tcStatus << std::endl; - 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; - } - - if (tcStatus != RETURN_OK) { - sif::debug << "PUSDistributor::handlePacket: Error with " << tcStatus - << ", 0x"<< std::hex << tcStatus << std::dec << std::endl; - return this->queueMap.end(); - } else { - return queueMapIt; - } - -} - -//uint16_t PUSDistributor::createDestination( uint8_t service_id, uint8_t subservice_id ) { -// return ( service_id << 8 ) + subservice_id; -//} - -ReturnValue_t PUSDistributor::registerService(AcceptsTelecommandsIF* service) { - uint16_t serviceId = service->getIdentifier(); - //info << "Service ID: " << (int)serviceId << std::endl; - MessageQueueId_t queue = service->getRequestQueue(); - auto returnPair = queueMap.emplace(serviceId, queue); - if (not returnPair.second) { - //TODO Return Code - sif::error << "PUSDistributor::registerService: Service ID already" - " exists in map." << std::endl; - return HasReturnvaluesIF::RETURN_FAILED; - } - 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, - ¤tPacket, 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, - ¤tPacket); - return RETURN_OK; - } -} - -uint16_t PUSDistributor::getIdentifier() { - return checker.getApid(); -} - -ReturnValue_t PUSDistributor::initialize() { - CCSDSDistributorIF* ccsdsDistributor = - objectManager->get(packetSource); - if (ccsdsDistributor == NULL) { - return RETURN_FAILED; - } else { - return ccsdsDistributor->registerApplication(this); - } -} +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tcdistribution/CCSDSDistributorIF.h" +#include "../tcdistribution/PUSDistributor.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(), + currentPacket(), tcStatus(RETURN_FAILED), + packetSource(setPacketSource) {} + +PUSDistributor::~PUSDistributor() {} + +TcDistributor::TcMqMapIter PUSDistributor::selectDestination() { + TcMqMapIter queueMapIt = this->queueMap.end(); + this->currentPacket.setStoreAddress(this->currentMessage.getStorageId()); + if (currentPacket.getWholeData() != NULL) { + tcStatus = checker.checkPacket(¤tPacket); +// sif::debug << "PUSDistributor::handlePacket: packetCheck returned with " +// << (int)tcStatus << std::endl; + 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; + } + + if (tcStatus != RETURN_OK) { + sif::debug << "PUSDistributor::handlePacket: Error with " << tcStatus + << ", 0x"<< std::hex << tcStatus << std::dec << std::endl; + return this->queueMap.end(); + } else { + return queueMapIt; + } + +} + +//uint16_t PUSDistributor::createDestination( uint8_t service_id, uint8_t subservice_id ) { +// return ( service_id << 8 ) + subservice_id; +//} + +ReturnValue_t PUSDistributor::registerService(AcceptsTelecommandsIF* service) { + uint16_t serviceId = service->getIdentifier(); + //info << "Service ID: " << (int)serviceId << std::endl; + MessageQueueId_t queue = service->getRequestQueue(); + auto returnPair = queueMap.emplace(serviceId, queue); + if (not returnPair.second) { + //TODO Return Code + sif::error << "PUSDistributor::registerService: Service ID already" + " exists in map." << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + 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, + ¤tPacket, 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, + ¤tPacket); + return RETURN_OK; + } +} + +uint16_t PUSDistributor::getIdentifier() { + return checker.getApid(); +} + +ReturnValue_t PUSDistributor::initialize() { + CCSDSDistributorIF* ccsdsDistributor = + objectManager->get(packetSource); + if (ccsdsDistributor == NULL) { + return RETURN_FAILED; + } else { + return ccsdsDistributor->registerApplication(this); + } +} diff --git a/tcdistribution/PUSDistributor.h b/tcdistribution/PUSDistributor.h index be84de1b..b8cff1f4 100644 --- a/tcdistribution/PUSDistributor.h +++ b/tcdistribution/PUSDistributor.h @@ -1,77 +1,77 @@ -#ifndef FRAMEWORK_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ -#define FRAMEWORK_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../tcdistribution/PUSDistributorIF.h" -#include "../tcdistribution/TcDistributor.h" -#include "../tcdistribution/TcPacketCheck.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; - /** - * 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); -}; - -#endif /* FRAMEWORK_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ */ +#ifndef FRAMEWORK_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ +#define FRAMEWORK_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../tcdistribution/PUSDistributorIF.h" +#include "../tcdistribution/TcDistributor.h" +#include "../tcdistribution/TcPacketCheck.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; + /** + * 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); +}; + +#endif /* FRAMEWORK_TCDISTRIBUTION_PUSDISTRIBUTOR_H_ */ diff --git a/tcdistribution/PUSDistributorIF.h b/tcdistribution/PUSDistributorIF.h index 7967a74e..5e27b35c 100644 --- a/tcdistribution/PUSDistributorIF.h +++ b/tcdistribution/PUSDistributorIF.h @@ -1,26 +1,26 @@ -#ifndef PUSDISTRIBUTORIF_H_ -#define 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 /* PUSDISTRIBUTORIF_H_ */ +#ifndef PUSDISTRIBUTORIF_H_ +#define 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 /* PUSDISTRIBUTORIF_H_ */ diff --git a/tcdistribution/TcDistributor.cpp b/tcdistribution/TcDistributor.cpp index 45a20fd9..b0354558 100644 --- a/tcdistribution/TcDistributor.cpp +++ b/tcdistribution/TcDistributor.cpp @@ -1,54 +1,54 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../tcdistribution/TcDistributor.h" -#include "../tmtcservices/TmTcMessage.h" -#include "../ipc/QueueFactory.h" - -TcDistributor::TcDistributor(object_id_t set_object_id) : - SystemObject(set_object_id), tcQueue(NULL) { - 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; - for (TcMqMapIter it = this->queueMap.begin(); it != this->queueMap.end(); - it++) { - sif::debug << it->first << "\t| 0x" << std::hex << it->second << std::dec - << std::endl; - } - sif::debug << std::dec; - -} - -ReturnValue_t TcDistributor::callbackAfterSending(ReturnValue_t queueStatus) { - return RETURN_OK; -} +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tcdistribution/TcDistributor.h" +#include "../tmtcservices/TmTcMessage.h" +#include "../ipc/QueueFactory.h" + +TcDistributor::TcDistributor(object_id_t set_object_id) : + SystemObject(set_object_id), tcQueue(NULL) { + 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; + for (TcMqMapIter it = this->queueMap.begin(); it != this->queueMap.end(); + it++) { + sif::debug << it->first << "\t| 0x" << std::hex << it->second << std::dec + << std::endl; + } + sif::debug << std::dec; + +} + +ReturnValue_t TcDistributor::callbackAfterSending(ReturnValue_t queueStatus) { + return RETURN_OK; +} diff --git a/tcdistribution/TcDistributor.h b/tcdistribution/TcDistributor.h index 987f2eaa..95b7d25b 100644 --- a/tcdistribution/TcDistributor.h +++ b/tcdistribution/TcDistributor.h @@ -1,120 +1,120 @@ -#ifndef FRAMEWORK_TMTCSERVICES_TCDISTRIBUTOR_H_ -#define FRAMEWORK_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 const uint8_t INTERFACE_ID = CLASS_ID::PACKET_DISTRIBUTION; - static const ReturnValue_t PACKET_LOST = MAKE_RETURN_CODE( 1 ); - static const ReturnValue_t DESTINATION_NOT_FOUND = MAKE_RETURN_CODE( 2 ); - /** - * 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 set_object_id ); - /** - * 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; - /** - * 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 const uint8_t DISTRIBUTER_MAX_PACKETS = 128; -}; - - -#endif /* TCDISTRIBUTOR_H_ */ +#ifndef FRAMEWORK_TMTCSERVICES_TCDISTRIBUTOR_H_ +#define FRAMEWORK_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 const uint8_t INTERFACE_ID = CLASS_ID::PACKET_DISTRIBUTION; + static const ReturnValue_t PACKET_LOST = MAKE_RETURN_CODE( 1 ); + static const ReturnValue_t DESTINATION_NOT_FOUND = MAKE_RETURN_CODE( 2 ); + /** + * 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 set_object_id ); + /** + * 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; + /** + * 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 const uint8_t DISTRIBUTER_MAX_PACKETS = 128; +}; + + +#endif /* TCDISTRIBUTOR_H_ */ diff --git a/tcdistribution/TcPacketCheck.cpp b/tcdistribution/TcPacketCheck.cpp index 98dfaeb8..4701ce23 100644 --- a/tcdistribution/TcPacketCheck.cpp +++ b/tcdistribution/TcPacketCheck.cpp @@ -1,37 +1,37 @@ -#include "../globalfunctions/CRC.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../storagemanager/StorageManagerIF.h" -#include "../tcdistribution/TcPacketCheck.h" -#include "../tmtcservices/VerificationCodes.h" - -TcPacketCheck::TcPacketCheck( uint16_t set_apid ) : apid(set_apid) { -} - -ReturnValue_t TcPacketCheck::checkPacket( TcPacketStored* current_packet ) { - uint16_t calculated_crc = CRC::crc16ccitt( current_packet->getWholeData(), current_packet->getFullSize() ); - if ( calculated_crc != 0 ) { - return INCORRECT_CHECKSUM; - } - bool condition = !(current_packet->hasSecondaryHeader()) || - current_packet->getPacketVersionNumber() != CCSDS_VERSION_NUMBER || - !(current_packet->isTelecommand()); - if ( condition ) { - return INCORRECT_PRIMARY_HEADER; - } - if ( current_packet->getAPID() != this->apid ) - return ILLEGAL_APID; - - if ( !current_packet->isSizeCorrect() ) { - return INCOMPLETE_PACKET; - } - condition = (current_packet->getSecondaryHeaderFlag() != CCSDS_SECONDARY_HEADER_FLAG) || - (current_packet->getPusVersionNumber() != PUS_VERSION_NUMBER); - if ( condition ) { - return INCORRECT_SECONDARY_HEADER; - } - return RETURN_OK; -} - -uint16_t TcPacketCheck::getApid() const { - return apid; -} +#include "../globalfunctions/CRC.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tcdistribution/TcPacketCheck.h" +#include "../tmtcservices/VerificationCodes.h" + +TcPacketCheck::TcPacketCheck( uint16_t set_apid ) : apid(set_apid) { +} + +ReturnValue_t TcPacketCheck::checkPacket( TcPacketStored* current_packet ) { + uint16_t calculated_crc = CRC::crc16ccitt( current_packet->getWholeData(), current_packet->getFullSize() ); + if ( calculated_crc != 0 ) { + return INCORRECT_CHECKSUM; + } + bool condition = !(current_packet->hasSecondaryHeader()) || + current_packet->getPacketVersionNumber() != CCSDS_VERSION_NUMBER || + !(current_packet->isTelecommand()); + if ( condition ) { + return INCORRECT_PRIMARY_HEADER; + } + if ( current_packet->getAPID() != this->apid ) + return ILLEGAL_APID; + + if ( !current_packet->isSizeCorrect() ) { + return INCOMPLETE_PACKET; + } + condition = (current_packet->getSecondaryHeaderFlag() != CCSDS_SECONDARY_HEADER_FLAG) || + (current_packet->getPusVersionNumber() != PUS_VERSION_NUMBER); + if ( condition ) { + return INCORRECT_SECONDARY_HEADER; + } + return RETURN_OK; +} + +uint16_t TcPacketCheck::getApid() const { + return apid; +} diff --git a/tcdistribution/TcPacketCheck.h b/tcdistribution/TcPacketCheck.h index 9fe1db72..4ba269f5 100644 --- a/tcdistribution/TcPacketCheck.h +++ b/tcdistribution/TcPacketCheck.h @@ -1,59 +1,59 @@ -#ifndef TCPACKETCHECK_H_ -#define 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 const uint8_t CCSDS_VERSION_NUMBER = 0; - /** - * Describes the secondary header a packet must have to pass. - */ - static const uint8_t CCSDS_SECONDARY_HEADER_FLAG = 0; - /** - * Describes the TC Packet PUS Version Number a packet must have to pass. - */ - static const 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 set_apid ); - /** - * 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* current_packet ); - - uint16_t getApid() const; -}; - - -#endif /* TCPACKETCHECK_H_ */ +#ifndef TCPACKETCHECK_H_ +#define 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 const uint8_t CCSDS_VERSION_NUMBER = 0; + /** + * Describes the secondary header a packet must have to pass. + */ + static const uint8_t CCSDS_SECONDARY_HEADER_FLAG = 0; + /** + * Describes the TC Packet PUS Version Number a packet must have to pass. + */ + static const 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 set_apid ); + /** + * 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* current_packet ); + + uint16_t getApid() const; +}; + + +#endif /* TCPACKETCHECK_H_ */ diff --git a/thermal/AbstractTemperatureSensor.cpp b/thermal/AbstractTemperatureSensor.cpp index 5d821367..40b305af 100644 --- a/thermal/AbstractTemperatureSensor.cpp +++ b/thermal/AbstractTemperatureSensor.cpp @@ -1,70 +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 command; - ReturnValue_t result = commandQueue->receiveMessage(&command); - if (result == HasReturnvaluesIF::RETURN_OK) { - 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); - } -} - -ReturnValue_t AbstractTemperatureSensor::setHealth(HealthState health) { - healthHelper.setHealth(health); - return HasReturnvaluesIF::RETURN_OK; -} - -HasHealthIF::HealthState AbstractTemperatureSensor::getHealth() { - return healthHelper.getHealth(); -} +#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 command; + ReturnValue_t result = commandQueue->receiveMessage(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + 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); + } +} + +ReturnValue_t AbstractTemperatureSensor::setHealth(HealthState health) { + healthHelper.setHealth(health); + return HasReturnvaluesIF::RETURN_OK; +} + +HasHealthIF::HealthState AbstractTemperatureSensor::getHealth() { + return healthHelper.getHealth(); +} diff --git a/thermal/AbstractTemperatureSensor.h b/thermal/AbstractTemperatureSensor.h index 462b41f2..cc064ce4 100644 --- a/thermal/AbstractTemperatureSensor.h +++ b/thermal/AbstractTemperatureSensor.h @@ -1,64 +1,64 @@ -#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" - -/** - * @defgroup thermal Thermal Components - * @brief Contains all components related to thermal tasks (sensors, heaters) - */ - -/** - * @brief Base class for Temperature Sensor, implements all important interfaces. - * Please use the TemperatureSensor class to implement the actual sensors. - * @ingroup thermal - */ -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_ */ +#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" + +/** + * @defgroup thermal Thermal Components + * @brief Contains all components related to thermal tasks (sensors, heaters) + */ + +/** + * @brief Base class for Temperature Sensor, implements all important interfaces. + * Please use the TemperatureSensor class to implement the actual sensors. + * @ingroup thermal + */ +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/thermal/AcceptsThermalMessagesIF.h b/thermal/AcceptsThermalMessagesIF.h index 9fe03499..5fbd6bb3 100644 --- a/thermal/AcceptsThermalMessagesIF.h +++ b/thermal/AcceptsThermalMessagesIF.h @@ -1,22 +1,22 @@ -/** - * \file AcceptsThermalMessagesIF.h - * - * \date 16.02.2020 - */ - -#ifndef FRAMEWORK_THERMAL_ACCEPTSTHERMALMESSAGESIF_H_ -#define FRAMEWORK_THERMAL_ACCEPTSTHERMALMESSAGESIF_H_ -#include "../ipc/MessageQueueSenderIF.h" - -class AcceptsThermalMessagesIF { -public: - - /** - * @brief This is the empty virtual destructor as required for C++ interfaces. - */ - virtual ~AcceptsThermalMessagesIF() { } - - virtual MessageQueueId_t getReceptionQueue() const = 0; -}; - -#endif /* FRAMEWORK_THERMAL_ACCEPTSTHERMALMESSAGESIF_H_ */ +/** + * \file AcceptsThermalMessagesIF.h + * + * \date 16.02.2020 + */ + +#ifndef FRAMEWORK_THERMAL_ACCEPTSTHERMALMESSAGESIF_H_ +#define FRAMEWORK_THERMAL_ACCEPTSTHERMALMESSAGESIF_H_ +#include "../ipc/MessageQueueSenderIF.h" + +class AcceptsThermalMessagesIF { +public: + + /** + * @brief This is the empty virtual destructor as required for C++ interfaces. + */ + virtual ~AcceptsThermalMessagesIF() { } + + virtual MessageQueueId_t getReceptionQueue() const = 0; +}; + +#endif /* FRAMEWORK_THERMAL_ACCEPTSTHERMALMESSAGESIF_H_ */ diff --git a/thermal/CoreComponent.h b/thermal/CoreComponent.h index 87a6972e..8093711d 100644 --- a/thermal/CoreComponent.h +++ b/thermal/CoreComponent.h @@ -1,96 +1,96 @@ -#ifndef MISSION_CONTROLLERS_TCS_CORECOMPONENT_H_ -#define MISSION_CONTROLLERS_TCS_CORECOMPONENT_H_ - -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/GlobalPoolVariable.h" -#include "../thermal/ThermalComponentIF.h" -#include "../thermal/AbstractTemperatureSensor.h" -#include "../thermal/ThermalModule.h" -#include "../thermal/ThermalMonitor.h" - -// TODO: Documentaiton, how to use this? only use Thermal Component, which inherits core component? -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, GlobDataSet *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; - - gp_float_t temperature; - gp_int8_t targetState; - gp_int8_t currentState; - gp_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_ */ +#ifndef MISSION_CONTROLLERS_TCS_CORECOMPONENT_H_ +#define MISSION_CONTROLLERS_TCS_CORECOMPONENT_H_ + +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/GlobalPoolVariable.h" +#include "../thermal/ThermalComponentIF.h" +#include "../thermal/AbstractTemperatureSensor.h" +#include "../thermal/ThermalModule.h" +#include "../thermal/ThermalMonitor.h" + +// TODO: Documentaiton, how to use this? only use Thermal Component, which inherits core component? +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, GlobDataSet *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; + + gp_float_t temperature; + gp_int8_t targetState; + gp_int8_t currentState; + gp_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/thermal/Heater.cpp b/thermal/Heater.cpp index 94dd91b5..2bee47f1 100644 --- a/thermal/Heater.cpp +++ b/thermal/Heater.cpp @@ -1,350 +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 command; - ReturnValue_t result = commandQueue->receiveMessage(&command); - if (result == HasReturnvaluesIF::RETURN_OK) { - result = healthHelper.handleHealthCommand(&command); - if (result == HasReturnvaluesIF::RETURN_OK) { - return; - } - parameterHelper.handleParameterMessage(&command); - } -} - -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; - } - } -} +#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 command; + ReturnValue_t result = commandQueue->receiveMessage(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + result = healthHelper.handleHealthCommand(&command); + if (result == HasReturnvaluesIF::RETURN_OK) { + return; + } + parameterHelper.handleParameterMessage(&command); + } +} + +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/thermal/Heater.h b/thermal/Heater.h index 8121a774..63fe2066 100644 --- a/thermal/Heater.h +++ b/thermal/Heater.h @@ -1,90 +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_ */ +#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/thermal/RedundantHeater.h b/thermal/RedundantHeater.h index 4a82de07..76537542 100644 --- a/thermal/RedundantHeater.h +++ b/thermal/RedundantHeater.h @@ -1,50 +1,50 @@ -#ifndef REDUNDANTHEATER_H_ -#define REDUNDANTHEATER_H_ - -#include "../thermal/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_ */ - +#ifndef REDUNDANTHEATER_H_ +#define REDUNDANTHEATER_H_ + +#include "../thermal/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/thermal/TemperatureSensor.h b/thermal/TemperatureSensor.h index cf2dffd4..9b4f0e60 100644 --- a/thermal/TemperatureSensor.h +++ b/thermal/TemperatureSensor.h @@ -1,222 +1,222 @@ -#ifndef TEMPERATURESENSOR_H_ -#define TEMPERATURESENSOR_H_ - -#include "../thermal/AbstractTemperatureSensor.h" -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/GlobalPoolVariable.h" -#include "../monitoring/LimitMonitor.h" - -/** - * @brief This building block handles non-linear value conversion and - * range checks for analog temperature sensors. - * @details This class can be used to perform all necessary tasks for temperature sensors. - * A sensor can be instantiated by calling the constructor. - * The temperature is calculated from an input value with - * the calculateOutputTemperature() function. Range checking and - * limit monitoring is performed automatically. - * The inputType specifies the type of the raw input while the - * limitType specifies the type of the upper and lower limit to check against. - * @ingroup thermal - */ - -template -class TemperatureSensor: public AbstractTemperatureSensor { -public: - /** - * This structure contains parameters required for range checking - * and the conversion from the input value to the output temperature. - * a, b and c can be any parameters required to calculate the output - * temperature from the input value, depending on the formula used. - * - * The parameters a,b and c are used in the calculateOutputTemperature() call. - * - * The lower and upper limits can be specified in any type, for example float for C values - * or any other type for raw values. - */ - struct Parameters { - float a; - float b; - float c; - limitType lowerLimit; - limitType upperLimit; - float maxGradient; - }; - - /** - * Forward declaration for explicit instantiation of used parameters. - */ - struct UsedParameters { - UsedParameters(Parameters parameters) : - a(parameters.a), b(parameters.b), c(parameters.c), - gradient(parameters.maxGradient) {} - float a; - float b; - float c; - float gradient; - }; - - /** - * Instantiate Temperature Sensor Object. - * @param setObjectid objectId of the sensor object - * @param inputValue Input value which is converted to a temperature - * @param poolVariable Pool Variable to store the temperature value - * @param vectorIndex Vector Index for the sensor monitor - * @param parameters Calculation parameters, temperature limits, gradient limit - * @param datapoolId Datapool ID of the output temperature - * @param outputSet Output dataset for the output temperature to fetch it with read() - * @param thermalModule respective thermal module, if it has one - */ - TemperatureSensor(object_id_t setObjectid, - inputType *inputValue, PoolVariableIF *poolVariable, - uint8_t vectorIndex, uint32_t datapoolId, Parameters parameters = {0, 0, 0, 0, 0, 0}, - GlobDataSet *outputSet = NULL, ThermalModuleIF *thermalModule = NULL) : - AbstractTemperatureSensor(setObjectid, thermalModule), parameters(parameters), - inputValue(inputValue), poolVariable(poolVariable), - outputTemperature(datapoolId, outputSet, PoolVariableIF::VAR_WRITE), - sensorMonitor(setObjectid, DOMAIN_ID_SENSOR, - GlobalDataPool::poolIdAndPositionToPid(poolVariable->getDataPoolId(), vectorIndex), - DEFAULT_CONFIRMATION_COUNT, parameters.lowerLimit, parameters.upperLimit, - TEMP_SENSOR_LOW, TEMP_SENSOR_HIGH), - oldTemperature(20), uptimeOfOldTemperature( { INVALID_TEMPERATURE, 0 }) { - } - - -protected: - /** - * This formula is used to calculate the temperature from an input value - * with an arbitrary type. - * A default implementation is provided but can be replaced depending - * on the required calculation. - * @param inputTemperature - * @return - */ - virtual float calculateOutputTemperature(inputType inputValue) { - return parameters.a * inputValue * inputValue - + parameters.b * inputValue + parameters.c; - } - - -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; - - inputType * inputValue; - - PoolVariableIF *poolVariable; - - gp_float_t outputTemperature; - - LimitMonitor sensorMonitor; - - float oldTemperature; - timeval uptimeOfOldTemperature; - - void doChildOperation() { - if (!poolVariable->isValid() - || !healthHelper.healthTable->isHealthy(getObjectId())) { - setInvalid(); - return; - } - - outputTemperature = calculateOutputTemperature(*inputValue); - 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 maxGradient 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(outputTemperature.value); - - if (sensorMonitor.isOutOfLimits()) { - uptimeOfOldTemperature.tv_sec = INVALID_UPTIME; - outputTemperature.setValid(PoolVariableIF::INVALID); - outputTemperature = INVALID_TEMPERATURE; - } else { - oldTemperature = outputTemperature; - uptimeOfOldTemperature = uptime; - } - } - -public: - float getTemperature() { - return outputTemperature; - } - - bool isValid() { - return outputTemperature.isValid(); - } - - 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; - - 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_ */ +#ifndef TEMPERATURESENSOR_H_ +#define TEMPERATURESENSOR_H_ + +#include "../thermal/AbstractTemperatureSensor.h" +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/GlobalPoolVariable.h" +#include "../monitoring/LimitMonitor.h" + +/** + * @brief This building block handles non-linear value conversion and + * range checks for analog temperature sensors. + * @details This class can be used to perform all necessary tasks for temperature sensors. + * A sensor can be instantiated by calling the constructor. + * The temperature is calculated from an input value with + * the calculateOutputTemperature() function. Range checking and + * limit monitoring is performed automatically. + * The inputType specifies the type of the raw input while the + * limitType specifies the type of the upper and lower limit to check against. + * @ingroup thermal + */ + +template +class TemperatureSensor: public AbstractTemperatureSensor { +public: + /** + * This structure contains parameters required for range checking + * and the conversion from the input value to the output temperature. + * a, b and c can be any parameters required to calculate the output + * temperature from the input value, depending on the formula used. + * + * The parameters a,b and c are used in the calculateOutputTemperature() call. + * + * The lower and upper limits can be specified in any type, for example float for C values + * or any other type for raw values. + */ + struct Parameters { + float a; + float b; + float c; + limitType lowerLimit; + limitType upperLimit; + float maxGradient; + }; + + /** + * Forward declaration for explicit instantiation of used parameters. + */ + struct UsedParameters { + UsedParameters(Parameters parameters) : + a(parameters.a), b(parameters.b), c(parameters.c), + gradient(parameters.maxGradient) {} + float a; + float b; + float c; + float gradient; + }; + + /** + * Instantiate Temperature Sensor Object. + * @param setObjectid objectId of the sensor object + * @param inputValue Input value which is converted to a temperature + * @param poolVariable Pool Variable to store the temperature value + * @param vectorIndex Vector Index for the sensor monitor + * @param parameters Calculation parameters, temperature limits, gradient limit + * @param datapoolId Datapool ID of the output temperature + * @param outputSet Output dataset for the output temperature to fetch it with read() + * @param thermalModule respective thermal module, if it has one + */ + TemperatureSensor(object_id_t setObjectid, + inputType *inputValue, PoolVariableIF *poolVariable, + uint8_t vectorIndex, uint32_t datapoolId, Parameters parameters = {0, 0, 0, 0, 0, 0}, + GlobDataSet *outputSet = NULL, ThermalModuleIF *thermalModule = NULL) : + AbstractTemperatureSensor(setObjectid, thermalModule), parameters(parameters), + inputValue(inputValue), poolVariable(poolVariable), + outputTemperature(datapoolId, outputSet, PoolVariableIF::VAR_WRITE), + sensorMonitor(setObjectid, DOMAIN_ID_SENSOR, + GlobalDataPool::poolIdAndPositionToPid(poolVariable->getDataPoolId(), vectorIndex), + DEFAULT_CONFIRMATION_COUNT, parameters.lowerLimit, parameters.upperLimit, + TEMP_SENSOR_LOW, TEMP_SENSOR_HIGH), + oldTemperature(20), uptimeOfOldTemperature( { INVALID_TEMPERATURE, 0 }) { + } + + +protected: + /** + * This formula is used to calculate the temperature from an input value + * with an arbitrary type. + * A default implementation is provided but can be replaced depending + * on the required calculation. + * @param inputTemperature + * @return + */ + virtual float calculateOutputTemperature(inputType inputValue) { + return parameters.a * inputValue * inputValue + + parameters.b * inputValue + parameters.c; + } + + +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; + + inputType * inputValue; + + PoolVariableIF *poolVariable; + + gp_float_t outputTemperature; + + LimitMonitor sensorMonitor; + + float oldTemperature; + timeval uptimeOfOldTemperature; + + void doChildOperation() { + if (!poolVariable->isValid() + || !healthHelper.healthTable->isHealthy(getObjectId())) { + setInvalid(); + return; + } + + outputTemperature = calculateOutputTemperature(*inputValue); + 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 maxGradient 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(outputTemperature.value); + + if (sensorMonitor.isOutOfLimits()) { + uptimeOfOldTemperature.tv_sec = INVALID_UPTIME; + outputTemperature.setValid(PoolVariableIF::INVALID); + outputTemperature = INVALID_TEMPERATURE; + } else { + oldTemperature = outputTemperature; + uptimeOfOldTemperature = uptime; + } + } + +public: + float getTemperature() { + return outputTemperature; + } + + bool isValid() { + return outputTemperature.isValid(); + } + + 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; + + 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/thermal/ThermalComponentIF.h b/thermal/ThermalComponentIF.h index f5f49ba1..522d4e44 100644 --- a/thermal/ThermalComponentIF.h +++ b/thermal/ThermalComponentIF.h @@ -1,114 +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_ */ +#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/thermal/ThermalModule.cpp b/thermal/ThermalModule.cpp index 7ee6b96b..fbd6939e 100644 --- a/thermal/ThermalModule.cpp +++ b/thermal/ThermalModule.cpp @@ -1,288 +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, - GlobDataSet *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, GlobDataSet* 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) { - GlobDataSet mySet; - gp_int8_t 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); - } -} +#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, + GlobDataSet *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, GlobDataSet* 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) { + GlobDataSet mySet; + gp_int8_t 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/thermal/ThermalModule.h b/thermal/ThermalModule.h index 46692640..41be6baa 100644 --- a/thermal/ThermalModule.h +++ b/thermal/ThermalModule.h @@ -1,95 +1,95 @@ -#ifndef THERMALMODULE_H_ -#define THERMALMODULE_H_ - -#include "../datapoolglob/GlobalDataSet.h" -#include "../datapoolglob/GlobalPoolVariable.h" -#include "../devicehandlers/HealthDevice.h" -#include "../events/EventReportingProxyIF.h" -#include "ThermalModuleIF.h" -#include -#include "tcsDefinitions.h" -#include "RedundantHeater.h" -class PowerSwitchIF; - -/** - * @brief Allows creation of different thermal control domains within a system. - */ -class ThermalModule: public ThermalModuleIF { - friend class ThermalController; -public: - struct Parameters { - float heaterOn; - float hysteresis; - }; - - ThermalModule(uint32_t moduleTemperaturePoolId, uint32_t currentStatePoolId, - uint32_t targetStatePoolId, GlobDataSet *dataSet, Parameters parameters, - RedundantHeater::Parameters heaterParameters); - - ThermalModule(uint32_t moduleTemperaturePoolId, GlobDataSet *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; - - gp_float_t moduleTemperature; - - RedundantHeater *heater; - - gp_int8_t currentState; - gp_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_ */ +#ifndef THERMALMODULE_H_ +#define THERMALMODULE_H_ + +#include "../datapoolglob/GlobalDataSet.h" +#include "../datapoolglob/GlobalPoolVariable.h" +#include "../devicehandlers/HealthDevice.h" +#include "../events/EventReportingProxyIF.h" +#include "ThermalModuleIF.h" +#include +#include "tcsDefinitions.h" +#include "RedundantHeater.h" +class PowerSwitchIF; + +/** + * @brief Allows creation of different thermal control domains within a system. + */ +class ThermalModule: public ThermalModuleIF { + friend class ThermalController; +public: + struct Parameters { + float heaterOn; + float hysteresis; + }; + + ThermalModule(uint32_t moduleTemperaturePoolId, uint32_t currentStatePoolId, + uint32_t targetStatePoolId, GlobDataSet *dataSet, Parameters parameters, + RedundantHeater::Parameters heaterParameters); + + ThermalModule(uint32_t moduleTemperaturePoolId, GlobDataSet *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; + + gp_float_t moduleTemperature; + + RedundantHeater *heater; + + gp_int8_t currentState; + gp_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/thermal/ThermalMonitor.cpp b/thermal/ThermalMonitor.cpp index af0413bf..fc0823c3 100644 --- a/thermal/ThermalMonitor.cpp +++ b/thermal/ThermalMonitor.cpp @@ -1,68 +1,68 @@ -#include "../thermal/ThermalMonitor.h" -#include "../thermal/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; - } -} +#include "../thermal/ThermalMonitor.h" +#include "../thermal/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/thermal/ThermalMonitor.h b/thermal/ThermalMonitor.h index 69528600..680c0cb0 100644 --- a/thermal/ThermalMonitor.h +++ b/thermal/ThermalMonitor.h @@ -1,26 +1,26 @@ -#ifndef FRAMEWORK_THERMAL_THERMALMONITOR_H_ -#define FRAMEWORK_THERMAL_THERMALMONITOR_H_ - -#include "../monitoring/MonitorReporter.h" -#include "../thermal/ThermalComponentIF.h" - -/** - * What does it do. How to use it. - */ -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_ */ +#ifndef FRAMEWORK_THERMAL_THERMALMONITOR_H_ +#define FRAMEWORK_THERMAL_THERMALMONITOR_H_ + +#include "../monitoring/MonitorReporter.h" +#include "../thermal/ThermalComponentIF.h" + +/** + * What does it do. How to use it. + */ +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/timemanager/CCSDSTime.cpp b/timemanager/CCSDSTime.cpp index 7124d8bd..130b3175 100644 --- a/timemanager/CCSDSTime.cpp +++ b/timemanager/CCSDSTime.cpp @@ -1,602 +1,602 @@ -#include "../timemanager/CCSDSTime.h" -#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. -#ifdef NEWLIB_NANO_NO_C99_IO - 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); - -} +#include "../timemanager/CCSDSTime.h" +#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. +#ifdef NEWLIB_NANO_NO_C99_IO + 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/timemanager/CCSDSTime.h b/timemanager/CCSDSTime.h index 6bb3a75c..b9efa49c 100644 --- a/timemanager/CCSDSTime.h +++ b/timemanager/CCSDSTime.h @@ -1,233 +1,233 @@ -#ifndef CCSDSTIME_H_ -#define CCSDSTIME_H_ - -// COULDDO: have calls in Clock.h which return time quality and use timespec accordingly - -#include "../timemanager/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_ */ +#ifndef CCSDSTIME_H_ +#define CCSDSTIME_H_ + +// COULDDO: have calls in Clock.h which return time quality and use timespec accordingly + +#include "../timemanager/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/timemanager/Clock.h b/timemanager/Clock.h index c7436252..9463b5a6 100644 --- a/timemanager/Clock.h +++ b/timemanager/Clock.h @@ -1,155 +1,155 @@ -#ifndef FRAMEWORK_TIMEMANAGER_CLOCK_H_ -#define FRAMEWORK_TIMEMANAGER_CLOCK_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../ipc/MutexFactory.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; -using dur_seconds_t = double; - -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_ */ +#ifndef FRAMEWORK_TIMEMANAGER_CLOCK_H_ +#define FRAMEWORK_TIMEMANAGER_CLOCK_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../ipc/MutexFactory.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; +using dur_seconds_t = double; + +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/timemanager/Countdown.cpp b/timemanager/Countdown.cpp index 5b019f2b..95cce029 100644 --- a/timemanager/Countdown.cpp +++ b/timemanager/Countdown.cpp @@ -1,45 +1,45 @@ -/** - * @file Countdown.cpp - * @brief This file defines the Countdown class. - * @date 21.03.2013 - * @author baetz - */ - - -#include "../timemanager/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; -} +/** + * @file Countdown.cpp + * @brief This file defines the Countdown class. + * @date 21.03.2013 + * @author baetz + */ + + +#include "../timemanager/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/timemanager/Countdown.h b/timemanager/Countdown.h index 741ab0f8..20380e71 100644 --- a/timemanager/Countdown.h +++ b/timemanager/Countdown.h @@ -1,31 +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 "../timemanager/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_ */ +/** + * @file Countdown.h + * @brief This file defines the Countdown class. + * @date 21.03.2013 + * @author baetz + */ + +#ifndef COUNTDOWN_H_ +#define COUNTDOWN_H_ + +#include "../timemanager/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/timemanager/ReceivesTimeInfoIF.h b/timemanager/ReceivesTimeInfoIF.h index 09915e83..680e94f2 100644 --- a/timemanager/ReceivesTimeInfoIF.h +++ b/timemanager/ReceivesTimeInfoIF.h @@ -1,32 +1,32 @@ -/** - * @file ReceivesTimeInfoIF.h - * @brief This file defines the ReceivesTimeInfoIF class. - * @date 26.02.2013 - * @author baetz - */ - -#ifndef RECEIVESTIMEINFOIF_H_ -#define RECEIVESTIMEINFOIF_H_ -#include "../ipc/MessageQueueSenderIF.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_ */ +/** + * @file ReceivesTimeInfoIF.h + * @brief This file defines the ReceivesTimeInfoIF class. + * @date 26.02.2013 + * @author baetz + */ + +#ifndef RECEIVESTIMEINFOIF_H_ +#define RECEIVESTIMEINFOIF_H_ +#include "../ipc/MessageQueueSenderIF.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/timemanager/Stopwatch.cpp b/timemanager/Stopwatch.cpp index bf69d3ce..637c5078 100644 --- a/timemanager/Stopwatch.cpp +++ b/timemanager/Stopwatch.cpp @@ -1,57 +1,57 @@ -#include "../timemanager/Stopwatch.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include - -Stopwatch::Stopwatch(bool displayOnDestruction, - StopwatchDisplayMode displayMode): displayOnDestruction( - displayOnDestruction), displayMode(displayMode) { - // Measures start time on initialization. - Clock::getClock_timeval(&startTime); -} - -void Stopwatch::start() { - Clock::getClock_timeval(&startTime); -} - -dur_millis_t Stopwatch::stop() { - stopInternal(); - return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000; -} - -dur_seconds_t 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::getClock_timeval(&endTime); - elapsedTime = endTime - startTime; -} +#include "../timemanager/Stopwatch.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include + +Stopwatch::Stopwatch(bool displayOnDestruction, + StopwatchDisplayMode displayMode): displayOnDestruction( + displayOnDestruction), displayMode(displayMode) { + // Measures start time on initialization. + Clock::getClock_timeval(&startTime); +} + +void Stopwatch::start() { + Clock::getClock_timeval(&startTime); +} + +dur_millis_t Stopwatch::stop() { + stopInternal(); + return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000; +} + +dur_seconds_t 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::getClock_timeval(&endTime); + elapsedTime = endTime - startTime; +} diff --git a/timemanager/Stopwatch.h b/timemanager/Stopwatch.h index 30735b2c..191929dc 100644 --- a/timemanager/Stopwatch.h +++ b/timemanager/Stopwatch.h @@ -1,71 +1,71 @@ -#ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ -#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ -#include "../timemanager/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(); - /** - * Calculates the elapsed time since start and returns it - * @return elapsed time in seconds (double precision) - */ - dur_seconds_t 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 /* FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ */ +#ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ +#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ +#include "../timemanager/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(); + /** + * Calculates the elapsed time since start and returns it + * @return elapsed time in seconds (double precision) + */ + dur_seconds_t 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 /* FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ */ diff --git a/timemanager/TimeMessage.cpp b/timemanager/TimeMessage.cpp index a60718a4..6e8dcd60 100644 --- a/timemanager/TimeMessage.cpp +++ b/timemanager/TimeMessage.cpp @@ -1,37 +1,37 @@ -/** - * @file TimeMessage.cpp - * @brief This file defines the TimeMessage class. - * @date 26.02.2013 - * @author baetz - */ - -#include "../timemanager/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; -} +/** + * @file TimeMessage.cpp + * @brief This file defines the TimeMessage class. + * @date 26.02.2013 + * @author baetz + */ + +#include "../timemanager/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/timemanager/TimeMessage.h b/timemanager/TimeMessage.h index 1e440e5d..bb83708c 100644 --- a/timemanager/TimeMessage.h +++ b/timemanager/TimeMessage.h @@ -1,56 +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 "../timemanager/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_ */ +/** + * @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 "../timemanager/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/timemanager/TimeStamperIF.h b/timemanager/TimeStamperIF.h index 49e0ec18..bdc5e7e3 100644 --- a/timemanager/TimeStamperIF.h +++ b/timemanager/TimeStamperIF.h @@ -1,24 +1,24 @@ -#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); - - static const uint8_t MISSION_TIMESTAMP_SIZE = 8; //!< This is a mission-specific constant and determines the total size reserved for timestamps. - virtual ReturnValue_t addTimeStamp(uint8_t* buffer, const uint8_t maxSize) = 0; - virtual ~TimeStamperIF() {} -}; - - - -#endif /* FRAMEWORK_TIMEMANAGER_TIMESTAMPERIF_H_ */ +#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); + + static const uint8_t MISSION_TIMESTAMP_SIZE = 8; //!< This is a mission-specific constant and determines the total size reserved for timestamps. + virtual ReturnValue_t addTimeStamp(uint8_t* buffer, const uint8_t maxSize) = 0; + virtual ~TimeStamperIF() {} +}; + + + +#endif /* FRAMEWORK_TIMEMANAGER_TIMESTAMPERIF_H_ */ diff --git a/tmstorage/TmStoreBackendIF.h b/tmstorage/TmStoreBackendIF.h index 0e20a314..bfabdd31 100644 --- a/tmstorage/TmStoreBackendIF.h +++ b/tmstorage/TmStoreBackendIF.h @@ -1,95 +1,95 @@ -#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() {} - - /** - * What do I need to implement here ? - * @param opCode - * @return - */ - virtual ReturnValue_t performOperation(uint8_t opCode) = 0; - virtual ReturnValue_t initialize() = 0; - - /** - * Implement the storage of TM packets to mass memory - * @param tmPacket - * @return - */ - 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; - - /** - * TODO: Adapt for file management system? - * @param startAddress - * @param endAddress - * @return - */ - 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_ */ +#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() {} + + /** + * What do I need to implement here ? + * @param opCode + * @return + */ + virtual ReturnValue_t performOperation(uint8_t opCode) = 0; + virtual ReturnValue_t initialize() = 0; + + /** + * Implement the storage of TM packets to mass memory + * @param tmPacket + * @return + */ + 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; + + /** + * TODO: Adapt for file management system? + * @param startAddress + * @param endAddress + * @return + */ + 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/tmstorage/TmStoreFrontendIF.h b/tmstorage/TmStoreFrontendIF.h index 7a2b3c9e..e0a7e078 100644 --- a/tmstorage/TmStoreFrontendIF.h +++ b/tmstorage/TmStoreFrontendIF.h @@ -1,63 +1,63 @@ -#ifndef PLATFORM_TMTCSERVICES_TMSTOREFRONTENDIF_H_ -#define PLATFORM_TMTCSERVICES_TMSTOREFRONTENDIF_H_ - -#include "../returnvalues/HasReturnvaluesIF.h" -#include "../tmstorage/TmStorePackets.h" -#include "../ipc/MessageQueueSenderIF.h" -class TmPacketMinimal; -class SpacePacketBase; -class TmStoreBackendIF; - -class TmStoreFrontendIF { -public: - virtual TmStoreBackendIF* getBackend() const = 0; - - /** - * What do I need to implement here? - * This is propably used by PUS Service 15 so we should propably check for messages.. - * Provide base implementation? - * @param opCode - * @return - */ - 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_ */ +#ifndef PLATFORM_TMTCSERVICES_TMSTOREFRONTENDIF_H_ +#define PLATFORM_TMTCSERVICES_TMSTOREFRONTENDIF_H_ + +#include "../returnvalues/HasReturnvaluesIF.h" +#include "../tmstorage/TmStorePackets.h" +#include "../ipc/MessageQueueSenderIF.h" +class TmPacketMinimal; +class SpacePacketBase; +class TmStoreBackendIF; + +class TmStoreFrontendIF { +public: + virtual TmStoreBackendIF* getBackend() const = 0; + + /** + * What do I need to implement here? + * This is propably used by PUS Service 15 so we should propably check for messages.. + * Provide base implementation? + * @param opCode + * @return + */ + 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/tmstorage/TmStoreMessage.cpp b/tmstorage/TmStoreMessage.cpp index 0436442b..753ac1b7 100644 --- a/tmstorage/TmStoreMessage.cpp +++ b/tmstorage/TmStoreMessage.cpp @@ -1,165 +1,165 @@ -#include "../objectmanager/ObjectManagerIF.h" -#include "../tmstorage/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(CommandMessage::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); -} +#include "../objectmanager/ObjectManagerIF.h" +#include "../tmstorage/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(CommandMessage::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/tmstorage/TmStoreMessage.h b/tmstorage/TmStoreMessage.h index ce10519b..59354a20 100644 --- a/tmstorage/TmStoreMessage.h +++ b/tmstorage/TmStoreMessage.h @@ -1,63 +1,63 @@ -#ifndef FRAMEWORK_TMSTORAGE_TMSTOREMESSAGE_H_ -#define FRAMEWORK_TMSTORAGE_TMSTOREMESSAGE_H_ - -#include "../ipc/CommandMessage.h" -#include "../storagemanager/StorageManagerIF.h" -#include "../tmstorage/TmStorePackets.h" -#include "../objectmanager/SystemObjectIF.h" -class TmStoreMessage { -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_ */ +#ifndef FRAMEWORK_TMSTORAGE_TMSTOREMESSAGE_H_ +#define FRAMEWORK_TMSTORAGE_TMSTOREMESSAGE_H_ + +#include "../ipc/CommandMessage.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tmstorage/TmStorePackets.h" +#include "../objectmanager/SystemObjectIF.h" +class TmStoreMessage { +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/tmstorage/TmStorePackets.h b/tmstorage/TmStorePackets.h index 3c8e1a24..4ce64fe6 100644 --- a/tmstorage/TmStorePackets.h +++ b/tmstorage/TmStorePackets.h @@ -1,300 +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_ */ +#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/tmtcpacket/SpacePacket.cpp b/tmtcpacket/SpacePacket.cpp index c04e143f..2bf6e3dc 100644 --- a/tmtcpacket/SpacePacket.cpp +++ b/tmtcpacket/SpacePacket.cpp @@ -1,27 +1,27 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../tmtcpacket/ccsds_header.h" -#include "../tmtcpacket/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; - } -} +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tmtcpacket/ccsds_header.h" +#include "../tmtcpacket/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/tmtcpacket/SpacePacket.h b/tmtcpacket/SpacePacket.h index a75b9ae3..ea1d0468 100644 --- a/tmtcpacket/SpacePacket.h +++ b/tmtcpacket/SpacePacket.h @@ -1,70 +1,70 @@ -#ifndef SPACEPACKET_H_ -#define SPACEPACKET_H_ - -#include "../tmtcpacket/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_ */ +#ifndef SPACEPACKET_H_ +#define SPACEPACKET_H_ + +#include "../tmtcpacket/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/tmtcpacket/SpacePacketBase.cpp b/tmtcpacket/SpacePacketBase.cpp index 3d3a51e3..044ce510 100644 --- a/tmtcpacket/SpacePacketBase.cpp +++ b/tmtcpacket/SpacePacketBase.cpp @@ -1,104 +1,104 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../tmtcpacket/SpacePacketBase.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); -} +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tmtcpacket/SpacePacketBase.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/tmtcpacket/SpacePacketBase.h b/tmtcpacket/SpacePacketBase.h index a132bfd1..c7a63ae6 100644 --- a/tmtcpacket/SpacePacketBase.h +++ b/tmtcpacket/SpacePacketBase.h @@ -1,177 +1,177 @@ -#ifndef SPACEPACKETBASE_H_ -#define SPACEPACKETBASE_H_ - -#include "../tmtcpacket/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 /* SPACEPACKETBASE_H_ */ +#ifndef SPACEPACKETBASE_H_ +#define SPACEPACKETBASE_H_ + +#include "../tmtcpacket/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 /* SPACEPACKETBASE_H_ */ diff --git a/tmtcpacket/packetmatcher/ApidMatcher.h b/tmtcpacket/packetmatcher/ApidMatcher.h index 7b1b89ff..4f196ac9 100644 --- a/tmtcpacket/packetmatcher/ApidMatcher.h +++ b/tmtcpacket/packetmatcher/ApidMatcher.h @@ -1,40 +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_ */ +#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/tmtcpacket/packetmatcher/PacketMatchTree.cpp b/tmtcpacket/packetmatcher/PacketMatchTree.cpp index 356467dc..610829e5 100644 --- a/tmtcpacket/packetmatcher/PacketMatchTree.cpp +++ b/tmtcpacket/packetmatcher/PacketMatchTree.cpp @@ -1,195 +1,195 @@ -#include "../../tmtcpacket/packetmatcher/ApidMatcher.h" -#include "../../tmtcpacket/packetmatcher/PacketMatchTree.h" -#include "../../tmtcpacket/packetmatcher/ServiceMatcher.h" -#include "../../tmtcpacket/packetmatcher/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); -} +#include "../../tmtcpacket/packetmatcher/ApidMatcher.h" +#include "../../tmtcpacket/packetmatcher/PacketMatchTree.h" +#include "../../tmtcpacket/packetmatcher/ServiceMatcher.h" +#include "../../tmtcpacket/packetmatcher/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/tmtcpacket/packetmatcher/PacketMatchTree.h b/tmtcpacket/packetmatcher/PacketMatchTree.h index 0d246d21..86fb087e 100644 --- a/tmtcpacket/packetmatcher/PacketMatchTree.h +++ b/tmtcpacket/packetmatcher/PacketMatchTree.h @@ -1,36 +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_ */ - +#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/tmtcpacket/packetmatcher/ServiceMatcher.h b/tmtcpacket/packetmatcher/ServiceMatcher.h index e22288f4..eba23d75 100644 --- a/tmtcpacket/packetmatcher/ServiceMatcher.h +++ b/tmtcpacket/packetmatcher/ServiceMatcher.h @@ -1,39 +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_ */ +#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/tmtcpacket/packetmatcher/SubserviceMatcher.h b/tmtcpacket/packetmatcher/SubserviceMatcher.h index 1b173b1a..a9b6def8 100644 --- a/tmtcpacket/packetmatcher/SubserviceMatcher.h +++ b/tmtcpacket/packetmatcher/SubserviceMatcher.h @@ -1,40 +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_ */ +#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/tmtcpacket/pus/PacketTimestampInterpreterIF.h b/tmtcpacket/pus/PacketTimestampInterpreterIF.h index 9e442ed6..dd0c0328 100644 --- a/tmtcpacket/pus/PacketTimestampInterpreterIF.h +++ b/tmtcpacket/pus/PacketTimestampInterpreterIF.h @@ -1,16 +1,16 @@ -#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_ */ +#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/tmtcpacket/pus/TcPacketBase.cpp b/tmtcpacket/pus/TcPacketBase.cpp index 3dc15872..b62db535 100644 --- a/tmtcpacket/pus/TcPacketBase.cpp +++ b/tmtcpacket/pus/TcPacketBase.cpp @@ -1,93 +1,93 @@ -#include "../../globalfunctions/CRC.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../tmtcpacket/pus/TcPacketBase.h" -#include - -TcPacketBase::TcPacketBase(const uint8_t* set_data) : - SpacePacketBase(set_data) { - tcData = (TcPacketPointer*) set_data; -} - -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; -} - -void TcPacketBase::setAppData(uint8_t * appData, uint16_t dataLen) { - memcpy(&tcData->appData, appData, dataLen); - SpacePacketBase::setPacketDataLength(dataLen + - sizeof(PUSTcDataFieldHeader) + TcPacketBase::CRC_SIZE - 1); -} - -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() { - uint8_t * wholeData = getWholeData(); - sif::debug << "TcPacket contains: " << std::endl; - for (uint8_t count = 0; count < getFullSize(); ++count) { - sif::debug << std::hex << (uint16_t) wholeData[count] << " "; - } - sif::debug << std::dec << std::endl; -} - -void TcPacketBase::initializeTcPacket(uint16_t apid, uint16_t sequenceCount, - uint8_t ack, uint8_t service, uint8_t subservice) { - initSpacePacketHeader(true, true, apid, sequenceCount); - memset(&tcData->dataField, 0, sizeof(tcData->dataField)); - setPacketDataLength(sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1); - //Data Field Header: - //Set CCSDS_secondary_header_flag to 0, version number to 001 and ack to 0000 - 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; -} +#include "../../globalfunctions/CRC.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tmtcpacket/pus/TcPacketBase.h" +#include + +TcPacketBase::TcPacketBase(const uint8_t* set_data) : + SpacePacketBase(set_data) { + tcData = (TcPacketPointer*) set_data; +} + +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; +} + +void TcPacketBase::setAppData(uint8_t * appData, uint16_t dataLen) { + memcpy(&tcData->appData, appData, dataLen); + SpacePacketBase::setPacketDataLength(dataLen + + sizeof(PUSTcDataFieldHeader) + TcPacketBase::CRC_SIZE - 1); +} + +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() { + uint8_t * wholeData = getWholeData(); + sif::debug << "TcPacket contains: " << std::endl; + for (uint8_t count = 0; count < getFullSize(); ++count) { + sif::debug << std::hex << (uint16_t) wholeData[count] << " "; + } + sif::debug << std::dec << std::endl; +} + +void TcPacketBase::initializeTcPacket(uint16_t apid, uint16_t sequenceCount, + uint8_t ack, uint8_t service, uint8_t subservice) { + initSpacePacketHeader(true, true, apid, sequenceCount); + memset(&tcData->dataField, 0, sizeof(tcData->dataField)); + setPacketDataLength(sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1); + //Data Field Header: + //Set CCSDS_secondary_header_flag to 0, version number to 001 and ack to 0000 + 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/tmtcpacket/pus/TcPacketBase.h b/tmtcpacket/pus/TcPacketBase.h index 1cfe233b..b3ef5fe7 100644 --- a/tmtcpacket/pus/TcPacketBase.h +++ b/tmtcpacket/pus/TcPacketBase.h @@ -1,204 +1,204 @@ -#ifndef TCPACKETBASE_H_ -#define 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 { -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; -public: - static const uint16_t TC_PACKET_MIN_SIZE = (sizeof(CCSDSPrimaryHeader) + sizeof(PUSTcDataFieldHeader) + 2); - /** - * With this constant for the acknowledge field responses on all levels are expected. - */ - static const uint8_t ACK_ALL = 0b1111; - /** - * With this constant for the acknowledge field a response on acceptance is expected. - */ - static const uint8_t ACK_ACCEPTANCE = 0b0001; - /** - * With this constant for the acknowledge field a response on start of execution is expected. - */ - static const uint8_t ACK_START = 0b0010; - /** - * With this constant for the acknowledge field responses on execution steps are expected. - */ - static const uint8_t ACK_STEP = 0b0100; - /** - * With this constant for the acknowledge field a response on completion is expected. - */ - static const uint8_t ACK_COMPLETION = 0b1000; - /** - * With this constant for the acknowledge field no responses are expected. - */ - static const uint8_t ACK_NONE = 0b000; - /** - * 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. - */ - TcPacketBase( const uint8_t* set_data ); - /** - * This is the empty default destructor. - */ - virtual ~TcPacketBase(); - /** - * Initializes the Tc Packet header. - * @param apid APID used. - * @param service PUS Service - * @param subservice PUS Subservice - * @param packetSubcounter Additional subcounter used. - */ - /** - * 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); - /** - * 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(); - - /** - * Copies the supplied data to the internal TC application data field. - * @param pData - * @param dataLen - */ - void setAppData(uint8_t * appData, uint16_t dataLen); - - /** - * 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 ); - - /** - * 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); -}; - - -#endif /* TCPACKETBASE_H_ */ +#ifndef TCPACKETBASE_H_ +#define 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 { +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; +public: + static const uint16_t TC_PACKET_MIN_SIZE = (sizeof(CCSDSPrimaryHeader) + sizeof(PUSTcDataFieldHeader) + 2); + /** + * With this constant for the acknowledge field responses on all levels are expected. + */ + static const uint8_t ACK_ALL = 0b1111; + /** + * With this constant for the acknowledge field a response on acceptance is expected. + */ + static const uint8_t ACK_ACCEPTANCE = 0b0001; + /** + * With this constant for the acknowledge field a response on start of execution is expected. + */ + static const uint8_t ACK_START = 0b0010; + /** + * With this constant for the acknowledge field responses on execution steps are expected. + */ + static const uint8_t ACK_STEP = 0b0100; + /** + * With this constant for the acknowledge field a response on completion is expected. + */ + static const uint8_t ACK_COMPLETION = 0b1000; + /** + * With this constant for the acknowledge field no responses are expected. + */ + static const uint8_t ACK_NONE = 0b000; + /** + * 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. + */ + TcPacketBase( const uint8_t* set_data ); + /** + * This is the empty default destructor. + */ + virtual ~TcPacketBase(); + /** + * Initializes the Tc Packet header. + * @param apid APID used. + * @param service PUS Service + * @param subservice PUS Subservice + * @param packetSubcounter Additional subcounter used. + */ + /** + * 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); + /** + * 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(); + + /** + * Copies the supplied data to the internal TC application data field. + * @param pData + * @param dataLen + */ + void setAppData(uint8_t * appData, uint16_t dataLen); + + /** + * 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 ); + + /** + * 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); +}; + + +#endif /* TCPACKETBASE_H_ */ diff --git a/tmtcpacket/pus/TcPacketStored.cpp b/tmtcpacket/pus/TcPacketStored.cpp index 64b369ea..d3474569 100644 --- a/tmtcpacket/pus/TcPacketStored.cpp +++ b/tmtcpacket/pus/TcPacketStored.cpp @@ -1,118 +1,118 @@ -#include "../../objectmanager/ObjectManagerIF.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../tmtcpacket/pus/TcPacketStored.h" - -#include - -TcPacketStored::TcPacketStored(store_address_t setAddress) : - TcPacketBase(nullptr), storeAddress(setAddress) { - this->setStoreAddress(this->storeAddress); -} - -TcPacketStored::TcPacketStored(uint8_t service, uint8_t subservice, - uint16_t apid, uint8_t sequence_count, const uint8_t* data, - size_t size, uint8_t ack ) : - TcPacketBase(nullptr) { - this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; - if (!this->checkAndSetStore()) { - return; - } - uint8_t* p_data = nullptr; - ReturnValue_t returnValue = this->store->getFreeElement(&this->storeAddress, - (TC_PACKET_MIN_SIZE + size), &p_data); - if (returnValue != HasReturnvaluesIF::RETURN_OK) { - sif::warning << "TcPacketStored::TcPacketStored: Could not get " - "free element from store!" << std::endl; - return; - } - this->setData(p_data); - initializeTcPacket(apid, sequence_count, 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(NULL) { - 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( NULL); - 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* temp_data = NULL; - size_t temp_size; - ReturnValue_t status = StorageManagerIF::RETURN_FAILED; - if (this->checkAndSetStore()) { - status = this->store->getData(this->storeAddress, &temp_data, - &temp_size); - } - if (status == StorageManagerIF::RETURN_OK) { - this->setData(temp_data); - } else { - this->setData(NULL); - this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; - } -} - -store_address_t TcPacketStored::getStoreAddress() { - return this->storeAddress; -} - -bool TcPacketStored::isSizeCorrect() { - const uint8_t* temp_data = NULL; - 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; -} - -StorageManagerIF* TcPacketStored::store = NULL; - -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(NULL); - } - } -} +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tmtcpacket/pus/TcPacketStored.h" + +#include + +TcPacketStored::TcPacketStored(store_address_t setAddress) : + TcPacketBase(nullptr), storeAddress(setAddress) { + this->setStoreAddress(this->storeAddress); +} + +TcPacketStored::TcPacketStored(uint8_t service, uint8_t subservice, + uint16_t apid, uint8_t sequence_count, const uint8_t* data, + size_t size, uint8_t ack ) : + TcPacketBase(nullptr) { + this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + if (!this->checkAndSetStore()) { + return; + } + uint8_t* p_data = nullptr; + ReturnValue_t returnValue = this->store->getFreeElement(&this->storeAddress, + (TC_PACKET_MIN_SIZE + size), &p_data); + if (returnValue != HasReturnvaluesIF::RETURN_OK) { + sif::warning << "TcPacketStored::TcPacketStored: Could not get " + "free element from store!" << std::endl; + return; + } + this->setData(p_data); + initializeTcPacket(apid, sequence_count, 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(NULL) { + 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( NULL); + 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* temp_data = NULL; + size_t temp_size; + ReturnValue_t status = StorageManagerIF::RETURN_FAILED; + if (this->checkAndSetStore()) { + status = this->store->getData(this->storeAddress, &temp_data, + &temp_size); + } + if (status == StorageManagerIF::RETURN_OK) { + this->setData(temp_data); + } else { + this->setData(NULL); + this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + } +} + +store_address_t TcPacketStored::getStoreAddress() { + return this->storeAddress; +} + +bool TcPacketStored::isSizeCorrect() { + const uint8_t* temp_data = NULL; + 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; +} + +StorageManagerIF* TcPacketStored::store = NULL; + +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(NULL); + } + } +} diff --git a/tmtcpacket/pus/TcPacketStored.h b/tmtcpacket/pus/TcPacketStored.h index 90cf2fba..c81b71b7 100644 --- a/tmtcpacket/pus/TcPacketStored.h +++ b/tmtcpacket/pus/TcPacketStored.h @@ -1,110 +1,110 @@ -#ifndef TCPACKETSTORED_H_ -#define TCPACKETSTORED_H_ - -#include "../../storagemanager/StorageManagerIF.h" -#include "../../tmtcpacket/pus/TcPacketBase.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 { -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(); -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 ack Set's the packet's Ack field, - * which specifies number and size of verification packets returned - * for this command. - * @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. - */ - TcPacketStored( uint8_t service, uint8_t subservice, uint16_t apid, - 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); - - 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(); -}; - - -#endif /* TCPACKETSTORED_H_ */ +#ifndef TCPACKETSTORED_H_ +#define TCPACKETSTORED_H_ + +#include "../../storagemanager/StorageManagerIF.h" +#include "../../tmtcpacket/pus/TcPacketBase.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 { +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(); +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 ack Set's the packet's Ack field, + * which specifies number and size of verification packets returned + * for this command. + * @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. + */ + TcPacketStored( uint8_t service, uint8_t subservice, uint16_t apid, + 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); + + 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(); +}; + + +#endif /* TCPACKETSTORED_H_ */ diff --git a/tmtcpacket/pus/TmPacketBase.cpp b/tmtcpacket/pus/TmPacketBase.cpp index 630471cc..66d67600 100644 --- a/tmtcpacket/pus/TmPacketBase.cpp +++ b/tmtcpacket/pus/TmPacketBase.cpp @@ -1,119 +1,119 @@ -#include "../../globalfunctions/CRC.h" -#include "../../objectmanager/ObjectManagerIF.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../tmtcpacket/pus/TmPacketBase.h" -#include "../../timemanager/CCSDSTime.h" -#include - -TmPacketBase::TmPacketBase(uint8_t* set_data) : - SpacePacketBase(set_data) { - tm_data = (TmPacketPointer*) set_data; -} - -TmPacketBase::~TmPacketBase() { - //Nothing to do. -} - -uint8_t TmPacketBase::getService() { - return tm_data->data_field.service_type; -} - -uint8_t TmPacketBase::getSubService() { - return tm_data->data_field.service_subtype; -} - -uint8_t* TmPacketBase::getSourceData() { - return &tm_data->data; -} - -uint16_t TmPacketBase::getSourceDataSize() { - return getPacketDataLength() - sizeof(tm_data->data_field) - - CRC_SIZE + 1; -} - -uint16_t TmPacketBase::getErrorControl() { - uint32_t size = getSourceDataSize() + CRC_SIZE; - uint8_t* p_to_buffer = &tm_data->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); - tm_data = (TmPacketPointer*) p_Data; -} - -void TmPacketBase::print() { - /*uint8_t * wholeData = getWholeData(); - debug << "TmPacket contains: " << std::endl; - for (uint8_t count = 0; count < getFullSize(); ++count ) { - debug << std::hex << (uint16_t)wholeData[count] << " "; - } - debug << std::dec << std::endl;*/ -} - -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, tm_data->data_field.time, - &tempSize, sizeof(tm_data->data_field.time)); -} - -uint8_t* TmPacketBase::getPacketTimeRaw() const{ - return tm_data->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(&tm_data->data_field, 0, sizeof(tm_data->data_field)); - //Set CCSDS_secondary header flag to 0, version number to 001 and ack to 0000 - // 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 - tm_data->data_field.version_type_ack = 0b00010000; - tm_data->data_field.service_type = service; - tm_data->data_field.service_subtype = subservice; - tm_data->data_field.subcounter = packetSubcounter; - //Timestamp packet - if (checkAndSetStamper()) { - timeStamper->addTimeStamp(tm_data->data_field.time, sizeof(tm_data->data_field.time)); - } -} - -void TmPacketBase::setSourceData(uint8_t* sourceData, size_t sourceSize) { - memcpy(getSourceData(), sourceData, sourceSize); - setSourceDataSize(sourceSize); -} - -void TmPacketBase::setSourceDataSize(uint16_t size) { - setPacketDataLength(size + sizeof(PUSTmDataFieldHeader) + CRC_SIZE - 1); -} - -uint32_t TmPacketBase::getTimestampSize() const { - return sizeof(tm_data->data_field.time); -} - -TimeStamperIF* TmPacketBase::timeStamper = NULL; -object_id_t TmPacketBase::timeStamperId = 0; +#include "../../globalfunctions/CRC.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tmtcpacket/pus/TmPacketBase.h" +#include "../../timemanager/CCSDSTime.h" +#include + +TmPacketBase::TmPacketBase(uint8_t* set_data) : + SpacePacketBase(set_data) { + tm_data = (TmPacketPointer*) set_data; +} + +TmPacketBase::~TmPacketBase() { + //Nothing to do. +} + +uint8_t TmPacketBase::getService() { + return tm_data->data_field.service_type; +} + +uint8_t TmPacketBase::getSubService() { + return tm_data->data_field.service_subtype; +} + +uint8_t* TmPacketBase::getSourceData() { + return &tm_data->data; +} + +uint16_t TmPacketBase::getSourceDataSize() { + return getPacketDataLength() - sizeof(tm_data->data_field) + - CRC_SIZE + 1; +} + +uint16_t TmPacketBase::getErrorControl() { + uint32_t size = getSourceDataSize() + CRC_SIZE; + uint8_t* p_to_buffer = &tm_data->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); + tm_data = (TmPacketPointer*) p_Data; +} + +void TmPacketBase::print() { + /*uint8_t * wholeData = getWholeData(); + debug << "TmPacket contains: " << std::endl; + for (uint8_t count = 0; count < getFullSize(); ++count ) { + debug << std::hex << (uint16_t)wholeData[count] << " "; + } + debug << std::dec << std::endl;*/ +} + +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, tm_data->data_field.time, + &tempSize, sizeof(tm_data->data_field.time)); +} + +uint8_t* TmPacketBase::getPacketTimeRaw() const{ + return tm_data->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(&tm_data->data_field, 0, sizeof(tm_data->data_field)); + //Set CCSDS_secondary header flag to 0, version number to 001 and ack to 0000 + // 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 + tm_data->data_field.version_type_ack = 0b00010000; + tm_data->data_field.service_type = service; + tm_data->data_field.service_subtype = subservice; + tm_data->data_field.subcounter = packetSubcounter; + //Timestamp packet + if (checkAndSetStamper()) { + timeStamper->addTimeStamp(tm_data->data_field.time, sizeof(tm_data->data_field.time)); + } +} + +void TmPacketBase::setSourceData(uint8_t* sourceData, size_t sourceSize) { + memcpy(getSourceData(), sourceData, sourceSize); + setSourceDataSize(sourceSize); +} + +void TmPacketBase::setSourceDataSize(uint16_t size) { + setPacketDataLength(size + sizeof(PUSTmDataFieldHeader) + CRC_SIZE - 1); +} + +uint32_t TmPacketBase::getTimestampSize() const { + return sizeof(tm_data->data_field.time); +} + +TimeStamperIF* TmPacketBase::timeStamper = NULL; +object_id_t TmPacketBase::timeStamperId = 0; diff --git a/tmtcpacket/pus/TmPacketBase.h b/tmtcpacket/pus/TmPacketBase.h index acd08d72..96dbd5c6 100644 --- a/tmtcpacket/pus/TmPacketBase.h +++ b/tmtcpacket/pus/TmPacketBase.h @@ -1,186 +1,186 @@ -#ifndef TMPACKETBASE_H_ -#define TMPACKETBASE_H_ - -#include "../../timemanager/TimeStamperIF.h" -#include "../../tmtcpacket/SpacePacketBase.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 [TBD]. - * @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); //!< Minimum size of a valid PUS Telemetry Packet. - static const uint32_t MISSION_TM_PACKET_MAX_SIZE = 2048; //!< Maximum size of a TM Packet in this mission. - static const uint8_t VERSION_NUMBER_BYTE_PUS_A = 0b00010000; //!< First byte of secondary header for PUS-A packets. - /** - * 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* set_data ); - /** - * This is the empty default destructor. - */ - virtual ~TmPacketBase(); - /** - * 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); - /** - * 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(); - - /** - * 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); - /** - * 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(); - /** - * With this method, the Error Control Field is updated to match the - * current content of the packet. - */ - void setErrorControl(); - /** - * This sets the source data. It copies the provided data to - * the internal TmPacketPointer source data location. - * @param sourceData - * @param sourceSize - */ - void setSourceData(uint8_t* sourceData, size_t sourceSize); - /** - * 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* p_Data ); - /** - * 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; - - uint32_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* tm_data; - /** - * The timeStamper is responsible for adding a timestamp to the packet. - * It is initialized lazy. - */ - static TimeStamperIF* timeStamper; - - static object_id_t timeStamperId; //!< The ID to use when looking for a time stamper. - /** - * Checks if a time stamper is available and tries to set it if not. - * @return Returns false if setting failed. - */ - bool checkAndSetStamper(); -}; - - -#endif /* TMPACKETBASE_H_ */ +#ifndef TMPACKETBASE_H_ +#define TMPACKETBASE_H_ + +#include "../../timemanager/TimeStamperIF.h" +#include "../../tmtcpacket/SpacePacketBase.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 [TBD]. + * @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); //!< Minimum size of a valid PUS Telemetry Packet. + static const uint32_t MISSION_TM_PACKET_MAX_SIZE = 2048; //!< Maximum size of a TM Packet in this mission. + static const uint8_t VERSION_NUMBER_BYTE_PUS_A = 0b00010000; //!< First byte of secondary header for PUS-A packets. + /** + * 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* set_data ); + /** + * This is the empty default destructor. + */ + virtual ~TmPacketBase(); + /** + * 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); + /** + * 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(); + + /** + * 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); + /** + * 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(); + /** + * With this method, the Error Control Field is updated to match the + * current content of the packet. + */ + void setErrorControl(); + /** + * This sets the source data. It copies the provided data to + * the internal TmPacketPointer source data location. + * @param sourceData + * @param sourceSize + */ + void setSourceData(uint8_t* sourceData, size_t sourceSize); + /** + * 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* p_Data ); + /** + * 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; + + uint32_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* tm_data; + /** + * The timeStamper is responsible for adding a timestamp to the packet. + * It is initialized lazy. + */ + static TimeStamperIF* timeStamper; + + static object_id_t timeStamperId; //!< The ID to use when looking for a time stamper. + /** + * Checks if a time stamper is available and tries to set it if not. + * @return Returns false if setting failed. + */ + bool checkAndSetStamper(); +}; + + +#endif /* TMPACKETBASE_H_ */ diff --git a/tmtcpacket/pus/TmPacketMinimal.cpp b/tmtcpacket/pus/TmPacketMinimal.cpp index ee56d46f..c7c495ae 100644 --- a/tmtcpacket/pus/TmPacketMinimal.cpp +++ b/tmtcpacket/pus/TmPacketMinimal.cpp @@ -1,45 +1,45 @@ -#include "../../tmtcpacket/pus/TmPacketMinimal.h" -#include -#include -#include "../../tmtcpacket/pus/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; +#include "../../tmtcpacket/pus/TmPacketMinimal.h" +#include +#include +#include "../../tmtcpacket/pus/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/tmtcpacket/pus/TmPacketMinimal.h b/tmtcpacket/pus/TmPacketMinimal.h index e7b362bf..728acb15 100644 --- a/tmtcpacket/pus/TmPacketMinimal.h +++ b/tmtcpacket/pus/TmPacketMinimal.h @@ -1,84 +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_ */ +#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/tmtcpacket/pus/TmPacketStored.cpp b/tmtcpacket/pus/TmPacketStored.cpp index 9476dcd6..17b49f43 100644 --- a/tmtcpacket/pus/TmPacketStored.cpp +++ b/tmtcpacket/pus/TmPacketStored.cpp @@ -1,146 +1,146 @@ -#include "../../objectmanager/ObjectManagerIF.h" -#include "../../serviceinterface/ServiceInterfaceStream.h" -#include "../../tmtcpacket/pus/TmPacketStored.h" -#include "../../tmtcservices/TmTcMessage.h" -#include - -TmPacketStored::TmPacketStored(store_address_t setAddress) : - TmPacketBase(NULL), 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 (!checkAndSetStore()) { - return; - } - uint8_t *pData = NULL; - ReturnValue_t returnValue = store->getFreeElement(&storeAddress, - (TmPacketBase::TM_PACKET_MIN_SIZE + size + headerSize), &pData); - - if (returnValue != HasReturnvaluesIF::RETURN_OK) { - sif::debug << "TmPacketStored::TmPacketStored: " - "Issue getting free storage" << std::endl; - 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); -} - -// todo: Endianness flags as optional parameter? -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 (!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(NULL); -} - -void TmPacketStored::setStoreAddress(store_address_t setAddress) { - storeAddress = setAddress; - const uint8_t *temp_data = NULL; - size_t temp_size; - if (!checkAndSetStore()) { - return; - } - ReturnValue_t status = store->getData(storeAddress, &temp_data, &temp_size); - if (status == StorageManagerIF::RETURN_OK) { - setData(temp_data); - } else { - setData(NULL); - storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; - } -} - -bool TmPacketStored::checkAndSetStore() { - if (store == NULL) { - store = objectManager->get(objects::TM_STORE); - if (store == NULL) { - sif::error << "TmPacketStored::TmPacketStored: TM Store not found!" - << std::endl; - return false; - } - } - return true; -} - -StorageManagerIF *TmPacketStored::store = NULL; -InternalErrorReporterIF *TmPacketStored::internalErrorReporter = NULL; - -ReturnValue_t TmPacketStored::sendPacket(MessageQueueId_t destination, - MessageQueueId_t sentFrom, bool doErrorReporting) { - if (getWholeData() == NULL) { - //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 == NULL) { - internalErrorReporter = objectManager->get( - objects::INTERNAL_ERROR_REPORTER); - } - if (internalErrorReporter != NULL) { - internalErrorReporter->lostTm(); - } -} +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../tmtcpacket/pus/TmPacketStored.h" +#include "../../tmtcservices/TmTcMessage.h" +#include + +TmPacketStored::TmPacketStored(store_address_t setAddress) : + TmPacketBase(NULL), 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 (!checkAndSetStore()) { + return; + } + uint8_t *pData = NULL; + ReturnValue_t returnValue = store->getFreeElement(&storeAddress, + (TmPacketBase::TM_PACKET_MIN_SIZE + size + headerSize), &pData); + + if (returnValue != HasReturnvaluesIF::RETURN_OK) { + sif::debug << "TmPacketStored::TmPacketStored: " + "Issue getting free storage" << std::endl; + 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); +} + +// todo: Endianness flags as optional parameter? +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 (!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(NULL); +} + +void TmPacketStored::setStoreAddress(store_address_t setAddress) { + storeAddress = setAddress; + const uint8_t *temp_data = NULL; + size_t temp_size; + if (!checkAndSetStore()) { + return; + } + ReturnValue_t status = store->getData(storeAddress, &temp_data, &temp_size); + if (status == StorageManagerIF::RETURN_OK) { + setData(temp_data); + } else { + setData(NULL); + storeAddress.raw = StorageManagerIF::INVALID_ADDRESS; + } +} + +bool TmPacketStored::checkAndSetStore() { + if (store == NULL) { + store = objectManager->get(objects::TM_STORE); + if (store == NULL) { + sif::error << "TmPacketStored::TmPacketStored: TM Store not found!" + << std::endl; + return false; + } + } + return true; +} + +StorageManagerIF *TmPacketStored::store = NULL; +InternalErrorReporterIF *TmPacketStored::internalErrorReporter = NULL; + +ReturnValue_t TmPacketStored::sendPacket(MessageQueueId_t destination, + MessageQueueId_t sentFrom, bool doErrorReporting) { + if (getWholeData() == NULL) { + //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 == NULL) { + internalErrorReporter = objectManager->get( + objects::INTERNAL_ERROR_REPORTER); + } + if (internalErrorReporter != NULL) { + internalErrorReporter->lostTm(); + } +} diff --git a/tmtcpacket/pus/TmPacketStored.h b/tmtcpacket/pus/TmPacketStored.h index 195c3272..98e37ed5 100644 --- a/tmtcpacket/pus/TmPacketStored.h +++ b/tmtcpacket/pus/TmPacketStored.h @@ -1,96 +1,96 @@ -#ifndef TMPACKETSTORED_H_ -#define TMPACKETSTORED_H_ - -#include "../../serialize/SerializeIF.h" -#include "../../storagemanager/StorageManagerIF.h" -#include "../../tmtcpacket/pus/TmPacketBase.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 { -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(); -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 = NULL, uint32_t size = 0, const uint8_t* headerData = NULL, 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 = NULL); - /** - * 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 ); -}; - - -#endif /* TMPACKETSTORED_H_ */ +#ifndef TMPACKETSTORED_H_ +#define TMPACKETSTORED_H_ + +#include "../../serialize/SerializeIF.h" +#include "../../storagemanager/StorageManagerIF.h" +#include "../../tmtcpacket/pus/TmPacketBase.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 { +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(); +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 = NULL, uint32_t size = 0, const uint8_t* headerData = NULL, 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 = NULL); + /** + * 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 ); +}; + + +#endif /* TMPACKETSTORED_H_ */ diff --git a/tmtcservices/AcceptsTelecommandsIF.h b/tmtcservices/AcceptsTelecommandsIF.h index 13928df5..bff0e955 100644 --- a/tmtcservices/AcceptsTelecommandsIF.h +++ b/tmtcservices/AcceptsTelecommandsIF.h @@ -1,43 +1,43 @@ -#ifndef FRAMEWORK_TMTCSERVICES_ACCEPTSTELECOMMANDSIF_H_ -#define FRAMEWORK_TMTCSERVICES_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); // is this used anywhere or can it be removed? - 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 /* FRAMEWORK_TMTCSERVICES_ACCEPTSTELECOMMANDSIF_H_ */ +#ifndef FRAMEWORK_TMTCSERVICES_ACCEPTSTELECOMMANDSIF_H_ +#define FRAMEWORK_TMTCSERVICES_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); // is this used anywhere or can it be removed? + 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 /* FRAMEWORK_TMTCSERVICES_ACCEPTSTELECOMMANDSIF_H_ */ diff --git a/tmtcservices/AcceptsTelemetryIF.h b/tmtcservices/AcceptsTelemetryIF.h index bcb38666..2325dbe0 100644 --- a/tmtcservices/AcceptsTelemetryIF.h +++ b/tmtcservices/AcceptsTelemetryIF.h @@ -1,26 +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_ */ +#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/tmtcservices/AcceptsVerifyMessageIF.h b/tmtcservices/AcceptsVerifyMessageIF.h index a312390e..c9318ab0 100644 --- a/tmtcservices/AcceptsVerifyMessageIF.h +++ b/tmtcservices/AcceptsVerifyMessageIF.h @@ -1,15 +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_ */ +#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/tmtcservices/CommandingServiceBase.cpp b/tmtcservices/CommandingServiceBase.cpp index 3325b10b..e5a87f18 100644 --- a/tmtcservices/CommandingServiceBase.cpp +++ b/tmtcservices/CommandingServiceBase.cpp @@ -1,427 +1,427 @@ -#include "../tcdistribution/PUSDistributorIF.h" -#include "../tmtcservices/AcceptsTelemetryIF.h" -#include "../objectmanager/ObjectManagerIF.h" - -#include "../tmtcservices/CommandingServiceBase.h" -#include "../tmtcservices/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->command, &iter->state, - &nextCommand, iter->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->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->tcInfo.ackFlags, - iter->tcInfo.tcPacketId, iter->tcInfo.tcSequenceControl, - result, ++iter->step, failureParameter1, - failureParameter2); - } else { - verificationReporter.sendFailureReport( - TC_VERIFY::COMPLETION_FAILURE, iter->tcInfo.ackFlags, - iter->tcInfo.tcPacketId, iter->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->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->tcInfo.ackFlags, iter->tcInfo.tcPacketId, - iter->tcInfo.tcSequenceControl, ++iter->step); - } - else { - verificationReporter.sendSuccessReport( - TC_VERIFY::COMPLETION_SUCCESS, - iter->tcInfo.ackFlags, iter->tcInfo.tcPacketId, - iter->tcInfo.tcSequenceControl, 0); - checkAndExecuteFifo(iter); - } - } - else { - if (isStep) { - nextCommand->clearCommandMessage(); - verificationReporter.sendFailureReport( - TC_VERIFY::PROGRESS_FAILURE, iter->tcInfo.ackFlags, - iter->tcInfo.tcPacketId, - iter->tcInfo.tcSequenceControl, sendResult, - ++iter->step, failureParameter1, failureParameter2); - } else { - nextCommand->clearCommandMessage(); - verificationReporter.sendFailureReport( - TC_VERIFY::COMPLETION_FAILURE, - iter->tcInfo.ackFlags, iter->tcInfo.tcPacketId, - iter->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->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->subservice = storedPacket->getSubService(); - result = prepareCommand(&command, iter->subservice, - storedPacket->getApplicationData(), - storedPacket->getApplicationDataSize(), &iter->state, - iter->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->uptimeOfStart); - iter->step = 0; - iter->subservice = storedPacket->getSubService(); - iter->command = command.getCommand(); - iter->tcInfo.ackFlags = storedPacket->getAcknowledgeFlags(); - iter->tcInfo.tcPacketId = storedPacket->getPacketId(); - iter->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->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->uptimeOfStart + (timeoutSeconds * 1000)) < uptime) { - verificationReporter.sendFailureReport( - TC_VERIFY::COMPLETION_FAILURE, iter->tcInfo.ackFlags, - iter->tcInfo.tcPacketId, iter->tcInfo.tcSequenceControl, - TIMEOUT); - checkAndExecuteFifo(iter); - } - } -} - -void CommandingServiceBase::setTaskIF(PeriodicTaskIF* task_) { - executingTask = task_; -} +#include "../tcdistribution/PUSDistributorIF.h" +#include "../tmtcservices/AcceptsTelemetryIF.h" +#include "../objectmanager/ObjectManagerIF.h" + +#include "../tmtcservices/CommandingServiceBase.h" +#include "../tmtcservices/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->command, &iter->state, + &nextCommand, iter->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->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->tcInfo.ackFlags, + iter->tcInfo.tcPacketId, iter->tcInfo.tcSequenceControl, + result, ++iter->step, failureParameter1, + failureParameter2); + } else { + verificationReporter.sendFailureReport( + TC_VERIFY::COMPLETION_FAILURE, iter->tcInfo.ackFlags, + iter->tcInfo.tcPacketId, iter->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->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->tcInfo.ackFlags, iter->tcInfo.tcPacketId, + iter->tcInfo.tcSequenceControl, ++iter->step); + } + else { + verificationReporter.sendSuccessReport( + TC_VERIFY::COMPLETION_SUCCESS, + iter->tcInfo.ackFlags, iter->tcInfo.tcPacketId, + iter->tcInfo.tcSequenceControl, 0); + checkAndExecuteFifo(iter); + } + } + else { + if (isStep) { + nextCommand->clearCommandMessage(); + verificationReporter.sendFailureReport( + TC_VERIFY::PROGRESS_FAILURE, iter->tcInfo.ackFlags, + iter->tcInfo.tcPacketId, + iter->tcInfo.tcSequenceControl, sendResult, + ++iter->step, failureParameter1, failureParameter2); + } else { + nextCommand->clearCommandMessage(); + verificationReporter.sendFailureReport( + TC_VERIFY::COMPLETION_FAILURE, + iter->tcInfo.ackFlags, iter->tcInfo.tcPacketId, + iter->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->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->subservice = storedPacket->getSubService(); + result = prepareCommand(&command, iter->subservice, + storedPacket->getApplicationData(), + storedPacket->getApplicationDataSize(), &iter->state, + iter->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->uptimeOfStart); + iter->step = 0; + iter->subservice = storedPacket->getSubService(); + iter->command = command.getCommand(); + iter->tcInfo.ackFlags = storedPacket->getAcknowledgeFlags(); + iter->tcInfo.tcPacketId = storedPacket->getPacketId(); + iter->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->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->uptimeOfStart + (timeoutSeconds * 1000)) < uptime) { + verificationReporter.sendFailureReport( + TC_VERIFY::COMPLETION_FAILURE, iter->tcInfo.ackFlags, + iter->tcInfo.tcPacketId, iter->tcInfo.tcSequenceControl, + TIMEOUT); + checkAndExecuteFifo(iter); + } + } +} + +void CommandingServiceBase::setTaskIF(PeriodicTaskIF* task_) { + executingTask = task_; +} diff --git a/tmtcservices/CommandingServiceBase.h b/tmtcservices/CommandingServiceBase.h index 32173d81..4a180445 100644 --- a/tmtcservices/CommandingServiceBase.h +++ b/tmtcservices/CommandingServiceBase.h @@ -1,348 +1,348 @@ -#ifndef FRAMEWORK_TMTCSERVICES_COMMANDINGSERVICEBASE_H_ -#define FRAMEWORK_TMTCSERVICES_COMMANDINGSERVICEBASE_H_ - -#include "../objectmanager/SystemObject.h" -#include "../storagemanager/StorageManagerIF.h" -#include "../tasks/ExecutableObjectIF.h" -#include "../ipc/MessageQueueIF.h" -#include "../tmtcservices/AcceptsTelecommandsIF.h" - -#include "../tmtcservices/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: - 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 { - 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; - }; - - 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 /* COMMANDINGSERVICEBASE_H_ */ +#ifndef FRAMEWORK_TMTCSERVICES_COMMANDINGSERVICEBASE_H_ +#define FRAMEWORK_TMTCSERVICES_COMMANDINGSERVICEBASE_H_ + +#include "../objectmanager/SystemObject.h" +#include "../storagemanager/StorageManagerIF.h" +#include "../tasks/ExecutableObjectIF.h" +#include "../ipc/MessageQueueIF.h" +#include "../tmtcservices/AcceptsTelecommandsIF.h" + +#include "../tmtcservices/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: + 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 { + 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; + }; + + 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 /* COMMANDINGSERVICEBASE_H_ */ diff --git a/tmtcservices/PusServiceBase.cpp b/tmtcservices/PusServiceBase.cpp index a8b74e39..9d4aabd9 100644 --- a/tmtcservices/PusServiceBase.cpp +++ b/tmtcservices/PusServiceBase.cpp @@ -1,123 +1,123 @@ -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../tcdistribution/PUSDistributorIF.h" -#include "../tmtcservices/AcceptsTelemetryIF.h" -#include "../tmtcservices/PusServiceBase.h" -#include "../tmtcservices/PusVerificationReport.h" -#include "../tmtcservices/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); -// if(status != MessageQueueIF::EMPTY) { -// sif::debug << "PusServiceBase::performOperation: Receiving from " -// << "MQ ID: " << std::hex << "0x" << std::setw(8) -// << std::setfill('0') << this->requestQueue->getId() -// << std::dec << " returned: " << status << std::setfill(' ') -// << 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 " - << 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 or distributor == nullptr) { - sif::error << "PusServiceBase::PusServiceBase: Service " - << this->serviceId << ": Configuration error. Make sure " - << "packetSource and packetDestination are defined correctly" - << std::endl; - return ObjectManagerIF::CHILD_INIT_FAILED; - } - this->requestQueue->setDefaultDestination( - destService->getReportReceptionQueue()); - distributor->registerService(this); - return HasReturnvaluesIF::RETURN_OK; -} - -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; -} +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../tcdistribution/PUSDistributorIF.h" +#include "../tmtcservices/AcceptsTelemetryIF.h" +#include "../tmtcservices/PusServiceBase.h" +#include "../tmtcservices/PusVerificationReport.h" +#include "../tmtcservices/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); +// if(status != MessageQueueIF::EMPTY) { +// sif::debug << "PusServiceBase::performOperation: Receiving from " +// << "MQ ID: " << std::hex << "0x" << std::setw(8) +// << std::setfill('0') << this->requestQueue->getId() +// << std::dec << " returned: " << status << std::setfill(' ') +// << 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 " + << 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 or distributor == nullptr) { + sif::error << "PusServiceBase::PusServiceBase: Service " + << this->serviceId << ": Configuration error. Make sure " + << "packetSource and packetDestination are defined correctly" + << std::endl; + return ObjectManagerIF::CHILD_INIT_FAILED; + } + this->requestQueue->setDefaultDestination( + destService->getReportReceptionQueue()); + distributor->registerService(this); + return HasReturnvaluesIF::RETURN_OK; +} + +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/tmtcservices/PusServiceBase.h b/tmtcservices/PusServiceBase.h index f9431275..7c8f504c 100644 --- a/tmtcservices/PusServiceBase.h +++ b/tmtcservices/PusServiceBase.h @@ -1,159 +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 "../tmtcservices/AcceptsTelecommandsIF.h" -#include "../tmtcservices/VerificationCodes.h" -#include "../tmtcservices/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_ */ +#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 "../tmtcservices/AcceptsTelecommandsIF.h" +#include "../tmtcservices/VerificationCodes.h" +#include "../tmtcservices/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/tmtcservices/PusVerificationReport.cpp b/tmtcservices/PusVerificationReport.cpp index d83380a2..369519d8 100644 --- a/tmtcservices/PusVerificationReport.cpp +++ b/tmtcservices/PusVerificationReport.cpp @@ -1,163 +1,163 @@ -#include "../serialize/SerializeAdapter.h" -#include "../tmtcservices/PusVerificationReport.h" - -PusVerificationMessage::PusVerificationMessage() { -} - -//PusVerificationMessage::PusVerificationMessage(uint8_t set_report_id, -// TcPacketBase* current_packet, ReturnValue_t set_error_code, -// uint8_t set_step, uint32_t parameter1, uint32_t parameter2) { -// uint8_t ackFlags = current_packet->getAcknowledgeFlags(); -// uint16_t tcPacketId = current_packet->getPacketId(); -// uint16_t tcSequenceControl = current_packet->getPacketSequenceControl(); -// 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); -//} - -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; -} +#include "../serialize/SerializeAdapter.h" +#include "../tmtcservices/PusVerificationReport.h" + +PusVerificationMessage::PusVerificationMessage() { +} + +//PusVerificationMessage::PusVerificationMessage(uint8_t set_report_id, +// TcPacketBase* current_packet, ReturnValue_t set_error_code, +// uint8_t set_step, uint32_t parameter1, uint32_t parameter2) { +// uint8_t ackFlags = current_packet->getAcknowledgeFlags(); +// uint16_t tcPacketId = current_packet->getPacketId(); +// uint16_t tcSequenceControl = current_packet->getPacketSequenceControl(); +// 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); +//} + +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/tmtcservices/PusVerificationReport.h b/tmtcservices/PusVerificationReport.h index 26a1eaed..c0a46758 100644 --- a/tmtcservices/PusVerificationReport.h +++ b/tmtcservices/PusVerificationReport.h @@ -1,77 +1,77 @@ -#ifndef PUSVERIFICATIONREPORT_H_ -#define PUSVERIFICATIONREPORT_H_ - -#include "../ipc/MessageQueueMessage.h" -#include "../tmtcpacket/pus/TcPacketBase.h" -#include "../tmtcservices/VerificationCodes.h" - -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, TcPacketBase* current_packet, ReturnValue_t set_error_code = 0, uint8_t set_step = 0, uint32_t parameter1 = 0, uint32_t parameter2 = 0 ); - 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 /* PUSVERIFICATIONREPORT_H_ */ +#ifndef PUSVERIFICATIONREPORT_H_ +#define PUSVERIFICATIONREPORT_H_ + +#include "../ipc/MessageQueueMessage.h" +#include "../tmtcpacket/pus/TcPacketBase.h" +#include "../tmtcservices/VerificationCodes.h" + +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, TcPacketBase* current_packet, ReturnValue_t set_error_code = 0, uint8_t set_step = 0, uint32_t parameter1 = 0, uint32_t parameter2 = 0 ); + 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 /* PUSVERIFICATIONREPORT_H_ */ diff --git a/tmtcservices/SourceSequenceCounter.h b/tmtcservices/SourceSequenceCounter.h index 0db53380..1cc58758 100644 --- a/tmtcservices/SourceSequenceCounter.h +++ b/tmtcservices/SourceSequenceCounter.h @@ -1,30 +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_ */ +/** + * @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/tmtcservices/TmTcBridge.cpp b/tmtcservices/TmTcBridge.cpp index 706bbbf2..8c2f15e5 100644 --- a/tmtcservices/TmTcBridge.cpp +++ b/tmtcservices/TmTcBridge.cpp @@ -1,237 +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; -} +#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/tmtcservices/TmTcBridge.h b/tmtcservices/TmTcBridge.h index 763980dc..62cfdaac 100644 --- a/tmtcservices/TmTcBridge.h +++ b/tmtcservices/TmTcBridge.h @@ -1,162 +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_ */ +#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/tmtcservices/TmTcMessage.cpp b/tmtcservices/TmTcMessage.cpp index 0fe3229c..a715225b 100644 --- a/tmtcservices/TmTcMessage.cpp +++ b/tmtcservices/TmTcMessage.cpp @@ -1,29 +1,29 @@ -#include "../tmtcservices/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) ); -} +#include "../tmtcservices/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/tmtcservices/TmTcMessage.h b/tmtcservices/TmTcMessage.h index ad97c056..b5e1ff8d 100644 --- a/tmtcservices/TmTcMessage.h +++ b/tmtcservices/TmTcMessage.h @@ -1,50 +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_ */ +#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/tmtcservices/VerificationReporter.cpp b/tmtcservices/VerificationReporter.cpp index e9a43855..4f9f4e54 100644 --- a/tmtcservices/VerificationReporter.cpp +++ b/tmtcservices/VerificationReporter.cpp @@ -1,105 +1,105 @@ -#include "../tmtcservices/VerificationReporter.h" - -#include "../ipc/MessageQueueIF.h" -#include "../tmtcservices/AcceptsVerifyMessageIF.h" -#include "../tmtcservices/PusVerificationReport.h" -#include "../serviceinterface/ServiceInterfaceStream.h" -#include "../objectmanager/frameworkObjects.h" - -object_id_t VerificationReporter::messageReceiver = objects::PUS_SERVICE_1; - -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 << 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 << 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; - - } - this->acknowledgeQueue = temp->getVerificationQueue(); -} +#include "../tmtcservices/VerificationReporter.h" + +#include "../ipc/MessageQueueIF.h" +#include "../tmtcservices/AcceptsVerifyMessageIF.h" +#include "../tmtcservices/PusVerificationReport.h" +#include "../serviceinterface/ServiceInterfaceStream.h" +#include "../objectmanager/frameworkObjects.h" + +object_id_t VerificationReporter::messageReceiver = objects::PUS_SERVICE_1; + +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 << 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 << 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; + + } + this->acknowledgeQueue = temp->getVerificationQueue(); +} diff --git a/tmtcservices/VerificationReporter.h b/tmtcservices/VerificationReporter.h index 86aea826..4d92f6b3 100644 --- a/tmtcservices/VerificationReporter.h +++ b/tmtcservices/VerificationReporter.h @@ -1,50 +1,50 @@ -#ifndef FRAMEWORK_TMTCSERVICES_VERIFICATIONREPORTER_H_ -#define FRAMEWORK_TMTCSERVICES_VERIFICATIONREPORTER_H_ - -#include "../objectmanager/ObjectManagerIF.h" -#include "../tmtcservices/PusVerificationReport.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 /* FRAMEWORK_TMTCSERVICES_VERIFICATIONREPORTER_H_ */ +#ifndef FRAMEWORK_TMTCSERVICES_VERIFICATIONREPORTER_H_ +#define FRAMEWORK_TMTCSERVICES_VERIFICATIONREPORTER_H_ + +#include "../objectmanager/ObjectManagerIF.h" +#include "../tmtcservices/PusVerificationReport.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 /* FRAMEWORK_TMTCSERVICES_VERIFICATIONREPORTER_H_ */