• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class ConfigFile
44 {
45 public:
46     /**
47      * Initializes the configuration file path.
48      *
49      * @param[in]  aFilePath  A pointer to the null-terminated file path.
50      */
51     explicit ConfigFile(const char *aFilePath);
52 
53     /**
54      * Gets a configuration from the configuration file.
55      *
56      * @param[in]      aKey          The key string associated with the requested configuration.
57      * @param[in,out]  aIterator     A reference to an iterator. MUST be initialized to 0 or the behavior is undefined.
58      * @param[out]     aValue        A pointer to where the new value string of the configuration should be read from.
59      *                               The @p aValue string will be terminated with `\0` if this method returns success.
60      *                               May be `nullptr` if performing a presence check.
61      * @param[in]      aValueLength  The max length of the data pointed to by @p aValue.
62      *
63      * @retval OT_ERROR_NONE          The given configuration was found and fetched successfully.
64      * @retval OT_ERROR_NOT_FOUND     The given key or iterator was not found in the configuration file.
65      * @retval OT_ERROR_INVALID_ARGS  If @p aKey was NULL.
66      */
67     otError Get(const char *aKey, int &aIterator, char *aValue, int aValueLength) const;
68 
69     /**
70      * Adds a configuration to the configuration file.
71      *
72      * @param[in]  aKey    The key string associated with the requested configuration.
73      * @param[in]  aValue  A pointer to where the new value string of the configuration should be written.
74      *
75      * @retval OT_ERROR_NONE          The given key was found and removed successfully.
76      * @retval OT_ERROR_INVALID_ARGS  If @p aKey or @p aValue was NULL.
77      */
78     otError Add(const char *aKey, const char *aValue);
79 
80     /**
81      * Removes all configurations with the same key string from the configuration file.
82      *
83      * @param[in]  aKey  The key string associated with the requested configuration.
84      *
85      * @retval OT_ERROR_NONE          The given key was found and removed successfully.
86      * @retval OT_ERROR_INVALID_ARGS  If @p aKey was NULL.
87      */
88     otError Clear(const char *aKey);
89 
90     /**
91      * Indicates whether the given key exists.
92      *
93      * @param[in]  aKey  The key string associated with the requested configuration.
94      *
95      * @retval TRUE  If the key exists in the configuration file.
96      * @retval FALSE If the key does not exist in the configuration file.
97      */
98     bool HasKey(const char *aKey) const;
99 
100     /**
101      * Indicates whether the configuration file exists.
102      *
103      * @retval TRUE  If the configuration file exists.
104      * @retval FALSE If the configuration file does not exist.
105      */
106     bool DoesExist(void) const;
107 
108 private:
109     const char               *kCommentDelimiter = "#";
110     const char               *kSwapSuffix       = ".swap";
111     static constexpr uint16_t kLineMaxSize      = 512;
112     static constexpr uint16_t kFilePathMaxSize  = PATH_MAX;
113 
114     void Strip(char *aString) const;
115 
116     const char *mFilePath;
117 };
118 
119 } // namespace Posix
120 } // namespace ot
121 
122 #endif // OT_POSIX_PLATFORM_CONFIG_FILE_HPP_
123