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::bus& 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 json loadJSONFromFile(const char* path) 67 { 68 std::ifstream ifs(path); 69 if (!ifs.good()) 70 { 71 log<level::ERR>(std::string("Unable to open file " 72 "PATH=" + 73 std::string(path)) 74 .c_str()); 75 return nullptr; 76 } 77 auto data = json::parse(ifs, nullptr, false); 78 if (data.is_discarded()) 79 { 80 log<level::ERR>(std::string("Failed to parse json " 81 "PATH=" + 82 std::string(path)) 83 .c_str()); 84 return nullptr; 85 } 86 return data; 87 } 88 89 phosphor::pmbus::Type getPMBusAccessType(const json& json) 90 { 91 using namespace phosphor::pmbus; 92 Type type; 93 94 auto typeStr = json.at("inventoryPMBusAccessType"); 95 96 if (typeStr == "Hwmon") 97 { 98 type = Type::Hwmon; 99 } 100 else if (typeStr == "DeviceDebug") 101 { 102 type = Type::DeviceDebug; 103 } 104 else if (typeStr == "Debug") 105 { 106 type = Type::Debug; 107 } 108 else if (typeStr == "HwmonDeviceDebug") 109 { 110 type = Type::HwmonDeviceDebug; 111 } 112 else 113 { 114 type = Type::Base; 115 } 116 return type; 117 } 118 119 bool isPoweredOn(sdbusplus::bus::bus& bus, bool defaultState) 120 { 121 int32_t state = defaultState; 122 123 try 124 { 125 // When state = 1, system is powered on 126 auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); 127 getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus, 128 state); 129 } 130 catch (std::exception& e) 131 { 132 log<level::INFO>("Failed to get power state."); 133 } 134 return state != 0; 135 } 136 137 std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus) 138 { 139 std::vector<std::string> paths; 140 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, 141 MAPPER_INTERFACE, "GetSubTreePaths"); 142 method.append(INVENTORY_OBJ_PATH); 143 method.append(0); // Depth 0 to search all 144 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE})); 145 auto reply = bus.call(method); 146 147 reply.read(paths); 148 return paths; 149 } 150 151 } // namespace util 152 } // namespace power 153 } // namespace phosphor 154