blob: 4a6165547a50147e547cee865c9c61402397abf2 [file] [log] [blame]
James Feist6714a252018-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 Yoo9ced0a32018-10-25 10:42:39 -070029static constexpr unsigned int sensorPollMs = 500;
30static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070031
32// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070033static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070034
James Feistcc6d4fe2018-11-14 16:38:26 -080035static constexpr double roundFactor = 10000; // 3 decimal places
James Feistce3fca42018-11-21 12:58:24 -080036static constexpr double maxReading = 20;
37static constexpr double minReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080038
James Feist6714a252018-09-10 15:26:18 -070039ADCSensor::ADCSensor(const std::string &path,
40 sdbusplus::asio::object_server &objectServer,
41 std::shared_ptr<sdbusplus::asio::connection> &conn,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042 boost::asio::io_service &io, const std::string &sensorName,
James Feist6714a252018-09-10 15:26:18 -070043 std::vector<thresholds::Threshold> &&_thresholds,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070044 const double scaleFactor,
James Feist6714a252018-09-10 15:26:18 -070045 const std::string &sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -070046 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
James Feistce3fca42018-11-21 12:58:24 -080047 std::move(_thresholds), sensorConfiguration,
48 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
49 objServer(objectServer), scaleFactor(scaleFactor),
50 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070051{
James Feist251c7822018-09-12 12:54:15 -070052 sensorInterface = objectServer.add_interface(
53 "/xyz/openbmc_project/sensors/voltage/" + name,
54 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070055 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070056 {
James Feist251c7822018-09-12 12:54:15 -070057 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070058 "/xyz/openbmc_project/sensors/voltage/" + name,
59 "xyz.openbmc_project.Sensor.Threshold.Warning");
60 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070061 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070062 {
James Feist251c7822018-09-12 12:54:15 -070063 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070064 "/xyz/openbmc_project/sensors/voltage/" + name,
65 "xyz.openbmc_project.Sensor.Threshold.Critical");
66 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070067 setInitialProperties(conn);
68 setupRead();
James Feist6714a252018-09-10 15:26:18 -070069}
70
71ADCSensor::~ADCSensor()
72{
73 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074 inputDev.close();
75 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070076 objServer.remove_interface(thresholdInterfaceWarning);
77 objServer.remove_interface(thresholdInterfaceCritical);
78 objServer.remove_interface(sensorInterface);
James Feist6714a252018-09-10 15:26:18 -070079}
80
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070081void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070082{
83 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070084 inputDev, readBuf, '\n',
James Feist6714a252018-09-10 15:26:18 -070085 [&](const boost::system::error_code &ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070087}
88
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089void ADCSensor::handleResponse(const boost::system::error_code &err)
James Feist6714a252018-09-10 15:26:18 -070090{
91 if (err == boost::system::errc::bad_file_descriptor)
92 {
93 return; // we're being destroyed
94 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -070096
97 if (!err)
98 {
99 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700101
102 // todo read scaling factors from configuration
103 try
104 {
James Feistcc6d4fe2018-11-14 16:38:26 -0800105 double nvalue = std::stof(response);
106
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700107 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800108 nvalue = std::round(nvalue * roundFactor) / roundFactor;
109
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +0530110 if (overridenState)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530111 {
112 nvalue = overriddenValue;
113 }
James Feistcc6d4fe2018-11-14 16:38:26 -0800114
James Feist6714a252018-09-10 15:26:18 -0700115 if (nvalue != value)
116 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700118 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700120 }
121 catch (std::invalid_argument)
122 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700123 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700124 }
125 }
126 else
127 {
James Feist6714a252018-09-10 15:26:18 -0700128
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700129 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700130 }
James Feistdc6c55f2018-10-31 12:53:20 -0700131 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700132 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700133 {
James Feistdc6c55f2018-10-31 12:53:20 -0700134 std::cerr << "Failure to read sensor " << name << " at " << path
135 << " ec:" << err << "\n";
136 }
137
138 if (errCount >= warnAfterErrorCount)
139 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700140 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700141 }
142
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700143 responseStream.clear();
144 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700145 int fd = open(path.c_str(), O_RDONLY);
146 if (fd <= 0)
147 {
148 return; // we're no longer valid
149 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700150 inputDev.assign(fd);
151 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
152 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist6714a252018-09-10 15:26:18 -0700153 if (ec == boost::asio::error::operation_aborted)
154 {
155 return; // we're being canceled
156 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700157 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700158 });
159}
160
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700161void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700162{
James Feist251c7822018-09-12 12:54:15 -0700163 thresholds::checkThresholds(this);
James Feist6714a252018-09-10 15:26:18 -0700164}