forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchime-server.cpp
185 lines (154 loc) · 5.72 KB
/
chime-server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
*
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/****************************************************************************'
* @file
* @brief Implementation for the Chime Server Cluster
***************************************************************************/
#include "chime-server.h"
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/CommandHandlerInterfaceRegistry.h>
#include <app/ConcreteAttributePath.h>
#include <app/InteractionModelEngine.h>
#include <app/util/attribute-storage.h>
#include <app/util/util.h>
#include <protocols/interaction_model/StatusCode.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::DataModel;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::Chime;
using namespace chip::app::Clusters::Chime::Attributes;
using chip::Protocols::InteractionModel::Status;
namespace chip {
namespace app {
namespace Clusters {
ChimeServer::ChimeServer(EndpointId endpointId, ChimeDelegate & delegate) :
AttributeAccessInterface(MakeOptional(endpointId), Chime::Id), CommandHandlerInterface(MakeOptional(endpointId), Chime::Id),
mDelegate(delegate)
{
mDelegate.SetChimeServer(this);
}
ChimeServer::~ChimeServer()
{
AttributeAccessInterfaceRegistry::Instance().Unregister(this);
CommandHandlerInterfaceRegistry::Instance().UnregisterCommandHandler(this);
}
CHIP_ERROR ChimeServer::Init()
{
VerifyOrReturnError(AttributeAccessInterfaceRegistry::Instance().Register(this), CHIP_ERROR_INTERNAL);
ReturnErrorOnFailure(CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(this));
return CHIP_NO_ERROR;
}
// AttributeAccessInterface
CHIP_ERROR ChimeServer::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
VerifyOrDie(aPath.mClusterId == Chime::Id);
switch (aPath.mAttributeId)
{
case InstalledChimeSounds::Id:
return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
uint8_t index = 0;
CHIP_ERROR err = CHIP_NO_ERROR;
// runs through all the Chime sounds available one by one
do
{
Chime::Structs::ChimeSoundStruct::Type chimeSound;
if ((err = this->mDelegate.GetChimeSoundByIndex(index, chimeSound)) == CHIP_NO_ERROR)
{
ReturnErrorOnFailure(encoder.Encode(chimeSound));
index++;
}
} while (err == CHIP_NO_ERROR);
if (err == CHIP_ERROR_NOT_FOUND)
{
return CHIP_NO_ERROR;
}
return err;
});
case ActiveChimeID::Id:
return aEncoder.Encode(mDelegate.GetActiveChimeId());
case Enabled::Id:
return aEncoder.Encode(mDelegate.GetEnabled());
}
return CHIP_NO_ERROR;
}
CHIP_ERROR ChimeServer::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
{
VerifyOrDie(aPath.mClusterId == Chime::Id);
switch (aPath.mAttributeId)
{
case ActiveChimeID::Id: {
uint8_t newValue;
ReturnErrorOnFailure(aDecoder.Decode(newValue));
ReturnErrorOnFailure(mDelegate.SetActiveChimeId(newValue));
return CHIP_NO_ERROR;
}
case Enabled::Id: {
bool newValue;
ReturnErrorOnFailure(aDecoder.Decode(newValue));
ReturnErrorOnFailure(mDelegate.SetEnabled(newValue));
return CHIP_NO_ERROR;
}
default:
// Unknown attribute
return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute);
}
}
CHIP_ERROR ChimeServer::SetActiveChimeId(uint8_t soundId)
{
uint8_t currentSoundId = mDelegate.GetActiveChimeId();
bool activeIdChanged = !(currentSoundId == soundId);
VerifyOrReturnError(activeIdChanged, CHIP_NO_ERROR);
VerifyOrDie(mDelegate.SetActiveChimeId(soundId) == CHIP_NO_ERROR);
MatterReportingAttributeChangeCallback(GetEndpointId(), Chime::Id, ActiveChimeID::Id);
return CHIP_NO_ERROR;
}
CHIP_ERROR ChimeServer::SetEnabled(bool enabled)
{
bool currentlyEnabled = mDelegate.GetEnabled();
bool enableChanged = !(currentlyEnabled == enabled);
VerifyOrReturnError(enableChanged, CHIP_NO_ERROR);
VerifyOrDie(mDelegate.SetEnabled(enabled) == CHIP_NO_ERROR);
MatterReportingAttributeChangeCallback(GetEndpointId(), Chime::Id, Enabled::Id);
return CHIP_NO_ERROR;
}
void ChimeServer::InvokeCommand(HandlerContext & ctx)
{
switch (ctx.mRequestPath.mCommandId)
{
case Commands::PlayChimeSound::Id:
CommandHandlerInterface::HandleCommand<Commands::PlayChimeSound::DecodableType>(
ctx, [this](HandlerContext & ctx, const auto & req) { HandlePlayChimeSound(ctx, req); });
break;
}
}
void ChimeServer::HandlePlayChimeSound(HandlerContext & ctx, const Commands::PlayChimeSound::DecodableType & req)
{
ChipLogDetail(Zcl, "Chime: PlayChimeSound");
// call the delegate to play the chime
Status status = mDelegate.playChimeSound();
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
} // namespace Clusters
} // namespace app
} // namespace chip
/** @brief Chime Cluster Server Init
*
* Server Init
*
*/
void MatterChimePluginServerInitCallback() {}