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/SomeIpParser.h"
12 :
13 : #include "someip/ISomeIpSerializable.h"
14 : #include "someip/PrimitiveTypes.h"
15 :
16 : namespace someip
17 : {
18 5 : void skip(SomeIpParser& parser, uint16_t const tag)
19 : {
20 5 : uint32_t const wireType = (static_cast<uint32_t>(tag) >> 12U);
21 5 : switch (wireType)
22 : {
23 1 : case 0U:
24 : {
25 1 : parser.skip(sizeof(uint8_t));
26 1 : break;
27 : }
28 1 : case 1U:
29 : {
30 1 : parser.skip(sizeof(uint16_t));
31 1 : break;
32 : }
33 1 : case 2U:
34 : {
35 1 : parser.skip(sizeof(uint32_t));
36 1 : break;
37 : }
38 1 : case 3U:
39 : {
40 1 : parser.skip(sizeof(uint64_t));
41 1 : break;
42 : }
43 1 : default:
44 : {
45 1 : uint32_t length = 0U;
46 1 : parser >> length;
47 1 : parser.skip(static_cast<size_t>(length));
48 1 : break;
49 : }
50 : }
51 5 : }
52 :
53 82 : SomeIpParser::SomeIpParser(::etl::span<uint8_t const> const buffer)
54 82 : : _buffer(buffer), _currentPos(0U)
55 82 : {}
56 :
57 9 : void SomeIpParser::operator>>(float_t& item)
58 : {
59 9 : if (!isGood())
60 : {
61 1 : return;
62 : }
63 8 : if (hasSpace(4))
64 : {
65 7 : uint32_t const bits = ::etl::be_uint32_t(&_buffer[_currentPos]);
66 7 : item = ::someip::internal::unpackIEEE754<float_t, uint32_t, 8U>(bits);
67 7 : _currentPos += 4U;
68 : }
69 : }
70 :
71 12 : void SomeIpParser::operator>>(double_t& item)
72 : {
73 12 : if (!isGood())
74 : {
75 1 : return;
76 : }
77 11 : if (hasSpace(8))
78 : {
79 10 : if (hasSpace(sizeof(item)))
80 : {
81 10 : uint64_t const bits = ::etl::be_uint64_t(&_buffer[_currentPos]);
82 10 : item = ::someip::internal::unpackIEEE754<double_t, uint64_t, 11U>(bits);
83 10 : _currentPos += 8U;
84 : }
85 : }
86 : }
87 :
88 1 : void SomeIpParser::operator>>(ISomeIpSerializable& item) { item.parseFromArray(*this); }
89 :
90 7 : void SomeIpParser::skip(size_t const bytes)
91 : {
92 7 : if (hasSpace(bytes))
93 : {
94 6 : _currentPos += bytes;
95 : }
96 7 : }
97 :
98 3 : uint32_t SomeIpParser::readTypeFieldSize()
99 : {
100 3 : _bigEndian = true;
101 3 : uint8_t const typeSize = _typeFieldSize;
102 :
103 3 : if (typeSize == 1U)
104 : {
105 1 : uint8_t fieldSize = 0U;
106 1 : operator>>(fieldSize);
107 1 : return static_cast<uint32_t>(fieldSize);
108 : }
109 2 : if (typeSize == 2U)
110 : {
111 1 : uint16_t fieldSize = 0U;
112 1 : operator>>(fieldSize);
113 1 : return static_cast<uint32_t>(fieldSize);
114 : }
115 :
116 : // default is uint32_t field size
117 1 : uint32_t fieldSize = 0U;
118 1 : operator>>(fieldSize);
119 1 : return fieldSize;
120 : }
121 :
122 : } // namespace someip
|