Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +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 "PSUEvent.hpp" |
| 18 | |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 19 | #include <systemd/sd-journal.h> |
| 20 | |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 21 | #include <boost/container/flat_map.hpp> |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 22 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 23 | #include <memory> |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 24 | #include <sdbusplus/asio/connection.hpp> |
| 25 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 26 | #include <stdexcept> |
| 27 | #include <string> |
| 28 | #include <utility> |
| 29 | #include <variant> |
| 30 | #include <vector> |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 31 | |
| 32 | PSUCombineEvent::PSUCombineEvent( |
| 33 | sdbusplus::asio::object_server& objectServer, boost::asio::io_service& io, |
| 34 | const std::string& psuName, |
| 35 | boost::container::flat_map<std::string, std::vector<std::string>>& |
| 36 | eventPathList, |
| 37 | const std::string& combineEventName) : |
| 38 | objServer(objectServer) |
| 39 | { |
| 40 | eventInterface = objServer.add_interface( |
| 41 | "/xyz/openbmc_project/State/Decorator/" + psuName + "_" + |
| 42 | combineEventName, |
| 43 | "xyz.openbmc_project.State.Decorator.OperationalStatus"); |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 44 | eventInterface->register_property("functional", true); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 45 | |
| 46 | if (!eventInterface->initialize()) |
| 47 | { |
| 48 | std::cerr << "error initializing event interface\n"; |
| 49 | } |
| 50 | |
| 51 | std::shared_ptr<std::set<std::string>> combineEvent = |
| 52 | std::make_shared<std::set<std::string>>(); |
| 53 | for (const auto& pathList : eventPathList) |
| 54 | { |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 55 | std::shared_ptr<std::set<std::string>> assert = |
| 56 | std::make_shared<std::set<std::string>>(); |
| 57 | |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 58 | const std::string& eventName = pathList.first; |
| 59 | std::string eventPSUName = eventName + psuName; |
| 60 | for (const auto& path : pathList.second) |
| 61 | { |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 62 | std::shared_ptr<bool> state = std::make_shared<bool>(false); |
| 63 | events[eventPSUName].emplace_back(std::make_unique<PSUSubEvent>( |
| 64 | eventInterface, path, io, eventName, assert, combineEvent, |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 65 | state, psuName)); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 66 | asserts.emplace_back(assert); |
| 67 | states.emplace_back(state); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | PSUCombineEvent::~PSUCombineEvent() |
| 73 | { |
| 74 | events.clear(); |
| 75 | objServer.remove_interface(eventInterface); |
| 76 | } |
| 77 | |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 78 | static boost::container::flat_map<std::string, |
| 79 | std::pair<std::string, std::string>> |
| 80 | logID = { |
| 81 | {"PredictiveFailure", |
| 82 | {"OpenBMC.0.1.PowerSupplyFailurePredicted", |
| 83 | "OpenBMC.0.1.PowerSupplyPredictedFailureRecovered"}}, |
| 84 | {"Failure", |
| 85 | {"OpenBMC.0.1.PowerSupplyFailed", "OpenBMC.0.1.PowerSupplyRecovered"}}, |
| 86 | {"ACLost", |
Cheng C Yang | bb732ef | 2019-09-18 16:25:58 +0800 | [diff] [blame] | 87 | {"OpenBMC.0.1.PowerSupplyPowerLost", |
| 88 | "OpenBMC.0.1.PowerSupplyPowerRestored"}}, |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 89 | {"FanFault", |
| 90 | {"OpenBMC.0.1.PowerSupplyFanFailed", |
| 91 | "OpenBMC.0.1.PowerSupplyFanRecovered"}}, |
jayaprakash Mutyala | d12f23e | 2019-12-03 23:03:52 +0000 | [diff] [blame] | 92 | {"ConfigureError", |
| 93 | {"OpenBMC.0.1.PowerSupplyConfigurationError", |
| 94 | "OpenBMC.0.1.PowerSupplyConfigurationErrorRecovered"}}}; |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 95 | |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 96 | PSUSubEvent::PSUSubEvent( |
| 97 | std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface, |
| 98 | const std::string& path, boost::asio::io_service& io, |
| 99 | const std::string& eventName, |
| 100 | std::shared_ptr<std::set<std::string>> asserts, |
| 101 | std::shared_ptr<std::set<std::string>> combineEvent, |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 102 | std::shared_ptr<bool> state, const std::string& psuName) : |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 103 | eventInterface(eventInterface), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 104 | asserts(asserts), combineEvent(combineEvent), assertState(state), |
| 105 | errCount(0), path(path), eventName(eventName), waitTimer(io), |
| 106 | inputDev(io, open(path.c_str(), O_RDONLY)), psuName(psuName) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 107 | { |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 108 | auto found = logID.find(eventName); |
| 109 | if (found == logID.end()) |
| 110 | { |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 111 | assertMessage.clear(); |
| 112 | deassertMessage.clear(); |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 113 | } |
| 114 | else |
| 115 | { |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 116 | assertMessage = found->second.first; |
| 117 | deassertMessage = found->second.second; |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | auto fanPos = path.find("fan"); |
| 121 | if (fanPos != std::string::npos) |
| 122 | { |
| 123 | fanName = path.substr(fanPos); |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 124 | auto fanNamePos = fanName.find("_"); |
| 125 | if (fanNamePos != std::string::npos) |
| 126 | { |
| 127 | fanName = fanName.substr(0, fanNamePos); |
| 128 | } |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 129 | } |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 130 | setupRead(); |
| 131 | } |
| 132 | |
| 133 | void PSUSubEvent::setupRead(void) |
| 134 | { |
| 135 | boost::asio::async_read_until( |
| 136 | inputDev, readBuf, '\n', |
| 137 | [&](const boost::system::error_code& ec, |
| 138 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
| 139 | } |
| 140 | |
| 141 | PSUSubEvent::~PSUSubEvent() |
| 142 | { |
| 143 | inputDev.close(); |
| 144 | waitTimer.cancel(); |
| 145 | } |
| 146 | |
| 147 | void PSUSubEvent::handleResponse(const boost::system::error_code& err) |
| 148 | { |
| 149 | if (err == boost::system::errc::bad_file_descriptor) |
| 150 | { |
| 151 | return; |
| 152 | } |
| 153 | std::istream responseStream(&readBuf); |
| 154 | if (!err) |
| 155 | { |
| 156 | std::string response; |
| 157 | try |
| 158 | { |
| 159 | std::getline(responseStream, response); |
| 160 | int nvalue = std::stof(response); |
| 161 | responseStream.clear(); |
| 162 | if (nvalue != value) |
| 163 | { |
| 164 | updateValue(nvalue); |
| 165 | } |
| 166 | errCount = 0; |
| 167 | } |
| 168 | catch (const std::invalid_argument&) |
| 169 | { |
| 170 | errCount++; |
| 171 | } |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | errCount++; |
| 176 | } |
| 177 | if (errCount >= warnAfterErrorCount) |
| 178 | { |
| 179 | if (errCount == warnAfterErrorCount) |
| 180 | { |
| 181 | std::cerr << "Failure to read event at " << path << "\n"; |
| 182 | } |
| 183 | updateValue(0); |
| 184 | errCount++; |
| 185 | } |
| 186 | responseStream.clear(); |
| 187 | inputDev.close(); |
| 188 | int fd = open(path.c_str(), O_RDONLY); |
Jae Hyun Yoo | ef9665a | 2019-10-10 10:26:39 -0700 | [diff] [blame] | 189 | if (fd < 0) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 190 | { |
| 191 | return; |
| 192 | } |
| 193 | inputDev.assign(fd); |
| 194 | waitTimer.expires_from_now(boost::posix_time::milliseconds(eventPollMs)); |
| 195 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
| 196 | if (ec == boost::asio::error::operation_aborted) |
| 197 | { |
| 198 | return; |
| 199 | } |
| 200 | setupRead(); |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | // Any of the sub events of one event is asserted, then the event will be |
| 205 | // asserted. Only if none of the sub events are asserted, the event will be |
| 206 | // deasserted. |
| 207 | void PSUSubEvent::updateValue(const int& newValue) |
| 208 | { |
| 209 | if (newValue == 0) |
| 210 | { |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 211 | // log deassert only after all asserts are gone |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 212 | if (!(*asserts).empty()) |
| 213 | { |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 214 | auto found = (*asserts).find(path); |
| 215 | if (found == (*asserts).end()) |
| 216 | { |
| 217 | return; |
| 218 | } |
| 219 | (*asserts).erase(path); |
| 220 | |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | if (*assertState == true) |
| 224 | { |
| 225 | *assertState = false; |
| 226 | auto foundCombine = (*combineEvent).find(eventName); |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 227 | if (foundCombine == (*combineEvent).end()) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 228 | { |
| 229 | return; |
| 230 | } |
| 231 | (*combineEvent).erase(eventName); |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 232 | if (!deassertMessage.empty()) |
| 233 | { |
| 234 | // Fan Failed has two args |
| 235 | std::string sendMessage = eventName + " deassert"; |
| 236 | if (deassertMessage == "OpenBMC.0.1.PowerSupplyFanRecovered") |
| 237 | { |
| 238 | sd_journal_send( |
| 239 | "MESSAGE=%s", sendMessage.c_str(), "PRIORITY=%i", |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 240 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 241 | deassertMessage.c_str(), "REDFISH_MESSAGE_ARGS=%s,%s", |
| 242 | psuName.c_str(), fanName.c_str(), NULL); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | sd_journal_send( |
| 247 | "MESSAGE=%s", sendMessage.c_str(), "PRIORITY=%i", |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 248 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 249 | deassertMessage.c_str(), "REDFISH_MESSAGE_ARGS=%s", |
| 250 | psuName.c_str(), NULL); |
| 251 | } |
| 252 | } |
| 253 | |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 254 | if ((*combineEvent).empty()) |
| 255 | { |
| 256 | eventInterface->set_property("functional", true); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | { |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 262 | if ((*assertState == false) && ((*asserts).empty())) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 263 | { |
| 264 | *assertState = true; |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 265 | if (!assertMessage.empty()) |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 266 | { |
| 267 | // Fan Failed has two args |
| 268 | std::string sendMessage = eventName + " assert"; |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 269 | if (assertMessage == "OpenBMC.0.1.PowerSupplyFanFailed") |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 270 | { |
Cheng C Yang | 9b53a62 | 2019-08-27 22:13:58 +0800 | [diff] [blame] | 271 | sd_journal_send( |
| 272 | "MESSAGE=%s", sendMessage.c_str(), "PRIORITY=%i", |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 273 | LOG_WARNING, "REDFISH_MESSAGE_ID=%s", |
| 274 | assertMessage.c_str(), "REDFISH_MESSAGE_ARGS=%s,%s", |
| 275 | psuName.c_str(), fanName.c_str(), NULL); |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 276 | } |
| 277 | else |
| 278 | { |
| 279 | sd_journal_send( |
| 280 | "MESSAGE=%s", sendMessage.c_str(), "PRIORITY=%i", |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 281 | LOG_WARNING, "REDFISH_MESSAGE_ID=%s", |
| 282 | assertMessage.c_str(), "REDFISH_MESSAGE_ARGS=%s", |
| 283 | psuName.c_str(), NULL); |
Cheng C Yang | 5665db2 | 2019-07-12 00:57:30 +0800 | [diff] [blame] | 284 | } |
| 285 | } |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 286 | if ((*combineEvent).empty()) |
| 287 | { |
| 288 | eventInterface->set_property("functional", false); |
| 289 | } |
| 290 | (*combineEvent).emplace(eventName); |
| 291 | } |
Yong Li | 591b1e4 | 2019-12-19 17:46:31 +0800 | [diff] [blame] | 292 | (*asserts).emplace(path); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 293 | } |
| 294 | value = newValue; |
| 295 | } |