Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CHIP devices advertise as operational devices via mDNS #4132

Merged
merged 26 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ca26e5d
Initial definition of an mDNS advertiser
andy31415 Dec 8, 2020
97a79c2
Make chip app server listen on mdns by default
andy31415 Dec 8, 2020
38b611f
Replace DiscoveryManager with advertiser
andy31415 Dec 8, 2020
fce86db
Fix compilation, ensure we shutdown before we listen for mDNS server,…
andy31415 Dec 8, 2020
ac60dc8
Always advertise as operational, add some more logging
andy31415 Dec 8, 2020
cb54ed0
Register delegates, add some logging, fix PTR records
andy31415 Dec 8, 2020
ab9472d
Remove errand space
andy31415 Dec 8, 2020
22123ae
Fix crash in ESP code on broadcast
andy31415 Dec 8, 2020
6996da8
Fix return value: ref return does not work well
andy31415 Dec 8, 2020
91b6dca
Update logging verbosity on ESP32: chip already configures its loggin…
andy31415 Dec 8, 2020
b4d0811
hex format server name
andy31415 Dec 8, 2020
1568e4d
Better logging, fix server discovery
andy31415 Dec 8, 2020
89d7583
Update registration of names
andy31415 Dec 8, 2020
6a09a52
Restyle fixes
andy31415 Dec 8, 2020
dd48fe5
Make ipv4 in minmdns optional
andy31415 Dec 8, 2020
cd68341
Fix logic error in interface lister
andy31415 Dec 8, 2020
43b6bc8
Move Clone into SystemPacketBuffer. Clean up a bit of handle usage in…
andy31415 Dec 9, 2020
0f08552
Make stringbuilder a support class
andy31415 Dec 9, 2020
a3a14e3
Restyle fixes
andy31415 Dec 9, 2020
5d454f0
Merge branch 'master' into 01_chip_mdns_operational
andy31415 Dec 9, 2020
15abce0
Fix build after merge with master
andy31415 Dec 9, 2020
d44085b
Rename Clone to CloneData
andy31415 Dec 10, 2020
785c4cd
Merge branch 'master' into 01_chip_mdns_operational
andy31415 Dec 10, 2020
40a11c1
Replace minimal-mdns with minimal
andy31415 Dec 10, 2020
1446836
Clariy update for interface iteration
andy31415 Dec 10, 2020
bde31eb
Merge branch 'master' into 01_chip_mdns_operational
andy31415 Dec 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can still guard with IsFullyProvisioned to support advertising _chipc._udp for IP rendezvous.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. The reason I made it advertise fully here is to always have functional mDNS to test with.

My next PRs (maybe pending some cleanup in usage) would be to add _chipc._udp vs operational advertisiement.

.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