blob: 53fe965a86c42b55d9473b3421d6cf068ce71ec0 [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
Faisal Awadaf661ecb2025-07-18 14:03:18 -050033constexpr auto DECORATOR_CHASSIS_ID =
34 "xyz.openbmc_project.Inventory.Decorator.Slot";
35constexpr auto CHASSIS_ID_PROPERTY = "SlotNumber";
36
Lei YU7dc31bb2019-08-30 10:07:08 +080037using json = nlohmann::json;
Matt Spinler48b4a432017-08-04 11:57:37 -050038
Matt Spinlerf0f02b92018-10-25 16:12:43 -050039std::string getService(const std::string& path, const std::string& interface,
Patrick Williams7354ce62022-07-22 19:26:56 -050040 sdbusplus::bus_t& bus, bool logError)
Matt Spinler974c9162017-08-04 08:36:37 -050041{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050042 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
43 MAPPER_INTERFACE, "GetObject");
Matt Spinler974c9162017-08-04 08:36:37 -050044
45 method.append(path);
46 method.append(std::vector<std::string>({interface}));
47
48 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050049
50 std::map<std::string, std::vector<std::string>> response;
51 reply.read(response);
52
53 if (response.empty())
54 {
Matthew Barthd2624402020-02-03 15:35:12 -060055 if (logError)
56 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +000057 lg2::error("Error in mapper response for getting service name "
58 "PATH={PATH} INTERFACE={INTERFACE}",
59 "PATH", path, "INTERFACE", interface);
Matthew Barthd2624402020-02-03 15:35:12 -060060 }
Matt Spinler974c9162017-08-04 08:36:37 -050061 return std::string{};
62 }
63
64 return response.begin()->first;
65}
66
Patrick Williams7354ce62022-07-22 19:26:56 -050067DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path,
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000068 const std::string& interface,
69 const std::string& service)
70{
71 DbusPropertyMap properties;
72
73 auto serviceStr = service;
74 if (serviceStr.empty())
75 {
76 serviceStr = getService(path, interface, bus);
77 if (serviceStr.empty())
78 {
79 return properties;
80 }
81 }
82
83 auto method = bus.new_method_call(serviceStr.c_str(), path.c_str(),
84 PROPERTY_INTF, "GetAll");
85 method.append(interface);
86 auto reply = bus.call(method);
87 reply.read(properties);
88 return properties;
89}
90
Patrick Williams7354ce62022-07-22 19:26:56 -050091DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
Brandon Wymanc761b5f2020-11-05 18:30:41 -060092 const std::string& interface, int32_t depth)
93{
Shawn McCarney8270b7d2023-06-26 11:35:51 -050094 return getSubTree(bus, path, std::vector<std::string>({interface}), depth);
95}
96
97DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
98 const std::vector<std::string>& interfaces,
99 int32_t depth)
100{
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600101 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
102 MAPPER_INTERFACE, "GetSubTree");
103 mapperCall.append(path);
104 mapperCall.append(depth);
Shawn McCarney8270b7d2023-06-26 11:35:51 -0500105 mapperCall.append(interfaces);
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600106
107 auto reply = bus.call(mapperCall);
108
109 DbusSubtree response;
110 reply.read(response);
111 return response;
112}
113
Matt Spinler06594222023-05-01 10:44:43 -0500114std::vector<DbusPath> getAssociatedSubTreePaths(
115 sdbusplus::bus_t& bus,
116 const sdbusplus::message::object_path& associationPath,
117 const sdbusplus::message::object_path& path,
118 const std::vector<std::string>& interfaces, int32_t depth)
119{
Patrick Williamsf5402192024-08-16 15:20:53 -0400120 auto mapperCall =
121 bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE,
122 "GetAssociatedSubTreePaths");
Matt Spinler06594222023-05-01 10:44:43 -0500123 mapperCall.append(associationPath);
124 mapperCall.append(path);
125 mapperCall.append(depth);
126 mapperCall.append(interfaces);
127
128 auto reply = bus.call(mapperCall);
129
130 std::vector<DbusPath> response;
131 reply.read(response);
132 return response;
133}
134
Lei YU7dc31bb2019-08-30 10:07:08 +0800135json loadJSONFromFile(const char* path)
136{
137 std::ifstream ifs(path);
138 if (!ifs.good())
139 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000140 lg2::error("Unable to open file PATH={PATH}", "PATH", path);
Lei YU7dc31bb2019-08-30 10:07:08 +0800141 return nullptr;
142 }
143 auto data = json::parse(ifs, nullptr, false);
144 if (data.is_discarded())
145 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000146 lg2::error("Failed to parse json PATH={PATH}", "PATH", path);
Lei YU7dc31bb2019-08-30 10:07:08 +0800147 return nullptr;
148 }
149 return data;
150}
151
Lei YU40705462019-10-09 17:07:11 +0800152phosphor::pmbus::Type getPMBusAccessType(const json& json)
153{
154 using namespace phosphor::pmbus;
155 Type type;
156
157 auto typeStr = json.at("inventoryPMBusAccessType");
158
159 if (typeStr == "Hwmon")
160 {
161 type = Type::Hwmon;
162 }
163 else if (typeStr == "DeviceDebug")
164 {
165 type = Type::DeviceDebug;
166 }
167 else if (typeStr == "Debug")
168 {
169 type = Type::Debug;
170 }
171 else if (typeStr == "HwmonDeviceDebug")
172 {
173 type = Type::HwmonDeviceDebug;
174 }
175 else
176 {
177 type = Type::Base;
178 }
179 return type;
180}
181
Patrick Williams7354ce62022-07-22 19:26:56 -0500182bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState)
Lei YUcfc040c2019-10-29 17:10:26 +0800183{
Lei YUe8c9cd62019-11-04 14:24:41 +0800184 int32_t state = defaultState;
Lei YUcfc040c2019-10-29 17:10:26 +0800185
186 try
187 {
Lei YUe8c9cd62019-11-04 14:24:41 +0800188 // When state = 1, system is powered on
Lei YUcfc040c2019-10-29 17:10:26 +0800189 auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
190 getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus,
191 state);
192 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500193 catch (const std::exception& e)
Lei YUcfc040c2019-10-29 17:10:26 +0800194 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000195 lg2::info("Failed to get power state.");
Lei YUcfc040c2019-10-29 17:10:26 +0800196 }
197 return state != 0;
198}
199
Patrick Williams7354ce62022-07-22 19:26:56 -0500200std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus)
Lei YUe8c9cd62019-11-04 14:24:41 +0800201{
202 std::vector<std::string> paths;
203 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
204 MAPPER_INTERFACE, "GetSubTreePaths");
205 method.append(INVENTORY_OBJ_PATH);
206 method.append(0); // Depth 0 to search all
207 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
208 auto reply = bus.call(method);
209
210 reply.read(paths);
211 return paths;
212}
213
Faisal Awadaf661ecb2025-07-18 14:03:18 -0500214std::vector<std::string> getChassisInventoryPaths(sdbusplus::bus_t& bus)
215{
216 std::vector<std::string> paths;
217 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
218 MAPPER_INTERFACE, "GetSubTreePaths");
219 method.append(INVENTORY_OBJ_PATH);
220 method.append(0); // Depth 0 to search all
221 method.append(std::vector<std::string>({CHASSIS_IFACE}));
222 auto reply = bus.call(method);
223
224 reply.read(paths);
225 return paths;
226}
227
228uint64_t getChassisInventoryUniqueId(sdbusplus::bus_t& bus,
229 const std::string& path)
230{
231 uint32_t chassisId;
232 getProperty(DECORATOR_CHASSIS_ID, CHASSIS_ID_PROPERTY, path,
233 INVENTORY_MGR_IFACE, bus, chassisId);
234 return static_cast<uint64_t>(chassisId);
235}
236
237uint64_t getParentEMUniqueId(sdbusplus::bus_t& bus, const std::string& path)
238{
239 namespace fs = std::filesystem;
240 uint64_t chassisId;
241 fs::path fspath(path);
242 getProperty(DECORATOR_CHASSIS_ID, CHASSIS_ID_PROPERTY, fspath.parent_path(),
243 ENTITY_MGR_SERVICE, bus, chassisId);
244 return chassisId;
245}
246
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500247} // namespace util
248} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800249} // namespace phosphor