blob: 399b17e7c3f17c16b3a0c37fac4b4855036732da [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>
19#include <chrono>
Brad Bishop74aa4dd2017-01-06 09:50:31 -050020#include <algorithm>
Brad Bishope55ef3d2016-12-19 09:12:40 -050021#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050022#include "hwmon.hpp"
23#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050024#include "mainloop.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050025#include "env.hpp"
Brad Bishope0b7d052017-01-06 15:30:23 -050026#include "thresholds.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050027
Saqib Khan973886d2017-03-15 14:01:16 -050028// Initialization for Warning Objects
29decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
30 &WarningObject::warningLow;
31decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
32 &WarningObject::warningHigh;
33decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
34 &WarningObject::warningLow;
35decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
36 &WarningObject::warningHigh;
37decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
38 &WarningObject::warningAlarmLow;
39decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
40 &WarningObject::warningAlarmHigh;
41
42// Initialization for Critical Objects
43decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
44 &CriticalObject::criticalLow;
45decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
46 &CriticalObject::criticalHigh;
47decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
48 &CriticalObject::criticalLow;
49decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
50 &CriticalObject::criticalHigh;
51decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
52 &CriticalObject::criticalAlarmLow;
53decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
54 &CriticalObject::criticalAlarmHigh;
55
56
Brad Bishop9c7b6e02016-12-19 12:43:36 -050057using namespace std::literals::chrono_literals;
58
Brad Bishop74aa4dd2017-01-06 09:50:31 -050059static constexpr auto typeAttrMap =
60{
61 // 1 - hwmon class
62 // 2 - unit
63 // 3 - sysfs scaling factor
64 std::make_tuple(
65 hwmon::type::ctemp,
66 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050067 -3,
68 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050069 std::make_tuple(
70 hwmon::type::cfan,
71 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050072 0,
73 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050074 std::make_tuple(
75 hwmon::type::cvolt,
76 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050077 -3,
78 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050079 std::make_tuple(
80 hwmon::type::ccurr,
81 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050082 -3,
83 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050084 std::make_tuple(
85 hwmon::type::cenergy,
86 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050087 -6,
88 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050089 std::make_tuple(
90 hwmon::type::cpower,
91 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050092 -6,
93 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050094};
95
96auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
97{
98 return std::get<0>(attrs);
99}
100
101auto getUnit(decltype(typeAttrMap)::const_reference attrs)
102{
103 return std::get<1>(attrs);
104}
105
106auto getScale(decltype(typeAttrMap)::const_reference attrs)
107{
108 return std::get<2>(attrs);
109}
110
Brad Bishopadd98512017-01-06 22:01:19 -0500111auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
112{
113 return std::get<3>(attrs);
114}
115
Brad Bishop951a79e2017-01-06 21:55:11 -0500116using AttributeIterator = decltype(*typeAttrMap.begin());
117using Attributes
118 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
119
120auto getAttributes(const std::string& type, Attributes& attributes)
121{
122 // *INDENT-OFF*
123 auto a = std::find_if(
124 typeAttrMap.begin(),
125 typeAttrMap.end(),
126 [&](const auto & e)
127 {
128 return type == getHwmonType(e);
129 });
130 // *INDENT-ON*
131
132 if (a == typeAttrMap.end())
133 {
134 return false;
135 }
136
137 attributes = *a;
138 return true;
139}
140
Brad Bishope9fdee02017-01-06 10:43:29 -0500141auto addValue(const SensorSet::key_type& sensor,
Brad Bishop4db64422017-02-16 11:33:32 -0500142 const std::string& hwmonRoot,
143 const std::string& instance,
144 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500145{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500146 static constexpr bool deferSignals = true;
147
Brad Bishope9fdee02017-01-06 10:43:29 -0500148 // Get the initial value for the value interface.
149 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
150 auto& obj = std::get<Object>(info);
151 auto& objPath = std::get<std::string>(info);
152
Brad Bishop4db64422017-02-16 11:33:32 -0500153 int val = readSysfsWithCallout(hwmonRoot,
154 instance,
155 sensor.first,
156 sensor.second,
157 hwmon::entry::input);
Brad Bishop30dbcee2017-01-18 07:55:42 -0500158 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500159 iface->value(val);
160
Brad Bishop951a79e2017-01-06 21:55:11 -0500161 Attributes attrs;
162 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500163 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500164 iface->unit(getUnit(attrs));
165 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500166 }
167
168 obj[InterfaceType::VALUE] = iface;
169 return iface;
170}
171
Brad Bishopb9e2b072016-12-19 13:47:10 -0500172MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500173 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500174 const std::string& path,
175 const char* prefix,
176 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500177 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500178 _manager(_bus, root),
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500179 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500180 _hwmonRoot(),
181 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500182 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500183 _root(root),
184 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500185{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500186 std::string p = path;
187 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500188 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500189 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500190 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500191
192 auto n = p.rfind('/');
193 if (n != std::string::npos)
194 {
195 _instance.assign(p.substr(n + 1));
196 _hwmonRoot.assign(p.substr(0, n));
197 }
198
199 assert(!_instance.empty());
200 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500201}
202
203void MainLoop::shutdown() noexcept
204{
205 _shutdown = true;
206}
207
208void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500209{
210 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500211 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500212
Brad Bishop75b4ab82017-01-06 09:33:50 -0500213 for (auto& i : *sensors)
214 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500215 // Get sensor configuration from the environment.
216
Brad Bishop73831cd2017-01-06 09:37:22 -0500217 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500218 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500219 if (label.empty())
220 {
221 continue;
222 }
223
Brad Bishopadd98512017-01-06 22:01:19 -0500224 Attributes attrs;
225 if (!getAttributes(i.first.first, attrs))
226 {
227 continue;
228 }
229
Brad Bishop075f7a22017-01-06 09:45:08 -0500230 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500231 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500232 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500233 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500234 objectPath.append(label);
235
Brad Bishopf7426cf2017-01-06 15:36:43 -0500236 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop4db64422017-02-16 11:33:32 -0500237 auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500238 auto sensorValue = valueInterface->value();
239 addThreshold<WarningObject>(i.first, sensorValue, info);
240 addThreshold<CriticalObject>(i.first, sensorValue, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500241
Brad Bishop30dbcee2017-01-18 07:55:42 -0500242 // All the interfaces have been created. Go ahead
243 // and emit InterfacesAdded.
244 valueInterface->emit_object_added();
245
Brad Bishop075f7a22017-01-06 09:45:08 -0500246 auto value = std::make_tuple(
247 std::move(i.second),
248 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500249 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500250
Brad Bishop75b4ab82017-01-06 09:33:50 -0500251 state[std::move(i.first)] = std::move(value);
252 }
253
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500254 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500255 std::string busname{_prefix};
256 busname.append(1, '.');
257 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500258 _bus.request_name(busname.c_str());
259 }
260
Brad Bishope55ef3d2016-12-19 09:12:40 -0500261 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
262 // ensure the objects all exist?
263
264 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500265 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500266 {
267 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500268 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500269 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500270 auto& attrs = std::get<0>(i.second);
271 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500272 {
273 // Read value from sensor.
Brad Bishop4db64422017-02-16 11:33:32 -0500274 int value = readSysfsWithCallout(_hwmonRoot,
275 _instance,
276 i.first.first,
277 i.first.second,
278 hwmon::entry::input);
Brad Bishopf7426cf2017-01-06 15:36:43 -0500279 auto& objInfo = std::get<ObjectInfo>(i.second);
280 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500281
Brad Bishope0b7d052017-01-06 15:30:23 -0500282 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500283 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500284 auto valueIface = std::shared_ptr<ValueObject>();
285 auto warnIface = std::shared_ptr<WarningObject>();
286 auto critIface = std::shared_ptr<CriticalObject>();
287
288 switch (iface.first)
289 {
290 case InterfaceType::VALUE:
291 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
292 (iface.second);
293 valueIface->value(value);
294 break;
295 case InterfaceType::WARN:
296 checkThresholds<WarningObject>(iface.second, value);
297 break;
298 case InterfaceType::CRIT:
299 checkThresholds<CriticalObject>(iface.second, value);
300 break;
301 default:
302 break;
303 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500304 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500305 }
306 }
307
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500308 // Respond to DBus
309 _bus.process_discard();
310
Brad Bishope55ef3d2016-12-19 09:12:40 -0500311 // Sleep until next interval.
312 // TODO: Issue#5 - Make this configurable.
313 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500314 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500315
316 // TODO: Issue#7 - Should probably periodically check the SensorSet
317 // for new entries.
318 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500319}
320
321// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4