1 /*
2  *  Copyright (c) 2016-2017, 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 managing MeshCoP Datasets.
32  *
33  */
34 
35 #ifndef MESHCOP_DATASET_LOCAL_HPP_
36 #define MESHCOP_DATASET_LOCAL_HPP_
37 
38 #include "openthread-core-config.h"
39 
40 #include "common/locator.hpp"
41 #include "meshcop/dataset.hpp"
42 #include "meshcop/meshcop_tlvs.hpp"
43 
44 namespace ot {
45 namespace MeshCoP {
46 
47 class DatasetLocal : public InstanceLocator
48 {
49 public:
50     /**
51      * This constructor initializes the object.
52      *
53      * @param[in]  aInstance  A reference to an OpenThread instance.
54      * @param[in]  aType      The type of the dataset, active or pending.
55      *
56      */
57     DatasetLocal(Instance &aInstance, Dataset::Type aType);
58 
59     /**
60      * This method indicates whether this is an Active or Pending Dataset.
61      *
62      * @returns The Dataset type.
63      *
64      */
GetType(void) const65     Dataset::Type GetType(void) const { return mType; }
66 
67     /**
68      * This method clears the Dataset.
69      *
70      */
71     void Clear(void);
72 
73     /**
74      * This method indicates whether an Active or Pending Dataset is saved in non-volatile memory.
75      *
76      * @retval TRUE  if an Active or Pending Dataset is saved in non-volatile memory.
77      * @retval FALSE if an Active or Pending Dataset is not saved in non-volatile memory.
78      *
79      */
IsSaved(void) const80     bool IsSaved(void) const { return mSaved; }
81 
82     /**
83      * This method indicates whether an Active (Pending) Timestamp is present in the Active (Pending) Dataset.
84      *
85      * @retval TRUE  if an Active/Pending Timestamp is present.
86      * @retval FALSE if an Active/Pending Timestamp is not present.
87      *
88      */
IsTimestampPresent(void) const89     bool IsTimestampPresent(void) const { return mTimestampPresent; }
90 
91     /**
92      * This method restores and retrieves the dataset from non-volatile memory.
93      *
94      * This method also sets the memory-cached timestamp for subsequent calls to `Compare()`.
95      *
96      * @param[out]  aDataset  Where to place the dataset.
97      *
98      * @retval kErrorNone      Successfully retrieved the dataset.
99      * @retval kErrorNotFound  There is no corresponding dataset stored in non-volatile memory.
100      *
101      */
102     Error Restore(Dataset &aDataset);
103 
104     /**
105      * This method retrieves the dataset from non-volatile memory.
106      *
107      * @param[out]  aDataset  Where to place the dataset.
108      *
109      * @retval kErrorNone      Successfully retrieved the dataset.
110      * @retval kErrorNotFound  There is no corresponding dataset stored in non-volatile memory.
111      *
112      */
113     Error Read(Dataset &aDataset) const;
114 
115     /**
116      * This method retrieves the dataset from non-volatile memory.
117      *
118      * @param[out]  aDatasetInfo  Where to place the dataset as `Dataset::Info`.
119      *
120      * @retval kErrorNone      Successfully retrieved the dataset.
121      * @retval kErrorNotFound  There is no corresponding dataset stored in non-volatile memory.
122      *
123      */
124     Error Read(Dataset::Info &aDatasetInfo) const;
125 
126     /**
127      * This method retrieves the dataset from non-volatile memory.
128      *
129      * @param[out]  aDataset  Where to place the dataset.
130      *
131      * @retval kErrorNone      Successfully retrieved the dataset.
132      * @retval kErrorNotFound  There is no corresponding dataset stored in non-volatile memory.
133      *
134      */
135     Error Read(otOperationalDatasetTlvs &aDataset) const;
136 
137     /**
138      * This method returns the local time this dataset was last updated or restored.
139      *
140      * @returns The local time this dataset was last updated or restored.
141      *
142      */
GetUpdateTime(void) const143     TimeMilli GetUpdateTime(void) const { return mUpdateTime; }
144 
145     /**
146      * This method stores the dataset into non-volatile memory.
147      *
148      * @param[in] aDatasetInfo     The Dataset to save as `Dataset::Info`.
149      *
150      * @retval kErrorNone             Successfully saved the dataset.
151      * @retval kErrorNotImplemented   The platform does not implement settings functionality.
152      *
153      */
154     Error Save(const Dataset::Info &aDatasetInfo);
155 
156     /**
157      * This method stores the dataset into non-volatile memory.
158      *
159      * @param[in]  aDataset  The Dataset to save as `otOperationalDatasetTlvs`.
160      *
161      * @retval kErrorNone             Successfully saved the dataset.
162      * @retval kErrorNotImplemented   The platform does not implement settings functionality.
163      *
164      */
165     Error Save(const otOperationalDatasetTlvs &aDataset);
166 
167     /**
168      * This method stores the dataset into non-volatile memory.
169      *
170      * @param[in]  aDataset  The Dataset to save.
171      *
172      * @retval kErrorNone             Successfully saved the dataset.
173      * @retval kErrorNotImplemented   The platform does not implement settings functionality.
174      *
175      */
176     Error Save(const Dataset &aDataset);
177 
178     /**
179      * This method compares this dataset to another based on the timestamp.
180      *
181      * @param[in]  aCompare  A reference to the timestamp to compare.
182      *
183      * @retval -1  if @p aCompare is older than this dataset.
184      * @retval  0  if @p aCompare is equal to this dataset.
185      * @retval  1  if @p aCompare is newer than this dataset.
186      *
187      */
188     int Compare(const Timestamp *aCompare);
189 
190 private:
IsActive(void) const191     bool IsActive(void) const { return (mType == Dataset::kActive); }
192     void SetTimestamp(const Dataset &aDataset);
193 
194     Timestamp     mTimestamp;            ///< Active or Pending Timestamp
195     TimeMilli     mUpdateTime;           ///< Local time last updated
196     Dataset::Type mType;                 ///< Active or Pending
197     bool          mTimestampPresent : 1; ///< Whether a timestamp is present
198     bool          mSaved : 1;            ///< Whether a dataset is saved in non-volatile
199 };
200 
201 } // namespace MeshCoP
202 } // namespace ot
203 
204 #endif // MESHCOP_DATASET_LOCAL_HPP_
205