blob: d1859c107684d63870b099238456bba96de4ac77 [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,
Ed Tanous1f978632023-02-28 18:16:39 -080041 boost::asio::io_context& 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,
Matt Simmeringcafd72f2022-12-16 15:35:12 -080047 const std::string& label, size_t tSize, double pollRate,
48 const std::shared_ptr<I2CDevice>& i2cDevice) :
Zhikui Renda98f092021-11-01 09:41:08 -070049 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
50 objectType, false, false, max, min, conn, powerState),
Matt Simmeringcafd72f2022-12-16 15:35:12 -080051 i2cDevice(i2cDevice), objServer(objectServer),
Ed Tanous16966b52021-09-15 15:06:59 -070052 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{
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800102 deactivate();
103
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
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800112bool PSUSensor::isActive()
113{
114 return inputDev.is_open();
115}
116
117void PSUSensor::activate(const std::string& newPath,
118 const std::shared_ptr<I2CDevice>& newI2CDevice)
119{
120 path = newPath;
121 i2cDevice = newI2CDevice;
122 inputDev.open(path, boost::asio::random_access_file::read_only);
123 markAvailable(true);
124 setupRead();
125}
126
127void PSUSensor::deactivate()
128{
129 markAvailable(false);
130 // close the input dev to cancel async operations
131 inputDev.close();
132 waitTimer.cancel();
133 i2cDevice = nullptr;
134 path = "";
135}
136
Cheng C Yang209ec562019-03-12 16:37:44 +0800137void PSUSensor::setupRead(void)
138{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000139 if (!readingStateGood())
140 {
141 markAvailable(false);
142 updateValue(std::numeric_limits<double>::quiet_NaN());
143 restartRead();
144 return;
145 }
146
Ed Tanous16966b52021-09-15 15:06:59 -0700147 if (buffer == nullptr)
148 {
149 std::cerr << "Buffer was invalid?";
150 return;
151 }
152
153 std::weak_ptr<PSUSensor> weak = weak_from_this();
154 // Note, we are building a asio buffer that is one char smaller than
155 // the actual data structure, so that we can always append the null
156 // terminator. This can go away once std::from_chars<double> is available
157 // in the standard
158 inputDev.async_read_some_at(
159 0, boost::asio::buffer(buffer->data(), buffer->size() - 1),
160 [weak, buffer{buffer}](const boost::system::error_code& ec,
161 size_t bytesRead) {
162 std::shared_ptr<PSUSensor> self = weak.lock();
163 if (!self)
Ed Tanousbb679322022-05-16 16:10:00 -0700164 {
Ed Tanous16966b52021-09-15 15:06:59 -0700165 return;
Ed Tanousbb679322022-05-16 16:10:00 -0700166 }
Ed Tanous16966b52021-09-15 15:06:59 -0700167
168 self->handleResponse(ec, bytesRead);
169 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800170}
171
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000172void PSUSensor::restartRead(void)
173{
174 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800175 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000176 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
177 if (ec == boost::asio::error::operation_aborted)
178 {
179 std::cerr << "Failed to reschedule\n";
180 return;
181 }
182 std::shared_ptr<PSUSensor> self = weakRef.lock();
183 if (self)
184 {
185 self->setupRead();
186 }
187 });
188}
189
Kuiying Wangbcf76712021-03-19 11:17:58 +0800190// Create a buffer expected to be able to hold more characters than will be
191// present in the input file.
Ed Tanous16966b52021-09-15 15:06:59 -0700192void PSUSensor::handleResponse(const boost::system::error_code& err,
193 size_t bytesRead)
Cheng C Yang209ec562019-03-12 16:37:44 +0800194{
Ed Tanous16966b52021-09-15 15:06:59 -0700195 if (err == boost::asio::error::operation_aborted)
196 {
197 std::cerr << "Read aborted\n";
198 return;
199 }
Yong Libf8b1da2020-04-15 16:32:50 +0800200 if ((err == boost::system::errc::bad_file_descriptor) ||
201 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800202 {
Paul Fertser34533542021-07-20 08:29:09 +0000203 std::cerr << "Bad file descriptor for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800204 return;
205 }
Zhikui Renb6f89a02023-08-15 13:53:02 -0700206 if (err || bytesRead == 0)
207 {
208 if (readingStateGood())
209 {
210 std::cerr << name << " read failed\n";
211 }
212 restartRead();
213 return;
214 }
215
Ed Tanous16966b52021-09-15 15:06:59 -0700216 // null terminate the string so we don't walk off the end
217 std::array<char, 128>& bufferRef = *buffer;
218 bufferRef[bytesRead] = '\0';
Kuiying Wangbcf76712021-03-19 11:17:58 +0800219
Ed Tanous16966b52021-09-15 15:06:59 -0700220 try
Cheng C Yang209ec562019-03-12 16:37:44 +0800221 {
Ed Tanous16966b52021-09-15 15:06:59 -0700222 rawValue = std::stod(bufferRef.data());
223 updateValue((rawValue / sensorFactor) + sensorOffset);
Cheng C Yang209ec562019-03-12 16:37:44 +0800224 }
Ed Tanous16966b52021-09-15 15:06:59 -0700225 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800226 {
Ed Tanous16966b52021-09-15 15:06:59 -0700227 std::cerr << "Could not parse input from " << path << "\n";
James Feist961bf092020-07-01 16:38:12 -0700228 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800229 }
230
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000231 restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +0800232}
233
234void PSUSensor::checkThresholds(void)
235{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000236 if (!readingStateGood())
237 {
238 return;
239 }
240
241 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
Cheng C Yang209ec562019-03-12 16:37:44 +0800242}