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