Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dns-sd] Extended discovery improvements #16290

Merged
merged 6 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 32 additions & 42 deletions src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <lib/core/Optional.h>
#include <lib/dnssd/Advertiser.h>
#include <lib/dnssd/ServiceNaming.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <lib/support/Span.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/ReliableMessageProtocolConfig.h>
Expand Down Expand Up @@ -78,34 +79,34 @@ bool DnssdServer::HaveOperationalCredentials()
}
}

ChipLogProgress(Discovery, "Failed to find a valid admin pairing. Node ID unknown");
return false;
}

#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY

constexpr const char kExtendedDiscoveryTimeoutKeypairStorage[] = "ExtDiscKey";

void DnssdServer::SetExtendedDiscoveryTimeoutSecs(int16_t secs)
{
ChipLogDetail(Discovery, "SetExtendedDiscoveryTimeoutSecs %d", secs);
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(kExtendedDiscoveryTimeoutKeypairStorage, &secs, sizeof(secs));
ChipLogDetail(Discovery, "Setting extended discovery timeout to %" PRId16 "s", secs);
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(DefaultStorageKeyAllocator::DNSExtendedDiscoveryTimeout(), secs);
}

int16_t DnssdServer::GetExtendedDiscoveryTimeoutSecs()
{
CHIP_ERROR err = CHIP_NO_ERROR;
int16_t secs;
CHIP_ERROR err = chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(
DefaultStorageKeyAllocator::DNSExtendedDiscoveryTimeout(), &secs);

err = chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(kExtendedDiscoveryTimeoutKeypairStorage, &secs, sizeof(secs));
if (err != CHIP_NO_ERROR)
if (err == CHIP_NO_ERROR)
{
return secs;
}

if (err != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
ChipLogError(Discovery, "Failed to get extended timeout configuration err: %s", chip::ErrorStr(err));
secs = CHIP_DEVICE_CONFIG_EXTENDED_DISCOVERY_TIMEOUT_SECS;
ChipLogError(Discovery, "Failed to load extended discovery timeout: %" CHIP_ERROR_FORMAT, err.Format());
}

ChipLogDetail(Discovery, "GetExtendedDiscoveryTimeoutSecs %d", secs);
return secs;
return CHIP_DEVICE_CONFIG_EXTENDED_DISCOVERY_TIMEOUT_SECS;
}

/// Callback from Extended Discovery Expiration timer
Expand All @@ -118,11 +119,11 @@ void DnssdServer::OnExtendedDiscoveryExpiration(System::Layer * aSystemLayer, vo
{
if (!DnssdServer::OnExpiration(mExtendedDiscoveryExpiration))
{
ChipLogDetail(Discovery, "OnExtendedDiscoveryExpiration callback for cleared session");
ChipLogDetail(Discovery, "Extended discovery timeout cancelled");
return;
}

ChipLogDetail(Discovery, "OnExtendedDiscoveryExpiration callback for valid session");
ChipLogDetail(Discovery, "Extended discovery timed out");

mExtendedDiscoveryExpiration = kTimeoutCleared;
}
Expand Down Expand Up @@ -220,7 +221,7 @@ CHIP_ERROR DnssdServer::ScheduleDiscoveryExpiration()
{
return CHIP_NO_ERROR;
}
ChipLogDetail(Discovery, "Scheduling Discovery timeout in secs=%d", mDiscoveryTimeoutSecs);
ChipLogDetail(Discovery, "Scheduling discovery timeout in %" PRId16 "s", mDiscoveryTimeoutSecs);

mDiscoveryExpiration = mTimeSource.GetMonotonicTimestamp() + System::Clock::Seconds16(mDiscoveryTimeoutSecs);

Expand All @@ -236,7 +237,7 @@ CHIP_ERROR DnssdServer::ScheduleExtendedDiscoveryExpiration()
{
return CHIP_NO_ERROR;
}
ChipLogDetail(Discovery, "Scheduling Extended Discovery timeout in secs=%d", extendedDiscoveryTimeoutSecs);
ChipLogDetail(Discovery, "Scheduling extended discovery timeout in %" PRId16 "s", extendedDiscoveryTimeoutSecs);

mExtendedDiscoveryExpiration = mTimeSource.GetMonotonicTimestamp() + System::Clock::Seconds16(extendedDiscoveryTimeoutSecs);

Expand Down Expand Up @@ -448,7 +449,7 @@ void DnssdServer::StartServer()

void DnssdServer::StartServer(Dnssd::CommissioningMode mode)
{
ChipLogDetail(Discovery, "DNS-SD StartServer mode=%d", static_cast<int>(mode));
ChipLogProgress(Discovery, "Updating services using commissioning mode %d", static_cast<int>(mode));

ClearTimeouts();

Expand All @@ -472,44 +473,33 @@ void DnssdServer::StartServer(Dnssd::CommissioningMode mode)
ChipLogError(Discovery, "Failed to advertise operational node: %s", chip::ErrorStr(err));
}

if (HaveOperationalCredentials())
if (mode != chip::Dnssd::CommissioningMode::kDisabled)
{
ChipLogProgress(Discovery, "Have operational credentials");
if (mode != chip::Dnssd::CommissioningMode::kDisabled)
err = AdvertiseCommissionableNode(mode);
if (err != CHIP_NO_ERROR)
{
err = AdvertiseCommissionableNode(mode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise commissionable node: %s", chip::ErrorStr(err));
}
// no need to set timeout because callers are currently doing that and their timeout might be longer than the default
ChipLogError(Discovery, "Failed to advertise commissionable node: %s", chip::ErrorStr(err));
}
#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
else if (GetExtendedDiscoveryTimeoutSecs() != CHIP_DEVICE_CONFIG_DISCOVERY_DISABLED)

// If any fabrics exist, the commissioning window must have been opened by the administrator
// commissioning cluster commands which take care of the timeout.
if (!HaveOperationalCredentials())
{
err = AdvertiseCommissionableNode(mode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise extended commissionable node: %s", chip::ErrorStr(err));
}
// set timeout
ScheduleExtendedDiscoveryExpiration();
ScheduleDiscoveryExpiration();
Damian-Nordic marked this conversation as resolved.
Show resolved Hide resolved
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
}
else
#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
else if (GetExtendedDiscoveryTimeoutSecs() != CHIP_DEVICE_CONFIG_DISCOVERY_DISABLED)
{
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY
ChipLogProgress(Discovery, "Start dns-sd server - no current nodeId");
err = AdvertiseCommissionableNode(mode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise unprovisioned commissionable node: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to advertise extended commissionable node: %s", chip::ErrorStr(err));
}
// set timeout
ScheduleDiscoveryExpiration();
#endif
ScheduleExtendedDiscoveryExpiration();
Damian-Nordic marked this conversation as resolved.
Show resolved Hide resolved
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
err = AdvertiseCommissioner();
Expand Down
23 changes: 2 additions & 21 deletions src/include/platform/CHIPDeviceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1162,32 +1162,13 @@

// -------------------- Device DNS-SD Configuration --------------------

/**
* CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY
*
* Enable MDNS commissionable node advertising when not yet provisioned.
*
* This should be 1 for WiFi SoftAP devices, ethernet devices, and (probably) bridge devices
*
* This should be 0 for Thread/BLE devices and WiFi/BLE devices
*/
#ifndef CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 0
#else
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1
#endif
#endif

/**
* CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS
*
* Time in seconds that a factory new device will advertise commissionable node discovery.
*
* Only valid when CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY==1
*/
#ifndef CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS 15 * 60
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS (15 * 60)
#endif

/**
Expand Down Expand Up @@ -1256,7 +1237,7 @@
*/
#define CHIP_DEVICE_CONFIG_DISCOVERY_DISABLED 0
#define CHIP_DEVICE_CONFIG_DISCOVERY_NO_TIMEOUT -1
#define CHIP_DEVICE_CONFIG_EXTENDED_DISCOVERY_TIMEOUT_SECS CHIP_DEVICE_CONFIG_DISCOVERY_NO_TIMEOUT
#define CHIP_DEVICE_CONFIG_EXTENDED_DISCOVERY_TIMEOUT_SECS (15 * 60)

/**
* CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DEVICE_TYPE
Expand Down
3 changes: 3 additions & 0 deletions src/lib/support/DefaultStorageKeyAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class DefaultStorageKeyAllocator
static const char * OTACurrentProvider() { return "o/cp"; }
static const char * OTAUpdateToken() { return "o/ut"; }

// [G]lobal [D]NS-related keys
static const char * DNSExtendedDiscoveryTimeout() { return "g/d/edt"; }

private:
// The ENFORCE_FORMAT args are "off by one" because this is a class method,
// with an implicit "this" as first arg.
Expand Down
2 changes: 0 additions & 2 deletions src/platform/Darwin/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 0

#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1

// ========== Platform-specific Configuration =========

// These are configuration options that are unique to Darwin platforms.
Expand Down
1 change: 0 additions & 1 deletion src/platform/EFR32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#endif /* defined(SL_WIFI) */

#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1

#define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE 1

Expand Down
2 changes: 0 additions & 2 deletions src/platform/Linux/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@

#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 0

#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1

// ========== Platform-specific Configuration =========

// These are configuration options that are unique to Linux platforms.
Expand Down
1 change: 0 additions & 1 deletion src/platform/cc13x2_26x2/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@

#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT 1
#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1
1 change: 0 additions & 1 deletion src/platform/nrfconnect/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT 1
#define CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES (CHIP_CONFIG_MAX_FABRICS + 1)
#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1
#ifdef CONFIG_CHIP_ENABLE_DNS_CLIENT
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT 1
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY 1
Expand Down
1 change: 0 additions & 1 deletion src/platform/nxp/k32w/k32w0/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,4 @@
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT 1
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT 1
#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1
#endif
1 change: 0 additions & 1 deletion src/platform/qpg/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#endif

#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1

#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 0

Expand Down
2 changes: 0 additions & 2 deletions src/platform/webos/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@

#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 0

#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONABLE_DISCOVERY 1

// ========== Platform-specific Configuration =========

// These are configuration options that are unique to webOs platforms.
Expand Down
5 changes: 3 additions & 2 deletions src/test_driver/linux-cirque/CommissioningTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
CHIP_REPO = os.path.join(os.path.abspath(
os.path.dirname(__file__)), "..", "..", "..")
TEST_EXTPANID = "fedcba9876543210"
TEST_DISCRIMINATOR = 3840

DEVICE_CONFIG = {
'device0': {
Expand Down Expand Up @@ -81,8 +82,8 @@ def run_controller_test(self):
if device['type'] == 'MobileDevice']

for server in server_ids:
self.execute_device_cmd(server, "CHIPCirqueDaemon.py -- run gdb -return-child-result -q -ex \"set pagination off\" -ex run -ex \"bt 25\" --args {} --thread".format(
os.path.join(CHIP_REPO, "out/debug/standalone/chip-all-clusters-app")))
self.execute_device_cmd(server, "CHIPCirqueDaemon.py -- run gdb -return-child-result -q -ex \"set pagination off\" -ex run -ex \"bt 25\" --args {} --thread --discriminator {}".format(
os.path.join(CHIP_REPO, "out/debug/standalone/chip-all-clusters-app"), TEST_DISCRIMINATOR))

self.reset_thread_devices(server_ids)

Expand Down
5 changes: 3 additions & 2 deletions src/test_driver/linux-cirque/MobileDeviceTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
CHIP_REPO = os.path.join(os.path.abspath(
os.path.dirname(__file__)), "..", "..", "..")
TEST_EXTPANID = "fedcba9876543210"
TEST_DISCRIMINATOR = 3840

DEVICE_CONFIG = {
'device0': {
Expand Down Expand Up @@ -81,8 +82,8 @@ def run_controller_test(self):
if device['type'] == 'MobileDevice']

for server in server_ids:
self.execute_device_cmd(server, "CHIPCirqueDaemon.py -- run gdb -return-child-result -q -ex \"set pagination off\" -ex run -ex \"bt 25\" --args {} --thread".format(
os.path.join(CHIP_REPO, "out/debug/standalone/chip-all-clusters-app")))
self.execute_device_cmd(server, "CHIPCirqueDaemon.py -- run gdb -return-child-result -q -ex \"set pagination off\" -ex run -ex \"bt 25\" --args {} --thread --discriminator {}".format(
os.path.join(CHIP_REPO, "out/debug/standalone/chip-all-clusters-app"), TEST_DISCRIMINATOR))

self.reset_thread_devices(server_ids)

Expand Down