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