Skip to content

Commit 03fc117

Browse files
committed
Add utility to get user default values for configurable parameters for darwin
- Add support for getting a user default value for the SRP resolve timeout for DNS-SD. If user default value exists, ovveride the timeout. - Update the SRP resolve timeout to 3 secs The default can be set by using `defaults write org.csa-iot.matter.darwindefaults SRPTimeoutOverride <timeoutinMsecs>`
1 parent 8a4dffc commit 03fc117

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

src/platform/Darwin/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ static_library("Darwin") {
7676
"PosixConfig.h",
7777
"SystemPlatformConfig.h",
7878
"SystemTimeSupport.cpp",
79+
"UserDefaultUtils.mm",
80+
"UserDefaultUtils.h"
7981
]
8082

8183
if (chip_enable_wifi) {

src/platform/Darwin/DnssdImpl.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "DnssdImpl.h"
1818
#include "DnssdType.h"
1919
#include "MdnsError.h"
20+
#include "UserDefaultUtils.h"
2021

2122
#include <cstdio>
2223

@@ -29,6 +30,7 @@
2930

3031
using namespace chip::Dnssd;
3132
using namespace chip::Dnssd::Internal;
33+
using namespace chip::DeviceLayer::Utils;
3234

3335
namespace {
3436

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

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

4244
constexpr DNSServiceFlags kRegisterFlags = kDNSServiceFlagsNoAutoRename;
4345
constexpr DNSServiceFlags kBrowseFlags = kDNSServiceFlagsShareConnection;
@@ -76,8 +78,16 @@ void LogOnFailure(const char * name, DNSServiceErrorType err)
7678
*/
7779
CHIP_ERROR StartSRPTimer(uint16_t timeoutInMSecs, ResolveContext * ctx)
7880
{
81+
// Check to see if an user default value exists for the SRP timeout. If it does, override the timeoutInMSecs with user default value.
82+
// To override the timeout value, use ` defaults write org.csa-iot.matter.darwindefaults SRPTimeoutOverride <timeoutinMsecs>`
83+
// See UserDefaultUtils.mm for details
84+
uint16_t userDefaultSRPTimeout = getUserDefaultDnssdSRPTimeout();
85+
if (userDefaultSRPTimeout)
86+
{
87+
timeoutInMSecs = userDefaultSRPTimeout;
88+
}
7989
VerifyOrReturnValue(ctx != nullptr, CHIP_ERROR_INCORRECT_STATE);
80-
ChipLogProgress(Discovery, "Starting timer to wait for possible SRP resolve results for %s", ctx->instanceName.c_str());
90+
ChipLogProgress(Discovery, "Starting timer to wait for %d milliseconds for possible SRP resolve results for %s", timeoutInMSecs, ctx->instanceName.c_str());
8191
return chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds16(timeoutInMSecs),
8292
ResolveContext::SRPTimerExpiredCallback, static_cast<void *>(ctx));
8393
}

src/platform/Darwin/DnssdImpl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ struct ResolveContext : public GenericContext
262262
std::shared_ptr<uint32_t> consumerCounter;
263263
BrowseContext * const browseThatCausedResolve; // Can be null
264264

265-
// Indicates whether the timer for 250 msecs should be started
266-
// to give the resolve on SRP domain some extra time to complete.
265+
// Indicates whether the timer should be started to give the resolve
266+
// on SRP domain some extra time to complete.
267267
bool shouldStartSRPTimerForResolve = false;
268268
bool isSRPTimerRunning = false;
269269

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace chip {
19+
namespace DeviceLayer {
20+
namespace Utils {
21+
22+
uint16_t getUserDefaultDnssdSRPTimeout();
23+
24+
} // namespace Utils
25+
} // namespace DeviceLayer
26+
} // namespace chip
27+
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @file
20+
* Implementation for an utility to set configurable parametes via user defaults.
21+
*/
22+
#import <Foundation/Foundation.h>
23+
#import "UserDefaultUtils.h"
24+
25+
26+
static NSString * const kUserDefaultDomain = @"org.csa-iot.matter.darwindefaults";
27+
static NSString * const kSRPTimeoutUserDefaultKey = @"SRPTimeoutOverride";
28+
29+
namespace chip {
30+
namespace DeviceLayer {
31+
namespace Utils {
32+
33+
uint16_t getUserDefaultDnssdSRPTimeout()
34+
{
35+
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kUserDefaultDomain];
36+
return static_cast<uint16_t>([defaults integerForKey:kSRPTimeoutUserDefaultKey]);
37+
}
38+
39+
} // namespace Utils
40+
} // namespace DeviceLayer
41+
} // namespace chip

0 commit comments

Comments
 (0)