blob: a6758d44686ca8d2537f9368f6dbfdff2a06834d [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001/**
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 Bishop9c7b6e02016-12-19 12:43:36 -050018#include <cstring>
19#include <cstdlib>
20#include <chrono>
Brad Bishope55ef3d2016-12-19 09:12:40 -050021#include "sensorset.hpp"
22#include "sensorcache.hpp"
23#include "hwmon.hpp"
24#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050025#include "mainloop.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050026
Brad Bishop9c7b6e02016-12-19 12:43:36 -050027using namespace std::literals::chrono_literals;
28
Brad Bishopb9e2b072016-12-19 13:47:10 -050029MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050030 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050031 const std::string& path,
32 const char* prefix,
33 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -050034 : _bus(std::move(bus)),
35 _manager(sdbusplus::server::manager::manager(_bus, root)),
36 _shutdown(false),
Brad Bishopb9e2b072016-12-19 13:47:10 -050037 _path(path),
38 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -050039 _root(root),
40 state()
Brad Bishopd499ca62016-12-19 09:24:50 -050041{
Brad Bishop9c7b6e02016-12-19 12:43:36 -050042 if (_path.back() == '/')
43 {
44 _path.pop_back();
45 }
Brad Bishopd499ca62016-12-19 09:24:50 -050046}
47
48void MainLoop::shutdown() noexcept
49{
50 _shutdown = true;
51}
52
53void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -050054{
55 // Check sysfs for available sensors.
Brad Bishopd499ca62016-12-19 09:24:50 -050056 auto sensors = std::make_unique<SensorSet>(_path);
Brad Bishope55ef3d2016-12-19 09:12:40 -050057 auto sensor_cache = std::make_unique<SensorCache>();
58
Brad Bishop75b4ab82017-01-06 09:33:50 -050059 for (auto& i : *sensors)
60 {
Brad Bishop73831cd2017-01-06 09:37:22 -050061 // 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 Bishop47378502017-01-06 09:47:41 -050077 // Get the initial value for the value interface.
78 auto sysfsPath = make_sysfs_path(
79 _path,
80 i.first.first,
81 i.first.second,
82 hwmon::entry::input);
83 int val = 0;
84 read_sysfs(sysfsPath, val);
85
Brad Bishop075f7a22017-01-06 09:45:08 -050086 Object o;
87 std::string objectPath{_root};
88
89 objectPath.append("/");
90 objectPath.append(i.first.first);
91 objectPath.append("/");
92 objectPath.append(label);
93
94 auto iface = std::make_shared<ValueObject>(_bus, objectPath.c_str());
Brad Bishop47378502017-01-06 09:47:41 -050095 iface->value(val);
Brad Bishop075f7a22017-01-06 09:45:08 -050096 o.emplace(InterfaceType::VALUE, iface);
97
98 auto value = std::make_tuple(
99 std::move(i.second),
100 std::move(label),
101 std::move(o));
Brad Bishop73831cd2017-01-06 09:37:22 -0500102
Brad Bishop75b4ab82017-01-06 09:33:50 -0500103 state[std::move(i.first)] = std::move(value);
104 }
105
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500106 {
107 struct Free
108 {
109 void operator()(char* ptr) const
110 {
111 free(ptr);
112 }
113 };
114
115 auto copy = std::unique_ptr<char, Free>(strdup(_path.c_str()));
116 auto busname = std::string(_prefix) + '.' + basename(copy.get());
117 _bus.request_name(busname.c_str());
118 }
119
Brad Bishope55ef3d2016-12-19 09:12:40 -0500120 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
121 // ensure the objects all exist?
122
123 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500124 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500125 {
126 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500127 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500128 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500129 auto& attrs = std::get<0>(i.second);
130 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500131 {
132 // Read value from sensor.
133 int value = 0;
Brad Bishopd499ca62016-12-19 09:24:50 -0500134 read_sysfs(make_sysfs_path(_path,
Brad Bishope55ef3d2016-12-19 09:12:40 -0500135 i.first.first, i.first.second,
136 hwmon::entry::input),
137 value);
138
139 // Update sensor cache.
140 if (sensor_cache->update(i.first, value))
141 {
142 // TODO: Issue#4 - dbus event here.
143 std::cout << i.first.first << i.first.second << " = "
144 << value << std::endl;
145 }
146 }
147 }
148
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500149 // Respond to DBus
150 _bus.process_discard();
151
Brad Bishope55ef3d2016-12-19 09:12:40 -0500152 // Sleep until next interval.
153 // TODO: Issue#5 - Make this configurable.
154 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500155 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500156
157 // TODO: Issue#7 - Should probably periodically check the SensorSet
158 // for new entries.
159 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500160}
161
162// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4