blob: 1ac09883a0f74c01abc01bb2cc6ffcd34d428036 [file] [log] [blame]
James Feist139cb572018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 Intel 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
17#include <unistd.h>
18
19#include <ADCSensor.hpp>
20#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
22#include <boost/date_time/posix_time/posix_time.hpp>
23#include <iostream>
24#include <limits>
25#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <string>
28
29static constexpr unsigned int SENSOR_POLL_MS = 500;
30static constexpr size_t WARN_AFTER_ERROR_COUNT = 10;
31
32// scaling factor from hwmon
33static constexpr unsigned int SENSOR_SCALE_FACTOR = 1000;
34
35ADCSensor::ADCSensor(const std::string &path,
36 sdbusplus::asio::object_server &objectServer,
37 std::shared_ptr<sdbusplus::asio::connection> &conn,
38 boost::asio::io_service &io,
39 const std::string &sensor_name,
40 std::vector<thresholds::Threshold> &&_thresholds,
41 const double scale_factor,
42 const std::string &sensorConfiguration) :
43 path(path),
44 objServer(objectServer), configuration(sensorConfiguration),
45 name(boost::replace_all_copy(sensor_name, " ", "_")),
46 thresholds(std::move(_thresholds)), scale_factor(scale_factor),
47 sensor_interface(objectServer.add_interface(
48 "/xyz/openbmc_project/sensors/voltage/" + name,
49 "xyz.openbmc_project.Sensor.Value")),
50 input_dev(io, open(path.c_str(), O_RDONLY)), wait_timer(io),
51 value(std::numeric_limits<double>::quiet_NaN()), err_count(0),
52 // todo, get these from config
53 max_value(20), min_value(0)
54{
55 if (thresholds::HasWarningInterface(thresholds))
56 {
57 threshold_interface_warning = objectServer.add_interface(
58 "/xyz/openbmc_project/sensors/voltage/" + name,
59 "xyz.openbmc_project.Sensor.Threshold.Warning");
60 }
61 if (thresholds::HasCriticalInterface(thresholds))
62 {
63 threshold_interface_critical = objectServer.add_interface(
64 "/xyz/openbmc_project/sensors/voltage/" + name,
65 "xyz.openbmc_project.Sensor.Threshold.Critical");
66 }
67 set_initial_properties(conn);
68 setup_read();
69}
70
71ADCSensor::~ADCSensor()
72{
73 // close the input dev to cancel async operations
74 input_dev.close();
75 wait_timer.cancel();
76 objServer.remove_interface(threshold_interface_warning);
77 objServer.remove_interface(threshold_interface_critical);
78 objServer.remove_interface(sensor_interface);
79}
80
81void ADCSensor::setup_read(void)
82{
83 boost::asio::async_read_until(
84 input_dev, read_buf, '\n',
85 [&](const boost::system::error_code &ec,
86 std::size_t /*bytes_transfered*/) { handle_response(ec); });
87}
88
89void ADCSensor::handle_response(const boost::system::error_code &err)
90{
91 if (err == boost::system::errc::bad_file_descriptor)
92 {
93 return; // we're being destroyed
94 }
95 std::istream response_stream(&read_buf);
96
97 if (!err)
98 {
99 std::string response;
100 std::getline(response_stream, response);
101
102 // todo read scaling factors from configuration
103 try
104 {
105 float nvalue = std::stof(response);
106
107 nvalue = (nvalue / SENSOR_SCALE_FACTOR) / scale_factor;
108
109 if (nvalue != value)
110 {
111 update_value(nvalue);
112 }
113 err_count = 0;
114 }
115 catch (std::invalid_argument)
116 {
117 err_count++;
118 }
119 }
120 else
121 {
122 std::cerr << "Failure to read sensor " << name << " at " << path
123 << " ec:" << err << "\n";
124
125 err_count++;
126 }
127
128 // only send value update once
129 if (err_count == WARN_AFTER_ERROR_COUNT)
130 {
131 update_value(0);
132 }
133
134 response_stream.clear();
135 input_dev.close();
136 int fd = open(path.c_str(), O_RDONLY);
137 if (fd <= 0)
138 {
139 return; // we're no longer valid
140 }
141 input_dev.assign(fd);
142 wait_timer.expires_from_now(
143 boost::posix_time::milliseconds(SENSOR_POLL_MS));
144 wait_timer.async_wait([&](const boost::system::error_code &ec) {
145 if (ec == boost::asio::error::operation_aborted)
146 {
147 return; // we're being canceled
148 }
149 setup_read();
150 });
151}
152
153void ADCSensor::check_thresholds(void)
154{
155 if (thresholds.empty())
156 return;
157 for (auto threshold : thresholds)
158 {
159 if (threshold.direction == thresholds::Direction::HIGH)
160 {
161 if (value > threshold.value)
162 {
163 assert_thresholds(threshold.level, threshold.direction, true);
164 }
165 else
166 {
167 assert_thresholds(threshold.level, threshold.direction, false);
168 }
169 }
170 else
171 {
172 if (value < threshold.value)
173 {
174 assert_thresholds(threshold.level, threshold.direction, true);
175 }
176 else
177 {
178 assert_thresholds(threshold.level, threshold.direction, false);
179 }
180 }
181 }
182}
183
184void ADCSensor::update_value(const double &new_value)
185{
186 bool ret = sensor_interface->set_property("Value", new_value);
187 value = new_value;
188 check_thresholds();
189}
190
191void ADCSensor::assert_thresholds(thresholds::Level level,
192 thresholds::Direction direction, bool assert)
193{
194 std::string property;
195 std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
196 if (level == thresholds::Level::WARNING &&
197 direction == thresholds::Direction::HIGH)
198 {
199 property = "WarningAlarmHigh";
200 interface = threshold_interface_warning;
201 }
202 else if (level == thresholds::Level::WARNING &&
203 direction == thresholds::Direction::LOW)
204 {
205 property = "WarningAlarmLow";
206 interface = threshold_interface_warning;
207 }
208 else if (level == thresholds::Level::CRITICAL &&
209 direction == thresholds::Direction::HIGH)
210 {
211 property = "CriticalAlarmHigh";
212 interface = threshold_interface_critical;
213 }
214 else if (level == thresholds::Level::CRITICAL &&
215 direction == thresholds::Direction::LOW)
216 {
217 property = "CriticalAlarmLow";
218 interface = threshold_interface_critical;
219 }
220 else
221 {
222 std::cerr << "Unknown threshold, level " << level << "direction "
223 << direction << "\n";
224 return;
225 }
226 if (!interface)
227 {
228 std::cout << "trying to set uninitialized interface\n";
229 return;
230 }
231 interface->set_property(property, assert);
232}
233
234void ADCSensor::set_initial_properties(
235 std::shared_ptr<sdbusplus::asio::connection> &conn)
236{
237 // todo, get max and min from configuration
238 sensor_interface->register_property("MaxValue", max_value);
239 sensor_interface->register_property("MinValue", min_value);
240 sensor_interface->register_property("Value", value);
241
242 for (auto &threshold : thresholds)
243 {
244 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
245 std::string level;
246 std::string alarm;
247 if (threshold.level == thresholds::Level::CRITICAL)
248 {
249 iface = threshold_interface_critical;
250 if (threshold.direction == thresholds::Direction::HIGH)
251 {
252 level = "CriticalHigh";
253 alarm = "CriticalAlarmHigh";
254 }
255 else
256 {
257 level = "CriticalLow";
258 alarm = "CriticalAlarmLow";
259 }
260 }
261 else if (threshold.level == thresholds::Level::WARNING)
262 {
263 iface = threshold_interface_warning;
264 if (threshold.direction == thresholds::Direction::HIGH)
265 {
266 level = "WarningHigh";
267 alarm = "WarningAlarmHigh";
268 }
269 else
270 {
271 level = "WarningLow";
272 alarm = "WarningAlarmLow";
273 }
274 }
275 else
276 {
277 std::cerr << "Unknown threshold level" << threshold.level << "\n";
278 continue;
279 }
280 if (!iface)
281 {
282 std::cout << "trying to set uninitialized interface\n";
283 continue;
284 }
285 iface->register_property(
286 level, threshold.value,
287 [&](const double &request, double &oldValue) {
288 oldValue = request; // todo, just let the config do this?
289 threshold.value = request;
290 thresholds::persistThreshold(
291 configuration, "xyz.openbmc_project.Configuration.ADC",
292 threshold, conn);
293 return 1;
294 });
295 iface->register_property(alarm, false);
296 }
297 if (!sensor_interface->initialize())
298 {
299 std::cerr << "error initializing value interface\n";
300 }
301 if (threshold_interface_warning &&
302 !threshold_interface_warning->initialize())
303 {
304 std::cerr << "error initializing warning threshold interface\n";
305 }
306
307 if (threshold_interface_critical &&
308 !threshold_interface_critical->initialize())
309 {
310 std::cerr << "error initializing critical threshold interface\n";
311 }
312}