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, |
Matthew Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 37 | sdbusplus::bus::bus& 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 | |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 66 | DbusPropertyMap getAllProperties(sdbusplus::bus::bus& bus, |
| 67 | const std::string& path, |
| 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 | |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 91 | DbusSubtree getSubTree(sdbusplus::bus::bus& bus, const std::string& path, |
| 92 | const std::string& interface, int32_t depth) |
| 93 | { |
| 94 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 95 | MAPPER_INTERFACE, "GetSubTree"); |
| 96 | mapperCall.append(path); |
| 97 | mapperCall.append(depth); |
| 98 | mapperCall.append(std::vector<std::string>({interface})); |
| 99 | |
| 100 | auto reply = bus.call(mapperCall); |
| 101 | |
| 102 | DbusSubtree response; |
| 103 | reply.read(response); |
| 104 | return response; |
| 105 | } |
| 106 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 107 | json loadJSONFromFile(const char* path) |
| 108 | { |
| 109 | std::ifstream ifs(path); |
| 110 | if (!ifs.good()) |
| 111 | { |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 112 | log<level::ERR>(std::string("Unable to open file " |
| 113 | "PATH=" + |
| 114 | std::string(path)) |
| 115 | .c_str()); |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 116 | return nullptr; |
| 117 | } |
| 118 | auto data = json::parse(ifs, nullptr, false); |
| 119 | if (data.is_discarded()) |
| 120 | { |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 121 | log<level::ERR>(std::string("Failed to parse json " |
| 122 | "PATH=" + |
| 123 | std::string(path)) |
| 124 | .c_str()); |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 125 | return nullptr; |
| 126 | } |
| 127 | return data; |
| 128 | } |
| 129 | |
Lei YU | 4070546 | 2019-10-09 17:07:11 +0800 | [diff] [blame] | 130 | phosphor::pmbus::Type getPMBusAccessType(const json& json) |
| 131 | { |
| 132 | using namespace phosphor::pmbus; |
| 133 | Type type; |
| 134 | |
| 135 | auto typeStr = json.at("inventoryPMBusAccessType"); |
| 136 | |
| 137 | if (typeStr == "Hwmon") |
| 138 | { |
| 139 | type = Type::Hwmon; |
| 140 | } |
| 141 | else if (typeStr == "DeviceDebug") |
| 142 | { |
| 143 | type = Type::DeviceDebug; |
| 144 | } |
| 145 | else if (typeStr == "Debug") |
| 146 | { |
| 147 | type = Type::Debug; |
| 148 | } |
| 149 | else if (typeStr == "HwmonDeviceDebug") |
| 150 | { |
| 151 | type = Type::HwmonDeviceDebug; |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | type = Type::Base; |
| 156 | } |
| 157 | return type; |
| 158 | } |
| 159 | |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 160 | bool isPoweredOn(sdbusplus::bus::bus& bus, bool defaultState) |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 161 | { |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 162 | int32_t state = defaultState; |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 163 | |
| 164 | try |
| 165 | { |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 166 | // When state = 1, system is powered on |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 167 | auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); |
| 168 | getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus, |
| 169 | state); |
| 170 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 171 | catch (const std::exception& e) |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 172 | { |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 173 | log<level::INFO>("Failed to get power state."); |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 174 | } |
| 175 | return state != 0; |
| 176 | } |
| 177 | |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 178 | std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus) |
| 179 | { |
| 180 | std::vector<std::string> paths; |
| 181 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 182 | MAPPER_INTERFACE, "GetSubTreePaths"); |
| 183 | method.append(INVENTORY_OBJ_PATH); |
| 184 | method.append(0); // Depth 0 to search all |
| 185 | method.append(std::vector<std::string>({PSU_INVENTORY_IFACE})); |
| 186 | auto reply = bus.call(method); |
| 187 | |
| 188 | reply.read(paths); |
| 189 | return paths; |
| 190 | } |
| 191 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 192 | } // namespace util |
| 193 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 194 | } // namespace phosphor |