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) :
|
|
|
|
m_serialize_length(serializeLength),
|
|
|
|
m_const_buffer(static_cast<const uint8_t *>(buffer)), m_buffer(nullptr),
|
|
|
|
m_buffer_length(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>
|
|
|
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(void* buffer, count_t bufferLength,
|
2019-11-02 23:30:12 +01:00
|
|
|
bool serializeLength) :
|
2020-04-05 15:30:31 +02:00
|
|
|
m_serialize_length(serializeLength), m_buffer_length(bufferLength) {
|
|
|
|
uint8_t * member_buffer = static_cast<uint8_t *>(buffer);
|
|
|
|
m_buffer = member_buffer;
|
|
|
|
m_const_buffer = 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-05 17:58:39 +02:00
|
|
|
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer, size_t* size,
|
|
|
|
const size_t max_size, bool bigEndian) const {
|
2020-04-05 15:30:31 +02:00
|
|
|
uint32_t serializedLength = m_buffer_length;
|
|
|
|
if (m_serialize_length) {
|
2018-07-12 16:29:32 +02:00
|
|
|
serializedLength += AutoSerializeAdapter::getSerializedSize(
|
2020-04-05 15:30:31 +02:00
|
|
|
&m_buffer_length);
|
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-05 15:30:31 +02:00
|
|
|
if (m_serialize_length) {
|
|
|
|
AutoSerializeAdapter::serialize(&m_buffer_length, buffer, size,
|
2018-07-12 16:29:32 +02:00
|
|
|
max_size, bigEndian);
|
|
|
|
}
|
2020-04-05 15:33:24 +02:00
|
|
|
if (m_const_buffer != nullptr) {
|
2020-04-05 15:30:31 +02:00
|
|
|
memcpy(*buffer, m_const_buffer, m_buffer_length);
|
2020-04-05 15:33:24 +02:00
|
|
|
} else if (m_buffer != nullptr) {
|
2020-04-05 15:30:31 +02:00
|
|
|
memcpy(*buffer, m_buffer, m_buffer_length);
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2020-04-05 15:30:31 +02:00
|
|
|
*size += m_buffer_length;
|
|
|
|
(*buffer) += m_buffer_length;
|
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-05 15:30:31 +02:00
|
|
|
if (m_serialize_length) {
|
|
|
|
return m_buffer_length + AutoSerializeAdapter::getSerializedSize(&m_buffer_length);
|
2018-07-12 16:29:32 +02:00
|
|
|
} else {
|
2020-04-05 15:30:31 +02:00
|
|
|
return m_buffer_length;
|
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-05 15:30:31 +02:00
|
|
|
if(m_serialize_length){
|
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(
|
|
|
|
&m_buffer_length);
|
|
|
|
if((*size - m_buffer_length - 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-05 15:30:31 +02:00
|
|
|
if (*size - m_buffer_length >= 0) {
|
|
|
|
*size -= m_buffer_length;
|
|
|
|
memcpy(m_buffer, *buffer, m_buffer_length);
|
|
|
|
(*buffer) += m_buffer_length;
|
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) {
|
|
|
|
error << "Wrong access function for stored type ! Use getConstBuffer()" << std::endl;
|
|
|
|
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() {
|
|
|
|
if(m_const_buffer == nullptr) {
|
|
|
|
error << "Wrong access function for stored type ! Use getBuffer()" << std::endl;
|
|
|
|
return nullptr;
|
2020-01-13 01:21:53 +01:00
|
|
|
}
|
2020-04-05 15:30:31 +02:00
|
|
|
return m_const_buffer;
|
2020-01-13 01:21:53 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
|
|
|
void SerialBufferAdapter<count_t>::setBuffer(void * buffer, count_t buffer_length) {
|
|
|
|
m_buffer = static_cast<uint8_t *>(buffer);
|
|
|
|
m_buffer_length = 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>;
|
|
|
|
|