getter/setter functions for serial buffer adapter

This commit is contained in:
Robin Müller 2020-08-24 14:38:59 +02:00
parent 49d4b6ebc7
commit 1b4c4de3fa
2 changed files with 206 additions and 129 deletions

View File

@ -1,94 +1,128 @@
#include "SerialBufferAdapter.h" #include "../serialize/SerialBufferAdapter.h"
#include <cstring> #include "../serviceinterface/ServiceInterfaceStream.h"
#include <cstring>
template<typename count_t>
template<typename T> SerialBufferAdapter<count_t>::SerialBufferAdapter(const uint8_t* buffer,
SerialBufferAdapter<T>::SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength, bool serializeLength) :
T bufferLength, bool serializeLenght) : serializeLength(serializeLength),
serializeLength(serializeLenght), constBuffer(buffer), buffer(NULL), bufferLength( constBuffer(buffer), buffer(nullptr),
bufferLength) { bufferLength(bufferLength) {}
}
template<typename count_t>
template<typename T> SerialBufferAdapter<count_t>::SerialBufferAdapter(uint8_t* buffer,
SerialBufferAdapter<T>::SerialBufferAdapter(uint8_t* buffer, T bufferLength, count_t bufferLength, bool serializeLength) :
bool serializeLenght) : serializeLength(serializeLength), constBuffer(buffer), buffer(buffer),
serializeLength(serializeLenght), constBuffer(NULL), buffer(buffer), bufferLength( bufferLength(bufferLength) {}
bufferLength) {
}
template<typename count_t>
template<typename T> SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
SerialBufferAdapter<T>::~SerialBufferAdapter() { }
}
template<typename T>
template<typename T> ReturnValue_t SerialBufferAdapter<T>::serialize(uint8_t** buffer, size_t* size,
ReturnValue_t SerialBufferAdapter<T>::serialize(uint8_t** buffer, size_t* size, size_t maxSize, Endianness streamEndianness) const {
size_t maxSize, Endianness streamEndianness) const { uint32_t serializedLength = bufferLength;
uint32_t serializedLength = bufferLength; if (serializeLength) {
if (serializeLength) { serializedLength += SerializeAdapter::getSerializedSize(
serializedLength += SerializeAdapter::getSerializedSize( &bufferLength);
&bufferLength); }
} if (*size + serializedLength > maxSize) {
if (*size + serializedLength > maxSize) { return BUFFER_TOO_SHORT;
return BUFFER_TOO_SHORT; } else {
} else { if (serializeLength) {
if (serializeLength) { SerializeAdapter::serialize(&bufferLength, buffer, size,
SerializeAdapter::serialize(&bufferLength, buffer, size, maxSize, streamEndianness);
maxSize, streamEndianness); }
} if (constBuffer != nullptr) {
if (this->constBuffer != NULL) { memcpy(*buffer, this->constBuffer, bufferLength);
memcpy(*buffer, this->constBuffer, bufferLength); }
} else if (this->buffer != NULL) { else if (buffer != nullptr) {
memcpy(*buffer, this->buffer, bufferLength); // This will propably be never reached, constBuffer should always be
} else { // set if non-const buffer is set.
return HasReturnvaluesIF::RETURN_FAILED; memcpy(*buffer, this->buffer, bufferLength);
} }
*size += bufferLength; else {
(*buffer) += bufferLength; return HasReturnvaluesIF::RETURN_FAILED;
return HasReturnvaluesIF::RETURN_OK; }
} *size += bufferLength;
} (*buffer) += bufferLength;
return HasReturnvaluesIF::RETURN_OK;
template<typename T> }
size_t SerialBufferAdapter<T>::getSerializedSize() const { }
if (serializeLength) {
return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength); template<typename T>
} else { size_t SerialBufferAdapter<T>::getSerializedSize() const {
return bufferLength; if (serializeLength) {
} return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength);
} } else {
template<typename T> return bufferLength;
ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer, }
size_t* size, Endianness streamEndianness) { }
//TODO Ignores Endian flag!
if (buffer != NULL) { template<typename T>
if(serializeLength){ ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer,
T serializedSize = SerializeAdapter::getSerializedSize( size_t* size, Endianness streamEndianness) {
&bufferLength); //TODO Ignores Endian flag!
if((*size - bufferLength - serializedSize) >= 0){ if (buffer != NULL) {
*buffer += serializedSize; if(serializeLength){
*size -= serializedSize; T serializedSize = SerializeAdapter::getSerializedSize(
}else{ &bufferLength);
return STREAM_TOO_SHORT; if(bufferLength + serializedSize <= *size) {
} *buffer += serializedSize;
} *size -= serializedSize;
//No Else If, go on with buffer }
if (*size - bufferLength >= 0) { else {
*size -= bufferLength; return STREAM_TOO_SHORT;
memcpy(this->buffer, *buffer, bufferLength); }
(*buffer) += bufferLength; }
return HasReturnvaluesIF::RETURN_OK; //No Else If, go on with buffer
} else { if (bufferLength <= *size) {
return STREAM_TOO_SHORT; *size -= bufferLength;
} memcpy(this->buffer, *buffer, bufferLength);
} else { (*buffer) += bufferLength;
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_OK;
} }
} else {
return STREAM_TOO_SHORT;
}
//forward Template declaration for linker }
template class SerialBufferAdapter<uint8_t>; else {
template class SerialBufferAdapter<uint16_t>; return HasReturnvaluesIF::RETURN_FAILED;
template class SerialBufferAdapter<uint32_t>; }
}
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: 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;
}
//forward Template declaration for linker
template class SerialBufferAdapter<uint8_t>;
template class SerialBufferAdapter<uint16_t>;
template class SerialBufferAdapter<uint32_t>;

View File

@ -1,35 +1,78 @@
#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"
/** /**
* \ingroup serialize * This adapter provides an interface for SerializeIF to serialize or deserialize
*/ * buffers with no length header but a known size.
template<typename T> *
class SerialBufferAdapter: public SerializeIF { * Additionally, the buffer length can be serialized too and will be put in
public: * front of the serialized buffer.
SerialBufferAdapter(const uint8_t * buffer, T bufferLength, bool serializeLenght = false); *
SerialBufferAdapter(uint8_t* buffer, T bufferLength, * Can be used with SerialLinkedListAdapter by declaring a SerializeElement with
bool serializeLenght = false); * SerialElement<SerialBufferAdapter<bufferLengthType(will be uint8_t mostly)>>.
* Right now, the SerialBufferAdapter must always
virtual ~SerialBufferAdapter(); * be initialized with the buffer and size !
*
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, * \ingroup serialize
size_t maxSize, Endianness streamEndianness) const override; */
template<typename count_t>
virtual size_t getSerializedSize() const override; class SerialBufferAdapter: public SerializeIF {
public:
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
Endianness streamEndianness) override; /**
private: * Constructor for constant uint8_t buffer. Length field can be serialized optionally.
bool serializeLength; * Type of length can be supplied as template type.
const uint8_t *constBuffer; * @param buffer
uint8_t *buffer; * @param bufferLength
T bufferLength; * @param serializeLength
}; */
SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength,
bool serializeLength = false);
#endif /* SERIALBUFFERADAPTER_H_ */ /**
* 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_ */