Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame^] | 3 | #include <phosphor-logging/lg2.hpp> |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 4 | |
| 5 | namespace phosphor |
| 6 | { |
| 7 | namespace state |
| 8 | { |
| 9 | namespace manager |
| 10 | { |
| 11 | namespace utils |
| 12 | { |
| 13 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame^] | 14 | PHOSPHOR_LOG2_USING; |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 15 | |
| 16 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 17 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 18 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 19 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 20 | |
| 21 | std::string getService(sdbusplus::bus::bus& bus, std::string path, |
| 22 | std::string interface) |
| 23 | { |
| 24 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 25 | MAPPER_INTERFACE, "GetObject"); |
| 26 | |
| 27 | mapper.append(path, std::vector<std::string>({interface})); |
| 28 | |
| 29 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 30 | mapperResponse; |
| 31 | |
| 32 | try |
| 33 | { |
| 34 | auto mapperResponseMsg = bus.call(mapper); |
| 35 | |
| 36 | mapperResponseMsg.read(mapperResponse); |
| 37 | if (mapperResponse.empty()) |
| 38 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame^] | 39 | error("Error no matching service with path {PATH} and interface " |
| 40 | "{INTERFACE}", |
| 41 | "PATH", path, "INTERFACE", interface); |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 42 | throw std::runtime_error("Error no matching service"); |
| 43 | } |
| 44 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 45 | catch (const sdbusplus::exception::exception& e) |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 46 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame^] | 47 | error("Error in mapper call with path {PATH}, interface " |
| 48 | "{INTERFACE}, and exception {ERROR}", |
| 49 | "PATH", path, "INTERFACE", interface, "ERROR", e); |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 50 | throw; |
| 51 | } |
| 52 | |
| 53 | return mapperResponse.begin()->first; |
| 54 | } |
| 55 | |
| 56 | void setProperty(sdbusplus::bus::bus& bus, const std::string& path, |
| 57 | const std::string& interface, const std::string& property, |
| 58 | const std::string& value) |
| 59 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 60 | std::variant<std::string> variantValue = value; |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 61 | std::string service = getService(bus, path, interface); |
| 62 | |
| 63 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 64 | PROPERTY_INTERFACE, "Set"); |
| 65 | |
| 66 | method.append(interface, property, variantValue); |
| 67 | bus.call_noreply(method); |
| 68 | |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | } // namespace utils |
| 73 | } // namespace manager |
| 74 | } // namespace state |
| 75 | } // namespace phosphor |