Lei YU | fb19d99 | 2020-09-25 17:10:49 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
George Liu | 666f96a | 2024-06-03 19:41:40 +0800 | [diff] [blame] | 3 | #include <phosphor-logging/lg2.hpp> |
Lei YU | fb19d99 | 2020-09-25 17:10:49 +0800 | [diff] [blame] | 4 | |
| 5 | #include <algorithm> |
| 6 | |
Lei YU | fb19d99 | 2020-09-25 17:10:49 +0800 | [diff] [blame] | 7 | namespace utils |
| 8 | { |
| 9 | |
| 10 | namespace // anonymous |
| 11 | { |
| 12 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 13 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 14 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 15 | } // namespace |
| 16 | |
| 17 | const UtilsInterface& getUtils() |
| 18 | { |
| 19 | static Utils utils; |
| 20 | return utils; |
| 21 | } |
| 22 | |
| 23 | std::string Utils::getService(sdbusplus::bus::bus& bus, const char* path, |
| 24 | const char* interface) const |
| 25 | { |
| 26 | auto services = getServices(bus, path, interface); |
| 27 | if (services.empty()) |
| 28 | { |
| 29 | return {}; |
| 30 | } |
| 31 | return services[0]; |
| 32 | } |
| 33 | |
| 34 | std::vector<std::string> Utils::getServices(sdbusplus::bus::bus& bus, |
| 35 | const char* path, |
| 36 | const char* interface) const |
| 37 | { |
| 38 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 39 | MAPPER_INTERFACE, "GetObject"); |
| 40 | |
| 41 | mapper.append(path, std::vector<std::string>({interface})); |
| 42 | try |
| 43 | { |
| 44 | auto mapperResponseMsg = bus.call(mapper); |
| 45 | |
| 46 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 47 | mapperResponse; |
| 48 | mapperResponseMsg.read(mapperResponse); |
| 49 | if (mapperResponse.empty()) |
| 50 | { |
George Liu | 666f96a | 2024-06-03 19:41:40 +0800 | [diff] [blame] | 51 | lg2::error("Error reading mapper response"); |
Lei YU | fb19d99 | 2020-09-25 17:10:49 +0800 | [diff] [blame] | 52 | throw std::runtime_error("Error reading mapper response"); |
| 53 | } |
| 54 | std::vector<std::string> ret; |
| 55 | for (const auto& i : mapperResponse) |
| 56 | { |
| 57 | ret.emplace_back(i.first); |
| 58 | } |
| 59 | return ret; |
| 60 | } |
| 61 | catch (const sdbusplus::exception::SdBusError& ex) |
| 62 | { |
George Liu | 666f96a | 2024-06-03 19:41:40 +0800 | [diff] [blame] | 63 | lg2::error( |
| 64 | "GetObject call failed, path: {PATH}, interface: {INTERFACE}", |
| 65 | "PATH", path, "INTERFACE", interface); |
Lei YU | fb19d99 | 2020-09-25 17:10:49 +0800 | [diff] [blame] | 66 | throw std::runtime_error("GetObject call failed"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | any Utils::getPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 71 | const char* path, const char* interface, |
| 72 | const char* propertyName) const |
| 73 | { |
| 74 | auto method = bus.new_method_call(service, path, |
| 75 | "org.freedesktop.DBus.Properties", "Get"); |
| 76 | method.append(interface, propertyName); |
| 77 | try |
| 78 | { |
| 79 | PropertyType value{}; |
| 80 | auto reply = bus.call(method); |
| 81 | reply.read(value); |
| 82 | return any(value); |
| 83 | } |
| 84 | catch (const sdbusplus::exception::SdBusError& ex) |
| 85 | { |
George Liu | 666f96a | 2024-06-03 19:41:40 +0800 | [diff] [blame] | 86 | lg2::error( |
| 87 | "GetProperty call failed, path: {PATH}, interface: {INTERFACE}, property: {PROPERTY}", |
| 88 | "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName); |
Lei YU | fb19d99 | 2020-09-25 17:10:49 +0800 | [diff] [blame] | 89 | throw std::runtime_error("GetProperty call failed"); |
| 90 | } |
| 91 | } |
| 92 | |
Lei YU | 2e09746 | 2020-09-27 14:31:04 +0800 | [diff] [blame] | 93 | void Utils::setPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 94 | const char* path, const char* interface, |
| 95 | const char* propertyName, ValueType&& value) const |
| 96 | { |
| 97 | auto method = bus.new_method_call(service, path, |
| 98 | "org.freedesktop.DBus.Properties", "Set"); |
| 99 | method.append(interface, propertyName, value); |
| 100 | try |
| 101 | { |
| 102 | bus.call_noreply(method); |
| 103 | } |
| 104 | catch (const sdbusplus::exception::SdBusError& ex) |
| 105 | { |
George Liu | 666f96a | 2024-06-03 19:41:40 +0800 | [diff] [blame] | 106 | lg2::error( |
| 107 | "SetProperty call failed, path: {PATH}, interface: {INTERFACE}, property: {PROPERTY}", |
| 108 | "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName); |
Lei YU | 2e09746 | 2020-09-27 14:31:04 +0800 | [diff] [blame] | 109 | throw std::runtime_error("SetProperty call failed"); |
| 110 | } |
| 111 | } |
| 112 | |
Lei YU | fb19d99 | 2020-09-25 17:10:49 +0800 | [diff] [blame] | 113 | } // namespace utils |