blob: 4470504a905eea9b8498041b67b62008dd21819c [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 Bishop74aa4dd2017-01-06 09:50:31 -050021#include <algorithm>
Brad Bishope55ef3d2016-12-19 09:12:40 -050022#include "sensorset.hpp"
23#include "sensorcache.hpp"
24#include "hwmon.hpp"
25#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050026#include "mainloop.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050027
Brad Bishop9c7b6e02016-12-19 12:43:36 -050028using namespace std::literals::chrono_literals;
29
Brad Bishop74aa4dd2017-01-06 09:50:31 -050030static 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
49auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
50{
51 return std::get<0>(attrs);
52}
53
54auto getUnit(decltype(typeAttrMap)::const_reference attrs)
55{
56 return std::get<1>(attrs);
57}
58
59auto getScale(decltype(typeAttrMap)::const_reference attrs)
60{
61 return std::get<2>(attrs);
62}
63
Brad Bishopb9e2b072016-12-19 13:47:10 -050064MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050065 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050066 const std::string& path,
67 const char* prefix,
68 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -050069 : _bus(std::move(bus)),
70 _manager(sdbusplus::server::manager::manager(_bus, root)),
71 _shutdown(false),
Brad Bishopb9e2b072016-12-19 13:47:10 -050072 _path(path),
73 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -050074 _root(root),
75 state()
Brad Bishopd499ca62016-12-19 09:24:50 -050076{
Brad Bishop9c7b6e02016-12-19 12:43:36 -050077 if (_path.back() == '/')
78 {
79 _path.pop_back();
80 }
Brad Bishopd499ca62016-12-19 09:24:50 -050081}
82
83void MainLoop::shutdown() noexcept
84{
85 _shutdown = true;
86}
87
88void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -050089{
90 // Check sysfs for available sensors.
Brad Bishopd499ca62016-12-19 09:24:50 -050091 auto sensors = std::make_unique<SensorSet>(_path);
Brad Bishope55ef3d2016-12-19 09:12:40 -050092 auto sensor_cache = std::make_unique<SensorCache>();
93
Brad Bishop75b4ab82017-01-06 09:33:50 -050094 for (auto& i : *sensors)
95 {
Brad Bishop73831cd2017-01-06 09:37:22 -050096 // Ignore inputs without a label.
97 std::string envKey = "LABEL_" + i.first.first + i.first.second;
98 std::string label;
99
100 auto env = getenv(envKey.c_str());
101
102 if (env)
103 {
104 label.assign(env);
105 }
106
107 if (label.empty())
108 {
109 continue;
110 }
111
Brad Bishop47378502017-01-06 09:47:41 -0500112 // Get the initial value for the value interface.
113 auto sysfsPath = make_sysfs_path(
114 _path,
115 i.first.first,
116 i.first.second,
117 hwmon::entry::input);
118 int val = 0;
119 read_sysfs(sysfsPath, val);
120
Brad Bishop075f7a22017-01-06 09:45:08 -0500121 Object o;
122 std::string objectPath{_root};
123
124 objectPath.append("/");
125 objectPath.append(i.first.first);
126 objectPath.append("/");
127 objectPath.append(label);
128
129 auto iface = std::make_shared<ValueObject>(_bus, objectPath.c_str());
Brad Bishop47378502017-01-06 09:47:41 -0500130 iface->value(val);
Brad Bishop74aa4dd2017-01-06 09:50:31 -0500131
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 Bishop075f7a22017-01-06 09:45:08 -0500145 o.emplace(InterfaceType::VALUE, iface);
146
147 auto value = std::make_tuple(
148 std::move(i.second),
149 std::move(label),
150 std::move(o));
Brad Bishop73831cd2017-01-06 09:37:22 -0500151
Brad Bishop75b4ab82017-01-06 09:33:50 -0500152 state[std::move(i.first)] = std::move(value);
153 }
154
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500155 {
156 struct Free
157 {
158 void operator()(char* ptr) const
159 {
160 free(ptr);
161 }
162 };
163
164 auto copy = std::unique_ptr<char, Free>(strdup(_path.c_str()));
165 auto busname = std::string(_prefix) + '.' + basename(copy.get());
166 _bus.request_name(busname.c_str());
167 }
168
Brad Bishope55ef3d2016-12-19 09:12:40 -0500169 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
170 // ensure the objects all exist?
171
172 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500173 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500174 {
175 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500176 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500177 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500178 auto& attrs = std::get<0>(i.second);
179 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500180 {
181 // Read value from sensor.
182 int value = 0;
Brad Bishopd499ca62016-12-19 09:24:50 -0500183 read_sysfs(make_sysfs_path(_path,
Brad Bishope55ef3d2016-12-19 09:12:40 -0500184 i.first.first, i.first.second,
185 hwmon::entry::input),
186 value);
187
188 // Update sensor cache.
189 if (sensor_cache->update(i.first, value))
190 {
191 // TODO: Issue#4 - dbus event here.
192 std::cout << i.first.first << i.first.second << " = "
193 << value << std::endl;
194 }
195 }
196 }
197
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500198 // Respond to DBus
199 _bus.process_discard();
200
Brad Bishope55ef3d2016-12-19 09:12:40 -0500201 // Sleep until next interval.
202 // TODO: Issue#5 - Make this configurable.
203 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500204 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500205
206 // TODO: Issue#7 - Should probably periodically check the SensorSet
207 // for new entries.
208 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500209}
210
211// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4