LCOV - code coverage report
Current view: top level - app/server - Dnssd.cpp (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 79 134 59.0 %
Date: 2024-02-15 08:20:41 Functions: 9 13 69.2 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *    Copyright (c) 2021 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 <app/server/Dnssd.h>
      19             : 
      20             : #include <app-common/zap-generated/cluster-enums.h>
      21             : #include <inttypes.h>
      22             : #include <lib/core/Optional.h>
      23             : #include <lib/dnssd/Advertiser.h>
      24             : #include <lib/dnssd/ServiceNaming.h>
      25             : #include <lib/support/DefaultStorageKeyAllocator.h>
      26             : #include <lib/support/Span.h>
      27             : #include <lib/support/logging/CHIPLogging.h>
      28             : #include <messaging/ReliableMessageProtocolConfig.h>
      29             : #include <platform/CHIPDeviceLayer.h>
      30             : #include <platform/CommissionableDataProvider.h>
      31             : #include <platform/ConfigurationManager.h>
      32             : #include <platform/DeviceInstanceInfoProvider.h>
      33             : #include <protocols/secure_channel/PASESession.h>
      34             : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
      35             : #include <setup_payload/AdditionalDataPayloadGenerator.h>
      36             : #endif
      37             : #include <credentials/FabricTable.h>
      38             : #include <setup_payload/SetupPayload.h>
      39             : #include <system/TimeSource.h>
      40             : 
      41             : namespace chip {
      42             : namespace app {
      43             : namespace {
      44             : 
      45           2 : void OnPlatformEvent(const DeviceLayer::ChipDeviceEvent * event)
      46             : {
      47           2 :     switch (event->Type)
      48             :     {
      49           1 :     case DeviceLayer::DeviceEventType::kDnssdInitialized:
      50             :     case DeviceLayer::DeviceEventType::kDnssdRestartNeeded:
      51           1 :         app::DnssdServer::Instance().StartServer();
      52           1 :         break;
      53           1 :     default:
      54           1 :         break;
      55             :     }
      56           2 : }
      57             : 
      58           2 : void OnPlatformEventWrapper(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
      59             : {
      60             :     (void) arg;
      61           2 :     OnPlatformEvent(event);
      62           2 : }
      63             : 
      64             : } // namespace
      65             : 
      66             : constexpr System::Clock::Timestamp DnssdServer::kTimeoutCleared;
      67             : 
      68           8 : bool DnssdServer::HaveOperationalCredentials()
      69             : {
      70           8 :     VerifyOrDie(mFabricTable != nullptr);
      71             : 
      72             :     // Look for any fabric info that has a useful operational identity.
      73           8 :     return mFabricTable->FabricCount() != 0;
      74             : }
      75             : 
      76             : #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
      77             : 
      78             : void DnssdServer::SetExtendedDiscoveryTimeoutSecs(int32_t secs)
      79             : {
      80             :     ChipLogDetail(Discovery, "Setting extended discovery timeout to %" PRId32 "s", secs);
      81             :     mExtendedDiscoveryTimeoutSecs = MakeOptional(secs);
      82             : 
      83             :     if (mExtendedDiscoveryExpiration != kTimeoutCleared &&
      84             :         mExtendedDiscoveryExpiration > mTimeSource.GetMonotonicTimestamp() + System::Clock::Seconds32(secs))
      85             :     {
      86             :         // Reset our timer to the new (shorter) timeout.
      87             :         ScheduleExtendedDiscoveryExpiration();
      88             :     }
      89             : }
      90             : 
      91             : int32_t DnssdServer::GetExtendedDiscoveryTimeoutSecs()
      92             : {
      93             :     return mExtendedDiscoveryTimeoutSecs.ValueOr(CHIP_DEVICE_CONFIG_EXTENDED_DISCOVERY_TIMEOUT_SECS);
      94             : }
      95             : 
      96             : /// Callback from Extended Discovery Expiration timer
      97             : void HandleExtendedDiscoveryExpiration(System::Layer * aSystemLayer, void * aAppState)
      98             : {
      99             :     DnssdServer::Instance().OnExtendedDiscoveryExpiration(aSystemLayer, aAppState);
     100             : }
     101             : 
     102             : void DnssdServer::OnExtendedDiscoveryExpiration(System::Layer * aSystemLayer, void * aAppState)
     103             : {
     104             :     ChipLogDetail(Discovery, "Extended discovery timed out");
     105             : 
     106             :     mExtendedDiscoveryExpiration = kTimeoutCleared;
     107             : 
     108             :     // Reset our advertising, now that we have flagged ourselves as possibly not
     109             :     // needing extended discovery anymore.
     110             :     StartServer();
     111             : }
     112             : 
     113             : CHIP_ERROR DnssdServer::ScheduleExtendedDiscoveryExpiration()
     114             : {
     115             :     int32_t extendedDiscoveryTimeoutSecs = GetExtendedDiscoveryTimeoutSecs();
     116             :     if (extendedDiscoveryTimeoutSecs == CHIP_DEVICE_CONFIG_DISCOVERY_NO_TIMEOUT)
     117             :     {
     118             :         return CHIP_NO_ERROR;
     119             :     }
     120             :     ChipLogDetail(Discovery, "Scheduling extended discovery timeout in %" PRId32 "s", extendedDiscoveryTimeoutSecs);
     121             : 
     122             :     mExtendedDiscoveryExpiration = mTimeSource.GetMonotonicTimestamp() + System::Clock::Seconds32(extendedDiscoveryTimeoutSecs);
     123             : 
     124             :     return DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(extendedDiscoveryTimeoutSecs),
     125             :                                                  HandleExtendedDiscoveryExpiration, nullptr);
     126             : }
     127             : #endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
     128             : 
     129           0 : CHIP_ERROR DnssdServer::GetCommissionableInstanceName(char * buffer, size_t bufferLen)
     130             : {
     131           0 :     auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance();
     132           0 :     return mdnsAdvertiser.GetCommissionableInstanceName(buffer, bufferLen);
     133             : }
     134             : 
     135          13 : CHIP_ERROR DnssdServer::SetEphemeralDiscriminator(Optional<uint16_t> discriminator)
     136             : {
     137          13 :     VerifyOrReturnError(discriminator.ValueOr(0) <= kMaxDiscriminatorValue, CHIP_ERROR_INVALID_ARGUMENT);
     138          13 :     mEphemeralDiscriminator = discriminator;
     139             : 
     140          13 :     return CHIP_NO_ERROR;
     141             : }
     142             : 
     143             : #if CHIP_CONFIG_ENABLE_ICD_SERVER
     144             : template <class AdvertisingParams>
     145             : void DnssdServer::AddICDKeyToAdvertisement(AdvertisingParams & advParams)
     146             : {
     147             :     VerifyOrDieWithMsg(mICDManager != nullptr, Discovery,
     148             :                        "Invalid pointer to the ICDManager which is required for the LIT operating mode");
     149             : 
     150             :     Dnssd::ICDModeAdvertise ICDModeToAdvertise = Dnssd::ICDModeAdvertise::kNone;
     151             :     // Only advertise the ICD key if the device can operate as a LIT
     152             :     if (mICDManager->SupportsFeature(Clusters::IcdManagement::Feature::kLongIdleTimeSupport))
     153             :     {
     154             :         if (mICDManager->GetICDMode() == ICDConfigurationData::ICDMode::LIT)
     155             :         {
     156             :             ICDModeToAdvertise = Dnssd::ICDModeAdvertise::kLIT;
     157             :         }
     158             :         else
     159             :         {
     160             :             ICDModeToAdvertise = Dnssd::ICDModeAdvertise::kSIT;
     161             :         }
     162             :     }
     163             : 
     164             :     advParams.SetICDModeToAdvertise(ICDModeToAdvertise);
     165             : }
     166             : #endif
     167             : 
     168             : /// Set MDNS operational advertisement
     169          15 : CHIP_ERROR DnssdServer::AdvertiseOperational()
     170             : {
     171          15 :     VerifyOrDie(mFabricTable != nullptr);
     172             : 
     173          15 :     for (const FabricInfo & fabricInfo : *mFabricTable)
     174             :     {
     175           0 :         if (!fabricInfo.ShouldAdvertiseIdentity())
     176             :         {
     177           0 :             continue;
     178             :         }
     179             : 
     180             :         uint8_t macBuffer[DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
     181           0 :         MutableByteSpan mac(macBuffer);
     182           0 :         if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
     183             :         {
     184           0 :             ChipLogError(Discovery, "Failed to get primary mac address of device. Generating a random one.");
     185           0 :             Crypto::DRBG_get_bytes(macBuffer, sizeof(macBuffer));
     186             :         }
     187             : 
     188           0 :         auto advertiseParameters = chip::Dnssd::OperationalAdvertisingParameters()
     189           0 :                                        .SetPeerId(fabricInfo.GetPeerId())
     190           0 :                                        .SetMac(mac)
     191           0 :                                        .SetPort(GetSecuredPort())
     192           0 :                                        .SetInterfaceId(GetInterfaceId())
     193           0 :                                        .SetLocalMRPConfig(GetLocalMRPConfig())
     194           0 :                                        .EnableIpV4(true);
     195             : 
     196             : #if CHIP_CONFIG_ENABLE_ICD_SERVER
     197             :         AddICDKeyToAdvertisement(advertiseParameters);
     198             : #endif
     199             : 
     200           0 :         auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance();
     201             : 
     202           0 :         ChipLogProgress(Discovery, "Advertise operational node " ChipLogFormatX64 "-" ChipLogFormatX64,
     203             :                         ChipLogValueX64(advertiseParameters.GetPeerId().GetCompressedFabricId()),
     204             :                         ChipLogValueX64(advertiseParameters.GetPeerId().GetNodeId()));
     205             :         // Should we keep trying to advertise the other operational
     206             :         // identities on failure?
     207           0 :         ReturnErrorOnFailure(mdnsAdvertiser.Advertise(advertiseParameters));
     208           0 :     }
     209          15 :     return CHIP_NO_ERROR;
     210             : }
     211             : 
     212           8 : CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::CommissioningMode mode)
     213             : {
     214           8 :     auto advertiseParameters = chip::Dnssd::CommissionAdvertisingParameters()
     215           8 :                                    .SetPort(commissionableNode ? GetSecuredPort() : GetUnsecuredPort())
     216          16 :                                    .SetInterfaceId(GetInterfaceId())
     217           8 :                                    .EnableIpV4(true);
     218           8 :     advertiseParameters.SetCommissionAdvertiseMode(commissionableNode ? chip::Dnssd::CommssionAdvertiseMode::kCommissionableNode
     219             :                                                                       : chip::Dnssd::CommssionAdvertiseMode::kCommissioner);
     220             : 
     221           8 :     advertiseParameters.SetCommissioningMode(mode);
     222             : 
     223             :     char pairingInst[chip::Dnssd::kKeyPairingInstructionMaxLength + 1];
     224             : 
     225             :     uint8_t macBuffer[DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
     226           8 :     MutableByteSpan mac(macBuffer);
     227           8 :     if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
     228             :     {
     229           0 :         ChipLogError(Discovery, "Failed to get primary mac address of device. Generating a random one.");
     230           0 :         Crypto::DRBG_get_bytes(macBuffer, sizeof(macBuffer));
     231             :     }
     232           8 :     advertiseParameters.SetMac(mac);
     233             : 
     234             :     uint16_t value;
     235             :     uint32_t val32;
     236           8 :     if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetVendorId(value) != CHIP_NO_ERROR)
     237             :     {
     238           0 :         ChipLogDetail(Discovery, "Vendor ID not known");
     239             :     }
     240             :     else
     241             :     {
     242           8 :         advertiseParameters.SetVendorId(chip::Optional<uint16_t>::Value(value));
     243             :     }
     244             : 
     245           8 :     if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetProductId(value) != CHIP_NO_ERROR)
     246             :     {
     247           0 :         ChipLogDetail(Discovery, "Product ID not known");
     248             :     }
     249             :     else
     250             :     {
     251           8 :         advertiseParameters.SetProductId(chip::Optional<uint16_t>::Value(value));
     252             :     }
     253             : 
     254           8 :     if (DeviceLayer::ConfigurationMgr().IsCommissionableDeviceTypeEnabled() &&
     255           8 :         DeviceLayer::ConfigurationMgr().GetDeviceTypeId(val32) == CHIP_NO_ERROR)
     256             :     {
     257           0 :         advertiseParameters.SetDeviceType(chip::Optional<uint32_t>::Value(val32));
     258             :     }
     259             : 
     260             :     char deviceName[chip::Dnssd::kKeyDeviceNameMaxLength + 1];
     261           8 :     if (DeviceLayer::ConfigurationMgr().IsCommissionableDeviceNameEnabled() &&
     262           8 :         DeviceLayer::ConfigurationMgr().GetCommissionableDeviceName(deviceName, sizeof(deviceName)) == CHIP_NO_ERROR)
     263             :     {
     264           0 :         advertiseParameters.SetDeviceName(chip::Optional<const char *>::Value(deviceName));
     265             :     }
     266             : 
     267           8 :     advertiseParameters.SetLocalMRPConfig(GetLocalMRPConfig());
     268             : 
     269             : #if CHIP_CONFIG_ENABLE_ICD_SERVER
     270             :     AddICDKeyToAdvertisement(advertiseParameters);
     271             : #endif
     272             : 
     273           8 :     if (commissionableNode)
     274             :     {
     275           8 :         uint16_t discriminator = 0;
     276           8 :         CHIP_ERROR error       = DeviceLayer::GetCommissionableDataProvider()->GetSetupDiscriminator(discriminator);
     277           8 :         if (error != CHIP_NO_ERROR)
     278             :         {
     279           0 :             ChipLogError(Discovery,
     280             :                          "Setup discriminator read error (%" CHIP_ERROR_FORMAT ")! Critical error, will not be commissionable.",
     281             :                          error.Format());
     282           0 :             return error;
     283             :         }
     284             : 
     285             :         // Override discriminator with temporary one if one is set
     286           8 :         discriminator = mEphemeralDiscriminator.ValueOr(discriminator);
     287             : 
     288           8 :         advertiseParameters.SetShortDiscriminator(static_cast<uint8_t>((discriminator >> 8) & 0x0F))
     289           8 :             .SetLongDiscriminator(discriminator);
     290             : 
     291             : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
     292             :         char rotatingDeviceIdHexBuffer[RotatingDeviceId::kHexMaxLength];
     293             :         ReturnErrorOnFailure(GenerateRotatingDeviceId(rotatingDeviceIdHexBuffer, ArraySize(rotatingDeviceIdHexBuffer)));
     294             :         advertiseParameters.SetRotatingDeviceId(chip::Optional<const char *>::Value(rotatingDeviceIdHexBuffer));
     295             : #endif
     296             : 
     297           8 :         if (!HaveOperationalCredentials())
     298             :         {
     299           8 :             if (DeviceLayer::ConfigurationMgr().GetInitialPairingHint(value) != CHIP_NO_ERROR)
     300             :             {
     301           0 :                 ChipLogDetail(Discovery, "DNS-SD Pairing Hint not set");
     302             :             }
     303             :             else
     304             :             {
     305           8 :                 advertiseParameters.SetPairingHint(chip::Optional<uint16_t>::Value(value));
     306             :             }
     307             : 
     308           8 :             if (DeviceLayer::ConfigurationMgr().GetInitialPairingInstruction(pairingInst, sizeof(pairingInst)) != CHIP_NO_ERROR)
     309             :             {
     310           0 :                 ChipLogDetail(Discovery, "DNS-SD Pairing Instruction not set");
     311             :             }
     312             :             else
     313             :             {
     314           8 :                 advertiseParameters.SetPairingInstruction(chip::Optional<const char *>::Value(pairingInst));
     315             :             }
     316             :         }
     317             :         else
     318             :         {
     319           0 :             if (DeviceLayer::ConfigurationMgr().GetSecondaryPairingHint(value) != CHIP_NO_ERROR)
     320             :             {
     321           0 :                 ChipLogDetail(Discovery, "DNS-SD Pairing Hint not set");
     322             :             }
     323             :             else
     324             :             {
     325           0 :                 advertiseParameters.SetPairingHint(chip::Optional<uint16_t>::Value(value));
     326             :             }
     327             : 
     328           0 :             if (DeviceLayer::ConfigurationMgr().GetSecondaryPairingInstruction(pairingInst, sizeof(pairingInst)) != CHIP_NO_ERROR)
     329             :             {
     330           0 :                 ChipLogDetail(Discovery, "DNS-SD Pairing Instruction not set");
     331             :             }
     332             :             else
     333             :             {
     334           0 :                 advertiseParameters.SetPairingInstruction(chip::Optional<const char *>::Value(pairingInst));
     335             :             }
     336             :         }
     337             :     }
     338             : #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_PASSCODE
     339             :     else
     340             :     {
     341             :         advertiseParameters.SetCommissionerPasscodeSupported(Optional<bool>(true));
     342             :     }
     343             : #endif
     344             : 
     345           8 :     auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance();
     346             : 
     347           8 :     ChipLogProgress(Discovery, "Advertise commission parameter vendorID=%u productID=%u discriminator=%04u/%02u cm=%u cp=%u",
     348             :                     advertiseParameters.GetVendorId().ValueOr(0), advertiseParameters.GetProductId().ValueOr(0),
     349             :                     advertiseParameters.GetLongDiscriminator(), advertiseParameters.GetShortDiscriminator(),
     350             :                     to_underlying(advertiseParameters.GetCommissioningMode()),
     351             :                     advertiseParameters.GetCommissionerPasscodeSupported().ValueOr(false) ? 1 : 0);
     352           8 :     return mdnsAdvertiser.Advertise(advertiseParameters);
     353           8 : }
     354             : 
     355           0 : CHIP_ERROR DnssdServer::AdvertiseCommissioner()
     356             : {
     357           0 :     return Advertise(false /* commissionableNode */, chip::Dnssd::CommissioningMode::kDisabled);
     358             : }
     359             : 
     360           8 : CHIP_ERROR DnssdServer::AdvertiseCommissionableNode(chip::Dnssd::CommissioningMode mode)
     361             : {
     362             : #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
     363             :     mCurrentCommissioningMode = mode;
     364             :     if (mode != Dnssd::CommissioningMode::kDisabled)
     365             :     {
     366             :         // We're not doing extended discovery right now.
     367             :         DeviceLayer::SystemLayer().CancelTimer(HandleExtendedDiscoveryExpiration, nullptr);
     368             :         mExtendedDiscoveryExpiration = kTimeoutCleared;
     369             :     }
     370             : #endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
     371             : 
     372           8 :     return Advertise(true /* commissionableNode */, mode);
     373             : }
     374             : 
     375          15 : void DnssdServer::StartServer()
     376             : {
     377          15 :     Dnssd::CommissioningMode mode = Dnssd::CommissioningMode::kDisabled;
     378          15 :     if (mCommissioningModeProvider)
     379             :     {
     380          15 :         mode = mCommissioningModeProvider->GetCommissioningMode();
     381             :     }
     382          15 :     return StartServer(mode);
     383             : }
     384             : 
     385           0 : void DnssdServer::StopServer()
     386             : {
     387             :     // Make sure we don't hold on to a dangling fabric table pointer.
     388           0 :     mFabricTable = nullptr;
     389             : 
     390           0 :     DeviceLayer::PlatformMgr().RemoveEventHandler(OnPlatformEventWrapper, 0);
     391             : 
     392             : #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
     393             :     if (mExtendedDiscoveryExpiration != kTimeoutCleared)
     394             :     {
     395             :         DeviceLayer::SystemLayer().CancelTimer(HandleExtendedDiscoveryExpiration, nullptr);
     396             :         mExtendedDiscoveryExpiration = kTimeoutCleared;
     397             :     }
     398             : #endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
     399             : 
     400           0 :     if (Dnssd::ServiceAdvertiser::Instance().IsInitialized())
     401             :     {
     402           0 :         auto err = Dnssd::ServiceAdvertiser::Instance().RemoveServices();
     403           0 :         if (err != CHIP_NO_ERROR)
     404             :         {
     405           0 :             ChipLogError(Discovery, "Failed to remove advertised services: %" CHIP_ERROR_FORMAT, err.Format());
     406             :         }
     407             : 
     408           0 :         Dnssd::ServiceAdvertiser::Instance().Shutdown();
     409             :     }
     410           0 : }
     411             : 
     412          15 : void DnssdServer::StartServer(Dnssd::CommissioningMode mode)
     413             : {
     414          15 :     ChipLogProgress(Discovery, "Updating services using commissioning mode %d", static_cast<int>(mode));
     415             : 
     416          15 :     DeviceLayer::PlatformMgr().AddEventHandler(OnPlatformEventWrapper, 0);
     417             : 
     418          15 :     CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(chip::DeviceLayer::UDPEndPointManager());
     419          15 :     if (err != CHIP_NO_ERROR)
     420             :     {
     421           0 :         ChipLogError(Discovery, "Failed to initialize advertiser: %" CHIP_ERROR_FORMAT, err.Format());
     422             :     }
     423             : 
     424          15 :     err = Dnssd::ServiceAdvertiser::Instance().RemoveServices();
     425          15 :     if (err != CHIP_NO_ERROR)
     426             :     {
     427           0 :         ChipLogError(Discovery, "Failed to remove advertised services: %" CHIP_ERROR_FORMAT, err.Format());
     428             :     }
     429             : 
     430          15 :     err = AdvertiseOperational();
     431          15 :     if (err != CHIP_NO_ERROR)
     432             :     {
     433           0 :         ChipLogError(Discovery, "Failed to advertise operational node: %" CHIP_ERROR_FORMAT, err.Format());
     434             :     }
     435             : 
     436          15 :     if (mode != Dnssd::CommissioningMode::kDisabled)
     437             :     {
     438           8 :         err = AdvertiseCommissionableNode(mode);
     439           8 :         if (err != CHIP_NO_ERROR)
     440             :         {
     441           0 :             ChipLogError(Discovery, "Failed to advertise commissionable node: %" CHIP_ERROR_FORMAT, err.Format());
     442             :         }
     443             :     }
     444             : #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
     445             :     else if (GetExtendedDiscoveryTimeoutSecs() != CHIP_DEVICE_CONFIG_DISCOVERY_DISABLED)
     446             :     {
     447             :         bool alwaysAdvertiseExtended = (GetExtendedDiscoveryTimeoutSecs() == CHIP_DEVICE_CONFIG_DISCOVERY_NO_TIMEOUT);
     448             :         // We do extended discovery advertising in three cases:
     449             :         // 1) We don't have a timeout for extended discovery.
     450             :         // 2) We are transitioning out of commissioning mode (basic or enhanced)
     451             :         //    and should therefore start extended discovery.
     452             :         // 3) We are resetting advertising while we are in the middle of an
     453             :         //    existing extended discovery advertising period.
     454             :         if (alwaysAdvertiseExtended || mCurrentCommissioningMode != Dnssd::CommissioningMode::kDisabled ||
     455             :             mExtendedDiscoveryExpiration != kTimeoutCleared)
     456             :         {
     457             :             err = AdvertiseCommissionableNode(mode);
     458             :             if (err != CHIP_NO_ERROR)
     459             :             {
     460             :                 ChipLogError(Discovery, "Failed to advertise extended commissionable node: %" CHIP_ERROR_FORMAT, err.Format());
     461             :             }
     462             :             if (mExtendedDiscoveryExpiration == kTimeoutCleared)
     463             :             {
     464             :                 // set timeout
     465             :                 ScheduleExtendedDiscoveryExpiration();
     466             :             }
     467             :         }
     468             :     }
     469             : #endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
     470             : 
     471             : #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
     472             :     err = AdvertiseCommissioner();
     473             :     if (err != CHIP_NO_ERROR)
     474             :     {
     475             :         ChipLogError(Discovery, "Failed to advertise commissioner: %" CHIP_ERROR_FORMAT, err.Format());
     476             :     }
     477             : #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
     478             : 
     479          15 :     err = Dnssd::ServiceAdvertiser::Instance().FinalizeServiceUpdate();
     480          15 :     if (err != CHIP_NO_ERROR)
     481             :     {
     482           0 :         ChipLogError(Discovery, "Failed to finalize service update: %" CHIP_ERROR_FORMAT, err.Format());
     483             :     }
     484          15 : }
     485             : 
     486             : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
     487             : CHIP_ERROR DnssdServer::GenerateRotatingDeviceId(char rotatingDeviceIdHexBuffer[], size_t rotatingDeviceIdHexBufferSize)
     488             : {
     489             :     AdditionalDataPayloadGeneratorParams additionalDataPayloadParams;
     490             :     uint8_t rotatingDeviceIdUniqueId[chip::DeviceLayer::ConfigurationManager::kRotatingDeviceIDUniqueIDLength];
     491             :     MutableByteSpan rotatingDeviceIdUniqueIdSpan(rotatingDeviceIdUniqueId);
     492             :     size_t rotatingDeviceIdValueOutputSize = 0;
     493             : 
     494             :     ReturnErrorOnFailure(
     495             :         chip::DeviceLayer::GetDeviceInstanceInfoProvider()->GetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueIdSpan));
     496             :     ReturnErrorOnFailure(
     497             :         chip::DeviceLayer::ConfigurationMgr().GetLifetimeCounter(additionalDataPayloadParams.rotatingDeviceIdLifetimeCounter));
     498             :     additionalDataPayloadParams.rotatingDeviceIdUniqueId = rotatingDeviceIdUniqueIdSpan;
     499             : 
     500             :     return AdditionalDataPayloadGenerator().generateRotatingDeviceIdAsHexString(
     501             :         additionalDataPayloadParams, rotatingDeviceIdHexBuffer, rotatingDeviceIdHexBufferSize, rotatingDeviceIdValueOutputSize);
     502             : }
     503             : #endif
     504             : 
     505           0 : void DnssdServer::OnICDModeChange()
     506             : {
     507             :     // ICDMode changed, restart DNS-SD advertising, because SII and ICD key are affected by this change.
     508             :     // StartServer will take care of setting the operational and commissionable advertisements
     509           0 :     StartServer();
     510           0 : }
     511             : 
     512             : } // namespace app
     513             : } // namespace chip

Generated by: LCOV version 1.14