blob: 0077d33d6a861e41de0bf83828a7f546bc150d3d [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
Brad Bishop9c7b6e02016-12-19 12:43:36 -050028using namespace std::literals::chrono_literals;
29
Brad Bishop74aa4dd2017-01-06 09:50:31 -050030static constexpr auto typeAttrMap =
31{
32 // 1 - hwmon class
33 // 2 - unit
34 // 3 - sysfs scaling factor
35 std::make_tuple(
36 hwmon::type::ctemp,
37 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050038 -3,
39 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050040 std::make_tuple(
41 hwmon::type::cfan,
42 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050043 0,
44 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050045 std::make_tuple(
46 hwmon::type::cvolt,
47 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050048 -3,
49 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050050 std::make_tuple(
51 hwmon::type::ccurr,
52 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050053 -3,
54 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050055 std::make_tuple(
56 hwmon::type::cenergy,
57 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050058 -6,
59 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050060 std::make_tuple(
61 hwmon::type::cpower,
62 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050063 -6,
64 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050065};
66
67auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
68{
69 return std::get<0>(attrs);
70}
71
72auto getUnit(decltype(typeAttrMap)::const_reference attrs)
73{
74 return std::get<1>(attrs);
75}
76
77auto getScale(decltype(typeAttrMap)::const_reference attrs)
78{
79 return std::get<2>(attrs);
80}
81
Brad Bishopadd98512017-01-06 22:01:19 -050082auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
83{
84 return std::get<3>(attrs);
85}
86
Brad Bishop951a79e2017-01-06 21:55:11 -050087using AttributeIterator = decltype(*typeAttrMap.begin());
88using Attributes
89 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
90
91auto getAttributes(const std::string& type, Attributes& attributes)
92{
93 // *INDENT-OFF*
94 auto a = std::find_if(
95 typeAttrMap.begin(),
96 typeAttrMap.end(),
97 [&](const auto & e)
98 {
99 return type == getHwmonType(e);
100 });
101 // *INDENT-ON*
102
103 if (a == typeAttrMap.end())
104 {
105 return false;
106 }
107
108 attributes = *a;
109 return true;
110}
111
Brad Bishope9fdee02017-01-06 10:43:29 -0500112auto addValue(const SensorSet::key_type& sensor,
Brad Bishop4db64422017-02-16 11:33:32 -0500113 const std::string& hwmonRoot,
114 const std::string& instance,
115 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500116{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500117 static constexpr bool deferSignals = true;
118
Brad Bishope9fdee02017-01-06 10:43:29 -0500119 // Get the initial value for the value interface.
120 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
121 auto& obj = std::get<Object>(info);
122 auto& objPath = std::get<std::string>(info);
123
Brad Bishop4db64422017-02-16 11:33:32 -0500124 int val = readSysfsWithCallout(hwmonRoot,
125 instance,
126 sensor.first,
127 sensor.second,
128 hwmon::entry::input);
Brad Bishop30dbcee2017-01-18 07:55:42 -0500129 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500130 iface->value(val);
131
Brad Bishop951a79e2017-01-06 21:55:11 -0500132 Attributes attrs;
133 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500134 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500135 iface->unit(getUnit(attrs));
136 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500137 }
138
139 obj[InterfaceType::VALUE] = iface;
140 return iface;
141}
142
Brad Bishopb9e2b072016-12-19 13:47:10 -0500143MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500144 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500145 const std::string& path,
146 const char* prefix,
147 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500148 : _bus(std::move(bus)),
149 _manager(sdbusplus::server::manager::manager(_bus, root)),
150 _shutdown(false),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500151 _hwmonRoot(),
152 _instance(),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500153 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500154 _root(root),
155 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500156{
Brad Bishopb8740fc2017-02-24 23:38:37 -0500157 std::string p = path;
158 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500159 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500160 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500161 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500162
163 auto n = p.rfind('/');
164 if (n != std::string::npos)
165 {
166 _instance.assign(p.substr(n + 1));
167 _hwmonRoot.assign(p.substr(0, n));
168 }
169
170 assert(!_instance.empty());
171 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500172}
173
174void MainLoop::shutdown() noexcept
175{
176 _shutdown = true;
177}
178
179void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500180{
181 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500182 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500183
Brad Bishop75b4ab82017-01-06 09:33:50 -0500184 for (auto& i : *sensors)
185 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500186 // Get sensor configuration from the environment.
187
Brad Bishop73831cd2017-01-06 09:37:22 -0500188 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500189 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500190 if (label.empty())
191 {
192 continue;
193 }
194
Brad Bishopadd98512017-01-06 22:01:19 -0500195 Attributes attrs;
196 if (!getAttributes(i.first.first, attrs))
197 {
198 continue;
199 }
200
Brad Bishop075f7a22017-01-06 09:45:08 -0500201 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500202 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500203 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500204 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500205 objectPath.append(label);
206
Brad Bishopf7426cf2017-01-06 15:36:43 -0500207 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishop4db64422017-02-16 11:33:32 -0500208 auto valueInterface = addValue(i.first, _hwmonRoot, _instance, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500209 auto sensorValue = valueInterface->value();
210 addThreshold<WarningObject>(i.first, sensorValue, info);
211 addThreshold<CriticalObject>(i.first, sensorValue, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500212
Brad Bishop30dbcee2017-01-18 07:55:42 -0500213 // All the interfaces have been created. Go ahead
214 // and emit InterfacesAdded.
215 valueInterface->emit_object_added();
216
Brad Bishop075f7a22017-01-06 09:45:08 -0500217 auto value = std::make_tuple(
218 std::move(i.second),
219 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500220 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500221
Brad Bishop75b4ab82017-01-06 09:33:50 -0500222 state[std::move(i.first)] = std::move(value);
223 }
224
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500225 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500226 std::string busname{_prefix};
227 busname.append(1, '.');
228 busname.append(_instance);
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500229 _bus.request_name(busname.c_str());
230 }
231
Brad Bishope55ef3d2016-12-19 09:12:40 -0500232 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
233 // ensure the objects all exist?
234
235 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500236 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500237 {
238 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500239 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500240 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500241 auto& attrs = std::get<0>(i.second);
242 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500243 {
244 // Read value from sensor.
Brad Bishop4db64422017-02-16 11:33:32 -0500245 int value = readSysfsWithCallout(_hwmonRoot,
246 _instance,
247 i.first.first,
248 i.first.second,
249 hwmon::entry::input);
Brad Bishopf7426cf2017-01-06 15:36:43 -0500250 auto& objInfo = std::get<ObjectInfo>(i.second);
251 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500252
Brad Bishope0b7d052017-01-06 15:30:23 -0500253 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500254 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500255 auto valueIface = std::shared_ptr<ValueObject>();
256 auto warnIface = std::shared_ptr<WarningObject>();
257 auto critIface = std::shared_ptr<CriticalObject>();
258
259 switch (iface.first)
260 {
261 case InterfaceType::VALUE:
262 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
263 (iface.second);
264 valueIface->value(value);
265 break;
266 case InterfaceType::WARN:
267 checkThresholds<WarningObject>(iface.second, value);
268 break;
269 case InterfaceType::CRIT:
270 checkThresholds<CriticalObject>(iface.second, value);
271 break;
272 default:
273 break;
274 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500275 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500276 }
277 }
278
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500279 // Respond to DBus
280 _bus.process_discard();
281
Brad Bishope55ef3d2016-12-19 09:12:40 -0500282 // Sleep until next interval.
283 // TODO: Issue#5 - Make this configurable.
284 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500285 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500286
287 // TODO: Issue#7 - Should probably periodically check the SensorSet
288 // for new entries.
289 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500290}
291
292// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4