Skip to content

Commit 4d9380a

Browse files
committed
Chime Cluster Definition and server code
1 parent 6c0ba6e commit 4d9380a

File tree

10 files changed

+364
-0
lines changed

10 files changed

+364
-0
lines changed

.github/workflows/tests.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ jobs:
112112
src/app/zap-templates/zcl/data-model/chip/chip-ota.xml \
113113
src/app/zap-templates/zcl/data-model/chip/chip-types.xml \
114114
src/app/zap-templates/zcl/data-model/chip/channel-cluster.xml \
115+
src/app/zap-templates/zcl/data-model/chip/chime-cluster.xml \
115116
src/app/zap-templates/zcl/data-model/chip/clusters-extensions.xml \
116117
src/app/zap-templates/zcl/data-model/chip/color-control-cluster.xml \
117118
src/app/zap-templates/zcl/data-model/chip/commissioner-control-cluster.xml \

scripts/rules.matterlint

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ load "../src/app/zap-templates/zcl/data-model/chip/boolean-state-cluster.xml";
1515
load "../src/app/zap-templates/zcl/data-model/chip/actions-cluster.xml";
1616
load "../src/app/zap-templates/zcl/data-model/chip/bridged-device-basic-information.xml";
1717
load "../src/app/zap-templates/zcl/data-model/chip/channel-cluster.xml";
18+
load "../src/app/zap-templates/zcl/data-model/chip/chime-cluster.xml";
1819
load "../src/app/zap-templates/zcl/data-model/chip/chip-ota.xml";
1920
load "../src/app/zap-templates/zcl/data-model/chip/chip-types.xml";
2021
load "../src/app/zap-templates/zcl/data-model/chip/clusters-extensions.xml";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 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+
/****************************************************************************'
19+
* @file
20+
* @brief Implementation for the Chime Server Cluster
21+
***************************************************************************/
22+
23+
24+
#include "chime-server.h"
25+
26+
#include <app/AttributeAccessInterfaceRegistry.h>
27+
#include <app/CommandHandlerInterfaceRegistry.h>
28+
#include <app/ConcreteAttributePath.h>
29+
#include <app/InteractionModelEngine.h>
30+
#include <app/util/attribute-storage.h>
31+
#include <app/util/util.h>
32+
#include <protocols/interaction_model/StatusCode.h>
33+
34+
using namespace chip;
35+
using namespace chip::app;
36+
using namespace chip::app::DataModel;
37+
using namespace chip::app::Clusters;
38+
using namespace chip::app::Clusters::Chime;
39+
using namespace chip::app::Clusters::Chime::Attributes;
40+
using chip::Protocols::InteractionModel::Status;
41+
42+
43+
namespace chip {
44+
namespace app {
45+
namespace Clusters {
46+
47+
ChimeServer::ChimeServer(EndpointId endpointId, ChimeDelegate & delegate) :
48+
AttributeAccessInterface(MakeOptional(endpointId), Chime::Id),
49+
CommandHandlerInterface(MakeOptional(endpointId), Chime::Id), mDelegate(delegate)
50+
{
51+
mDelegate.SetChimeServer(this);
52+
}
53+
54+
ChimeServer::~ChimeServer()
55+
{
56+
AttributeAccessInterfaceRegistry::Instance().Unregister(this);
57+
CommandHandlerInterfaceRegistry::Instance().UnregisterCommandHandler(this);
58+
}
59+
60+
61+
CHIP_ERROR ChimeServer::Init()
62+
{
63+
VerifyOrReturnError(AttributeAccessInterfaceRegistry::Instance().Register(this), CHIP_ERROR_INTERNAL);
64+
ReturnErrorOnFailure(CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(this));
65+
return CHIP_NO_ERROR;
66+
}
67+
68+
// AttributeAccessInterface
69+
CHIP_ERROR ChimeServer::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
70+
{
71+
VerifyOrDie(aPath.mClusterId == Chime::Id);
72+
73+
switch (aPath.mAttributeId)
74+
{
75+
case InstalledChimeSounds::Id:
76+
return aEncoder.Encode(mDelegate.GetInstalledChimeSounds());
77+
78+
case ActiveChimeSoundId::Id:
79+
return aEncoder.Encode(mDelegate.GetActiveChimeSoundId());
80+
81+
case Enabled::Id:
82+
return aEncoder.Encode(mDelegate.GetEnabled());
83+
}
84+
85+
return CHIP_NO_ERROR;
86+
}
87+
88+
CHIP_ERROR ChimeServer::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
89+
{
90+
VerifyOrDie(aPath.mClusterId == Chime::Id);
91+
92+
switch (aPath.mAttributeId)
93+
{
94+
case ActiveChimeSoundId::Id: {
95+
uint8_t newValue;
96+
ReturnErrorOnFailure(aDecoder.Decode(newValue));
97+
ReturnErrorOnFailure(SetActiveChimeSoundId(newValue));
98+
return CHIP_NO_ERROR;
99+
100+
}
101+
case Enabled::Id: {
102+
bool newValue;
103+
ReturnErrorOnFailure(aDecoder.Decode(newValue));
104+
ReturnErrorOnFailure(mDelegate.SetEnabled(newValue));
105+
return CHIP_NO_ERROR;
106+
}
107+
108+
default:
109+
// Unknown attribute
110+
return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute);
111+
}
112+
113+
}
114+
115+
CHIP_ERROR ChimeServer::SetActiveChimeSoundId(uint8_t soundId)
116+
{
117+
uint8_t currentSoundId = mDelegate.GetActiveChimeSoundId();
118+
bool activeSoundIdChanged = !(currentSoundId == soundId);
119+
120+
VerifyOrReturnError(activeSoundIdChanged, CHIP_NO_ERROR);
121+
VerifyOrDie(mDelegate.SetActiveChimeSoundId(soundId) == CHIP_NO_ERROR);
122+
MatterReportingAttributeChangeCallback(GetEndpointId(), Chime::Id, ActiveChimeSoundId::Id);
123+
124+
return CHIP_NO_ERROR;
125+
}
126+
127+
CHIP_ERROR ChimeServer::SetEnabled(bool enabled)
128+
{
129+
bool currentlyEnabled = mDelegate.GetEnabled();
130+
bool enableChanged = !(currentlyEnabled == enabled);
131+
132+
VerifyOrReturnError(enableChanged, CHIP_NO_ERROR);
133+
VerifyOrDie(mDelegate.SetEnabled(enabled) == CHIP_NO_ERROR);
134+
MatterReportingAttributeChangeCallback(GetEndpointId(), Chime::Id, Enabled ::Id);
135+
136+
return CHIP_NO_ERROR;
137+
}
138+
139+
void ChimeServer::InvokeCommand(HandlerContext & ctx)
140+
{
141+
switch (ctx.mRequestPath.mCommandId)
142+
{
143+
case Commands::PlayChimeSound::Id:
144+
CommandHandlerInterface::HandleCommand<Commands::PlayChimeSound::DecodableType>(
145+
ctx, [this](HandlerContext & ctx, const auto & req) { HandlePlayChimeSound(ctx, req); });
146+
break;
147+
}
148+
}
149+
150+
void ChimeServer::HandlePlayChimeSound(HandlerContext & ctx, const Commands::PlayChimeSound::DecodableType & req)
151+
{
152+
153+
ChipLogDetail(Zcl, "Chime: PlayChimeSound");
154+
155+
// call the delegate to play the chime
156+
Status status = mDelegate.playChimeSound();
157+
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
158+
}
159+
160+
} // namespace Clusters
161+
} // namespace app
162+
} // namespace chip
163+
164+
/** @brief Chime Cluster Server Init
165+
*
166+
* Server Init
167+
*
168+
*/
169+
void MatterChimePluginServerInitCallback(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 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/AttributeAccessInterface.h>
23+
#include <app/CommandHandlerInterface.h>
24+
#include <app/ConcreteAttributePath.h>
25+
#include <app/InteractionModelEngine.h>
26+
#include <app/MessageDef/StatusIB.h>
27+
#include <app/reporting/reporting.h>
28+
#include <app/util/attribute-storage.h>
29+
#include <lib/core/CHIPError.h>
30+
#include <protocols/interaction_model/StatusCode.h>
31+
32+
namespace chip {
33+
namespace app {
34+
namespace Clusters {
35+
36+
class ChimeDelegate;
37+
38+
39+
class ChimeServer : private AttributeAccessInterface, private CommandHandlerInterface
40+
{
41+
public:
42+
43+
ChimeServer(EndpointId endpointId, ChimeDelegate & delegate);
44+
~ChimeServer();
45+
46+
CHIP_ERROR Init();
47+
48+
private:
49+
ChimeDelegate & mDelegate;
50+
51+
EndpointId GetEndpointId() { return AttributeAccessInterface::GetEndpointId().Value(); }
52+
53+
// AttributeAccessInterface
54+
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
55+
CHIP_ERROR Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder) override;
56+
CHIP_ERROR SetActiveChimeSoundId(uint8_t chimeSoundId);
57+
CHIP_ERROR SetEnabled(bool enabled);
58+
59+
// CommandHandlerInterface
60+
void InvokeCommand(HandlerContext & ctx) override;
61+
62+
void HandlePlayChimeSound(HandlerContext & ctx, const Chime::Commands::PlayChimeSound::DecodableType & req);
63+
64+
};
65+
66+
/** @brief
67+
* Defines methods for implementing application-specific logic for the Chime Cluster.
68+
*/
69+
class ChimeDelegate
70+
{
71+
public:
72+
ChimeDelegate() = default;
73+
74+
virtual ~ChimeDelegate() = default;
75+
76+
// Get Attribute Methods
77+
virtual DataModel::List<const Chime::Structs::ChimeSoundStruct::Type>& GetInstalledChimeSounds() = 0;
78+
virtual uint8_t GetActiveChimeSoundId() = 0;
79+
virtual bool GetEnabled() = 0;
80+
81+
82+
// Set Attribute Methods
83+
virtual CHIP_ERROR SetActiveChimeSoundId(uint8_t chimeSoundId) = 0;
84+
virtual CHIP_ERROR SetEnabled(bool enabled) = 0;
85+
86+
// Commands
87+
/**
88+
* @brief Delegate should implement a handler to play the currently active chime sound.
89+
* It should report Status::Success if successful and may
90+
* return other Status codes if it fails
91+
*/
92+
virtual Protocols::InteractionModel::Status playChimeSound() = 0;
93+
94+
95+
96+
97+
private:
98+
friend class ChimeServer;
99+
100+
ChimeServer * mChimeServer = nullptr;
101+
102+
void SetChimeServer(ChimeServer * chimeServer) { mChimeServer = chimeServer; }
103+
104+
protected:
105+
ChimeServer * GetChimeServer() const { return mChimeServer; }
106+
};
107+
108+
} // namespace Clusters
109+
} // namespace app
110+
} // namespace chip
111+

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

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ CommandHandlerInterfaceOnlyClusters:
3636
- RVC Operational State
3737
- Sample MEI
3838
- Microwave Oven Control
39+
- Chime
3940
- Energy EVSE
4041
- Energy EVSE Mode
4142
- Device Energy Management
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Copyright (c) 2024 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+
<configurator>
18+
<domain name="CHIP"/>
19+
20+
<struct name="ChimeSoundStruct" apiMaturity="provisional">
21+
<cluster code="0x0556"/>
22+
<item name="ChimeId" type="int8u"/>
23+
<item name="Name" type="char_string" max="48"/>
24+
</struct>
25+
26+
<cluster apiMaturity="provisional">
27+
<name>Chime</name>
28+
<domain>Camera</domain>
29+
<code>0x0556</code>
30+
<define>CHIME_CLUSTER</define>
31+
<description>Attributes and commands to configure and play Chime sounds</description>
32+
33+
<!-- cluster revision -->
34+
<globalAttribute side="either" code="0xFFFD" value="1"/>
35+
36+
<attribute side="server" code="0x0000" define="CHIME_INSTALLED_CHIME_SOUNDS" type="array" entryType="ChimeSoundStruct" min="1" max="255" writable="false" optional="false">
37+
<description>InstalledChimeSounds</description>
38+
<access op="read" privilege="view"/>
39+
</attribute>
40+
<attribute side="server" code="0x0001" define="CHIME_ACTIVE_CHIME_SOUND_ID" type="int8u" default="0x00" optional="false">
41+
<description>ActiveChimeSoundId</description>
42+
<access op="read" privilege="view"/>
43+
<access op="write" privilege="operate"/>
44+
</attribute>
45+
<attribute side="server" code="0x0002" define="CHIME_ENABLED" type="boolean" default="1" optional="false">
46+
<description>Enabled</description>
47+
<access op="read" privilege="view"/>
48+
<access op="write" privilege="operate"/>
49+
</attribute>
50+
51+
<command source="client" code="0x00" name="PlayChimeSound" optional="false">
52+
<description>Plays the currently active chime sound.</description>
53+
<access op="invoke" privilege="operate"/>
54+
</command>
55+
56+
</cluster>
57+
</configurator>

src/app/zap-templates/zcl/zcl-with-test-extensions.json

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"bridged-device-basic-information.xml",
3030
"chip-ota.xml",
3131
"channel-cluster.xml",
32+
"chime-cluster.xml",
3233
"clusters-extensions.xml",
3334
"color-control-cluster.xml",
3435
"commissioner-control-cluster.xml",
@@ -191,6 +192,7 @@
191192
"MaxPathsPerInvoke"
192193
],
193194
"Bridged Device Basic Information": ["ProductAppearance"],
195+
"Chime": ["ActiveChimeSoundId", "Enabled"],
194196
"Descriptor": ["ClusterRevision", "FeatureMap"],
195197
"Device Energy Management": [
196198
"ESAType",

src/app/zap-templates/zcl/zcl.json

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"bridged-device-basic-information.xml",
2525
"chip-ota.xml",
2626
"channel-cluster.xml",
27+
"chime-cluster.xml",
2728
"clusters-extensions.xml",
2829
"color-control-cluster.xml",
2930
"commissioner-control-cluster.xml",
@@ -185,6 +186,7 @@
185186
"MaxPathsPerInvoke"
186187
],
187188
"Bridged Device Basic Information": ["ProductAppearance"],
189+
"Chime": ["ActiveChimeSoundId", "Enabled"],
188190
"Descriptor": ["ClusterRevision", "FeatureMap"],
189191
"Device Energy Management": [
190192
"ESAType",

0 commit comments

Comments
 (0)