blob: 1096ed0672037f85ba7d79fea8908878774f8323 [file] [log] [blame]
Tao Linf2e94222023-10-31 17:38:17 +08001#include "dbusUtils.hpp"
2
3#include <xyz/openbmc_project/ObjectMapper/common.hpp>
4
5const char* propIntf = "org.freedesktop.DBus.Properties";
6const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
7const char* mapperPath = "/xyz/openbmc_project/object_mapper";
8const char* mapperIntf =
9 sdbusplus::common::xyz::openbmc_project::ObjectMapper::interface;
10
11const char* methodGetObject = "GetObject";
12const char* methodGet = "Get";
13const char* methodSet = "Set";
14
15using namespace sdbusplus::xyz::openbmc_project::Common::Error;
16
17std::string getService(sdbusplus::bus_t& bus, const std::string& path,
18 const char* intf)
19{
20 /* Get mapper object for sensor path */
21 auto mapper = bus.new_method_call(mapperBusName, mapperPath, mapperIntf,
22 methodGetObject);
23
24 mapper.append(path.c_str());
25 mapper.append(std::vector<std::string>({intf}));
26
27 std::unordered_map<std::string, std::vector<std::string>> resp;
28
29 try
30 {
31 auto msg = bus.call(mapper);
32 msg.read(resp);
33 }
34 catch (const sdbusplus::exception_t& ex)
35 {
36 if (ex.name() == std::string(sdbusplus::xyz::openbmc_project::Common::
37 Error::ResourceNotFound::errName))
38 {
39 // The service isn't on D-Bus yet.
40 return std::string{};
41 }
42 else if (ex.name() == std::string("org.freedesktop.DBus.Error.Timeout"))
43 {
44 lg2::info("Mapper timeout while looking up {PATH}", "PATH", path);
45 return std::string{};
46 }
47
48 throw;
49 }
50
51 if (resp.begin() == resp.end())
52 {
53 // Shouldn't happen, if the mapper can't find it it is handled above.
54 throw std::runtime_error("Unable to find Object: " + path);
55 }
56
57 return resp.begin()->first;
58}
59
60int setDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
61 const std::string& path, const std::string& intf,
62 const std::string& property, const Value& value)
63{
64 try
65 {
66 auto method = bus.new_method_call(service.c_str(), path.c_str(),
67 propIntf, methodSet);
68 method.append(intf, property, value);
69 auto msg = bus.call(method);
70 }
71 catch (const sdbusplus::exception_t& e)
72 {
73 lg2::error(
Manojkiran Eda5f07fa32024-06-17 14:26:17 +053074 "Failed to set dbus property. service:{SERVICE} path:{PATH} intf:{INTF} Property:{PROP},{ERROR}",
Tao Linf2e94222023-10-31 17:38:17 +080075 "SERVICE", service, "PATH", path, "INTF", intf, "PROP", property,
76 "ERROR", e);
77 return -1;
78 }
79
80 return 0;
81}