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, |
| 44 | double max, double min) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 45 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 46 | std::move(_thresholds), sensorConfiguration, objectType, max, min), |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 47 | path(path), objServer(objectServer), |
| 48 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0), |
| 49 | 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 | |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 60 | std::string dbusPath = sensorPathPrefix + sensorTypeName + name; |
| 61 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 62 | sensorInterface = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 63 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 64 | |
| 65 | if (thresholds::hasWarningInterface(thresholds)) |
| 66 | { |
| 67 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 68 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 69 | } |
| 70 | if (thresholds::hasCriticalInterface(thresholds)) |
| 71 | { |
| 72 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 73 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 74 | } |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 75 | association = objectServer.add_interface(dbusPath, association::interface); |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 76 | |
Patrick Venture | 4316218 | 2019-10-23 10:44:53 -0700 | [diff] [blame] | 77 | setInitialProperties(conn); |
| 78 | |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 79 | createInventoryAssoc(conn, association, configurationPath); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 80 | setupRead(); |
| 81 | } |
| 82 | |
| 83 | PSUSensor::~PSUSensor() |
| 84 | { |
| 85 | inputDev.close(); |
| 86 | waitTimer.cancel(); |
| 87 | objServer.remove_interface(sensorInterface); |
| 88 | objServer.remove_interface(thresholdInterfaceWarning); |
| 89 | objServer.remove_interface(thresholdInterfaceCritical); |
| 90 | } |
| 91 | |
| 92 | void PSUSensor::setupRead(void) |
| 93 | { |
| 94 | boost::asio::async_read_until( |
| 95 | inputDev, readBuf, '\n', |
| 96 | [&](const boost::system::error_code& ec, |
| 97 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
| 98 | } |
| 99 | |
| 100 | void PSUSensor::handleResponse(const boost::system::error_code& err) |
| 101 | { |
| 102 | if (err == boost::system::errc::bad_file_descriptor) |
| 103 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 104 | std::cerr << "Bad file descriptor from " << path << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | std::istream responseStream(&readBuf); |
| 108 | if (!err) |
| 109 | { |
| 110 | std::string response; |
| 111 | try |
| 112 | { |
| 113 | std::getline(responseStream, response); |
| 114 | float nvalue = std::stof(response); |
| 115 | responseStream.clear(); |
| 116 | nvalue /= sensorFactor; |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 117 | |
| 118 | if constexpr (DEBUG) |
| 119 | { |
| 120 | std::cerr << "Read " << path << " scale " << sensorFactor |
| 121 | << " value " << nvalue << "\n"; |
| 122 | } |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 123 | if (static_cast<double>(nvalue) != value) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 124 | { |
Josh Lehan | 432d1ed | 2019-10-16 12:23:31 -0700 | [diff] [blame] | 125 | if constexpr (DEBUG) |
| 126 | { |
| 127 | std::cerr << "Update " << path << " from " << value |
| 128 | << " to " << nvalue << "\n"; |
| 129 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 130 | updateValue(nvalue); |
| 131 | } |
| 132 | errCount = 0; |
| 133 | } |
| 134 | catch (const std::invalid_argument&) |
| 135 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 136 | std::cerr << "Could not parse " << response << " from path " << path |
| 137 | << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 138 | errCount++; |
| 139 | } |
| 140 | } |
| 141 | else |
| 142 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 143 | std::cerr << "System error " << err << " from path " << path << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 144 | errCount++; |
| 145 | } |
| 146 | |
| 147 | if (errCount >= warnAfterErrorCount) |
| 148 | { |
| 149 | if (errCount == warnAfterErrorCount) |
| 150 | { |
| 151 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 152 | << "\n"; |
| 153 | } |
| 154 | updateValue(0); |
| 155 | errCount++; |
| 156 | } |
| 157 | |
| 158 | responseStream.clear(); |
| 159 | inputDev.close(); |
| 160 | int fd = open(path.c_str(), O_RDONLY); |
Jae Hyun Yoo | ef9665a | 2019-10-10 10:26:39 -0700 | [diff] [blame] | 161 | if (fd < 0) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 162 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 163 | std::cerr << "Failed to open path " << path << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | inputDev.assign(fd); |
| 167 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
| 168 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
| 169 | if (ec == boost::asio::error::operation_aborted) |
| 170 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 171 | std::cerr << "Failed to reschedule wait for " << path << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | setupRead(); |
| 175 | }); |
| 176 | } |
| 177 | |
| 178 | void PSUSensor::checkThresholds(void) |
| 179 | { |
| 180 | thresholds::checkThresholds(this); |
| 181 | } |