Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #include "someip/SomeIpSerializer.h"
12 :
13 : #include "someip/PrimitiveTypes.h"
14 :
15 : #include <etl/algorithm.h>
16 :
17 : namespace someip
18 : {
19 88 : SomeIpSerializer::SomeIpSerializer(::etl::span<uint8_t> const buffer)
20 88 : : _buffer(buffer), _currentPos(0U)
21 88 : {}
22 :
23 9 : void SomeIpSerializer::operator<<(float_t const item)
24 : {
25 9 : if (!isGood())
26 : {
27 1 : return;
28 : }
29 8 : if (hasSpace(4))
30 : {
31 : // switch to big endian temporarily
32 7 : uint32_t const tmp = ::someip::internal::packIEEE754<uint32_t, float_t, 8U>(item);
33 7 : ::etl::be_uint32_ext_t{&_buffer[_currentPos]} = tmp;
34 7 : _currentPos += 4U;
35 : }
36 : }
37 :
38 12 : void SomeIpSerializer::operator<<(double_t const item)
39 : {
40 12 : if (!isGood())
41 : {
42 1 : return;
43 : }
44 11 : if (hasSpace(8))
45 : {
46 10 : uint64_t const tmp = ::someip::internal::packIEEE754<uint64_t, double_t, 11U>(item);
47 10 : ::etl::be_uint64_ext_t{&_buffer[_currentPos]} = tmp;
48 10 : _currentPos += 8U;
49 : }
50 : }
51 :
52 22 : void SomeIpSerializer::operator<<(ISomeIpSerializable const& item) { item.serializeToArray(*this); }
53 :
54 2 : void SomeIpSerializer::operator<<(::etl::span<uint8_t const> const data)
55 : {
56 2 : if (!isGood())
57 : {
58 0 : return;
59 : }
60 2 : if (hasSpace(data.size()))
61 : {
62 1 : auto dest = _buffer.subspan(_currentPos);
63 1 : etl::copy(data.begin(), data.end(), dest.begin());
64 1 : _currentPos += data.size();
65 : }
66 : }
67 :
68 3 : void SomeIpSerializer::writeTypeFieldSize(uint32_t const fieldType)
69 : {
70 3 : bigEndian();
71 3 : uint8_t const typeSize = getTypeFieldSize();
72 :
73 3 : if (typeSize == 1U)
74 : {
75 1 : operator<<(static_cast<uint8_t>(fieldType));
76 1 : return;
77 : }
78 2 : if (typeSize == 2U)
79 : {
80 1 : operator<<(static_cast<uint16_t>(fieldType));
81 1 : return;
82 : }
83 :
84 : // default is uint32_t field size
85 1 : operator<<(static_cast<uint32_t>(fieldType));
86 : }
87 :
88 : } // namespace someip
|