blob: 78d0b1a3e4a26db666e20730b2c1979409a459d3 [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,
40 -3),
41 std::make_tuple(
42 hwmon::type::cfan,
43 ValueInterface::Unit::RPMS,
44 0),
45 std::make_tuple(
46 hwmon::type::cvolt,
47 ValueInterface::Unit::Volts,
48 -3),
49};
50
51auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
52{
53 return std::get<0>(attrs);
54}
55
56auto getUnit(decltype(typeAttrMap)::const_reference attrs)
57{
58 return std::get<1>(attrs);
59}
60
61auto getScale(decltype(typeAttrMap)::const_reference attrs)
62{
63 return std::get<2>(attrs);
64}
65
Brad Bishop951a79e2017-01-06 21:55:11 -050066using AttributeIterator = decltype(*typeAttrMap.begin());
67using Attributes
68 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
69
70auto getAttributes(const std::string& type, Attributes& attributes)
71{
72 // *INDENT-OFF*
73 auto a = std::find_if(
74 typeAttrMap.begin(),
75 typeAttrMap.end(),
76 [&](const auto & e)
77 {
78 return type == getHwmonType(e);
79 });
80 // *INDENT-ON*
81
82 if (a == typeAttrMap.end())
83 {
84 return false;
85 }
86
87 attributes = *a;
88 return true;
89}
90
Brad Bishope9fdee02017-01-06 10:43:29 -050091auto addValue(const SensorSet::key_type& sensor,
92 const std::string& sysfsRoot, ObjectInfo& info)
93{
94 // Get the initial value for the value interface.
95 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
96 auto& obj = std::get<Object>(info);
97 auto& objPath = std::get<std::string>(info);
98
99 auto sysfsPath = make_sysfs_path(
100 sysfsRoot,
101 sensor.first,
102 sensor.second,
103 hwmon::entry::input);
104 int val = 0;
105 read_sysfs(sysfsPath, val);
106
107 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str());
108 iface->value(val);
109
Brad Bishop951a79e2017-01-06 21:55:11 -0500110 Attributes attrs;
111 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500112 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500113 iface->unit(getUnit(attrs));
114 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500115 }
116
117 obj[InterfaceType::VALUE] = iface;
118 return iface;
119}
120
Brad Bishopb9e2b072016-12-19 13:47:10 -0500121MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500122 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500123 const std::string& path,
124 const char* prefix,
125 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500126 : _bus(std::move(bus)),
127 _manager(sdbusplus::server::manager::manager(_bus, root)),
128 _shutdown(false),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500129 _path(path),
130 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500131 _root(root),
132 state()
Brad Bishopd499ca62016-12-19 09:24:50 -0500133{
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500134 if (_path.back() == '/')
135 {
136 _path.pop_back();
137 }
Brad Bishopd499ca62016-12-19 09:24:50 -0500138}
139
140void MainLoop::shutdown() noexcept
141{
142 _shutdown = true;
143}
144
145void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500146{
147 // Check sysfs for available sensors.
Brad Bishopd499ca62016-12-19 09:24:50 -0500148 auto sensors = std::make_unique<SensorSet>(_path);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500149
Brad Bishop75b4ab82017-01-06 09:33:50 -0500150 for (auto& i : *sensors)
151 {
Brad Bishopf3df6b42017-01-06 10:14:09 -0500152 // Get sensor configuration from the environment.
153
Brad Bishop73831cd2017-01-06 09:37:22 -0500154 // Ignore inputs without a label.
Brad Bishopf3df6b42017-01-06 10:14:09 -0500155 auto label = getEnv("LABEL", i.first);
Brad Bishop73831cd2017-01-06 09:37:22 -0500156 if (label.empty())
157 {
158 continue;
159 }
160
Brad Bishop075f7a22017-01-06 09:45:08 -0500161 std::string objectPath{_root};
Brad Bishop075f7a22017-01-06 09:45:08 -0500162 objectPath.append("/");
163 objectPath.append(i.first.first);
164 objectPath.append("/");
165 objectPath.append(label);
166
Brad Bishopf7426cf2017-01-06 15:36:43 -0500167 ObjectInfo info(&_bus, std::move(objectPath), Object());
Brad Bishope9fdee02017-01-06 10:43:29 -0500168 auto valueInterface = addValue(i.first, _path, info);
Brad Bishope0b7d052017-01-06 15:30:23 -0500169 auto sensorValue = valueInterface->value();
170 addThreshold<WarningObject>(i.first, sensorValue, info);
171 addThreshold<CriticalObject>(i.first, sensorValue, info);
Brad Bishop075f7a22017-01-06 09:45:08 -0500172
173 auto value = std::make_tuple(
174 std::move(i.second),
175 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500176 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500177
Brad Bishop75b4ab82017-01-06 09:33:50 -0500178 state[std::move(i.first)] = std::move(value);
179 }
180
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500181 {
Brad Bishopab795a12017-01-05 20:50:49 -0500182 auto copy = std::unique_ptr<char, phosphor::utility::Free<char>>(strdup(
183 _path.c_str()));
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500184 auto busname = std::string(_prefix) + '.' + basename(copy.get());
185 _bus.request_name(busname.c_str());
186 }
187
Brad Bishope55ef3d2016-12-19 09:12:40 -0500188 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
189 // ensure the objects all exist?
190
191 // Polling loop.
Brad Bishopd499ca62016-12-19 09:24:50 -0500192 while (!_shutdown)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500193 {
194 // Iterate through all the sensors.
Brad Bishop75b4ab82017-01-06 09:33:50 -0500195 for (auto& i : state)
Brad Bishope55ef3d2016-12-19 09:12:40 -0500196 {
Brad Bishop75b4ab82017-01-06 09:33:50 -0500197 auto& attrs = std::get<0>(i.second);
198 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500199 {
200 // Read value from sensor.
201 int value = 0;
Brad Bishopd499ca62016-12-19 09:24:50 -0500202 read_sysfs(make_sysfs_path(_path,
Brad Bishope55ef3d2016-12-19 09:12:40 -0500203 i.first.first, i.first.second,
204 hwmon::entry::input),
205 value);
206
Brad Bishopf7426cf2017-01-06 15:36:43 -0500207 auto& objInfo = std::get<ObjectInfo>(i.second);
208 auto& obj = std::get<Object>(objInfo);
Brad Bishopdddb7152017-01-06 09:54:23 -0500209
Brad Bishope0b7d052017-01-06 15:30:23 -0500210 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500211 {
Brad Bishope0b7d052017-01-06 15:30:23 -0500212 auto valueIface = std::shared_ptr<ValueObject>();
213 auto warnIface = std::shared_ptr<WarningObject>();
214 auto critIface = std::shared_ptr<CriticalObject>();
215
216 switch (iface.first)
217 {
218 case InterfaceType::VALUE:
219 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
220 (iface.second);
221 valueIface->value(value);
222 break;
223 case InterfaceType::WARN:
224 checkThresholds<WarningObject>(iface.second, value);
225 break;
226 case InterfaceType::CRIT:
227 checkThresholds<CriticalObject>(iface.second, value);
228 break;
229 default:
230 break;
231 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500232 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500233 }
234 }
235
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500236 // Respond to DBus
237 _bus.process_discard();
238
Brad Bishope55ef3d2016-12-19 09:12:40 -0500239 // Sleep until next interval.
240 // TODO: Issue#5 - Make this configurable.
241 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500242 _bus.wait((1000000us).count());
Brad Bishope55ef3d2016-12-19 09:12:40 -0500243
244 // TODO: Issue#7 - Should probably periodically check the SensorSet
245 // for new entries.
246 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500247}
248
249// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4