blob: 4398f61387a1f0ea1e404ceb37067f07607b6e2e [file] [log] [blame]
James Feist139cb572018-09-10 15:26:18 -07001/*
2// Copyright (c) 2018 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 <TachSensor.hpp>
20#include <Utils.hpp>
21#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/date_time/posix_time/posix_time.hpp>
James Feist7c391352018-10-26 14:09:45 -070024#include <fstream>
James Feist139cb572018-09-10 15:26:18 -070025#include <iostream>
26#include <limits>
27#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29#include <string>
30
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070031static constexpr unsigned int pwmPollMs = 500;
32static constexpr size_t warnAfterErrorCount = 10;
James Feist139cb572018-09-10 15:26:18 -070033
34TachSensor::TachSensor(const std::string &path,
35 sdbusplus::asio::object_server &objectServer,
36 std::shared_ptr<sdbusplus::asio::connection> &conn,
James Feist7c391352018-10-26 14:09:45 -070037 std::unique_ptr<PresenceSensor> &&presence,
James Feist139cb572018-09-10 15:26:18 -070038 boost::asio::io_service &io, const std::string &fanName,
39 std::vector<thresholds::Threshold> &&_thresholds,
40 const std::string &sensorConfiguration) :
James Feistaf79dd32018-09-12 12:54:15 -070041 Sensor(),
42 path(path), objServer(objectServer), dbusConnection(conn),
James Feist7c391352018-10-26 14:09:45 -070043 presence(std::move(presence)),
James Feist139cb572018-09-10 15:26:18 -070044 name(boost::replace_all_copy(fanName, " ", "_")),
James Feistaf79dd32018-09-12 12:54:15 -070045 configuration(sensorConfiguration),
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070046 inputDev(io, open(path.c_str(), O_RDONLY)), 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(25000), minValue(0)
James Feist139cb572018-09-10 15:26:18 -070049{
James Feistaf79dd32018-09-12 12:54:15 -070050 thresholds = std::move(_thresholds);
51 sensorInterface = objectServer.add_interface(
52 "/xyz/openbmc_project/sensors/fan_tach/" + name,
53 "xyz.openbmc_project.Sensor.Value");
54
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070055 if (thresholds::hasWarningInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070056 {
James Feistaf79dd32018-09-12 12:54:15 -070057 thresholdInterfaceWarning = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070058 "/xyz/openbmc_project/sensors/fan_tach/" + name,
59 "xyz.openbmc_project.Sensor.Threshold.Warning");
60 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070061 if (thresholds::hasCriticalInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070062 {
James Feistaf79dd32018-09-12 12:54:15 -070063 thresholdInterfaceCritical = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070064 "/xyz/openbmc_project/sensors/fan_tach/" + name,
65 "xyz.openbmc_project.Sensor.Threshold.Critical");
66 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070067 setInitialProperties(conn);
James Feist139cb572018-09-10 15:26:18 -070068 isPowerOn(dbusConnection); // first call initializes
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070069 setupRead();
James Feist139cb572018-09-10 15:26:18 -070070}
71
72TachSensor::~TachSensor()
73{
74 // close the input dev to cancel async operations
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070075 inputDev.close();
76 waitTimer.cancel();
James Feistaf79dd32018-09-12 12:54:15 -070077 objServer.remove_interface(thresholdInterfaceWarning);
78 objServer.remove_interface(thresholdInterfaceCritical);
79 objServer.remove_interface(sensorInterface);
James Feist139cb572018-09-10 15:26:18 -070080}
81
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070082void TachSensor::setupRead(void)
James Feist139cb572018-09-10 15:26:18 -070083{
84 boost::asio::async_read_until(
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070085 inputDev, readBuf, '\n',
James Feist139cb572018-09-10 15:26:18 -070086 [&](const boost::system::error_code &ec,
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070087 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist139cb572018-09-10 15:26:18 -070088}
89
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070090void TachSensor::handleResponse(const boost::system::error_code &err)
James Feist139cb572018-09-10 15:26:18 -070091{
92 if (err == boost::system::errc::bad_file_descriptor)
93 {
94 return; // we're being destroyed
95 }
James Feist7c391352018-10-26 14:09:45 -070096 bool missing = false;
James Feist3452f0e2018-10-31 10:08:47 -070097 size_t pollTime = pwmPollMs;
James Feist7c391352018-10-26 14:09:45 -070098 if (presence)
99 {
100 if (!presence->getValue())
101 {
James Feist3452f0e2018-10-31 10:08:47 -0700102 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7c391352018-10-26 14:09:45 -0700103 missing = true;
James Feist3452f0e2018-10-31 10:08:47 -0700104 pollTime = sensorFailedPollTimeMs;
James Feist7c391352018-10-26 14:09:45 -0700105 }
106 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700107 std::istream responseStream(&readBuf);
James Feist7c391352018-10-26 14:09:45 -0700108 if (!missing)
James Feist139cb572018-09-10 15:26:18 -0700109 {
James Feist7c391352018-10-26 14:09:45 -0700110 if (!err)
James Feist139cb572018-09-10 15:26:18 -0700111 {
James Feist7c391352018-10-26 14:09:45 -0700112 std::string response;
113 try
James Feist139cb572018-09-10 15:26:18 -0700114 {
James Feist7c391352018-10-26 14:09:45 -0700115 std::getline(responseStream, response);
116 float nvalue = std::stof(response);
117 responseStream.clear();
118 if (nvalue != value)
119 {
120 updateValue(nvalue);
121 }
122 errCount = 0;
James Feist139cb572018-09-10 15:26:18 -0700123 }
James Feist7c391352018-10-26 14:09:45 -0700124 catch (const std::invalid_argument &)
125 {
126 errCount++;
127 }
James Feist139cb572018-09-10 15:26:18 -0700128 }
129 else
130 {
James Feist3452f0e2018-10-31 10:08:47 -0700131 pollTime = sensorFailedPollTimeMs;
James Feist7c391352018-10-26 14:09:45 -0700132 errCount++;
133 }
134 // only send value update once
135 if (errCount == warnAfterErrorCount)
136 {
137 // only an error if power is on
138 if (isPowerOn(dbusConnection))
139 {
140 std::cerr << "Failure to read sensor " << name << " at " << path
141 << "\n";
142 updateValue(0);
143 }
144 else
145 {
146 errCount = 0; // check power again in 10 cycles
James Feist3452f0e2018-10-31 10:08:47 -0700147 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7c391352018-10-26 14:09:45 -0700148 }
James Feist139cb572018-09-10 15:26:18 -0700149 }
150 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700151 responseStream.clear();
152 inputDev.close();
James Feist139cb572018-09-10 15:26:18 -0700153 int fd = open(path.c_str(), O_RDONLY);
154 if (fd <= 0)
155 {
156 return; // we're no longer valid
157 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700158 inputDev.assign(fd);
James Feist7c391352018-10-26 14:09:45 -0700159 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700160 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist139cb572018-09-10 15:26:18 -0700161 if (ec == boost::asio::error::operation_aborted)
162 {
163 return; // we're being canceled
164 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700165 setupRead();
James Feist139cb572018-09-10 15:26:18 -0700166 });
167}
168
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700169void TachSensor::checkThresholds(void)
James Feist139cb572018-09-10 15:26:18 -0700170{
James Feistaf79dd32018-09-12 12:54:15 -0700171 thresholds::checkThresholds(this);
James Feist139cb572018-09-10 15:26:18 -0700172}
173
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700174void TachSensor::updateValue(const double &newValue)
James Feist139cb572018-09-10 15:26:18 -0700175{
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700176 sensorInterface->set_property("Value", newValue);
177 value = newValue;
178 checkThresholds();
James Feist139cb572018-09-10 15:26:18 -0700179}
180
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700181void TachSensor::setInitialProperties(
James Feist139cb572018-09-10 15:26:18 -0700182 std::shared_ptr<sdbusplus::asio::connection> &conn)
183{
184 // todo, get max and min from configuration
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700185 sensorInterface->register_property("MaxValue", maxValue);
186 sensorInterface->register_property("MinValue", minValue);
James Feistaf79dd32018-09-12 12:54:15 -0700187 sensorInterface->register_property("Value", value);
James Feist139cb572018-09-10 15:26:18 -0700188
189 for (auto &threshold : thresholds)
190 {
191 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
192 std::string level;
193 std::string alarm;
194 if (threshold.level == thresholds::Level::CRITICAL)
195 {
James Feistaf79dd32018-09-12 12:54:15 -0700196 iface = thresholdInterfaceCritical;
James Feist139cb572018-09-10 15:26:18 -0700197 if (threshold.direction == thresholds::Direction::HIGH)
198 {
199 level = "CriticalHigh";
200 alarm = "CriticalAlarmHigh";
201 }
202 else
203 {
204 level = "CriticalLow";
205 alarm = "CriticalAlarmLow";
206 }
207 }
208 else if (threshold.level == thresholds::Level::WARNING)
209 {
James Feistaf79dd32018-09-12 12:54:15 -0700210 iface = thresholdInterfaceWarning;
James Feist139cb572018-09-10 15:26:18 -0700211 if (threshold.direction == thresholds::Direction::HIGH)
212 {
213 level = "WarningHigh";
214 alarm = "WarningAlarmHigh";
215 }
216 else
217 {
218 level = "WarningLow";
219 alarm = "WarningAlarmLow";
220 }
221 }
222 else
223 {
224 std::cerr << "Unknown threshold level" << threshold.level << "\n";
225 continue;
226 }
227 if (!iface)
228 {
229 std::cout << "trying to set uninitialized interface\n";
230 continue;
231 }
232 iface->register_property(
233 level, threshold.value,
234 [&](const double &request, double &oldValue) {
235 oldValue = request; // todo, just let the config do this?
236 threshold.value = request;
237 thresholds::persistThreshold(
238 configuration,
239 "xyz.openbmc_project.Configuration.AspeedFan", threshold,
240 conn);
241 return 1;
242 });
243 iface->register_property(alarm, false);
244 }
James Feistaf79dd32018-09-12 12:54:15 -0700245 if (!sensorInterface->initialize())
James Feist139cb572018-09-10 15:26:18 -0700246 {
247 std::cerr << "error initializing value interface\n";
248 }
James Feistaf79dd32018-09-12 12:54:15 -0700249 if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize())
James Feist139cb572018-09-10 15:26:18 -0700250 {
251 std::cerr << "error initializing warning threshold interface\n";
252 }
253
James Feistaf79dd32018-09-12 12:54:15 -0700254 if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize())
James Feist139cb572018-09-10 15:26:18 -0700255 {
256 std::cerr << "error initializing critical threshold interface\n";
257 }
258}
James Feist7c391352018-10-26 14:09:45 -0700259
260PresenceSensor::PresenceSensor(const size_t index, bool inverted,
261 boost::asio::io_service &io) :
262 inverted(inverted),
263 inputDev(io)
264{
265 // todo: use gpiodaemon
266 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
267 fd = open((device + "/value").c_str(), O_RDONLY);
268 if (fd < 0)
269 {
270 std::cerr << "Error opening gpio " << index << "\n";
271 return;
272 }
273
274 std::ofstream deviceFile(device + "/edge");
275 if (!deviceFile.good())
276 {
277 std::cerr << "Error setting edge " << device << "\n";
278 return;
279 }
280 deviceFile << "both";
281 deviceFile.close();
282
283 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
284 monitorPresence();
285 read();
286}
287
288PresenceSensor::~PresenceSensor()
289{
290 inputDev.close();
291 close(fd);
292}
293
294void PresenceSensor::monitorPresence(void)
295{
296 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
297 [this](const boost::system::error_code &ec) {
298 if (ec == boost::system::errc::bad_file_descriptor)
299 {
300 return; // we're being destroyed
301 }
302 else if (ec)
303 {
304 std::cerr
305 << "Error on presence sensor socket\n";
306 }
307 else
308 {
309 read();
310 }
311 monitorPresence();
312 });
313}
314
315void PresenceSensor::read(void)
316{
317 constexpr size_t readSize = sizeof("0");
318 std::string readBuf;
319 readBuf.resize(readSize);
320 lseek(fd, 0, SEEK_SET);
321 size_t r = ::read(fd, readBuf.data(), readSize);
322 if (r != 1)
323 {
324 std::cerr << "Error reading gpio\n";
325 }
326 else
327 {
328 bool value = std::stoi(readBuf);
329 if (inverted)
330 {
331 value = !value;
332 }
333 status = value;
334 }
335}
336
337bool PresenceSensor::getValue(void)
338{
339 return status;
340}