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