Skip to content

Commit

Permalink
[mdns] Fix Resolver initialization (#5347)
Browse files Browse the repository at this point in the history
ChipMdnsInit() call has recently been moved to
ServiceAdvertiser::Start, hence the function is never called
on the mDNS resolver side. Call the initialization function
when necessary.
  • Loading branch information
Damian-Nordic authored Mar 12, 2021
1 parent bf21749 commit 571f843
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/lib/mdns/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,28 @@ constexpr uint64_t kUndefinedNodeId = 0;
namespace chip {
namespace Mdns {

DiscoveryImplPlatform::DiscoveryImplPlatform()
{
mCommissionInstanceName = GetRandU64();
}
DiscoveryImplPlatform DiscoveryImplPlatform::sManager;

CHIP_ERROR DiscoveryImplPlatform::Start(Inet::InetLayer * inetLayer, uint16_t port)
DiscoveryImplPlatform::DiscoveryImplPlatform() = default;

CHIP_ERROR DiscoveryImplPlatform::Init()
{
CHIP_ERROR error = CHIP_NO_ERROR;
if (!mMdnsInitialized)
{
ReturnErrorOnFailure(ChipMdnsInit(HandleMdnsInit, HandleMdnsError, this));
mMdnsInitialized = true;
}
else
{
error = ChipMdnsStopPublish();
mCommissionInstanceName = GetRandU64();
mMdnsInitialized = true;
}

return CHIP_NO_ERROR;
}

CHIP_ERROR DiscoveryImplPlatform::Start(Inet::InetLayer * inetLayer, uint16_t port)
{
ReturnErrorOnFailure(Init());

CHIP_ERROR error = ChipMdnsStopPublish();

if (error != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to initialize platform mdns: %s", ErrorStr(error));
Expand Down Expand Up @@ -280,6 +284,8 @@ CHIP_ERROR DiscoveryImplPlatform::SetResolverDelegate(ResolverDelegate * delegat

CHIP_ERROR DiscoveryImplPlatform::ResolveNodeId(uint64_t nodeId, uint64_t fabricId, Inet::IPAddressType type)
{
ReturnErrorOnFailure(Init());

MdnsService service;

snprintf(service.mName, sizeof(service.mName), "%" PRIX64 "-%" PRIX64, nodeId, fabricId);
Expand Down Expand Up @@ -357,12 +363,6 @@ void DiscoveryImplPlatform::HandleNodeIdResolve(void * context, MdnsService * re

DiscoveryImplPlatform & DiscoveryImplPlatform::GetInstance()
{
// TODO: Clean Mdns initialization order
// Previously sManager was a global object, but DiscoveryImplPlatform constructor calls
// platform-specific ChipMdnsInit() which for Linux initializes MdnsAvahi global object
// and that may lead to improper initialization, since the order in which global objects'
// constructors are called is undefined.
static DiscoveryImplPlatform sManager;
return sManager;
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/mdns/Discovery_ImplPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace Mdns {
class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver
{
public:
CHIP_ERROR Init();

CHIP_ERROR Start(Inet::InetLayer * inetLayer, uint16_t port) override;

/// Advertises the CHIP node as an operational node
Expand Down

0 comments on commit 571f843

Please sign in to comment.