Skip to content

Commit

Permalink
Advertise all of our available operational identities. (#8923)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Aug 14, 2021
1 parent 351c939 commit 1986466
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/app/server/Mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1986466

Please sign in to comment.