blob: a07d558101681b92507659007b3db1fffb16889c [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>
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
63
Brad Bishop9c7b6e02016-12-19 12:43:36 -050064
Brad Bishop74aa4dd2017-01-06 09:50:31 -050065static constexpr auto typeAttrMap =
66{
67 // 1 - hwmon class
68 // 2 - unit
69 // 3 - sysfs scaling factor
70 std::make_tuple(
71 hwmon::type::ctemp,
72 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050073 -3,
74 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050075 std::make_tuple(
76 hwmon::type::cfan,
77 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050078 0,
79 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050080 std::make_tuple(
81 hwmon::type::cvolt,
82 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050083 -3,
84 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050085 std::make_tuple(
86 hwmon::type::ccurr,
87 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050088 -3,
89 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050090 std::make_tuple(
91 hwmon::type::cenergy,
92 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050093 -6,
94 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050095 std::make_tuple(
96 hwmon::type::cpower,
97 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050098 -6,
99 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -0500100};
101
102auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
103{
104 return std::get<0>(attrs);
105}
106
107auto getUnit(decltype(typeAttrMap)::const_reference attrs)
108{
109 return std::get<1>(attrs);
110}
111
112auto getScale(decltype(typeAttrMap)::const_reference attrs)
113{
114 return std::get<2>(attrs);
115}
116
Brad Bishopadd98512017-01-06 22:01:19 -0500117auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
118{
119 return std::get<3>(attrs);
120}
121
Brad Bishop951a79e2017-01-06 21:55:11 -0500122using AttributeIterator = decltype(*typeAttrMap.begin());
123using Attributes
124 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
125
126auto getAttributes(const std::string& type, Attributes& attributes)
127{
128 // *INDENT-OFF*
129 auto a = std::find_if(
130 typeAttrMap.begin(),
131 typeAttrMap.end(),
132 [&](const auto & e)
133 {
134 return type == getHwmonType(e);
135 });
136 // *INDENT-ON*
137
138 if (a == typeAttrMap.end())
139 {
140 return false;
141 }
142
143 attributes = *a;
144 return true;
145}
146
Brad Bishope9fdee02017-01-06 10:43:29 -0500147auto addValue(const SensorSet::key_type& sensor,
Brad Bishop4db64422017-02-16 11:33:32 -0500148 const std::string& hwmonRoot,
149 const std::string& instance,
150 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500151{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500152 static constexpr bool deferSignals = true;
153
Brad Bishope9fdee02017-01-06 10:43:29 -0500154 // Get the initial value for the value interface.
155 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
156 auto& obj = std::get<Object>(info);
157 auto& objPath = std::get<std::string>(info);
158
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500159 int val = 0;
160 bool retry = true;
161 size_t count = 10;
Patrick Venture1e6324f2017-06-01 14:07:05 -0700162
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500163
164 //Retry for up to a second if device is busy
165
166 while (retry)
167 {
168 try
169 {
170 val = sysfs::readSysfsWithCallout(hwmonRoot,
171 instance,
172 sensor.first,
173 sensor.second,
174 hwmon::entry::input,
175 count > 0); //throw DeviceBusy until last attempt
176 }
177 catch (sysfs::DeviceBusyException& e)
178 {
179 count--;
180 std::this_thread::sleep_for(std::chrono::milliseconds{100});
181 continue;
182 }
183 catch(const std::exception& ioe)
184 {
185 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
186 commit<ReadFailure>();
187
188 return static_cast<std::shared_ptr<ValueObject>>(nullptr);
189 }
190 retry = false;
Patrick Venture1e6324f2017-06-01 14:07:05 -0700191 }
192
Brad Bishop30dbcee2017-01-18 07:55:42 -0500193 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500194 iface->value(val);
195
Brad Bishop951a79e2017-01-06 21:55:11 -0500196 Attributes attrs;
197 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500198 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500199 iface->unit(getUnit(attrs));
200 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500201 }
202
203 obj[InterfaceType::VALUE] = iface;
204 return iface;
205}
206
Brad Bishopb9e2b072016-12-19 13:47:10 -0500207MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500208 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500209 const std::string& path,
210 const char* prefix,
211 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500212 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500213 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500214 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500215 _hwmonRoot(),
216 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500217 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500218 _root(root),
219 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500220{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500221 std::string p = path;
222 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500223 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500224 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500225 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500226
227 auto n = p.rfind('/');
228 if (n != std::string::npos)
229 {
230 _instance.assign(p.substr(n + 1));
231 _hwmonRoot.assign(p.substr(0, n));
232 }
233
234 assert(!_instance.empty());
235 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500236}
237
238void MainLoop::shutdown() noexcept
239{
240 _shutdown = true;
241}
242
243void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500244{
245 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500246 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500247
Brad Bishop75b4ab82017-01-06 09:33:50 -0500248 for (auto& i : *sensors)
249 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530250 std::string label;
Brad Bishopf3df6b42017-01-06 10:14:09 -0500251
Tom Joseph1f8a9582017-06-12 20:10:59 +0530252 /*
253 * Check if the value of the MODE_<item><X> env variable for the sensor
254 * is "label", then read the sensor number from the <item><X>_label
255 * file. The name of the DBUS object would be the value of the env
256 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
257 * does'nt exist, then the name of DBUS object is the value of the env
258 * variable LABEL_<item><X>.
259 */
260 auto mode = getEnv("MODE", i.first);
261 if (!mode.compare(hwmon::entry::label))
Brad Bishop73831cd2017-01-06 09:37:22 -0500262 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530263 label = getIndirectLabelEnv(
264 "LABEL", _hwmonRoot + '/' + _instance + '/', i.first);
265 if (label.empty())
266 {
267 continue;
268 }
269 }
270 else
271 {
272 // Ignore inputs without a label.
273 label = getEnv("LABEL", i.first);
274 if (label.empty())
275 {
276 continue;
277 }
Brad Bishop73831cd2017-01-06 09:37:22 -0500278 }
279
Brad Bishopadd98512017-01-06 22:01:19 -0500280 Attributes attrs;
281 if (!getAttributes(i.first.first, attrs))
282 {
283 continue;
284 }
285
Brad Bishop075f7a22017-01-06 09:45:08 -0500286 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500287 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500288 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500289 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500290 objectPath.append(label);
291
Brad Bishopf7426cf2017-01-06 15:36:43 -0500292 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop4db64422017-02-16 11:33:32 -0500293 auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700294 if (!valueInterface)
295 {
296 continue; /* skip adding this sensor for now. */
297 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500298 auto sensorValue = valueInterface->value();
299 addThreshold<WarningObject>(i.first, sensorValue, info);
300 addThreshold<CriticalObject>(i.first, sensorValue, info);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600301 //TODO openbmc/openbmc#1347
302 // Handle application restarts to set/refresh fan speed values
Matt Spinler0a8de642017-05-11 10:59:39 -0500303 auto target = addTarget<hwmon::FanSpeed>(
304 i.first, _hwmonRoot, _instance, info);
305
306 if (target)
307 {
308 target->enable();
309 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500310
Brad Bishop30dbcee2017-01-18 07:55:42 -0500311 // All the interfaces have been created. Go ahead
312 // and emit InterfacesAdded.
313 valueInterface->emit_object_added();
314
Brad Bishop075f7a22017-01-06 09:45:08 -0500315 auto value = std::make_tuple(
316 std::move(i.second),
317 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500318 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500319
Brad Bishop75b4ab82017-01-06 09:33:50 -0500320 state[std::move(i.first)] = std::move(value);
321 }
322
Patrick Venture62503a42017-05-23 07:30:29 -0700323 /* If there are no sensors specified by labels, exit. */
324 if (0 == state.size())
325 {
326 return;
327 }
328
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500329 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500330 std::string busname{_prefix};
331 busname.append(1, '.');
332 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500333 _bus.request_name(busname.c_str());
334 }
335
Patrick Ventureab10f162017-05-22 09:44:50 -0700336 {
337 auto interval = getenv("INTERVAL");
338 if (interval)
339 {
340 _interval = strtoull(interval, NULL, 10);
341 }
342 }
343
Brad Bishope55ef3d2016-12-19 09:12:40 -0500344 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
345 // ensure the objects all exist?
346
347 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500348 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500349 {
Patrick Venture1e6324f2017-06-01 14:07:05 -0700350 std::vector<SensorSet::key_type> destroy;
Brad Bishope55ef3d2016-12-19 09:12:40 -0500351 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500352 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500353 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500354 auto& attrs = std::get<0>(i.second);
355 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500356 {
357 // Read value from sensor.
Patrick Venture1e6324f2017-06-01 14:07:05 -0700358 int value;
359 try
Brad Bishopdddb7152017-01-06 09:54:23 -0500360 {
Matt Spinler3b8e36e2017-07-28 10:44:45 -0500361 try
362 {
363 value = sysfs::readSysfsWithCallout(_hwmonRoot,
364 _instance,
365 i.first.first,
366 i.first.second,
367 hwmon::entry::input);
368 }
369 catch (sysfs::DeviceBusyException& e)
370 {
371 //Just go with the current values and try again later.
372 //TODO: openbmc/openbmc#2048 could keep an eye on
373 //how long the device is actually busy.
374 continue;
375 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500376
Patrick Venture1e6324f2017-06-01 14:07:05 -0700377 auto& objInfo = std::get<ObjectInfo>(i.second);
378 auto& obj = std::get<Object>(objInfo);
379
380 for (auto& iface : obj)
Brad Bishope0b7d052017-01-06 15:30:23 -0500381 {
Patrick Venture1e6324f2017-06-01 14:07:05 -0700382 auto valueIface = std::shared_ptr<ValueObject>();
383 auto warnIface = std::shared_ptr<WarningObject>();
384 auto critIface = std::shared_ptr<CriticalObject>();
385
386 switch (iface.first)
387 {
388 case InterfaceType::VALUE:
389 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
390 (iface.second);
391 valueIface->value(value);
392 break;
393 case InterfaceType::WARN:
394 checkThresholds<WarningObject>(iface.second, value);
395 break;
396 case InterfaceType::CRIT:
397 checkThresholds<CriticalObject>(iface.second, value);
398 break;
399 default:
400 break;
401 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500402 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500403 }
Patrick Venture1e6324f2017-06-01 14:07:05 -0700404 catch (const std::exception& e)
405 {
406 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
407 commit<ReadFailure>();
408
409 destroy.push_back(i.first);
410 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500411 }
412 }
413
Patrick Venture1e6324f2017-06-01 14:07:05 -0700414 for (auto& i : destroy)
415 {
416 state.erase(i);
417 }
418
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500419 // Respond to DBus
420 _bus.process_discard();
421
Brad Bishope55ef3d2016-12-19 09:12:40 -0500422 // Sleep until next interval.
Brad Bishope55ef3d2016-12-19 09:12:40 -0500423 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Patrick Ventureab10f162017-05-22 09:44:50 -0700424 _bus.wait(_interval);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500425
426 // TODO: Issue#7 - Should probably periodically check the SensorSet
427 // for new entries.
428 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500429}
430
431// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4