blob: 486a1752dcf275a6cd13eaebace330702e63d1ef [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 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
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500160 int val = 0;
161 bool retry = true;
162 size_t count = 10;
Patrick Venture1e6324f2017-06-01 14:07:05 -0700163
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500164
165 //Retry for up to a second if device is busy
166
167 while (retry)
168 {
169 try
170 {
171 val = sysfs::readSysfsWithCallout(hwmonRoot,
172 instance,
173 sensor.first,
174 sensor.second,
175 hwmon::entry::input,
176 count > 0); //throw DeviceBusy until last attempt
177 }
178 catch (sysfs::DeviceBusyException& e)
179 {
180 count--;
181 std::this_thread::sleep_for(std::chrono::milliseconds{100});
182 continue;
183 }
184 catch(const std::exception& ioe)
185 {
186 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
187 commit<ReadFailure>();
188
189 return static_cast<std::shared_ptr<ValueObject>>(nullptr);
190 }
191 retry = false;
Patrick Venture1e6324f2017-06-01 14:07:05 -0700192 }
193
Brad Bishop30dbcee2017-01-18 07:55:42 -0500194 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500195 iface->value(val);
196
Brad Bishop951a79e2017-01-06 21:55:11 -0500197 Attributes attrs;
198 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500199 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500200 iface->unit(getUnit(attrs));
201 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500202 }
203
204 obj[InterfaceType::VALUE] = iface;
205 return iface;
206}
207
Brad Bishopb9e2b072016-12-19 13:47:10 -0500208MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500209 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500210 const std::string& path,
211 const char* prefix,
212 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500213 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500214 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500215 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500216 _hwmonRoot(),
217 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500218 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500219 _root(root),
220 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500221{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500222 std::string p = path;
223 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500224 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500225 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500226 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500227
228 auto n = p.rfind('/');
229 if (n != std::string::npos)
230 {
231 _instance.assign(p.substr(n + 1));
232 _hwmonRoot.assign(p.substr(0, n));
233 }
234
235 assert(!_instance.empty());
236 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500237}
238
239void MainLoop::shutdown() noexcept
240{
241 _shutdown = true;
242}
243
244void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500245{
246 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500247 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500248
Brad Bishop75b4ab82017-01-06 09:33:50 -0500249 for (auto& i : *sensors)
250 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530251 std::string label;
Brad Bishopf3df6b42017-01-06 10:14:09 -0500252
Tom Joseph1f8a9582017-06-12 20:10:59 +0530253 /*
254 * Check if the value of the MODE_<item><X> env variable for the sensor
255 * is "label", then read the sensor number from the <item><X>_label
256 * file. The name of the DBUS object would be the value of the env
257 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
258 * does'nt exist, then the name of DBUS object is the value of the env
259 * variable LABEL_<item><X>.
260 */
261 auto mode = getEnv("MODE", i.first);
262 if (!mode.compare(hwmon::entry::label))
Brad Bishop73831cd2017-01-06 09:37:22 -0500263 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530264 label = getIndirectLabelEnv(
265 "LABEL", _hwmonRoot + '/' + _instance + '/', i.first);
266 if (label.empty())
267 {
268 continue;
269 }
270 }
271 else
272 {
273 // Ignore inputs without a label.
274 label = getEnv("LABEL", i.first);
275 if (label.empty())
276 {
277 continue;
278 }
Brad Bishop73831cd2017-01-06 09:37:22 -0500279 }
280
Brad Bishopadd98512017-01-06 22:01:19 -0500281 Attributes attrs;
282 if (!getAttributes(i.first.first, attrs))
283 {
284 continue;
285 }
286
Brad Bishop075f7a22017-01-06 09:45:08 -0500287 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500288 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500289 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500290 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500291 objectPath.append(label);
292
Brad Bishopf7426cf2017-01-06 15:36:43 -0500293 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop4db64422017-02-16 11:33:32 -0500294 auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700295 if (!valueInterface)
296 {
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500297#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700298 continue; /* skip adding this sensor for now. */
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500299#else
300 exit(EXIT_FAILURE);
301#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700302 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500303 auto sensorValue = valueInterface->value();
304 addThreshold<WarningObject>(i.first, sensorValue, info);
305 addThreshold<CriticalObject>(i.first, sensorValue, info);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600306 //TODO openbmc/openbmc#1347
307 // Handle application restarts to set/refresh fan speed values
Matt Spinler0a8de642017-05-11 10:59:39 -0500308 auto target = addTarget<hwmon::FanSpeed>(
309 i.first, _hwmonRoot, _instance, info);
310
311 if (target)
312 {
313 target->enable();
314 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500315
Brad Bishop30dbcee2017-01-18 07:55:42 -0500316 // All the interfaces have been created. Go ahead
317 // and emit InterfacesAdded.
318 valueInterface->emit_object_added();
319
Brad Bishop075f7a22017-01-06 09:45:08 -0500320 auto value = std::make_tuple(
321 std::move(i.second),
322 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500323 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500324
Brad Bishop75b4ab82017-01-06 09:33:50 -0500325 state[std::move(i.first)] = std::move(value);
326 }
327
Patrick Venture62503a42017-05-23 07:30:29 -0700328 /* If there are no sensors specified by labels, exit. */
329 if (0 == state.size())
330 {
331 return;
332 }
333
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500334 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500335 std::string busname{_prefix};
336 busname.append(1, '.');
337 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500338 _bus.request_name(busname.c_str());
339 }
340
Patrick Ventureab10f162017-05-22 09:44:50 -0700341 {
342 auto interval = getenv("INTERVAL");
343 if (interval)
344 {
345 _interval = strtoull(interval, NULL, 10);
346 }
347 }
348
Brad Bishope55ef3d2016-12-19 09:12:40 -0500349 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
350 // ensure the objects all exist?
351
352 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500353 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500354 {
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500355#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700356 std::vector<SensorSet::key_type> destroy;
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500357#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500358 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500359 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500360 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500361 auto& attrs = std::get<0>(i.second);
362 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500363 {
364 // Read value from sensor.
Patrick Venture1e6324f2017-06-01 14:07:05 -0700365 int value;
366 try
Brad Bishopdddb7152017-01-06 09:54:23 -0500367 {
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500368 try
369 {
370 value = sysfs::readSysfsWithCallout(_hwmonRoot,
371 _instance,
372 i.first.first,
373 i.first.second,
374 hwmon::entry::input);
375 }
376 catch (sysfs::DeviceBusyException& e)
377 {
378 //Just go with the current values and try again later.
379 //TODO: openbmc/openbmc#2048 could keep an eye on
380 //how long the device is actually busy.
381 continue;
382 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500383
Patrick Venture1e6324f2017-06-01 14:07:05 -0700384 auto& objInfo = std::get<ObjectInfo>(i.second);
385 auto& obj = std::get<Object>(objInfo);
386
387 for (auto& iface : obj)
Brad Bishope0b7d052017-01-06 15:30:23 -0500388 {
Patrick Venture1e6324f2017-06-01 14:07:05 -0700389 auto valueIface = std::shared_ptr<ValueObject>();
390 auto warnIface = std::shared_ptr<WarningObject>();
391 auto critIface = std::shared_ptr<CriticalObject>();
392
393 switch (iface.first)
394 {
395 case InterfaceType::VALUE:
396 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
397 (iface.second);
398 valueIface->value(value);
399 break;
400 case InterfaceType::WARN:
401 checkThresholds<WarningObject>(iface.second, value);
402 break;
403 case InterfaceType::CRIT:
404 checkThresholds<CriticalObject>(iface.second, value);
405 break;
406 default:
407 break;
408 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500409 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500410 }
Patrick Venture1e6324f2017-06-01 14:07:05 -0700411 catch (const std::exception& e)
412 {
413 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
414 commit<ReadFailure>();
415
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500416#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700417 destroy.push_back(i.first);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500418#else
419 exit(EXIT_FAILURE);
420#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700421 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500422 }
423 }
424
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500425#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700426 for (auto& i : destroy)
427 {
428 state.erase(i);
429 }
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500430#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700431
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500432 // Respond to DBus
433 _bus.process_discard();
434
Brad Bishope55ef3d2016-12-19 09:12:40 -0500435 // Sleep until next interval.
Brad Bishope55ef3d2016-12-19 09:12:40 -0500436 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Patrick Ventureab10f162017-05-22 09:44:50 -0700437 _bus.wait(_interval);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500438
439 // TODO: Issue#7 - Should probably periodically check the SensorSet
440 // for new entries.
441 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500442}
443
444// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4