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