Skip to content

Commit

Permalink
Move all attributes logic to Chef application
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinpan1 committed Aug 20, 2024
1 parent 6f42e70 commit 97b8560
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
33 changes: 28 additions & 5 deletions examples/chef/common/clusters/media-input/MediaInputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
*/

#include <app/util/config.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <map>

#ifdef MATTER_DM_PLUGIN_MEDIA_INPUT_SERVER
#include "MediaInputManager.h"

using namespace std;
using namespace chip;
using namespace chip::app::Clusters::MediaInput;
using Protocols::InteractionModel::Status;

MediaInputManager::MediaInputManager()
MediaInputManager::MediaInputManager(chip::EndpointId endpoint):mEndpoint(endpoint)
{
struct InputData inputData1(1, chip::app::Clusters::MediaInput::InputTypeEnum::kHdmi, "HDMI 1",
"High-Definition Multimedia Interface");
Expand All @@ -35,7 +39,13 @@ MediaInputManager::MediaInputManager()
"High-Definition Multimedia Interface");
mInputs.push_back(inputData3);

mCurrentInput = 1;
// Sync the attributes from delegate
Status status = Attributes::CurrentInput::Get(endpoint, &mCurrentInput);

if (Status::Success != status) {
ChipLogError(Zcl, "Unable to save CurrentInput attribute ");
mCurrentInput = 1;
}
}

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

bool MediaInputManager::HandleSelectInput(const uint8_t index)
{
if (mCurrentInput == index) {
ChipLogProgress(Zcl, "CurrentInput is same as new value: %u", index);
return true;
}
for (auto const & inputData : mInputs)
{
if (inputData.index == index)
{
mCurrentInput = index;
// Sync the CurrentInput to attribute storage while reporting changes
Status status = chip::app::Clusters::MediaInput::Attributes::CurrentInput::Set(mEndpoint, index);
if (Status::Success != status) {
ChipLogError(Zcl, "CurrentInput is not stored successfully");
}
return true;
}
}
Expand Down Expand Up @@ -100,10 +119,14 @@ bool MediaInputManager::HandleRenameInput(const uint8_t index, const chip::CharS
return false;
}

static MediaInputManager mediaInputManager;
static std::map<chip::EndpointId, std::unique_ptr<MediaInputManager>> gMediaInputManagerInstance{};
void emberAfMediaInputClusterInitCallback(EndpointId endpoint)
{
ChipLogProgress(Zcl, "TV Linux App: MediaInput::SetDefaultDelegate");
chip::app::Clusters::MediaInput::SetDefaultDelegate(endpoint, &mediaInputManager);
ChipLogProgress(Zcl, "TV Linux App: MediaInput::SetDefaultDelegate, endpoint=%x", endpoint);

gMediaInputManagerInstance[endpoint] = std::make_unique<MediaInputManager>(endpoint);

chip::app::Clusters::MediaInput::SetDefaultDelegate(endpoint, gMediaInputManagerInstance[endpoint].get());

}
#endif // MATTER_DM_PLUGIN_MEDIA_INPUT_SERVER
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MediaInputManager : public chip::app::Clusters::MediaInput::Delegate
using InputInfoType = chip::app::Clusters::MediaInput::Structs::InputInfoStruct::Type;

public:
MediaInputManager();
MediaInputManager(chip::EndpointId endpoint);

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

protected:
chip::EndpointId mEndpoint;
uint8_t mCurrentInput;
std::vector<InputData> mInputs;

Expand Down
1 change: 0 additions & 1 deletion examples/common/pigweed/rpc_services/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class Attributes : public pw_rpc::nanopb::Attributes::Service<Attributes>
default:
return pw::Status::InvalidArgument();
}

RETURN_STATUS_IF_NOT_OK(
emberAfWriteAttribute(request.metadata.endpoint, request.metadata.cluster, request.metadata.attribute_id,
const_cast<uint8_t *>(static_cast<const uint8_t *>(data)), request.metadata.type));
Expand Down
24 changes: 4 additions & 20 deletions src/app/clusters/media-input-server/media-input-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include <app/CommandHandler.h>
#include <app/ConcreteCommandPath.h>
#include <app/data-model/Encode.h>
#include <app/data-model/Decode.h>
#include <app/data-model/Nullable.h>
#include <app/util/attribute-storage.h>
#include <app/util/config.h>
#include <platform/CHIPDeviceConfig.h>
Expand Down Expand Up @@ -84,12 +82,6 @@ void SetDefaultDelegate(EndpointId endpoint, Delegate * delegate)
if (ep < kMediaInputDelegateTableSize)
{
gDelegateTable[ep] = delegate;
// Sync the attributes from delegate
Status status = Attributes::CurrentInput::Set(endpoint, delegate->HandleGetCurrentInput());

if (Status::Success != status) {
ChipLogError(Zcl, "Unable to save CurrentInput attribute ");
}
}
else
{
Expand Down Expand Up @@ -164,7 +156,7 @@ CHIP_ERROR MediaInputAttrAccess::Read(const app::ConcreteReadAttributePath & aPa

return CHIP_NO_ERROR;
}

CHIP_ERROR MediaInputAttrAccess::ReadInputListAttribute(app::AttributeValueEncoder & aEncoder, Delegate * delegate)
{
return delegate->HandleGetInputList(aEncoder);
Expand All @@ -189,22 +181,14 @@ bool emberAfMediaInputClusterSelectInputCallback(app::CommandHandler * command,
Status status = Status::Failure;

auto & input = commandData.index;
uint8_t currentInput = 0;

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

currentInput = delegate->HandleGetCurrentInput();

if (currentInput == input) {
ChipLogProgress(Zcl, "Endpoint %x CurrentInput already set to new value: %u", endpoint, input);
} else {
VerifyOrExit(delegate->HandleSelectInput(input), err = CHIP_ERROR_INVALID_ARGUMENT);
// Sync attribute to storage
VerifyOrExit(Status::Success == (status = chip::app::Clusters::MediaInput::Attributes::CurrentInput::Set(endpoint, input)), err = CHIP_ERROR_INTERNAL);
ChipLogProgress(Zcl, "Endpoint %x CurrentInput set to new value: %u successfully", endpoint, input);
if (!delegate->HandleSelectInput(input))
{
status = Status::Failure;
}
status = Status::Success;

exit:
if (err != CHIP_NO_ERROR)
Expand Down

0 comments on commit 97b8560

Please sign in to comment.