blob: c2d7815c795516ada031e0fe2524f7fbd7627424 [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 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +000054 lg2::error("Error in mapper response for getting service name "
55 "PATH={PATH} INTERFACE={INTERFACE}",
56 "PATH", path, "INTERFACE", interface);
Matthew Barthd2624402020-02-03 15:35:12 -060057 }
Matt Spinler974c9162017-08-04 08:36:37 -050058 return std::string{};
59 }
60
61 return response.begin()->first;
62}
63
Patrick Williams7354ce62022-07-22 19:26:56 -050064DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path,
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000065 const std::string& interface,
66 const std::string& service)
67{
68 DbusPropertyMap properties;
69
70 auto serviceStr = service;
71 if (serviceStr.empty())
72 {
73 serviceStr = getService(path, interface, bus);
74 if (serviceStr.empty())
75 {
76 return properties;
77 }
78 }
79
80 auto method = bus.new_method_call(serviceStr.c_str(), path.c_str(),
81 PROPERTY_INTF, "GetAll");
82 method.append(interface);
83 auto reply = bus.call(method);
84 reply.read(properties);
85 return properties;
86}
87
Patrick Williams7354ce62022-07-22 19:26:56 -050088DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
Brandon Wymanc761b5f2020-11-05 18:30:41 -060089 const std::string& interface, int32_t depth)
90{
Shawn McCarney8270b7d2023-06-26 11:35:51 -050091 return getSubTree(bus, path, std::vector<std::string>({interface}), depth);
92}
93
94DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
95 const std::vector<std::string>& interfaces,
96 int32_t depth)
97{
Brandon Wymanc761b5f2020-11-05 18:30:41 -060098 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
99 MAPPER_INTERFACE, "GetSubTree");
100 mapperCall.append(path);
101 mapperCall.append(depth);
Shawn McCarney8270b7d2023-06-26 11:35:51 -0500102 mapperCall.append(interfaces);
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600103
104 auto reply = bus.call(mapperCall);
105
106 DbusSubtree response;
107 reply.read(response);
108 return response;
109}
110
Matt Spinler06594222023-05-01 10:44:43 -0500111std::vector<DbusPath> getAssociatedSubTreePaths(
112 sdbusplus::bus_t& bus,
113 const sdbusplus::message::object_path& associationPath,
114 const sdbusplus::message::object_path& path,
115 const std::vector<std::string>& interfaces, int32_t depth)
116{
Patrick Williamsf5402192024-08-16 15:20:53 -0400117 auto mapperCall =
118 bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE,
119 "GetAssociatedSubTreePaths");
Matt Spinler06594222023-05-01 10:44:43 -0500120 mapperCall.append(associationPath);
121 mapperCall.append(path);
122 mapperCall.append(depth);
123 mapperCall.append(interfaces);
124
125 auto reply = bus.call(mapperCall);
126
127 std::vector<DbusPath> response;
128 reply.read(response);
129 return response;
130}
131
Lei YU7dc31bb2019-08-30 10:07:08 +0800132json loadJSONFromFile(const char* path)
133{
134 std::ifstream ifs(path);
135 if (!ifs.good())
136 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000137 lg2::error("Unable to open file PATH={PATH}", "PATH", path);
Lei YU7dc31bb2019-08-30 10:07:08 +0800138 return nullptr;
139 }
140 auto data = json::parse(ifs, nullptr, false);
141 if (data.is_discarded())
142 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000143 lg2::error("Failed to parse json PATH={PATH}", "PATH", path);
Lei YU7dc31bb2019-08-30 10:07:08 +0800144 return nullptr;
145 }
146 return data;
147}
148
Lei YU40705462019-10-09 17:07:11 +0800149phosphor::pmbus::Type getPMBusAccessType(const json& json)
150{
151 using namespace phosphor::pmbus;
152 Type type;
153
154 auto typeStr = json.at("inventoryPMBusAccessType");
155
156 if (typeStr == "Hwmon")
157 {
158 type = Type::Hwmon;
159 }
160 else if (typeStr == "DeviceDebug")
161 {
162 type = Type::DeviceDebug;
163 }
164 else if (typeStr == "Debug")
165 {
166 type = Type::Debug;
167 }
168 else if (typeStr == "HwmonDeviceDebug")
169 {
170 type = Type::HwmonDeviceDebug;
171 }
172 else
173 {
174 type = Type::Base;
175 }
176 return type;
177}
178
Patrick Williams7354ce62022-07-22 19:26:56 -0500179bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState)
Lei YUcfc040c2019-10-29 17:10:26 +0800180{
Lei YUe8c9cd62019-11-04 14:24:41 +0800181 int32_t state = defaultState;
Lei YUcfc040c2019-10-29 17:10:26 +0800182
183 try
184 {
Lei YUe8c9cd62019-11-04 14:24:41 +0800185 // When state = 1, system is powered on
Lei YUcfc040c2019-10-29 17:10:26 +0800186 auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
187 getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus,
188 state);
189 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500190 catch (const std::exception& e)
Lei YUcfc040c2019-10-29 17:10:26 +0800191 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000192 lg2::info("Failed to get power state.");
Lei YUcfc040c2019-10-29 17:10:26 +0800193 }
194 return state != 0;
195}
196
Patrick Williams7354ce62022-07-22 19:26:56 -0500197std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus)
Lei YUe8c9cd62019-11-04 14:24:41 +0800198{
199 std::vector<std::string> paths;
200 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
201 MAPPER_INTERFACE, "GetSubTreePaths");
202 method.append(INVENTORY_OBJ_PATH);
203 method.append(0); // Depth 0 to search all
204 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
205 auto reply = bus.call(method);
206
207 reply.read(paths);
208 return paths;
209}
210
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500211} // namespace util
212} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800213} // namespace phosphor