blob: b72f5c153362b75e74413316b41104f9c1638eab [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) :
James Feistaf79dd32018-09-12 12:54:15 -070043 Sensor(),
44 path(path), objServer(objectServer), configuration(sensorConfiguration),
James Feist139cb572018-09-10 15:26:18 -070045 name(boost::replace_all_copy(sensor_name, " ", "_")),
James Feistaf79dd32018-09-12 12:54:15 -070046 scale_factor(scale_factor), input_dev(io, open(path.c_str(), O_RDONLY)),
47 wait_timer(io), err_count(0),
James Feist139cb572018-09-10 15:26:18 -070048 // todo, get these from config
49 max_value(20), min_value(0)
50{
James Feistaf79dd32018-09-12 12:54:15 -070051 thresholds = std::move(_thresholds);
52 sensorInterface = objectServer.add_interface(
53 "/xyz/openbmc_project/sensors/voltage/" + name,
54 "xyz.openbmc_project.Sensor.Value");
James Feist139cb572018-09-10 15:26:18 -070055 if (thresholds::HasWarningInterface(thresholds))
56 {
James Feistaf79dd32018-09-12 12:54:15 -070057 thresholdInterfaceWarning = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070058 "/xyz/openbmc_project/sensors/voltage/" + name,
59 "xyz.openbmc_project.Sensor.Threshold.Warning");
60 }
61 if (thresholds::HasCriticalInterface(thresholds))
62 {
James Feistaf79dd32018-09-12 12:54:15 -070063 thresholdInterfaceCritical = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070064 "/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();
James Feistaf79dd32018-09-12 12:54:15 -070076 objServer.remove_interface(thresholdInterfaceWarning);
77 objServer.remove_interface(thresholdInterfaceCritical);
78 objServer.remove_interface(sensorInterface);
James Feist139cb572018-09-10 15:26:18 -070079}
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{
James Feistaf79dd32018-09-12 12:54:15 -0700155 thresholds::checkThresholds(this);
James Feist139cb572018-09-10 15:26:18 -0700156}
157
158void ADCSensor::update_value(const double &new_value)
159{
James Feistaf79dd32018-09-12 12:54:15 -0700160 bool ret = sensorInterface->set_property("Value", new_value);
James Feist139cb572018-09-10 15:26:18 -0700161 value = new_value;
162 check_thresholds();
163}
164
James Feist139cb572018-09-10 15:26:18 -0700165void ADCSensor::set_initial_properties(
166 std::shared_ptr<sdbusplus::asio::connection> &conn)
167{
168 // todo, get max and min from configuration
James Feistaf79dd32018-09-12 12:54:15 -0700169 sensorInterface->register_property("MaxValue", max_value);
170 sensorInterface->register_property("MinValue", min_value);
171 sensorInterface->register_property("Value", value);
James Feist139cb572018-09-10 15:26:18 -0700172
173 for (auto &threshold : thresholds)
174 {
175 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
176 std::string level;
177 std::string alarm;
178 if (threshold.level == thresholds::Level::CRITICAL)
179 {
James Feistaf79dd32018-09-12 12:54:15 -0700180 iface = thresholdInterfaceCritical;
James Feist139cb572018-09-10 15:26:18 -0700181 if (threshold.direction == thresholds::Direction::HIGH)
182 {
183 level = "CriticalHigh";
184 alarm = "CriticalAlarmHigh";
185 }
186 else
187 {
188 level = "CriticalLow";
189 alarm = "CriticalAlarmLow";
190 }
191 }
192 else if (threshold.level == thresholds::Level::WARNING)
193 {
James Feistaf79dd32018-09-12 12:54:15 -0700194 iface = thresholdInterfaceWarning;
James Feist139cb572018-09-10 15:26:18 -0700195 if (threshold.direction == thresholds::Direction::HIGH)
196 {
197 level = "WarningHigh";
198 alarm = "WarningAlarmHigh";
199 }
200 else
201 {
202 level = "WarningLow";
203 alarm = "WarningAlarmLow";
204 }
205 }
206 else
207 {
208 std::cerr << "Unknown threshold level" << threshold.level << "\n";
209 continue;
210 }
211 if (!iface)
212 {
213 std::cout << "trying to set uninitialized interface\n";
214 continue;
215 }
216 iface->register_property(
217 level, threshold.value,
218 [&](const double &request, double &oldValue) {
219 oldValue = request; // todo, just let the config do this?
220 threshold.value = request;
221 thresholds::persistThreshold(
222 configuration, "xyz.openbmc_project.Configuration.ADC",
223 threshold, conn);
224 return 1;
225 });
226 iface->register_property(alarm, false);
227 }
James Feistaf79dd32018-09-12 12:54:15 -0700228 if (!sensorInterface->initialize())
James Feist139cb572018-09-10 15:26:18 -0700229 {
230 std::cerr << "error initializing value interface\n";
231 }
James Feistaf79dd32018-09-12 12:54:15 -0700232 if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize())
James Feist139cb572018-09-10 15:26:18 -0700233 {
234 std::cerr << "error initializing warning threshold interface\n";
235 }
236
James Feistaf79dd32018-09-12 12:54:15 -0700237 if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize())
James Feist139cb572018-09-10 15:26:18 -0700238 {
239 std::cerr << "error initializing critical threshold interface\n";
240 }
241}