Skip to content

Commit

Permalink
[Chore] Convert chip::Optional to std::optional for dnssd/types.h (#3…
Browse files Browse the repository at this point in the history
…3179)

* 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 <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Apr 30, 2024
1 parent 0724414 commit 6004917
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 174 deletions.
16 changes: 8 additions & 8 deletions examples/chip-tool/commands/common/RemoteDataModelLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,24 @@ CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::CommissionNodeData & nodeDat
value["port"] = resolutionData.port;
value["numIPs"] = static_cast<uint8_t>(resolutionData.numIPs);

if (resolutionData.mrpRetryIntervalIdle.HasValue())
if (resolutionData.mrpRetryIntervalIdle.has_value())
{
value["mrpRetryIntervalIdle"] = resolutionData.mrpRetryIntervalIdle.Value().count();
value["mrpRetryIntervalIdle"] = resolutionData.mrpRetryIntervalIdle->count();
}

if (resolutionData.mrpRetryIntervalActive.HasValue())
if (resolutionData.mrpRetryIntervalActive.has_value())
{
value["mrpRetryIntervalActive"] = resolutionData.mrpRetryIntervalActive.Value().count();
value["mrpRetryIntervalActive"] = resolutionData.mrpRetryIntervalActive->count();
}

if (resolutionData.mrpRetryActiveThreshold.HasValue())
if (resolutionData.mrpRetryActiveThreshold.has_value())
{
value["mrpRetryActiveThreshold"] = resolutionData.mrpRetryActiveThreshold.Value().count();
value["mrpRetryActiveThreshold"] = resolutionData.mrpRetryActiveThreshold->count();
}

if (resolutionData.isICDOperatingAsLIT.HasValue())
if (resolutionData.isICDOperatingAsLIT.has_value())
{
value["isICDOperatingAsLIT"] = resolutionData.isICDOperatingAsLIT.Value();
value["isICDOperatingAsLIT"] = *(resolutionData.isICDOperatingAsLIT);
}

Json::Value rootValue;
Expand Down
8 changes: 4 additions & 4 deletions src/controller/SetUpCodePairer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,14 @@ SetUpCodePairerParameters::SetUpCodePairerParameters(const Dnssd::CommonResoluti
auto & ip = data.ipAddress[index];
SetPeerAddress(Transport::PeerAddress::UDP(ip, data.port, ip.IsIPv6LinkLocal() ? data.interfaceId : Inet::InterfaceId::Null()));

if (data.mrpRetryIntervalIdle.HasValue())
if (data.mrpRetryIntervalIdle.has_value())
{
SetIdleInterval(data.mrpRetryIntervalIdle.Value());
SetIdleInterval(*data.mrpRetryIntervalIdle);
}

if (data.mrpRetryIntervalActive.HasValue())
if (data.mrpRetryIntervalActive.has_value())
{
SetActiveInterval(data.mrpRetryIntervalActive.Value());
SetActiveInterval(*data.mrpRetryIntervalActive);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,31 @@ void pychip_CommissionableNodeController_PrintDiscoveredCommissioners(
ChipLogProgress(Discovery, "\tRotating Id\t\t%s", rotatingId);
ChipLogProgress(Discovery, "\tPairing Instruction\t%s", dnsSdInfo->pairingInstruction);
ChipLogProgress(Discovery, "\tPairing Hint\t\t%u", dnsSdInfo->pairingHint);
if (dnsSdInfo->GetMrpRetryIntervalIdle().HasValue())

auto idleInterval = dnsSdInfo->GetMrpRetryIntervalIdle();
if (idleInterval.has_value())
{
ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", dnsSdInfo->GetMrpRetryIntervalIdle().Value().count());
ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", idleInterval->count());
}
else
{
ChipLogProgress(Discovery, "\tMrp Interval idle\tNot present");
}
if (dnsSdInfo->GetMrpRetryIntervalActive().HasValue())

auto activeInterval = dnsSdInfo->GetMrpRetryIntervalIdle();
if (activeInterval.has_value())
{
ChipLogProgress(Discovery, "\tMrp Interval active\t%u", dnsSdInfo->GetMrpRetryIntervalActive().Value().count());
ChipLogProgress(Discovery, "\tMrp Interval active\t%u", activeInterval->count());
}
else
{
ChipLogProgress(Discovery, "\tMrp Interval active\tNot present");
}
ChipLogProgress(Discovery, "\tSupports TCP\t\t%d", dnsSdInfo->supportsTcp);

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

for (unsigned j = 0; j < dnsSdInfo->numIPs; ++j)
Expand Down
39 changes: 25 additions & 14 deletions src/controller/python/ChipDeviceController-Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,23 @@ void pychip_DeviceController_IterateDiscoveredCommissionableNodes(Controller::De
jsonVal["deviceName"] = dnsSdInfo->deviceName;
jsonVal["pairingInstruction"] = dnsSdInfo->pairingInstruction;
jsonVal["pairingHint"] = dnsSdInfo->pairingHint;
if (dnsSdInfo->GetMrpRetryIntervalIdle().HasValue())

auto idleInterval = dnsSdInfo->GetMrpRetryIntervalIdle();
if (idleInterval.has_value())
{
jsonVal["mrpRetryIntervalIdle"] = dnsSdInfo->GetMrpRetryIntervalIdle().Value().count();
jsonVal["mrpRetryIntervalIdle"] = idleInterval->count();
}
if (dnsSdInfo->GetMrpRetryIntervalActive().HasValue())

auto activeInterval = dnsSdInfo->GetMrpRetryIntervalActive();
if (activeInterval.has_value())
{
jsonVal["mrpRetryIntervalActive"] = dnsSdInfo->GetMrpRetryIntervalActive().Value().count();
jsonVal["mrpRetryIntervalActive"] = activeInterval->count();
}
if (dnsSdInfo->GetMrpRetryActiveThreshold().HasValue())

auto activeThreshold = dnsSdInfo->GetMrpRetryActiveThreshold();
if (activeThreshold.has_value())
{
jsonVal["mrpRetryActiveThreshold"] = dnsSdInfo->GetMrpRetryActiveThreshold().Value().count();
jsonVal["mrpRetryActiveThreshold"] = activeThreshold->count();
}
jsonVal["supportsTcp"] = dnsSdInfo->supportsTcp;
{
Expand All @@ -147,9 +153,9 @@ void pychip_DeviceController_IterateDiscoveredCommissionableNodes(Controller::De
}
jsonVal["addresses"] = addresses;
}
if (dnsSdInfo->isICDOperatingAsLIT.HasValue())
if (dnsSdInfo->isICDOperatingAsLIT.has_value())
{
jsonVal["isICDOperatingAsLIT"] = dnsSdInfo->isICDOperatingAsLIT.Value();
jsonVal["isICDOperatingAsLIT"] = *(dnsSdInfo->isICDOperatingAsLIT);
}
if (dnsSdInfo->rotatingIdLen > 0)
{
Expand Down Expand Up @@ -188,26 +194,31 @@ void pychip_DeviceController_PrintDiscoveredDevices(Controller::DeviceCommission
ChipLogProgress(Discovery, "\tRotating Id\t\t%s", rotatingId);
ChipLogProgress(Discovery, "\tPairing Instruction\t%s", dnsSdInfo->pairingInstruction);
ChipLogProgress(Discovery, "\tPairing Hint\t\t%u", dnsSdInfo->pairingHint);
if (dnsSdInfo->GetMrpRetryIntervalIdle().HasValue())

auto idleInterval = dnsSdInfo->GetMrpRetryIntervalIdle();
if (idleInterval.has_value())
{
ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", dnsSdInfo->GetMrpRetryIntervalIdle().Value().count());
ChipLogProgress(Discovery, "\tMrp Interval idle\t%u", idleInterval->count());
}
else
{
ChipLogProgress(Discovery, "\tMrp Interval idle\tNot present");
}
if (dnsSdInfo->GetMrpRetryIntervalActive().HasValue())

auto activeInterval = dnsSdInfo->GetMrpRetryIntervalActive();
if (activeInterval.has_value())
{
ChipLogProgress(Discovery, "\tMrp Interval active\t%u", dnsSdInfo->GetMrpRetryIntervalActive().Value().count());
ChipLogProgress(Discovery, "\tMrp Interval active\t%u", activeInterval->count());
}
else
{
ChipLogProgress(Discovery, "\tMrp Interval active\tNot present");
}

ChipLogProgress(Discovery, "\tSupports TCP\t\t%d", dnsSdInfo->supportsTcp);
if (dnsSdInfo->isICDOperatingAsLIT.HasValue())
if (dnsSdInfo->isICDOperatingAsLIT.has_value())
{
ChipLogProgress(Discovery, "\tICD is operating as a\t%s", dnsSdInfo->isICDOperatingAsLIT.Value() ? "LIT" : "SIT");
ChipLogProgress(Discovery, "\tICD is operating as a\t%s", *(dnsSdInfo->isICDOperatingAsLIT) ? "LIT" : "SIT");
}
for (unsigned j = 0; j < dnsSdInfo->numIPs; ++j)
{
Expand Down
4 changes: 2 additions & 2 deletions src/lib/address_resolve/AddressResolve_DefaultImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ void Resolver::OnOperationalNodeResolved(const Dnssd::ResolvedNodeData & nodeDat
result.mrpRemoteConfig = nodeData.resolutionData.GetRemoteMRPConfig();
result.supportsTcp = nodeData.resolutionData.supportsTcp;

if (nodeData.resolutionData.isICDOperatingAsLIT.HasValue())
if (nodeData.resolutionData.isICDOperatingAsLIT.has_value())
{
result.isICDOperatingAsLIT = nodeData.resolutionData.isICDOperatingAsLIT.Value();
result.isICDOperatingAsLIT = *(nodeData.resolutionData.isICDOperatingAsLIT);
}

for (size_t i = 0; i < nodeData.resolutionData.numIPs; i++)
Expand Down
18 changes: 9 additions & 9 deletions src/lib/dnssd/TxtFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ bool MakeBoolFromAsciiDecimal(const ByteSpan & val)
return val.size() == 1 && static_cast<char>(*val.data()) == '1';
}

Optional<bool> MakeOptionalBoolFromAsciiDecimal(const ByteSpan & val)
std::optional<bool> MakeOptionalBoolFromAsciiDecimal(const ByteSpan & val)
{
char character = static_cast<char>(*val.data());
if (val.size() == 1 && ((character == '1') || (character == '0')))
{
return MakeOptional(character == '1');
return std::make_optional(character == '1');
}
return NullOptional;
return std::nullopt;
}

size_t GetPlusSignIdx(const ByteSpan & value)
Expand Down Expand Up @@ -188,27 +188,27 @@ uint8_t GetCommissionerPasscode(const ByteSpan & value)
return MakeBoolFromAsciiDecimal(value);
}

Optional<System::Clock::Milliseconds32> GetRetryInterval(const ByteSpan & value)
std::optional<System::Clock::Milliseconds32> GetRetryInterval(const ByteSpan & value)
{
const auto undefined = std::numeric_limits<uint32_t>::max();
const auto retryInterval = MakeU32FromAsciiDecimal(value, undefined);

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

return NullOptional;
return std::nullopt;
}

Optional<System::Clock::Milliseconds16> GetRetryActiveThreshold(const ByteSpan & value)
std::optional<System::Clock::Milliseconds16> GetRetryActiveThreshold(const ByteSpan & value)
{
const auto retryInterval = MakeU16FromAsciiDecimal(value);

if (retryInterval == 0)
{
return NullOptional;
return std::nullopt;
}

return MakeOptional(System::Clock::Milliseconds16(retryInterval));
return std::make_optional(System::Clock::Milliseconds16(retryInterval));
}

TxtFieldKey GetTxtFieldKey(const ByteSpan & key)
Expand Down
50 changes: 25 additions & 25 deletions src/lib/dnssd/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include <cstdint>
#include <cstring>
#include <optional>

#include <inet/InetInterface.h>
#include <lib/core/CHIPError.h>
#include <lib/core/Optional.h>
#include <lib/core/PeerId.h>
#include <lib/dnssd/Constants.h>
#include <lib/support/BytesToHex.h>
Expand Down Expand Up @@ -91,10 +91,10 @@ struct CommonResolutionData
uint16_t port = 0;
char hostName[kHostNameMaxLength + 1] = {};
bool supportsTcp = false;
Optional<bool> isICDOperatingAsLIT;
Optional<System::Clock::Milliseconds32> mrpRetryIntervalIdle;
Optional<System::Clock::Milliseconds32> mrpRetryIntervalActive;
Optional<System::Clock::Milliseconds16> mrpRetryActiveThreshold;
std::optional<bool> isICDOperatingAsLIT;
std::optional<System::Clock::Milliseconds32> mrpRetryIntervalIdle;
std::optional<System::Clock::Milliseconds32> mrpRetryIntervalActive;
std::optional<System::Clock::Milliseconds16> mrpRetryActiveThreshold;

CommonResolutionData() { Reset(); }

Expand All @@ -103,32 +103,32 @@ struct CommonResolutionData
ReliableMessageProtocolConfig GetRemoteMRPConfig() const
{
const ReliableMessageProtocolConfig defaultConfig = GetDefaultMRPConfig();
return ReliableMessageProtocolConfig(GetMrpRetryIntervalIdle().ValueOr(defaultConfig.mIdleRetransTimeout),
GetMrpRetryIntervalActive().ValueOr(defaultConfig.mActiveRetransTimeout),
GetMrpRetryActiveThreshold().ValueOr(defaultConfig.mActiveThresholdTime));
return ReliableMessageProtocolConfig(GetMrpRetryIntervalIdle().value_or(defaultConfig.mIdleRetransTimeout),
GetMrpRetryIntervalActive().value_or(defaultConfig.mActiveRetransTimeout),
GetMrpRetryActiveThreshold().value_or(defaultConfig.mActiveThresholdTime));
}
Optional<System::Clock::Milliseconds32> GetMrpRetryIntervalIdle() const { return mrpRetryIntervalIdle; }
Optional<System::Clock::Milliseconds32> GetMrpRetryIntervalActive() const { return mrpRetryIntervalActive; }
Optional<System::Clock::Milliseconds16> GetMrpRetryActiveThreshold() const { return mrpRetryActiveThreshold; }
std::optional<System::Clock::Milliseconds32> GetMrpRetryIntervalIdle() const { return mrpRetryIntervalIdle; }
std::optional<System::Clock::Milliseconds32> GetMrpRetryIntervalActive() const { return mrpRetryIntervalActive; }
std::optional<System::Clock::Milliseconds16> GetMrpRetryActiveThreshold() const { return mrpRetryActiveThreshold; }

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

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

void Reset()
{
memset(hostName, 0, sizeof(hostName));
mrpRetryIntervalIdle = NullOptional;
mrpRetryIntervalActive = NullOptional;
mrpRetryActiveThreshold = NullOptional;
isICDOperatingAsLIT = NullOptional;
mrpRetryIntervalIdle = std::nullopt;
mrpRetryIntervalActive = std::nullopt;
mrpRetryActiveThreshold = std::nullopt;
isICDOperatingAsLIT = std::nullopt;
numIPs = 0;
port = 0;
supportsTcp = false;
Expand Down Expand Up @@ -157,34 +157,34 @@ struct CommonResolutionData
{
ChipLogDetail(Discovery, "\tPort: %u", port);
}
if (mrpRetryIntervalIdle.HasValue())
if (mrpRetryIntervalIdle.has_value())
{
ChipLogDetail(Discovery, "\tMrp Interval idle: %" PRIu32 " ms", mrpRetryIntervalIdle.Value().count());
ChipLogDetail(Discovery, "\tMrp Interval idle: %" PRIu32 " ms", mrpRetryIntervalIdle->count());
}
else
{
ChipLogDetail(Discovery, "\tMrp Interval idle: not present");
}
if (mrpRetryIntervalActive.HasValue())
if (mrpRetryIntervalActive.has_value())
{
ChipLogDetail(Discovery, "\tMrp Interval active: %" PRIu32 " ms", mrpRetryIntervalActive.Value().count());
ChipLogDetail(Discovery, "\tMrp Interval active: %" PRIu32 " ms", mrpRetryIntervalActive->count());
}
else
{
ChipLogDetail(Discovery, "\tMrp Interval active: not present");
}
if (mrpRetryActiveThreshold.HasValue())
if (mrpRetryActiveThreshold.has_value())
{
ChipLogDetail(Discovery, "\tMrp Active Threshold: %u ms", mrpRetryActiveThreshold.Value().count());
ChipLogDetail(Discovery, "\tMrp Active Threshold: %u ms", mrpRetryActiveThreshold->count());
}
else
{
ChipLogDetail(Discovery, "\tMrp Active Threshold: not present");
}
ChipLogDetail(Discovery, "\tTCP Supported: %d", supportsTcp);
if (isICDOperatingAsLIT.HasValue())
if (isICDOperatingAsLIT.has_value())
{
ChipLogDetail(Discovery, "\tThe ICD operates in %s", isICDOperatingAsLIT.Value() ? "LIT" : "SIT");
ChipLogDetail(Discovery, "\tThe ICD operates in %s", *isICDOperatingAsLIT ? "LIT" : "SIT");
}
else
{
Expand Down
10 changes: 4 additions & 6 deletions src/lib/dnssd/tests/TestIncrementalResolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,8 @@ TEST(TestIncrementalResolve, TestParseOperational)
EXPECT_EQ(nodeData.resolutionData.numIPs, 1u);
EXPECT_EQ(nodeData.resolutionData.port, 0x1234);
EXPECT_FALSE(nodeData.resolutionData.supportsTcp);
EXPECT_FALSE(nodeData.resolutionData.GetMrpRetryIntervalActive().HasValue());
EXPECT_TRUE(nodeData.resolutionData.GetMrpRetryIntervalIdle().HasValue());
EXPECT_EQ(nodeData.resolutionData.GetMrpRetryIntervalIdle().Value(), chip::System::Clock::Milliseconds32(23));
EXPECT_FALSE(nodeData.resolutionData.GetMrpRetryIntervalActive().has_value());
EXPECT_EQ(nodeData.resolutionData.GetMrpRetryIntervalIdle(), std::make_optional(chip::System::Clock::Milliseconds32(23)));

Inet::IPAddress addr;
EXPECT_TRUE(Inet::IPAddress::FromString("fe80::abcd:ef11:2233:4455", addr));
Expand Down Expand Up @@ -396,9 +395,8 @@ TEST(TestIncrementalResolve, TestParseCommissionable)
EXPECT_EQ(nodeData.numIPs, 2u);
EXPECT_EQ(nodeData.port, 0x1234);
EXPECT_FALSE(nodeData.supportsTcp);
EXPECT_TRUE(nodeData.GetMrpRetryIntervalActive().HasValue());
EXPECT_EQ(nodeData.GetMrpRetryIntervalActive().Value(), chip::System::Clock::Milliseconds32(321));
EXPECT_FALSE(nodeData.GetMrpRetryIntervalIdle().HasValue());
EXPECT_EQ(nodeData.GetMrpRetryIntervalActive(), std::make_optional(chip::System::Clock::Milliseconds32(321)));
EXPECT_FALSE(nodeData.GetMrpRetryIntervalIdle().has_value());

Inet::IPAddress addr;
EXPECT_TRUE(Inet::IPAddress::FromString("fe80::abcd:ef11:2233:4455", addr));
Expand Down
Loading

0 comments on commit 6004917

Please sign in to comment.