Skip to content

Commit 71fb16c

Browse files
committed
open command start
1 parent 02906e1 commit 71fb16c

File tree

3 files changed

+177
-8
lines changed

3 files changed

+177
-8
lines changed

src/app/clusters/valve-configuration-and-control-server/valve-configuration-and-control-cluster-logic.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,15 @@ CHIP_ERROR ClusterLogic::HandleOpenCommand(std::optional<DataModel::Nullable<Ela
454454
return CHIP_NO_ERROR;
455455
}
456456

457-
void ClusterLogic::HandleCloseInternal()
457+
CHIP_ERROR ClusterLogic::HandleCloseCommand()
458+
{
459+
VerifyOrReturnError(mInitialized, CHIP_ERROR_INCORRECT_STATE);
460+
DeviceLayer::SystemLayer().CancelTimer(HandleUpdateRemainingDuration, this);
461+
return HandleCloseInternal();
462+
}
463+
464+
CHIP_ERROR ClusterLogic::HandleCloseInternal()
458465
{
459-
// TODO: call the delegate and add to tests
460466
CHIP_ERROR err;
461467
BitMask<ValveFaultBitmap> faults;
462468
if (mConformance.HasFeature(Feature::kLevel))
@@ -497,6 +503,7 @@ void ClusterLogic::HandleCloseInternal()
497503
mState.SetTargetLevel(DataModel::NullNullable);
498504
mState.SetTargetState(DataModel::NullNullable);
499505
mState.SetAutoCloseTime(DataModel::NullNullable);
506+
return err;
500507
}
501508

502509
void ClusterLogic::HandleUpdateRemainingDuration(System::Layer * systemLayer, void * context)

src/app/clusters/valve-configuration-and-control-server/valve-configuration-and-control-cluster-logic.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ class ClusterLogic
176176
// Calls delegate HandleOpen function after validating the parameters
177177
CHIP_ERROR HandleOpenCommand(std::optional<DataModel::Nullable<ElapsedS>> openDuration, std::optional<Percent> targetLevel);
178178

179+
// Return CHIP_ERROR_INCORRECT_STATE if the class has not been initialized.
180+
// Calls delegate HandleClose function after validating the parameters and stops any open duration timers.
181+
CHIP_ERROR HandleCloseCommand();
182+
179183
private:
180184
// Determines if the level value is allowed per the level step.
181185
bool ValueCompliesWithLevelStep(const uint8_t value);
@@ -193,7 +197,7 @@ class ClusterLogic
193197
void HandleUpdateRemainingDurationInternal();
194198
// Internal function called by HandleUpdateRemainingDuration to call the close function in the delegate and
195199
// set all the attributes back to their closed state.
196-
void HandleCloseInternal();
200+
CHIP_ERROR HandleCloseInternal();
197201

198202
bool mInitialized = false;
199203
System::Clock::Milliseconds64 mDurationStarted = System::Clock::Milliseconds64(0);

src/app/tests/TestValveConfigurationAndControl.cpp

+163-5
Original file line numberDiff line numberDiff line change
@@ -1947,9 +1947,168 @@ TEST_F(TestValveConfigurationAndControlClusterLogic, TestCloseCalledAtOpenDurati
19471947
//=========================================================================================
19481948
// Tests for handling close commands
19491949
//=========================================================================================
1950-
// while remaining duration is null and valve is open
19511950
// while remaining duration is not null and valve is open
1951+
1952+
TEST_F(TestValveConfigurationAndControlClusterLogic, TestCloseCommandOpenValveDurationLevel)
1953+
{
1954+
TestDelegateLevel delegate;
1955+
TestPersistentStorageDelegate storageDelegate;
1956+
EndpointId endpoint = 0;
1957+
MockedMatterContext context(endpoint, storageDelegate);
1958+
ClusterLogic logic(delegate, context);
1959+
1960+
ClusterConformance conformance = { .featureMap = to_underlying(Feature::kLevel) | to_underlying(Feature::kTimeSync),
1961+
.supportsDefaultOpenLevel = true,
1962+
.supportsValveFault = true,
1963+
.supportsLevelStep = true };
1964+
EXPECT_EQ(logic.Init(conformance), CHIP_NO_ERROR);
1965+
1966+
gSystemLayerAndClock.SetMonotonic(0_ms64);
1967+
gSystemLayerAndClock.Clear();
1968+
DataModel::Nullable<ElapsedS> openDuration;
1969+
openDuration.SetNonNull(2u);
1970+
EXPECT_EQ(logic.HandleOpenCommand(std::make_optional(openDuration), std::nullopt), CHIP_NO_ERROR);
1971+
EXPECT_TRUE(HasAttributeChanges(context.GetDirtyList(), Attributes::OpenDuration::Id));
1972+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 0);
1973+
1974+
gSystemLayerAndClock.AdvanceMonotonic(1000_ms64);
1975+
EXPECT_EQ(logic.HandleCloseCommand(), CHIP_NO_ERROR);
1976+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
1977+
1978+
// Ensure the timer was cancelled
1979+
gSystemLayerAndClock.AdvanceMonotonic(1000_ms64);
1980+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
1981+
}
1982+
1983+
TEST_F(TestValveConfigurationAndControlClusterLogic, TestCloseCommandOpenValveDurationNoLevel)
1984+
{
1985+
TestDelegateNoLevel delegate;
1986+
TestPersistentStorageDelegate storageDelegate;
1987+
EndpointId endpoint = 0;
1988+
MockedMatterContext context(endpoint, storageDelegate);
1989+
ClusterLogic logic(delegate, context);
1990+
1991+
ClusterConformance conformance = {
1992+
.featureMap = 0, .supportsDefaultOpenLevel = false, .supportsValveFault = true, .supportsLevelStep = false
1993+
};
1994+
EXPECT_EQ(logic.Init(conformance), CHIP_NO_ERROR);
1995+
1996+
gSystemLayerAndClock.SetMonotonic(0_ms64);
1997+
gSystemLayerAndClock.Clear();
1998+
DataModel::Nullable<ElapsedS> openDuration;
1999+
openDuration.SetNonNull(2u);
2000+
EXPECT_EQ(logic.HandleOpenCommand(std::make_optional(openDuration), std::nullopt), CHIP_NO_ERROR);
2001+
EXPECT_TRUE(HasAttributeChanges(context.GetDirtyList(), Attributes::OpenDuration::Id));
2002+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 0);
2003+
2004+
gSystemLayerAndClock.AdvanceMonotonic(2000_ms64);
2005+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
2006+
2007+
gSystemLayerAndClock.AdvanceMonotonic(1000_ms64);
2008+
EXPECT_EQ(logic.HandleCloseCommand(), CHIP_NO_ERROR);
2009+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
2010+
2011+
// Ensure the timer was cancelled
2012+
gSystemLayerAndClock.AdvanceMonotonic(1000_ms64);
2013+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
2014+
}
2015+
2016+
// while remaining duration is null and valve is open
2017+
TEST_F(TestValveConfigurationAndControlClusterLogic, TestCloseCommandOpenValveNoDurationLevel)
2018+
{
2019+
TestDelegateLevel delegate;
2020+
TestPersistentStorageDelegate storageDelegate;
2021+
EndpointId endpoint = 0;
2022+
MockedMatterContext context(endpoint, storageDelegate);
2023+
ClusterLogic logic(delegate, context);
2024+
2025+
ClusterConformance conformance = { .featureMap = to_underlying(Feature::kLevel) | to_underlying(Feature::kTimeSync),
2026+
.supportsDefaultOpenLevel = true,
2027+
.supportsValveFault = true,
2028+
.supportsLevelStep = true };
2029+
EXPECT_EQ(logic.Init(conformance), CHIP_NO_ERROR);
2030+
2031+
gSystemLayerAndClock.SetMonotonic(0_ms64);
2032+
gSystemLayerAndClock.Clear();
2033+
EXPECT_EQ(logic.HandleOpenCommand(std::nullopt, std::nullopt), CHIP_NO_ERROR);
2034+
EXPECT_TRUE(HasAttributeChanges(context.GetDirtyList(), Attributes::OpenDuration::Id));
2035+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 0);
2036+
2037+
gSystemLayerAndClock.AdvanceMonotonic(1000_ms64);
2038+
EXPECT_EQ(logic.HandleCloseCommand(), CHIP_NO_ERROR);
2039+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
2040+
}
2041+
2042+
TEST_F(TestValveConfigurationAndControlClusterLogic, TestCloseCommandOpenValveNoDurationNoLevel)
2043+
{
2044+
TestDelegateNoLevel delegate;
2045+
TestPersistentStorageDelegate storageDelegate;
2046+
EndpointId endpoint = 0;
2047+
MockedMatterContext context(endpoint, storageDelegate);
2048+
ClusterLogic logic(delegate, context);
2049+
2050+
ClusterConformance conformance = {
2051+
.featureMap = 0, .supportsDefaultOpenLevel = false, .supportsValveFault = true, .supportsLevelStep = false
2052+
};
2053+
EXPECT_EQ(logic.Init(conformance), CHIP_NO_ERROR);
2054+
2055+
gSystemLayerAndClock.SetMonotonic(0_ms64);
2056+
gSystemLayerAndClock.Clear();
2057+
EXPECT_EQ(logic.HandleOpenCommand(std::nullopt, std::nullopt), CHIP_NO_ERROR);
2058+
EXPECT_TRUE(HasAttributeChanges(context.GetDirtyList(), Attributes::OpenDuration::Id));
2059+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 0);
2060+
2061+
gSystemLayerAndClock.AdvanceMonotonic(2000_ms64);
2062+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
2063+
2064+
gSystemLayerAndClock.AdvanceMonotonic(1000_ms64);
2065+
EXPECT_EQ(logic.HandleCloseCommand(), CHIP_NO_ERROR);
2066+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
2067+
}
2068+
19522069
// while valve is closed
2070+
TEST_F(TestValveConfigurationAndControlClusterLogic, TestCloseCommandClosedLevel)
2071+
{
2072+
TestDelegateLevel delegate;
2073+
TestPersistentStorageDelegate storageDelegate;
2074+
EndpointId endpoint = 0;
2075+
MockedMatterContext context(endpoint, storageDelegate);
2076+
ClusterLogic logic(delegate, context);
2077+
2078+
ClusterConformance conformance = { .featureMap = to_underlying(Feature::kLevel) | to_underlying(Feature::kTimeSync),
2079+
.supportsDefaultOpenLevel = true,
2080+
.supportsValveFault = true,
2081+
.supportsLevelStep = true };
2082+
EXPECT_EQ(logic.Init(conformance), CHIP_NO_ERROR);
2083+
2084+
gSystemLayerAndClock.SetMonotonic(0_ms64);
2085+
gSystemLayerAndClock.Clear();
2086+
2087+
EXPECT_EQ(logic.HandleCloseCommand(), CHIP_NO_ERROR);
2088+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
2089+
}
2090+
2091+
TEST_F(TestValveConfigurationAndControlClusterLogic, TestCloseCommandClosedNoLevel)
2092+
{
2093+
TestDelegateNoLevel delegate;
2094+
TestPersistentStorageDelegate storageDelegate;
2095+
EndpointId endpoint = 0;
2096+
MockedMatterContext context(endpoint, storageDelegate);
2097+
ClusterLogic logic(delegate, context);
2098+
2099+
ClusterConformance conformance = {
2100+
.featureMap = 0, .supportsDefaultOpenLevel = false, .supportsValveFault = true, .supportsLevelStep = false
2101+
};
2102+
EXPECT_EQ(logic.Init(conformance), CHIP_NO_ERROR);
2103+
2104+
gSystemLayerAndClock.SetMonotonic(0_ms64);
2105+
gSystemLayerAndClock.Clear();
2106+
2107+
EXPECT_EQ(logic.HandleCloseCommand(), CHIP_NO_ERROR);
2108+
EXPECT_EQ(delegate.numHandleCloseValveCalls, 1);
2109+
}
2110+
// Before init
2111+
// simulated failures return error
19532112

19542113
//=========================================================================================
19552114
// Tests for timing for async read updates to current / target level and state
@@ -1972,10 +2131,9 @@ TEST_F(TestValveConfigurationAndControlClusterLogic, TestCloseCalledAtOpenDurati
19722131
//=========================================================================================
19732132
// Tests for attribute callbacks from delegates
19742133
//=========================================================================================
1975-
// TODO: Should the delegate call the cluster logic class direclty, or should this be piped through the delegate class so the app
1976-
// layer ONLY has to interact with the delegate?
1977-
// Test setter for valve fault Test attribute change notifications are sent out and not
1978-
// sent out when the attribute is change do the same value - add in prior
2134+
// TODO: Should the delegate call the cluster logic class direclty, or should this be piped through the delegate class so the
2135+
// app layer ONLY has to interact with the delegate? Test setter for valve fault Test attribute change notifications are sent
2136+
// out and not sent out when the attribute is change do the same value - add in prior
19792137

19802138
//=========================================================================================
19812139
// Tests for attribute callbacks from delegates

0 commit comments

Comments
 (0)