xref: /aosp_15_r20/external/openthread/src/posix/platform/configuration.hpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2022, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker  *  All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker  *
5*cfb92d14SAndroid Build Coastguard Worker  *  Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker  *  modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker  *  1. Redistributions of source code must strain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker  *  2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker  *     documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker  *  3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker  *     names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker  *     derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker  *
16*cfb92d14SAndroid Build Coastguard Worker  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker  *  POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker  */
28*cfb92d14SAndroid Build Coastguard Worker 
29*cfb92d14SAndroid Build Coastguard Worker #ifndef OT_POSIX_PLATFORM_CONFIGURATION_HPP_
30*cfb92d14SAndroid Build Coastguard Worker #define OT_POSIX_PLATFORM_CONFIGURATION_HPP_
31*cfb92d14SAndroid Build Coastguard Worker 
32*cfb92d14SAndroid Build Coastguard Worker #include "openthread-posix-config.h"
33*cfb92d14SAndroid Build Coastguard Worker 
34*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
35*cfb92d14SAndroid Build Coastguard Worker 
36*cfb92d14SAndroid Build Coastguard Worker #include <stdio.h>
37*cfb92d14SAndroid Build Coastguard Worker #include <string.h>
38*cfb92d14SAndroid Build Coastguard Worker 
39*cfb92d14SAndroid Build Coastguard Worker #include <openthread/error.h>
40*cfb92d14SAndroid Build Coastguard Worker #include <openthread/logging.h>
41*cfb92d14SAndroid Build Coastguard Worker #include <openthread/platform/radio.h>
42*cfb92d14SAndroid Build Coastguard Worker 
43*cfb92d14SAndroid Build Coastguard Worker #include "config_file.hpp"
44*cfb92d14SAndroid Build Coastguard Worker #include "logger.hpp"
45*cfb92d14SAndroid Build Coastguard Worker #include "power.hpp"
46*cfb92d14SAndroid Build Coastguard Worker 
47*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
48*cfb92d14SAndroid Build Coastguard Worker 
49*cfb92d14SAndroid Build Coastguard Worker namespace ot {
50*cfb92d14SAndroid Build Coastguard Worker namespace Posix {
51*cfb92d14SAndroid Build Coastguard Worker 
52*cfb92d14SAndroid Build Coastguard Worker /**
53*cfb92d14SAndroid Build Coastguard Worker  * Updates the target power table and calibrated power table to the RCP.
54*cfb92d14SAndroid Build Coastguard Worker  *
55*cfb92d14SAndroid Build Coastguard Worker  */
56*cfb92d14SAndroid Build Coastguard Worker class Configuration : public Logger<Configuration>
57*cfb92d14SAndroid Build Coastguard Worker {
58*cfb92d14SAndroid Build Coastguard Worker public:
59*cfb92d14SAndroid Build Coastguard Worker     static const char kLogModuleName[]; ///< Module name used for logging.
60*cfb92d14SAndroid Build Coastguard Worker 
Configuration(void)61*cfb92d14SAndroid Build Coastguard Worker     Configuration(void)
62*cfb92d14SAndroid Build Coastguard Worker         : mFactoryConfigFile(OPENTHREAD_POSIX_CONFIG_FACTORY_CONFIG_FILE)
63*cfb92d14SAndroid Build Coastguard Worker         , mProductConfigFile(OPENTHREAD_POSIX_CONFIG_PRODUCT_CONFIG_FILE)
64*cfb92d14SAndroid Build Coastguard Worker         , mRegionCode(0)
65*cfb92d14SAndroid Build Coastguard Worker         , mSupportedChannelMask(kDefaultChannelMask)
66*cfb92d14SAndroid Build Coastguard Worker         , mPreferredChannelMask(kDefaultChannelMask)
67*cfb92d14SAndroid Build Coastguard Worker     {
68*cfb92d14SAndroid Build Coastguard Worker     }
69*cfb92d14SAndroid Build Coastguard Worker 
70*cfb92d14SAndroid Build Coastguard Worker     /**
71*cfb92d14SAndroid Build Coastguard Worker      * Set the region code.
72*cfb92d14SAndroid Build Coastguard Worker      *
73*cfb92d14SAndroid Build Coastguard Worker      * The radio region format is the 2-bytes ascii representation of the
74*cfb92d14SAndroid Build Coastguard Worker      * ISO 3166 alpha-2 code.
75*cfb92d14SAndroid Build Coastguard Worker      *
76*cfb92d14SAndroid Build Coastguard Worker      * @param[in]  aRegionCode  The radio region.
77*cfb92d14SAndroid Build Coastguard Worker      *
78*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_NONE             Successfully set region code.
79*cfb92d14SAndroid Build Coastguard Worker      * @retval  OT_ERROR_FAILED           Failed to set the region code.
80*cfb92d14SAndroid Build Coastguard Worker      *
81*cfb92d14SAndroid Build Coastguard Worker      */
82*cfb92d14SAndroid Build Coastguard Worker     otError SetRegion(uint16_t aRegionCode);
83*cfb92d14SAndroid Build Coastguard Worker 
84*cfb92d14SAndroid Build Coastguard Worker     /**
85*cfb92d14SAndroid Build Coastguard Worker      * Get the region code.
86*cfb92d14SAndroid Build Coastguard Worker      *
87*cfb92d14SAndroid Build Coastguard Worker      * The radio region format is the 2-bytes ascii representation of the
88*cfb92d14SAndroid Build Coastguard Worker      * ISO 3166 alpha-2 code.
89*cfb92d14SAndroid Build Coastguard Worker      *
90*cfb92d14SAndroid Build Coastguard Worker      * @returns  The region code.
91*cfb92d14SAndroid Build Coastguard Worker      *
92*cfb92d14SAndroid Build Coastguard Worker      */
GetRegion(void) const93*cfb92d14SAndroid Build Coastguard Worker     uint16_t GetRegion(void) const { return mRegionCode; }
94*cfb92d14SAndroid Build Coastguard Worker 
95*cfb92d14SAndroid Build Coastguard Worker     /**
96*cfb92d14SAndroid Build Coastguard Worker      * Get the radio supported channel mask that the device is allowed to be on.
97*cfb92d14SAndroid Build Coastguard Worker      *
98*cfb92d14SAndroid Build Coastguard Worker      * @returns The radio supported channel mask.
99*cfb92d14SAndroid Build Coastguard Worker      *
100*cfb92d14SAndroid Build Coastguard Worker      */
GetSupportedChannelMask(void) const101*cfb92d14SAndroid Build Coastguard Worker     uint32_t GetSupportedChannelMask(void) const { return mSupportedChannelMask; }
102*cfb92d14SAndroid Build Coastguard Worker 
103*cfb92d14SAndroid Build Coastguard Worker     /**
104*cfb92d14SAndroid Build Coastguard Worker      * Gets the radio preferred channel mask that the device prefers to form on.
105*cfb92d14SAndroid Build Coastguard Worker      *
106*cfb92d14SAndroid Build Coastguard Worker      * @returns The radio preferred channel mask.
107*cfb92d14SAndroid Build Coastguard Worker      *
108*cfb92d14SAndroid Build Coastguard Worker      */
GetPreferredChannelMask(void) const109*cfb92d14SAndroid Build Coastguard Worker     uint32_t GetPreferredChannelMask(void) const { return mPreferredChannelMask; }
110*cfb92d14SAndroid Build Coastguard Worker 
111*cfb92d14SAndroid Build Coastguard Worker     /**
112*cfb92d14SAndroid Build Coastguard Worker      * Indicates whether the configuration file are valid.
113*cfb92d14SAndroid Build Coastguard Worker      *
114*cfb92d14SAndroid Build Coastguard Worker      * @retval TRUE  If there are any valid configuration keys in the configuration file.
115*cfb92d14SAndroid Build Coastguard Worker      * @retval FALSE If the configuration file doesn't exist or there is no key in the configuration file.
116*cfb92d14SAndroid Build Coastguard Worker      *
117*cfb92d14SAndroid Build Coastguard Worker      */
118*cfb92d14SAndroid Build Coastguard Worker     bool IsValid(void) const;
119*cfb92d14SAndroid Build Coastguard Worker 
120*cfb92d14SAndroid Build Coastguard Worker private:
121*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
122*cfb92d14SAndroid Build Coastguard Worker     static const char kKeyCalibratedPower[];
123*cfb92d14SAndroid Build Coastguard Worker #endif
124*cfb92d14SAndroid Build Coastguard Worker     static const char         kKeyTargetPower[];
125*cfb92d14SAndroid Build Coastguard Worker     static const char         kKeyRegionDomainMapping[];
126*cfb92d14SAndroid Build Coastguard Worker     static const char         kKeySupportedChannelMask[];
127*cfb92d14SAndroid Build Coastguard Worker     static const char         kKeyPreferredChannelMask[];
128*cfb92d14SAndroid Build Coastguard Worker     static const char         kCommaDelimiter[];
129*cfb92d14SAndroid Build Coastguard Worker     static constexpr uint16_t kMaxValueSize        = 512;
130*cfb92d14SAndroid Build Coastguard Worker     static constexpr uint16_t kRegionCodeWorldWide = 0x5757;    // Region Code: "WW"
131*cfb92d14SAndroid Build Coastguard Worker     static constexpr uint32_t kDefaultChannelMask  = 0x7fff800; // Channel 11 ~ 26
132*cfb92d14SAndroid Build Coastguard Worker 
StringToRegionCode(const char * aString) const133*cfb92d14SAndroid Build Coastguard Worker     uint16_t StringToRegionCode(const char *aString) const
134*cfb92d14SAndroid Build Coastguard Worker     {
135*cfb92d14SAndroid Build Coastguard Worker         return static_cast<uint16_t>(((aString[0] & 0xFF) << 8) | ((aString[1] & 0xFF) << 0));
136*cfb92d14SAndroid Build Coastguard Worker     }
137*cfb92d14SAndroid Build Coastguard Worker     otError GetDomain(uint16_t aRegionCode, Power::Domain &aDomain);
138*cfb92d14SAndroid Build Coastguard Worker     otError GetChannelMask(const char *aKey, const Power::Domain &aDomain, uint32_t &aChannelMask);
139*cfb92d14SAndroid Build Coastguard Worker     otError UpdateChannelMasks(const Power::Domain &aDomain);
140*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
141*cfb92d14SAndroid Build Coastguard Worker     otError UpdateTargetPower(const Power::Domain &aDomain);
142*cfb92d14SAndroid Build Coastguard Worker     otError UpdateCalibratedPower(void);
143*cfb92d14SAndroid Build Coastguard Worker     otError GetNextTargetPower(const Power::Domain &aDomain, int &aIterator, Power::TargetPower &aTargetPower);
144*cfb92d14SAndroid Build Coastguard Worker #endif
145*cfb92d14SAndroid Build Coastguard Worker 
146*cfb92d14SAndroid Build Coastguard Worker     ConfigFile mFactoryConfigFile;
147*cfb92d14SAndroid Build Coastguard Worker     ConfigFile mProductConfigFile;
148*cfb92d14SAndroid Build Coastguard Worker     uint16_t   mRegionCode;
149*cfb92d14SAndroid Build Coastguard Worker     uint32_t   mSupportedChannelMask;
150*cfb92d14SAndroid Build Coastguard Worker     uint32_t   mPreferredChannelMask;
151*cfb92d14SAndroid Build Coastguard Worker };
152*cfb92d14SAndroid Build Coastguard Worker 
153*cfb92d14SAndroid Build Coastguard Worker } // namespace Posix
154*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
155*cfb92d14SAndroid Build Coastguard Worker 
156*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
157*cfb92d14SAndroid Build Coastguard Worker #endif // OT_POSIX_PLATFORM_CONFIGURATION_HPP_
158