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 | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 21 | #include "sensorset.hpp" |
| 22 | #include "sensorcache.hpp" |
| 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 | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 26 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 27 | using namespace std::literals::chrono_literals; |
| 28 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 29 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 30 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 31 | const std::string& path, |
| 32 | const char* prefix, |
| 33 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 34 | : _bus(std::move(bus)), |
| 35 | _manager(sdbusplus::server::manager::manager(_bus, root)), |
| 36 | _shutdown(false), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 37 | _path(path), |
| 38 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 39 | _root(root), |
| 40 | state() |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 41 | { |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 42 | if (_path.back() == '/') |
| 43 | { |
| 44 | _path.pop_back(); |
| 45 | } |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void MainLoop::shutdown() noexcept |
| 49 | { |
| 50 | _shutdown = true; |
| 51 | } |
| 52 | |
| 53 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 54 | { |
| 55 | // Check sysfs for available sensors. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 56 | auto sensors = std::make_unique<SensorSet>(_path); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 57 | auto sensor_cache = std::make_unique<SensorCache>(); |
| 58 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 59 | for (auto& i : *sensors) |
| 60 | { |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 61 | // Ignore inputs without a label. |
| 62 | std::string envKey = "LABEL_" + i.first.first + i.first.second; |
| 63 | std::string label; |
| 64 | |
| 65 | auto env = getenv(envKey.c_str()); |
| 66 | |
| 67 | if (env) |
| 68 | { |
| 69 | label.assign(env); |
| 70 | } |
| 71 | |
| 72 | if (label.empty()) |
| 73 | { |
| 74 | continue; |
| 75 | } |
| 76 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 77 | Object o; |
| 78 | std::string objectPath{_root}; |
| 79 | |
| 80 | objectPath.append("/"); |
| 81 | objectPath.append(i.first.first); |
| 82 | objectPath.append("/"); |
| 83 | objectPath.append(label); |
| 84 | |
| 85 | auto iface = std::make_shared<ValueObject>(_bus, objectPath.c_str()); |
| 86 | o.emplace(InterfaceType::VALUE, iface); |
| 87 | |
| 88 | auto value = std::make_tuple( |
| 89 | std::move(i.second), |
| 90 | std::move(label), |
| 91 | std::move(o)); |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 92 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 93 | state[std::move(i.first)] = std::move(value); |
| 94 | } |
| 95 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 96 | { |
| 97 | struct Free |
| 98 | { |
| 99 | void operator()(char* ptr) const |
| 100 | { |
| 101 | free(ptr); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | auto copy = std::unique_ptr<char, Free>(strdup(_path.c_str())); |
| 106 | auto busname = std::string(_prefix) + '.' + basename(copy.get()); |
| 107 | _bus.request_name(busname.c_str()); |
| 108 | } |
| 109 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 110 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 111 | // ensure the objects all exist? |
| 112 | |
| 113 | // Polling loop. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 114 | while (!_shutdown) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 115 | { |
| 116 | // Iterate through all the sensors. |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 117 | for (auto& i : state) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 118 | { |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 119 | auto& attrs = std::get<0>(i.second); |
| 120 | if (attrs.find(hwmon::entry::input) != attrs.end()) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 121 | { |
| 122 | // Read value from sensor. |
| 123 | int value = 0; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 124 | read_sysfs(make_sysfs_path(_path, |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 125 | i.first.first, i.first.second, |
| 126 | hwmon::entry::input), |
| 127 | value); |
| 128 | |
| 129 | // Update sensor cache. |
| 130 | if (sensor_cache->update(i.first, value)) |
| 131 | { |
| 132 | // TODO: Issue#4 - dbus event here. |
| 133 | std::cout << i.first.first << i.first.second << " = " |
| 134 | << value << std::endl; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 139 | // Respond to DBus |
| 140 | _bus.process_discard(); |
| 141 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 142 | // Sleep until next interval. |
| 143 | // TODO: Issue#5 - Make this configurable. |
| 144 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 145 | _bus.wait((1000000us).count()); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 146 | |
| 147 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 148 | // for new entries. |
| 149 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |