Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM 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 | #include "utility.hpp" |
| 17 | |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 18 | #include "types.hpp" |
| 19 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 20 | #include <fstream> |
| 21 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 22 | namespace phosphor |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 23 | { |
| 24 | namespace power |
| 25 | { |
| 26 | namespace util |
| 27 | { |
| 28 | |
| 29 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 30 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 31 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 32 | |
Faisal Awada | f661ecb | 2025-07-18 14:03:18 -0500 | [diff] [blame^] | 33 | constexpr auto DECORATOR_CHASSIS_ID = |
| 34 | "xyz.openbmc_project.Inventory.Decorator.Slot"; |
| 35 | constexpr auto CHASSIS_ID_PROPERTY = "SlotNumber"; |
| 36 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 37 | using json = nlohmann::json; |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 38 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 39 | std::string getService(const std::string& path, const std::string& interface, |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 40 | sdbusplus::bus_t& bus, bool logError) |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 41 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 42 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 43 | MAPPER_INTERFACE, "GetObject"); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 44 | |
| 45 | method.append(path); |
| 46 | method.append(std::vector<std::string>({interface})); |
| 47 | |
| 48 | auto reply = bus.call(method); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 49 | |
| 50 | std::map<std::string, std::vector<std::string>> response; |
| 51 | reply.read(response); |
| 52 | |
| 53 | if (response.empty()) |
| 54 | { |
Matthew Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 55 | if (logError) |
| 56 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 57 | lg2::error("Error in mapper response for getting service name " |
| 58 | "PATH={PATH} INTERFACE={INTERFACE}", |
| 59 | "PATH", path, "INTERFACE", interface); |
Matthew Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 60 | } |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 61 | return std::string{}; |
| 62 | } |
| 63 | |
| 64 | return response.begin()->first; |
| 65 | } |
| 66 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 67 | DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path, |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 68 | const std::string& interface, |
| 69 | const std::string& service) |
| 70 | { |
| 71 | DbusPropertyMap properties; |
| 72 | |
| 73 | auto serviceStr = service; |
| 74 | if (serviceStr.empty()) |
| 75 | { |
| 76 | serviceStr = getService(path, interface, bus); |
| 77 | if (serviceStr.empty()) |
| 78 | { |
| 79 | return properties; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | auto method = bus.new_method_call(serviceStr.c_str(), path.c_str(), |
| 84 | PROPERTY_INTF, "GetAll"); |
| 85 | method.append(interface); |
| 86 | auto reply = bus.call(method); |
| 87 | reply.read(properties); |
| 88 | return properties; |
| 89 | } |
| 90 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 91 | DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path, |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 92 | const std::string& interface, int32_t depth) |
| 93 | { |
Shawn McCarney | 8270b7d | 2023-06-26 11:35:51 -0500 | [diff] [blame] | 94 | return getSubTree(bus, path, std::vector<std::string>({interface}), depth); |
| 95 | } |
| 96 | |
| 97 | DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path, |
| 98 | const std::vector<std::string>& interfaces, |
| 99 | int32_t depth) |
| 100 | { |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 101 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 102 | MAPPER_INTERFACE, "GetSubTree"); |
| 103 | mapperCall.append(path); |
| 104 | mapperCall.append(depth); |
Shawn McCarney | 8270b7d | 2023-06-26 11:35:51 -0500 | [diff] [blame] | 105 | mapperCall.append(interfaces); |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 106 | |
| 107 | auto reply = bus.call(mapperCall); |
| 108 | |
| 109 | DbusSubtree response; |
| 110 | reply.read(response); |
| 111 | return response; |
| 112 | } |
| 113 | |
Matt Spinler | 0659422 | 2023-05-01 10:44:43 -0500 | [diff] [blame] | 114 | std::vector<DbusPath> getAssociatedSubTreePaths( |
| 115 | sdbusplus::bus_t& bus, |
| 116 | const sdbusplus::message::object_path& associationPath, |
| 117 | const sdbusplus::message::object_path& path, |
| 118 | const std::vector<std::string>& interfaces, int32_t depth) |
| 119 | { |
Patrick Williams | f540219 | 2024-08-16 15:20:53 -0400 | [diff] [blame] | 120 | auto mapperCall = |
| 121 | bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE, |
| 122 | "GetAssociatedSubTreePaths"); |
Matt Spinler | 0659422 | 2023-05-01 10:44:43 -0500 | [diff] [blame] | 123 | mapperCall.append(associationPath); |
| 124 | mapperCall.append(path); |
| 125 | mapperCall.append(depth); |
| 126 | mapperCall.append(interfaces); |
| 127 | |
| 128 | auto reply = bus.call(mapperCall); |
| 129 | |
| 130 | std::vector<DbusPath> response; |
| 131 | reply.read(response); |
| 132 | return response; |
| 133 | } |
| 134 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 135 | json loadJSONFromFile(const char* path) |
| 136 | { |
| 137 | std::ifstream ifs(path); |
| 138 | if (!ifs.good()) |
| 139 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 140 | lg2::error("Unable to open file PATH={PATH}", "PATH", path); |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 141 | return nullptr; |
| 142 | } |
| 143 | auto data = json::parse(ifs, nullptr, false); |
| 144 | if (data.is_discarded()) |
| 145 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 146 | lg2::error("Failed to parse json PATH={PATH}", "PATH", path); |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 147 | return nullptr; |
| 148 | } |
| 149 | return data; |
| 150 | } |
| 151 | |
Lei YU | 4070546 | 2019-10-09 17:07:11 +0800 | [diff] [blame] | 152 | phosphor::pmbus::Type getPMBusAccessType(const json& json) |
| 153 | { |
| 154 | using namespace phosphor::pmbus; |
| 155 | Type type; |
| 156 | |
| 157 | auto typeStr = json.at("inventoryPMBusAccessType"); |
| 158 | |
| 159 | if (typeStr == "Hwmon") |
| 160 | { |
| 161 | type = Type::Hwmon; |
| 162 | } |
| 163 | else if (typeStr == "DeviceDebug") |
| 164 | { |
| 165 | type = Type::DeviceDebug; |
| 166 | } |
| 167 | else if (typeStr == "Debug") |
| 168 | { |
| 169 | type = Type::Debug; |
| 170 | } |
| 171 | else if (typeStr == "HwmonDeviceDebug") |
| 172 | { |
| 173 | type = Type::HwmonDeviceDebug; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | type = Type::Base; |
| 178 | } |
| 179 | return type; |
| 180 | } |
| 181 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 182 | bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState) |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 183 | { |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 184 | int32_t state = defaultState; |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 185 | |
| 186 | try |
| 187 | { |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 188 | // When state = 1, system is powered on |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 189 | auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); |
| 190 | getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus, |
| 191 | state); |
| 192 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 193 | catch (const std::exception& e) |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 194 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 195 | lg2::info("Failed to get power state."); |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 196 | } |
| 197 | return state != 0; |
| 198 | } |
| 199 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 200 | std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus) |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 201 | { |
| 202 | std::vector<std::string> paths; |
| 203 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 204 | MAPPER_INTERFACE, "GetSubTreePaths"); |
| 205 | method.append(INVENTORY_OBJ_PATH); |
| 206 | method.append(0); // Depth 0 to search all |
| 207 | method.append(std::vector<std::string>({PSU_INVENTORY_IFACE})); |
| 208 | auto reply = bus.call(method); |
| 209 | |
| 210 | reply.read(paths); |
| 211 | return paths; |
| 212 | } |
| 213 | |
Faisal Awada | f661ecb | 2025-07-18 14:03:18 -0500 | [diff] [blame^] | 214 | std::vector<std::string> getChassisInventoryPaths(sdbusplus::bus_t& bus) |
| 215 | { |
| 216 | std::vector<std::string> paths; |
| 217 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 218 | MAPPER_INTERFACE, "GetSubTreePaths"); |
| 219 | method.append(INVENTORY_OBJ_PATH); |
| 220 | method.append(0); // Depth 0 to search all |
| 221 | method.append(std::vector<std::string>({CHASSIS_IFACE})); |
| 222 | auto reply = bus.call(method); |
| 223 | |
| 224 | reply.read(paths); |
| 225 | return paths; |
| 226 | } |
| 227 | |
| 228 | uint64_t getChassisInventoryUniqueId(sdbusplus::bus_t& bus, |
| 229 | const std::string& path) |
| 230 | { |
| 231 | uint32_t chassisId; |
| 232 | getProperty(DECORATOR_CHASSIS_ID, CHASSIS_ID_PROPERTY, path, |
| 233 | INVENTORY_MGR_IFACE, bus, chassisId); |
| 234 | return static_cast<uint64_t>(chassisId); |
| 235 | } |
| 236 | |
| 237 | uint64_t getParentEMUniqueId(sdbusplus::bus_t& bus, const std::string& path) |
| 238 | { |
| 239 | namespace fs = std::filesystem; |
| 240 | uint64_t chassisId; |
| 241 | fs::path fspath(path); |
| 242 | getProperty(DECORATOR_CHASSIS_ID, CHASSIS_ID_PROPERTY, fspath.parent_path(), |
| 243 | ENTITY_MGR_SERVICE, bus, chassisId); |
| 244 | return chassisId; |
| 245 | } |
| 246 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 247 | } // namespace util |
| 248 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 249 | } // namespace phosphor |