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 | */ |
| 16 | #include <iostream> |
| 17 | #include <memory> |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 18 | #include <cstdlib> |
| 19 | #include <chrono> |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 20 | #include <algorithm> |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 21 | #include "sensorset.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 22 | #include "hwmon.hpp" |
| 23 | #include "sysfs.hpp" |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 24 | #include "mainloop.hpp" |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 25 | #include "env.hpp" |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 26 | #include "thresholds.hpp" |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 27 | #include "targets.hpp" |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame^] | 28 | #include "fan_speed.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 29 | |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 30 | // Initialization for Warning Objects |
| 31 | decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo = |
| 32 | &WarningObject::warningLow; |
| 33 | decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi = |
| 34 | &WarningObject::warningHigh; |
| 35 | decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo = |
| 36 | &WarningObject::warningLow; |
| 37 | decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi = |
| 38 | &WarningObject::warningHigh; |
| 39 | decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo = |
| 40 | &WarningObject::warningAlarmLow; |
| 41 | decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi = |
| 42 | &WarningObject::warningAlarmHigh; |
| 43 | |
| 44 | // Initialization for Critical Objects |
| 45 | decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo = |
| 46 | &CriticalObject::criticalLow; |
| 47 | decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi = |
| 48 | &CriticalObject::criticalHigh; |
| 49 | decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo = |
| 50 | &CriticalObject::criticalLow; |
| 51 | decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi = |
| 52 | &CriticalObject::criticalHigh; |
| 53 | decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo = |
| 54 | &CriticalObject::criticalAlarmLow; |
| 55 | decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi = |
| 56 | &CriticalObject::criticalAlarmHigh; |
| 57 | |
| 58 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 59 | using namespace std::literals::chrono_literals; |
| 60 | |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 61 | static constexpr auto typeAttrMap = |
| 62 | { |
| 63 | // 1 - hwmon class |
| 64 | // 2 - unit |
| 65 | // 3 - sysfs scaling factor |
| 66 | std::make_tuple( |
| 67 | hwmon::type::ctemp, |
| 68 | ValueInterface::Unit::DegreesC, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 69 | -3, |
| 70 | "temperature"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 71 | std::make_tuple( |
| 72 | hwmon::type::cfan, |
| 73 | ValueInterface::Unit::RPMS, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 74 | 0, |
| 75 | "fan_tach"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 76 | std::make_tuple( |
| 77 | hwmon::type::cvolt, |
| 78 | ValueInterface::Unit::Volts, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 79 | -3, |
| 80 | "voltage"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 81 | std::make_tuple( |
| 82 | hwmon::type::ccurr, |
| 83 | ValueInterface::Unit::Amperes, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 84 | -3, |
| 85 | "current"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 86 | std::make_tuple( |
| 87 | hwmon::type::cenergy, |
| 88 | ValueInterface::Unit::Joules, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 89 | -6, |
| 90 | "energy"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 91 | std::make_tuple( |
| 92 | hwmon::type::cpower, |
| 93 | ValueInterface::Unit::Watts, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 94 | -6, |
| 95 | "power"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | auto getHwmonType(decltype(typeAttrMap)::const_reference attrs) |
| 99 | { |
| 100 | return std::get<0>(attrs); |
| 101 | } |
| 102 | |
| 103 | auto getUnit(decltype(typeAttrMap)::const_reference attrs) |
| 104 | { |
| 105 | return std::get<1>(attrs); |
| 106 | } |
| 107 | |
| 108 | auto getScale(decltype(typeAttrMap)::const_reference attrs) |
| 109 | { |
| 110 | return std::get<2>(attrs); |
| 111 | } |
| 112 | |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 113 | auto getNamespace(decltype(typeAttrMap)::const_reference attrs) |
| 114 | { |
| 115 | return std::get<3>(attrs); |
| 116 | } |
| 117 | |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 118 | using AttributeIterator = decltype(*typeAttrMap.begin()); |
| 119 | using Attributes |
| 120 | = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type; |
| 121 | |
| 122 | auto getAttributes(const std::string& type, Attributes& attributes) |
| 123 | { |
| 124 | // *INDENT-OFF* |
| 125 | auto a = std::find_if( |
| 126 | typeAttrMap.begin(), |
| 127 | typeAttrMap.end(), |
| 128 | [&](const auto & e) |
| 129 | { |
| 130 | return type == getHwmonType(e); |
| 131 | }); |
| 132 | // *INDENT-ON* |
| 133 | |
| 134 | if (a == typeAttrMap.end()) |
| 135 | { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | attributes = *a; |
| 140 | return true; |
| 141 | } |
| 142 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 143 | auto addValue(const SensorSet::key_type& sensor, |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 144 | const std::string& hwmonRoot, |
| 145 | const std::string& instance, |
| 146 | ObjectInfo& info) |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 147 | { |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 148 | static constexpr bool deferSignals = true; |
| 149 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 150 | // Get the initial value for the value interface. |
| 151 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 152 | auto& obj = std::get<Object>(info); |
| 153 | auto& objPath = std::get<std::string>(info); |
| 154 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 155 | int val = readSysfsWithCallout(hwmonRoot, |
| 156 | instance, |
| 157 | sensor.first, |
| 158 | sensor.second, |
| 159 | hwmon::entry::input); |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 160 | auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 161 | iface->value(val); |
| 162 | |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 163 | Attributes attrs; |
| 164 | if (getAttributes(sensor.first, attrs)) |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 165 | { |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 166 | iface->unit(getUnit(attrs)); |
| 167 | iface->scale(getScale(attrs)); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | obj[InterfaceType::VALUE] = iface; |
| 171 | return iface; |
| 172 | } |
| 173 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 174 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 175 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 176 | const std::string& path, |
| 177 | const char* prefix, |
| 178 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 179 | : _bus(std::move(bus)), |
Brad Bishop | 03e8735 | 2017-03-07 00:12:22 -0500 | [diff] [blame] | 180 | _manager(_bus, root), |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 181 | _shutdown(false), |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 182 | _hwmonRoot(), |
| 183 | _instance(), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 184 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 185 | _root(root), |
| 186 | state() |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 187 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 188 | std::string p = path; |
| 189 | while (!p.empty() && p.back() == '/') |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 190 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 191 | p.pop_back(); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 192 | } |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 193 | |
| 194 | auto n = p.rfind('/'); |
| 195 | if (n != std::string::npos) |
| 196 | { |
| 197 | _instance.assign(p.substr(n + 1)); |
| 198 | _hwmonRoot.assign(p.substr(0, n)); |
| 199 | } |
| 200 | |
| 201 | assert(!_instance.empty()); |
| 202 | assert(!_hwmonRoot.empty()); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void MainLoop::shutdown() noexcept |
| 206 | { |
| 207 | _shutdown = true; |
| 208 | } |
| 209 | |
| 210 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 211 | { |
| 212 | // Check sysfs for available sensors. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 213 | auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 214 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 215 | for (auto& i : *sensors) |
| 216 | { |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 217 | // Get sensor configuration from the environment. |
| 218 | |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 219 | // Ignore inputs without a label. |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 220 | auto label = getEnv("LABEL", i.first); |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 221 | if (label.empty()) |
| 222 | { |
| 223 | continue; |
| 224 | } |
| 225 | |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 226 | Attributes attrs; |
| 227 | if (!getAttributes(i.first.first, attrs)) |
| 228 | { |
| 229 | continue; |
| 230 | } |
| 231 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 232 | std::string objectPath{_root}; |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 233 | objectPath.append(1, '/'); |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 234 | objectPath.append(getNamespace(attrs)); |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 235 | objectPath.append(1, '/'); |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 236 | objectPath.append(label); |
| 237 | |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 238 | ObjectInfo info(&_bus, std::move(objectPath), Object()); |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 239 | auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info); |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 240 | auto sensorValue = valueInterface->value(); |
| 241 | addThreshold<WarningObject>(i.first, sensorValue, info); |
| 242 | addThreshold<CriticalObject>(i.first, sensorValue, info); |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 243 | //TODO openbmc/openbmc#1347 |
| 244 | // Handle application restarts to set/refresh fan speed values |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame^] | 245 | addTarget<hwmon::FanSpeed>(i.first, _hwmonRoot, _instance, info); |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 246 | |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 247 | // All the interfaces have been created. Go ahead |
| 248 | // and emit InterfacesAdded. |
| 249 | valueInterface->emit_object_added(); |
| 250 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 251 | auto value = std::make_tuple( |
| 252 | std::move(i.second), |
| 253 | std::move(label), |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 254 | std::move(info)); |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 255 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 256 | state[std::move(i.first)] = std::move(value); |
| 257 | } |
| 258 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 259 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 260 | std::string busname{_prefix}; |
| 261 | busname.append(1, '.'); |
| 262 | busname.append(_instance); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 263 | _bus.request_name(busname.c_str()); |
| 264 | } |
| 265 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 266 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 267 | // ensure the objects all exist? |
| 268 | |
| 269 | // Polling loop. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 270 | while (!_shutdown) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 271 | { |
| 272 | // Iterate through all the sensors. |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 273 | for (auto& i : state) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 274 | { |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 275 | auto& attrs = std::get<0>(i.second); |
| 276 | if (attrs.find(hwmon::entry::input) != attrs.end()) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 277 | { |
| 278 | // Read value from sensor. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 279 | int value = readSysfsWithCallout(_hwmonRoot, |
| 280 | _instance, |
| 281 | i.first.first, |
| 282 | i.first.second, |
| 283 | hwmon::entry::input); |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 284 | auto& objInfo = std::get<ObjectInfo>(i.second); |
| 285 | auto& obj = std::get<Object>(objInfo); |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 286 | |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 287 | for (auto& iface : obj) |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 288 | { |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 289 | auto valueIface = std::shared_ptr<ValueObject>(); |
| 290 | auto warnIface = std::shared_ptr<WarningObject>(); |
| 291 | auto critIface = std::shared_ptr<CriticalObject>(); |
| 292 | |
| 293 | switch (iface.first) |
| 294 | { |
| 295 | case InterfaceType::VALUE: |
| 296 | valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>> |
| 297 | (iface.second); |
| 298 | valueIface->value(value); |
| 299 | break; |
| 300 | case InterfaceType::WARN: |
| 301 | checkThresholds<WarningObject>(iface.second, value); |
| 302 | break; |
| 303 | case InterfaceType::CRIT: |
| 304 | checkThresholds<CriticalObject>(iface.second, value); |
| 305 | break; |
| 306 | default: |
| 307 | break; |
| 308 | } |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 309 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 313 | // Respond to DBus |
| 314 | _bus.process_discard(); |
| 315 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 316 | // Sleep until next interval. |
| 317 | // TODO: Issue#5 - Make this configurable. |
| 318 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 319 | _bus.wait((1000000us).count()); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 320 | |
| 321 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 322 | // for new entries. |
| 323 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |