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