| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 19 | #include <sdbusplus/bus.hpp> |
| 20 | #include <sdbusplus/bus/match.hpp> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 21 | #include <sdbusplus/exception.hpp> |
| 22 | #include <sdbusplus/message.hpp> |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame^] | 23 | #include <xyz/openbmc_project/ObjectMapper/common.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 24 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 25 | #include <array> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 26 | #include <iostream> |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 27 | #include <set> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 28 | #include <string> |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
| 30 | #include <variant> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 31 | #include <vector> |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 32 | |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame^] | 33 | using ObjectMapper = sdbusplus::common::xyz::openbmc_project::ObjectMapper; |
| 34 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 35 | namespace pid_control |
| 36 | { |
| 37 | |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 38 | namespace properties |
| 39 | { |
| 40 | |
| 41 | constexpr const char* interface = "org.freedesktop.DBus.Properties"; |
| 42 | constexpr const char* get = "Get"; |
| 43 | constexpr const char* getAll = "GetAll"; |
| 44 | |
| 45 | } // namespace properties |
| 46 | |
| 47 | namespace redundancy |
| 48 | { |
| 49 | |
| 50 | constexpr const char* collection = "Collection"; |
| 51 | constexpr const char* status = "Status"; |
| 52 | constexpr const char* interface = "xyz.openbmc_project.Control.FanRedundancy"; |
| 53 | |
| 54 | } // namespace redundancy |
| 55 | |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 56 | DbusPassiveRedundancy::DbusPassiveRedundancy(sdbusplus::bus_t& bus) : |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 57 | match(bus, |
| 58 | "type='signal',member='PropertiesChanged',arg0namespace='" + |
| 59 | std::string(redundancy::interface) + "'", |
| Jayanth Othayoth | 2922eeb | 2024-12-08 07:47:37 -0600 | [diff] [blame] | 60 | |
| 61 | [this](sdbusplus::message_t& message) { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 62 | std::string objectName; |
| 63 | std::unordered_map< |
| 64 | std::string, |
| 65 | std::variant<std::string, std::vector<std::string>>> |
| 66 | result; |
| 67 | try |
| 68 | { |
| 69 | message.read(objectName, result); |
| 70 | } |
| 71 | catch (const sdbusplus::exception_t&) |
| 72 | { |
| 73 | std::cerr << "Error reading match data"; |
| 74 | return; |
| 75 | } |
| 76 | auto findStatus = result.find("Status"); |
| 77 | if (findStatus == result.end()) |
| 78 | { |
| 79 | return; |
| 80 | } |
| 81 | std::string status = std::get<std::string>(findStatus->second); |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 82 | |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 83 | auto methodCall = passiveBus.new_method_call( |
| 84 | message.get_sender(), message.get_path(), |
| 85 | properties::interface, properties::get); |
| 86 | methodCall.append(redundancy::interface, redundancy::collection); |
| 87 | std::variant<std::vector<std::string>> collection; |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 88 | |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 89 | try |
| 90 | { |
| 91 | auto reply = passiveBus.call(methodCall); |
| 92 | reply.read(collection); |
| 93 | } |
| 94 | catch (const sdbusplus::exception_t&) |
| 95 | { |
| 96 | std::cerr << "Error reading match data"; |
| 97 | return; |
| 98 | } |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 99 | |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 100 | auto data = std::get<std::vector<std::string>>(collection); |
| 101 | if (status.rfind("Failed") != std::string::npos) |
| 102 | { |
| 103 | failed.insert(data.begin(), data.end()); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | for (const auto& d : data) |
| 108 | { |
| 109 | failed.erase(d); |
| 110 | } |
| 111 | } |
| Jayanth Othayoth | 2922eeb | 2024-12-08 07:47:37 -0600 | [diff] [blame] | 112 | }), |
| Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 113 | passiveBus(bus) |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 114 | { |
| 115 | populateFailures(); |
| 116 | } |
| 117 | |
| 118 | void DbusPassiveRedundancy::populateFailures(void) |
| 119 | { |
| 120 | auto mapper = passiveBus.new_method_call( |
| Alexander Hansen | 1ec3d13 | 2025-10-27 17:19:50 +0100 | [diff] [blame^] | 121 | ObjectMapper::default_service, ObjectMapper::instance_path, |
| 122 | ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree); |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 123 | mapper.append("/", 0, std::array<const char*, 1>{redundancy::interface}); |
| 124 | std::unordered_map< |
| 125 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 126 | respData; |
| 127 | try |
| 128 | { |
| 129 | auto resp = passiveBus.call(mapper); |
| 130 | resp.read(respData); |
| 131 | } |
| Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 132 | catch (const sdbusplus::exception_t&) |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 133 | { |
| 134 | std::cerr << "Populate Failures Mapper Error\n"; |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * The subtree response looks like: |
| 140 | * {path : |
| 141 | * {busname: |
| 142 | * {interface, interface, interface, ...} |
| 143 | * } |
| 144 | * } |
| 145 | * |
| Manojkiran Eda | 7ca8887 | 2024-06-17 11:55:48 +0530 | [diff] [blame] | 146 | * This loops through this structure to pre-populate the already failed |
| 147 | * items |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 148 | */ |
| 149 | |
| 150 | for (const auto& [path, interfaceDict] : respData) |
| 151 | { |
| 152 | for (const auto& [owner, _] : interfaceDict) |
| 153 | { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 154 | auto call = passiveBus.new_method_call( |
| 155 | owner.c_str(), path.c_str(), properties::interface, |
| 156 | properties::getAll); |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 157 | call.append(redundancy::interface); |
| 158 | |
| 159 | std::unordered_map< |
| 160 | std::string, |
| 161 | std::variant<std::string, std::vector<std::string>>> |
| 162 | getAll; |
| 163 | try |
| 164 | { |
| 165 | auto data = passiveBus.call(call); |
| 166 | data.read(getAll); |
| 167 | } |
| Patrick Williams | 0001ee0 | 2021-10-06 14:44:22 -0500 | [diff] [blame] | 168 | catch (const sdbusplus::exception_t&) |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 169 | { |
| 170 | std::cerr << "Populate Failures Mapper Error\n"; |
| 171 | return; |
| 172 | } |
| 173 | std::string status = |
| 174 | std::get<std::string>(getAll[redundancy::status]); |
| 175 | if (status.rfind("Failed") == std::string::npos) |
| 176 | { |
| 177 | continue; |
| 178 | } |
| 179 | std::vector<std::string> collection = |
| 180 | std::get<std::vector<std::string>>( |
| 181 | getAll[redundancy::collection]); |
| 182 | failed.insert(collection.begin(), collection.end()); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | const std::set<std::string>& DbusPassiveRedundancy::getFailed() |
| 188 | { |
| 189 | return failed; |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // namespace pid_control |