| 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 | } | 
|  | 45 |  | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 46 | throw; | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | if (resp.begin() == resp.end()) | 
|  | 50 | { | 
| Matt Spinler | f8db722 | 2020-11-10 08:15:03 -0600 | [diff] [blame] | 51 | // 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] | 52 | throw std::runtime_error("Unable to find Object: " + path); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | return resp.begin()->first; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | template <typename T> | 
|  | 59 |  | 
| Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 60 | T getDbusProperty(sdbusplus::bus_t& bus, const std::string& service, | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 61 | const std::string& path, const std::string& intf, | 
|  | 62 | const std::string& property) | 
|  | 63 | { | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 64 | Value value; | 
|  | 65 |  | 
|  | 66 | auto method = | 
|  | 67 | bus.new_method_call(service.c_str(), path.c_str(), propIntf, methodGet); | 
|  | 68 |  | 
|  | 69 | method.append(intf, property); | 
|  | 70 |  | 
| Harvey Wu | 187582b | 2021-03-30 09:42:39 +0800 | [diff] [blame] | 71 | try | 
|  | 72 | { | 
|  | 73 | auto msg = bus.call(method); | 
|  | 74 | msg.read(value); | 
|  | 75 | } | 
| Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 76 | catch (const sdbusplus::exception_t& ex) | 
| Harvey Wu | 187582b | 2021-03-30 09:42:39 +0800 | [diff] [blame] | 77 | { | 
|  | 78 | return std::numeric_limits<T>::quiet_NaN(); | 
|  | 79 | } | 
| Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 80 |  | 
|  | 81 | return std::get<T>(value); | 
|  | 82 | } | 
| Tao Lin | 91799db | 2022-07-27 21:02:20 +0800 | [diff] [blame] | 83 |  | 
|  | 84 | int setDbusProperty(sdbusplus::bus_t& bus, const std::string& service, | 
|  | 85 | const std::string& path, const std::string& intf, | 
|  | 86 | const std::string& property, const Value& value) | 
|  | 87 | { | 
|  | 88 | try | 
|  | 89 | { | 
|  | 90 | auto method = bus.new_method_call(service.c_str(), path.c_str(), | 
|  | 91 | propIntf, methodSet); | 
|  | 92 | method.append(intf, property, value); | 
|  | 93 | auto msg = bus.call(method); | 
|  | 94 | } | 
|  | 95 | catch (const sdbusplus::exception_t& e) | 
|  | 96 | { | 
|  | 97 | lg2::error( | 
|  | 98 | "Faild to set dbus property. service:{SERVICE} path:{PATH} intf:{INTF} Property:{PROP},{ERRMSG}", | 
|  | 99 | "SERVICE", service, "PATH", path, "INTF", intf, "PROP", property, | 
|  | 100 | "ERRMSG", e); | 
|  | 101 | return -1; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | return 0; | 
|  | 105 | } |