Merge pull request 'getter/setter functions for serial buffer adapter' (#170) from KSat/fsfw:mueller/SerialBufferAdapter into master
This commit is contained in:
commit
e0a2d09716
@ -1,89 +1,123 @@
|
|||||||
#include "SerialBufferAdapter.h"
|
#include "../serialize/SerialBufferAdapter.h"
|
||||||
|
#include "../serviceinterface/ServiceInterfaceStream.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
template<typename count_t>
|
||||||
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(const uint8_t* buffer,
|
||||||
|
count_t bufferLength, bool serializeLength) :
|
||||||
|
serializeLength(serializeLength),
|
||||||
|
constBuffer(buffer), buffer(nullptr),
|
||||||
|
bufferLength(bufferLength) {}
|
||||||
|
|
||||||
|
template<typename count_t>
|
||||||
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(uint8_t* buffer,
|
||||||
|
count_t bufferLength, bool serializeLength) :
|
||||||
|
serializeLength(serializeLength), constBuffer(buffer), buffer(buffer),
|
||||||
|
bufferLength(bufferLength) {}
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
SerialBufferAdapter<T>::SerialBufferAdapter(const uint8_t* buffer,
|
SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
|
||||||
T bufferLength, bool serializeLenght) :
|
|
||||||
serializeLength(serializeLenght), constBuffer(buffer), buffer(NULL), bufferLength(
|
|
||||||
bufferLength) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
SerialBufferAdapter<T>::SerialBufferAdapter(uint8_t* buffer, T bufferLength,
|
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer,
|
||||||
bool serializeLenght) :
|
size_t* size, size_t maxSize, Endianness streamEndianness) const {
|
||||||
serializeLength(serializeLenght), constBuffer(NULL), buffer(buffer), bufferLength(
|
|
||||||
bufferLength) {
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
SerialBufferAdapter<T>::~SerialBufferAdapter() {
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
ReturnValue_t SerialBufferAdapter<T>::serialize(uint8_t** buffer, size_t* size,
|
|
||||||
size_t maxSize, Endianness streamEndianness) const {
|
|
||||||
uint32_t serializedLength = bufferLength;
|
|
||||||
if (serializeLength) {
|
if (serializeLength) {
|
||||||
serializedLength += SerializeAdapter::getSerializedSize(
|
ReturnValue_t result = SerializeAdapter::serialize(&bufferLength,
|
||||||
&bufferLength);
|
buffer, size, maxSize, streamEndianness);
|
||||||
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
if (*size + serializedLength > maxSize) {
|
}
|
||||||
|
|
||||||
|
if (*size + bufferLength > maxSize) {
|
||||||
return BUFFER_TOO_SHORT;
|
return BUFFER_TOO_SHORT;
|
||||||
} else {
|
|
||||||
if (serializeLength) {
|
|
||||||
SerializeAdapter::serialize(&bufferLength, buffer, size,
|
|
||||||
maxSize, streamEndianness);
|
|
||||||
}
|
}
|
||||||
if (this->constBuffer != NULL) {
|
|
||||||
memcpy(*buffer, this->constBuffer, bufferLength);
|
if (this->constBuffer != nullptr) {
|
||||||
} else if (this->buffer != NULL) {
|
std::memcpy(*buffer, this->constBuffer, bufferLength);
|
||||||
memcpy(*buffer, this->buffer, bufferLength);
|
}
|
||||||
} else {
|
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;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
*size += bufferLength;
|
*size += bufferLength;
|
||||||
(*buffer) += bufferLength;
|
(*buffer) += bufferLength;
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
size_t SerialBufferAdapter<T>::getSerializedSize() const {
|
size_t SerialBufferAdapter<count_t>::getSerializedSize() const {
|
||||||
if (serializeLength) {
|
if (serializeLength) {
|
||||||
return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength);
|
return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength);
|
||||||
} else {
|
} else {
|
||||||
return bufferLength;
|
return bufferLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
template<typename T>
|
|
||||||
ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer,
|
template<typename count_t>
|
||||||
|
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
|
||||||
size_t* size, Endianness streamEndianness) {
|
size_t* size, Endianness streamEndianness) {
|
||||||
//TODO Ignores Endian flag!
|
if (this->buffer == nullptr) {
|
||||||
if (buffer != NULL) {
|
|
||||||
if(serializeLength){
|
|
||||||
T serializedSize = SerializeAdapter::getSerializedSize(
|
|
||||||
&bufferLength);
|
|
||||||
if((*size - bufferLength - serializedSize) >= 0){
|
|
||||||
*buffer += serializedSize;
|
|
||||||
*size -= serializedSize;
|
|
||||||
}else{
|
|
||||||
return STREAM_TOO_SHORT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//No Else If, go on with buffer
|
|
||||||
if (*size - bufferLength >= 0) {
|
|
||||||
*size -= bufferLength;
|
|
||||||
memcpy(this->buffer, *buffer, bufferLength);
|
|
||||||
(*buffer) += bufferLength;
|
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
|
||||||
} else {
|
|
||||||
return STREAM_TOO_SHORT;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
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<typename count_t>
|
||||||
|
uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
|
||||||
|
if(buffer == nullptr) {
|
||||||
|
sif::error << "Wrong access function for stored type !"
|
||||||
|
" Use getConstBuffer()." << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename count_t>
|
||||||
|
const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() {
|
||||||
|
if(constBuffer == nullptr) {
|
||||||
|
sif::error << "SerialBufferAdapter::getConstBuffer:"
|
||||||
|
" Buffers are unitialized!" << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return constBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename count_t>
|
||||||
|
void SerialBufferAdapter<count_t>::setBuffer(uint8_t* buffer,
|
||||||
|
count_t bufferLength) {
|
||||||
|
this->buffer = buffer;
|
||||||
|
this->constBuffer = buffer;
|
||||||
|
this->bufferLength = bufferLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -91,4 +125,5 @@ ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer,
|
|||||||
template class SerialBufferAdapter<uint8_t>;
|
template class SerialBufferAdapter<uint8_t>;
|
||||||
template class SerialBufferAdapter<uint16_t>;
|
template class SerialBufferAdapter<uint16_t>;
|
||||||
template class SerialBufferAdapter<uint32_t>;
|
template class SerialBufferAdapter<uint32_t>;
|
||||||
|
template class SerialBufferAdapter<uint64_t>;
|
||||||
|
|
||||||
|
@ -1,18 +1,47 @@
|
|||||||
#ifndef SERIALBUFFERADAPTER_H_
|
#ifndef SERIALBUFFERADAPTER_H_
|
||||||
#define SERIALBUFFERADAPTER_H_
|
#define SERIALBUFFERADAPTER_H_
|
||||||
|
|
||||||
#include "SerializeIF.h"
|
#include "../serialize/SerializeIF.h"
|
||||||
#include "SerializeAdapter.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<SerialBufferAdapter<bufferLengthType(will be uint8_t mostly)>>.
|
||||||
|
* Right now, the SerialBufferAdapter must always
|
||||||
|
* be initialized with the buffer and size !
|
||||||
|
*
|
||||||
* \ingroup serialize
|
* \ingroup serialize
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
class SerialBufferAdapter: public SerializeIF {
|
class SerialBufferAdapter: public SerializeIF {
|
||||||
public:
|
public:
|
||||||
SerialBufferAdapter(const uint8_t * buffer, T bufferLength, bool serializeLenght = false);
|
|
||||||
SerialBufferAdapter(uint8_t* buffer, T bufferLength,
|
/**
|
||||||
bool serializeLenght = false);
|
* 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 ~SerialBufferAdapter();
|
||||||
|
|
||||||
@ -21,15 +50,29 @@ public:
|
|||||||
|
|
||||||
virtual size_t getSerializedSize() 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,
|
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
||||||
Endianness streamEndianness) override;
|
Endianness streamEndianness) override;
|
||||||
|
|
||||||
|
uint8_t * getBuffer();
|
||||||
|
const uint8_t * getConstBuffer();
|
||||||
|
void setBuffer(uint8_t* buffer, count_t bufferLength);
|
||||||
private:
|
private:
|
||||||
bool serializeLength;
|
bool serializeLength = false;
|
||||||
const uint8_t *constBuffer;
|
const uint8_t *constBuffer = nullptr;
|
||||||
uint8_t *buffer;
|
uint8_t *buffer = nullptr;
|
||||||
T bufferLength;
|
count_t bufferLength = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* SERIALBUFFERADAPTER_H_ */
|
#endif /* SERIALBUFFERADAPTER_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user