fsfw/parameters/ParameterWrapper.h

200 lines
6.3 KiB
C
Raw Normal View History

2020-08-28 18:46:47 +02:00
#ifndef FSFW_PARAMETERS_PARAMETERWRAPPER_H_
#define FSFW_PARAMETERS_PARAMETERWRAPPER_H_
2020-08-13 20:53:35 +02:00
#include "../returnvalues/HasReturnvaluesIF.h"
#include "../serialize/SerializeAdapter.h"
#include "../serialize/SerializeIF.h"
#include "../globalfunctions/Type.h"
2020-08-28 18:46:47 +02:00
#include <cstddef>
2020-08-28 18:46:47 +02:00
/**
2021-01-31 17:40:20 +01:00
* @brief This wrapper encapsulates the access to parameters provided by HasParametersIF.
2020-08-28 18:46:47 +02:00
* @details
2021-01-31 17:40:20 +01:00
* This wrapper is used by the ParameterHelper to interface with the on-board parameters
* exposed by the software via the HasParametersIF. A handle of this wrapper is passed
* to the user which then can be used to set or dump the parameters.
*
* The wrapper provides a set of setter functions. The user should call those setter functions,
* supplying an address to the local parameters. The user can also deserialize or
* serialize the parameter data. Please note that this will also serialize and deserialize
* the parameter information field (4 bytes) containing the ECSS PTC, PFC and rows and columns
* number.
2020-08-28 18:46:47 +02:00
*/
class ParameterWrapper: public SerializeIF {
friend class DataPoolParameterWrapper;
public:
static const uint8_t INTERFACE_ID = CLASS_ID::PARAMETER_WRAPPER;
2021-01-31 17:40:20 +01:00
static const ReturnValue_t UNKNOWN_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);
2020-10-29 17:57:27 +01:00
static const ReturnValue_t COLUMN_OR_ROWS_ZERO = MAKE_RETURN_CODE(0x08);
ParameterWrapper();
ParameterWrapper(Type type, uint8_t rows, uint8_t columns, void *data);
2021-01-31 17:40:20 +01:00
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);
2020-08-28 18:46:47 +02:00
/**
* 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<typename T>
2020-08-28 18:46:47 +02:00
ReturnValue_t getElement(T *value, uint8_t row = 0,
uint8_t column = 0) const;
template<typename T>
void set(T *data, uint8_t rows, uint8_t columns) {
this->data = data;
this->readonlyData = data;
this->type = PodTypeConversion<T>::type;
this->rows = rows;
this->columns = columns;
this->pointsToStream = false;
}
template<typename T>
void set(const T *readonlyData, uint8_t rows, uint8_t columns) {
2020-10-29 17:57:27 +01:00
this->data = nullptr;
this->readonlyData = readonlyData;
this->type = PodTypeConversion<T>::type;
this->rows = rows;
this->columns = columns;
this->pointsToStream = false;
}
2021-01-31 17:40:20 +01:00
/**
* Setter function for scalar non-const entries
* @tparam T
* @param member
*/
template<typename T>
void set(T& member) {
this->set(&member, 1, 1);
}
2021-01-31 17:40:20 +01:00
/**
* Setter function for scalar const entries.
* TODO: This is confusing, it should not be called set. Maybe we should call all functions
* assign instead?
* @tparam T
* @param readonlyMember
*/
template<typename T>
void set(const T& readonlyMember) {
this->set(&readonlyMember, 1, 1);
}
template<typename T>
void setVector(T& member) {
2021-01-31 17:40:20 +01:00
/* For a vector entry, the number of rows will be one
(left to right, top to bottom indexing) */
this->set(member, 1, sizeof(member) / sizeof(member[0]));
}
template<typename T>
void setVector(const T& member) {
2021-01-31 17:40:20 +01:00
/* For a vector entry, the number of rows will be one
(left to right, top to bottom indexing) */
this->set(member, 1, sizeof(member) / sizeof(member[0]));
}
template<typename T>
void setMatrix(T& member) {
2020-10-29 17:57:27 +01:00
this->set(member[0], sizeof(member)/sizeof(member[0]),
sizeof(member[0])/sizeof(member[0][0]));
}
template<typename T>
void setMatrix(const T& member) {
2020-10-29 17:57:27 +01:00
this->set(member[0], sizeof(member)/sizeof(member[0]),
sizeof(member[0])/sizeof(member[0][0]));
}
2020-08-28 18:46:47 +02:00
2020-10-29 17:57:27 +01:00
ReturnValue_t set(Type type, uint8_t rows, uint8_t columns,
const void *data, size_t dataSize);
ReturnValue_t set(const uint8_t *stream, size_t streamSize,
2020-08-28 18:46:47 +02:00
const uint8_t **remainingStream = nullptr,
size_t *remainingSize = nullptr);
ReturnValue_t copyFrom(const ParameterWrapper *from,
uint16_t startWritingAtIndex);
private:
2020-10-29 17:57:27 +01:00
void convertLinearIndexToRowAndColumn(uint16_t index,
uint8_t *row, uint8_t *column);
uint16_t convertRowAndColumnToLinearIndex(uint8_t row,
uint8_t column);
2020-08-28 18:46:47 +02:00
bool pointsToStream = false;
Type type;
2020-08-28 18:46:47 +02:00
uint8_t rows = 0;
uint8_t columns = 0;
void *data = nullptr;
const void *readonlyData = nullptr;
template<typename T>
ReturnValue_t serializeData(uint8_t** buffer, size_t* size,
size_t maxSize, Endianness streamEndianness) const;
template<typename T>
ReturnValue_t deSerializeData(uint8_t startingRow, uint8_t startingColumn,
const void *from, uint8_t fromRows, uint8_t fromColumns);
};
2020-08-28 18:46:47 +02:00
template <typename T>
inline ReturnValue_t ParameterWrapper::getElement(T *value, uint8_t row,
uint8_t column) const {
if (readonlyData == nullptr){
return NOT_SET;
}
if (PodTypeConversion<T>::type != type) {
return DATATYPE_MISSMATCH;
}
if ((row >= rows) or (column >= columns)) {
return OUT_OF_BOUNDS;
}
if (pointsToStream) {
const uint8_t *streamWithType = static_cast<const uint8_t*>(readonlyData);
streamWithType += (row * columns + column) * type.getSize();
2020-10-29 17:57:27 +01:00
size_t size = type.getSize();
2020-08-28 18:46:47 +02:00
return SerializeAdapter::deSerialize(value, &streamWithType,
2020-10-29 17:57:27 +01:00
&size, SerializeIF::Endianness::BIG);
2020-08-28 18:46:47 +02:00
}
else {
const T *dataWithType = static_cast<const T*>(readonlyData);
*value = dataWithType[row * columns + column];
return HasReturnvaluesIF::RETURN_OK;
}
}
#endif /* FSFW_PARAMETERS_PARAMETERWRAPPER_H_ */