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