fsfw/parameters/ParameterWrapper.h

173 lines
5.0 KiB
C++

#ifndef FSFW_PARAMETERS_PARAMETERWRAPPER_H_
#define FSFW_PARAMETERS_PARAMETERWRAPPER_H_
#include "../returnvalues/HasReturnvaluesIF.h"
#include "../serialize/SerializeAdapter.h"
#include "../serialize/SerializeIF.h"
#include "../globalfunctions/Type.h"
#include <cstddef>
/**
* @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);
static const ReturnValue_t COLUMN_OR_ROWS_ZERO = MAKE_RETURN_CODE(0x08);
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<typename T>
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) {
this->data = nullptr;
this->readonlyData = readonlyData;
this->type = PodTypeConversion<T>::type;
this->rows = rows;
this->columns = columns;
this->pointsToStream = false;
}
template<typename T>
void set(T& member) {
this->set(&member, 1, 1);
}
template<typename T>
void set(const T& readonlyMember) {
this->set(&readonlyMember, 1, 1);
}
template<typename T>
void setVector(T& member) {
this->set(member, sizeof(member)/sizeof(member[0]), 1);
}
template<typename T>
void setVector(const T& member) {
this->set(member, 1, sizeof(member)/sizeof(member[0]));
}
template<typename T>
void setMatrix(T& member) {
this->set(member[0], sizeof(member)/sizeof(member[0]),
sizeof(member[0])/sizeof(member[0][0]));
}
template<typename T>
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:
void convertLinearIndexToRowAndColumn(uint16_t index,
uint8_t *row, uint8_t *column);
uint16_t convertRowAndColumnToLinearIndex(uint8_t row,
uint8_t column);
bool pointsToStream = false;
Type type;
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);
};
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();
int32_t size = type.getSize();
return SerializeAdapter::deSerialize(value, &streamWithType,
&size, true);
}
else {
const T *dataWithType = static_cast<const T*>(readonlyData);
*value = dataWithType[row * columns + column];
return HasReturnvaluesIF::RETURN_OK;
}
}
#endif /* FSFW_PARAMETERS_PARAMETERWRAPPER_H_ */