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