blob: 984ac5f72343714721b18b49a8598210252fd4cd [file] [log] [blame]
Vijay Khemka7452a862020-08-11 16:01:23 -07001#include <phosphor-logging/elog-errors.hpp>
Tao Lin91799db2022-07-27 21:02:20 +08002#include <phosphor-logging/lg2.hpp>
Vijay Khemka7452a862020-08-11 16:01:23 -07003#include <xyz/openbmc_project/Common/error.hpp>
4
Tao Lin91799db2022-07-27 21:02:20 +08005const constexpr char* entityManagerBusName =
6 "xyz.openbmc_project.EntityManager";
Vijay Khemka7452a862020-08-11 16:01:23 -07007const char* propIntf = "org.freedesktop.DBus.Properties";
8const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
9const char* mapperPath = "/xyz/openbmc_project/object_mapper";
10const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
11
12const char* methodGetObject = "GetObject";
13const char* methodGet = "Get";
Tao Lin91799db2022-07-27 21:02:20 +080014const char* methodSet = "Set";
Vijay Khemka7452a862020-08-11 16:01:23 -070015
Vijay Khemka7452a862020-08-11 16:01:23 -070016using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17
18using Value = std::variant<int64_t, double, std::string, bool>;
19
Patrick Williams8e11ccc2022-07-22 19:26:57 -050020std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Vijay Khemka7452a862020-08-11 16:01:23 -070021 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 Khemka7452a862020-08-11 16:01:23 -070035 msg.read(resp);
Vijay Khemka7452a862020-08-11 16:01:23 -070036 }
Patrick Williams8e11ccc2022-07-22 19:26:57 -050037 catch (const sdbusplus::exception_t& ex)
Vijay Khemka7452a862020-08-11 16:01:23 -070038 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060039 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 Spinler8859c842023-02-06 11:34:18 -060045 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 Spinlerf8db7222020-11-10 08:15:03 -060050
Vijay Khemka7452a862020-08-11 16:01:23 -070051 throw;
52 }
53
54 if (resp.begin() == resp.end())
55 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060056 // Shouldn't happen, if the mapper can't find it it is handled above.
Vijay Khemka7452a862020-08-11 16:01:23 -070057 throw std::runtime_error("Unable to find Object: " + path);
58 }
59
60 return resp.begin()->first;
61}
62
63template <typename T>
64
Patrick Williams8e11ccc2022-07-22 19:26:57 -050065T getDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
Vijay Khemka7452a862020-08-11 16:01:23 -070066 const std::string& path, const std::string& intf,
67 const std::string& property)
68{
Vijay Khemka7452a862020-08-11 16:01:23 -070069 Value value;
70
Patrick Williams1226f202023-05-10 07:51:16 -050071 auto method = bus.new_method_call(service.c_str(), path.c_str(), propIntf,
72 methodGet);
Vijay Khemka7452a862020-08-11 16:01:23 -070073
74 method.append(intf, property);
75
Harvey Wu187582b2021-03-30 09:42:39 +080076 try
77 {
78 auto msg = bus.call(method);
79 msg.read(value);
80 }
Patrick Williams8e11ccc2022-07-22 19:26:57 -050081 catch (const sdbusplus::exception_t& ex)
Harvey Wu187582b2021-03-30 09:42:39 +080082 {
83 return std::numeric_limits<T>::quiet_NaN();
84 }
Vijay Khemka7452a862020-08-11 16:01:23 -070085
86 return std::get<T>(value);
87}
Tao Lin91799db2022-07-27 21:02:20 +080088
89int 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}