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