Skip to content

Commit b4650b9

Browse files
nivi-applerestyled-commitsbzbarsky-apple
authored
Add utility to get user default values for configurable parameters fo… (#33056)
* 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>` * Restyled by whitespace * Restyled by clang-format * Restyled by gn * Address review comments * Restyled by clang-format * Add a check for filtering negative values and returning 0 * Apply suggestions from code review Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Return an optional uint16_t value from GetUserDefaultDnssdSRPTimeoutInMSecs * Restyled by clang-format * Move the inclusion of optional header file in UserDefaults.h * Include UserDefaults.h header in UserDefaults.mm file * Include header cstdint from UserDefaults.h --------- Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent b008715 commit b4650b9

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
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+
"UserDefaults.h",
80+
"UserDefaults.mm",
7981
]
8082

8183
if (chip_enable_wifi) {

src/platform/Darwin/DnssdImpl.cpp

+9-1
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 "UserDefaults.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::Platform;
3234

3335
namespace {
3436

@@ -76,8 +78,14 @@ void LogOnFailure(const char * name, DNSServiceErrorType err)
7678
*/
7779
CHIP_ERROR StartSRPTimer(uint16_t timeoutInMSecs, ResolveContext * ctx)
7880
{
81+
// Check to see if a user default value exists for the SRP timeout. If it does, override the timeoutInMSecs with user default
82+
// value. To override the timeout value, use ` defaults write org.csa-iot.matter.darwin SRPTimeoutInMSecsOverride
83+
// <timeoutinMsecs>` See UserDefaults.mm for details.
84+
timeoutInMSecs = GetUserDefaultDnssdSRPTimeoutInMSecs().value_or(timeoutInMSecs);
85+
7986
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());
87+
ChipLogProgress(Discovery, "Starting timer to wait for %d milliseconds for possible SRP resolve results for %s", timeoutInMSecs,
88+
ctx->instanceName.c_str());
8189
return chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds16(timeoutInMSecs),
8290
ResolveContext::SRPTimerExpiredCallback, static_cast<void *>(ctx));
8391
}

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

src/platform/Darwin/UserDefaults.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <cstdint>
20+
#include <optional>
21+
22+
namespace chip {
23+
namespace Platform {
24+
25+
std::optional<uint16_t> GetUserDefaultDnssdSRPTimeoutInMSecs();
26+
27+
} // namespace Platform
28+
} // namespace chip

src/platform/Darwin/UserDefaults.mm

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
#include "UserDefaults.h"
19+
#include <lib/support/SafeInt.h>
20+
#include <lib/support/logging/CHIPLogging.h>
21+
22+
#import <Foundation/Foundation.h>
23+
24+
static NSString * const kUserDefaultDomain = @"org.csa-iot.matter.darwin";
25+
static NSString * const kSRPTimeoutInMsecsUserDefaultKey = @"SRPTimeoutInMSecsOverride";
26+
27+
namespace chip {
28+
namespace Platform {
29+
30+
std::optional<uint16_t> GetUserDefaultDnssdSRPTimeoutInMSecs()
31+
{
32+
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kUserDefaultDomain];
33+
NSInteger srpTimeoutValue = [defaults integerForKey:kSRPTimeoutInMsecsUserDefaultKey];
34+
if (CanCastTo<uint16_t>(srpTimeoutValue)) {
35+
uint16_t timeoutinMsecs = static_cast<uint16_t>(srpTimeoutValue);
36+
ChipLogProgress(Discovery, "Got a user default value for Dnssd SRP timeout - %d msecs", timeoutinMsecs);
37+
return std::make_optional(timeoutinMsecs);
38+
}
39+
return std::nullopt;
40+
}
41+
42+
} // namespace Platform
43+
} // namespace chip

0 commit comments

Comments
 (0)