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