fsfw/serialize/SerialBufferAdapter.cpp

131 lines
3.7 KiB
C++
Raw Normal View History

#include <framework/serialize/SerialBufferAdapter.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
2020-01-16 19:07:53 +01:00
#include <cstring>
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-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;
}
2020-04-05 15:30:31 +02:00
template<typename count_t>
SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
}
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) {
serializedLength += AutoSerializeAdapter::getSerializedSize(
2020-04-07 22:16:43 +02:00
&bufferLength);
}
if (*size + serializedLength > max_size) {
return BUFFER_TOO_SHORT;
} else {
2020-04-07 22:16:43 +02:00
if (serializeLength) {
AutoSerializeAdapter::serialize(&bufferLength, buffer, size,
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);
} else {
return HasReturnvaluesIF::RETURN_FAILED;
}
2020-04-07 22:16:43 +02:00
*size += bufferLength;
(*buffer) += bufferLength;
return HasReturnvaluesIF::RETURN_OK;
}
}
2020-04-05 15:30:31 +02:00
template<typename count_t>
size_t SerialBufferAdapter<count_t>::getSerializedSize() const {
2020-04-07 22:16:43 +02:00
if (serializeLength) {
return bufferLength + AutoSerializeAdapter::getSerializedSize(&bufferLength);
} else {
2020-04-07 22:16:43 +02:00
return bufferLength;
}
}
2020-04-05 15:30:31 +02:00
template<typename count_t>
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
ssize_t* size, bool bigEndian) {
//TODO Ignores Endian flag!
if (buffer != NULL) {
2020-04-07 22:16:43 +02:00
if(serializeLength){
// 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){
*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;
return HasReturnvaluesIF::RETURN_OK;
} else {
return STREAM_TOO_SHORT;
}
} else {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
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-04-05 15:30:31 +02:00
return m_buffer;
}
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-04-07 22:16:43 +02:00
return constBuffer;
}
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;
}
//forward Template declaration for linker
template class SerialBufferAdapter<uint8_t>;
template class SerialBufferAdapter<uint16_t>;
template class SerialBufferAdapter<uint32_t>;