Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2021 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : #pragma once
20 :
21 : #include <app/CASEClientPool.h>
22 : #include <app/OperationalSessionSetup.h>
23 : #include <app/OperationalSessionSetupPool.h>
24 : #include <lib/core/CHIPConfig.h>
25 : #include <lib/core/CHIPCore.h>
26 : #include <lib/support/Pool.h>
27 : #include <platform/CHIPDeviceLayer.h>
28 : #include <transport/SessionDelegate.h>
29 : #include <transport/SessionManager.h>
30 : #include <transport/SessionUpdateDelegate.h>
31 :
32 : namespace chip {
33 :
34 : struct CASESessionManagerConfig
35 : {
36 : CASEClientInitParams sessionInitParams;
37 : CASEClientPoolDelegate * clientPool = nullptr;
38 : OperationalSessionSetupPoolDelegate * sessionSetupPool = nullptr;
39 : };
40 :
41 : /**
42 : * This class provides the following
43 : * 1. Manage a pool of operational device proxy objects for peer nodes that have active message exchange with the local node.
44 : * 2. The pool contains atmost one device proxy object for a given peer node.
45 : * 3. API to lookup an existing proxy object, or allocate a new one by triggering session establishment with the peer node.
46 : * 4. During session establishment, trigger node ID resolution (if needed), and update the DNS-SD cache (if resolution is
47 : * successful)
48 : */
49 : class CASESessionManager : public OperationalSessionReleaseDelegate, public SessionUpdateDelegate
50 : {
51 : public:
52 1 : CASESessionManager() = default;
53 1 : virtual ~CASESessionManager()
54 1 : {
55 1 : if (mConfig.sessionInitParams.Validate() == CHIP_NO_ERROR)
56 : {
57 1 : mConfig.sessionInitParams.exchangeMgr->GetReliableMessageMgr()->RegisterSessionUpdateDelegate(nullptr);
58 : }
59 1 : }
60 :
61 : CHIP_ERROR Init(chip::System::Layer * systemLayer, const CASESessionManagerConfig & params);
62 : void Shutdown();
63 :
64 : /**
65 : * Find an existing session for the given node ID, or trigger a new session
66 : * request.
67 : *
68 : * The caller can optionally provide `onConnection` and `onFailure` callback
69 : * objects. If provided, these will be used to inform the caller about
70 : * successful or failed connection establishment.
71 : *
72 : * If the connection is already established, the `onConnection` callback
73 : * will be immediately called, before FindOrEstablishSession returns.
74 : *
75 : * The `onFailure` callback may be called before the FindOrEstablishSession
76 : * call returns, for error cases that are detected synchronously.
77 : *
78 : * attemptCount can be used to automatically retry multiple times if session
79 : * setup is not successful.
80 : */
81 : void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
82 : Callback::Callback<OnDeviceConnectionFailure> * onFailure,
83 : #if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
84 : uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr,
85 : #endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
86 : TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
87 :
88 : /**
89 : * Find an existing session for the given node ID or trigger a new session request.
90 : *
91 : * The caller can optionally provide `onConnection` and `onSetupFailure`
92 : * callback objects. If provided, these will be used to inform the caller about successful or
93 : * failed connection establishment.
94 : *
95 : * If the connection is already established, the `onConnection` callback will be immediately called,
96 : * before `FindOrEstablishSession` returns.
97 : *
98 : * The `onSetupFailure` callback may be called before the `FindOrEstablishSession`
99 : * call returns, for error cases that are detected synchronously.
100 : *
101 : * The `attemptCount` parameter can be used to automatically retry multiple times if session setup is
102 : * not successful.
103 : *
104 : * @param peerId The node ID to find or establish a session with.
105 : * @param onConnection A callback to be called upon successful connection establishment.
106 : * @param onSetupFailure A callback to be called upon an extended device connection failure.
107 : * @param attemptCount The number of retry attempts if session setup fails (default is 1).
108 : * @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
109 : * @param transportPayloadCapability An indicator of what payload types the session needs to be able to transport.
110 : */
111 : void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
112 : Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
113 : #if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
114 : uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr,
115 : #endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
116 : TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
117 :
118 : /**
119 : * Find an existing session for the given node ID or trigger a new session request.
120 : *
121 : * The caller can optionally provide `onConnection`
122 : * callback objects. If provided, these will be used to inform the caller about successful connection establishment.
123 : *
124 : * If the connection is already established, the `onConnection` callback will be immediately called,
125 : * before `FindOrEstablishSession` returns.
126 : *
127 : * The `attemptCount` parameter can be used to automatically retry multiple times if session setup is
128 : * not successful.
129 : *
130 : * This function allows passing 'nullptr' for the error handler to compile, which is useful in scenarios where error
131 : * handling is not needed.
132 : *
133 : * @param peerId The node ID to find or establish a session with.
134 : * @param onConnection A callback to be called upon successful connection establishment.
135 : * @param attemptCount The number of retry attempts if session setup fails (default is 1).
136 : * @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
137 : * @param transportPayloadCapability An indicator of what payload types the session needs to be able to transport.
138 : */
139 : void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection, std::nullptr_t,
140 : #if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
141 : uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr,
142 : #endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
143 : TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
144 :
145 : void ReleaseSession(const ScopedNodeId & peerId);
146 : void ReleaseSessionsForFabric(FabricIndex fabricIndex);
147 :
148 : void ReleaseAllSessions();
149 :
150 : /**
151 : * This API returns the address for the given node ID.
152 : * If the CASESessionManager is configured with a DNS-SD cache, the cache is looked up
153 : * for the node ID.
154 : * If the DNS-SD cache is not available, the CASESessionManager looks up the list for
155 : * an ongoing session with the peer node. If the session doesn't exist, the API will return
156 : * `CHIP_ERROR_NOT_CONNECTED` error.
157 : */
158 : CHIP_ERROR GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr,
159 : TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
160 :
161 : //////////// OperationalSessionReleaseDelegate Implementation ///////////////
162 : void ReleaseSession(OperationalSessionSetup * device) override;
163 :
164 : //////////// SessionUpdateDelegate Implementation ///////////////
165 : void UpdatePeerAddress(ScopedNodeId peerId) override;
166 :
167 : private:
168 : OperationalSessionSetup * FindExistingSessionSetup(const ScopedNodeId & peerId, bool forAddressUpdate = false) const;
169 :
170 : Optional<SessionHandle> FindExistingSession(
171 : const ScopedNodeId & peerId,
172 : const TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload) const;
173 :
174 : void FindOrEstablishSessionHelper(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
175 : Callback::Callback<OnDeviceConnectionFailure> * onFailure,
176 : Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
177 : #if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
178 : uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry,
179 : #endif
180 : TransportPayloadCapability transportPayloadCapability);
181 :
182 : CASESessionManagerConfig mConfig;
183 : };
184 :
185 : } // namespace chip
|