blob: 4d54ffe24441891e08885c909877a7470067a000 [file] [log] [blame]
James Feist03a02ad2019-02-06 12:38:50 -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
James Feistdc7bbdc2019-05-16 15:33:06 -070017#include "callback_manager.hpp"
18
Ed Tanousa2441982023-02-28 13:38:56 -080019#include <boost/asio/io_context.hpp>
Zhikui Ren291d6382020-09-25 15:06:04 -070020#include <boost/asio/steady_timer.hpp>
James Feist03a02ad2019-02-06 12:38:50 -080021#include <boost/container/flat_map.hpp>
James Feist03a02ad2019-02-06 12:38:50 -080022#include <sdbusplus/asio/connection.hpp>
23#include <sdbusplus/asio/object_server.hpp>
Jason M. Bills0d5c0712020-12-08 10:10:47 -080024
James Feist03a02ad2019-02-06 12:38:50 -080025#include <variant>
26
27constexpr const char* fatalLedPath =
28 "/xyz/openbmc_project/led/groups/status_critical";
29constexpr const char* criticalLedPath =
30 "/xyz/openbmc_project/led/groups/status_non_critical";
31constexpr const char* warningLedPath =
32 "/xyz/openbmc_project/led/groups/status_degraded";
James Feiste409fc72019-03-05 12:19:43 -080033constexpr const char* okLedPath = "/xyz/openbmc_project/led/groups/status_ok";
James Feist03a02ad2019-02-06 12:38:50 -080034
35constexpr const char* ledIface = "xyz.openbmc_project.Led.Group";
36constexpr const char* ledAssertProp = "Asserted";
37constexpr const char* ledManagerBusname =
38 "xyz.openbmc_project.LED.GroupManager";
39
James Feistdc7bbdc2019-05-16 15:33:06 -070040std::unique_ptr<AssociationManager> associationManager;
41
James Feiste409fc72019-03-05 12:19:43 -080042enum class StatusSetting
43{
44 none,
45 ok,
46 warn,
47 critical,
48 fatal
49};
50
James Feist03a02ad2019-02-06 12:38:50 -080051constexpr const bool debug = false;
52
53// final led state tracking
James Feiste409fc72019-03-05 12:19:43 -080054StatusSetting currentPriority = StatusSetting::none;
James Feist03a02ad2019-02-06 12:38:50 -080055
56// maps of <object-path, <property, asserted>>
57boost::container::flat_map<std::string,
58 boost::container::flat_map<std::string, bool>>
59 fatalAssertMap;
60boost::container::flat_map<std::string,
61 boost::container::flat_map<std::string, bool>>
62 criticalAssertMap;
63boost::container::flat_map<std::string,
64 boost::container::flat_map<std::string, bool>>
65 warningAssertMap;
66
67std::vector<std::string> assertedInMap(
68 const boost::container::flat_map<
69 std::string, boost::container::flat_map<std::string, bool>>& map)
70{
71 std::vector<std::string> ret;
72 // if any of the properties are true, return true
73 for (const auto& pair : map)
74 {
75 for (const auto& item : pair.second)
76 {
77 if (item.second)
78 {
79 ret.push_back(pair.first);
80 }
81 }
82 }
83 return ret;
84}
85
Richard Marian Thomaiyar28161212019-04-13 21:11:23 +053086void updateLedStatus(std::shared_ptr<sdbusplus::asio::connection>& conn,
87 bool forceRefresh = false)
James Feist03a02ad2019-02-06 12:38:50 -080088{
James Feistdc7bbdc2019-05-16 15:33:06 -070089 std::vector<std::string> fatalVector = assertedInMap(fatalAssertMap);
90 bool fatal = fatalVector.size();
James Feist03a02ad2019-02-06 12:38:50 -080091
James Feistdc7bbdc2019-05-16 15:33:06 -070092 std::vector<std::string> criticalVector = assertedInMap(criticalAssertMap);
93 bool critical = criticalVector.size();
James Feist03a02ad2019-02-06 12:38:50 -080094
James Feistdc7bbdc2019-05-16 15:33:06 -070095 std::vector<std::string> warningVector = assertedInMap(warningAssertMap);
96 bool warn = warningVector.size();
James Feist03a02ad2019-02-06 12:38:50 -080097
James Feistdc7bbdc2019-05-16 15:33:06 -070098 associationManager->setLocalAssociations(fatalVector, criticalVector,
99 warningVector);
James Feiste409fc72019-03-05 12:19:43 -0800100
101 StatusSetting last = currentPriority;
sunitakx1a9dde92021-06-28 13:42:18 +0000102 std::vector<std::pair<std::string, std::variant<bool>>> ledsToSet;
103 if (forceRefresh)
104 {
105 ledsToSet.push_back(std::make_pair(fatalLedPath, false));
106 ledsToSet.push_back(std::make_pair(criticalLedPath, false));
107 ledsToSet.push_back(std::make_pair(warningLedPath, false));
108 ledsToSet.push_back(std::make_pair(okLedPath, false));
109 for (const auto& ledPair : ledsToSet)
110 {
111 conn->async_method_call(
112 [ledPair](const boost::system::error_code ec) {
PavanKumarInteldf992902023-11-29 09:09:06 +0000113 std::ios_base::fmtflags originalFlags = std::cerr.flags();
sunitakx1a9dde92021-06-28 13:42:18 +0000114 if (ec)
115 {
116 std::cerr << "Cannot set " << ledPair.first << " to "
117 << std::boolalpha
118 << std::get<bool>(ledPair.second) << "\n";
PavanKumarIntel8e0b9db2023-10-30 16:50:27 +0000119 std::cerr.flags(originalFlags);
sunitakx1a9dde92021-06-28 13:42:18 +0000120 }
121 if constexpr (debug)
122 {
123 std::cerr << "Set " << ledPair.first << " to "
124 << std::boolalpha
125 << std::get<bool>(ledPair.second) << "\n";
PavanKumarIntel8e0b9db2023-10-30 16:50:27 +0000126 std::cerr.flags(originalFlags);
sunitakx1a9dde92021-06-28 13:42:18 +0000127 }
128 },
129 ledManagerBusname, ledPair.first,
130 "org.freedesktop.DBus.Properties", "Set", ledIface,
131 ledAssertProp, ledPair.second);
132 }
133 }
James Feiste409fc72019-03-05 12:19:43 -0800134 if (fatal)
James Feist03a02ad2019-02-06 12:38:50 -0800135 {
James Feiste409fc72019-03-05 12:19:43 -0800136 currentPriority = StatusSetting::fatal;
137 }
138 else if (critical)
139 {
140 currentPriority = StatusSetting::critical;
141 }
142 else if (warn)
143 {
144 currentPriority = StatusSetting::warn;
145 }
146 else
147 {
148 currentPriority = StatusSetting::ok;
149 }
150
Richard Marian Thomaiyar28161212019-04-13 21:11:23 +0530151 if (last != currentPriority || forceRefresh)
James Feiste409fc72019-03-05 12:19:43 -0800152 {
153 switch (currentPriority)
154 {
155 case (StatusSetting::fatal):
156 {
157 ledsToSet.push_back(std::make_pair(fatalLedPath, true));
158 ledsToSet.push_back(std::make_pair(criticalLedPath, false));
159 ledsToSet.push_back(std::make_pair(warningLedPath, false));
160 ledsToSet.push_back(std::make_pair(okLedPath, false));
161 break;
162 }
163 case (StatusSetting::critical):
164 {
165 ledsToSet.push_back(std::make_pair(fatalLedPath, false));
166 ledsToSet.push_back(std::make_pair(criticalLedPath, true));
167 ledsToSet.push_back(std::make_pair(warningLedPath, false));
168 ledsToSet.push_back(std::make_pair(okLedPath, false));
169 break;
170 }
171 case (StatusSetting::warn):
172 {
173 ledsToSet.push_back(std::make_pair(fatalLedPath, false));
174 ledsToSet.push_back(std::make_pair(criticalLedPath, false));
175 ledsToSet.push_back(std::make_pair(warningLedPath, true));
176 ledsToSet.push_back(std::make_pair(okLedPath, false));
177 break;
178 }
179 case (StatusSetting::ok):
180 {
181 ledsToSet.push_back(std::make_pair(fatalLedPath, false));
182 ledsToSet.push_back(std::make_pair(criticalLedPath, false));
183 ledsToSet.push_back(std::make_pair(warningLedPath, false));
184 ledsToSet.push_back(std::make_pair(okLedPath, true));
185 break;
186 }
Jason M. Bills69ae8082025-08-01 08:53:34 -0700187 case (StatusSetting::none):
188 default:
189 break;
James Feiste409fc72019-03-05 12:19:43 -0800190 }
James Feist03a02ad2019-02-06 12:38:50 -0800191 }
192
193 for (const auto& ledPair : ledsToSet)
194 {
195 conn->async_method_call(
196 [ledPair](const boost::system::error_code ec) {
197 if (ec)
198 {
199 std::cerr << "Cannot set " << ledPair.first << " to "
200 << std::boolalpha
201 << std::get<bool>(ledPair.second) << "\n";
202 }
203 if constexpr (debug)
204 {
Jason M. Bills2b973a52025-07-31 11:06:04 -0700205 std::cerr
206 << "Set " << ledPair.first << " to " << std::boolalpha
207 << std::get<bool>(ledPair.second) << "\n";
James Feist03a02ad2019-02-06 12:38:50 -0800208 }
209 },
210 ledManagerBusname, ledPair.first, "org.freedesktop.DBus.Properties",
211 "Set", ledIface, ledAssertProp, ledPair.second);
212 }
213}
214
215void createThresholdMatch(std::shared_ptr<sdbusplus::asio::connection>& conn)
216{
Patrick Williamsff1c36e2022-07-22 19:26:56 -0500217 static sdbusplus::bus::match_t match(
218 static_cast<sdbusplus::bus_t&>(*conn),
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000219 "type='signal',member='ThresholdAsserted'",
Patrick Williamsff1c36e2022-07-22 19:26:56 -0500220 [&conn](sdbusplus::message_t& message) {
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000221 std::string sensorName;
222 std::string thresholdInterface;
223 std::string event;
224 bool assert;
225 double assertValue;
James Feist03a02ad2019-02-06 12:38:50 -0800226
James Feist7b238402019-06-25 11:56:49 -0700227 try
228 {
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000229 message.read(sensorName, thresholdInterface, event, assert,
230 assertValue);
James Feist7b238402019-06-25 11:56:49 -0700231 }
232 catch (sdbusplus::exception_t&)
233 {
234 return;
235 }
James Feist03a02ad2019-02-06 12:38:50 -0800236 if constexpr (debug)
237 {
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000238 std::cerr << "Threshold callback: SensorName = " << sensorName
239 << ", Event = " << event << ", Asserted = " << assert
James Feist03a02ad2019-02-06 12:38:50 -0800240 << "\n";
241 }
242
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000243 if (event == "CriticalAlarmLow")
James Feist03a02ad2019-02-06 12:38:50 -0800244 {
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000245 criticalAssertMap[message.get_path()]["low"] = assert;
James Feist03a02ad2019-02-06 12:38:50 -0800246 }
sunitakxf1a39982021-11-25 14:01:44 +0000247 else if (event == "CriticalAlarmHigh")
James Feist03a02ad2019-02-06 12:38:50 -0800248 {
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000249 criticalAssertMap[message.get_path()]["high"] = assert;
James Feist03a02ad2019-02-06 12:38:50 -0800250 }
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000251 else if (event == "WarningAlarmLow")
James Feist03a02ad2019-02-06 12:38:50 -0800252 {
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000253 warningAssertMap[message.get_path()]["low"] = assert;
James Feist03a02ad2019-02-06 12:38:50 -0800254 }
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000255 else if (event == "WarningAlarmHigh")
James Feist03a02ad2019-02-06 12:38:50 -0800256 {
Chalapathi Venkataramashettyd0b776e2021-09-30 11:01:52 +0000257 warningAssertMap[message.get_path()]["high"] = assert;
James Feist03a02ad2019-02-06 12:38:50 -0800258 }
James Feist03a02ad2019-02-06 12:38:50 -0800259
James Feist7b238402019-06-25 11:56:49 -0700260 associationManager->setSensorAssociations(
261 assertedInMap(criticalAssertMap),
262 assertedInMap(warningAssertMap));
263
264 updateLedStatus(conn);
265 });
266}
267
268void createAssociationMatch(std::shared_ptr<sdbusplus::asio::connection>& conn)
269{
Patrick Williamsff1c36e2022-07-22 19:26:56 -0500270 static sdbusplus::bus::match_t match(
271 static_cast<sdbusplus::bus_t&>(*conn),
James Feiste409fc72019-03-05 12:19:43 -0800272 "type='signal',interface='org.freedesktop.DBus.Properties',"
James Feist7b238402019-06-25 11:56:49 -0700273 "arg0namespace='" +
274 std::string(associationIface) + "'",
Patrick Williamsff1c36e2022-07-22 19:26:56 -0500275 [&conn](sdbusplus::message_t& message) {
James Feist7b238402019-06-25 11:56:49 -0700276 if (message.get_path() == rootPath)
277 {
278 return; // it's us
279 }
280 std::string objectName;
281 boost::container::flat_map<std::string,
282 std::variant<std::vector<Association>>>
283 values;
284 try
285 {
286 message.read(objectName, values);
287 }
288 catch (sdbusplus::exception_t&)
289 {
290 return;
291 }
292
293 if constexpr (debug)
294 {
295 std::cerr << "Association callback " << message.get_path()
296 << "\n";
297 }
298
James Feist56078402019-10-29 16:00:38 -0700299 auto findAssociations = values.find("Associations");
James Feist7b238402019-06-25 11:56:49 -0700300 if (findAssociations == values.end())
301 {
302 return;
303 }
304 const std::vector<Association>* associations =
305 std::get_if<std::vector<Association>>(
306 &findAssociations->second);
307
308 if (associations == nullptr)
309 {
310 std::cerr << "Illegal Association on " << message.get_path()
311 << "\n";
312 return;
313 }
314
315 bool localWarning = false;
316 bool localCritical = false;
317 bool globalWarning = false;
318 bool globalCritical = false;
319
320 for (const auto& [forward, reverse, path] : *associations)
321 {
322 if (path == rootPath)
323 {
324 globalWarning = globalWarning ? true : reverse == "warning";
325 globalCritical =
326 globalCritical ? true : reverse == "critical";
327
328 if constexpr (1)
329 {
330 std::cerr << "got global ";
331 }
332 }
333 else
334 {
335 localWarning = localWarning ? true : reverse == "warning";
336 localCritical =
337 localCritical ? true : reverse == "critical";
338 }
339 if (globalCritical && localCritical)
340 {
341 break;
342 }
343 }
344
345 bool fatal = globalCritical && localCritical;
346 bool critical = globalWarning && localCritical;
James Feist56078402019-10-29 16:00:38 -0700347 bool warning = globalWarning && !critical;
James Feist7b238402019-06-25 11:56:49 -0700348
349 fatalAssertMap[message.get_path()]["association"] = fatal;
350 criticalAssertMap[message.get_path()]["association"] = critical;
351 warningAssertMap[message.get_path()]["association"] = warning;
352
353 updateLedStatus(conn);
354 });
James Feist03a02ad2019-02-06 12:38:50 -0800355}
356
Jason M. Bills69ae8082025-08-01 08:53:34 -0700357int main(int /*argc*/, char** /*argv*/)
James Feist03a02ad2019-02-06 12:38:50 -0800358{
Ed Tanousa2441982023-02-28 13:38:56 -0800359 boost::asio::io_context io;
James Feist03a02ad2019-02-06 12:38:50 -0800360 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
361 conn->request_name("xyz.openbmc_project.CallbackManager");
362 sdbusplus::asio::object_server objServer(conn);
James Feistdc7bbdc2019-05-16 15:33:06 -0700363 std::shared_ptr<sdbusplus::asio::dbus_interface> rootIface =
364 objServer.add_interface(rootPath,
James Feist03a02ad2019-02-06 12:38:50 -0800365 "xyz.openbmc_project.CallbackManager");
Jason M. Bills2b973a52025-07-31 11:06:04 -0700366 rootIface->register_method("RetriggerLEDUpdate", [&conn]() {
367 updateLedStatus(conn, true);
368 });
James Feistdc7bbdc2019-05-16 15:33:06 -0700369 rootIface->initialize();
370
371 std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
372 objServer.add_interface(rootPath, globalInventoryIface);
373 inventoryIface->initialize();
374
375 associationManager = std::make_unique<AssociationManager>(objServer, conn);
James Feist03a02ad2019-02-06 12:38:50 -0800376
377 createThresholdMatch(conn);
James Feist7b238402019-06-25 11:56:49 -0700378 createAssociationMatch(conn);
James Feiste409fc72019-03-05 12:19:43 -0800379 updateLedStatus(conn);
James Feist03a02ad2019-02-06 12:38:50 -0800380
381 io.run();
382
383 return 0;
384}