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> |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 21 | #include <boost/asio/read_until.hpp> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 22 | #include <boost/date_time/posix_time/posix_time.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 23 | #include <sdbusplus/asio/connection.hpp> |
| 24 | #include <sdbusplus/asio/object_server.hpp> |
| 25 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 26 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 27 | #include <istream> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 28 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 29 | #include <memory> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 30 | #include <string> |
Paul Fertser | 3453354 | 2021-07-20 08:29:09 +0000 | [diff] [blame] | 31 | #include <system_error> |
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, |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 44 | const PowerState& powerState, |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 45 | const std::string& sensorUnits, unsigned int factor, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 46 | double max, double min, double offset, |
| 47 | const std::string& label, size_t tSize, double pollRate) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 48 | Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration, |
| 49 | objectType, false, false, max, min, conn, powerState), |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 50 | objServer(objectServer), inputDev(io), waitTimer(io), path(path), |
| 51 | sensorFactor(factor), sensorOffset(offset), thresholdTimer(io) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 52 | { |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 53 | std::string unitPath = sensor_paths::getPathForUnits(sensorUnits); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 54 | if constexpr (debug) |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 55 | { |
| 56 | std::cerr << "Constructed sensor: path " << path << " type " |
| 57 | << objectType << " config " << sensorConfiguration |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 58 | << " typename " << unitPath << " factor " << factor << " min " |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 59 | << min << " max " << max << " offset " << offset << " name \"" |
| 60 | << sensorName << "\"\n"; |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 61 | } |
Lei YU | 7170a23 | 2021-02-04 16:19:27 +0800 | [diff] [blame] | 62 | if (pollRate > 0.0) |
| 63 | { |
| 64 | sensorPollMs = static_cast<unsigned int>(pollRate * 1000); |
| 65 | } |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 66 | |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 67 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 68 | fd = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 69 | if (fd < 0) |
| 70 | { |
| 71 | std::cerr << "PSU sensor failed to open file\n"; |
| 72 | return; |
| 73 | } |
| 74 | inputDev.assign(fd); |
| 75 | |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 76 | std::string dbusPath = sensorPathPrefix + unitPath + "/" + name; |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 77 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 78 | sensorInterface = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 79 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 80 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 81 | for (const auto& threshold : thresholds) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 82 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 83 | std::string interface = thresholds::getInterface(threshold.level); |
| 84 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 85 | objectServer.add_interface(dbusPath, interface); |
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 | { |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 93 | setInitialProperties(sensorUnits); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 94 | } |
| 95 | else |
| 96 | { |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 97 | setInitialProperties(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); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | PSUSensor::~PSUSensor() |
| 106 | { |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 107 | waitTimer.cancel(); |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 108 | inputDev.close(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 109 | objServer.remove_interface(sensorInterface); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 110 | for (const auto& iface : thresholdInterfaces) |
| 111 | { |
| 112 | objServer.remove_interface(iface); |
| 113 | } |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 114 | objServer.remove_interface(association); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void PSUSensor::setupRead(void) |
| 118 | { |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 119 | if (!readingStateGood()) |
| 120 | { |
| 121 | markAvailable(false); |
| 122 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 123 | restartRead(); |
| 124 | return; |
| 125 | } |
| 126 | |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 127 | std::weak_ptr<PSUSensor> weakRef = weak_from_this(); |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 128 | inputDev.async_wait(boost::asio::posix::descriptor_base::wait_read, |
| 129 | [weakRef](const boost::system::error_code& ec) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 130 | std::shared_ptr<PSUSensor> self = weakRef.lock(); |
| 131 | if (self) |
| 132 | { |
| 133 | self->handleResponse(ec); |
| 134 | } |
| 135 | }); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 138 | void PSUSensor::restartRead(void) |
| 139 | { |
| 140 | std::weak_ptr<PSUSensor> weakRef = weak_from_this(); |
| 141 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
| 142 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 143 | if (ec == boost::asio::error::operation_aborted) |
| 144 | { |
| 145 | std::cerr << "Failed to reschedule\n"; |
| 146 | return; |
| 147 | } |
| 148 | std::shared_ptr<PSUSensor> self = weakRef.lock(); |
| 149 | if (self) |
| 150 | { |
| 151 | self->setupRead(); |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 156 | // Create a buffer expected to be able to hold more characters than will be |
| 157 | // present in the input file. |
| 158 | static constexpr uint32_t psuBufLen = 128; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 159 | void PSUSensor::handleResponse(const boost::system::error_code& err) |
| 160 | { |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 161 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 162 | (err == boost::asio::error::misc_errors::not_found)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 163 | { |
Paul Fertser | 3453354 | 2021-07-20 08:29:09 +0000 | [diff] [blame] | 164 | std::cerr << "Bad file descriptor for " << path << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 165 | return; |
| 166 | } |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 167 | |
| 168 | std::string buffer; |
| 169 | buffer.resize(psuBufLen); |
| 170 | lseek(fd, 0, SEEK_SET); |
| 171 | int rdLen = read(fd, buffer.data(), psuBufLen); |
| 172 | |
| 173 | if (rdLen > 0) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 174 | { |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 175 | try |
| 176 | { |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 177 | rawValue = std::stod(buffer); |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 178 | updateValue((rawValue / sensorFactor) + sensorOffset); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 179 | } |
| 180 | catch (const std::invalid_argument&) |
| 181 | { |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 182 | std::cerr << "Could not parse input from " << path << "\n"; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 183 | incrementError(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | else |
| 187 | { |
Paul Fertser | 3453354 | 2021-07-20 08:29:09 +0000 | [diff] [blame] | 188 | std::cerr |
| 189 | << "System error " << errno << " (" |
| 190 | << std::generic_category().default_error_condition(errno).message() |
| 191 | << ") reading from " << path << ", line: " << __LINE__ << "\n"; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 192 | incrementError(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 193 | } |
| 194 | |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 195 | lseek(fd, 0, SEEK_SET); |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 196 | restartRead(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void PSUSensor::checkThresholds(void) |
| 200 | { |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 201 | if (!readingStateGood()) |
| 202 | { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 207 | } |