George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 3 | #include <phosphor-logging/lg2.hpp> |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 4 | |
| 5 | namespace phosphor |
| 6 | { |
| 7 | namespace led |
| 8 | { |
| 9 | namespace utils |
| 10 | { |
| 11 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 12 | // Get service name |
| 13 | const std::string DBusHandler::getService(const std::string& path, |
| 14 | const std::string& interface) const |
| 15 | { |
| 16 | |
| 17 | using InterfaceList = std::vector<std::string>; |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame^] | 18 | std::unordered_map<std::string, std::vector<std::string>> mapperResponse; |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 19 | |
| 20 | auto& bus = DBusHandler::getBus(); |
| 21 | |
| 22 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 23 | MAPPER_IFACE, "GetObject"); |
| 24 | mapper.append(path, InterfaceList({interface})); |
| 25 | |
| 26 | auto mapperResponseMsg = bus.call(mapper); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 27 | mapperResponseMsg.read(mapperResponse); |
| 28 | if (mapperResponse.empty()) |
| 29 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 30 | lg2::error( |
| 31 | "Failed to read getService mapper response, OBJECT_PATH = {PATH}, INTERFACE = {INTERFACE}", |
| 32 | "PATH", path, "INTERFACE", interface); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 33 | return ""; |
| 34 | } |
| 35 | |
| 36 | // the value here will be the service name |
| 37 | return mapperResponse.cbegin()->first; |
| 38 | } |
| 39 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 40 | // Get all properties |
| 41 | const PropertyMap |
| 42 | DBusHandler::getAllProperties(const std::string& objectPath, |
| 43 | const std::string& interface) const |
| 44 | { |
| 45 | PropertyMap properties; |
| 46 | |
| 47 | auto& bus = DBusHandler::getBus(); |
| 48 | auto service = getService(objectPath, interface); |
| 49 | if (service.empty()) |
| 50 | { |
| 51 | return properties; |
| 52 | } |
| 53 | |
| 54 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 55 | DBUS_PROPERTY_IFACE, "GetAll"); |
| 56 | method.append(interface); |
| 57 | |
| 58 | auto reply = bus.call(method); |
| 59 | reply.read(properties); |
| 60 | |
| 61 | return properties; |
| 62 | } |
| 63 | |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 64 | // Get the property name |
| 65 | const PropertyValue |
| 66 | DBusHandler::getProperty(const std::string& objectPath, |
| 67 | const std::string& interface, |
| 68 | const std::string& propertyName) const |
| 69 | { |
| 70 | PropertyValue value{}; |
| 71 | |
| 72 | auto& bus = DBusHandler::getBus(); |
| 73 | auto service = getService(objectPath, interface); |
| 74 | if (service.empty()) |
| 75 | { |
| 76 | return value; |
| 77 | } |
| 78 | |
| 79 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 80 | DBUS_PROPERTY_IFACE, "Get"); |
| 81 | method.append(interface, propertyName); |
| 82 | |
| 83 | auto reply = bus.call(method); |
| 84 | reply.read(value); |
| 85 | |
| 86 | return value; |
| 87 | } |
| 88 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 89 | // Set property |
| 90 | void DBusHandler::setProperty(const std::string& objectPath, |
| 91 | const std::string& interface, |
| 92 | const std::string& propertyName, |
| 93 | const PropertyValue& value) const |
| 94 | { |
| 95 | auto& bus = DBusHandler::getBus(); |
| 96 | auto service = getService(objectPath, interface); |
| 97 | if (service.empty()) |
| 98 | { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 103 | DBUS_PROPERTY_IFACE, "Set"); |
| 104 | method.append(interface.c_str(), propertyName.c_str(), value); |
| 105 | |
| 106 | bus.call_noreply(method); |
| 107 | } |
| 108 | |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 109 | const std::vector<std::string> |
| 110 | DBusHandler::getSubTreePaths(const std::string& objectPath, |
| 111 | const std::string& interface) |
| 112 | { |
| 113 | std::vector<std::string> paths; |
| 114 | |
| 115 | auto& bus = DBusHandler::getBus(); |
| 116 | |
| 117 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 118 | MAPPER_IFACE, "GetSubTreePaths"); |
| 119 | method.append(objectPath.c_str()); |
| 120 | method.append(0); // Depth 0 to search all |
| 121 | method.append(std::vector<std::string>({interface.c_str()})); |
| 122 | auto reply = bus.call(method); |
| 123 | |
| 124 | reply.read(paths); |
| 125 | |
| 126 | return paths; |
| 127 | } |
| 128 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 129 | } // namespace utils |
| 130 | } // namespace led |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 131 | } // namespace phosphor |