• 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 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 class Configuration : public Logger<Configuration>
56 {
57 public:
58     static const char kLogModuleName[]; ///< Module name used for logging.
59 
Configuration(void)60     Configuration(void)
61         : mFactoryConfigFile(OPENTHREAD_POSIX_CONFIG_FACTORY_CONFIG_FILE)
62         , mProductConfigFile(OPENTHREAD_POSIX_CONFIG_PRODUCT_CONFIG_FILE)
63         , mRegionCode(0)
64         , mSupportedChannelMask(kDefaultChannelMask)
65         , mPreferredChannelMask(kDefaultChannelMask)
66     {
67     }
68 
69     /**
70      * Set the region code.
71      *
72      * The radio region format is the 2-bytes ascii representation of the
73      * ISO 3166 alpha-2 code.
74      *
75      * @param[in]  aRegionCode  The radio region.
76      *
77      * @retval  OT_ERROR_NONE             Successfully set region code.
78      * @retval  OT_ERROR_FAILED           Failed to set the region code.
79      */
80     otError SetRegion(uint16_t aRegionCode);
81 
82     /**
83      * Get the region code.
84      *
85      * The radio region format is the 2-bytes ascii representation of the
86      * ISO 3166 alpha-2 code.
87      *
88      * @returns  The region code.
89      */
GetRegion(void) const90     uint16_t GetRegion(void) const { return mRegionCode; }
91 
92     /**
93      * Get the radio supported channel mask that the device is allowed to be on.
94      *
95      * @returns The radio supported channel mask.
96      */
GetSupportedChannelMask(void) const97     uint32_t GetSupportedChannelMask(void) const { return mSupportedChannelMask; }
98 
99     /**
100      * Gets the radio preferred channel mask that the device prefers to form on.
101      *
102      * @returns The radio preferred channel mask.
103      */
GetPreferredChannelMask(void) const104     uint32_t GetPreferredChannelMask(void) const { return mPreferredChannelMask; }
105 
106     /**
107      * Indicates whether the configuration file are valid.
108      *
109      * @retval TRUE  If there are any valid configuration keys in the configuration file.
110      * @retval FALSE If the configuration file doesn't exist or there is no key in the configuration file.
111      */
112     bool IsValid(void) const;
113 
114 private:
115 #if OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
116     static const char kKeyCalibratedPower[];
117 #endif
118     static const char         kKeyTargetPower[];
119     static const char         kKeyRegionDomainMapping[];
120     static const char         kKeySupportedChannelMask[];
121     static const char         kKeyPreferredChannelMask[];
122     static const char         kCommaDelimiter[];
123     static constexpr uint16_t kMaxValueSize        = 512;
124     static constexpr uint16_t kRegionCodeWorldWide = 0x5757;    // Region Code: "WW"
125     static constexpr uint32_t kDefaultChannelMask  = 0x7fff800; // Channel 11 ~ 26
126 
StringToRegionCode(const char * aString) const127     uint16_t StringToRegionCode(const char *aString) const
128     {
129         return static_cast<uint16_t>(((aString[0] & 0xFF) << 8) | ((aString[1] & 0xFF) << 0));
130     }
131     otError GetDomain(uint16_t aRegionCode, Power::Domain &aDomain);
132     otError GetChannelMask(const char *aKey, const Power::Domain &aDomain, uint32_t &aChannelMask);
133     otError UpdateChannelMasks(const Power::Domain &aDomain);
134 #if OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
135     otError UpdateTargetPower(const Power::Domain &aDomain);
136     otError UpdateCalibratedPower(void);
137     otError GetNextTargetPower(const Power::Domain &aDomain, int &aIterator, Power::TargetPower &aTargetPower);
138 #endif
139 
140     ConfigFile mFactoryConfigFile;
141     ConfigFile mProductConfigFile;
142     uint16_t   mRegionCode;
143     uint32_t   mSupportedChannelMask;
144     uint32_t   mPreferredChannelMask;
145 };
146 
147 } // namespace Posix
148 } // namespace ot
149 
150 #endif // OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
151 #endif // OT_POSIX_PLATFORM_CONFIGURATION_HPP_
152