Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 1 | #include <phosphor-logging/elog-errors.hpp> |
Tao Lin | 91799db | 2022-07-27 21:02:20 +0800 | [diff] [blame] | 2 | #include <phosphor-logging/lg2.hpp> |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 3 | #include <xyz/openbmc_project/Common/error.hpp> |
| 4 | |
Tao Lin | 91799db | 2022-07-27 21:02:20 +0800 | [diff] [blame] | 5 | const constexpr char* entityManagerBusName = |
| 6 | "xyz.openbmc_project.EntityManager"; |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 7 | const char* propIntf = "org.freedesktop.DBus.Properties"; |
| 8 | const char* mapperBusName = "xyz.openbmc_project.ObjectMapper"; |
| 9 | const char* mapperPath = "/xyz/openbmc_project/object_mapper"; |
| 10 | const char* mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 11 | |
| 12 | const char* methodGetObject = "GetObject"; |
| 13 | const char* methodGet = "Get"; |
Tao Lin | 91799db | 2022-07-27 21:02:20 +0800 | [diff] [blame] | 14 | const char* methodSet = "Set"; |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 15 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 16 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 17 | |
| 18 | using Value = std::variant<int64_t, double, std::string, bool>; |
| 19 | |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 20 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 21 | const char* intf) |
| 22 | { |
| 23 | /* Get mapper object for sensor path */ |
| 24 | auto mapper = bus.new_method_call(mapperBusName, mapperPath, mapperIntf, |
| 25 | methodGetObject); |
| 26 | |
| 27 | mapper.append(path.c_str()); |
| 28 | mapper.append(std::vector<std::string>({intf})); |
| 29 | |
| 30 | std::unordered_map<std::string, std::vector<std::string>> resp; |
| 31 | |
| 32 | try |
| 33 | { |
| 34 | auto msg = bus.call(mapper); |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 35 | msg.read(resp); |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 36 | } |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 37 | catch (const sdbusplus::exception_t& ex) |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 38 | { |
Matt Spinler | f8db722 | 2020-11-10 08:15:03 -0600 | [diff] [blame] | 39 | if (ex.name() == std::string(sdbusplus::xyz::openbmc_project::Common:: |
| 40 | Error::ResourceNotFound::errName)) |
| 41 | { |
| 42 | // The service isn't on D-Bus yet. |
| 43 | return std::string{}; |
| 44 | } |
Matt Spinler | 8859c84 | 2023-02-06 11:34:18 -0600 | [diff] [blame] | 45 | else if (ex.name() == std::string("org.freedesktop.DBus.Error.Timeout")) |
| 46 | { |
| 47 | lg2::info("Mapper timeout while looking up {PATH}", "PATH", path); |
| 48 | return std::string{}; |
| 49 | } |
Matt Spinler | f8db722 | 2020-11-10 08:15:03 -0600 | [diff] [blame] | 50 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 51 | throw; |
| 52 | } |
| 53 | |
| 54 | if (resp.begin() == resp.end()) |
| 55 | { |
Matt Spinler | f8db722 | 2020-11-10 08:15:03 -0600 | [diff] [blame] | 56 | // Shouldn't happen, if the mapper can't find it it is handled above. |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 57 | throw std::runtime_error("Unable to find Object: " + path); |
| 58 | } |
| 59 | |
| 60 | return resp.begin()->first; |
| 61 | } |
| 62 | |
| 63 | template <typename T> |
| 64 | |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 65 | T getDbusProperty(sdbusplus::bus_t& bus, const std::string& service, |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 66 | const std::string& path, const std::string& intf, |
| 67 | const std::string& property) |
| 68 | { |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 69 | Value value; |
| 70 | |
| 71 | auto method = |
| 72 | bus.new_method_call(service.c_str(), path.c_str(), propIntf, methodGet); |
| 73 | |
| 74 | method.append(intf, property); |
| 75 | |
Harvey Wu | 187582b | 2021-03-30 09:42:39 +0800 | [diff] [blame] | 76 | try |
| 77 | { |
| 78 | auto msg = bus.call(method); |
| 79 | msg.read(value); |
| 80 | } |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 81 | catch (const sdbusplus::exception_t& ex) |
Harvey Wu | 187582b | 2021-03-30 09:42:39 +0800 | [diff] [blame] | 82 | { |
| 83 | return std::numeric_limits<T>::quiet_NaN(); |
| 84 | } |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 85 | |
| 86 | return std::get<T>(value); |
| 87 | } |
Tao Lin | 91799db | 2022-07-27 21:02:20 +0800 | [diff] [blame] | 88 | |
| 89 | int setDbusProperty(sdbusplus::bus_t& bus, const std::string& service, |
| 90 | const std::string& path, const std::string& intf, |
| 91 | const std::string& property, const Value& value) |
| 92 | { |
| 93 | try |
| 94 | { |
| 95 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 96 | propIntf, methodSet); |
| 97 | method.append(intf, property, value); |
| 98 | auto msg = bus.call(method); |
| 99 | } |
| 100 | catch (const sdbusplus::exception_t& e) |
| 101 | { |
| 102 | lg2::error( |
| 103 | "Faild to set dbus property. service:{SERVICE} path:{PATH} intf:{INTF} Property:{PROP},{ERRMSG}", |
| 104 | "SERVICE", service, "PATH", path, "INTF", intf, "PROP", property, |
| 105 | "ERRMSG", e); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |