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 : #pragma once
12 :
13 : #include "busid/BusId.h"
14 :
15 : #include "transport/LogicalAddress.h"
16 : #include "transport/TransportMessage.h"
17 :
18 : #include <etl/array.h>
19 : #include <etl/span.h>
20 :
21 : #include <array>
22 :
23 : #include <platform/estdint.h>
24 :
25 : namespace transport
26 : {
27 : class TransportConfiguration
28 : {
29 : public:
30 : TransportConfiguration() = delete;
31 :
32 : static constexpr size_t NUMBER_OF_ETHERNET_TESTERS = 3;
33 : static constexpr size_t NUMBER_OF_CAN_TESTERS = 2;
34 : static constexpr size_t NUMBER_OF_ADDRESS_LISTS = 2;
35 :
36 : using EthernetTesters = std::array<LogicalAddress, NUMBER_OF_ETHERNET_TESTERS>;
37 : using CanTesters = std::array<LogicalAddress, NUMBER_OF_CAN_TESTERS>;
38 : using LogicalAddressConverterUT = LogicalAddressConverter<NUMBER_OF_ADDRESS_LISTS>;
39 :
40 : static constexpr EthernetTesters TESTER_ADDRESS_RANGE_ETHERNET
41 : = {{{0x0ECDU, 0x00F0U}, {0x0E12U, 0x00F1U}, {0x0EFFU, 0x00F2U}}};
42 : static constexpr CanTesters TESTER_ADDRESS_RANGE_CAN
43 : = {{{0x0E10U, 0x00F3U}, {0x0E01U, 0x00F4U}}};
44 :
45 : /**
46 : * Functional addressing
47 : */
48 : static uint16_t const FUNCTIONAL_ALL_ISO14229 = 0x00DFU;
49 :
50 : enum
51 : {
52 : INVALID_DIAG_ADDRESS = 0xFFU
53 : };
54 :
55 : /**
56 : * Maximum payload size for functionally addressed messages
57 : */
58 : static uint16_t const MAX_FUNCTIONAL_MESSAGE_PAYLOAD_SIZE = 6U;
59 :
60 : /**
61 : * Buffer size for diagnostic payload
62 : */
63 : static uint16_t const DIAG_PAYLOAD_SIZE = 4095U;
64 :
65 : /**
66 : * Number of full size transport messages
67 : */
68 : static uint8_t const NUMBER_OF_FULL_SIZE_TRANSPORT_MESSAGES = 5U;
69 :
70 : /**
71 : * Maximum number of transport messages
72 : */
73 : static uint8_t const MAXIMUM_NUMBER_OF_TRANSPORT_MESSAGES
74 : = NUMBER_OF_FULL_SIZE_TRANSPORT_MESSAGES * 8U;
75 :
76 : static bool isFunctionalAddress(uint16_t address);
77 :
78 : static bool isFunctionallyAddressed(TransportMessage const& message);
79 :
80 : static bool is2ByteTesterAddress(uint16_t address);
81 :
82 : static bool is1ByteTesterAddress(uint16_t address);
83 :
84 : static bool isTesterAddress(uint16_t address);
85 :
86 : /// True if the bus uses 1-byte diagnostic addresses (i.e. not a DoIP bus).
87 : static bool is1ByteDiagAddressBus(uint8_t busId);
88 :
89 : static uint16_t convert2ByteAddressTo1Byte(uint16_t address);
90 :
91 : static uint16_t convert1ByteAddressTo2Byte(uint16_t address);
92 :
93 : static bool isFromTester(TransportMessage const& message);
94 : };
95 :
96 : /**
97 : * This function checks if the provided 16-bit address matches
98 : * the constant FUNCTIONAL_ALL_ISO14229.
99 : * \return true if they are equal and false otherwise.
100 : */
101 29 : inline bool TransportConfiguration::isFunctionalAddress(uint16_t const address)
102 : {
103 29 : return (FUNCTIONAL_ALL_ISO14229 == address);
104 : }
105 :
106 7 : inline bool TransportConfiguration::is1ByteDiagAddressBus(uint8_t const busId)
107 : {
108 : // Only the Ethernet/DoIP buses use 2-byte diagnostic addresses.
109 7 : return (busId != ::busid::ETH_0) && (busId != ::busid::ETH_1);
110 : }
111 :
112 : /**
113 : * This function checks if the target ID of the provided TransportMessage
114 : * object matches the constant FUNCTIONAL_ALL_ISO14229.
115 : * \return true if they are equal and false otherwise.
116 : */
117 10 : inline bool TransportConfiguration::isFunctionallyAddressed(TransportMessage const& message)
118 : {
119 10 : return isFunctionalAddress(message.getTargetId());
120 : }
121 :
122 : inline bool TransportConfiguration::is2ByteTesterAddress(uint16_t const address)
123 : {
124 : return addressfinder::is2ByteAddressIn(address, TESTER_ADDRESS_RANGE_ETHERNET)
125 : || addressfinder::is2ByteAddressIn(address, TESTER_ADDRESS_RANGE_CAN);
126 : }
127 :
128 5 : inline bool TransportConfiguration::is1ByteTesterAddress(uint16_t const address)
129 : {
130 5 : return addressfinder::is1ByteAddressIn(address, TESTER_ADDRESS_RANGE_ETHERNET)
131 5 : || addressfinder::is1ByteAddressIn(address, TESTER_ADDRESS_RANGE_CAN);
132 : }
133 :
134 : /**
135 : * This function checks if the provided 2-byte address is a supported tester address.
136 : *
137 : * \return true if it is part of the bounded external tester set and false otherwise.
138 : */
139 : inline bool TransportConfiguration::isTesterAddress(uint16_t const address)
140 : {
141 : return is2ByteTesterAddress(address);
142 : }
143 :
144 2 : inline uint16_t TransportConfiguration::convert2ByteAddressTo1Byte(uint16_t const address)
145 : {
146 2 : if ((address & 0xFF00U) != 0x0E00U)
147 : {
148 1 : return address;
149 : }
150 :
151 1 : return LogicalAddressConverterUT::convert2ByteAddressTo1Byte(address);
152 : }
153 :
154 5 : inline uint16_t TransportConfiguration::convert1ByteAddressTo2Byte(uint16_t const address)
155 : {
156 5 : if ((address & 0xFFF0U) != 0x00F0U)
157 : {
158 3 : return address;
159 : }
160 :
161 2 : return LogicalAddressConverterUT::convert1ByteAddressTo2Byte(address);
162 : }
163 :
164 : /**
165 : * This function determines if the source ID of the provided TransportMessage object
166 : * corresponds to a 1-byte tester address.
167 : *
168 : * \return true if it does and false otherwise.
169 : */
170 5 : inline bool TransportConfiguration::isFromTester(TransportMessage const& message)
171 : {
172 5 : return is1ByteTesterAddress(message.getSourceId());
173 : }
174 :
175 : // Explicit template specialization declaration (definition in TransportConfiguration.cpp)
176 : template<>
177 : etl::array<::etl::span<LogicalAddress const>, TransportConfiguration::NUMBER_OF_ADDRESS_LISTS> const
178 : TransportConfiguration::LogicalAddressConverterUT::TESTER_ADDRESS_LISTS;
179 :
180 : } // namespace transport
|