• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2024, 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 #ifndef OT_POSIX_PLATFORM_SETTINGS_FILE_HPP_
30 #define OT_POSIX_PLATFORM_SETTINGS_FILE_HPP_
31 
32 #include "openthread-posix-config.h"
33 #include "platform-posix.h"
34 
35 namespace ot {
36 namespace Posix {
37 
38 class SettingsFile
39 {
40 public:
SettingsFile(void)41     SettingsFile(void)
42         : mSettingsFd(-1)
43     {
44     }
45 
46     /**
47      * Performs the initialization for the settings file.
48      *
49      * @param[in]  aSettingsFileBaseName    A pointer to the base name of the settings file.
50      *
51      * @retval OT_ERROR_NONE    The given settings file was initialized successfully.
52      * @retval OT_ERROR_PARSE   The key-value format could not be parsed (invalid format).
53      */
54     otError Init(const char *aSettingsFileBaseName);
55 
56     /**
57      * Performs the de-initialization for the settings file.
58      */
59     void Deinit(void);
60 
61     /**
62      * Gets a setting from the settings file.
63      *
64      * @param[in]      aKey          The key associated with the requested setting.
65      * @param[in]      aIndex        The index of the specific item to get.
66      * @param[out]     aValue        A pointer to where the value of the setting should be written.
67      * @param[in,out]  aValueLength  A pointer to the length of the value.
68      *
69      * @retval OT_ERROR_NONE        The given setting was found and fetched successfully.
70      * @retval OT_ERROR_NOT_FOUND   The given key or index was not found in the setting store.
71      */
72     otError Get(uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength);
73 
74     /**
75      * Sets a setting in the settings file.
76      *
77      * @param[in]  aKey          The key associated with the requested setting.
78      * @param[in]  aValue        A pointer to where the new value of the setting should be read from.
79      * @param[in]  aValueLength  The length of the data pointed to by aValue.
80      */
81     void Set(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
82 
83     /**
84      * Adds a setting to the settings file.
85      *
86      * @param[in]  aKey          The key associated with the requested setting.
87      * @param[in]  aValue        A pointer to where the new value of the setting should be read from.
88      * @param[in]  aValueLength  The length of the data pointed to by aValue.
89      */
90     void Add(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
91 
92     /**
93      * Removes a setting from the settings file.
94      *
95      * @param[in]  aKey       The key associated with the requested setting.
96      * @param[in]  aIndex     The index of the value to be removed. If set to -1, all values for this aKey will be
97      *                        removed.
98      *
99      * @retval OT_ERROR_NONE        The given key and index was found and removed successfully.
100      * @retval OT_ERROR_NOT_FOUND   The given key or index was not found in the setting store.
101      */
102     otError Delete(uint16_t aKey, int aIndex);
103 
104     /**
105      * Deletes all settings from the setting file.
106      */
107     void Wipe(void);
108 
109 private:
110     static const size_t kMaxFileDirectorySize   = sizeof(OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH);
111     static const size_t kSlashLength            = 1;
112     static const size_t kMaxFileBaseNameSize    = 64;
113     static const size_t kMaxFileExtensionLength = 5; ///< The length of `.Swap` or `.data`.
114     static const size_t kMaxFilePathSize =
115         kMaxFileDirectorySize + kSlashLength + kMaxFileBaseNameSize + kMaxFileExtensionLength;
116 
117     otError Delete(uint16_t aKey, int aIndex, int *aSwapFd);
118     void    GetSettingsFilePath(char aFileName[kMaxFilePathSize], bool aSwap);
119     int     SwapOpen(void);
120     void    SwapWrite(int aFd, uint16_t aLength);
121     void    SwapPersist(int aFd);
122     void    SwapDiscard(int aFd);
123 
124     char mSettingFileBaseName[kMaxFileBaseNameSize];
125     int  mSettingsFd;
126 };
127 
128 } // namespace Posix
129 } // namespace ot
130 
131 #endif // OT_POSIX_PLATFORM_SETTINGS_FILE_HPP_
132