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