• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020, 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 Dataset Updater.
32  *
33  */
34 
35 #include "dataset_updater.hpp"
36 
37 #if (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
38 
39 #include "common/code_utils.hpp"
40 #include "common/locator_getters.hpp"
41 #include "common/log.hpp"
42 #include "common/random.hpp"
43 #include "instance/instance.hpp"
44 #include "meshcop/timestamp.hpp"
45 
46 namespace ot {
47 namespace MeshCoP {
48 
DatasetUpdater(Instance & aInstance)49 DatasetUpdater::DatasetUpdater(Instance &aInstance)
50     : InstanceLocator(aInstance)
51     , mTimer(aInstance)
52     , mDataset(nullptr)
53 {
54 }
55 
RequestUpdate(const Dataset::Info & aDataset,UpdaterCallback aCallback,void * aContext)56 Error DatasetUpdater::RequestUpdate(const Dataset::Info &aDataset, UpdaterCallback aCallback, void *aContext)
57 {
58     Error    error   = kErrorNone;
59     Message *message = nullptr;
60 
61     VerifyOrExit(!Get<Mle::Mle>().IsDisabled(), error = kErrorInvalidState);
62     VerifyOrExit(mDataset == nullptr, error = kErrorBusy);
63 
64     VerifyOrExit(!aDataset.IsActiveTimestampPresent() && !aDataset.IsPendingTimestampPresent(),
65                  error = kErrorInvalidArgs);
66 
67     message = Get<MessagePool>().Allocate(Message::kTypeOther);
68     VerifyOrExit(message != nullptr, error = kErrorNoBufs);
69 
70     SuccessOrExit(error = message->Append(aDataset));
71 
72     mCallback.Set(aCallback, aContext);
73     mDataset = message;
74 
75     mTimer.Start(1);
76 
77 exit:
78     FreeMessageOnError(message, error);
79     return error;
80 }
81 
CancelUpdate(void)82 void DatasetUpdater::CancelUpdate(void)
83 {
84     VerifyOrExit(mDataset != nullptr);
85 
86     FreeMessage(mDataset);
87     mDataset = nullptr;
88     mTimer.Stop();
89 
90 exit:
91     return;
92 }
93 
HandleTimer(void)94 void DatasetUpdater::HandleTimer(void) { PreparePendingDataset(); }
95 
PreparePendingDataset(void)96 void DatasetUpdater::PreparePendingDataset(void)
97 {
98     Dataset       dataset;
99     Dataset::Info requestedDataset;
100     Error         error;
101 
102     VerifyOrExit(!Get<Mle::Mle>().IsDisabled(), error = kErrorInvalidState);
103 
104     IgnoreError(mDataset->Read(0, requestedDataset));
105 
106     error = Get<ActiveDatasetManager>().Read(dataset);
107 
108     if (error != kErrorNone)
109     {
110         // If there is no valid Active Dataset but MLE is not disabled,
111         // set the timer to try again after the retry interval. This
112         // handles the situation where a dataset update request comes
113         // right after the network is formed but before the active
114         // dataset is created.
115 
116         mTimer.Start(kRetryInterval);
117         ExitNow(error = kErrorNone);
118     }
119 
120     IgnoreError(dataset.SetFrom(requestedDataset));
121 
122     if (!requestedDataset.IsDelayPresent())
123     {
124         uint32_t delay = kDefaultDelay;
125 
126         SuccessOrExit(error = dataset.Write<DelayTimerTlv>(delay));
127     }
128 
129     {
130         Timestamp timestamp;
131 
132         if (Get<PendingDatasetManager>().GetTimestamp() != nullptr)
133         {
134             timestamp = *Get<PendingDatasetManager>().GetTimestamp();
135         }
136         else
137         {
138             timestamp.Clear();
139         }
140 
141         timestamp.AdvanceRandomTicks();
142         dataset.SetTimestamp(Dataset::kPending, timestamp);
143     }
144 
145     {
146         Timestamp timestamp = dataset.FindTlv(Tlv::kActiveTimestamp)->ReadValueAs<ActiveTimestampTlv>();
147 
148         timestamp.AdvanceRandomTicks();
149         dataset.SetTimestamp(Dataset::kActive, timestamp);
150     }
151 
152     SuccessOrExit(error = Get<PendingDatasetManager>().Save(dataset));
153 
154 exit:
155     if (error != kErrorNone)
156     {
157         Finish(error);
158     }
159 }
160 
Finish(Error aError)161 void DatasetUpdater::Finish(Error aError)
162 {
163     OT_ASSERT(mDataset != nullptr);
164 
165     FreeMessage(mDataset);
166     mDataset = nullptr;
167 
168     mCallback.InvokeIfSet(aError);
169 }
170 
HandleNotifierEvents(Events aEvents)171 void DatasetUpdater::HandleNotifierEvents(Events aEvents)
172 {
173     Dataset::Info requestedDataset;
174     Dataset::Info dataset;
175 
176     VerifyOrExit(mDataset != nullptr);
177 
178     VerifyOrExit(aEvents.ContainsAny(kEventActiveDatasetChanged | kEventPendingDatasetChanged));
179 
180     IgnoreError(mDataset->Read(0, requestedDataset));
181 
182     if (aEvents.Contains(kEventActiveDatasetChanged) && Get<ActiveDatasetManager>().Read(dataset) == kErrorNone)
183     {
184         if (requestedDataset.IsSubsetOf(dataset))
185         {
186             Finish(kErrorNone);
187         }
188         else
189         {
190             Timestamp requestedDatasetTimestamp;
191             Timestamp activeDatasetTimestamp;
192 
193             requestedDataset.GetActiveTimestamp(requestedDatasetTimestamp);
194             dataset.GetActiveTimestamp(activeDatasetTimestamp);
195             if (Timestamp::Compare(requestedDatasetTimestamp, activeDatasetTimestamp) <= 0)
196             {
197                 Finish(kErrorAlready);
198             }
199         }
200     }
201 
202     if (aEvents.Contains(kEventPendingDatasetChanged) && Get<PendingDatasetManager>().Read(dataset) == kErrorNone)
203     {
204         if (!requestedDataset.IsSubsetOf(dataset))
205         {
206             Finish(kErrorAlready);
207         }
208     }
209 
210 exit:
211     return;
212 }
213 
214 } // namespace MeshCoP
215 } // namespace ot
216 
217 #endif // #if (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
218