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 /**
30  * @file
31  * @brief
32  *   This file includes definitions for the platform power calibration module.
33  */
34 #ifndef POWER_CALIBRATION_HPP_
35 #define POWER_CALIBRATION_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_CONFIG_POWER_CALIBRATION_ENABLE && OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
40 
41 #include <openthread/platform/radio.h>
42 
43 #include "common/array.hpp"
44 #include "common/numeric_limits.hpp"
45 #include "radio/radio.hpp"
46 
47 namespace ot {
48 namespace Utils {
49 
50 /**
51  * Implements power calibration module.
52  *
53  * The power calibration module implements the radio platform power calibration APIs. It mainly stores the calibrated
54  * power table and the target power table, provides an API for the platform to get the raw power setting of the
55  * specified channel.
56  */
57 class PowerCalibration : public InstanceLocator, private NonCopyable
58 {
59 public:
60     explicit PowerCalibration(Instance &aInstance);
61 
62     /**
63      * Add a calibrated power of the specified channel to the power calibration table.
64      *
65      * @param[in] aChannel                The radio channel.
66      * @param[in] aActualPower            The actual power in 0.01dBm.
67      * @param[in] aRawPowerSetting        A pointer to the raw power setting byte array.
68      * @param[in] aRawPowerSettingLength  The length of the @p aRawPowerSetting.
69      *
70      * @retval kErrorNone         Successfully added the calibrated power to the power calibration table.
71      * @retval kErrorNoBufs       No available entry in the power calibration table.
72      * @retval kErrorInvalidArgs  The @p aChannel, @p aActualPower or @p aRawPowerSetting is invalid or the
73      *                            @ aActualPower already exists in the power calibration table.
74      */
75     Error AddCalibratedPower(uint8_t        aChannel,
76                              int16_t        aActualPower,
77                              const uint8_t *aRawPowerSetting,
78                              uint16_t       aRawPowerSettingLength);
79 
80     /**
81      * Clear all calibrated powers from the power calibration table.
82      */
83     void ClearCalibratedPowers(void);
84 
85     /**
86      * Set the target power for the given channel.
87      *
88      * @param[in]  aChannel      The radio channel.
89      * @param[in]  aTargetPower  The target power in 0.01dBm. Passing `INT16_MAX` will disable this channel.
90      *
91      * @retval  kErrorNone         Successfully set the target power.
92      * @retval  kErrorInvalidArgs  The @p aChannel or @p aTargetPower is invalid.
93      */
94     Error SetChannelTargetPower(uint8_t aChannel, int16_t aTargetPower);
95 
96     /**
97      * Get the power settings for the given channel.
98      *
99      * Platform radio layer should parse the raw power setting based on the radio layer defined format and set the
100      * parameters of each radio hardware module.
101      *
102      * @param[in]      aChannel                The radio channel.
103      * @param[out]     aTargetPower            A pointer to the target power in 0.01 dBm. May be set to nullptr if
104      *                                         the caller doesn't want to get the target power.
105      * @param[out]     aActualPower            A pointer to the actual power in 0.01 dBm. May be set to nullptr if
106      *                                         the caller doesn't want to get the actual power.
107      * @param[out]     aRawPowerSetting        A pointer to the raw power setting byte array.
108      * @param[in,out]  aRawPowerSettingLength  On input, a pointer to the size of @p aRawPowerSetting.
109      *                                         On output, a pointer to the length of the raw power setting data.
110      *
111      * @retval  kErrorNone         Successfully got the target power.
112      * @retval  kErrorInvalidArgs  The @p aChannel is invalid, @p aRawPowerSetting or @p aRawPowerSettingLength is
113      *                             nullptr or @aRawPowerSettingLength is too short.
114      * @retval  kErrorNotFound     The power settings for the @p aChannel was not found.
115      */
116     Error GetPowerSettings(uint8_t   aChannel,
117                            int16_t  *aTargetPower,
118                            int16_t  *aActualPower,
119                            uint8_t  *aRawPowerSetting,
120                            uint16_t *aRawPowerSettingLength);
121 
122 private:
123     class CalibratedPowerEntry
124     {
125     public:
126         static constexpr uint16_t kMaxRawPowerSettingSize = OPENTHREAD_CONFIG_POWER_CALIBRATION_RAW_POWER_SETTING_SIZE;
127 
CalibratedPowerEntry(void)128         CalibratedPowerEntry(void)
129             : mActualPower(kInvalidPower)
130             , mLength(0)
131         {
132         }
133 
134         void    Init(int16_t aActualPower, const uint8_t *aRawPowerSetting, uint16_t aRawPowerSettingLength);
135         Error   GetRawPowerSetting(uint8_t *aRawPowerSetting, uint16_t *aRawPowerSettingLength);
GetActualPower(void) const136         int16_t GetActualPower(void) const { return mActualPower; }
Matches(int16_t aActualPower) const137         bool    Matches(int16_t aActualPower) const { return aActualPower == mActualPower; }
138 
139     private:
140         int16_t  mActualPower;
141         uint8_t  mSettings[kMaxRawPowerSettingSize];
142         uint16_t mLength;
143     };
144 
IsPowerUpdated(void) const145     bool IsPowerUpdated(void) const { return mCalibratedPowerIndex == kInvalidIndex; }
IsChannelValid(uint8_t aChannel) const146     bool IsChannelValid(uint8_t aChannel) const
147     {
148         return ((aChannel >= Radio::kChannelMin) && (aChannel <= Radio::kChannelMax));
149     }
150 
151     static constexpr uint8_t  kInvalidIndex = NumericLimits<uint8_t>::kMax;
152     static constexpr uint16_t kInvalidPower = NumericLimits<int16_t>::kMax;
153     static constexpr uint16_t kMaxNumCalibratedPowers =
154         OPENTHREAD_CONFIG_POWER_CALIBRATION_NUM_CALIBRATED_POWER_ENTRIES;
155     static constexpr uint16_t kNumChannels = Radio::kChannelMax - Radio::kChannelMin + 1;
156 
157     static_assert(kMaxNumCalibratedPowers < NumericLimits<uint8_t>::kMax,
158                   "kMaxNumCalibratedPowers is larger than or equal to max");
159 
160     typedef Array<CalibratedPowerEntry, kMaxNumCalibratedPowers> CalibratedPowerTable;
161 
162     uint8_t              mLastChannel;
163     int16_t              mTargetPowerTable[kNumChannels];
164     uint8_t              mCalibratedPowerIndex;
165     CalibratedPowerTable mCalibratedPowerTables[kNumChannels];
166 };
167 } // namespace Utils
168 } // namespace ot
169 
170 #endif // OPENTHREAD_CONFIG_POWER_CALIBRATION_ENABLE && OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
171 #endif // POWER_CALIBRATION_HPP_
172