From c6f9a0d664aada79b685a2ebd3e1cf59d945e43e Mon Sep 17 00:00:00 2001 From: ex_liangpy4 Date: Fri, 15 Dec 2023 19:38:28 +0800 Subject: [PATCH] add api for ModeBase and updated MicrowaveOvenControl --- .../include/microwave-oven-device.h | 2 +- .../microwave-oven-control-server.cpp | 8 +++++++- .../mode-base-server/mode-base-server.cpp | 20 +++++++++++++++++++ .../mode-base-server/mode-base-server.h | 3 +++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/examples/microwave-oven-app/microwave-oven-common/include/microwave-oven-device.h b/examples/microwave-oven-app/microwave-oven-common/include/microwave-oven-device.h index c5543c957b77d1..a3f0bd32831f73 100644 --- a/examples/microwave-oven-app/microwave-oven-common/include/microwave-oven-device.h +++ b/examples/microwave-oven-app/microwave-oven-common/include/microwave-oven-device.h @@ -54,7 +54,7 @@ class ExampleMicrowaveOvenDevice : public MicrowaveOvenControl::Delegate, * @param aClustersEndpoint The endpoint ID where all the microwave oven clusters exist. */ explicit ExampleMicrowaveOvenDevice(EndpointId aClustersEndpoint) : - mOperationalStateInstance(this, aClustersEndpoint, OperationalState::Id), + mOperationalStateInstance(this, aClustersEndpoint), mMicrowaveOvenModeInstance(this, aClustersEndpoint, MicrowaveOvenMode::Id, 0), mMicrowaveOvenControlInstance(this, aClustersEndpoint, MicrowaveOvenControl::Id, mOperationalStateInstance, mMicrowaveOvenModeInstance) diff --git a/src/app/clusters/microwave-oven-control-server/microwave-oven-control-server.cpp b/src/app/clusters/microwave-oven-control-server/microwave-oven-control-server.cpp index d231865e67d2f5..67b22fe78597d8 100644 --- a/src/app/clusters/microwave-oven-control-server/microwave-oven-control-server.cpp +++ b/src/app/clusters/microwave-oven-control-server/microwave-oven-control-server.cpp @@ -159,6 +159,7 @@ void Instance::HandleSetCookingParameters(HandlerContext & ctx, const Commands:: ChipLogDetail(Zcl, "Microwave Oven Control: HandleSetCookingParameters"); Status status; uint8_t opState; + uint8_t modeValue; uint8_t reqCookMode; uint32_t reqCookTime; uint8_t reqPowerSetting; @@ -172,7 +173,12 @@ void Instance::HandleSetCookingParameters(HandlerContext & ctx, const Commands:: VerifyOrExit(cookMode.HasValue() || cookTime.HasValue() || powerSetting.HasValue(), status = Status::InvalidCommand; ChipLogError(Zcl, "Microwave Oven Control: Failed to set cooking parameters, all command fields are missing ")); - reqCookMode = cookMode.ValueOr(to_underlying(MicrowaveOvenMode::ModeTag::kNormal) - kDerivedModeTag); + modeValue = 0; + VerifyOrExit(mMicrowaveOvenModeInstance.GetModeValueByModeTag(to_underlying(MicrowaveOvenMode::ModeTag::kNormal), modeValue) == CHIP_NO_ERROR, + status = Status::InvalidCommand; + ChipLogError(Zcl, "Microwave Oven Control: Failed to set cookMode, Normal mode is not found")); + + reqCookMode = cookMode.ValueOr(modeValue); VerifyOrExit(mMicrowaveOvenModeInstance.IsSupportedMode(reqCookMode), status = Status::InvalidCommand; ChipLogError(Zcl, "Microwave Oven Control: Failed to set cookMode, cookMode is not supported")); diff --git a/src/app/clusters/mode-base-server/mode-base-server.cpp b/src/app/clusters/mode-base-server/mode-base-server.cpp index 2a86f1e1ba4dbf..b8eba954de4198 100644 --- a/src/app/clusters/mode-base-server/mode-base-server.cpp +++ b/src/app/clusters/mode-base-server/mode-base-server.cpp @@ -259,6 +259,26 @@ bool Instance::IsSupportedMode(uint8_t modeValue) return false; } +CHIP_ERROR Instance::GetModeValueByModeTag(uint16_t modeTagValue, uint8_t & value) +{ + ModeTagStructType tagsBuffer[kMaxNumOfModeTags]; + DataModel::List mTags(tagsBuffer); + for (uint8_t i = 0; mDelegate->GetModeTagsByIndex(i, mTags) != CHIP_ERROR_PROVIDER_LIST_EXHAUSTED; i++) + { + for(uint8_t ii = 0; ii < mTags.size() ;ii++) + { + if (mTags[ii].value == modeTagValue) + { + mDelegate->GetModeValueByIndex(i, value); + return CHIP_NO_ERROR; + } + } + mTags = tagsBuffer; + } + ChipLogDetail(Zcl, "Cannot find a mode with mode tag %x", modeTagValue); + return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED; +} + // private methods template void Instance::HandleCommand(HandlerContext & handlerContext, FuncT func) diff --git a/src/app/clusters/mode-base-server/mode-base-server.h b/src/app/clusters/mode-base-server/mode-base-server.h index 256670d09341a3..bf7b82a79bf169 100644 --- a/src/app/clusters/mode-base-server/mode-base-server.h +++ b/src/app/clusters/mode-base-server/mode-base-server.h @@ -124,6 +124,9 @@ class Instance : public CommandHandlerInterface, public AttributeAccessInterface // Unregisters this instance if already registered. void Shutdown(); + // Get mode value by mode tag + CHIP_ERROR GetModeValueByModeTag(uint16_t modeTag, uint8_t & value); + private: Delegate * mDelegate;