serial buffer adapted improvements
This commit is contained in:
parent
80b1d28bc8
commit
4a35035b28
@ -2,83 +2,79 @@
|
|||||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
SerialBufferAdapter<T>::SerialBufferAdapter(const uint8_t* buffer,
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(const void* buffer,
|
||||||
T bufferLength, bool serializeLength) :
|
count_t bufferLength, bool serializeLength) :
|
||||||
currentBufferType(bufferType::CONST), serializeLength(serializeLength),
|
m_serialize_length(serializeLength),
|
||||||
constBuffer(buffer), buffer(NULL), bufferLength(bufferLength) {
|
m_const_buffer(static_cast<const uint8_t *>(buffer)), m_buffer(nullptr),
|
||||||
|
m_buffer_length(bufferLength) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
SerialBufferAdapter<T>::SerialBufferAdapter(uint8_t* buffer, T bufferLength,
|
SerialBufferAdapter<count_t>::SerialBufferAdapter(void* buffer, count_t bufferLength,
|
||||||
bool serializeLength) :
|
bool serializeLength) :
|
||||||
currentBufferType(bufferType::NORMAL),serializeLength(serializeLength), constBuffer(NULL), buffer(buffer),
|
m_serialize_length(serializeLength), m_buffer_length(bufferLength) {
|
||||||
bufferLength(bufferLength) {
|
uint8_t * member_buffer = static_cast<uint8_t *>(buffer);
|
||||||
|
m_buffer = member_buffer;
|
||||||
|
m_const_buffer = member_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
SerialBufferAdapter<T>::SerialBufferAdapter(uint32_t* buffer,
|
template<typename count_t>
|
||||||
T bufferLength, bool serializeLength) :
|
SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
|
||||||
currentBufferType(bufferType::NORMAL),serializeLength(serializeLength),
|
|
||||||
constBuffer(NULL), buffer(reinterpret_cast<uint8_t *>(buffer)),
|
|
||||||
bufferLength(bufferLength*4) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
SerialBufferAdapter<T>::~SerialBufferAdapter() {
|
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer, uint32_t* size,
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
ReturnValue_t SerialBufferAdapter<T>::serialize(uint8_t** buffer, uint32_t* size,
|
|
||||||
const uint32_t max_size, bool bigEndian) const {
|
const uint32_t max_size, bool bigEndian) const {
|
||||||
uint32_t serializedLength = bufferLength;
|
uint32_t serializedLength = m_buffer_length;
|
||||||
if (serializeLength) {
|
if (m_serialize_length) {
|
||||||
serializedLength += AutoSerializeAdapter::getSerializedSize(
|
serializedLength += AutoSerializeAdapter::getSerializedSize(
|
||||||
&bufferLength);
|
&m_buffer_length);
|
||||||
}
|
}
|
||||||
if (*size + serializedLength > max_size) {
|
if (*size + serializedLength > max_size) {
|
||||||
return BUFFER_TOO_SHORT;
|
return BUFFER_TOO_SHORT;
|
||||||
} else {
|
} else {
|
||||||
if (serializeLength) {
|
if (m_serialize_length) {
|
||||||
AutoSerializeAdapter::serialize(&bufferLength, buffer, size,
|
AutoSerializeAdapter::serialize(&m_buffer_length, buffer, size,
|
||||||
max_size, bigEndian);
|
max_size, bigEndian);
|
||||||
}
|
}
|
||||||
if (this->constBuffer != NULL) {
|
if (this->m_const_buffer != nullptr) {
|
||||||
memcpy(*buffer, this->constBuffer, bufferLength);
|
memcpy(*buffer, m_const_buffer, m_buffer_length);
|
||||||
} else if (this->buffer != NULL) {
|
} else if (this->m_buffer != nullptr) {
|
||||||
memcpy(*buffer, this->buffer, bufferLength);
|
memcpy(*buffer, m_buffer, m_buffer_length);
|
||||||
} else {
|
} else {
|
||||||
return HasReturnvaluesIF::RETURN_FAILED;
|
return HasReturnvaluesIF::RETURN_FAILED;
|
||||||
}
|
}
|
||||||
*size += bufferLength;
|
*size += m_buffer_length;
|
||||||
(*buffer) += bufferLength;
|
(*buffer) += m_buffer_length;
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
uint32_t SerialBufferAdapter<T>::getSerializedSize() const {
|
uint32_t SerialBufferAdapter<count_t>::getSerializedSize() const {
|
||||||
if (serializeLength) {
|
if (m_serialize_length) {
|
||||||
return bufferLength + AutoSerializeAdapter::getSerializedSize(&bufferLength);
|
return m_buffer_length + AutoSerializeAdapter::getSerializedSize(&m_buffer_length);
|
||||||
} else {
|
} else {
|
||||||
return bufferLength;
|
return m_buffer_length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer,
|
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
|
||||||
int32_t* size, bool bigEndian) {
|
int32_t* size, bool bigEndian) {
|
||||||
//TODO Ignores Endian flag!
|
//TODO Ignores Endian flag!
|
||||||
if (buffer != NULL) {
|
if (buffer != NULL) {
|
||||||
if(serializeLength){
|
if(m_serialize_length){
|
||||||
// Suggestion (would require removing rest of the block inside this if clause !):
|
// Suggestion (would require removing rest of the block inside this if clause !):
|
||||||
//ReturnValue_t result = AutoSerializeAdapter::deSerialize(&bufferLength,buffer,size,bigEndian);
|
//ReturnValue_t result = AutoSerializeAdapter::deSerialize(&bufferLength,buffer,size,bigEndian);
|
||||||
//if (result != HasReturnvaluesIF::RETURN_OK) {
|
//if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||||
// return result;
|
// return result;
|
||||||
//}
|
//}
|
||||||
T serializedSize = AutoSerializeAdapter::getSerializedSize(
|
count_t serializedSize = AutoSerializeAdapter::getSerializedSize(
|
||||||
&bufferLength);
|
&m_buffer_length);
|
||||||
if((*size - bufferLength - serializedSize) >= 0){
|
if((*size - m_buffer_length - serializedSize) >= 0){
|
||||||
*buffer += serializedSize;
|
*buffer += serializedSize;
|
||||||
*size -= serializedSize;
|
*size -= serializedSize;
|
||||||
}else{
|
}else{
|
||||||
@ -86,10 +82,10 @@ ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//No Else If, go on with buffer
|
//No Else If, go on with buffer
|
||||||
if (*size - bufferLength >= 0) {
|
if (*size - m_buffer_length >= 0) {
|
||||||
*size -= bufferLength;
|
*size -= m_buffer_length;
|
||||||
memcpy(this->buffer, *buffer, bufferLength);
|
memcpy(m_buffer, *buffer, m_buffer_length);
|
||||||
(*buffer) += bufferLength;
|
(*buffer) += m_buffer_length;
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
} else {
|
} else {
|
||||||
return STREAM_TOO_SHORT;
|
return STREAM_TOO_SHORT;
|
||||||
@ -99,35 +95,30 @@ ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
uint8_t * SerialBufferAdapter<T>::getBuffer() {
|
uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
|
||||||
if(currentBufferType != NORMAL) {
|
if(m_buffer == nullptr) {
|
||||||
warning << "Wrong access function for stored type ! Use getConstBuffer()" << std::endl;
|
error << "Wrong access function for stored type ! Use getConstBuffer()" << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return buffer;
|
return m_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
const uint8_t * SerialBufferAdapter<T>::getConstBuffer() {
|
const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() {
|
||||||
if(currentBufferType != CONST) {
|
if(m_const_buffer == nullptr) {
|
||||||
warning << "Wrong access function for stored type ! Use getBuffer()" << std::endl;
|
error << "Wrong access function for stored type ! Use getBuffer()" << std::endl;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return constBuffer;
|
return m_const_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
void SerialBufferAdapter<T>::setBuffer(uint8_t * buffer_, T bufferLength_) {
|
void SerialBufferAdapter<count_t>::setBuffer(void * buffer, count_t buffer_length) {
|
||||||
buffer = buffer_;
|
m_buffer = static_cast<uint8_t *>(buffer);
|
||||||
bufferLength = bufferLength_;
|
m_buffer_length = buffer_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void SerialBufferAdapter<T>::setBuffer(uint32_t * buffer_, T bufferLength_) {
|
|
||||||
buffer = reinterpret_cast<uint8_t *>(buffer_);
|
|
||||||
bufferLength = 4 * bufferLength_;
|
|
||||||
}
|
|
||||||
|
|
||||||
//forward Template declaration for linker
|
//forward Template declaration for linker
|
||||||
template class SerialBufferAdapter<uint8_t>;
|
template class SerialBufferAdapter<uint8_t>;
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
*
|
*
|
||||||
* \ingroup serialize
|
* \ingroup serialize
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename count_t>
|
||||||
class SerialBufferAdapter: public SerializeIF {
|
class SerialBufferAdapter: public SerializeIF {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for constant uint8_t buffer. Length field can be serialized optionally.
|
* Constructor for constant uint8_t buffer. Length field can be serialized optionally.
|
||||||
* Type of length can be supplied as template type.
|
* Type of length can be supplied as template type.
|
||||||
@ -26,7 +27,7 @@ public:
|
|||||||
* @param bufferLength
|
* @param bufferLength
|
||||||
* @param serializeLength
|
* @param serializeLength
|
||||||
*/
|
*/
|
||||||
SerialBufferAdapter(const uint8_t * buffer, T bufferLength, bool serializeLength = false);
|
SerialBufferAdapter(const void* buffer, count_t bufferLength, bool serializeLength = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for non-constant uint8_t buffer. Length field can be serialized optionally.
|
* Constructor for non-constant uint8_t buffer. Length field can be serialized optionally.
|
||||||
@ -35,16 +36,7 @@ public:
|
|||||||
* @param bufferLength
|
* @param bufferLength
|
||||||
* @param serializeLength
|
* @param serializeLength
|
||||||
*/
|
*/
|
||||||
SerialBufferAdapter(uint8_t* buffer, T bufferLength, bool serializeLength = false);
|
SerialBufferAdapter(void* buffer, count_t bufferLength, bool serializeLength = false);
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructoor for non-constant uint32_t buffer. Length field can be serialized optionally.
|
|
||||||
* Type of length can be supplied as template type.
|
|
||||||
* @param buffer
|
|
||||||
* @param bufferLength
|
|
||||||
* @param serializeLength
|
|
||||||
*/
|
|
||||||
SerialBufferAdapter(uint32_t* buffer,T bufferLength, bool serializeLength = false);
|
|
||||||
|
|
||||||
virtual ~SerialBufferAdapter();
|
virtual ~SerialBufferAdapter();
|
||||||
|
|
||||||
@ -58,20 +50,12 @@ public:
|
|||||||
|
|
||||||
uint8_t * getBuffer();
|
uint8_t * getBuffer();
|
||||||
const uint8_t * getConstBuffer();
|
const uint8_t * getConstBuffer();
|
||||||
void setBuffer(uint8_t * buffer_, T bufferLength_);
|
void setBuffer(void* buffer_, count_t bufferLength_);
|
||||||
void setBuffer(uint32_t * buffer_, T bufferLength_);
|
|
||||||
private:
|
private:
|
||||||
|
bool m_serialize_length = false;
|
||||||
enum bufferType {
|
const uint8_t *m_const_buffer = nullptr;
|
||||||
NORMAL,
|
uint8_t *m_buffer = nullptr;
|
||||||
CONST
|
count_t m_buffer_length = 0;
|
||||||
};
|
|
||||||
bufferType currentBufferType;
|
|
||||||
|
|
||||||
bool serializeLength;
|
|
||||||
const uint8_t *constBuffer;
|
|
||||||
uint8_t *buffer;
|
|
||||||
T bufferLength;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user