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