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 strain 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_CONFIGURATION_HPP_
30 #define OT_POSIX_PLATFORM_CONFIGURATION_HPP_
31 
32 #include "openthread-posix-config.h"
33 
34 #if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
35 
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include <openthread/error.h>
40 #include <openthread/logging.h>
41 #include <openthread/platform/radio.h>
42 
43 #include "config_file.hpp"
44 #include "logger.hpp"
45 #include "power.hpp"
46 
47 #include "common/code_utils.hpp"
48 
49 namespace ot {
50 namespace Posix {
51 
52 /**
53  * Updates the target power table and calibrated power table to the RCP.
54  *
55  */
56 class Configuration : public Logger<Configuration>
57 {
58 public:
59     static const char kLogModuleName[]; ///< Module name used for logging.
60 
Configuration(void)61     Configuration(void)
62         : mFactoryConfigFile(OPENTHREAD_POSIX_CONFIG_FACTORY_CONFIG_FILE)
63         , mProductConfigFile(OPENTHREAD_POSIX_CONFIG_PRODUCT_CONFIG_FILE)
64         , mRegionCode(0)
65         , mSupportedChannelMask(kDefaultChannelMask)
66         , mPreferredChannelMask(kDefaultChannelMask)
67     {
68     }
69 
70     /**
71      * Set the region code.
72      *
73      * The radio region format is the 2-bytes ascii representation of the
74      * ISO 3166 alpha-2 code.
75      *
76      * @param[in]  aRegionCode  The radio region.
77      *
78      * @retval  OT_ERROR_NONE             Successfully set region code.
79      * @retval  OT_ERROR_FAILED           Failed to set the region code.
80      *
81      */
82     otError SetRegion(uint16_t aRegionCode);
83 
84     /**
85      * Get the region code.
86      *
87      * The radio region format is the 2-bytes ascii representation of the
88      * ISO 3166 alpha-2 code.
89      *
90      * @returns  The region code.
91      *
92      */
GetRegion(void) const93     uint16_t GetRegion(void) const { return mRegionCode; }
94 
95     /**
96      * Get the radio supported channel mask that the device is allowed to be on.
97      *
98      * @returns The radio supported channel mask.
99      *
100      */
GetSupportedChannelMask(void) const101     uint32_t GetSupportedChannelMask(void) const { return mSupportedChannelMask; }
102 
103     /**
104      * Gets the radio preferred channel mask that the device prefers to form on.
105      *
106      * @returns The radio preferred channel mask.
107      *
108      */
GetPreferredChannelMask(void) const109     uint32_t GetPreferredChannelMask(void) const { return mPreferredChannelMask; }
110 
111     /**
112      * Indicates whether the configuration file are valid.
113      *
114      * @retval TRUE  If there are any valid configuration keys in the configuration file.
115      * @retval FALSE If the configuration file doesn't exist or there is no key in the configuration file.
116      *
117      */
118     bool IsValid(void) const;
119 
120 private:
121 #if OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
122     static const char kKeyCalibratedPower[];
123 #endif
124     static const char         kKeyTargetPower[];
125     static const char         kKeyRegionDomainMapping[];
126     static const char         kKeySupportedChannelMask[];
127     static const char         kKeyPreferredChannelMask[];
128     static const char         kCommaDelimiter[];
129     static constexpr uint16_t kMaxValueSize        = 512;
130     static constexpr uint16_t kRegionCodeWorldWide = 0x5757;    // Region Code: "WW"
131     static constexpr uint32_t kDefaultChannelMask  = 0x7fff800; // Channel 11 ~ 26
132 
StringToRegionCode(const char * aString) const133     uint16_t StringToRegionCode(const char *aString) const
134     {
135         return static_cast<uint16_t>(((aString[0] & 0xFF) << 8) | ((aString[1] & 0xFF) << 0));
136     }
137     otError GetDomain(uint16_t aRegionCode, Power::Domain &aDomain);
138     otError GetChannelMask(const char *aKey, const Power::Domain &aDomain, uint32_t &aChannelMask);
139     otError UpdateChannelMasks(const Power::Domain &aDomain);
140 #if OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
141     otError UpdateTargetPower(const Power::Domain &aDomain);
142     otError UpdateCalibratedPower(void);
143     otError GetNextTargetPower(const Power::Domain &aDomain, int &aIterator, Power::TargetPower &aTargetPower);
144 #endif
145 
146     ConfigFile mFactoryConfigFile;
147     ConfigFile mProductConfigFile;
148     uint16_t   mRegionCode;
149     uint32_t   mSupportedChannelMask;
150     uint32_t   mPreferredChannelMask;
151 };
152 
153 } // namespace Posix
154 } // namespace ot
155 
156 #endif // OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
157 #endif // OT_POSIX_PLATFORM_CONFIGURATION_HPP_
158