Merge remote-tracking branch 'upstream/master' into mueller_FIFO_static_normal

This commit is contained in:
Robin Müller 2020-07-07 17:43:03 +02:00
commit e9166ec4c7
13 changed files with 664 additions and 110 deletions

View File

@ -1,13 +1,34 @@
#include <framework/datapool/PoolEntry.h> #include <framework/datapool/PoolEntry.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <framework/globalfunctions/arrayprinter.h>
#include <cstring>
template <typename T> template <typename T>
PoolEntry<T>::PoolEntry( T* initValue, uint8_t set_length, uint8_t set_valid ) : length(set_length), valid(set_valid) { PoolEntry<T>::PoolEntry(std::initializer_list<T> initValue, uint8_t setLength,
bool setValid ) : length(setLength), valid(setValid) {
this->address = new T[this->length]; this->address = new T[this->length];
if (initValue != NULL) { if(initValue.size() == 0) {
memcpy(this->address, initValue, this->getByteSize() ); 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 <typename T>
PoolEntry<T>::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 { } else {
memset(this->address, 0, this->getByteSize() ); std::memset(this->address, 0, this->getByteSize() );
} }
} }
@ -34,21 +55,20 @@ void* PoolEntry<T>::getRawData() {
} }
template <typename T> template <typename T>
void PoolEntry<T>::setValid( uint8_t isValid ) { void PoolEntry<T>::setValid(bool isValid) {
this->valid = isValid; this->valid = isValid;
} }
template <typename T> template <typename T>
uint8_t PoolEntry<T>::getValid() { bool PoolEntry<T>::getValid() {
return valid; return valid;
} }
template <typename T> template <typename T>
void PoolEntry<T>::print() { void PoolEntry<T>::print() {
for (uint8_t size = 0; size < this->length; size++ ) { sif::debug << "Pool Entry Validity: " <<
sif::debug << "| " << std::hex << (double)this->address[size] (this->valid? " (valid) " : " (invalid) ") << std::endl;
<< (this->valid? " (valid) " : " (invalid) "); arrayprinter::print(reinterpret_cast<uint8_t*>(address), length);
}
sif::debug << std::dec << std::endl; sif::debug << std::dec << std::endl;
} }

View File

@ -1,81 +1,126 @@
#ifndef POOLENTRY_H_ #ifndef FRAMEWORK_DATAPOOL_POOLENTRY_H_
#define POOLENTRY_H_ #define FRAMEWORK_DATAPOOL_POOLENTRY_H_
#include <framework/datapool/PoolEntryIF.h> #include <framework/datapool/PoolEntryIF.h>
#include <stddef.h>
#include <cstring> #include <initializer_list>
#include <type_traits>
#include <cstddef>
/** /**
* \brief This is a small helper class that defines a single data pool entry. * @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.
* *
* \details The helper is used to store all information together with the data as a single data pool entry. * It is prepared for use with plain old data types, but may be
* The content's type is defined by the template argument. * extended to complex types if necessary. It can be initialized with a
* It is prepared for use with plain old data types, * certain value, size and validity flag.
* 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 the DataPool class.
* *
* \ingroup data_pool * 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 <typename T> template <typename T>
class PoolEntry : public PoolEntryIF { class PoolEntry : public PoolEntryIF {
public: public:
static_assert(not std::is_same<T, bool>::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 * @brief In the classe's constructor, space is allocated on the heap and
* potential init values are copied to that space. * potential init values are copied to that space.
* \param initValue A pointer to the single value or array that holds the init value. * @details
* With the default value (NULL), the entry is initalized with all 0. * Not passing any arguments will initialize an non-array pool entry
* \param set_length Defines the array length of this entry. * (setLength = 1) with an initial invalid state.
* \param set_valid Sets the initialization flag. It is invalid (0) by default. * 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( T* initValue = NULL, uint8_t set_length = 1, uint8_t set_valid = 0 ); PoolEntry(std::initializer_list<T> initValue = {}, uint8_t setLength = 1,
bool setValid = false);
/** /**
* \brief The allocated memory for the variable is freed in the destructor. * @brief In the classe's constructor, space is allocated on the heap and
* \details As the data pool is global, this dtor is only called on program exit. * potential init values are copied to that space.
* PoolEntries shall never be copied, as a copy might delete the variable on the heap. * @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(); ~PoolEntry();
/** /**
* \brief This is the address pointing to the allocated memory. * @brief This is the address pointing to the allocated memory.
*/ */
T* address; T* address;
/** /**
* \brief This attribute stores the length information. * @brief This attribute stores the length information.
*/ */
uint8_t length; uint8_t length;
/** /**
* \brief Here, the validity information for a variable is stored. * @brief Here, the validity information for a variable is stored.
* Every entry (single variable or vector) has one valid flag. * Every entry (single variable or vector) has one valid flag.
*/ */
uint8_t valid; uint8_t valid;
/** /**
* \brief getSize returns the array size of the entry. * @brief getSize returns the array size of the entry.
* \details A single parameter has size 1. * @details A single parameter has size 1.
*/ */
uint8_t getSize(); uint8_t getSize();
/** /**
* \brief This operation returns the size in bytes. * @brief This operation returns the size in bytes.
* \details The size is calculated by sizeof(type) * array_size. * @details The size is calculated by sizeof(type) * array_size.
*/ */
uint16_t getByteSize(); uint16_t getByteSize();
/** /**
* \brief This operation returns a the address pointer casted to void*. * @brief This operation returns a the address pointer casted to void*.
*/ */
void* getRawData(); void* getRawData();
/** /**
* \brief This method allows to set the valid information of the pool entry. * @brief This method allows to set the valid information
* of the pool entry.
*/ */
void setValid( uint8_t isValid ); void setValid( bool isValid );
/** /**
* \brief This method allows to get the valid information of the pool entry. * @brief This method allows to get the valid information
* of the pool entry.
*/ */
uint8_t getValid(); bool getValid();
/** /**
* \brief This is a debug method that prints all values and the valid information to the screen. * @brief This is a debug method that prints all values and the valid
* It prints all array entries in a row. * information to the screen. It prints all array entries in a row.
*/ */
void print(); void print();

View File

@ -1,62 +1,57 @@
/** #ifndef FRAMEWORK_DATAPOOL_POOLENTRYIF_H_
* \file PoolEntryIF.h #define FRAMEWORK_DATAPOOL_POOLENTRYIF_H_
*
* \brief This file holds the class that defines the Interface for Pool Entry elements.
*
* \date 10/18/2012
*
* \author Bastian Baetz
*/
#ifndef POOLENTRYIF_H_
#define POOLENTRYIF_H_
#include <framework/globalfunctions/Type.h> #include <framework/globalfunctions/Type.h>
#include <stdint.h> #include <cstdint>
/** /**
* \brief This interface defines the access possibilities to a single data pool entry. * @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.
* *
* \details The interface provides methods to determine the size and the validity information of a value. * @author Bastian Baetz
* It also defines a method to receive a pointer to the raw data content. * @ingroup data_pool
* It is mainly used by DataPool itself, but also as a return pointer.
*
* \ingroup data_pool
* *
*/ */
class PoolEntryIF { class PoolEntryIF {
public: public:
/** /**
* \brief This is an empty virtual destructor, as it is proposed for C++ interfaces. * @brief This is an empty virtual destructor,
* as it is required for C++ interfaces.
*/ */
virtual ~PoolEntryIF() { virtual ~PoolEntryIF() {
} }
/** /**
* \brief getSize returns the array size of the entry. A single variable parameter has size 1. * @brief getSize returns the array size of the entry.
* A single variable parameter has size 1.
*/ */
virtual uint8_t getSize() = 0; virtual uint8_t getSize() = 0;
/** /**
* \brief This operation returns the size in bytes, which is calculated by * @brief This operation returns the size in bytes, which is calculated by
* sizeof(type) * array_size. * sizeof(type) * array_size.
*/ */
virtual uint16_t getByteSize() = 0; virtual uint16_t getByteSize() = 0;
/** /**
* \brief This operation returns a the address pointer casted to void*. * @brief This operation returns a the address pointer casted to void*.
*/ */
virtual void* getRawData() = 0; virtual void* getRawData() = 0;
/** /**
* \brief This method allows to set the valid information of the pool entry. * @brief This method allows to set the valid information of the pool entry.
*/ */
virtual void setValid(uint8_t isValid) = 0; virtual void setValid(bool isValid) = 0;
/** /**
* \brief This method allows to set the valid information of the pool entry. * @brief This method allows to set the valid information of the pool entry.
*/ */
virtual uint8_t getValid() = 0; virtual bool getValid() = 0;
/** /**
* \brief This is a debug method that prints all values and the valid information to the screen. * @brief This is a debug method that prints all values and the valid
* It prints all array entries in a row. * 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; virtual void print() = 0;
/** /**

View File

@ -4,6 +4,8 @@
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <framework/osal/Endiness.h> #include <framework/osal/Endiness.h>
#include <cstring>
PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry, PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry,
DataSetIF* data_set, ReadWriteMode_t setReadWriteMode) : DataSetIF* data_set, ReadWriteMode_t setReadWriteMode) :
dataPoolId(set_id), arrayEntry(setArrayEntry), valid(false), type(Type::UNKNOWN_TYPE), typeSize( dataPoolId(set_id), arrayEntry(setArrayEntry), valid(false), type(Type::UNKNOWN_TYPE), typeSize(

View File

@ -1,16 +1,18 @@
#include <framework/timemanager/Clock.h> #include <framework/timemanager/Clock.h>
#include <framework/globalfunctions/timevalOperations.h> #include <framework/globalfunctions/timevalOperations.h>
#include <stdlib.h> #include <framework/osal/FreeRTOS/Timekeeper.h>
#include "Timekeeper.h"
#include <FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <task.h> #include <freertos/task.h>
#include <stdlib.h>
#include <time.h>
//TODO sanitize input? //TODO sanitize input?
//TODO much of this code can be reused for tick-only systems //TODO much of this code can be reused for tick-only systems
uint16_t Clock::leapSeconds = 0; uint16_t Clock::leapSeconds = 0;
MutexIF* Clock::timeMutex = NULL; MutexIF* Clock::timeMutex = nullptr;
uint32_t Clock::getTicksPerSecond(void) { uint32_t Clock::getTicksPerSecond(void) {
return 1000; return 1000;
@ -56,7 +58,6 @@ ReturnValue_t Clock::getUptime(timeval* uptime) {
timeval Clock::getUptime() { timeval Clock::getUptime() {
TickType_t ticksSinceStart = xTaskGetTickCount(); TickType_t ticksSinceStart = xTaskGetTickCount();
return Timekeeper::ticksToTimeval(ticksSinceStart); return Timekeeper::ticksToTimeval(ticksSinceStart);
} }
@ -128,7 +129,7 @@ ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) {
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) { ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) {
//SHOULDDO: works not for dates in the past (might have less leap seconds) //SHOULDDO: works not for dates in the past (might have less leap seconds)
if (timeMutex == NULL) { if (timeMutex == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }

View File

@ -1,20 +1,19 @@
#include "Timekeeper.h" #include <framework/osal/FreeRTOS/Timekeeper.h>
#include <FreeRTOSConfig.h>
Timekeeper::Timekeeper() : #include "FreeRTOSConfig.h"
offset( { 0, 0 }) {
// TODO Auto-generated constructor stub
} Timekeeper * Timekeeper::myinstance = nullptr;
Timekeeper * Timekeeper::myinstance = NULL; Timekeeper::Timekeeper() : offset( { 0, 0 } ) {}
Timekeeper::~Timekeeper() {}
const timeval& Timekeeper::getOffset() const { const timeval& Timekeeper::getOffset() const {
return offset; return offset;
} }
Timekeeper* Timekeeper::instance() { Timekeeper* Timekeeper::instance() {
if (myinstance == NULL) { if (myinstance == nullptr) {
myinstance = new Timekeeper(); myinstance = new Timekeeper();
} }
return myinstance; return myinstance;
@ -24,10 +23,6 @@ void Timekeeper::setOffset(const timeval& offset) {
this->offset = offset; this->offset = offset;
} }
Timekeeper::~Timekeeper() {
// TODO Auto-generated destructor stub
}
timeval Timekeeper::ticksToTimeval(TickType_t ticks) { timeval Timekeeper::ticksToTimeval(TickType_t ticks) {
timeval uptime; timeval uptime;
uptime.tv_sec = ticks / configTICK_RATE_HZ; uptime.tv_sec = ticks / configTICK_RATE_HZ;
@ -40,3 +35,7 @@ timeval Timekeeper::ticksToTimeval(TickType_t ticks) {
return uptime; return uptime;
} }
TickType_t Timekeeper::getTicks() {
return xTaskGetTickCount();
}

View File

@ -3,7 +3,9 @@
#include <framework/timemanager/Clock.h> #include <framework/timemanager/Clock.h>
#include <FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h>
/** /**
* A Class to basically store the time difference between uptime and UTC * A Class to basically store the time difference between uptime and UTC
@ -25,6 +27,11 @@ public:
virtual ~Timekeeper(); virtual ~Timekeeper();
static timeval ticksToTimeval(TickType_t ticks); static timeval ticksToTimeval(TickType_t ticks);
/**
* Get elapsed time in system ticks.
* @return
*/
static TickType_t getTicks();
const timeval& getOffset() const; const timeval& getOffset() const;
void setOffset(const timeval& offset); void setOffset(const timeval& offset);

View File

@ -1,10 +1,10 @@
#include <sys/time.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <time.h>
#include <framework/timemanager/Clock.h> #include <framework/timemanager/Clock.h>
#include <sys/time.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <linux/sysinfo.h> #include <linux/sysinfo.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
//#include <fstream> //#include <fstream>
@ -65,6 +65,15 @@ ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
return HasReturnvaluesIF::RETURN_OK; 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) { ReturnValue_t Clock::getUptime(timeval* uptime) {
//TODO This is not posix compatible and delivers only seconds precision //TODO This is not posix compatible and delivers only seconds precision
struct sysinfo sysInfo; struct sysinfo sysInfo;

View File

@ -2,12 +2,15 @@
#define FRAMEWORK_TIMEMANAGER_CLOCK_H_ #define FRAMEWORK_TIMEMANAGER_CLOCK_H_
#include <framework/returnvalues/HasReturnvaluesIF.h> #include <framework/returnvalues/HasReturnvaluesIF.h>
#include <stdint.h>
#include <sys/time.h>
#include <framework/ipc/MutexFactory.h> #include <framework/ipc/MutexFactory.h>
#include <framework/globalfunctions/timevalOperations.h> #include <framework/globalfunctions/timevalOperations.h>
#include <cstdint>
#include <sys/time.h>
//! Don't use these for time points, type is not large enough for UNIX epoch.
typedef uint32_t dur_millis_t;
typedef double dur_seconds_t;
class Clock { class Clock {
public: public:
@ -21,7 +24,7 @@ public:
uint32_t usecond; //!< Microseconds, 0 .. 999999 uint32_t usecond; //!< Microseconds, 0 .. 999999
} TimeOfDay_t; } TimeOfDay_t;
/**static Clock* TimeOfDay_t(); /**
* This method returns the number of clock ticks per second. * This method returns the number of clock ticks per second.
* In RTEMS, this is typically 1000. * In RTEMS, this is typically 1000.
* @return The number of ticks. * @return The number of ticks.
@ -33,22 +36,23 @@ public:
* This system call sets the system time. * This system call sets the system time.
* To set the time, it uses a TimeOfDay_t struct. * To set the time, it uses a TimeOfDay_t struct.
* @param time The struct with the time settings to set. * @param time The struct with the time settings to set.
* @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. * @return -@c RETURN_OK on success. Otherwise, the OS failure code
* is returned.
*/ */
static ReturnValue_t setClock(const TimeOfDay_t* time); static ReturnValue_t setClock(const TimeOfDay_t* time);
/** /**
* This system call sets the system time. * This system call sets the system time.
* To set the time, it uses a timeval struct. * To set the time, it uses a timeval struct.
* @param time The struct with the time settings to set. * @param time The struct with the time settings to set.
* @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. * @return -@c RETURN_OK on success. Otherwise, the OS failure code is returned.
*/ */
static ReturnValue_t setClock(const timeval* time); static ReturnValue_t setClock(const timeval* time);
/** /**
* This system call returns the current system clock in timeval format. * 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 * The timval format has the fields @c tv_sec with seconds and @c tv_usec with
* microseconds since an OS-defined epoch. * microseconds since an OS-defined epoch.
* @param time A pointer to a timeval struct where the current time is stored. * @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. * @return @c RETURN_OK on success. Otherwise, the OS failure code is returned.
*/ */
static ReturnValue_t getClock_timeval(timeval* time); static ReturnValue_t getClock_timeval(timeval* time);
@ -56,7 +60,7 @@ public:
* Get the time since boot in a timeval struct * Get the time since boot in a timeval struct
* *
* @param[out] time A pointer to a timeval struct where the uptime is stored. * @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. * @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() * @deprecated, I do not think this should be able to fail, use timeval getUptime()
*/ */

57
timemanager/Stopwatch.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <framework/timemanager/Stopwatch.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <iomanip>
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;
}

71
timemanager/Stopwatch.h Normal file
View File

@ -0,0 +1,71 @@
#ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#include <framework/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_ */

192
tmtcservices/TmTcBridge.cpp Normal file
View File

@ -0,0 +1,192 @@
#include <framework/tmtcservices/TmTcBridge.h>
#include <framework/ipc/QueueFactory.h>
#include <framework/tmtcservices/AcceptsTelecommandsIF.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <framework/globalfunctions/arrayprinter.h>
TmTcBridge::TmTcBridge(object_id_t objectId_,
object_id_t ccsdsPacketDistributor_): SystemObject(objectId_),
ccsdsPacketDistributor(ccsdsPacketDistributor_)
{
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: 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: Number of packets stored "
"exceeds limits. Keeping default value." << std::endl;
return RETURN_FAILED;
}
}
ReturnValue_t TmTcBridge::initialize() {
tcStore = objectManager->get<StorageManagerIF>(objects::TC_STORE);
if (tcStore == NULL) {
return RETURN_FAILED;
}
tmStore = objectManager->get<StorageManagerIF>(objects::TM_STORE);
if (tmStore == NULL) {
return RETURN_FAILED;
}
AcceptsTelecommandsIF* tcDistributor =
objectManager->get<AcceptsTelecommandsIF>(ccsdsPacketDistributor);
if (tcDistributor == NULL) {
return RETURN_FAILED;
}
TmTcReceptionQueue->setDefaultDestination(tcDistributor->getRequestQueue());
return RETURN_OK;
}
ReturnValue_t TmTcBridge::performOperation(uint8_t operationCode) {
ReturnValue_t result;
result = handleTc();
if(result != RETURN_OK) {
sif::error << "TMTC Bridge: Error handling TCs" << std::endl;
}
result = handleTm();
if (result != RETURN_OK) {
sif::error << "TMTC Bridge: Error handling TMs" << std::endl;
}
return result;
}
ReturnValue_t TmTcBridge::handleTc() {
uint8_t * recvBuffer = nullptr;
size_t recvLen = 0;
ReturnValue_t result = receiveTc(&recvBuffer, &recvLen);
return result;
}
ReturnValue_t TmTcBridge::handleTm() {
ReturnValue_t result = handleTmQueue();
if(result != RETURN_OK) {
sif::error << "TMTC Bridge: Reading TM Queue failed" << std::endl;
return RETURN_FAILED;
}
if(tmStored and communicationLinkUp) {
result = handleStoredTm();
}
return result;
}
ReturnValue_t TmTcBridge::handleTmQueue() {
TmTcMessage message;
const uint8_t* data = nullptr;
size_t size = 0;
for (ReturnValue_t result = TmTcReceptionQueue->receiveMessage(&message);
result == RETURN_OK; result = TmTcReceptionQueue->receiveMessage(&message))
{
if(communicationLinkUp == false) {
result = storeDownlinkData(&message);
return result;
}
result = tmStore->getData(message.getStorageId(), &data, &size);
if (result != HasReturnvaluesIF::RETURN_OK) {
continue;
}
result = sendTm(data, size);
if (result != RETURN_OK) {
sif::error << "TMTC Bridge: Could not send TM packet"<< std::endl;
tmStore->deleteData(message.getStorageId());
return result;
}
tmStore->deleteData(message.getStorageId());
}
return RETURN_OK;
}
ReturnValue_t TmTcBridge::storeDownlinkData(TmTcMessage *message) {
//debug << "TMTC Bridge: Comm Link down. "
// "Saving packet ID to be sent later\r\n" << std::flush;
store_address_t storeId = 0;
if(tmFifo.full()) {
sif::error << "TMTC Bridge: TM downlink max. number of stored packet IDs "
"reached! Overwriting old data" << std::endl;
tmFifo.retrieve(&storeId);
tmStore->deleteData(storeId);
}
storeId = message->getStorageId();
tmFifo.insert(storeId);
tmStored = true;
return RETURN_OK;
}
ReturnValue_t TmTcBridge::handleStoredTm() {
uint8_t counter = 0;
ReturnValue_t result = RETURN_OK;
while(not tmFifo.empty() and counter < sentPacketsPerCycle) {
//info << "TMTC Bridge: Sending stored TM data. There are "
// << (int) fifo.size() << " left to send\r\n" << std::flush;
store_address_t storeId;
const uint8_t* data = nullptr;
size_t size = 0;
tmFifo.retrieve(&storeId);
result = tmStore->getData(storeId, &data, &size);
sendTm(data,size);
if(result != RETURN_OK) {
sif::error << "TMTC Bridge: Could not send stored downlink data"
<< std::endl;
result = RETURN_FAILED;
}
counter ++;
if(tmFifo.empty()) {
tmStored = false;
}
tmStore->deleteData(storeId);
}
return result;
}
void TmTcBridge::registerCommConnect() {
if(not communicationLinkUp) {
//info << "TMTC Bridge: Registered Comm Link Connect" << std::endl;
communicationLinkUp = true;
}
}
void TmTcBridge::registerCommDisconnect() {
//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);
}

152
tmtcservices/TmTcBridge.h Normal file
View File

@ -0,0 +1,152 @@
#ifndef FRAMEWORK_TMTCSERVICES_TMTCBRIDGE_H_
#define FRAMEWORK_TMTCSERVICES_TMTCBRIDGE_H_
#include <framework/tmtcservices/AcceptsTelemetryIF.h>
#include <framework/tasks/ExecutableObjectIF.h>
#include <framework/ipc/MessageQueueIF.h>
#include <framework/storagemanager/StorageManagerIF.h>
#include <framework/objectmanager/SystemObject.h>
#include <framework/tmtcservices/TmTcMessage.h>
#include <framework/container/FIFO.h>
class TmTcBridge : public AcceptsTelemetryIF,
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 ccsdsPacketDistributor_);
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);
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;
/**
* Return TMTC Reception Queue
* @param virtualChannel
* @return
*/
MessageQueueId_t getReportReceptionQueue(
uint8_t virtualChannel = 0) override;
protected:
//! Used to send and receive TMTC messages.
//! TmTcMessage is used to transport messages between tasks.
MessageQueueIF* TmTcReceptionQueue = nullptr;
StorageManagerIF* tcStore = nullptr;
StorageManagerIF* tmStore = nullptr;
object_id_t ccsdsPacketDistributor = 0;
//! Used to specify whether communication link is up
bool communicationLinkUp = false;
bool tmStored = false;
/**
* @brief Handle TC reception
* @details
* Default implementation provided, but is empty.
* Child handler should override this in most cases orsend TC to the
* TC distributor directly with the address of the reception queue by
* calling getReportRecptionQueue()
* @return
*/
virtual ReturnValue_t handleTc();
/**
* Implemented by child class. Perform receiving of Telecommand,
* for example by implementing specific drivers or wrappers,
* e.g. UART Communication or an ethernet stack
* @param recvBuffer [out] Received data
* @param size [out] Size of received data
* @return
*/
virtual ReturnValue_t receiveTc(uint8_t ** recvBuffer, size_t * size) = 0;
/**
* 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. UART communication or lwIP stack.
* @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.
*/
FIFO<store_address_t, LIMIT_DOWNLINK_PACKETS_STORED> tmFifo;
uint8_t sentPacketsPerCycle = DEFAULT_STORED_DATA_SENT_PER_CYCLE;
uint8_t maxNumberOfPacketsStored = DEFAULT_DOWNLINK_PACKETS_STORED;
};
#endif /* FRAMEWORK_TMTCSERVICES_TMTCBRIDGE_H_ */