Skip to content

Commit

Permalink
Simplify the ChannelManager.cpp codes
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinpan1 committed Feb 10, 2024
1 parent 9b7d9d7 commit f45529a
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions examples/chef/common/clusters/channel/ChannelManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,26 @@ bool isChannelMatched(const ChannelInfoType & channel, const CharSpan & match)

void ChannelManager::HandleChangeChannel(CommandResponseHelper<ChangeChannelResponseType> & helper, const CharSpan & match)
{
std::vector<ChannelInfoType> matchedChannels;
uint16_t iFirstMatchedChannel = 0;
for (auto const & channel : mChannels)
int iMatchedChannel = -1;
ChangeChannelResponseType response;

for (uint16_t i = 0; i < mChannels.size(); i++)
{
// verify if CharSpan matches channel name
// or callSign or affiliateCallSign or majorNumber.minorNumber
if (isChannelMatched(channel, match))
if (isChannelMatched(mChannels[i], match))
{
matchedChannels.push_back(channel);
}
else if (matchedChannels.empty())
{
iFirstMatchedChannel++;
}
if (iMatchedChannel != -1) {
// Error: Found multiple matches
response.status = chip::app::Clusters::Channel::StatusEnum::kMultipleMatches;
helper.Success(response);
return;
}
iMatchedChannel = i;
}
}

ChangeChannelResponseType response;

// Error: Found multiple matches
if (matchedChannels.size() > 1)
{
response.status = chip::app::Clusters::Channel::StatusEnum::kMultipleMatches;
helper.Success(response);
}
else if (matchedChannels.empty())
if (iMatchedChannel == -1)
{
// Error: Found no match
response.status = chip::app::Clusters::Channel::StatusEnum::kNoMatches;
Expand All @@ -185,8 +180,8 @@ void ChannelManager::HandleChangeChannel(CommandResponseHelper<ChangeChannelResp
{
response.status = chip::app::Clusters::Channel::StatusEnum::kSuccess;
response.data = chip::MakeOptional(CharSpan::fromCharString("data response"));
mCurrentChannel = matchedChannels[0];
mCurrentChannelIndex = iFirstMatchedChannel;
mCurrentChannel = mChannels[iMatchedChannel];
mCurrentChannelIndex = iMatchedChannel;
helper.Success(response);
}
}
Expand All @@ -206,6 +201,7 @@ bool ChannelManager::HandleChangeChannelByNumber(const uint16_t & majorNumber, c
channelChanged = true;
mCurrentChannelIndex = index;
mCurrentChannel = channel;
return channelChanged;
}
}
index++;
Expand Down

0 comments on commit f45529a

Please sign in to comment.