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 | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame^] | 198 | std::string MainLoop::getID(SensorSet::container_t::const_reference sensor) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 199 | { |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 200 | std::string id; |
| 201 | |
| 202 | /* |
| 203 | * Check if the value of the MODE_<item><X> env variable for the sensor |
| 204 | * is "label", then read the sensor number from the <item><X>_label |
| 205 | * file. The name of the DBUS object would be the value of the env |
| 206 | * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable |
| 207 | * doesn't exist, then the name of DBUS object is the value of the env |
| 208 | * variable LABEL_<item><X>. |
| 209 | */ |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 210 | auto mode = env::getEnv("MODE", sensor.first); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 211 | if (!mode.compare(hwmon::entry::label)) |
| 212 | { |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 213 | id = env::getIndirectID( |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 214 | _hwmonRoot + '/' + _instance + '/', sensor.first); |
| 215 | |
| 216 | if (id.empty()) |
| 217 | { |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame^] | 218 | return id; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | // Use the ID we looked up above if there was one, |
| 223 | // otherwise use the standard one. |
| 224 | id = (id.empty()) ? sensor.first.second : id; |
| 225 | |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame^] | 226 | return id; |
| 227 | } |
| 228 | |
| 229 | SensorIdentifiers MainLoop::getIdentifiers( |
| 230 | SensorSet::container_t::const_reference sensor) |
| 231 | { |
| 232 | std::string id = getID(sensor); |
| 233 | std::string label; |
| 234 | |
| 235 | if (!id.empty()) |
| 236 | { |
| 237 | // Ignore inputs without a label. |
| 238 | label = env::getEnv("LABEL", sensor.first.first, id); |
| 239 | } |
| 240 | |
| 241 | return std::make_tuple(std::move(id), |
| 242 | std::move(label)); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Reads the environment parameters of a sensor and creates an object with |
| 247 | * atleast the `Value` interface, otherwise returns without creating the object. |
| 248 | * If the `Value` interface is successfully created, by reading the sensor's |
| 249 | * corresponding sysfs file's value, the additional interfaces for the sensor |
| 250 | * are created and the InterfacesAdded signal is emitted. The sensor is then |
| 251 | * moved to the list for sensor state monitoring within the main loop. |
| 252 | */ |
| 253 | void MainLoop::getObject(SensorSet::container_t::const_reference sensor) |
| 254 | { |
| 255 | auto properties = getIdentifiers(sensor); |
| 256 | if (std::get<sensorID>(properties).empty() || |
| 257 | std::get<sensorLabel>(properties).empty()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 258 | { |
| 259 | return; |
| 260 | } |
| 261 | |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 262 | hwmon::Attributes attrs; |
| 263 | if (!hwmon::getAttributes(sensor.first.first, attrs)) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 264 | { |
| 265 | return; |
| 266 | } |
| 267 | |
Matthew Barth | b798527 | 2018-04-17 10:50:36 -0500 | [diff] [blame] | 268 | // Get list of return codes for removing sensors on device |
| 269 | auto devRmRCs = env::getEnv("REMOVERCS"); |
| 270 | // Add sensor removal return codes defined at the device level |
| 271 | addRemoveRCs(sensor.first, devRmRCs); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 272 | |
| 273 | std::string objectPath{_root}; |
| 274 | objectPath.append(1, '/'); |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 275 | objectPath.append(hwmon::getNamespace(attrs)); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 276 | objectPath.append(1, '/'); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame^] | 277 | objectPath.append(std::get<sensorLabel>(properties)); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 278 | |
| 279 | ObjectInfo info(&_bus, std::move(objectPath), Object()); |
Matthew Barth | d4beecf | 2018-04-03 15:50:22 -0500 | [diff] [blame] | 280 | RetryIO retryIO(sysfs::hwmonio::retries, sysfs::hwmonio::delay); |
| 281 | if (rmSensors.find(sensor.first) != rmSensors.end()) |
| 282 | { |
| 283 | // When adding a sensor that was purposely removed, |
| 284 | // don't retry on errors when reading its value |
| 285 | std::get<size_t>(retryIO) = 0; |
| 286 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 287 | auto valueInterface = static_cast< |
| 288 | std::shared_ptr<ValueObject>>(nullptr); |
| 289 | try |
| 290 | { |
Matthew Barth | d4beecf | 2018-04-03 15:50:22 -0500 | [diff] [blame] | 291 | valueInterface = addValue(sensor.first, retryIO, ioAccess, info, |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 292 | _isOCC); |
| 293 | } |
| 294 | catch (const std::system_error& e) |
| 295 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 296 | auto file = sysfs::make_sysfs_path( |
| 297 | ioAccess.path(), |
| 298 | sensor.first.first, |
| 299 | sensor.first.second, |
| 300 | hwmon::entry::cinput); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 301 | #ifndef REMOVE_ON_FAIL |
| 302 | // Check sensorAdjusts for sensor removal RCs |
| 303 | const auto& it = sensorAdjusts.find(sensor.first); |
| 304 | if (it != sensorAdjusts.end()) |
| 305 | { |
| 306 | auto rmRCit = it->second.rmRCs.find(e.code().value()); |
| 307 | if (rmRCit != std::end(it->second.rmRCs)) |
| 308 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 309 | // Return code found in sensor return code removal list |
| 310 | if (rmSensors.find(sensor.first) == rmSensors.end()) |
| 311 | { |
| 312 | // Trace for sensor not already removed from dbus |
| 313 | log<level::INFO>("Sensor not added to dbus for read fail", |
| 314 | entry("FILE=%s", file.c_str()), |
| 315 | entry("RC=%d", e.code().value())); |
| 316 | rmSensors[std::move(sensor.first)] = |
| 317 | std::move(sensor.second); |
| 318 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | } |
| 322 | #endif |
| 323 | using namespace sdbusplus::xyz::openbmc_project:: |
| 324 | Sensor::Device::Error; |
| 325 | report<ReadFailure>( |
| 326 | xyz::openbmc_project::Sensor::Device:: |
| 327 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 328 | xyz::openbmc_project::Sensor::Device:: |
| 329 | ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str())); |
| 330 | |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 331 | log<level::INFO>("Logging failing sysfs file", |
| 332 | entry("FILE=%s", file.c_str())); |
| 333 | #ifdef REMOVE_ON_FAIL |
| 334 | return; /* skip adding this sensor for now. */ |
| 335 | #else |
| 336 | exit(EXIT_FAILURE); |
| 337 | #endif |
| 338 | } |
| 339 | auto sensorValue = valueInterface->value(); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame^] | 340 | addThreshold<WarningObject>(sensor.first.first, |
| 341 | std::get<sensorID>(properties), |
| 342 | sensorValue, |
| 343 | info); |
| 344 | addThreshold<CriticalObject>(sensor.first.first, |
| 345 | std::get<sensorID>(properties), |
| 346 | sensorValue, |
| 347 | info); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 348 | |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 349 | auto target = addTarget<hwmon::FanSpeed>( |
| 350 | sensor.first, ioAccess, _devPath, info); |
| 351 | if (target) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 352 | { |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 353 | target->enable(); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 354 | } |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 355 | addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 356 | |
| 357 | // All the interfaces have been created. Go ahead |
| 358 | // and emit InterfacesAdded. |
| 359 | valueInterface->emit_object_added(); |
| 360 | |
| 361 | auto value = std::make_tuple( |
| 362 | std::move(sensor.second), |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame^] | 363 | std::move(std::get<sensorLabel>(properties)), |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 364 | std::move(info)); |
| 365 | |
| 366 | state[std::move(sensor.first)] = std::move(value); |
| 367 | } |
| 368 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 369 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 370 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 371 | const std::string& path, |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 372 | const std::string& devPath, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 373 | const char* prefix, |
| 374 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 375 | : _bus(std::move(bus)), |
Brad Bishop | 03e8735 | 2017-03-07 00:12:22 -0500 | [diff] [blame] | 376 | _manager(_bus, root), |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 377 | _hwmonRoot(), |
| 378 | _instance(), |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 379 | _devPath(devPath), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 380 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 381 | _root(root), |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 382 | state(), |
| 383 | ioAccess(path) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 384 | { |
Matthew Barth | a23babd | 2018-03-16 10:03:27 -0500 | [diff] [blame] | 385 | if (path.find("occ") != std::string::npos) |
| 386 | { |
| 387 | _isOCC = true; |
| 388 | } |
| 389 | |
Patrick Venture | 73a50c7 | 2018-04-17 15:19:03 -0700 | [diff] [blame] | 390 | // Strip off any trailing slashes. |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 391 | std::string p = path; |
| 392 | while (!p.empty() && p.back() == '/') |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 393 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 394 | p.pop_back(); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 395 | } |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 396 | |
Patrick Venture | 73a50c7 | 2018-04-17 15:19:03 -0700 | [diff] [blame] | 397 | // Given the furthest right /, set instance to |
| 398 | // the basename, and hwmonRoot to the leading path. |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 399 | auto n = p.rfind('/'); |
| 400 | if (n != std::string::npos) |
| 401 | { |
| 402 | _instance.assign(p.substr(n + 1)); |
| 403 | _hwmonRoot.assign(p.substr(0, n)); |
| 404 | } |
| 405 | |
| 406 | assert(!_instance.empty()); |
| 407 | assert(!_hwmonRoot.empty()); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void MainLoop::shutdown() noexcept |
| 411 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 412 | timer->state(phosphor::hwmon::timer::OFF); |
| 413 | sd_event_exit(loop, 0); |
| 414 | loop = nullptr; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 418 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 419 | init(); |
| 420 | |
| 421 | sd_event_default(&loop); |
| 422 | |
| 423 | std::function<void()> callback(std::bind( |
| 424 | &MainLoop::read, this)); |
| 425 | try |
| 426 | { |
| 427 | timer = std::make_unique<phosphor::hwmon::Timer>( |
| 428 | loop, callback, |
| 429 | std::chrono::microseconds(_interval), |
| 430 | phosphor::hwmon::timer::ON); |
| 431 | |
| 432 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
| 433 | |
| 434 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 435 | // for new entries. |
| 436 | |
| 437 | _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT); |
| 438 | sd_event_loop(loop); |
| 439 | } |
| 440 | catch (const std::system_error& e) |
| 441 | { |
| 442 | log<level::ERR>("Error in sysfs polling loop", |
| 443 | entry("ERROR=%s", e.what())); |
| 444 | throw; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void MainLoop::init() |
| 449 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 450 | // Check sysfs for available sensors. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 451 | auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 452 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 453 | for (auto& i : *sensors) |
| 454 | { |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 455 | getObject(i); |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 456 | } |
| 457 | |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 458 | /* If there are no sensors specified by labels, exit. */ |
| 459 | if (0 == state.size()) |
| 460 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 461 | exit(0); |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 464 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 465 | std::string busname{_prefix}; |
Brad Bishop | 4d9a35b | 2017-11-14 22:33:03 -0500 | [diff] [blame] | 466 | busname.append(1, '-'); |
| 467 | busname.append( |
| 468 | std::to_string(std::hash<decltype(_devPath)>{}(_devPath))); |
| 469 | busname.append(".Hwmon1"); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 470 | _bus.request_name(busname.c_str()); |
| 471 | } |
| 472 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 473 | { |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 474 | auto interval = env::getEnv("INTERVAL"); |
| 475 | if (!interval.empty()) |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 476 | { |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 477 | _interval = strtoull(interval.c_str(), NULL, 10); |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 480 | } |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 481 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 482 | void MainLoop::read() |
| 483 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 484 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 485 | // ensure the objects all exist? |
| 486 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 487 | // Iterate through all the sensors. |
| 488 | for (auto& i : state) |
| 489 | { |
| 490 | auto& attrs = std::get<0>(i.second); |
| 491 | if (attrs.find(hwmon::entry::input) != attrs.end()) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 492 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 493 | // Read value from sensor. |
| 494 | int64_t value; |
| 495 | std::string input = hwmon::entry::cinput; |
| 496 | if (i.first.first == "pwm") { |
| 497 | input = ""; |
| 498 | } |
| 499 | |
| 500 | try |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 501 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 502 | // Retry for up to a second if device is busy |
| 503 | // or has a transient error. |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 504 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 505 | value = ioAccess.read( |
| 506 | i.first.first, |
| 507 | i.first.second, |
| 508 | input, |
| 509 | sysfs::hwmonio::retries, |
| 510 | sysfs::hwmonio::delay, |
| 511 | _isOCC); |
| 512 | |
| 513 | value = adjustValue(i.first, value); |
| 514 | |
| 515 | auto& objInfo = std::get<ObjectInfo>(i.second); |
| 516 | auto& obj = std::get<Object>(objInfo); |
| 517 | |
| 518 | for (auto& iface : obj) |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 519 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 520 | auto valueIface = std::shared_ptr<ValueObject>(); |
| 521 | auto warnIface = std::shared_ptr<WarningObject>(); |
| 522 | auto critIface = std::shared_ptr<CriticalObject>(); |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 523 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 524 | switch (iface.first) |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 525 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 526 | case InterfaceType::VALUE: |
| 527 | valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>> |
| 528 | (iface.second); |
| 529 | valueIface->value(value); |
| 530 | break; |
| 531 | case InterfaceType::WARN: |
| 532 | checkThresholds<WarningObject>(iface.second, value); |
| 533 | break; |
| 534 | case InterfaceType::CRIT: |
| 535 | checkThresholds<CriticalObject>(iface.second, value); |
| 536 | break; |
| 537 | default: |
| 538 | break; |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 539 | } |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 540 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 541 | } |
| 542 | catch (const std::system_error& e) |
| 543 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 544 | auto file = sysfs::make_sysfs_path( |
| 545 | ioAccess.path(), |
| 546 | i.first.first, |
| 547 | i.first.second, |
| 548 | hwmon::entry::cinput); |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 549 | #ifndef REMOVE_ON_FAIL |
| 550 | // Check sensorAdjusts for sensor removal RCs |
| 551 | const auto& it = sensorAdjusts.find(i.first); |
| 552 | if (it != sensorAdjusts.end()) |
| 553 | { |
| 554 | auto rmRCit = it->second.rmRCs.find(e.code().value()); |
| 555 | if (rmRCit != std::end(it->second.rmRCs)) |
| 556 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 557 | // Return code found in sensor return code removal list |
| 558 | if (rmSensors.find(i.first) == rmSensors.end()) |
| 559 | { |
| 560 | // Trace for sensor not already removed from dbus |
| 561 | log<level::INFO>( |
| 562 | "Remove sensor from dbus for read fail", |
| 563 | entry("FILE=%s", file.c_str()), |
| 564 | entry("RC=%d", e.code().value())); |
| 565 | // Mark this sensor to be removed from dbus |
| 566 | rmSensors[i.first] = std::get<0>(i.second); |
| 567 | } |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 568 | continue; |
| 569 | } |
| 570 | } |
| 571 | #endif |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 572 | using namespace sdbusplus::xyz::openbmc_project:: |
| 573 | Sensor::Device::Error; |
| 574 | report<ReadFailure>( |
| 575 | xyz::openbmc_project::Sensor::Device:: |
| 576 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 577 | xyz::openbmc_project::Sensor::Device:: |
| 578 | ReadFailure::CALLOUT_DEVICE_PATH( |
| 579 | _devPath.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 580 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 581 | log<level::INFO>("Logging failing sysfs file", |
| 582 | entry("FILE=%s", file.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 583 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 584 | #ifdef REMOVE_ON_FAIL |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 585 | rmSensors[i.first] = std::get<0>(i.second); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 586 | #else |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 587 | exit(EXIT_FAILURE); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 588 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 589 | } |
| 590 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 591 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 592 | |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 593 | // Remove any sensors marked for removal |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 594 | for (auto& i : rmSensors) |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 595 | { |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 596 | state.erase(i.first); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 597 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 598 | |
| 599 | #ifndef REMOVE_ON_FAIL |
| 600 | // Attempt to add any sensors that were removed |
| 601 | auto it = rmSensors.begin(); |
| 602 | while (it != rmSensors.end()) |
| 603 | { |
| 604 | if (state.find(it->first) == state.end()) |
| 605 | { |
| 606 | SensorSet::container_t::value_type ssValueType = |
| 607 | std::make_pair(it->first, it->second); |
| 608 | getObject(ssValueType); |
| 609 | if (state.find(it->first) != state.end()) |
| 610 | { |
| 611 | // Sensor object added, erase entry from removal list |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 612 | auto file = sysfs::make_sysfs_path( |
| 613 | ioAccess.path(), |
| 614 | it->first.first, |
| 615 | it->first.second, |
| 616 | hwmon::entry::cinput); |
| 617 | log<level::INFO>( |
| 618 | "Added sensor to dbus after successful read", |
| 619 | entry("FILE=%s", file.c_str())); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 620 | it = rmSensors.erase(it); |
| 621 | } |
| 622 | else |
| 623 | { |
| 624 | ++it; |
| 625 | } |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | // Sanity check to remove sensors that were re-added |
| 630 | it = rmSensors.erase(it); |
| 631 | } |
| 632 | } |
| 633 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |