blob: 60117bb609e2d46ae5acd723c8a3b6171b510203 [file] [log] [blame]
Lei YUfb19d992020-09-25 17:10:49 +08001#include "utils.hpp"
2
George Liu666f96a2024-06-03 19:41:40 +08003#include <phosphor-logging/lg2.hpp>
Lei YUfb19d992020-09-25 17:10:49 +08004
5#include <algorithm>
6
Lei YUfb19d992020-09-25 17:10:49 +08007namespace utils
8{
9
10namespace // anonymous
11{
12constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
13constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
14constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
15} // namespace
16
17const UtilsInterface& getUtils()
18{
19 static Utils utils;
20 return utils;
21}
22
23std::string Utils::getService(sdbusplus::bus::bus& bus, const char* path,
24 const char* interface) const
25{
26 auto services = getServices(bus, path, interface);
27 if (services.empty())
28 {
29 return {};
30 }
31 return services[0];
32}
33
34std::vector<std::string> Utils::getServices(sdbusplus::bus::bus& bus,
35 const char* path,
36 const char* interface) const
37{
38 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
39 MAPPER_INTERFACE, "GetObject");
40
41 mapper.append(path, std::vector<std::string>({interface}));
42 try
43 {
44 auto mapperResponseMsg = bus.call(mapper);
45
46 std::vector<std::pair<std::string, std::vector<std::string>>>
47 mapperResponse;
48 mapperResponseMsg.read(mapperResponse);
49 if (mapperResponse.empty())
50 {
George Liu666f96a2024-06-03 19:41:40 +080051 lg2::error("Error reading mapper response");
Lei YUfb19d992020-09-25 17:10:49 +080052 throw std::runtime_error("Error reading mapper response");
53 }
54 std::vector<std::string> ret;
55 for (const auto& i : mapperResponse)
56 {
57 ret.emplace_back(i.first);
58 }
59 return ret;
60 }
61 catch (const sdbusplus::exception::SdBusError& ex)
62 {
George Liu666f96a2024-06-03 19:41:40 +080063 lg2::error(
64 "GetObject call failed, path: {PATH}, interface: {INTERFACE}",
65 "PATH", path, "INTERFACE", interface);
Lei YUfb19d992020-09-25 17:10:49 +080066 throw std::runtime_error("GetObject call failed");
67 }
68}
69
70any Utils::getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
71 const char* path, const char* interface,
72 const char* propertyName) const
73{
74 auto method = bus.new_method_call(service, path,
75 "org.freedesktop.DBus.Properties", "Get");
76 method.append(interface, propertyName);
77 try
78 {
79 PropertyType value{};
80 auto reply = bus.call(method);
81 reply.read(value);
82 return any(value);
83 }
84 catch (const sdbusplus::exception::SdBusError& ex)
85 {
George Liu666f96a2024-06-03 19:41:40 +080086 lg2::error(
87 "GetProperty call failed, path: {PATH}, interface: {INTERFACE}, property: {PROPERTY}",
88 "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName);
Lei YUfb19d992020-09-25 17:10:49 +080089 throw std::runtime_error("GetProperty call failed");
90 }
91}
92
Lei YU2e097462020-09-27 14:31:04 +080093void Utils::setPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
94 const char* path, const char* interface,
95 const char* propertyName, ValueType&& value) const
96{
97 auto method = bus.new_method_call(service, path,
98 "org.freedesktop.DBus.Properties", "Set");
99 method.append(interface, propertyName, value);
100 try
101 {
102 bus.call_noreply(method);
103 }
104 catch (const sdbusplus::exception::SdBusError& ex)
105 {
George Liu666f96a2024-06-03 19:41:40 +0800106 lg2::error(
107 "SetProperty call failed, path: {PATH}, interface: {INTERFACE}, property: {PROPERTY}",
108 "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName);
Lei YU2e097462020-09-27 14:31:04 +0800109 throw std::runtime_error("SetProperty call failed");
110 }
111}
112
Lei YUfb19d992020-09-25 17:10:49 +0800113} // namespace utils