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