summaryrefslogtreecommitdiff
path: root/implementation/message/src/serializer.cpp
blob: cfa32ec94bb153d6eb36858c2938e808724fdd38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2014-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <cstring>

#ifdef VSOMEIP_DEBUGGING
#include <iomanip>
#include <sstream>

#include <vsomeip/logger.hpp>
#endif

#include <vsomeip/serializable.hpp>

#include "../include/serializer.hpp"
#include "../../utility/include/byteorder.hpp"

namespace vsomeip {

serializer::serializer()
	: data_(0), capacity_(0), position_(0), remaining_(0) {
}

serializer::~serializer() {
};

bool serializer::serialize(const serializable *_from) {
	return (_from && _from->serialize(this));
}

bool serializer::serialize(const uint8_t _value) {
	if (1 > remaining_)
		return false;

	*position_++ = _value;
	remaining_--;

	return true;
}

bool serializer::serialize(const uint16_t _value) {
	if (2 > remaining_)
		return false;

	*position_++ = VSOMEIP_WORD_BYTE1(_value);
	*position_++ = VSOMEIP_WORD_BYTE0(_value);
	remaining_ -= 2;

	return true;
}

bool serializer::serialize(const uint32_t _value, bool _omit_last_byte) {
	if (3 > remaining_ || (!_omit_last_byte && 4 > remaining_))
		return false;

	if (!_omit_last_byte) {
		*position_++ = VSOMEIP_LONG_BYTE3(_value);
		remaining_--;
	}
	*position_++ = VSOMEIP_LONG_BYTE2(_value);
	*position_++ = VSOMEIP_LONG_BYTE1(_value);
	*position_++ = VSOMEIP_LONG_BYTE0(_value);
	remaining_ -= 3;

	return true;
}

bool serializer::serialize(const uint8_t *_data, uint32_t _length) {
	if (_length > remaining_)
		return false;

	::memcpy(position_, _data, _length);
	position_ += _length;
	remaining_ -= _length;

	return true;
}

byte_t * serializer::get_data() const {
	return data_;
}

uint32_t serializer::get_capacity() const {
	return capacity_;
}

uint32_t serializer::get_size() const {
	return capacity_ - remaining_;
}

void serializer::create_data(uint32_t _capacity) {
	if (0 != data_)
		delete [] data_;

	data_ = new byte_t[_capacity];
	position_ = data_;
	if (0 != data_) {
		capacity_ = remaining_ = _capacity;
	} else {
		capacity_ = remaining_ = 0;
	}
}

void serializer::set_data(byte_t *_data, uint32_t _capacity) {
	delete [] data_;

	data_ = _data;
	position_ = _data;

	if (0 != data_) {
		capacity_ = remaining_ = _capacity;
	} else {
		capacity_ = remaining_ = 0;
	}
}

void serializer::reset() {
	position_ = data_;
	remaining_ = capacity_;
}

#ifdef VSOMEIP_DEBUGGING
void serializer::show() {
	std::stringstream its_data;
	its_data << "SERIALIZED: ";
	for (int i = 0; i < position_ - data_; ++i)
		its_data << std::setw(2) << std::setfill('0')
				 << std::hex << (int)data_[i];
	VSOMEIP_DEBUG << its_data.str();
}
#endif

} // namespace vsomeip