1 #include <phosphor-logging/elog-errors.hpp>
2 #include <phosphor-logging/lg2.hpp>
3 #include <xyz/openbmc_project/Common/error.hpp>
4 
5 const constexpr char* entityManagerBusName =
6     "xyz.openbmc_project.EntityManager";
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";
14 const char* methodSet = "Set";
15 
16 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17 
18 using Value = std::variant<int64_t, double, std::string, bool>;
19 
20 std::string getService(sdbusplus::bus_t& bus, const std::string& path,
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);
35         msg.read(resp);
36     }
37     catch (const sdbusplus::exception_t& ex)
38     {
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         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         }
50 
51         throw;
52     }
53 
54     if (resp.begin() == resp.end())
55     {
56         // Shouldn't happen, if the mapper can't find it it is handled above.
57         throw std::runtime_error("Unable to find Object: " + path);
58     }
59 
60     return resp.begin()->first;
61 }
62 
63 template <typename T>
64 
65 T getDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
66                   const std::string& path, const std::string& intf,
67                   const std::string& property)
68 {
69     Value value;
70 
71     auto method = bus.new_method_call(service.c_str(), path.c_str(), propIntf,
72                                       methodGet);
73 
74     method.append(intf, property);
75 
76     try
77     {
78         auto msg = bus.call(method);
79         msg.read(value);
80     }
81     catch (const sdbusplus::exception_t& ex)
82     {
83         return std::numeric_limits<T>::quiet_NaN();
84     }
85 
86     return std::get<T>(value);
87 }
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 }
111