1 /* 2 * Copyright (c) 2025, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for implementing OpenThread DNS-SD platform APIs. 32 */ 33 34 #ifndef OTBR_AGENT_POSIX_DNSSD_HPP_ 35 #define OTBR_AGENT_POSIX_DNSSD_HPP_ 36 37 #include "openthread-br/config.h" 38 39 #include <functional> 40 #include <string> 41 42 #include <openthread/instance.h> 43 #include <openthread/platform/dnssd.h> 44 45 #include "common/code_utils.hpp" 46 #include "common/dns_utils.hpp" 47 #include "host/thread_host.hpp" 48 #include "mdns/mdns.hpp" 49 50 namespace otbr { 51 52 /** 53 * This class implements the DNS-SD platform. 54 * 55 */ 56 class DnssdPlatform : public Mdns::StateObserver, private NonCopyable 57 { 58 public: 59 /** 60 * Initializes the `DnssdPlatform` instance 61 * 62 * @param[in] aPublisher A reference to `Mdns::Publisher` to use. 63 */ 64 DnssdPlatform(Mdns::Publisher &aPublisher); 65 66 /** 67 * Starts the `DnssdPlatform` module. 68 */ 69 void Start(void); 70 71 /** 72 * Stops the `DnssdPlatform` module 73 */ 74 void Stop(void); 75 76 /** 77 * Gets the singleton `DnssdPlatform` instance. 78 * 79 * @returns A reference to the `DnssdPlatform` instance. 80 */ Get(void)81 static DnssdPlatform &Get(void) { return *sDnssdPlatform; } 82 83 typedef std::function<void(otPlatDnssdState)> DnssdStateChangeCallback; 84 85 /** 86 * Sets a Dnssd State changed callback. 87 * 88 * The main usage of this method is to call `otPlatDnssdStateHandleStateChange` to notify OT core about the change 89 * when the dnssd state changes. We shouldn't directly call `otPlatDnssdStateHandleStateChange` in this module' 90 * because it only fits the RCP case. 91 * 92 * @param[in] aCallback The callback to be invoked when the dnssd state changes. 93 */ 94 void SetDnssdStateChangedCallback(DnssdStateChangeCallback aCallback); 95 96 //----------------------------------------------------------------------------------------------------------------- 97 // `otPlatDnssd` APIs (see `openthread/include/openthread/platform/dnssd.h` for detailed documentation). 98 99 typedef otPlatDnssdState State; 100 typedef otPlatDnssdService Service; 101 typedef otPlatDnssdHost Host; 102 typedef otPlatDnssdKey Key; 103 typedef otPlatDnssdRequestId RequestId; 104 typedef std::function<void(otPlatDnssdRequestId, otError)> RegisterCallback; 105 GetState(void) const106 State GetState(void) const { return mState; } 107 void RegisterService(const Service &aService, RequestId aRequestId, RegisterCallback aCallback); 108 void UnregisterService(const Service &aService, RequestId aRequestId, RegisterCallback aCallback); 109 void RegisterHost(const Host &aHost, RequestId aRequestId, RegisterCallback aCallback); 110 void UnregisterHost(const Host &aHost, RequestId aRequestId, RegisterCallback aCallback); 111 void RegisterKey(const Key &aKey, RequestId aRequestId, RegisterCallback aCallback); 112 void UnregisterKey(const Key &aKey, RequestId aRequestId, RegisterCallback aCallback); 113 114 private: 115 static constexpr State kStateReady = OT_PLAT_DNSSD_READY; 116 static constexpr State kStateStopped = OT_PLAT_DNSSD_STOPPED; 117 118 void HandleMdnsState(Mdns::Publisher::State aState) override; 119 120 void UpdateState(void); 121 Mdns::Publisher::ResultCallback MakePublisherCallback(RequestId aRequestId, RegisterCallback aCallback); 122 123 static std::string KeyNameFor(const Key &aKey); 124 125 static DnssdPlatform *sDnssdPlatform; 126 127 Mdns::Publisher &mPublisher; 128 State mState; 129 bool mRunning; 130 Mdns::Publisher::State mPublisherState; 131 DnssdStateChangeCallback mStateChangeCallback; 132 }; 133 134 } // namespace otbr 135 136 #endif // OTBR_AGENT_POSIX_DNSSD_HPP_ 137