Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / src / lib / mdns / DiscoveryManager.h
1 /*
2  *
3  *    Copyright (c) 2020 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 "core/CHIPError.h"
21 #include "inet/InetInterface.h"
22 #include "lib/mdns/platform/Mdns.h"
23 #include "platform/CHIPDeviceConfig.h"
24
25 namespace chip {
26 namespace Mdns {
27
28 class ResolveDelegate
29 {
30 public:
31     virtual void HandleNodeIdResolve(CHIP_ERROR error, uint64_t nodeId, const MdnsService & address) = 0;
32     virtual ~ResolveDelegate() {}
33 };
34
35 class DiscoveryManager
36 {
37 public:
38     /**
39      * This method initializes the publisher.
40      *
41      */
42     CHIP_ERROR Init();
43
44     /**
45      * This method publishes the device on mDNS.
46      *
47      * This function will fetch device name and other information and publish them
48      * via mDNS. If device meta data has changed, you can call this function again
49      * to update the information.
50      *
51      * @param[in] addressType The protocol version of the IP address.
52      * @param[in] interface   The interface to send mDNS multicast.
53      *
54      */
55     CHIP_ERROR StartPublishDevice(chip::Inet::IPAddressType addressType = chip::Inet::kIPAddressType_Any,
56                                   chip::Inet::InterfaceId interface     = INET_NULL_INTERFACEID);
57
58     /**
59      * This function stops publishing the device on mDNS.
60      *
61      */
62     CHIP_ERROR StopPublishDevice();
63
64     /**
65      * This function registers the delegate to handle node id resolve results.
66      *
67      */
68     CHIP_ERROR RegisterResolveDelegate(ResolveDelegate * delegate);
69
70     /**
71      * This function resolves a node id to its address.
72      *
73      */
74     CHIP_ERROR ResolveNodeId(uint64_t nodeId, uint64_t fabricId, chip::Inet::IPAddressType type = chip::Inet::kIPAddressType_Any);
75
76     static DiscoveryManager & GetInstance() { return sManager; }
77
78 private:
79     DiscoveryManager() = default;
80
81     DiscoveryManager(const DiscoveryManager &) = delete;
82     DiscoveryManager & operator=(const DiscoveryManager &) = delete;
83
84     CHIP_ERROR PublishUnprovisionedDevice(chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface);
85     CHIP_ERROR PublishProvisionedDevice(chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface);
86     CHIP_ERROR SetupHostname();
87
88     static void HandleNodeIdResolve(void * context, MdnsService * result, CHIP_ERROR error);
89     static void HandleMdnsInit(void * context, CHIP_ERROR initError);
90     static void HandleMdnsError(void * context, CHIP_ERROR initError);
91
92 #if CHIP_ENABLE_MDNS
93     uint64_t mUnprovisionedInstanceName;
94     bool mMdnsInitialized               = false;
95     bool mIsPublishingProvisionedDevice = false;
96     bool mIsPublishing                  = false;
97 #endif // CHIP_ENABLE_MDNS
98     ResolveDelegate * mResolveDelegate = nullptr;
99
100     static DiscoveryManager sManager;
101 };
102
103 } // namespace Mdns
104 } // namespace chip