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
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
template<typename T>
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t SerialBufferAdapter<T>::serialize(uint8_t** buffer, size_t* size,
|
|
|
|
size_t maxSize, Endianness streamEndianness) const {
|
2020-04-07 22:16:43 +02:00
|
|
|
uint32_t serializedLength = bufferLength;
|
|
|
|
if (serializeLength) {
|
2020-04-21 21:34:03 +02:00
|
|
|
serializedLength += SerializeAdapter::getSerializedSize(
|
2020-04-07 22:16:43 +02:00
|
|
|
&bufferLength);
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
2020-04-21 22:28:43 +02:00
|
|
|
if (*size + serializedLength > maxSize) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return BUFFER_TOO_SHORT;
|
|
|
|
} else {
|
2020-04-07 22:16:43 +02:00
|
|
|
if (serializeLength) {
|
2020-04-21 21:34:03 +02:00
|
|
|
SerializeAdapter::serialize(&bufferLength, buffer, size,
|
2020-04-21 22:28:43 +02:00
|
|
|
maxSize, streamEndianness);
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
2020-04-07 22:16:43 +02:00
|
|
|
if (constBuffer != nullptr) {
|
2020-07-01 14:17:55 +02:00
|
|
|
memcpy(*buffer, this->constBuffer, bufferLength);
|
2020-06-11 16:46:18 +02:00
|
|
|
}
|
|
|
|
else if (buffer != nullptr) {
|
|
|
|
// This will propably be never reached, constBuffer should always be
|
|
|
|
// set if non-const buffer is set.
|
2020-07-01 14:17:55 +02:00
|
|
|
memcpy(*buffer, this->buffer, bufferLength);
|
2020-06-11 16:46:18 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2020-07-01 14:17:55 +02:00
|
|
|
*size += bufferLength;
|
|
|
|
(*buffer) += bufferLength;
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
template<typename T>
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t SerialBufferAdapter<T>::getSerializedSize() const {
|
2020-04-07 22:16:43 +02:00
|
|
|
if (serializeLength) {
|
2020-04-21 21:34:03 +02:00
|
|
|
return bufferLength + SerializeAdapter::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
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
template<typename T>
|
|
|
|
ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer,
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t* size, Endianness streamEndianness) {
|
2018-07-12 16:29:32 +02:00
|
|
|
//TODO Ignores Endian flag!
|
2016-06-15 23:48:41 +02:00
|
|
|
if (buffer != NULL) {
|
2018-07-12 16:29:32 +02:00
|
|
|
if(serializeLength){
|
2020-04-21 21:34:03 +02:00
|
|
|
T serializedSize = SerializeAdapter::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-06-11 16:46:18 +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-06-11 16:46:18 +02:00
|
|
|
count_t bufferLength) {
|
2020-06-05 18:20:21 +02:00
|
|
|
this->buffer = buffer;
|
2020-06-11 16:46:18 +02:00
|
|
|
this->constBuffer = buffer;
|
|
|
|
this->bufferLength = bufferLength;
|
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>;
|
|
|
|
|