Skip to content

Commit d1f865a

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 9aeff06 commit d1f865a

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

+1897
-286
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::kDefault);
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::kLarge);
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 {
@@ -85,7 +86,8 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
8586
,
8687
uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr
8788
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
88-
);
89+
,
90+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kDefault);
8991

9092
/**
9193
* Find an existing session for the given node ID or trigger a new session request.
@@ -108,14 +110,16 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
108110
* @param onSetupFailure A callback to be called upon an extended device connection failure.
109111
* @param attemptCount The number of retry attempts if session setup fails (default is 1).
110112
* @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
113+
* @param transportPayloadCapability An indicator of whether to use a transport that supports transfer of large payloads.
111114
*/
112115
void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
113116
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure
114117
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
115118
,
116119
uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr
117120
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
118-
);
121+
,
122+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kDefault);
119123

120124
/**
121125
* Find an existing session for the given node ID or trigger a new session request.
@@ -136,13 +140,15 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
136140
* @param onConnection A callback to be called upon successful connection establishment.
137141
* @param attemptCount The number of retry attempts if session setup fails (default is 1).
138142
* @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
143+
* @param transportPayloadCapability An indicator of whether to use a transport that supports transfer of large payloads.
139144
*/
140145
void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection, nullptr_t
141146
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
142147
,
143148
uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr
144149
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
145-
);
150+
,
151+
TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kDefault);
146152

147153
void ReleaseSessionsForFabric(FabricIndex fabricIndex);
148154

@@ -158,6 +164,8 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
158164
*/
159165
CHIP_ERROR GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr);
160166

167+
CHIP_ERROR GetPeerAddressForLargePayloadSession(const ScopedNodeId & peerId, Transport::PeerAddress & addr);
168+
161169
//////////// OperationalSessionReleaseDelegate Implementation ///////////////
162170
void ReleaseSession(OperationalSessionSetup * device) override;
163171

@@ -167,15 +175,18 @@ class CASESessionManager : public OperationalSessionReleaseDelegate, public Sess
167175
private:
168176
OperationalSessionSetup * FindExistingSessionSetup(const ScopedNodeId & peerId, bool forAddressUpdate = false) const;
169177

170-
Optional<SessionHandle> FindExistingSession(const ScopedNodeId & peerId) const;
178+
Optional<SessionHandle>
179+
FindExistingSession(const ScopedNodeId & peerId,
180+
const TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kDefault) const;
171181

172182
void FindOrEstablishSessionHelper(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
173183
Callback::Callback<OnDeviceConnectionFailure> * onFailure,
174184
Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
175185
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
176186
uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry
177187
#endif
178-
);
188+
,
189+
TransportPayloadCapability transportPayloadCapability);
179190

180191
CASESessionManagerConfig mConfig;
181192
};

src/app/OperationalSessionSetup.cpp

+26-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,21 @@ 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+
if (mTransportPayloadCapability == TransportPayloadCapability::kLarge)
300+
{
301+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
302+
// Set the transport type for carrying large payloads
303+
mDeviceAddress.SetTransportType(chip::Transport::Type::kTcp);
304+
#else
305+
ChipLogError(Discovery, "Cannot setup session for large payload. Appropriate Transport unavailable");
306+
307+
CleanupCASEClient();
308+
309+
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
310+
#endif
311+
}
312+
294313
CHIP_ERROR err = mCASEClient->EstablishSession(mInitParams, mPeerId, mDeviceAddress, config, this);
295314
if (err != CHIP_NO_ERROR)
296315
{

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::kDefault);
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::kDefault);
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::kDefault;
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::kDefault);
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)