Skip to content

Commit d67e603

Browse files
authored
CASE: move busy wait time parsing in status report module (project-chip#38491)
* move busy wait time parsing in status report module * remove the unnecessary comment and use std::optional instead of chip::Optional * fix clang-tidy
1 parent b2867e6 commit d67e603

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

src/protocols/secure_channel/PairingSession.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,11 @@ class DLL_EXPORT PairingSession : public SessionDelegate
183183
}
184184

185185
Optional<uintptr_t> protocolData;
186-
if (report.GetGeneralCode() == Protocols::SecureChannel::GeneralStatusCode::kBusy &&
187-
report.GetProtocolCode() == Protocols::SecureChannel::kProtocolCodeBusy)
186+
if (report.IsBusy() && report.GetMinimumWaitTime().has_value())
188187
{
189-
if (!report.GetProtocolData().IsNull())
190-
{
191-
Encoding::LittleEndian::Reader reader(report.GetProtocolData()->Start(), report.GetProtocolData()->DataLength());
192-
193-
uint16_t minimumWaitTime = 0;
194-
CHIP_ERROR waitTimeErr = reader.Read16(&minimumWaitTime).StatusCode();
195-
if (waitTimeErr != CHIP_NO_ERROR)
196-
{
197-
ChipLogError(SecureChannel, "Failed to read the minimum wait time: %" CHIP_ERROR_FORMAT, waitTimeErr.Format());
198-
}
199-
else
200-
{
201-
ChipLogProgress(SecureChannel, "Received busy status report with minimum wait time: %u ms", minimumWaitTime);
202-
protocolData.Emplace(minimumWaitTime);
203-
}
204-
}
188+
System::Clock::Milliseconds16 minimumWaitTime = report.GetMinimumWaitTime().value();
189+
ChipLogProgress(SecureChannel, "Received busy status report with minimum wait time: %u ms", minimumWaitTime.count());
190+
protocolData.Emplace(minimumWaitTime.count());
205191
}
206192

207193
// It's very important that we propagate the return value from

src/protocols/secure_channel/StatusReport.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
#include <optional>
1920
#include <protocols/secure_channel/Constants.h>
2021
#include <protocols/secure_channel/StatusReport.h>
2122

@@ -70,6 +71,14 @@ CHIP_ERROR StatusReport::Parse(System::PacketBufferHandle buf)
7071
{
7172
return CHIP_ERROR_NO_MEMORY;
7273
}
74+
75+
if (IsBusy())
76+
{
77+
Encoding::LittleEndian::Reader reader(mProtocolData->Start(), mProtocolData->DataLength());
78+
uint16_t tmpMinimumWaitTime = 0;
79+
ReturnErrorOnFailure(reader.Read16(&tmpMinimumWaitTime).StatusCode());
80+
mMinimumWaitTime = std::make_optional<System::Clock::Milliseconds16>(tmpMinimumWaitTime);
81+
}
7382
}
7483
else
7584
{
@@ -95,6 +104,11 @@ size_t StatusReport::Size() const
95104
return WriteToBuffer(emptyBuf).Needed();
96105
}
97106

107+
bool StatusReport::IsBusy() const
108+
{
109+
return (mGeneralCode == GeneralStatusCode::kBusy && mProtocolCode == kProtocolCodeBusy);
110+
}
111+
98112
System::PacketBufferHandle StatusReport::MakeBusyStatusReportMessage(System::Clock::Milliseconds16 minimumWaitTime)
99113
{
100114
using namespace Protocols::SecureChannel;
@@ -113,6 +127,7 @@ System::PacketBufferHandle StatusReport::MakeBusyStatusReportMessage(System::Clo
113127

114128
// Build a busy status report
115129
StatusReport statusReport(GeneralStatusCode::kBusy, Protocols::SecureChannel::Id, kProtocolCodeBusy, std::move(handle));
130+
statusReport.mMinimumWaitTime = std::make_optional<System::Clock::Milliseconds16>(minimumWaitTime);
116131

117132
// Build the status report message
118133
handle = System::PacketBufferHandle::New(statusReport.Size());

src/protocols/secure_channel/StatusReport.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
#pragma once
2020

21+
#include <lib/core/Optional.h>
2122
#include <lib/support/BufferWriter.h>
23+
#include <optional>
2224
#include <protocols/Protocols.h>
2325
#include <protocols/secure_channel/Constants.h>
2426
#include <system/SystemClock.h>
@@ -95,6 +97,17 @@ class DLL_EXPORT StatusReport
9597
uint16_t GetProtocolCode() const { return mProtocolCode; }
9698
const System::PacketBufferHandle & GetProtocolData() const { return mProtocolData; }
9799

100+
/**
101+
* Returns the minimum wait time if the status report is of type busy.
102+
* If status report is not busy, then this is an empty optional.
103+
*/
104+
std::optional<System::Clock::Milliseconds16> GetMinimumWaitTime() const { return mMinimumWaitTime; }
105+
106+
/**
107+
* Returns true if the status report is of type busy.
108+
*/
109+
bool IsBusy() const;
110+
98111
/**
99112
* Builds a busy status report with protocol data containing the minimum wait time.
100113
*
@@ -108,7 +121,7 @@ class DLL_EXPORT StatusReport
108121
GeneralStatusCode mGeneralCode;
109122
Protocols::Id mProtocolId;
110123
uint16_t mProtocolCode;
111-
124+
std::optional<System::Clock::Milliseconds16> mMinimumWaitTime;
112125
System::PacketBufferHandle mProtocolData;
113126
};
114127

src/protocols/secure_channel/tests/TestStatusReport.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,13 @@ TEST_F(TestStatusReport, TestMakeBusyStatusReport)
124124
EXPECT_EQ(reportToParse.GetGeneralCode(), generalCode);
125125
EXPECT_EQ(reportToParse.GetProtocolId(), protocolId);
126126
EXPECT_EQ(reportToParse.GetProtocolCode(), protocolCode);
127-
128-
const System::PacketBufferHandle & rcvData = reportToParse.GetProtocolData();
129-
ASSERT_FALSE(rcvData.IsNull());
130-
EXPECT_EQ(rcvData->DataLength(), sizeof(minimumWaitTime));
131-
132-
uint16_t readMinimumWaitTime = 0;
133-
Encoding::LittleEndian::Reader reader(rcvData->Start(), rcvData->DataLength());
134-
EXPECT_EQ(reader.Read16(&readMinimumWaitTime).StatusCode(), CHIP_NO_ERROR);
135-
EXPECT_EQ(System::Clock::Milliseconds16(readMinimumWaitTime), minimumWaitTime);
127+
EXPECT_TRUE(reportToParse.IsBusy());
128+
auto tmpMinimumWaitTime = reportToParse.GetMinimumWaitTime();
129+
EXPECT_TRUE(tmpMinimumWaitTime.has_value());
130+
if (tmpMinimumWaitTime.has_value())
131+
{
132+
EXPECT_EQ(tmpMinimumWaitTime.value(), minimumWaitTime);
133+
}
136134
}
137135

138136
// Test Suite

0 commit comments

Comments
 (0)