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 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 17 | #include "PSUSensor.hpp" |
| 18 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 21 | #include <boost/algorithm/string/predicate.hpp> |
| 22 | #include <boost/algorithm/string/replace.hpp> |
| 23 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 24 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 25 | #include <istream> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 26 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 27 | #include <memory> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 28 | #include <sdbusplus/asio/connection.hpp> |
| 29 | #include <sdbusplus/asio/object_server.hpp> |
| 30 | #include <string> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 31 | #include <vector> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 32 | |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 33 | static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/"; |
| 34 | |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 35 | static constexpr bool DEBUG = false; |
| 36 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 37 | PSUSensor::PSUSensor(const std::string& path, const std::string& objectType, |
| 38 | sdbusplus::asio::object_server& objectServer, |
| 39 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 40 | boost::asio::io_service& io, const std::string& sensorName, |
| 41 | std::vector<thresholds::Threshold>&& _thresholds, |
| 42 | const std::string& sensorConfiguration, |
| 43 | std::string& sensorTypeName, unsigned int factor, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 44 | double max, double min, const std::string& label, |
| 45 | size_t tSize) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 46 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 47 | std::move(_thresholds), sensorConfiguration, objectType, max, min), |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame^] | 48 | std::enable_shared_from_this<PSUSensor>(), objServer(objectServer), |
| 49 | inputDev(io), waitTimer(io), path(path), errCount(0), sensorFactor(factor) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 50 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 51 | if constexpr (DEBUG) |
| 52 | { |
| 53 | std::cerr << "Constructed sensor: path " << path << " type " |
| 54 | << objectType << " config " << sensorConfiguration |
| 55 | << " typename " << sensorTypeName << " factor " << factor |
| 56 | << " min " << min << " max " << max << " name \"" |
| 57 | << sensorName << "\"\n"; |
| 58 | } |
| 59 | |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 60 | fd = open(path.c_str(), O_RDONLY); |
| 61 | if (fd < 0) |
| 62 | { |
| 63 | std::cerr << "PSU sensor failed to open file\n"; |
| 64 | return; |
| 65 | } |
| 66 | inputDev.assign(fd); |
| 67 | |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 68 | std::string dbusPath = sensorPathPrefix + sensorTypeName + name; |
| 69 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 70 | sensorInterface = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 71 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 72 | |
| 73 | if (thresholds::hasWarningInterface(thresholds)) |
| 74 | { |
| 75 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 76 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 77 | } |
| 78 | if (thresholds::hasCriticalInterface(thresholds)) |
| 79 | { |
| 80 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 81 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 82 | } |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 83 | |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 84 | // This should be called before initializing association. |
| 85 | // createInventoryAssoc() does add more associations before doing |
| 86 | // register and initialize "Associations" property. |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 87 | if (label.empty() || tSize == _thresholds.size()) |
| 88 | { |
| 89 | setInitialProperties(conn); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | setInitialProperties(conn, label, tSize); |
| 94 | } |
Patrick Venture | 4316218 | 2019-10-23 10:44:53 -0700 | [diff] [blame] | 95 | |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 96 | association = objectServer.add_interface(dbusPath, association::interface); |
| 97 | |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 98 | createInventoryAssoc(conn, association, configurationPath); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | PSUSensor::~PSUSensor() |
| 102 | { |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 103 | waitTimer.cancel(); |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 104 | inputDev.close(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 105 | objServer.remove_interface(sensorInterface); |
| 106 | objServer.remove_interface(thresholdInterfaceWarning); |
| 107 | objServer.remove_interface(thresholdInterfaceCritical); |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 108 | objServer.remove_interface(association); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void PSUSensor::setupRead(void) |
| 112 | { |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame^] | 113 | std::shared_ptr<boost::asio::streambuf> buffer = |
| 114 | std::make_shared<boost::asio::streambuf>(); |
| 115 | std::weak_ptr<PSUSensor> weakRef = weak_from_this(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 116 | boost::asio::async_read_until( |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame^] | 117 | inputDev, *buffer, '\n', |
| 118 | [weakRef, buffer](const boost::system::error_code& ec, |
| 119 | std::size_t /*bytes_transfered*/) { |
| 120 | std::shared_ptr<PSUSensor> self = weakRef.lock(); |
| 121 | if (self) |
| 122 | { |
| 123 | self->readBuf = buffer; |
| 124 | self->handleResponse(ec); |
| 125 | } |
| 126 | }); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void PSUSensor::handleResponse(const boost::system::error_code& err) |
| 130 | { |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame^] | 131 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 132 | (err == boost::asio::error::misc_errors::not_found)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 133 | { |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 134 | std::cerr << "Bad file descriptor from\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 135 | return; |
| 136 | } |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame^] | 137 | std::istream responseStream(readBuf.get()); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 138 | if (!err) |
| 139 | { |
| 140 | std::string response; |
| 141 | try |
| 142 | { |
| 143 | std::getline(responseStream, response); |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 144 | double nvalue = std::stod(response); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 145 | responseStream.clear(); |
| 146 | nvalue /= sensorFactor; |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 147 | |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 148 | updateValue(nvalue); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 149 | errCount = 0; |
| 150 | } |
| 151 | catch (const std::invalid_argument&) |
| 152 | { |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 153 | std::cerr << "Could not parse " << response << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 154 | errCount++; |
| 155 | } |
| 156 | } |
| 157 | else |
| 158 | { |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 159 | std::cerr << "System error " << err << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 160 | errCount++; |
| 161 | } |
| 162 | |
| 163 | if (errCount >= warnAfterErrorCount) |
| 164 | { |
| 165 | if (errCount == warnAfterErrorCount) |
| 166 | { |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 167 | std::cerr << "Failure to read sensor " << name << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 168 | } |
| 169 | updateValue(0); |
| 170 | errCount++; |
| 171 | } |
| 172 | |
Cheng C Yang | a97f134 | 2020-02-11 15:10:41 +0800 | [diff] [blame] | 173 | lseek(fd, 0, SEEK_SET); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 174 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame^] | 175 | |
| 176 | std::weak_ptr<PSUSensor> weakRef = weak_from_this(); |
| 177 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 178 | std::shared_ptr<PSUSensor> self = weakRef.lock(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 179 | if (ec == boost::asio::error::operation_aborted) |
| 180 | { |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 181 | std::cerr << "Failed to reschedule\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 182 | return; |
| 183 | } |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame^] | 184 | if (self) |
| 185 | { |
| 186 | self->setupRead(); |
| 187 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 188 | }); |
| 189 | } |
| 190 | |
| 191 | void PSUSensor::checkThresholds(void) |
| 192 | { |
| 193 | thresholds::checkThresholds(this); |
| 194 | } |