-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[python/mdns] Add "resolve" command for operational discovery (#5025)
* [mdns] Add Resolver interface Expose the mDNS resolver functionality implemented for Avahi/Linux using a new Resolver interface. * [mdns] Fix build with chip_mdns_advertiser="platform" * [mdns/python] Add command to resolve address of a CHIP device Add DeviceAddressUpdater class which updates device addresses in the device controller based on responses from mDNS resolver. A result of an update/resolution request can be passed to an optional DeviceAddressUpdateDelegate. Add "resolve <fabricid> <nodeid>" command to the Python CHIP controller. Usage: 1. Build CHIP with chip_mdns_advertiser="platform" argument. 2. Start Python CHIP controller. 3. Pair a CHIP device using "connect -ble" command. 4. Resolve the node ID using "resolve <fabricid> <nodeid>". Note that Thread devices currently don't support the DNS-SD service registration, but one can still test the new command by registering the necessary services manually, e.g. avahi-publish-address test-host.local. fd11:22::1 & avahi-publish-publish-service -H test-host.local \ <fabricid-hex>-<nodeid-hex> _chip._tcp 11097 * [mdns] Fix improper global object initialization order. On Linux, DiscoveryImplPlatform constructor refers to MdnsAvahi global object which may not yet be initialized. Some refactoring in the code is required to come up with a better solution, but for now make sure that DiscoveryImplPlatform is not initialized prior to MdnsAvahi. * [mdns] Apply code review comments Also, fix updating the device address in the device controller as previously the connection state wasn't updated.
- Loading branch information
1 parent
fc666df
commit a578416
Showing
19 changed files
with
595 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include "DeviceAddressUpdater.h" | ||
|
||
#include <controller/CHIPDeviceController.h> | ||
#include <support/ReturnMacros.h> | ||
|
||
namespace chip { | ||
namespace Controller { | ||
|
||
CHIP_ERROR DeviceAddressUpdater::Init(DeviceController * controller, DeviceAddressUpdateDelegate * delegate) | ||
{ | ||
VerifyOrReturnError(mController == nullptr, CHIP_ERROR_INCORRECT_STATE); | ||
VerifyOrReturnError(mDelegate == nullptr, CHIP_ERROR_INCORRECT_STATE); | ||
|
||
mController = controller; | ||
mDelegate = delegate; | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
void DeviceAddressUpdater::OnNodeIdResolved(NodeId nodeId, const Mdns::ResolvedNodeData & nodeData) | ||
{ | ||
CHIP_ERROR error = CHIP_NO_ERROR; | ||
Device * device = nullptr; | ||
|
||
VerifyOrExit(nodeData.mAddress.Type() != Inet::kIPAddressType_Any, error = CHIP_ERROR_INVALID_ADDRESS); | ||
VerifyOrExit(mController != nullptr, error = CHIP_ERROR_INCORRECT_STATE); | ||
SuccessOrExit(error = mController->GetDevice(nodeId, &device)); | ||
|
||
device->UpdateAddress(Transport::PeerAddress::UDP(nodeData.mAddress, nodeData.mPort, nodeData.mInterfaceId)); | ||
|
||
exit: | ||
if (mDelegate != nullptr) | ||
{ | ||
mDelegate->OnAddressUpdateComplete(nodeId, error); | ||
} | ||
} | ||
|
||
void DeviceAddressUpdater::OnNodeIdResolutionFailed(NodeId nodeId, CHIP_ERROR error) | ||
{ | ||
if (mDelegate != nullptr) | ||
{ | ||
mDelegate->OnAddressUpdateComplete(nodeId, error); | ||
} | ||
} | ||
|
||
} // namespace Controller | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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 <mdns/Resolver.h> | ||
#include <support/DLLUtil.h> | ||
#include <transport/raw/MessageHeader.h> | ||
|
||
namespace chip { | ||
namespace Controller { | ||
|
||
class DeviceController; | ||
|
||
/// Callbacks for CHIP device address resolution | ||
class DLL_EXPORT DeviceAddressUpdateDelegate | ||
{ | ||
public: | ||
virtual ~DeviceAddressUpdateDelegate() {} | ||
virtual void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) = 0; | ||
}; | ||
|
||
/// Class for updating CHIP devices' addresses based on responses from mDNS Resolver | ||
class DLL_EXPORT DeviceAddressUpdater : public Mdns::ResolverDelegate | ||
{ | ||
public: | ||
CHIP_ERROR Init(DeviceController * controller, DeviceAddressUpdateDelegate * delegate = nullptr); | ||
|
||
private: | ||
// Mdns::ResolverDelegate Implementation | ||
void OnNodeIdResolved(NodeId nodeId, const Mdns::ResolvedNodeData & nodeData) override; | ||
void OnNodeIdResolutionFailed(NodeId nodeId, CHIP_ERROR error) override; | ||
|
||
DeviceController * mController = nullptr; | ||
DeviceAddressUpdateDelegate * mDelegate = nullptr; | ||
}; | ||
|
||
} // namespace Controller | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.