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 
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) { return AsCoreType(aHost).GetKeyLease(); }
153 
otSrpServerHostGetNextService(const otSrpServerHost * aHost,const otSrpServerService * aService)154 const otSrpServerService *otSrpServerHostGetNextService(const otSrpServerHost    *aHost,
155                                                         const otSrpServerService *aService)
156 {
157     return AsCoreType(aHost).FindNextService(AsCoreTypePtr(aService), Srp::Server::kFlagsBaseTypeServiceOnly);
158 }
159 
otSrpServerHostFindNextService(const otSrpServerHost * aHost,const otSrpServerService * aPrevService,otSrpServerServiceFlags aFlags,const char * aServiceName,const char * aInstanceName)160 const otSrpServerService *otSrpServerHostFindNextService(const otSrpServerHost    *aHost,
161                                                          const otSrpServerService *aPrevService,
162                                                          otSrpServerServiceFlags   aFlags,
163                                                          const char               *aServiceName,
164                                                          const char               *aInstanceName)
165 {
166     return AsCoreType(aHost).FindNextService(AsCoreTypePtr(aPrevService), aFlags, aServiceName, aInstanceName);
167 }
168 
otSrpServerServiceIsDeleted(const otSrpServerService * aService)169 bool otSrpServerServiceIsDeleted(const otSrpServerService *aService) { return AsCoreType(aService).IsDeleted(); }
170 
otSrpServerServiceIsSubType(const otSrpServerService * aService)171 bool otSrpServerServiceIsSubType(const otSrpServerService *aService) { return AsCoreType(aService).IsSubType(); }
172 
otSrpServerServiceGetFullName(const otSrpServerService * aService)173 const char *otSrpServerServiceGetFullName(const otSrpServerService *aService)
174 {
175     return AsCoreType(aService).GetInstanceName();
176 }
177 
otSrpServerServiceGetInstanceName(const otSrpServerService * aService)178 const char *otSrpServerServiceGetInstanceName(const otSrpServerService *aService)
179 {
180     return AsCoreType(aService).GetInstanceName();
181 }
182 
otSrpServerServiceGetServiceName(const otSrpServerService * aService)183 const char *otSrpServerServiceGetServiceName(const otSrpServerService *aService)
184 {
185     return AsCoreType(aService).GetServiceName();
186 }
187 
otSrpServerServiceGetServiceSubTypeLabel(const otSrpServerService * aService,char * aLabel,uint8_t aMaxSize)188 otError otSrpServerServiceGetServiceSubTypeLabel(const otSrpServerService *aService, char *aLabel, uint8_t aMaxSize)
189 {
190     return AsCoreType(aService).GetServiceSubTypeLabel(aLabel, aMaxSize);
191 }
192 
otSrpServerServiceGetPort(const otSrpServerService * aService)193 uint16_t otSrpServerServiceGetPort(const otSrpServerService *aService) { return AsCoreType(aService).GetPort(); }
194 
otSrpServerServiceGetWeight(const otSrpServerService * aService)195 uint16_t otSrpServerServiceGetWeight(const otSrpServerService *aService) { return AsCoreType(aService).GetWeight(); }
196 
otSrpServerServiceGetPriority(const otSrpServerService * aService)197 uint16_t otSrpServerServiceGetPriority(const otSrpServerService *aService)
198 {
199     return AsCoreType(aService).GetPriority();
200 }
201 
otSrpServerServiceGetTtl(const otSrpServerService * aService)202 uint32_t otSrpServerServiceGetTtl(const otSrpServerService *aService) { return AsCoreType(aService).GetTtl(); }
203 
otSrpServerServiceGetTxtData(const otSrpServerService * aService,uint16_t * aDataLength)204 const uint8_t *otSrpServerServiceGetTxtData(const otSrpServerService *aService, uint16_t *aDataLength)
205 {
206     *aDataLength = AsCoreType(aService).GetTxtDataLength();
207 
208     return AsCoreType(aService).GetTxtData();
209 }
210 
otSrpServerServiceGetHost(const otSrpServerService * aService)211 const otSrpServerHost *otSrpServerServiceGetHost(const otSrpServerService *aService)
212 {
213     return &AsCoreType(aService).GetHost();
214 }
215 
otSrpServerServiceGetLeaseInfo(const otSrpServerService * aService,otSrpServerLeaseInfo * aLeaseInfo)216 void otSrpServerServiceGetLeaseInfo(const otSrpServerService *aService, otSrpServerLeaseInfo *aLeaseInfo)
217 {
218     AsCoreType(aService).GetLeaseInfo(*aLeaseInfo);
219 }
220 
221 #endif // OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
222