blob: 1da9602cfc9dc9f63ec37ebc80c4d8d73dcf660e [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
George Liu405ea282024-08-22 19:36:41 +080013std::string DBusHandler::getService(const std::string& path,
George Liuf0592552024-08-23 09:46:17 +080014 const std::string& interface)
George Liu1c737af2020-10-16 09:07:02 +080015{
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
George Liu405ea282024-08-22 19:36:41 +080040PropertyMap DBusHandler::getAllProperties(const std::string& objectPath,
George Liuf0592552024-08-23 09:46:17 +080041 const std::string& interface)
George Liub6151622020-11-23 18:16:18 +080042{
43 PropertyMap properties;
44
45 auto& bus = DBusHandler::getBus();
46 auto service = getService(objectPath, interface);
47 if (service.empty())
48 {
49 return properties;
50 }
51
52 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
George Liu1f0b7152023-07-18 09:24:34 +080053 proIntf, "GetAll");
George Liub6151622020-11-23 18:16:18 +080054 method.append(interface);
55
56 auto reply = bus.call(method);
57 reply.read(properties);
58
59 return properties;
60}
61
George Liu4c5f5332020-10-10 17:04:28 +080062// Get the property name
George Liu405ea282024-08-22 19:36:41 +080063PropertyValue DBusHandler::getProperty(const std::string& objectPath,
64 const std::string& interface,
George Liuf0592552024-08-23 09:46:17 +080065 const std::string& propertyName)
George Liu4c5f5332020-10-10 17:04:28 +080066{
67 PropertyValue value{};
68
69 auto& bus = DBusHandler::getBus();
70 auto service = getService(objectPath, interface);
71 if (service.empty())
72 {
73 return value;
74 }
75
76 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
George Liu1f0b7152023-07-18 09:24:34 +080077 proIntf, "Get");
George Liu4c5f5332020-10-10 17:04:28 +080078 method.append(interface, propertyName);
79
80 auto reply = bus.call(method);
81 reply.read(value);
82
83 return value;
84}
85
George Liu1c737af2020-10-16 09:07:02 +080086// Set property
Patrick Williams543ac9f2024-08-16 15:19:59 -040087void DBusHandler::setProperty(
88 const std::string& objectPath, const std::string& interface,
George Liuf0592552024-08-23 09:46:17 +080089 const std::string& propertyName, const PropertyValue& value)
George Liu1c737af2020-10-16 09:07:02 +080090{
91 auto& bus = DBusHandler::getBus();
92 auto service = getService(objectPath, interface);
93 if (service.empty())
94 {
95 return;
96 }
97
98 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
George Liu1f0b7152023-07-18 09:24:34 +080099 proIntf, "Set");
George Liu1c737af2020-10-16 09:07:02 +0800100 method.append(interface.c_str(), propertyName.c_str(), value);
101
102 bus.call_noreply(method);
103}
104
George Liu405ea282024-08-22 19:36:41 +0800105std::vector<std::string> DBusHandler::getSubTreePaths(
George Liuf0592552024-08-23 09:46:17 +0800106 const std::string& objectPath, const std::string& interface)
George Liu616a0712021-02-18 10:50:24 +0800107{
108 std::vector<std::string> paths;
109
110 auto& bus = DBusHandler::getBus();
111
George Liu1f0b7152023-07-18 09:24:34 +0800112 auto method = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
113 "GetSubTreePaths");
George Liu616a0712021-02-18 10:50:24 +0800114 method.append(objectPath.c_str());
115 method.append(0); // Depth 0 to search all
116 method.append(std::vector<std::string>({interface.c_str()}));
117 auto reply = bus.call(method);
118
119 reply.read(paths);
120
121 return paths;
122}
123
George Liu1c737af2020-10-16 09:07:02 +0800124} // namespace utils
125} // namespace led
George Liu87fd11c2020-11-23 16:40:14 +0800126} // namespace phosphor