Skip to content

Commit 33e93f2

Browse files
committed
Commit the draft to add_event in post attr change
Commit the draft to add_event in post attribute change callback
1 parent f29ccbe commit 33e93f2

File tree

7 files changed

+936
-14
lines changed

7 files changed

+936
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <app-common/zap-generated/attributes/Accessors.h>
20+
#include <app/clusters/switch-server/switch-server.h>
21+
#include <app/server/Server.h>
22+
#include <app/util/att-storage.h>
23+
#include <platform/PlatformManager.h>
24+
#include "stubs.h"
25+
26+
using namespace chip;
27+
using namespace chip::app;
28+
using namespace chip::app::Clusters;
29+
using namespace chip::app::Clusters::Switch;
30+
using namespace chip::DeviceLayer;
31+
32+
class SwitchEventHandler
33+
{
34+
public:
35+
SwitchEventHandler(EndpointId endpoint):mEndpointId(endpoint){};
36+
37+
/**
38+
* Should be called when the latching switch is moved to a new position.
39+
*/
40+
void OnSwitchLatchedHandler(uint8_t newPosition);
41+
42+
/**
43+
* Should be called when the momentary switch starts to be pressed.
44+
*/
45+
void OnSwitchInitialPressedHandler(uint8_t newPosition);
46+
47+
/**
48+
* Should be called when the momentary switch has been pressed for a "long" time.
49+
*/
50+
void OnSwitchLongPressedHandler(uint8_t newPosition);
51+
52+
/**
53+
* Should be called when the momentary switch has been released.
54+
*/
55+
void OnSwitchShortReleasedHandler(uint8_t previousPosition);
56+
57+
/**
58+
* Should be called when the momentary switch has been released after having been pressed for a long time.
59+
*/
60+
void OnSwitchLongReleasedHandler(uint8_t previousPosition);
61+
62+
/**
63+
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
64+
* sequence, during that sequence.
65+
*/
66+
void OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count);
67+
68+
/**
69+
* Should be called to indicate how many times the momentary switch has been pressed in a multi-press
70+
* sequence, after it has been detected that the sequence has ended.
71+
*/
72+
void OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count);
73+
74+
private:
75+
EndpointId mEndpointId;
76+
};
77+
78+
79+
void SwitchEventHandler::OnSwitchLatchedHandler(uint8_t newPosition)
80+
{
81+
ChipLogDetail(NotSpecified, "The latching switch is moved to a new position:%d", newPosition);
82+
83+
Clusters::SwitchServer::Instance().OnSwitchLatch(mEndpointId, newPosition);
84+
}
85+
86+
void SwitchEventHandler::OnSwitchInitialPressedHandler(uint8_t newPosition)
87+
{
88+
ChipLogDetail(NotSpecified, "The new position when the momentary switch starts to be pressed:%d", newPosition);
89+
90+
Clusters::SwitchServer::Instance().OnInitialPress(mEndpointId, newPosition);
91+
}
92+
93+
void SwitchEventHandler::OnSwitchLongPressedHandler(uint8_t newPosition)
94+
{
95+
ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed for a long time:%d", newPosition);
96+
97+
Clusters::SwitchServer::Instance().OnLongPress(mEndpointId, newPosition);
98+
}
99+
100+
void SwitchEventHandler::OnSwitchShortReleasedHandler(uint8_t previousPosition)
101+
{
102+
ChipLogDetail(NotSpecified, "The the previous value of the CurrentPosition when the momentary switch has been released:%d",
103+
previousPosition);
104+
105+
Clusters::SwitchServer::Instance().OnShortRelease(mEndpointId, previousPosition);
106+
}
107+
108+
void SwitchEventHandler::OnSwitchLongReleasedHandler(uint8_t previousPosition)
109+
{
110+
ChipLogDetail(NotSpecified,
111+
"The the previous value of the CurrentPosition when the momentary switch has been released after having been "
112+
"pressed for a long time:%d",
113+
previousPosition);
114+
115+
Clusters::SwitchServer::Instance().OnLongRelease(mEndpointId, previousPosition);
116+
}
117+
118+
void SwitchEventHandler::OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count)
119+
{
120+
ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed in a multi-press sequence:%d",
121+
newPosition);
122+
ChipLogDetail(NotSpecified, "%d times the momentary switch has been pressed", count);
123+
124+
Clusters::SwitchServer::Instance().OnMultiPressOngoing(mEndpointId, newPosition, count);
125+
}
126+
127+
void SwitchEventHandler::OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count)
128+
{
129+
ChipLogDetail(NotSpecified, "The previous position when the momentary switch has been pressed in a multi-press sequence:%d",
130+
previousPosition);
131+
ChipLogDetail(NotSpecified, "%d times the momentary switch has been pressed", count);
132+
133+
Clusters::SwitchServer::Instance().OnMultiPressComplete(mEndpointId, previousPosition, count);
134+
}
135+
136+
static std::map<int, SwitchEventHandler *> gSwitchEventHandlers{};
137+
138+
139+
class SwitchAttributeDelegate : public AttributeDelegate
140+
{
141+
public:
142+
SwitchAttributeDelegate (ClusterId clusterId) : AttributeDelegate(clusterId) {}
143+
144+
void PostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, uint8_t * value) override;
145+
146+
private:
147+
};
148+
149+
150+
SwitchEventHandler * GetSwitchEventHandler(EndpointId endpointId)
151+
{
152+
if (gSwitchEventHandlers.find(endpointId) == gSwitchEventHandlers.end()) {
153+
return nullptr;
154+
}
155+
156+
return gSwitchEventHandlers[endpointId];
157+
}
158+
159+
void SwitchAttributeDelegate::PostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, uint8_t * value)
160+
{
161+
printf("\033[41m %s, %d \033[0m \n", __func__, __LINE__);
162+
ChipLogProgress(Zcl, "SwitchAttributeDelegate::PostAttributeChangeCallback Endpoint: %d, Cluster: " ChipLogFormatMEI ", Type: %u, length %u", attributePath.mEndpointId, ChipLogValueMEI(attributePath.mClusterId), type, size);
163+
164+
switch (attributePath.mAttributeId) {
165+
case Attributes::CurrentPosition::Id: {
166+
SwitchEventHandler *eventHandler = GetSwitchEventHandler(attributePath.mEndpointId);
167+
uint8_t newPosition = *value;
168+
169+
if (eventHandler) {
170+
eventHandler->OnSwitchLatchedHandler(newPosition);
171+
}
172+
}
173+
break;
174+
default:
175+
break;
176+
}
177+
}
178+
179+
void emberAfSwitchClusterInitCallback(EndpointId endpoint)
180+
{
181+
ChipLogProgress(Zcl, "Chef: emberAfSwitchClusterInitCallback");
182+
gSwitchEventHandlers[endpoint] = new SwitchEventHandler(endpoint);
183+
RegisterApplicationAttributeDelegate(Switch::Id, new SwitchAttributeDelegate(Switch::Id));
184+
printf("\033[44m %s, %d, Switch::ID=%u \033[0m \n", __func__, __LINE__, Switch::Id);
185+
}
186+
187+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <app-common/zap-generated/cluster-objects.h>
22+
#include <app/clusters/switch-server/switch-server.h>
23+
24+
#include <protocols/interaction_model/StatusCode.h>
25+
26+
namespace chip {
27+
namespace app {
28+
namespace Clusters {
29+
30+
namespace Switch {
31+
32+
} // namespace Switch
33+
} // namespace Clusters
34+
} // namespace app
35+
} // namespace chip
36+
37+
38+
39+
//class AllClustersCommandDelegate : public NamedPipeCommandDelegate
40+
//{
41+
//public:
42+
// void OnEventCommandReceived(const char * json) override;
43+
//};

examples/chef/common/stubs.cpp

+47-12
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,36 @@
1919
#include "chef-concentration-measurement.h"
2020
#endif
2121

22+
#include "stubs.h"
23+
#include <map>
24+
2225
using chip::app::DataModel::Nullable;
2326

2427
using namespace chip;
2528
using namespace chip::app;
2629
using namespace chip::app::Clusters;
2730

31+
static std::map<ClusterId, AttributeDelegate * > gApplicationAttributeDelegates{};
32+
33+
void chip::app::RegisterApplicationAttributeDelegate(ClusterId clusterId, AttributeDelegate * delegate)
34+
{
35+
// TODO assert (gApplicationAttributeDelegates.find(clusterId) == gApplicationAttributeDelegates.end() )
36+
37+
gApplicationAttributeDelegates[clusterId] = delegate;
38+
39+
}
40+
41+
42+
AttributeDelegate * GetApplicationAttributeDelegate(ClusterId clusterId)
43+
{
44+
if (gApplicationAttributeDelegates.find(clusterId) == gApplicationAttributeDelegates.end()) {
45+
return nullptr;
46+
}
47+
48+
return gApplicationAttributeDelegates[clusterId];
49+
}
50+
51+
2852
Protocols::InteractionModel::Status emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
2953
const EmberAfAttributeMetadata * attributeMetadata,
3054
uint8_t * buffer, uint16_t maxReadLength)
@@ -116,22 +140,33 @@ void emberAfPluginSmokeCoAlarmSelfTestRequestCommand(EndpointId endpointId) {}
116140
void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
117141
uint8_t * value)
118142
{
119-
ClusterId clusterId = attributePath.mClusterId;
120-
AttributeId attributeId = attributePath.mAttributeId;
121-
ChipLogProgress(Zcl, "Cluster callback: " ChipLogFormatMEI, ChipLogValueMEI(clusterId));
143+
// AttributeId attributeId = attributePath.mAttributeId;
144+
ChipLogProgress(Zcl, "MatterPostAttributeChangeCallback Endpoint: %d, Cluster: " ChipLogFormatMEI ", Type: %u, length %u", attributePath.mEndpointId, ChipLogValueMEI(attributePath.mClusterId), type, size);
145+
printf("\033[41m %s, %d \033[0m \n", __func__, __LINE__);
122146

123-
if (clusterId == OnOff::Id && attributeId == OnOff::Attributes::OnOff::Id)
124-
{
125-
ChipLogProgress(Zcl, "OnOff attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u", ChipLogValueMEI(attributeId),
126-
type, *value, size);
147+
AttributeDelegate * delegate = GetApplicationAttributeDelegate(attributePath.mClusterId);
148+
if (delegate) {
149+
delegate->PostAttributeChangeCallback(attributePath, type, size, value);
127150
}
128-
else if (clusterId == LevelControl::Id)
129-
{
130-
ChipLogProgress(Zcl, "Level Control attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u",
131-
ChipLogValueMEI(attributeId), type, *value, size);
132151

133-
// WIP Apply attribute change to Light
152+
#if 0
153+
switch (clusterId) {
154+
case OnOff::Id:
155+
ChipLogProgress(Zcl, "OnOff Cluster attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u", ChipLogValueMEI(attributeId),
156+
type, *value, size);
157+
break;
158+
case LevelControl::Id:
159+
ChipLogProgress(Zcl, "Level Control Cluster attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u",
160+
ChipLogValueMEI(attributeId), type, *value, size);
161+
break;
162+
case Switch::Id:
163+
ChipLogProgress(Zcl, "Switch Cluster attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u",
164+
ChipLogValueMEI(attributeId), type, *value, size);
165+
break;
166+
default:
167+
break;
134168
}
169+
#endif
135170
}
136171

137172
/** @brief OnOff Cluster Init

examples/chef/common/stubs.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <app-common/zap-generated/cluster-objects.h>
22+
#include <protocols/interaction_model/StatusCode.h>
23+
24+
namespace chip {
25+
namespace app {
26+
27+
class AttributeDelegate
28+
{
29+
public:
30+
AttributeDelegate (ClusterId clusterId) : mClusterId(clusterId) {}
31+
/**
32+
* xxxx
33+
*/
34+
virtual void PostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, uint8_t * value) {}
35+
/**
36+
* xxxx
37+
*/
38+
virtual Protocols::InteractionModel::Status ExternalAttributeWriteCallback(EndpointId endpoint, ClusterId clusterId, const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer) {return Protocols::InteractionModel::Status::Success;}
39+
/**
40+
* xxxx
41+
*/
42+
virtual Protocols::InteractionModel::Status ExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId, const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer, uint16_t maxReadLength) {return Protocols::InteractionModel::Status::Success;}
43+
44+
virtual ~AttributeDelegate() {}
45+
46+
private:
47+
ClusterId mClusterId;
48+
};
49+
50+
void RegisterApplicationAttributeDelegate(ClusterId clusterId, AttributeDelegate * delegate);
51+
52+
} // namespace app
53+
} // namespace chip
54+
55+
56+
57+
//class AllClustersCommandDelegate : public NamedPipeCommandDelegate
58+
//{
59+
//public:
60+
// void OnEventCommandReceived(const char * json) override;
61+
//};

0 commit comments

Comments
 (0)