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/instance.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     Instance &instance = *static_cast<Instance *>(aInstance);
51 
52     return instance.Get<MeshCoP::Commissioner>().Start(aStateCallback, aJoinerCallback, aCallbackContext);
53 }
54 
otCommissionerStop(otInstance * aInstance)55 otError otCommissionerStop(otInstance *aInstance)
56 {
57     Instance &instance = *static_cast<Instance *>(aInstance);
58 
59     return instance.Get<MeshCoP::Commissioner>().Stop(/* aResign */ true);
60 }
61 
otCommissionerAddJoiner(otInstance * aInstance,const otExtAddress * aEui64,const char * aPskd,uint32_t aTimeout)62 otError otCommissionerAddJoiner(otInstance *aInstance, const otExtAddress *aEui64, const char *aPskd, uint32_t aTimeout)
63 {
64     Error                  error;
65     MeshCoP::Commissioner &commissioner = static_cast<Instance *>(aInstance)->Get<MeshCoP::Commissioner>();
66 
67     if (aEui64 == nullptr)
68     {
69         error = commissioner.AddJoinerAny(aPskd, aTimeout);
70         ExitNow();
71     }
72 
73     error = commissioner.AddJoiner(*static_cast<const Mac::ExtAddress *>(aEui64), aPskd, aTimeout);
74 
75 exit:
76     return error;
77 }
78 
otCommissionerAddJoinerWithDiscerner(otInstance * aInstance,const otJoinerDiscerner * aDiscerner,const char * aPskd,uint32_t aTimeout)79 otError otCommissionerAddJoinerWithDiscerner(otInstance *             aInstance,
80                                              const otJoinerDiscerner *aDiscerner,
81                                              const char *             aPskd,
82                                              uint32_t                 aTimeout)
83 {
84     Instance &instance = *static_cast<Instance *>(aInstance);
85 
86     return instance.Get<MeshCoP::Commissioner>().AddJoiner(*static_cast<const MeshCoP::JoinerDiscerner *>(aDiscerner),
87                                                            aPskd, aTimeout);
88 }
89 
otCommissionerGetNextJoinerInfo(otInstance * aInstance,uint16_t * aIterator,otJoinerInfo * aJoiner)90 otError otCommissionerGetNextJoinerInfo(otInstance *aInstance, uint16_t *aIterator, otJoinerInfo *aJoiner)
91 {
92     Instance &instance = *static_cast<Instance *>(aInstance);
93 
94     return instance.Get<MeshCoP::Commissioner>().GetNextJoinerInfo(*aIterator, *aJoiner);
95 }
96 
otCommissionerRemoveJoiner(otInstance * aInstance,const otExtAddress * aEui64)97 otError otCommissionerRemoveJoiner(otInstance *aInstance, const otExtAddress *aEui64)
98 {
99     Error                  error;
100     MeshCoP::Commissioner &commissioner = static_cast<Instance *>(aInstance)->Get<MeshCoP::Commissioner>();
101 
102     if (aEui64 == nullptr)
103     {
104         error = commissioner.RemoveJoinerAny(/* aTimeout */ 0);
105         ExitNow();
106     }
107 
108     error = commissioner.RemoveJoiner(*static_cast<const Mac::ExtAddress *>(aEui64), /* aTimeout */ 0);
109 
110 exit:
111     return error;
112 }
113 
otCommissionerRemoveJoinerWithDiscerner(otInstance * aInstance,const otJoinerDiscerner * aDiscerner)114 otError otCommissionerRemoveJoinerWithDiscerner(otInstance *aInstance, const otJoinerDiscerner *aDiscerner)
115 {
116     Instance &instance = *static_cast<Instance *>(aInstance);
117 
118     return instance.Get<MeshCoP::Commissioner>().RemoveJoiner(
119         *static_cast<const MeshCoP::JoinerDiscerner *>(aDiscerner), 0);
120 }
121 
otCommissionerSetProvisioningUrl(otInstance * aInstance,const char * aProvisioningUrl)122 otError otCommissionerSetProvisioningUrl(otInstance *aInstance, const char *aProvisioningUrl)
123 {
124     Instance &instance = *static_cast<Instance *>(aInstance);
125 
126     return instance.Get<MeshCoP::Commissioner>().SetProvisioningUrl(aProvisioningUrl);
127 }
128 
otCommissionerGetProvisioningUrl(otInstance * aInstance)129 const char *otCommissionerGetProvisioningUrl(otInstance *aInstance)
130 {
131     Instance &instance = *static_cast<Instance *>(aInstance);
132 
133     return instance.Get<MeshCoP::Commissioner>().GetProvisioningUrl();
134 }
135 
otCommissionerAnnounceBegin(otInstance * aInstance,uint32_t aChannelMask,uint8_t aCount,uint16_t aPeriod,const otIp6Address * aAddress)136 otError otCommissionerAnnounceBegin(otInstance *        aInstance,
137                                     uint32_t            aChannelMask,
138                                     uint8_t             aCount,
139                                     uint16_t            aPeriod,
140                                     const otIp6Address *aAddress)
141 {
142     Instance &instance = *static_cast<Instance *>(aInstance);
143 
144     return instance.Get<MeshCoP::Commissioner>().GetAnnounceBeginClient().SendRequest(
145         aChannelMask, aCount, aPeriod, *static_cast<const Ip6::Address *>(aAddress));
146 }
147 
otCommissionerEnergyScan(otInstance * aInstance,uint32_t aChannelMask,uint8_t aCount,uint16_t aPeriod,uint16_t aScanDuration,const otIp6Address * aAddress,otCommissionerEnergyReportCallback aCallback,void * aContext)148 otError otCommissionerEnergyScan(otInstance *                       aInstance,
149                                  uint32_t                           aChannelMask,
150                                  uint8_t                            aCount,
151                                  uint16_t                           aPeriod,
152                                  uint16_t                           aScanDuration,
153                                  const otIp6Address *               aAddress,
154                                  otCommissionerEnergyReportCallback aCallback,
155                                  void *                             aContext)
156 {
157     Instance &instance = *static_cast<Instance *>(aInstance);
158 
159     return instance.Get<MeshCoP::Commissioner>().GetEnergyScanClient().SendQuery(
160         aChannelMask, aCount, aPeriod, aScanDuration, *static_cast<const Ip6::Address *>(aAddress), aCallback,
161         aContext);
162 }
163 
otCommissionerPanIdQuery(otInstance * aInstance,uint16_t aPanId,uint32_t aChannelMask,const otIp6Address * aAddress,otCommissionerPanIdConflictCallback aCallback,void * aContext)164 otError otCommissionerPanIdQuery(otInstance *                        aInstance,
165                                  uint16_t                            aPanId,
166                                  uint32_t                            aChannelMask,
167                                  const otIp6Address *                aAddress,
168                                  otCommissionerPanIdConflictCallback aCallback,
169                                  void *                              aContext)
170 {
171     Instance &instance = *static_cast<Instance *>(aInstance);
172 
173     return instance.Get<MeshCoP::Commissioner>().GetPanIdQueryClient().SendQuery(
174         aPanId, aChannelMask, *static_cast<const Ip6::Address *>(aAddress), aCallback, aContext);
175 }
176 
otCommissionerSendMgmtGet(otInstance * aInstance,const uint8_t * aTlvs,uint8_t aLength)177 otError otCommissionerSendMgmtGet(otInstance *aInstance, const uint8_t *aTlvs, uint8_t aLength)
178 {
179     Instance &instance = *static_cast<Instance *>(aInstance);
180 
181     return instance.Get<MeshCoP::Commissioner>().SendMgmtCommissionerGetRequest(aTlvs, aLength);
182 }
183 
otCommissionerSendMgmtSet(otInstance * aInstance,const otCommissioningDataset * aDataset,const uint8_t * aTlvs,uint8_t aLength)184 otError otCommissionerSendMgmtSet(otInstance *                  aInstance,
185                                   const otCommissioningDataset *aDataset,
186                                   const uint8_t *               aTlvs,
187                                   uint8_t                       aLength)
188 {
189     Instance &instance = *static_cast<Instance *>(aInstance);
190 
191     return instance.Get<MeshCoP::Commissioner>().SendMgmtCommissionerSetRequest(*aDataset, aTlvs, aLength);
192 }
193 
otCommissionerGetSessionId(otInstance * aInstance)194 uint16_t otCommissionerGetSessionId(otInstance *aInstance)
195 {
196     Instance &instance = *static_cast<Instance *>(aInstance);
197 
198     return instance.Get<MeshCoP::Commissioner>().GetSessionId();
199 }
200 
otCommissionerGetState(otInstance * aInstance)201 otCommissionerState otCommissionerGetState(otInstance *aInstance)
202 {
203     Instance &instance = *static_cast<Instance *>(aInstance);
204 
205     return static_cast<otCommissionerState>(instance.Get<MeshCoP::Commissioner>().GetState());
206 }
207 
208 #endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE
209