From b4650b9db0ffc80a87bb03c2f5660d1b6247a61f Mon Sep 17 00:00:00 2001 From: Nivi Sarkar <55898241+nivi-apple@users.noreply.github.com> Date: Fri, 19 Apr 2024 22:50:56 -0700 Subject: [PATCH] =?UTF-8?q?Add=20utility=20to=20get=20user=20default=20val?= =?UTF-8?q?ues=20for=20configurable=20parameters=20fo=E2=80=A6=20(#33056)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 ` * 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 * 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 Co-authored-by: Boris Zbarsky --- src/platform/Darwin/BUILD.gn | 2 ++ src/platform/Darwin/DnssdImpl.cpp | 10 ++++++- src/platform/Darwin/DnssdImpl.h | 4 +-- src/platform/Darwin/UserDefaults.h | 28 +++++++++++++++++++ src/platform/Darwin/UserDefaults.mm | 43 +++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/platform/Darwin/UserDefaults.h create mode 100644 src/platform/Darwin/UserDefaults.mm diff --git a/src/platform/Darwin/BUILD.gn b/src/platform/Darwin/BUILD.gn index 8728178735404d..9dbb8460ae0566 100644 --- a/src/platform/Darwin/BUILD.gn +++ b/src/platform/Darwin/BUILD.gn @@ -76,6 +76,8 @@ static_library("Darwin") { "PosixConfig.h", "SystemPlatformConfig.h", "SystemTimeSupport.cpp", + "UserDefaults.h", + "UserDefaults.mm", ] if (chip_enable_wifi) { diff --git a/src/platform/Darwin/DnssdImpl.cpp b/src/platform/Darwin/DnssdImpl.cpp index 4156c7a4bf0924..b80e5958310e68 100644 --- a/src/platform/Darwin/DnssdImpl.cpp +++ b/src/platform/Darwin/DnssdImpl.cpp @@ -17,6 +17,7 @@ #include "DnssdImpl.h" #include "DnssdType.h" #include "MdnsError.h" +#include "UserDefaults.h" #include @@ -29,6 +30,7 @@ using namespace chip::Dnssd; using namespace chip::Dnssd::Internal; +using namespace chip::Platform; namespace { @@ -76,8 +78,14 @@ void LogOnFailure(const char * name, DNSServiceErrorType err) */ CHIP_ERROR StartSRPTimer(uint16_t timeoutInMSecs, ResolveContext * ctx) { + // Check to see if a user default value exists for the SRP timeout. If it does, override the timeoutInMSecs with user default + // value. To override the timeout value, use ` defaults write org.csa-iot.matter.darwin SRPTimeoutInMSecsOverride + // ` See UserDefaults.mm for details. + timeoutInMSecs = GetUserDefaultDnssdSRPTimeoutInMSecs().value_or(timeoutInMSecs); + VerifyOrReturnValue(ctx != nullptr, CHIP_ERROR_INCORRECT_STATE); - ChipLogProgress(Discovery, "Starting timer to wait for possible SRP resolve results for %s", ctx->instanceName.c_str()); + ChipLogProgress(Discovery, "Starting timer to wait for %d milliseconds for possible SRP resolve results for %s", timeoutInMSecs, + ctx->instanceName.c_str()); return chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds16(timeoutInMSecs), ResolveContext::SRPTimerExpiredCallback, static_cast(ctx)); } diff --git a/src/platform/Darwin/DnssdImpl.h b/src/platform/Darwin/DnssdImpl.h index 42ae55fdd9d4c0..3bf78fac6cb2bd 100644 --- a/src/platform/Darwin/DnssdImpl.h +++ b/src/platform/Darwin/DnssdImpl.h @@ -262,8 +262,8 @@ struct ResolveContext : public GenericContext std::shared_ptr consumerCounter; BrowseContext * const browseThatCausedResolve; // Can be null - // Indicates whether the timer for 250 msecs should be started - // to give the resolve on SRP domain some extra time to complete. + // Indicates whether the timer should be started to give the resolve + // on SRP domain some extra time to complete. bool shouldStartSRPTimerForResolve = false; bool isSRPTimerRunning = false; diff --git a/src/platform/Darwin/UserDefaults.h b/src/platform/Darwin/UserDefaults.h new file mode 100644 index 00000000000000..5df5d2064256b9 --- /dev/null +++ b/src/platform/Darwin/UserDefaults.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace chip { +namespace Platform { + +std::optional GetUserDefaultDnssdSRPTimeoutInMSecs(); + +} // namespace Platform +} // namespace chip diff --git a/src/platform/Darwin/UserDefaults.mm b/src/platform/Darwin/UserDefaults.mm new file mode 100644 index 00000000000000..d24b0a4ee20f37 --- /dev/null +++ b/src/platform/Darwin/UserDefaults.mm @@ -0,0 +1,43 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UserDefaults.h" +#include +#include + +#import + +static NSString * const kUserDefaultDomain = @"org.csa-iot.matter.darwin"; +static NSString * const kSRPTimeoutInMsecsUserDefaultKey = @"SRPTimeoutInMSecsOverride"; + +namespace chip { +namespace Platform { + + std::optional GetUserDefaultDnssdSRPTimeoutInMSecs() + { + NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kUserDefaultDomain]; + NSInteger srpTimeoutValue = [defaults integerForKey:kSRPTimeoutInMsecsUserDefaultKey]; + if (CanCastTo(srpTimeoutValue)) { + uint16_t timeoutinMsecs = static_cast(srpTimeoutValue); + ChipLogProgress(Discovery, "Got a user default value for Dnssd SRP timeout - %d msecs", timeoutinMsecs); + return std::make_optional(timeoutinMsecs); + } + return std::nullopt; + } + +} // namespace Platform +} // namespace chip