Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2023 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include "ICDConfigurationData.h"
19 : #include <lib/support/CodeUtils.h>
20 :
21 : namespace chip {
22 :
23 : ICDConfigurationData ICDConfigurationData::instance;
24 :
25 40 : System::Clock::Milliseconds32 ICDConfigurationData::GetSlowPollingInterval()
26 : {
27 : // When LIT capable device operates in SIT mode, it shall transition to use the mSITPollingInterval
28 : // if this one is shorter than the configured mLITPollingInterval.
29 : // Either way, the slow poll interval used SHALL NOT be greater than the SIT mode polling threshold, per spec.
30 : // This is important for ICD device configured for LIT operation but currently operating as a SIT
31 : // due to a lack of client registration
32 40 : if (mFeatureMap.Has(app::Clusters::IcdManagement::Feature::kLongIdleTimeSupport) && mICDMode == ICDMode::SIT)
33 : {
34 : // mSITPollingInterval cannot be configured to a value greater than kSITPollingThreshold.
35 : // The SIT slow polling interval compliance is therefore always respected by using the smallest
36 : // value from mLITPollingInterval or mSITPollingInterval;
37 3 : return std::min(mLITPollingInterval, mSITPollingInterval);
38 : }
39 :
40 37 : return mLITPollingInterval;
41 : }
42 :
43 6 : CHIP_ERROR ICDConfigurationData::SetSlowPollingInterval(System::Clock::Milliseconds32 slowPollInterval)
44 : {
45 6 : bool isLITSupported = mFeatureMap.Has(app::Clusters::IcdManagement::Feature::kLongIdleTimeSupport);
46 : // If LIT is not supported, the slow polling interval cannot be set higher than kSITPollingThreshold.
47 6 : VerifyOrReturnError((isLITSupported || slowPollInterval <= kSITPollingThreshold), CHIP_ERROR_INVALID_ARGUMENT);
48 :
49 5 : mLITPollingInterval = slowPollInterval;
50 5 : return CHIP_NO_ERROR;
51 : };
52 :
53 3 : CHIP_ERROR ICDConfigurationData::SetSITPollingInterval(System::Clock::Milliseconds32 pollingInterval)
54 : {
55 3 : VerifyOrReturnError(pollingInterval <= kSITPollingThreshold, CHIP_ERROR_INVALID_ARGUMENT);
56 2 : mSITPollingInterval = pollingInterval;
57 2 : return CHIP_NO_ERROR;
58 : }
59 :
60 9 : CHIP_ERROR ICDConfigurationData::SetModeDurations(Optional<System::Clock::Milliseconds32> activeModeDuration,
61 : Optional<System::Clock::Milliseconds32> idleModeDuration)
62 : {
63 9 : VerifyOrReturnError(activeModeDuration.HasValue() || idleModeDuration.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);
64 :
65 : // Convert idleModeDuration to seconds for the correct precision
66 9 : std::optional<System::Clock::Seconds32> tmpIdleModeDuration = std::nullopt;
67 9 : if (idleModeDuration.HasValue())
68 3 : tmpIdleModeDuration = std::chrono::duration_cast<System::Clock::Seconds32>(idleModeDuration.Value());
69 :
70 : // Here we set shortIdleModeDuration the same as idleModeDuration to maintain the api previous behaviour
71 9 : return SetModeDurations(activeModeDuration.std_optional(), tmpIdleModeDuration, tmpIdleModeDuration);
72 : }
73 :
74 15 : CHIP_ERROR ICDConfigurationData::SetModeDurations(std::optional<System::Clock::Milliseconds32> activeModeDuration,
75 : std::optional<System::Clock::Seconds32> idleModeDuration,
76 : std::optional<System::Clock::Seconds32> shortIdleModeDuration)
77 : {
78 15 : VerifyOrReturnError(activeModeDuration.has_value() || idleModeDuration.has_value() || shortIdleModeDuration.has_value(),
79 : CHIP_ERROR_INVALID_ARGUMENT);
80 :
81 14 : System::Clock::Milliseconds32 tmpActiveModeDuration = activeModeDuration.value_or(mActiveModeDuration);
82 14 : System::Clock::Seconds32 tmpIdleModeDuration = idleModeDuration.value_or(mIdleModeDuration);
83 :
84 14 : VerifyOrReturnError(tmpActiveModeDuration <= tmpIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
85 12 : VerifyOrReturnError(tmpIdleModeDuration <= kMaxIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
86 12 : VerifyOrReturnError(tmpIdleModeDuration >= kMinIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
87 :
88 : System::Clock::Seconds32 tmpShortIdleModeDuration;
89 12 : if (shortIdleModeDuration.has_value())
90 : {
91 : // shortIdleModeDuration was provided, it shall be lesser than or equal to idleModeDuration.
92 5 : tmpShortIdleModeDuration = shortIdleModeDuration.value();
93 : }
94 : else
95 : {
96 : // shortIdleModeDuration was not provided. To ensure correct mode transitions and device compliance,
97 : // shortIdleModeDuration must not exceed idleModeDuration, so we use the smaller of the current shortIdleModeDuration
98 : // and the resultant idleModeDuration.
99 : // This approach overwrites a previous valid shortIdleModeDuration rather than erroring the call but maintains the previous
100 : // api behavior.
101 7 : tmpShortIdleModeDuration = std::min(mShortIdleModeDuration, tmpIdleModeDuration);
102 : }
103 :
104 12 : VerifyOrReturnError(tmpShortIdleModeDuration <= tmpIdleModeDuration, CHIP_ERROR_INVALID_ARGUMENT);
105 :
106 11 : mIdleModeDuration = tmpIdleModeDuration;
107 11 : mActiveModeDuration = tmpActiveModeDuration;
108 11 : mShortIdleModeDuration = tmpShortIdleModeDuration;
109 :
110 11 : return CHIP_NO_ERROR;
111 : }
112 :
113 32 : bool ICDConfigurationData::ShouldUseShortIdle()
114 : {
115 32 : VerifyOrReturnValue(mShortIdleModeDuration < mIdleModeDuration, false);
116 0 : return (mFeatureMap.Has(app::Clusters::IcdManagement::Feature::kLongIdleTimeSupport) && mICDMode == ICDMode::SIT);
117 : }
118 :
119 31 : System::Clock::Seconds32 ICDConfigurationData::GetModeBasedIdleModeDuration()
120 : {
121 31 : return ShouldUseShortIdle() ? mShortIdleModeDuration : mIdleModeDuration;
122 : }
123 :
124 : } // namespace chip
|