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/as_core_type.hpp"
41 #include "common/locator_getters.hpp"
42
43 using namespace ot;
44
otSrpClientStart(otInstance * aInstance,const otSockAddr * aServerSockAddr)45 otError otSrpClientStart(otInstance *aInstance, const otSockAddr *aServerSockAddr)
46 {
47 return AsCoreType(aInstance).Get<Srp::Client>().Start(AsCoreType(aServerSockAddr));
48 }
49
otSrpClientStop(otInstance * aInstance)50 void otSrpClientStop(otInstance *aInstance)
51 {
52 return AsCoreType(aInstance).Get<Srp::Client>().Stop();
53 }
54
otSrpClientIsRunning(otInstance * aInstance)55 bool otSrpClientIsRunning(otInstance *aInstance)
56 {
57 return AsCoreType(aInstance).Get<Srp::Client>().IsRunning();
58 }
59
otSrpClientGetServerAddress(otInstance * aInstance)60 const otSockAddr *otSrpClientGetServerAddress(otInstance *aInstance)
61 {
62 return &AsCoreType(aInstance).Get<Srp::Client>().GetServerAddress();
63 }
64
otSrpClientSetCallback(otInstance * aInstance,otSrpClientCallback aCallback,void * aContext)65 void otSrpClientSetCallback(otInstance *aInstance, otSrpClientCallback aCallback, void *aContext)
66 {
67 AsCoreType(aInstance).Get<Srp::Client>().SetCallback(aCallback, aContext);
68 }
69
70 #if OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE
otSrpClientEnableAutoStartMode(otInstance * aInstance,otSrpClientAutoStartCallback aCallback,void * aContext)71 void otSrpClientEnableAutoStartMode(otInstance *aInstance, otSrpClientAutoStartCallback aCallback, void *aContext)
72 {
73 AsCoreType(aInstance).Get<Srp::Client>().EnableAutoStartMode(aCallback, aContext);
74 }
75
otSrpClientDisableAutoStartMode(otInstance * aInstance)76 void otSrpClientDisableAutoStartMode(otInstance *aInstance)
77 {
78 AsCoreType(aInstance).Get<Srp::Client>().DisableAutoStartMode();
79 }
80
otSrpClientIsAutoStartModeEnabled(otInstance * aInstance)81 bool otSrpClientIsAutoStartModeEnabled(otInstance *aInstance)
82 {
83 return AsCoreType(aInstance).Get<Srp::Client>().IsAutoStartModeEnabled();
84 }
85 #endif // OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE
86
otSrpClientGetTtl(otInstance * aInstance)87 uint32_t otSrpClientGetTtl(otInstance *aInstance)
88 {
89 return AsCoreType(aInstance).Get<Srp::Client>().GetTtl();
90 }
91
otSrpClientSetTtl(otInstance * aInstance,uint32_t aTtl)92 void otSrpClientSetTtl(otInstance *aInstance, uint32_t aTtl)
93 {
94 return AsCoreType(aInstance).Get<Srp::Client>().SetTtl(aTtl);
95 }
96
otSrpClientGetLeaseInterval(otInstance * aInstance)97 uint32_t otSrpClientGetLeaseInterval(otInstance *aInstance)
98 {
99 return AsCoreType(aInstance).Get<Srp::Client>().GetLeaseInterval();
100 }
101
otSrpClientSetLeaseInterval(otInstance * aInstance,uint32_t aInterval)102 void otSrpClientSetLeaseInterval(otInstance *aInstance, uint32_t aInterval)
103 {
104 return AsCoreType(aInstance).Get<Srp::Client>().SetLeaseInterval(aInterval);
105 }
106
otSrpClientGetKeyLeaseInterval(otInstance * aInstance)107 uint32_t otSrpClientGetKeyLeaseInterval(otInstance *aInstance)
108 {
109 return AsCoreType(aInstance).Get<Srp::Client>().GetKeyLeaseInterval();
110 }
111
otSrpClientSetKeyLeaseInterval(otInstance * aInstance,uint32_t aInterval)112 void otSrpClientSetKeyLeaseInterval(otInstance *aInstance, uint32_t aInterval)
113 {
114 return AsCoreType(aInstance).Get<Srp::Client>().SetKeyLeaseInterval(aInterval);
115 }
116
otSrpClientGetHostInfo(otInstance * aInstance)117 const otSrpClientHostInfo *otSrpClientGetHostInfo(otInstance *aInstance)
118 {
119 return &AsCoreType(aInstance).Get<Srp::Client>().GetHostInfo();
120 }
121
otSrpClientSetHostName(otInstance * aInstance,const char * aName)122 otError otSrpClientSetHostName(otInstance *aInstance, const char *aName)
123 {
124 return AsCoreType(aInstance).Get<Srp::Client>().SetHostName(aName);
125 }
126
otSrpClientEnableAutoHostAddress(otInstance * aInstance)127 otError otSrpClientEnableAutoHostAddress(otInstance *aInstance)
128 {
129 return AsCoreType(aInstance).Get<Srp::Client>().EnableAutoHostAddress();
130 }
131
otSrpClientSetHostAddresses(otInstance * aInstance,const otIp6Address * aIp6Addresses,uint8_t aNumAddresses)132 otError otSrpClientSetHostAddresses(otInstance *aInstance, const otIp6Address *aIp6Addresses, uint8_t aNumAddresses)
133 {
134 return AsCoreType(aInstance).Get<Srp::Client>().SetHostAddresses(AsCoreTypePtr(aIp6Addresses), aNumAddresses);
135 }
136
otSrpClientAddService(otInstance * aInstance,otSrpClientService * aService)137 otError otSrpClientAddService(otInstance *aInstance, otSrpClientService *aService)
138 {
139 return AsCoreType(aInstance).Get<Srp::Client>().AddService(AsCoreType(aService));
140 }
141
otSrpClientRemoveService(otInstance * aInstance,otSrpClientService * aService)142 otError otSrpClientRemoveService(otInstance *aInstance, otSrpClientService *aService)
143 {
144 return AsCoreType(aInstance).Get<Srp::Client>().RemoveService(AsCoreType(aService));
145 }
146
otSrpClientClearService(otInstance * aInstance,otSrpClientService * aService)147 otError otSrpClientClearService(otInstance *aInstance, otSrpClientService *aService)
148 {
149 return AsCoreType(aInstance).Get<Srp::Client>().ClearService(AsCoreType(aService));
150 }
151
otSrpClientGetServices(otInstance * aInstance)152 const otSrpClientService *otSrpClientGetServices(otInstance *aInstance)
153 {
154 return AsCoreType(aInstance).Get<Srp::Client>().GetServices().GetHead();
155 }
156
otSrpClientRemoveHostAndServices(otInstance * aInstance,bool aRemoveKeyLease,bool aSendUnregToServer)157 otError otSrpClientRemoveHostAndServices(otInstance *aInstance, bool aRemoveKeyLease, bool aSendUnregToServer)
158 {
159 return AsCoreType(aInstance).Get<Srp::Client>().RemoveHostAndServices(aRemoveKeyLease, aSendUnregToServer);
160 }
161
otSrpClientClearHostAndServices(otInstance * aInstance)162 void otSrpClientClearHostAndServices(otInstance *aInstance)
163 {
164 AsCoreType(aInstance).Get<Srp::Client>().ClearHostAndServices();
165 }
166
167 #if OPENTHREAD_CONFIG_SRP_CLIENT_DOMAIN_NAME_API_ENABLE
otSrpClientGetDomainName(otInstance * aInstance)168 const char *otSrpClientGetDomainName(otInstance *aInstance)
169 {
170 return AsCoreType(aInstance).Get<Srp::Client>().GetDomainName();
171 }
172
otSrpClientSetDomainName(otInstance * aInstance,const char * aDomainName)173 otError otSrpClientSetDomainName(otInstance *aInstance, const char *aDomainName)
174 {
175 return AsCoreType(aInstance).Get<Srp::Client>().SetDomainName(aDomainName);
176 }
177 #endif // OPENTHREAD_CONFIG_SRP_CLIENT_DOMAIN_NAME_API_ENABLE
178
otSrpClientItemStateToString(otSrpClientItemState aItemState)179 const char *otSrpClientItemStateToString(otSrpClientItemState aItemState)
180 {
181 OT_ASSERT(aItemState <= OT_SRP_CLIENT_ITEM_STATE_REMOVED);
182
183 return Srp::Client::ItemStateToString(MapEnum(aItemState));
184 }
185
186 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
otSrpClientSetServiceKeyRecordEnabled(otInstance * aInstance,bool aEnabled)187 void otSrpClientSetServiceKeyRecordEnabled(otInstance *aInstance, bool aEnabled)
188 {
189 AsCoreType(aInstance).Get<Srp::Client>().SetServiceKeyRecordEnabled(aEnabled);
190 }
191
otSrpClientIsServiceKeyRecordEnabled(otInstance * aInstance)192 bool otSrpClientIsServiceKeyRecordEnabled(otInstance *aInstance)
193 {
194 return AsCoreType(aInstance).Get<Srp::Client>().IsServiceKeyRecordEnabled();
195 }
196 #endif
197
198 #endif // OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE
199