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/UdpConfig.h"
12 :
13 : #include "someip/logger.h"
14 :
15 : #include <etl/span.h>
16 :
17 : // Logger API uses printf-style varargs for fixed diagnostic messages in this module.
18 : // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg)
19 :
20 : namespace someip
21 : {
22 : using ::util::logger::SOMEIP;
23 :
24 70 : UdpProxyConfig::UdpProxyConfig(
25 : ::etl::span<UdpProxy> proxies,
26 : ::etl::ivector<UdpProxy*>& serverProxies,
27 70 : ::etl::ivector<uint16_t>& ports)
28 70 : : _proxies(proxies), _serverProxies(serverProxies), _ports(ports)
29 70 : {}
30 :
31 39 : void UdpProxyConfig::setListener(INetworkListener& listener) const
32 : {
33 82 : for (UdpProxy& proxy : _proxies)
34 : {
35 43 : proxy.setListener(listener);
36 : }
37 39 : }
38 :
39 13 : size_t UdpProxyConfig::getSize() const { return _proxies.size(); }
40 :
41 46 : UdpProxy* UdpProxyConfig::getProxy(size_t const pos) const { return &(_proxies[pos]); }
42 :
43 7 : UdpProxy* UdpProxyConfig::nextProxy() const
44 : {
45 9 : for (UdpProxy& proxy : _proxies)
46 : {
47 7 : if (!proxy.isOpen())
48 : {
49 5 : return &proxy;
50 : }
51 : }
52 :
53 2 : return nullptr;
54 : }
55 :
56 13 : UdpProxy* UdpProxyConfig::getOpenProxy(uint16_t const localPort) const
57 : {
58 22 : for (UdpProxy& proxy : _proxies)
59 : {
60 14 : if (proxy.isOpen())
61 : {
62 7 : auto const portResult = proxy.getLocalPort();
63 7 : if (portResult.has_value() && (portResult.value() == localPort))
64 : {
65 5 : return &proxy;
66 : }
67 : }
68 : }
69 :
70 8 : return nullptr;
71 : }
72 :
73 8 : bool UdpProxyConfig::initPort(uint16_t const port) const
74 : {
75 8 : if (!_ports.full())
76 : {
77 6 : _ports.push_back(port);
78 6 : return true;
79 : }
80 :
81 2 : return false;
82 : }
83 :
84 15 : ::etl::expected<uint16_t, PortError> UdpProxyConfig::getPort(size_t const pos) const
85 : {
86 15 : if (_ports.size() > pos)
87 : {
88 9 : uint16_t const port = _ports[pos];
89 9 : if (port == port::INVALID)
90 : {
91 0 : return ::etl::unexpected<PortError>(PortError::NOT_INITIALIZED);
92 : }
93 9 : return port;
94 : }
95 12 : return ::etl::unexpected<PortError>(PortError::OUT_OF_RANGE);
96 : }
97 :
98 5 : void UdpProxyConfig::addToServers(UdpProxy& proxy)
99 : {
100 5 : if (!_serverProxies.full())
101 : {
102 5 : proxy.incRefCounter();
103 5 : _serverProxies.push_back(&proxy);
104 : }
105 5 : }
106 :
107 1 : void UdpProxyConfig::shutdownPort(uint16_t const port)
108 : {
109 1 : UdpProxy* const proxy = getOpenProxy(port);
110 1 : if (nullptr != proxy)
111 : {
112 1 : releaseServer(*proxy);
113 1 : proxy->close();
114 : }
115 1 : }
116 :
117 5 : void UdpProxyConfig::releaseServer(UdpProxy& proxy)
118 : {
119 : etl::ivector<someip::UdpProxy*, void>::iterator it
120 5 : = etl::find(_serverProxies.begin(), _serverProxies.end(), &proxy);
121 5 : if (it != _serverProxies.end())
122 : {
123 3 : proxy.decRefCounter();
124 3 : (void)_serverProxies.erase(it);
125 : }
126 5 : }
127 :
128 4 : void UdpProxyConfig::close()
129 : {
130 8 : for (size_t i = 0U; i < _proxies.size(); ++i)
131 : {
132 4 : UdpProxy& proxy = _proxies[i];
133 4 : releaseServer(proxy);
134 4 : if (proxy.isOpen())
135 : {
136 1 : INFO_LOG(SOMEIP, "Network: close proxy[%d]", i);
137 1 : proxy.close();
138 : }
139 : }
140 4 : }
141 :
142 : } // namespace someip
143 :
144 : // NOLINTEND(cppcoreguidelines-pro-type-vararg)
|