Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <lib/dnssd/Resolver.h>
21 :
22 : namespace chip {
23 : namespace Dnssd {
24 :
25 : /**
26 : * Convenience class for discovering Matter commissioners and commissionable nodes.
27 : *
28 : * The class simplifies the usage of the global DNS-SD resolver to discover Matter nodes by managing
29 : * the lifetime of the discovery context object.
30 : *
31 : * The proxy provides a method to register an external commissioning delegate whose
32 : * `OnNodeDiscovered` method is called whenever a new node is discovered. The delegate is
33 : * automatically unregistered when the proxy is shut down or destroyed.
34 : *
35 : * The proxy can be safely shut down or destroyed even if the last discovery operation has not
36 : * finished yet.
37 : */
38 : class ResolverProxy
39 : {
40 : public:
41 8 : explicit ResolverProxy(Resolver * resolver = nullptr) : mResolver(resolver != nullptr ? *resolver : Resolver::Instance()) {}
42 9 : ~ResolverProxy() { Shutdown(); }
43 :
44 : CHIP_ERROR Init(Inet::EndPointManager<Inet::UDPEndPoint> * udpEndPoint = nullptr);
45 : void Shutdown();
46 :
47 12 : void SetDiscoveryDelegate(DiscoverNodeDelegate * delegate)
48 : {
49 12 : if (mContext != nullptr)
50 : {
51 6 : mContext->SetDiscoveryDelegate(delegate);
52 : }
53 12 : }
54 :
55 : CHIP_ERROR DiscoverCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter());
56 : CHIP_ERROR DiscoverCommissioners(DiscoveryFilter filter = DiscoveryFilter());
57 : CHIP_ERROR DiscoverOperationalNodes(DiscoveryFilter filter = DiscoveryFilter());
58 : CHIP_ERROR StopDiscovery();
59 :
60 : private:
61 : Resolver & mResolver;
62 : DiscoveryContext * mContext = nullptr;
63 : };
64 :
65 : } // namespace Dnssd
66 : } // namespace chip
|