blob: 31b02ac5dec1e4f4fb6eeed78caf4ac5529ae19e [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 <cstdlib>
19#include <chrono>
Brad Bishop74aa4dd2017-01-06 09:50:31 -050020#include <algorithm>
Brad Bishope55ef3d2016-12-19 09:12:40 -050021#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050022#include "hwmon.hpp"
23#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050024#include "mainloop.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050025#include "env.hpp"
Brad Bishope0b7d052017-01-06 15:30:23 -050026#include "thresholds.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,
Brad Bishopadd98512017-01-06 22:01:19 -050038 -3,
39 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050040 std::make_tuple(
41 hwmon::type::cfan,
42 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050043 0,
44 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050045 std::make_tuple(
46 hwmon::type::cvolt,
47 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050048 -3,
49 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050050 std::make_tuple(
51 hwmon::type::ccurr,
52 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050053 -3,
54 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050055 std::make_tuple(
56 hwmon::type::cenergy,
57 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050058 -6,
59 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050060 std::make_tuple(
61 hwmon::type::cpower,
62 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050063 -6,
64 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050065};
66
67auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
68{
69 return std::get<0>(attrs);
70}
71
72auto getUnit(decltype(typeAttrMap)::const_reference attrs)
73{
74 return std::get<1>(attrs);
75}
76
77auto getScale(decltype(typeAttrMap)::const_reference attrs)
78{
79 return std::get<2>(attrs);
80}
81
Brad Bishopadd98512017-01-06 22:01:19 -050082auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
83{
84 return std::get<3>(attrs);
85}
86
Brad Bishop951a79e2017-01-06 21:55:11 -050087using AttributeIterator = decltype(*typeAttrMap.begin());
88using Attributes
89 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
90
91auto getAttributes(const std::string& type, Attributes& attributes)
92{
93 // *INDENT-OFF*
94 auto a = std::find_if(
95 typeAttrMap.begin(),
96 typeAttrMap.end(),
97 [&](const auto & e)
98 {
99 return type == getHwmonType(e);
100 });
101 // *INDENT-ON*
102
103 if (a == typeAttrMap.end())
104 {
105 return false;
106 }
107
108 attributes = *a;
109 return true;
110}
111
Brad Bishope9fdee02017-01-06 10:43:29 -0500112auto addValue(const SensorSet::key_type& sensor,
113 const std::string& sysfsRoot, ObjectInfo& info)
114{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500115 static constexpr bool deferSignals = true;
116
Brad Bishope9fdee02017-01-06 10:43:29 -0500117 // Get the initial value for the value interface.
118 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
119 auto& obj = std::get<Object>(info);
120 auto& objPath = std::get<std::string>(info);
121
122 auto sysfsPath = make_sysfs_path(
123 sysfsRoot,
124 sensor.first,
125 sensor.second,
126 hwmon::entry::input);
127 int val = 0;
128 read_sysfs(sysfsPath, val);
129
Brad Bishop30dbcee2017-01-18 07:55:42 -0500130 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500131 iface->value(val);
132
Brad Bishop951a79e2017-01-06 21:55:11 -0500133 Attributes attrs;
134 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500135 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500136 iface->unit(getUnit(attrs));
137 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500138 }
139
140 obj[InterfaceType::VALUE] = iface;
141 return iface;
142}
143
Brad Bishopb9e2b072016-12-19 13:47:10 -0500144MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500145 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500146 const std::string& path,
147 const char* prefix,
148 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500149 : _bus(std::move(bus)),
150 _manager(sdbusplus::server::manager::manager(_bus, root)),
151 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500152 _hwmonRoot(),
153 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500154 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500155 _root(root),
156 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500157{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500158 std::string p = path;
159 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500160 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500161 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500162 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500163
164 auto n = p.rfind('/');
165 if (n != std::string::npos)
166 {
167 _instance.assign(p.substr(n + 1));
168 _hwmonRoot.assign(p.substr(0, n));
169 }
170
171 assert(!_instance.empty());
172 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500173}
174
175void MainLoop::shutdown() noexcept
176{
177 _shutdown = true;
178}
179
180void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500181{
182 // Check sysfs for available sensors.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500183 std::string hwmonPath = _hwmonRoot + '/' + _instance;
184 auto sensors = std::make_unique<SensorSet>(hwmonPath);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500185
Brad Bishop75b4ab82017-01-06 09:33:50 -0500186 for (auto& i : *sensors)
187 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500188 // Get sensor configuration from the environment.
189
Brad Bishop73831cd2017-01-06 09:37:22 -0500190 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500191 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500192 if (label.empty())
193 {
194 continue;
195 }
196
Brad Bishopadd98512017-01-06 22:01:19 -0500197 Attributes attrs;
198 if (!getAttributes(i.first.first, attrs))
199 {
200 continue;
201 }
202
Brad Bishop075f7a22017-01-06 09:45:08 -0500203 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500204 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500205 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500206 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500207 objectPath.append(label);
208
Brad Bishopf7426cf2017-01-06 15:36:43 -0500209 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishopb8740fc2017-02-24 23:38:37 -0500210 auto valueInterface = addValue(i.first, hwmonPath, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500211 auto sensorValue = valueInterface->value();
212 addThreshold<WarningObject>(i.first, sensorValue, info);
213 addThreshold<CriticalObject>(i.first, sensorValue, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500214
Brad Bishop30dbcee2017-01-18 07:55:42 -0500215 // All the interfaces have been created. Go ahead
216 // and emit InterfacesAdded.
217 valueInterface->emit_object_added();
218
Brad Bishop075f7a22017-01-06 09:45:08 -0500219 auto value = std::make_tuple(
220 std::move(i.second),
221 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500222 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500223
Brad Bishop75b4ab82017-01-06 09:33:50 -0500224 state[std::move(i.first)] = std::move(value);
225 }
226
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500227 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500228 std::string busname{_prefix};
229 busname.append(1, '.');
230 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500231 _bus.request_name(busname.c_str());
232 }
233
Brad Bishope55ef3d2016-12-19 09:12:40 -0500234 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
235 // ensure the objects all exist?
236
237 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500238 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500239 {
240 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500241 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500242 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500243 auto& attrs = std::get<0>(i.second);
244 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500245 {
246 // Read value from sensor.
247 int value = 0;
Brad Bishopb8740fc2017-02-24 23:38:37 -0500248 read_sysfs(make_sysfs_path(hwmonPath,
Brad Bishope55ef3d2016-12-19 09:12:40 -0500249 i.first.first, i.first.second,
250 hwmon::entry::input),
251 value);
252
Brad Bishopf7426cf2017-01-06 15:36:43 -0500253 auto& objInfo = std::get<ObjectInfo>(i.second);
254 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500255
Brad Bishope0b7d052017-01-06 15:30:23 -0500256 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500257 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500258 auto valueIface = std::shared_ptr<ValueObject>();
259 auto warnIface = std::shared_ptr<WarningObject>();
260 auto critIface = std::shared_ptr<CriticalObject>();
261
262 switch (iface.first)
263 {
264 case InterfaceType::VALUE:
265 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
266 (iface.second);
267 valueIface->value(value);
268 break;
269 case InterfaceType::WARN:
270 checkThresholds<WarningObject>(iface.second, value);
271 break;
272 case InterfaceType::CRIT:
273 checkThresholds<CriticalObject>(iface.second, value);
274 break;
275 default:
276 break;
277 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500278 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500279 }
280 }
281
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500282 // Respond to DBus
283 _bus.process_discard();
284
Brad Bishope55ef3d2016-12-19 09:12:40 -0500285 // Sleep until next interval.
286 // TODO: Issue#5 - Make this configurable.
287 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500288 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500289
290 // TODO: Issue#7 - Should probably periodically check the SensorSet
291 // for new entries.
292 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500293}
294
295// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4