blob: c9a21e5b813a62a61d6f706c2467ef65e3171f50 [file] [log] [blame]
George Liu1c737af2020-10-16 09:07:02 +08001#include "utils.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5namespace phosphor
6{
7namespace led
8{
9namespace utils
10{
11
12using namespace phosphor::logging;
13
14// Get service name
15const std::string DBusHandler::getService(const std::string& path,
16 const std::string& interface) const
17{
18
19 using InterfaceList = std::vector<std::string>;
20 std::map<std::string, std::vector<std::string>> mapperResponse;
21
22 auto& bus = DBusHandler::getBus();
23
24 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
25 MAPPER_IFACE, "GetObject");
26 mapper.append(path, InterfaceList({interface}));
27
28 auto mapperResponseMsg = bus.call(mapper);
29 if (mapperResponseMsg.is_method_error())
30 {
31 log<level::ERR>("Failed to invoke ObjectMapper method",
32 entry("OBJECT_PATH=%s", path.c_str()),
33 entry("INTERFACE=%s", interface.c_str()));
34 return "";
35 }
36
37 mapperResponseMsg.read(mapperResponse);
38 if (mapperResponse.empty())
39 {
40 log<level::ERR>("Failed to read getService mapper response",
41 entry("OBJECT_PATH=%s", path.c_str()),
42 entry("INTERFACE=%s", interface.c_str()));
43 return "";
44 }
45
46 // the value here will be the service name
47 return mapperResponse.cbegin()->first;
48}
49
George Liu4c5f5332020-10-10 17:04:28 +080050// Get the property name
51const PropertyValue
52 DBusHandler::getProperty(const std::string& objectPath,
53 const std::string& interface,
54 const std::string& propertyName) const
55{
56 PropertyValue value{};
57
58 auto& bus = DBusHandler::getBus();
59 auto service = getService(objectPath, interface);
60 if (service.empty())
61 {
62 return value;
63 }
64
65 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
66 DBUS_PROPERTY_IFACE, "Get");
67 method.append(interface, propertyName);
68
69 auto reply = bus.call(method);
70 reply.read(value);
71
72 return value;
73}
74
George Liu1c737af2020-10-16 09:07:02 +080075// Set property
76void DBusHandler::setProperty(const std::string& objectPath,
77 const std::string& interface,
78 const std::string& propertyName,
79 const PropertyValue& value) const
80{
81 auto& bus = DBusHandler::getBus();
82 auto service = getService(objectPath, interface);
83 if (service.empty())
84 {
85 return;
86 }
87
88 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
89 DBUS_PROPERTY_IFACE, "Set");
90 method.append(interface.c_str(), propertyName.c_str(), value);
91
92 bus.call_noreply(method);
93}
94
95} // namespace utils
96} // namespace led
97} // namespace phosphor