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 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 16 | using InterfaceList = std::vector<std::string>; |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 17 | std::unordered_map<std::string, std::vector<std::string>> mapperResponse; |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 18 | |
| 19 | auto& bus = DBusHandler::getBus(); |
| 20 | |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame^] | 21 | auto mapper = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf, |
| 22 | "GetObject"); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 23 | mapper.append(path, InterfaceList({interface})); |
| 24 | |
| 25 | auto mapperResponseMsg = bus.call(mapper); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 26 | mapperResponseMsg.read(mapperResponse); |
| 27 | if (mapperResponse.empty()) |
| 28 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 29 | lg2::error( |
| 30 | "Failed to read getService mapper response, OBJECT_PATH = {PATH}, INTERFACE = {INTERFACE}", |
| 31 | "PATH", path, "INTERFACE", interface); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 32 | return ""; |
| 33 | } |
| 34 | |
| 35 | // the value here will be the service name |
| 36 | return mapperResponse.cbegin()->first; |
| 37 | } |
| 38 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 39 | // Get all properties |
| 40 | const 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 Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame^] | 54 | proIntf, "GetAll"); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 55 | method.append(interface); |
| 56 | |
| 57 | auto reply = bus.call(method); |
| 58 | reply.read(properties); |
| 59 | |
| 60 | return properties; |
| 61 | } |
| 62 | |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 63 | // Get the property name |
| 64 | const 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 Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame^] | 79 | proIntf, "Get"); |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 80 | method.append(interface, propertyName); |
| 81 | |
| 82 | auto reply = bus.call(method); |
| 83 | reply.read(value); |
| 84 | |
| 85 | return value; |
| 86 | } |
| 87 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 88 | // Set property |
| 89 | void 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 Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame^] | 102 | proIntf, "Set"); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 103 | method.append(interface.c_str(), propertyName.c_str(), value); |
| 104 | |
| 105 | bus.call_noreply(method); |
| 106 | } |
| 107 | |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 108 | const 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 Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame^] | 116 | auto method = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf, |
| 117 | "GetSubTreePaths"); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 118 | 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 Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 128 | } // namespace utils |
| 129 | } // namespace led |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 130 | } // namespace phosphor |