2016-06-15 23:48:41 +02:00
|
|
|
#include <framework/serialize/SerialBufferAdapter.h>
|
2020-01-13 01:21:53 +01:00
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
2020-01-16 19:07:53 +01:00
|
|
|
#include <cstring>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
2020-06-05 18:20:21 +02:00
|
|
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(const uint8_t* buffer,
|
2020-04-05 15:30:31 +02:00
|
|
|
count_t bufferLength, bool serializeLength) :
|
2020-04-07 22:16:43 +02:00
|
|
|
serializeLength(serializeLength),
|
2020-06-05 18:20:21 +02:00
|
|
|
constBuffer(buffer), buffer(nullptr),
|
|
|
|
bufferLength(bufferLength) {}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
2020-06-05 18:20:21 +02:00
|
|
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(uint8_t* buffer,
|
2020-04-13 16:27:05 +02:00
|
|
|
count_t bufferLength, bool serializeLength) :
|
2020-06-05 20:18:38 +02:00
|
|
|
serializeLength(serializeLength), constBuffer(buffer), buffer(buffer),
|
2020-06-05 18:20:21 +02:00
|
|
|
bufferLength(bufferLength) {}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2019-12-01 17:48:05 +01:00
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
|
|
|
SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
2020-04-13 16:27:05 +02:00
|
|
|
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer,
|
|
|
|
size_t* size, const size_t max_size, bool bigEndian) const {
|
2020-04-07 22:16:43 +02:00
|
|
|
uint32_t serializedLength = bufferLength;
|
|
|
|
if (serializeLength) {
|
2018-07-12 16:29:32 +02:00
|
|
|
serializedLength += AutoSerializeAdapter::getSerializedSize(
|
2020-04-07 22:16:43 +02:00
|
|
|
&bufferLength);
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
if (*size + serializedLength > max_size) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return BUFFER_TOO_SHORT;
|
|
|
|
} else {
|
2020-04-07 22:16:43 +02:00
|
|
|
if (serializeLength) {
|
|
|
|
AutoSerializeAdapter::serialize(&bufferLength, buffer, size,
|
2018-07-12 16:29:32 +02:00
|
|
|
max_size, bigEndian);
|
|
|
|
}
|
2020-04-07 22:16:43 +02:00
|
|
|
if (constBuffer != nullptr) {
|
|
|
|
memcpy(*buffer, constBuffer, bufferLength);
|
2020-06-05 18:20:21 +02:00
|
|
|
} else if (buffer != nullptr) {
|
|
|
|
memcpy(*buffer, buffer, bufferLength);
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2020-04-07 22:16:43 +02:00
|
|
|
*size += bufferLength;
|
|
|
|
(*buffer) += bufferLength;
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
2020-04-05 21:54:11 +02:00
|
|
|
size_t SerialBufferAdapter<count_t>::getSerializedSize() const {
|
2020-04-07 22:16:43 +02:00
|
|
|
if (serializeLength) {
|
|
|
|
return bufferLength + AutoSerializeAdapter::getSerializedSize(&bufferLength);
|
2018-07-12 16:29:32 +02:00
|
|
|
} else {
|
2020-04-07 22:16:43 +02:00
|
|
|
return bufferLength;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-04-15 15:53:46 +02:00
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
|
|
|
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
|
2020-04-15 15:53:46 +02:00
|
|
|
size_t* size, bool bigEndian) {
|
2018-07-12 16:29:32 +02:00
|
|
|
//TODO Ignores Endian flag!
|
2020-04-15 15:53:46 +02:00
|
|
|
if (buffer != nullptr) {
|
|
|
|
if(serializeLength) {
|
2020-04-05 15:30:31 +02:00
|
|
|
count_t serializedSize = AutoSerializeAdapter::getSerializedSize(
|
2020-04-07 22:16:43 +02:00
|
|
|
&bufferLength);
|
2020-06-05 18:20:21 +02:00
|
|
|
if(bufferLength + serializedSize <= *size) {
|
2018-07-12 16:29:32 +02:00
|
|
|
*buffer += serializedSize;
|
|
|
|
*size -= serializedSize;
|
2020-04-15 15:53:46 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-07-12 16:29:32 +02:00
|
|
|
return STREAM_TOO_SHORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//No Else If, go on with buffer
|
2020-06-05 15:53:20 +02:00
|
|
|
if (bufferLength <= *size) {
|
2020-04-07 22:16:43 +02:00
|
|
|
*size -= bufferLength;
|
2020-06-05 18:36:22 +02:00
|
|
|
memcpy(this->buffer, *buffer, bufferLength);
|
2020-04-07 22:16:43 +02:00
|
|
|
(*buffer) += bufferLength;
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2020-04-15 15:53:46 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-15 23:48:41 +02:00
|
|
|
return STREAM_TOO_SHORT;
|
|
|
|
}
|
2020-04-15 15:53:46 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
|
|
|
uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
|
2020-06-05 18:20:21 +02:00
|
|
|
if(buffer == nullptr) {
|
2020-04-23 21:59:24 +02:00
|
|
|
sif::error << "Wrong access function for stored type !"
|
2020-04-13 16:27:05 +02:00
|
|
|
" Use getConstBuffer()" << std::endl;
|
2020-04-05 15:30:31 +02:00
|
|
|
return nullptr;
|
2020-01-13 01:21:53 +01:00
|
|
|
}
|
2020-06-05 18:20:21 +02:00
|
|
|
return buffer;
|
2019-12-01 17:48:05 +01:00
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
|
|
|
const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() {
|
2020-04-07 22:16:43 +02:00
|
|
|
if(constBuffer == nullptr) {
|
2020-06-05 18:20:21 +02:00
|
|
|
sif::error << "SerialBufferAdapter: Buffers are unitialized!" << std::endl;
|
2020-04-05 15:30:31 +02:00
|
|
|
return nullptr;
|
2020-01-13 01:21:53 +01:00
|
|
|
}
|
2020-04-07 22:16:43 +02:00
|
|
|
return constBuffer;
|
2020-01-13 01:21:53 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
2020-06-05 18:20:21 +02:00
|
|
|
void SerialBufferAdapter<count_t>::setBuffer(uint8_t* buffer,
|
2020-04-13 16:27:05 +02:00
|
|
|
count_t buffer_length) {
|
2020-06-05 18:20:21 +02:00
|
|
|
this->buffer = buffer;
|
2020-04-07 22:16:43 +02:00
|
|
|
bufferLength = buffer_length;
|
2020-01-04 16:37:08 +01:00
|
|
|
}
|
2019-12-09 12:27:14 +01:00
|
|
|
|
2020-01-13 01:21:53 +01:00
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
//forward Template declaration for linker
|
|
|
|
template class SerialBufferAdapter<uint8_t>;
|
|
|
|
template class SerialBufferAdapter<uint16_t>;
|
|
|
|
template class SerialBufferAdapter<uint32_t>;
|
|
|
|
|