Skip to content

Commit

Permalink
Refactor time members
Browse files Browse the repository at this point in the history
  • Loading branch information
mkardous-silabs committed Feb 2, 2024
1 parent 725decf commit 2a74320
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/app/ReadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using Status = Protocols::InteractionModel::Status;
uint16_t ReadHandler::GetPublisherSelectedIntervalLimit()
{
#if CHIP_CONFIG_ENABLE_ICD_SERVER
return static_cast<uint16_t>(ICDConfigurationData::GetInstance().GetIdleModeDurationSec());
return std::chrono::duration_cast<System::Clock::Seconds16>(ICDConfigurationData::GetInstance().GetIdleModeDuration()).count();
#else
return kSubscriptionMaxIntervalPublisherLimit;
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/app/SubscriptionsInfoProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SubscriptionsInfoProvider
*
* @param[in] aFabricIndex fabric index of the subject
* @param[in] subjectID NodeId of the subject
* subjectID may encode a CAT in the reserved section of the NodeID.
*
* @return true subject has at least one active subscription with the device
* false subject doesn't have any active subscription with the device
Expand All @@ -54,6 +55,7 @@ class SubscriptionsInfoProvider
*
* @param[in] aFabricIndex fabric index of the subject
* @param[in] subjectID NodeId of the subject
* subjectID may encode a CAT in the reserved section of the NodeID
*
* @return true subject has at least one persisted subscription with the device
* false subject doesn't have any persisted subscription with the device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ CHIP_ERROR IcdManagementAttributeAccess::Read(const ConcreteReadAttributePath &

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

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

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

CHIP_ERROR IcdManagementAttributeAccess::ReadRegisteredClients(EndpointId endpoint, AttributeValueEncoder & encoder)
Expand Down
5 changes: 4 additions & 1 deletion src/app/icd/server/ICDCheckInSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ CHIP_ERROR ICDCheckInSender::SendCheckInMsg(const Transport::PeerAddress & addr)
size_t writtenBytes = 0;
Encoding::LittleEndian::BufferWriter writer(activeModeThresholdBuffer, sizeof(activeModeThresholdBuffer));

writer.Put16(ICDConfigurationData::GetInstance().GetActiveModeThresholdMs());
uint16_t activeModeThreshold_ms =
std::chrono::duration_cast<System::Clock::Seconds16>(ICDConfigurationData::GetInstance().GetActiveModeThreshold())
.count();
writer.Put16(activeModeThreshold_ms);
VerifyOrReturnError(writer.Fit(writtenBytes), CHIP_ERROR_INTERNAL);

ByteSpan activeModeThresholdByteSpan(writer.Buffer(), writtenBytes);
Expand Down
24 changes: 14 additions & 10 deletions src/app/icd/server/ICDConfigurationData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@ System::Clock::Milliseconds32 ICDConfigurationData::GetSlowPollingInterval()
return mSlowPollingInterval;
}

CHIP_ERROR ICDConfigurationData::SetModeDurations(Optional<uint32_t> activeModeDuration_ms, Optional<uint32_t> idleModeDuration_ms)
CHIP_ERROR ICDConfigurationData::SetModeDurations(Optional<System::Clock::Milliseconds32> activeModeDuration,
Optional<System::Clock::Milliseconds32> idleModeDuration)
{
VerifyOrReturnError(activeModeDuration_ms.HasValue() || idleModeDuration_ms.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(activeModeDuration.HasValue() || idleModeDuration.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);

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

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

mIdleModeDuration_s = tmpIdleModeDuration_s;
mActiveModeDuration_ms = tmpActiveModeDuration_ms;
VerifyOrReturnError(tmpActiveModeDuration <= tmpIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(tmpIdleModeDuration <= kMaxIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(tmpIdleModeDuration >= kMinIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);

mIdleModeDuration = tmpIdleModeDuration;
mActiveModeDuration = tmpActiveModeDuration;

return CHIP_NO_ERROR;
}
Expand Down
34 changes: 18 additions & 16 deletions src/app/icd/server/ICDConfigurationData.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class ICDConfigurationData

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

uint32_t GetIdleModeDurationSec() { return mIdleModeDuration_s; }
System::Clock::Seconds32 GetIdleModeDuration() { return mIdleModeDuration; }

uint32_t GetActiveModeDurationMs() { return mActiveModeDuration_ms; }
System::Clock::Milliseconds32 GetActiveModeDuration() { return mActiveModeDuration; }

uint16_t GetActiveModeThresholdMs() { return mActiveThreshold_ms; }
System::Clock::Milliseconds32 GetActiveModeThreshold() { return mActiveThreshold; }

uint32_t GetICDCounter() { return mICDCounter; }

Expand All @@ -68,7 +68,7 @@ class ICDConfigurationData

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

uint32_t GetMinLitActiveModeThresholdMs() { return kMinLitActiveModeThreshold_ms; }
System::Clock::Milliseconds32 GetMinLitActiveModeThreshold() { return kMinLitActiveModeThreshold; }

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

static constexpr uint32_t kMinLitActiveModeThreshold_ms = 5000;
static constexpr System::Clock::Milliseconds32 kMinLitActiveModeThreshold = System::Clock::Milliseconds32(5000);

/**
* @brief Change the ActiveModeDuration or the IdleModeDuration value
* If only one value is provided, check will be done agaisn't the other already set value.
*
* @param[in] activeModeDuration_ms new ActiveModeDuration value
* @param[in] idleModeDuration_ms new IdleModeDuration value
* @param[in] activeModeDuration new ActiveModeDuration value
* @param[in] idleModeDuration new IdleModeDuration value
* The precision of the IdleModeDuration must be seconds.
* @return CHIP_ERROR CHIP_ERROR_INVALID_ARGUMENT is returned if idleModeDuration_ms is smaller than activeModeDuration_ms
* is returned if idleModeDuration_ms is greater than 64800000 ms
* is returned if idleModeDuration_ms is smaller than 1000 ms
* is returned if no valid values are provided
* CHIP_NO_ERROR is returned if the new intervals were set
*/
CHIP_ERROR SetModeDurations(Optional<uint32_t> activeModeDuration_ms, Optional<uint32_t> idleModeDuration_ms);
CHIP_ERROR SetModeDurations(Optional<System::Clock::Milliseconds32> activeModeDuration,
Optional<System::Clock::Milliseconds32> idleModeDuration);

static constexpr uint32_t kMaxIdleModeDuration_s = (18 * kSecondsPerHour);
static constexpr uint32_t kMinIdleModeDuration_s = 1;
static constexpr System::Clock::Seconds32 kMaxIdleModeDuration = System::Clock::Seconds32(18 * kSecondsPerHour);
static constexpr System::Clock::Seconds32 kMinIdleModeDuration = System::Clock::Seconds32(1);

static_assert((CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC) <= kMaxIdleModeDuration_s,
static_assert((CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC) <= kMaxIdleModeDuration.count(),
"Spec requires the IdleModeDuration to be equal or inferior to 64800s.");
static_assert((CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC) >= kMinIdleModeDuration_s,
static_assert((CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC) >= kMinIdleModeDuration.count(),
"Spec requires the IdleModeDuration to be equal or greater to 1s.");
uint32_t mIdleModeDuration_s = CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC;
System::Clock::Seconds32 mIdleModeDuration = System::Clock::Seconds32(CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC);

static_assert((CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS) <= (CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC * kMillisecondsPerSecond),
static_assert(System::Clock::Milliseconds32(CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS) <=
System::Clock::Seconds32(CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC),
"Spec requires the IdleModeDuration be equal or greater to the ActiveModeDuration.");
uint32_t mActiveModeDuration_ms = CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS;
System::Clock::Milliseconds32 mActiveModeDuration = System::Clock::Milliseconds32(CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS);

uint16_t mActiveThreshold_ms = CHIP_CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS;
System::Clock::Milliseconds32 mActiveThreshold = System::Clock::Milliseconds32(CHIP_CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS);

uint32_t mICDCounter = 0;

Expand Down
43 changes: 24 additions & 19 deletions src/app/icd/server/ICDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void ICDManager::Init(PersistentStorageDelegate * storage, FabricTable * fabricT
"The CheckIn protocol feature is required for LIT support.");
VerifyOrDieWithMsg(SupportsFeature(Feature::kUserActiveModeTrigger), AppServer,
"The user ActiveMode trigger feature is required for LIT support.");
VerifyOrDieWithMsg(ICDConfigurationData::GetInstance().GetMinLitActiveModeThresholdMs() <=
ICDConfigurationData::GetInstance().GetActiveModeThresholdMs(),
VerifyOrDieWithMsg(ICDConfigurationData::GetInstance().GetMinLitActiveModeThreshold() <=
ICDConfigurationData::GetInstance().GetActiveModeThreshold(),
AppServer, "The minimum ActiveModeThreshold value for a LIT ICD is 5 seconds.");
// Disabling check until LIT support is compelte
// VerifyOrDieWithMsg((GetSlowPollingInterval() <= GetSITPollingThreshold()) , AppServer,
Expand Down Expand Up @@ -280,10 +280,9 @@ void ICDManager::UpdateOperationState(OperationalState state)
// When the active mode interval is 0, we stay in idleMode until a notification brings the icd into active mode
// unless the device would need to send Check-In messages
// TODO(#30281) : Verify how persistent subscriptions affects this at ICDManager::Init
if (ICDConfigurationData::GetInstance().GetActiveModeDurationMs() > 0 || CheckInMessagesWouldBeSent())
if (ICDConfigurationData::GetInstance().GetActiveModeDuration() > System::Clock::kZero || CheckInMessagesWouldBeSent())
{
uint32_t idleModeDuration = ICDConfigurationData::GetInstance().GetIdleModeDurationSec();
DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(idleModeDuration), OnIdleModeDone, this);
DeviceLayer::SystemLayer().StartTimer(ICDConfigurationData::GetInstance().GetIdleModeDuration(), OnIdleModeDone, this);
}

System::Clock::Milliseconds32 slowPollInterval = ICDConfigurationData::GetInstance().GetSlowPollingInterval();
Expand All @@ -305,21 +304,24 @@ void ICDManager::UpdateOperationState(OperationalState state)
// Make sure the idle mode timer is stopped
DeviceLayer::SystemLayer().CancelTimer(OnIdleModeDone, this);

mOperationalState = OperationalState::ActiveMode;
uint32_t activeModeDuration = ICDConfigurationData::GetInstance().GetActiveModeDurationMs();
mOperationalState = OperationalState::ActiveMode;
System::Clock::Milliseconds32 activeModeDuration = ICDConfigurationData::GetInstance().GetActiveModeDuration();

if (activeModeDuration == 0 && !mKeepActiveFlags.HasAny())
if (activeModeDuration == System::Clock::kZero && !mKeepActiveFlags.HasAny())
{
// A Network Activity triggered the active mode and activeModeDuration is 0.
// Stay active for at least Active Mode Threshold.
activeModeDuration = ICDConfigurationData::GetInstance().GetActiveModeThresholdMs();
activeModeDuration = ICDConfigurationData::GetInstance().GetActiveModeThreshold();
}

DeviceLayer::SystemLayer().StartTimer(System::Clock::Timeout(activeModeDuration), OnActiveModeDone, this);
DeviceLayer::SystemLayer().StartTimer(activeModeDuration, OnActiveModeDone, this);

uint32_t activeModeJitterInterval =
(activeModeDuration >= ICD_ACTIVE_TIME_JITTER_MS) ? activeModeDuration - ICD_ACTIVE_TIME_JITTER_MS : 0;
DeviceLayer::SystemLayer().StartTimer(System::Clock::Timeout(activeModeJitterInterval), OnTransitionToIdle, this);
System::Clock::Milliseconds32 activeModeJitterInterval = System::Clock::Milliseconds32(ICD_ACTIVE_TIME_JITTER_MS);
activeModeJitterInterval = (activeModeDuration >= activeModeJitterInterval)
? activeModeDuration - activeModeJitterInterval
: System::Clock::kZero;

DeviceLayer::SystemLayer().StartTimer(activeModeJitterInterval, OnTransitionToIdle, this);

CHIP_ERROR err =
DeviceLayer::ConnectivityMgr().SetPollingInterval(ICDConfigurationData::GetInstance().GetFastPollingInterval());
Expand All @@ -337,14 +339,17 @@ void ICDManager::UpdateOperationState(OperationalState state)
}
else
{
uint16_t activeModeThreshold = ICDConfigurationData::GetInstance().GetActiveModeThresholdMs();
DeviceLayer::SystemLayer().ExtendTimerTo(System::Clock::Timeout(activeModeThreshold), OnActiveModeDone, this);
uint16_t activeModeJitterThreshold =
(activeModeThreshold >= ICD_ACTIVE_TIME_JITTER_MS) ? activeModeThreshold - ICD_ACTIVE_TIME_JITTER_MS : 0;
System::Clock::Milliseconds32 activeModeThreshold = ICDConfigurationData::GetInstance().GetActiveModeThreshold();
DeviceLayer::SystemLayer().ExtendTimerTo(activeModeThreshold, OnActiveModeDone, this);

System::Clock::Milliseconds32 activeModeJitterThreshold = System::Clock::Milliseconds32(ICD_ACTIVE_TIME_JITTER_MS);
activeModeJitterThreshold = (activeModeThreshold >= activeModeJitterThreshold)
? activeModeThreshold - activeModeJitterThreshold
: System::Clock::kZero;

if (!mTransitionToIdleCalled)
{
DeviceLayer::SystemLayer().ExtendTimerTo(System::Clock::Timeout(activeModeJitterThreshold), OnTransitionToIdle,
this);
DeviceLayer::SystemLayer().ExtendTimerTo(activeModeJitterThreshold, OnTransitionToIdle, this);
}
}
}
Expand Down
Loading

0 comments on commit 2a74320

Please sign in to comment.