Skip to content

Commit 45ff4a8

Browse files
committed
Return an optional uint16_t value from GetUserDefaultDnssdSRPTimeoutInMSecs
1 parent 1a3027e commit 45ff4a8

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/platform/Darwin/DnssdImpl.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
using namespace chip::Dnssd;
3232
using namespace chip::Dnssd::Internal;
33-
using namespace chip::DeviceLayer;
33+
using namespace chip::Platform;
3434

3535
namespace {
3636

@@ -39,7 +39,7 @@ constexpr char kLocalDot[] = "local.";
3939
constexpr char kSRPDot[] = "default.service.arpa.";
4040

4141
// The extra time in milliseconds that we will wait for the resolution on the SRP domain to complete.
42-
constexpr uint16_t kSRPTimeoutInMsec = 3000;
42+
constexpr uint16_t kSRPTimeoutInMsec = 250;
4343

4444
constexpr DNSServiceFlags kRegisterFlags = kDNSServiceFlagsNoAutoRename;
4545
constexpr DNSServiceFlags kBrowseFlags = kDNSServiceFlagsShareConnection;
@@ -81,11 +81,8 @@ CHIP_ERROR StartSRPTimer(uint16_t timeoutInMSecs, ResolveContext * ctx)
8181
// Check to see if a user default value exists for the SRP timeout. If it does, override the timeoutInMSecs with user default
8282
// value. To override the timeout value, use ` defaults write org.csa-iot.matter.darwin SRPTimeoutInMSecsOverride
8383
// <timeoutinMsecs>` See UserDefaults.mm for details.
84-
uint16_t userDefaultSRPTimeoutInMsecs = getUserDefaultDnssdSRPTimeoutInMSecs();
85-
if (userDefaultSRPTimeoutInMsecs)
86-
{
87-
timeoutInMSecs = userDefaultSRPTimeoutInMsecs;
88-
}
84+
timeoutInMSecs = GetUserDefaultDnssdSRPTimeoutInMSecs().value_or(timeoutInMSecs);
85+
8986
VerifyOrReturnValue(ctx != nullptr, CHIP_ERROR_INCORRECT_STATE);
9087
ChipLogProgress(Discovery, "Starting timer to wait for %d milliseconds for possible SRP resolve results for %s", timeoutInMSecs,
9188
ctx->instanceName.c_str());

src/platform/Darwin/UserDefaults.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#pragma once
1818

1919
namespace chip {
20-
namespace DeviceLayer {
20+
namespace Platform {
2121

22-
uint16_t GetUserDefaultDnssdSRPTimeoutInMSecs();
22+
std::optional<uint16_t> GetUserDefaultDnssdSRPTimeoutInMSecs();
2323

24-
} // namespace DeviceLayer
24+
} // namespace Platform
2525
} // namespace chip

src/platform/Darwin/UserDefaults.mm

+14-4
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,28 @@
1717

1818
#import <Foundation/Foundation.h>
1919

20+
#include <lib/support/logging/CHIPLogging.h>
21+
#include <lib/support/SafeInt.h>
22+
#include <optional>
23+
2024
static NSString * const kUserDefaultDomain = @"org.csa-iot.matter.darwin";
2125
static NSString * const kSRPTimeoutInMsecsUserDefaultKey = @"SRPTimeoutInMSecsOverride";
2226

2327
namespace chip {
24-
namespace DeviceLayer {
28+
namespace Platform {
2529

26-
uint16_t getUserDefaultDnssdSRPTimeoutInMSecs()
30+
std::optional<uint16_t> GetUserDefaultDnssdSRPTimeoutInMSecs()
2731
{
2832
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kUserDefaultDomain];
2933
NSInteger srpTimeoutValue = [defaults integerForKey:kSRPTimeoutInMsecsUserDefaultKey];
30-
return (srpTimeoutValue > 0 && srpTimeoutValue < UINT16_MAX) ? static_cast<uint16_t>(srpTimeoutValue) : 0;
34+
if (CanCastTo<uint16_t>(srpTimeoutValue))
35+
{
36+
uint16_t timeoutinMsecs = static_cast<uint16_t>(srpTimeoutValue);
37+
ChipLogProgress(Discovery, "Got a user default value for Dnssd SRP timeout - %d msecs", timeoutinMsecs);
38+
return std::make_optional(timeoutinMsecs);
39+
}
40+
return std::nullopt;
3141
}
3242

33-
} // namespace DeviceLayer
43+
} // namespace Platform
3444
} // namespace chip

0 commit comments

Comments
 (0)