Skip to content

Commit 97b8560

Browse files
committed
Move all attributes logic to Chef application
1 parent 6f42e70 commit 97b8560

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

examples/chef/common/clusters/media-input/MediaInputManager.cpp

+28-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616
*/
1717

1818
#include <app/util/config.h>
19+
#include <app-common/zap-generated/attributes/Accessors.h>
20+
#include <map>
21+
1922
#ifdef MATTER_DM_PLUGIN_MEDIA_INPUT_SERVER
2023
#include "MediaInputManager.h"
2124

2225
using namespace std;
2326
using namespace chip;
2427
using namespace chip::app::Clusters::MediaInput;
28+
using Protocols::InteractionModel::Status;
2529

26-
MediaInputManager::MediaInputManager()
30+
MediaInputManager::MediaInputManager(chip::EndpointId endpoint):mEndpoint(endpoint)
2731
{
2832
struct InputData inputData1(1, chip::app::Clusters::MediaInput::InputTypeEnum::kHdmi, "HDMI 1",
2933
"High-Definition Multimedia Interface");
@@ -35,7 +39,13 @@ MediaInputManager::MediaInputManager()
3539
"High-Definition Multimedia Interface");
3640
mInputs.push_back(inputData3);
3741

38-
mCurrentInput = 1;
42+
// Sync the attributes from delegate
43+
Status status = Attributes::CurrentInput::Get(endpoint, &mCurrentInput);
44+
45+
if (Status::Success != status) {
46+
ChipLogError(Zcl, "Unable to save CurrentInput attribute ");
47+
mCurrentInput = 1;
48+
}
3949
}
4050

4151
CHIP_ERROR MediaInputManager::HandleGetInputList(chip::app::AttributeValueEncoder & aEncoder)
@@ -56,11 +66,20 @@ uint8_t MediaInputManager::HandleGetCurrentInput()
5666

5767
bool MediaInputManager::HandleSelectInput(const uint8_t index)
5868
{
69+
if (mCurrentInput == index) {
70+
ChipLogProgress(Zcl, "CurrentInput is same as new value: %u", index);
71+
return true;
72+
}
5973
for (auto const & inputData : mInputs)
6074
{
6175
if (inputData.index == index)
6276
{
6377
mCurrentInput = index;
78+
// Sync the CurrentInput to attribute storage while reporting changes
79+
Status status = chip::app::Clusters::MediaInput::Attributes::CurrentInput::Set(mEndpoint, index);
80+
if (Status::Success != status) {
81+
ChipLogError(Zcl, "CurrentInput is not stored successfully");
82+
}
6483
return true;
6584
}
6685
}
@@ -100,10 +119,14 @@ bool MediaInputManager::HandleRenameInput(const uint8_t index, const chip::CharS
100119
return false;
101120
}
102121

103-
static MediaInputManager mediaInputManager;
122+
static std::map<chip::EndpointId, std::unique_ptr<MediaInputManager>> gMediaInputManagerInstance{};
104123
void emberAfMediaInputClusterInitCallback(EndpointId endpoint)
105124
{
106-
ChipLogProgress(Zcl, "TV Linux App: MediaInput::SetDefaultDelegate");
107-
chip::app::Clusters::MediaInput::SetDefaultDelegate(endpoint, &mediaInputManager);
125+
ChipLogProgress(Zcl, "TV Linux App: MediaInput::SetDefaultDelegate, endpoint=%x", endpoint);
126+
127+
gMediaInputManagerInstance[endpoint] = std::make_unique<MediaInputManager>(endpoint);
128+
129+
chip::app::Clusters::MediaInput::SetDefaultDelegate(endpoint, gMediaInputManagerInstance[endpoint].get());
130+
108131
}
109132
#endif // MATTER_DM_PLUGIN_MEDIA_INPUT_SERVER

examples/chef/common/clusters/media-input/MediaInputManager.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class MediaInputManager : public chip::app::Clusters::MediaInput::Delegate
2929
using InputInfoType = chip::app::Clusters::MediaInput::Structs::InputInfoStruct::Type;
3030

3131
public:
32-
MediaInputManager();
32+
MediaInputManager(chip::EndpointId endpoint);
3333

3434
CHIP_ERROR HandleGetInputList(chip::app::AttributeValueEncoder & aEncoder) override;
3535
uint8_t HandleGetCurrentInput() override;
@@ -63,6 +63,7 @@ class MediaInputManager : public chip::app::Clusters::MediaInput::Delegate
6363
};
6464

6565
protected:
66+
chip::EndpointId mEndpoint;
6667
uint8_t mCurrentInput;
6768
std::vector<InputData> mInputs;
6869

examples/common/pigweed/rpc_services/Attributes.h

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class Attributes : public pw_rpc::nanopb::Attributes::Service<Attributes>
7373
default:
7474
return pw::Status::InvalidArgument();
7575
}
76-
7776
RETURN_STATUS_IF_NOT_OK(
7877
emberAfWriteAttribute(request.metadata.endpoint, request.metadata.cluster, request.metadata.attribute_id,
7978
const_cast<uint8_t *>(static_cast<const uint8_t *>(data)), request.metadata.type));

src/app/clusters/media-input-server/media-input-server.cpp

+4-20
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
#include <app/CommandHandler.h>
3131
#include <app/ConcreteCommandPath.h>
3232
#include <app/data-model/Encode.h>
33-
#include <app/data-model/Decode.h>
34-
#include <app/data-model/Nullable.h>
3533
#include <app/util/attribute-storage.h>
3634
#include <app/util/config.h>
3735
#include <platform/CHIPDeviceConfig.h>
@@ -84,12 +82,6 @@ void SetDefaultDelegate(EndpointId endpoint, Delegate * delegate)
8482
if (ep < kMediaInputDelegateTableSize)
8583
{
8684
gDelegateTable[ep] = delegate;
87-
// Sync the attributes from delegate
88-
Status status = Attributes::CurrentInput::Set(endpoint, delegate->HandleGetCurrentInput());
89-
90-
if (Status::Success != status) {
91-
ChipLogError(Zcl, "Unable to save CurrentInput attribute ");
92-
}
9385
}
9486
else
9587
{
@@ -164,7 +156,7 @@ CHIP_ERROR MediaInputAttrAccess::Read(const app::ConcreteReadAttributePath & aPa
164156

165157
return CHIP_NO_ERROR;
166158
}
167-
159+
168160
CHIP_ERROR MediaInputAttrAccess::ReadInputListAttribute(app::AttributeValueEncoder & aEncoder, Delegate * delegate)
169161
{
170162
return delegate->HandleGetInputList(aEncoder);
@@ -189,22 +181,14 @@ bool emberAfMediaInputClusterSelectInputCallback(app::CommandHandler * command,
189181
Status status = Status::Failure;
190182

191183
auto & input = commandData.index;
192-
uint8_t currentInput = 0;
193184

194185
Delegate * delegate = GetDelegate(endpoint);
195186
VerifyOrExit(isDelegateNull(delegate, endpoint) != true, err = CHIP_ERROR_INCORRECT_STATE);
196187

197-
currentInput = delegate->HandleGetCurrentInput();
198-
199-
if (currentInput == input) {
200-
ChipLogProgress(Zcl, "Endpoint %x CurrentInput already set to new value: %u", endpoint, input);
201-
} else {
202-
VerifyOrExit(delegate->HandleSelectInput(input), err = CHIP_ERROR_INVALID_ARGUMENT);
203-
// Sync attribute to storage
204-
VerifyOrExit(Status::Success == (status = chip::app::Clusters::MediaInput::Attributes::CurrentInput::Set(endpoint, input)), err = CHIP_ERROR_INTERNAL);
205-
ChipLogProgress(Zcl, "Endpoint %x CurrentInput set to new value: %u successfully", endpoint, input);
188+
if (!delegate->HandleSelectInput(input))
189+
{
190+
status = Status::Failure;
206191
}
207-
status = Status::Success;
208192

209193
exit:
210194
if (err != CHIP_NO_ERROR)

0 commit comments

Comments
 (0)