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 |
George Liu | 405ea28 | 2024-08-22 19:36:41 +0800 | [diff] [blame] | 13 | std::string DBusHandler::getService(const std::string& path, |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 14 | const std::string& interface) |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 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 |
George Liu | 405ea28 | 2024-08-22 19:36:41 +0800 | [diff] [blame] | 40 | PropertyMap DBusHandler::getAllProperties(const std::string& objectPath, |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 41 | const std::string& interface) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 42 | { |
| 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 Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 53 | proIntf, "GetAll"); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 54 | method.append(interface); |
| 55 | |
| 56 | auto reply = bus.call(method); |
| 57 | reply.read(properties); |
| 58 | |
| 59 | return properties; |
| 60 | } |
| 61 | |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 62 | // Get the property name |
George Liu | 405ea28 | 2024-08-22 19:36:41 +0800 | [diff] [blame] | 63 | PropertyValue DBusHandler::getProperty(const std::string& objectPath, |
| 64 | const std::string& interface, |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 65 | const std::string& propertyName) |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 66 | { |
| 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 Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 77 | proIntf, "Get"); |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 78 | method.append(interface, propertyName); |
| 79 | |
| 80 | auto reply = bus.call(method); |
| 81 | reply.read(value); |
| 82 | |
| 83 | return value; |
| 84 | } |
| 85 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 86 | // Set property |
Patrick Williams | 543ac9f | 2024-08-16 15:19:59 -0400 | [diff] [blame] | 87 | void DBusHandler::setProperty( |
| 88 | const std::string& objectPath, const std::string& interface, |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 89 | const std::string& propertyName, const PropertyValue& value) |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 90 | { |
| 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 Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 99 | proIntf, "Set"); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 100 | method.append(interface.c_str(), propertyName.c_str(), value); |
| 101 | |
| 102 | bus.call_noreply(method); |
| 103 | } |
| 104 | |
George Liu | 405ea28 | 2024-08-22 19:36:41 +0800 | [diff] [blame] | 105 | std::vector<std::string> DBusHandler::getSubTreePaths( |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 106 | const std::string& objectPath, const std::string& interface) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 107 | { |
| 108 | std::vector<std::string> paths; |
| 109 | |
| 110 | auto& bus = DBusHandler::getBus(); |
| 111 | |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 112 | auto method = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf, |
| 113 | "GetSubTreePaths"); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 114 | 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 Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 124 | } // namespace utils |
| 125 | } // namespace led |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 126 | } // namespace phosphor |