Skip to content

Commit

Permalink
Make CHIP devices advertise as operational devices via mDNS (#4132)
Browse files Browse the repository at this point in the history
* Initial definition of an mDNS advertiser

* Make chip app server listen on mdns by default

* Replace DiscoveryManager with advertiser

* Fix compilation, ensure we shutdown before we listen for mDNS server, to make sure multiple start calls work

* Always advertise as operational, add some more logging

* Register delegates, add some logging, fix PTR records

* Remove errand space

* Fix crash in ESP code on broadcast

* Fix return value: ref return does not work well

* Update logging verbosity on ESP32: chip already configures its logging, so mark esp verbosity to verbose

* hex format server name

* Better logging, fix server discovery

* Update registration of names

* Restyle fixes

* Make ipv4 in minmdns optional

* Fix logic error in interface lister

* Move Clone into SystemPacketBuffer. Clean up a bit of handle usage in mdns

* Make stringbuilder a support class

* Restyle fixes

* Fix build after merge with master

* Rename Clone to CloneData

* Replace minimal-mdns with minimal

* Clariy update for interface iteration
  • Loading branch information
andy31415 authored Dec 11, 2020
1 parent fb469ce commit 27d3ea8
Show file tree
Hide file tree
Showing 21 changed files with 778 additions and 33 deletions.
13 changes: 10 additions & 3 deletions examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "gen/attribute-id.h"
#include "gen/cluster-id.h"
#include <app/util/basic-types.h>
#include <lib/mdns/DiscoveryManager.h>
#include <lib/mdns/Advertiser.h>
#include <support/CodeUtils.h>

static const char * TAG = "app-devicecallbacks";
Expand Down Expand Up @@ -92,7 +92,11 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event
{
ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT);
wifiLED.Set(true);
chip::Mdns::DiscoveryManager::GetInstance().StartPublishDevice();

if (chip::Mdns::ServiceAdvertiser::Instance().Start(&DeviceLayer::InetLayer, chip::Mdns::kMdnsPort) != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to start mDNS advertisement");
}
}
else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost)
{
Expand All @@ -102,7 +106,10 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event
if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established)
{
ESP_LOGI(TAG, "IPv6 Server ready...");
chip::Mdns::DiscoveryManager::GetInstance().StartPublishDevice();
if (chip::Mdns::ServiceAdvertiser::Instance().Start(&DeviceLayer::InetLayer, chip::Mdns::kMdnsPort) != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to start mDNS advertisement");
}
}
else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost)
{
Expand Down
7 changes: 5 additions & 2 deletions examples/common/chip-app-server/RendezvousServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#if CHIP_ENABLE_OPENTHREAD
#include <platform/ThreadStackManager.h>
#endif
#include <lib/mdns/DiscoveryManager.h>
#include <mdns/Advertiser.h>

using namespace ::chip::Inet;
using namespace ::chip::Transport;
Expand Down Expand Up @@ -97,7 +97,10 @@ void RendezvousServer::OnRendezvousStatusUpdate(Status status, CHIP_ERROR err)

case RendezvousSessionDelegate::NetworkProvisioningSuccess:
ChipLogProgress(AppServer, "Device was assigned network credentials");
chip::Mdns::DiscoveryManager::GetInstance().StartPublishDevice();
if (chip::Mdns::ServiceAdvertiser::Instance().Start(&DeviceLayer::InetLayer, chip::Mdns::kMdnsPort) != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Failed to start mDNS advertisement");
}
if (mDelegate != nullptr)
{
mDelegate->OnRendezvousStopped();
Expand Down
22 changes: 21 additions & 1 deletion examples/common/chip-app-server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <inet/InetError.h>
#include <inet/InetLayer.h>
#include <lib/mdns/DiscoveryManager.h>
#include <mdns/Advertiser.h>
#include <platform/CHIPDeviceLayer.h>
#include <setup_payload/SetupPayload.h>
#include <support/CodeUtils.h>
Expand Down Expand Up @@ -158,11 +159,30 @@ void InitServer(AppDelegate * delegate)
SuccessOrExit(err = gRendezvousServer.Init(params, &gTransports));
}

// TODO: advertise this only when really operational once we support both
// operational and commisioning advertising is supported.
{
constexpr uint64_t kTestFabricId = 5544332211;
err = Mdns::ServiceAdvertiser::Instance().Advertise(Mdns::OperationalAdvertisingParameters()
.SetFabricId(kTestFabricId)
.SetNodeId(chip::kTestDeviceNodeId)
.SetPort(CHIP_PORT)
#if INET_CONFIG_ENABLE_IPV4
.EnableIpV4(true)
#else
.EnableIpV4(false)
#endif
);
SuccessOrExit(err);
}

err = Mdns::ServiceAdvertiser::Instance().Start(&DeviceLayer::InetLayer, chip::Mdns::kMdnsPort);
SuccessOrExit(err);

err = gSessions.NewPairing(peer, chip::kTestControllerNodeId, &gTestPairing);
SuccessOrExit(err);

gSessions.SetDelegate(&gCallbacks);
chip::Mdns::DiscoveryManager::GetInstance().StartPublishDevice(kIPAddressType_IPv6);

exit:
if (err != CHIP_NO_ERROR)
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal-mdns/AllInterfaceListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class AllInterfaces : public mdns::Minimal::ListenIterator
return true; // not a usable interface
}
char name[64];
if (!mIterator.GetInterfaceName(name, sizeof(name)) == CHIP_NO_ERROR)
if (mIterator.GetInterfaceName(name, sizeof(name)) != CHIP_NO_ERROR)
{
printf("!!!! FAILED TO GET INTERFACE NAME\n");
return true;
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal-mdns/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void BroadcastPacket(mdns::Minimal::ServerBase * server)
return;
}

if (server->BroadcastSend(buffer.Release_ForNow(), gOptions.querySendPort) != CHIP_NO_ERROR)
if (server->BroadcastSend(std::move(buffer), gOptions.querySendPort) != CHIP_NO_ERROR)
{
printf("Error sending\n");
return;
Expand Down
88 changes: 88 additions & 0 deletions src/lib/mdns/Advertiser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <cstdint>

#include <core/CHIPError.h>
#include <inet/InetLayer.h>

namespace chip {
namespace Mdns {

static constexpr uint16_t kMdnsPort = 5353;

/// Defines parameters required for advertising a CHIP node
/// over mDNS as an 'operationally ready' node.
class OperationalAdvertisingParameters
{
public:
OperationalAdvertisingParameters & SetFabricId(uint64_t fabricId)
{
mFabricId = fabricId;
return *this;
}
uint64_t GetFabricId() const { return mFabricId; }

OperationalAdvertisingParameters & SetNodeId(uint64_t nodeId)
{
mNodeId = nodeId;
return *this;
}
uint64_t GetNodeId() const { return mNodeId; }

OperationalAdvertisingParameters & SetPort(uint16_t port)
{
mPort = port;
return *this;
}
uint64_t GetPort() const { return mPort; }

OperationalAdvertisingParameters & EnableIpV4(bool enable)
{
mEnableIPv4 = enable;
return *this;
}
bool IsIPv4Enabled() const { return mEnableIPv4; }

private:
uint64_t mFabricId = 0;
uint64_t mNodeId = 0;
uint16_t mPort = CHIP_PORT;
bool mEnableIPv4 = true;
};

/// Handles advertising of CHIP nodes
class ServiceAdvertiser
{
public:
virtual ~ServiceAdvertiser() {}

/// Starts the advertiser. Items 'Advertised' will become visible.
/// May be called before OR after Advertise() calls.
virtual CHIP_ERROR Start(chip::Inet::InetLayer * inetLayer, uint16_t port) = 0;

/// Advertises the CHIP node as an operational node
virtual CHIP_ERROR Advertise(const OperationalAdvertisingParameters & params) = 0;

/// Provides the system-wide implementation of the service advertiser
static ServiceAdvertiser & Instance();
};

} // namespace Mdns
} // namespace chip
Loading

0 comments on commit 27d3ea8

Please sign in to comment.