Skip to content

Commit f45529a

Browse files
committed
Simplify the ChannelManager.cpp codes
1 parent 9b7d9d7 commit f45529a

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

examples/chef/common/clusters/channel/ChannelManager.cpp

+17-21
Original file line numberDiff line numberDiff line change
@@ -151,31 +151,26 @@ bool isChannelMatched(const ChannelInfoType & channel, const CharSpan & match)
151151

152152
void ChannelManager::HandleChangeChannel(CommandResponseHelper<ChangeChannelResponseType> & helper, const CharSpan & match)
153153
{
154-
std::vector<ChannelInfoType> matchedChannels;
155-
uint16_t iFirstMatchedChannel = 0;
156-
for (auto const & channel : mChannels)
154+
int iMatchedChannel = -1;
155+
ChangeChannelResponseType response;
156+
157+
for (uint16_t i = 0; i < mChannels.size(); i++)
157158
{
158159
// verify if CharSpan matches channel name
159160
// or callSign or affiliateCallSign or majorNumber.minorNumber
160-
if (isChannelMatched(channel, match))
161+
if (isChannelMatched(mChannels[i], match))
161162
{
162-
matchedChannels.push_back(channel);
163-
}
164-
else if (matchedChannels.empty())
165-
{
166-
iFirstMatchedChannel++;
167-
}
163+
if (iMatchedChannel != -1) {
164+
// Error: Found multiple matches
165+
response.status = chip::app::Clusters::Channel::StatusEnum::kMultipleMatches;
166+
helper.Success(response);
167+
return;
168+
}
169+
iMatchedChannel = i;
170+
}
168171
}
169172

170-
ChangeChannelResponseType response;
171-
172-
// Error: Found multiple matches
173-
if (matchedChannels.size() > 1)
174-
{
175-
response.status = chip::app::Clusters::Channel::StatusEnum::kMultipleMatches;
176-
helper.Success(response);
177-
}
178-
else if (matchedChannels.empty())
173+
if (iMatchedChannel == -1)
179174
{
180175
// Error: Found no match
181176
response.status = chip::app::Clusters::Channel::StatusEnum::kNoMatches;
@@ -185,8 +180,8 @@ void ChannelManager::HandleChangeChannel(CommandResponseHelper<ChangeChannelResp
185180
{
186181
response.status = chip::app::Clusters::Channel::StatusEnum::kSuccess;
187182
response.data = chip::MakeOptional(CharSpan::fromCharString("data response"));
188-
mCurrentChannel = matchedChannels[0];
189-
mCurrentChannelIndex = iFirstMatchedChannel;
183+
mCurrentChannel = mChannels[iMatchedChannel];
184+
mCurrentChannelIndex = iMatchedChannel;
190185
helper.Success(response);
191186
}
192187
}
@@ -206,6 +201,7 @@ bool ChannelManager::HandleChangeChannelByNumber(const uint16_t & majorNumber, c
206201
channelChanged = true;
207202
mCurrentChannelIndex = index;
208203
mCurrentChannel = channel;
204+
return channelChanged;
209205
}
210206
}
211207
index++;

0 commit comments

Comments
 (0)