@@ -63,21 +63,27 @@ constexpr chip::FabricIndex gFabricIndex = 0;
63
63
chip::Protocols::Echo::EchoClient gEchoClient ;
64
64
65
65
chip::TransportMgr<chip::Transport::UDP> gUDPManager ;
66
+ #if INET_CONFIG_ENABLE_TCP_ENDPOINT
66
67
chip::TransportMgr<chip::Transport::TCP<kMaxTcpActiveConnectionCount , kMaxTcpPendingPackets >> gTCPManager ;
67
- chip::Inet::IPAddress gDestAddr ;
68
- chip::SessionHolder gSession ;
69
68
70
69
chip::Transport::AppTCPConnectionCallbackCtxt gAppTCPConnCbCtxt ;
71
70
chip::Transport::ActiveTCPConnectionState * gActiveTCPConnState = nullptr ;
72
71
73
- // The last time a CHIP Echo was attempted to be sent.
74
- chip::System::Clock::Timestamp gLastEchoTime = chip::System::Clock:: kZero ;
72
+ static void HandleConnectionAttemptComplete (chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
73
+ static void HandleConnectionClosed ( chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr) ;
75
74
76
75
// True, if client is still connecting to the server, false otherwise.
77
76
static bool gClientConInProgress = false ;
78
77
79
78
// True, once client connection to server is established.
80
79
static bool gClientConEstablished = false ;
80
+ #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
81
+
82
+ chip::Inet::IPAddress gDestAddr ;
83
+ chip::SessionHolder gSession ;
84
+
85
+ // The last time a CHIP Echo was attempted to be sent.
86
+ chip::System::Clock::Timestamp gLastEchoTime = chip::System::Clock::kZero ;
81
87
82
88
// The handle to the TCP connection to the peer.
83
89
// static chip::Transport::ActiveTCPConnectionState * gCon = nullptr;
@@ -95,9 +101,6 @@ uint64_t gTCPConnAttemptCount = 0;
95
101
CHIP_ERROR SendEchoRequest ();
96
102
void EchoTimerHandler (chip::System::Layer * systemLayer, void * appState);
97
103
98
- static void HandleConnectionAttemptComplete (chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
99
- static void HandleConnectionClosed (chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
100
-
101
104
void Shutdown ()
102
105
{
103
106
chip::DeviceLayer::SystemLayer ().CancelTimer (EchoTimerHandler, nullptr );
@@ -170,7 +173,7 @@ CHIP_ERROR SendEchoRequest()
170
173
return err;
171
174
}
172
175
173
- CHIP_ERROR EstablishSecureSession (chip::Transport::ActiveTCPConnectionState * conn = nullptr )
176
+ CHIP_ERROR EstablishSecureSession ()
174
177
{
175
178
char peerAddrBuf[chip::Transport::PeerAddress::kMaxToStringSize ];
176
179
chip::Transport::PeerAddress peerAddr;
@@ -195,18 +198,21 @@ CHIP_ERROR EstablishSecureSession(chip::Transport::ActiveTCPConnectionState * co
195
198
}
196
199
else
197
200
{
201
+ #if INET_CONFIG_ENABLE_TCP_ENDPOINT
198
202
if (gUseTCP )
199
203
{
200
- printf (" Associating secure session with connection %p\n " , conn );
201
- gSession .Get ().Value ()->AsSecureSession ()->SetTCPConnection (conn );
204
+ printf (" Associating secure session with connection %p\n " , gActiveTCPConnState );
205
+ gSession .Get ().Value ()->AsSecureSession ()->SetTCPConnection (gActiveTCPConnState );
202
206
}
207
+ #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
203
208
204
209
printf (" Successfully established secure session with peer at %s\n " , peerAddrBuf);
205
210
}
206
211
207
212
return err;
208
213
}
209
214
215
+ #if INET_CONFIG_ENABLE_TCP_ENDPOINT
210
216
void CloseConnection ()
211
217
{
212
218
char peerAddrBuf[chip::Transport::PeerAddress::kMaxToStringSize ];
@@ -225,7 +231,7 @@ void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState *
225
231
{
226
232
chip::DeviceLayer::PlatformMgr ().StopEventLoopTask ();
227
233
228
- if (err != CHIP_NO_ERROR)
234
+ if (err != CHIP_NO_ERROR || conn != gActiveTCPConnState )
229
235
{
230
236
printf (" Connection FAILED with err: %s\n " , chip::ErrorStr (err));
231
237
@@ -235,7 +241,7 @@ void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState *
235
241
return ;
236
242
}
237
243
238
- err = EstablishSecureSession (conn );
244
+ err = EstablishSecureSession ();
239
245
if (err != CHIP_NO_ERROR)
240
246
{
241
247
printf (" Secure session FAILED with err: %s\n " , chip::ErrorStr (err));
@@ -281,6 +287,7 @@ void EstablishTCPConnection()
281
287
282
288
gClientConInProgress = true ;
283
289
}
290
+ #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
284
291
285
292
void HandleEchoResponseReceived (chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle && payload)
286
293
{
@@ -332,6 +339,7 @@ int main(int argc, char * argv[])
332
339
333
340
InitializeChip ();
334
341
342
+ #if INET_CONFIG_ENABLE_TCP_ENDPOINT
335
343
if (gUseTCP )
336
344
{
337
345
err = gTCPManager .Init (chip::Transport::TcpListenParameters (chip::DeviceLayer::TCPEndPointManager ())
@@ -348,6 +356,7 @@ int main(int argc, char * argv[])
348
356
gAppTCPConnCbCtxt .connClosedCb = HandleConnectionClosed;
349
357
}
350
358
else
359
+ #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
351
360
{
352
361
err = gUDPManager .Init (chip::Transport::UdpListenParameters (chip::DeviceLayer::UDPEndPointManager ())
353
362
.SetAddressType (chip::Inet::IPAddressType::kIPv6 )
@@ -365,13 +374,14 @@ int main(int argc, char * argv[])
365
374
err = gMessageCounterManager .Init (&gExchangeManager );
366
375
SuccessOrExit (err);
367
376
377
+ #if INET_CONFIG_ENABLE_TCP_ENDPOINT
368
378
if (gUseTCP )
369
379
{
370
380
371
381
while (!gClientConEstablished )
372
382
{
373
383
// For TCP transport, attempt to establish the connection to the CHIP echo responder.
374
- // On Connection completion, call EstablishSecureSession(conn );
384
+ // On Connection completion, call EstablishSecureSession();
375
385
EstablishTCPConnection ();
376
386
377
387
chip::DeviceLayer::PlatformMgr ().RunEventLoop ();
@@ -383,9 +393,10 @@ int main(int argc, char * argv[])
383
393
}
384
394
}
385
395
else
396
+ #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
386
397
{
387
398
// Start the CHIP session to the CHIP echo responder.
388
- err = EstablishSecureSession (nullptr );
399
+ err = EstablishSecureSession ();
389
400
SuccessOrExit (err);
390
401
}
391
402
@@ -402,11 +413,13 @@ int main(int argc, char * argv[])
402
413
403
414
gUDPManager .Close ();
404
415
416
+ #if INET_CONFIG_ENABLE_TCP_ENDPOINT
405
417
if (gUseTCP )
406
418
{
407
419
gTCPManager .TCPDisconnect (chip::Transport::PeerAddress::TCP (gDestAddr ));
408
420
}
409
421
gTCPManager .Close ();
422
+ #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
410
423
411
424
Shutdown ();
412
425
0 commit comments