blob: b2ae7ffa41b8a079b6bad912b6967d0a8278bddc [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"
Brad Bishope55ef3d2016-12-19 09:12:40 -050023#include "hwmon.hpp"
24#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050025#include "mainloop.hpp"
Brad Bishopab795a12017-01-05 20:50:49 -050026#include "util.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050027#include "env.hpp"
Brad Bishope0b7d052017-01-06 15:30:23 -050028#include "thresholds.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050029
Brad Bishop9c7b6e02016-12-19 12:43:36 -050030using namespace std::literals::chrono_literals;
31
Brad Bishop74aa4dd2017-01-06 09:50:31 -050032static constexpr auto typeAttrMap =
33{
34 // 1 - hwmon class
35 // 2 - unit
36 // 3 - sysfs scaling factor
37 std::make_tuple(
38 hwmon::type::ctemp,
39 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050040 -3,
41 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050042 std::make_tuple(
43 hwmon::type::cfan,
44 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050045 0,
46 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050047 std::make_tuple(
48 hwmon::type::cvolt,
49 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050050 -3,
51 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050052 std::make_tuple(
53 hwmon::type::ccurr,
54 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050055 -3,
56 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050057 std::make_tuple(
58 hwmon::type::cenergy,
59 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050060 -6,
61 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050062 std::make_tuple(
63 hwmon::type::cpower,
64 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050065 -6,
66 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050067};
68
69auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
70{
71 return std::get<0>(attrs);
72}
73
74auto getUnit(decltype(typeAttrMap)::const_reference attrs)
75{
76 return std::get<1>(attrs);
77}
78
79auto getScale(decltype(typeAttrMap)::const_reference attrs)
80{
81 return std::get<2>(attrs);
82}
83
Brad Bishopadd98512017-01-06 22:01:19 -050084auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
85{
86 return std::get<3>(attrs);
87}
88
Brad Bishop951a79e2017-01-06 21:55:11 -050089using AttributeIterator = decltype(*typeAttrMap.begin());
90using Attributes
91 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
92
93auto getAttributes(const std::string& type, Attributes& attributes)
94{
95 // *INDENT-OFF*
96 auto a = std::find_if(
97 typeAttrMap.begin(),
98 typeAttrMap.end(),
99 [&](const auto & e)
100 {
101 return type == getHwmonType(e);
102 });
103 // *INDENT-ON*
104
105 if (a == typeAttrMap.end())
106 {
107 return false;
108 }
109
110 attributes = *a;
111 return true;
112}
113
Brad Bishope9fdee02017-01-06 10:43:29 -0500114auto addValue(const SensorSet::key_type& sensor,
115 const std::string& sysfsRoot, ObjectInfo& info)
116{
117 // 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
130 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str());
131 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 Bishopb9e2b072016-12-19 13:47:10 -0500152 _path(path),
153 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500154 _root(root),
155 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500156{
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500157 if (_path.back() == '/')
158 {
159 _path.pop_back();
160 }
Brad Bishopd499ca62016-12-19 09:24:50 -0500161}
162
163void MainLoop::shutdown() noexcept
164{
165 _shutdown = true;
166}
167
168void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500169{
170 // Check sysfs for available sensors.
Brad Bishopd499ca62016-12-19 09:24:50 -0500171 auto sensors = std::make_unique<SensorSet>(_path);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500172
Brad Bishop75b4ab82017-01-06 09:33:50 -0500173 for (auto& i : *sensors)
174 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500175 // Get sensor configuration from the environment.
176
Brad Bishop73831cd2017-01-06 09:37:22 -0500177 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500178 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500179 if (label.empty())
180 {
181 continue;
182 }
183
Brad Bishopadd98512017-01-06 22:01:19 -0500184 Attributes attrs;
185 if (!getAttributes(i.first.first, attrs))
186 {
187 continue;
188 }
189
Brad Bishop075f7a22017-01-06 09:45:08 -0500190 std::string objectPath{_root};
Brad Bishop075f7a22017-01-06 09:45:08 -0500191 objectPath.append("/");
Brad Bishopadd98512017-01-06 22:01:19 -0500192 objectPath.append(getNamespace(attrs));
Brad Bishop075f7a22017-01-06 09:45:08 -0500193 objectPath.append("/");
194 objectPath.append(label);
195
Brad Bishopf7426cf2017-01-06 15:36:43 -0500196 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishope9fdee02017-01-06 10:43:29 -0500197 auto valueInterface = addValue(i.first, _path, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500198 auto sensorValue = valueInterface->value();
199 addThreshold<WarningObject>(i.first, sensorValue, info);
200 addThreshold<CriticalObject>(i.first, sensorValue, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500201
202 auto value = std::make_tuple(
203 std::move(i.second),
204 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500205 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500206
Brad Bishop75b4ab82017-01-06 09:33:50 -0500207 state[std::move(i.first)] = std::move(value);
208 }
209
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500210 {
Brad Bishopab795a12017-01-05 20:50:49 -0500211 auto copy = std::unique_ptr<char, phosphor::utility::Free<char>>(strdup(
212 _path.c_str()));
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500213 auto busname = std::string(_prefix) + '.' + basename(copy.get());
214 _bus.request_name(busname.c_str());
215 }
216
Brad Bishope55ef3d2016-12-19 09:12:40 -0500217 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
218 // ensure the objects all exist?
219
220 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500221 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500222 {
223 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500224 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500225 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500226 auto& attrs = std::get<0>(i.second);
227 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500228 {
229 // Read value from sensor.
230 int value = 0;
Brad Bishopd499ca62016-12-19 09:24:50 -0500231 read_sysfs(make_sysfs_path(_path,
Brad Bishope55ef3d2016-12-19 09:12:40 -0500232 i.first.first, i.first.second,
233 hwmon::entry::input),
234 value);
235
Brad Bishopf7426cf2017-01-06 15:36:43 -0500236 auto& objInfo = std::get<ObjectInfo>(i.second);
237 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500238
Brad Bishope0b7d052017-01-06 15:30:23 -0500239 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500240 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500241 auto valueIface = std::shared_ptr<ValueObject>();
242 auto warnIface = std::shared_ptr<WarningObject>();
243 auto critIface = std::shared_ptr<CriticalObject>();
244
245 switch (iface.first)
246 {
247 case InterfaceType::VALUE:
248 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
249 (iface.second);
250 valueIface->value(value);
251 break;
252 case InterfaceType::WARN:
253 checkThresholds<WarningObject>(iface.second, value);
254 break;
255 case InterfaceType::CRIT:
256 checkThresholds<CriticalObject>(iface.second, value);
257 break;
258 default:
259 break;
260 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500261 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500262 }
263 }
264
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500265 // Respond to DBus
266 _bus.process_discard();
267
Brad Bishope55ef3d2016-12-19 09:12:40 -0500268 // Sleep until next interval.
269 // TODO: Issue#5 - Make this configurable.
270 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500271 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500272
273 // TODO: Issue#7 - Should probably periodically check the SensorSet
274 // for new entries.
275 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500276}
277
278// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4