blob: 6e2934f1ec36042e6b15160fd1d9d67d2c788ad9 [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>
Ed Tanous16966b52021-09-15 15:06:59 -070020#include <boost/asio/random_access_file.hpp>
James Feist8086aba2020-08-25 16:00:59 -070021#include <boost/asio/read_until.hpp>
James Feist38fb5982020-05-28 10:09:54 -070022#include <sdbusplus/asio/connection.hpp>
23#include <sdbusplus/asio/object_server.hpp>
24
Cheng C Yang209ec562019-03-12 16:37:44 +080025#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070026#include <istream>
Cheng C Yang209ec562019-03-12 16:37:44 +080027#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080029#include <string>
Paul Fertser34533542021-07-20 08:29:09 +000030#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080032
James Feist690895f2019-04-23 13:01:08 -070033static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
34
Ed Tanous8a57ec02020-10-09 12:46:52 -070035static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070036
Cheng C Yang209ec562019-03-12 16:37:44 +080037PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
38 sdbusplus::asio::object_server& objectServer,
39 std::shared_ptr<sdbusplus::asio::connection>& conn,
40 boost::asio::io_service& io, const std::string& sensorName,
Ed Tanous8a57ec02020-10-09 12:46:52 -070041 std::vector<thresholds::Threshold>&& thresholdsIn,
Cheng C Yang209ec562019-03-12 16:37:44 +080042 const std::string& sensorConfiguration,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000043 const PowerState& powerState,
Zev Weiss6b6891c2021-04-22 02:46:21 -050044 const std::string& sensorUnits, unsigned int factor,
Jeff Line41d52f2021-04-07 19:38:51 +080045 double max, double min, double offset,
46 const std::string& label, size_t tSize, double pollRate) :
Zhikui Renda98f092021-11-01 09:41:08 -070047 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
48 objectType, false, false, max, min, conn, powerState),
Ed Tanous16966b52021-09-15 15:06:59 -070049 objServer(objectServer),
50 inputDev(io, path, boost::asio::random_access_file::read_only),
51 waitTimer(io), path(path), sensorFactor(factor), sensorOffset(offset),
52 thresholdTimer(io)
Cheng C Yang209ec562019-03-12 16:37:44 +080053{
Ed Tanous16966b52021-09-15 15:06:59 -070054 buffer = std::make_shared<std::array<char, 128>>();
Zev Weiss6b6891c2021-04-22 02:46:21 -050055 std::string unitPath = sensor_paths::getPathForUnits(sensorUnits);
Ed Tanous8a57ec02020-10-09 12:46:52 -070056 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -070057 {
58 std::cerr << "Constructed sensor: path " << path << " type "
59 << objectType << " config " << sensorConfiguration
Zev Weiss6b6891c2021-04-22 02:46:21 -050060 << " typename " << unitPath << " factor " << factor << " min "
Jeff Line41d52f2021-04-07 19:38:51 +080061 << min << " max " << max << " offset " << offset << " name \""
62 << sensorName << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -070063 }
Lei YU7170a232021-02-04 16:19:27 +080064 if (pollRate > 0.0)
65 {
66 sensorPollMs = static_cast<unsigned int>(pollRate * 1000);
67 }
Josh Lehan49cfba92019-10-08 16:50:42 -070068
Zev Weiss6b6891c2021-04-22 02:46:21 -050069 std::string dbusPath = sensorPathPrefix + unitPath + "/" + name;
James Feist690895f2019-04-23 13:01:08 -070070
Cheng C Yang209ec562019-03-12 16:37:44 +080071 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070072 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080073
Jayashree Dhanapal56678082022-01-04 17:27:20 +053074 for (const auto& threshold : thresholds)
Cheng C Yang209ec562019-03-12 16:37:44 +080075 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053076 std::string interface = thresholds::getInterface(threshold.level);
77 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
78 objectServer.add_interface(dbusPath, interface);
Cheng C Yang209ec562019-03-12 16:37:44 +080079 }
James Feist690895f2019-04-23 13:01:08 -070080
AppaRao Pulic82213c2020-02-27 01:24:58 +053081 // This should be called before initializing association.
82 // createInventoryAssoc() does add more associations before doing
83 // register and initialize "Associations" property.
Ed Tanous8a57ec02020-10-09 12:46:52 -070084 if (label.empty() || tSize == thresholds.size())
Cheng C Yang6b1247a2020-03-09 23:48:39 +080085 {
Andrei Kartashev39287412022-02-04 16:04:47 +030086 setInitialProperties(sensorUnits);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080087 }
88 else
89 {
Andrei Kartashev39287412022-02-04 16:04:47 +030090 setInitialProperties(sensorUnits, label, tSize);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080091 }
Patrick Venture43162182019-10-23 10:44:53 -070092
AppaRao Pulic82213c2020-02-27 01:24:58 +053093 association = objectServer.add_interface(dbusPath, association::interface);
94
Cheng C Yang5580f2f2019-09-19 09:01:47 +080095 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080096}
97
98PSUSensor::~PSUSensor()
99{
Cheng C Yang209ec562019-03-12 16:37:44 +0800100 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +0800101 inputDev.close();
Cheng C Yang209ec562019-03-12 16:37:44 +0800102 objServer.remove_interface(sensorInterface);
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530103 for (const auto& iface : thresholdInterfaces)
104 {
105 objServer.remove_interface(iface);
106 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530107 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800108}
109
110void PSUSensor::setupRead(void)
111{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000112 if (!readingStateGood())
113 {
114 markAvailable(false);
115 updateValue(std::numeric_limits<double>::quiet_NaN());
116 restartRead();
117 return;
118 }
119
Ed Tanous16966b52021-09-15 15:06:59 -0700120 if (buffer == nullptr)
121 {
122 std::cerr << "Buffer was invalid?";
123 return;
124 }
125
126 std::weak_ptr<PSUSensor> weak = weak_from_this();
127 // Note, we are building a asio buffer that is one char smaller than
128 // the actual data structure, so that we can always append the null
129 // terminator. This can go away once std::from_chars<double> is available
130 // in the standard
131 inputDev.async_read_some_at(
132 0, boost::asio::buffer(buffer->data(), buffer->size() - 1),
133 [weak, buffer{buffer}](const boost::system::error_code& ec,
134 size_t bytesRead) {
135 std::shared_ptr<PSUSensor> self = weak.lock();
136 if (!self)
Ed Tanousbb679322022-05-16 16:10:00 -0700137 {
Ed Tanous16966b52021-09-15 15:06:59 -0700138 return;
Ed Tanousbb679322022-05-16 16:10:00 -0700139 }
Ed Tanous16966b52021-09-15 15:06:59 -0700140
141 self->handleResponse(ec, bytesRead);
142 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800143}
144
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000145void PSUSensor::restartRead(void)
146{
147 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700148 waitTimer.expires_from_now(std::chrono::milliseconds(sensorPollMs));
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000149 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
150 if (ec == boost::asio::error::operation_aborted)
151 {
152 std::cerr << "Failed to reschedule\n";
153 return;
154 }
155 std::shared_ptr<PSUSensor> self = weakRef.lock();
156 if (self)
157 {
158 self->setupRead();
159 }
160 });
161}
162
Kuiying Wangbcf76712021-03-19 11:17:58 +0800163// Create a buffer expected to be able to hold more characters than will be
164// present in the input file.
Ed Tanous16966b52021-09-15 15:06:59 -0700165void PSUSensor::handleResponse(const boost::system::error_code& err,
166 size_t bytesRead)
Cheng C Yang209ec562019-03-12 16:37:44 +0800167{
Ed Tanous16966b52021-09-15 15:06:59 -0700168 if (err == boost::asio::error::operation_aborted)
169 {
170 std::cerr << "Read aborted\n";
171 return;
172 }
Yong Libf8b1da2020-04-15 16:32:50 +0800173 if ((err == boost::system::errc::bad_file_descriptor) ||
174 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800175 {
Paul Fertser34533542021-07-20 08:29:09 +0000176 std::cerr << "Bad file descriptor for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800177 return;
178 }
Ed Tanous16966b52021-09-15 15:06:59 -0700179 // null terminate the string so we don't walk off the end
180 std::array<char, 128>& bufferRef = *buffer;
181 bufferRef[bytesRead] = '\0';
Kuiying Wangbcf76712021-03-19 11:17:58 +0800182
Ed Tanous16966b52021-09-15 15:06:59 -0700183 try
Cheng C Yang209ec562019-03-12 16:37:44 +0800184 {
Ed Tanous16966b52021-09-15 15:06:59 -0700185 rawValue = std::stod(bufferRef.data());
186 updateValue((rawValue / sensorFactor) + sensorOffset);
Cheng C Yang209ec562019-03-12 16:37:44 +0800187 }
Ed Tanous16966b52021-09-15 15:06:59 -0700188 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800189 {
Ed Tanous16966b52021-09-15 15:06:59 -0700190 std::cerr << "Could not parse input from " << path << "\n";
James Feist961bf092020-07-01 16:38:12 -0700191 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800192 }
193
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000194 restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +0800195}
196
197void PSUSensor::checkThresholds(void)
198{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000199 if (!readingStateGood())
200 {
201 return;
202 }
203
204 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
Cheng C Yang209ec562019-03-12 16:37:44 +0800205}