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>
|
|
|
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(const void* buffer,
|
|
|
|
count_t bufferLength, bool serializeLength) :
|
2020-04-07 22:16:43 +02:00
|
|
|
serializeLength(serializeLength),
|
|
|
|
constBuffer(static_cast<const uint8_t *>(buffer)), m_buffer(nullptr),
|
|
|
|
bufferLength(bufferLength) {
|
2020-01-13 01:21:53 +01:00
|
|
|
|
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
|
|
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(void* buffer,
|
|
|
|
count_t bufferLength, bool serializeLength) :
|
2020-04-07 22:16:43 +02:00
|
|
|
serializeLength(serializeLength), bufferLength(bufferLength) {
|
2020-04-05 15:30:31 +02:00
|
|
|
uint8_t * member_buffer = static_cast<uint8_t *>(buffer);
|
|
|
|
m_buffer = member_buffer;
|
2020-04-07 22:16:43 +02:00
|
|
|
constBuffer = member_buffer;
|
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-04-05 15:33:24 +02:00
|
|
|
} else if (m_buffer != nullptr) {
|
2020-04-07 22:16:43 +02:00
|
|
|
memcpy(*buffer, m_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-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
|
|
|
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
|
2020-04-05 22:42:48 +02:00
|
|
|
ssize_t* size, bool bigEndian) {
|
2018-07-12 16:29:32 +02:00
|
|
|
//TODO Ignores Endian flag!
|
2016-06-15 23:48:41 +02:00
|
|
|
if (buffer != NULL) {
|
2020-04-07 22:16:43 +02:00
|
|
|
if(serializeLength){
|
2020-01-18 15:03:22 +01:00
|
|
|
// Suggestion (would require removing rest of the block inside this if clause !):
|
|
|
|
//ReturnValue_t result = AutoSerializeAdapter::deSerialize(&bufferLength,buffer,size,bigEndian);
|
|
|
|
//if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
// return result;
|
|
|
|
//}
|
2020-04-05 15:30:31 +02:00
|
|
|
count_t serializedSize = AutoSerializeAdapter::getSerializedSize(
|
2020-04-07 22:16:43 +02:00
|
|
|
&bufferLength);
|
|
|
|
if((*size - bufferLength - serializedSize) >= 0){
|
2018-07-12 16:29:32 +02:00
|
|
|
*buffer += serializedSize;
|
|
|
|
*size -= serializedSize;
|
|
|
|
}else{
|
|
|
|
return STREAM_TOO_SHORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//No Else If, go on with buffer
|
2020-04-07 22:16:43 +02:00
|
|
|
if (*size - bufferLength >= 0) {
|
|
|
|
*size -= bufferLength;
|
|
|
|
memcpy(m_buffer, *buffer, bufferLength);
|
|
|
|
(*buffer) += bufferLength;
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return STREAM_TOO_SHORT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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() {
|
|
|
|
if(m_buffer == nullptr) {
|
2020-04-13 16:27:05 +02:00
|
|
|
error << "Wrong access function for stored type !"
|
|
|
|
" Use getConstBuffer()" << std::endl;
|
2020-04-05 15:30:31 +02:00
|
|
|
return nullptr;
|
2020-01-13 01:21:53 +01:00
|
|
|
}
|
2020-04-05 15:30:31 +02:00
|
|
|
return m_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-04-13 16:27:05 +02:00
|
|
|
error << "Wrong access function for stored type !"
|
|
|
|
" Use getBuffer()" << 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-04-13 16:27:05 +02:00
|
|
|
void SerialBufferAdapter<count_t>::setBuffer(void * buffer,
|
|
|
|
count_t buffer_length) {
|
2020-04-05 15:30:31 +02:00
|
|
|
m_buffer = static_cast<uint8_t *>(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>;
|
|
|
|
|