Skip to content

Commit 6004917

Browse files
andy31415andreilitvin
authored andcommitted
[Chore] Convert chip::Optional to std::optional for dnssd/types.h (#33179)
* Replace some chip Optional with std::optional * Restyle * a few more updates * Also fix RemoteDataLogger.cpp * Code review feedback: added some bracketing * Re-do the bracketing * Fix some of the unchecked optional access warnings * Also fix TestIncrementalResolve * Fix Commissionable script binding * Fix more files * Restyle --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com>
1 parent 0724414 commit 6004917

10 files changed

+173
-174
lines changed

examples/chip-tool/commands/common/RemoteDataModelLogger.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,24 @@ CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::CommissionNodeData & nodeDat
245245
value["port"] = resolutionData.port;
246246
value["numIPs"] = static_cast<uint8_t>(resolutionData.numIPs);
247247

248-
if (resolutionData.mrpRetryIntervalIdle.HasValue())
248+
if (resolutionData.mrpRetryIntervalIdle.has_value())
249249
{
250-
value["mrpRetryIntervalIdle"] = resolutionData.mrpRetryIntervalIdle.Value().count();
250+
value["mrpRetryIntervalIdle"] = resolutionData.mrpRetryIntervalIdle->count();
251251
}
252252

253-
if (resolutionData.mrpRetryIntervalActive.HasValue())
253+
if (resolutionData.mrpRetryIntervalActive.has_value())
254254
{
255-
value["mrpRetryIntervalActive"] = resolutionData.mrpRetryIntervalActive.Value().count();
255+
value["mrpRetryIntervalActive"] = resolutionData.mrpRetryIntervalActive->count();
256256
}
257257

258-
if (resolutionData.mrpRetryActiveThreshold.HasValue())
258+
if (resolutionData.mrpRetryActiveThreshold.has_value())
259259
{
260-
value["mrpRetryActiveThreshold"] = resolutionData.mrpRetryActiveThreshold.Value().count();
260+
value["mrpRetryActiveThreshold"] = resolutionData.mrpRetryActiveThreshold->count();
261261
}
262262

263-
if (resolutionData.isICDOperatingAsLIT.HasValue())
263+
if (resolutionData.isICDOperatingAsLIT.has_value())
264264
{
265-
value["isICDOperatingAsLIT"] = resolutionData.isICDOperatingAsLIT.Value();
265+
value["isICDOperatingAsLIT"] = *(resolutionData.isICDOperatingAsLIT);
266266
}
267267

268268
Json::Value rootValue;

src/controller/SetUpCodePairer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -620,14 +620,14 @@ SetUpCodePairerParameters::SetUpCodePairerParameters(const Dnssd::CommonResoluti
620620
auto & ip = data.ipAddress[index];
621621
SetPeerAddress(Transport::PeerAddress::UDP(ip, data.port, ip.IsIPv6LinkLocal() ? data.interfaceId : Inet::InterfaceId::Null()));
622622

623-
if (data.mrpRetryIntervalIdle.HasValue())
623+
if (data.mrpRetryIntervalIdle.has_value())
624624
{
625-
SetIdleInterval(data.mrpRetryIntervalIdle.Value());
625+
SetIdleInterval(*data.mrpRetryIntervalIdle);
626626
}
627627

628-
if (data.mrpRetryIntervalActive.HasValue())
628+
if (data.mrpRetryIntervalActive.has_value())
629629
{
630-
SetActiveInterval(data.mrpRetryIntervalActive.Value());
630+
SetActiveInterval(*data.mrpRetryIntervalActive);
631631
}
632632
}
633633

src/controller/python/ChipCommissionableNodeController-ScriptBinding.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,31 @@ void pychip_CommissionableNodeController_PrintDiscoveredCommissioners(
9797
ChipLogProgress(Discovery, "\tRotating Id\t\t%s", rotatingId);
9898
ChipLogProgress(Discovery, "\tPairing Instruction\t%s", dnsSdInfo->pairingInstruction);
9999
ChipLogProgress(Discovery, "\tPairing Hint\t\t%u", dnsSdInfo->pairingHint);
100-
if (dnsSdInfo->GetMrpRetryIntervalIdle().HasValue())
100+
101+
auto idleInterval = dnsSdInfo->GetMrpRetryIntervalIdle();
102+
if (idleInterval.has_value())
101103
{
102-
ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", dnsSdInfo->GetMrpRetryIntervalIdle().Value().count());
104+
ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", idleInterval->count());
103105
}
104106
else
105107
{
106108
ChipLogProgress(Discovery, "\tMrp Interval idle\tNot present");
107109
}
108-
if (dnsSdInfo->GetMrpRetryIntervalActive().HasValue())
110+
111+
auto activeInterval = dnsSdInfo->GetMrpRetryIntervalIdle();
112+
if (activeInterval.has_value())
109113
{
110-
ChipLogProgress(Discovery, "\tMrp Interval active\t%u", dnsSdInfo->GetMrpRetryIntervalActive().Value().count());
114+
ChipLogProgress(Discovery, "\tMrp Interval active\t%u", activeInterval->count());
111115
}
112116
else
113117
{
114118
ChipLogProgress(Discovery, "\tMrp Interval active\tNot present");
115119
}
116120
ChipLogProgress(Discovery, "\tSupports TCP\t\t%d", dnsSdInfo->supportsTcp);
117121

118-
if (dnsSdInfo->isICDOperatingAsLIT.HasValue())
122+
if (dnsSdInfo->isICDOperatingAsLIT.has_value())
119123
{
120-
ChipLogProgress(Discovery, "\tICD is operating as a\t%s", dnsSdInfo->isICDOperatingAsLIT.Value() ? "LIT" : "SIT");
124+
ChipLogProgress(Discovery, "\tICD is operating as a\t%s", *(dnsSdInfo->isICDOperatingAsLIT) ? "LIT" : "SIT");
121125
}
122126

123127
for (unsigned j = 0; j < dnsSdInfo->numIPs; ++j)

src/controller/python/ChipDeviceController-Discovery.cpp

+25-14
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,23 @@ void pychip_DeviceController_IterateDiscoveredCommissionableNodes(Controller::De
124124
jsonVal["deviceName"] = dnsSdInfo->deviceName;
125125
jsonVal["pairingInstruction"] = dnsSdInfo->pairingInstruction;
126126
jsonVal["pairingHint"] = dnsSdInfo->pairingHint;
127-
if (dnsSdInfo->GetMrpRetryIntervalIdle().HasValue())
127+
128+
auto idleInterval = dnsSdInfo->GetMrpRetryIntervalIdle();
129+
if (idleInterval.has_value())
128130
{
129-
jsonVal["mrpRetryIntervalIdle"] = dnsSdInfo->GetMrpRetryIntervalIdle().Value().count();
131+
jsonVal["mrpRetryIntervalIdle"] = idleInterval->count();
130132
}
131-
if (dnsSdInfo->GetMrpRetryIntervalActive().HasValue())
133+
134+
auto activeInterval = dnsSdInfo->GetMrpRetryIntervalActive();
135+
if (activeInterval.has_value())
132136
{
133-
jsonVal["mrpRetryIntervalActive"] = dnsSdInfo->GetMrpRetryIntervalActive().Value().count();
137+
jsonVal["mrpRetryIntervalActive"] = activeInterval->count();
134138
}
135-
if (dnsSdInfo->GetMrpRetryActiveThreshold().HasValue())
139+
140+
auto activeThreshold = dnsSdInfo->GetMrpRetryActiveThreshold();
141+
if (activeThreshold.has_value())
136142
{
137-
jsonVal["mrpRetryActiveThreshold"] = dnsSdInfo->GetMrpRetryActiveThreshold().Value().count();
143+
jsonVal["mrpRetryActiveThreshold"] = activeThreshold->count();
138144
}
139145
jsonVal["supportsTcp"] = dnsSdInfo->supportsTcp;
140146
{
@@ -147,9 +153,9 @@ void pychip_DeviceController_IterateDiscoveredCommissionableNodes(Controller::De
147153
}
148154
jsonVal["addresses"] = addresses;
149155
}
150-
if (dnsSdInfo->isICDOperatingAsLIT.HasValue())
156+
if (dnsSdInfo->isICDOperatingAsLIT.has_value())
151157
{
152-
jsonVal["isICDOperatingAsLIT"] = dnsSdInfo->isICDOperatingAsLIT.Value();
158+
jsonVal["isICDOperatingAsLIT"] = *(dnsSdInfo->isICDOperatingAsLIT);
153159
}
154160
if (dnsSdInfo->rotatingIdLen > 0)
155161
{
@@ -188,26 +194,31 @@ void pychip_DeviceController_PrintDiscoveredDevices(Controller::DeviceCommission
188194
ChipLogProgress(Discovery, "\tRotating Id\t\t%s", rotatingId);
189195
ChipLogProgress(Discovery, "\tPairing Instruction\t%s", dnsSdInfo->pairingInstruction);
190196
ChipLogProgress(Discovery, "\tPairing Hint\t\t%u", dnsSdInfo->pairingHint);
191-
if (dnsSdInfo->GetMrpRetryIntervalIdle().HasValue())
197+
198+
auto idleInterval = dnsSdInfo->GetMrpRetryIntervalIdle();
199+
if (idleInterval.has_value())
192200
{
193-
ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", dnsSdInfo->GetMrpRetryIntervalIdle().Value().count());
201+
ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", idleInterval->count());
194202
}
195203
else
196204
{
197205
ChipLogProgress(Discovery, "\tMrp Interval idle\tNot present");
198206
}
199-
if (dnsSdInfo->GetMrpRetryIntervalActive().HasValue())
207+
208+
auto activeInterval = dnsSdInfo->GetMrpRetryIntervalActive();
209+
if (activeInterval.has_value())
200210
{
201-
ChipLogProgress(Discovery, "\tMrp Interval active\t%u", dnsSdInfo->GetMrpRetryIntervalActive().Value().count());
211+
ChipLogProgress(Discovery, "\tMrp Interval active\t%u", activeInterval->count());
202212
}
203213
else
204214
{
205215
ChipLogProgress(Discovery, "\tMrp Interval active\tNot present");
206216
}
217+
207218
ChipLogProgress(Discovery, "\tSupports TCP\t\t%d", dnsSdInfo->supportsTcp);
208-
if (dnsSdInfo->isICDOperatingAsLIT.HasValue())
219+
if (dnsSdInfo->isICDOperatingAsLIT.has_value())
209220
{
210-
ChipLogProgress(Discovery, "\tICD is operating as a\t%s", dnsSdInfo->isICDOperatingAsLIT.Value() ? "LIT" : "SIT");
221+
ChipLogProgress(Discovery, "\tICD is operating as a\t%s", *(dnsSdInfo->isICDOperatingAsLIT) ? "LIT" : "SIT");
211222
}
212223
for (unsigned j = 0; j < dnsSdInfo->numIPs; ++j)
213224
{

src/lib/address_resolve/AddressResolve_DefaultImpl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ void Resolver::OnOperationalNodeResolved(const Dnssd::ResolvedNodeData & nodeDat
290290
result.mrpRemoteConfig = nodeData.resolutionData.GetRemoteMRPConfig();
291291
result.supportsTcp = nodeData.resolutionData.supportsTcp;
292292

293-
if (nodeData.resolutionData.isICDOperatingAsLIT.HasValue())
293+
if (nodeData.resolutionData.isICDOperatingAsLIT.has_value())
294294
{
295-
result.isICDOperatingAsLIT = nodeData.resolutionData.isICDOperatingAsLIT.Value();
295+
result.isICDOperatingAsLIT = *(nodeData.resolutionData.isICDOperatingAsLIT);
296296
}
297297

298298
for (size_t i = 0; i < nodeData.resolutionData.numIPs; i++)

src/lib/dnssd/TxtFields.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ bool MakeBoolFromAsciiDecimal(const ByteSpan & val)
105105
return val.size() == 1 && static_cast<char>(*val.data()) == '1';
106106
}
107107

108-
Optional<bool> MakeOptionalBoolFromAsciiDecimal(const ByteSpan & val)
108+
std::optional<bool> MakeOptionalBoolFromAsciiDecimal(const ByteSpan & val)
109109
{
110110
char character = static_cast<char>(*val.data());
111111
if (val.size() == 1 && ((character == '1') || (character == '0')))
112112
{
113-
return MakeOptional(character == '1');
113+
return std::make_optional(character == '1');
114114
}
115-
return NullOptional;
115+
return std::nullopt;
116116
}
117117

118118
size_t GetPlusSignIdx(const ByteSpan & value)
@@ -188,27 +188,27 @@ uint8_t GetCommissionerPasscode(const ByteSpan & value)
188188
return MakeBoolFromAsciiDecimal(value);
189189
}
190190

191-
Optional<System::Clock::Milliseconds32> GetRetryInterval(const ByteSpan & value)
191+
std::optional<System::Clock::Milliseconds32> GetRetryInterval(const ByteSpan & value)
192192
{
193193
const auto undefined = std::numeric_limits<uint32_t>::max();
194194
const auto retryInterval = MakeU32FromAsciiDecimal(value, undefined);
195195

196196
if (retryInterval != undefined && retryInterval <= kMaxRetryInterval.count())
197-
return MakeOptional(System::Clock::Milliseconds32(retryInterval));
197+
return std::make_optional(System::Clock::Milliseconds32(retryInterval));
198198

199-
return NullOptional;
199+
return std::nullopt;
200200
}
201201

202-
Optional<System::Clock::Milliseconds16> GetRetryActiveThreshold(const ByteSpan & value)
202+
std::optional<System::Clock::Milliseconds16> GetRetryActiveThreshold(const ByteSpan & value)
203203
{
204204
const auto retryInterval = MakeU16FromAsciiDecimal(value);
205205

206206
if (retryInterval == 0)
207207
{
208-
return NullOptional;
208+
return std::nullopt;
209209
}
210210

211-
return MakeOptional(System::Clock::Milliseconds16(retryInterval));
211+
return std::make_optional(System::Clock::Milliseconds16(retryInterval));
212212
}
213213

214214
TxtFieldKey GetTxtFieldKey(const ByteSpan & key)

src/lib/dnssd/Types.h

+25-25
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
#include <cstdint>
2121
#include <cstring>
22+
#include <optional>
2223

2324
#include <inet/InetInterface.h>
2425
#include <lib/core/CHIPError.h>
25-
#include <lib/core/Optional.h>
2626
#include <lib/core/PeerId.h>
2727
#include <lib/dnssd/Constants.h>
2828
#include <lib/support/BytesToHex.h>
@@ -91,10 +91,10 @@ struct CommonResolutionData
9191
uint16_t port = 0;
9292
char hostName[kHostNameMaxLength + 1] = {};
9393
bool supportsTcp = false;
94-
Optional<bool> isICDOperatingAsLIT;
95-
Optional<System::Clock::Milliseconds32> mrpRetryIntervalIdle;
96-
Optional<System::Clock::Milliseconds32> mrpRetryIntervalActive;
97-
Optional<System::Clock::Milliseconds16> mrpRetryActiveThreshold;
94+
std::optional<bool> isICDOperatingAsLIT;
95+
std::optional<System::Clock::Milliseconds32> mrpRetryIntervalIdle;
96+
std::optional<System::Clock::Milliseconds32> mrpRetryIntervalActive;
97+
std::optional<System::Clock::Milliseconds16> mrpRetryActiveThreshold;
9898

9999
CommonResolutionData() { Reset(); }
100100

@@ -103,32 +103,32 @@ struct CommonResolutionData
103103
ReliableMessageProtocolConfig GetRemoteMRPConfig() const
104104
{
105105
const ReliableMessageProtocolConfig defaultConfig = GetDefaultMRPConfig();
106-
return ReliableMessageProtocolConfig(GetMrpRetryIntervalIdle().ValueOr(defaultConfig.mIdleRetransTimeout),
107-
GetMrpRetryIntervalActive().ValueOr(defaultConfig.mActiveRetransTimeout),
108-
GetMrpRetryActiveThreshold().ValueOr(defaultConfig.mActiveThresholdTime));
106+
return ReliableMessageProtocolConfig(GetMrpRetryIntervalIdle().value_or(defaultConfig.mIdleRetransTimeout),
107+
GetMrpRetryIntervalActive().value_or(defaultConfig.mActiveRetransTimeout),
108+
GetMrpRetryActiveThreshold().value_or(defaultConfig.mActiveThresholdTime));
109109
}
110-
Optional<System::Clock::Milliseconds32> GetMrpRetryIntervalIdle() const { return mrpRetryIntervalIdle; }
111-
Optional<System::Clock::Milliseconds32> GetMrpRetryIntervalActive() const { return mrpRetryIntervalActive; }
112-
Optional<System::Clock::Milliseconds16> GetMrpRetryActiveThreshold() const { return mrpRetryActiveThreshold; }
110+
std::optional<System::Clock::Milliseconds32> GetMrpRetryIntervalIdle() const { return mrpRetryIntervalIdle; }
111+
std::optional<System::Clock::Milliseconds32> GetMrpRetryIntervalActive() const { return mrpRetryIntervalActive; }
112+
std::optional<System::Clock::Milliseconds16> GetMrpRetryActiveThreshold() const { return mrpRetryActiveThreshold; }
113113

114114
bool IsDeviceTreatedAsSleepy(const ReliableMessageProtocolConfig * defaultMRPConfig) const
115115
{
116116
// If either session interval (Idle - SII, Active - SAI) has a value and that value is greater
117117
// than the value passed to this function, then the peer device will be treated as if it is
118118
// a Sleepy End Device (SED)
119-
return (mrpRetryIntervalIdle.HasValue() && (mrpRetryIntervalIdle.Value() > defaultMRPConfig->mIdleRetransTimeout)) ||
120-
(mrpRetryIntervalActive.HasValue() && (mrpRetryIntervalActive.Value() > defaultMRPConfig->mActiveRetransTimeout));
119+
return (mrpRetryIntervalIdle.has_value() && (*mrpRetryIntervalIdle > defaultMRPConfig->mIdleRetransTimeout)) ||
120+
(mrpRetryIntervalActive.has_value() && (*mrpRetryIntervalActive > defaultMRPConfig->mActiveRetransTimeout));
121121
}
122122

123123
bool IsHost(const char * host) const { return strcmp(host, hostName) == 0; }
124124

125125
void Reset()
126126
{
127127
memset(hostName, 0, sizeof(hostName));
128-
mrpRetryIntervalIdle = NullOptional;
129-
mrpRetryIntervalActive = NullOptional;
130-
mrpRetryActiveThreshold = NullOptional;
131-
isICDOperatingAsLIT = NullOptional;
128+
mrpRetryIntervalIdle = std::nullopt;
129+
mrpRetryIntervalActive = std::nullopt;
130+
mrpRetryActiveThreshold = std::nullopt;
131+
isICDOperatingAsLIT = std::nullopt;
132132
numIPs = 0;
133133
port = 0;
134134
supportsTcp = false;
@@ -157,34 +157,34 @@ struct CommonResolutionData
157157
{
158158
ChipLogDetail(Discovery, "\tPort: %u", port);
159159
}
160-
if (mrpRetryIntervalIdle.HasValue())
160+
if (mrpRetryIntervalIdle.has_value())
161161
{
162-
ChipLogDetail(Discovery, "\tMrp Interval idle: %" PRIu32 " ms", mrpRetryIntervalIdle.Value().count());
162+
ChipLogDetail(Discovery, "\tMrp Interval idle: %" PRIu32 " ms", mrpRetryIntervalIdle->count());
163163
}
164164
else
165165
{
166166
ChipLogDetail(Discovery, "\tMrp Interval idle: not present");
167167
}
168-
if (mrpRetryIntervalActive.HasValue())
168+
if (mrpRetryIntervalActive.has_value())
169169
{
170-
ChipLogDetail(Discovery, "\tMrp Interval active: %" PRIu32 " ms", mrpRetryIntervalActive.Value().count());
170+
ChipLogDetail(Discovery, "\tMrp Interval active: %" PRIu32 " ms", mrpRetryIntervalActive->count());
171171
}
172172
else
173173
{
174174
ChipLogDetail(Discovery, "\tMrp Interval active: not present");
175175
}
176-
if (mrpRetryActiveThreshold.HasValue())
176+
if (mrpRetryActiveThreshold.has_value())
177177
{
178-
ChipLogDetail(Discovery, "\tMrp Active Threshold: %u ms", mrpRetryActiveThreshold.Value().count());
178+
ChipLogDetail(Discovery, "\tMrp Active Threshold: %u ms", mrpRetryActiveThreshold->count());
179179
}
180180
else
181181
{
182182
ChipLogDetail(Discovery, "\tMrp Active Threshold: not present");
183183
}
184184
ChipLogDetail(Discovery, "\tTCP Supported: %d", supportsTcp);
185-
if (isICDOperatingAsLIT.HasValue())
185+
if (isICDOperatingAsLIT.has_value())
186186
{
187-
ChipLogDetail(Discovery, "\tThe ICD operates in %s", isICDOperatingAsLIT.Value() ? "LIT" : "SIT");
187+
ChipLogDetail(Discovery, "\tThe ICD operates in %s", *isICDOperatingAsLIT ? "LIT" : "SIT");
188188
}
189189
else
190190
{

src/lib/dnssd/tests/TestIncrementalResolve.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,8 @@ TEST(TestIncrementalResolve, TestParseOperational)
307307
EXPECT_EQ(nodeData.resolutionData.numIPs, 1u);
308308
EXPECT_EQ(nodeData.resolutionData.port, 0x1234);
309309
EXPECT_FALSE(nodeData.resolutionData.supportsTcp);
310-
EXPECT_FALSE(nodeData.resolutionData.GetMrpRetryIntervalActive().HasValue());
311-
EXPECT_TRUE(nodeData.resolutionData.GetMrpRetryIntervalIdle().HasValue());
312-
EXPECT_EQ(nodeData.resolutionData.GetMrpRetryIntervalIdle().Value(), chip::System::Clock::Milliseconds32(23));
310+
EXPECT_FALSE(nodeData.resolutionData.GetMrpRetryIntervalActive().has_value());
311+
EXPECT_EQ(nodeData.resolutionData.GetMrpRetryIntervalIdle(), std::make_optional(chip::System::Clock::Milliseconds32(23)));
313312

314313
Inet::IPAddress addr;
315314
EXPECT_TRUE(Inet::IPAddress::FromString("fe80::abcd:ef11:2233:4455", addr));
@@ -396,9 +395,8 @@ TEST(TestIncrementalResolve, TestParseCommissionable)
396395
EXPECT_EQ(nodeData.numIPs, 2u);
397396
EXPECT_EQ(nodeData.port, 0x1234);
398397
EXPECT_FALSE(nodeData.supportsTcp);
399-
EXPECT_TRUE(nodeData.GetMrpRetryIntervalActive().HasValue());
400-
EXPECT_EQ(nodeData.GetMrpRetryIntervalActive().Value(), chip::System::Clock::Milliseconds32(321));
401-
EXPECT_FALSE(nodeData.GetMrpRetryIntervalIdle().HasValue());
398+
EXPECT_EQ(nodeData.GetMrpRetryIntervalActive(), std::make_optional(chip::System::Clock::Milliseconds32(321)));
399+
EXPECT_FALSE(nodeData.GetMrpRetryIntervalIdle().has_value());
402400

403401
Inet::IPAddress addr;
404402
EXPECT_TRUE(Inet::IPAddress::FromString("fe80::abcd:ef11:2233:4455", addr));

0 commit comments

Comments
 (0)