xref: /openbmc/phosphor-power/utility.cpp (revision f54021972b91be5058b50e9046bb0dd5a3b22a80)
1974c916eSMatt Spinler /**
2974c916eSMatt Spinler  * Copyright © 2017 IBM Corporation
3974c916eSMatt Spinler  *
4974c916eSMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5974c916eSMatt Spinler  * you may not use this file except in compliance with the License.
6974c916eSMatt Spinler  * You may obtain a copy of the License at
7974c916eSMatt Spinler  *
8974c916eSMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9974c916eSMatt Spinler  *
10974c916eSMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11974c916eSMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12974c916eSMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13974c916eSMatt Spinler  * See the License for the specific language governing permissions and
14974c916eSMatt Spinler  * limitations under the License.
15974c916eSMatt Spinler  */
16974c916eSMatt Spinler #include "utility.hpp"
17974c916eSMatt Spinler 
18cfc040c7SLei YU #include "types.hpp"
19cfc040c7SLei YU 
207dc31bb1SLei YU #include <fstream>
217dc31bb1SLei YU 
22ab093328SLei YU namespace phosphor
23974c916eSMatt Spinler {
24974c916eSMatt Spinler namespace power
25974c916eSMatt Spinler {
26974c916eSMatt Spinler namespace util
27974c916eSMatt Spinler {
28974c916eSMatt Spinler 
29974c916eSMatt Spinler constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
30974c916eSMatt Spinler constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
31974c916eSMatt Spinler constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
32974c916eSMatt Spinler 
33974c916eSMatt Spinler using namespace phosphor::logging;
347dc31bb1SLei YU using json = nlohmann::json;
3548b4a430SMatt Spinler 
getService(const std::string & path,const std::string & interface,sdbusplus::bus_t & bus,bool logError)36f0f02b9aSMatt Spinler std::string getService(const std::string& path, const std::string& interface,
377354ce62SPatrick Williams                        sdbusplus::bus_t& bus, bool logError)
38974c916eSMatt Spinler {
39f0f02b9aSMatt Spinler     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
40f0f02b9aSMatt Spinler                                       MAPPER_INTERFACE, "GetObject");
41974c916eSMatt Spinler 
42974c916eSMatt Spinler     method.append(path);
43974c916eSMatt Spinler     method.append(std::vector<std::string>({interface}));
44974c916eSMatt Spinler 
45974c916eSMatt Spinler     auto reply = bus.call(method);
46974c916eSMatt Spinler 
47974c916eSMatt Spinler     std::map<std::string, std::vector<std::string>> response;
48974c916eSMatt Spinler     reply.read(response);
49974c916eSMatt Spinler 
50974c916eSMatt Spinler     if (response.empty())
51974c916eSMatt Spinler     {
52d262440dSMatthew Barth         if (logError)
53d262440dSMatthew Barth         {
546a3fd2c2SJay Meyer             log<level::ERR>(
556a3fd2c2SJay Meyer                 std::string("Error in mapper response for getting service name "
566a3fd2c2SJay Meyer                             "PATH=" +
576a3fd2c2SJay Meyer                             path + " INTERFACE=" + interface)
586a3fd2c2SJay Meyer                     .c_str());
59d262440dSMatthew Barth         }
60974c916eSMatt Spinler         return std::string{};
61974c916eSMatt Spinler     }
62974c916eSMatt Spinler 
63974c916eSMatt Spinler     return response.begin()->first;
64974c916eSMatt Spinler }
65974c916eSMatt Spinler 
getAllProperties(sdbusplus::bus_t & bus,const std::string & path,const std::string & interface,const std::string & service)667354ce62SPatrick Williams DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path,
674e8b335eSAdriana Kobylak                                  const std::string& interface,
684e8b335eSAdriana Kobylak                                  const std::string& service)
694e8b335eSAdriana Kobylak {
704e8b335eSAdriana Kobylak     DbusPropertyMap properties;
714e8b335eSAdriana Kobylak 
724e8b335eSAdriana Kobylak     auto serviceStr = service;
734e8b335eSAdriana Kobylak     if (serviceStr.empty())
744e8b335eSAdriana Kobylak     {
754e8b335eSAdriana Kobylak         serviceStr = getService(path, interface, bus);
764e8b335eSAdriana Kobylak         if (serviceStr.empty())
774e8b335eSAdriana Kobylak         {
784e8b335eSAdriana Kobylak             return properties;
794e8b335eSAdriana Kobylak         }
804e8b335eSAdriana Kobylak     }
814e8b335eSAdriana Kobylak 
824e8b335eSAdriana Kobylak     auto method = bus.new_method_call(serviceStr.c_str(), path.c_str(),
834e8b335eSAdriana Kobylak                                       PROPERTY_INTF, "GetAll");
844e8b335eSAdriana Kobylak     method.append(interface);
854e8b335eSAdriana Kobylak     auto reply = bus.call(method);
864e8b335eSAdriana Kobylak     reply.read(properties);
874e8b335eSAdriana Kobylak     return properties;
884e8b335eSAdriana Kobylak }
894e8b335eSAdriana Kobylak 
getSubTree(sdbusplus::bus_t & bus,const std::string & path,const std::string & interface,int32_t depth)907354ce62SPatrick Williams DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
91c761b5fcSBrandon Wyman                        const std::string& interface, int32_t depth)
92c761b5fcSBrandon Wyman {
938270b7d1SShawn McCarney     return getSubTree(bus, path, std::vector<std::string>({interface}), depth);
948270b7d1SShawn McCarney }
958270b7d1SShawn McCarney 
getSubTree(sdbusplus::bus_t & bus,const std::string & path,const std::vector<std::string> & interfaces,int32_t depth)968270b7d1SShawn McCarney DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
978270b7d1SShawn McCarney                        const std::vector<std::string>& interfaces,
988270b7d1SShawn McCarney                        int32_t depth)
998270b7d1SShawn McCarney {
100c761b5fcSBrandon Wyman     auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
101c761b5fcSBrandon Wyman                                           MAPPER_INTERFACE, "GetSubTree");
102c761b5fcSBrandon Wyman     mapperCall.append(path);
103c761b5fcSBrandon Wyman     mapperCall.append(depth);
1048270b7d1SShawn McCarney     mapperCall.append(interfaces);
105c761b5fcSBrandon Wyman 
106c761b5fcSBrandon Wyman     auto reply = bus.call(mapperCall);
107c761b5fcSBrandon Wyman 
108c761b5fcSBrandon Wyman     DbusSubtree response;
109c761b5fcSBrandon Wyman     reply.read(response);
110c761b5fcSBrandon Wyman     return response;
111c761b5fcSBrandon Wyman }
112c761b5fcSBrandon Wyman 
getAssociatedSubTreePaths(sdbusplus::bus_t & bus,const sdbusplus::message::object_path & associationPath,const sdbusplus::message::object_path & path,const std::vector<std::string> & interfaces,int32_t depth)11306594229SMatt Spinler std::vector<DbusPath> getAssociatedSubTreePaths(
11406594229SMatt Spinler     sdbusplus::bus_t& bus,
11506594229SMatt Spinler     const sdbusplus::message::object_path& associationPath,
11606594229SMatt Spinler     const sdbusplus::message::object_path& path,
11706594229SMatt Spinler     const std::vector<std::string>& interfaces, int32_t depth)
11806594229SMatt Spinler {
119*f5402197SPatrick Williams     auto mapperCall =
120*f5402197SPatrick Williams         bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE,
12106594229SMatt Spinler                             "GetAssociatedSubTreePaths");
12206594229SMatt Spinler     mapperCall.append(associationPath);
12306594229SMatt Spinler     mapperCall.append(path);
12406594229SMatt Spinler     mapperCall.append(depth);
12506594229SMatt Spinler     mapperCall.append(interfaces);
12606594229SMatt Spinler 
12706594229SMatt Spinler     auto reply = bus.call(mapperCall);
12806594229SMatt Spinler 
12906594229SMatt Spinler     std::vector<DbusPath> response;
13006594229SMatt Spinler     reply.read(response);
13106594229SMatt Spinler     return response;
13206594229SMatt Spinler }
13306594229SMatt Spinler 
loadJSONFromFile(const char * path)1347dc31bb1SLei YU json loadJSONFromFile(const char* path)
1357dc31bb1SLei YU {
1367dc31bb1SLei YU     std::ifstream ifs(path);
1377dc31bb1SLei YU     if (!ifs.good())
1387dc31bb1SLei YU     {
1396a3fd2c2SJay Meyer         log<level::ERR>(std::string("Unable to open file "
1406a3fd2c2SJay Meyer                                     "PATH=" +
1416a3fd2c2SJay Meyer                                     std::string(path))
1426a3fd2c2SJay Meyer                             .c_str());
1437dc31bb1SLei YU         return nullptr;
1447dc31bb1SLei YU     }
1457dc31bb1SLei YU     auto data = json::parse(ifs, nullptr, false);
1467dc31bb1SLei YU     if (data.is_discarded())
1477dc31bb1SLei YU     {
1486a3fd2c2SJay Meyer         log<level::ERR>(std::string("Failed to parse json "
1496a3fd2c2SJay Meyer                                     "PATH=" +
1506a3fd2c2SJay Meyer                                     std::string(path))
1516a3fd2c2SJay Meyer                             .c_str());
1527dc31bb1SLei YU         return nullptr;
1537dc31bb1SLei YU     }
1547dc31bb1SLei YU     return data;
1557dc31bb1SLei YU }
1567dc31bb1SLei YU 
getPMBusAccessType(const json & json)1574070546eSLei YU phosphor::pmbus::Type getPMBusAccessType(const json& json)
1584070546eSLei YU {
1594070546eSLei YU     using namespace phosphor::pmbus;
1604070546eSLei YU     Type type;
1614070546eSLei YU 
1624070546eSLei YU     auto typeStr = json.at("inventoryPMBusAccessType");
1634070546eSLei YU 
1644070546eSLei YU     if (typeStr == "Hwmon")
1654070546eSLei YU     {
1664070546eSLei YU         type = Type::Hwmon;
1674070546eSLei YU     }
1684070546eSLei YU     else if (typeStr == "DeviceDebug")
1694070546eSLei YU     {
1704070546eSLei YU         type = Type::DeviceDebug;
1714070546eSLei YU     }
1724070546eSLei YU     else if (typeStr == "Debug")
1734070546eSLei YU     {
1744070546eSLei YU         type = Type::Debug;
1754070546eSLei YU     }
1764070546eSLei YU     else if (typeStr == "HwmonDeviceDebug")
1774070546eSLei YU     {
1784070546eSLei YU         type = Type::HwmonDeviceDebug;
1794070546eSLei YU     }
1804070546eSLei YU     else
1814070546eSLei YU     {
1824070546eSLei YU         type = Type::Base;
1834070546eSLei YU     }
1844070546eSLei YU     return type;
1854070546eSLei YU }
1864070546eSLei YU 
isPoweredOn(sdbusplus::bus_t & bus,bool defaultState)1877354ce62SPatrick Williams bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState)
188cfc040c7SLei YU {
189e8c9cd64SLei YU     int32_t state = defaultState;
190cfc040c7SLei YU 
191cfc040c7SLei YU     try
192cfc040c7SLei YU     {
193e8c9cd64SLei YU         // When state = 1, system is powered on
194cfc040c7SLei YU         auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
195cfc040c7SLei YU         getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus,
196cfc040c7SLei YU                              state);
197cfc040c7SLei YU     }
198c1d4de5eSPatrick Williams     catch (const std::exception& e)
199cfc040c7SLei YU     {
200e8c9cd64SLei YU         log<level::INFO>("Failed to get power state.");
201cfc040c7SLei YU     }
202cfc040c7SLei YU     return state != 0;
203cfc040c7SLei YU }
204cfc040c7SLei YU 
getPSUInventoryPaths(sdbusplus::bus_t & bus)2057354ce62SPatrick Williams std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus)
206e8c9cd64SLei YU {
207e8c9cd64SLei YU     std::vector<std::string> paths;
208e8c9cd64SLei YU     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
209e8c9cd64SLei YU                                       MAPPER_INTERFACE, "GetSubTreePaths");
210e8c9cd64SLei YU     method.append(INVENTORY_OBJ_PATH);
211e8c9cd64SLei YU     method.append(0); // Depth 0 to search all
212e8c9cd64SLei YU     method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
213e8c9cd64SLei YU     auto reply = bus.call(method);
214e8c9cd64SLei YU 
215e8c9cd64SLei YU     reply.read(paths);
216e8c9cd64SLei YU     return paths;
217e8c9cd64SLei YU }
218e8c9cd64SLei YU 
219f0f02b9aSMatt Spinler } // namespace util
220f0f02b9aSMatt Spinler } // namespace power
221ab093328SLei YU } // namespace phosphor
222