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 18 #include "types.hpp" 19 20 #include <fstream> 21 22 namespace phosphor 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; 34 using json = nlohmann::json; 35 36 std::string getService(const std::string& path, const std::string& interface, 37 sdbusplus::bus_t& bus, bool logError) 38 { 39 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, 40 MAPPER_INTERFACE, "GetObject"); 41 42 method.append(path); 43 method.append(std::vector<std::string>({interface})); 44 45 auto reply = bus.call(method); 46 47 std::map<std::string, std::vector<std::string>> response; 48 reply.read(response); 49 50 if (response.empty()) 51 { 52 if (logError) 53 { 54 log<level::ERR>( 55 std::string("Error in mapper response for getting service name " 56 "PATH=" + 57 path + " INTERFACE=" + interface) 58 .c_str()); 59 } 60 return std::string{}; 61 } 62 63 return response.begin()->first; 64 } 65 66 DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path, 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 90 DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path, 91 const std::string& interface, int32_t depth) 92 { 93 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, 94 MAPPER_INTERFACE, "GetSubTree"); 95 mapperCall.append(path); 96 mapperCall.append(depth); 97 mapperCall.append(std::vector<std::string>({interface})); 98 99 auto reply = bus.call(mapperCall); 100 101 DbusSubtree response; 102 reply.read(response); 103 return response; 104 } 105 106 std::vector<DbusPath> getAssociatedSubTreePaths( 107 sdbusplus::bus_t& bus, 108 const sdbusplus::message::object_path& associationPath, 109 const sdbusplus::message::object_path& path, 110 const std::vector<std::string>& interfaces, int32_t depth) 111 { 112 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, 113 MAPPER_INTERFACE, 114 "GetAssociatedSubTreePaths"); 115 mapperCall.append(associationPath); 116 mapperCall.append(path); 117 mapperCall.append(depth); 118 mapperCall.append(interfaces); 119 120 auto reply = bus.call(mapperCall); 121 122 std::vector<DbusPath> response; 123 reply.read(response); 124 return response; 125 } 126 127 json loadJSONFromFile(const char* path) 128 { 129 std::ifstream ifs(path); 130 if (!ifs.good()) 131 { 132 log<level::ERR>(std::string("Unable to open file " 133 "PATH=" + 134 std::string(path)) 135 .c_str()); 136 return nullptr; 137 } 138 auto data = json::parse(ifs, nullptr, false); 139 if (data.is_discarded()) 140 { 141 log<level::ERR>(std::string("Failed to parse json " 142 "PATH=" + 143 std::string(path)) 144 .c_str()); 145 return nullptr; 146 } 147 return data; 148 } 149 150 phosphor::pmbus::Type getPMBusAccessType(const json& json) 151 { 152 using namespace phosphor::pmbus; 153 Type type; 154 155 auto typeStr = json.at("inventoryPMBusAccessType"); 156 157 if (typeStr == "Hwmon") 158 { 159 type = Type::Hwmon; 160 } 161 else if (typeStr == "DeviceDebug") 162 { 163 type = Type::DeviceDebug; 164 } 165 else if (typeStr == "Debug") 166 { 167 type = Type::Debug; 168 } 169 else if (typeStr == "HwmonDeviceDebug") 170 { 171 type = Type::HwmonDeviceDebug; 172 } 173 else 174 { 175 type = Type::Base; 176 } 177 return type; 178 } 179 180 bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState) 181 { 182 int32_t state = defaultState; 183 184 try 185 { 186 // When state = 1, system is powered on 187 auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); 188 getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus, 189 state); 190 } 191 catch (const std::exception& e) 192 { 193 log<level::INFO>("Failed to get power state."); 194 } 195 return state != 0; 196 } 197 198 std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus) 199 { 200 std::vector<std::string> paths; 201 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, 202 MAPPER_INTERFACE, "GetSubTreePaths"); 203 method.append(INVENTORY_OBJ_PATH); 204 method.append(0); // Depth 0 to search all 205 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE})); 206 auto reply = bus.call(method); 207 208 reply.read(paths); 209 return paths; 210 } 211 212 } // namespace util 213 } // namespace power 214 } // namespace phosphor 215