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