blob: 09c62cf29a15d238edec1c79ccdaa901d2d2ce27 [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
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070029static constexpr unsigned int sensorPollMs = 500;
30static constexpr size_t warnAfterErrorCount = 10;
James Feist139cb572018-09-10 15:26:18 -070031
32// scaling factor from hwmon
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070033static constexpr unsigned int sensorScaleFactor = 1000;
James Feist139cb572018-09-10 15:26:18 -070034
35ADCSensor::ADCSensor(const std::string &path,
36 sdbusplus::asio::object_server &objectServer,
37 std::shared_ptr<sdbusplus::asio::connection> &conn,
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070038 boost::asio::io_service &io, const std::string &sensorName,
James Feist139cb572018-09-10 15:26:18 -070039 std::vector<thresholds::Threshold> &&_thresholds,
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070040 const double scaleFactor,
James Feist139cb572018-09-10 15:26:18 -070041 const std::string &sensorConfiguration) :
James Feist39132a62018-10-31 12:53:20 -070042 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
43 std::move(_thresholds)),
44 objServer(objectServer), configuration(sensorConfiguration),
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070045 scaleFactor(scaleFactor), inputDev(io, open(path.c_str(), O_RDONLY)),
46 waitTimer(io), errCount(0),
James Feist139cb572018-09-10 15:26:18 -070047 // todo, get these from config
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070048 maxValue(20), minValue(0)
James Feist139cb572018-09-10 15:26:18 -070049{
James Feistaf79dd32018-09-12 12:54:15 -070050 sensorInterface = objectServer.add_interface(
51 "/xyz/openbmc_project/sensors/voltage/" + name,
52 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070053 if (thresholds::hasWarningInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070054 {
James Feistaf79dd32018-09-12 12:54:15 -070055 thresholdInterfaceWarning = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070056 "/xyz/openbmc_project/sensors/voltage/" + name,
57 "xyz.openbmc_project.Sensor.Threshold.Warning");
58 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070059 if (thresholds::hasCriticalInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070060 {
James Feistaf79dd32018-09-12 12:54:15 -070061 thresholdInterfaceCritical = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070062 "/xyz/openbmc_project/sensors/voltage/" + name,
63 "xyz.openbmc_project.Sensor.Threshold.Critical");
64 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070065 setInitialProperties(conn);
66 setupRead();
James Feist139cb572018-09-10 15:26:18 -070067}
68
69ADCSensor::~ADCSensor()
70{
71 // close the input dev to cancel async operations
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070072 inputDev.close();
73 waitTimer.cancel();
James Feistaf79dd32018-09-12 12:54:15 -070074 objServer.remove_interface(thresholdInterfaceWarning);
75 objServer.remove_interface(thresholdInterfaceCritical);
76 objServer.remove_interface(sensorInterface);
James Feist139cb572018-09-10 15:26:18 -070077}
78
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070079void ADCSensor::setupRead(void)
James Feist139cb572018-09-10 15:26:18 -070080{
81 boost::asio::async_read_until(
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070082 inputDev, readBuf, '\n',
James Feist139cb572018-09-10 15:26:18 -070083 [&](const boost::system::error_code &ec,
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070084 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist139cb572018-09-10 15:26:18 -070085}
86
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070087void ADCSensor::handleResponse(const boost::system::error_code &err)
James Feist139cb572018-09-10 15:26:18 -070088{
89 if (err == boost::system::errc::bad_file_descriptor)
90 {
91 return; // we're being destroyed
92 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070093 std::istream responseStream(&readBuf);
James Feist139cb572018-09-10 15:26:18 -070094
95 if (!err)
96 {
97 std::string response;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070098 std::getline(responseStream, response);
James Feist139cb572018-09-10 15:26:18 -070099
100 // todo read scaling factors from configuration
101 try
102 {
103 float nvalue = std::stof(response);
104
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700105 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feist139cb572018-09-10 15:26:18 -0700106
107 if (nvalue != value)
108 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700109 updateValue(nvalue);
James Feist139cb572018-09-10 15:26:18 -0700110 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700111 errCount = 0;
James Feist139cb572018-09-10 15:26:18 -0700112 }
113 catch (std::invalid_argument)
114 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700115 errCount++;
James Feist139cb572018-09-10 15:26:18 -0700116 }
117 }
118 else
119 {
James Feist139cb572018-09-10 15:26:18 -0700120
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700121 errCount++;
James Feist139cb572018-09-10 15:26:18 -0700122 }
James Feist39132a62018-10-31 12:53:20 -0700123 // only print once
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700124 if (errCount == warnAfterErrorCount)
James Feist139cb572018-09-10 15:26:18 -0700125 {
James Feist39132a62018-10-31 12:53:20 -0700126 std::cerr << "Failure to read sensor " << name << " at " << path
127 << " ec:" << err << "\n";
128 }
129
130 if (errCount >= warnAfterErrorCount)
131 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700132 updateValue(0);
James Feist139cb572018-09-10 15:26:18 -0700133 }
134
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700135 responseStream.clear();
136 inputDev.close();
James Feist139cb572018-09-10 15:26:18 -0700137 int fd = open(path.c_str(), O_RDONLY);
138 if (fd <= 0)
139 {
140 return; // we're no longer valid
141 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700142 inputDev.assign(fd);
143 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
144 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist139cb572018-09-10 15:26:18 -0700145 if (ec == boost::asio::error::operation_aborted)
146 {
147 return; // we're being canceled
148 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700149 setupRead();
James Feist139cb572018-09-10 15:26:18 -0700150 });
151}
152
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700153void ADCSensor::checkThresholds(void)
James Feist139cb572018-09-10 15:26:18 -0700154{
James Feistaf79dd32018-09-12 12:54:15 -0700155 thresholds::checkThresholds(this);
James Feist139cb572018-09-10 15:26:18 -0700156}
157
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700158void ADCSensor::updateValue(const double &newValue)
James Feist139cb572018-09-10 15:26:18 -0700159{
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700160 bool ret = sensorInterface->set_property("Value", newValue);
161 value = newValue;
162 checkThresholds();
James Feist139cb572018-09-10 15:26:18 -0700163}
164
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700165void ADCSensor::setInitialProperties(
James Feist139cb572018-09-10 15:26:18 -0700166 std::shared_ptr<sdbusplus::asio::connection> &conn)
167{
168 // todo, get max and min from configuration
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700169 sensorInterface->register_property("MaxValue", maxValue);
170 sensorInterface->register_property("MinValue", minValue);
James Feistaf79dd32018-09-12 12:54:15 -0700171 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}