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