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/SomeIpStack.h"
12 :
13 : #include "someip/INetwork.h"
14 : #include "someip/logger.h"
15 :
16 : // Logger API uses printf-style varargs for fixed diagnostic messages in this module.
17 : // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg)
18 :
19 : namespace someip
20 : {
21 : using ::util::logger::SOMEIP;
22 :
23 6 : SomeIpStack::SomeIpStack(
24 : INetwork& network,
25 : IRpcSender& rpcSender,
26 : IEventReceiver& eventReceiver,
27 : IEventSender& eventSender,
28 6 : IServiceRegistry& registry)
29 6 : : _network(network)
30 6 : , _rpcSender(rpcSender)
31 6 : , _eventReceiver(eventReceiver)
32 6 : , _eventSender(eventSender)
33 6 : , _state(State::UNDEFINED)
34 6 : , _serviceRegistry(registry)
35 6 : {}
36 :
37 1 : bool SomeIpStack::initSdPort(uint16_t const port)
38 : {
39 1 : if (isInitialized())
40 : {
41 0 : return false;
42 : }
43 :
44 1 : INFO_LOG(SOMEIP, "SomeIpStack::initSdPort(%d)", port);
45 :
46 1 : return _network.initSdPort(port);
47 : }
48 :
49 1 : bool SomeIpStack::initUdpPort(uint16_t const port)
50 : {
51 1 : if (isInitialized())
52 : {
53 0 : return false;
54 : }
55 :
56 1 : INFO_LOG(SOMEIP, "SomeIpStack::initUdpPort(%d)", port);
57 :
58 1 : return _network.initUdpPort(port);
59 : }
60 :
61 1 : bool SomeIpStack::initTcpPort(uint16_t const port)
62 : {
63 1 : if (isInitialized())
64 : {
65 0 : return false;
66 : }
67 :
68 1 : INFO_LOG(SOMEIP, "SomeIpStack::initTcpPort(%d)", port);
69 :
70 1 : return _network.initTcpPort(port);
71 : }
72 :
73 8 : bool SomeIpStack::isInitialized() const { return _state != State::UNDEFINED; }
74 :
75 5 : bool SomeIpStack::isStarted() const { return _state == State::STARTED; }
76 :
77 1 : bool SomeIpStack::init()
78 : {
79 1 : if (isInitialized())
80 : {
81 0 : return true;
82 : }
83 :
84 1 : INFO_LOG(SOMEIP, "SomeIpStack::init()");
85 :
86 1 : if (doInit())
87 : {
88 1 : _state = State::STOPPED;
89 1 : return true;
90 : }
91 :
92 0 : ERROR_LOG(SOMEIP, "SomeIpStack::init() failed");
93 0 : return false;
94 : }
95 :
96 1 : bool SomeIpStack::start()
97 : {
98 1 : if (!isInitialized())
99 : {
100 0 : if (!init())
101 : {
102 0 : return false;
103 : }
104 : }
105 :
106 1 : if (isStarted())
107 : {
108 0 : return true;
109 : }
110 :
111 1 : INFO_LOG(SOMEIP, "SomeIpStack::start()");
112 :
113 1 : if (doStart())
114 : {
115 1 : _state = State::STARTED;
116 1 : return true;
117 : }
118 :
119 0 : ERROR_LOG(SOMEIP, "SomeIpStack::start() failed");
120 0 : return false;
121 : }
122 :
123 1 : void SomeIpStack::stop()
124 : {
125 1 : if (!isStarted())
126 : {
127 0 : return;
128 : }
129 :
130 1 : INFO_LOG(SOMEIP, "SomeIpStack::stop()");
131 :
132 1 : doStop();
133 :
134 1 : _state = State::STOPPED;
135 : }
136 :
137 1 : void SomeIpStack::shutdown()
138 : {
139 1 : if (isStarted())
140 : {
141 0 : stop();
142 : }
143 :
144 1 : if (!isInitialized())
145 : {
146 0 : return;
147 : }
148 :
149 1 : INFO_LOG(SOMEIP, "SomeIpStack::shutdown()");
150 :
151 1 : doShutdown();
152 :
153 1 : _state = State::UNDEFINED;
154 : }
155 :
156 1 : bool SomeIpStack::registerServiceQuery(ServiceQuery& query)
157 : {
158 1 : return _serviceRegistry.registerServiceQuery(query);
159 : }
160 :
161 1 : void SomeIpStack::unregisterServiceQuery(ServiceQuery& query)
162 : {
163 1 : _serviceRegistry.unregisterServiceQuery(query);
164 1 : }
165 :
166 1 : void SomeIpStack::addEventListener(IEventListener& listener)
167 : {
168 1 : _eventReceiver.addEventListener(listener);
169 1 : }
170 :
171 1 : void SomeIpStack::removeEventListener(IEventListener& listener)
172 : {
173 1 : _eventReceiver.removeEventListener(listener);
174 1 : }
175 :
176 1 : bool SomeIpStack::registerProvidedService(ProvidedService& service)
177 : {
178 1 : return _serviceRegistry.registerProvidedService(service);
179 : }
180 :
181 1 : void SomeIpStack::unregisterProvidedService(ProvidedService& service)
182 : {
183 1 : _serviceRegistry.unregisterProvidedService(service);
184 1 : }
185 :
186 : } // namespace someip
187 :
188 : // NOLINTEND(cppcoreguidelines-pro-type-vararg)
|