| 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 |  | 
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "PSUSensor.hpp" | 
|  | 18 |  | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include "DeviceMgmt.hpp" | 
|  | 20 | #include "SensorPaths.hpp" | 
|  | 21 | #include "Thresholds.hpp" | 
|  | 22 | #include "Utils.hpp" | 
|  | 23 | #include "sensor.hpp" | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 24 |  | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 25 | #include <boost/asio/buffer.hpp> | 
|  | 26 | #include <boost/asio/error.hpp> | 
|  | 27 | #include <boost/asio/io_context.hpp> | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 28 | #include <boost/asio/random_access_file.hpp> | 
| George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 29 | #include <phosphor-logging/lg2.hpp> | 
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 30 | #include <sdbusplus/asio/connection.hpp> | 
|  | 31 | #include <sdbusplus/asio/object_server.hpp> | 
|  | 32 |  | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 33 | #include <array> | 
|  | 34 | #include <chrono> | 
|  | 35 | #include <cstddef> | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 36 | #include <limits> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 37 | #include <memory> | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 38 | #include <stdexcept> | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 39 | #include <string> | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 40 | #include <utility> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 41 | #include <vector> | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 42 |  | 
| James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 43 | static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/"; | 
|  | 44 |  | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 45 | static constexpr bool debug = false; | 
| Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 46 |  | 
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 47 | PSUSensor::PSUSensor( | 
|  | 48 | const std::string& path, const std::string& objectType, | 
|  | 49 | sdbusplus::asio::object_server& objectServer, | 
|  | 50 | std::shared_ptr<sdbusplus::asio::connection>& conn, | 
|  | 51 | boost::asio::io_context& io, const std::string& sensorName, | 
|  | 52 | std::vector<thresholds::Threshold>&& thresholdsIn, | 
|  | 53 | const std::string& sensorConfiguration, const PowerState& powerState, | 
|  | 54 | const std::string& sensorUnits, unsigned int factor, double max, double min, | 
|  | 55 | double offset, const std::string& label, size_t tSize, double pollRate, | 
|  | 56 | const std::shared_ptr<I2CDevice>& i2cDevice) : | 
| Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 57 | Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration, | 
|  | 58 | objectType, false, false, max, min, conn, powerState), | 
| Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 59 | i2cDevice(i2cDevice), objServer(objectServer), | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 60 | inputDev(io, path, boost::asio::random_access_file::read_only), | 
|  | 61 | waitTimer(io), path(path), sensorFactor(factor), sensorOffset(offset), | 
|  | 62 | thresholdTimer(io) | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 63 | { | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 64 | buffer = std::make_shared<std::array<char, 128>>(); | 
| Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 65 | std::string unitPath = sensor_paths::getPathForUnits(sensorUnits); | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 66 | if constexpr (debug) | 
| Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 67 | { | 
| George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 68 | lg2::debug( | 
|  | 69 | "Constructed sensor - path: {PATH}, type: {TYPE}, config: {CONFIG}, " | 
|  | 70 | "typename: {TYPENAME}, factor: {FACTOR}, min: {MIN}, max: {MAX}, " | 
|  | 71 | "offset: {OFFSET}, name: {NAME}", | 
|  | 72 | "PATH", path, "TYPE", objectType, "CONFIG", sensorConfiguration, | 
|  | 73 | "TYPENAME", unitPath, "FACTOR", factor, "MIN", min, "MAX", max, | 
|  | 74 | "OFFSET", offset, "NAME", sensorName); | 
| Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 75 | } | 
| Lei YU | 7170a23 | 2021-02-04 16:19:27 +0800 | [diff] [blame] | 76 | if (pollRate > 0.0) | 
|  | 77 | { | 
|  | 78 | sensorPollMs = static_cast<unsigned int>(pollRate * 1000); | 
|  | 79 | } | 
| Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 80 |  | 
| Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 81 | std::string dbusPath = sensorPathPrefix + unitPath + "/" + name; | 
| James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 82 |  | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 83 | sensorInterface = objectServer.add_interface( | 
| James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 84 | dbusPath, "xyz.openbmc_project.Sensor.Value"); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 85 |  | 
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 86 | for (const auto& threshold : thresholds) | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 87 | { | 
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 88 | std::string interface = thresholds::getInterface(threshold.level); | 
|  | 89 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = | 
|  | 90 | objectServer.add_interface(dbusPath, interface); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 91 | } | 
| James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 92 |  | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 93 | // This should be called before initializing association. | 
|  | 94 | // createInventoryAssoc() does add more associations before doing | 
|  | 95 | // register and initialize "Associations" property. | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 96 | if (label.empty() || tSize == thresholds.size()) | 
| Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 97 | { | 
| Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 98 | setInitialProperties(sensorUnits); | 
| Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 99 | } | 
|  | 100 | else | 
|  | 101 | { | 
| Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 102 | setInitialProperties(sensorUnits, label, tSize); | 
| Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 103 | } | 
| Patrick Venture | 4316218 | 2019-10-23 10:44:53 -0700 | [diff] [blame] | 104 |  | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 105 | association = objectServer.add_interface(dbusPath, association::interface); | 
|  | 106 |  | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 107 | createInventoryAssoc(conn, association, configurationPath); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
|  | 110 | PSUSensor::~PSUSensor() | 
|  | 111 | { | 
| Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 112 | deactivate(); | 
|  | 113 |  | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 114 | objServer.remove_interface(sensorInterface); | 
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 115 | for (const auto& iface : thresholdInterfaces) | 
|  | 116 | { | 
|  | 117 | objServer.remove_interface(iface); | 
|  | 118 | } | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 119 | objServer.remove_interface(association); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 122 | bool PSUSensor::isActive() | 
|  | 123 | { | 
|  | 124 | return inputDev.is_open(); | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | void PSUSensor::activate(const std::string& newPath, | 
|  | 128 | const std::shared_ptr<I2CDevice>& newI2CDevice) | 
|  | 129 | { | 
| Chau Ly | 416c96a | 2024-09-19 07:27:09 +0000 | [diff] [blame] | 130 | if (isActive()) | 
|  | 131 | { | 
|  | 132 | // Avoid activating an active sensor | 
|  | 133 | return; | 
|  | 134 | } | 
| Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 135 | path = newPath; | 
|  | 136 | i2cDevice = newI2CDevice; | 
|  | 137 | inputDev.open(path, boost::asio::random_access_file::read_only); | 
|  | 138 | markAvailable(true); | 
|  | 139 | setupRead(); | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | void PSUSensor::deactivate() | 
|  | 143 | { | 
|  | 144 | markAvailable(false); | 
|  | 145 | // close the input dev to cancel async operations | 
|  | 146 | inputDev.close(); | 
|  | 147 | waitTimer.cancel(); | 
|  | 148 | i2cDevice = nullptr; | 
|  | 149 | path = ""; | 
|  | 150 | } | 
|  | 151 |  | 
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 152 | void PSUSensor::setupRead() | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 153 | { | 
| Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 154 | if (!readingStateGood()) | 
|  | 155 | { | 
|  | 156 | markAvailable(false); | 
|  | 157 | updateValue(std::numeric_limits<double>::quiet_NaN()); | 
|  | 158 | restartRead(); | 
|  | 159 | return; | 
|  | 160 | } | 
|  | 161 |  | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 162 | if (buffer == nullptr) | 
|  | 163 | { | 
| George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 164 | lg2::error("Buffer was invalid?"); | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 165 | return; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | std::weak_ptr<PSUSensor> weak = weak_from_this(); | 
|  | 169 | // Note, we are building a asio buffer that is one char smaller than | 
|  | 170 | // the actual data structure, so that we can always append the null | 
|  | 171 | // terminator.  This can go away once std::from_chars<double> is available | 
|  | 172 | // in the standard | 
|  | 173 | inputDev.async_read_some_at( | 
|  | 174 | 0, boost::asio::buffer(buffer->data(), buffer->size() - 1), | 
|  | 175 | [weak, buffer{buffer}](const boost::system::error_code& ec, | 
|  | 176 | size_t bytesRead) { | 
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 177 | std::shared_ptr<PSUSensor> self = weak.lock(); | 
|  | 178 | if (!self) | 
|  | 179 | { | 
|  | 180 | return; | 
|  | 181 | } | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 182 |  | 
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 183 | self->handleResponse(ec, bytesRead); | 
|  | 184 | }); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 187 | void PSUSensor::restartRead() | 
| Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 188 | { | 
|  | 189 | std::weak_ptr<PSUSensor> weakRef = weak_from_this(); | 
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 190 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); | 
| Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 191 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { | 
|  | 192 | if (ec == boost::asio::error::operation_aborted) | 
|  | 193 | { | 
| George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 194 | lg2::error("Failed to reschedule"); | 
| Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 195 | return; | 
|  | 196 | } | 
|  | 197 | std::shared_ptr<PSUSensor> self = weakRef.lock(); | 
|  | 198 | if (self) | 
|  | 199 | { | 
|  | 200 | self->setupRead(); | 
|  | 201 | } | 
|  | 202 | }); | 
|  | 203 | } | 
|  | 204 |  | 
| Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 205 | // Create a buffer expected to be able to hold more characters than will be | 
|  | 206 | // present in the input file. | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 207 | void PSUSensor::handleResponse(const boost::system::error_code& err, | 
|  | 208 | size_t bytesRead) | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 209 | { | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 210 | if (err == boost::asio::error::operation_aborted) | 
|  | 211 | { | 
| George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 212 | lg2::error("Read aborted"); | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 213 | return; | 
|  | 214 | } | 
| Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 215 | if ((err == boost::system::errc::bad_file_descriptor) || | 
|  | 216 | (err == boost::asio::error::misc_errors::not_found)) | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 217 | { | 
| George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 218 | lg2::error("Bad file descriptor for '{PATH}'", "PATH", path); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 219 | return; | 
|  | 220 | } | 
| Zhikui Ren | b6f89a0 | 2023-08-15 13:53:02 -0700 | [diff] [blame] | 221 | if (err || bytesRead == 0) | 
|  | 222 | { | 
|  | 223 | if (readingStateGood()) | 
|  | 224 | { | 
| George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 225 | lg2::error("'{NAME}' read failed", "NAME", name); | 
| Zhikui Ren | b6f89a0 | 2023-08-15 13:53:02 -0700 | [diff] [blame] | 226 | } | 
|  | 227 | restartRead(); | 
|  | 228 | return; | 
|  | 229 | } | 
|  | 230 |  | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 231 | // null terminate the string so we don't walk off the end | 
|  | 232 | std::array<char, 128>& bufferRef = *buffer; | 
|  | 233 | bufferRef[bytesRead] = '\0'; | 
| Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 234 |  | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 235 | try | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 236 | { | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 237 | rawValue = std::stod(bufferRef.data()); | 
|  | 238 | updateValue((rawValue / sensorFactor) + sensorOffset); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 239 | } | 
| Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame] | 240 | catch (const std::invalid_argument&) | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 241 | { | 
| George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 242 | lg2::error("Could not parse input from '{PATH}'", "PATH", path); | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 243 | incrementError(); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 246 | restartRead(); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 249 | void PSUSensor::checkThresholds() | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 250 | { | 
| Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 251 | if (!readingStateGood()) | 
|  | 252 | { | 
|  | 253 | return; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer); | 
| Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 257 | } |