From cfd131687a9838072e382cff846fbabba536169e Mon Sep 17 00:00:00 2001 From: Damian Krolik Date: Fri, 16 Jul 2021 14:14:16 +0200 Subject: [PATCH] [nrfconnect] Enable commissionable node advertising 1. Clean up DNS-SD default configuration as different settings were defined in two different places. 2. Fix spawning kServiceProvisioningChange event for OpenThread platforms for cases where provided dataset is incomplete and must be pulled from a network leader. 3. Clean up conditions for enabling commissionable node advertising. 4. Enable commissionable node advertising for nRF Connect platform. --- .../platform/nrfconnect/util/ThreadUtil.cpp | 8 +- src/app/server/Mdns.cpp | 30 ++-- src/app/server/RendezvousServer.cpp | 15 +- src/include/platform/CHIPDeviceConfig.h | 142 +++++++++--------- src/include/platform/ConnectivityManager.h | 6 + .../GenericConfigurationManagerImpl.cpp | 8 +- .../internal/GenericConnectivityManagerImpl.h | 17 +++ ...nericThreadStackManagerImpl_OpenThread.cpp | 14 +- .../nrfconnect/CHIPDevicePlatformConfig.h | 2 + 9 files changed, 133 insertions(+), 109 deletions(-) diff --git a/examples/platform/nrfconnect/util/ThreadUtil.cpp b/examples/platform/nrfconnect/util/ThreadUtil.cpp index 4b751f5c069d38..c0315347156caf 100644 --- a/examples/platform/nrfconnect/util/ThreadUtil.cpp +++ b/examples/platform/nrfconnect/util/ThreadUtil.cpp @@ -19,7 +19,8 @@ #include "ThreadUtil.h" #include -#include + +#include #include #include @@ -33,18 +34,17 @@ void StartDefaultThreadNetwork(void) constexpr uint8_t masterkey[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; - constexpr uint8_t meshLocalPrefix[] = { 0xfd, 0x11, 0x11, 0x11, 0x11, 0x22, 0x00, 0x00 }; - net_bytes_from_str(xpanid, sizeof(xpanid), CONFIG_OPENTHREAD_XPANID); dataset.SetChannel(CONFIG_OPENTHREAD_CHANNEL); dataset.SetExtendedPanId(xpanid); dataset.SetMasterKey(masterkey); - dataset.SetMeshLocalPrefix(meshLocalPrefix); dataset.SetNetworkName(CONFIG_OPENTHREAD_NETWORK_NAME); dataset.SetPanId(CONFIG_OPENTHREAD_PANID); chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(false); chip::DeviceLayer::ThreadStackMgr().SetThreadProvision(dataset.AsByteSpan()); chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(true); + + chip::app::Mdns::StartServer(); } diff --git a/src/app/server/Mdns.cpp b/src/app/server/Mdns.cpp index 6a62056246da49..72d3fd2c1c61f2 100644 --- a/src/app/server/Mdns.cpp +++ b/src/app/server/Mdns.cpp @@ -48,16 +48,16 @@ NodeId GetCurrentNodeId() // mdns advertises a single node id as parameter. // Search for one admin pairing and use its node id. - auto pairing = GetGlobalAdminPairingTable().cbegin(); - if (pairing != GetGlobalAdminPairingTable().cend()) + for (const Transport::AdminPairingInfo & pairing : GetGlobalAdminPairingTable()) { - ChipLogProgress(Discovery, "Found admin pairing for admin %" PRIX16 ", node 0x" ChipLogFormatX64, pairing->GetAdminId(), - ChipLogValueX64(pairing->GetNodeId())); - return pairing->GetNodeId(); + if (pairing.GetNodeId() != kUndefinedNodeId) + { + return pairing.GetNodeId(); + } } - ChipLogError(Discovery, "Failed to find a valid admin pairing. Node ID unknown"); - return chip::kTestDeviceNodeId; + ChipLogProgress(Discovery, "Failed to find a valid admin pairing. Node ID unknown"); + return kUndefinedNodeId; } // Requires an 8-byte mac to accommodate thread. @@ -252,20 +252,17 @@ void StartServer() // TODO: advertise this only when really operational once we support both // operational and commisioning advertising is supported. - if (DeviceLayer::ConfigurationMgr().IsFullyProvisioned()) + if (DeviceLayer::ConfigurationMgr().IsFullyProvisioned() && GetCurrentNodeId() != kUndefinedNodeId) { - err = app::Mdns::AdvertiseOperational(); + SuccessOrExit(err = app::Mdns::AdvertiseOperational()); #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY - err = app::Mdns::AdvertiseCommissionableNode(); + SuccessOrExit(err = app::Mdns::AdvertiseCommissionableNode()); #endif } - else + else if (DeviceLayer::ConnectivityMgr().IsNetworkProvisioned()) { -// TODO: Thread devices are not able to advertise using mDNS before being provisioned, -// so configuraton should be added to enable commissioning advertising based on supported -// Rendezvous methods. -#if (!CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_UNPROVISIONED_MDNS) - err = app::Mdns::AdvertiseCommissionableNode(); +#if CHIP_DEVICE_CONFIG_ENABLE_UNPROVISIONED_MDNS + SuccessOrExit(err = app::Mdns::AdvertiseCommissionableNode()); #endif } @@ -273,6 +270,7 @@ void StartServer() err = app::Mdns::AdvertiseCommisioner(); #endif +exit: if (err != CHIP_NO_ERROR) { ChipLogError(Discovery, "Failed to start mDNS server: %s", chip::ErrorStr(err)); diff --git a/src/app/server/RendezvousServer.cpp b/src/app/server/RendezvousServer.cpp index 969cd3223d316f..f97ce08a668b63 100644 --- a/src/app/server/RendezvousServer.cpp +++ b/src/app/server/RendezvousServer.cpp @@ -47,8 +47,9 @@ static const char * kSpake2pKeyExchangeSalt = "SPAKE2P Key Salt"; void RendezvousServer::OnPlatformEvent(const DeviceLayer::ChipDeviceEvent * event) { - if (event->Type == DeviceLayer::DeviceEventType::kCommissioningComplete) + switch (event->Type) { + case DeviceLayer::DeviceEventType::kCommissioningComplete: if (event->CommissioningComplete.status == CHIP_NO_ERROR) { ChipLogProgress(Discovery, "Commissioning completed successfully"); @@ -59,11 +60,13 @@ void RendezvousServer::OnPlatformEvent(const DeviceLayer::ChipDeviceEvent * even ChipError::FormatError(event->CommissioningComplete.status)); } // TODO: Commissioning complete means we can finalize the admin in our storage - } - else if (event->Type == DeviceLayer::DeviceEventType::kOperationalNetworkEnabled) - { - app::Mdns::AdvertiseOperational(); - ChipLogError(Discovery, "Operational advertising enabled"); + break; + case DeviceLayer::DeviceEventType::kOperationalNetworkEnabled: + case DeviceLayer::DeviceEventType::kServiceProvisioningChange: + app::Mdns::StartServer(); + break; + default: + break; } } diff --git a/src/include/platform/CHIPDeviceConfig.h b/src/include/platform/CHIPDeviceConfig.h index a475df7d1a7f4a..dabf18d62e6d91 100644 --- a/src/include/platform/CHIPDeviceConfig.h +++ b/src/include/platform/CHIPDeviceConfig.h @@ -608,75 +608,6 @@ #define CHIP_DEVICE_CONFIG_ENABLE_JUST_IN_TIME_PROVISIONING 0 #endif -// -------------------- Service Discovery Configuration ----------------------- - -/** - * CHIP_DEVICE_CONFIG_ENABLE_MDNS - * - * Enable support to use MDNS for service advertising and discovery in CHIP. - */ -#ifndef CHIP_DEVICE_CONFIG_ENABLE_MDNS -#define CHIP_DEVICE_CONFIG_ENABLE_MDNS 0 -#endif - -/** - * CHIP_DEVICE_CONFIG_ENABLE_UNPROVISIONED_MDNS - * - * 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_UNPROVISIONED_MDNS -#define CHIP_DEVICE_CONFIG_ENABLE_UNPROVISIONED_MDNS 0 -#endif - -/** - * CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES - * - * Maximum number of CHIP Commissioners or Commissionable Nodes that can be discovered - */ -#ifndef CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES -#define CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES 10 -#endif - -/** - * CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT - * - * Enable support to DNS-SD SRP client usage for service advertising and discovery in CHIP. - */ -#ifndef CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT -#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT 0 -#endif - -/** - * CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES - * - * Amount of services available for advertising using SRP. - */ -#ifndef CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES -#define CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES 3 -#endif - -/** - * CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY - * - * Enable support to Commissionable Discovery for Thread devices. - */ -#ifndef CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY -#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY 0 -#endif - -/** - * CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT - * - * Enable support to DNS client usage for resolving and browsing services in CHIP. - */ -#ifndef CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT -#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT 0 -#endif - // -------------------- Thread Configuration -------------------- /** @@ -770,6 +701,42 @@ #define CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI 0 #endif +/** + * CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + * + * Enable support to DNS-SD SRP client usage for service advertising and discovery in CHIP. + */ +#ifndef CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT 0 +#endif + +/** + * CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES + * + * Amount of services available for advertising using SRP. + */ +#ifndef CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES +#define CHIP_DEVICE_CONFIG_THREAD_SRP_MAX_SERVICES 3 +#endif + +/** + * CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY + * + * Enable support to Commissionable Discovery for Thread devices. + */ +#ifndef CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY 0 +#endif + +/** + * CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT + * + * Enable support to DNS client usage for resolving and browsing services in CHIP. + */ +#ifndef CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT +#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT 0 +#endif + // -------------------- Trait Manager Configuration -------------------- /** @@ -1103,7 +1070,42 @@ #define CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME __TIME__ #endif -// -------------------- Device DNS-SD Advertising Configuration -------------------- +// -------------------- Device DNS-SD Configuration -------------------- + +/** + * CHIP_DEVICE_CONFIG_ENABLE_MDNS + * + * Enable support to use MDNS for service advertising and discovery in CHIP. + */ +#ifndef CHIP_DEVICE_CONFIG_ENABLE_MDNS +#define CHIP_DEVICE_CONFIG_ENABLE_MDNS 0 +#endif + +/** + * CHIP_DEVICE_CONFIG_ENABLE_UNPROVISIONED_MDNS + * + * 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_UNPROVISIONED_MDNS +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD +#define CHIP_DEVICE_CONFIG_ENABLE_UNPROVISIONED_MDNS 0 +#else +#define CHIP_DEVICE_CONFIG_ENABLE_UNPROVISIONED_MDNS 1 +#endif +#endif + +/** + * CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES + * + * Maximum number of CHIP Commissioners or Commissionable Nodes that can be discovered + */ +#ifndef CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES +#define CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES 10 +#endif /** * CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY diff --git a/src/include/platform/ConnectivityManager.h b/src/include/platform/ConnectivityManager.h index 0b38b7b461022f..8369077b5a26e2 100644 --- a/src/include/platform/ConnectivityManager.h +++ b/src/include/platform/ConnectivityManager.h @@ -161,6 +161,7 @@ class ConnectivityManager bool HaveServiceConnectivityViaThread(); // Internet connectivity methods + bool IsNetworkProvisioned(); bool HaveIPv4InternetConnectivity(); bool HaveIPv6InternetConnectivity(); @@ -438,6 +439,11 @@ inline bool ConnectivityManager::HaveServiceConnectivityViaThread() return static_cast(this)->_HaveServiceConnectivityViaThread(); } +inline bool ConnectivityManager::IsNetworkProvisioned() +{ + return static_cast(this)->_IsNetworkProvisioned(); +} + inline Ble::BleLayer * ConnectivityManager::GetBleLayer() { return static_cast(this)->_GetBleLayer(); diff --git a/src/include/platform/internal/GenericConfigurationManagerImpl.cpp b/src/include/platform/internal/GenericConfigurationManagerImpl.cpp index cb88223b9b6c4f..47f29546db3796 100644 --- a/src/include/platform/internal/GenericConfigurationManagerImpl.cpp +++ b/src/include/platform/internal/GenericConfigurationManagerImpl.cpp @@ -910,13 +910,7 @@ bool GenericConfigurationManagerImpl::_IsFullyProvisioned() return true; #else // CHIP_BYPASS_RENDEZVOUS - return -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION - ConnectivityMgr().IsWiFiStationProvisioned() && -#endif -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - ConnectivityMgr().IsThreadProvisioned() && -#endif + return ConnectivityMgr().IsNetworkProvisioned() && #if CHIP_DEVICE_CONFIG_ENABLE_JUST_IN_TIME_PROVISIONING (!UseManufacturerCredentialsAsOperational() && _OperationalDeviceCredentialsProvisioned()) && #endif diff --git a/src/include/platform/internal/GenericConnectivityManagerImpl.h b/src/include/platform/internal/GenericConnectivityManagerImpl.h index 6315c25a701b51..ffd4c11b659ca9 100644 --- a/src/include/platform/internal/GenericConnectivityManagerImpl.h +++ b/src/include/platform/internal/GenericConnectivityManagerImpl.h @@ -46,6 +46,7 @@ class GenericConnectivityManagerImpl void _SetUserSelectedMode(bool val); uint16_t _GetUserSelectedModeTimeout(); void _SetUserSelectedModeTimeout(uint16_t val); + bool _IsNetworkProvisioned(); private: ImplClass * Impl() { return static_cast(this); } @@ -71,6 +72,22 @@ template inline void GenericConnectivityManagerImpl::_SetUserSelectedModeTimeout(uint16_t val) {} +template +inline bool GenericConnectivityManagerImpl::_IsNetworkProvisioned() +{ + bool result = true; + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION + result = result && ConnectivityMgr().IsWiFiStationProvisioned(); +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + result = result && ConnectivityMgr().IsThreadProvisioned(); +#endif + + return result; +} + } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp index f315402631751b..3d65419c3adb20 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp @@ -87,6 +87,14 @@ void GenericThreadStackManagerImpl_OpenThread::OnOpenThreadStateChang event.ThreadStateChange.OpenThread.Flags = flags; PlatformMgr().PostEvent(&event); + + if (flags & OT_CHANGED_ACTIVE_DATASET) + { + // post an event alerting other subsystems about change in provisioning state + event.Type = DeviceEventType::kServiceProvisioningChange; + event.ServiceProvisioningChange.IsServiceProvisioned = true; + PlatformMgr().PostEvent(&event); + } } template @@ -238,12 +246,6 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_SetThreadProvis otErr = otDatasetSetActiveTlvs(mOTInst, &tlvs); Impl()->UnlockThreadStack(); - // post an event alerting other subsystems about change in provisioning state - ChipDeviceEvent event; - event.Type = DeviceEventType::kServiceProvisioningChange; - event.ServiceProvisioningChange.IsServiceProvisioned = true; - PlatformMgr().PostEvent(&event); - return MapOpenThreadError(otErr); } diff --git a/src/platform/nrfconnect/CHIPDevicePlatformConfig.h b/src/platform/nrfconnect/CHIPDevicePlatformConfig.h index b2096a58d07f0a..b032eed3e69aa3 100644 --- a/src/platform/nrfconnect/CHIPDevicePlatformConfig.h +++ b/src/platform/nrfconnect/CHIPDevicePlatformConfig.h @@ -75,6 +75,8 @@ #ifdef CONFIG_CHIP_ENABLE_DNSSD_SRP #define CHIP_DEVICE_CONFIG_ENABLE_MDNS 1 #define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT 1 +#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1 +#define CHIP_DEVICE_CONFIG_ENABLE_UNPROVISIONED_MDNS 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