forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReliableMessageMgr.cpp
589 lines (500 loc) · 22.8 KB
/
ReliableMessageMgr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* Copyright (c) 2020-2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file implements the CHIP reliable message protocol.
*
*/
#include <errno.h>
#include <inttypes.h>
#include <app/icd/server/ICDServerConfig.h>
#include <lib/support/BitFlags.h>
#include <lib/support/CHIPFaultInjection.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/ErrorCategory.h>
#include <messaging/ExchangeMessageDispatch.h>
#include <messaging/ExchangeMgr.h>
#include <messaging/Flags.h>
#include <messaging/ReliableMessageContext.h>
#include <messaging/ReliableMessageMgr.h>
#include <platform/ConnectivityManager.h>
#include <tracing/metric_event.h>
#if CHIP_CONFIG_ENABLE_ICD_SERVER
#include <app/icd/server/ICDConfigurationData.h> // nogncheck
#include <app/icd/server/ICDNotifier.h> // nogncheck
#endif
using namespace chip::System::Clock::Literals;
namespace chip {
namespace Messaging {
System::Clock::Timeout ReliableMessageMgr::sAdditionalMRPBackoffTime = CHIP_CONFIG_MRP_RETRY_INTERVAL_SENDER_BOOST;
ReliableMessageMgr::RetransTableEntry::RetransTableEntry(ReliableMessageContext * rc) :
ec(*rc->GetExchangeContext()), nextRetransTime(0), sendCount(0)
{
ec->SetWaitingForAck(true);
}
ReliableMessageMgr::RetransTableEntry::~RetransTableEntry()
{
ec->SetWaitingForAck(false);
}
ReliableMessageMgr::ReliableMessageMgr(ObjectPool<ExchangeContext, CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS> & contextPool) :
mContextPool(contextPool), mSystemLayer(nullptr)
{}
ReliableMessageMgr::~ReliableMessageMgr() {}
void ReliableMessageMgr::Init(chip::System::Layer * systemLayer)
{
mSystemLayer = systemLayer;
}
void ReliableMessageMgr::Shutdown()
{
StopTimer();
// Clear the retransmit table
mRetransTable.ForEachActiveObject([&](auto * entry) {
mRetransTable.ReleaseObject(entry);
return Loop::Continue;
});
mSystemLayer = nullptr;
}
void ReliableMessageMgr::TicklessDebugDumpRetransTable(const char * log)
{
#if defined(RMP_TICKLESS_DEBUG)
ChipLogDetail(ExchangeManager, "%s", log);
mRetransTable.ForEachActiveObject([&](auto * entry) {
ChipLogDetail(ExchangeManager,
"EC:" ChipLogFormatExchange " MessageCounter:" ChipLogFormatMessageCounter
" NextRetransTimeCtr: 0x" ChipLogFormatX64,
ChipLogValueExchange(&entry->ec.Get()), entry->retainedBuf.GetMessageCounter(),
ChipLogValueX64(entry->nextRetransTime.count()));
return Loop::Continue;
});
#endif
}
void ReliableMessageMgr::ExecuteActions()
{
System::Clock::Timestamp now = System::SystemClock().GetMonotonicTimestamp();
#if defined(RMP_TICKLESS_DEBUG)
ChipLogDetail(ExchangeManager, "ReliableMessageMgr::ExecuteActions at 0x" ChipLogFormatX64 "ms", ChipLogValueX64(now.count()));
#endif
ExecuteForAllContext([&](ReliableMessageContext * rc) {
if (rc->IsAckPending())
{
if (rc->mNextAckTime <= now)
{
#if defined(RMP_TICKLESS_DEBUG)
ChipLogDetail(ExchangeManager, "ReliableMessageMgr::ExecuteActions sending ACK %p", rc);
#endif
rc->SendStandaloneAckMessage();
}
}
});
// Retransmit / cancel anything in the retrans table whose retrans timeout has expired
mRetransTable.ForEachActiveObject([&](auto * entry) {
if (entry->nextRetransTime > now)
return Loop::Continue;
VerifyOrDie(!entry->retainedBuf.IsNull());
// Don't check whether the session in the exchange is valid, because when the session is released, the retrans entry is
// cleared inside ExchangeContext::OnSessionReleased, so the session must be valid if the entry exists.
auto session = entry->ec->GetSessionHandle();
uint8_t sendCount = entry->sendCount;
#if CHIP_ERROR_LOGGING || CHIP_PROGRESS_LOGGING
uint32_t messageCounter = entry->retainedBuf.GetMessageCounter();
auto fabricIndex = session->GetFabricIndex();
auto destination = kUndefinedNodeId;
if (session->IsSecureSession())
{
destination = session->AsSecureSession()->GetPeerNodeId();
}
#endif // CHIP_ERROR_LOGGING || CHIP_DETAIL_LOGGING
if (sendCount == CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS)
{
// Make sure our exchange stays alive until we are done working with it.
ExchangeHandle ec(entry->ec);
ChipLogError(ExchangeManager,
"<<%d [E:" ChipLogFormatExchange " S:%u M:" ChipLogFormatMessageCounter
"] (%s) Msg Retransmission to %u:" ChipLogFormatX64 " failure (max retries:%d)",
sendCount + 1, ChipLogValueExchange(&entry->ec.Get()), session->SessionIdForLogging(), messageCounter,
Transport::GetSessionTypeString(session), fabricIndex, ChipLogValueX64(destination),
CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS);
if (mAnalyticsDelegate)
{
ReliableMessageAnalyticsDelegate::TransmitEvent event = { .nodeId = destination,
.fabricIndex = fabricIndex,
.eventType =
ReliableMessageAnalyticsDelegate::EventType::kFailed,
.messageCounter = messageCounter };
mAnalyticsDelegate->OnTransmitEvent(event);
}
// If the exchange is expecting a response, it will handle sending
// this notification once it detects that it has not gotten a
// response. Otherwise, we need to do it.
if (!ec->IsResponseExpected())
{
if (session->IsSecureSession() && session->AsSecureSession()->IsCASESession())
{
session->AsSecureSession()->MarkAsDefunct();
}
session->NotifySessionHang();
}
// Do not StartTimer, we will schedule the timer at the end of the timer handler.
mRetransTable.ReleaseObject(entry);
return Loop::Continue;
}
entry->sendCount++;
ChipLogProgress(ExchangeManager,
"<<%d [E:" ChipLogFormatExchange " S:%u M:" ChipLogFormatMessageCounter
"] (%s) Msg Retransmission to %u:" ChipLogFormatX64,
entry->sendCount, ChipLogValueExchange(&entry->ec.Get()), session->SessionIdForLogging(), messageCounter,
Transport::GetSessionTypeString(session), fabricIndex, ChipLogValueX64(destination));
MATTER_LOG_METRIC(Tracing::kMetricDeviceRMPRetryCount, entry->sendCount);
CalculateNextRetransTime(*entry);
SendFromRetransTable(entry);
return Loop::Continue;
});
TicklessDebugDumpRetransTable("ReliableMessageMgr::ExecuteActions Dumping mRetransTable entries after processing");
}
void ReliableMessageMgr::Timeout(System::Layer * aSystemLayer, void * aAppState)
{
ReliableMessageMgr * manager = reinterpret_cast<ReliableMessageMgr *>(aAppState);
VerifyOrDie((aSystemLayer != nullptr) && (manager != nullptr));
#if defined(RMP_TICKLESS_DEBUG)
ChipLogDetail(ExchangeManager, "ReliableMessageMgr::Timeout");
#endif
// Execute any actions that are due this tick
manager->ExecuteActions();
// Calculate next physical wakeup
manager->StartTimer();
}
CHIP_ERROR ReliableMessageMgr::AddToRetransTable(ReliableMessageContext * rc, RetransTableEntry ** rEntry)
{
VerifyOrReturnError(!rc->IsWaitingForAck(), CHIP_ERROR_INCORRECT_STATE);
*rEntry = mRetransTable.CreateObject(rc);
if (*rEntry == nullptr)
{
ChipLogError(ExchangeManager, "mRetransTable Already Full");
return CHIP_ERROR_RETRANS_TABLE_FULL;
}
return CHIP_NO_ERROR;
}
System::Clock::Timeout ReliableMessageMgr::GetBackoff(System::Clock::Timeout baseInterval, uint8_t sendCount,
bool computeMaxPossible)
{
// See section "4.11.8. Parameters and Constants" for the parameters below:
// MRP_BACKOFF_JITTER = 0.25
constexpr uint32_t MRP_BACKOFF_JITTER_BASE = 1024;
// MRP_BACKOFF_MARGIN = 1.1
constexpr uint32_t MRP_BACKOFF_MARGIN_NUMERATOR = 1127;
constexpr uint32_t MRP_BACKOFF_MARGIN_DENOMINATOR = 1024;
// MRP_BACKOFF_BASE = 1.6
constexpr uint32_t MRP_BACKOFF_BASE_NUMERATOR = 16;
constexpr uint32_t MRP_BACKOFF_BASE_DENOMINATOR = 10;
constexpr int MRP_BACKOFF_THRESHOLD = 1;
// Implement `i = MRP_BACKOFF_MARGIN * i` from section "4.12.2.1. Retransmissions", where:
// i == interval
System::Clock::Milliseconds64 interval = baseInterval;
interval *= MRP_BACKOFF_MARGIN_NUMERATOR;
interval /= MRP_BACKOFF_MARGIN_DENOMINATOR;
// Implement:
// mrpBackoffTime = i * MRP_BACKOFF_BASE^(max(0,n-MRP_BACKOFF_THRESHOLD)) * (1.0 + random(0,1) * MRP_BACKOFF_JITTER)
// from section "4.12.2.1. Retransmissions", where:
// i == interval
// n == sendCount
// 1. Calculate exponent `max(0,n−MRP_BACKOFF_THRESHOLD)`
int exponent = sendCount - MRP_BACKOFF_THRESHOLD;
if (exponent < 0)
exponent = 0; // Enforce floor
if (exponent > 4)
exponent = 4; // Enforce reasonable maximum after 5 tries
// 2. Calculate `mrpBackoffTime = i * MRP_BACKOFF_BASE^(max(0,n-MRP_BACKOFF_THRESHOLD))`
uint32_t backoffNum = 1;
uint32_t backoffDenom = 1;
for (int i = 0; i < exponent; i++)
{
backoffNum *= MRP_BACKOFF_BASE_NUMERATOR;
backoffDenom *= MRP_BACKOFF_BASE_DENOMINATOR;
}
System::Clock::Milliseconds64 mrpBackoffTime = interval * backoffNum / backoffDenom;
// 3. Calculate `mrpBackoffTime *= (1.0 + random(0,1) * MRP_BACKOFF_JITTER)`
uint32_t jitter = MRP_BACKOFF_JITTER_BASE + (computeMaxPossible ? UINT8_MAX : Crypto::GetRandU8());
mrpBackoffTime = mrpBackoffTime * jitter / MRP_BACKOFF_JITTER_BASE;
#if CHIP_CONFIG_ENABLE_ICD_SERVER
// Implement:
// "An ICD sender SHOULD increase t to also account for its own sleepy interval
// required to receive the acknowledgment"
mrpBackoffTime += ICDConfigurationData::GetInstance().GetFastPollingInterval();
#endif
mrpBackoffTime += sAdditionalMRPBackoffTime;
return std::chrono::duration_cast<System::Clock::Timeout>(mrpBackoffTime);
}
void ReliableMessageMgr::StartRetransmision(RetransTableEntry * entry)
{
CalculateNextRetransTime(*entry);
StartTimer();
}
bool ReliableMessageMgr::CheckAndRemRetransTable(ReliableMessageContext * rc, uint32_t ackMessageCounter)
{
bool removed = false;
mRetransTable.ForEachActiveObject([&](auto * entry) {
if (entry->ec->GetReliableMessageContext() == rc && entry->retainedBuf.GetMessageCounter() == ackMessageCounter)
{
if (mAnalyticsDelegate)
{
auto session = entry->ec->GetSessionHandle();
auto fabricIndex = session->GetFabricIndex();
auto destination = kUndefinedNodeId;
if (session->IsSecureSession())
{
destination = session->AsSecureSession()->GetPeerNodeId();
}
ReliableMessageAnalyticsDelegate::TransmitEvent event = {
.nodeId = destination,
.fabricIndex = fabricIndex,
.eventType = ReliableMessageAnalyticsDelegate::EventType::kAcknowledged,
.messageCounter = ackMessageCounter
};
mAnalyticsDelegate->OnTransmitEvent(event);
}
// Clear the entry from the retransmision table.
ClearRetransTable(*entry);
ChipLogDetail(ExchangeManager,
"Rxd Ack; Removing MessageCounter:" ChipLogFormatMessageCounter
" from Retrans Table on exchange " ChipLogFormatExchange,
ackMessageCounter, ChipLogValueExchange(rc->GetExchangeContext()));
removed = true;
return Loop::Break;
}
return Loop::Continue;
});
return removed;
}
CHIP_ERROR ReliableMessageMgr::SendFromRetransTable(RetransTableEntry * entry)
{
if (!entry->ec->HasSessionHandle())
{
// Using same error message for all errors to reduce code size.
ChipLogError(ExchangeManager,
"Crit-err %" CHIP_ERROR_FORMAT " when sending CHIP MessageCounter:" ChipLogFormatMessageCounter
" on exchange " ChipLogFormatExchange ", send tries: %d",
CHIP_ERROR_INCORRECT_STATE.Format(), entry->retainedBuf.GetMessageCounter(),
ChipLogValueExchange(&entry->ec.Get()), entry->sendCount);
ClearRetransTable(*entry);
return CHIP_ERROR_INCORRECT_STATE;
}
auto * sessionManager = entry->ec->GetExchangeMgr()->GetSessionManager();
CHIP_ERROR err = sessionManager->SendPreparedMessage(entry->ec->GetSessionHandle(), entry->retainedBuf);
err = MapSendError(err, entry->ec->GetExchangeId(), entry->ec->IsInitiator());
if (err == CHIP_NO_ERROR)
{
#if CHIP_CONFIG_ENABLE_ICD_SERVER
app::ICDNotifier::GetInstance().NotifyNetworkActivityNotification();
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
#if CHIP_CONFIG_RESOLVE_PEER_ON_FIRST_TRANSMIT_FAILURE
const ExchangeManager * exchangeMgr = entry->ec->GetExchangeMgr();
// TODO: investigate why in ReliableMessageMgr::CheckResendApplicationMessageWithPeerExchange unit test released exchange
// context with mExchangeMgr==nullptr is used.
if (exchangeMgr)
{
// After the first failure notify session manager to refresh device data
if (entry->sendCount == 1 && mSessionUpdateDelegate != nullptr && entry->ec->GetSessionHandle()->IsSecureSession() &&
entry->ec->GetSessionHandle()->AsSecureSession()->IsCASESession())
{
ChipLogDetail(ExchangeManager, "Notify session manager to update peer address");
mSessionUpdateDelegate->UpdatePeerAddress(entry->ec->GetSessionHandle()->GetPeer());
}
}
#endif // CHIP_CONFIG_RESOLVE_PEER_ON_FIRST_TRANSMIT_FAILURE
}
else
{
// Remove from table
// Using same error message for all errors to reduce code size.
ChipLogError(ExchangeManager,
"Crit-err %" CHIP_ERROR_FORMAT " when sending CHIP MessageCounter:" ChipLogFormatMessageCounter
" on exchange " ChipLogFormatExchange ", send tries: %d",
err.Format(), entry->retainedBuf.GetMessageCounter(), ChipLogValueExchange(&entry->ec.Get()),
entry->sendCount);
ClearRetransTable(*entry);
}
return err;
}
void ReliableMessageMgr::ClearRetransTable(ReliableMessageContext * rc)
{
mRetransTable.ForEachActiveObject([&](auto * entry) {
if (entry->ec->GetReliableMessageContext() == rc)
{
ClearRetransTable(*entry);
return Loop::Break;
}
return Loop::Continue;
});
}
void ReliableMessageMgr::ClearRetransTable(RetransTableEntry & entry)
{
mRetransTable.ReleaseObject(&entry);
// Expire any virtual ticks that have expired so all wakeup sources reflect the current time
StartTimer();
}
void ReliableMessageMgr::StartTimer()
{
// When do we need to next wake up to send an ACK?
System::Clock::Timestamp nextWakeTime = System::Clock::Timestamp::max();
ExecuteForAllContext([&](ReliableMessageContext * rc) {
if (rc->IsAckPending() && rc->mNextAckTime < nextWakeTime)
{
nextWakeTime = rc->mNextAckTime;
}
});
// When do we need to next wake up for ReliableMessageProtocol retransmit?
mRetransTable.ForEachActiveObject([&](auto * entry) {
if (entry->nextRetransTime < nextWakeTime)
{
nextWakeTime = entry->nextRetransTime;
}
return Loop::Continue;
});
StopTimer();
if (nextWakeTime != System::Clock::Timestamp::max())
{
const System::Clock::Timestamp now = System::SystemClock().GetMonotonicTimestamp();
const auto nextWakeDelay = (nextWakeTime > now) ? nextWakeTime - now : 0_ms;
#if defined(RMP_TICKLESS_DEBUG)
ChipLogDetail(ExchangeManager,
"ReliableMessageMgr::StartTimer at 0x" ChipLogFormatX64 "ms wake at 0x" ChipLogFormatX64
"ms (in 0x" ChipLogFormatX64 "ms)",
ChipLogValueX64(now.count()), ChipLogValueX64(nextWakeTime.count()), ChipLogValueX64(nextWakeDelay.count()));
#endif
VerifyOrDie(mSystemLayer->StartTimer(nextWakeDelay, Timeout, this) == CHIP_NO_ERROR);
}
else
{
#if defined(RMP_TICKLESS_DEBUG)
ChipLogDetail(ExchangeManager, "ReliableMessageMgr skipped timer");
#endif
}
TicklessDebugDumpRetransTable("ReliableMessageMgr::StartTimer Dumping mRetransTable entries after setting wakeup times");
}
void ReliableMessageMgr::StopTimer()
{
mSystemLayer->CancelTimer(Timeout, this);
}
void ReliableMessageMgr::RegisterSessionUpdateDelegate(SessionUpdateDelegate * sessionUpdateDelegate)
{
mSessionUpdateDelegate = sessionUpdateDelegate;
}
void ReliableMessageMgr::RegisterAnalyticsDelegate(ReliableMessageAnalyticsDelegate * analyticsDelegate)
{
mAnalyticsDelegate = analyticsDelegate;
}
CHIP_ERROR ReliableMessageMgr::MapSendError(CHIP_ERROR error, uint16_t exchangeId, bool isInitiator)
{
if (
#if CHIP_SYSTEM_CONFIG_USE_LWIP
error == System::MapErrorLwIP(ERR_MEM)
#else
error == CHIP_ERROR_POSIX(ENOBUFS)
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
)
{
// sendmsg on BSD-based systems never blocks, no matter how the
// socket is configured, and will return ENOBUFS in situation in
// which Linux, for example, blocks.
//
// This is typically a transient situation, so we pretend like this
// packet drop happened somewhere on the network instead of inside
// sendmsg and will just resend it in the normal MRP way later.
//
// Similarly, on LwIP an ERR_MEM on send indicates a likely
// temporary lack of TX buffers.
ChipLogError(ExchangeManager, "Ignoring transient send error: %" CHIP_ERROR_FORMAT " on exchange " ChipLogFormatExchangeId,
error.Format(), ChipLogValueExchangeId(exchangeId, isInitiator));
error = CHIP_NO_ERROR;
}
return error;
}
void ReliableMessageMgr::SetAdditionalMRPBackoffTime(const Optional<System::Clock::Timeout> & additionalTime)
{
sAdditionalMRPBackoffTime = additionalTime.ValueOr(CHIP_CONFIG_MRP_RETRY_INTERVAL_SENDER_BOOST);
}
void ReliableMessageMgr::CalculateNextRetransTime(RetransTableEntry & entry)
{
System::Clock::Timeout baseTimeout = System::Clock::Timeout(0);
const auto sessionHandle = entry.ec->GetSessionHandle();
// Check if we have received at least one application-level message
if (entry.ec->HasReceivedAtLeastOneMessage())
{
// If we have received at least one message, assume peer is active and use ActiveRetransTimeout
baseTimeout = sessionHandle->GetRemoteMRPConfig().mActiveRetransTimeout;
}
else
{
// If we haven't received at least one message
// Choose active/idle timeout from PeerActiveMode of session per 4.11.2.1. Retransmissions.
baseTimeout = sessionHandle->GetMRPBaseTimeout();
}
System::Clock::Timeout backoff = ReliableMessageMgr::GetBackoff(baseTimeout, entry.sendCount);
entry.nextRetransTime = System::SystemClock().GetMonotonicTimestamp() + backoff;
#if CHIP_PROGRESS_LOGGING
const auto config = sessionHandle->GetRemoteMRPConfig();
uint32_t messageCounter = entry.retainedBuf.GetMessageCounter();
auto fabricIndex = sessionHandle->GetFabricIndex();
auto destination = kUndefinedNodeId;
bool peerIsActive = false;
if (sessionHandle->IsSecureSession())
{
peerIsActive = sessionHandle->AsSecureSession()->IsPeerActive();
destination = sessionHandle->AsSecureSession()->GetPeerNodeId();
}
else if (sessionHandle->IsUnauthenticatedSession())
{
peerIsActive = sessionHandle->AsUnauthenticatedSession()->IsPeerActive();
}
// For initial send the packet has already been submitted to transport layer successfully.
// On re-transmits we do not know if transport layer is unable to retransmit for some
// reason, so saying we have sent re-transmit here is a little presumptuous.
if (mAnalyticsDelegate)
{
ReliableMessageAnalyticsDelegate::TransmitEvent event = {
.nodeId = destination,
.fabricIndex = fabricIndex,
.eventType = entry.sendCount == 0 ? ReliableMessageAnalyticsDelegate::EventType::kInitialSend
: ReliableMessageAnalyticsDelegate::EventType::kRetransmission,
.messageCounter = messageCounter
};
mAnalyticsDelegate->OnTransmitEvent(event);
}
ChipLogProgress(ExchangeManager,
"??%d [E:" ChipLogFormatExchange " S:%u M:" ChipLogFormatMessageCounter
"] (%s) Msg Retransmission to %u:" ChipLogFormatX64 " scheduled for %" PRIu32
"ms from now [State:%s II:%" PRIu32 " AI:%" PRIu32 " AT:%u]",
entry.sendCount + 1, ChipLogValueExchange(&entry.ec.Get()), sessionHandle->SessionIdForLogging(),
messageCounter, Transport::GetSessionTypeString(sessionHandle), fabricIndex, ChipLogValueX64(destination),
backoff.count(), peerIsActive ? "Active" : "Idle", config.mIdleRetransTimeout.count(),
config.mActiveRetransTimeout.count(), config.mActiveThresholdTime.count());
#endif // CHIP_PROGRESS_LOGGING
}
#if CHIP_CONFIG_TEST
int ReliableMessageMgr::TestGetCountRetransTable()
{
int count = 0;
mRetransTable.ForEachActiveObject([&](auto * entry) {
count++;
return Loop::Continue;
});
return count;
}
#endif // CHIP_CONFIG_TEST
} // namespace Messaging
} // namespace chip