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