blob: 27bb8b19b1e7e2be6999af585e5adb3033332a30 [file] [log] [blame]
Cheng C Yang58b2b532019-05-31 00:19:45 +08001/*
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
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <PSUEvent.hpp>
Ed Tanous6cb732a2021-02-18 15:33:51 -080018#include <SensorPaths.hpp>
James Feist8086aba2020-08-25 16:00:59 -070019#include <boost/asio/io_service.hpp>
20#include <boost/asio/read_until.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
Patrick Williams0c42f402021-08-27 16:05:45 -050022#include <phosphor-logging/lg2.hpp>
Cheng C Yang58b2b532019-05-31 00:19:45 +080023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025
26#include <iostream>
27#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <stdexcept>
29#include <string>
30#include <utility>
31#include <variant>
32#include <vector>
Cheng C Yang58b2b532019-05-31 00:19:45 +080033
34PSUCombineEvent::PSUCombineEvent(
Yong Li3046a022020-04-03 13:01:02 +080035 sdbusplus::asio::object_server& objectServer,
36 std::shared_ptr<sdbusplus::asio::connection>& conn,
37 boost::asio::io_service& io, const std::string& psuName,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000038 const PowerState& powerState,
Cheng C Yang58b2b532019-05-31 00:19:45 +080039 boost::container::flat_map<std::string, std::vector<std::string>>&
40 eventPathList,
Cheng C Yang202a1ff2020-01-09 09:34:22 +080041 boost::container::flat_map<
42 std::string,
43 boost::container::flat_map<std::string, std::vector<std::string>>>&
44 groupEventPathList,
Lei YU7170a232021-02-04 16:19:27 +080045 const std::string& combineEventName, double pollRate) :
Cheng C Yang58b2b532019-05-31 00:19:45 +080046 objServer(objectServer)
47{
Ed Tanous6cb732a2021-02-18 15:33:51 -080048 std::string psuNameEscaped = sensor_paths::escapePathForDbus(psuName);
Cheng C Yang58b2b532019-05-31 00:19:45 +080049 eventInterface = objServer.add_interface(
Ed Tanous6cb732a2021-02-18 15:33:51 -080050 "/xyz/openbmc_project/State/Decorator/" + psuNameEscaped + "_" +
Cheng C Yang58b2b532019-05-31 00:19:45 +080051 combineEventName,
52 "xyz.openbmc_project.State.Decorator.OperationalStatus");
James Feistb6c0b912019-07-09 12:21:44 -070053 eventInterface->register_property("functional", true);
Cheng C Yang58b2b532019-05-31 00:19:45 +080054
55 if (!eventInterface->initialize())
56 {
57 std::cerr << "error initializing event interface\n";
58 }
59
60 std::shared_ptr<std::set<std::string>> combineEvent =
61 std::make_shared<std::set<std::string>>();
Zev Weissb85145c2022-08-12 18:21:02 -070062 for (const auto& [eventName, paths] : eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +080063 {
Yong Li591b1e42019-12-19 17:46:31 +080064 std::shared_ptr<std::set<std::string>> assert =
65 std::make_shared<std::set<std::string>>();
Cheng C Yang202a1ff2020-01-09 09:34:22 +080066 std::shared_ptr<bool> state = std::make_shared<bool>(false);
Yong Li591b1e42019-12-19 17:46:31 +080067
Cheng C Yang58b2b532019-05-31 00:19:45 +080068 std::string eventPSUName = eventName + psuName;
Zev Weissb85145c2022-08-12 18:21:02 -070069 for (const auto& path : paths)
Cheng C Yang58b2b532019-05-31 00:19:45 +080070 {
Yong Libf8b1da2020-04-15 16:32:50 +080071 auto p = std::make_shared<PSUSubEvent>(
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000072 eventInterface, path, conn, io, powerState, eventName,
73 eventName, assert, combineEvent, state, psuName, pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +080074 p->setupRead();
75
76 events[eventPSUName].emplace_back(p);
Cheng C Yang58b2b532019-05-31 00:19:45 +080077 asserts.emplace_back(assert);
78 states.emplace_back(state);
79 }
80 }
Cheng C Yang202a1ff2020-01-09 09:34:22 +080081
Zev Weissb85145c2022-08-12 18:21:02 -070082 for (const auto& [eventName, groupEvents] : groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +080083 {
Zev Weissb85145c2022-08-12 18:21:02 -070084 for (const auto& [groupEventName, paths] : groupEvents)
Cheng C Yang202a1ff2020-01-09 09:34:22 +080085 {
86 std::shared_ptr<std::set<std::string>> assert =
87 std::make_shared<std::set<std::string>>();
88 std::shared_ptr<bool> state = std::make_shared<bool>(false);
89
Cheng C Yang202a1ff2020-01-09 09:34:22 +080090 std::string eventPSUName = groupEventName + psuName;
Zev Weissb85145c2022-08-12 18:21:02 -070091 for (const auto& path : paths)
Cheng C Yang202a1ff2020-01-09 09:34:22 +080092 {
Yong Libf8b1da2020-04-15 16:32:50 +080093 auto p = std::make_shared<PSUSubEvent>(
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000094 eventInterface, path, conn, io, powerState, groupEventName,
Zev Weissb85145c2022-08-12 18:21:02 -070095 eventName, assert, combineEvent, state, psuName, pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +080096 p->setupRead();
97 events[eventPSUName].emplace_back(p);
98
Cheng C Yang202a1ff2020-01-09 09:34:22 +080099 asserts.emplace_back(assert);
100 states.emplace_back(state);
101 }
102 }
103 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800104}
105
106PSUCombineEvent::~PSUCombineEvent()
107{
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800108 // Clear unique_ptr first
Zev Weissb85145c2022-08-12 18:21:02 -0700109 for (auto& [psuName, subEvents] : events)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800110 {
Zev Weissb85145c2022-08-12 18:21:02 -0700111 for (auto& subEventPtr : subEvents)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800112 {
113 subEventPtr.reset();
114 }
115 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800116 events.clear();
117 objServer.remove_interface(eventInterface);
118}
119
Cheng C Yang9b53a622019-08-27 22:13:58 +0800120static boost::container::flat_map<std::string,
121 std::pair<std::string, std::string>>
122 logID = {
123 {"PredictiveFailure",
124 {"OpenBMC.0.1.PowerSupplyFailurePredicted",
125 "OpenBMC.0.1.PowerSupplyPredictedFailureRecovered"}},
126 {"Failure",
127 {"OpenBMC.0.1.PowerSupplyFailed", "OpenBMC.0.1.PowerSupplyRecovered"}},
128 {"ACLost",
Cheng C Yangbb732ef2019-09-18 16:25:58 +0800129 {"OpenBMC.0.1.PowerSupplyPowerLost",
130 "OpenBMC.0.1.PowerSupplyPowerRestored"}},
Cheng C Yang9b53a622019-08-27 22:13:58 +0800131 {"FanFault",
132 {"OpenBMC.0.1.PowerSupplyFanFailed",
133 "OpenBMC.0.1.PowerSupplyFanRecovered"}},
jayaprakash Mutyalad12f23e2019-12-03 23:03:52 +0000134 {"ConfigureError",
135 {"OpenBMC.0.1.PowerSupplyConfigurationError",
136 "OpenBMC.0.1.PowerSupplyConfigurationErrorRecovered"}}};
Cheng C Yang5665db22019-07-12 00:57:30 +0800137
Cheng C Yang58b2b532019-05-31 00:19:45 +0800138PSUSubEvent::PSUSubEvent(
139 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
Yong Li3046a022020-04-03 13:01:02 +0800140 const std::string& path, std::shared_ptr<sdbusplus::asio::connection>& conn,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000141 boost::asio::io_service& io, const PowerState& powerState,
142 const std::string& groupEventName, const std::string& eventName,
Cheng C Yang58b2b532019-05-31 00:19:45 +0800143 std::shared_ptr<std::set<std::string>> asserts,
144 std::shared_ptr<std::set<std::string>> combineEvent,
Lei YU7170a232021-02-04 16:19:27 +0800145 std::shared_ptr<bool> state, const std::string& psuName, double pollRate) :
Ed Tanous2049bd22022-07-09 07:20:26 -0700146 eventInterface(std::move(eventInterface)),
147 asserts(std::move(asserts)), combineEvent(std::move(combineEvent)),
148 assertState(std::move(state)), path(path), eventName(eventName),
Ed Tanous16966b52021-09-15 15:06:59 -0700149 readState(powerState), waitTimer(io),
150
151 inputDev(io, path, boost::asio::random_access_file::read_only),
152 psuName(psuName), groupEventName(groupEventName), systemBus(conn)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800153{
Ed Tanous16966b52021-09-15 15:06:59 -0700154 buffer = std::make_shared<std::array<char, 128>>();
Lei YU7170a232021-02-04 16:19:27 +0800155 if (pollRate > 0.0)
156 {
157 eventPollMs = static_cast<unsigned int>(pollRate * 1000);
158 }
Ed Tanous99c44092022-01-14 09:59:09 -0800159
Cheng C Yang5665db22019-07-12 00:57:30 +0800160 auto found = logID.find(eventName);
161 if (found == logID.end())
162 {
Cheng C Yang9b53a622019-08-27 22:13:58 +0800163 assertMessage.clear();
164 deassertMessage.clear();
Cheng C Yang5665db22019-07-12 00:57:30 +0800165 }
166 else
167 {
Cheng C Yang9b53a622019-08-27 22:13:58 +0800168 assertMessage = found->second.first;
169 deassertMessage = found->second.second;
Cheng C Yang5665db22019-07-12 00:57:30 +0800170 }
171
172 auto fanPos = path.find("fan");
173 if (fanPos != std::string::npos)
174 {
175 fanName = path.substr(fanPos);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700176 auto fanNamePos = fanName.find('_');
Cheng C Yang9b53a622019-08-27 22:13:58 +0800177 if (fanNamePos != std::string::npos)
178 {
179 fanName = fanName.substr(0, fanNamePos);
180 }
Cheng C Yang5665db22019-07-12 00:57:30 +0800181 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800182}
183
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000184PSUSubEvent::~PSUSubEvent()
185{
186 waitTimer.cancel();
187 inputDev.close();
188}
189
Cheng C Yang58b2b532019-05-31 00:19:45 +0800190void PSUSubEvent::setupRead(void)
191{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000192 if (!readingStateGood(readState))
193 {
194 // Deassert the event
195 updateValue(0);
196 restartRead();
197 return;
198 }
Ed Tanous16966b52021-09-15 15:06:59 -0700199 if (!buffer)
200 {
201 std::cerr << "Buffer was invalid?";
202 return;
203 }
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000204
Yong Libf8b1da2020-04-15 16:32:50 +0800205 std::weak_ptr<PSUSubEvent> weakRef = weak_from_this();
Ed Tanous16966b52021-09-15 15:06:59 -0700206 inputDev.async_read_some_at(
207 0, boost::asio::buffer(buffer->data(), buffer->size() - 1),
208 [weakRef, buffer{buffer}](const boost::system::error_code& ec,
209 std::size_t bytesTransferred) {
Ed Tanousbb679322022-05-16 16:10:00 -0700210 std::shared_ptr<PSUSubEvent> self = weakRef.lock();
211 if (self)
212 {
Ed Tanous16966b52021-09-15 15:06:59 -0700213 self->handleResponse(ec, bytesTransferred);
Ed Tanousbb679322022-05-16 16:10:00 -0700214 }
Yong Libf8b1da2020-04-15 16:32:50 +0800215 });
Cheng C Yang58b2b532019-05-31 00:19:45 +0800216}
217
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000218void PSUSubEvent::restartRead()
Cheng C Yang58b2b532019-05-31 00:19:45 +0800219{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000220 std::weak_ptr<PSUSubEvent> weakRef = weak_from_this();
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700221 waitTimer.expires_from_now(std::chrono::milliseconds(eventPollMs));
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000222 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
223 if (ec == boost::asio::error::operation_aborted)
224 {
225 return;
226 }
227 std::shared_ptr<PSUSubEvent> self = weakRef.lock();
228 if (self)
229 {
230 self->setupRead();
231 }
232 });
Cheng C Yang58b2b532019-05-31 00:19:45 +0800233}
234
Ed Tanous16966b52021-09-15 15:06:59 -0700235void PSUSubEvent::handleResponse(const boost::system::error_code& err,
236 size_t bytesTransferred)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800237{
Ed Tanous16966b52021-09-15 15:06:59 -0700238 if (err == boost::asio::error::operation_aborted)
239 {
240 return;
241 }
242
Yong Libf8b1da2020-04-15 16:32:50 +0800243 if ((err == boost::system::errc::bad_file_descriptor) ||
244 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang58b2b532019-05-31 00:19:45 +0800245 {
246 return;
247 }
Ed Tanous16966b52021-09-15 15:06:59 -0700248 if (!buffer)
249 {
250 std::cerr << "Buffer was invalid?";
251 return;
252 }
253 // null terminate the string so we don't walk off the end
254 std::array<char, 128>& bufferRef = *buffer;
255 bufferRef[bytesTransferred] = '\0';
256
Cheng C Yang58b2b532019-05-31 00:19:45 +0800257 if (!err)
258 {
259 std::string response;
260 try
261 {
Ed Tanous16966b52021-09-15 15:06:59 -0700262 int nvalue = std::stoi(bufferRef.data());
Josh Lehan833661a2020-03-04 17:35:41 -0800263 updateValue(nvalue);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800264 errCount = 0;
265 }
266 catch (const std::invalid_argument&)
267 {
268 errCount++;
269 }
270 }
271 else
272 {
273 errCount++;
274 }
275 if (errCount >= warnAfterErrorCount)
276 {
277 if (errCount == warnAfterErrorCount)
278 {
279 std::cerr << "Failure to read event at " << path << "\n";
280 }
281 updateValue(0);
282 errCount++;
283 }
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000284 restartRead();
Cheng C Yang58b2b532019-05-31 00:19:45 +0800285}
286
287// Any of the sub events of one event is asserted, then the event will be
288// asserted. Only if none of the sub events are asserted, the event will be
289// deasserted.
290void PSUSubEvent::updateValue(const int& newValue)
291{
Josh Lehan833661a2020-03-04 17:35:41 -0800292 // Take no action if value already equal
293 // Same semantics as Sensor::updateValue(const double&)
294 if (newValue == value)
295 {
296 return;
297 }
298
Cheng C Yang58b2b532019-05-31 00:19:45 +0800299 if (newValue == 0)
300 {
Yong Li591b1e42019-12-19 17:46:31 +0800301 // log deassert only after all asserts are gone
Cheng C Yang58b2b532019-05-31 00:19:45 +0800302 if (!(*asserts).empty())
303 {
Yong Li591b1e42019-12-19 17:46:31 +0800304 auto found = (*asserts).find(path);
305 if (found == (*asserts).end())
306 {
307 return;
308 }
309 (*asserts).erase(path);
310
Cheng C Yang58b2b532019-05-31 00:19:45 +0800311 return;
312 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700313 if (*assertState)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800314 {
315 *assertState = false;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800316 auto foundCombine = (*combineEvent).find(groupEventName);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800317 if (foundCombine == (*combineEvent).end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800318 {
319 return;
320 }
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800321 (*combineEvent).erase(groupEventName);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800322 if (!deassertMessage.empty())
323 {
324 // Fan Failed has two args
Cheng C Yang9b53a622019-08-27 22:13:58 +0800325 if (deassertMessage == "OpenBMC.0.1.PowerSupplyFanRecovered")
326 {
Patrick Williams0c42f402021-08-27 16:05:45 -0500327 lg2::info("{EVENT} deassert", "EVENT", eventName,
328 "REDFISH_MESSAGE_ID", deassertMessage,
329 "REDFISH_MESSAGE_ARGS",
330 (psuName + ',' + fanName));
Cheng C Yang9b53a622019-08-27 22:13:58 +0800331 }
332 else
333 {
Patrick Williams0c42f402021-08-27 16:05:45 -0500334 lg2::info("{EVENT} deassert", "EVENT", eventName,
335 "REDFISH_MESSAGE_ID", deassertMessage,
336 "REDFISH_MESSAGE_ARGS", psuName);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800337 }
338 }
339
Cheng C Yang58b2b532019-05-31 00:19:45 +0800340 if ((*combineEvent).empty())
341 {
342 eventInterface->set_property("functional", true);
343 }
344 }
345 }
346 else
347 {
Paul Fertser34533542021-07-20 08:29:09 +0000348 std::cerr << "PSUSubEvent asserted by " << path << "\n";
349
Ed Tanous2049bd22022-07-09 07:20:26 -0700350 if ((!*assertState) && ((*asserts).empty()))
Cheng C Yang58b2b532019-05-31 00:19:45 +0800351 {
352 *assertState = true;
Cheng C Yang9b53a622019-08-27 22:13:58 +0800353 if (!assertMessage.empty())
Cheng C Yang5665db22019-07-12 00:57:30 +0800354 {
355 // Fan Failed has two args
Cheng C Yang9b53a622019-08-27 22:13:58 +0800356 if (assertMessage == "OpenBMC.0.1.PowerSupplyFanFailed")
Cheng C Yang5665db22019-07-12 00:57:30 +0800357 {
Patrick Williams0c42f402021-08-27 16:05:45 -0500358 lg2::warning("{EVENT} assert", "EVENT", eventName,
359 "REDFISH_MESSAGE_ID", assertMessage,
360 "REDFISH_MESSAGE_ARGS",
361 (psuName + ',' + fanName));
Cheng C Yang5665db22019-07-12 00:57:30 +0800362 }
363 else
364 {
Patrick Williams0c42f402021-08-27 16:05:45 -0500365 lg2::warning("{EVENT} assert", "EVENT", eventName,
366 "REDFISH_MESSAGE_ID", assertMessage,
367 "REDFISH_MESSAGE_ARGS", psuName);
Cheng C Yang5665db22019-07-12 00:57:30 +0800368 }
369 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800370 if ((*combineEvent).empty())
371 {
372 eventInterface->set_property("functional", false);
373 }
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800374 (*combineEvent).emplace(groupEventName);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800375 }
Yong Li591b1e42019-12-19 17:46:31 +0800376 (*asserts).emplace(path);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800377 }
378 value = newValue;
379}