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 returns a pointer to the Timestamp or `nullptr` when it is not present in the Dataset.
93      *
94      * @returns A pointer to the Timestamp or `nullptr` if timestamp is not present in the Dataset.
95      *
96      */
GetTimestamp(void) const97     const Timestamp *GetTimestamp(void) const { return mTimestampPresent ? &mTimestamp : nullptr; }
98 
99     /**
100      * This method restores and retrieves the dataset from non-volatile memory.
101      *
102      * This method also sets the memory-cached timestamp for subsequent calls to `Compare()`.
103      *
104      * @param[out]  aDataset  Where to place the dataset.
105      *
106      * @retval kErrorNone      Successfully retrieved the dataset.
107      * @retval kErrorNotFound  There is no corresponding dataset stored in non-volatile memory.
108      *
109      */
110     Error Restore(Dataset &aDataset);
111 
112     /**
113      * This method retrieves the dataset from non-volatile memory.
114      *
115      * @param[out]  aDataset  Where to place the dataset.
116      *
117      * @retval kErrorNone      Successfully retrieved the dataset.
118      * @retval kErrorNotFound  There is no corresponding dataset stored in non-volatile memory.
119      *
120      */
121     Error Read(Dataset &aDataset) const;
122 
123     /**
124      * This method retrieves the dataset from non-volatile memory.
125      *
126      * @param[out]  aDatasetInfo  Where to place the dataset as `Dataset::Info`.
127      *
128      * @retval kErrorNone      Successfully retrieved the dataset.
129      * @retval kErrorNotFound  There is no corresponding dataset stored in non-volatile memory.
130      *
131      */
132     Error Read(Dataset::Info &aDatasetInfo) const;
133 
134     /**
135      * This method retrieves the dataset from non-volatile memory.
136      *
137      * @param[out]  aDataset  Where to place the dataset.
138      *
139      * @retval kErrorNone      Successfully retrieved the dataset.
140      * @retval kErrorNotFound  There is no corresponding dataset stored in non-volatile memory.
141      *
142      */
143     Error Read(otOperationalDatasetTlvs &aDataset) const;
144 
145     /**
146      * This method returns the local time this dataset was last updated or restored.
147      *
148      * @returns The local time this dataset was last updated or restored.
149      *
150      */
GetUpdateTime(void) const151     TimeMilli GetUpdateTime(void) const { return mUpdateTime; }
152 
153     /**
154      * This method stores the dataset into non-volatile memory.
155      *
156      * @param[in] aDatasetInfo     The Dataset to save as `Dataset::Info`.
157      *
158      * @retval kErrorNone             Successfully saved the dataset.
159      * @retval kErrorNotImplemented   The platform does not implement settings functionality.
160      *
161      */
162     Error Save(const Dataset::Info &aDatasetInfo);
163 
164     /**
165      * This method stores the dataset into non-volatile memory.
166      *
167      * @param[in]  aDataset  The Dataset to save as `otOperationalDatasetTlvs`.
168      *
169      * @retval kErrorNone             Successfully saved the dataset.
170      * @retval kErrorNotImplemented   The platform does not implement settings functionality.
171      *
172      */
173     Error Save(const otOperationalDatasetTlvs &aDataset);
174 
175     /**
176      * This method stores the dataset into non-volatile memory.
177      *
178      * @param[in]  aDataset  The Dataset to save.
179      *
180      * @retval kErrorNone             Successfully saved the dataset.
181      * @retval kErrorNotImplemented   The platform does not implement settings functionality.
182      *
183      */
184     Error Save(const Dataset &aDataset);
185 
186 private:
IsActive(void) const187     bool IsActive(void) const { return (mType == Dataset::kActive); }
188     void SetTimestamp(const Dataset &aDataset);
189 #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
190     void MoveKeysToSecureStorage(Dataset &aDataset) const;
191     void DestroySecurelyStoredKeys(void) const;
192     void EmplaceSecurelyStoredKeys(Dataset &aDataset) const;
193 #endif
194 
195     Timestamp     mTimestamp;            ///< Active or Pending Timestamp
196     TimeMilli     mUpdateTime;           ///< Local time last updated
197     Dataset::Type mType;                 ///< Active or Pending
198     bool          mTimestampPresent : 1; ///< Whether a timestamp is present
199     bool          mSaved : 1;            ///< Whether a dataset is saved in non-volatile
200 };
201 
202 } // namespace MeshCoP
203 } // namespace ot
204 
205 #endif // MESHCOP_DATASET_LOCAL_HPP_
206