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 POSIX_PLATFORM_POWER_UPDATER_HPP_
30 #define POSIX_PLATFORM_POWER_UPDATER_HPP_
31 
32 #include "openthread-posix-config.h"
33 
34 #if OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_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 "power.hpp"
45 #include "common/code_utils.hpp"
46 
47 namespace ot {
48 namespace Posix {
49 
50 /**
51  * Updates the target power table and calibrated power table to the RCP.
52  *
53  */
54 class PowerUpdater
55 {
56 public:
PowerUpdater(void)57     PowerUpdater(void)
58         : mFactoryConfigFile(kFactoryConfigFile)
59         , mProductConfigFile(kProductConfigFile)
60         , mRegionCode(0)
61     {
62     }
63 
64     /**
65      * Set the region code.
66      *
67      * The radio region format is the 2-bytes ascii representation of the
68      * ISO 3166 alpha-2 code.
69      *
70      * @param[in]  aRegionCode  The radio region.
71      *
72      * @retval  OT_ERROR_NONE             Successfully set region code.
73      * @retval  OT_ERROR_FAILED           Failed to set the region code.
74      *
75      */
76     otError SetRegion(uint16_t aRegionCode);
77 
78     /**
79      * Get the region code.
80      *
81      * The radio region format is the 2-bytes ascii representation of the
82      * ISO 3166 alpha-2 code.
83      *
84      * @returns  The region code.
85      *
86      */
GetRegion(void) const87     uint16_t GetRegion(void) const { return mRegionCode; }
88 
89 private:
90     const char               *kFactoryConfigFile      = OPENTHREAD_POSIX_CONFIG_FACTORY_CONFIG_FILE;
91     const char               *kProductConfigFile      = OPENTHREAD_POSIX_CONFIG_PRODUCT_CONFIG_FILE;
92     const char               *kKeyCalibratedPower     = "calibrated_power";
93     const char               *kKeyTargetPower         = "target_power";
94     const char               *kKeyRegionDomainMapping = "region_domain_mapping";
95     const char               *kCommaDelimiter         = ",";
96     static constexpr uint16_t kMaxValueSize           = 512;
97     static constexpr uint16_t kRegionCodeWorldWide    = 0x5757; // Region Code: "WW"
98 
StringToRegionCode(const char * aString) const99     uint16_t StringToRegionCode(const char *aString) const
100     {
101         return static_cast<uint16_t>(((aString[0] & 0xFF) << 8) | ((aString[1] & 0xFF) << 0));
102     }
103     otError GetDomain(uint16_t aRegionCode, Power::Domain &aDomain);
104     otError GetNextTargetPower(const Power::Domain &aDomain, int &aIterator, Power::TargetPower &aTargetPower);
105     otError UpdateCalibratedPower(void);
106 
107     ConfigFile mFactoryConfigFile;
108     ConfigFile mProductConfigFile;
109     uint16_t   mRegionCode;
110 };
111 
112 } // namespace Posix
113 } // namespace ot
114 
115 #endif // OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
116 #endif // POSIX_PLATFORM_POWER_UPDATER_HPP_
117