1 /*
2  *  Copyright (c) 2020, 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 defines the OpenThread SRP server API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
37 
38 #include <openthread/srp_server.h>
39 
40 #include "common/as_core_type.hpp"
41 #include "common/locator_getters.hpp"
42 
43 using namespace ot;
44 
otSrpServerGetDomain(otInstance * aInstance)45 const char *otSrpServerGetDomain(otInstance *aInstance) { return AsCoreType(aInstance).Get<Srp::Server>().GetDomain(); }
46 
otSrpServerSetDomain(otInstance * aInstance,const char * aDomain)47 otError otSrpServerSetDomain(otInstance *aInstance, const char *aDomain)
48 {
49     return AsCoreType(aInstance).Get<Srp::Server>().SetDomain(aDomain);
50 }
51 
otSrpServerGetState(otInstance * aInstance)52 otSrpServerState otSrpServerGetState(otInstance *aInstance)
53 {
54     return MapEnum(AsCoreType(aInstance).Get<Srp::Server>().GetState());
55 }
56 
otSrpServerGetPort(otInstance * aInstance)57 uint16_t otSrpServerGetPort(otInstance *aInstance) { return AsCoreType(aInstance).Get<Srp::Server>().GetPort(); }
58 
otSrpServerGetAddressMode(otInstance * aInstance)59 otSrpServerAddressMode otSrpServerGetAddressMode(otInstance *aInstance)
60 {
61     return MapEnum(AsCoreType(aInstance).Get<Srp::Server>().GetAddressMode());
62 }
63 
otSrpServerSetAddressMode(otInstance * aInstance,otSrpServerAddressMode aMode)64 otError otSrpServerSetAddressMode(otInstance *aInstance, otSrpServerAddressMode aMode)
65 {
66     return AsCoreType(aInstance).Get<Srp::Server>().SetAddressMode(MapEnum(aMode));
67 }
68 
otSrpServerGetAnycastModeSequenceNumber(otInstance * aInstance)69 uint8_t otSrpServerGetAnycastModeSequenceNumber(otInstance *aInstance)
70 {
71     return AsCoreType(aInstance).Get<Srp::Server>().GetAnycastModeSequenceNumber();
72 }
73 
otSrpServerSetAnycastModeSequenceNumber(otInstance * aInstance,uint8_t aSequenceNumber)74 otError otSrpServerSetAnycastModeSequenceNumber(otInstance *aInstance, uint8_t aSequenceNumber)
75 {
76     return AsCoreType(aInstance).Get<Srp::Server>().SetAnycastModeSequenceNumber(aSequenceNumber);
77 }
78 
otSrpServerSetEnabled(otInstance * aInstance,bool aEnabled)79 void otSrpServerSetEnabled(otInstance *aInstance, bool aEnabled)
80 {
81     AsCoreType(aInstance).Get<Srp::Server>().SetEnabled(aEnabled);
82 }
83 
84 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
otSrpServerSetAutoEnableMode(otInstance * aInstance,bool aEnabled)85 void otSrpServerSetAutoEnableMode(otInstance *aInstance, bool aEnabled)
86 {
87     AsCoreType(aInstance).Get<Srp::Server>().SetAutoEnableMode(aEnabled);
88 }
89 
otSrpServerIsAutoEnableMode(otInstance * aInstance)90 bool otSrpServerIsAutoEnableMode(otInstance *aInstance)
91 {
92     return AsCoreType(aInstance).Get<Srp::Server>().IsAutoEnableMode();
93 }
94 #endif
95 
otSrpServerGetTtlConfig(otInstance * aInstance,otSrpServerTtlConfig * aTtlConfig)96 void otSrpServerGetTtlConfig(otInstance *aInstance, otSrpServerTtlConfig *aTtlConfig)
97 {
98     AsCoreType(aInstance).Get<Srp::Server>().GetTtlConfig(AsCoreType(aTtlConfig));
99 }
100 
otSrpServerSetTtlConfig(otInstance * aInstance,const otSrpServerTtlConfig * aTtlConfig)101 otError otSrpServerSetTtlConfig(otInstance *aInstance, const otSrpServerTtlConfig *aTtlConfig)
102 {
103     return AsCoreType(aInstance).Get<Srp::Server>().SetTtlConfig(AsCoreType(aTtlConfig));
104 }
105 
otSrpServerGetLeaseConfig(otInstance * aInstance,otSrpServerLeaseConfig * aLeaseConfig)106 void otSrpServerGetLeaseConfig(otInstance *aInstance, otSrpServerLeaseConfig *aLeaseConfig)
107 {
108     AsCoreType(aInstance).Get<Srp::Server>().GetLeaseConfig(AsCoreType(aLeaseConfig));
109 }
110 
otSrpServerSetLeaseConfig(otInstance * aInstance,const otSrpServerLeaseConfig * aLeaseConfig)111 otError otSrpServerSetLeaseConfig(otInstance *aInstance, const otSrpServerLeaseConfig *aLeaseConfig)
112 {
113     return AsCoreType(aInstance).Get<Srp::Server>().SetLeaseConfig(AsCoreType(aLeaseConfig));
114 }
115 
otSrpServerSetServiceUpdateHandler(otInstance * aInstance,otSrpServerServiceUpdateHandler aServiceHandler,void * aContext)116 void otSrpServerSetServiceUpdateHandler(otInstance                     *aInstance,
117                                         otSrpServerServiceUpdateHandler aServiceHandler,
118                                         void                           *aContext)
119 {
120     AsCoreType(aInstance).Get<Srp::Server>().SetServiceHandler(aServiceHandler, aContext);
121 }
122 
otSrpServerHandleServiceUpdateResult(otInstance * aInstance,otSrpServerServiceUpdateId aId,otError aError)123 void otSrpServerHandleServiceUpdateResult(otInstance *aInstance, otSrpServerServiceUpdateId aId, otError aError)
124 {
125     AsCoreType(aInstance).Get<Srp::Server>().HandleServiceUpdateResult(aId, aError);
126 }
127 
otSrpServerGetNextHost(otInstance * aInstance,const otSrpServerHost * aHost)128 const otSrpServerHost *otSrpServerGetNextHost(otInstance *aInstance, const otSrpServerHost *aHost)
129 {
130     return AsCoreType(aInstance).Get<Srp::Server>().GetNextHost(AsCoreTypePtr(aHost));
131 }
132 
otSrpServerGetResponseCounters(otInstance * aInstance)133 const otSrpServerResponseCounters *otSrpServerGetResponseCounters(otInstance *aInstance)
134 {
135     return AsCoreType(aInstance).Get<Srp::Server>().GetResponseCounters();
136 }
137 
otSrpServerHostIsDeleted(const otSrpServerHost * aHost)138 bool otSrpServerHostIsDeleted(const otSrpServerHost *aHost) { return AsCoreType(aHost).IsDeleted(); }
139 
otSrpServerHostGetFullName(const otSrpServerHost * aHost)140 const char *otSrpServerHostGetFullName(const otSrpServerHost *aHost) { return AsCoreType(aHost).GetFullName(); }
141 
otSrpServerHostMatchesFullName(const otSrpServerHost * aHost,const char * aFullName)142 bool otSrpServerHostMatchesFullName(const otSrpServerHost *aHost, const char *aFullName)
143 {
144     return AsCoreType(aHost).Matches(aFullName);
145 }
146 
otSrpServerHostGetAddresses(const otSrpServerHost * aHost,uint8_t * aAddressesNum)147 const otIp6Address *otSrpServerHostGetAddresses(const otSrpServerHost *aHost, uint8_t *aAddressesNum)
148 {
149     return AsCoreType(aHost).GetAddresses(*aAddressesNum);
150 }
151 
otSrpServerHostGetLeaseInfo(const otSrpServerHost * aHost,otSrpServerLeaseInfo * aLeaseInfo)152 void otSrpServerHostGetLeaseInfo(const otSrpServerHost *aHost, otSrpServerLeaseInfo *aLeaseInfo)
153 {
154     AsCoreType(aHost).GetLeaseInfo(*aLeaseInfo);
155 }
156 
otSrpServerHostGetKeyLease(const otSrpServerHost * aHost)157 uint32_t otSrpServerHostGetKeyLease(const otSrpServerHost *aHost) { return AsCoreType(aHost).GetKeyLease(); }
158 
otSrpServerHostGetNextService(const otSrpServerHost * aHost,const otSrpServerService * aService)159 const otSrpServerService *otSrpServerHostGetNextService(const otSrpServerHost    *aHost,
160                                                         const otSrpServerService *aService)
161 {
162     return AsCoreType(aHost).GetNextService(AsCoreTypePtr(aService));
163 }
164 
otSrpServerServiceIsDeleted(const otSrpServerService * aService)165 bool otSrpServerServiceIsDeleted(const otSrpServerService *aService) { return AsCoreType(aService).IsDeleted(); }
166 
otSrpServerServiceGetInstanceName(const otSrpServerService * aService)167 const char *otSrpServerServiceGetInstanceName(const otSrpServerService *aService)
168 {
169     return AsCoreType(aService).GetInstanceName();
170 }
171 
otSrpServerServiceMatchesInstanceName(const otSrpServerService * aService,const char * aInstanceName)172 bool otSrpServerServiceMatchesInstanceName(const otSrpServerService *aService, const char *aInstanceName)
173 {
174     return AsCoreType(aService).MatchesInstanceName(aInstanceName);
175 }
176 
otSrpServerServiceGetInstanceLabel(const otSrpServerService * aService)177 const char *otSrpServerServiceGetInstanceLabel(const otSrpServerService *aService)
178 {
179     return AsCoreType(aService).GetInstanceLabel();
180 }
181 
otSrpServerServiceGetServiceName(const otSrpServerService * aService)182 const char *otSrpServerServiceGetServiceName(const otSrpServerService *aService)
183 {
184     return AsCoreType(aService).GetServiceName();
185 }
186 
otSrpServerServiceMatchesServiceName(const otSrpServerService * aService,const char * aServiceName)187 bool otSrpServerServiceMatchesServiceName(const otSrpServerService *aService, const char *aServiceName)
188 {
189     return AsCoreType(aService).MatchesServiceName(aServiceName);
190 }
191 
otSrpServerServiceGetNumberOfSubTypes(const otSrpServerService * aService)192 uint16_t otSrpServerServiceGetNumberOfSubTypes(const otSrpServerService *aService)
193 {
194     return AsCoreType(aService).GetNumberOfSubTypes();
195 }
196 
otSrpServerServiceGetSubTypeServiceNameAt(const otSrpServerService * aService,uint16_t aIndex)197 const char *otSrpServerServiceGetSubTypeServiceNameAt(const otSrpServerService *aService, uint16_t aIndex)
198 {
199     return AsCoreType(aService).GetSubTypeServiceNameAt(aIndex);
200 }
201 
otSrpServerServiceHasSubTypeServiceName(const otSrpServerService * aService,const char * aSubTypeServiceName)202 bool otSrpServerServiceHasSubTypeServiceName(const otSrpServerService *aService, const char *aSubTypeServiceName)
203 {
204     return AsCoreType(aService).HasSubTypeServiceName(aSubTypeServiceName);
205 }
206 
otSrpServerParseSubTypeServiceName(const char * aSubTypeServiceName,char * aLabel,uint8_t aLabelSize)207 otError otSrpServerParseSubTypeServiceName(const char *aSubTypeServiceName, char *aLabel, uint8_t aLabelSize)
208 {
209     return Srp::Server::Service::ParseSubTypeServiceName(aSubTypeServiceName, aLabel, aLabelSize);
210 }
211 
otSrpServerServiceGetPort(const otSrpServerService * aService)212 uint16_t otSrpServerServiceGetPort(const otSrpServerService *aService) { return AsCoreType(aService).GetPort(); }
213 
otSrpServerServiceGetWeight(const otSrpServerService * aService)214 uint16_t otSrpServerServiceGetWeight(const otSrpServerService *aService) { return AsCoreType(aService).GetWeight(); }
215 
otSrpServerServiceGetPriority(const otSrpServerService * aService)216 uint16_t otSrpServerServiceGetPriority(const otSrpServerService *aService)
217 {
218     return AsCoreType(aService).GetPriority();
219 }
220 
otSrpServerServiceGetTtl(const otSrpServerService * aService)221 uint32_t otSrpServerServiceGetTtl(const otSrpServerService *aService) { return AsCoreType(aService).GetTtl(); }
222 
otSrpServerServiceGetTxtData(const otSrpServerService * aService,uint16_t * aDataLength)223 const uint8_t *otSrpServerServiceGetTxtData(const otSrpServerService *aService, uint16_t *aDataLength)
224 {
225     *aDataLength = AsCoreType(aService).GetTxtDataLength();
226 
227     return AsCoreType(aService).GetTxtData();
228 }
229 
otSrpServerServiceGetHost(const otSrpServerService * aService)230 const otSrpServerHost *otSrpServerServiceGetHost(const otSrpServerService *aService)
231 {
232     return &AsCoreType(aService).GetHost();
233 }
234 
otSrpServerServiceGetLeaseInfo(const otSrpServerService * aService,otSrpServerLeaseInfo * aLeaseInfo)235 void otSrpServerServiceGetLeaseInfo(const otSrpServerService *aService, otSrpServerLeaseInfo *aLeaseInfo)
236 {
237     AsCoreType(aService).GetLeaseInfo(*aLeaseInfo);
238 }
239 
240 #endif // OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
241