Skip to content

Commit 9ed9c72

Browse files
[chore] Replace all usages of chip::Optional with std::optional in src/lib/dnssd (#33200)
* std optional in recordwriter * Fix a call * Change some advertiser.h to std::optional * Fix usage in advertiser.cpp * Make more things compile * Restyle * make clang-tidy happy * Replace chip optional from active resolveattempts * Fix platform dns * More test fixes * Fix one more compile to not have optional at all in dnssd * Restyle * Add back removed header * Some compile fixes for IP address * more compile fixes - tested that qpg thermostat compiles for me now * Fix tizen build * Commissioner passcode fix * make clang-tidy happy * Use references for MRPConfig optionals, as the mrpconfig may be larger * Reduce scope of optional usage * Use references everywhere for Advertiser * One more use of a reference * One more const reference passing to try to reduce code size * Restyle * Attempt to make clang-tidy happy in unit tst * Disable the check via a nolint * Code review: use C++17 if initializers to scope out temporary optional values * Use a few more references * Apply code review comment * Undo return as reference changes in getters for optional --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com>
1 parent f6bbfee commit 9ed9c72

18 files changed

+304
-292
lines changed

examples/minimal-mdns/advertiser.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
#include <cstdio>
1818
#include <memory>
19+
#include <optional>
1920

2021
#include <arpa/inet.h>
2122
#include <strings.h>
@@ -45,18 +46,18 @@ struct Options
4546
AdvertisingMode advertisingMode = AdvertisingMode::kCommissionableNode;
4647

4748
// commissionable node / commissioner params
48-
Optional<uint16_t> vendorId;
49-
Optional<uint16_t> productId;
50-
Optional<uint32_t> deviceType;
51-
Optional<const char *> deviceName;
49+
std::optional<uint16_t> vendorId;
50+
std::optional<uint16_t> productId;
51+
std::optional<uint32_t> deviceType;
52+
std::optional<const char *> deviceName;
5253

5354
// commissionable node params
5455
uint8_t shortDiscriminator = 52;
5556
uint16_t longDiscriminator = 840;
5657
Dnssd::CommissioningMode commissioningMode = Dnssd::CommissioningMode::kDisabled;
57-
Optional<const char *> rotatingId;
58-
Optional<const char *> pairingInstr;
59-
Optional<uint16_t> pairingHint;
58+
std::optional<const char *> rotatingId;
59+
std::optional<const char *> pairingInstr;
60+
std::optional<uint16_t> pairingHint;
6061

6162
// operational params
6263
uint64_t fabricId = 12345;
@@ -130,10 +131,10 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,
130131
gOptions.longDiscriminator = static_cast<uint16_t>(atoi(aValue));
131132
return true;
132133
case kOptionCommissioningVendorId:
133-
gOptions.vendorId = Optional<uint16_t>::Value(static_cast<uint16_t>(atoi(aValue)));
134+
gOptions.vendorId = std::make_optional<uint16_t>(static_cast<uint16_t>(atoi(aValue)));
134135
return true;
135136
case kOptionCommissioningProductId:
136-
gOptions.productId = Optional<uint16_t>::Value(static_cast<uint16_t>(atoi(aValue)));
137+
gOptions.productId = std::make_optional<uint16_t>(static_cast<uint16_t>(atoi(aValue)));
137138
return true;
138139
case kOptionCommissioningMode:
139140
cm = static_cast<uint8_t>(atoi(aValue));
@@ -147,19 +148,19 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,
147148
}
148149
return true;
149150
case kOptionCommissioningDeviceType:
150-
gOptions.deviceType = Optional<uint32_t>::Value(static_cast<uint32_t>(atoi(aValue)));
151+
gOptions.deviceType = std::make_optional<uint32_t>(static_cast<uint32_t>(atoi(aValue)));
151152
return true;
152153
case kOptionCommissioningDeviceName:
153-
gOptions.deviceName = Optional<const char *>::Value(static_cast<const char *>(aValue));
154+
gOptions.deviceName = std::make_optional<const char *>(static_cast<const char *>(aValue));
154155
return true;
155156
case kOptionCommissioningRotatingId:
156-
gOptions.rotatingId = Optional<const char *>::Value(static_cast<const char *>(aValue));
157+
gOptions.rotatingId = std::make_optional<const char *>(static_cast<const char *>(aValue));
157158
return true;
158159
case kOptionCommissioningPairingInstr:
159-
gOptions.pairingInstr = Optional<const char *>::Value(static_cast<const char *>(aValue));
160+
gOptions.pairingInstr = std::make_optional<const char *>(static_cast<const char *>(aValue));
160161
return true;
161162
case kOptionCommissioningPairingHint:
162-
gOptions.pairingHint = Optional<uint16_t>::Value(static_cast<uint16_t>(atoi(aValue)));
163+
gOptions.pairingHint = std::make_optional<uint16_t>(static_cast<uint16_t>(atoi(aValue)));
163164
return true;
164165
case kOptionOperationalFabricId:
165166
if (sscanf(aValue, "%" SCNx64, &gOptions.fabricId) != 1)

src/app/server/Dnssd.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ CHIP_ERROR DnssdServer::AdvertiseOperational()
206206
.SetMac(mac)
207207
.SetPort(GetSecuredPort())
208208
.SetInterfaceId(GetInterfaceId())
209-
.SetLocalMRPConfig(GetLocalMRPConfig())
209+
.SetLocalMRPConfig(GetLocalMRPConfig().std_optional())
210210
.EnableIpV4(true);
211211

212212
#if CHIP_CONFIG_ENABLE_ICD_SERVER
@@ -251,7 +251,7 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
251251
}
252252
else
253253
{
254-
advertiseParameters.SetVendorId(chip::Optional<uint16_t>::Value(value));
254+
advertiseParameters.SetVendorId(std::make_optional<uint16_t>(value));
255255
}
256256

257257
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetProductId(value) != CHIP_NO_ERROR)
@@ -260,23 +260,23 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
260260
}
261261
else
262262
{
263-
advertiseParameters.SetProductId(chip::Optional<uint16_t>::Value(value));
263+
advertiseParameters.SetProductId(std::make_optional<uint16_t>(value));
264264
}
265265

266266
if (DeviceLayer::ConfigurationMgr().IsCommissionableDeviceTypeEnabled() &&
267267
DeviceLayer::ConfigurationMgr().GetDeviceTypeId(val32) == CHIP_NO_ERROR)
268268
{
269-
advertiseParameters.SetDeviceType(chip::Optional<uint32_t>::Value(val32));
269+
advertiseParameters.SetDeviceType(std::make_optional<uint32_t>(val32));
270270
}
271271

272272
char deviceName[chip::Dnssd::kKeyDeviceNameMaxLength + 1];
273273
if (DeviceLayer::ConfigurationMgr().IsCommissionableDeviceNameEnabled() &&
274274
DeviceLayer::ConfigurationMgr().GetCommissionableDeviceName(deviceName, sizeof(deviceName)) == CHIP_NO_ERROR)
275275
{
276-
advertiseParameters.SetDeviceName(chip::Optional<const char *>::Value(deviceName));
276+
advertiseParameters.SetDeviceName(std::make_optional<const char *>(deviceName));
277277
}
278278

279-
advertiseParameters.SetLocalMRPConfig(GetLocalMRPConfig());
279+
advertiseParameters.SetLocalMRPConfig(GetLocalMRPConfig().std_optional());
280280

281281
#if CHIP_CONFIG_ENABLE_ICD_SERVER
282282
AddICDKeyToAdvertisement(advertiseParameters);
@@ -303,7 +303,7 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
303303
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
304304
char rotatingDeviceIdHexBuffer[RotatingDeviceId::kHexMaxLength];
305305
ReturnErrorOnFailure(GenerateRotatingDeviceId(rotatingDeviceIdHexBuffer, ArraySize(rotatingDeviceIdHexBuffer)));
306-
advertiseParameters.SetRotatingDeviceId(chip::Optional<const char *>::Value(rotatingDeviceIdHexBuffer));
306+
advertiseParameters.SetRotatingDeviceId(std::make_optional<const char *>(rotatingDeviceIdHexBuffer));
307307
#endif
308308

309309
if (!HaveOperationalCredentials())
@@ -314,7 +314,7 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
314314
}
315315
else
316316
{
317-
advertiseParameters.SetPairingHint(chip::Optional<uint16_t>::Value(value));
317+
advertiseParameters.SetPairingHint(std::make_optional<uint16_t>(value));
318318
}
319319

320320
if (DeviceLayer::ConfigurationMgr().GetInitialPairingInstruction(pairingInst, sizeof(pairingInst)) != CHIP_NO_ERROR)
@@ -323,7 +323,7 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
323323
}
324324
else
325325
{
326-
advertiseParameters.SetPairingInstruction(chip::Optional<const char *>::Value(pairingInst));
326+
advertiseParameters.SetPairingInstruction(std::make_optional<const char *>(pairingInst));
327327
}
328328
}
329329
else
@@ -334,7 +334,7 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
334334
}
335335
else
336336
{
337-
advertiseParameters.SetPairingHint(chip::Optional<uint16_t>::Value(value));
337+
advertiseParameters.SetPairingHint(std::make_optional<uint16_t>(value));
338338
}
339339

340340
if (DeviceLayer::ConfigurationMgr().GetSecondaryPairingInstruction(pairingInst, sizeof(pairingInst)) != CHIP_NO_ERROR)
@@ -343,24 +343,24 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
343343
}
344344
else
345345
{
346-
advertiseParameters.SetPairingInstruction(chip::Optional<const char *>::Value(pairingInst));
346+
advertiseParameters.SetPairingInstruction(std::make_optional<const char *>(pairingInst));
347347
}
348348
}
349349
}
350350
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_PASSCODE
351351
else
352352
{
353-
advertiseParameters.SetCommissionerPasscodeSupported(Optional<bool>(true));
353+
advertiseParameters.SetCommissionerPasscodeSupported(std::make_optional<bool>(true));
354354
}
355355
#endif
356356

357357
auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance();
358358

359359
ChipLogProgress(Discovery, "Advertise commission parameter vendorID=%u productID=%u discriminator=%04u/%02u cm=%u cp=%u",
360-
advertiseParameters.GetVendorId().ValueOr(0), advertiseParameters.GetProductId().ValueOr(0),
360+
advertiseParameters.GetVendorId().value_or(0), advertiseParameters.GetProductId().value_or(0),
361361
advertiseParameters.GetLongDiscriminator(), advertiseParameters.GetShortDiscriminator(),
362362
to_underlying(advertiseParameters.GetCommissioningMode()),
363-
advertiseParameters.GetCommissionerPasscodeSupported().ValueOr(false) ? 1 : 0);
363+
advertiseParameters.GetCommissionerPasscodeSupported().value_or(false) ? 1 : 0);
364364
return mdnsAdvertiser.Advertise(advertiseParameters);
365365
}
366366

src/lib/dnssd/ActiveResolveAttempts.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ void ActiveResolveAttempts::MarkPending(ScheduledAttempt && attempt)
218218
entryToUse->nextRetryDelay = System::Clock::Seconds16(1);
219219
}
220220

221-
Optional<System::Clock::Timeout> ActiveResolveAttempts::GetTimeUntilNextExpectedResponse() const
221+
std::optional<System::Clock::Timeout> ActiveResolveAttempts::GetTimeUntilNextExpectedResponse() const
222222
{
223-
Optional<System::Clock::Timeout> minDelay = Optional<System::Clock::Timeout>::Missing();
223+
std::optional<System::Clock::Timeout> minDelay = std::nullopt;
224224

225225
chip::System::Clock::Timestamp now = mClock->GetMonotonicTimestamp();
226226

@@ -234,20 +234,20 @@ Optional<System::Clock::Timeout> ActiveResolveAttempts::GetTimeUntilNextExpected
234234
if (now >= entry.queryDueTime)
235235
{
236236
// found an entry that needs processing right now
237-
return Optional<System::Clock::Timeout>::Value(0);
237+
return std::make_optional<System::Clock::Timeout>(0);
238238
}
239239

240240
System::Clock::Timeout entryDelay = entry.queryDueTime - now;
241-
if (!minDelay.HasValue() || (minDelay.Value() > entryDelay))
241+
if (!minDelay.has_value() || (*minDelay > entryDelay))
242242
{
243-
minDelay.SetValue(entryDelay);
243+
minDelay.emplace(entryDelay);
244244
}
245245
}
246246

247247
return minDelay;
248248
}
249249

250-
Optional<ActiveResolveAttempts::ScheduledAttempt> ActiveResolveAttempts::NextScheduled()
250+
std::optional<ActiveResolveAttempts::ScheduledAttempt> ActiveResolveAttempts::NextScheduled()
251251
{
252252
chip::System::Clock::Timestamp now = mClock->GetMonotonicTimestamp();
253253

@@ -273,13 +273,13 @@ Optional<ActiveResolveAttempts::ScheduledAttempt> ActiveResolveAttempts::NextSch
273273
entry.queryDueTime = now + entry.nextRetryDelay;
274274
entry.nextRetryDelay *= 2;
275275

276-
Optional<ScheduledAttempt> attempt = MakeOptional(entry.attempt);
277-
entry.attempt.firstSend = false;
276+
std::optional<ScheduledAttempt> attempt = std::make_optional(entry.attempt);
277+
entry.attempt.firstSend = false;
278278

279279
return attempt;
280280
}
281281

282-
return Optional<ScheduledAttempt>::Missing();
282+
return std::nullopt;
283283
}
284284

285285
bool ActiveResolveAttempts::ShouldResolveIpAddress(PeerId peerId) const

src/lib/dnssd/ActiveResolveAttempts.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

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

23-
#include <lib/core/Optional.h>
2424
#include <lib/core/PeerId.h>
2525
#include <lib/dnssd/Resolver.h>
2626
#include <lib/dnssd/minimal_mdns/core/HeapQName.h>
@@ -273,7 +273,7 @@ class ActiveResolveAttempts
273273
// Get minimum time until the next pending reply is required.
274274
//
275275
// Returns missing if no actively tracked elements exist.
276-
chip::Optional<chip::System::Clock::Timeout> GetTimeUntilNextExpectedResponse() const;
276+
std::optional<chip::System::Clock::Timeout> GetTimeUntilNextExpectedResponse() const;
277277

278278
// Get the peer Id that needs scheduling for a query
279279
//
@@ -283,7 +283,7 @@ class ActiveResolveAttempts
283283
// now'
284284
// - there is NO sorting implied by this call. Returned value will be
285285
// any peer that needs a new request sent
286-
chip::Optional<ScheduledAttempt> NextScheduled();
286+
std::optional<ScheduledAttempt> NextScheduled();
287287

288288
/// Check if any of the pending queries are for the given host name for
289289
/// IP resolution.

0 commit comments

Comments
 (0)