1 /* 2 * Copyright (c) 2023, 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 infrastructure DNS-SD (mDNS) platform. 32 */ 33 34 #ifndef DNSSD_HPP_ 35 #define DNSSD_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE 40 41 #include <openthread/platform/dnssd.h> 42 43 #include "common/clearable.hpp" 44 #include "common/locator.hpp" 45 #include "common/non_copyable.hpp" 46 #include "net/ip6_address.hpp" 47 48 namespace ot { 49 50 /** 51 * @addtogroup core-dns 52 * 53 * @brief 54 * This module includes definitions for DNS-SD (mDNS) platform. 55 * 56 * @{ 57 * 58 */ 59 60 extern "C" void otPlatDnssdStateHandleStateChange(otInstance *aInstance); 61 62 /** 63 * Represents DNS-SD (mDNS) platform. 64 * 65 */ 66 class Dnssd : public InstanceLocator, private NonCopyable 67 { 68 friend void otPlatDnssdStateHandleStateChange(otInstance *aInstance); 69 70 public: 71 /** 72 * Represents state of DNS-SD platform. 73 * 74 */ 75 enum State : uint8_t 76 { 77 kStopped = OT_PLAT_DNSSD_STOPPED, ///< Stopped and unable to register any service or host. 78 kReady = OT_PLAT_DNSSD_READY ///< Running and ready to register service or host. 79 }; 80 81 typedef otPlatDnssdRequestId RequestId; ///< A request ID. 82 typedef otPlatDnssdRegisterCallback RegisterCallback; ///< The registration request callback 83 84 class Host : public otPlatDnssdHost, public Clearable<Host> ///< Host information. 85 { 86 }; 87 88 class Service : public otPlatDnssdService, public Clearable<Service> ///< Service information. 89 { 90 }; 91 92 class Key : public otPlatDnssdKey, public Clearable<Key> ///< Key information 93 { 94 }; 95 96 /** 97 * Represents a range of `RequestId` values. 98 * 99 * The range is stored using start and end ID values. The implementation handles the case when ID values roll over. 100 * 101 */ 102 struct RequestIdRange : public Clearable<RequestIdRange> 103 { 104 /** 105 * Initializes a range as empty. 106 * 107 */ RequestIdRangeot::Dnssd::RequestIdRange108 RequestIdRange(void) 109 : mStart(0) 110 , mEnd(0) 111 { 112 } 113 114 /** 115 * Adds a request ID to the range. 116 * 117 * @param[in] aId The ID to add to the range. 118 * 119 */ 120 void Add(RequestId aId); 121 122 /** 123 * Removes a request ID from the range. 124 * 125 * @param[in] aId The ID to remove from the range. 126 * 127 */ 128 void Remove(RequestId aId); 129 130 /** 131 * Indicates whether or not a given ID is contained within the range. 132 * 133 * @param[in] aId The ID to check. 134 * 135 * @retval TRUE The @p aID is contained within the range. 136 * @retval FALSE The @p aId is not contained within the range. 137 * 138 */ 139 bool Contains(RequestId aId) const; 140 141 /** 142 * Indicates whether or not the range is empty. 143 * 144 * @retval TRUE The range is empty. 145 * @retval FALSE The range is not empty. 146 * 147 */ IsEmptyot::Dnssd::RequestIdRange148 bool IsEmpty(void) const { return (mStart == mEnd); } 149 150 private: 151 // The range is represented as all `RequestId` values from 152 // `mStart` up to, but not including, `mEnd`. It uses serial 153 // number arithmetic logic when comparing `RequestId` values, 154 // so `Contains()` and other methods work correctly even when 155 // the ID value rolls over. 156 157 RequestId mStart; 158 RequestId mEnd; 159 }; 160 161 /** 162 * Initializes `Dnssd` object. 163 * 164 * @param[in] aInstance The OpenThread instance. 165 * 166 */ 167 explicit Dnssd(Instance &aInstance); 168 169 /** 170 * Gets the current state of DNS-SD platform module. 171 * 172 * @returns The current state of DNS-SD platform. 173 * 174 */ 175 State GetState(void) const; 176 177 /** 178 * Indicates whether or not DNS-SD platform is ready (in `kReady` state). 179 * 180 * @retval TRUE The DNS-SD platform is ready. 181 * @retval FALSE The DNS-SD platform is not ready. 182 * 183 */ IsReady(void) const184 bool IsReady(void) const { return GetState() == kReady; } 185 186 /** 187 * Registers or updates a service on the infrastructure network's DNS-SD module. 188 * 189 * Refer to the documentation for `otPlatDnssdRegisterService()`, for a more detailed description of the behavior 190 * of this method. 191 * 192 * @param[in] aService Information about service to unregister. 193 * @param[in] aRequestId The ID associated with this request. 194 * @param[in] aCallback The callback function pointer to report the outcome (may be `nullptr`). 195 * 196 */ 197 void RegisterService(const Service &aService, RequestId aRequestId, RegisterCallback aCallback); 198 199 /** 200 * Unregisters a service on the infrastructure network's DNS-SD module. 201 * 202 * Refer to the documentation for `otPlatDnssdUnregisterService()`, for a more detailed description of the behavior 203 * of this method. 204 * 205 * @param[in] aService Information about service to unregister. 206 * @param[in] aRequestId The ID associated with this request. 207 * @param[in] aCallback The callback function pointer to report the outcome (may be `nullptr`). 208 * 209 */ 210 void UnregisterService(const Service &aService, RequestId aRequestId, RegisterCallback aCallback); 211 212 /** 213 * Registers or updates a host on the infrastructure network's DNS-SD module. 214 * 215 * Refer to the documentation for `otPlatDnssdRegisterHost()`, for a more detailed description of the behavior 216 * of this method. 217 * 218 * @param[in] aHost Information about host to register. 219 * @param[in] aRequestId The ID associated with this request. 220 * @param[in] aCallback The callback function pointer to report the outcome (may be `nullptr`). 221 * 222 */ 223 void RegisterHost(const Host &aHost, RequestId aRequestId, RegisterCallback aCallback); 224 225 /** 226 * Unregisters a host on the infrastructure network's DNS-SD module. 227 * 228 * Refer to the documentation for `otPlatDnssdUnregisterHost()`, for a more detailed description of the behavior 229 * of this method. 230 * 231 * @param[in] aHost Information about the host to unregister. 232 * @param[in] aRequestId The ID associated with this request. 233 * @param[in] aCallback The callback function pointer to report the outcome (may be NULL if no callback needed). 234 * 235 */ 236 void UnregisterHost(const Host &aHost, RequestId aRequestId, RegisterCallback aCallback); 237 238 /** 239 * Registers or updates a key record on the infrastructure network's DNS-SD module. 240 * 241 * Refer to the documentation for `otPlatDnssdRegisterKey()`, for a more detailed description of the behavior 242 * of this method. 243 * 244 * @param[in] aKey Information about the key to register. 245 * @param[in] aRequestId The ID associated with this request. 246 * @param[in] aCallback The callback function pointer to report the outcome (may be `nullptr`). 247 * 248 */ 249 void RegisterKey(const Key &aKey, RequestId aRequestId, RegisterCallback aCallback); 250 251 /** 252 * Unregisters a key record on the infrastructure network's DNS-SD module. 253 * 254 * Refer to the documentation for `otPlatDnssdUnregisterKey()`, for a more detailed description of the behavior 255 * of this method. 256 * 257 * @param[in] aKey Information about the key to unregister. 258 * @param[in] aRequestId The ID associated with this request. 259 * @param[in] aCallback The callback function pointer to report the outcome (may be NULL if no callback needed). 260 * 261 */ 262 void UnregisterKey(const Key &aKey, RequestId aRequestId, RegisterCallback aCallback); 263 264 private: 265 void HandleStateChange(void); 266 }; 267 268 /** 269 * @} 270 * 271 */ 272 273 DefineMapEnum(otPlatDnssdState, Dnssd::State); 274 DefineCoreType(otPlatDnssdService, Dnssd::Service); 275 DefineCoreType(otPlatDnssdHost, Dnssd::Host); 276 DefineCoreType(otPlatDnssdKey, Dnssd::Key); 277 278 } // namespace ot 279 280 #endif // OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE 281 282 #endif // DNSSD_HPP_ 283