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 <fstream> 19 20 namespace phosphor 21 { 22 namespace power 23 { 24 namespace util 25 { 26 27 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; 28 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; 29 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; 30 31 using namespace phosphor::logging; 32 using json = nlohmann::json; 33 34 std::string getService(const std::string& path, const std::string& interface, 35 sdbusplus::bus::bus& bus) 36 { 37 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, 38 MAPPER_INTERFACE, "GetObject"); 39 40 method.append(path); 41 method.append(std::vector<std::string>({interface})); 42 43 auto reply = bus.call(method); 44 45 std::map<std::string, std::vector<std::string>> response; 46 reply.read(response); 47 48 if (response.empty()) 49 { 50 log<level::ERR>("Error in mapper response for getting service name", 51 entry("PATH=%s", path.c_str()), 52 entry("INTERFACE=%s", interface.c_str())); 53 return std::string{}; 54 } 55 56 return response.begin()->first; 57 } 58 59 json loadJSONFromFile(const char* path) 60 { 61 std::ifstream ifs(path); 62 if (!ifs.good()) 63 { 64 log<level::ERR>("Unable to open file", entry("PATH=%s", path)); 65 return nullptr; 66 } 67 auto data = json::parse(ifs, nullptr, false); 68 if (data.is_discarded()) 69 { 70 log<level::ERR>("Failed to parse json", entry("PATH=%s", path)); 71 return nullptr; 72 } 73 return data; 74 } 75 76 phosphor::pmbus::Type getPMBusAccessType(const json& json) 77 { 78 using namespace phosphor::pmbus; 79 Type type; 80 81 auto typeStr = json.at("inventoryPMBusAccessType"); 82 83 if (typeStr == "Hwmon") 84 { 85 type = Type::Hwmon; 86 } 87 else if (typeStr == "DeviceDebug") 88 { 89 type = Type::DeviceDebug; 90 } 91 else if (typeStr == "Debug") 92 { 93 type = Type::Debug; 94 } 95 else if (typeStr == "HwmonDeviceDebug") 96 { 97 type = Type::HwmonDeviceDebug; 98 } 99 else 100 { 101 type = Type::Base; 102 } 103 return type; 104 } 105 106 } // namespace util 107 } // namespace power 108 } // namespace phosphor 109