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