Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1 | /* |
| 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 Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 19 | #include <PSUSensor.hpp> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 20 | #include <boost/algorithm/string/predicate.hpp> |
| 21 | #include <boost/algorithm/string/replace.hpp> |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 22 | #include <boost/asio/read_until.hpp> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 23 | #include <boost/date_time/posix_time/posix_time.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 24 | #include <sdbusplus/asio/connection.hpp> |
| 25 | #include <sdbusplus/asio/object_server.hpp> |
| 26 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 27 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 28 | #include <istream> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 29 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 30 | #include <memory> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 31 | #include <string> |
Paul Fertser | 3453354 | 2021-07-20 08:29:09 +0000 | [diff] [blame] | 32 | #include <system_error> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 33 | #include <vector> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 34 | |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 35 | static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/"; |
| 36 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 37 | static constexpr bool debug = false; |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 38 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 39 | PSUSensor::PSUSensor(const std::string& path, const std::string& objectType, |
| 40 | sdbusplus::asio::object_server& objectServer, |
| 41 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 42 | boost::asio::io_service& io, const std::string& sensorName, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 43 | std::vector<thresholds::Threshold>&& thresholdsIn, |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 44 | const std::string& sensorConfiguration, |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 45 | const PowerState& powerState, |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 46 | const std::string& sensorUnits, unsigned int factor, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 47 | double max, double min, double offset, |
| 48 | const std::string& label, size_t tSize, double pollRate) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 49 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 50 | std::move(thresholdsIn), sensorConfiguration, objectType, false, max, |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 51 | min, conn, powerState), |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 52 | std::enable_shared_from_this<PSUSensor>(), objServer(objectServer), |
Zbigniew Kurzynski | 484b9b3 | 2020-06-18 18:15:55 +0200 | [diff] [blame] | 53 | inputDev(io), waitTimer(io), path(path), pathRatedMax(""), pathRatedMin(""), |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 54 | sensorFactor(factor), minMaxReadCounter(0), sensorOffset(offset), |
| 55 | thresholdTimer(io) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 56 | { |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 57 | std::string unitPath = sensor_paths::getPathForUnits(sensorUnits); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 58 | if constexpr (debug) |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 59 | { |
| 60 | std::cerr << "Constructed sensor: path " << path << " type " |
| 61 | << objectType << " config " << sensorConfiguration |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 62 | << " typename " << unitPath << " factor " << factor << " min " |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 63 | << min << " max " << max << " offset " << offset << " name \"" |
| 64 | << sensorName << "\"\n"; |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 65 | } |
Lei YU | 7170a23 | 2021-02-04 16:19:27 +0800 | [diff] [blame] | 66 | if (pollRate > 0.0) |
| 67 | { |
| 68 | sensorPollMs = static_cast<unsigned int>(pollRate * 1000); |
| 69 | } |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 70 | |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 71 | fd = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 72 | if (fd < 0) |
| 73 | { |
| 74 | std::cerr << "PSU sensor failed to open file\n"; |
| 75 | return; |
| 76 | } |
| 77 | inputDev.assign(fd); |
| 78 | |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 79 | std::string dbusPath = sensorPathPrefix + unitPath + "/" + name; |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 80 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 81 | sensorInterface = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 82 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 83 | |
| 84 | if (thresholds::hasWarningInterface(thresholds)) |
| 85 | { |
| 86 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 87 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 88 | } |
| 89 | if (thresholds::hasCriticalInterface(thresholds)) |
| 90 | { |
| 91 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 92 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 93 | } |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 94 | |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 95 | // This should be called before initializing association. |
| 96 | // createInventoryAssoc() does add more associations before doing |
| 97 | // register and initialize "Associations" property. |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 98 | if (label.empty() || tSize == thresholds.size()) |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 99 | { |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 100 | setInitialProperties(conn, sensorUnits); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 104 | setInitialProperties(conn, sensorUnits, label, tSize); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 105 | } |
Patrick Venture | 4316218 | 2019-10-23 10:44:53 -0700 | [diff] [blame] | 106 | |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 107 | association = objectServer.add_interface(dbusPath, association::interface); |
| 108 | |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 109 | createInventoryAssoc(conn, association, configurationPath); |
Zbigniew Kurzynski | 484b9b3 | 2020-06-18 18:15:55 +0200 | [diff] [blame] | 110 | |
| 111 | if (auto fileParts = splitFileName(path)) |
| 112 | { |
Zbigniew Kurzynski | dbfd466 | 2020-09-28 18:06:00 +0200 | [diff] [blame] | 113 | auto& [type, nr, item] = *fileParts; |
Zbigniew Kurzynski | 484b9b3 | 2020-06-18 18:15:55 +0200 | [diff] [blame] | 114 | if (item.compare("input") == 0) |
| 115 | { |
| 116 | pathRatedMax = boost::replace_all_copy(path, item, "rated_max"); |
| 117 | pathRatedMin = boost::replace_all_copy(path, item, "rated_min"); |
| 118 | } |
| 119 | } |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 120 | if constexpr (debug) |
Zbigniew Kurzynski | 484b9b3 | 2020-06-18 18:15:55 +0200 | [diff] [blame] | 121 | { |
| 122 | std::cerr << "File: " << pathRatedMax |
| 123 | << " will be used to update MaxValue\n"; |
| 124 | std::cerr << "File: " << pathRatedMin |
| 125 | << " will be used to update MinValue\n"; |
| 126 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | PSUSensor::~PSUSensor() |
| 130 | { |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 131 | waitTimer.cancel(); |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 132 | inputDev.close(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 133 | objServer.remove_interface(sensorInterface); |
| 134 | objServer.remove_interface(thresholdInterfaceWarning); |
| 135 | objServer.remove_interface(thresholdInterfaceCritical); |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 136 | objServer.remove_interface(association); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void PSUSensor::setupRead(void) |
| 140 | { |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 141 | if (!readingStateGood()) |
| 142 | { |
| 143 | markAvailable(false); |
| 144 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 145 | restartRead(); |
| 146 | return; |
| 147 | } |
| 148 | |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 149 | std::weak_ptr<PSUSensor> weakRef = weak_from_this(); |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 150 | inputDev.async_wait(boost::asio::posix::descriptor_base::wait_read, |
| 151 | [weakRef](const boost::system::error_code& ec) { |
| 152 | std::shared_ptr<PSUSensor> self = weakRef.lock(); |
| 153 | if (self) |
| 154 | { |
| 155 | self->handleResponse(ec); |
| 156 | } |
| 157 | }); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 158 | } |
| 159 | |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 160 | void PSUSensor::restartRead(void) |
| 161 | { |
| 162 | std::weak_ptr<PSUSensor> weakRef = weak_from_this(); |
| 163 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
| 164 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 165 | if (ec == boost::asio::error::operation_aborted) |
| 166 | { |
| 167 | std::cerr << "Failed to reschedule\n"; |
| 168 | return; |
| 169 | } |
| 170 | std::shared_ptr<PSUSensor> self = weakRef.lock(); |
| 171 | if (self) |
| 172 | { |
| 173 | self->setupRead(); |
| 174 | } |
| 175 | }); |
| 176 | } |
| 177 | |
Zbigniew Kurzynski | 484b9b3 | 2020-06-18 18:15:55 +0200 | [diff] [blame] | 178 | void PSUSensor::updateMinMaxValues(void) |
| 179 | { |
| 180 | if (auto newVal = readFile(pathRatedMin, sensorFactor)) |
| 181 | { |
| 182 | updateProperty(sensorInterface, minValue, *newVal, "MinValue"); |
| 183 | } |
| 184 | |
| 185 | if (auto newVal = readFile(pathRatedMax, sensorFactor)) |
| 186 | { |
| 187 | updateProperty(sensorInterface, maxValue, *newVal, "MaxValue"); |
| 188 | } |
| 189 | } |
| 190 | |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 191 | // Create a buffer expected to be able to hold more characters than will be |
| 192 | // present in the input file. |
| 193 | static constexpr uint32_t psuBufLen = 128; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 194 | void PSUSensor::handleResponse(const boost::system::error_code& err) |
| 195 | { |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 196 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 197 | (err == boost::asio::error::misc_errors::not_found)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 198 | { |
Paul Fertser | 3453354 | 2021-07-20 08:29:09 +0000 | [diff] [blame] | 199 | std::cerr << "Bad file descriptor for " << path << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 200 | return; |
| 201 | } |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 202 | |
| 203 | std::string buffer; |
| 204 | buffer.resize(psuBufLen); |
| 205 | lseek(fd, 0, SEEK_SET); |
| 206 | int rdLen = read(fd, buffer.data(), psuBufLen); |
| 207 | |
| 208 | if (rdLen > 0) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 209 | { |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 210 | try |
| 211 | { |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 212 | rawValue = std::stod(buffer); |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 213 | updateValue((rawValue / sensorFactor) + sensorOffset); |
Zbigniew Kurzynski | 484b9b3 | 2020-06-18 18:15:55 +0200 | [diff] [blame] | 214 | if (minMaxReadCounter++ % 8 == 0) |
| 215 | { |
| 216 | updateMinMaxValues(); |
| 217 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 218 | } |
| 219 | catch (const std::invalid_argument&) |
| 220 | { |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 221 | std::cerr << "Could not parse input from " << path << "\n"; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 222 | incrementError(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | else |
| 226 | { |
Paul Fertser | 3453354 | 2021-07-20 08:29:09 +0000 | [diff] [blame] | 227 | std::cerr |
| 228 | << "System error " << errno << " (" |
| 229 | << std::generic_category().default_error_condition(errno).message() |
| 230 | << ") reading from " << path << ", line: " << __LINE__ << "\n"; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 231 | incrementError(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 232 | } |
| 233 | |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 234 | lseek(fd, 0, SEEK_SET); |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 235 | restartRead(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void PSUSensor::checkThresholds(void) |
| 239 | { |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 240 | if (!readingStateGood()) |
| 241 | { |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 246 | } |