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 | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 28 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 29 | using namespace std::literals::chrono_literals; |
| 30 | |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 31 | static constexpr auto typeAttrMap = |
| 32 | { |
| 33 | // 1 - hwmon class |
| 34 | // 2 - unit |
| 35 | // 3 - sysfs scaling factor |
| 36 | std::make_tuple( |
| 37 | hwmon::type::ctemp, |
| 38 | ValueInterface::Unit::DegreesC, |
| 39 | -3), |
| 40 | std::make_tuple( |
| 41 | hwmon::type::cfan, |
| 42 | ValueInterface::Unit::RPMS, |
| 43 | 0), |
| 44 | std::make_tuple( |
| 45 | hwmon::type::cvolt, |
| 46 | ValueInterface::Unit::Volts, |
| 47 | -3), |
| 48 | }; |
| 49 | |
| 50 | auto getHwmonType(decltype(typeAttrMap)::const_reference attrs) |
| 51 | { |
| 52 | return std::get<0>(attrs); |
| 53 | } |
| 54 | |
| 55 | auto getUnit(decltype(typeAttrMap)::const_reference attrs) |
| 56 | { |
| 57 | return std::get<1>(attrs); |
| 58 | } |
| 59 | |
| 60 | auto getScale(decltype(typeAttrMap)::const_reference attrs) |
| 61 | { |
| 62 | return std::get<2>(attrs); |
| 63 | } |
| 64 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 65 | auto addValue(const SensorSet::key_type& sensor, |
| 66 | const std::string& sysfsRoot, ObjectInfo& info) |
| 67 | { |
| 68 | // Get the initial value for the value interface. |
| 69 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 70 | auto& obj = std::get<Object>(info); |
| 71 | auto& objPath = std::get<std::string>(info); |
| 72 | |
| 73 | auto sysfsPath = make_sysfs_path( |
| 74 | sysfsRoot, |
| 75 | sensor.first, |
| 76 | sensor.second, |
| 77 | hwmon::entry::input); |
| 78 | int val = 0; |
| 79 | read_sysfs(sysfsPath, val); |
| 80 | |
| 81 | auto iface = std::make_shared<ValueObject>(bus, objPath.c_str()); |
| 82 | iface->value(val); |
| 83 | |
| 84 | // *INDENT-OFF* |
| 85 | const auto& attrs = std::find_if( |
| 86 | typeAttrMap.begin(), |
| 87 | typeAttrMap.end(), |
| 88 | [&](const auto & e) |
| 89 | { |
| 90 | return sensor.first == getHwmonType(e); |
| 91 | }); |
| 92 | // *INDENT-ON* |
| 93 | |
| 94 | if (attrs != typeAttrMap.end()) |
| 95 | { |
| 96 | iface->unit(getUnit(*attrs)); |
| 97 | iface->scale(getScale(*attrs)); |
| 98 | } |
| 99 | |
| 100 | obj[InterfaceType::VALUE] = iface; |
| 101 | return iface; |
| 102 | } |
| 103 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 104 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 105 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 106 | const std::string& path, |
| 107 | const char* prefix, |
| 108 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 109 | : _bus(std::move(bus)), |
| 110 | _manager(sdbusplus::server::manager::manager(_bus, root)), |
| 111 | _shutdown(false), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 112 | _path(path), |
| 113 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 114 | _root(root), |
| 115 | state() |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 116 | { |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 117 | if (_path.back() == '/') |
| 118 | { |
| 119 | _path.pop_back(); |
| 120 | } |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void MainLoop::shutdown() noexcept |
| 124 | { |
| 125 | _shutdown = true; |
| 126 | } |
| 127 | |
| 128 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 129 | { |
| 130 | // Check sysfs for available sensors. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 131 | auto sensors = std::make_unique<SensorSet>(_path); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 132 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 133 | for (auto& i : *sensors) |
| 134 | { |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 135 | // Get sensor configuration from the environment. |
| 136 | |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 137 | // Ignore inputs without a label. |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 138 | auto label = getEnv("LABEL", i.first); |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 139 | if (label.empty()) |
| 140 | { |
| 141 | continue; |
| 142 | } |
| 143 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 144 | std::string objectPath{_root}; |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 145 | objectPath.append("/"); |
| 146 | objectPath.append(i.first.first); |
| 147 | objectPath.append("/"); |
| 148 | objectPath.append(label); |
| 149 | |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 150 | ObjectInfo info(&_bus, std::move(objectPath), Object()); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 151 | auto valueInterface = addValue(i.first, _path, info); |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 152 | |
| 153 | auto value = std::make_tuple( |
| 154 | std::move(i.second), |
| 155 | std::move(label), |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 156 | std::move(info)); |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 157 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 158 | state[std::move(i.first)] = std::move(value); |
| 159 | } |
| 160 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 161 | { |
Brad Bishop | ab795a1 | 2017-01-05 20:50:49 -0500 | [diff] [blame] | 162 | auto copy = std::unique_ptr<char, phosphor::utility::Free<char>>(strdup( |
| 163 | _path.c_str())); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 164 | auto busname = std::string(_prefix) + '.' + basename(copy.get()); |
| 165 | _bus.request_name(busname.c_str()); |
| 166 | } |
| 167 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 168 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 169 | // ensure the objects all exist? |
| 170 | |
| 171 | // Polling loop. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 172 | while (!_shutdown) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 173 | { |
| 174 | // Iterate through all the sensors. |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 175 | for (auto& i : state) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 176 | { |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 177 | auto& attrs = std::get<0>(i.second); |
| 178 | if (attrs.find(hwmon::entry::input) != attrs.end()) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 179 | { |
| 180 | // Read value from sensor. |
| 181 | int value = 0; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 182 | read_sysfs(make_sysfs_path(_path, |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 183 | i.first.first, i.first.second, |
| 184 | hwmon::entry::input), |
| 185 | value); |
| 186 | |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 187 | auto& objInfo = std::get<ObjectInfo>(i.second); |
| 188 | auto& obj = std::get<Object>(objInfo); |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 189 | auto iface = obj.find(InterfaceType::VALUE); |
| 190 | |
| 191 | if (iface != obj.end()) |
| 192 | { |
| 193 | auto realIface = std::experimental::any_cast<std::shared_ptr<ValueObject>> |
| 194 | (iface->second); |
| 195 | realIface->value(value); |
| 196 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 200 | // Respond to DBus |
| 201 | _bus.process_discard(); |
| 202 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 203 | // Sleep until next interval. |
| 204 | // TODO: Issue#5 - Make this configurable. |
| 205 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 206 | _bus.wait((1000000us).count()); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 207 | |
| 208 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 209 | // for new entries. |
| 210 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |