blob: ad8e2321cc936eccc4cad1b59a247ae82ecdd83d [file] [log] [blame]
Matt Spinler974c9162017-08-04 08:36:37 -05001/**
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
Lei YUcfc040c2019-10-29 17:10:26 +080018#include "types.hpp"
19
Lei YU7dc31bb2019-08-30 10:07:08 +080020#include <fstream>
21
Lei YUab093322019-10-09 16:43:22 +080022namespace phosphor
Matt Spinler974c9162017-08-04 08:36:37 -050023{
24namespace power
25{
26namespace util
27{
28
29constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
30constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
31constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
32
33using namespace phosphor::logging;
Lei YU7dc31bb2019-08-30 10:07:08 +080034using json = nlohmann::json;
Matt Spinler48b4a432017-08-04 11:57:37 -050035
Matt Spinlerf0f02b92018-10-25 16:12:43 -050036std::string getService(const std::string& path, const std::string& interface,
Patrick Williams7354ce62022-07-22 19:26:56 -050037 sdbusplus::bus_t& bus, bool logError)
Matt Spinler974c9162017-08-04 08:36:37 -050038{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050039 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
40 MAPPER_INTERFACE, "GetObject");
Matt Spinler974c9162017-08-04 08:36:37 -050041
42 method.append(path);
43 method.append(std::vector<std::string>({interface}));
44
45 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050046
47 std::map<std::string, std::vector<std::string>> response;
48 reply.read(response);
49
50 if (response.empty())
51 {
Matthew Barthd2624402020-02-03 15:35:12 -060052 if (logError)
53 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -050054 log<level::ERR>(
55 std::string("Error in mapper response for getting service name "
56 "PATH=" +
57 path + " INTERFACE=" + interface)
58 .c_str());
Matthew Barthd2624402020-02-03 15:35:12 -060059 }
Matt Spinler974c9162017-08-04 08:36:37 -050060 return std::string{};
61 }
62
63 return response.begin()->first;
64}
65
Patrick Williams7354ce62022-07-22 19:26:56 -050066DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path,
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000067 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
Patrick Williams7354ce62022-07-22 19:26:56 -050090DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
Brandon Wymanc761b5f2020-11-05 18:30:41 -060091 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
Lei YU7dc31bb2019-08-30 10:07:08 +0800106json loadJSONFromFile(const char* path)
107{
108 std::ifstream ifs(path);
109 if (!ifs.good())
110 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500111 log<level::ERR>(std::string("Unable to open file "
112 "PATH=" +
113 std::string(path))
114 .c_str());
Lei YU7dc31bb2019-08-30 10:07:08 +0800115 return nullptr;
116 }
117 auto data = json::parse(ifs, nullptr, false);
118 if (data.is_discarded())
119 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500120 log<level::ERR>(std::string("Failed to parse json "
121 "PATH=" +
122 std::string(path))
123 .c_str());
Lei YU7dc31bb2019-08-30 10:07:08 +0800124 return nullptr;
125 }
126 return data;
127}
128
Lei YU40705462019-10-09 17:07:11 +0800129phosphor::pmbus::Type getPMBusAccessType(const json& json)
130{
131 using namespace phosphor::pmbus;
132 Type type;
133
134 auto typeStr = json.at("inventoryPMBusAccessType");
135
136 if (typeStr == "Hwmon")
137 {
138 type = Type::Hwmon;
139 }
140 else if (typeStr == "DeviceDebug")
141 {
142 type = Type::DeviceDebug;
143 }
144 else if (typeStr == "Debug")
145 {
146 type = Type::Debug;
147 }
148 else if (typeStr == "HwmonDeviceDebug")
149 {
150 type = Type::HwmonDeviceDebug;
151 }
152 else
153 {
154 type = Type::Base;
155 }
156 return type;
157}
158
Patrick Williams7354ce62022-07-22 19:26:56 -0500159bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState)
Lei YUcfc040c2019-10-29 17:10:26 +0800160{
Lei YUe8c9cd62019-11-04 14:24:41 +0800161 int32_t state = defaultState;
Lei YUcfc040c2019-10-29 17:10:26 +0800162
163 try
164 {
Lei YUe8c9cd62019-11-04 14:24:41 +0800165 // When state = 1, system is powered on
Lei YUcfc040c2019-10-29 17:10:26 +0800166 auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
167 getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus,
168 state);
169 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500170 catch (const std::exception& e)
Lei YUcfc040c2019-10-29 17:10:26 +0800171 {
Lei YUe8c9cd62019-11-04 14:24:41 +0800172 log<level::INFO>("Failed to get power state.");
Lei YUcfc040c2019-10-29 17:10:26 +0800173 }
174 return state != 0;
175}
176
Patrick Williams7354ce62022-07-22 19:26:56 -0500177std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus)
Lei YUe8c9cd62019-11-04 14:24:41 +0800178{
179 std::vector<std::string> paths;
180 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
181 MAPPER_INTERFACE, "GetSubTreePaths");
182 method.append(INVENTORY_OBJ_PATH);
183 method.append(0); // Depth 0 to search all
184 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
185 auto reply = bus.call(method);
186
187 reply.read(paths);
188 return paths;
189}
190
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500191} // namespace util
192} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800193} // namespace phosphor