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 "someip/SomeIpConstants.h"
14 : #include "someip/UdpProxy.h"
15 :
16 : #include <etl/expected.h>
17 : #include <etl/span.h>
18 : #include <etl/vector.h>
19 : #include <cstdint>
20 :
21 : namespace someip
22 : {
23 : /**
24 : * The UDP proxy configuration.
25 : */
26 : class UdpProxyConfig
27 : {
28 : public:
29 : UdpProxyConfig(
30 : ::etl::span<UdpProxy> proxies,
31 : ::etl::ivector<UdpProxy*>& serverProxies,
32 : ::etl::ivector<uint16_t>& ports);
33 :
34 : void setListener(INetworkListener& listener) const;
35 :
36 : size_t getSize() const;
37 :
38 : UdpProxy* getProxy(size_t pos) const;
39 : UdpProxy* nextProxy() const;
40 : UdpProxy* getOpenProxy(uint16_t localPort) const;
41 :
42 : bool initPort(uint16_t port) const;
43 : ::etl::expected<uint16_t, PortError> getPort(size_t pos) const;
44 :
45 : void close();
46 :
47 : void shutdownPort(uint16_t port);
48 :
49 : void addToServers(UdpProxy& proxy);
50 :
51 : private:
52 : void releaseServer(UdpProxy& proxy);
53 :
54 : ::etl::span<UdpProxy> _proxies;
55 : ::etl::ivector<UdpProxy*>& _serverProxies;
56 : ::etl::ivector<uint16_t>& _ports;
57 : };
58 :
59 : /**
60 : * The UDP configuration.
61 : */
62 : class UdpConfig
63 : {
64 : public:
65 : UdpConfig(UdpConfig const&) = delete;
66 : UdpConfig& operator=(UdpConfig const&) = delete;
67 :
68 : UdpProxyConfig& _sdProxies;
69 : UdpProxyConfig& _rpcProxies;
70 :
71 : protected:
72 32 : UdpConfig(UdpProxyConfig& sdProxies, UdpProxyConfig& rpcProxies)
73 32 : : _sdProxies(sdProxies), _rpcProxies(rpcProxies)
74 32 : {}
75 : };
76 :
77 : namespace internal
78 : {
79 : /**
80 : * Internal UDP proxy resources.
81 : */
82 : template<class SocketType, uint8_t NumSockets>
83 : class UdpProxyResources : public UdpProxyConfig
84 : {
85 : public:
86 60 : UdpProxyResources(
87 : ::etl::span<uint8_t> const& inputBuffer, ::etl::span<uint8_t> const& outputBuffer)
88 : : UdpProxyConfig(
89 194 : ::etl::span<UdpProxy>(&_proxyArray[0], NumSockets), _serverProxyList, _portList)
90 : {
91 127 : for (uint8_t i = 0U; i < NumSockets; ++i)
92 : {
93 67 : UdpProxy& proxy = _proxyArray[i];
94 67 : proxy.setSocket(_socketArray[i]);
95 67 : proxy.setInputBuffer(inputBuffer);
96 67 : proxy.setOutputBuffer(outputBuffer);
97 : }
98 60 : }
99 :
100 : private:
101 : SocketType _socketArray[NumSockets];
102 : UdpProxy _proxyArray[NumSockets];
103 :
104 : ::etl::vector<UdpProxy*, NumSockets> _serverProxyList;
105 : ::etl::vector<uint16_t, NumSockets> _portList;
106 : };
107 :
108 : // Specialization for zero proxies
109 : template<class SocketType>
110 : class UdpProxyResources<SocketType, 0> : public UdpProxyConfig
111 : {
112 : public:
113 5 : UdpProxyResources(::etl::span<uint8_t> const&, ::etl::span<uint8_t> const&)
114 5 : : UdpProxyConfig(::etl::span<UdpProxy>(), _emptyServerProxies, _emptyPorts)
115 5 : {}
116 :
117 : // Override methods that would modify the static vectors to be no-ops
118 0 : bool initPort(uint16_t) const { return false; }
119 :
120 0 : void shutdownPort(uint16_t) {}
121 :
122 0 : void addToServers(UdpProxy&) {}
123 :
124 : private:
125 : static ::etl::vector<UdpProxy*, 1> _emptyServerProxies;
126 : static ::etl::vector<uint16_t, 1> _emptyPorts;
127 : };
128 :
129 : // Define static members for the template specialization
130 : template<class SocketType>
131 : ::etl::vector<UdpProxy*, 1> UdpProxyResources<SocketType, 0>::_emptyServerProxies;
132 :
133 : template<class SocketType>
134 : ::etl::vector<uint16_t, 1> UdpProxyResources<SocketType, 0>::_emptyPorts;
135 :
136 : /**
137 : * Internal UDP resources.
138 : */
139 : template<class UdpSocketType, uint8_t NumUdpSdSockets, uint8_t NumUdpRpcSockets>
140 : class UdpResources : public UdpConfig
141 : {
142 : public:
143 32 : UdpResources(::etl::span<uint8_t> const& inputBuffer, ::etl::span<uint8_t> const& outputBuffer)
144 : : UdpConfig(_sdResources, _rpcResources)
145 32 : , _sdResources(inputBuffer, outputBuffer)
146 64 : , _rpcResources(inputBuffer, outputBuffer)
147 32 : {}
148 :
149 : private:
150 : UdpProxyResources<UdpSocketType, NumUdpSdSockets> _sdResources;
151 : UdpProxyResources<UdpSocketType, NumUdpRpcSockets> _rpcResources;
152 : };
153 :
154 : } // namespace internal
155 : } // namespace someip
|