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