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 implements the OpenThread SRP client APIs.
32 */
33
34 #include "openthread-core-config.h"
35
36 #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE
37
38 #include <openthread/srp_client.h>
39
40 #include "common/instance.hpp"
41 #include "common/locator_getters.hpp"
42 #include "net/srp_client.hpp"
43
44 using namespace ot;
45
otSrpClientStart(otInstance * aInstance,const otSockAddr * aServerSockAddr)46 otError otSrpClientStart(otInstance *aInstance, const otSockAddr *aServerSockAddr)
47 {
48 Instance &instance = *static_cast<Instance *>(aInstance);
49
50 return instance.Get<Srp::Client>().Start(*static_cast<const Ip6::SockAddr *>(aServerSockAddr));
51 }
52
otSrpClientStop(otInstance * aInstance)53 void otSrpClientStop(otInstance *aInstance)
54 {
55 Instance &instance = *static_cast<Instance *>(aInstance);
56
57 return instance.Get<Srp::Client>().Stop();
58 }
59
otSrpClientIsRunning(otInstance * aInstance)60 bool otSrpClientIsRunning(otInstance *aInstance)
61 {
62 Instance &instance = *static_cast<Instance *>(aInstance);
63
64 return instance.Get<Srp::Client>().IsRunning();
65 }
66
otSrpClientGetServerAddress(otInstance * aInstance)67 const otSockAddr *otSrpClientGetServerAddress(otInstance *aInstance)
68 {
69 Instance &instance = *static_cast<Instance *>(aInstance);
70
71 return &instance.Get<Srp::Client>().GetServerAddress();
72 }
73
otSrpClientSetCallback(otInstance * aInstance,otSrpClientCallback aCallback,void * aContext)74 void otSrpClientSetCallback(otInstance *aInstance, otSrpClientCallback aCallback, void *aContext)
75 {
76 Instance &instance = *static_cast<Instance *>(aInstance);
77
78 instance.Get<Srp::Client>().SetCallback(aCallback, aContext);
79 }
80
81 #if OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE
otSrpClientEnableAutoStartMode(otInstance * aInstance,otSrpClientAutoStartCallback aCallback,void * aContext)82 void otSrpClientEnableAutoStartMode(otInstance *aInstance, otSrpClientAutoStartCallback aCallback, void *aContext)
83 {
84 Instance &instance = *static_cast<Instance *>(aInstance);
85
86 instance.Get<Srp::Client>().EnableAutoStartMode(aCallback, aContext);
87 }
88
otSrpClientDisableAutoStartMode(otInstance * aInstance)89 void otSrpClientDisableAutoStartMode(otInstance *aInstance)
90 {
91 Instance &instance = *static_cast<Instance *>(aInstance);
92
93 instance.Get<Srp::Client>().DisableAutoStartMode();
94 }
95
otSrpClientIsAutoStartModeEnabled(otInstance * aInstance)96 bool otSrpClientIsAutoStartModeEnabled(otInstance *aInstance)
97 {
98 Instance &instance = *static_cast<Instance *>(aInstance);
99
100 return instance.Get<Srp::Client>().IsAutoStartModeEnabled();
101 }
102 #endif // OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE
103
otSrpClientGetLeaseInterval(otInstance * aInstance)104 uint32_t otSrpClientGetLeaseInterval(otInstance *aInstance)
105 {
106 Instance &instance = *static_cast<Instance *>(aInstance);
107
108 return instance.Get<Srp::Client>().GetLeaseInterval();
109 }
110
otSrpClientSetLeaseInterval(otInstance * aInstance,uint32_t aInterval)111 void otSrpClientSetLeaseInterval(otInstance *aInstance, uint32_t aInterval)
112 {
113 Instance &instance = *static_cast<Instance *>(aInstance);
114
115 return instance.Get<Srp::Client>().SetLeaseInterval(aInterval);
116 }
117
otSrpClientGetKeyLeaseInterval(otInstance * aInstance)118 uint32_t otSrpClientGetKeyLeaseInterval(otInstance *aInstance)
119 {
120 Instance &instance = *static_cast<Instance *>(aInstance);
121
122 return instance.Get<Srp::Client>().GetKeyLeaseInterval();
123 }
124
otSrpClientSetKeyLeaseInterval(otInstance * aInstance,uint32_t aInterval)125 void otSrpClientSetKeyLeaseInterval(otInstance *aInstance, uint32_t aInterval)
126 {
127 Instance &instance = *static_cast<Instance *>(aInstance);
128
129 return instance.Get<Srp::Client>().SetKeyLeaseInterval(aInterval);
130 }
131
otSrpClientGetHostInfo(otInstance * aInstance)132 const otSrpClientHostInfo *otSrpClientGetHostInfo(otInstance *aInstance)
133 {
134 Instance &instance = *static_cast<Instance *>(aInstance);
135
136 return &instance.Get<Srp::Client>().GetHostInfo();
137 }
138
otSrpClientSetHostName(otInstance * aInstance,const char * aName)139 otError otSrpClientSetHostName(otInstance *aInstance, const char *aName)
140 {
141 Instance &instance = *static_cast<Instance *>(aInstance);
142
143 return instance.Get<Srp::Client>().SetHostName(aName);
144 }
145
otSrpClientSetHostAddresses(otInstance * aInstance,const otIp6Address * aIp6Addresses,uint8_t aNumAddresses)146 otError otSrpClientSetHostAddresses(otInstance *aInstance, const otIp6Address *aIp6Addresses, uint8_t aNumAddresses)
147 {
148 Instance &instance = *static_cast<Instance *>(aInstance);
149
150 return instance.Get<Srp::Client>().SetHostAddresses(static_cast<const Ip6::Address *>(aIp6Addresses),
151 aNumAddresses);
152 }
153
otSrpClientAddService(otInstance * aInstance,otSrpClientService * aService)154 otError otSrpClientAddService(otInstance *aInstance, otSrpClientService *aService)
155 {
156 Instance &instance = *static_cast<Instance *>(aInstance);
157
158 return instance.Get<Srp::Client>().AddService(*static_cast<Srp::Client::Service *>(aService));
159 }
160
otSrpClientRemoveService(otInstance * aInstance,otSrpClientService * aService)161 otError otSrpClientRemoveService(otInstance *aInstance, otSrpClientService *aService)
162 {
163 Instance &instance = *static_cast<Instance *>(aInstance);
164
165 return instance.Get<Srp::Client>().RemoveService(*static_cast<Srp::Client::Service *>(aService));
166 }
167
otSrpClientClearService(otInstance * aInstance,otSrpClientService * aService)168 otError otSrpClientClearService(otInstance *aInstance, otSrpClientService *aService)
169 {
170 Instance &instance = *static_cast<Instance *>(aInstance);
171
172 return instance.Get<Srp::Client>().ClearService(*static_cast<Srp::Client::Service *>(aService));
173 }
174
otSrpClientGetServices(otInstance * aInstance)175 const otSrpClientService *otSrpClientGetServices(otInstance *aInstance)
176 {
177 Instance &instance = *static_cast<Instance *>(aInstance);
178
179 return instance.Get<Srp::Client>().GetServices().GetHead();
180 }
181
otSrpClientRemoveHostAndServices(otInstance * aInstance,bool aRemoveKeyLease,bool aSendUnregToServer)182 otError otSrpClientRemoveHostAndServices(otInstance *aInstance, bool aRemoveKeyLease, bool aSendUnregToServer)
183 {
184 Instance &instance = *static_cast<Instance *>(aInstance);
185
186 return instance.Get<Srp::Client>().RemoveHostAndServices(aRemoveKeyLease, aSendUnregToServer);
187 }
188
otSrpClientClearHostAndServices(otInstance * aInstance)189 void otSrpClientClearHostAndServices(otInstance *aInstance)
190 {
191 Instance &instance = *static_cast<Instance *>(aInstance);
192
193 instance.Get<Srp::Client>().ClearHostAndServices();
194 }
195
196 #if OPENTHREAD_CONFIG_SRP_CLIENT_DOMAIN_NAME_API_ENABLE
otSrpClientGetDomainName(otInstance * aInstance)197 const char *otSrpClientGetDomainName(otInstance *aInstance)
198 {
199 Instance &instance = *static_cast<Instance *>(aInstance);
200
201 return instance.Get<Srp::Client>().GetDomainName();
202 }
203
otSrpClientSetDomainName(otInstance * aInstance,const char * aDomainName)204 otError otSrpClientSetDomainName(otInstance *aInstance, const char *aDomainName)
205 {
206 Instance &instance = *static_cast<Instance *>(aInstance);
207
208 return instance.Get<Srp::Client>().SetDomainName(aDomainName);
209 }
210 #endif // OPENTHREAD_CONFIG_SRP_CLIENT_DOMAIN_NAME_API_ENABLE
211
otSrpClientItemStateToString(otSrpClientItemState aItemState)212 const char *otSrpClientItemStateToString(otSrpClientItemState aItemState)
213 {
214 OT_ASSERT(aItemState <= OT_SRP_CLIENT_ITEM_STATE_REMOVED);
215
216 return Srp::Client::ItemStateToString(static_cast<Srp::Client::ItemState>(aItemState));
217 }
218
219 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
otSrpClientSetServiceKeyRecordEnabled(otInstance * aInstance,bool aEnabled)220 void otSrpClientSetServiceKeyRecordEnabled(otInstance *aInstance, bool aEnabled)
221 {
222 Instance &instance = *static_cast<Instance *>(aInstance);
223
224 instance.Get<Srp::Client>().SetServiceKeyRecordEnabled(aEnabled);
225 }
226
otSrpClientIsServiceKeyRecordEnabled(otInstance * aInstance)227 bool otSrpClientIsServiceKeyRecordEnabled(otInstance *aInstance)
228 {
229 Instance &instance = *static_cast<Instance *>(aInstance);
230
231 return instance.Get<Srp::Client>().IsServiceKeyRecordEnabled();
232 }
233 #endif
234
235 #endif // OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE
236