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), |
| 39 | _root(root) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 40 | { |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 41 | if (_path.back() == '/') |
| 42 | { |
| 43 | _path.pop_back(); |
| 44 | } |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void MainLoop::shutdown() noexcept |
| 48 | { |
| 49 | _shutdown = true; |
| 50 | } |
| 51 | |
| 52 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 53 | { |
| 54 | // Check sysfs for available sensors. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 55 | auto sensors = std::make_unique<SensorSet>(_path); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 56 | auto sensor_cache = std::make_unique<SensorCache>(); |
| 57 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 58 | { |
| 59 | struct Free |
| 60 | { |
| 61 | void operator()(char* ptr) const |
| 62 | { |
| 63 | free(ptr); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | auto copy = std::unique_ptr<char, Free>(strdup(_path.c_str())); |
| 68 | auto busname = std::string(_prefix) + '.' + basename(copy.get()); |
| 69 | _bus.request_name(busname.c_str()); |
| 70 | } |
| 71 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 72 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 73 | // ensure the objects all exist? |
| 74 | |
| 75 | // Polling loop. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 76 | while (!_shutdown) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 77 | { |
| 78 | // Iterate through all the sensors. |
| 79 | for (auto& i : *sensors) |
| 80 | { |
| 81 | if (i.second.find(hwmon::entry::input) != i.second.end()) |
| 82 | { |
| 83 | // Read value from sensor. |
| 84 | int value = 0; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 85 | read_sysfs(make_sysfs_path(_path, |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 86 | i.first.first, i.first.second, |
| 87 | hwmon::entry::input), |
| 88 | value); |
| 89 | |
| 90 | // Update sensor cache. |
| 91 | if (sensor_cache->update(i.first, value)) |
| 92 | { |
| 93 | // TODO: Issue#4 - dbus event here. |
| 94 | std::cout << i.first.first << i.first.second << " = " |
| 95 | << value << std::endl; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 100 | // Respond to DBus |
| 101 | _bus.process_discard(); |
| 102 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 103 | // Sleep until next interval. |
| 104 | // TODO: Issue#5 - Make this configurable. |
| 105 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 106 | _bus.wait((1000000us).count()); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 107 | |
| 108 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 109 | // for new entries. |
| 110 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |