Skip to content

Commit 73e3c83

Browse files
[SED] Review switching between polling modes (project-chip#14418)
* [SED] Review switching between polling modes Sleepy End Device polling mode switching behaves incorrectly in some scenarios. Request fast-polling mode when commissioning window over IP (not BLE) is open. Refactor the switching code in ExchangeContext to make sure that a single exchange can request fast-polling mode only once and that the request is always withdrawn. Also, request the fast-polling while waiting for ACK. * Review comment
1 parent 2294bcc commit 73e3c83

File tree

4 files changed

+80
-33
lines changed

4 files changed

+80
-33
lines changed

src/app/server/CommissioningWindowManager.cpp

+19-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <lib/support/CodeUtils.h>
2323
#include <platform/CHIPDeviceLayer.h>
2424

25+
using namespace chip::app::Clusters;
26+
2527
namespace {
2628

2729
// As per specifications (Section 13.3), Nodes SHALL exit commissioning mode after 20 failed commission attempts.
@@ -259,7 +261,7 @@ CHIP_ERROR CommissioningWindowManager::OpenEnhancedCommissioningWindow(uint16_t
259261

260262
void CommissioningWindowManager::CloseCommissioningWindow()
261263
{
262-
if (mWindowStatus != app::Clusters::AdministratorCommissioning::CommissioningWindowStatus::kWindowNotOpen)
264+
if (mWindowStatus != AdministratorCommissioning::CommissioningWindowStatus::kWindowNotOpen)
263265
{
264266
ChipLogProgress(AppServer, "Closing pairing window");
265267
Cleanup();
@@ -273,10 +275,18 @@ CHIP_ERROR CommissioningWindowManager::StartAdvertisement()
273275
DeviceLayer::ConfigurationMgr().NotifyOfAdvertisementStart();
274276
#endif
275277

278+
#if CHIP_DEVICE_CONFIG_ENABLE_SED
279+
if (!mIsBLE && mWindowStatus == AdministratorCommissioning::CommissioningWindowStatus::kWindowNotOpen)
280+
{
281+
DeviceLayer::ConnectivityMgr().RequestSEDFastPollingMode(true);
282+
}
283+
#endif
284+
276285
if (mIsBLE)
277286
{
278287
ReturnErrorOnFailure(chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true));
279288
}
289+
280290
if (mAppDelegate != nullptr)
281291
{
282292
mAppDelegate->OnPairingWindowOpened();
@@ -285,22 +295,18 @@ CHIP_ERROR CommissioningWindowManager::StartAdvertisement()
285295
Dnssd::CommissioningMode commissioningMode;
286296
if (mUseECM)
287297
{
288-
mWindowStatus = app::Clusters::AdministratorCommissioning::CommissioningWindowStatus::kEnhancedWindowOpen;
298+
mWindowStatus = AdministratorCommissioning::CommissioningWindowStatus::kEnhancedWindowOpen;
289299
commissioningMode = Dnssd::CommissioningMode::kEnabledEnhanced;
290300
}
291301
else
292302
{
293-
mWindowStatus = app::Clusters::AdministratorCommissioning::CommissioningWindowStatus::kBasicWindowOpen;
303+
mWindowStatus = AdministratorCommissioning::CommissioningWindowStatus::kBasicWindowOpen;
294304
commissioningMode = Dnssd::CommissioningMode::kEnabledBasic;
295305
}
296306

297307
// reset all advertising, indicating we are in commissioningMode
298308
app::DnssdServer::Instance().StartServer(commissioningMode);
299309

300-
#if CHIP_DEVICE_CONFIG_ENABLE_SED
301-
DeviceLayer::ConnectivityMgr().RequestSEDFastPollingMode(true);
302-
#endif
303-
304310
return CHIP_NO_ERROR;
305311
}
306312

@@ -311,12 +317,15 @@ CHIP_ERROR CommissioningWindowManager::StopAdvertisement(bool aShuttingDown)
311317
mServer->GetExchangeManager().UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::PBKDFParamRequest);
312318
mPairingSession.Clear();
313319

314-
mWindowStatus = app::Clusters::AdministratorCommissioning::CommissioningWindowStatus::kWindowNotOpen;
315-
316320
#if CHIP_DEVICE_CONFIG_ENABLE_SED
317-
DeviceLayer::ConnectivityMgr().RequestSEDFastPollingMode(false);
321+
if (!mIsBLE && mWindowStatus != AdministratorCommissioning::CommissioningWindowStatus::kWindowNotOpen)
322+
{
323+
DeviceLayer::ConnectivityMgr().RequestSEDFastPollingMode(false);
324+
}
318325
#endif
319326

327+
mWindowStatus = AdministratorCommissioning::CommissioningWindowStatus::kWindowNotOpen;
328+
320329
// If aShuttingDown, don't try to change our DNS-SD advertisements.
321330
if (!aShuttingDown)
322331
{

src/messaging/ExchangeContext.cpp

+27-16
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,30 @@ void ExchangeContext::SetResponseTimeout(Timeout timeout)
8787
#if CONFIG_DEVICE_LAYER && CHIP_DEVICE_CONFIG_ENABLE_SED
8888
void ExchangeContext::UpdateSEDPollingMode()
8989
{
90-
SessionHandle sessionHandle = GetSessionHandle();
91-
Transport::Session::SessionType sessType = sessionHandle->GetSessionType();
90+
Transport::PeerAddress address;
9291

93-
// During PASE session, which happen on BLE, the session is kUnauthenticated
94-
// So AsSecureSession() ends up faulting the system
95-
if (sessType != Transport::Session::SessionType::kUnauthenticated)
92+
switch (GetSessionHandle()->GetSessionType())
9693
{
97-
if (sessionHandle->AsSecureSession()->GetPeerAddress().GetTransportType() != Transport::Type::kBle)
98-
{
99-
if (!IsResponseExpected() && !IsSendExpected() && (mExchangeMgr->GetNumActiveExchanges() == 1))
100-
{
101-
chip::DeviceLayer::ConnectivityMgr().RequestSEDFastPollingMode(false);
102-
}
103-
else
104-
{
105-
chip::DeviceLayer::ConnectivityMgr().RequestSEDFastPollingMode(true);
106-
}
107-
}
94+
case Transport::Session::SessionType::kSecure:
95+
address = GetSessionHandle()->AsSecureSession()->GetPeerAddress();
96+
break;
97+
case Transport::Session::SessionType::kUnauthenticated:
98+
address = GetSessionHandle()->AsUnauthenticatedSession()->GetPeerAddress();
99+
break;
100+
default:
101+
return;
102+
}
103+
104+
VerifyOrReturn(address.GetTransportType() != Transport::Type::kBle);
105+
UpdateSEDPollingMode(IsResponseExpected() || IsSendExpected() || IsMessageNotAcked());
106+
}
107+
108+
void ExchangeContext::UpdateSEDPollingMode(bool fastPollingMode)
109+
{
110+
if (fastPollingMode != IsRequestingFastPollingMode())
111+
{
112+
SetRequestingFastPollingMode(fastPollingMode);
113+
DeviceLayer::ConnectivityMgr().RequestSEDFastPollingMode(fastPollingMode);
108114
}
109115
}
110116
#endif
@@ -287,6 +293,11 @@ ExchangeContext::~ExchangeContext()
287293
VerifyOrDie(mExchangeMgr != nullptr && GetReferenceCount() == 0);
288294
VerifyOrDie(!IsAckPending());
289295

296+
#if CONFIG_DEVICE_LAYER && CHIP_DEVICE_CONFIG_ENABLE_SED
297+
// Make sure that the exchange withdraws the request for Sleepy End Device fast-polling mode.
298+
UpdateSEDPollingMode(false);
299+
#endif
300+
290301
// Ideally, in this scenario, the retransmit table should
291302
// be clear of any outstanding messages for this context and
292303
// the boolean parameter passed to DoClose() should not matter.

src/messaging/ExchangeContext.h

+14-7
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,23 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext,
245245
void MessageHandled();
246246

247247
/**
248-
* Updates Sleepy End Device polling interval in the following way:
248+
* Updates Sleepy End Device polling mode in the following way:
249249
* - does nothing for exchanges over Bluetooth LE
250-
* - set IDLE polling mode if all conditions are met:
251-
* - device doesn't expect getting response nor sending message
252-
* - there is no other active exchange than the current one
253-
* - set ACTIVE polling mode if any of the conditions is met:
254-
* - device expects getting response or sending message
255-
* - there is another active exchange
250+
* - requests fast-polling (active) mode if there are more messages,
251+
* including MRP acknowledgements, expected to be sent or received on
252+
* this exchange.
253+
* - withdraws the request for fast-polling (active) mode, otherwise.
256254
*/
257255
void UpdateSEDPollingMode();
256+
257+
/**
258+
* Requests or withdraws the request for Sleepy End Device fast-polling mode
259+
* based on the argument value.
260+
*
261+
* Note that the device switches to the slow-polling (idle) mode if no
262+
* exchange nor other component requests the fast-polling mode.
263+
*/
264+
void UpdateSEDPollingMode(bool fastPollingMode);
258265
};
259266

260267
} // namespace Messaging

src/messaging/ReliableMessageContext.h

+20
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ class ReliableMessageContext
153153
/// Set whether there is a message hasn't been acknowledged.
154154
void SetMessageNotAcked(bool messageNotAcked);
155155

156+
/// Set if this exchange is requesting Sleepy End Device fast-polling mode
157+
void SetRequestingFastPollingMode(bool fastPollingMode);
158+
159+
/// Determine whether this exchange is requesting Sleepy End Device fast-polling mode
160+
bool IsRequestingFastPollingMode() const;
161+
156162
/**
157163
* Get the reliable message manager that corresponds to this reliable
158164
* message context.
@@ -194,8 +200,12 @@ class ReliableMessageContext
194200

195201
/// When set, signifies that we are currently in the middle of HandleMessage.
196202
kFlagHandlingMessage = (1u << 9),
203+
197204
/// When set, we have had Close() or Abort() called on us already.
198205
kFlagClosed = (1u << 10),
206+
207+
/// When set, signifies that the exchange is requesting Sleepy End Device fast-polling mode.
208+
kFlagFastPollingMode = (1u << 11),
199209
};
200210

201211
BitFlags<Flags> mFlags; // Internal state flags
@@ -258,6 +268,11 @@ inline bool ReliableMessageContext::HasPiggybackAckPending() const
258268
return mFlags.Has(Flags::kFlagAckMessageCounterIsValid);
259269
}
260270

271+
inline bool ReliableMessageContext::IsRequestingFastPollingMode() const
272+
{
273+
return mFlags.Has(Flags::kFlagFastPollingMode);
274+
}
275+
261276
inline void ReliableMessageContext::SetAutoRequestAck(bool autoReqAck)
262277
{
263278
mFlags.Set(Flags::kFlagAutoRequestAck, autoReqAck);
@@ -283,5 +298,10 @@ inline void ReliableMessageContext::SetMessageNotAcked(bool messageNotAcked)
283298
mFlags.Set(Flags::kFlagMesageNotAcked, messageNotAcked);
284299
}
285300

301+
inline void ReliableMessageContext::SetRequestingFastPollingMode(bool fastPollingMode)
302+
{
303+
mFlags.Set(Flags::kFlagFastPollingMode, fastPollingMode);
304+
}
305+
286306
} // namespace Messaging
287307
} // namespace chip

0 commit comments

Comments
 (0)