Skip to content

Commit

Permalink
Making py chip-device-ctrl discoverable over DNS-SD as a commissioner
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Sep 10, 2021
1 parent d05f16f commit 4e1caad
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/app/server/Mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <inttypes.h>

#include <core/Optional.h>
#include <mdns/Advertiser.h>
#include <mdns/ServiceNaming.h>
#include <messaging/ReliableMessageProtocolConfig.h>
#include <platform/CHIPDeviceLayer.h>
Expand Down Expand Up @@ -137,19 +136,15 @@ CHIP_ERROR AdvertiseOperational()
return CHIP_NO_ERROR;
}

/// Overloaded utility method for commissioner and commissionable advertisement
/// This method is used for both commissioner discovery and commissionable node discovery since
/// they share many fields.
/// commissionableNode = true : advertise commissionable node
/// commissionableNode = false : advertise commissioner
CHIP_ERROR Advertise(bool commissionableNode, CommissioningMode mode)
CHIP_ERROR BuildCommissionAdvertisingParameters(bool commissionableNode, CommissioningMode mode,
chip::Mdns::CommissionAdvertisingParameters & advertiseParameters)
{
bool commissioningMode = (mode != CommissioningMode::kDisabled);
bool additionalCommissioning = (mode == CommissioningMode::kEnabledEnhanced);

auto advertiseParameters = chip::Mdns::CommissionAdvertisingParameters()
.SetPort(commissionableNode ? GetSecuredPort() : GetUnsecuredPort())
.EnableIpV4(true);
advertiseParameters.SetPort(commissionableNode ? GetSecuredPort() : GetUnsecuredPort()).EnableIpV4(true);
advertiseParameters.SetCommissionAdvertiseMode(commissionableNode ? chip::Mdns::CommssionAdvertiseMode::kCommissionableNode
: chip::Mdns::CommssionAdvertiseMode::kCommissioner);

Expand Down Expand Up @@ -246,7 +241,18 @@ CHIP_ERROR Advertise(bool commissionableNode, CommissioningMode mode)
advertiseParameters.SetPairingInstr(chip::Optional<const char *>::Value(pairingInst));
}
}
return CHIP_NO_ERROR;
}

/// Overloaded utility method for commissioner and commissionable advertisement
/// This method is used for both commissioner discovery and commissionable node discovery since
/// they share many fields.
/// commissionableNode = true : advertise commissionable node
/// commissionableNode = false : advertise commissioner
CHIP_ERROR Advertise(bool commissionableNode, CommissioningMode mode)
{
auto advertiseParameters = chip::Mdns::CommissionAdvertisingParameters();
BuildCommissionAdvertisingParameters(commissionableNode, mode, advertiseParameters);
auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance();

ChipLogProgress(Discovery, "Advertise commission parameter vendorID=%u productID=%u discriminator=%04u/%02u",
Expand Down
4 changes: 4 additions & 0 deletions src/app/server/Mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <core/CHIPError.h>
#include <mdns/Advertiser.h>
#include <stddef.h>

namespace chip {
Expand Down Expand Up @@ -52,6 +53,9 @@ CHIP_ERROR AdvertiseCommissioner();
/// Set MDNS commissionable node advertisement
CHIP_ERROR AdvertiseCommissionableNode(CommissioningMode mode);

CHIP_ERROR BuildCommissionAdvertisingParameters(bool commissionableNode, CommissioningMode mode,
chip::Mdns::CommissionAdvertisingParameters & advertiseParameters);

/// (Re-)starts the minmdns server
/// - if device has not yet been commissioned, then commissioning mode will show as enabled (CM=1, AC=0)
/// - if device has been commissioned, then commissioning mode will reflect the state of mode argument
Expand Down
17 changes: 17 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,12 @@ CHIP_ERROR DeviceCommissioner::Init(CommissionerInitParams params)

mUdcServer->SetInstanceNameResolver(this);
mUdcServer->SetUserConfirmationProvider(this);

// Advertise as commissioner
if (mAdvertiseParameters != nullptr)
{
ReturnErrorOnFailure(chip::Mdns::ServiceAdvertiser::Instance().Advertise(*mAdvertiseParameters));
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -1494,6 +1500,17 @@ CHIP_ERROR DeviceCommissioner::SetUdcListenPort(uint16_t listenPort)
return CHIP_NO_ERROR;
}

CHIP_ERROR DeviceCommissioner::SetCommissionAdvertisingParameters(chip::Mdns::CommissionAdvertisingParameters * advertiseParameters)
{
if (mState == State::Initialized)
{
return CHIP_ERROR_INCORRECT_STATE;
}

mAdvertiseParameters = advertiseParameters;
return CHIP_NO_ERROR;
}

void DeviceCommissioner::FindCommissionableNode(char * instanceName)
{
Mdns::DiscoveryFilter filter(Mdns::DiscoveryFilterType::kInstanceName, instanceName);
Expand Down
8 changes: 6 additions & 2 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <core/CHIPTLV.h>
#include <credentials/CHIPOperationalCredentials.h>
#include <lib/support/Span.h>
#include <mdns/Advertiser.h>
#include <messaging/ExchangeMgr.h>
#include <messaging/ExchangeMgrDelegate.h>
#include <protocols/secure_channel/MessageCounterManager.h>
Expand Down Expand Up @@ -425,6 +426,8 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
* Set port for User Directed Commissioning
*/
CHIP_ERROR SetUdcListenPort(uint16_t listenPort);

CHIP_ERROR SetCommissionAdvertisingParameters(chip::Mdns::CommissionAdvertisingParameters * advertiseParameters);
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

/**
Expand Down Expand Up @@ -606,8 +609,9 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY // make this commissioner discoverable
UserDirectedCommissioningServer * mUdcServer = nullptr;
// mUdcTransportMgr is for insecure communication (ex. user directed commissioning)
DeviceTransportMgr * mUdcTransportMgr = nullptr;
uint16_t mUdcListenPort = CHIP_UDC_PORT;
DeviceTransportMgr * mUdcTransportMgr = nullptr;
uint16_t mUdcListenPort = CHIP_UDC_PORT;
chip::Mdns::CommissionAdvertisingParameters * mAdvertiseParameters = nullptr;
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

void PersistDeviceList();
Expand Down
8 changes: 8 additions & 0 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

#include <app/CommandSender.h>
#include <app/InteractionModelEngine.h>
#include <app/server/Mdns.h>
#include <controller/CHIPDevice.h>
#include <controller/CHIPDeviceController.h>
#include <controller/ExampleOperationalCredentialsIssuer.h>
Expand Down Expand Up @@ -210,6 +211,13 @@ ChipError::StorageType pychip_DeviceController_NewDeviceController(chip::Control
initParams.controllerNOC = nocSpan;

(*outDevCtrl)->SetUdpListenPort(CHIP_PORT + 1);
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY // make this commissioner discoverable
auto advertiseParameters = chip::Mdns::CommissionAdvertisingParameters();
chip::app::Mdns::BuildCommissionAdvertisingParameters(false, chip::app::Mdns::CommissioningMode::kDisabled,
advertiseParameters);
(*outDevCtrl)->SetCommissionAdvertisingParameters(&advertiseParameters);
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

err = (*outDevCtrl)->Init(initParams);
VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger());

Expand Down

0 comments on commit 4e1caad

Please sign in to comment.