blob: 8eb418fa882ad505589d740cb3a08191155f94c3 [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"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060027#include "targets.hpp"
Matthew Barth048ac872017-03-09 14:36:08 -060028#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050029
Saqib Khan973886d2017-03-15 14:01:16 -050030// Initialization for Warning Objects
31decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
32 &WarningObject::warningLow;
33decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
34 &WarningObject::warningHigh;
35decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
36 &WarningObject::warningLow;
37decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
38 &WarningObject::warningHigh;
39decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
40 &WarningObject::warningAlarmLow;
41decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
42 &WarningObject::warningAlarmHigh;
43
44// Initialization for Critical Objects
45decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
46 &CriticalObject::criticalLow;
47decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
48 &CriticalObject::criticalHigh;
49decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
50 &CriticalObject::criticalLow;
51decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
52 &CriticalObject::criticalHigh;
53decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
54 &CriticalObject::criticalAlarmLow;
55decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
56 &CriticalObject::criticalAlarmHigh;
57
58
Brad Bishop9c7b6e02016-12-19 12:43:36 -050059using namespace std::literals::chrono_literals;
60
Brad Bishop74aa4dd2017-01-06 09:50:31 -050061static constexpr auto typeAttrMap =
62{
63 // 1 - hwmon class
64 // 2 - unit
65 // 3 - sysfs scaling factor
66 std::make_tuple(
67 hwmon::type::ctemp,
68 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050069 -3,
70 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050071 std::make_tuple(
72 hwmon::type::cfan,
73 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050074 0,
75 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050076 std::make_tuple(
77 hwmon::type::cvolt,
78 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050079 -3,
80 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050081 std::make_tuple(
82 hwmon::type::ccurr,
83 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050084 -3,
85 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050086 std::make_tuple(
87 hwmon::type::cenergy,
88 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050089 -6,
90 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050091 std::make_tuple(
92 hwmon::type::cpower,
93 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050094 -6,
95 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050096};
97
98auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
99{
100 return std::get<0>(attrs);
101}
102
103auto getUnit(decltype(typeAttrMap)::const_reference attrs)
104{
105 return std::get<1>(attrs);
106}
107
108auto getScale(decltype(typeAttrMap)::const_reference attrs)
109{
110 return std::get<2>(attrs);
111}
112
Brad Bishopadd98512017-01-06 22:01:19 -0500113auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
114{
115 return std::get<3>(attrs);
116}
117
Brad Bishop951a79e2017-01-06 21:55:11 -0500118using AttributeIterator = decltype(*typeAttrMap.begin());
119using Attributes
120 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
121
122auto getAttributes(const std::string& type, Attributes& attributes)
123{
124 // *INDENT-OFF*
125 auto a = std::find_if(
126 typeAttrMap.begin(),
127 typeAttrMap.end(),
128 [&](const auto & e)
129 {
130 return type == getHwmonType(e);
131 });
132 // *INDENT-ON*
133
134 if (a == typeAttrMap.end())
135 {
136 return false;
137 }
138
139 attributes = *a;
140 return true;
141}
142
Brad Bishope9fdee02017-01-06 10:43:29 -0500143auto addValue(const SensorSet::key_type& sensor,
Brad Bishop4db64422017-02-16 11:33:32 -0500144 const std::string& hwmonRoot,
145 const std::string& instance,
146 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500147{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500148 static constexpr bool deferSignals = true;
149
Brad Bishope9fdee02017-01-06 10:43:29 -0500150 // Get the initial value for the value interface.
151 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
152 auto& obj = std::get<Object>(info);
153 auto& objPath = std::get<std::string>(info);
154
Brad Bishop4db64422017-02-16 11:33:32 -0500155 int val = readSysfsWithCallout(hwmonRoot,
156 instance,
157 sensor.first,
158 sensor.second,
159 hwmon::entry::input);
Brad Bishop30dbcee2017-01-18 07:55:42 -0500160 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500161 iface->value(val);
162
Brad Bishop951a79e2017-01-06 21:55:11 -0500163 Attributes attrs;
164 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500165 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500166 iface->unit(getUnit(attrs));
167 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500168 }
169
170 obj[InterfaceType::VALUE] = iface;
171 return iface;
172}
173
Brad Bishopb9e2b072016-12-19 13:47:10 -0500174MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500175 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500176 const std::string& path,
177 const char* prefix,
178 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500179 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500180 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500181 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500182 _hwmonRoot(),
183 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500184 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500185 _root(root),
186 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500187{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500188 std::string p = path;
189 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500190 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500191 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500192 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500193
194 auto n = p.rfind('/');
195 if (n != std::string::npos)
196 {
197 _instance.assign(p.substr(n + 1));
198 _hwmonRoot.assign(p.substr(0, n));
199 }
200
201 assert(!_instance.empty());
202 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500203}
204
205void MainLoop::shutdown() noexcept
206{
207 _shutdown = true;
208}
209
210void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500211{
212 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500213 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500214
Brad Bishop75b4ab82017-01-06 09:33:50 -0500215 for (auto& i : *sensors)
216 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500217 // Get sensor configuration from the environment.
218
Brad Bishop73831cd2017-01-06 09:37:22 -0500219 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500220 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500221 if (label.empty())
222 {
223 continue;
224 }
225
Brad Bishopadd98512017-01-06 22:01:19 -0500226 Attributes attrs;
227 if (!getAttributes(i.first.first, attrs))
228 {
229 continue;
230 }
231
Brad Bishop075f7a22017-01-06 09:45:08 -0500232 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500233 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500234 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500235 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500236 objectPath.append(label);
237
Brad Bishopf7426cf2017-01-06 15:36:43 -0500238 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop4db64422017-02-16 11:33:32 -0500239 auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500240 auto sensorValue = valueInterface->value();
241 addThreshold<WarningObject>(i.first, sensorValue, info);
242 addThreshold<CriticalObject>(i.first, sensorValue, info);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600243 //TODO openbmc/openbmc#1347
244 // Handle application restarts to set/refresh fan speed values
Matt Spinler0a8de642017-05-11 10:59:39 -0500245 auto target = addTarget<hwmon::FanSpeed>(
246 i.first, _hwmonRoot, _instance, info);
247
248 if (target)
249 {
250 target->enable();
251 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500252
Brad Bishop30dbcee2017-01-18 07:55:42 -0500253 // All the interfaces have been created. Go ahead
254 // and emit InterfacesAdded.
255 valueInterface->emit_object_added();
256
Brad Bishop075f7a22017-01-06 09:45:08 -0500257 auto value = std::make_tuple(
258 std::move(i.second),
259 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500260 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500261
Brad Bishop75b4ab82017-01-06 09:33:50 -0500262 state[std::move(i.first)] = std::move(value);
263 }
264
Patrick Venture62503a42017-05-23 07:30:29 -0700265 /* If there are no sensors specified by labels, exit. */
266 if (0 == state.size())
267 {
268 return;
269 }
270
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500271 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500272 std::string busname{_prefix};
273 busname.append(1, '.');
274 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500275 _bus.request_name(busname.c_str());
276 }
277
Brad Bishope55ef3d2016-12-19 09:12:40 -0500278 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
279 // ensure the objects all exist?
280
281 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500282 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500283 {
284 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500285 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500286 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500287 auto& attrs = std::get<0>(i.second);
288 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500289 {
290 // Read value from sensor.
Brad Bishop4db64422017-02-16 11:33:32 -0500291 int value = readSysfsWithCallout(_hwmonRoot,
292 _instance,
293 i.first.first,
294 i.first.second,
295 hwmon::entry::input);
Brad Bishopf7426cf2017-01-06 15:36:43 -0500296 auto& objInfo = std::get<ObjectInfo>(i.second);
297 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500298
Brad Bishope0b7d052017-01-06 15:30:23 -0500299 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500300 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500301 auto valueIface = std::shared_ptr<ValueObject>();
302 auto warnIface = std::shared_ptr<WarningObject>();
303 auto critIface = std::shared_ptr<CriticalObject>();
304
305 switch (iface.first)
306 {
307 case InterfaceType::VALUE:
308 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
309 (iface.second);
310 valueIface->value(value);
311 break;
312 case InterfaceType::WARN:
313 checkThresholds<WarningObject>(iface.second, value);
314 break;
315 case InterfaceType::CRIT:
316 checkThresholds<CriticalObject>(iface.second, value);
317 break;
318 default:
319 break;
320 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500321 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500322 }
323 }
324
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500325 // Respond to DBus
326 _bus.process_discard();
327
Brad Bishope55ef3d2016-12-19 09:12:40 -0500328 // Sleep until next interval.
329 // TODO: Issue#5 - Make this configurable.
330 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500331 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500332
333 // TODO: Issue#7 - Should probably periodically check the SensorSet
334 // for new entries.
335 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500336}
337
338// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4