1 /*
2 * Copyright (c) 2016, 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 Commissioner API.
32 */
33
34 #include "openthread-core-config.h"
35
36 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE
37
38 #include <openthread/commissioner.h>
39
40 #include "common/as_core_type.hpp"
41 #include "common/locator_getters.hpp"
42
43 using namespace ot;
44
otCommissionerStart(otInstance * aInstance,otCommissionerStateCallback aStateCallback,otCommissionerJoinerCallback aJoinerCallback,void * aCallbackContext)45 otError otCommissionerStart(otInstance * aInstance,
46 otCommissionerStateCallback aStateCallback,
47 otCommissionerJoinerCallback aJoinerCallback,
48 void * aCallbackContext)
49 {
50 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().Start(aStateCallback, aJoinerCallback, aCallbackContext);
51 }
52
otCommissionerGetId(otInstance * aInstance)53 const char *otCommissionerGetId(otInstance *aInstance)
54 {
55 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().GetId();
56 }
57
otCommissionerSetId(otInstance * aInstance,const char * aId)58 otError otCommissionerSetId(otInstance *aInstance, const char *aId)
59 {
60 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().SetId(aId);
61 }
62
otCommissionerStop(otInstance * aInstance)63 otError otCommissionerStop(otInstance *aInstance)
64 {
65 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().Stop();
66 }
67
otCommissionerAddJoiner(otInstance * aInstance,const otExtAddress * aEui64,const char * aPskd,uint32_t aTimeout)68 otError otCommissionerAddJoiner(otInstance *aInstance, const otExtAddress *aEui64, const char *aPskd, uint32_t aTimeout)
69 {
70 Error error;
71 MeshCoP::Commissioner &commissioner = AsCoreType(aInstance).Get<MeshCoP::Commissioner>();
72
73 if (aEui64 == nullptr)
74 {
75 error = commissioner.AddJoinerAny(aPskd, aTimeout);
76 ExitNow();
77 }
78
79 error = commissioner.AddJoiner(AsCoreType(aEui64), aPskd, aTimeout);
80
81 exit:
82 return error;
83 }
84
otCommissionerAddJoinerWithDiscerner(otInstance * aInstance,const otJoinerDiscerner * aDiscerner,const char * aPskd,uint32_t aTimeout)85 otError otCommissionerAddJoinerWithDiscerner(otInstance * aInstance,
86 const otJoinerDiscerner *aDiscerner,
87 const char * aPskd,
88 uint32_t aTimeout)
89 {
90 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().AddJoiner(AsCoreType(aDiscerner), aPskd, aTimeout);
91 }
92
otCommissionerGetNextJoinerInfo(otInstance * aInstance,uint16_t * aIterator,otJoinerInfo * aJoiner)93 otError otCommissionerGetNextJoinerInfo(otInstance *aInstance, uint16_t *aIterator, otJoinerInfo *aJoiner)
94 {
95 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().GetNextJoinerInfo(*aIterator, *aJoiner);
96 }
97
otCommissionerRemoveJoiner(otInstance * aInstance,const otExtAddress * aEui64)98 otError otCommissionerRemoveJoiner(otInstance *aInstance, const otExtAddress *aEui64)
99 {
100 Error error;
101 MeshCoP::Commissioner &commissioner = AsCoreType(aInstance).Get<MeshCoP::Commissioner>();
102
103 if (aEui64 == nullptr)
104 {
105 error = commissioner.RemoveJoinerAny(/* aTimeout */ 0);
106 ExitNow();
107 }
108
109 error = commissioner.RemoveJoiner(AsCoreType(aEui64), /* aTimeout */ 0);
110
111 exit:
112 return error;
113 }
114
otCommissionerRemoveJoinerWithDiscerner(otInstance * aInstance,const otJoinerDiscerner * aDiscerner)115 otError otCommissionerRemoveJoinerWithDiscerner(otInstance *aInstance, const otJoinerDiscerner *aDiscerner)
116 {
117 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().RemoveJoiner(AsCoreType(aDiscerner), 0);
118 }
119
otCommissionerSetProvisioningUrl(otInstance * aInstance,const char * aProvisioningUrl)120 otError otCommissionerSetProvisioningUrl(otInstance *aInstance, const char *aProvisioningUrl)
121 {
122 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().SetProvisioningUrl(aProvisioningUrl);
123 }
124
otCommissionerGetProvisioningUrl(otInstance * aInstance)125 const char *otCommissionerGetProvisioningUrl(otInstance *aInstance)
126 {
127 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().GetProvisioningUrl();
128 }
129
otCommissionerAnnounceBegin(otInstance * aInstance,uint32_t aChannelMask,uint8_t aCount,uint16_t aPeriod,const otIp6Address * aAddress)130 otError otCommissionerAnnounceBegin(otInstance * aInstance,
131 uint32_t aChannelMask,
132 uint8_t aCount,
133 uint16_t aPeriod,
134 const otIp6Address *aAddress)
135 {
136 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().GetAnnounceBeginClient().SendRequest(
137 aChannelMask, aCount, aPeriod, AsCoreType(aAddress));
138 }
139
otCommissionerEnergyScan(otInstance * aInstance,uint32_t aChannelMask,uint8_t aCount,uint16_t aPeriod,uint16_t aScanDuration,const otIp6Address * aAddress,otCommissionerEnergyReportCallback aCallback,void * aContext)140 otError otCommissionerEnergyScan(otInstance * aInstance,
141 uint32_t aChannelMask,
142 uint8_t aCount,
143 uint16_t aPeriod,
144 uint16_t aScanDuration,
145 const otIp6Address * aAddress,
146 otCommissionerEnergyReportCallback aCallback,
147 void * aContext)
148 {
149 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().GetEnergyScanClient().SendQuery(
150 aChannelMask, aCount, aPeriod, aScanDuration, AsCoreType(aAddress), aCallback, aContext);
151 }
152
otCommissionerPanIdQuery(otInstance * aInstance,uint16_t aPanId,uint32_t aChannelMask,const otIp6Address * aAddress,otCommissionerPanIdConflictCallback aCallback,void * aContext)153 otError otCommissionerPanIdQuery(otInstance * aInstance,
154 uint16_t aPanId,
155 uint32_t aChannelMask,
156 const otIp6Address * aAddress,
157 otCommissionerPanIdConflictCallback aCallback,
158 void * aContext)
159 {
160 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().GetPanIdQueryClient().SendQuery(
161 aPanId, aChannelMask, AsCoreType(aAddress), aCallback, aContext);
162 }
163
otCommissionerSendMgmtGet(otInstance * aInstance,const uint8_t * aTlvs,uint8_t aLength)164 otError otCommissionerSendMgmtGet(otInstance *aInstance, const uint8_t *aTlvs, uint8_t aLength)
165 {
166 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().SendMgmtCommissionerGetRequest(aTlvs, aLength);
167 }
168
otCommissionerSendMgmtSet(otInstance * aInstance,const otCommissioningDataset * aDataset,const uint8_t * aTlvs,uint8_t aLength)169 otError otCommissionerSendMgmtSet(otInstance * aInstance,
170 const otCommissioningDataset *aDataset,
171 const uint8_t * aTlvs,
172 uint8_t aLength)
173 {
174 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().SendMgmtCommissionerSetRequest(AsCoreType(aDataset),
175 aTlvs, aLength);
176 }
177
otCommissionerGetSessionId(otInstance * aInstance)178 uint16_t otCommissionerGetSessionId(otInstance *aInstance)
179 {
180 return AsCoreType(aInstance).Get<MeshCoP::Commissioner>().GetSessionId();
181 }
182
otCommissionerGetState(otInstance * aInstance)183 otCommissionerState otCommissionerGetState(otInstance *aInstance)
184 {
185 return MapEnum(AsCoreType(aInstance).Get<MeshCoP::Commissioner>().GetState());
186 }
187
188 #endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE
189