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/SessionManager.h"
12 :
13 : #include "someip/logger.h"
14 :
15 : // Logger API uses printf-style varargs for fixed diagnostic messages in this module.
16 : // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg)
17 :
18 : namespace someip
19 : {
20 : using ::ip::IPAddress;
21 : using ::util::logger::SOMEIP;
22 :
23 : namespace internal
24 : {
25 46 : SessionEntry::SessionEntry() : _sessionId(INITIAL_SESSION_ID), _rebootFlag(true) {}
26 :
27 40 : void SessionEntry::clear()
28 : {
29 40 : _sessionId = INITIAL_SESSION_ID;
30 40 : _rebootFlag = true;
31 40 : }
32 :
33 131090 : void SessionEntry::increment()
34 : {
35 131090 : if (_sessionId == MAX_SESSION_ID)
36 : {
37 2 : _sessionId = INITIAL_SESSION_ID;
38 2 : _rebootFlag = false;
39 : }
40 : else
41 : {
42 131088 : ++_sessionId;
43 : }
44 131090 : }
45 :
46 19 : SessionEndpoint::SessionEndpoint() : SessionEntry() {}
47 :
48 19 : void SessionEndpoint::clear() { SessionEntry::clear(); }
49 :
50 : } // namespace internal
51 :
52 27 : SessionManager::SessionManager(EndpointMap& map) : _endpoints(map), _multicast() {}
53 :
54 21 : void SessionManager::init()
55 : {
56 21 : _endpoints.clear();
57 21 : _multicast.clear();
58 21 : }
59 :
60 65536 : void SessionManager::getSessionInfoForNextMulticastMessage(uint16_t& sessionId, bool& rebootFlag)
61 : {
62 65536 : sessionId = _multicast._sessionId;
63 65536 : rebootFlag = _multicast._rebootFlag;
64 :
65 65536 : _multicast.increment();
66 65536 : }
67 :
68 65555 : void SessionManager::getSessionInfoForNextUnicastMessage(
69 : IPAddress const& ipAddress, uint16_t& sessionId, bool& rebootFlag)
70 : {
71 65555 : EndpointMap::iterator const iter = addEndpoint(ipAddress);
72 65555 : if (_endpoints.end() == iter)
73 : {
74 1 : WARN_LOG(SOMEIP, "SessionManager: unable to add endpoint");
75 1 : sessionId = 0U;
76 1 : rebootFlag = false;
77 1 : return;
78 : }
79 :
80 65554 : internal::SessionEndpoint& endpoint = iter->second;
81 :
82 65554 : sessionId = endpoint._sessionId;
83 65554 : rebootFlag = endpoint._rebootFlag;
84 :
85 65554 : endpoint.increment();
86 : }
87 :
88 : // private
89 65555 : SessionManager::EndpointMap::iterator SessionManager::addEndpoint(IPAddress const& ipAddress)
90 : {
91 65555 : EndpointMap::iterator const iter = _endpoints.find(ipAddress);
92 65555 : if (iter != _endpoints.end())
93 : {
94 65535 : return iter; // endpoint already added
95 : }
96 :
97 20 : if (_endpoints.full() == true)
98 : {
99 1 : return _endpoints.end();
100 : }
101 :
102 19 : internal::SessionEndpoint& endpoint = _endpoints[ipAddress];
103 19 : endpoint.clear();
104 :
105 19 : return _endpoints.find(ipAddress);
106 : }
107 :
108 : } // namespace someip
109 :
110 : // NOLINTEND(cppcoreguidelines-pro-type-vararg)
|