Skip to content

Commit

Permalink
Using struct InputData&OuptutData to refine logics
Browse files Browse the repository at this point in the history
in Chef common/clusters/audio-output/AudioOutputManager.*
and common/clusters/media-input/MediaInputManager.*
  • Loading branch information
erwinpan1 committed Feb 16, 2024
1 parent 1218c75 commit f38e842
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 55 deletions.
35 changes: 15 additions & 20 deletions examples/chef/common/clusters/audio-output/AudioOutputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(i);
mOutputs.push_back(outputInfo);
}
mCurrentOutput = 1;
}

uint8_t AudioOutputManager::HandleGetCurrentOutput()
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand Down
24 changes: 20 additions & 4 deletions examples/chef/common/clusters/audio-output/AudioOutputManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutputInfoType> mOutputs;
// Magic numbers are here on purpose, please allocate memory
static constexpr size_t mNameLenMax = 32;
char mOutputName[10][mNameLenMax];
std::vector<struct OutputData> mOutputs;
};
48 changes: 22 additions & 26 deletions examples/chef/common/clusters/media-input/MediaInputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(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;
});
Expand All @@ -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;
Expand All @@ -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<uint16_t>(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<uint16_t>(inputData.inputType), (mCurrentInput == inputData.index ? 1 : 0),
inputData.name.c_str(), inputData.description.c_str());
}
return true;
}
Expand All @@ -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;
}
}
Expand Down
30 changes: 25 additions & 5 deletions examples/chef/common/clusters/media-input/MediaInputManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputInfoType> mInputs;
// Magic numbers are here on purpose, please allocate memory
static constexpr size_t mNameLenMax = 32;
char mInputName[10][mNameLenMax];
std::vector<InputData> mInputs;

private:
static constexpr int mTotalInput = 3;
};

0 comments on commit f38e842

Please sign in to comment.