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/instance.hpp"
41 #include "common/locator_getters.hpp"
42 
43 using namespace ot;
44 
otSrpServerGetDomain(otInstance * aInstance)45 const char *otSrpServerGetDomain(otInstance *aInstance)
46 {
47     Instance &instance = *static_cast<Instance *>(aInstance);
48 
49     return instance.Get<Srp::Server>().GetDomain();
50 }
51 
otSrpServerSetDomain(otInstance * aInstance,const char * aDomain)52 otError otSrpServerSetDomain(otInstance *aInstance, const char *aDomain)
53 {
54     Instance &instance = *static_cast<Instance *>(aInstance);
55 
56     return instance.Get<Srp::Server>().SetDomain(aDomain);
57 }
58 
otSrpServerGetState(otInstance * aInstance)59 otSrpServerState otSrpServerGetState(otInstance *aInstance)
60 {
61     Instance &instance = *static_cast<Instance *>(aInstance);
62 
63     return static_cast<otSrpServerState>(instance.Get<Srp::Server>().GetState());
64 }
65 
otSrpServerSetEnabled(otInstance * aInstance,bool aEnabled)66 void otSrpServerSetEnabled(otInstance *aInstance, bool aEnabled)
67 {
68     Instance &instance = *static_cast<Instance *>(aInstance);
69 
70     instance.Get<Srp::Server>().SetEnabled(aEnabled);
71 }
72 
otSrpServerGetLeaseConfig(otInstance * aInstance,otSrpServerLeaseConfig * aLeaseConfig)73 void otSrpServerGetLeaseConfig(otInstance *aInstance, otSrpServerLeaseConfig *aLeaseConfig)
74 {
75     Instance &instance = *static_cast<Instance *>(aInstance);
76 
77     instance.Get<Srp::Server>().GetLeaseConfig(static_cast<Srp::Server::LeaseConfig &>(*aLeaseConfig));
78 }
79 
otSrpServerSetLeaseConfig(otInstance * aInstance,const otSrpServerLeaseConfig * aLeaseConfig)80 otError otSrpServerSetLeaseConfig(otInstance *aInstance, const otSrpServerLeaseConfig *aLeaseConfig)
81 {
82     Instance &instance = *static_cast<Instance *>(aInstance);
83 
84     return instance.Get<Srp::Server>().SetLeaseConfig(static_cast<const Srp::Server::LeaseConfig &>(*aLeaseConfig));
85 }
86 
otSrpServerSetServiceUpdateHandler(otInstance * aInstance,otSrpServerServiceUpdateHandler aServiceHandler,void * aContext)87 void otSrpServerSetServiceUpdateHandler(otInstance *                    aInstance,
88                                         otSrpServerServiceUpdateHandler aServiceHandler,
89                                         void *                          aContext)
90 {
91     Instance &instance = *static_cast<Instance *>(aInstance);
92 
93     instance.Get<Srp::Server>().SetServiceHandler(aServiceHandler, aContext);
94 }
95 
otSrpServerHandleServiceUpdateResult(otInstance * aInstance,otSrpServerServiceUpdateId aId,otError aError)96 void otSrpServerHandleServiceUpdateResult(otInstance *aInstance, otSrpServerServiceUpdateId aId, otError aError)
97 {
98     Instance &instance = *static_cast<Instance *>(aInstance);
99 
100     instance.Get<Srp::Server>().HandleServiceUpdateResult(aId, aError);
101 }
102 
otSrpServerGetNextHost(otInstance * aInstance,const otSrpServerHost * aHost)103 const otSrpServerHost *otSrpServerGetNextHost(otInstance *aInstance, const otSrpServerHost *aHost)
104 {
105     Instance &instance = *static_cast<Instance *>(aInstance);
106 
107     return instance.Get<Srp::Server>().GetNextHost(static_cast<const Srp::Server::Host *>(aHost));
108 }
109 
otSrpServerHostIsDeleted(const otSrpServerHost * aHost)110 bool otSrpServerHostIsDeleted(const otSrpServerHost *aHost)
111 {
112     return static_cast<const Srp::Server::Host *>(aHost)->IsDeleted();
113 }
114 
otSrpServerHostGetFullName(const otSrpServerHost * aHost)115 const char *otSrpServerHostGetFullName(const otSrpServerHost *aHost)
116 {
117     return static_cast<const Srp::Server::Host *>(aHost)->GetFullName();
118 }
119 
otSrpServerHostGetAddresses(const otSrpServerHost * aHost,uint8_t * aAddressesNum)120 const otIp6Address *otSrpServerHostGetAddresses(const otSrpServerHost *aHost, uint8_t *aAddressesNum)
121 {
122     auto host = static_cast<const Srp::Server::Host *>(aHost);
123 
124     return host->GetAddresses(*aAddressesNum);
125 }
126 
otSrpServerHostGetNextService(const otSrpServerHost * aHost,const otSrpServerService * aService)127 const otSrpServerService *otSrpServerHostGetNextService(const otSrpServerHost *   aHost,
128                                                         const otSrpServerService *aService)
129 {
130     auto host = static_cast<const Srp::Server::Host *>(aHost);
131 
132     return host->FindNextService(static_cast<const Srp::Server::Service *>(aService),
133                                  Srp::Server::kFlagsBaseTypeServiceOnly);
134 }
135 
otSrpServerHostFindNextService(const otSrpServerHost * aHost,const otSrpServerService * aPrevService,otSrpServerServiceFlags aFlags,const char * aServiceName,const char * aInstanceName)136 const otSrpServerService *otSrpServerHostFindNextService(const otSrpServerHost *   aHost,
137                                                          const otSrpServerService *aPrevService,
138                                                          otSrpServerServiceFlags   aFlags,
139                                                          const char *              aServiceName,
140                                                          const char *              aInstanceName)
141 {
142     auto host = static_cast<const Srp::Server::Host *>(aHost);
143 
144     return host->FindNextService(static_cast<const Srp::Server::Service *>(aPrevService), aFlags, aServiceName,
145                                  aInstanceName);
146 }
147 
otSrpServerServiceIsDeleted(const otSrpServerService * aService)148 bool otSrpServerServiceIsDeleted(const otSrpServerService *aService)
149 {
150     return static_cast<const Srp::Server::Service *>(aService)->IsDeleted();
151 }
152 
otSrpServerServiceIsSubType(const otSrpServerService * aService)153 bool otSrpServerServiceIsSubType(const otSrpServerService *aService)
154 {
155     return static_cast<const Srp::Server::Service *>(aService)->IsSubType();
156 }
157 
otSrpServerServiceGetFullName(const otSrpServerService * aService)158 const char *otSrpServerServiceGetFullName(const otSrpServerService *aService)
159 {
160     return static_cast<const Srp::Server::Service *>(aService)->GetInstanceName();
161 }
162 
otSrpServerServiceGetInstanceName(const otSrpServerService * aService)163 const char *otSrpServerServiceGetInstanceName(const otSrpServerService *aService)
164 {
165     return static_cast<const Srp::Server::Service *>(aService)->GetInstanceName();
166 }
167 
otSrpServerServiceGetServiceName(const otSrpServerService * aService)168 const char *otSrpServerServiceGetServiceName(const otSrpServerService *aService)
169 {
170     return static_cast<const Srp::Server::Service *>(aService)->GetServiceName();
171 }
172 
otSrpServerServiceGetServiceSubTypeLabel(const otSrpServerService * aService,char * aLabel,uint8_t aMaxSize)173 otError otSrpServerServiceGetServiceSubTypeLabel(const otSrpServerService *aService, char *aLabel, uint8_t aMaxSize)
174 {
175     return static_cast<const Srp::Server::Service *>(aService)->GetServiceSubTypeLabel(aLabel, aMaxSize);
176 }
177 
otSrpServerServiceGetPort(const otSrpServerService * aService)178 uint16_t otSrpServerServiceGetPort(const otSrpServerService *aService)
179 {
180     return static_cast<const Srp::Server::Service *>(aService)->GetPort();
181 }
182 
otSrpServerServiceGetWeight(const otSrpServerService * aService)183 uint16_t otSrpServerServiceGetWeight(const otSrpServerService *aService)
184 {
185     return static_cast<const Srp::Server::Service *>(aService)->GetWeight();
186 }
187 
otSrpServerServiceGetPriority(const otSrpServerService * aService)188 uint16_t otSrpServerServiceGetPriority(const otSrpServerService *aService)
189 {
190     return static_cast<const Srp::Server::Service *>(aService)->GetPriority();
191 }
192 
otSrpServerServiceGetTxtData(const otSrpServerService * aService,uint16_t * aDataLength)193 const uint8_t *otSrpServerServiceGetTxtData(const otSrpServerService *aService, uint16_t *aDataLength)
194 {
195     const Srp::Server::Service &service = *static_cast<const Srp::Server::Service *>(aService);
196 
197     *aDataLength = service.GetTxtDataLength();
198 
199     return service.GetTxtData();
200 }
201 
otSrpServerServiceGetHost(const otSrpServerService * aService)202 const otSrpServerHost *otSrpServerServiceGetHost(const otSrpServerService *aService)
203 {
204     return &static_cast<const Srp::Server::Service *>(aService)->GetHost();
205 }
206 
207 #endif // OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
208