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
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
template<typename T>
|
|
|
|
SerialBufferAdapter<T>::SerialBufferAdapter(const uint8_t* buffer,
|
2019-11-02 23:30:12 +01:00
|
|
|
T bufferLength, bool serializeLength) :
|
2020-01-13 01:21:53 +01:00
|
|
|
currentBufferType(bufferType::CONST), serializeLength(serializeLength),
|
|
|
|
constBuffer(buffer), buffer(NULL), bufferLength(bufferLength) {
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
template<typename T>
|
|
|
|
SerialBufferAdapter<T>::SerialBufferAdapter(uint8_t* buffer, T bufferLength,
|
2019-11-02 23:30:12 +01:00
|
|
|
bool serializeLength) :
|
2020-01-13 01:21:53 +01:00
|
|
|
currentBufferType(bufferType::NORMAL),serializeLength(serializeLength), constBuffer(NULL), buffer(buffer),
|
|
|
|
bufferLength(bufferLength) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:48:05 +01:00
|
|
|
template<typename T>
|
|
|
|
SerialBufferAdapter<T>::SerialBufferAdapter(uint32_t* buffer,
|
|
|
|
T bufferLength, bool serializeLength) :
|
2020-01-13 01:21:53 +01:00
|
|
|
currentBufferType(bufferType::NORMAL),serializeLength(serializeLength),
|
|
|
|
constBuffer(NULL), buffer(reinterpret_cast<uint8_t *>(buffer)),
|
2019-12-01 17:48:05 +01:00
|
|
|
bufferLength(bufferLength*4) {
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
template<typename T>
|
|
|
|
SerialBufferAdapter<T>::~SerialBufferAdapter() {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
template<typename T>
|
|
|
|
ReturnValue_t SerialBufferAdapter<T>::serialize(uint8_t** buffer, uint32_t* size,
|
2016-06-15 23:48:41 +02:00
|
|
|
const uint32_t max_size, bool bigEndian) const {
|
2018-07-12 16:29:32 +02:00
|
|
|
uint32_t serializedLength = bufferLength;
|
|
|
|
if (serializeLength) {
|
|
|
|
serializedLength += AutoSerializeAdapter::getSerializedSize(
|
|
|
|
&bufferLength);
|
|
|
|
}
|
|
|
|
if (*size + serializedLength > max_size) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return BUFFER_TOO_SHORT;
|
|
|
|
} else {
|
2018-07-12 16:29:32 +02:00
|
|
|
if (serializeLength) {
|
|
|
|
AutoSerializeAdapter::serialize(&bufferLength, buffer, size,
|
|
|
|
max_size, bigEndian);
|
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
if (this->constBuffer != NULL) {
|
|
|
|
memcpy(*buffer, this->constBuffer, bufferLength);
|
|
|
|
} else if (this->buffer != NULL) {
|
|
|
|
memcpy(*buffer, this->buffer, bufferLength);
|
|
|
|
} else {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
*size += bufferLength;
|
2018-07-12 16:29:32 +02:00
|
|
|
(*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>
|
|
|
|
uint32_t SerialBufferAdapter<T>::getSerializedSize() const {
|
|
|
|
if (serializeLength) {
|
|
|
|
return bufferLength + AutoSerializeAdapter::getSerializedSize(&bufferLength);
|
|
|
|
} else {
|
|
|
|
return bufferLength;
|
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
template<typename T>
|
|
|
|
ReturnValue_t SerialBufferAdapter<T>::deSerialize(const uint8_t** buffer,
|
2016-06-15 23:48:41 +02:00
|
|
|
int32_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) {
|
2018-07-12 16:29:32 +02:00
|
|
|
if(serializeLength){
|
|
|
|
T serializedSize = AutoSerializeAdapter::getSerializedSize(
|
|
|
|
&bufferLength);
|
|
|
|
if((*size - bufferLength - serializedSize) >= 0){
|
|
|
|
*buffer += serializedSize;
|
|
|
|
*size -= serializedSize;
|
|
|
|
}else{
|
|
|
|
return STREAM_TOO_SHORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//No Else If, go on with buffer
|
2016-06-15 23:48:41 +02:00
|
|
|
if (*size - bufferLength >= 0) {
|
|
|
|
*size -= bufferLength;
|
|
|
|
memcpy(this->buffer, *buffer, bufferLength);
|
2018-07-12 16:29:32 +02:00
|
|
|
(*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
|
|
|
|
2019-12-01 17:48:05 +01:00
|
|
|
template<typename T>
|
|
|
|
uint8_t * SerialBufferAdapter<T>::getBuffer() {
|
2020-01-13 01:21:53 +01:00
|
|
|
if(currentBufferType != NORMAL) {
|
|
|
|
warning << "Wrong access function for stored type ! Use getConstBuffer()" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-01 17:48:05 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2020-01-13 01:21:53 +01:00
|
|
|
template<typename T>
|
|
|
|
const uint8_t * SerialBufferAdapter<T>::getConstBuffer() {
|
|
|
|
if(currentBufferType != CONST) {
|
|
|
|
warning << "Wrong access function for stored type ! Use getBuffer()" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return constBuffer;
|
|
|
|
}
|
|
|
|
|
2020-01-04 16:37:08 +01:00
|
|
|
template<typename T>
|
|
|
|
void SerialBufferAdapter<T>::setBuffer(uint8_t * buffer_) {
|
|
|
|
buffer = buffer_;
|
|
|
|
}
|
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>;
|
|
|
|
|