blob: 1da6b6905f773d5c32a324af559868cb1dbcc3b8 [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>
Brad Bishop74aa4dd2017-01-06 09:50:31 -050019#include <algorithm>
Brad Bishope55ef3d2016-12-19 09:12:40 -050020#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050021#include "hwmon.hpp"
22#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050023#include "mainloop.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050024#include "env.hpp"
Brad Bishope0b7d052017-01-06 15:30:23 -050025#include "thresholds.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060026#include "targets.hpp"
Matthew Barth048ac872017-03-09 14:36:08 -060027#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050028
Saqib Khan973886d2017-03-15 14:01:16 -050029// Initialization for Warning Objects
30decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
31 &WarningObject::warningLow;
32decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
33 &WarningObject::warningHigh;
34decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
35 &WarningObject::warningLow;
36decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
37 &WarningObject::warningHigh;
38decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
39 &WarningObject::warningAlarmLow;
40decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
41 &WarningObject::warningAlarmHigh;
42
43// Initialization for Critical Objects
44decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
45 &CriticalObject::criticalLow;
46decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
47 &CriticalObject::criticalHigh;
48decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
49 &CriticalObject::criticalLow;
50decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
51 &CriticalObject::criticalHigh;
52decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
53 &CriticalObject::criticalAlarmLow;
54decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
55 &CriticalObject::criticalAlarmHigh;
56
57
Brad Bishop9c7b6e02016-12-19 12:43:36 -050058
Brad Bishop74aa4dd2017-01-06 09:50:31 -050059static constexpr auto typeAttrMap =
60{
61 // 1 - hwmon class
62 // 2 - unit
63 // 3 - sysfs scaling factor
64 std::make_tuple(
65 hwmon::type::ctemp,
66 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050067 -3,
68 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050069 std::make_tuple(
70 hwmon::type::cfan,
71 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050072 0,
73 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050074 std::make_tuple(
75 hwmon::type::cvolt,
76 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050077 -3,
78 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050079 std::make_tuple(
80 hwmon::type::ccurr,
81 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050082 -3,
83 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050084 std::make_tuple(
85 hwmon::type::cenergy,
86 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050087 -6,
88 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050089 std::make_tuple(
90 hwmon::type::cpower,
91 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050092 -6,
93 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050094};
95
96auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
97{
98 return std::get<0>(attrs);
99}
100
101auto getUnit(decltype(typeAttrMap)::const_reference attrs)
102{
103 return std::get<1>(attrs);
104}
105
106auto getScale(decltype(typeAttrMap)::const_reference attrs)
107{
108 return std::get<2>(attrs);
109}
110
Brad Bishopadd98512017-01-06 22:01:19 -0500111auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
112{
113 return std::get<3>(attrs);
114}
115
Brad Bishop951a79e2017-01-06 21:55:11 -0500116using AttributeIterator = decltype(*typeAttrMap.begin());
117using Attributes
118 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
119
120auto getAttributes(const std::string& type, Attributes& attributes)
121{
122 // *INDENT-OFF*
123 auto a = std::find_if(
124 typeAttrMap.begin(),
125 typeAttrMap.end(),
126 [&](const auto & e)
127 {
128 return type == getHwmonType(e);
129 });
130 // *INDENT-ON*
131
132 if (a == typeAttrMap.end())
133 {
134 return false;
135 }
136
137 attributes = *a;
138 return true;
139}
140
Brad Bishope9fdee02017-01-06 10:43:29 -0500141auto addValue(const SensorSet::key_type& sensor,
Brad Bishop4db64422017-02-16 11:33:32 -0500142 const std::string& hwmonRoot,
143 const std::string& instance,
144 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500145{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500146 static constexpr bool deferSignals = true;
147
Brad Bishope9fdee02017-01-06 10:43:29 -0500148 // Get the initial value for the value interface.
149 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
150 auto& obj = std::get<Object>(info);
151 auto& objPath = std::get<std::string>(info);
152
Brad Bishop4db64422017-02-16 11:33:32 -0500153 int val = readSysfsWithCallout(hwmonRoot,
154 instance,
155 sensor.first,
156 sensor.second,
157 hwmon::entry::input);
Brad Bishop30dbcee2017-01-18 07:55:42 -0500158 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500159 iface->value(val);
160
Brad Bishop951a79e2017-01-06 21:55:11 -0500161 Attributes attrs;
162 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500163 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500164 iface->unit(getUnit(attrs));
165 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500166 }
167
168 obj[InterfaceType::VALUE] = iface;
169 return iface;
170}
171
Brad Bishopb9e2b072016-12-19 13:47:10 -0500172MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500173 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500174 const std::string& path,
175 const char* prefix,
176 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500177 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500178 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500179 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500180 _hwmonRoot(),
181 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500182 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500183 _root(root),
184 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500185{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500186 std::string p = path;
187 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500188 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500189 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500190 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500191
192 auto n = p.rfind('/');
193 if (n != std::string::npos)
194 {
195 _instance.assign(p.substr(n + 1));
196 _hwmonRoot.assign(p.substr(0, n));
197 }
198
199 assert(!_instance.empty());
200 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500201}
202
203void MainLoop::shutdown() noexcept
204{
205 _shutdown = true;
206}
207
208void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500209{
210 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500211 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500212
Brad Bishop75b4ab82017-01-06 09:33:50 -0500213 for (auto& i : *sensors)
214 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500215 // Get sensor configuration from the environment.
216
Brad Bishop73831cd2017-01-06 09:37:22 -0500217 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500218 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500219 if (label.empty())
220 {
221 continue;
222 }
223
Brad Bishopadd98512017-01-06 22:01:19 -0500224 Attributes attrs;
225 if (!getAttributes(i.first.first, attrs))
226 {
227 continue;
228 }
229
Brad Bishop075f7a22017-01-06 09:45:08 -0500230 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500231 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500232 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500233 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500234 objectPath.append(label);
235
Brad Bishopf7426cf2017-01-06 15:36:43 -0500236 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop4db64422017-02-16 11:33:32 -0500237 auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500238 auto sensorValue = valueInterface->value();
239 addThreshold<WarningObject>(i.first, sensorValue, info);
240 addThreshold<CriticalObject>(i.first, sensorValue, info);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600241 //TODO openbmc/openbmc#1347
242 // Handle application restarts to set/refresh fan speed values
Matt Spinler0a8de642017-05-11 10:59:39 -0500243 auto target = addTarget<hwmon::FanSpeed>(
244 i.first, _hwmonRoot, _instance, info);
245
246 if (target)
247 {
248 target->enable();
249 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500250
Brad Bishop30dbcee2017-01-18 07:55:42 -0500251 // All the interfaces have been created. Go ahead
252 // and emit InterfacesAdded.
253 valueInterface->emit_object_added();
254
Brad Bishop075f7a22017-01-06 09:45:08 -0500255 auto value = std::make_tuple(
256 std::move(i.second),
257 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500258 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500259
Brad Bishop75b4ab82017-01-06 09:33:50 -0500260 state[std::move(i.first)] = std::move(value);
261 }
262
Patrick Venture62503a42017-05-23 07:30:29 -0700263 /* If there are no sensors specified by labels, exit. */
264 if (0 == state.size())
265 {
266 return;
267 }
268
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500269 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500270 std::string busname{_prefix};
271 busname.append(1, '.');
272 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500273 _bus.request_name(busname.c_str());
274 }
275
Patrick Ventureab10f162017-05-22 09:44:50 -0700276 {
277 auto interval = getenv("INTERVAL");
278 if (interval)
279 {
280 _interval = strtoull(interval, NULL, 10);
281 }
282 }
283
Brad Bishope55ef3d2016-12-19 09:12:40 -0500284 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
285 // ensure the objects all exist?
286
287 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500288 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500289 {
290 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500291 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500292 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500293 auto& attrs = std::get<0>(i.second);
294 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500295 {
296 // Read value from sensor.
Brad Bishop4db64422017-02-16 11:33:32 -0500297 int value = readSysfsWithCallout(_hwmonRoot,
298 _instance,
299 i.first.first,
300 i.first.second,
301 hwmon::entry::input);
Brad Bishopf7426cf2017-01-06 15:36:43 -0500302 auto& objInfo = std::get<ObjectInfo>(i.second);
303 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500304
Brad Bishope0b7d052017-01-06 15:30:23 -0500305 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500306 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500307 auto valueIface = std::shared_ptr<ValueObject>();
308 auto warnIface = std::shared_ptr<WarningObject>();
309 auto critIface = std::shared_ptr<CriticalObject>();
310
311 switch (iface.first)
312 {
313 case InterfaceType::VALUE:
314 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
315 (iface.second);
316 valueIface->value(value);
317 break;
318 case InterfaceType::WARN:
319 checkThresholds<WarningObject>(iface.second, value);
320 break;
321 case InterfaceType::CRIT:
322 checkThresholds<CriticalObject>(iface.second, value);
323 break;
324 default:
325 break;
326 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500327 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500328 }
329 }
330
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500331 // Respond to DBus
332 _bus.process_discard();
333
Brad Bishope55ef3d2016-12-19 09:12:40 -0500334 // Sleep until next interval.
Brad Bishope55ef3d2016-12-19 09:12:40 -0500335 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Patrick Ventureab10f162017-05-22 09:44:50 -0700336 _bus.wait(_interval);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500337
338 // TODO: Issue#7 - Should probably periodically check the SensorSet
339 // for new entries.
340 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500341}
342
343// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4