Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 1 | #pragma once |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 2 | #include "tach_sensor.hpp" |
| 3 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 4 | #include <memory> |
| 5 | |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 6 | namespace phosphor |
| 7 | { |
| 8 | namespace fan |
| 9 | { |
| 10 | namespace trust |
| 11 | { |
| 12 | |
Matthew Barth | 6f31d19 | 2018-01-30 13:06:27 -0600 | [diff] [blame] | 13 | constexpr auto sensorName = 0; |
| 14 | constexpr auto inTrust = 1; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 15 | using GroupDefinition = std::tuple<std::string, bool>; |
Matthew Barth | 6f31d19 | 2018-01-30 13:06:27 -0600 | [diff] [blame] | 16 | |
Matthew Barth | a1c8832 | 2018-02-16 12:38:27 -0600 | [diff] [blame] | 17 | struct GroupSensor |
| 18 | { |
| 19 | std::shared_ptr<monitor::TachSensor> sensor; |
| 20 | bool inTrust; |
| 21 | }; |
| 22 | |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 23 | /** |
| 24 | * @class Group |
| 25 | * |
| 26 | * An abstract sensor trust group base class. |
| 27 | * |
| 28 | * Supports the ability to know if a fan speed sensor value can |
| 29 | * be trusted or not, where if it isn't trusted then it shouldn't |
| 30 | * be used to determine if the fan is faulted or not. |
| 31 | * |
| 32 | * It's a group in that there can be multiple sensors in the group |
| 33 | * and the trust of all sensors depends on something about those sensors. |
| 34 | * For example, if all sensors in the group report a speed of zero, |
| 35 | * then no sensor in the group is trusted. All sensors in the group |
| 36 | * have the same trust value. |
| 37 | * |
| 38 | * Trust is calculated when checkTrust() is called after a group |
| 39 | * sensor's tach value changes. |
| 40 | * |
| 41 | * A derived class must override checkGroupTrust(). |
| 42 | */ |
| 43 | class Group |
| 44 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 45 | public: |
| 46 | Group() = delete; |
| 47 | virtual ~Group() = default; |
| 48 | Group(const Group&) = delete; |
| 49 | Group& operator=(const Group&) = delete; |
| 50 | Group(Group&&) = default; |
| 51 | Group& operator=(Group&&) = default; |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 52 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 53 | /** |
| 54 | * Constructor |
| 55 | * |
| 56 | * @param[in] names - the names and inclusion of sensors in the group |
| 57 | */ |
| 58 | explicit Group(const std::vector<GroupDefinition>& names) : _names(names) |
| 59 | {} |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 60 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 61 | /** |
| 62 | * Used to register a TachSensor object with the group. |
| 63 | * It's only added to the group if the sensor's name is |
| 64 | * in the group's list of names. |
| 65 | * |
| 66 | * @param[in] sensor - the TachSensor to register |
| 67 | */ |
| 68 | void registerSensor(std::shared_ptr<monitor::TachSensor>& sensor) |
| 69 | { |
| 70 | auto found = std::find_if( |
| 71 | _names.begin(), _names.end(), [&sensor](const auto& name) { |
| 72 | return monitor::FAN_SENSOR_PATH + std::get<sensorName>(name) == |
| 73 | sensor->name(); |
| 74 | }); |
| 75 | |
| 76 | if (found != _names.end()) |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 77 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 78 | _sensors.push_back({sensor, std::get<inTrust>(*found)}); |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 79 | } |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 80 | } |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 81 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 82 | /** |
| 83 | * Says if a sensor belongs to the group. |
| 84 | * |
| 85 | * After all sensors have registered, this can be |
| 86 | * used to say if a TachSensor is in the group. |
| 87 | * |
| 88 | * @param[in] sensor - the TachSensor object |
| 89 | */ |
| 90 | bool inGroup(const monitor::TachSensor& sensor) |
| 91 | { |
| 92 | return (std::find_if(_sensors.begin(), _sensors.end(), |
| 93 | [&sensor](const auto& s) { |
| 94 | return sensor.name() == s.sensor->name(); |
| 95 | }) != _sensors.end()); |
| 96 | } |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 97 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 98 | /** |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame^] | 99 | * Cancels monitoring on all sensors in the group. |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 100 | * |
| 101 | * Called when the group just changed to not trusted, |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame^] | 102 | * so that its sensors' monitoring method does not |
| 103 | * cause them to be considered faulted. |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 104 | */ |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame^] | 105 | void cancelMonitoring() |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 106 | { |
| 107 | std::for_each(_sensors.begin(), _sensors.end(), |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame^] | 108 | [](const auto& s) { s.sensor->resetMethod(); }); |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame^] | 112 | * Starts monitoring on all sensors in the group by processing their current |
| 113 | * state |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 114 | * |
| 115 | * Called when the group just changed to trusted. |
| 116 | */ |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame^] | 117 | void startMonitoring() |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 118 | { |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame^] | 119 | std::for_each(_sensors.begin(), _sensors.end(), |
| 120 | [](const auto& s) { s.sensor->processState(); }); |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 121 | } |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 122 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 123 | /** |
| 124 | * Determines the trust for this group based on this |
| 125 | * sensor's latest status. |
| 126 | * |
| 127 | * Calls the derived class's checkGroupTrust function |
| 128 | * and updates the class with the results. |
| 129 | * |
| 130 | * If this is called with a sensor not in the group, |
| 131 | * it will be considered trusted. |
| 132 | * |
| 133 | * @param[in] sensor - TachSensor object |
| 134 | * |
| 135 | * @return tuple<bool, bool> - |
| 136 | * field 0 - the trust value |
| 137 | * field 1 - if that trust value changed since last call |
| 138 | * to checkTrust |
| 139 | */ |
| 140 | auto checkTrust(const monitor::TachSensor& sensor) |
| 141 | { |
| 142 | if (inGroup(sensor)) |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 143 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 144 | auto trust = checkGroupTrust(); |
| 145 | |
| 146 | setTrust(trust); |
| 147 | |
| 148 | return std::tuple<bool, bool>(_trusted, _stateChange); |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 149 | } |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 150 | return std::tuple<bool, bool>(true, false); |
| 151 | } |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 152 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 153 | /** |
| 154 | * Says if all sensors in the group are currently trusted, |
| 155 | * as determined by the last call to checkTrust(). |
| 156 | * |
| 157 | * @return bool - if the group's sensors are trusted or not |
| 158 | */ |
| 159 | inline auto getTrust() const |
| 160 | { |
| 161 | return _trusted; |
| 162 | } |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 163 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 164 | /** |
| 165 | * Says if the trust value changed in the last call to |
| 166 | * checkTrust() |
| 167 | * |
| 168 | * @return bool - if the trust changed or not |
| 169 | */ |
| 170 | inline auto trustChanged() const |
| 171 | { |
| 172 | return _stateChange; |
| 173 | } |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 174 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 175 | protected: |
| 176 | /** |
| 177 | * The sensor objects and their trust inclusion in the group. |
| 178 | * |
| 179 | * Added by registerSensor(). |
| 180 | */ |
| 181 | std::vector<GroupSensor> _sensors; |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 182 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 183 | private: |
| 184 | /** |
| 185 | * Checks if the group's sensors are trusted. |
| 186 | * |
| 187 | * The derived class must override this function |
| 188 | * to provide custom functionality. |
| 189 | * |
| 190 | * @return bool - if group is trusted or not |
| 191 | */ |
| 192 | virtual bool checkGroupTrust() = 0; |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 193 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 194 | /** |
| 195 | * Sets the trust value on the object. |
| 196 | * |
| 197 | * @param[in] trust - the new trust value |
| 198 | */ |
| 199 | inline void setTrust(bool trust) |
| 200 | { |
| 201 | _stateChange = (trust != _trusted); |
| 202 | _trusted = trust; |
| 203 | } |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 204 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 205 | /** |
| 206 | * The current trust state of the group |
| 207 | */ |
| 208 | bool _trusted = true; |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 209 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 210 | /** |
| 211 | * If the trust value changed in the last call to checkTrust |
| 212 | */ |
| 213 | bool _stateChange = false; |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 214 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 215 | /** |
| 216 | * The names of the sensors and whether it is included in |
| 217 | * determining trust for this group |
| 218 | */ |
| 219 | const std::vector<GroupDefinition> _names; |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame^] | 220 | }; // namespace trust |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 221 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 222 | } // namespace trust |
| 223 | } // namespace fan |
| 224 | } // namespace phosphor |