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