blob: 015a367e54028004c3ff44c2a00a32e692f504b1 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001/*
2// Copyright (c) 2019 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 <PSUSensor.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
James Feist690895f2019-04-23 13:01:08 -070029static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
30
Cheng C Yang209ec562019-03-12 16:37:44 +080031PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
32 sdbusplus::asio::object_server& objectServer,
33 std::shared_ptr<sdbusplus::asio::connection>& conn,
34 boost::asio::io_service& io, const std::string& sensorName,
35 std::vector<thresholds::Threshold>&& _thresholds,
36 const std::string& sensorConfiguration,
37 std::string& sensorTypeName, unsigned int factor,
38 double max, double min) :
James Feist930fcde2019-05-28 12:58:43 -070039 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Cheng C Yang209ec562019-03-12 16:37:44 +080040 std::move(_thresholds), sensorConfiguration, objectType, max, min),
James Feist930fcde2019-05-28 12:58:43 -070041 path(path), objServer(objectServer),
42 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0),
43 sensorFactor(factor)
Cheng C Yang209ec562019-03-12 16:37:44 +080044{
James Feist690895f2019-04-23 13:01:08 -070045 std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
46
Cheng C Yang209ec562019-03-12 16:37:44 +080047 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070048 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080049
50 if (thresholds::hasWarningInterface(thresholds))
51 {
52 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070053 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080054 }
55 if (thresholds::hasCriticalInterface(thresholds))
56 {
57 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070058 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080059 }
Cheng C Yang5580f2f2019-09-19 09:01:47 +080060 setInitialProperties(conn);
61
James Feist690895f2019-04-23 13:01:08 -070062 association =
63 objectServer.add_interface(dbusPath, "org.openbmc.Associations");
64
Cheng C Yang5580f2f2019-09-19 09:01:47 +080065 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080066 setupRead();
67}
68
69PSUSensor::~PSUSensor()
70{
71 inputDev.close();
72 waitTimer.cancel();
73 objServer.remove_interface(sensorInterface);
74 objServer.remove_interface(thresholdInterfaceWarning);
75 objServer.remove_interface(thresholdInterfaceCritical);
76}
77
78void PSUSensor::setupRead(void)
79{
80 boost::asio::async_read_until(
81 inputDev, readBuf, '\n',
82 [&](const boost::system::error_code& ec,
83 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
84}
85
86void PSUSensor::handleResponse(const boost::system::error_code& err)
87{
88 if (err == boost::system::errc::bad_file_descriptor)
89 {
90 return;
91 }
92 std::istream responseStream(&readBuf);
93 if (!err)
94 {
95 std::string response;
96 try
97 {
98 std::getline(responseStream, response);
99 float nvalue = std::stof(response);
100 responseStream.clear();
101 nvalue /= sensorFactor;
James Feistb6c0b912019-07-09 12:21:44 -0700102 if (static_cast<double>(nvalue) != value)
Cheng C Yang209ec562019-03-12 16:37:44 +0800103 {
104 updateValue(nvalue);
105 }
106 errCount = 0;
107 }
108 catch (const std::invalid_argument&)
109 {
110 errCount++;
111 }
112 }
113 else
114 {
115 errCount++;
116 }
117
118 if (errCount >= warnAfterErrorCount)
119 {
120 if (errCount == warnAfterErrorCount)
121 {
122 std::cerr << "Failure to read sensor " << name << " at " << path
123 << "\n";
124 }
125 updateValue(0);
126 errCount++;
127 }
128
129 responseStream.clear();
130 inputDev.close();
131 int fd = open(path.c_str(), O_RDONLY);
132 if (fd <= 0)
133 {
134 return;
135 }
136 inputDev.assign(fd);
137 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
138 waitTimer.async_wait([&](const boost::system::error_code& ec) {
139 if (ec == boost::asio::error::operation_aborted)
140 {
141 return;
142 }
143 setupRead();
144 });
145}
146
147void PSUSensor::checkThresholds(void)
148{
149 thresholds::checkThresholds(this);
150}