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 includes definitions for Dataset Updater.
32  */
33 
34 #ifndef DATASET_UPDATER_HPP_
35 #define DATASET_UPDATER_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
40 
41 #include <openthread/dataset_updater.h>
42 
43 #include "common/callback.hpp"
44 #include "common/locator.hpp"
45 #include "common/message.hpp"
46 #include "common/non_copyable.hpp"
47 #include "common/notifier.hpp"
48 #include "common/timer.hpp"
49 #include "meshcop/dataset.hpp"
50 #include "meshcop/meshcop_tlvs.hpp"
51 
52 namespace ot {
53 namespace MeshCoP {
54 
55 /**
56  * Implements the Dataset Updater.
57  *
58  */
59 class DatasetUpdater : public InstanceLocator, private NonCopyable
60 {
61     friend class ot::Notifier;
62 
63 public:
64     /**
65      * Initializes a `DatasetUpdater` object.
66      *
67      * @param[in]   aInstance  A reference to the OpenThread instance.
68      *
69      */
70     explicit DatasetUpdater(Instance &aInstance);
71 
72     /**
73      * Represents the callback function pointer which is called when a Dataset update request finishes,
74      * reporting success or failure status of the request.
75      *
76      * The function pointer has the syntax `void (*UpdaterCallback)(Error aError, void *aContext)`.
77      *
78      */
79     typedef otDatasetUpdaterCallback UpdaterCallback;
80 
81     /**
82      * Requests an update to Operational Dataset.
83      *
84      * @p aDataset should contain the fields to be updated and their new value. It must not contain Active or Pending
85      * Timestamp fields. The Delay field is optional, if not provided a default value (`kDefaultDelay`) would be used.
86      *
87      * @param[in]  aDataset                Dataset info containing fields to change.
88      * @param[in]  aCallback               A callback to indicate when Dataset update request finishes.
89      * @param[in]  aContext                An arbitrary context passed to callback.
90      *
91      * @retval kErrorNone           Dataset update started successfully (@p aCallback will be invoked on completion).
92      * @retval kErrorInvalidState   Device is disabled (MLE is disabled).
93      * @retval kErrorInvalidArgs    The @p aDataset is not valid (contains Active or Pending Timestamp).
94      * @retval kErrorBusy           Cannot start update, a previous one is ongoing.
95      * @retval kErrorNoBufs         Could not allocated buffer to save Dataset.
96      *
97      */
98     Error RequestUpdate(const Dataset::Info &aDataset, UpdaterCallback aCallback, void *aContext);
99 
100     /**
101      * Cancels an ongoing (if any) Operational Dataset update request.
102      *
103      */
104     void CancelUpdate(void);
105 
106     /**
107      * Indicates whether there is an ongoing Operation Dataset update request.
108      *
109      * @retval TRUE    There is an ongoing update.
110      * @retval FALSE   There is no ongoing update.
111      *
112      */
IsUpdateOngoing(void) const113     bool IsUpdateOngoing(void) const { return mDataset != nullptr; }
114 
115 private:
116     // Default delay (in ms) in Pending Dataset.
117     static constexpr uint32_t kDefaultDelay = OPENTHREAD_CONFIG_DATASET_UPDATER_DEFAULT_DELAY;
118 
119     // Retry interval (in ms) when preparing and/or sending Pending Dataset fails.
120     static constexpr uint32_t kRetryInterval = 1000;
121 
122     void HandleTimer(void);
123     void PreparePendingDataset(void);
124     void Finish(Error aError);
125     void HandleNotifierEvents(Events aEvents);
126 
127     using UpdaterTimer = TimerMilliIn<DatasetUpdater, &DatasetUpdater::HandleTimer>;
128 
129     Callback<UpdaterCallback> mCallback;
130     UpdaterTimer              mTimer;
131     Message                  *mDataset;
132 };
133 
134 } // namespace MeshCoP
135 } // namespace ot
136 
137 #endif // (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
138 
139 #endif // DATASET_UPDATER_HPP_
140