blob: 217c34c678ffa7c87c2a1123da15e964385ed8a3 [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>
Patrick Venture1e6324f2017-06-01 14:07:05 -070019
20#include <phosphor-logging/elog-errors.hpp>
Matt Spinlerf9c83c42017-08-10 08:51:45 -050021#include "config.h"
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 Bishopf3df6b42017-01-06 10:14:09 -050026#include "env.hpp"
Brad Bishope0b7d052017-01-06 15:30:23 -050027#include "thresholds.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060028#include "targets.hpp"
Matthew Barth048ac872017-03-09 14:36:08 -060029#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050030
Patrick Venture1e6324f2017-06-01 14:07:05 -070031#include <xyz/openbmc_project/Sensor/Device/error.hpp>
32
33using namespace phosphor::logging;
34
Saqib Khan973886d2017-03-15 14:01:16 -050035// Initialization for Warning Objects
36decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
37 &WarningObject::warningLow;
38decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
39 &WarningObject::warningHigh;
40decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
41 &WarningObject::warningLow;
42decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
43 &WarningObject::warningHigh;
44decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
45 &WarningObject::warningAlarmLow;
46decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
47 &WarningObject::warningAlarmHigh;
48
49// Initialization for Critical Objects
50decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
51 &CriticalObject::criticalLow;
52decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
53 &CriticalObject::criticalHigh;
54decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
55 &CriticalObject::criticalLow;
56decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
57 &CriticalObject::criticalHigh;
58decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
59 &CriticalObject::criticalAlarmLow;
60decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
61 &CriticalObject::criticalAlarmHigh;
62
Brad Bishop74aa4dd2017-01-06 09:50:31 -050063static constexpr auto typeAttrMap =
64{
65 // 1 - hwmon class
66 // 2 - unit
67 // 3 - sysfs scaling factor
68 std::make_tuple(
69 hwmon::type::ctemp,
70 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050071 -3,
72 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050073 std::make_tuple(
74 hwmon::type::cfan,
75 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050076 0,
77 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050078 std::make_tuple(
79 hwmon::type::cvolt,
80 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050081 -3,
82 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050083 std::make_tuple(
84 hwmon::type::ccurr,
85 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050086 -3,
87 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050088 std::make_tuple(
89 hwmon::type::cenergy,
90 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050091 -6,
92 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050093 std::make_tuple(
94 hwmon::type::cpower,
95 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050096 -6,
97 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050098};
99
100auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
101{
102 return std::get<0>(attrs);
103}
104
105auto getUnit(decltype(typeAttrMap)::const_reference attrs)
106{
107 return std::get<1>(attrs);
108}
109
110auto getScale(decltype(typeAttrMap)::const_reference attrs)
111{
112 return std::get<2>(attrs);
113}
114
Brad Bishopadd98512017-01-06 22:01:19 -0500115auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
116{
117 return std::get<3>(attrs);
118}
119
Brad Bishop951a79e2017-01-06 21:55:11 -0500120using AttributeIterator = decltype(*typeAttrMap.begin());
121using Attributes
122 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
123
124auto getAttributes(const std::string& type, Attributes& attributes)
125{
126 // *INDENT-OFF*
127 auto a = std::find_if(
128 typeAttrMap.begin(),
129 typeAttrMap.end(),
130 [&](const auto & e)
131 {
132 return type == getHwmonType(e);
133 });
134 // *INDENT-ON*
135
136 if (a == typeAttrMap.end())
137 {
138 return false;
139 }
140
141 attributes = *a;
142 return true;
143}
144
Brad Bishope9fdee02017-01-06 10:43:29 -0500145auto addValue(const SensorSet::key_type& sensor,
Brad Bishop751043e2017-08-29 11:13:46 -0400146 const std::string& devPath,
147 sysfs::hwmonio::HwmonIO& ioAccess,
Brad Bishop4db64422017-02-16 11:33:32 -0500148 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500149{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500150 static constexpr bool deferSignals = true;
151
Brad Bishope9fdee02017-01-06 10:43:29 -0500152 // Get the initial value for the value interface.
153 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
154 auto& obj = std::get<Object>(info);
155 auto& objPath = std::get<std::string>(info);
156
Brad Bishop751043e2017-08-29 11:13:46 -0400157 auto val = 0;
158 try
159 {
160 // Retry for up to a second if device is busy
Brad Bishop754d38c2017-09-08 00:46:58 -0400161 // or has a transient error.
162 val = ioAccess.read(
Brad Bishop751043e2017-08-29 11:13:46 -0400163 sensor.first,
164 sensor.second,
Brad Bishop754d38c2017-09-08 00:46:58 -0400165 hwmon::entry::cinput,
166 sysfs::hwmonio::retries,
167 sysfs::hwmonio::delay);
Brad Bishop751043e2017-08-29 11:13:46 -0400168 }
169 catch (const std::system_error& e)
170 {
171 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
172 report<ReadFailure>(
173 xyz::openbmc_project::Sensor::Device::
174 ReadFailure::CALLOUT_ERRNO(e.code().value()),
175 xyz::openbmc_project::Sensor::Device::
176 ReadFailure::CALLOUT_DEVICE_PATH(devPath.c_str()));
177
178 return static_cast<std::shared_ptr<ValueObject>>(nullptr);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700179 }
180
Brad Bishop30dbcee2017-01-18 07:55:42 -0500181 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500182 iface->value(val);
183
Brad Bishop951a79e2017-01-06 21:55:11 -0500184 Attributes attrs;
185 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500186 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500187 iface->unit(getUnit(attrs));
188 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500189 }
190
191 obj[InterfaceType::VALUE] = iface;
192 return iface;
193}
194
Brad Bishopb9e2b072016-12-19 13:47:10 -0500195MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500196 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500197 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400198 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500199 const char* prefix,
200 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500201 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500202 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500203 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500204 _hwmonRoot(),
205 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400206 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500207 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500208 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400209 state(),
210 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500211{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500212 std::string p = path;
213 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500214 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500215 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500216 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500217
218 auto n = p.rfind('/');
219 if (n != std::string::npos)
220 {
221 _instance.assign(p.substr(n + 1));
222 _hwmonRoot.assign(p.substr(0, n));
223 }
224
225 assert(!_instance.empty());
226 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500227}
228
229void MainLoop::shutdown() noexcept
230{
231 _shutdown = true;
232}
233
234void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500235{
236 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500237 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500238
Brad Bishop75b4ab82017-01-06 09:33:50 -0500239 for (auto& i : *sensors)
240 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530241 std::string label;
Brad Bishopf3df6b42017-01-06 10:14:09 -0500242
Tom Joseph1f8a9582017-06-12 20:10:59 +0530243 /*
244 * Check if the value of the MODE_<item><X> env variable for the sensor
245 * is "label", then read the sensor number from the <item><X>_label
246 * file. The name of the DBUS object would be the value of the env
247 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
248 * does'nt exist, then the name of DBUS object is the value of the env
249 * variable LABEL_<item><X>.
250 */
251 auto mode = getEnv("MODE", i.first);
252 if (!mode.compare(hwmon::entry::label))
Brad Bishop73831cd2017-01-06 09:37:22 -0500253 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530254 label = getIndirectLabelEnv(
255 "LABEL", _hwmonRoot + '/' + _instance + '/', i.first);
256 if (label.empty())
257 {
258 continue;
259 }
260 }
261 else
262 {
263 // Ignore inputs without a label.
264 label = getEnv("LABEL", i.first);
265 if (label.empty())
266 {
267 continue;
268 }
Brad Bishop73831cd2017-01-06 09:37:22 -0500269 }
270
Brad Bishopadd98512017-01-06 22:01:19 -0500271 Attributes attrs;
272 if (!getAttributes(i.first.first, attrs))
273 {
274 continue;
275 }
276
Brad Bishop075f7a22017-01-06 09:45:08 -0500277 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500278 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500279 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500280 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500281 objectPath.append(label);
282
Brad Bishopf7426cf2017-01-06 15:36:43 -0500283 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop751043e2017-08-29 11:13:46 -0400284 auto valueInterface = addValue(i.first, _devPath, ioAccess, info);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700285 if (!valueInterface)
286 {
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500287#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700288 continue; /* skip adding this sensor for now. */
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500289#else
290 exit(EXIT_FAILURE);
291#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700292 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500293 auto sensorValue = valueInterface->value();
294 addThreshold<WarningObject>(i.first, sensorValue, info);
295 addThreshold<CriticalObject>(i.first, sensorValue, info);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600296 //TODO openbmc/openbmc#1347
297 // Handle application restarts to set/refresh fan speed values
Matt Spinler0a8de642017-05-11 10:59:39 -0500298 auto target = addTarget<hwmon::FanSpeed>(
Brad Bishop751043e2017-08-29 11:13:46 -0400299 i.first, ioAccess.path(), _devPath, info);
Matt Spinler0a8de642017-05-11 10:59:39 -0500300
301 if (target)
302 {
303 target->enable();
304 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500305
Brad Bishop30dbcee2017-01-18 07:55:42 -0500306 // All the interfaces have been created. Go ahead
307 // and emit InterfacesAdded.
308 valueInterface->emit_object_added();
309
Brad Bishop075f7a22017-01-06 09:45:08 -0500310 auto value = std::make_tuple(
311 std::move(i.second),
312 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500313 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500314
Brad Bishop75b4ab82017-01-06 09:33:50 -0500315 state[std::move(i.first)] = std::move(value);
316 }
317
Patrick Venture62503a42017-05-23 07:30:29 -0700318 /* If there are no sensors specified by labels, exit. */
319 if (0 == state.size())
320 {
321 return;
322 }
323
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500324 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500325 std::string busname{_prefix};
326 busname.append(1, '.');
327 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500328 _bus.request_name(busname.c_str());
329 }
330
Patrick Ventureab10f162017-05-22 09:44:50 -0700331 {
332 auto interval = getenv("INTERVAL");
333 if (interval)
334 {
335 _interval = strtoull(interval, NULL, 10);
336 }
337 }
338
Brad Bishope55ef3d2016-12-19 09:12:40 -0500339 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
340 // ensure the objects all exist?
341
342 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500343 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500344 {
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500345#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700346 std::vector<SensorSet::key_type> destroy;
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500347#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500348 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500349 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500350 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500351 auto& attrs = std::get<0>(i.second);
352 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500353 {
354 // Read value from sensor.
Patrick Venture1e6324f2017-06-01 14:07:05 -0700355 int value;
356 try
Brad Bishopdddb7152017-01-06 09:54:23 -0500357 {
Brad Bishop754d38c2017-09-08 00:46:58 -0400358 // Retry for up to a second if device is busy
359 // or has a transient error.
360
Brad Bishop751043e2017-08-29 11:13:46 -0400361 value = ioAccess.read(
362 i.first.first,
363 i.first.second,
Brad Bishop754d38c2017-09-08 00:46:58 -0400364 hwmon::entry::cinput,
365 sysfs::hwmonio::retries,
366 sysfs::hwmonio::delay);
Brad Bishope0b7d052017-01-06 15:30:23 -0500367
Patrick Venture1e6324f2017-06-01 14:07:05 -0700368 auto& objInfo = std::get<ObjectInfo>(i.second);
369 auto& obj = std::get<Object>(objInfo);
370
371 for (auto& iface : obj)
Brad Bishope0b7d052017-01-06 15:30:23 -0500372 {
Patrick Venture1e6324f2017-06-01 14:07:05 -0700373 auto valueIface = std::shared_ptr<ValueObject>();
374 auto warnIface = std::shared_ptr<WarningObject>();
375 auto critIface = std::shared_ptr<CriticalObject>();
376
377 switch (iface.first)
378 {
379 case InterfaceType::VALUE:
380 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
381 (iface.second);
382 valueIface->value(value);
383 break;
384 case InterfaceType::WARN:
385 checkThresholds<WarningObject>(iface.second, value);
386 break;
387 case InterfaceType::CRIT:
388 checkThresholds<CriticalObject>(iface.second, value);
389 break;
390 default:
391 break;
392 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500393 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500394 }
Brad Bishop751043e2017-08-29 11:13:46 -0400395 catch (const std::system_error& e)
Patrick Venture1e6324f2017-06-01 14:07:05 -0700396 {
Brad Bishop751043e2017-08-29 11:13:46 -0400397 using namespace sdbusplus::xyz::openbmc_project::
398 Sensor::Device::Error;
399 report<ReadFailure>(
400 xyz::openbmc_project::Sensor::Device::
401 ReadFailure::CALLOUT_ERRNO(e.code().value()),
402 xyz::openbmc_project::Sensor::Device::
403 ReadFailure::CALLOUT_DEVICE_PATH(
404 _devPath.c_str()));
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500405#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700406 destroy.push_back(i.first);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500407#else
408 exit(EXIT_FAILURE);
409#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700410 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500411 }
412 }
413
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500414#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700415 for (auto& i : destroy)
416 {
417 state.erase(i);
418 }
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500419#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700420
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500421 // Respond to DBus
422 _bus.process_discard();
423
Brad Bishope55ef3d2016-12-19 09:12:40 -0500424 // Sleep until next interval.
Brad Bishope55ef3d2016-12-19 09:12:40 -0500425 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Patrick Ventureab10f162017-05-22 09:44:50 -0700426 _bus.wait(_interval);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500427
428 // TODO: Issue#7 - Should probably periodically check the SensorSet
429 // for new entries.
430 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500431}
432
433// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4