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