Skip to content

Commit aa1002c

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 de43a78 commit aa1002c

Some content is hidden

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

43 files changed

+1923
-289
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

+30-13
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Cal
3535
,
3636
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
3737
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
38-
)
38+
,
39+
TransportPayloadCapability transportPayloadCapability)
3940
{
4041
FindOrEstablishSessionHelper(peerId, onConnection, onFailure, nullptr
4142
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
4243
,
4344
attemptCount, onRetry
4445
#endif
45-
);
46+
,
47+
transportPayloadCapability);
4648
}
4749

4850
void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
@@ -51,14 +53,16 @@ void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Cal
5153
,
5254
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
5355
#endif
54-
)
56+
,
57+
TransportPayloadCapability transportPayloadCapability)
5558
{
5659
FindOrEstablishSessionHelper(peerId, onConnection, nullptr, onSetupFailure
5760
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
5861
,
5962
attemptCount, onRetry
6063
#endif
61-
);
64+
,
65+
transportPayloadCapability);
6266
}
6367

6468
void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
@@ -67,14 +71,16 @@ void CASESessionManager::FindOrEstablishSession(const ScopedNodeId & peerId, Cal
6771
,
6872
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
6973
#endif
70-
)
74+
,
75+
TransportPayloadCapability transportPayloadCapability)
7176
{
7277
FindOrEstablishSessionHelper(peerId, onConnection, nullptr, nullptr
7378
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
7479
,
7580
attemptCount, onRetry
7681
#endif
77-
);
82+
,
83+
transportPayloadCapability);
7884
}
7985

8086
void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerId,
@@ -85,7 +91,8 @@ void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerI
8591
,
8692
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
8793
#endif
88-
)
94+
,
95+
TransportPayloadCapability transportPayloadCapability)
8996
{
9097
ChipLogDetail(CASESessionManager, "FindOrEstablishSession: PeerId = [%d:" ChipLogFormatX64 "]", peerId.GetFabricIndex(),
9198
ChipLogValueX64(peerId.GetNodeId()));
@@ -124,12 +131,12 @@ void CASESessionManager::FindOrEstablishSessionHelper(const ScopedNodeId & peerI
124131

125132
if (onFailure != nullptr)
126133
{
127-
session->Connect(onConnection, onFailure);
134+
session->Connect(onConnection, onFailure, transportPayloadCapability);
128135
}
129136

130137
if (onSetupFailure != nullptr)
131138
{
132-
session->Connect(onConnection, onSetupFailure);
139+
session->Connect(onConnection, onSetupFailure, transportPayloadCapability);
133140
}
134141
}
135142

@@ -146,7 +153,16 @@ void CASESessionManager::ReleaseAllSessions()
146153
CHIP_ERROR CASESessionManager::GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr)
147154
{
148155
ReturnErrorOnFailure(mConfig.sessionInitParams.Validate());
149-
auto optionalSessionHandle = FindExistingSession(peerId);
156+
auto optionalSessionHandle = FindExistingSession(peerId, TransportPayloadCapability::kMRPPayload);
157+
ReturnErrorCodeIf(!optionalSessionHandle.HasValue(), CHIP_ERROR_NOT_CONNECTED);
158+
addr = optionalSessionHandle.Value()->AsSecureSession()->GetPeerAddress();
159+
return CHIP_NO_ERROR;
160+
}
161+
162+
CHIP_ERROR CASESessionManager::GetPeerAddressForLargePayloadSession(const ScopedNodeId & peerId, Transport::PeerAddress & addr)
163+
{
164+
ReturnErrorOnFailure(mConfig.sessionInitParams.Validate());
165+
auto optionalSessionHandle = FindExistingSession(peerId, TransportPayloadCapability::kLargePayload);
150166
ReturnErrorCodeIf(!optionalSessionHandle.HasValue(), CHIP_ERROR_NOT_CONNECTED);
151167
addr = optionalSessionHandle.Value()->AsSecureSession()->GetPeerAddress();
152168
return CHIP_NO_ERROR;
@@ -182,10 +198,11 @@ OperationalSessionSetup * CASESessionManager::FindExistingSessionSetup(const Sco
182198
return mConfig.sessionSetupPool->FindSessionSetup(peerId, forAddressUpdate);
183199
}
184200

185-
Optional<SessionHandle> CASESessionManager::FindExistingSession(const ScopedNodeId & peerId) const
201+
Optional<SessionHandle> CASESessionManager::FindExistingSession(const ScopedNodeId & peerId,
202+
const TransportPayloadCapability transportPayloadCapability) const
186203
{
187-
return mConfig.sessionInitParams.sessionManager->FindSecureSessionForNode(peerId,
188-
MakeOptional(Transport::SecureSession::Type::kCASE));
204+
return mConfig.sessionInitParams.sessionManager->FindSecureSessionForNode(
205+
peerId, MakeOptional(Transport::SecureSession::Type::kCASE), transportPayloadCapability);
189206
}
190207

191208
void CASESessionManager::ReleaseSession(OperationalSessionSetup * session)

src/app/CASESessionManager.h

+16-5
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 whether to use a transport that supports transfer of large payloads.
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 whether to use a transport that supports transfer of large payloads.
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

@@ -156,6 +162,8 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
156162
*/
157163
CHIP_ERROR GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr);
158164

165+
CHIP_ERROR GetPeerAddressForLargePayloadSession(const ScopedNodeId & peerId, Transport::PeerAddress & addr);
166+
159167
//////////// OperationalSessionReleaseDelegate Implementation ///////////////
160168
void ReleaseSession(OperationalSessionSetup * device) override;
161169

@@ -165,15 +173,18 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
165173
private:
166174
OperationalSessionSetup * FindExistingSessionSetup(const ScopedNodeId & peerId, bool forAddressUpdate = false) const;
167175

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

170180
void FindOrEstablishSessionHelper(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
171181
Callback::Callback<OnDeviceConnectionFailure> * onFailure,
172182
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
173183
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
174184
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
175185
#endif
176-
);
186+
,
187+
TransportPayloadCapability transportPayloadCapability);
177188

178189
CASESessionManagerConfig mConfig;
179190
};

src/app/OperationalSessionSetup.cpp

+27-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)
@@ -291,6 +295,22 @@ CHIP_ERROR OperationalSessionSetup::EstablishConnection(const ReliableMessagePro
291295
mCASEClient = mClientPool->Allocate();
292296
ReturnErrorCodeIf(mCASEClient == nullptr, CHIP_ERROR_NO_MEMORY);
293297

298+
// TODO: Combine LargePayload flag with DNS-SD advertisements from peer.
299+
// Issue #32348.
300+
if (mTransportPayloadCapability == TransportPayloadCapability::kLargePayload)
301+
{
302+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
303+
// Set the transport type for carrying large payloads
304+
mDeviceAddress.SetTransportType(chip::Transport::Type::kTcp);
305+
#else
306+
ChipLogError(Discovery, "Cannot setup session for large payload. Appropriate Transport unavailable");
307+
308+
CleanupCASEClient();
309+
310+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
311+
#endif
312+
}
313+
294314
CHIP_ERROR err = mCASEClient->EstablishSession(mInitParams, mPeerId, mDeviceAddress, config, this);
295315
if (err != CHIP_NO_ERROR)
296316
{

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 kLarge 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 kLarge 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

@@ -305,6 +313,8 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,
305313

306314
bool mPerformingAddressUpdate = false;
307315

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

340350
void Connect(Callback::Callback<OnDeviceConnected> * onConnection, Callback::Callback<OnDeviceConnectionFailure> * onFailure,
341-
Callback::Callback<OnSetupFailure> * onSetupFailure);
351+
Callback::Callback<OnSetupFailure> * onSetupFailure,
352+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);
342353

343354
void EnqueueConnectionCallbacks(Callback::Callback<OnDeviceConnected> * onConnection,
344355
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

src/app/server/Server.h

+10
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ namespace chip {
7777

7878
inline constexpr size_t kMaxBlePendingPackets = 1;
7979

80+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
81+
inline constexpr size_t kMaxTcpActiveConnectionCount = CHIP_CONFIG_DEVICE_MAX_ACTIVE_TCP_CONNECTIONS;
82+
83+
inline constexpr size_t kMaxTcpPendingPackets = CHIP_CONFIG_MAX_TCP_PENDING_PACKETS;
84+
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
85+
8086
//
8187
// NOTE: Please do not alter the order of template specialization here as the logic
8288
// in the Server impl depends on this.
@@ -89,6 +95,10 @@ using ServerTransportMgr = chip::TransportMgr<chip::Transport::UDP
8995
#if CONFIG_NETWORK_LAYER_BLE
9096
,
9197
chip::Transport::BLE<kMaxBlePendingPackets>
98+
#endif
99+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
100+
,
101+
chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>
92102
#endif
93103
>;
94104

0 commit comments

Comments
 (0)