Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 1 | /* |
Jonathan Hui | 4435017 | 2016-09-13 15:57:11 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, The OpenThread Authors. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 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 | * This file includes definitions for storing and processing link quality information. |
| 32 | */ |
| 33 | |
| 34 | #ifndef LINK_QUALITY_HPP_ |
| 35 | #define LINK_QUALITY_HPP_ |
| 36 | |
Abtin Keshavarzian | 2ea5644 | 2017-10-17 09:46:34 -0700 | [diff] [blame] | 37 | #include "openthread-core-config.h" |
| 38 | |
Jonathan Hui | fbfd76a | 2017-05-05 11:28:29 -0700 | [diff] [blame] | 39 | #include <openthread/types.h> |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 40 | #include <openthread/platform/radio.h> |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 41 | |
Abtin Keshavarzian | 4b918f8 | 2018-06-11 09:10:52 -0700 | [diff] [blame^] | 42 | #include "common/string.hpp" |
| 43 | |
Jonathan Hui | 1eabda6 | 2017-04-27 22:29:05 -0700 | [diff] [blame] | 44 | namespace ot { |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * @addtogroup core-link-quality |
| 48 | * |
| 49 | * @brief |
| 50 | * This module includes definitions for Thread link quality metrics. |
| 51 | * |
| 52 | * @{ |
| 53 | */ |
| 54 | |
Abtin Keshavarzian | c2d2274 | 2018-01-22 08:56:18 -0800 | [diff] [blame] | 55 | /** |
| 56 | * This class implements an operation Success Rate Tracker. |
| 57 | * |
| 58 | * This can be used to track different link quality related metrics, e.g., CCA failure rate, frame tx success rate). |
| 59 | * The success rate is maintained using an exponential moving IIR averaging filter with a `uint16_t` as the storage. |
| 60 | * |
| 61 | */ |
| 62 | class SuccessRateTracker |
| 63 | { |
| 64 | public: |
| 65 | enum |
| 66 | { |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 67 | kMaxRateValue = 0xffff, ///< Indicates value corresponding to maximum (failure/success) rate of 100%. |
Abtin Keshavarzian | c2d2274 | 2018-01-22 08:56:18 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * This constructor initializes a `SuccessRateTracker` instance. |
| 72 | * |
| 73 | * After initialization the tracker starts with success rate 100% (failure rate 0%). |
| 74 | * |
| 75 | */ |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 76 | SuccessRateTracker(void) |
| 77 | : mFailureRate(0) |
| 78 | { |
| 79 | } |
Abtin Keshavarzian | c2d2274 | 2018-01-22 08:56:18 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * This method resets the tracker to its initialized state, setting success rate to 100%. |
| 83 | * |
| 84 | */ |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 85 | void Reset(void) { mFailureRate = 0; } |
Abtin Keshavarzian | c2d2274 | 2018-01-22 08:56:18 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * This method adds a sample (success or failure) to `SuccessRateTracker`. |
| 89 | * |
| 90 | * @param[in] aSuccess The sample status be added, `true` for success, `false` for failure. |
| 91 | * @param[in] aWeight The weight coefficient used for adding the new sample into average. |
| 92 | * |
| 93 | */ |
| 94 | void AddSample(bool aSuccess, uint16_t aWeight = kDefaultWeight); |
| 95 | |
| 96 | /** |
| 97 | * This method returns the average failure rate. |
| 98 | * |
| 99 | * @retval the average failure rate `[0-kMaxRateValue]` with `kMaxRateValue` corresponding to 100%. |
| 100 | * |
| 101 | */ |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 102 | uint16_t GetFailureRate(void) const { return mFailureRate; } |
Abtin Keshavarzian | c2d2274 | 2018-01-22 08:56:18 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * This method returns the average success rate. |
| 106 | * |
| 107 | * @retval the average success rate as [0-kMaxRateValue] with `kMaxRateValue` corresponding to 100%. |
| 108 | * |
| 109 | */ |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 110 | uint16_t GetSuccessRate(void) const { return kMaxRateValue - mFailureRate; } |
Abtin Keshavarzian | c2d2274 | 2018-01-22 08:56:18 -0800 | [diff] [blame] | 111 | |
| 112 | private: |
| 113 | enum |
| 114 | { |
| 115 | kDefaultWeight = 64, |
| 116 | }; |
| 117 | |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 118 | uint16_t mFailureRate; |
Abtin Keshavarzian | c2d2274 | 2018-01-22 08:56:18 -0800 | [diff] [blame] | 119 | }; |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * This class implements a Received Signal Strength (RSS) averager. |
| 123 | * |
| 124 | * The average is maintained using an adaptive exponentially weighted moving filter. |
| 125 | * |
| 126 | */ |
| 127 | class RssAverager |
| 128 | { |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 129 | public: |
| 130 | enum |
| 131 | { |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 132 | kStringSize = 10, ///< Max chars needed for a string representation of average (@sa ToString()). |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /** |
Abtin Keshavarzian | 4b918f8 | 2018-06-11 09:10:52 -0700 | [diff] [blame^] | 136 | * This type defines the fixed-length `String` object returned from `ToString()`. |
| 137 | * |
| 138 | */ |
| 139 | typedef String<kStringSize> InfoString; |
| 140 | |
| 141 | /** |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 142 | * This method reset the averager and clears the average value. |
| 143 | * |
| 144 | */ |
| 145 | void Reset(void); |
| 146 | |
| 147 | /** |
| 148 | * This method indicates whether the averager contains an average (i.e., at least one RSS value has been added). |
| 149 | * |
| 150 | * @retval true If the average value is available (at least one RSS value has been added). |
| 151 | * @retval false Averager is empty (no RSS value added yet). |
| 152 | * |
| 153 | */ |
| 154 | bool HasAverage(void) const { return (mCount != 0); } |
| 155 | |
| 156 | /** |
| 157 | * This method adds a received signal strength (RSS) value to the average. |
| 158 | * |
| 159 | * If @p aRss is OT_RADIO_RSSI_INVALID, it is ignored and error status OT_ERROR_INVALID_ARGS is returned. |
| 160 | * The value of RSS is capped at 0dBm (i.e., for any given RSS value higher than 0dBm, 0dBm is used instead). |
| 161 | * |
| 162 | * @param[in] aRss Received signal strength value (in dBm) to be added to the average. |
| 163 | * |
| 164 | * @retval OT_ERROR_NONE New RSS value added to average successfully. |
| 165 | * @retval OT_ERROR_INVALID_ARGS Value of @p aRss is OT_RADIO_RSSI_INVALID. |
| 166 | * |
| 167 | */ |
| 168 | otError Add(int8_t aRss); |
| 169 | |
| 170 | /** |
| 171 | * This method returns the current average signal strength value maintained by the averager. |
| 172 | * |
| 173 | * @returns The current average value (in dBm) or OT_RADIO_RSSI_INVALID if no average is available. |
| 174 | * |
| 175 | */ |
| 176 | int8_t GetAverage(void) const; |
| 177 | |
| 178 | /** |
| 179 | * This method returns an raw/encoded version of current average signal strength value. The raw value is the |
| 180 | * average multiplied by a precision factor (currently set as -8). |
| 181 | * |
| 182 | * @returns The current average multiplied by precision factor or zero if no average is available. |
| 183 | * |
| 184 | */ |
| 185 | uint16_t GetRaw(void) const { return mAverage; } |
| 186 | |
| 187 | /** |
| 188 | * This method converts the current average RSS value to a human-readable string (e.g., "-80.375"). If the |
| 189 | * average is unknown, empty string is returned. |
| 190 | * |
Abtin Keshavarzian | 4b918f8 | 2018-06-11 09:10:52 -0700 | [diff] [blame^] | 191 | * @returns An `InfoString` object containing the string representation of average RSS. |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 192 | * |
| 193 | */ |
Abtin Keshavarzian | 4b918f8 | 2018-06-11 09:10:52 -0700 | [diff] [blame^] | 194 | InfoString ToString(void) const; |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 195 | |
| 196 | private: |
| 197 | /* |
| 198 | * The RssAverager uses an adaptive exponentially weighted filter to maintain the average. It keeps track of |
| 199 | * current average and the number of added RSS values (up to a 8). |
| 200 | * |
| 201 | * For the first 8 added RSS values, the average is the arithmetic mean of the added values (e.g., if 5 values are |
| 202 | * added, the average is sum of the 5 added RSS values divided by 5. After the 8th RSS value, a weighted filter is |
| 203 | * used with coefficients (1/8, 7/8), i.e., newAverage = 1/8 * newRss + 7/8 * oldAverage. |
| 204 | * |
| 205 | * To add to accuracy of the averaging process, the RSS values and the maintained average are multiplied by a |
| 206 | * precision factor of -8. |
| 207 | * |
| 208 | */ |
| 209 | |
| 210 | enum |
| 211 | { |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 212 | kPrecisionBitShift = 3, // Precision multiple for RSS average (1 << PrecisionBitShift). |
| 213 | kPrecision = (1 << kPrecisionBitShift), |
| 214 | kPrecisionBitMask = (kPrecision - 1), |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 215 | |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 216 | kCoeffBitShift = 3, // Coefficient used for exponentially weighted filter (1 << kCoeffBitShift). |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | // Member variables fit into two bytes. |
| 220 | |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 221 | uint16_t mAverage : 11; // The raw average signal strength value (stored as RSS times precision multiple). |
| 222 | uint16_t mCount : 5; // Number of RSS values added to averager so far (limited to 2^kCoeffBitShift-1). |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 225 | /** |
| 226 | * This class encapsulates/stores all relevant information about quality of a link, including average received signal |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 227 | * strength (RSS), last RSS, link margin, and link quality. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 228 | * |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 229 | */ |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 230 | class LinkQualityInfo |
| 231 | { |
| 232 | public: |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 233 | enum |
| 234 | { |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 235 | kInfoStringSize = 50, ///< Max chars needed for the info string representation (@sa ToInfoString()) |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 238 | /** |
Abtin Keshavarzian | 4b918f8 | 2018-06-11 09:10:52 -0700 | [diff] [blame^] | 239 | * This type defines the fixed-length `String` object returned from `ToInfoString()`. |
| 240 | * |
| 241 | */ |
| 242 | typedef String<kInfoStringSize> InfoString; |
| 243 | |
| 244 | /** |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 245 | * This constructor initializes the object. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 246 | * |
| 247 | */ |
| 248 | LinkQualityInfo(void); |
| 249 | |
| 250 | /** |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 251 | * This method clears the all the data in the object. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 252 | * |
| 253 | */ |
| 254 | void Clear(void); |
| 255 | |
| 256 | /** |
| 257 | * This method adds a new received signal strength (RSS) value to the average. |
| 258 | * |
Shu Chen | 66f6e70 | 2017-05-05 11:38:37 +0800 | [diff] [blame] | 259 | * @param[in] aNoiseFloor The noise floor value (in dBm). |
Abtin Keshavarzian | b3976f5 | 2017-04-14 14:32:55 -0700 | [diff] [blame] | 260 | * @param[in] aRss A new received signal strength value (in dBm) to be added to the average. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 261 | * |
| 262 | */ |
Shu Chen | 66f6e70 | 2017-05-05 11:38:37 +0800 | [diff] [blame] | 263 | void AddRss(int8_t aNoiseFloor, int8_t aRss); |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 264 | |
| 265 | /** |
Abtin Keshavarzian | c2d2274 | 2018-01-22 08:56:18 -0800 | [diff] [blame] | 266 | * This method returns the current average received signal strength value. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 267 | * |
Shu Chen | 6e3d663 | 2017-06-13 02:05:40 +0800 | [diff] [blame] | 268 | * @returns The current average value or @c OT_RADIO_RSSI_INVALID if no average is available. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 269 | * |
| 270 | */ |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 271 | int8_t GetAverageRss(void) const { return mRssAverager.GetAverage(); } |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 272 | |
| 273 | /** |
| 274 | * This method returns an encoded version of current average signal strength value. The encoded value is the |
| 275 | * average multiplied by a precision factor (currently -8). |
| 276 | * |
| 277 | * @returns The current average multiplied by precision factor or zero if no average is available. |
| 278 | * |
| 279 | */ |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 280 | uint16_t GetAverageRssRaw(void) const { return mRssAverager.GetRaw(); } |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 281 | |
| 282 | /** |
Abtin Keshavarzian | 4b918f8 | 2018-06-11 09:10:52 -0700 | [diff] [blame^] | 283 | * This method converts the link quality info to info/debug human-readable string. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 284 | * |
Abtin Keshavarzian | 4b918f8 | 2018-06-11 09:10:52 -0700 | [diff] [blame^] | 285 | * @returns An `InfoString` representing the link quality info. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 286 | * |
| 287 | */ |
Abtin Keshavarzian | 4b918f8 | 2018-06-11 09:10:52 -0700 | [diff] [blame^] | 288 | InfoString ToInfoString(void) const; |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * This method returns the link margin. The link margin is calculated using the link's current average received |
| 292 | * signal strength (RSS) and average noise floor. |
| 293 | * |
Shu Chen | 66f6e70 | 2017-05-05 11:38:37 +0800 | [diff] [blame] | 294 | * @param[in] aNoiseFloor The noise floor value (in dBm). |
Jonathan Hui | 28a5645 | 2016-08-29 21:49:50 -0700 | [diff] [blame] | 295 | * |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 296 | * @returns Link margin derived from average received signal strength and average noise floor. |
| 297 | * |
| 298 | */ |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 299 | uint8_t GetLinkMargin(int8_t aNoiseFloor) const { return ConvertRssToLinkMargin(aNoiseFloor, GetAverageRss()); } |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * Returns the current one-way link quality value. The link quality value is a number 0-3. |
| 303 | * |
| 304 | * The link quality is calculated by comparing the current link margin with a set of thresholds (per Thread spec). |
| 305 | * More specifically, link margin > 20 dB gives link quality 3, link margin > 10 dB gives link quality 2, |
| 306 | * link margin > 2 dB gives link quality 1, and link margin below or equal to 2 dB yields link quality of 0. |
| 307 | * |
| 308 | * In order to ensure that a link margin near the boundary of two different link quality values does not cause |
| 309 | * frequent changes, a hysteresis of 2 dB is applied when determining the link quality. For example, the average |
| 310 | * link margin must be at least 12 dB to change a quality 1 link to a quality 2 link. |
| 311 | * |
Shu Chen | 66f6e70 | 2017-05-05 11:38:37 +0800 | [diff] [blame] | 312 | * @param[in] aNoiseFloor The noise floor value (in dBm). |
Jonathan Hui | 28a5645 | 2016-08-29 21:49:50 -0700 | [diff] [blame] | 313 | * |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 314 | * @returns The current link quality value (value 0-3 as per Thread specification). |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 315 | * |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 316 | */ |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 317 | uint8_t GetLinkQuality(void) const { return mLinkQuality; } |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 318 | |
| 319 | /** |
Adam Eliot | 8952104 | 2017-03-28 09:41:57 -0700 | [diff] [blame] | 320 | * Returns the most recent RSS value. |
| 321 | * |
| 322 | * @returns The most recent RSS |
| 323 | * |
| 324 | */ |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 325 | int8_t GetLastRss(void) const { return mLastRss; } |
Adam Eliot | 8952104 | 2017-03-28 09:41:57 -0700 | [diff] [blame] | 326 | |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 327 | #if OPENTHREAD_CONFIG_ENABLE_TX_ERROR_RATE_TRACKING |
| 328 | |
| 329 | /** |
| 330 | * This method adds a MAC frame transmission status (success/failure) and updates the frame tx error rate. |
| 331 | * |
| 332 | * @param[in] aTxStatus Success/Failure of MAC frame transmission (`true` -> success, `false` -> failure). |
| 333 | * |
| 334 | */ |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 335 | void AddFrameTxStatus(bool aTxStatus) |
| 336 | { |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 337 | mFrameErrorRate.AddSample(aTxStatus, OPENTHREAD_CONFIG_FRAME_TX_ERR_RATE_AVERAGING_WINDOW); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * This method adds a message transmission status (success/failure) and updates the message error rate. |
| 342 | * |
| 343 | * @param[in] aTxStatus Success/Failure of message (`true` -> success, `false` -> message tx failed). |
| 344 | * A larger (IPv6) message may be fragmented and sent as multiple MAC frames. The message |
| 345 | * transmission is considered a failure, if any of its fragments fail after all MAC retry |
| 346 | * attempts. |
| 347 | * |
| 348 | */ |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 349 | void AddMessageTxStatus(bool aTxStatus) |
| 350 | { |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 351 | mMessageErrorRate.AddSample(aTxStatus, OPENTHREAD_CONFIG_IPV6_TX_ERR_RATE_AVERAGING_WINDOW); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * This method returns the MAC frame transmission error rate for the link. |
| 356 | * |
| 357 | * The rate is maintained over a window of (roughly) last `OPENTHREAD_CONFIG_FRAME_TX_ERR_RATE_AVERAGING_WINDOW` |
| 358 | * frame transmissions. |
| 359 | * |
| 360 | * @returns The error rate with maximum value `0xffff` corresponding to 100% failure rate. |
| 361 | * |
| 362 | */ |
| 363 | uint16_t GetFrameErrorRate(void) const { return mFrameErrorRate.GetFailureRate(); } |
| 364 | |
| 365 | /** |
| 366 | * This method returns the message error rate for the link. |
| 367 | * |
| 368 | * The rate is maintained over a window of (roughly) last `OPENTHREAD_CONFIG_IPV6_TX_ERR_RATE_AVERAGING_WINDOW` |
| 369 | * (IPv6) messages. |
| 370 | * |
| 371 | * Note that a larger (IPv6) message can be fragmented and sent as multiple MAC frames. The message transmission is |
| 372 | * considered a failure, if any of its fragments fail after all MAC retry attempts. |
| 373 | * |
| 374 | * @returns The error rate with maximum value `0xffff` corresponding to 100% failure rate. |
| 375 | * |
| 376 | */ |
| 377 | uint16_t GetMessageErrorRate(void) const { return mMessageErrorRate.GetFailureRate(); } |
| 378 | |
| 379 | #endif // OPENTHREAD_CONFIG_ENABLE_TX_ERROR_RATE_TRACKING |
| 380 | |
Adam Eliot | 8952104 | 2017-03-28 09:41:57 -0700 | [diff] [blame] | 381 | /** |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 382 | * This method converts a received signal strength value to a link margin value. |
| 383 | * |
Shu Chen | 66f6e70 | 2017-05-05 11:38:37 +0800 | [diff] [blame] | 384 | * @param[in] aNoiseFloor The noise floor value (in dBm). |
Abtin Keshavarzian | b3976f5 | 2017-04-14 14:32:55 -0700 | [diff] [blame] | 385 | * @param[in] aRss The received signal strength value (in dBm). |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 386 | * |
| 387 | * @returns The link margin value. |
| 388 | * |
| 389 | */ |
Shu Chen | 66f6e70 | 2017-05-05 11:38:37 +0800 | [diff] [blame] | 390 | static uint8_t ConvertRssToLinkMargin(int8_t aNoiseFloor, int8_t aRss); |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 391 | |
| 392 | /** |
| 393 | * This method converts a link margin value to a link quality value. |
| 394 | * |
| 395 | * @param[in] aLinkMargin The Link Margin in dB. |
| 396 | * |
| 397 | * @returns The link quality value (0-3). |
| 398 | * |
| 399 | */ |
| 400 | static uint8_t ConvertLinkMarginToLinkQuality(uint8_t aLinkMargin); |
| 401 | |
| 402 | /** |
| 403 | * This method converts a received signal strength value to a link quality value. |
| 404 | * |
Shu Chen | 66f6e70 | 2017-05-05 11:38:37 +0800 | [diff] [blame] | 405 | * @param[in] aNoiseFloor The noise floor value (in dBm). |
Abtin Keshavarzian | b3976f5 | 2017-04-14 14:32:55 -0700 | [diff] [blame] | 406 | * @param[in] aRss The received signal strength value (in dBm). |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 407 | * |
| 408 | * @returns The link quality value (0-3). |
| 409 | * |
| 410 | */ |
Shu Chen | 66f6e70 | 2017-05-05 11:38:37 +0800 | [diff] [blame] | 411 | static uint8_t ConvertRssToLinkQuality(int8_t aNoiseFloor, int8_t aRss); |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 412 | |
rongli | 502c6ca | 2017-07-12 13:38:49 +0800 | [diff] [blame] | 413 | /** |
| 414 | * This method converts a link quality value to a typical received signal strength value . |
| 415 | * @note only for test |
| 416 | * |
| 417 | * @param[in] aNoiseFloor The noise floor value (in dBm). |
| 418 | * @param[in] aLinkQuality The link quality value in [0, 3]. |
| 419 | * |
| 420 | * @returns The typical platform rssi. |
| 421 | * |
| 422 | */ |
| 423 | static int8_t ConvertLinkQualityToRss(int8_t aNoiseFloor, uint8_t aLinkQuality); |
| 424 | |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 425 | private: |
| 426 | enum |
| 427 | { |
| 428 | // Constants for obtaining link quality from link margin: |
| 429 | |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 430 | kThreshold3 = 20, ///< Link margin threshold for quality 3 link. |
| 431 | kThreshold2 = 10, ///< Link margin threshold for quality 2 link. |
| 432 | kThreshold1 = 2, ///< Link margin threshold for quality 1 link. |
| 433 | kHysteresisThreshold = 2, ///< Link margin hysteresis threshold. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 434 | |
rongli | 502c6ca | 2017-07-12 13:38:49 +0800 | [diff] [blame] | 435 | // constants for test: |
| 436 | |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 437 | kLinkQuality3LinkMargin = 50, ///< link margin for Link Quality 3 (21 - 255) |
| 438 | kLinkQuality2LinkMargin = 15, ///< link margin for Link Quality 3 (21 - 255) |
| 439 | kLinkQuality1LinkMargin = 5, ///< link margin for Link Quality 3 (21 - 255) |
| 440 | kLinkQuality0LinkMargin = 0, ///< link margin for Link Quality 3 (21 - 255) |
rongli | 502c6ca | 2017-07-12 13:38:49 +0800 | [diff] [blame] | 441 | |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 442 | kNoLinkQuality = 0xff, // Used to indicate that there is no previous/last link quality. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 443 | }; |
| 444 | |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 445 | void SetLinkQuality(uint8_t aLinkQuality) { mLinkQuality = aLinkQuality; } |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 446 | |
| 447 | /* Static private method to calculate the link quality from a given link margin while taking into account the last |
| 448 | * link quality value and adding the hysteresis value to the thresholds. If there is no previous value for link |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 449 | * quality, the constant kNoLinkQuality should be passed as the second argument. |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 450 | * |
| 451 | */ |
| 452 | static uint8_t CalculateLinkQuality(uint8_t aLinkMargin, uint8_t aLastLinkQuality); |
| 453 | |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 454 | RssAverager mRssAverager; |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 455 | uint8_t mLinkQuality; |
Abtin Keshavarzian | 92b0ddc | 2017-07-11 09:10:54 -0700 | [diff] [blame] | 456 | int8_t mLastRss; |
Abtin Keshavarzian | 68a605f | 2018-01-31 12:40:23 -0800 | [diff] [blame] | 457 | #if OPENTHREAD_CONFIG_ENABLE_TX_ERROR_RATE_TRACKING |
| 458 | SuccessRateTracker mFrameErrorRate; |
| 459 | SuccessRateTracker mMessageErrorRate; |
| 460 | #endif |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | /** |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 464 | * @} |
| 465 | */ |
| 466 | |
Jonathan Hui | 69d98d4 | 2018-02-06 14:14:57 -0800 | [diff] [blame] | 467 | } // namespace ot |
Abtin Keshavarzian | cb33898 | 2016-06-27 14:15:11 -0700 | [diff] [blame] | 468 | |
| 469 | #endif // LINK_QUALITY_HPP_ |