usb: Add utility for D-Bus
Putting all D-Bus related operation to the utils file.
Tested: Call the set/getProperty functions locally and successfully
set/get the corresponding attribute value.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I0e0c33fd6ec5db3a47babaf417b0c708709e580b
diff --git a/utils.cpp b/utils.cpp
index 38de0e4..410ddc8 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -41,6 +41,45 @@
return response[0].first;
}
+const PropertyValue getProperty(sdbusplus::bus::bus& bus,
+ const std::string& objectPath,
+ const std::string& interface,
+ const std::string& propertyName)
+{
+ PropertyValue value{};
+ auto service = getService(bus, objectPath, interface);
+ if (service.empty())
+ {
+ return value;
+ }
+
+ auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
+ "org.freedesktop.DBus.Properties", "Get");
+ method.append(interface, propertyName);
+
+ auto reply = bus.call(method);
+ reply.read(value);
+
+ return value;
+}
+
+void setProperty(sdbusplus::bus::bus& bus, const std::string& objectPath,
+ const std::string& interface, const std::string& propertyName,
+ const PropertyValue& value)
+{
+ auto service = getService(bus, objectPath, interface);
+ if (service.empty())
+ {
+ return;
+ }
+
+ auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
+ "org.freedesktop.DBus.Properties", "Set");
+ method.append(interface.c_str(), propertyName.c_str(), value);
+
+ bus.call_noreply(method);
+}
+
void mergeFiles(std::vector<std::string>& srcFiles, std::string& dstFile)
{
std::ofstream outFile(dstFile, std::ios::out);