blob: 986fe5916f84fbaeca95b622500ca4b35688eb7f [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "PSUSensor.hpp"
18
Cheng C Yang209ec562019-03-12 16:37:44 +080019#include <unistd.h>
20
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>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25
Cheng C Yang209ec562019-03-12 16:37:44 +080026#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <istream>
Cheng C Yang209ec562019-03-12 16:37:44 +080028#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080030#include <string>
Paul Fertser34533542021-07-20 08:29:09 +000031#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080033
James Feist690895f2019-04-23 13:01:08 -070034static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
35
Ed Tanous8a57ec02020-10-09 12:46:52 -070036static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070037
Cheng C Yang209ec562019-03-12 16:37:44 +080038PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
39 sdbusplus::asio::object_server& objectServer,
40 std::shared_ptr<sdbusplus::asio::connection>& conn,
41 boost::asio::io_service& io, const std::string& sensorName,
Ed Tanous8a57ec02020-10-09 12:46:52 -070042 std::vector<thresholds::Threshold>&& thresholdsIn,
Cheng C Yang209ec562019-03-12 16:37:44 +080043 const std::string& sensorConfiguration,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000044 const PowerState& powerState,
Zev Weiss6b6891c2021-04-22 02:46:21 -050045 const std::string& sensorUnits, unsigned int factor,
Jeff Line41d52f2021-04-07 19:38:51 +080046 double max, double min, double offset,
47 const std::string& label, size_t tSize, double pollRate) :
Zhikui Renda98f092021-11-01 09:41:08 -070048 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
49 objectType, false, false, max, min, conn, powerState),
Ed Tanous16966b52021-09-15 15:06:59 -070050 objServer(objectServer),
51 inputDev(io, path, boost::asio::random_access_file::read_only),
52 waitTimer(io), path(path), sensorFactor(factor), sensorOffset(offset),
53 thresholdTimer(io)
Cheng C Yang209ec562019-03-12 16:37:44 +080054{
Ed Tanous16966b52021-09-15 15:06:59 -070055 buffer = std::make_shared<std::array<char, 128>>();
Zev Weiss6b6891c2021-04-22 02:46:21 -050056 std::string unitPath = sensor_paths::getPathForUnits(sensorUnits);
Ed Tanous8a57ec02020-10-09 12:46:52 -070057 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -070058 {
59 std::cerr << "Constructed sensor: path " << path << " type "
60 << objectType << " config " << sensorConfiguration
Zev Weiss6b6891c2021-04-22 02:46:21 -050061 << " typename " << unitPath << " factor " << factor << " min "
Jeff Line41d52f2021-04-07 19:38:51 +080062 << min << " max " << max << " offset " << offset << " name \""
63 << sensorName << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -070064 }
Lei YU7170a232021-02-04 16:19:27 +080065 if (pollRate > 0.0)
66 {
67 sensorPollMs = static_cast<unsigned int>(pollRate * 1000);
68 }
Josh Lehan49cfba92019-10-08 16:50:42 -070069
Zev Weiss6b6891c2021-04-22 02:46:21 -050070 std::string dbusPath = sensorPathPrefix + unitPath + "/" + name;
James Feist690895f2019-04-23 13:01:08 -070071
Cheng C Yang209ec562019-03-12 16:37:44 +080072 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070073 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080074
Jayashree Dhanapal56678082022-01-04 17:27:20 +053075 for (const auto& threshold : thresholds)
Cheng C Yang209ec562019-03-12 16:37:44 +080076 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053077 std::string interface = thresholds::getInterface(threshold.level);
78 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
79 objectServer.add_interface(dbusPath, interface);
Cheng C Yang209ec562019-03-12 16:37:44 +080080 }
James Feist690895f2019-04-23 13:01:08 -070081
AppaRao Pulic82213c2020-02-27 01:24:58 +053082 // This should be called before initializing association.
83 // createInventoryAssoc() does add more associations before doing
84 // register and initialize "Associations" property.
Ed Tanous8a57ec02020-10-09 12:46:52 -070085 if (label.empty() || tSize == thresholds.size())
Cheng C Yang6b1247a2020-03-09 23:48:39 +080086 {
Andrei Kartashev39287412022-02-04 16:04:47 +030087 setInitialProperties(sensorUnits);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080088 }
89 else
90 {
Andrei Kartashev39287412022-02-04 16:04:47 +030091 setInitialProperties(sensorUnits, label, tSize);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080092 }
Patrick Venture43162182019-10-23 10:44:53 -070093
AppaRao Pulic82213c2020-02-27 01:24:58 +053094 association = objectServer.add_interface(dbusPath, association::interface);
95
Cheng C Yang5580f2f2019-09-19 09:01:47 +080096 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080097}
98
99PSUSensor::~PSUSensor()
100{
Cheng C Yang209ec562019-03-12 16:37:44 +0800101 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +0800102 inputDev.close();
Cheng C Yang209ec562019-03-12 16:37:44 +0800103 objServer.remove_interface(sensorInterface);
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530104 for (const auto& iface : thresholdInterfaces)
105 {
106 objServer.remove_interface(iface);
107 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530108 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800109}
110
111void PSUSensor::setupRead(void)
112{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000113 if (!readingStateGood())
114 {
115 markAvailable(false);
116 updateValue(std::numeric_limits<double>::quiet_NaN());
117 restartRead();
118 return;
119 }
120
Ed Tanous16966b52021-09-15 15:06:59 -0700121 if (buffer == nullptr)
122 {
123 std::cerr << "Buffer was invalid?";
124 return;
125 }
126
127 std::weak_ptr<PSUSensor> weak = weak_from_this();
128 // Note, we are building a asio buffer that is one char smaller than
129 // the actual data structure, so that we can always append the null
130 // terminator. This can go away once std::from_chars<double> is available
131 // in the standard
132 inputDev.async_read_some_at(
133 0, boost::asio::buffer(buffer->data(), buffer->size() - 1),
134 [weak, buffer{buffer}](const boost::system::error_code& ec,
135 size_t bytesRead) {
136 std::shared_ptr<PSUSensor> self = weak.lock();
137 if (!self)
Ed Tanousbb679322022-05-16 16:10:00 -0700138 {
Ed Tanous16966b52021-09-15 15:06:59 -0700139 return;
Ed Tanousbb679322022-05-16 16:10:00 -0700140 }
Ed Tanous16966b52021-09-15 15:06:59 -0700141
142 self->handleResponse(ec, bytesRead);
143 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800144}
145
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000146void PSUSensor::restartRead(void)
147{
148 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700149 waitTimer.expires_from_now(std::chrono::milliseconds(sensorPollMs));
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000150 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
151 if (ec == boost::asio::error::operation_aborted)
152 {
153 std::cerr << "Failed to reschedule\n";
154 return;
155 }
156 std::shared_ptr<PSUSensor> self = weakRef.lock();
157 if (self)
158 {
159 self->setupRead();
160 }
161 });
162}
163
Kuiying Wangbcf76712021-03-19 11:17:58 +0800164// Create a buffer expected to be able to hold more characters than will be
165// present in the input file.
Ed Tanous16966b52021-09-15 15:06:59 -0700166void PSUSensor::handleResponse(const boost::system::error_code& err,
167 size_t bytesRead)
Cheng C Yang209ec562019-03-12 16:37:44 +0800168{
Ed Tanous16966b52021-09-15 15:06:59 -0700169 if (err == boost::asio::error::operation_aborted)
170 {
171 std::cerr << "Read aborted\n";
172 return;
173 }
Yong Libf8b1da2020-04-15 16:32:50 +0800174 if ((err == boost::system::errc::bad_file_descriptor) ||
175 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800176 {
Paul Fertser34533542021-07-20 08:29:09 +0000177 std::cerr << "Bad file descriptor for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800178 return;
179 }
Ed Tanous16966b52021-09-15 15:06:59 -0700180 // null terminate the string so we don't walk off the end
181 std::array<char, 128>& bufferRef = *buffer;
182 bufferRef[bytesRead] = '\0';
Kuiying Wangbcf76712021-03-19 11:17:58 +0800183
Ed Tanous16966b52021-09-15 15:06:59 -0700184 try
Cheng C Yang209ec562019-03-12 16:37:44 +0800185 {
Ed Tanous16966b52021-09-15 15:06:59 -0700186 rawValue = std::stod(bufferRef.data());
187 updateValue((rawValue / sensorFactor) + sensorOffset);
Cheng C Yang209ec562019-03-12 16:37:44 +0800188 }
Ed Tanous16966b52021-09-15 15:06:59 -0700189 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800190 {
Ed Tanous16966b52021-09-15 15:06:59 -0700191 std::cerr << "Could not parse input from " << path << "\n";
James Feist961bf092020-07-01 16:38:12 -0700192 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800193 }
194
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000195 restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +0800196}
197
198void PSUSensor::checkThresholds(void)
199{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000200 if (!readingStateGood())
201 {
202 return;
203 }
204
205 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
Cheng C Yang209ec562019-03-12 16:37:44 +0800206}