xref: /aosp_15_r20/external/openthread/src/posix/platform/config_file.hpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1 /*
2  *  Copyright (c) 2022, 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_CONFIG_FILE_HPP_
30 #define OT_POSIX_PLATFORM_CONFIG_FILE_HPP_
31 
32 #include <assert.h>
33 #include <limits.h>
34 #include <stdint.h>
35 #include <openthread/error.h>
36 
37 namespace ot {
38 namespace Posix {
39 
40 /**
41  * Provides read/write/clear methods for key/value configuration files.
42  *
43  */
44 class ConfigFile
45 {
46 public:
47     /**
48      * Initializes the configuration file path.
49      *
50      * @param[in]  aFilePath  A pointer to the null-terminated file path.
51      *
52      */
53     explicit ConfigFile(const char *aFilePath);
54 
55     /**
56      * Gets a configuration from the configuration file.
57      *
58      * @param[in]      aKey          The key string associated with the requested configuration.
59      * @param[in,out]  aIterator     A reference to an iterator. MUST be initialized to 0 or the behavior is undefined.
60      * @param[out]     aValue        A pointer to where the new value string of the configuration should be read from.
61      *                               The @p aValue string will be terminated with `\0` if this method returns success.
62      *                               May be `nullptr` if performing a presence check.
63      * @param[in]      aValueLength  The max length of the data pointed to by @p aValue.
64      *
65      * @retval OT_ERROR_NONE          The given configuration was found and fetched successfully.
66      * @retval OT_ERROR_NOT_FOUND     The given key or iterator was not found in the configuration file.
67      * @retval OT_ERROR_INVALID_ARGS  If @p aKey was NULL.
68      *
69      */
70     otError Get(const char *aKey, int &aIterator, char *aValue, int aValueLength) const;
71 
72     /**
73      * Adds a configuration to the configuration file.
74      *
75      * @param[in]  aKey    The key string associated with the requested configuration.
76      * @param[in]  aValue  A pointer to where the new value string of the configuration should be written.
77      *
78      * @retval OT_ERROR_NONE          The given key was found and removed successfully.
79      * @retval OT_ERROR_INVALID_ARGS  If @p aKey or @p aValue was NULL.
80      *
81      */
82     otError Add(const char *aKey, const char *aValue);
83 
84     /**
85      * Removes all configurations with the same key string from the configuration file.
86      *
87      * @param[in]  aKey  The key string associated with the requested configuration.
88      *
89      * @retval OT_ERROR_NONE          The given key was found and removed successfully.
90      * @retval OT_ERROR_INVALID_ARGS  If @p aKey was NULL.
91      *
92      */
93     otError Clear(const char *aKey);
94 
95     /**
96      * Indicates whether the given key exists.
97      *
98      * @param[in]  aKey  The key string associated with the requested configuration.
99      *
100      * @retval TRUE  If the key exists in the configuration file.
101      * @retval FALSE If the key does not exist in the configuration file.
102      *
103      */
104     bool HasKey(const char *aKey) const;
105 
106     /**
107      * Indicates whether the configuration file exists.
108      *
109      * @retval TRUE  If the configuration file exists.
110      * @retval FALSE If the configuration file does not exist.
111      *
112      */
113     bool DoesExist(void) const;
114 
115 private:
116     const char               *kCommentDelimiter = "#";
117     const char               *kSwapSuffix       = ".swap";
118     static constexpr uint16_t kLineMaxSize      = 512;
119     static constexpr uint16_t kFilePathMaxSize  = PATH_MAX;
120 
121     void Strip(char *aString) const;
122 
123     const char *mFilePath;
124 };
125 
126 } // namespace Posix
127 } // namespace ot
128 
129 #endif // OT_POSIX_PLATFORM_CONFIG_FILE_HPP_
130