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