blob: 1bef2881607048b2ecdba1293bae913e348c860d [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 <cstring>
19#include <cstdlib>
20#include <chrono>
Brad Bishop74aa4dd2017-01-06 09:50:31 -050021#include <algorithm>
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 Bishopab795a12017-01-05 20:50:49 -050026#include "util.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050027#include "env.hpp"
Brad Bishope0b7d052017-01-06 15:30:23 -050028#include "thresholds.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050029
Brad Bishop9c7b6e02016-12-19 12:43:36 -050030using namespace std::literals::chrono_literals;
31
Brad Bishop74aa4dd2017-01-06 09:50:31 -050032static constexpr auto typeAttrMap =
33{
34 // 1 - hwmon class
35 // 2 - unit
36 // 3 - sysfs scaling factor
37 std::make_tuple(
38 hwmon::type::ctemp,
39 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050040 -3,
41 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050042 std::make_tuple(
43 hwmon::type::cfan,
44 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050045 0,
46 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050047 std::make_tuple(
48 hwmon::type::cvolt,
49 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050050 -3,
51 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050052 std::make_tuple(
53 hwmon::type::ccurr,
54 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -050055 -3,
56 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050057 std::make_tuple(
58 hwmon::type::cenergy,
59 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -050060 -6,
61 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050062 std::make_tuple(
63 hwmon::type::cpower,
64 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -050065 -6,
66 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050067};
68
69auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
70{
71 return std::get<0>(attrs);
72}
73
74auto getUnit(decltype(typeAttrMap)::const_reference attrs)
75{
76 return std::get<1>(attrs);
77}
78
79auto getScale(decltype(typeAttrMap)::const_reference attrs)
80{
81 return std::get<2>(attrs);
82}
83
Brad Bishopadd98512017-01-06 22:01:19 -050084auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
85{
86 return std::get<3>(attrs);
87}
88
Brad Bishop951a79e2017-01-06 21:55:11 -050089using AttributeIterator = decltype(*typeAttrMap.begin());
90using Attributes
91 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
92
93auto getAttributes(const std::string& type, Attributes& attributes)
94{
95 // *INDENT-OFF*
96 auto a = std::find_if(
97 typeAttrMap.begin(),
98 typeAttrMap.end(),
99 [&](const auto & e)
100 {
101 return type == getHwmonType(e);
102 });
103 // *INDENT-ON*
104
105 if (a == typeAttrMap.end())
106 {
107 return false;
108 }
109
110 attributes = *a;
111 return true;
112}
113
Brad Bishope9fdee02017-01-06 10:43:29 -0500114auto addValue(const SensorSet::key_type& sensor,
115 const std::string& sysfsRoot, ObjectInfo& info)
116{
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
124 auto sysfsPath = make_sysfs_path(
125 sysfsRoot,
126 sensor.first,
127 sensor.second,
128 hwmon::entry::input);
129 int val = 0;
130 read_sysfs(sysfsPath, val);
131
Brad Bishop30dbcee2017-01-18 07:55:42 -0500132 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500133 iface->value(val);
134
Brad Bishop951a79e2017-01-06 21:55:11 -0500135 Attributes attrs;
136 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500137 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500138 iface->unit(getUnit(attrs));
139 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500140 }
141
142 obj[InterfaceType::VALUE] = iface;
143 return iface;
144}
145
Brad Bishopb9e2b072016-12-19 13:47:10 -0500146MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500147 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500148 const std::string& path,
149 const char* prefix,
150 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500151 : _bus(std::move(bus)),
152 _manager(sdbusplus::server::manager::manager(_bus, root)),
153 _shutdown(false),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500154 _path(path),
155 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500156 _root(root),
157 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500158{
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500159 if (_path.back() == '/')
160 {
161 _path.pop_back();
162 }
Brad Bishopd499ca62016-12-19 09:24:50 -0500163}
164
165void MainLoop::shutdown() noexcept
166{
167 _shutdown = true;
168}
169
170void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500171{
172 // Check sysfs for available sensors.
Brad Bishopd499ca62016-12-19 09:24:50 -0500173 auto sensors = std::make_unique<SensorSet>(_path);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500174
Brad Bishop75b4ab82017-01-06 09:33:50 -0500175 for (auto& i : *sensors)
176 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500177 // Get sensor configuration from the environment.
178
Brad Bishop73831cd2017-01-06 09:37:22 -0500179 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500180 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500181 if (label.empty())
182 {
183 continue;
184 }
185
Brad Bishopadd98512017-01-06 22:01:19 -0500186 Attributes attrs;
187 if (!getAttributes(i.first.first, attrs))
188 {
189 continue;
190 }
191
Brad Bishop075f7a22017-01-06 09:45:08 -0500192 std::string objectPath{_root};
Brad Bishop075f7a22017-01-06 09:45:08 -0500193 objectPath.append("/");
Brad Bishopadd98512017-01-06 22:01:19 -0500194 objectPath.append(getNamespace(attrs));
Brad Bishop075f7a22017-01-06 09:45:08 -0500195 objectPath.append("/");
196 objectPath.append(label);
197
Brad Bishopf7426cf2017-01-06 15:36:43 -0500198 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishope9fdee02017-01-06 10:43:29 -0500199 auto valueInterface = addValue(i.first, _path, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500200 auto sensorValue = valueInterface->value();
201 addThreshold<WarningObject>(i.first, sensorValue, info);
202 addThreshold<CriticalObject>(i.first, sensorValue, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500203
Brad Bishop30dbcee2017-01-18 07:55:42 -0500204 // All the interfaces have been created. Go ahead
205 // and emit InterfacesAdded.
206 valueInterface->emit_object_added();
207
Brad Bishop075f7a22017-01-06 09:45:08 -0500208 auto value = std::make_tuple(
209 std::move(i.second),
210 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500211 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500212
Brad Bishop75b4ab82017-01-06 09:33:50 -0500213 state[std::move(i.first)] = std::move(value);
214 }
215
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500216 {
Brad Bishopab795a12017-01-05 20:50:49 -0500217 auto copy = std::unique_ptr<char, phosphor::utility::Free<char>>(strdup(
218 _path.c_str()));
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500219 auto busname = std::string(_prefix) + '.' + basename(copy.get());
220 _bus.request_name(busname.c_str());
221 }
222
Brad Bishope55ef3d2016-12-19 09:12:40 -0500223 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
224 // ensure the objects all exist?
225
226 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500227 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500228 {
229 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500230 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500231 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500232 auto& attrs = std::get<0>(i.second);
233 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500234 {
235 // Read value from sensor.
236 int value = 0;
Brad Bishopd499ca62016-12-19 09:24:50 -0500237 read_sysfs(make_sysfs_path(_path,
Brad Bishope55ef3d2016-12-19 09:12:40 -0500238 i.first.first, i.first.second,
239 hwmon::entry::input),
240 value);
241
Brad Bishopf7426cf2017-01-06 15:36:43 -0500242 auto& objInfo = std::get<ObjectInfo>(i.second);
243 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500244
Brad Bishope0b7d052017-01-06 15:30:23 -0500245 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500246 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500247 auto valueIface = std::shared_ptr<ValueObject>();
248 auto warnIface = std::shared_ptr<WarningObject>();
249 auto critIface = std::shared_ptr<CriticalObject>();
250
251 switch (iface.first)
252 {
253 case InterfaceType::VALUE:
254 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
255 (iface.second);
256 valueIface->value(value);
257 break;
258 case InterfaceType::WARN:
259 checkThresholds<WarningObject>(iface.second, value);
260 break;
261 case InterfaceType::CRIT:
262 checkThresholds<CriticalObject>(iface.second, value);
263 break;
264 default:
265 break;
266 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500267 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500268 }
269 }
270
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500271 // Respond to DBus
272 _bus.process_discard();
273
Brad Bishope55ef3d2016-12-19 09:12:40 -0500274 // Sleep until next interval.
275 // TODO: Issue#5 - Make this configurable.
276 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500277 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500278
279 // TODO: Issue#7 - Should probably periodically check the SensorSet
280 // for new entries.
281 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500282}
283
284// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4