blob: 7e579a84bc3935b53c63199e4faca93683ca5050 [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,
40 -3),
41 std::make_tuple(
42 hwmon::type::cfan,
43 ValueInterface::Unit::RPMS,
44 0),
45 std::make_tuple(
46 hwmon::type::cvolt,
47 ValueInterface::Unit::Volts,
48 -3),
Brad Bishop5afe21a2017-01-06 20:44:05 -050049 std::make_tuple(
50 hwmon::type::ccurr,
51 ValueInterface::Unit::Amperes,
52 -3),
53 std::make_tuple(
54 hwmon::type::cenergy,
55 ValueInterface::Unit::Joules,
56 -6),
57 std::make_tuple(
58 hwmon::type::cpower,
59 ValueInterface::Unit::Watts,
60 -6),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050061};
62
63auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
64{
65 return std::get<0>(attrs);
66}
67
68auto getUnit(decltype(typeAttrMap)::const_reference attrs)
69{
70 return std::get<1>(attrs);
71}
72
73auto getScale(decltype(typeAttrMap)::const_reference attrs)
74{
75 return std::get<2>(attrs);
76}
77
Brad Bishop951a79e2017-01-06 21:55:11 -050078using AttributeIterator = decltype(*typeAttrMap.begin());
79using Attributes
80 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
81
82auto getAttributes(const std::string& type, Attributes& attributes)
83{
84 // *INDENT-OFF*
85 auto a = std::find_if(
86 typeAttrMap.begin(),
87 typeAttrMap.end(),
88 [&](const auto & e)
89 {
90 return type == getHwmonType(e);
91 });
92 // *INDENT-ON*
93
94 if (a == typeAttrMap.end())
95 {
96 return false;
97 }
98
99 attributes = *a;
100 return true;
101}
102
Brad Bishope9fdee02017-01-06 10:43:29 -0500103auto addValue(const SensorSet::key_type& sensor,
104 const std::string& sysfsRoot, ObjectInfo& info)
105{
106 // Get the initial value for the value interface.
107 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
108 auto& obj = std::get<Object>(info);
109 auto& objPath = std::get<std::string>(info);
110
111 auto sysfsPath = make_sysfs_path(
112 sysfsRoot,
113 sensor.first,
114 sensor.second,
115 hwmon::entry::input);
116 int val = 0;
117 read_sysfs(sysfsPath, val);
118
119 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str());
120 iface->value(val);
121
Brad Bishop951a79e2017-01-06 21:55:11 -0500122 Attributes attrs;
123 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500124 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500125 iface->unit(getUnit(attrs));
126 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500127 }
128
129 obj[InterfaceType::VALUE] = iface;
130 return iface;
131}
132
Brad Bishopb9e2b072016-12-19 13:47:10 -0500133MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500134 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500135 const std::string& path,
136 const char* prefix,
137 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500138 : _bus(std::move(bus)),
139 _manager(sdbusplus::server::manager::manager(_bus, root)),
140 _shutdown(false),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500141 _path(path),
142 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500143 _root(root),
144 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500145{
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500146 if (_path.back() == '/')
147 {
148 _path.pop_back();
149 }
Brad Bishopd499ca62016-12-19 09:24:50 -0500150}
151
152void MainLoop::shutdown() noexcept
153{
154 _shutdown = true;
155}
156
157void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500158{
159 // Check sysfs for available sensors.
Brad Bishopd499ca62016-12-19 09:24:50 -0500160 auto sensors = std::make_unique<SensorSet>(_path);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500161
Brad Bishop75b4ab82017-01-06 09:33:50 -0500162 for (auto& i : *sensors)
163 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500164 // Get sensor configuration from the environment.
165
Brad Bishop73831cd2017-01-06 09:37:22 -0500166 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500167 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500168 if (label.empty())
169 {
170 continue;
171 }
172
Brad Bishop075f7a22017-01-06 09:45:08 -0500173 std::string objectPath{_root};
Brad Bishop075f7a22017-01-06 09:45:08 -0500174 objectPath.append("/");
175 objectPath.append(i.first.first);
176 objectPath.append("/");
177 objectPath.append(label);
178
Brad Bishopf7426cf2017-01-06 15:36:43 -0500179 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishope9fdee02017-01-06 10:43:29 -0500180 auto valueInterface = addValue(i.first, _path, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500181 auto sensorValue = valueInterface->value();
182 addThreshold<WarningObject>(i.first, sensorValue, info);
183 addThreshold<CriticalObject>(i.first, sensorValue, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500184
185 auto value = std::make_tuple(
186 std::move(i.second),
187 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500188 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500189
Brad Bishop75b4ab82017-01-06 09:33:50 -0500190 state[std::move(i.first)] = std::move(value);
191 }
192
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500193 {
Brad Bishopab795a12017-01-05 20:50:49 -0500194 auto copy = std::unique_ptr<char, phosphor::utility::Free<char>>(strdup(
195 _path.c_str()));
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500196 auto busname = std::string(_prefix) + '.' + basename(copy.get());
197 _bus.request_name(busname.c_str());
198 }
199
Brad Bishope55ef3d2016-12-19 09:12:40 -0500200 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
201 // ensure the objects all exist?
202
203 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500204 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500205 {
206 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500207 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500208 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500209 auto& attrs = std::get<0>(i.second);
210 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500211 {
212 // Read value from sensor.
213 int value = 0;
Brad Bishopd499ca62016-12-19 09:24:50 -0500214 read_sysfs(make_sysfs_path(_path,
Brad Bishope55ef3d2016-12-19 09:12:40 -0500215 i.first.first, i.first.second,
216 hwmon::entry::input),
217 value);
218
Brad Bishopf7426cf2017-01-06 15:36:43 -0500219 auto& objInfo = std::get<ObjectInfo>(i.second);
220 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500221
Brad Bishope0b7d052017-01-06 15:30:23 -0500222 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500223 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500224 auto valueIface = std::shared_ptr<ValueObject>();
225 auto warnIface = std::shared_ptr<WarningObject>();
226 auto critIface = std::shared_ptr<CriticalObject>();
227
228 switch (iface.first)
229 {
230 case InterfaceType::VALUE:
231 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
232 (iface.second);
233 valueIface->value(value);
234 break;
235 case InterfaceType::WARN:
236 checkThresholds<WarningObject>(iface.second, value);
237 break;
238 case InterfaceType::CRIT:
239 checkThresholds<CriticalObject>(iface.second, value);
240 break;
241 default:
242 break;
243 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500244 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500245 }
246 }
247
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500248 // Respond to DBus
249 _bus.process_discard();
250
Brad Bishope55ef3d2016-12-19 09:12:40 -0500251 // Sleep until next interval.
252 // TODO: Issue#5 - Make this configurable.
253 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500254 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500255
256 // TODO: Issue#7 - Should probably periodically check the SensorSet
257 // for new entries.
258 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500259}
260
261// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4