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