2020-08-18 13:09:15 +02:00
|
|
|
#include "../serialize/SerialBufferAdapter.h"
|
|
|
|
#include "../serviceinterface/ServiceInterfaceStream.h"
|
|
|
|
#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 count_t>
|
|
|
|
SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
|
|
|
|
}
|
|
|
|
|
2020-08-25 13:17:57 +02:00
|
|
|
template<typename count_t>
|
|
|
|
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer,
|
|
|
|
size_t* size, size_t maxSize, Endianness streamEndianness) const {
|
2020-08-18 13:09:15 +02:00
|
|
|
uint32_t serializedLength = bufferLength;
|
|
|
|
if (serializeLength) {
|
|
|
|
serializedLength += SerializeAdapter::getSerializedSize(
|
|
|
|
&bufferLength);
|
|
|
|
}
|
|
|
|
if (*size + serializedLength > maxSize) {
|
|
|
|
return BUFFER_TOO_SHORT;
|
2020-08-24 14:48:22 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-08-18 13:09:15 +02:00
|
|
|
if (serializeLength) {
|
|
|
|
SerializeAdapter::serialize(&bufferLength, buffer, size,
|
|
|
|
maxSize, streamEndianness);
|
|
|
|
}
|
2020-08-24 14:48:22 +02:00
|
|
|
if (this->constBuffer != nullptr) {
|
2020-08-18 13:09:15 +02:00
|
|
|
memcpy(*buffer, this->constBuffer, bufferLength);
|
|
|
|
}
|
2020-08-24 14:48:22 +02:00
|
|
|
else if (this->buffer != nullptr) {
|
2020-08-18 13:09:15 +02:00
|
|
|
// This will propably be never reached, constBuffer should always be
|
|
|
|
// set if non-const buffer is set.
|
|
|
|
memcpy(*buffer, this->buffer, bufferLength);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
*size += bufferLength;
|
|
|
|
(*buffer) += bufferLength;
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 13:17:57 +02:00
|
|
|
template<typename count_t>
|
|
|
|
size_t SerialBufferAdapter<count_t>::getSerializedSize() const {
|
2020-08-18 13:09:15 +02:00
|
|
|
if (serializeLength) {
|
|
|
|
return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength);
|
|
|
|
} else {
|
|
|
|
return bufferLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 13:17:57 +02:00
|
|
|
template<typename count_t>
|
|
|
|
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
|
2020-08-18 13:09:15 +02:00
|
|
|
size_t* size, Endianness streamEndianness) {
|
2020-08-25 13:29:10 +02:00
|
|
|
if (this->buffer == nullptr) {
|
|
|
|
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;
|
2020-08-18 13:09:15 +02:00
|
|
|
}
|
2020-08-25 13:29:10 +02:00
|
|
|
if(lengthField > bufferLength) {
|
|
|
|
return TOO_MANY_ELEMENTS;
|
2020-08-18 13:09:15 +02:00
|
|
|
}
|
2020-08-25 13:29:10 +02:00
|
|
|
bufferLength = lengthField;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bufferLength <= *size) {
|
|
|
|
*size -= bufferLength;
|
|
|
|
std::memcpy(this->buffer, *buffer, bufferLength);
|
|
|
|
(*buffer) += bufferLength;
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2020-08-18 13:09:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-08-25 13:29:10 +02:00
|
|
|
return STREAM_TOO_SHORT;
|
2020-08-18 13:09:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-08-25 13:17:57 +02:00
|
|
|
sif::error << "SerialBufferAdapter::getConstBuffer:"
|
|
|
|
" Buffers are unitialized!" << std::endl;
|
2020-08-18 13:09:15 +02:00
|
|
|
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>;
|
|
|
|
|