blob: 7af3096bf6b666c6c792196bfe19235e7fba3ea9 [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"
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
Matthew Barthbf7b7b12017-03-07 15:46:59 -060057// Initialization for Target objects
58decltype(Targets<FanSpeedObject>::setTarget)
59 Targets<FanSpeedObject>::setTarget = &FanSpeedObject::target;
60decltype(Targets<FanSpeedObject>::getTarget)
61 Targets<FanSpeedObject>::getTarget = &FanSpeedObject::target;
62
Saqib Khan973886d2017-03-15 14:01:16 -050063
Brad Bishop9c7b6e02016-12-19 12:43:36 -050064using namespace std::literals::chrono_literals;
65
Brad Bishop74aa4dd2017-01-06 09:50:31 -050066static constexpr auto typeAttrMap =
67{
68 // 1 - hwmon class
69 // 2 - unit
70 // 3 - sysfs scaling factor
71 std::make_tuple(
72 hwmon::type::ctemp,
73 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050074 -3,
75 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050076 std::make_tuple(
77 hwmon::type::cfan,
78 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050079 0,
80 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050081 std::make_tuple(
82 hwmon::type::cvolt,
83 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050084 -3,
85 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050086 std::make_tuple(
87 hwmon::type::ccurr,
88 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050089 -3,
90 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050091 std::make_tuple(
92 hwmon::type::cenergy,
93 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050094 -6,
95 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050096 std::make_tuple(
97 hwmon::type::cpower,
98 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050099 -6,
100 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -0500101};
102
103auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
104{
105 return std::get<0>(attrs);
106}
107
108auto getUnit(decltype(typeAttrMap)::const_reference attrs)
109{
110 return std::get<1>(attrs);
111}
112
113auto getScale(decltype(typeAttrMap)::const_reference attrs)
114{
115 return std::get<2>(attrs);
116}
117
Brad Bishopadd98512017-01-06 22:01:19 -0500118auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
119{
120 return std::get<3>(attrs);
121}
122
Brad Bishop951a79e2017-01-06 21:55:11 -0500123using AttributeIterator = decltype(*typeAttrMap.begin());
124using Attributes
125 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
126
127auto getAttributes(const std::string& type, Attributes& attributes)
128{
129 // *INDENT-OFF*
130 auto a = std::find_if(
131 typeAttrMap.begin(),
132 typeAttrMap.end(),
133 [&](const auto & e)
134 {
135 return type == getHwmonType(e);
136 });
137 // *INDENT-ON*
138
139 if (a == typeAttrMap.end())
140 {
141 return false;
142 }
143
144 attributes = *a;
145 return true;
146}
147
Brad Bishope9fdee02017-01-06 10:43:29 -0500148auto addValue(const SensorSet::key_type& sensor,
Brad Bishop4db64422017-02-16 11:33:32 -0500149 const std::string& hwmonRoot,
150 const std::string& instance,
151 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500152{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500153 static constexpr bool deferSignals = true;
154
Brad Bishope9fdee02017-01-06 10:43:29 -0500155 // Get the initial value for the value interface.
156 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
157 auto& obj = std::get<Object>(info);
158 auto& objPath = std::get<std::string>(info);
159
Brad Bishop4db64422017-02-16 11:33:32 -0500160 int val = readSysfsWithCallout(hwmonRoot,
161 instance,
162 sensor.first,
163 sensor.second,
164 hwmon::entry::input);
Brad Bishop30dbcee2017-01-18 07:55:42 -0500165 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500166 iface->value(val);
167
Brad Bishop951a79e2017-01-06 21:55:11 -0500168 Attributes attrs;
169 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500170 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500171 iface->unit(getUnit(attrs));
172 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500173 }
174
175 obj[InterfaceType::VALUE] = iface;
176 return iface;
177}
178
Brad Bishopb9e2b072016-12-19 13:47:10 -0500179MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500180 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500181 const std::string& path,
182 const char* prefix,
183 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500184 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500185 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500186 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500187 _hwmonRoot(),
188 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500189 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500190 _root(root),
191 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500192{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500193 std::string p = path;
194 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500195 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500196 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500197 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500198
199 auto n = p.rfind('/');
200 if (n != std::string::npos)
201 {
202 _instance.assign(p.substr(n + 1));
203 _hwmonRoot.assign(p.substr(0, n));
204 }
205
206 assert(!_instance.empty());
207 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500208}
209
210void MainLoop::shutdown() noexcept
211{
212 _shutdown = true;
213}
214
215void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500216{
217 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500218 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500219
Brad Bishop75b4ab82017-01-06 09:33:50 -0500220 for (auto& i : *sensors)
221 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500222 // Get sensor configuration from the environment.
223
Brad Bishop73831cd2017-01-06 09:37:22 -0500224 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500225 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500226 if (label.empty())
227 {
228 continue;
229 }
230
Brad Bishopadd98512017-01-06 22:01:19 -0500231 Attributes attrs;
232 if (!getAttributes(i.first.first, attrs))
233 {
234 continue;
235 }
236
Brad Bishop075f7a22017-01-06 09:45:08 -0500237 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500238 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500239 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500240 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500241 objectPath.append(label);
242
Brad Bishopf7426cf2017-01-06 15:36:43 -0500243 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop4db64422017-02-16 11:33:32 -0500244 auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500245 auto sensorValue = valueInterface->value();
246 addThreshold<WarningObject>(i.first, sensorValue, info);
247 addThreshold<CriticalObject>(i.first, sensorValue, info);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600248 //TODO openbmc/openbmc#1347
249 // Handle application restarts to set/refresh fan speed values
250 addTarget<FanSpeedObject>(i.first, _hwmonRoot, _instance, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500251
Brad Bishop30dbcee2017-01-18 07:55:42 -0500252 // All the interfaces have been created. Go ahead
253 // and emit InterfacesAdded.
254 valueInterface->emit_object_added();
255
Brad Bishop075f7a22017-01-06 09:45:08 -0500256 auto value = std::make_tuple(
257 std::move(i.second),
258 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500259 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500260
Brad Bishop75b4ab82017-01-06 09:33:50 -0500261 state[std::move(i.first)] = std::move(value);
262 }
263
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500264 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500265 std::string busname{_prefix};
266 busname.append(1, '.');
267 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500268 _bus.request_name(busname.c_str());
269 }
270
Brad Bishope55ef3d2016-12-19 09:12:40 -0500271 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
272 // ensure the objects all exist?
273
274 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500275 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500276 {
277 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500278 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500279 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500280 auto& attrs = std::get<0>(i.second);
281 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500282 {
283 // Read value from sensor.
Brad Bishop4db64422017-02-16 11:33:32 -0500284 int value = readSysfsWithCallout(_hwmonRoot,
285 _instance,
286 i.first.first,
287 i.first.second,
288 hwmon::entry::input);
Brad Bishopf7426cf2017-01-06 15:36:43 -0500289 auto& objInfo = std::get<ObjectInfo>(i.second);
290 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500291
Brad Bishope0b7d052017-01-06 15:30:23 -0500292 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500293 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500294 auto valueIface = std::shared_ptr<ValueObject>();
295 auto warnIface = std::shared_ptr<WarningObject>();
296 auto critIface = std::shared_ptr<CriticalObject>();
297
298 switch (iface.first)
299 {
300 case InterfaceType::VALUE:
301 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
302 (iface.second);
303 valueIface->value(value);
304 break;
305 case InterfaceType::WARN:
306 checkThresholds<WarningObject>(iface.second, value);
307 break;
308 case InterfaceType::CRIT:
309 checkThresholds<CriticalObject>(iface.second, value);
310 break;
311 default:
312 break;
313 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500314 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500315 }
316 }
317
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500318 // Respond to DBus
319 _bus.process_discard();
320
Brad Bishope55ef3d2016-12-19 09:12:40 -0500321 // Sleep until next interval.
322 // TODO: Issue#5 - Make this configurable.
323 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500324 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500325
326 // TODO: Issue#7 - Should probably periodically check the SensorSet
327 // for new entries.
328 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500329}
330
331// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4