• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 implements infrastructure DNS-SD (mDNS) platform APIs.
32  */
33 
34 #include "dnssd.hpp"
35 
36 #if OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE
37 
38 #include "common/code_utils.hpp"
39 #include "common/locator_getters.hpp"
40 #include "instance/instance.hpp"
41 
42 namespace ot {
43 
44 //---------------------------------------------------------------------------------------------------------------------
45 // Dnssd::RequestIdRange
46 
Add(RequestId aId)47 void Dnssd::RequestIdRange::Add(RequestId aId)
48 {
49     if (IsEmpty())
50     {
51         mStart = aId;
52         mEnd   = aId + 1;
53     }
54     else if (SerialNumber::IsLess(aId, mStart)) // Equivalent to (aId < mStart)
55     {
56         mStart = aId;
57     }
58     else if (!SerialNumber::IsLess(aId, mEnd)) // Equivalent to !(aId < mEd) -> (aId >= mEnd)
59     {
60         mEnd = aId + 1;
61     }
62 }
63 
Remove(RequestId aId)64 void Dnssd::RequestIdRange::Remove(RequestId aId)
65 {
66     if (!IsEmpty())
67     {
68         if (aId == mStart)
69         {
70             mStart++;
71         }
72         else if (aId + 1 == mEnd)
73         {
74             mEnd--;
75         }
76     }
77 }
78 
Contains(RequestId aId) const79 bool Dnssd::RequestIdRange::Contains(RequestId aId) const
80 {
81     // Equivalent to `(aId >= mStart) && (aId < mEnd)`
82 
83     return (!SerialNumber::IsLess(aId, mStart) && SerialNumber::IsLess(aId, mEnd));
84 }
85 
86 //---------------------------------------------------------------------------------------------------------------------
87 // Dnssd
88 
Dnssd(Instance & aInstance)89 Dnssd::Dnssd(Instance &aInstance)
90     : InstanceLocator(aInstance)
91 {
92 }
93 
GetState(void) const94 Dnssd::State Dnssd::GetState(void) const { return MapEnum(otPlatDnssdGetState(&GetInstance())); }
95 
RegisterService(const Service & aService,RequestId aRequestId,RegisterCallback aCallback)96 void Dnssd::RegisterService(const Service &aService, RequestId aRequestId, RegisterCallback aCallback)
97 {
98     if (IsReady())
99     {
100         otPlatDnssdRegisterService(&GetInstance(), &aService, aRequestId, aCallback);
101     }
102 }
103 
UnregisterService(const Service & aService,RequestId aRequestId,RegisterCallback aCallback)104 void Dnssd::UnregisterService(const Service &aService, RequestId aRequestId, RegisterCallback aCallback)
105 {
106     if (IsReady())
107     {
108         otPlatDnssdUnregisterService(&GetInstance(), &aService, aRequestId, aCallback);
109     }
110 }
111 
RegisterHost(const Host & aHost,RequestId aRequestId,RegisterCallback aCallback)112 void Dnssd::RegisterHost(const Host &aHost, RequestId aRequestId, RegisterCallback aCallback)
113 {
114     if (IsReady())
115     {
116         otPlatDnssdRegisterHost(&GetInstance(), &aHost, aRequestId, aCallback);
117     }
118 }
119 
UnregisterHost(const Host & aHost,RequestId aRequestId,RegisterCallback aCallback)120 void Dnssd::UnregisterHost(const Host &aHost, RequestId aRequestId, RegisterCallback aCallback)
121 {
122     if (IsReady())
123     {
124         otPlatDnssdUnregisterHost(&GetInstance(), &aHost, aRequestId, aCallback);
125     }
126 }
127 
RegisterKey(const Key & aKey,RequestId aRequestId,RegisterCallback aCallback)128 void Dnssd::RegisterKey(const Key &aKey, RequestId aRequestId, RegisterCallback aCallback)
129 {
130     if (IsReady())
131     {
132         otPlatDnssdRegisterKey(&GetInstance(), &aKey, aRequestId, aCallback);
133     }
134 }
135 
UnregisterKey(const Key & aKey,RequestId aRequestId,RegisterCallback aCallback)136 void Dnssd::UnregisterKey(const Key &aKey, RequestId aRequestId, RegisterCallback aCallback)
137 {
138     if (IsReady())
139     {
140         otPlatDnssdUnregisterKey(&GetInstance(), &aKey, aRequestId, aCallback);
141     }
142 }
143 
HandleStateChange(void)144 void Dnssd::HandleStateChange(void)
145 {
146 #if OPENTHREAD_CONFIG_SRP_SERVER_ADVERTISING_PROXY_ENABLE
147     Get<Srp::AdvertisingProxy>().HandleDnssdPlatformStateChange();
148 #endif
149 }
150 
otPlatDnssdStateHandleStateChange(otInstance * aInstance)151 extern "C" void otPlatDnssdStateHandleStateChange(otInstance *aInstance)
152 {
153     AsCoreType(aInstance).Get<Dnssd>().HandleStateChange();
154 }
155 
156 } // namespace ot
157 
158 #endif // OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE
159