Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 16 | #include "config.h" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 17 | |
| 18 | #include "mainloop.hpp" |
| 19 | |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 20 | #include "env.hpp" |
| 21 | #include "fan_pwm.hpp" |
| 22 | #include "fan_speed.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 23 | #include "hwmon.hpp" |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 24 | #include "hwmonio.hpp" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 25 | #include "sensor.hpp" |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 26 | #include "sensorset.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 27 | #include "sysfs.hpp" |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 28 | #include "targets.hpp" |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 29 | #include "thresholds.hpp" |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 30 | #include "util.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 31 | |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 32 | #include <phosphor-logging/elog-errors.hpp> |
| 33 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 34 | |
William A. Kennington III | 0e74975 | 2018-11-06 15:25:41 -0800 | [diff] [blame] | 35 | #include <cassert> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 36 | #include <cstdlib> |
Patrick Williams | 6412993 | 2024-02-13 21:10:17 -0600 | [diff] [blame] | 37 | #include <format> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 38 | #include <functional> |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 39 | #include <future> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 40 | #include <iostream> |
| 41 | #include <memory> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 42 | #include <sstream> |
| 43 | #include <string> |
| 44 | #include <unordered_set> |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 45 | |
| 46 | using namespace phosphor::logging; |
| 47 | |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 48 | // Initialization for Warning Objects |
| 49 | decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo = |
| 50 | &WarningObject::warningLow; |
| 51 | decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi = |
| 52 | &WarningObject::warningHigh; |
| 53 | decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo = |
| 54 | &WarningObject::warningLow; |
| 55 | decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi = |
| 56 | &WarningObject::warningHigh; |
Patrick Williams | 3a81614 | 2021-10-06 15:36:42 -0500 | [diff] [blame] | 57 | decltype(Thresholds<WarningObject>::alarmLo) |
| 58 | Thresholds<WarningObject>::alarmLo = &WarningObject::warningAlarmLow; |
| 59 | decltype(Thresholds<WarningObject>::alarmHi) |
| 60 | Thresholds<WarningObject>::alarmHi = &WarningObject::warningAlarmHigh; |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 61 | decltype(Thresholds<WarningObject>::getAlarmLow) |
| 62 | Thresholds<WarningObject>::getAlarmLow = &WarningObject::warningAlarmLow; |
| 63 | decltype(Thresholds<WarningObject>::getAlarmHigh) |
| 64 | Thresholds<WarningObject>::getAlarmHigh = &WarningObject::warningAlarmHigh; |
| 65 | decltype(Thresholds<WarningObject>::assertLowSignal) |
| 66 | Thresholds<WarningObject>::assertLowSignal = |
| 67 | &WarningObject::warningLowAlarmAsserted; |
| 68 | decltype(Thresholds<WarningObject>::assertHighSignal) |
| 69 | Thresholds<WarningObject>::assertHighSignal = |
| 70 | &WarningObject::warningHighAlarmAsserted; |
| 71 | decltype(Thresholds<WarningObject>::deassertLowSignal) |
| 72 | Thresholds<WarningObject>::deassertLowSignal = |
| 73 | &WarningObject::warningLowAlarmDeasserted; |
| 74 | decltype(Thresholds<WarningObject>::deassertHighSignal) |
| 75 | Thresholds<WarningObject>::deassertHighSignal = |
| 76 | &WarningObject::warningHighAlarmDeasserted; |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 77 | |
| 78 | // Initialization for Critical Objects |
| 79 | decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo = |
| 80 | &CriticalObject::criticalLow; |
| 81 | decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi = |
| 82 | &CriticalObject::criticalHigh; |
| 83 | decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo = |
| 84 | &CriticalObject::criticalLow; |
| 85 | decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi = |
| 86 | &CriticalObject::criticalHigh; |
Patrick Williams | 3a81614 | 2021-10-06 15:36:42 -0500 | [diff] [blame] | 87 | decltype(Thresholds<CriticalObject>::alarmLo) |
| 88 | Thresholds<CriticalObject>::alarmLo = &CriticalObject::criticalAlarmLow; |
| 89 | decltype(Thresholds<CriticalObject>::alarmHi) |
| 90 | Thresholds<CriticalObject>::alarmHi = &CriticalObject::criticalAlarmHigh; |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 91 | decltype(Thresholds<CriticalObject>::getAlarmLow) |
| 92 | Thresholds<CriticalObject>::getAlarmLow = &CriticalObject::criticalAlarmLow; |
| 93 | decltype(Thresholds<CriticalObject>::getAlarmHigh) |
| 94 | Thresholds<CriticalObject>::getAlarmHigh = |
| 95 | &CriticalObject::criticalAlarmHigh; |
| 96 | decltype(Thresholds<CriticalObject>::assertLowSignal) |
| 97 | Thresholds<CriticalObject>::assertLowSignal = |
| 98 | &CriticalObject::criticalLowAlarmAsserted; |
| 99 | decltype(Thresholds<CriticalObject>::assertHighSignal) |
| 100 | Thresholds<CriticalObject>::assertHighSignal = |
| 101 | &CriticalObject::criticalHighAlarmAsserted; |
| 102 | decltype(Thresholds<CriticalObject>::deassertLowSignal) |
| 103 | Thresholds<CriticalObject>::deassertLowSignal = |
| 104 | &CriticalObject::criticalLowAlarmDeasserted; |
| 105 | decltype(Thresholds<CriticalObject>::deassertHighSignal) |
| 106 | Thresholds<CriticalObject>::deassertHighSignal = |
| 107 | &CriticalObject::criticalHighAlarmDeasserted; |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 108 | |
Matt Spinler | ecac0ae | 2020-07-08 13:09:08 -0500 | [diff] [blame] | 109 | void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value) |
Patrick Venture | feb744a | 2019-06-26 19:07:48 -0700 | [diff] [blame] | 110 | { |
| 111 | for (auto& iface : ifaces) |
| 112 | { |
| 113 | switch (iface.first) |
| 114 | { |
Patrick Williams | c8e818d | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 115 | // clang-format off |
Patrick Venture | feb744a | 2019-06-26 19:07:48 -0700 | [diff] [blame] | 116 | case InterfaceType::VALUE: |
| 117 | { |
| 118 | auto& valueIface = |
| 119 | std::any_cast<std::shared_ptr<ValueObject>&>(iface.second); |
| 120 | valueIface->value(value); |
| 121 | } |
| 122 | break; |
Kun Yi | 94a04c4 | 2019-08-21 09:43:20 -0700 | [diff] [blame] | 123 | // clang-format on |
Patrick Venture | feb744a | 2019-06-26 19:07:48 -0700 | [diff] [blame] | 124 | case InterfaceType::WARN: |
| 125 | checkThresholds<WarningObject>(iface.second, value); |
| 126 | break; |
| 127 | case InterfaceType::CRIT: |
| 128 | checkThresholds<CriticalObject>(iface.second, value); |
| 129 | break; |
| 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 136 | std::string MainLoop::getID(SensorSet::container_t::const_reference sensor) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 137 | { |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 138 | std::string id; |
| 139 | |
| 140 | /* |
| 141 | * Check if the value of the MODE_<item><X> env variable for the sensor |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 142 | * is set. If it is, then read the from the <item><X>_<mode> |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 143 | * file. The name of the DBUS object would be the value of the env |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 144 | * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 145 | * doesn't exist, then the name of DBUS object is the value of the env |
| 146 | * variable LABEL_<item><X>. |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 147 | * |
| 148 | * For example, if MODE_temp1 = "label", then code reads the temp1_label |
| 149 | * file. If it has a 5 in it, then it will use the following entry to |
| 150 | * name the object: LABEL_temp5 = "My DBus object name". |
| 151 | * |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 152 | */ |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 153 | auto mode = env::getEnv("MODE", sensor.first); |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 154 | if (!mode.empty()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 155 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 156 | id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode, |
| 157 | sensor.first); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 158 | |
| 159 | if (id.empty()) |
| 160 | { |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 161 | return id; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | // Use the ID we looked up above if there was one, |
| 166 | // otherwise use the standard one. |
| 167 | id = (id.empty()) ? sensor.first.second : id; |
| 168 | |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 169 | return id; |
| 170 | } |
| 171 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 172 | SensorIdentifiers |
| 173 | MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor) |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 174 | { |
| 175 | std::string id = getID(sensor); |
| 176 | std::string label; |
George Liu | c9d6161 | 2022-10-12 14:31:39 +0800 | [diff] [blame] | 177 | std::string accuracy; |
Lakshmi Yadlapati | 47fb49a | 2023-10-19 14:47:08 -0500 | [diff] [blame] | 178 | std::string priority; |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 179 | |
| 180 | if (!id.empty()) |
| 181 | { |
| 182 | // Ignore inputs without a label. |
| 183 | label = env::getEnv("LABEL", sensor.first.first, id); |
George Liu | c9d6161 | 2022-10-12 14:31:39 +0800 | [diff] [blame] | 184 | accuracy = env::getEnv("ACCURACY", sensor.first.first, id); |
Lakshmi Yadlapati | 47fb49a | 2023-10-19 14:47:08 -0500 | [diff] [blame] | 185 | priority = env::getEnv("PRIORITY", sensor.first.first, id); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Lakshmi Yadlapati | 47fb49a | 2023-10-19 14:47:08 -0500 | [diff] [blame] | 188 | return std::make_tuple(std::move(id), std::move(label), std::move(accuracy), |
| 189 | std::move(priority)); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Reads the environment parameters of a sensor and creates an object with |
Manojkiran Eda | 30cab62 | 2024-06-17 14:50:58 +0530 | [diff] [blame] | 194 | * at least the `Value` interface, otherwise returns without creating the |
| 195 | * object. If the `Value` interface is successfully created, by reading the |
| 196 | * sensor's corresponding sysfs file's value, the additional interfaces for |
| 197 | * the sensor are created and the InterfacesAdded signal is emitted. The |
| 198 | * object's state data is then returned for sensor state monitoring within |
| 199 | * the main loop. |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 200 | */ |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 201 | std::optional<ObjectStateData> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 202 | MainLoop::getObject(SensorSet::container_t::const_reference sensor) |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 203 | { |
| 204 | auto properties = getIdentifiers(sensor); |
| 205 | if (std::get<sensorID>(properties).empty() || |
| 206 | std::get<sensorLabel>(properties).empty()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 207 | { |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 208 | return {}; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 209 | } |
| 210 | |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 211 | hwmon::Attributes attrs; |
| 212 | if (!hwmon::getAttributes(sensor.first.first, attrs)) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 213 | { |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 214 | return {}; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 217 | const auto& [sensorSetKey, sensorAttrs] = sensor; |
| 218 | const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey; |
| 219 | |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 220 | /* Note: The sensor objects all share the same ioAccess object. */ |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 221 | auto sensorObj = |
| 222 | std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess, _devPath); |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 223 | |
Matthew Barth | b798527 | 2018-04-17 10:50:36 -0500 | [diff] [blame] | 224 | // Get list of return codes for removing sensors on device |
| 225 | auto devRmRCs = env::getEnv("REMOVERCS"); |
| 226 | // Add sensor removal return codes defined at the device level |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 227 | sensorObj->addRemoveRCs(devRmRCs); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 228 | |
| 229 | std::string objectPath{_root}; |
| 230 | objectPath.append(1, '/'); |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 231 | objectPath.append(hwmon::getNamespace(attrs)); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 232 | objectPath.append(1, '/'); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 233 | objectPath.append(std::get<sensorLabel>(properties)); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 234 | |
Patrick Venture | 6206723 | 2019-06-19 17:39:33 -0700 | [diff] [blame] | 235 | ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap()); |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 236 | RetryIO retryIO(hwmonio::retries, hwmonio::delay); |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 237 | if (_rmSensors.find(sensorSetKey) != _rmSensors.end()) |
Matthew Barth | d4beecf | 2018-04-03 15:50:22 -0500 | [diff] [blame] | 238 | { |
| 239 | // When adding a sensor that was purposely removed, |
| 240 | // don't retry on errors when reading its value |
| 241 | std::get<size_t>(retryIO) = 0; |
| 242 | } |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 243 | auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 244 | try |
| 245 | { |
George Liu | c9d6161 | 2022-10-12 14:31:39 +0800 | [diff] [blame] | 246 | // Add accuracy interface |
| 247 | auto accuracyStr = std::get<sensorAccuracy>(properties); |
| 248 | try |
| 249 | { |
| 250 | if (!accuracyStr.empty()) |
| 251 | { |
| 252 | auto accuracy = stod(accuracyStr); |
| 253 | sensorObj->addAccuracy(info, accuracy); |
| 254 | } |
| 255 | } |
| 256 | catch (const std::invalid_argument&) |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 257 | {} |
George Liu | c9d6161 | 2022-10-12 14:31:39 +0800 | [diff] [blame] | 258 | |
Lakshmi Yadlapati | 47fb49a | 2023-10-19 14:47:08 -0500 | [diff] [blame] | 259 | // Add priority interface |
| 260 | auto priorityStr = std::get<sensorPriority>(properties); |
| 261 | try |
| 262 | { |
| 263 | if (!priorityStr.empty()) |
| 264 | { |
| 265 | auto priority = std::stoul(priorityStr); |
| 266 | sensorObj->addPriority(info, priority); |
| 267 | } |
| 268 | } |
| 269 | catch (const std::invalid_argument&) |
| 270 | {} |
| 271 | |
Matthew Barth | ca44c2e | 2018-04-24 15:33:25 -0500 | [diff] [blame] | 272 | // Add status interface based on _fault file being present |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 273 | sensorObj->addStatus(info); |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 274 | valueInterface = sensorObj->addValue(retryIO, info, _timedoutMap); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 275 | } |
| 276 | catch (const std::system_error& e) |
| 277 | { |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 278 | auto file = |
| 279 | sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType, |
| 280 | sensorSysfsNum, hwmon::entry::cinput); |
Patrick Venture | 0892c3f | 2019-06-27 14:24:03 -0700 | [diff] [blame] | 281 | |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 282 | // Check sensorAdjusts for sensor removal RCs |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 283 | auto& sAdjusts = sensorObj->getAdjusts(); |
| 284 | if (sAdjusts.rmRCs.count(e.code().value()) > 0) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 285 | { |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 286 | // Return code found in sensor return code removal list |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 287 | if (_rmSensors.find(sensorSetKey) == _rmSensors.end()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 288 | { |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 289 | // Trace for sensor not already removed from dbus |
| 290 | log<level::INFO>("Sensor not added to dbus for read fail", |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 291 | entry("FILE=%s", file.c_str()), |
| 292 | entry("RC=%d", e.code().value())); |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 293 | _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 294 | } |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 295 | return {}; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 296 | } |
Patrick Venture | 0892c3f | 2019-06-27 14:24:03 -0700 | [diff] [blame] | 297 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 298 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 299 | report<ReadFailure>( |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 300 | xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO( |
| 301 | e.code().value()), |
| 302 | xyz::openbmc_project::Sensor::Device::ReadFailure:: |
| 303 | CALLOUT_DEVICE_PATH(_devPath.c_str())); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 304 | |
Patrick Williams | 6412993 | 2024-02-13 21:10:17 -0600 | [diff] [blame] | 305 | log<level::INFO>(std::format("Failing sysfs file: {} errno: {}", file, |
Matt Spinler | 6a391de | 2020-07-08 13:03:10 -0500 | [diff] [blame] | 306 | e.code().value()) |
| 307 | .c_str()); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 308 | exit(EXIT_FAILURE); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 309 | } |
| 310 | auto sensorValue = valueInterface->value(); |
Matt Spinler | ecac0ae | 2020-07-08 13:09:08 -0500 | [diff] [blame] | 311 | int64_t scale = sensorObj->getScale(); |
| 312 | |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 313 | addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties), |
| 314 | sensorValue, info, scale); |
| 315 | addThreshold<CriticalObject>(sensorSysfsType, |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 316 | std::get<sensorID>(properties), sensorValue, |
| 317 | info, scale); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 318 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 319 | auto target = |
| 320 | addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath, info); |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 321 | if (target) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 322 | { |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 323 | target->enable(); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 324 | } |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 325 | addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 326 | |
| 327 | // All the interfaces have been created. Go ahead |
| 328 | // and emit InterfacesAdded. |
| 329 | valueInterface->emit_object_added(); |
| 330 | |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 331 | // Save sensor object specifications |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 332 | _sensorObjects[sensorSetKey] = std::move(sensorObj); |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 333 | |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 334 | return std::make_pair(std::move(std::get<sensorLabel>(properties)), |
| 335 | std::move(info)); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Patrick Williams | ad6043f | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 338 | MainLoop::MainLoop(sdbusplus::bus_t&& bus, const std::string& param, |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 339 | const std::string& path, const std::string& devPath, |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 340 | const char* prefix, const char* root, |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 341 | const std::string& instanceId, |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 342 | const hwmonio::HwmonIOInterface* ioIntf) : |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 343 | _bus(std::move(bus)), _manager(_bus, root), _pathParam(param), _hwmonRoot(), |
| 344 | _instance(), _devPath(devPath), _prefix(prefix), _root(root), _state(), |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 345 | _instanceId(instanceId), _ioAccess(ioIntf), |
| 346 | _event(sdeventplus::Event::get_default()), |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 347 | _timer(_event, std::bind(&MainLoop::read, this)) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 348 | { |
Patrick Venture | 73a50c7 | 2018-04-17 15:19:03 -0700 | [diff] [blame] | 349 | // Strip off any trailing slashes. |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 350 | std::string p = path; |
| 351 | while (!p.empty() && p.back() == '/') |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 352 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 353 | p.pop_back(); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 354 | } |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 355 | |
Patrick Venture | 73a50c7 | 2018-04-17 15:19:03 -0700 | [diff] [blame] | 356 | // Given the furthest right /, set instance to |
| 357 | // the basename, and hwmonRoot to the leading path. |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 358 | auto n = p.rfind('/'); |
| 359 | if (n != std::string::npos) |
| 360 | { |
| 361 | _instance.assign(p.substr(n + 1)); |
| 362 | _hwmonRoot.assign(p.substr(0, n)); |
| 363 | } |
| 364 | |
| 365 | assert(!_instance.empty()); |
| 366 | assert(!_hwmonRoot.empty()); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void MainLoop::shutdown() noexcept |
| 370 | { |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 371 | _event.exit(0); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 375 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 376 | init(); |
| 377 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 378 | std::function<void()> callback(std::bind(&MainLoop::read, this)); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 379 | try |
| 380 | { |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 381 | _timer.restart(std::chrono::microseconds(_interval)); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 382 | |
| 383 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
| 384 | |
| 385 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 386 | // for new entries. |
| 387 | |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 388 | _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT); |
| 389 | _event.loop(); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 390 | } |
William A. Kennington III | 0fe4cb3 | 2018-10-18 19:19:58 -0700 | [diff] [blame] | 391 | catch (const std::exception& e) |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 392 | { |
| 393 | log<level::ERR>("Error in sysfs polling loop", |
| 394 | entry("ERROR=%s", e.what())); |
| 395 | throw; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void MainLoop::init() |
| 400 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 401 | // Check sysfs for available sensors. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 402 | auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 403 | |
Patrick Venture | db7ecb6 | 2018-10-23 19:42:23 -0700 | [diff] [blame] | 404 | for (const auto& i : *sensors) |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 405 | { |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 406 | auto object = getObject(i); |
| 407 | if (object) |
| 408 | { |
| 409 | // Construct the SensorSet value |
| 410 | // std::tuple<SensorSet::mapped_type, |
| 411 | // std::string(Sensor Label), |
| 412 | // ObjectInfo> |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 413 | auto value = |
| 414 | std::make_tuple(std::move(i.second), std::move((*object).first), |
| 415 | std::move((*object).second)); |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 416 | |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 417 | _state[std::move(i.first)] = std::move(value); |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 418 | } |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 419 | |
| 420 | // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>> |
| 421 | if ((i.first.first == hwmon::type::power) && |
| 422 | (phosphor::utility::isAverageEnvSet(i.first))) |
| 423 | { |
| 424 | _average.setAverageValue(i.first, std::make_pair(0, 0)); |
| 425 | } |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 428 | /* If there are no sensors specified by labels, exit. */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 429 | if (0 == _state.size()) |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 430 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 431 | exit(0); |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 434 | { |
Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame] | 435 | std::stringstream ss; |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 436 | std::string id = _instanceId; |
| 437 | if (id.empty()) |
| 438 | { |
| 439 | id = |
| 440 | std::to_string(std::hash<std::string>{}(_devPath + _pathParam)); |
| 441 | } |
| 442 | ss << _prefix << "-" << id << ".Hwmon1"; |
Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame] | 443 | |
| 444 | _bus.request_name(ss.str().c_str()); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 445 | } |
| 446 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 447 | { |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 448 | auto interval = env::getEnv("INTERVAL"); |
| 449 | if (!interval.empty()) |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 450 | { |
Patrick Venture | 50cf1c5 | 2018-04-18 09:21:41 -0700 | [diff] [blame] | 451 | _interval = std::strtoull(interval.c_str(), NULL, 10); |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 454 | } |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 455 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 456 | void MainLoop::read() |
| 457 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 458 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 459 | // ensure the objects all exist? |
| 460 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 461 | // Iterate through all the sensors. |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 462 | for (auto& [sensorSetKey, sensorStateTuple] : _state) |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 463 | { |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 464 | const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey; |
| 465 | auto& [attrs, unused, objInfo] = sensorStateTuple; |
| 466 | |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 467 | if (attrs.find(hwmon::entry::input) == attrs.end()) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 468 | { |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 469 | continue; |
| 470 | } |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 471 | |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 472 | // Read value from sensor. |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 473 | std::string input = hwmon::entry::input; |
| 474 | if (sensorSysfsType == hwmon::type::pwm) |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 475 | { |
| 476 | input = ""; |
| 477 | } |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 478 | // If type is power and AVERAGE_power* is true in env, use average |
| 479 | // instead of input |
| 480 | else if ((sensorSysfsType == hwmon::type::power) && |
| 481 | (phosphor::utility::isAverageEnvSet(sensorSetKey))) |
| 482 | { |
| 483 | input = hwmon::entry::average; |
| 484 | } |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 485 | |
Matt Spinler | ecac0ae | 2020-07-08 13:09:08 -0500 | [diff] [blame] | 486 | SensorValueType value; |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 487 | auto& obj = std::get<InterfaceMap>(objInfo); |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 488 | std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey]; |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 489 | |
| 490 | auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>( |
| 491 | obj[InterfaceType::STATUS]); |
| 492 | // As long as addStatus is called before addValue, statusIface |
| 493 | // should never be nullptr. |
| 494 | assert(statusIface); |
| 495 | |
| 496 | try |
| 497 | { |
| 498 | if (sensor->hasFaultFile()) |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 499 | { |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 500 | auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum, |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 501 | hwmon::entry::fault, |
| 502 | hwmonio::retries, hwmonio::delay); |
| 503 | // Skip reading from a sensor with a valid fault file |
| 504 | // and set the functional property accordingly |
| 505 | if (!statusIface->functional((fault == 0) ? true : false)) |
Matthew Barth | 27c4a39 | 2018-04-25 14:38:51 -0500 | [diff] [blame] | 506 | { |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 507 | continue; |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 508 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 509 | } |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 510 | |
| 511 | { |
| 512 | // RAII object for GPIO unlock / lock |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 513 | auto locker = sensor::gpioUnlock(sensor->getGpio()); |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 514 | |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 515 | // For sensors with attribute ASYNC_READ_TIMEOUT, |
| 516 | // spawn a thread with timeout |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 517 | auto asyncReadTimeout = |
| 518 | env::getEnv("ASYNC_READ_TIMEOUT", sensorSetKey); |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 519 | if (!asyncReadTimeout.empty()) |
| 520 | { |
| 521 | std::chrono::milliseconds asyncTimeout{ |
| 522 | std::stoi(asyncReadTimeout)}; |
| 523 | value = sensor::asyncRead( |
| 524 | sensorSetKey, _ioAccess, asyncTimeout, _timedoutMap, |
| 525 | sensorSysfsType, sensorSysfsNum, input, |
| 526 | hwmonio::retries, hwmonio::delay); |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | // Retry for up to a second if device is busy |
| 531 | // or has a transient error. |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 532 | value = |
| 533 | _ioAccess->read(sensorSysfsType, sensorSysfsNum, input, |
| 534 | hwmonio::retries, hwmonio::delay); |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 537 | // Set functional property to true if we could read sensor |
| 538 | statusIface->functional(true); |
| 539 | |
| 540 | value = sensor->adjustValue(value); |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 541 | |
| 542 | if (input == hwmon::entry::average) |
| 543 | { |
| 544 | // Calculate the values of averageMap based on current |
| 545 | // average value, current average_interval value, previous |
| 546 | // average value, previous average_interval value |
| 547 | int64_t interval = |
| 548 | _ioAccess->read(sensorSysfsType, sensorSysfsNum, |
| 549 | hwmon::entry::caverage_interval, |
| 550 | hwmonio::retries, hwmonio::delay); |
| 551 | auto ret = _average.getAverageValue(sensorSetKey); |
| 552 | assert(ret); |
| 553 | |
| 554 | const auto& [preAverage, preInterval] = *ret; |
| 555 | |
| 556 | auto calValue = Average::calcAverage( |
| 557 | preAverage, preInterval, value, interval); |
| 558 | if (calValue) |
| 559 | { |
| 560 | // Update previous values in averageMap before the |
| 561 | // variable value is changed next |
| 562 | _average.setAverageValue( |
| 563 | sensorSetKey, std::make_pair(value, interval)); |
| 564 | // Update value to be calculated average |
| 565 | value = calValue.value(); |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | // the value of |
| 570 | // power*_average_interval is not changed yet, use the |
| 571 | // previous calculated average instead. So skip dbus |
| 572 | // update. |
| 573 | continue; |
| 574 | } |
| 575 | } |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | updateSensorInterfaces(obj, value); |
| 579 | } |
| 580 | catch (const std::system_error& e) |
| 581 | { |
Matt Spinler | d8cacfd | 2021-04-26 09:56:21 -0500 | [diff] [blame] | 582 | #if UPDATE_FUNCTIONAL_ON_FAIL |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 583 | // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was |
| 584 | // thrown, set the functional property to false. |
| 585 | // We cannot set this with the 'continue' in the lower block |
| 586 | // as the code may exit before reaching it. |
| 587 | statusIface->functional(false); |
| 588 | #endif |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 589 | auto file = sysfs::make_sysfs_path( |
| 590 | _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input); |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 591 | |
| 592 | // Check sensorAdjusts for sensor removal RCs |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 593 | auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts(); |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 594 | if (sAdjusts.rmRCs.count(e.code().value()) > 0) |
| 595 | { |
| 596 | // Return code found in sensor return code removal list |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 597 | if (_rmSensors.find(sensorSetKey) == _rmSensors.end()) |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 598 | { |
| 599 | // Trace for sensor not already removed from dbus |
| 600 | log<level::INFO>("Remove sensor from dbus for read fail", |
| 601 | entry("FILE=%s", file.c_str()), |
| 602 | entry("RC=%d", e.code().value())); |
| 603 | // Mark this sensor to be removed from dbus |
Kun Yi | 501ade2 | 2019-07-15 15:00:32 -0700 | [diff] [blame] | 604 | _rmSensors[sensorSetKey] = attrs; |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 605 | } |
| 606 | continue; |
| 607 | } |
Matt Spinler | d8cacfd | 2021-04-26 09:56:21 -0500 | [diff] [blame] | 608 | #if UPDATE_FUNCTIONAL_ON_FAIL |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 609 | // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set |
| 610 | continue; |
| 611 | #endif |
| 612 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device:: |
| 613 | Error; |
| 614 | report<ReadFailure>( |
| 615 | xyz::openbmc_project::Sensor::Device::ReadFailure:: |
| 616 | CALLOUT_ERRNO(e.code().value()), |
| 617 | xyz::openbmc_project::Sensor::Device::ReadFailure:: |
| 618 | CALLOUT_DEVICE_PATH(_devPath.c_str())); |
| 619 | |
Patrick Williams | 6412993 | 2024-02-13 21:10:17 -0600 | [diff] [blame] | 620 | log<level::INFO>(std::format("Failing sysfs file: {} errno: {}", |
Matt Spinler | 6a391de | 2020-07-08 13:03:10 -0500 | [diff] [blame] | 621 | file, e.code().value()) |
| 622 | .c_str()); |
Kun Yi | 553552c | 2019-07-15 22:14:21 -0700 | [diff] [blame] | 623 | |
| 624 | exit(EXIT_FAILURE); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 625 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 626 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 627 | |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 628 | removeSensors(); |
| 629 | |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 630 | addDroppedSensors(); |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | void MainLoop::removeSensors() |
| 634 | { |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 635 | // Remove any sensors marked for removal |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 636 | for (const auto& i : _rmSensors) |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 637 | { |
Matthew Barth | d0ce792 | 2019-06-06 09:23:37 -0500 | [diff] [blame] | 638 | // Remove sensor object from dbus using emit_object_removed() |
| 639 | auto& objInfo = std::get<ObjectInfo>(_state[i.first]); |
| 640 | auto& objPath = std::get<std::string>(objInfo); |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 641 | |
Matthew Barth | d0ce792 | 2019-06-06 09:23:37 -0500 | [diff] [blame] | 642 | _bus.emit_object_removed(objPath.c_str()); |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 643 | |
Matthew Barth | d0ce792 | 2019-06-06 09:23:37 -0500 | [diff] [blame] | 644 | // Erase sensor object info |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 645 | _state.erase(i.first); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 646 | } |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 647 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 648 | |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 649 | void MainLoop::addDroppedSensors() |
| 650 | { |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 651 | // Attempt to add any sensors that were removed |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 652 | auto it = _rmSensors.begin(); |
| 653 | while (it != _rmSensors.end()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 654 | { |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 655 | if (_state.find(it->first) == _state.end()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 656 | { |
| 657 | SensorSet::container_t::value_type ssValueType = |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 658 | std::make_pair(it->first, it->second); |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 659 | |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 660 | auto object = getObject(ssValueType); |
| 661 | if (object) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 662 | { |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 663 | // Construct the SensorSet value |
| 664 | // std::tuple<SensorSet::mapped_type, |
| 665 | // std::string(Sensor Label), |
| 666 | // ObjectInfo> |
| 667 | auto value = std::make_tuple(std::move(ssValueType.second), |
| 668 | std::move((*object).first), |
| 669 | std::move((*object).second)); |
| 670 | |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 671 | _state[std::move(ssValueType.first)] = std::move(value); |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 672 | |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 673 | std::string input = hwmon::entry::input; |
| 674 | // If type is power and AVERAGE_power* is true in env, use |
| 675 | // average instead of input |
| 676 | if ((it->first.first == hwmon::type::power) && |
| 677 | (phosphor::utility::isAverageEnvSet(it->first))) |
| 678 | { |
| 679 | input = hwmon::entry::average; |
| 680 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 681 | // Sensor object added, erase entry from removal list |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 682 | auto file = |
| 683 | sysfs::make_sysfs_path(_ioAccess->path(), it->first.first, |
| 684 | it->first.second, input); |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 685 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 686 | log<level::INFO>("Added sensor to dbus after successful read", |
| 687 | entry("FILE=%s", file.c_str())); |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 688 | |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 689 | it = _rmSensors.erase(it); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 690 | } |
| 691 | else |
| 692 | { |
| 693 | ++it; |
| 694 | } |
| 695 | } |
| 696 | else |
| 697 | { |
| 698 | // Sanity check to remove sensors that were re-added |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 699 | it = _rmSensors.erase(it); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 700 | } |
| 701 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 702 | } |