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 Operational Dataset API (for both FTD and MTD).
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #include <openthread/dataset.h>
37 
38 #include "common/as_core_type.hpp"
39 #include "common/locator_getters.hpp"
40 #include "meshcop/dataset_manager.hpp"
41 #include "meshcop/meshcop.hpp"
42 
43 using namespace ot;
44 
otDatasetIsCommissioned(otInstance * aInstance)45 bool otDatasetIsCommissioned(otInstance *aInstance)
46 {
47     return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().IsCommissioned();
48 }
49 
otDatasetGetActive(otInstance * aInstance,otOperationalDataset * aDataset)50 otError otDatasetGetActive(otInstance *aInstance, otOperationalDataset *aDataset)
51 {
52     return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().Read(AsCoreType(aDataset));
53 }
54 
otDatasetGetActiveTlvs(otInstance * aInstance,otOperationalDatasetTlvs * aDataset)55 otError otDatasetGetActiveTlvs(otInstance *aInstance, otOperationalDatasetTlvs *aDataset)
56 {
57     AssertPointerIsNotNull(aDataset);
58 
59     return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().Read(*aDataset);
60 }
61 
otDatasetSetActive(otInstance * aInstance,const otOperationalDataset * aDataset)62 otError otDatasetSetActive(otInstance *aInstance, const otOperationalDataset *aDataset)
63 {
64     return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().Save(AsCoreType(aDataset));
65 }
66 
otDatasetSetActiveTlvs(otInstance * aInstance,const otOperationalDatasetTlvs * aDataset)67 otError otDatasetSetActiveTlvs(otInstance *aInstance, const otOperationalDatasetTlvs *aDataset)
68 {
69     AssertPointerIsNotNull(aDataset);
70 
71     return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().Save(*aDataset);
72 }
73 
otDatasetGetPending(otInstance * aInstance,otOperationalDataset * aDataset)74 otError otDatasetGetPending(otInstance *aInstance, otOperationalDataset *aDataset)
75 {
76     return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().Read(AsCoreType(aDataset));
77 }
78 
otDatasetGetPendingTlvs(otInstance * aInstance,otOperationalDatasetTlvs * aDataset)79 otError otDatasetGetPendingTlvs(otInstance *aInstance, otOperationalDatasetTlvs *aDataset)
80 {
81     AssertPointerIsNotNull(aDataset);
82 
83     return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().Read(*aDataset);
84 }
85 
otDatasetSetPending(otInstance * aInstance,const otOperationalDataset * aDataset)86 otError otDatasetSetPending(otInstance *aInstance, const otOperationalDataset *aDataset)
87 {
88     return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().Save(AsCoreType(aDataset));
89 }
90 
otDatasetSetPendingTlvs(otInstance * aInstance,const otOperationalDatasetTlvs * aDataset)91 otError otDatasetSetPendingTlvs(otInstance *aInstance, const otOperationalDatasetTlvs *aDataset)
92 {
93     AssertPointerIsNotNull(aDataset);
94 
95     return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().Save(*aDataset);
96 }
97 
otDatasetSendMgmtActiveGet(otInstance * aInstance,const otOperationalDatasetComponents * aDatasetComponents,const uint8_t * aTlvTypes,uint8_t aLength,const otIp6Address * aAddress)98 otError otDatasetSendMgmtActiveGet(otInstance                           *aInstance,
99                                    const otOperationalDatasetComponents *aDatasetComponents,
100                                    const uint8_t                        *aTlvTypes,
101                                    uint8_t                               aLength,
102                                    const otIp6Address                   *aAddress)
103 {
104     return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().SendGetRequest(AsCoreType(aDatasetComponents),
105                                                                                      aTlvTypes, aLength, aAddress);
106 }
107 
otDatasetSendMgmtActiveSet(otInstance * aInstance,const otOperationalDataset * aDataset,const uint8_t * aTlvs,uint8_t aLength,otDatasetMgmtSetCallback aCallback,void * aContext)108 otError otDatasetSendMgmtActiveSet(otInstance                 *aInstance,
109                                    const otOperationalDataset *aDataset,
110                                    const uint8_t              *aTlvs,
111                                    uint8_t                     aLength,
112                                    otDatasetMgmtSetCallback    aCallback,
113                                    void                       *aContext)
114 {
115     return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().SendSetRequest(AsCoreType(aDataset), aTlvs,
116                                                                                      aLength, aCallback, aContext);
117 }
118 
otDatasetSendMgmtPendingGet(otInstance * aInstance,const otOperationalDatasetComponents * aDatasetComponents,const uint8_t * aTlvTypes,uint8_t aLength,const otIp6Address * aAddress)119 otError otDatasetSendMgmtPendingGet(otInstance                           *aInstance,
120                                     const otOperationalDatasetComponents *aDatasetComponents,
121                                     const uint8_t                        *aTlvTypes,
122                                     uint8_t                               aLength,
123                                     const otIp6Address                   *aAddress)
124 {
125     return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().SendGetRequest(AsCoreType(aDatasetComponents),
126                                                                                       aTlvTypes, aLength, aAddress);
127 }
128 
otDatasetSendMgmtPendingSet(otInstance * aInstance,const otOperationalDataset * aDataset,const uint8_t * aTlvs,uint8_t aLength,otDatasetMgmtSetCallback aCallback,void * aContext)129 otError otDatasetSendMgmtPendingSet(otInstance                 *aInstance,
130                                     const otOperationalDataset *aDataset,
131                                     const uint8_t              *aTlvs,
132                                     uint8_t                     aLength,
133                                     otDatasetMgmtSetCallback    aCallback,
134                                     void                       *aContext)
135 {
136     return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().SendSetRequest(AsCoreType(aDataset), aTlvs,
137                                                                                       aLength, aCallback, aContext);
138 }
139 
140 #if OPENTHREAD_FTD
otDatasetGeneratePskc(const char * aPassPhrase,const otNetworkName * aNetworkName,const otExtendedPanId * aExtPanId,otPskc * aPskc)141 otError otDatasetGeneratePskc(const char            *aPassPhrase,
142                               const otNetworkName   *aNetworkName,
143                               const otExtendedPanId *aExtPanId,
144                               otPskc                *aPskc)
145 {
146     return MeshCoP::GeneratePskc(aPassPhrase, AsCoreType(aNetworkName), AsCoreType(aExtPanId), AsCoreType(aPskc));
147 }
148 #endif
149 
otNetworkNameFromString(otNetworkName * aNetworkName,const char * aNameString)150 otError otNetworkNameFromString(otNetworkName *aNetworkName, const char *aNameString)
151 {
152     otError error = AsCoreType(aNetworkName).Set(aNameString);
153 
154     return (error == OT_ERROR_ALREADY) ? OT_ERROR_NONE : error;
155 }
156 
otDatasetParseTlvs(const otOperationalDatasetTlvs * aDatasetTlvs,otOperationalDataset * aDataset)157 otError otDatasetParseTlvs(const otOperationalDatasetTlvs *aDatasetTlvs, otOperationalDataset *aDataset)
158 {
159     Error            error = kErrorNone;
160     MeshCoP::Dataset dataset;
161 
162     AssertPointerIsNotNull(aDatasetTlvs);
163 
164     dataset.SetFrom(*aDatasetTlvs);
165     VerifyOrExit(dataset.IsValid(), error = kErrorInvalidArgs);
166     dataset.ConvertTo(AsCoreType(aDataset));
167 
168 exit:
169     return error;
170 }
171 
otDatasetConvertToTlvs(const otOperationalDataset * aDataset,otOperationalDatasetTlvs * aDatasetTlvs)172 otError otDatasetConvertToTlvs(const otOperationalDataset *aDataset, otOperationalDatasetTlvs *aDatasetTlvs)
173 {
174     Error            error = kErrorNone;
175     MeshCoP::Dataset dataset;
176 
177     AssertPointerIsNotNull(aDatasetTlvs);
178 
179     SuccessOrExit(error = dataset.SetFrom(AsCoreType(aDataset)));
180     dataset.ConvertTo(*aDatasetTlvs);
181 
182 exit:
183     return error;
184 }
185 
otDatasetUpdateTlvs(const otOperationalDataset * aDataset,otOperationalDatasetTlvs * aDatasetTlvs)186 otError otDatasetUpdateTlvs(const otOperationalDataset *aDataset, otOperationalDatasetTlvs *aDatasetTlvs)
187 {
188     Error            error = kErrorNone;
189     MeshCoP::Dataset dataset;
190 
191     AssertPointerIsNotNull(aDatasetTlvs);
192 
193     dataset.SetFrom(*aDatasetTlvs);
194     SuccessOrExit(error = dataset.SetFrom(AsCoreType(aDataset)));
195     dataset.ConvertTo(*aDatasetTlvs);
196 
197 exit:
198     return error;
199 }
200