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