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 | */ |
Brad Bishop | 4d9a35b | 2017-11-14 22:33:03 -0500 | [diff] [blame] | 16 | #include <functional> |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <memory> |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 19 | #include <cstdlib> |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 20 | #include <string> |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame] | 21 | #include <unordered_set> |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 22 | |
| 23 | #include <phosphor-logging/elog-errors.hpp> |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 24 | #include "config.h" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 25 | #include "sensorset.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 26 | #include "hwmon.hpp" |
| 27 | #include "sysfs.hpp" |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 28 | #include "mainloop.hpp" |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 29 | #include "env.hpp" |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 30 | #include "thresholds.hpp" |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 31 | #include "targets.hpp" |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 32 | #include "fan_speed.hpp" |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 33 | #include "fan_pwm.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 34 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 35 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 36 | |
| 37 | using namespace phosphor::logging; |
| 38 | |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 39 | // Initialization for Warning Objects |
| 40 | decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo = |
| 41 | &WarningObject::warningLow; |
| 42 | decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi = |
| 43 | &WarningObject::warningHigh; |
| 44 | decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo = |
| 45 | &WarningObject::warningLow; |
| 46 | decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi = |
| 47 | &WarningObject::warningHigh; |
| 48 | decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo = |
| 49 | &WarningObject::warningAlarmLow; |
| 50 | decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi = |
| 51 | &WarningObject::warningAlarmHigh; |
| 52 | |
| 53 | // Initialization for Critical Objects |
| 54 | decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo = |
| 55 | &CriticalObject::criticalLow; |
| 56 | decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi = |
| 57 | &CriticalObject::criticalHigh; |
| 58 | decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo = |
| 59 | &CriticalObject::criticalLow; |
| 60 | decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi = |
| 61 | &CriticalObject::criticalHigh; |
| 62 | decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo = |
| 63 | &CriticalObject::criticalAlarmLow; |
| 64 | decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi = |
| 65 | &CriticalObject::criticalAlarmHigh; |
| 66 | |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 67 | // The gain and offset to adjust a value |
| 68 | struct valueAdjust |
| 69 | { |
| 70 | double gain = 1.0; |
| 71 | int offset = 0; |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame] | 72 | std::unordered_set<int> rmRCs; |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // Store the valueAdjust for sensors |
| 76 | std::map<SensorSet::key_type, valueAdjust> sensorAdjusts; |
| 77 | |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 78 | static constexpr auto typeAttrMap = |
| 79 | { |
| 80 | // 1 - hwmon class |
| 81 | // 2 - unit |
| 82 | // 3 - sysfs scaling factor |
| 83 | std::make_tuple( |
| 84 | hwmon::type::ctemp, |
| 85 | ValueInterface::Unit::DegreesC, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 86 | -3, |
| 87 | "temperature"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 88 | std::make_tuple( |
| 89 | hwmon::type::cfan, |
| 90 | ValueInterface::Unit::RPMS, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 91 | 0, |
| 92 | "fan_tach"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 93 | std::make_tuple( |
| 94 | hwmon::type::cvolt, |
| 95 | ValueInterface::Unit::Volts, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 96 | -3, |
| 97 | "voltage"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 98 | std::make_tuple( |
| 99 | hwmon::type::ccurr, |
| 100 | ValueInterface::Unit::Amperes, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 101 | -3, |
| 102 | "current"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 103 | std::make_tuple( |
| 104 | hwmon::type::cenergy, |
| 105 | ValueInterface::Unit::Joules, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 106 | -6, |
| 107 | "energy"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 108 | std::make_tuple( |
| 109 | hwmon::type::cpower, |
| 110 | ValueInterface::Unit::Watts, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 111 | -6, |
| 112 | "power"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | auto getHwmonType(decltype(typeAttrMap)::const_reference attrs) |
| 116 | { |
| 117 | return std::get<0>(attrs); |
| 118 | } |
| 119 | |
| 120 | auto getUnit(decltype(typeAttrMap)::const_reference attrs) |
| 121 | { |
| 122 | return std::get<1>(attrs); |
| 123 | } |
| 124 | |
| 125 | auto getScale(decltype(typeAttrMap)::const_reference attrs) |
| 126 | { |
| 127 | return std::get<2>(attrs); |
| 128 | } |
| 129 | |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 130 | auto getNamespace(decltype(typeAttrMap)::const_reference attrs) |
| 131 | { |
| 132 | return std::get<3>(attrs); |
| 133 | } |
| 134 | |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 135 | using AttributeIterator = decltype(*typeAttrMap.begin()); |
| 136 | using Attributes |
| 137 | = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type; |
| 138 | |
| 139 | auto getAttributes(const std::string& type, Attributes& attributes) |
| 140 | { |
| 141 | // *INDENT-OFF* |
| 142 | auto a = std::find_if( |
| 143 | typeAttrMap.begin(), |
| 144 | typeAttrMap.end(), |
| 145 | [&](const auto & e) |
| 146 | { |
| 147 | return type == getHwmonType(e); |
| 148 | }); |
| 149 | // *INDENT-ON* |
| 150 | |
| 151 | if (a == typeAttrMap.end()) |
| 152 | { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | attributes = *a; |
| 157 | return true; |
| 158 | } |
| 159 | |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame] | 160 | void addRemoveRCs(const SensorSet::key_type& sensor, |
| 161 | const std::string& rcList) |
| 162 | { |
| 163 | // Convert to a char* for strtok |
| 164 | std::vector<char> rmRCs(rcList.c_str(), |
| 165 | rcList.c_str() + rcList.size() + 1); |
| 166 | auto rmRC = strtok(&rmRCs[0], ", "); |
| 167 | while (rmRC != nullptr) |
| 168 | { |
| 169 | try |
| 170 | { |
| 171 | sensorAdjusts[sensor].rmRCs.insert(std::stoi(rmRC)); |
| 172 | } |
| 173 | catch (const std::logic_error& le) |
| 174 | { |
| 175 | // Unable to convert to int, continue to next token |
| 176 | std::string name = sensor.first + "_" + sensor.second; |
| 177 | log<level::INFO>("Unable to convert sensor removal return code", |
| 178 | entry("SENSOR=%s", name.c_str()), |
| 179 | entry("RC=%s", rmRC), |
| 180 | entry("EXCEPTION=%s", le.what())); |
| 181 | } |
| 182 | rmRC = strtok(nullptr, ", "); |
| 183 | } |
| 184 | } |
| 185 | |
Matt Spinler | fee106b | 2017-11-29 15:18:05 -0600 | [diff] [blame] | 186 | int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value) |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 187 | { |
Patrick Venture | c1cece7 | 2017-11-07 12:09:49 -0800 | [diff] [blame] | 188 | // Because read doesn't have an out pointer to store errors. |
| 189 | // let's assume negative values are errors if they have this |
| 190 | // set. |
| 191 | #ifdef NEGATIVE_ERRNO_ON_FAIL |
| 192 | if (value < 0) |
| 193 | { |
| 194 | return value; |
| 195 | } |
| 196 | #endif |
| 197 | |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 198 | const auto& it = sensorAdjusts.find(sensor); |
| 199 | if (it != sensorAdjusts.end()) |
| 200 | { |
| 201 | // Adjust based on gain and offset |
| 202 | value = static_cast<decltype(value)>( |
| 203 | static_cast<double>(value) * it->second.gain |
| 204 | + it->second.offset); |
| 205 | } |
| 206 | return value; |
| 207 | } |
| 208 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 209 | auto addValue(const SensorSet::key_type& sensor, |
Matthew Barth | d4beecf | 2018-04-03 15:50:22 -0500 | [diff] [blame] | 210 | const RetryIO& retryIO, |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 211 | sysfs::hwmonio::HwmonIO& ioAccess, |
Matthew Barth | a23babd | 2018-03-16 10:03:27 -0500 | [diff] [blame] | 212 | ObjectInfo& info, |
| 213 | bool isOCC = false) |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 214 | { |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 215 | static constexpr bool deferSignals = true; |
| 216 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 217 | // Get the initial value for the value interface. |
| 218 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 219 | auto& obj = std::get<Object>(info); |
| 220 | auto& objPath = std::get<std::string>(info); |
| 221 | |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame] | 222 | auto senRmRCs = getEnv("REMOVERCS", sensor); |
| 223 | if (!senRmRCs.empty()) |
| 224 | { |
| 225 | // Add sensor removal return codes defined per sensor |
| 226 | addRemoveRCs(sensor, senRmRCs); |
| 227 | } |
| 228 | |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 229 | // Retry for up to a second if device is busy |
| 230 | // or has a transient error. |
| 231 | int64_t val = ioAccess.read( |
| 232 | sensor.first, |
| 233 | sensor.second, |
| 234 | hwmon::entry::cinput, |
Matthew Barth | d4beecf | 2018-04-03 15:50:22 -0500 | [diff] [blame] | 235 | std::get<size_t>(retryIO), |
| 236 | std::get<std::chrono::milliseconds>(retryIO), |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 237 | isOCC); |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 238 | |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 239 | auto gain = getEnv("GAIN", sensor); |
| 240 | if (!gain.empty()) |
| 241 | { |
| 242 | sensorAdjusts[sensor].gain = std::stod(gain); |
| 243 | } |
| 244 | |
| 245 | auto offset = getEnv("OFFSET", sensor); |
| 246 | if (!offset.empty()) |
| 247 | { |
| 248 | sensorAdjusts[sensor].offset = std::stoi(offset); |
| 249 | } |
| 250 | |
| 251 | val = adjustValue(sensor, val); |
| 252 | |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 253 | auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 254 | iface->value(val); |
| 255 | |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 256 | Attributes attrs; |
| 257 | if (getAttributes(sensor.first, attrs)) |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 258 | { |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 259 | iface->unit(getUnit(attrs)); |
| 260 | iface->scale(getScale(attrs)); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 261 | } |
| 262 | |
James Feist | 7bd5fcf | 2018-01-22 16:14:28 -0800 | [diff] [blame] | 263 | auto maxValue = getEnv("MAXVALUE", sensor); |
| 264 | if(!maxValue.empty()) |
| 265 | { |
| 266 | iface->maxValue(std::stoll(maxValue)); |
| 267 | } |
| 268 | auto minValue = getEnv("MINVALUE", sensor); |
| 269 | if(!minValue.empty()) |
| 270 | { |
| 271 | iface->minValue(std::stoll(minValue)); |
| 272 | } |
| 273 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 274 | obj[InterfaceType::VALUE] = iface; |
| 275 | return iface; |
| 276 | } |
| 277 | |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 278 | /** |
| 279 | * Reads the environment parameters of a sensor and creates an object with |
| 280 | * atleast the `Value` interface, otherwise returns without creating the object. |
| 281 | * If the `Value` interface is successfully created, by reading the sensor's |
| 282 | * corresponding sysfs file's value, the additional interfaces for the sensor |
| 283 | * are created and the InterfacesAdded signal is emitted. The sensor is then |
| 284 | * moved to the list for sensor state monitoring within the main loop. |
| 285 | */ |
| 286 | void MainLoop::getObject(SensorSet::container_t::const_reference sensor) |
| 287 | { |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 288 | // Get list of return codes for removing sensors on device |
| 289 | std::string deviceRmRCs; |
| 290 | auto devRmRCs = getenv("REMOVERCS"); |
| 291 | if (devRmRCs) |
| 292 | { |
| 293 | deviceRmRCs.assign(devRmRCs); |
| 294 | } |
| 295 | |
| 296 | std::string label; |
| 297 | std::string id; |
| 298 | |
| 299 | /* |
| 300 | * Check if the value of the MODE_<item><X> env variable for the sensor |
| 301 | * is "label", then read the sensor number from the <item><X>_label |
| 302 | * file. The name of the DBUS object would be the value of the env |
| 303 | * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable |
| 304 | * doesn't exist, then the name of DBUS object is the value of the env |
| 305 | * variable LABEL_<item><X>. |
| 306 | */ |
| 307 | auto mode = getEnv("MODE", sensor.first); |
| 308 | if (!mode.compare(hwmon::entry::label)) |
| 309 | { |
| 310 | id = getIndirectID( |
| 311 | _hwmonRoot + '/' + _instance + '/', sensor.first); |
| 312 | |
| 313 | if (id.empty()) |
| 314 | { |
| 315 | return; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // Use the ID we looked up above if there was one, |
| 320 | // otherwise use the standard one. |
| 321 | id = (id.empty()) ? sensor.first.second : id; |
| 322 | |
| 323 | // Ignore inputs without a label. |
| 324 | label = getEnv("LABEL", sensor.first.first, id); |
| 325 | if (label.empty()) |
| 326 | { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | Attributes attrs; |
| 331 | if (!getAttributes(sensor.first.first, attrs)) |
| 332 | { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (!deviceRmRCs.empty()) |
| 337 | { |
| 338 | // Add sensor removal return codes defined at the device level |
| 339 | addRemoveRCs(sensor.first, deviceRmRCs); |
| 340 | } |
| 341 | |
| 342 | std::string objectPath{_root}; |
| 343 | objectPath.append(1, '/'); |
| 344 | objectPath.append(getNamespace(attrs)); |
| 345 | objectPath.append(1, '/'); |
| 346 | objectPath.append(label); |
| 347 | |
| 348 | ObjectInfo info(&_bus, std::move(objectPath), Object()); |
Matthew Barth | d4beecf | 2018-04-03 15:50:22 -0500 | [diff] [blame] | 349 | RetryIO retryIO(sysfs::hwmonio::retries, sysfs::hwmonio::delay); |
| 350 | if (rmSensors.find(sensor.first) != rmSensors.end()) |
| 351 | { |
| 352 | // When adding a sensor that was purposely removed, |
| 353 | // don't retry on errors when reading its value |
| 354 | std::get<size_t>(retryIO) = 0; |
| 355 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 356 | auto valueInterface = static_cast< |
| 357 | std::shared_ptr<ValueObject>>(nullptr); |
| 358 | try |
| 359 | { |
Matthew Barth | d4beecf | 2018-04-03 15:50:22 -0500 | [diff] [blame] | 360 | valueInterface = addValue(sensor.first, retryIO, ioAccess, info, |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 361 | _isOCC); |
| 362 | } |
| 363 | catch (const std::system_error& e) |
| 364 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 365 | auto file = sysfs::make_sysfs_path( |
| 366 | ioAccess.path(), |
| 367 | sensor.first.first, |
| 368 | sensor.first.second, |
| 369 | hwmon::entry::cinput); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 370 | #ifndef REMOVE_ON_FAIL |
| 371 | // Check sensorAdjusts for sensor removal RCs |
| 372 | const auto& it = sensorAdjusts.find(sensor.first); |
| 373 | if (it != sensorAdjusts.end()) |
| 374 | { |
| 375 | auto rmRCit = it->second.rmRCs.find(e.code().value()); |
| 376 | if (rmRCit != std::end(it->second.rmRCs)) |
| 377 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 378 | // Return code found in sensor return code removal list |
| 379 | if (rmSensors.find(sensor.first) == rmSensors.end()) |
| 380 | { |
| 381 | // Trace for sensor not already removed from dbus |
| 382 | log<level::INFO>("Sensor not added to dbus for read fail", |
| 383 | entry("FILE=%s", file.c_str()), |
| 384 | entry("RC=%d", e.code().value())); |
| 385 | rmSensors[std::move(sensor.first)] = |
| 386 | std::move(sensor.second); |
| 387 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | } |
| 391 | #endif |
| 392 | using namespace sdbusplus::xyz::openbmc_project:: |
| 393 | Sensor::Device::Error; |
| 394 | report<ReadFailure>( |
| 395 | xyz::openbmc_project::Sensor::Device:: |
| 396 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 397 | xyz::openbmc_project::Sensor::Device:: |
| 398 | ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str())); |
| 399 | |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 400 | log<level::INFO>("Logging failing sysfs file", |
| 401 | entry("FILE=%s", file.c_str())); |
| 402 | #ifdef REMOVE_ON_FAIL |
| 403 | return; /* skip adding this sensor for now. */ |
| 404 | #else |
| 405 | exit(EXIT_FAILURE); |
| 406 | #endif |
| 407 | } |
| 408 | auto sensorValue = valueInterface->value(); |
| 409 | addThreshold<WarningObject>(sensor.first.first, id, sensorValue, info); |
| 410 | addThreshold<CriticalObject>(sensor.first.first, id, sensorValue, info); |
| 411 | |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 412 | auto target = addTarget<hwmon::FanSpeed>( |
| 413 | sensor.first, ioAccess, _devPath, info); |
| 414 | if (target) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 415 | { |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 416 | target->enable(); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 417 | } |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 418 | addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 419 | |
| 420 | // All the interfaces have been created. Go ahead |
| 421 | // and emit InterfacesAdded. |
| 422 | valueInterface->emit_object_added(); |
| 423 | |
| 424 | auto value = std::make_tuple( |
| 425 | std::move(sensor.second), |
| 426 | std::move(label), |
| 427 | std::move(info)); |
| 428 | |
| 429 | state[std::move(sensor.first)] = std::move(value); |
| 430 | } |
| 431 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 432 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 433 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 434 | const std::string& path, |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 435 | const std::string& devPath, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 436 | const char* prefix, |
| 437 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 438 | : _bus(std::move(bus)), |
Brad Bishop | 03e8735 | 2017-03-07 00:12:22 -0500 | [diff] [blame] | 439 | _manager(_bus, root), |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 440 | _hwmonRoot(), |
| 441 | _instance(), |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 442 | _devPath(devPath), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 443 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 444 | _root(root), |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 445 | state(), |
| 446 | ioAccess(path) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 447 | { |
Matthew Barth | a23babd | 2018-03-16 10:03:27 -0500 | [diff] [blame] | 448 | if (path.find("occ") != std::string::npos) |
| 449 | { |
| 450 | _isOCC = true; |
| 451 | } |
| 452 | |
Patrick Venture | 73a50c7 | 2018-04-17 15:19:03 -0700 | [diff] [blame^] | 453 | // Strip off any trailing slashes. |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 454 | std::string p = path; |
| 455 | while (!p.empty() && p.back() == '/') |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 456 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 457 | p.pop_back(); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 458 | } |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 459 | |
Patrick Venture | 73a50c7 | 2018-04-17 15:19:03 -0700 | [diff] [blame^] | 460 | // Given the furthest right /, set instance to |
| 461 | // the basename, and hwmonRoot to the leading path. |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 462 | auto n = p.rfind('/'); |
| 463 | if (n != std::string::npos) |
| 464 | { |
| 465 | _instance.assign(p.substr(n + 1)); |
| 466 | _hwmonRoot.assign(p.substr(0, n)); |
| 467 | } |
| 468 | |
| 469 | assert(!_instance.empty()); |
| 470 | assert(!_hwmonRoot.empty()); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void MainLoop::shutdown() noexcept |
| 474 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 475 | timer->state(phosphor::hwmon::timer::OFF); |
| 476 | sd_event_exit(loop, 0); |
| 477 | loop = nullptr; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 481 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 482 | init(); |
| 483 | |
| 484 | sd_event_default(&loop); |
| 485 | |
| 486 | std::function<void()> callback(std::bind( |
| 487 | &MainLoop::read, this)); |
| 488 | try |
| 489 | { |
| 490 | timer = std::make_unique<phosphor::hwmon::Timer>( |
| 491 | loop, callback, |
| 492 | std::chrono::microseconds(_interval), |
| 493 | phosphor::hwmon::timer::ON); |
| 494 | |
| 495 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
| 496 | |
| 497 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 498 | // for new entries. |
| 499 | |
| 500 | _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT); |
| 501 | sd_event_loop(loop); |
| 502 | } |
| 503 | catch (const std::system_error& e) |
| 504 | { |
| 505 | log<level::ERR>("Error in sysfs polling loop", |
| 506 | entry("ERROR=%s", e.what())); |
| 507 | throw; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | void MainLoop::init() |
| 512 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 513 | // Check sysfs for available sensors. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 514 | auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 515 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 516 | for (auto& i : *sensors) |
| 517 | { |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 518 | getObject(i); |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 519 | } |
| 520 | |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 521 | /* If there are no sensors specified by labels, exit. */ |
| 522 | if (0 == state.size()) |
| 523 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 524 | exit(0); |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 527 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 528 | std::string busname{_prefix}; |
Brad Bishop | 4d9a35b | 2017-11-14 22:33:03 -0500 | [diff] [blame] | 529 | busname.append(1, '-'); |
| 530 | busname.append( |
| 531 | std::to_string(std::hash<decltype(_devPath)>{}(_devPath))); |
| 532 | busname.append(".Hwmon1"); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 533 | _bus.request_name(busname.c_str()); |
| 534 | } |
| 535 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 536 | { |
| 537 | auto interval = getenv("INTERVAL"); |
| 538 | if (interval) |
| 539 | { |
| 540 | _interval = strtoull(interval, NULL, 10); |
| 541 | } |
| 542 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 543 | } |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 544 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 545 | void MainLoop::read() |
| 546 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 547 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 548 | // ensure the objects all exist? |
| 549 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 550 | // Iterate through all the sensors. |
| 551 | for (auto& i : state) |
| 552 | { |
| 553 | auto& attrs = std::get<0>(i.second); |
| 554 | if (attrs.find(hwmon::entry::input) != attrs.end()) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 555 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 556 | // Read value from sensor. |
| 557 | int64_t value; |
| 558 | std::string input = hwmon::entry::cinput; |
| 559 | if (i.first.first == "pwm") { |
| 560 | input = ""; |
| 561 | } |
| 562 | |
| 563 | try |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 564 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 565 | // Retry for up to a second if device is busy |
| 566 | // or has a transient error. |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 567 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 568 | value = ioAccess.read( |
| 569 | i.first.first, |
| 570 | i.first.second, |
| 571 | input, |
| 572 | sysfs::hwmonio::retries, |
| 573 | sysfs::hwmonio::delay, |
| 574 | _isOCC); |
| 575 | |
| 576 | value = adjustValue(i.first, value); |
| 577 | |
| 578 | auto& objInfo = std::get<ObjectInfo>(i.second); |
| 579 | auto& obj = std::get<Object>(objInfo); |
| 580 | |
| 581 | for (auto& iface : obj) |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 582 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 583 | auto valueIface = std::shared_ptr<ValueObject>(); |
| 584 | auto warnIface = std::shared_ptr<WarningObject>(); |
| 585 | auto critIface = std::shared_ptr<CriticalObject>(); |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 586 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 587 | switch (iface.first) |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 588 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 589 | case InterfaceType::VALUE: |
| 590 | valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>> |
| 591 | (iface.second); |
| 592 | valueIface->value(value); |
| 593 | break; |
| 594 | case InterfaceType::WARN: |
| 595 | checkThresholds<WarningObject>(iface.second, value); |
| 596 | break; |
| 597 | case InterfaceType::CRIT: |
| 598 | checkThresholds<CriticalObject>(iface.second, value); |
| 599 | break; |
| 600 | default: |
| 601 | break; |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 602 | } |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 603 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 604 | } |
| 605 | catch (const std::system_error& e) |
| 606 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 607 | auto file = sysfs::make_sysfs_path( |
| 608 | ioAccess.path(), |
| 609 | i.first.first, |
| 610 | i.first.second, |
| 611 | hwmon::entry::cinput); |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 612 | #ifndef REMOVE_ON_FAIL |
| 613 | // Check sensorAdjusts for sensor removal RCs |
| 614 | const auto& it = sensorAdjusts.find(i.first); |
| 615 | if (it != sensorAdjusts.end()) |
| 616 | { |
| 617 | auto rmRCit = it->second.rmRCs.find(e.code().value()); |
| 618 | if (rmRCit != std::end(it->second.rmRCs)) |
| 619 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 620 | // Return code found in sensor return code removal list |
| 621 | if (rmSensors.find(i.first) == rmSensors.end()) |
| 622 | { |
| 623 | // Trace for sensor not already removed from dbus |
| 624 | log<level::INFO>( |
| 625 | "Remove sensor from dbus for read fail", |
| 626 | entry("FILE=%s", file.c_str()), |
| 627 | entry("RC=%d", e.code().value())); |
| 628 | // Mark this sensor to be removed from dbus |
| 629 | rmSensors[i.first] = std::get<0>(i.second); |
| 630 | } |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 631 | continue; |
| 632 | } |
| 633 | } |
| 634 | #endif |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 635 | using namespace sdbusplus::xyz::openbmc_project:: |
| 636 | Sensor::Device::Error; |
| 637 | report<ReadFailure>( |
| 638 | xyz::openbmc_project::Sensor::Device:: |
| 639 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 640 | xyz::openbmc_project::Sensor::Device:: |
| 641 | ReadFailure::CALLOUT_DEVICE_PATH( |
| 642 | _devPath.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 643 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 644 | log<level::INFO>("Logging failing sysfs file", |
| 645 | entry("FILE=%s", file.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 646 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 647 | #ifdef REMOVE_ON_FAIL |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 648 | rmSensors[i.first] = std::get<0>(i.second); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 649 | #else |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 650 | exit(EXIT_FAILURE); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 651 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 652 | } |
| 653 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 654 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 655 | |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 656 | // Remove any sensors marked for removal |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 657 | for (auto& i : rmSensors) |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 658 | { |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 659 | state.erase(i.first); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 660 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 661 | |
| 662 | #ifndef REMOVE_ON_FAIL |
| 663 | // Attempt to add any sensors that were removed |
| 664 | auto it = rmSensors.begin(); |
| 665 | while (it != rmSensors.end()) |
| 666 | { |
| 667 | if (state.find(it->first) == state.end()) |
| 668 | { |
| 669 | SensorSet::container_t::value_type ssValueType = |
| 670 | std::make_pair(it->first, it->second); |
| 671 | getObject(ssValueType); |
| 672 | if (state.find(it->first) != state.end()) |
| 673 | { |
| 674 | // Sensor object added, erase entry from removal list |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 675 | auto file = sysfs::make_sysfs_path( |
| 676 | ioAccess.path(), |
| 677 | it->first.first, |
| 678 | it->first.second, |
| 679 | hwmon::entry::cinput); |
| 680 | log<level::INFO>( |
| 681 | "Added sensor to dbus after successful read", |
| 682 | entry("FILE=%s", file.c_str())); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 683 | it = rmSensors.erase(it); |
| 684 | } |
| 685 | else |
| 686 | { |
| 687 | ++it; |
| 688 | } |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | // Sanity check to remove sensors that were re-added |
| 693 | it = rmSensors.erase(it); |
| 694 | } |
| 695 | } |
| 696 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |