blob: 136d20f074661316fea0fc01d9ec928a56324190 [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
50// Set property
51void DBusHandler::setProperty(const std::string& objectPath,
52 const std::string& interface,
53 const std::string& propertyName,
54 const PropertyValue& value) const
55{
56 auto& bus = DBusHandler::getBus();
57 auto service = getService(objectPath, interface);
58 if (service.empty())
59 {
60 return;
61 }
62
63 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
64 DBUS_PROPERTY_IFACE, "Set");
65 method.append(interface.c_str(), propertyName.c_str(), value);
66
67 bus.call_noreply(method);
68}
69
70} // namespace utils
71} // namespace led
72} // namespace phosphor