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