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