Apply Upstream code (2021-03-15)
[platform/upstream/connectedhomeip.git] / src / lib / mdns / Discovery_ImplPlatform.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/Advertiser.h>
23 #include <lib/mdns/Resolver.h>
24 #include <lib/mdns/platform/Mdns.h>
25 #include <platform/CHIPDeviceConfig.h>
26
27 namespace chip {
28 namespace Mdns {
29
30 class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver
31 {
32 public:
33     CHIP_ERROR Init();
34
35     CHIP_ERROR Start(Inet::InetLayer * inetLayer, uint16_t port) override;
36
37     /// Advertises the CHIP node as an operational node
38     CHIP_ERROR Advertise(const OperationalAdvertisingParameters & params) override;
39
40     /// Advertises the CHIP node as a commisioning/commissionable node
41     CHIP_ERROR Advertise(const CommissionAdvertisingParameters & params) override;
42
43     /// This function stops publishing the device on mDNS.
44     CHIP_ERROR StopPublishDevice();
45
46     /// Registers a resolver delegate if none has been registered before
47     CHIP_ERROR SetResolverDelegate(ResolverDelegate * delegate) override;
48
49     /// Requests resolution of a node ID to its address
50     CHIP_ERROR ResolveNodeId(uint64_t nodeId, uint64_t fabricId, Inet::IPAddressType type) override;
51
52     static DiscoveryImplPlatform & GetInstance();
53
54 private:
55     DiscoveryImplPlatform();
56
57     DiscoveryImplPlatform(const DiscoveryImplPlatform &) = delete;
58     DiscoveryImplPlatform & operator=(const DiscoveryImplPlatform &) = delete;
59
60     CHIP_ERROR PublishUnprovisionedDevice(chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface);
61     CHIP_ERROR PublishProvisionedDevice(chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface);
62     CHIP_ERROR SetupHostname();
63
64     static void HandleNodeIdResolve(void * context, MdnsService * result, CHIP_ERROR error);
65     static void HandleMdnsInit(void * context, CHIP_ERROR initError);
66     static void HandleMdnsError(void * context, CHIP_ERROR initError);
67     static CHIP_ERROR GenerateRotatingDeviceId(char rotatingDeviceIdHexBuffer[], size_t & rotatingDeviceIdHexBufferSize);
68
69     OperationalAdvertisingParameters mOperationalAdvertisingParams;
70     bool mIsOperationalPublishing = false;
71     uint64_t mCommissionInstanceName;
72     CommissionAdvertisingParameters mCommissioningdvertisingParams;
73     bool mIsCommissionalPublishing = false;
74
75     bool mMdnsInitialized                = false;
76     ResolverDelegate * mResolverDelegate = nullptr;
77
78     static DiscoveryImplPlatform sManager;
79 };
80
81 } // namespace Mdns
82 } // namespace chip