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, |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 210 | const std::string& devPath, |
| 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, |
| 235 | sysfs::hwmonio::retries, |
| 236 | sysfs::hwmonio::delay, |
| 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 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 278 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 279 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 280 | const std::string& path, |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 281 | const std::string& devPath, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 282 | const char* prefix, |
| 283 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 284 | : _bus(std::move(bus)), |
Brad Bishop | 03e8735 | 2017-03-07 00:12:22 -0500 | [diff] [blame] | 285 | _manager(_bus, root), |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 286 | _hwmonRoot(), |
| 287 | _instance(), |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 288 | _devPath(devPath), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 289 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 290 | _root(root), |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 291 | state(), |
| 292 | ioAccess(path) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 293 | { |
Matthew Barth | a23babd | 2018-03-16 10:03:27 -0500 | [diff] [blame] | 294 | if (path.find("occ") != std::string::npos) |
| 295 | { |
| 296 | _isOCC = true; |
| 297 | } |
| 298 | |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 299 | std::string p = path; |
| 300 | while (!p.empty() && p.back() == '/') |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 301 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 302 | p.pop_back(); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 303 | } |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 304 | |
| 305 | auto n = p.rfind('/'); |
| 306 | if (n != std::string::npos) |
| 307 | { |
| 308 | _instance.assign(p.substr(n + 1)); |
| 309 | _hwmonRoot.assign(p.substr(0, n)); |
| 310 | } |
| 311 | |
| 312 | assert(!_instance.empty()); |
| 313 | assert(!_hwmonRoot.empty()); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void MainLoop::shutdown() noexcept |
| 317 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 318 | timer->state(phosphor::hwmon::timer::OFF); |
| 319 | sd_event_exit(loop, 0); |
| 320 | loop = nullptr; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 324 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 325 | init(); |
| 326 | |
| 327 | sd_event_default(&loop); |
| 328 | |
| 329 | std::function<void()> callback(std::bind( |
| 330 | &MainLoop::read, this)); |
| 331 | try |
| 332 | { |
| 333 | timer = std::make_unique<phosphor::hwmon::Timer>( |
| 334 | loop, callback, |
| 335 | std::chrono::microseconds(_interval), |
| 336 | phosphor::hwmon::timer::ON); |
| 337 | |
| 338 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
| 339 | |
| 340 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 341 | // for new entries. |
| 342 | |
| 343 | _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT); |
| 344 | sd_event_loop(loop); |
| 345 | } |
| 346 | catch (const std::system_error& e) |
| 347 | { |
| 348 | log<level::ERR>("Error in sysfs polling loop", |
| 349 | entry("ERROR=%s", e.what())); |
| 350 | throw; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void MainLoop::init() |
| 355 | { |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 356 | //If this device supports target speeds, |
| 357 | //check which type to use. |
| 358 | targetType fanTargetType = targetType::DEFAULT; |
| 359 | auto targetMode = getenv("TARGET_MODE"); |
| 360 | if (targetMode) |
| 361 | { |
| 362 | std::string type{targetMode}; |
| 363 | std::transform(type.begin(), type.end(), type.begin(), toupper); |
| 364 | |
| 365 | if (type == RPM_TARGET) |
| 366 | { |
| 367 | fanTargetType = targetType::RPM; |
| 368 | } |
| 369 | else if (type == PWM_TARGET) |
| 370 | { |
| 371 | fanTargetType = targetType::PWM; |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | log<level::ERR>( |
| 376 | "Invalid TARGET_MODE env var found", |
| 377 | entry("TARGET_MODE=%s", targetMode), |
| 378 | entry("DEVPATH=%s", _devPath.c_str())); |
| 379 | } |
| 380 | } |
| 381 | |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame] | 382 | // Get list of return codes for removing sensors on device |
| 383 | std::string deviceRmRCs; |
| 384 | auto devRmRCs = getenv("REMOVERCS"); |
| 385 | if (devRmRCs) |
| 386 | { |
| 387 | deviceRmRCs.assign(devRmRCs); |
| 388 | } |
| 389 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 390 | // Check sysfs for available sensors. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 391 | auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 392 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 393 | for (auto& i : *sensors) |
| 394 | { |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 395 | std::string label; |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 396 | std::string id; |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 397 | |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 398 | /* |
| 399 | * Check if the value of the MODE_<item><X> env variable for the sensor |
| 400 | * is "label", then read the sensor number from the <item><X>_label |
| 401 | * file. The name of the DBUS object would be the value of the env |
| 402 | * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 403 | * doesn't exist, then the name of DBUS object is the value of the env |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 404 | * variable LABEL_<item><X>. |
| 405 | */ |
| 406 | auto mode = getEnv("MODE", i.first); |
| 407 | if (!mode.compare(hwmon::entry::label)) |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 408 | { |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 409 | id = getIndirectID( |
| 410 | _hwmonRoot + '/' + _instance + '/', i.first); |
| 411 | |
| 412 | if (id.empty()) |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 413 | { |
| 414 | continue; |
| 415 | } |
| 416 | } |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 417 | |
| 418 | //In this loop, use the ID we looked up above if |
| 419 | //there was one, otherwise use the standard one. |
| 420 | id = (id.empty()) ? i.first.second : id; |
| 421 | |
| 422 | // Ignore inputs without a label. |
| 423 | label = getEnv("LABEL", i.first.first, id); |
| 424 | if (label.empty()) |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 425 | { |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 426 | continue; |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 429 | Attributes attrs; |
| 430 | if (!getAttributes(i.first.first, attrs)) |
| 431 | { |
| 432 | continue; |
| 433 | } |
| 434 | |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame] | 435 | if (!deviceRmRCs.empty()) |
| 436 | { |
| 437 | // Add sensor removal return codes defined at the device level |
| 438 | addRemoveRCs(i.first, deviceRmRCs); |
| 439 | } |
| 440 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 441 | std::string objectPath{_root}; |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 442 | objectPath.append(1, '/'); |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 443 | objectPath.append(getNamespace(attrs)); |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 444 | objectPath.append(1, '/'); |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 445 | objectPath.append(label); |
| 446 | |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 447 | ObjectInfo info(&_bus, std::move(objectPath), Object()); |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame^] | 448 | auto valueInterface = static_cast< |
| 449 | std::shared_ptr<ValueObject>>(nullptr); |
| 450 | try |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 451 | { |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame^] | 452 | valueInterface = addValue(i.first, _devPath, ioAccess, info, |
| 453 | _isOCC); |
| 454 | } |
| 455 | catch (const std::system_error& e) |
| 456 | { |
| 457 | #ifndef REMOVE_ON_FAIL |
| 458 | // Check sensorAdjusts for sensor removal RCs |
| 459 | const auto& it = sensorAdjusts.find(i.first); |
| 460 | if (it != sensorAdjusts.end()) |
| 461 | { |
| 462 | auto rmRCit = it->second.rmRCs.find(e.code().value()); |
| 463 | if (rmRCit != std::end(it->second.rmRCs)) |
| 464 | { |
| 465 | // Return code found in sensor removal list |
| 466 | // Skip adding this sensor for now |
| 467 | continue; |
| 468 | } |
| 469 | } |
| 470 | #endif |
| 471 | using namespace sdbusplus::xyz::openbmc_project:: |
| 472 | Sensor::Device::Error; |
| 473 | report<ReadFailure>( |
| 474 | xyz::openbmc_project::Sensor::Device:: |
| 475 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 476 | xyz::openbmc_project::Sensor::Device:: |
| 477 | ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str())); |
| 478 | |
| 479 | auto file = sysfs::make_sysfs_path( |
| 480 | ioAccess.path(), |
| 481 | i.first.first, |
| 482 | i.first.second, |
| 483 | hwmon::entry::cinput); |
| 484 | |
| 485 | log<level::INFO>("Logging failing sysfs file", |
| 486 | entry("FILE=%s", file.c_str())); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 487 | #ifdef REMOVE_ON_FAIL |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 488 | continue; /* skip adding this sensor for now. */ |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 489 | #else |
| 490 | exit(EXIT_FAILURE); |
| 491 | #endif |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 492 | } |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 493 | auto sensorValue = valueInterface->value(); |
Matt Spinler | ccfc77b | 2017-10-12 16:49:24 -0500 | [diff] [blame] | 494 | addThreshold<WarningObject>(i.first.first, id, sensorValue, info); |
| 495 | addThreshold<CriticalObject>(i.first.first, id, sensorValue, info); |
Matt Spinler | a3eb8e3 | 2017-10-12 16:42:49 -0500 | [diff] [blame] | 496 | |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 497 | if ((fanTargetType == targetType::RPM) || |
| 498 | (fanTargetType == targetType::DEFAULT)) |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 499 | { |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 500 | auto target = addTarget<hwmon::FanSpeed>( |
| 501 | i.first, ioAccess, _devPath, info); |
| 502 | |
| 503 | if (target) |
| 504 | { |
| 505 | target->enable(); |
| 506 | } |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 507 | } |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 508 | |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 509 | if ((fanTargetType == targetType::PWM) || |
| 510 | (fanTargetType == targetType::DEFAULT)) |
| 511 | { |
| 512 | addTarget<hwmon::FanPwm>(i.first, ioAccess, _devPath, info); |
| 513 | } |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 514 | |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 515 | // All the interfaces have been created. Go ahead |
| 516 | // and emit InterfacesAdded. |
| 517 | valueInterface->emit_object_added(); |
| 518 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 519 | auto value = std::make_tuple( |
| 520 | std::move(i.second), |
| 521 | std::move(label), |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 522 | std::move(info)); |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 523 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 524 | state[std::move(i.first)] = std::move(value); |
| 525 | } |
| 526 | |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 527 | /* If there are no sensors specified by labels, exit. */ |
| 528 | if (0 == state.size()) |
| 529 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 530 | exit(0); |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 533 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 534 | std::string busname{_prefix}; |
Brad Bishop | 4d9a35b | 2017-11-14 22:33:03 -0500 | [diff] [blame] | 535 | busname.append(1, '-'); |
| 536 | busname.append( |
| 537 | std::to_string(std::hash<decltype(_devPath)>{}(_devPath))); |
| 538 | busname.append(".Hwmon1"); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 539 | _bus.request_name(busname.c_str()); |
| 540 | } |
| 541 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 542 | { |
| 543 | auto interval = getenv("INTERVAL"); |
| 544 | if (interval) |
| 545 | { |
| 546 | _interval = strtoull(interval, NULL, 10); |
| 547 | } |
| 548 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 549 | } |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 550 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 551 | void MainLoop::read() |
| 552 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 553 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 554 | // ensure the objects all exist? |
| 555 | |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame^] | 556 | // Used in marking a sensor for removal from dbus |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 557 | std::vector<SensorSet::key_type> destroy; |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame^] | 558 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 559 | // Iterate through all the sensors. |
| 560 | for (auto& i : state) |
| 561 | { |
| 562 | auto& attrs = std::get<0>(i.second); |
| 563 | if (attrs.find(hwmon::entry::input) != attrs.end()) |
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 | // Read value from sensor. |
| 566 | int64_t value; |
| 567 | std::string input = hwmon::entry::cinput; |
| 568 | if (i.first.first == "pwm") { |
| 569 | input = ""; |
| 570 | } |
| 571 | |
| 572 | try |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 573 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 574 | // Retry for up to a second if device is busy |
| 575 | // or has a transient error. |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 576 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 577 | value = ioAccess.read( |
| 578 | i.first.first, |
| 579 | i.first.second, |
| 580 | input, |
| 581 | sysfs::hwmonio::retries, |
| 582 | sysfs::hwmonio::delay, |
| 583 | _isOCC); |
| 584 | |
| 585 | value = adjustValue(i.first, value); |
| 586 | |
| 587 | auto& objInfo = std::get<ObjectInfo>(i.second); |
| 588 | auto& obj = std::get<Object>(objInfo); |
| 589 | |
| 590 | for (auto& iface : obj) |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 591 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 592 | auto valueIface = std::shared_ptr<ValueObject>(); |
| 593 | auto warnIface = std::shared_ptr<WarningObject>(); |
| 594 | auto critIface = std::shared_ptr<CriticalObject>(); |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 595 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 596 | switch (iface.first) |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 597 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 598 | case InterfaceType::VALUE: |
| 599 | valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>> |
| 600 | (iface.second); |
| 601 | valueIface->value(value); |
| 602 | break; |
| 603 | case InterfaceType::WARN: |
| 604 | checkThresholds<WarningObject>(iface.second, value); |
| 605 | break; |
| 606 | case InterfaceType::CRIT: |
| 607 | checkThresholds<CriticalObject>(iface.second, value); |
| 608 | break; |
| 609 | default: |
| 610 | break; |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 611 | } |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 612 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 613 | } |
| 614 | catch (const std::system_error& e) |
| 615 | { |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame^] | 616 | #ifndef REMOVE_ON_FAIL |
| 617 | // Check sensorAdjusts for sensor removal RCs |
| 618 | const auto& it = sensorAdjusts.find(i.first); |
| 619 | if (it != sensorAdjusts.end()) |
| 620 | { |
| 621 | auto rmRCit = it->second.rmRCs.find(e.code().value()); |
| 622 | if (rmRCit != std::end(it->second.rmRCs)) |
| 623 | { |
| 624 | // Return code found in sensor removal list |
| 625 | // Mark this sensor to be removed from dbus |
| 626 | destroy.push_back(i.first); |
| 627 | continue; |
| 628 | } |
| 629 | } |
| 630 | #endif |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 631 | using namespace sdbusplus::xyz::openbmc_project:: |
| 632 | Sensor::Device::Error; |
| 633 | report<ReadFailure>( |
| 634 | xyz::openbmc_project::Sensor::Device:: |
| 635 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 636 | xyz::openbmc_project::Sensor::Device:: |
| 637 | ReadFailure::CALLOUT_DEVICE_PATH( |
| 638 | _devPath.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 639 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 640 | auto file = sysfs::make_sysfs_path( |
| 641 | ioAccess.path(), |
| 642 | i.first.first, |
| 643 | i.first.second, |
| 644 | hwmon::entry::cinput); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 645 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 646 | log<level::INFO>("Logging failing sysfs file", |
| 647 | entry("FILE=%s", file.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 648 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 649 | #ifdef REMOVE_ON_FAIL |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 650 | destroy.push_back(i.first); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 651 | #else |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 652 | exit(EXIT_FAILURE); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 653 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 654 | } |
| 655 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 656 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 657 | |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame^] | 658 | // Remove any sensors marked for removal |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 659 | for (auto& i : destroy) |
| 660 | { |
| 661 | state.erase(i); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 662 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |