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