blob: 640e587257be9e8d02799b1a46c04996e52c9699 [file] [log] [blame]
James Feist98b704e2019-06-03 16:24:53 -07001/*
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
17#include "dbuspassiveredundancy.hpp"
18
James Feist98b704e2019-06-03 16:24:53 -070019#include <sdbusplus/bus.hpp>
20#include <sdbusplus/bus/match.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070021
22#include <iostream>
James Feist98b704e2019-06-03 16:24:53 -070023#include <set>
24#include <unordered_map>
25#include <variant>
26
Patrick Venturea0764872020-08-08 07:48:43 -070027namespace pid_control
28{
29
James Feist98b704e2019-06-03 16:24:53 -070030namespace properties
31{
32
33constexpr const char* interface = "org.freedesktop.DBus.Properties";
34constexpr const char* get = "Get";
35constexpr const char* getAll = "GetAll";
36
37} // namespace properties
38
39namespace redundancy
40{
41
42constexpr const char* collection = "Collection";
43constexpr const char* status = "Status";
44constexpr const char* interface = "xyz.openbmc_project.Control.FanRedundancy";
45
46} // namespace redundancy
47
Patrick Williamsb228bc32022-07-22 19:26:56 -050048DbusPassiveRedundancy::DbusPassiveRedundancy(sdbusplus::bus_t& bus) :
James Feist98b704e2019-06-03 16:24:53 -070049 match(bus,
50 "type='signal',member='PropertiesChanged',arg0namespace='" +
51 std::string(redundancy::interface) + "'",
Patrick Williamse1dbb592023-10-20 11:19:22 -050052 std::move([this](sdbusplus::message_t& message) {
Patrick Williams8c051122023-05-10 07:50:59 -050053 std::string objectName;
54 std::unordered_map<std::string,
55 std::variant<std::string, std::vector<std::string>>>
56 result;
57 try
58 {
59 message.read(objectName, result);
60 }
61 catch (const sdbusplus::exception_t&)
62 {
63 std::cerr << "Error reading match data";
64 return;
65 }
66 auto findStatus = result.find("Status");
67 if (findStatus == result.end())
68 {
69 return;
70 }
71 std::string status = std::get<std::string>(findStatus->second);
James Feist98b704e2019-06-03 16:24:53 -070072
Patrick Williams8c051122023-05-10 07:50:59 -050073 auto methodCall =
74 passiveBus.new_method_call(message.get_sender(), message.get_path(),
75 properties::interface, properties::get);
76 methodCall.append(redundancy::interface, redundancy::collection);
77 std::variant<std::vector<std::string>> collection;
James Feist98b704e2019-06-03 16:24:53 -070078
Patrick Williams8c051122023-05-10 07:50:59 -050079 try
80 {
81 auto reply = passiveBus.call(methodCall);
82 reply.read(collection);
83 }
84 catch (const sdbusplus::exception_t&)
85 {
86 std::cerr << "Error reading match data";
87 return;
88 }
James Feist98b704e2019-06-03 16:24:53 -070089
Patrick Williams8c051122023-05-10 07:50:59 -050090 auto data = std::get<std::vector<std::string>>(collection);
91 if (status.rfind("Failed") != std::string::npos)
92 {
93 failed.insert(data.begin(), data.end());
94 }
95 else
96 {
97 for (const auto& d : data)
98 {
99 failed.erase(d);
100 }
101 }
Patrick Williamse1dbb592023-10-20 11:19:22 -0500102})),
103 passiveBus(bus)
James Feist98b704e2019-06-03 16:24:53 -0700104{
105 populateFailures();
106}
107
108void DbusPassiveRedundancy::populateFailures(void)
109{
110 auto mapper = passiveBus.new_method_call(
111 "xyz.openbmc_project.ObjectMapper",
112 "/xyz/openbmc_project/object_mapper",
113 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
114 mapper.append("/", 0, std::array<const char*, 1>{redundancy::interface});
115 std::unordered_map<
116 std::string, std::unordered_map<std::string, std::vector<std::string>>>
117 respData;
118 try
119 {
120 auto resp = passiveBus.call(mapper);
121 resp.read(respData);
122 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500123 catch (const sdbusplus::exception_t&)
James Feist98b704e2019-06-03 16:24:53 -0700124 {
125 std::cerr << "Populate Failures Mapper Error\n";
126 return;
127 }
128
129 /*
130 * The subtree response looks like:
131 * {path :
132 * {busname:
133 * {interface, interface, interface, ...}
134 * }
135 * }
136 *
137 * This loops through this structure to pre-poulate the already failed items
138 */
139
140 for (const auto& [path, interfaceDict] : respData)
141 {
142 for (const auto& [owner, _] : interfaceDict)
143 {
144 auto call = passiveBus.new_method_call(owner.c_str(), path.c_str(),
145 properties::interface,
146 properties::getAll);
147 call.append(redundancy::interface);
148
149 std::unordered_map<
150 std::string,
151 std::variant<std::string, std::vector<std::string>>>
152 getAll;
153 try
154 {
155 auto data = passiveBus.call(call);
156 data.read(getAll);
157 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500158 catch (const sdbusplus::exception_t&)
James Feist98b704e2019-06-03 16:24:53 -0700159 {
160 std::cerr << "Populate Failures Mapper Error\n";
161 return;
162 }
163 std::string status =
164 std::get<std::string>(getAll[redundancy::status]);
165 if (status.rfind("Failed") == std::string::npos)
166 {
167 continue;
168 }
169 std::vector<std::string> collection =
170 std::get<std::vector<std::string>>(
171 getAll[redundancy::collection]);
172 failed.insert(collection.begin(), collection.end());
173 }
174 }
175}
176
177const std::set<std::string>& DbusPassiveRedundancy::getFailed()
178{
179 return failed;
Patrick Venturea0764872020-08-08 07:48:43 -0700180}
181
182} // namespace pid_control