blob: 7399df47beed9fc9bb89d833819072c82b8f369f [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>
Patrick Venture1e6324f2017-06-01 14:07:05 -070020
21#include <phosphor-logging/elog-errors.hpp>
Matt Spinlerf9c83c42017-08-10 08:51:45 -050022#include "config.h"
Brad Bishope55ef3d2016-12-19 09:12:40 -050023#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050024#include "hwmon.hpp"
25#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050026#include "mainloop.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050027#include "env.hpp"
Brad Bishope0b7d052017-01-06 15:30:23 -050028#include "thresholds.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060029#include "targets.hpp"
Matthew Barth048ac872017-03-09 14:36:08 -060030#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050031
Patrick Venture1e6324f2017-06-01 14:07:05 -070032#include <xyz/openbmc_project/Sensor/Device/error.hpp>
33
34using namespace phosphor::logging;
35
Saqib Khan973886d2017-03-15 14:01:16 -050036// Initialization for Warning Objects
37decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
38 &WarningObject::warningLow;
39decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
40 &WarningObject::warningHigh;
41decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
42 &WarningObject::warningLow;
43decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
44 &WarningObject::warningHigh;
45decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
46 &WarningObject::warningAlarmLow;
47decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
48 &WarningObject::warningAlarmHigh;
49
50// Initialization for Critical Objects
51decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
52 &CriticalObject::criticalLow;
53decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
54 &CriticalObject::criticalHigh;
55decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
56 &CriticalObject::criticalLow;
57decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
58 &CriticalObject::criticalHigh;
59decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
60 &CriticalObject::criticalAlarmLow;
61decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
62 &CriticalObject::criticalAlarmHigh;
63
64
Brad Bishop9c7b6e02016-12-19 12:43:36 -050065
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 Bishop751043e2017-08-29 11:13:46 -0400149 const std::string& devPath,
150 sysfs::hwmonio::HwmonIO& ioAccess,
Brad Bishop4db64422017-02-16 11:33:32 -0500151 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 Bishop751043e2017-08-29 11:13:46 -0400160 auto readWithRetry = [&ioAccess](
161 const auto& type,
162 const auto& id,
163 const auto& sensor,
164 auto retries,
165 const auto& delay)
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500166 {
Brad Bishop751043e2017-08-29 11:13:46 -0400167 auto value = 0;
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500168
Brad Bishop751043e2017-08-29 11:13:46 -0400169 while(true)
170 {
171 try
172 {
173 value = ioAccess.read(type, id, sensor);
174 }
175 catch (const std::system_error& e)
176 {
177 if (e.code().value() != EAGAIN || !retries)
178 {
179 throw;
180 }
181
182 --retries;
183 std::this_thread::sleep_for(delay);
184 continue;
185 }
186 break;
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500187 }
Brad Bishop751043e2017-08-29 11:13:46 -0400188 return value;
189 };
190
191 static constexpr auto retries = 10;
192 auto val = 0;
193 try
194 {
195 // Retry for up to a second if device is busy
196 val = readWithRetry(
197 sensor.first,
198 sensor.second,
199 hwmon::entry::input,
200 retries,
201 std::chrono::milliseconds{100});
202 }
203 catch (const std::system_error& e)
204 {
205 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
206 report<ReadFailure>(
207 xyz::openbmc_project::Sensor::Device::
208 ReadFailure::CALLOUT_ERRNO(e.code().value()),
209 xyz::openbmc_project::Sensor::Device::
210 ReadFailure::CALLOUT_DEVICE_PATH(devPath.c_str()));
211
212 return static_cast<std::shared_ptr<ValueObject>>(nullptr);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700213 }
214
Brad Bishop30dbcee2017-01-18 07:55:42 -0500215 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500216 iface->value(val);
217
Brad Bishop951a79e2017-01-06 21:55:11 -0500218 Attributes attrs;
219 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500220 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500221 iface->unit(getUnit(attrs));
222 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500223 }
224
225 obj[InterfaceType::VALUE] = iface;
226 return iface;
227}
228
Brad Bishopb9e2b072016-12-19 13:47:10 -0500229MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500230 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500231 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400232 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500233 const char* prefix,
234 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500235 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500236 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500237 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500238 _hwmonRoot(),
239 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400240 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500241 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500242 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400243 state(),
244 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500245{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500246 std::string p = path;
247 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500248 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500249 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500250 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500251
252 auto n = p.rfind('/');
253 if (n != std::string::npos)
254 {
255 _instance.assign(p.substr(n + 1));
256 _hwmonRoot.assign(p.substr(0, n));
257 }
258
259 assert(!_instance.empty());
260 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500261}
262
263void MainLoop::shutdown() noexcept
264{
265 _shutdown = true;
266}
267
268void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500269{
270 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500271 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500272
Brad Bishop75b4ab82017-01-06 09:33:50 -0500273 for (auto& i : *sensors)
274 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530275 std::string label;
Brad Bishopf3df6b42017-01-06 10:14:09 -0500276
Tom Joseph1f8a9582017-06-12 20:10:59 +0530277 /*
278 * Check if the value of the MODE_<item><X> env variable for the sensor
279 * is "label", then read the sensor number from the <item><X>_label
280 * file. The name of the DBUS object would be the value of the env
281 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
282 * does'nt exist, then the name of DBUS object is the value of the env
283 * variable LABEL_<item><X>.
284 */
285 auto mode = getEnv("MODE", i.first);
286 if (!mode.compare(hwmon::entry::label))
Brad Bishop73831cd2017-01-06 09:37:22 -0500287 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530288 label = getIndirectLabelEnv(
289 "LABEL", _hwmonRoot + '/' + _instance + '/', i.first);
290 if (label.empty())
291 {
292 continue;
293 }
294 }
295 else
296 {
297 // Ignore inputs without a label.
298 label = getEnv("LABEL", i.first);
299 if (label.empty())
300 {
301 continue;
302 }
Brad Bishop73831cd2017-01-06 09:37:22 -0500303 }
304
Brad Bishopadd98512017-01-06 22:01:19 -0500305 Attributes attrs;
306 if (!getAttributes(i.first.first, attrs))
307 {
308 continue;
309 }
310
Brad Bishop075f7a22017-01-06 09:45:08 -0500311 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500312 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500313 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500314 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500315 objectPath.append(label);
316
Brad Bishopf7426cf2017-01-06 15:36:43 -0500317 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop751043e2017-08-29 11:13:46 -0400318 auto valueInterface = addValue(i.first, _devPath, ioAccess, info);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700319 if (!valueInterface)
320 {
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500321#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700322 continue; /* skip adding this sensor for now. */
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500323#else
324 exit(EXIT_FAILURE);
325#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700326 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500327 auto sensorValue = valueInterface->value();
328 addThreshold<WarningObject>(i.first, sensorValue, info);
329 addThreshold<CriticalObject>(i.first, sensorValue, info);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600330 //TODO openbmc/openbmc#1347
331 // Handle application restarts to set/refresh fan speed values
Matt Spinler0a8de642017-05-11 10:59:39 -0500332 auto target = addTarget<hwmon::FanSpeed>(
Brad Bishop751043e2017-08-29 11:13:46 -0400333 i.first, ioAccess.path(), _devPath, info);
Matt Spinler0a8de642017-05-11 10:59:39 -0500334
335 if (target)
336 {
337 target->enable();
338 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500339
Brad Bishop30dbcee2017-01-18 07:55:42 -0500340 // All the interfaces have been created. Go ahead
341 // and emit InterfacesAdded.
342 valueInterface->emit_object_added();
343
Brad Bishop075f7a22017-01-06 09:45:08 -0500344 auto value = std::make_tuple(
345 std::move(i.second),
346 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500347 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500348
Brad Bishop75b4ab82017-01-06 09:33:50 -0500349 state[std::move(i.first)] = std::move(value);
350 }
351
Patrick Venture62503a42017-05-23 07:30:29 -0700352 /* If there are no sensors specified by labels, exit. */
353 if (0 == state.size())
354 {
355 return;
356 }
357
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500358 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500359 std::string busname{_prefix};
360 busname.append(1, '.');
361 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500362 _bus.request_name(busname.c_str());
363 }
364
Patrick Ventureab10f162017-05-22 09:44:50 -0700365 {
366 auto interval = getenv("INTERVAL");
367 if (interval)
368 {
369 _interval = strtoull(interval, NULL, 10);
370 }
371 }
372
Brad Bishope55ef3d2016-12-19 09:12:40 -0500373 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
374 // ensure the objects all exist?
375
376 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500377 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500378 {
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500379#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700380 std::vector<SensorSet::key_type> destroy;
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500381#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500382 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500383 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500384 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500385 auto& attrs = std::get<0>(i.second);
386 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500387 {
388 // Read value from sensor.
Patrick Venture1e6324f2017-06-01 14:07:05 -0700389 int value;
390 try
Brad Bishopdddb7152017-01-06 09:54:23 -0500391 {
Brad Bishop751043e2017-08-29 11:13:46 -0400392 value = ioAccess.read(
393 i.first.first,
394 i.first.second,
395 hwmon::entry::input);
Brad Bishope0b7d052017-01-06 15:30:23 -0500396
Patrick Venture1e6324f2017-06-01 14:07:05 -0700397 auto& objInfo = std::get<ObjectInfo>(i.second);
398 auto& obj = std::get<Object>(objInfo);
399
400 for (auto& iface : obj)
Brad Bishope0b7d052017-01-06 15:30:23 -0500401 {
Patrick Venture1e6324f2017-06-01 14:07:05 -0700402 auto valueIface = std::shared_ptr<ValueObject>();
403 auto warnIface = std::shared_ptr<WarningObject>();
404 auto critIface = std::shared_ptr<CriticalObject>();
405
406 switch (iface.first)
407 {
408 case InterfaceType::VALUE:
409 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
410 (iface.second);
411 valueIface->value(value);
412 break;
413 case InterfaceType::WARN:
414 checkThresholds<WarningObject>(iface.second, value);
415 break;
416 case InterfaceType::CRIT:
417 checkThresholds<CriticalObject>(iface.second, value);
418 break;
419 default:
420 break;
421 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500422 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500423 }
Brad Bishop751043e2017-08-29 11:13:46 -0400424 catch (const std::system_error& e)
Patrick Venture1e6324f2017-06-01 14:07:05 -0700425 {
Brad Bishop751043e2017-08-29 11:13:46 -0400426 if (e.code().value() == EAGAIN)
427 {
428 //Just go with the current values and try again later.
429 //TODO: openbmc/openbmc#2048 could keep an eye on
430 //how long the device is actually busy.
431 continue;
432 }
433
434 using namespace sdbusplus::xyz::openbmc_project::
435 Sensor::Device::Error;
436 report<ReadFailure>(
437 xyz::openbmc_project::Sensor::Device::
438 ReadFailure::CALLOUT_ERRNO(e.code().value()),
439 xyz::openbmc_project::Sensor::Device::
440 ReadFailure::CALLOUT_DEVICE_PATH(
441 _devPath.c_str()));
Patrick Venture1e6324f2017-06-01 14:07:05 -0700442
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500443#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700444 destroy.push_back(i.first);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500445#else
446 exit(EXIT_FAILURE);
447#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700448 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500449 }
450 }
451
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500452#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700453 for (auto& i : destroy)
454 {
455 state.erase(i);
456 }
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500457#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700458
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500459 // Respond to DBus
460 _bus.process_discard();
461
Brad Bishope55ef3d2016-12-19 09:12:40 -0500462 // Sleep until next interval.
Brad Bishope55ef3d2016-12-19 09:12:40 -0500463 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Patrick Ventureab10f162017-05-22 09:44:50 -0700464 _bus.wait(_interval);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500465
466 // TODO: Issue#7 - Should probably periodically check the SensorSet
467 // for new entries.
468 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500469}
470
471// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4