blob: af63805b5cec38e635a557d5920bc54ecb7b6289 [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <PSUSensor.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080020#include <boost/algorithm/string/predicate.hpp>
Ed Tanous16966b52021-09-15 15:06:59 -070021#include <boost/asio/random_access_file.hpp>
James Feist8086aba2020-08-25 16:00:59 -070022#include <boost/asio/read_until.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26
Cheng C Yang209ec562019-03-12 16:37:44 +080027#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <istream>
Cheng C Yang209ec562019-03-12 16:37:44 +080029#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080031#include <string>
Paul Fertser34533542021-07-20 08:29:09 +000032#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080034
James Feist690895f2019-04-23 13:01:08 -070035static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
36
Ed Tanous8a57ec02020-10-09 12:46:52 -070037static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070038
Cheng C Yang209ec562019-03-12 16:37:44 +080039PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
40 sdbusplus::asio::object_server& objectServer,
41 std::shared_ptr<sdbusplus::asio::connection>& conn,
42 boost::asio::io_service& io, const std::string& sensorName,
Ed Tanous8a57ec02020-10-09 12:46:52 -070043 std::vector<thresholds::Threshold>&& thresholdsIn,
Cheng C Yang209ec562019-03-12 16:37:44 +080044 const std::string& sensorConfiguration,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000045 const PowerState& powerState,
Zev Weiss6b6891c2021-04-22 02:46:21 -050046 const std::string& sensorUnits, unsigned int factor,
Jeff Line41d52f2021-04-07 19:38:51 +080047 double max, double min, double offset,
48 const std::string& label, size_t tSize, double pollRate) :
Zhikui Renda98f092021-11-01 09:41:08 -070049 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
50 objectType, false, false, max, min, conn, powerState),
Ed Tanous16966b52021-09-15 15:06:59 -070051 objServer(objectServer),
52 inputDev(io, path, boost::asio::random_access_file::read_only),
53 waitTimer(io), path(path), sensorFactor(factor), sensorOffset(offset),
54 thresholdTimer(io)
Cheng C Yang209ec562019-03-12 16:37:44 +080055{
Ed Tanous16966b52021-09-15 15:06:59 -070056 buffer = std::make_shared<std::array<char, 128>>();
Zev Weiss6b6891c2021-04-22 02:46:21 -050057 std::string unitPath = sensor_paths::getPathForUnits(sensorUnits);
Ed Tanous8a57ec02020-10-09 12:46:52 -070058 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -070059 {
60 std::cerr << "Constructed sensor: path " << path << " type "
61 << objectType << " config " << sensorConfiguration
Zev Weiss6b6891c2021-04-22 02:46:21 -050062 << " typename " << unitPath << " factor " << factor << " min "
Jeff Line41d52f2021-04-07 19:38:51 +080063 << min << " max " << max << " offset " << offset << " name \""
64 << sensorName << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -070065 }
Lei YU7170a232021-02-04 16:19:27 +080066 if (pollRate > 0.0)
67 {
68 sensorPollMs = static_cast<unsigned int>(pollRate * 1000);
69 }
Josh Lehan49cfba92019-10-08 16:50:42 -070070
Zev Weiss6b6891c2021-04-22 02:46:21 -050071 std::string dbusPath = sensorPathPrefix + unitPath + "/" + name;
James Feist690895f2019-04-23 13:01:08 -070072
Cheng C Yang209ec562019-03-12 16:37:44 +080073 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070074 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080075
Jayashree Dhanapal56678082022-01-04 17:27:20 +053076 for (const auto& threshold : thresholds)
Cheng C Yang209ec562019-03-12 16:37:44 +080077 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053078 std::string interface = thresholds::getInterface(threshold.level);
79 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
80 objectServer.add_interface(dbusPath, interface);
Cheng C Yang209ec562019-03-12 16:37:44 +080081 }
James Feist690895f2019-04-23 13:01:08 -070082
AppaRao Pulic82213c2020-02-27 01:24:58 +053083 // This should be called before initializing association.
84 // createInventoryAssoc() does add more associations before doing
85 // register and initialize "Associations" property.
Ed Tanous8a57ec02020-10-09 12:46:52 -070086 if (label.empty() || tSize == thresholds.size())
Cheng C Yang6b1247a2020-03-09 23:48:39 +080087 {
Andrei Kartashev39287412022-02-04 16:04:47 +030088 setInitialProperties(sensorUnits);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080089 }
90 else
91 {
Andrei Kartashev39287412022-02-04 16:04:47 +030092 setInitialProperties(sensorUnits, label, tSize);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080093 }
Patrick Venture43162182019-10-23 10:44:53 -070094
AppaRao Pulic82213c2020-02-27 01:24:58 +053095 association = objectServer.add_interface(dbusPath, association::interface);
96
Cheng C Yang5580f2f2019-09-19 09:01:47 +080097 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080098}
99
100PSUSensor::~PSUSensor()
101{
Cheng C Yang209ec562019-03-12 16:37:44 +0800102 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +0800103 inputDev.close();
Cheng C Yang209ec562019-03-12 16:37:44 +0800104 objServer.remove_interface(sensorInterface);
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530105 for (const auto& iface : thresholdInterfaces)
106 {
107 objServer.remove_interface(iface);
108 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530109 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800110}
111
112void PSUSensor::setupRead(void)
113{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000114 if (!readingStateGood())
115 {
116 markAvailable(false);
117 updateValue(std::numeric_limits<double>::quiet_NaN());
118 restartRead();
119 return;
120 }
121
Ed Tanous16966b52021-09-15 15:06:59 -0700122 if (buffer == nullptr)
123 {
124 std::cerr << "Buffer was invalid?";
125 return;
126 }
127
128 std::weak_ptr<PSUSensor> weak = weak_from_this();
129 // Note, we are building a asio buffer that is one char smaller than
130 // the actual data structure, so that we can always append the null
131 // terminator. This can go away once std::from_chars<double> is available
132 // in the standard
133 inputDev.async_read_some_at(
134 0, boost::asio::buffer(buffer->data(), buffer->size() - 1),
135 [weak, buffer{buffer}](const boost::system::error_code& ec,
136 size_t bytesRead) {
137 std::shared_ptr<PSUSensor> self = weak.lock();
138 if (!self)
Ed Tanousbb679322022-05-16 16:10:00 -0700139 {
Ed Tanous16966b52021-09-15 15:06:59 -0700140 return;
Ed Tanousbb679322022-05-16 16:10:00 -0700141 }
Ed Tanous16966b52021-09-15 15:06:59 -0700142
143 self->handleResponse(ec, bytesRead);
144 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800145}
146
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000147void PSUSensor::restartRead(void)
148{
149 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
150 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
151 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
152 if (ec == boost::asio::error::operation_aborted)
153 {
154 std::cerr << "Failed to reschedule\n";
155 return;
156 }
157 std::shared_ptr<PSUSensor> self = weakRef.lock();
158 if (self)
159 {
160 self->setupRead();
161 }
162 });
163}
164
Kuiying Wangbcf76712021-03-19 11:17:58 +0800165// Create a buffer expected to be able to hold more characters than will be
166// present in the input file.
Ed Tanous16966b52021-09-15 15:06:59 -0700167void PSUSensor::handleResponse(const boost::system::error_code& err,
168 size_t bytesRead)
Cheng C Yang209ec562019-03-12 16:37:44 +0800169{
Ed Tanous16966b52021-09-15 15:06:59 -0700170 if (err == boost::asio::error::operation_aborted)
171 {
172 std::cerr << "Read aborted\n";
173 return;
174 }
Yong Libf8b1da2020-04-15 16:32:50 +0800175 if ((err == boost::system::errc::bad_file_descriptor) ||
176 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800177 {
Paul Fertser34533542021-07-20 08:29:09 +0000178 std::cerr << "Bad file descriptor for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800179 return;
180 }
Ed Tanous16966b52021-09-15 15:06:59 -0700181 // null terminate the string so we don't walk off the end
182 std::array<char, 128>& bufferRef = *buffer;
183 bufferRef[bytesRead] = '\0';
Kuiying Wangbcf76712021-03-19 11:17:58 +0800184
Ed Tanous16966b52021-09-15 15:06:59 -0700185 try
Cheng C Yang209ec562019-03-12 16:37:44 +0800186 {
Ed Tanous16966b52021-09-15 15:06:59 -0700187 rawValue = std::stod(bufferRef.data());
188 updateValue((rawValue / sensorFactor) + sensorOffset);
Cheng C Yang209ec562019-03-12 16:37:44 +0800189 }
Ed Tanous16966b52021-09-15 15:06:59 -0700190 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800191 {
Ed Tanous16966b52021-09-15 15:06:59 -0700192 std::cerr << "Could not parse input from " << path << "\n";
James Feist961bf092020-07-01 16:38:12 -0700193 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800194 }
195
Cheng C Yanga97f1342020-02-11 15:10:41 +0800196 lseek(fd, 0, SEEK_SET);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000197 restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +0800198}
199
200void PSUSensor::checkThresholds(void)
201{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000202 if (!readingStateGood())
203 {
204 return;
205 }
206
207 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
Cheng C Yang209ec562019-03-12 16:37:44 +0800208}