blob: 14b570f35434f7db381c99d5f48fa00c8f9aede9 [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
Patrick Venture1e6324f2017-06-01 14:07:05 -0700159 int val;
160 try
161 {
162 val = sysfs::readSysfsWithCallout(hwmonRoot,
163 instance,
164 sensor.first,
165 sensor.second,
166 hwmon::entry::input);
167 }
168 catch(const std::exception& ioe)
169 {
170 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
171 commit<ReadFailure>();
172
173 return static_cast<std::shared_ptr<ValueObject>>(nullptr);
174 }
175
Brad Bishop30dbcee2017-01-18 07:55:42 -0500176 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500177 iface->value(val);
178
Brad Bishop951a79e2017-01-06 21:55:11 -0500179 Attributes attrs;
180 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500181 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500182 iface->unit(getUnit(attrs));
183 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500184 }
185
186 obj[InterfaceType::VALUE] = iface;
187 return iface;
188}
189
Brad Bishopb9e2b072016-12-19 13:47:10 -0500190MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500191 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500192 const std::string& path,
193 const char* prefix,
194 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500195 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500196 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500197 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500198 _hwmonRoot(),
199 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500200 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500201 _root(root),
202 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500203{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500204 std::string p = path;
205 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500206 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500207 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500208 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500209
210 auto n = p.rfind('/');
211 if (n != std::string::npos)
212 {
213 _instance.assign(p.substr(n + 1));
214 _hwmonRoot.assign(p.substr(0, n));
215 }
216
217 assert(!_instance.empty());
218 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500219}
220
221void MainLoop::shutdown() noexcept
222{
223 _shutdown = true;
224}
225
226void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500227{
228 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500229 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500230
Brad Bishop75b4ab82017-01-06 09:33:50 -0500231 for (auto& i : *sensors)
232 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530233 std::string label;
Brad Bishopf3df6b42017-01-06 10:14:09 -0500234
Tom Joseph1f8a9582017-06-12 20:10:59 +0530235 /*
236 * Check if the value of the MODE_<item><X> env variable for the sensor
237 * is "label", then read the sensor number from the <item><X>_label
238 * file. The name of the DBUS object would be the value of the env
239 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
240 * does'nt exist, then the name of DBUS object is the value of the env
241 * variable LABEL_<item><X>.
242 */
243 auto mode = getEnv("MODE", i.first);
244 if (!mode.compare(hwmon::entry::label))
Brad Bishop73831cd2017-01-06 09:37:22 -0500245 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530246 label = getIndirectLabelEnv(
247 "LABEL", _hwmonRoot + '/' + _instance + '/', i.first);
248 if (label.empty())
249 {
250 continue;
251 }
252 }
253 else
254 {
255 // Ignore inputs without a label.
256 label = getEnv("LABEL", i.first);
257 if (label.empty())
258 {
259 continue;
260 }
Brad Bishop73831cd2017-01-06 09:37:22 -0500261 }
262
Brad Bishopadd98512017-01-06 22:01:19 -0500263 Attributes attrs;
264 if (!getAttributes(i.first.first, attrs))
265 {
266 continue;
267 }
268
Brad Bishop075f7a22017-01-06 09:45:08 -0500269 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500270 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500271 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500272 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500273 objectPath.append(label);
274
Brad Bishopf7426cf2017-01-06 15:36:43 -0500275 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop4db64422017-02-16 11:33:32 -0500276 auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700277 if (!valueInterface)
278 {
279 continue; /* skip adding this sensor for now. */
280 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500281 auto sensorValue = valueInterface->value();
282 addThreshold<WarningObject>(i.first, sensorValue, info);
283 addThreshold<CriticalObject>(i.first, sensorValue, info);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600284 //TODO openbmc/openbmc#1347
285 // Handle application restarts to set/refresh fan speed values
Matt Spinler0a8de642017-05-11 10:59:39 -0500286 auto target = addTarget<hwmon::FanSpeed>(
287 i.first, _hwmonRoot, _instance, info);
288
289 if (target)
290 {
291 target->enable();
292 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500293
Brad Bishop30dbcee2017-01-18 07:55:42 -0500294 // All the interfaces have been created. Go ahead
295 // and emit InterfacesAdded.
296 valueInterface->emit_object_added();
297
Brad Bishop075f7a22017-01-06 09:45:08 -0500298 auto value = std::make_tuple(
299 std::move(i.second),
300 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500301 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500302
Brad Bishop75b4ab82017-01-06 09:33:50 -0500303 state[std::move(i.first)] = std::move(value);
304 }
305
Patrick Venture62503a42017-05-23 07:30:29 -0700306 /* If there are no sensors specified by labels, exit. */
307 if (0 == state.size())
308 {
309 return;
310 }
311
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500312 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500313 std::string busname{_prefix};
314 busname.append(1, '.');
315 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500316 _bus.request_name(busname.c_str());
317 }
318
Patrick Ventureab10f162017-05-22 09:44:50 -0700319 {
320 auto interval = getenv("INTERVAL");
321 if (interval)
322 {
323 _interval = strtoull(interval, NULL, 10);
324 }
325 }
326
Brad Bishope55ef3d2016-12-19 09:12:40 -0500327 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
328 // ensure the objects all exist?
329
330 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500331 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500332 {
Patrick Venture1e6324f2017-06-01 14:07:05 -0700333 std::vector<SensorSet::key_type> destroy;
Brad Bishope55ef3d2016-12-19 09:12:40 -0500334 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500335 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500336 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500337 auto& attrs = std::get<0>(i.second);
338 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500339 {
340 // Read value from sensor.
Patrick Venture1e6324f2017-06-01 14:07:05 -0700341 int value;
342 try
Brad Bishopdddb7152017-01-06 09:54:23 -0500343 {
Patrick Venture1e6324f2017-06-01 14:07:05 -0700344 value = sysfs::readSysfsWithCallout(_hwmonRoot,
345 _instance,
346 i.first.first,
347 i.first.second,
348 hwmon::entry::input);
Brad Bishope0b7d052017-01-06 15:30:23 -0500349
Patrick Venture1e6324f2017-06-01 14:07:05 -0700350 auto& objInfo = std::get<ObjectInfo>(i.second);
351 auto& obj = std::get<Object>(objInfo);
352
353 for (auto& iface : obj)
Brad Bishope0b7d052017-01-06 15:30:23 -0500354 {
Patrick Venture1e6324f2017-06-01 14:07:05 -0700355 auto valueIface = std::shared_ptr<ValueObject>();
356 auto warnIface = std::shared_ptr<WarningObject>();
357 auto critIface = std::shared_ptr<CriticalObject>();
358
359 switch (iface.first)
360 {
361 case InterfaceType::VALUE:
362 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
363 (iface.second);
364 valueIface->value(value);
365 break;
366 case InterfaceType::WARN:
367 checkThresholds<WarningObject>(iface.second, value);
368 break;
369 case InterfaceType::CRIT:
370 checkThresholds<CriticalObject>(iface.second, value);
371 break;
372 default:
373 break;
374 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500375 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500376 }
Patrick Venture1e6324f2017-06-01 14:07:05 -0700377 catch (const std::exception& e)
378 {
379 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
380 commit<ReadFailure>();
381
382 destroy.push_back(i.first);
383 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500384 }
385 }
386
Patrick Venture1e6324f2017-06-01 14:07:05 -0700387 for (auto& i : destroy)
388 {
389 state.erase(i);
390 }
391
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500392 // Respond to DBus
393 _bus.process_discard();
394
Brad Bishope55ef3d2016-12-19 09:12:40 -0500395 // Sleep until next interval.
Brad Bishope55ef3d2016-12-19 09:12:40 -0500396 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Patrick Ventureab10f162017-05-22 09:44:50 -0700397 _bus.wait(_interval);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500398
399 // TODO: Issue#7 - Should probably periodically check the SensorSet
400 // for new entries.
401 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500402}
403
404// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4