Skip to content

Commit 4c26dd4

Browse files
committed
Clear timer when fabric is removed
1 parent 3d5abb5 commit 4c26dd4

File tree

4 files changed

+47
-43
lines changed

4 files changed

+47
-43
lines changed

scripts/tools/zap/tests/outputs/all-clusters-app/app-templates/endpoint_config.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,7 @@
22172217
}; \
22182218
const EmberAfGenericClusterFunction chipFuncArrayThermostatServer[] = { \
22192219
(EmberAfGenericClusterFunction) emberAfThermostatClusterServerInitCallback, \
2220+
(EmberAfGenericClusterFunction) MatterThermostatClusterServerShutdownCallback, \
22202221
(EmberAfGenericClusterFunction) MatterThermostatClusterServerPreAttributeChangedCallback, \
22212222
}; \
22222223
const EmberAfGenericClusterFunction chipFuncArrayFanControlServer[] = { \
@@ -3755,7 +3756,7 @@
37553756
.attributes = ZAP_ATTRIBUTE_INDEX(616), \
37563757
.attributeCount = 26, \
37573758
.clusterSize = 72, \
3758-
.mask = ZAP_CLUSTER_MASK(SERVER) | ZAP_CLUSTER_MASK(INIT_FUNCTION) | ZAP_CLUSTER_MASK(PRE_ATTRIBUTE_CHANGED_FUNCTION), \
3759+
.mask = ZAP_CLUSTER_MASK(SERVER) | ZAP_CLUSTER_MASK(INIT_FUNCTION) | ZAP_CLUSTER_MASK(SHUTDOWN_FUNCTION) | ZAP_CLUSTER_MASK(PRE_ATTRIBUTE_CHANGED_FUNCTION), \
37593760
.functions = chipFuncArrayThermostatServer, \
37603761
.acceptedCommandList = ZAP_GENERATED_COMMANDS_INDEX( 241 ), \
37613762
.generatedCommandList = ZAP_GENERATED_COMMANDS_INDEX( 246 ), \

src/app/clusters/thermostat-server/thermostat-server.cpp

+33-29
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <app/ConcreteAttributePath.h>
2929
#include <app/ConcreteCommandPath.h>
3030
#include <app/server/Server.h>
31+
#include <app/util/endpoint-config-api.h>
3132
#include <lib/core/CHIPEncoding.h>
3233
#include <platform/internal/CHIPDeviceLayerInternal.h>
3334

@@ -117,8 +118,7 @@ void TimerExpiredCallback(System::Layer * systemLayer, void * callbackContext)
117118
VerifyOrReturn(delegate != nullptr, ChipLogError(Zcl, "Delegate is null. Unable to handle timer expired"));
118119

119120
delegate->ClearPendingPresetList();
120-
gThermostatAttrAccess.SetAtomicWrite(endpoint, false);
121-
gThermostatAttrAccess.SetAtomicWriteScopedNodeId(endpoint, ScopedNodeId());
121+
gThermostatAttrAccess.SetAtomicWrite(endpoint, ScopedNodeId(), false);
122122
}
123123

124124
/**
@@ -206,8 +206,7 @@ void resetAtomicWrite(Delegate * delegate, EndpointId endpoint)
206206
delegate->ClearPendingPresetList();
207207
}
208208
ClearTimer(endpoint);
209-
gThermostatAttrAccess.SetAtomicWrite(endpoint, false);
210-
gThermostatAttrAccess.SetAtomicWriteScopedNodeId(endpoint, ScopedNodeId());
209+
gThermostatAttrAccess.SetAtomicWrite(endpoint, ScopedNodeId(), false);
211210
}
212211

213212
/**
@@ -633,14 +632,16 @@ void SetDefaultDelegate(EndpointId endpoint, Delegate * delegate)
633632
}
634633
}
635634

636-
void ThermostatAttrAccess::SetAtomicWrite(EndpointId endpoint, bool inProgress)
635+
void ThermostatAttrAccess::SetAtomicWrite(EndpointId endpoint, ScopedNodeId originatorNodeId, bool inProgress)
637636
{
638637
uint16_t ep =
639638
emberAfGetClusterServerEndpointIndex(endpoint, Thermostat::Id, MATTER_DM_THERMOSTAT_CLUSTER_SERVER_ENDPOINT_COUNT);
640639

641-
if (ep < ArraySize(mAtomicWriteState))
640+
if (ep < ArraySize(mAtomicWriteStates))
642641
{
643-
mAtomicWriteState[ep] = inProgress;
642+
mAtomicWriteStates[ep].inProgress = inProgress;
643+
mAtomicWriteStates[ep].endpointId = endpoint;
644+
mAtomicWriteStates[ep].nodeId = originatorNodeId;
644645
}
645646
}
646647

@@ -650,9 +651,9 @@ bool ThermostatAttrAccess::InAtomicWrite(EndpointId endpoint)
650651
uint16_t ep =
651652
emberAfGetClusterServerEndpointIndex(endpoint, Thermostat::Id, MATTER_DM_THERMOSTAT_CLUSTER_SERVER_ENDPOINT_COUNT);
652653

653-
if (ep < ArraySize(mAtomicWriteState))
654+
if (ep < ArraySize(mAtomicWriteStates))
654655
{
655-
inAtomicWrite = mAtomicWriteState[ep];
656+
inAtomicWrite = mAtomicWriteStates[ep].inProgress;
656657
}
657658
return inAtomicWrite;
658659
}
@@ -677,26 +678,15 @@ bool ThermostatAttrAccess::InAtomicWrite(CommandHandler * commandObj, EndpointId
677678
return GetAtomicWriteScopedNodeId(endpoint) == sourceNodeId;
678679
}
679680

680-
void ThermostatAttrAccess::SetAtomicWriteScopedNodeId(EndpointId endpoint, ScopedNodeId originatorNodeId)
681-
{
682-
uint16_t ep =
683-
emberAfGetClusterServerEndpointIndex(endpoint, Thermostat::Id, MATTER_DM_THERMOSTAT_CLUSTER_SERVER_ENDPOINT_COUNT);
684-
685-
if (ep < ArraySize(mAtomicWriteNodeIds))
686-
{
687-
mAtomicWriteNodeIds[ep] = originatorNodeId;
688-
}
689-
}
690-
691681
ScopedNodeId ThermostatAttrAccess::GetAtomicWriteScopedNodeId(EndpointId endpoint)
692682
{
693683
ScopedNodeId originatorNodeId = ScopedNodeId();
694684
uint16_t ep =
695685
emberAfGetClusterServerEndpointIndex(endpoint, Thermostat::Id, MATTER_DM_THERMOSTAT_CLUSTER_SERVER_ENDPOINT_COUNT);
696686

697-
if (ep < ArraySize(mAtomicWriteNodeIds))
687+
if (ep < ArraySize(mAtomicWriteStates))
698688
{
699-
originatorNodeId = mAtomicWriteNodeIds[ep];
689+
originatorNodeId = mAtomicWriteStates[ep].nodeId;
700690
}
701691
return originatorNodeId;
702692
}
@@ -981,13 +971,17 @@ CHIP_ERROR ThermostatAttrAccess::AppendPendingPreset(Thermostat::Delegate * dele
981971

982972
void ThermostatAttrAccess::OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex)
983973
{
984-
for (size_t i = 0; i < ArraySize(mAtomicWriteNodeIds); ++i)
974+
for (size_t i = 0; i < ArraySize(mAtomicWriteStates); ++i)
985975
{
986-
auto nodeId = mAtomicWriteNodeIds[i];
987-
if (nodeId.GetFabricIndex() == fabricIndex)
976+
auto atomicWriteState = mAtomicWriteStates[i];
977+
if (atomicWriteState.nodeId.GetFabricIndex() == fabricIndex)
988978
{
989-
mAtomicWriteNodeIds[i] = ScopedNodeId();
990-
mAtomicWriteState[i] = false;
979+
auto delegate = GetDelegate(atomicWriteState.endpointId);
980+
if (delegate == nullptr)
981+
{
982+
continue;
983+
}
984+
resetAtomicWrite(delegate, atomicWriteState.endpointId);
991985
}
992986
}
993987
}
@@ -1457,8 +1451,7 @@ void handleAtomicBegin(CommandHandler * commandObj, const ConcreteCommandPath &
14571451
timeout = std::min(timeout, maxTimeout);
14581452

14591453
ScheduleTimer(endpoint, System::Clock::Milliseconds16(timeout));
1460-
gThermostatAttrAccess.SetAtomicWrite(endpoint, true);
1461-
gThermostatAttrAccess.SetAtomicWriteScopedNodeId(endpoint, GetSourceScopedNodeId(commandObj));
1454+
gThermostatAttrAccess.SetAtomicWrite(endpoint, GetSourceScopedNodeId(commandObj), true);
14621455
sendAtomicResponse(commandObj, commandPath, imcode::Success, imcode::Success, imcode::Success, MakeOptional(timeout));
14631456
}
14641457

@@ -1912,3 +1905,14 @@ void MatterThermostatPluginServerInitCallback()
19121905
Server::GetInstance().GetFabricTable().AddFabricDelegate(&gThermostatAttrAccess);
19131906
registerAttributeAccessOverride(&gThermostatAttrAccess);
19141907
}
1908+
1909+
void MatterThermostatClusterServerShutdownCallback(EndpointId endpoint)
1910+
{
1911+
ChipLogProgress(Zcl, "Shutting down thermostat server cluster on endpoint %d", endpoint);
1912+
auto delegate = GetDelegate(endpoint);
1913+
1914+
if (delegate != nullptr)
1915+
{
1916+
resetAtomicWrite(delegate, endpoint);
1917+
}
1918+
}

src/app/clusters/thermostat-server/thermostat-server.h

+11-13
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ class ThermostatAttrAccess : public chip::app::AttributeAccessInterface, public
4848
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
4949
CHIP_ERROR Write(const ConcreteDataAttributePath & aPath, chip::app::AttributeValueDecoder & aDecoder) override;
5050

51-
/**
52-
* @brief Sets the scoped node id of the originator that sent the last successful
53-
* AtomicRequest of type BeginWrite for the given endpoint.
54-
*
55-
* @param[in] endpoint The endpoint.
56-
* @param[in] originatorNodeId The originator scoped node id.
57-
*/
58-
void SetAtomicWriteScopedNodeId(EndpointId endpoint, ScopedNodeId originatorNodeId);
59-
6051
/**
6152
* @brief Gets the scoped node id of the originator that sent the last successful
6253
* AtomicRequest of type BeginWrite for the given endpoint.
@@ -68,12 +59,13 @@ class ThermostatAttrAccess : public chip::app::AttributeAccessInterface, public
6859
ScopedNodeId GetAtomicWriteScopedNodeId(EndpointId endpoint);
6960

7061
/**
71-
* @brief Sets whether an atomic write is in progress for the given endpoint
62+
* @brief Sets whether an atomic write is in progress for the given endpoint and originatorNodeId
7263
*
7364
* @param[in] endpoint The endpoint.
65+
* @param[in] originatorNodeId The originator scoped node id.
7466
* @param[in] inProgress Whether or not an atomic write is in progress.
7567
*/
76-
void SetAtomicWrite(EndpointId endpoint, bool inProgress);
68+
void SetAtomicWrite(EndpointId endpoint, ScopedNodeId originatorNodeId, bool inProgress);
7769

7870
/**
7971
* @brief Gets whether an atomic write is in progress for the given endpoint
@@ -109,8 +101,14 @@ class ThermostatAttrAccess : public chip::app::AttributeAccessInterface, public
109101

110102
void OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex) override;
111103

112-
ScopedNodeId mAtomicWriteNodeIds[kThermostatEndpointCount];
113-
bool mAtomicWriteState[kThermostatEndpointCount];
104+
struct AtomicWriteState
105+
{
106+
bool inProgress;
107+
ScopedNodeId nodeId;
108+
EndpointId endpointId;
109+
};
110+
111+
AtomicWriteState mAtomicWriteStates[kThermostatEndpointCount];
114112
};
115113

116114
/**

src/app/common/templates/config-data.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ ClustersWithShutdownFunctions:
8181
- Color Control
8282
- Sample MEI
8383
- Scenes Management
84+
- Thermostat
8485

8586
ClustersWithPreAttributeChangeFunctions:
8687
- Door Lock

0 commit comments

Comments
 (0)