blob: 8a83f37884c864dc3b259e716b32cc85c6ca6cb6 [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "PSUEvent.hpp"
18
19#include "SensorPaths.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "Utils.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103021
Ed Tanouseacbfdd2024-04-04 12:00:24 -070022#include <boost/asio/buffer.hpp>
23#include <boost/asio/error.hpp>
Ed Tanous1f978632023-02-28 18:16:39 -080024#include <boost/asio/io_context.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070025#include <boost/asio/random_access_file.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070026#include <boost/container/flat_map.hpp>
Patrick Williams0c42f402021-08-27 16:05:45 -050027#include <phosphor-logging/lg2.hpp>
Cheng C Yang58b2b532019-05-31 00:19:45 +080028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030
Ed Tanouseacbfdd2024-04-04 12:00:24 -070031#include <array>
32#include <chrono>
33#include <cstddef>
James Feist38fb5982020-05-28 10:09:54 -070034#include <iostream>
35#include <memory>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <set>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <stdexcept>
38#include <string>
39#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070040#include <vector>
Cheng C Yang58b2b532019-05-31 00:19:45 +080041
42PSUCombineEvent::PSUCombineEvent(
Yong Li3046a022020-04-03 13:01:02 +080043 sdbusplus::asio::object_server& objectServer,
44 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080045 boost::asio::io_context& io, const std::string& psuName,
George Liu5b3542e2023-10-31 17:27:12 +080046 const PowerState& powerState, EventPathList& eventPathList,
47 GroupEventPathList& groupEventPathList, const std::string& combineEventName,
48 double pollRate) :
Cheng C Yang58b2b532019-05-31 00:19:45 +080049 objServer(objectServer)
50{
Ed Tanous6cb732a2021-02-18 15:33:51 -080051 std::string psuNameEscaped = sensor_paths::escapePathForDbus(psuName);
Cheng C Yang58b2b532019-05-31 00:19:45 +080052 eventInterface = objServer.add_interface(
Ed Tanous6cb732a2021-02-18 15:33:51 -080053 "/xyz/openbmc_project/State/Decorator/" + psuNameEscaped + "_" +
Cheng C Yang58b2b532019-05-31 00:19:45 +080054 combineEventName,
55 "xyz.openbmc_project.State.Decorator.OperationalStatus");
James Feistb6c0b912019-07-09 12:21:44 -070056 eventInterface->register_property("functional", true);
Cheng C Yang58b2b532019-05-31 00:19:45 +080057
58 if (!eventInterface->initialize())
59 {
60 std::cerr << "error initializing event interface\n";
61 }
62
63 std::shared_ptr<std::set<std::string>> combineEvent =
64 std::make_shared<std::set<std::string>>();
Zev Weissb85145c2022-08-12 18:21:02 -070065 for (const auto& [eventName, paths] : eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +080066 {
Yong Li591b1e42019-12-19 17:46:31 +080067 std::shared_ptr<std::set<std::string>> assert =
68 std::make_shared<std::set<std::string>>();
Cheng C Yang202a1ff2020-01-09 09:34:22 +080069 std::shared_ptr<bool> state = std::make_shared<bool>(false);
Yong Li591b1e42019-12-19 17:46:31 +080070
Cheng C Yang58b2b532019-05-31 00:19:45 +080071 std::string eventPSUName = eventName + psuName;
Zev Weissb85145c2022-08-12 18:21:02 -070072 for (const auto& path : paths)
Cheng C Yang58b2b532019-05-31 00:19:45 +080073 {
Yong Libf8b1da2020-04-15 16:32:50 +080074 auto p = std::make_shared<PSUSubEvent>(
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000075 eventInterface, path, conn, io, powerState, eventName,
76 eventName, assert, combineEvent, state, psuName, pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +080077 p->setupRead();
78
79 events[eventPSUName].emplace_back(p);
Cheng C Yang58b2b532019-05-31 00:19:45 +080080 asserts.emplace_back(assert);
81 states.emplace_back(state);
82 }
83 }
Cheng C Yang202a1ff2020-01-09 09:34:22 +080084
Zev Weissb85145c2022-08-12 18:21:02 -070085 for (const auto& [eventName, groupEvents] : groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +080086 {
Zev Weissb85145c2022-08-12 18:21:02 -070087 for (const auto& [groupEventName, paths] : groupEvents)
Cheng C Yang202a1ff2020-01-09 09:34:22 +080088 {
89 std::shared_ptr<std::set<std::string>> assert =
90 std::make_shared<std::set<std::string>>();
91 std::shared_ptr<bool> state = std::make_shared<bool>(false);
92
Cheng C Yang202a1ff2020-01-09 09:34:22 +080093 std::string eventPSUName = groupEventName + psuName;
Zev Weissb85145c2022-08-12 18:21:02 -070094 for (const auto& path : paths)
Cheng C Yang202a1ff2020-01-09 09:34:22 +080095 {
Yong Libf8b1da2020-04-15 16:32:50 +080096 auto p = std::make_shared<PSUSubEvent>(
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000097 eventInterface, path, conn, io, powerState, groupEventName,
Zev Weissb85145c2022-08-12 18:21:02 -070098 eventName, assert, combineEvent, state, psuName, pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +080099 p->setupRead();
100 events[eventPSUName].emplace_back(p);
101
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800102 asserts.emplace_back(assert);
103 states.emplace_back(state);
104 }
105 }
106 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800107}
108
109PSUCombineEvent::~PSUCombineEvent()
110{
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800111 // Clear unique_ptr first
Zev Weissb85145c2022-08-12 18:21:02 -0700112 for (auto& [psuName, subEvents] : events)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800113 {
Zev Weissb85145c2022-08-12 18:21:02 -0700114 for (auto& subEventPtr : subEvents)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800115 {
116 subEventPtr.reset();
117 }
118 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800119 events.clear();
120 objServer.remove_interface(eventInterface);
121}
122
Cheng C Yang9b53a622019-08-27 22:13:58 +0800123static boost::container::flat_map<std::string,
124 std::pair<std::string, std::string>>
125 logID = {
126 {"PredictiveFailure",
127 {"OpenBMC.0.1.PowerSupplyFailurePredicted",
128 "OpenBMC.0.1.PowerSupplyPredictedFailureRecovered"}},
129 {"Failure",
130 {"OpenBMC.0.1.PowerSupplyFailed", "OpenBMC.0.1.PowerSupplyRecovered"}},
131 {"ACLost",
Cheng C Yangbb732ef2019-09-18 16:25:58 +0800132 {"OpenBMC.0.1.PowerSupplyPowerLost",
133 "OpenBMC.0.1.PowerSupplyPowerRestored"}},
Cheng C Yang9b53a622019-08-27 22:13:58 +0800134 {"FanFault",
135 {"OpenBMC.0.1.PowerSupplyFanFailed",
136 "OpenBMC.0.1.PowerSupplyFanRecovered"}},
jayaprakash Mutyalad12f23e2019-12-03 23:03:52 +0000137 {"ConfigureError",
138 {"OpenBMC.0.1.PowerSupplyConfigurationError",
139 "OpenBMC.0.1.PowerSupplyConfigurationErrorRecovered"}}};
Cheng C Yang5665db22019-07-12 00:57:30 +0800140
Cheng C Yang58b2b532019-05-31 00:19:45 +0800141PSUSubEvent::PSUSubEvent(
142 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
Yong Li3046a022020-04-03 13:01:02 +0800143 const std::string& path, std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -0800144 boost::asio::io_context& io, const PowerState& powerState,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000145 const std::string& groupEventName, const std::string& eventName,
Cheng C Yang58b2b532019-05-31 00:19:45 +0800146 std::shared_ptr<std::set<std::string>> asserts,
147 std::shared_ptr<std::set<std::string>> combineEvent,
Lei YU7170a232021-02-04 16:19:27 +0800148 std::shared_ptr<bool> state, const std::string& psuName, double pollRate) :
Ed Tanous2049bd22022-07-09 07:20:26 -0700149 eventInterface(std::move(eventInterface)),
150 asserts(std::move(asserts)), combineEvent(std::move(combineEvent)),
151 assertState(std::move(state)), path(path), eventName(eventName),
Ed Tanous16966b52021-09-15 15:06:59 -0700152 readState(powerState), waitTimer(io),
153
154 inputDev(io, path, boost::asio::random_access_file::read_only),
155 psuName(psuName), groupEventName(groupEventName), systemBus(conn)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800156{
Ed Tanous16966b52021-09-15 15:06:59 -0700157 buffer = std::make_shared<std::array<char, 128>>();
Lei YU7170a232021-02-04 16:19:27 +0800158 if (pollRate > 0.0)
159 {
160 eventPollMs = static_cast<unsigned int>(pollRate * 1000);
161 }
Ed Tanous99c44092022-01-14 09:59:09 -0800162
Cheng C Yang5665db22019-07-12 00:57:30 +0800163 auto found = logID.find(eventName);
164 if (found == logID.end())
165 {
Cheng C Yang9b53a622019-08-27 22:13:58 +0800166 assertMessage.clear();
167 deassertMessage.clear();
Cheng C Yang5665db22019-07-12 00:57:30 +0800168 }
169 else
170 {
Cheng C Yang9b53a622019-08-27 22:13:58 +0800171 assertMessage = found->second.first;
172 deassertMessage = found->second.second;
Cheng C Yang5665db22019-07-12 00:57:30 +0800173 }
174
175 auto fanPos = path.find("fan");
176 if (fanPos != std::string::npos)
177 {
178 fanName = path.substr(fanPos);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700179 auto fanNamePos = fanName.find('_');
Cheng C Yang9b53a622019-08-27 22:13:58 +0800180 if (fanNamePos != std::string::npos)
181 {
182 fanName = fanName.substr(0, fanNamePos);
183 }
Cheng C Yang5665db22019-07-12 00:57:30 +0800184 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800185}
186
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000187PSUSubEvent::~PSUSubEvent()
188{
189 waitTimer.cancel();
190 inputDev.close();
191}
192
Ed Tanous201a1012024-04-03 18:07:28 -0700193void PSUSubEvent::setupRead()
Cheng C Yang58b2b532019-05-31 00:19:45 +0800194{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000195 if (!readingStateGood(readState))
196 {
197 // Deassert the event
198 updateValue(0);
199 restartRead();
200 return;
201 }
Ed Tanous16966b52021-09-15 15:06:59 -0700202 if (!buffer)
203 {
204 std::cerr << "Buffer was invalid?";
205 return;
206 }
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000207
Yong Libf8b1da2020-04-15 16:32:50 +0800208 std::weak_ptr<PSUSubEvent> weakRef = weak_from_this();
Ed Tanous16966b52021-09-15 15:06:59 -0700209 inputDev.async_read_some_at(
210 0, boost::asio::buffer(buffer->data(), buffer->size() - 1),
211 [weakRef, buffer{buffer}](const boost::system::error_code& ec,
212 std::size_t bytesTransferred) {
Ed Tanousbb679322022-05-16 16:10:00 -0700213 std::shared_ptr<PSUSubEvent> self = weakRef.lock();
214 if (self)
215 {
Ed Tanous16966b52021-09-15 15:06:59 -0700216 self->handleResponse(ec, bytesTransferred);
Ed Tanousbb679322022-05-16 16:10:00 -0700217 }
Patrick Williams597e8422023-10-20 11:19:01 -0500218 });
Cheng C Yang58b2b532019-05-31 00:19:45 +0800219}
220
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000221void PSUSubEvent::restartRead()
Cheng C Yang58b2b532019-05-31 00:19:45 +0800222{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000223 std::weak_ptr<PSUSubEvent> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800224 waitTimer.expires_after(std::chrono::milliseconds(eventPollMs));
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000225 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
226 if (ec == boost::asio::error::operation_aborted)
227 {
228 return;
229 }
230 std::shared_ptr<PSUSubEvent> self = weakRef.lock();
231 if (self)
232 {
233 self->setupRead();
234 }
235 });
Cheng C Yang58b2b532019-05-31 00:19:45 +0800236}
237
Ed Tanous16966b52021-09-15 15:06:59 -0700238void PSUSubEvent::handleResponse(const boost::system::error_code& err,
239 size_t bytesTransferred)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800240{
Ed Tanous16966b52021-09-15 15:06:59 -0700241 if (err == boost::asio::error::operation_aborted)
242 {
243 return;
244 }
245
Yong Libf8b1da2020-04-15 16:32:50 +0800246 if ((err == boost::system::errc::bad_file_descriptor) ||
247 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang58b2b532019-05-31 00:19:45 +0800248 {
249 return;
250 }
Ed Tanous16966b52021-09-15 15:06:59 -0700251 if (!buffer)
252 {
253 std::cerr << "Buffer was invalid?";
254 return;
255 }
256 // null terminate the string so we don't walk off the end
257 std::array<char, 128>& bufferRef = *buffer;
258 bufferRef[bytesTransferred] = '\0';
259
Cheng C Yang58b2b532019-05-31 00:19:45 +0800260 if (!err)
261 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800262 try
263 {
Ed Tanous16966b52021-09-15 15:06:59 -0700264 int nvalue = std::stoi(bufferRef.data());
Josh Lehan833661a2020-03-04 17:35:41 -0800265 updateValue(nvalue);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800266 errCount = 0;
267 }
268 catch (const std::invalid_argument&)
269 {
270 errCount++;
271 }
272 }
273 else
274 {
275 errCount++;
276 }
277 if (errCount >= warnAfterErrorCount)
278 {
279 if (errCount == warnAfterErrorCount)
280 {
281 std::cerr << "Failure to read event at " << path << "\n";
282 }
283 updateValue(0);
284 errCount++;
285 }
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000286 restartRead();
Cheng C Yang58b2b532019-05-31 00:19:45 +0800287}
288
289// Any of the sub events of one event is asserted, then the event will be
290// asserted. Only if none of the sub events are asserted, the event will be
291// deasserted.
292void PSUSubEvent::updateValue(const int& newValue)
293{
Josh Lehan833661a2020-03-04 17:35:41 -0800294 // Take no action if value already equal
295 // Same semantics as Sensor::updateValue(const double&)
296 if (newValue == value)
297 {
298 return;
299 }
300
Cheng C Yang58b2b532019-05-31 00:19:45 +0800301 if (newValue == 0)
302 {
Yong Li591b1e42019-12-19 17:46:31 +0800303 // log deassert only after all asserts are gone
Cheng C Yang58b2b532019-05-31 00:19:45 +0800304 if (!(*asserts).empty())
305 {
Yong Li591b1e42019-12-19 17:46:31 +0800306 auto found = (*asserts).find(path);
307 if (found == (*asserts).end())
308 {
309 return;
310 }
311 (*asserts).erase(path);
312
Cheng C Yang58b2b532019-05-31 00:19:45 +0800313 return;
314 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700315 if (*assertState)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800316 {
317 *assertState = false;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800318 auto foundCombine = (*combineEvent).find(groupEventName);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800319 if (foundCombine == (*combineEvent).end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800320 {
321 return;
322 }
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800323 (*combineEvent).erase(groupEventName);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800324 if (!deassertMessage.empty())
325 {
326 // Fan Failed has two args
Cheng C Yang9b53a622019-08-27 22:13:58 +0800327 if (deassertMessage == "OpenBMC.0.1.PowerSupplyFanRecovered")
328 {
Patrick Williams0c42f402021-08-27 16:05:45 -0500329 lg2::info("{EVENT} deassert", "EVENT", eventName,
330 "REDFISH_MESSAGE_ID", deassertMessage,
331 "REDFISH_MESSAGE_ARGS",
332 (psuName + ',' + fanName));
Cheng C Yang9b53a622019-08-27 22:13:58 +0800333 }
334 else
335 {
Patrick Williams0c42f402021-08-27 16:05:45 -0500336 lg2::info("{EVENT} deassert", "EVENT", eventName,
337 "REDFISH_MESSAGE_ID", deassertMessage,
338 "REDFISH_MESSAGE_ARGS", psuName);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800339 }
340 }
341
Cheng C Yang58b2b532019-05-31 00:19:45 +0800342 if ((*combineEvent).empty())
343 {
344 eventInterface->set_property("functional", true);
345 }
346 }
347 }
348 else
349 {
Paul Fertser34533542021-07-20 08:29:09 +0000350 std::cerr << "PSUSubEvent asserted by " << path << "\n";
351
Ed Tanous2049bd22022-07-09 07:20:26 -0700352 if ((!*assertState) && ((*asserts).empty()))
Cheng C Yang58b2b532019-05-31 00:19:45 +0800353 {
354 *assertState = true;
Cheng C Yang9b53a622019-08-27 22:13:58 +0800355 if (!assertMessage.empty())
Cheng C Yang5665db22019-07-12 00:57:30 +0800356 {
357 // Fan Failed has two args
Cheng C Yang9b53a622019-08-27 22:13:58 +0800358 if (assertMessage == "OpenBMC.0.1.PowerSupplyFanFailed")
Cheng C Yang5665db22019-07-12 00:57:30 +0800359 {
Patrick Williams0c42f402021-08-27 16:05:45 -0500360 lg2::warning("{EVENT} assert", "EVENT", eventName,
361 "REDFISH_MESSAGE_ID", assertMessage,
362 "REDFISH_MESSAGE_ARGS",
363 (psuName + ',' + fanName));
Cheng C Yang5665db22019-07-12 00:57:30 +0800364 }
365 else
366 {
Patrick Williams0c42f402021-08-27 16:05:45 -0500367 lg2::warning("{EVENT} assert", "EVENT", eventName,
368 "REDFISH_MESSAGE_ID", assertMessage,
369 "REDFISH_MESSAGE_ARGS", psuName);
Cheng C Yang5665db22019-07-12 00:57:30 +0800370 }
371 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800372 if ((*combineEvent).empty())
373 {
374 eventInterface->set_property("functional", false);
375 }
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800376 (*combineEvent).emplace(groupEventName);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800377 }
Yong Li591b1e42019-12-19 17:46:31 +0800378 (*asserts).emplace(path);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800379 }
380 value = newValue;
381}