blob: 0cee1bf44e69fd50c48f9f3bf4bd37ec3f866508 [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
Lei YU7dc31bb2019-08-30 10:07:08 +080033using json = nlohmann::json;
Matt Spinler48b4a432017-08-04 11:57:37 -050034
Matt Spinlerf0f02b92018-10-25 16:12:43 -050035std::string getService(const std::string& path, const std::string& interface,
Patrick Williams7354ce62022-07-22 19:26:56 -050036 sdbusplus::bus_t& bus, bool logError)
Matt Spinler974c9162017-08-04 08:36:37 -050037{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050038 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
39 MAPPER_INTERFACE, "GetObject");
Matt Spinler974c9162017-08-04 08:36:37 -050040
41 method.append(path);
42 method.append(std::vector<std::string>({interface}));
43
44 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050045
46 std::map<std::string, std::vector<std::string>> response;
47 reply.read(response);
48
49 if (response.empty())
50 {
Matthew Barthd2624402020-02-03 15:35:12 -060051 if (logError)
52 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +000053 lg2::error("Error in mapper response for getting service name "
54 "PATH={PATH} INTERFACE={INTERFACE}",
55 "PATH", path, "INTERFACE", interface);
Matthew Barthd2624402020-02-03 15:35:12 -060056 }
Matt Spinler974c9162017-08-04 08:36:37 -050057 return std::string{};
58 }
59
60 return response.begin()->first;
61}
62
Patrick Williams7354ce62022-07-22 19:26:56 -050063DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path,
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000064 const std::string& interface,
65 const std::string& service)
66{
67 DbusPropertyMap properties;
68
69 auto serviceStr = service;
70 if (serviceStr.empty())
71 {
72 serviceStr = getService(path, interface, bus);
73 if (serviceStr.empty())
74 {
75 return properties;
76 }
77 }
78
79 auto method = bus.new_method_call(serviceStr.c_str(), path.c_str(),
80 PROPERTY_INTF, "GetAll");
81 method.append(interface);
82 auto reply = bus.call(method);
83 reply.read(properties);
84 return properties;
85}
86
Patrick Williams7354ce62022-07-22 19:26:56 -050087DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
Brandon Wymanc761b5f2020-11-05 18:30:41 -060088 const std::string& interface, int32_t depth)
89{
Shawn McCarney8270b7d2023-06-26 11:35:51 -050090 return getSubTree(bus, path, std::vector<std::string>({interface}), depth);
91}
92
93DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
94 const std::vector<std::string>& interfaces,
95 int32_t depth)
96{
Brandon Wymanc761b5f2020-11-05 18:30:41 -060097 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
98 MAPPER_INTERFACE, "GetSubTree");
99 mapperCall.append(path);
100 mapperCall.append(depth);
Shawn McCarney8270b7d2023-06-26 11:35:51 -0500101 mapperCall.append(interfaces);
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600102
103 auto reply = bus.call(mapperCall);
104
105 DbusSubtree response;
106 reply.read(response);
107 return response;
108}
109
Matt Spinler06594222023-05-01 10:44:43 -0500110std::vector<DbusPath> getAssociatedSubTreePaths(
111 sdbusplus::bus_t& bus,
112 const sdbusplus::message::object_path& associationPath,
113 const sdbusplus::message::object_path& path,
114 const std::vector<std::string>& interfaces, int32_t depth)
115{
Patrick Williamsf5402192024-08-16 15:20:53 -0400116 auto mapperCall =
117 bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE,
118 "GetAssociatedSubTreePaths");
Matt Spinler06594222023-05-01 10:44:43 -0500119 mapperCall.append(associationPath);
120 mapperCall.append(path);
121 mapperCall.append(depth);
122 mapperCall.append(interfaces);
123
124 auto reply = bus.call(mapperCall);
125
126 std::vector<DbusPath> response;
127 reply.read(response);
128 return response;
129}
130
Lei YU7dc31bb2019-08-30 10:07:08 +0800131json loadJSONFromFile(const char* path)
132{
133 std::ifstream ifs(path);
134 if (!ifs.good())
135 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000136 lg2::error("Unable to open file PATH={PATH}", "PATH", path);
Lei YU7dc31bb2019-08-30 10:07:08 +0800137 return nullptr;
138 }
139 auto data = json::parse(ifs, nullptr, false);
140 if (data.is_discarded())
141 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000142 lg2::error("Failed to parse json PATH={PATH}", "PATH", path);
Lei YU7dc31bb2019-08-30 10:07:08 +0800143 return nullptr;
144 }
145 return data;
146}
147
Lei YU40705462019-10-09 17:07:11 +0800148phosphor::pmbus::Type getPMBusAccessType(const json& json)
149{
150 using namespace phosphor::pmbus;
151 Type type;
152
153 auto typeStr = json.at("inventoryPMBusAccessType");
154
155 if (typeStr == "Hwmon")
156 {
157 type = Type::Hwmon;
158 }
159 else if (typeStr == "DeviceDebug")
160 {
161 type = Type::DeviceDebug;
162 }
163 else if (typeStr == "Debug")
164 {
165 type = Type::Debug;
166 }
167 else if (typeStr == "HwmonDeviceDebug")
168 {
169 type = Type::HwmonDeviceDebug;
170 }
171 else
172 {
173 type = Type::Base;
174 }
175 return type;
176}
177
Patrick Williams7354ce62022-07-22 19:26:56 -0500178bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState)
Lei YUcfc040c2019-10-29 17:10:26 +0800179{
Lei YUe8c9cd62019-11-04 14:24:41 +0800180 int32_t state = defaultState;
Lei YUcfc040c2019-10-29 17:10:26 +0800181
182 try
183 {
Lei YUe8c9cd62019-11-04 14:24:41 +0800184 // When state = 1, system is powered on
Lei YUcfc040c2019-10-29 17:10:26 +0800185 auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
186 getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus,
187 state);
188 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500189 catch (const std::exception& e)
Lei YUcfc040c2019-10-29 17:10:26 +0800190 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000191 lg2::info("Failed to get power state.");
Lei YUcfc040c2019-10-29 17:10:26 +0800192 }
193 return state != 0;
194}
195
Patrick Williams7354ce62022-07-22 19:26:56 -0500196std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus)
Lei YUe8c9cd62019-11-04 14:24:41 +0800197{
198 std::vector<std::string> paths;
199 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
200 MAPPER_INTERFACE, "GetSubTreePaths");
201 method.append(INVENTORY_OBJ_PATH);
202 method.append(0); // Depth 0 to search all
203 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
204 auto reply = bus.call(method);
205
206 reply.read(paths);
207 return paths;
208}
209
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500210} // namespace util
211} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800212} // namespace phosphor