blob: 08101de15394973d3373087c62a2434b94672302 [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "PSUEvent.hpp"
18
Cheng C Yang5665db22019-07-12 00:57:30 +080019#include <systemd/sd-journal.h>
20
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
Cheng C Yang58b2b532019-05-31 00:19:45 +080022#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <memory>
Cheng C Yang58b2b532019-05-31 00:19:45 +080024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070026#include <stdexcept>
27#include <string>
28#include <utility>
29#include <variant>
30#include <vector>
Cheng C Yang58b2b532019-05-31 00:19:45 +080031
32PSUCombineEvent::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 Feistb6c0b912019-07-09 12:21:44 -070044 eventInterface->register_property("functional", true);
Cheng C Yang58b2b532019-05-31 00:19:45 +080045
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 {
55 const std::string& eventName = pathList.first;
56 std::string eventPSUName = eventName + psuName;
57 for (const auto& path : pathList.second)
58 {
59 std::shared_ptr<std::set<std::string>> assert =
60 std::make_shared<std::set<std::string>>();
61 std::shared_ptr<bool> state = std::make_shared<bool>(false);
62 events[eventPSUName].emplace_back(std::make_unique<PSUSubEvent>(
63 eventInterface, path, io, eventName, assert, combineEvent,
Cheng C Yang5665db22019-07-12 00:57:30 +080064 state, psuName));
Cheng C Yang58b2b532019-05-31 00:19:45 +080065 asserts.emplace_back(assert);
66 states.emplace_back(state);
67 }
68 }
69}
70
71PSUCombineEvent::~PSUCombineEvent()
72{
73 events.clear();
74 objServer.remove_interface(eventInterface);
75}
76
Cheng C Yang9b53a622019-08-27 22:13:58 +080077static boost::container::flat_map<std::string,
78 std::pair<std::string, std::string>>
79 logID = {
80 {"PredictiveFailure",
81 {"OpenBMC.0.1.PowerSupplyFailurePredicted",
82 "OpenBMC.0.1.PowerSupplyPredictedFailureRecovered"}},
83 {"Failure",
84 {"OpenBMC.0.1.PowerSupplyFailed", "OpenBMC.0.1.PowerSupplyRecovered"}},
85 {"ACLost",
Cheng C Yangbb732ef2019-09-18 16:25:58 +080086 {"OpenBMC.0.1.PowerSupplyPowerLost",
87 "OpenBMC.0.1.PowerSupplyPowerRestored"}},
Cheng C Yang9b53a622019-08-27 22:13:58 +080088 {"FanFault",
89 {"OpenBMC.0.1.PowerSupplyFanFailed",
90 "OpenBMC.0.1.PowerSupplyFanRecovered"}},
91 {"ConfigureError", {"OpenBMC.0.1.PowerSupplyConfigurationError", ""}}};
Cheng C Yang5665db22019-07-12 00:57:30 +080092
Cheng C Yang58b2b532019-05-31 00:19:45 +080093PSUSubEvent::PSUSubEvent(
94 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
95 const std::string& path, boost::asio::io_service& io,
96 const std::string& eventName,
97 std::shared_ptr<std::set<std::string>> asserts,
98 std::shared_ptr<std::set<std::string>> combineEvent,
Cheng C Yang5665db22019-07-12 00:57:30 +080099 std::shared_ptr<bool> state, const std::string& psuName) :
Cheng C Yang58b2b532019-05-31 00:19:45 +0800100 eventInterface(eventInterface),
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500101 asserts(asserts), combineEvent(combineEvent), assertState(state),
102 errCount(0), path(path), eventName(eventName), waitTimer(io),
103 inputDev(io, open(path.c_str(), O_RDONLY)), psuName(psuName)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800104{
Cheng C Yang5665db22019-07-12 00:57:30 +0800105 auto found = logID.find(eventName);
106 if (found == logID.end())
107 {
Cheng C Yang9b53a622019-08-27 22:13:58 +0800108 assertMessage.clear();
109 deassertMessage.clear();
Cheng C Yang5665db22019-07-12 00:57:30 +0800110 }
111 else
112 {
Cheng C Yang9b53a622019-08-27 22:13:58 +0800113 assertMessage = found->second.first;
114 deassertMessage = found->second.second;
Cheng C Yang5665db22019-07-12 00:57:30 +0800115 }
116
117 auto fanPos = path.find("fan");
118 if (fanPos != std::string::npos)
119 {
120 fanName = path.substr(fanPos);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800121 auto fanNamePos = fanName.find("_");
122 if (fanNamePos != std::string::npos)
123 {
124 fanName = fanName.substr(0, fanNamePos);
125 }
Cheng C Yang5665db22019-07-12 00:57:30 +0800126 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800127 setupRead();
128}
129
130void PSUSubEvent::setupRead(void)
131{
132 boost::asio::async_read_until(
133 inputDev, readBuf, '\n',
134 [&](const boost::system::error_code& ec,
135 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
136}
137
138PSUSubEvent::~PSUSubEvent()
139{
140 inputDev.close();
141 waitTimer.cancel();
142}
143
144void PSUSubEvent::handleResponse(const boost::system::error_code& err)
145{
146 if (err == boost::system::errc::bad_file_descriptor)
147 {
148 return;
149 }
150 std::istream responseStream(&readBuf);
151 if (!err)
152 {
153 std::string response;
154 try
155 {
156 std::getline(responseStream, response);
157 int nvalue = std::stof(response);
158 responseStream.clear();
159 if (nvalue != value)
160 {
161 updateValue(nvalue);
162 }
163 errCount = 0;
164 }
165 catch (const std::invalid_argument&)
166 {
167 errCount++;
168 }
169 }
170 else
171 {
172 errCount++;
173 }
174 if (errCount >= warnAfterErrorCount)
175 {
176 if (errCount == warnAfterErrorCount)
177 {
178 std::cerr << "Failure to read event at " << path << "\n";
179 }
180 updateValue(0);
181 errCount++;
182 }
183 responseStream.clear();
184 inputDev.close();
185 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700186 if (fd < 0)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800187 {
188 return;
189 }
190 inputDev.assign(fd);
191 waitTimer.expires_from_now(boost::posix_time::milliseconds(eventPollMs));
192 waitTimer.async_wait([&](const boost::system::error_code& ec) {
193 if (ec == boost::asio::error::operation_aborted)
194 {
195 return;
196 }
197 setupRead();
198 });
199}
200
201// Any of the sub events of one event is asserted, then the event will be
202// asserted. Only if none of the sub events are asserted, the event will be
203// deasserted.
204void PSUSubEvent::updateValue(const int& newValue)
205{
206 if (newValue == 0)
207 {
208 auto found = (*asserts).find(path);
209 if (found == (*asserts).end())
210 {
211 return;
212 }
Cheng C Yang9b53a622019-08-27 22:13:58 +0800213 (*asserts).erase(path);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800214
215 if (!(*asserts).empty())
216 {
217 return;
218 }
219 if (*assertState == true)
220 {
221 *assertState = false;
222 auto foundCombine = (*combineEvent).find(eventName);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800223 if (foundCombine == (*combineEvent).end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800224 {
225 return;
226 }
227 (*combineEvent).erase(eventName);
Cheng C Yang9b53a622019-08-27 22:13:58 +0800228 if (!deassertMessage.empty())
229 {
230 // Fan Failed has two args
231 std::string sendMessage = eventName + " deassert";
232 if (deassertMessage == "OpenBMC.0.1.PowerSupplyFanRecovered")
233 {
234 sd_journal_send(
235 "MESSAGE=%s", sendMessage.c_str(), "PRIORITY=%i",
236 LOG_ERR, "REDFISH_MESSAGE_ID=%s",
237 deassertMessage.c_str(), "REDFISH_MESSAGE_ARGS=%s,%s",
238 psuName.c_str(), fanName.c_str(), NULL);
239 }
240 else
241 {
242 sd_journal_send(
243 "MESSAGE=%s", sendMessage.c_str(), "PRIORITY=%i",
244 LOG_ERR, "REDFISH_MESSAGE_ID=%s",
245 deassertMessage.c_str(), "REDFISH_MESSAGE_ARGS=%s",
246 psuName.c_str(), NULL);
247 }
248 }
249
Cheng C Yang58b2b532019-05-31 00:19:45 +0800250 if ((*combineEvent).empty())
251 {
252 eventInterface->set_property("functional", true);
253 }
254 }
255 }
256 else
257 {
258 (*asserts).emplace(path);
259 if (*assertState == false)
260 {
261 *assertState = true;
Cheng C Yang9b53a622019-08-27 22:13:58 +0800262 if (!assertMessage.empty())
Cheng C Yang5665db22019-07-12 00:57:30 +0800263 {
264 // Fan Failed has two args
265 std::string sendMessage = eventName + " assert";
Cheng C Yang9b53a622019-08-27 22:13:58 +0800266 if (assertMessage == "OpenBMC.0.1.PowerSupplyFanFailed")
Cheng C Yang5665db22019-07-12 00:57:30 +0800267 {
Cheng C Yang9b53a622019-08-27 22:13:58 +0800268 sd_journal_send(
269 "MESSAGE=%s", sendMessage.c_str(), "PRIORITY=%i",
270 LOG_ERR, "REDFISH_MESSAGE_ID=%s", assertMessage.c_str(),
271 "REDFISH_MESSAGE_ARGS=%s,%s", psuName.c_str(),
272 fanName.c_str(), NULL);
Cheng C Yang5665db22019-07-12 00:57:30 +0800273 }
274 else
275 {
276 sd_journal_send(
277 "MESSAGE=%s", sendMessage.c_str(), "PRIORITY=%i",
Cheng C Yang9b53a622019-08-27 22:13:58 +0800278 LOG_ERR, "REDFISH_MESSAGE_ID=%s", assertMessage.c_str(),
Cheng C Yang5665db22019-07-12 00:57:30 +0800279 "REDFISH_MESSAGE_ARGS=%s", psuName.c_str(), NULL);
280 }
281 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800282 if ((*combineEvent).empty())
283 {
284 eventInterface->set_property("functional", false);
285 }
286 (*combineEvent).emplace(eventName);
287 }
288 }
289 value = newValue;
290}