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> |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 21 | #include <boost/asio/random_access_file.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) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 49 | Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration, |
| 50 | objectType, false, false, max, min, conn, powerState), |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 51 | objServer(objectServer), |
| 52 | inputDev(io, path, boost::asio::random_access_file::read_only), |
| 53 | waitTimer(io), path(path), sensorFactor(factor), sensorOffset(offset), |
| 54 | thresholdTimer(io) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 55 | { |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 56 | buffer = std::make_shared<std::array<char, 128>>(); |
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 | |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 71 | std::string dbusPath = sensorPathPrefix + unitPath + "/" + name; |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 72 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 73 | sensorInterface = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 74 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 75 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 76 | for (const auto& threshold : thresholds) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 77 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 78 | std::string interface = thresholds::getInterface(threshold.level); |
| 79 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 80 | objectServer.add_interface(dbusPath, interface); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 81 | } |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 82 | |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 83 | // This should be called before initializing association. |
| 84 | // createInventoryAssoc() does add more associations before doing |
| 85 | // register and initialize "Associations" property. |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 86 | if (label.empty() || tSize == thresholds.size()) |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 87 | { |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 88 | setInitialProperties(sensorUnits); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 89 | } |
| 90 | else |
| 91 | { |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 92 | setInitialProperties(sensorUnits, label, tSize); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 93 | } |
Patrick Venture | 4316218 | 2019-10-23 10:44:53 -0700 | [diff] [blame] | 94 | |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 95 | association = objectServer.add_interface(dbusPath, association::interface); |
| 96 | |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 97 | createInventoryAssoc(conn, association, configurationPath); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | PSUSensor::~PSUSensor() |
| 101 | { |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 102 | waitTimer.cancel(); |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 103 | inputDev.close(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 104 | objServer.remove_interface(sensorInterface); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 105 | for (const auto& iface : thresholdInterfaces) |
| 106 | { |
| 107 | objServer.remove_interface(iface); |
| 108 | } |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 109 | objServer.remove_interface(association); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void PSUSensor::setupRead(void) |
| 113 | { |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 114 | if (!readingStateGood()) |
| 115 | { |
| 116 | markAvailable(false); |
| 117 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 118 | restartRead(); |
| 119 | return; |
| 120 | } |
| 121 | |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 122 | if (buffer == nullptr) |
| 123 | { |
| 124 | std::cerr << "Buffer was invalid?"; |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | std::weak_ptr<PSUSensor> weak = weak_from_this(); |
| 129 | // Note, we are building a asio buffer that is one char smaller than |
| 130 | // the actual data structure, so that we can always append the null |
| 131 | // terminator. This can go away once std::from_chars<double> is available |
| 132 | // in the standard |
| 133 | inputDev.async_read_some_at( |
| 134 | 0, boost::asio::buffer(buffer->data(), buffer->size() - 1), |
| 135 | [weak, buffer{buffer}](const boost::system::error_code& ec, |
| 136 | size_t bytesRead) { |
| 137 | std::shared_ptr<PSUSensor> self = weak.lock(); |
| 138 | if (!self) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 139 | { |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 140 | return; |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 141 | } |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 142 | |
| 143 | self->handleResponse(ec, bytesRead); |
| 144 | }); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 145 | } |
| 146 | |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 147 | void PSUSensor::restartRead(void) |
| 148 | { |
| 149 | std::weak_ptr<PSUSensor> weakRef = weak_from_this(); |
| 150 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
| 151 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 152 | if (ec == boost::asio::error::operation_aborted) |
| 153 | { |
| 154 | std::cerr << "Failed to reschedule\n"; |
| 155 | return; |
| 156 | } |
| 157 | std::shared_ptr<PSUSensor> self = weakRef.lock(); |
| 158 | if (self) |
| 159 | { |
| 160 | self->setupRead(); |
| 161 | } |
| 162 | }); |
| 163 | } |
| 164 | |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 165 | // Create a buffer expected to be able to hold more characters than will be |
| 166 | // present in the input file. |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 167 | void PSUSensor::handleResponse(const boost::system::error_code& err, |
| 168 | size_t bytesRead) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 169 | { |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 170 | if (err == boost::asio::error::operation_aborted) |
| 171 | { |
| 172 | std::cerr << "Read aborted\n"; |
| 173 | return; |
| 174 | } |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 175 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 176 | (err == boost::asio::error::misc_errors::not_found)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 177 | { |
Paul Fertser | 3453354 | 2021-07-20 08:29:09 +0000 | [diff] [blame] | 178 | std::cerr << "Bad file descriptor for " << path << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 179 | return; |
| 180 | } |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 181 | // null terminate the string so we don't walk off the end |
| 182 | std::array<char, 128>& bufferRef = *buffer; |
| 183 | bufferRef[bytesRead] = '\0'; |
Kuiying Wang | bcf7671 | 2021-03-19 11:17:58 +0800 | [diff] [blame] | 184 | |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 185 | try |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 186 | { |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 187 | rawValue = std::stod(bufferRef.data()); |
| 188 | updateValue((rawValue / sensorFactor) + sensorOffset); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 189 | } |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 190 | catch (const std::invalid_argument&) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 191 | { |
Ed Tanous | 16966b5 | 2021-09-15 15:06:59 -0700 | [diff] [blame^] | 192 | std::cerr << "Could not parse input from " << path << "\n"; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 193 | incrementError(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 194 | } |
| 195 | |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 196 | lseek(fd, 0, SEEK_SET); |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 197 | restartRead(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void PSUSensor::checkThresholds(void) |
| 201 | { |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 202 | if (!readingStateGood()) |
| 203 | { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 208 | } |