diff --git a/src/app/server/Mdns.cpp b/src/app/server/Mdns.cpp index 953c374fe3be96..df63e2e4f58a20 100644 --- a/src/app/server/Mdns.cpp +++ b/src/app/server/Mdns.cpp @@ -39,25 +39,19 @@ namespace Mdns { namespace { -NodeId GetCurrentNodeId() +bool HaveOperationalCredentials() { - // TODO: once operational credentials are implemented, node ID should be read from them - - // TODO: once multi-admin is decided, figure out if a single node id - // is sufficient or if we need multi-node-id advertisement. Existing - // mdns advertises a single node id as parameter. - - // Search for one admin pairing and use its node id. + // Look for any fabric info that has a useful operational identity. for (const Transport::FabricInfo & fabricInfo : GetGlobalFabricTable()) { - if (fabricInfo.GetNodeId() != kUndefinedNodeId) + if (fabricInfo.IsInitialized()) { - return fabricInfo.GetNodeId(); + return true; } } ChipLogProgress(Discovery, "Failed to find a valid admin pairing. Node ID unknown"); - return kUndefinedNodeId; + return false; } // Requires an 8-byte mac to accommodate thread. @@ -93,30 +87,33 @@ CHIP_ERROR GetCommissionableInstanceName(char * buffer, size_t bufferLen) /// Set MDNS operational advertisement CHIP_ERROR AdvertiseOperational() { - uint64_t fabricId; - - if (DeviceLayer::ConfigurationMgr().GetFabricId(fabricId) != CHIP_NO_ERROR) + for (const Transport::FabricInfo & fabricInfo : GetGlobalFabricTable()) { - ChipLogError(Discovery, "Fabric ID not known. Using a default"); - fabricId = 5544332211; + if (fabricInfo.IsInitialized()) + { + uint8_t mac[8]; + + const auto advertiseParameters = + chip::Mdns::OperationalAdvertisingParameters() + .SetPeerId(PeerId().SetFabricId(fabricInfo.GetFabricId()).SetNodeId(fabricInfo.GetNodeId())) + .SetMac(FillMAC(mac)) + .SetPort(CHIP_PORT) + .SetMRPRetryIntervals(CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL, + CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL) + .EnableIpV4(true); + + auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance(); + + ChipLogProgress(Discovery, "Advertise operational node " ChipLogFormatX64 "-" ChipLogFormatX64, + ChipLogValueX64(advertiseParameters.GetPeerId().GetFabricId()), + ChipLogValueX64(advertiseParameters.GetPeerId().GetNodeId())); + // Should we keep trying to advertise the other operational + // identities on failure? + ReturnErrorOnFailure(mdnsAdvertiser.Advertise(advertiseParameters)); + } } - uint8_t mac[8]; - - const auto advertiseParameters = - chip::Mdns::OperationalAdvertisingParameters() - .SetPeerId(PeerId().SetFabricId(fabricId).SetNodeId(GetCurrentNodeId())) - .SetMac(FillMAC(mac)) - .SetPort(CHIP_PORT) - .SetMRPRetryIntervals(CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL, CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL) - .EnableIpV4(true); - - auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance(); - - ChipLogProgress(Discovery, "Advertise operational node " ChipLogFormatX64 "-" ChipLogFormatX64, - ChipLogValueX64(advertiseParameters.GetPeerId().GetFabricId()), - ChipLogValueX64(advertiseParameters.GetPeerId().GetNodeId())); - return mdnsAdvertiser.Advertise(advertiseParameters); + return CHIP_NO_ERROR; } /// Set MDNS commissioner advertisement @@ -250,11 +247,14 @@ void StartServer() ChipLogProgress(Discovery, "Start dns-sd server"); CHIP_ERROR err = chip::Mdns::ServiceAdvertiser::Instance().Start(&chip::DeviceLayer::InetLayer, chip::Mdns::kMdnsPort); - // TODO: advertise this only when really operational once we support both - // operational and commisioning advertising is supported. - if (GetCurrentNodeId() != kUndefinedNodeId) + err = app::Mdns::AdvertiseOperational(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Discovery, "Failed to advertise operational node: %s", chip::ErrorStr(err)); + } + + if (HaveOperationalCredentials()) { - err = app::Mdns::AdvertiseOperational(); #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY err = app::Mdns::AdvertiseCommissionableNode(); #endif