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