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 OT_ASSERT(aDataset != nullptr);
53
54 return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().Read(AsCoreType(aDataset));
55 }
56
otDatasetGetActiveTlvs(otInstance * aInstance,otOperationalDatasetTlvs * aDataset)57 otError otDatasetGetActiveTlvs(otInstance *aInstance, otOperationalDatasetTlvs *aDataset)
58 {
59 OT_ASSERT(aDataset != nullptr);
60
61 return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().Read(*aDataset);
62 }
63
otDatasetSetActive(otInstance * aInstance,const otOperationalDataset * aDataset)64 otError otDatasetSetActive(otInstance *aInstance, const otOperationalDataset *aDataset)
65 {
66 OT_ASSERT(aDataset != nullptr);
67
68 return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().Save(AsCoreType(aDataset));
69 }
70
otDatasetSetActiveTlvs(otInstance * aInstance,const otOperationalDatasetTlvs * aDataset)71 otError otDatasetSetActiveTlvs(otInstance *aInstance, const otOperationalDatasetTlvs *aDataset)
72 {
73 OT_ASSERT(aDataset != nullptr);
74
75 return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().Save(*aDataset);
76 }
77
otDatasetGetPending(otInstance * aInstance,otOperationalDataset * aDataset)78 otError otDatasetGetPending(otInstance *aInstance, otOperationalDataset *aDataset)
79 {
80 OT_ASSERT(aDataset != nullptr);
81
82 return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().Read(AsCoreType(aDataset));
83 }
84
otDatasetGetPendingTlvs(otInstance * aInstance,otOperationalDatasetTlvs * aDataset)85 otError otDatasetGetPendingTlvs(otInstance *aInstance, otOperationalDatasetTlvs *aDataset)
86 {
87 OT_ASSERT(aDataset != nullptr);
88
89 return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().Read(*aDataset);
90 }
91
otDatasetSetPending(otInstance * aInstance,const otOperationalDataset * aDataset)92 otError otDatasetSetPending(otInstance *aInstance, const otOperationalDataset *aDataset)
93 {
94 OT_ASSERT(aDataset != nullptr);
95
96 return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().Save(AsCoreType(aDataset));
97 }
98
otDatasetSetPendingTlvs(otInstance * aInstance,const otOperationalDatasetTlvs * aDataset)99 otError otDatasetSetPendingTlvs(otInstance *aInstance, const otOperationalDatasetTlvs *aDataset)
100 {
101 OT_ASSERT(aDataset != nullptr);
102
103 return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().Save(*aDataset);
104 }
105
otDatasetSendMgmtActiveGet(otInstance * aInstance,const otOperationalDatasetComponents * aDatasetComponents,const uint8_t * aTlvTypes,uint8_t aLength,const otIp6Address * aAddress)106 otError otDatasetSendMgmtActiveGet(otInstance * aInstance,
107 const otOperationalDatasetComponents *aDatasetComponents,
108 const uint8_t * aTlvTypes,
109 uint8_t aLength,
110 const otIp6Address * aAddress)
111 {
112 return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().SendGetRequest(AsCoreType(aDatasetComponents),
113 aTlvTypes, aLength, aAddress);
114 }
115
otDatasetSendMgmtActiveSet(otInstance * aInstance,const otOperationalDataset * aDataset,const uint8_t * aTlvs,uint8_t aLength,otDatasetMgmtSetCallback aCallback,void * aContext)116 otError otDatasetSendMgmtActiveSet(otInstance * aInstance,
117 const otOperationalDataset *aDataset,
118 const uint8_t * aTlvs,
119 uint8_t aLength,
120 otDatasetMgmtSetCallback aCallback,
121 void * aContext)
122 {
123 return AsCoreType(aInstance).Get<MeshCoP::ActiveDatasetManager>().SendSetRequest(AsCoreType(aDataset), aTlvs,
124 aLength, aCallback, aContext);
125 }
126
otDatasetSendMgmtPendingGet(otInstance * aInstance,const otOperationalDatasetComponents * aDatasetComponents,const uint8_t * aTlvTypes,uint8_t aLength,const otIp6Address * aAddress)127 otError otDatasetSendMgmtPendingGet(otInstance * aInstance,
128 const otOperationalDatasetComponents *aDatasetComponents,
129 const uint8_t * aTlvTypes,
130 uint8_t aLength,
131 const otIp6Address * aAddress)
132 {
133 return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().SendGetRequest(AsCoreType(aDatasetComponents),
134 aTlvTypes, aLength, aAddress);
135 }
136
otDatasetSendMgmtPendingSet(otInstance * aInstance,const otOperationalDataset * aDataset,const uint8_t * aTlvs,uint8_t aLength,otDatasetMgmtSetCallback aCallback,void * aContext)137 otError otDatasetSendMgmtPendingSet(otInstance * aInstance,
138 const otOperationalDataset *aDataset,
139 const uint8_t * aTlvs,
140 uint8_t aLength,
141 otDatasetMgmtSetCallback aCallback,
142 void * aContext)
143 {
144 return AsCoreType(aInstance).Get<MeshCoP::PendingDatasetManager>().SendSetRequest(AsCoreType(aDataset), aTlvs,
145 aLength, aCallback, aContext);
146 }
147
148 #if OPENTHREAD_FTD
otDatasetGeneratePskc(const char * aPassPhrase,const otNetworkName * aNetworkName,const otExtendedPanId * aExtPanId,otPskc * aPskc)149 otError otDatasetGeneratePskc(const char * aPassPhrase,
150 const otNetworkName * aNetworkName,
151 const otExtendedPanId *aExtPanId,
152 otPskc * aPskc)
153 {
154 return MeshCoP::GeneratePskc(aPassPhrase, AsCoreType(aNetworkName), AsCoreType(aExtPanId), AsCoreType(aPskc));
155 }
156 #endif // OPENTHREAD_FTD
157
otNetworkNameFromString(otNetworkName * aNetworkName,const char * aNameString)158 otError otNetworkNameFromString(otNetworkName *aNetworkName, const char *aNameString)
159 {
160 otError error = AsCoreType(aNetworkName).Set(aNameString);
161
162 return (error == OT_ERROR_ALREADY) ? OT_ERROR_NONE : error;
163 }
164
otDatasetParseTlvs(const otOperationalDatasetTlvs * aDatasetTlvs,otOperationalDataset * aDataset)165 otError otDatasetParseTlvs(const otOperationalDatasetTlvs *aDatasetTlvs, otOperationalDataset *aDataset)
166 {
167 Error error = kErrorNone;
168 MeshCoP::Dataset dataset;
169
170 dataset.SetFrom(*aDatasetTlvs);
171 VerifyOrExit(dataset.IsValid(), error = kErrorInvalidArgs);
172 dataset.ConvertTo(AsCoreType(aDataset));
173
174 exit:
175 return error;
176 }
177