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