Skip to content

Commit 4145d2d

Browse files
committed
TCP connection setup/management and CASESession association.
Add ConnectToPeer() API for explicit connection setup. Currently, connecting to a peer is coupled with sending a message to the peer. This decouples the two and creates a clear API for connecting to a peer address. Goes along with the existing Disconnect() API. This would be essential during activation of retained sessions by solely connecting to the peer and associating with the retained session. Surface Connection completion and Closure callbacks and hook them through SessionManager(TransportMgr delegate) and CASESession. Mark SecureSession as defunct on connection closures. Modify ActiveConnectionState in TCPBase to hold state for each connection, so that it is able to handle the various control flow paths. Associate a session with a connection object. Associate the PeerAddress with the session early. Pass the PeerAddress in the Find APIs. This helps check against the correct TransportType when searching for a Sesssion in the SessionTable. Add a `large payload` flag in EstablishSession() and Session lookup functions to create/associate with the correct session and transport. Have default configurations for TCP in a separate TCPConfig.h. Refactor echo_requester.cpp and echo_responder.cpp to use the session associated with the connection. Handle Connection closure at ExchangeMgr and uplevel to corresponding ExchangeContext using the corresponding session handle. Add tests around connection establishment in TestTCP.
1 parent d2c82d1 commit 4145d2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1880
-319
lines changed

examples/shell/shell_common/include/Globals.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include <protocols/secure_channel/MessageCounterManager.h>
2525
#include <transport/SessionHolder.h>
2626
#include <transport/SessionManager.h>
27+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
2728
#include <transport/raw/TCP.h>
29+
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
2830
#include <transport/raw/UDP.h>
2931

3032
#if INET_CONFIG_ENABLE_TCP_ENDPOINT

src/app/CASESessionManager.cpp

+29-32
Original file line numberDiff line numberDiff line change
@@ -30,62 +30,57 @@ CHIP_ERROR CASESessionManager::Init(chip::System::Layer * systemLayer, const CAS
3030
}
3131

3232
void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
33-
Callback::Callback<OnDeviceConnectionFailure> * onFailure
33+
Callback::Callback<OnDeviceConnectionFailure> * onFailure,
3434
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
35-
,
36-
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
35+
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry,
3736
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
38-
)
37+
TransportPayloadCapability transportPayloadCapability)
3938
{
4039
FindOrEstablishSessionHelper(peerId, onConnection, onFailure, nullptr
4140
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
4241
,
4342
attemptCount, onRetry
4443
#endif
45-
);
44+
,
45+
transportPayloadCapability);
4646
}
4747

4848
void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
49-
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure
49+
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
5050
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
51-
,
52-
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
51+
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry,
5352
#endif
54-
)
53+
TransportPayloadCapability transportPayloadCapability)
5554
{
56-
FindOrEstablishSessionHelper(peerId, onConnection, nullptr, onSetupFailure
55+
FindOrEstablishSessionHelper(peerId, onConnection, nullptr, onSetupFailure,
5756
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
58-
,
59-
attemptCount, onRetry
57+
attemptCount, onRetry,
6058
#endif
61-
);
59+
transportPayloadCapability);
6260
}
6361

6462
void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
65-
nullptr_t
63+
nullptr_t,
6664
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
67-
,
68-
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
65+
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry,
6966
#endif
70-
)
67+
TransportPayloadCapability transportPayloadCapability)
7168
{
72-
FindOrEstablishSessionHelper(peerId, onConnection, nullptr, nullptr
69+
FindOrEstablishSessionHelper(peerId, onConnection, nullptr, nullptr,
7370
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
74-
,
75-
attemptCount, onRetry
71+
attemptCount, onRetry,
7672
#endif
77-
);
73+
transportPayloadCapability);
7874
}
7975

8076
void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerId,
8177
Callback::Callback<OnDeviceConnected> * onConnection,
8278
Callback::Callback<OnDeviceConnectionFailure> * onFailure,
83-
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure
79+
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
8480
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
85-
,
86-
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
81+
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry,
8782
#endif
88-
)
83+
TransportPayloadCapability transportPayloadCapability)
8984
{
9085
ChipLogDetail(CASESessionManager, "FindOrEstablishSession: PeerId = [%d:" ChipLogFormatX64 "]", peerId.GetFabricIndex(),
9186
ChipLogValueX64(peerId.GetNodeId()));
@@ -124,12 +119,12 @@ void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerI
124119

125120
if (onFailure != nullptr)
126121
{
127-
session->Connect(onConnection, onFailure);
122+
session->Connect(onConnection, onFailure, transportPayloadCapability);
128123
}
129124

130125
if (onSetupFailure != nullptr)
131126
{
132-
session->Connect(onConnection, onSetupFailure);
127+
session->Connect(onConnection, onSetupFailure, transportPayloadCapability);
133128
}
134129
}
135130

@@ -143,10 +138,11 @@ void CASESessionManager::ReleaseAllSessions()
143138
mConfig.sessionSetupPool->ReleaseAllSessionSetup();
144139
}
145140

146-
CHIP_ERROR CASESessionManager::GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr)
141+
CHIP_ERROR CASESessionManager::GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr,
142+
TransportPayloadCapability transportPayloadCapability)
147143
{
148144
ReturnErrorOnFailure(mConfig.sessionInitParams.Validate());
149-
auto optionalSessionHandle = FindExistingSession(peerId);
145+
auto optionalSessionHandle = FindExistingSession(peerId, transportPayloadCapability);
150146
ReturnErrorCodeIf(!optionalSessionHandle.HasValue(), CHIP_ERROR_NOT_CONNECTED);
151147
addr = optionalSessionHandle.Value()->AsSecureSession()->GetPeerAddress();
152148
return CHIP_NO_ERROR;
@@ -182,10 +178,11 @@ OperationalSessionSetup * CASESessionManager::FindExistingSessionSetup(const Sco
182178
return mConfig.sessionSetupPool->FindSessionSetup(peerId, forAddressUpdate);
183179
}
184180

185-
Optional<SessionHandle> CASESessionManager::FindExistingSession(const ScopedNodeId & peerId) const
181+
Optional<SessionHandle> CASESessionManager::FindExistingSession(const ScopedNodeId & peerId,
182+
const TransportPayloadCapability transportPayloadCapability) const
186183
{
187-
return mConfig.sessionInitParams.sessionManager->FindSecureSessionForNode(peerId,
188-
MakeOptional(Transport::SecureSession::Type::kCASE));
184+
return mConfig.sessionInitParams.sessionManager->FindSecureSessionForNode(
185+
peerId, MakeOptional(Transport::SecureSession::Type::kCASE), transportPayloadCapability);
189186
}
190187

191188
void CASESessionManager::ReleaseSession(OperationalSessionSetup * session)

src/app/CASESessionManager.h

+16-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <lib/support/Pool.h>
2727
#include <platform/CHIPDeviceLayer.h>
2828
#include <transport/SessionDelegate.h>
29+
#include <transport/SessionManager.h>
2930
#include <transport/SessionUpdateDelegate.h>
3031

3132
namespace chip {
@@ -83,7 +84,8 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
8384
,
8485
uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr
8586
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
86-
);
87+
,
88+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
8789

8890
/**
8991
* Find an existing session for the given node ID or trigger a new session request.
@@ -106,14 +108,16 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
106108
* @param onSetupFailure A callback to be called upon an extended device connection failure.
107109
* @param attemptCount The number of retry attempts if session setup fails (default is 1).
108110
* @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
111+
* @param transportPayloadCapability An indicator of what payload types the session needs to be able to transport.
109112
*/
110113
void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
111114
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure
112115
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
113116
,
114117
uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr
115118
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
116-
);
119+
,
120+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
117121

118122
/**
119123
* Find an existing session for the given node ID or trigger a new session request.
@@ -134,13 +138,15 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
134138
* @param onConnection A callback to be called upon successful connection establishment.
135139
* @param attemptCount The number of retry attempts if session setup fails (default is 1).
136140
* @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
141+
* @param transportPayloadCapability An indicator of what payload types the session needs to be able to transport.
137142
*/
138143
void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection, nullptr_t
139144
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
140145
,
141146
uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr
142147
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
143-
);
148+
,
149+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
144150

145151
void ReleaseSessionsForFabric(FabricIndex fabricIndex);
146152

@@ -154,7 +160,8 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
154160
* an ongoing session with the peer node. If the session doesn't exist, the API will return
155161
* `CHIP_ERROR_NOT_CONNECTED` error.
156162
*/
157-
CHIP_ERROR GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr);
163+
CHIP_ERROR GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr,
164+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
158165

159166
//////////// OperationalSessionReleaseDelegate Implementation ///////////////
160167
void ReleaseSession(OperationalSessionSetup * device) override;
@@ -165,15 +172,17 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
165172
private:
166173
OperationalSessionSetup * FindExistingSessionSetup(const ScopedNodeId & peerId, bool forAddressUpdate = false) const;
167174

168-
Optional<SessionHandle> FindExistingSession(const ScopedNodeId & peerId) const;
175+
Optional<SessionHandle> FindExistingSession(
176+
const ScopedNodeId & peerId,
177+
const TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload) const;
169178

170179
void FindOrEstablishSessionHelper(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
171180
Callback::Callback<OnDeviceConnectionFailure> * onFailure,
172181
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
173182
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
174-
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
183+
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry,
175184
#endif
176-
);
185+
TransportPayloadCapability transportPayloadCapability);
177186

178187
CASESessionManagerConfig mConfig;
179188
};

src/app/OperationalSessionSetup.cpp

+21-7
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ bool OperationalSessionSetup::AttachToExistingSecureSession()
7676
mState == State::WaitingForRetry,
7777
false);
7878

79-
auto sessionHandle =
80-
mInitParams.sessionManager->FindSecureSessionForNode(mPeerId, MakeOptional(Transport::SecureSession::Type::kCASE));
79+
auto sessionHandle = mInitParams.sessionManager->FindSecureSessionForNode(
80+
mPeerId, MakeOptional(Transport::SecureSession::Type::kCASE), mTransportPayloadCapability);
8181
if (!sessionHandle.HasValue())
8282
return false;
8383

@@ -93,11 +93,13 @@ bool OperationalSessionSetup::AttachToExistingSecureSession()
9393

9494
void OperationalSessionSetup::Connect(Callback::Callback<OnDeviceConnected> * onConnection,
9595
Callback::Callback<OnDeviceConnectionFailure> * onFailure,
96-
Callback::Callback<OnSetupFailure> * onSetupFailure)
96+
Callback::Callback<OnSetupFailure> * onSetupFailure,
97+
TransportPayloadCapability transportPayloadCapability)
9798
{
9899
CHIP_ERROR err = CHIP_NO_ERROR;
99100
bool isConnected = false;
100101

102+
mTransportPayloadCapability = transportPayloadCapability;
101103
//
102104
// Always enqueue our user provided callbacks into our callback list.
103105
// If anything goes wrong below, we'll trigger failures (including any queued from
@@ -180,15 +182,17 @@ void OperationalSessionSetup::Connect(Callback::Callback<OnDeviceConnected> * on
180182
}
181183

182184
void OperationalSessionSetup::Connect(Callback::Callback<OnDeviceConnected> * onConnection,
183-
Callback::Callback<OnDeviceConnectionFailure> * onFailure)
185+
Callback::Callback<OnDeviceConnectionFailure> * onFailure,
186+
TransportPayloadCapability transportPayloadCapability)
184187
{
185-
Connect(onConnection, onFailure, nullptr);
188+
Connect(onConnection, onFailure, nullptr, transportPayloadCapability);
186189
}
187190

188191
void OperationalSessionSetup::Connect(Callback::Callback<OnDeviceConnected> * onConnection,
189-
Callback::Callback<OnSetupFailure> * onSetupFailure)
192+
Callback::Callback<OnSetupFailure> * onSetupFailure,
193+
TransportPayloadCapability transportPayloadCapability)
190194
{
191-
Connect(onConnection, nullptr, onSetupFailure);
195+
Connect(onConnection, nullptr, onSetupFailure, transportPayloadCapability);
192196
}
193197

194198
void OperationalSessionSetup::UpdateDeviceData(const Transport::PeerAddress & addr, const ReliableMessageProtocolConfig & config)
@@ -288,6 +292,16 @@ void OperationalSessionSetup::UpdateDeviceData(const Transport::PeerAddress & ad
288292

289293
CHIP_ERROR OperationalSessionSetup::EstablishConnection(const ReliableMessageProtocolConfig & config)
290294
{
295+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
296+
// TODO: Combine LargePayload flag with DNS-SD advertisements from peer.
297+
// Issue #32348.
298+
if (mTransportPayloadCapability == TransportPayloadCapability::kLargePayload)
299+
{
300+
// Set the transport type for carrying large payloads
301+
mDeviceAddress.SetTransportType(chip::Transport::Type::kTcp);
302+
}
303+
#endif
304+
291305
mCASEClient = mClientPool->Allocate();
292306
ReturnErrorCodeIf(mCASEClient == nullptr, CHIP_ERROR_NO_MEMORY);
293307

src/app/OperationalSessionSetup.h

+14-3
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,12 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,
202202
* `onFailure` may be called before the Connect call returns, for error
203203
* cases that are detected synchronously (e.g. inability to start an address
204204
* lookup).
205+
*
206+
* `transportPayloadCapability` is set to kLargePayload when the session needs to be established
207+
* over a transport that allows large payloads to be transferred, e.g., TCP.
205208
*/
206-
void Connect(Callback::Callback<OnDeviceConnected> * onConnection, Callback::Callback<OnDeviceConnectionFailure> * onFailure);
209+
void Connect(Callback::Callback<OnDeviceConnected> * onConnection, Callback::Callback<OnDeviceConnectionFailure> * onFailure,
210+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
207211

208212
/*
209213
* This function can be called to establish a secure session with the device.
@@ -219,8 +223,12 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,
219223
*
220224
* `onSetupFailure` may be called before the Connect call returns, for error cases that are detected synchronously
221225
* (e.g. inability to start an address lookup).
226+
*
227+
* `transportPayloadCapability` is set to kLargePayload when the session needs to be established
228+
* over a transport that allows large payloads to be transferred, e.g., TCP.
222229
*/
223-
void Connect(Callback::Callback<OnDeviceConnected> * onConnection, Callback::Callback<OnSetupFailure> * onSetupFailure);
230+
void Connect(Callback::Callback<OnDeviceConnected> * onConnection, Callback::Callback<OnSetupFailure> * onSetupFailure,
231+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
224232

225233
bool IsForAddressUpdate() const { return mPerformingAddressUpdate; }
226234

@@ -306,6 +314,8 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,
306314

307315
bool mPerformingAddressUpdate = false;
308316

317+
TransportPayloadCapability mTransportPayloadCapability = TransportPayloadCapability::kMRPPayload;
318+
309319
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
310320
// When we TryNextResult on the resolver, it will synchronously call back
311321
// into our OnNodeAddressResolved when it succeeds. We need to track
@@ -341,7 +351,8 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,
341351
void CleanupCASEClient();
342352

343353
void Connect(Callback::Callback<OnDeviceConnected> * onConnection, Callback::Callback<OnDeviceConnectionFailure> * onFailure,
344-
Callback::Callback<OnSetupFailure> * onSetupFailure);
354+
Callback::Callback<OnSetupFailure> * onSetupFailure,
355+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
345356

346357
void EnqueueConnectionCallbacks(Callback::Callback<OnDeviceConnected> * onConnection,
347358
Callback::Callback<OnDeviceConnectionFailure> * onFailure,

src/app/server/Server.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ using chip::Transport::BleListenParameters;
7070
#endif
7171
using chip::Transport::PeerAddress;
7272
using chip::Transport::UdpListenParameters;
73+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
74+
using chip::Transport::TcpListenParameters;
75+
#endif
7376

7477
namespace {
7578

@@ -200,6 +203,12 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
200203
#if CONFIG_NETWORK_LAYER_BLE
201204
,
202205
BleListenParameters(DeviceLayer::ConnectivityMgr().GetBleLayer())
206+
#endif
207+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
208+
,
209+
TcpListenParameters(DeviceLayer::TCPEndPointManager())
210+
.SetAddressType(IPAddressType::kIPv6)
211+
.SetListenPort(mOperationalServicePort)
203212
#endif
204213
);
205214

0 commit comments

Comments
 (0)