Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2024 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 "util/RoutineControlOptionParser.h"
12 :
13 : #include <etl/span.h>
14 : #include <etl/unaligned_type.h>
15 :
16 : namespace uds
17 : {
18 : uint8_t
19 1 : RoutineControlOptionParser::getLogicalBlockNumberLength(uint8_t const lengthFormatIdentifier)
20 : {
21 1 : return (lengthFormatIdentifier & 0xF0U) >> 4U;
22 : }
23 :
24 1 : uint8_t RoutineControlOptionParser::getMemoryAddressLength(uint8_t const lengthFormatIdentifier)
25 : {
26 1 : return (lengthFormatIdentifier & 0xF0U) >> 4U;
27 : }
28 :
29 1 : uint8_t RoutineControlOptionParser::getMemorySizeLength(uint8_t const lengthFormatIdentifier)
30 : {
31 1 : return (lengthFormatIdentifier & 0x0FU);
32 : }
33 :
34 : uint32_t
35 5 : RoutineControlOptionParser::parseParameter(uint8_t const* const buffer, uint8_t const length)
36 : {
37 5 : switch (length)
38 : {
39 1 : case 1U:
40 : {
41 1 : return static_cast<uint32_t>(*buffer);
42 : }
43 1 : case 2U:
44 : {
45 1 : return static_cast<uint32_t>(::etl::be_uint16_t(buffer));
46 : }
47 1 : case 3U:
48 : {
49 1 : ::etl::span<uint8_t const> const bufferView(buffer, 3U);
50 1 : return (static_cast<uint32_t>(bufferView[0U]) << 16)
51 1 : | static_cast<uint32_t>(::etl::be_uint16_t(bufferView.subspan(1U, 2U).data()));
52 : }
53 1 : case 4U:
54 : {
55 1 : return ::etl::be_uint32_t(buffer);
56 : }
57 1 : default:
58 : {
59 1 : return 0U;
60 : }
61 : }
62 : }
63 :
64 : } // namespace uds
|