Skip to content

Commit 83e47bf

Browse files
[ICD] Refactor ICDManager to use System::Clock types (project-chip#31881)
* Refactor time members * add using namespace * Fix ActiveModeThreshold type and cast
1 parent c58f062 commit 83e47bf

9 files changed

+100
-85
lines changed

src/app/ReadHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ using Status = Protocols::InteractionModel::Status;
4646
uint16_t ReadHandler::GetPublisherSelectedIntervalLimit()
4747
{
4848
#if CHIP_CONFIG_ENABLE_ICD_SERVER
49-
return static_cast<uint16_t>(ICDConfigurationData::GetInstance().GetIdleModeDurationSec());
49+
return std::chrono::duration_cast<System::Clock::Seconds16>(ICDConfigurationData::GetInstance().GetIdleModeDuration()).count();
5050
#else
5151
return kSubscriptionMaxIntervalPublisherLimit;
5252
#endif

src/app/SubscriptionsInfoProvider.h

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class SubscriptionsInfoProvider
4242
*
4343
* @param[in] aFabricIndex fabric index of the subject
4444
* @param[in] subjectID NodeId of the subject
45+
* subjectID may encode a CAT in the reserved section of the NodeID.
4546
*
4647
* @return true subject has at least one active subscription with the device
4748
* false subject doesn't have any active subscription with the device
@@ -54,6 +55,7 @@ class SubscriptionsInfoProvider
5455
*
5556
* @param[in] aFabricIndex fabric index of the subject
5657
* @param[in] subjectID NodeId of the subject
58+
* subjectID may encode a CAT in the reserved section of the NodeID
5759
*
5860
* @return true subject has at least one persisted subscription with the device
5961
* false subject doesn't have any persisted subscription with the device

src/app/clusters/icd-management-server/icd-management-server.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ CHIP_ERROR IcdManagementAttributeAccess::Read(const ConcreteReadAttributePath &
101101

102102
CHIP_ERROR IcdManagementAttributeAccess::ReadIdleModeDuration(EndpointId endpoint, AttributeValueEncoder & encoder)
103103
{
104-
return encoder.Encode(mICDConfigurationData->GetIdleModeDurationSec());
104+
return encoder.Encode(mICDConfigurationData->GetIdleModeDuration().count());
105105
}
106106

107107
CHIP_ERROR IcdManagementAttributeAccess::ReadActiveModeDuration(EndpointId endpoint, AttributeValueEncoder & encoder)
108108
{
109-
return encoder.Encode(mICDConfigurationData->GetActiveModeDurationMs());
109+
return encoder.Encode(mICDConfigurationData->GetActiveModeDuration().count());
110110
}
111111

112112
CHIP_ERROR IcdManagementAttributeAccess::ReadActiveModeThreshold(EndpointId endpoint, AttributeValueEncoder & encoder)
113113
{
114-
return encoder.Encode(mICDConfigurationData->GetActiveModeThresholdMs());
114+
return encoder.Encode(mICDConfigurationData->GetActiveModeThreshold().count());
115115
}
116116

117117
CHIP_ERROR IcdManagementAttributeAccess::ReadRegisteredClients(EndpointId endpoint, AttributeValueEncoder & encoder)

src/app/icd/server/ICDCheckInSender.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ CHIP_ERROR ICDCheckInSender::SendCheckInMsg(const Transport::PeerAddress & addr)
6464
size_t writtenBytes = 0;
6565
Encoding::LittleEndian::BufferWriter writer(activeModeThresholdBuffer, sizeof(activeModeThresholdBuffer));
6666

67-
writer.Put16(ICDConfigurationData::GetInstance().GetActiveModeThresholdMs());
67+
uint16_t activeModeThreshold_ms = ICDConfigurationData::GetInstance().GetActiveModeThreshold().count();
68+
writer.Put16(activeModeThreshold_ms);
6869
VerifyOrReturnError(writer.Fit(writtenBytes), CHIP_ERROR_INTERNAL);
6970

7071
ByteSpan activeModeThresholdByteSpan(writer.Buffer(), writtenBytes);

src/app/icd/server/ICDConfigurationData.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,24 @@ System::Clock::Milliseconds32 ICDConfigurationData::GetSlowPollingInterval()
3737
return mSlowPollingInterval;
3838
}
3939

40-
CHIP_ERROR ICDConfigurationData::SetModeDurations(Optional<uint32_t> activeModeDuration_ms, Optional<uint32_t> idleModeDuration_ms)
40+
CHIP_ERROR ICDConfigurationData::SetModeDurations(Optional<System::Clock::Milliseconds32> activeModeDuration,
41+
Optional<System::Clock::Milliseconds32> idleModeDuration)
4142
{
42-
VerifyOrReturnError(activeModeDuration_ms.HasValue() || idleModeDuration_ms.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);
43+
VerifyOrReturnError(activeModeDuration.HasValue() || idleModeDuration.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);
4344

44-
uint32_t tmpIdleModeDuration_s =
45-
idleModeDuration_ms.HasValue() ? (idleModeDuration_ms.Value() / kMillisecondsPerSecond) : mIdleModeDuration_s;
46-
uint32_t tmpActiveModeDuration_ms = activeModeDuration_ms.HasValue() ? activeModeDuration_ms.Value() : mActiveModeDuration_ms;
45+
// Convert idleModeDuration to seconds for the correct precision
46+
System::Clock::Seconds32 tmpIdleModeDuration = idleModeDuration.HasValue()
47+
? std::chrono::duration_cast<System::Clock::Seconds32>(idleModeDuration.Value())
48+
: mIdleModeDuration;
4749

48-
VerifyOrReturnError(tmpActiveModeDuration_ms <= (tmpIdleModeDuration_s * kMillisecondsPerSecond), CHIP_ERROR_INVALID_ARGUMENT);
49-
VerifyOrReturnError(tmpIdleModeDuration_s <= kMaxIdleModeDuration_s, CHIP_ERROR_INVALID_ARGUMENT);
50-
VerifyOrReturnError(tmpIdleModeDuration_s >= kMinIdleModeDuration_s, CHIP_ERROR_INVALID_ARGUMENT);
50+
System::Clock::Milliseconds32 tmpActiveModeDuration = activeModeDuration.ValueOr(mActiveModeDuration);
5151

52-
mIdleModeDuration_s = tmpIdleModeDuration_s;
53-
mActiveModeDuration_ms = tmpActiveModeDuration_ms;
52+
VerifyOrReturnError(tmpActiveModeDuration <= tmpIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
53+
VerifyOrReturnError(tmpIdleModeDuration <= kMaxIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
54+
VerifyOrReturnError(tmpIdleModeDuration >= kMinIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
55+
56+
mIdleModeDuration = tmpIdleModeDuration;
57+
mActiveModeDuration = tmpActiveModeDuration;
5458

5559
return CHIP_NO_ERROR;
5660
}

src/app/icd/server/ICDConfigurationData.h

+18-16
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class ICDConfigurationData
5454

5555
static ICDConfigurationData & GetInstance() { return instance; };
5656

57-
uint32_t GetIdleModeDurationSec() { return mIdleModeDuration_s; }
57+
System::Clock::Seconds32 GetIdleModeDuration() { return mIdleModeDuration; }
5858

59-
uint32_t GetActiveModeDurationMs() { return mActiveModeDuration_ms; }
59+
System::Clock::Milliseconds32 GetActiveModeDuration() { return mActiveModeDuration; }
6060

61-
uint16_t GetActiveModeThresholdMs() { return mActiveThreshold_ms; }
61+
System::Clock::Milliseconds16 GetActiveModeThreshold() { return mActiveThreshold; }
6262

6363
uint32_t GetICDCounter() { return mICDCounter; }
6464

@@ -68,7 +68,7 @@ class ICDConfigurationData
6868

6969
System::Clock::Milliseconds32 GetFastPollingInterval() { return mFastPollingInterval; }
7070

71-
uint32_t GetMinLitActiveModeThresholdMs() { return kMinLitActiveModeThreshold_ms; }
71+
System::Clock::Milliseconds16 GetMinLitActiveModeThreshold() { return kMinLitActiveModeThreshold; }
7272

7373
/**
7474
* If ICD_ENFORCE_SIT_SLOW_POLL_LIMIT is set to 0, function will always return the configured Slow Polling interval
@@ -103,37 +103,39 @@ class ICDConfigurationData
103103
void SetSlowPollingInterval(System::Clock::Milliseconds32 slowPollInterval) { mSlowPollingInterval = slowPollInterval; };
104104
void SetFastPollingInterval(System::Clock::Milliseconds32 fastPollInterval) { mFastPollingInterval = fastPollInterval; };
105105

106-
static constexpr uint32_t kMinLitActiveModeThreshold_ms = 5000;
106+
static constexpr System::Clock::Milliseconds16 kMinLitActiveModeThreshold = System::Clock::Milliseconds16(5000);
107107

108108
/**
109109
* @brief Change the ActiveModeDuration or the IdleModeDuration value
110110
* If only one value is provided, check will be done agaisn't the other already set value.
111111
*
112-
* @param[in] activeModeDuration_ms new ActiveModeDuration value
113-
* @param[in] idleModeDuration_ms new IdleModeDuration value
112+
* @param[in] activeModeDuration new ActiveModeDuration value
113+
* @param[in] idleModeDuration new IdleModeDuration value
114114
* The precision of the IdleModeDuration must be seconds.
115115
* @return CHIP_ERROR CHIP_ERROR_INVALID_ARGUMENT is returned if idleModeDuration_ms is smaller than activeModeDuration_ms
116116
* is returned if idleModeDuration_ms is greater than 64800000 ms
117117
* is returned if idleModeDuration_ms is smaller than 1000 ms
118118
* is returned if no valid values are provided
119119
* CHIP_NO_ERROR is returned if the new intervals were set
120120
*/
121-
CHIP_ERROR SetModeDurations(Optional<uint32_t> activeModeDuration_ms, Optional<uint32_t> idleModeDuration_ms);
121+
CHIP_ERROR SetModeDurations(Optional<System::Clock::Milliseconds32> activeModeDuration,
122+
Optional<System::Clock::Milliseconds32> idleModeDuration);
122123

123-
static constexpr uint32_t kMaxIdleModeDuration_s = (18 * kSecondsPerHour);
124-
static constexpr uint32_t kMinIdleModeDuration_s = 1;
124+
static constexpr System::Clock::Seconds32 kMaxIdleModeDuration = System::Clock::Seconds32(18 * kSecondsPerHour);
125+
static constexpr System::Clock::Seconds32 kMinIdleModeDuration = System::Clock::Seconds32(1);
125126

126-
static_assert((CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC) <= kMaxIdleModeDuration_s,
127+
static_assert((CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC) <= kMaxIdleModeDuration.count(),
127128
"Spec requires the IdleModeDuration to be equal or inferior to 64800s.");
128-
static_assert((CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC) >= kMinIdleModeDuration_s,
129+
static_assert((CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC) >= kMinIdleModeDuration.count(),
129130
"Spec requires the IdleModeDuration to be equal or greater to 1s.");
130-
uint32_t mIdleModeDuration_s = CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC;
131+
System::Clock::Seconds32 mIdleModeDuration = System::Clock::Seconds32(CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC);
131132

132-
static_assert((CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS) <= (CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC * kMillisecondsPerSecond),
133+
static_assert(System::Clock::Milliseconds32(CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS) <=
134+
System::Clock::Seconds32(CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC),
133135
"Spec requires the IdleModeDuration be equal or greater to the ActiveModeDuration.");
134-
uint32_t mActiveModeDuration_ms = CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS;
136+
System::Clock::Milliseconds32 mActiveModeDuration = System::Clock::Milliseconds32(CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS);
135137

136-
uint16_t mActiveThreshold_ms = CHIP_CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS;
138+
System::Clock::Milliseconds16 mActiveThreshold = System::Clock::Milliseconds16(CHIP_CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS);
137139

138140
uint32_t mICDCounter = 0;
139141

src/app/icd/server/ICDManager.cpp

+24-20
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace app {
3535
using namespace chip::app;
3636
using namespace chip::app::Clusters;
3737
using namespace chip::app::Clusters::IcdManagement;
38+
using namespace System::Clock;
3839

3940
static_assert(UINT8_MAX >= CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS,
4041
"ICDManager::mOpenExchangeContextCount cannot hold count for the max exchange count");
@@ -55,8 +56,8 @@ void ICDManager::Init(PersistentStorageDelegate * storage, FabricTable * fabricT
5556
"The CheckIn protocol feature is required for LIT support.");
5657
VerifyOrDieWithMsg(SupportsFeature(Feature::kUserActiveModeTrigger), AppServer,
5758
"The user ActiveMode trigger feature is required for LIT support.");
58-
VerifyOrDieWithMsg(ICDConfigurationData::GetInstance().GetMinLitActiveModeThresholdMs() <=
59-
ICDConfigurationData::GetInstance().GetActiveModeThresholdMs(),
59+
VerifyOrDieWithMsg(ICDConfigurationData::GetInstance().GetMinLitActiveModeThreshold() <=
60+
ICDConfigurationData::GetInstance().GetActiveModeThreshold(),
6061
AppServer, "The minimum ActiveModeThreshold value for a LIT ICD is 5 seconds.");
6162
// Disabling check until LIT support is compelte
6263
// VerifyOrDieWithMsg((GetSlowPollingInterval() <= GetSITPollingThreshold()) , AppServer,
@@ -280,13 +281,12 @@ void ICDManager::UpdateOperationState(OperationalState state)
280281
// When the active mode interval is 0, we stay in idleMode until a notification brings the icd into active mode
281282
// unless the device would need to send Check-In messages
282283
// TODO(#30281) : Verify how persistent subscriptions affects this at ICDManager::Init
283-
if (ICDConfigurationData::GetInstance().GetActiveModeDurationMs() > 0 || CheckInMessagesWouldBeSent())
284+
if (ICDConfigurationData::GetInstance().GetActiveModeDuration() > kZero || CheckInMessagesWouldBeSent())
284285
{
285-
uint32_t idleModeDuration = ICDConfigurationData::GetInstance().GetIdleModeDurationSec();
286-
DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(idleModeDuration), OnIdleModeDone, this);
286+
DeviceLayer::SystemLayer().StartTimer(ICDConfigurationData::GetInstance().GetIdleModeDuration(), OnIdleModeDone, this);
287287
}
288288

289-
System::Clock::Milliseconds32 slowPollInterval = ICDConfigurationData::GetInstance().GetSlowPollingInterval();
289+
Milliseconds32 slowPollInterval = ICDConfigurationData::GetInstance().GetSlowPollingInterval();
290290

291291
// Going back to Idle, all Check-In messages are sent
292292
mICDSenderPool.ReleaseAll();
@@ -305,21 +305,23 @@ void ICDManager::UpdateOperationState(OperationalState state)
305305
// Make sure the idle mode timer is stopped
306306
DeviceLayer::SystemLayer().CancelTimer(OnIdleModeDone, this);
307307

308-
mOperationalState = OperationalState::ActiveMode;
309-
uint32_t activeModeDuration = ICDConfigurationData::GetInstance().GetActiveModeDurationMs();
308+
mOperationalState = OperationalState::ActiveMode;
309+
Milliseconds32 activeModeDuration = ICDConfigurationData::GetInstance().GetActiveModeDuration();
310310

311-
if (activeModeDuration == 0 && !mKeepActiveFlags.HasAny())
311+
if (activeModeDuration == kZero && !mKeepActiveFlags.HasAny())
312312
{
313313
// A Network Activity triggered the active mode and activeModeDuration is 0.
314314
// Stay active for at least Active Mode Threshold.
315-
activeModeDuration = ICDConfigurationData::GetInstance().GetActiveModeThresholdMs();
315+
activeModeDuration = ICDConfigurationData::GetInstance().GetActiveModeThreshold();
316316
}
317317

318-
DeviceLayer::SystemLayer().StartTimer(System::Clock::Timeout(activeModeDuration), OnActiveModeDone, this);
318+
DeviceLayer::SystemLayer().StartTimer(activeModeDuration, OnActiveModeDone, this);
319319

320-
uint32_t activeModeJitterInterval =
321-
(activeModeDuration >= ICD_ACTIVE_TIME_JITTER_MS) ? activeModeDuration - ICD_ACTIVE_TIME_JITTER_MS : 0;
322-
DeviceLayer::SystemLayer().StartTimer(System::Clock::Timeout(activeModeJitterInterval), OnTransitionToIdle, this);
320+
Milliseconds32 activeModeJitterInterval = Milliseconds32(ICD_ACTIVE_TIME_JITTER_MS);
321+
activeModeJitterInterval =
322+
(activeModeDuration >= activeModeJitterInterval) ? activeModeDuration - activeModeJitterInterval : kZero;
323+
324+
DeviceLayer::SystemLayer().StartTimer(activeModeJitterInterval, OnTransitionToIdle, this);
323325

324326
CHIP_ERROR err =
325327
DeviceLayer::ConnectivityMgr().SetPollingInterval(ICDConfigurationData::GetInstance().GetFastPollingInterval());
@@ -337,14 +339,16 @@ void ICDManager::UpdateOperationState(OperationalState state)
337339
}
338340
else
339341
{
340-
uint16_t activeModeThreshold = ICDConfigurationData::GetInstance().GetActiveModeThresholdMs();
341-
DeviceLayer::SystemLayer().ExtendTimerTo(System::Clock::Timeout(activeModeThreshold), OnActiveModeDone, this);
342-
uint16_t activeModeJitterThreshold =
343-
(activeModeThreshold >= ICD_ACTIVE_TIME_JITTER_MS) ? activeModeThreshold - ICD_ACTIVE_TIME_JITTER_MS : 0;
342+
Milliseconds16 activeModeThreshold = ICDConfigurationData::GetInstance().GetActiveModeThreshold();
343+
DeviceLayer::SystemLayer().ExtendTimerTo(activeModeThreshold, OnActiveModeDone, this);
344+
345+
Milliseconds32 activeModeJitterThreshold = Milliseconds32(ICD_ACTIVE_TIME_JITTER_MS);
346+
activeModeJitterThreshold =
347+
(activeModeThreshold >= activeModeJitterThreshold) ? activeModeThreshold - activeModeJitterThreshold : kZero;
348+
344349
if (!mTransitionToIdleCalled)
345350
{
346-
DeviceLayer::SystemLayer().ExtendTimerTo(System::Clock::Timeout(activeModeJitterThreshold), OnTransitionToIdle,
347-
this);
351+
DeviceLayer::SystemLayer().ExtendTimerTo(activeModeJitterThreshold, OnTransitionToIdle, this);
348352
}
349353
}
350354
}

0 commit comments

Comments
 (0)