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 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 31 | PSUSensor::PSUSensor(const std::string& path, const std::string& objectType, |
| 32 | sdbusplus::asio::object_server& objectServer, |
| 33 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 34 | boost::asio::io_service& io, const std::string& sensorName, |
| 35 | std::vector<thresholds::Threshold>&& _thresholds, |
| 36 | const std::string& sensorConfiguration, |
| 37 | std::string& sensorTypeName, unsigned int factor, |
| 38 | double max, double min) : |
| 39 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), path, |
| 40 | std::move(_thresholds), sensorConfiguration, objectType, max, min), |
| 41 | objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)), |
| 42 | waitTimer(io), errCount(0), sensorFactor(factor) |
| 43 | { |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame^] | 44 | std::string dbusPath = sensorPathPrefix + sensorTypeName + name; |
| 45 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 46 | sensorInterface = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame^] | 47 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 48 | |
| 49 | if (thresholds::hasWarningInterface(thresholds)) |
| 50 | { |
| 51 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame^] | 52 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 53 | } |
| 54 | if (thresholds::hasCriticalInterface(thresholds)) |
| 55 | { |
| 56 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame^] | 57 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 58 | } |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame^] | 59 | association = |
| 60 | objectServer.add_interface(dbusPath, "org.openbmc.Associations"); |
| 61 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 62 | setInitialProperties(conn); |
| 63 | setupRead(); |
| 64 | } |
| 65 | |
| 66 | PSUSensor::~PSUSensor() |
| 67 | { |
| 68 | inputDev.close(); |
| 69 | waitTimer.cancel(); |
| 70 | objServer.remove_interface(sensorInterface); |
| 71 | objServer.remove_interface(thresholdInterfaceWarning); |
| 72 | objServer.remove_interface(thresholdInterfaceCritical); |
| 73 | } |
| 74 | |
| 75 | void PSUSensor::setupRead(void) |
| 76 | { |
| 77 | boost::asio::async_read_until( |
| 78 | inputDev, readBuf, '\n', |
| 79 | [&](const boost::system::error_code& ec, |
| 80 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
| 81 | } |
| 82 | |
| 83 | void PSUSensor::handleResponse(const boost::system::error_code& err) |
| 84 | { |
| 85 | if (err == boost::system::errc::bad_file_descriptor) |
| 86 | { |
| 87 | return; |
| 88 | } |
| 89 | std::istream responseStream(&readBuf); |
| 90 | if (!err) |
| 91 | { |
| 92 | std::string response; |
| 93 | try |
| 94 | { |
| 95 | std::getline(responseStream, response); |
| 96 | float nvalue = std::stof(response); |
| 97 | responseStream.clear(); |
| 98 | nvalue /= sensorFactor; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 99 | if (nvalue != value) |
| 100 | { |
| 101 | updateValue(nvalue); |
| 102 | } |
| 103 | errCount = 0; |
| 104 | } |
| 105 | catch (const std::invalid_argument&) |
| 106 | { |
| 107 | errCount++; |
| 108 | } |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | errCount++; |
| 113 | } |
| 114 | |
| 115 | if (errCount >= warnAfterErrorCount) |
| 116 | { |
| 117 | if (errCount == warnAfterErrorCount) |
| 118 | { |
| 119 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 120 | << "\n"; |
| 121 | } |
| 122 | updateValue(0); |
| 123 | errCount++; |
| 124 | } |
| 125 | |
| 126 | responseStream.clear(); |
| 127 | inputDev.close(); |
| 128 | int fd = open(path.c_str(), O_RDONLY); |
| 129 | if (fd <= 0) |
| 130 | { |
| 131 | return; |
| 132 | } |
| 133 | inputDev.assign(fd); |
| 134 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
| 135 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
| 136 | if (ec == boost::asio::error::operation_aborted) |
| 137 | { |
| 138 | return; |
| 139 | } |
| 140 | setupRead(); |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | void PSUSensor::checkThresholds(void) |
| 145 | { |
| 146 | thresholds::checkThresholds(this); |
| 147 | } |