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

Restyle Make CHIP devices advertise as operational devices via mDNS #4144

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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()
.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