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/IServiceAnnouncer.h"
14 : #include "someip/IServiceRegistry.h"
15 : #include "someip/QueryManager.h"
16 : #include "someip/SdMessageBuilder.h"
17 : #include "someip/ServiceAnnouncerTask.h"
18 : #include "someip/ServiceManager.h"
19 : #include "someip/SessionManager.h"
20 : #include "someip/SomeIpConstants.h"
21 :
22 : #include <async/Types.h>
23 : #include <async/util/Call.h>
24 :
25 : #include <ip/IPAddress.h>
26 :
27 : #include <etl/array.h>
28 : #include <etl/intrusive_forward_list.h>
29 : #include <etl/intrusive_links.h>
30 : #include <etl/pool.h>
31 :
32 : namespace common
33 : {
34 : class ITimeoutManager2;
35 : }
36 :
37 : namespace someip
38 : {
39 : class ServiceAnnouncer : public IServiceAnnouncer
40 : {
41 : public:
42 : ServiceAnnouncer(
43 : INetwork& network,
44 : ServiceManager& serviceManager,
45 : IServiceRegistry& serviceRegistry,
46 : ::async::ContextType const ethernetContext,
47 : QueryManager& queryManager,
48 : SessionManager& sessionManager);
49 :
50 : void init();
51 : void shutdown();
52 :
53 : void start() override;
54 : void stop() override;
55 :
56 : void respondToFindService(
57 : service_id::type serviceId,
58 : instance_id::type instanceId,
59 : major_version::type majorVersion,
60 : minor_version::type minorVersion,
61 : ttl::type ttl,
62 : ::ip::IPAddress const& sourceIpAddress,
63 : bool unicast) override;
64 :
65 : void respondToSubscribe(
66 : service_id::type serviceId,
67 : instance_id::type instanceId,
68 : major_version::type majorVersion,
69 : uint16_t reserved,
70 : eventgroup_id::type eventgroup,
71 : ttl::type ttl,
72 : ::ip::IPAddress const& sourceIpAddress,
73 : ::ip::IPAddress const& endpointIpAddress,
74 : uint16_t endpointPort,
75 : uint8_t endpointProto) override;
76 :
77 : void sendSubscribeAck(
78 : service_id::type serviceId,
79 : instance_id::type instanceId,
80 : eventgroup_id::type eventgroup,
81 : major_version::type majorVersion,
82 : uint16_t reserved,
83 : ttl::type ttl,
84 : ::ip::IPAddress const& sourceIpAddress) override;
85 :
86 : void sendSubscribeNack(
87 : service_id::type serviceId,
88 : instance_id::type instanceId,
89 : eventgroup_id::type eventgroup,
90 : major_version::type majorVersion,
91 : uint16_t reserved,
92 : ::ip::IPAddress const& sourceIpAddress) override;
93 :
94 : void sendSubscribeAckMulticast(
95 : service_id::type serviceId,
96 : instance_id::type instanceId,
97 : eventgroup_id::type eventgroup,
98 : major_version::type majorVersion,
99 : uint16_t reserved,
100 : ttl::type ttl,
101 : ::ip::IPAddress const& endpointAddress,
102 : uint16_t endpointPort,
103 : ::ip::IPAddress const& sourceIpAddress) override;
104 :
105 : void
106 : subscribe(ServiceDescription const& service, ::ip::IPAddress const& sourceAddress) override;
107 : void
108 : unsubscribe(ServiceDescription const& service, ::ip::IPAddress const& sourceAddress) override;
109 : void find(ServiceDescription const& service) override;
110 : void offer(ServiceDescription const& service) override;
111 : void stopOffer(ServiceDescription const& service) override;
112 :
113 : void sendStopOffers() override;
114 :
115 : void cyclic();
116 :
117 19 : size_t getNumPendingBrowseRequests() const { return _pendingBrowseRequests.size(); }
118 :
119 19 : size_t getNumPendingTxMessages() const { return _pendingTxMessages.size(); }
120 :
121 : protected:
122 : void checkPendingTasks(uint64_t now);
123 : void addProvidedServices(uint64_t now);
124 : void addQueries() const;
125 :
126 : private:
127 : static uint32_t const REQ_RES_MIN_DELAY_MS = 0U;
128 : static uint32_t const REQ_RES_MAX_DELAY_MS = 0U;
129 : static uint32_t const CYCLE_TIME_MS = 15U;
130 : static uint8_t const MAX_NUM_QUEUED_TASKS = 32U;
131 :
132 : static uint8_t const MAX_NUM_ENTRIES_PER_MESSAGE
133 : = 86U; // max. possible number of entries in payload ((1400 - 12) / 16 = 86)
134 : static uint8_t const MAX_NUM_OPTIONS_PER_MESSAGE = 16U;
135 :
136 : void sendDueMessages();
137 : void enqueueTxMessage(ServiceAnnouncerTask const& txMessage);
138 : void checkPendingTasksAndSendDueMessages();
139 : void triggerEventTimeout();
140 : void executeTask(ServiceAnnouncerTask const& task);
141 : void initializeMulticastMessage();
142 : void initializeUnicastMessage(::ip::IPAddress const& destinationAddress);
143 : void finalizeMessage();
144 : void getSessionInfoForNextMessage(uint16_t& sessionId, bool& rebootFlag);
145 : void processTaskOffer(ServiceAnnouncerTask const& task);
146 : void processTaskBrowseResults(ServiceAnnouncerTask const& task) const;
147 : void processTaskSubscribe(ServiceAnnouncerTask const& task);
148 : void processTaskSubscribeAck(ServiceAnnouncerTask const& task);
149 : void processTaskSubscribeAckMulticast(ServiceAnnouncerTask const& task);
150 : void processTaskSubscribeNack(ServiceAnnouncerTask const& task);
151 : void processTaskUnsubscribe(ServiceAnnouncerTask const& task);
152 : void releaseTask(ServiceAnnouncerTask& task);
153 : void addOffer(ServiceDescription& service);
154 : void addStopOffer(ServiceDescription& service);
155 : void addSubscribe(ServiceDescription& service);
156 : void addUnsubscribe(ServiceDescription& service);
157 : void addSubscribeAck(ServiceDescription const& service);
158 : void addSubscribeAckMulticast(ServiceDescription const& service);
159 : void addSubscribeNack(ServiceDescription const& service);
160 : void resetIfFull(SdMessageReturnCode);
161 : void sendMessage();
162 :
163 : INetwork& _network;
164 : ServiceManager& _serviceManager;
165 : IServiceRegistry& _serviceRegistry;
166 :
167 : ::async::ContextType const _ethernetContext;
168 : ::async::Function _cyclicFunction;
169 : ::async::TimeoutType _cyclicTimeout;
170 : ::async::Function _eventFunction;
171 : ::async::TimeoutType _eventTimeout;
172 :
173 : SdMessageBuilder _messageBuilder;
174 :
175 : ::etl::array<uint8_t, SD_PACKET_MAX_SIZE> _messageBuffer;
176 :
177 : bool _isStarted;
178 : ::ip::IPAddress _destinationAddress{};
179 :
180 : using tTaskPool = ::etl::pool<ServiceAnnouncerTask, MAX_NUM_QUEUED_TASKS>;
181 : using tPendingTaskList
182 : = ::etl::intrusive_forward_list<ServiceAnnouncerTask, ::etl::forward_link<0>>;
183 : tTaskPool _taskPool;
184 : tPendingTaskList _pendingBrowseRequests;
185 : tPendingTaskList _pendingTxMessages;
186 :
187 : QueryManager& _queryManager;
188 : SessionManager& _sessionManager;
189 : };
190 :
191 : } // namespace someip
|