blob: bc9430f28a0e855c62a25582665df42b4bb3feb5 [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
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#include "DeviceMgmt.hpp"
20#include "SensorPaths.hpp"
21#include "Thresholds.hpp"
22#include "Utils.hpp"
23#include "sensor.hpp"
Cheng C Yang209ec562019-03-12 16:37:44 +080024
Ed Tanouseacbfdd2024-04-04 12:00:24 -070025#include <boost/asio/buffer.hpp>
26#include <boost/asio/error.hpp>
27#include <boost/asio/io_context.hpp>
Ed Tanous16966b52021-09-15 15:06:59 -070028#include <boost/asio/random_access_file.hpp>
George Liu4155a5a2025-01-24 15:26:27 +080029#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <array>
34#include <chrono>
35#include <cstddef>
Cheng C Yang209ec562019-03-12 16:37:44 +080036#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <memory>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <stdexcept>
Cheng C Yang209ec562019-03-12 16:37:44 +080039#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070040#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070041#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080042
James Feist690895f2019-04-23 13:01:08 -070043static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
44
Patrick Williams2aaf7172024-08-16 15:20:40 -040045PSUSensor::PSUSensor(
46 const std::string& path, const std::string& objectType,
47 sdbusplus::asio::object_server& objectServer,
48 std::shared_ptr<sdbusplus::asio::connection>& conn,
49 boost::asio::io_context& io, const std::string& sensorName,
50 std::vector<thresholds::Threshold>&& thresholdsIn,
51 const std::string& sensorConfiguration, const PowerState& powerState,
52 const std::string& sensorUnits, unsigned int factor, double max, double min,
53 double offset, const std::string& label, size_t tSize, double pollRate,
54 const std::shared_ptr<I2CDevice>& i2cDevice) :
Zhikui Renda98f092021-11-01 09:41:08 -070055 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
56 objectType, false, false, max, min, conn, powerState),
Matt Simmeringcafd72f2022-12-16 15:35:12 -080057 i2cDevice(i2cDevice), objServer(objectServer),
Ed Tanous16966b52021-09-15 15:06:59 -070058 inputDev(io, path, boost::asio::random_access_file::read_only),
59 waitTimer(io), path(path), sensorFactor(factor), sensorOffset(offset),
60 thresholdTimer(io)
Cheng C Yang209ec562019-03-12 16:37:44 +080061{
Ed Tanous16966b52021-09-15 15:06:59 -070062 buffer = std::make_shared<std::array<char, 128>>();
Zev Weiss6b6891c2021-04-22 02:46:21 -050063 std::string unitPath = sensor_paths::getPathForUnits(sensorUnits);
Alexander Hansen89be6142025-09-18 15:36:16 +020064 lg2::debug(
65 "Constructed sensor - path: {PATH}, type: {TYPE}, config: {CONFIG}, "
66 "typename: {TYPENAME}, factor: {FACTOR}, min: {MIN}, max: {MAX}, "
67 "offset: {OFFSET}, name: {NAME}",
68 "PATH", path, "TYPE", objectType, "CONFIG", sensorConfiguration,
69 "TYPENAME", unitPath, "FACTOR", factor, "MIN", min, "MAX", max,
70 "OFFSET", offset, "NAME", sensorName);
Lei YU7170a232021-02-04 16:19:27 +080071 if (pollRate > 0.0)
72 {
73 sensorPollMs = static_cast<unsigned int>(pollRate * 1000);
74 }
Josh Lehan49cfba92019-10-08 16:50:42 -070075
Zev Weiss6b6891c2021-04-22 02:46:21 -050076 std::string dbusPath = sensorPathPrefix + unitPath + "/" + name;
James Feist690895f2019-04-23 13:01:08 -070077
Cheng C Yang209ec562019-03-12 16:37:44 +080078 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070079 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080080
Jayashree Dhanapal56678082022-01-04 17:27:20 +053081 for (const auto& threshold : thresholds)
Cheng C Yang209ec562019-03-12 16:37:44 +080082 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053083 std::string interface = thresholds::getInterface(threshold.level);
84 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
85 objectServer.add_interface(dbusPath, interface);
Cheng C Yang209ec562019-03-12 16:37:44 +080086 }
James Feist690895f2019-04-23 13:01:08 -070087
AppaRao Pulic82213c2020-02-27 01:24:58 +053088 // This should be called before initializing association.
89 // createInventoryAssoc() does add more associations before doing
90 // register and initialize "Associations" property.
Ed Tanous8a57ec02020-10-09 12:46:52 -070091 if (label.empty() || tSize == thresholds.size())
Cheng C Yang6b1247a2020-03-09 23:48:39 +080092 {
Andrei Kartashev39287412022-02-04 16:04:47 +030093 setInitialProperties(sensorUnits);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080094 }
95 else
96 {
Andrei Kartashev39287412022-02-04 16:04:47 +030097 setInitialProperties(sensorUnits, label, tSize);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080098 }
Patrick Venture43162182019-10-23 10:44:53 -070099
AppaRao Pulic82213c2020-02-27 01:24:58 +0530100 association = objectServer.add_interface(dbusPath, association::interface);
101
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800102 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +0800103}
104
105PSUSensor::~PSUSensor()
106{
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800107 deactivate();
108
Cheng C Yang209ec562019-03-12 16:37:44 +0800109 objServer.remove_interface(sensorInterface);
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530110 for (const auto& iface : thresholdInterfaces)
111 {
112 objServer.remove_interface(iface);
113 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530114 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800115}
116
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800117bool PSUSensor::isActive()
118{
119 return inputDev.is_open();
120}
121
122void PSUSensor::activate(const std::string& newPath,
123 const std::shared_ptr<I2CDevice>& newI2CDevice)
124{
Chau Ly416c96a2024-09-19 07:27:09 +0000125 if (isActive())
126 {
127 // Avoid activating an active sensor
128 return;
129 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800130 path = newPath;
131 i2cDevice = newI2CDevice;
132 inputDev.open(path, boost::asio::random_access_file::read_only);
133 markAvailable(true);
134 setupRead();
135}
136
137void PSUSensor::deactivate()
138{
139 markAvailable(false);
140 // close the input dev to cancel async operations
141 inputDev.close();
142 waitTimer.cancel();
143 i2cDevice = nullptr;
144 path = "";
145}
146
Ed Tanous201a1012024-04-03 18:07:28 -0700147void PSUSensor::setupRead()
Cheng C Yang209ec562019-03-12 16:37:44 +0800148{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000149 if (!readingStateGood())
150 {
151 markAvailable(false);
152 updateValue(std::numeric_limits<double>::quiet_NaN());
153 restartRead();
154 return;
155 }
156
Ed Tanous16966b52021-09-15 15:06:59 -0700157 if (buffer == nullptr)
158 {
George Liu4155a5a2025-01-24 15:26:27 +0800159 lg2::error("Buffer was invalid?");
Ed Tanous16966b52021-09-15 15:06:59 -0700160 return;
161 }
162
163 std::weak_ptr<PSUSensor> weak = weak_from_this();
164 // Note, we are building a asio buffer that is one char smaller than
165 // the actual data structure, so that we can always append the null
166 // terminator. This can go away once std::from_chars<double> is available
167 // in the standard
168 inputDev.async_read_some_at(
169 0, boost::asio::buffer(buffer->data(), buffer->size() - 1),
170 [weak, buffer{buffer}](const boost::system::error_code& ec,
171 size_t bytesRead) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400172 std::shared_ptr<PSUSensor> self = weak.lock();
173 if (!self)
174 {
175 return;
176 }
Ed Tanous16966b52021-09-15 15:06:59 -0700177
Patrick Williams2aaf7172024-08-16 15:20:40 -0400178 self->handleResponse(ec, bytesRead);
179 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800180}
181
Ed Tanous201a1012024-04-03 18:07:28 -0700182void PSUSensor::restartRead()
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000183{
184 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800185 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000186 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
187 if (ec == boost::asio::error::operation_aborted)
188 {
George Liu4155a5a2025-01-24 15:26:27 +0800189 lg2::error("Failed to reschedule");
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000190 return;
191 }
192 std::shared_ptr<PSUSensor> self = weakRef.lock();
193 if (self)
194 {
195 self->setupRead();
196 }
197 });
198}
199
Kuiying Wangbcf76712021-03-19 11:17:58 +0800200// Create a buffer expected to be able to hold more characters than will be
201// present in the input file.
Ed Tanous16966b52021-09-15 15:06:59 -0700202void PSUSensor::handleResponse(const boost::system::error_code& err,
203 size_t bytesRead)
Cheng C Yang209ec562019-03-12 16:37:44 +0800204{
Ed Tanous16966b52021-09-15 15:06:59 -0700205 if (err == boost::asio::error::operation_aborted)
206 {
George Liu4155a5a2025-01-24 15:26:27 +0800207 lg2::error("Read aborted");
Ed Tanous16966b52021-09-15 15:06:59 -0700208 return;
209 }
Yong Libf8b1da2020-04-15 16:32:50 +0800210 if ((err == boost::system::errc::bad_file_descriptor) ||
211 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800212 {
George Liu4155a5a2025-01-24 15:26:27 +0800213 lg2::error("Bad file descriptor for '{PATH}'", "PATH", path);
Cheng C Yang209ec562019-03-12 16:37:44 +0800214 return;
215 }
Zhikui Renb6f89a02023-08-15 13:53:02 -0700216 if (err || bytesRead == 0)
217 {
218 if (readingStateGood())
219 {
George Liu4155a5a2025-01-24 15:26:27 +0800220 lg2::error("'{NAME}' read failed", "NAME", name);
Zhikui Renb6f89a02023-08-15 13:53:02 -0700221 }
222 restartRead();
223 return;
224 }
225
Ed Tanous16966b52021-09-15 15:06:59 -0700226 // null terminate the string so we don't walk off the end
227 std::array<char, 128>& bufferRef = *buffer;
228 bufferRef[bytesRead] = '\0';
Kuiying Wangbcf76712021-03-19 11:17:58 +0800229
Ed Tanous16966b52021-09-15 15:06:59 -0700230 try
Cheng C Yang209ec562019-03-12 16:37:44 +0800231 {
Ed Tanous16966b52021-09-15 15:06:59 -0700232 rawValue = std::stod(bufferRef.data());
233 updateValue((rawValue / sensorFactor) + sensorOffset);
Cheng C Yang209ec562019-03-12 16:37:44 +0800234 }
Ed Tanous16966b52021-09-15 15:06:59 -0700235 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800236 {
George Liu4155a5a2025-01-24 15:26:27 +0800237 lg2::error("Could not parse input from '{PATH}'", "PATH", path);
James Feist961bf092020-07-01 16:38:12 -0700238 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800239 }
240
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000241 restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +0800242}
243
Ed Tanous201a1012024-04-03 18:07:28 -0700244void PSUSensor::checkThresholds()
Cheng C Yang209ec562019-03-12 16:37:44 +0800245{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000246 if (!readingStateGood())
247 {
248 return;
249 }
250
251 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
Cheng C Yang209ec562019-03-12 16:37:44 +0800252}