blob: 7b20ee1f0175db007aa584ae4318eb0729463374 [file] [log] [blame]
George Liu1c737af2020-10-16 09:07:02 +08001#include "utils.hpp"
2
George Liue9fb5c62021-07-01 14:05:32 +08003#include <phosphor-logging/lg2.hpp>
George Liu1c737af2020-10-16 09:07:02 +08004
5namespace phosphor
6{
7namespace led
8{
9namespace utils
10{
11
George Liu1c737af2020-10-16 09:07:02 +080012// Get service name
13const std::string DBusHandler::getService(const std::string& path,
14 const std::string& interface) const
15{
George Liu1c737af2020-10-16 09:07:02 +080016 using InterfaceList = std::vector<std::string>;
Patrick Williamsf2044032022-03-17 05:12:30 -050017 std::unordered_map<std::string, std::vector<std::string>> mapperResponse;
George Liu1c737af2020-10-16 09:07:02 +080018
19 auto& bus = DBusHandler::getBus();
20
George Liu1f0b7152023-07-18 09:24:34 +080021 auto mapper = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
22 "GetObject");
George Liu1c737af2020-10-16 09:07:02 +080023 mapper.append(path, InterfaceList({interface}));
24
25 auto mapperResponseMsg = bus.call(mapper);
George Liu1c737af2020-10-16 09:07:02 +080026 mapperResponseMsg.read(mapperResponse);
27 if (mapperResponse.empty())
28 {
George Liue9fb5c62021-07-01 14:05:32 +080029 lg2::error(
30 "Failed to read getService mapper response, OBJECT_PATH = {PATH}, INTERFACE = {INTERFACE}",
31 "PATH", path, "INTERFACE", interface);
George Liu1c737af2020-10-16 09:07:02 +080032 return "";
33 }
34
35 // the value here will be the service name
36 return mapperResponse.cbegin()->first;
37}
38
George Liub6151622020-11-23 18:16:18 +080039// Get all properties
40const PropertyMap
41 DBusHandler::getAllProperties(const std::string& objectPath,
42 const std::string& interface) const
43{
44 PropertyMap properties;
45
46 auto& bus = DBusHandler::getBus();
47 auto service = getService(objectPath, interface);
48 if (service.empty())
49 {
50 return properties;
51 }
52
53 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
George Liu1f0b7152023-07-18 09:24:34 +080054 proIntf, "GetAll");
George Liub6151622020-11-23 18:16:18 +080055 method.append(interface);
56
57 auto reply = bus.call(method);
58 reply.read(properties);
59
60 return properties;
61}
62
George Liu4c5f5332020-10-10 17:04:28 +080063// Get the property name
64const PropertyValue
65 DBusHandler::getProperty(const std::string& objectPath,
66 const std::string& interface,
67 const std::string& propertyName) const
68{
69 PropertyValue value{};
70
71 auto& bus = DBusHandler::getBus();
72 auto service = getService(objectPath, interface);
73 if (service.empty())
74 {
75 return value;
76 }
77
78 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
George Liu1f0b7152023-07-18 09:24:34 +080079 proIntf, "Get");
George Liu4c5f5332020-10-10 17:04:28 +080080 method.append(interface, propertyName);
81
82 auto reply = bus.call(method);
83 reply.read(value);
84
85 return value;
86}
87
George Liu1c737af2020-10-16 09:07:02 +080088// Set property
89void DBusHandler::setProperty(const std::string& objectPath,
90 const std::string& interface,
91 const std::string& propertyName,
92 const PropertyValue& value) const
93{
94 auto& bus = DBusHandler::getBus();
95 auto service = getService(objectPath, interface);
96 if (service.empty())
97 {
98 return;
99 }
100
101 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
George Liu1f0b7152023-07-18 09:24:34 +0800102 proIntf, "Set");
George Liu1c737af2020-10-16 09:07:02 +0800103 method.append(interface.c_str(), propertyName.c_str(), value);
104
105 bus.call_noreply(method);
106}
107
George Liu616a0712021-02-18 10:50:24 +0800108const std::vector<std::string>
109 DBusHandler::getSubTreePaths(const std::string& objectPath,
110 const std::string& interface)
111{
112 std::vector<std::string> paths;
113
114 auto& bus = DBusHandler::getBus();
115
George Liu1f0b7152023-07-18 09:24:34 +0800116 auto method = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
117 "GetSubTreePaths");
George Liu616a0712021-02-18 10:50:24 +0800118 method.append(objectPath.c_str());
119 method.append(0); // Depth 0 to search all
120 method.append(std::vector<std::string>({interface.c_str()}));
121 auto reply = bus.call(method);
122
123 reply.read(paths);
124
125 return paths;
126}
127
George Liu1c737af2020-10-16 09:07:02 +0800128} // namespace utils
129} // namespace led
George Liu87fd11c2020-11-23 16:40:14 +0800130} // namespace phosphor