blob: db632b68916f73d21d42e9995a9461e7464e7476 [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>
James Feist38fb5982020-05-28 10:09:54 -070029#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31
Ed Tanouseacbfdd2024-04-04 12:00:24 -070032#include <array>
33#include <chrono>
34#include <cstddef>
Cheng C Yang209ec562019-03-12 16:37:44 +080035#include <iostream>
36#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
Ed Tanous8a57ec02020-10-09 12:46:52 -070045static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070046
Cheng C Yang209ec562019-03-12 16:37:44 +080047PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
48 sdbusplus::asio::object_server& objectServer,
49 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080050 boost::asio::io_context& io, const std::string& sensorName,
Ed Tanous8a57ec02020-10-09 12:46:52 -070051 std::vector<thresholds::Threshold>&& thresholdsIn,
Cheng C Yang209ec562019-03-12 16:37:44 +080052 const std::string& sensorConfiguration,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000053 const PowerState& powerState,
Zev Weiss6b6891c2021-04-22 02:46:21 -050054 const std::string& sensorUnits, unsigned int factor,
Jeff Line41d52f2021-04-07 19:38:51 +080055 double max, double min, double offset,
Matt Simmeringcafd72f2022-12-16 15:35:12 -080056 const std::string& label, size_t tSize, double pollRate,
57 const std::shared_ptr<I2CDevice>& i2cDevice) :
Zhikui Renda98f092021-11-01 09:41:08 -070058 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
59 objectType, false, false, max, min, conn, powerState),
Matt Simmeringcafd72f2022-12-16 15:35:12 -080060 i2cDevice(i2cDevice), objServer(objectServer),
Ed Tanous16966b52021-09-15 15:06:59 -070061 inputDev(io, path, boost::asio::random_access_file::read_only),
62 waitTimer(io), path(path), sensorFactor(factor), sensorOffset(offset),
63 thresholdTimer(io)
Cheng C Yang209ec562019-03-12 16:37:44 +080064{
Ed Tanous16966b52021-09-15 15:06:59 -070065 buffer = std::make_shared<std::array<char, 128>>();
Zev Weiss6b6891c2021-04-22 02:46:21 -050066 std::string unitPath = sensor_paths::getPathForUnits(sensorUnits);
Ed Tanous8a57ec02020-10-09 12:46:52 -070067 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -070068 {
69 std::cerr << "Constructed sensor: path " << path << " type "
70 << objectType << " config " << sensorConfiguration
Zev Weiss6b6891c2021-04-22 02:46:21 -050071 << " typename " << unitPath << " factor " << factor << " min "
Jeff Line41d52f2021-04-07 19:38:51 +080072 << min << " max " << max << " offset " << offset << " name \""
73 << sensorName << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -070074 }
Lei YU7170a232021-02-04 16:19:27 +080075 if (pollRate > 0.0)
76 {
77 sensorPollMs = static_cast<unsigned int>(pollRate * 1000);
78 }
Josh Lehan49cfba92019-10-08 16:50:42 -070079
Zev Weiss6b6891c2021-04-22 02:46:21 -050080 std::string dbusPath = sensorPathPrefix + unitPath + "/" + name;
James Feist690895f2019-04-23 13:01:08 -070081
Cheng C Yang209ec562019-03-12 16:37:44 +080082 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070083 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080084
Jayashree Dhanapal56678082022-01-04 17:27:20 +053085 for (const auto& threshold : thresholds)
Cheng C Yang209ec562019-03-12 16:37:44 +080086 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053087 std::string interface = thresholds::getInterface(threshold.level);
88 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
89 objectServer.add_interface(dbusPath, interface);
Cheng C Yang209ec562019-03-12 16:37:44 +080090 }
James Feist690895f2019-04-23 13:01:08 -070091
AppaRao Pulic82213c2020-02-27 01:24:58 +053092 // This should be called before initializing association.
93 // createInventoryAssoc() does add more associations before doing
94 // register and initialize "Associations" property.
Ed Tanous8a57ec02020-10-09 12:46:52 -070095 if (label.empty() || tSize == thresholds.size())
Cheng C Yang6b1247a2020-03-09 23:48:39 +080096 {
Andrei Kartashev39287412022-02-04 16:04:47 +030097 setInitialProperties(sensorUnits);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080098 }
99 else
100 {
Andrei Kartashev39287412022-02-04 16:04:47 +0300101 setInitialProperties(sensorUnits, label, tSize);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800102 }
Patrick Venture43162182019-10-23 10:44:53 -0700103
AppaRao Pulic82213c2020-02-27 01:24:58 +0530104 association = objectServer.add_interface(dbusPath, association::interface);
105
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800106 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +0800107}
108
109PSUSensor::~PSUSensor()
110{
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800111 deactivate();
112
Cheng C Yang209ec562019-03-12 16:37:44 +0800113 objServer.remove_interface(sensorInterface);
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530114 for (const auto& iface : thresholdInterfaces)
115 {
116 objServer.remove_interface(iface);
117 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530118 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800119}
120
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800121bool PSUSensor::isActive()
122{
123 return inputDev.is_open();
124}
125
126void PSUSensor::activate(const std::string& newPath,
127 const std::shared_ptr<I2CDevice>& newI2CDevice)
128{
129 path = newPath;
130 i2cDevice = newI2CDevice;
131 inputDev.open(path, boost::asio::random_access_file::read_only);
132 markAvailable(true);
133 setupRead();
134}
135
136void PSUSensor::deactivate()
137{
138 markAvailable(false);
139 // close the input dev to cancel async operations
140 inputDev.close();
141 waitTimer.cancel();
142 i2cDevice = nullptr;
143 path = "";
144}
145
Ed Tanous201a1012024-04-03 18:07:28 -0700146void PSUSensor::setupRead()
Cheng C Yang209ec562019-03-12 16:37:44 +0800147{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000148 if (!readingStateGood())
149 {
150 markAvailable(false);
151 updateValue(std::numeric_limits<double>::quiet_NaN());
152 restartRead();
153 return;
154 }
155
Ed Tanous16966b52021-09-15 15:06:59 -0700156 if (buffer == nullptr)
157 {
158 std::cerr << "Buffer was invalid?";
159 return;
160 }
161
162 std::weak_ptr<PSUSensor> weak = weak_from_this();
163 // Note, we are building a asio buffer that is one char smaller than
164 // the actual data structure, so that we can always append the null
165 // terminator. This can go away once std::from_chars<double> is available
166 // in the standard
167 inputDev.async_read_some_at(
168 0, boost::asio::buffer(buffer->data(), buffer->size() - 1),
169 [weak, buffer{buffer}](const boost::system::error_code& ec,
170 size_t bytesRead) {
171 std::shared_ptr<PSUSensor> self = weak.lock();
172 if (!self)
Ed Tanousbb679322022-05-16 16:10:00 -0700173 {
Ed Tanous16966b52021-09-15 15:06:59 -0700174 return;
Ed Tanousbb679322022-05-16 16:10:00 -0700175 }
Ed Tanous16966b52021-09-15 15:06:59 -0700176
177 self->handleResponse(ec, bytesRead);
Patrick Williams597e8422023-10-20 11:19:01 -0500178 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800179}
180
Ed Tanous201a1012024-04-03 18:07:28 -0700181void PSUSensor::restartRead()
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000182{
183 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800184 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000185 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
186 if (ec == boost::asio::error::operation_aborted)
187 {
188 std::cerr << "Failed to reschedule\n";
189 return;
190 }
191 std::shared_ptr<PSUSensor> self = weakRef.lock();
192 if (self)
193 {
194 self->setupRead();
195 }
196 });
197}
198
Kuiying Wangbcf76712021-03-19 11:17:58 +0800199// Create a buffer expected to be able to hold more characters than will be
200// present in the input file.
Ed Tanous16966b52021-09-15 15:06:59 -0700201void PSUSensor::handleResponse(const boost::system::error_code& err,
202 size_t bytesRead)
Cheng C Yang209ec562019-03-12 16:37:44 +0800203{
Ed Tanous16966b52021-09-15 15:06:59 -0700204 if (err == boost::asio::error::operation_aborted)
205 {
206 std::cerr << "Read aborted\n";
207 return;
208 }
Yong Libf8b1da2020-04-15 16:32:50 +0800209 if ((err == boost::system::errc::bad_file_descriptor) ||
210 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800211 {
Paul Fertser34533542021-07-20 08:29:09 +0000212 std::cerr << "Bad file descriptor for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800213 return;
214 }
Zhikui Renb6f89a02023-08-15 13:53:02 -0700215 if (err || bytesRead == 0)
216 {
217 if (readingStateGood())
218 {
219 std::cerr << name << " read failed\n";
220 }
221 restartRead();
222 return;
223 }
224
Ed Tanous16966b52021-09-15 15:06:59 -0700225 // null terminate the string so we don't walk off the end
226 std::array<char, 128>& bufferRef = *buffer;
227 bufferRef[bytesRead] = '\0';
Kuiying Wangbcf76712021-03-19 11:17:58 +0800228
Ed Tanous16966b52021-09-15 15:06:59 -0700229 try
Cheng C Yang209ec562019-03-12 16:37:44 +0800230 {
Ed Tanous16966b52021-09-15 15:06:59 -0700231 rawValue = std::stod(bufferRef.data());
232 updateValue((rawValue / sensorFactor) + sensorOffset);
Cheng C Yang209ec562019-03-12 16:37:44 +0800233 }
Ed Tanous16966b52021-09-15 15:06:59 -0700234 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800235 {
Ed Tanous16966b52021-09-15 15:06:59 -0700236 std::cerr << "Could not parse input from " << path << "\n";
James Feist961bf092020-07-01 16:38:12 -0700237 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800238 }
239
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000240 restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +0800241}
242
Ed Tanous201a1012024-04-03 18:07:28 -0700243void PSUSensor::checkThresholds()
Cheng C Yang209ec562019-03-12 16:37:44 +0800244{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000245 if (!readingStateGood())
246 {
247 return;
248 }
249
250 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
Cheng C Yang209ec562019-03-12 16:37:44 +0800251}