From f38e84282e06c69e69c3bd558822aa67bb135b4d Mon Sep 17 00:00:00 2001 From: erwinpan1 Date: Fri, 16 Feb 2024 11:39:09 +0800 Subject: [PATCH] Using struct InputData&OuptutData to refine logics in Chef common/clusters/audio-output/AudioOutputManager.* and common/clusters/media-input/MediaInputManager.* --- .../audio-output/AudioOutputManager.cpp | 35 ++++++-------- .../audio-output/AudioOutputManager.h | 24 ++++++++-- .../media-input/MediaInputManager.cpp | 48 +++++++++---------- .../clusters/media-input/MediaInputManager.h | 30 ++++++++++-- 4 files changed, 82 insertions(+), 55 deletions(-) diff --git a/examples/chef/common/clusters/audio-output/AudioOutputManager.cpp b/examples/chef/common/clusters/audio-output/AudioOutputManager.cpp index cf1d387f9ce492..06a123a6549679 100644 --- a/examples/chef/common/clusters/audio-output/AudioOutputManager.cpp +++ b/examples/chef/common/clusters/audio-output/AudioOutputManager.cpp @@ -27,17 +27,14 @@ using chip::app::AttributeValueEncoder; AudioOutputManager::AudioOutputManager() { - const int totalOutput = 4; + struct OutputData outputData1(1, chip::app::Clusters::AudioOutput::OutputTypeEnum::kHdmi, "HDMI 1"); + mOutputs.push_back(outputData1); + struct OutputData outputData2(2, chip::app::Clusters::AudioOutput::OutputTypeEnum::kHdmi, "HDMI 2"); + mOutputs.push_back(outputData2); + struct OutputData outputData3(3, chip::app::Clusters::AudioOutput::OutputTypeEnum::kHdmi, "HDMI 3"); + mOutputs.push_back(outputData3); - for (uint8_t i = 1; i < totalOutput; ++i) - { - OutputInfoType outputInfo; - outputInfo.outputType = chip::app::Clusters::AudioOutput::OutputTypeEnum::kHdmi; - // note: safe only because of use of string literal - outputInfo.name = chip::CharSpan::fromCharString("HDMI"); - outputInfo.index = static_cast(i); - mOutputs.push_back(outputInfo); - } + mCurrentOutput = 1; } uint8_t AudioOutputManager::HandleGetCurrentOutput() @@ -48,23 +45,21 @@ uint8_t AudioOutputManager::HandleGetCurrentOutput() CHIP_ERROR AudioOutputManager::HandleGetOutputList(AttributeValueEncoder & aEncoder) { return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR { - for (auto const & outputInfo : this->mOutputs) + for (auto const & outputData : mOutputs) { - ReturnErrorOnFailure(encoder.Encode(outputInfo)); + ReturnErrorOnFailure(encoder.Encode(outputData.GetEncodable())); } return CHIP_NO_ERROR; }); } -bool AudioOutputManager::HandleRenameOutput(const uint8_t & index, const chip::CharSpan & name) +bool AudioOutputManager::HandleRenameOutput(const uint8_t & index, const chip::CharSpan & newName) { - for (OutputInfoType & output : mOutputs) + for (auto & outputData : mOutputs) { - if (output.index == index) + if (outputData.index == index) { - const size_t len = std::min(mNameLenMax, name.size()); - memcpy(mOutputName[index], name.data(), len); - output.name = mOutputName[index]; + outputData.Rename(newName); return true; } } @@ -74,9 +69,9 @@ bool AudioOutputManager::HandleRenameOutput(const uint8_t & index, const chip::C bool AudioOutputManager::HandleSelectOutput(const uint8_t & index) { - for (OutputInfoType & output : mOutputs) + for (auto & outputData : mOutputs) { - if (output.index == index) + if (outputData.index == index) { mCurrentOutput = index; return true; diff --git a/examples/chef/common/clusters/audio-output/AudioOutputManager.h b/examples/chef/common/clusters/audio-output/AudioOutputManager.h index 8a8f5e791763e2..5f9157de773ee7 100644 --- a/examples/chef/common/clusters/audio-output/AudioOutputManager.h +++ b/examples/chef/common/clusters/audio-output/AudioOutputManager.h @@ -33,10 +33,26 @@ class AudioOutputManager : public chip::app::Clusters::AudioOutput::Delegate bool HandleRenameOutput(const uint8_t & index, const chip::CharSpan & name) override; bool HandleSelectOutput(const uint8_t & index) override; + struct OutputData + { + uint8_t index; + chip::app::Clusters::AudioOutput::OutputTypeEnum outputType; + std::string name; + + OutputData(uint8_t i, chip::app::Clusters::AudioOutput::OutputTypeEnum t, const char * n) : index(i), outputType(t), name(n) + {} + void Rename(const chip::CharSpan & newName) { name.assign(newName.data(), newName.size()); } + OutputInfoType GetEncodable() const + { + OutputInfoType result; + result.index = index; + result.outputType = outputType; + result.name = chip::CharSpan::fromCharString(name.c_str()); + return result; + } + }; + protected: uint8_t mCurrentOutput = 1; - std::vector mOutputs; - // Magic numbers are here on purpose, please allocate memory - static constexpr size_t mNameLenMax = 32; - char mOutputName[10][mNameLenMax]; + std::vector mOutputs; }; diff --git a/examples/chef/common/clusters/media-input/MediaInputManager.cpp b/examples/chef/common/clusters/media-input/MediaInputManager.cpp index af8bc484c5e2f5..f7e853cc46a40b 100644 --- a/examples/chef/common/clusters/media-input/MediaInputManager.cpp +++ b/examples/chef/common/clusters/media-input/MediaInputManager.cpp @@ -25,25 +25,25 @@ using namespace chip::app::Clusters::MediaInput; MediaInputManager::MediaInputManager() { - mCurrentInput = 1; + struct InputData inputData1(1, chip::app::Clusters::MediaInput::InputTypeEnum::kHdmi, "HDMI 1", + "High-Definition Multimedia Interface"); + mInputs.push_back(inputData1); + struct InputData inputData2(2, chip::app::Clusters::MediaInput::InputTypeEnum::kHdmi, "HDMI 2", + "High-Definition Multimedia Interface"); + mInputs.push_back(inputData2); + struct InputData inputData3(3, chip::app::Clusters::MediaInput::InputTypeEnum::kHdmi, "HDMI 3", + "High-Definition Multimedia Interface"); + mInputs.push_back(inputData3); - for (int i = 1; i < mTotalInput; ++i) - { - InputInfoType inputInfo; - inputInfo.description = chip::CharSpan::fromCharString("High-Definition Multimedia Interface"); - inputInfo.name = chip::CharSpan::fromCharString("HDMI"); - inputInfo.inputType = chip::app::Clusters::MediaInput::InputTypeEnum::kHdmi; - inputInfo.index = static_cast(i); - mInputs.push_back(inputInfo); - } + mCurrentInput = 1; } CHIP_ERROR MediaInputManager::HandleGetInputList(chip::app::AttributeValueEncoder & aEncoder) { return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR { - for (auto const & inputInfo : this->mInputs) + for (auto const & inputData : this->mInputs) { - ReturnErrorOnFailure(encoder.Encode(inputInfo)); + ReturnErrorOnFailure(encoder.Encode(inputData.GetEncodable())); } return CHIP_NO_ERROR; }); @@ -56,9 +56,9 @@ uint8_t MediaInputManager::HandleGetCurrentInput() bool MediaInputManager::HandleSelectInput(const uint8_t index) { - for (InputInfoType & input : mInputs) + for (auto const & inputData : mInputs) { - if (input.index == index) + if (inputData.index == index) { mCurrentInput = index; return true; @@ -71,13 +71,11 @@ bool MediaInputManager::HandleSelectInput(const uint8_t index) bool MediaInputManager::HandleShowInputStatus() { ChipLogProgress(Zcl, " MediaInputManager::HandleShowInputStatus()"); - for (auto const & inputInfo : this->mInputs) + for (auto const & inputData : mInputs) { - string name(inputInfo.name.data(), inputInfo.name.size()); - string desc(inputInfo.description.data(), inputInfo.description.size()); - ChipLogProgress(Zcl, " [%d] type=%d selected=%d name=%s desc=%s", inputInfo.index, - static_cast(inputInfo.inputType), (mCurrentInput == inputInfo.index ? 1 : 0), name.c_str(), - desc.c_str()); + ChipLogProgress(Zcl, " [%d] type=%d selected=%d name=%s desc=%s", inputData.index, + static_cast(inputData.inputType), (mCurrentInput == inputData.index ? 1 : 0), + inputData.name.c_str(), inputData.description.c_str()); } return true; } @@ -88,15 +86,13 @@ bool MediaInputManager::HandleHideInputStatus() return true; } -bool MediaInputManager::HandleRenameInput(const uint8_t index, const chip::CharSpan & name) +bool MediaInputManager::HandleRenameInput(const uint8_t index, const chip::CharSpan & newName) { - for (InputInfoType & input : mInputs) + for (auto & inputData : mInputs) { - if (input.index == index) + if (inputData.index == index) { - const size_t len = std::min(mNameLenMax, name.size()); - memcpy(mInputName[index], name.data(), len); - input.name = mInputName[index]; + inputData.Rename(newName); return true; } } diff --git a/examples/chef/common/clusters/media-input/MediaInputManager.h b/examples/chef/common/clusters/media-input/MediaInputManager.h index cc66af301aeeb2..104db65e2bbe97 100644 --- a/examples/chef/common/clusters/media-input/MediaInputManager.h +++ b/examples/chef/common/clusters/media-input/MediaInputManager.h @@ -36,13 +36,33 @@ class MediaInputManager : public chip::app::Clusters::MediaInput::Delegate bool HandleHideInputStatus() override; bool HandleRenameInput(const uint8_t index, const chip::CharSpan & name) override; + struct InputData + { + uint8_t index; + chip::app::Clusters::MediaInput::InputTypeEnum inputType; + std::string name; + std::string description; + + InputData(uint8_t i, chip::app::Clusters::MediaInput::InputTypeEnum t, const char * n, const char * d) : + index(i), inputType(t), name(n), description(d) + {} + + void Rename(const chip::CharSpan & newName) { name.assign(newName.data(), newName.size()); } + + InputInfoType GetEncodable() const + { + InputInfoType result; + result.index = index; + result.inputType = inputType; + result.name = chip::CharSpan::fromCharString(name.c_str()); + result.description = chip::CharSpan::fromCharString(description.c_str()); + return result; + } + }; + protected: uint8_t mCurrentInput; - std::vector mInputs; - // Magic numbers are here on purpose, please allocate memory - static constexpr size_t mNameLenMax = 32; - char mInputName[10][mNameLenMax]; + std::vector mInputs; private: - static constexpr int mTotalInput = 3; };