blob: 34970b1044b86830f7a5e0948430c95e41f2e569 [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
Matthew Barth048ac872017-03-09 14:36:08 -0600245 addTarget<hwmon::FanSpeed>(i.first, _hwmonRoot, _instance, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500246
Brad Bishop30dbcee2017-01-18 07:55:42 -0500247 // All the interfaces have been created. Go ahead
248 // and emit InterfacesAdded.
249 valueInterface->emit_object_added();
250
Brad Bishop075f7a22017-01-06 09:45:08 -0500251 auto value = std::make_tuple(
252 std::move(i.second),
253 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500254 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500255
Brad Bishop75b4ab82017-01-06 09:33:50 -0500256 state[std::move(i.first)] = std::move(value);
257 }
258
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500259 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500260 std::string busname{_prefix};
261 busname.append(1, '.');
262 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500263 _bus.request_name(busname.c_str());
264 }
265
Brad Bishope55ef3d2016-12-19 09:12:40 -0500266 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
267 // ensure the objects all exist?
268
269 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500270 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500271 {
272 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500273 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500274 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500275 auto& attrs = std::get<0>(i.second);
276 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500277 {
278 // Read value from sensor.
Brad Bishop4db64422017-02-16 11:33:32 -0500279 int value = readSysfsWithCallout(_hwmonRoot,
280 _instance,
281 i.first.first,
282 i.first.second,
283 hwmon::entry::input);
Brad Bishopf7426cf2017-01-06 15:36:43 -0500284 auto& objInfo = std::get<ObjectInfo>(i.second);
285 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500286
Brad Bishope0b7d052017-01-06 15:30:23 -0500287 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500288 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500289 auto valueIface = std::shared_ptr<ValueObject>();
290 auto warnIface = std::shared_ptr<WarningObject>();
291 auto critIface = std::shared_ptr<CriticalObject>();
292
293 switch (iface.first)
294 {
295 case InterfaceType::VALUE:
296 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
297 (iface.second);
298 valueIface->value(value);
299 break;
300 case InterfaceType::WARN:
301 checkThresholds<WarningObject>(iface.second, value);
302 break;
303 case InterfaceType::CRIT:
304 checkThresholds<CriticalObject>(iface.second, value);
305 break;
306 default:
307 break;
308 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500309 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500310 }
311 }
312
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500313 // Respond to DBus
314 _bus.process_discard();
315
Brad Bishope55ef3d2016-12-19 09:12:40 -0500316 // Sleep until next interval.
317 // TODO: Issue#5 - Make this configurable.
318 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500319 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500320
321 // TODO: Issue#7 - Should probably periodically check the SensorSet
322 // for new entries.
323 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500324}
325
326// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4