• 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 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 class DatasetUpdater : public InstanceLocator, private NonCopyable
59 {
60     friend class ot::Notifier;
61 
62 public:
63     /**
64      * Default delay (in ms).
65      */
66     static constexpr uint32_t kDefaultDelay = OPENTHREAD_CONFIG_DATASET_UPDATER_DEFAULT_DELAY;
67 
68     /**
69      * Initializes a `DatasetUpdater` object.
70      *
71      * @param[in]   aInstance  A reference to the OpenThread instance.
72      */
73     explicit DatasetUpdater(Instance &aInstance);
74 
75     /**
76      * Represents the callback function pointer which is called when a Dataset update request finishes,
77      * reporting success or failure status of the request.
78      *
79      * The function pointer has the syntax `void (*UpdaterCallback)(Error aError, void *aContext)`.
80      */
81     typedef otDatasetUpdaterCallback UpdaterCallback;
82 
83     /**
84      * Requests an update to Operational Dataset.
85      *
86      * @p aDataset should contain the fields to be updated and their new value. It must not contain Active or Pending
87      * Timestamp fields. The Delay field is optional, if not provided a default value (`kDefaultDelay`) would be used.
88      *
89      * @param[in]  aDataset                Dataset info containing fields to change.
90      * @param[in]  aCallback               A callback to indicate when Dataset update request finishes.
91      * @param[in]  aContext                An arbitrary context passed to callback.
92      *
93      * @retval kErrorNone           Dataset update started successfully (@p aCallback will be invoked on completion).
94      * @retval kErrorInvalidState   Device is disabled or not fully configured (missing or incomplete Active Dataset).
95      * @retval kErrorAlready        The @p aDataset fields already match the existing Active Dataset.
96      * @retval kErrorInvalidArgs    The @p aDataset is not valid (contains Active or Pending Timestamp).
97      * @retval kErrorBusy           Cannot start update, a previous one is ongoing.
98      * @retval kErrorNoBufs         Could not allocated buffer to save Dataset.
99      */
100     Error RequestUpdate(const Dataset::Info &aDataset, UpdaterCallback aCallback, void *aContext);
101 
102     /**
103      * Cancels an ongoing (if any) Operational Dataset update request.
104      */
105     void CancelUpdate(void);
106 
107     /**
108      * Indicates whether there is an ongoing Operation Dataset update request.
109      *
110      * @retval TRUE    There is an ongoing update.
111      * @retval FALSE   There is no ongoing update.
112      */
IsUpdateOngoing(void) const113     bool IsUpdateOngoing(void) const { return (mDataset != nullptr); }
114 
115 private:
116     Error RequestUpdate(Dataset &aDataset, UpdaterCallback aCallback, void *aContext);
117     void  Finish(Error aError);
118     void  HandleNotifierEvents(Events aEvents);
119     void  HandleDatasetChanged(Dataset::Type aType);
120 
121     Message                  *mDataset;
122     Callback<UpdaterCallback> mCallback;
123 };
124 
125 } // namespace MeshCoP
126 } // namespace ot
127 
128 #endif // (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
129 
130 #endif // DATASET_UPDATER_HPP_
131