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