Matter SDK Coverage Report
Current view: top level - app/server - Dnssd.cpp (source / functions) Coverage Total Hit
Test: SHA:b879ecb8e99e175eea0a293a888bda853da2b19c Lines: 59.1 % 137 81
Test Date: 2025-01-17 19:00:11 Functions: 66.7 % 15 10

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

Generated by: LCOV version 2.0-1