Tao Lin | f2e9422 | 2023-10-31 17:38:17 +0800 | [diff] [blame] | 1 | #include "dbusUtils.hpp" |
| 2 | |
| 3 | #include <xyz/openbmc_project/ObjectMapper/common.hpp> |
| 4 | |
| 5 | const char* propIntf = "org.freedesktop.DBus.Properties"; |
| 6 | const char* mapperBusName = "xyz.openbmc_project.ObjectMapper"; |
| 7 | const char* mapperPath = "/xyz/openbmc_project/object_mapper"; |
| 8 | const char* mapperIntf = |
| 9 | sdbusplus::common::xyz::openbmc_project::ObjectMapper::interface; |
| 10 | |
| 11 | const char* methodGetObject = "GetObject"; |
| 12 | const char* methodGet = "Get"; |
| 13 | const char* methodSet = "Set"; |
| 14 | |
| 15 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 16 | |
| 17 | std::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 | |
| 60 | int 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 Eda | 5f07fa3 | 2024-06-17 14:26:17 +0530 | [diff] [blame] | 74 | "Failed to set dbus property. service:{SERVICE} path:{PATH} intf:{INTF} Property:{PROP},{ERROR}", |
Tao Lin | f2e9422 | 2023-10-31 17:38:17 +0800 | [diff] [blame] | 75 | "SERVICE", service, "PATH", path, "INTF", intf, "PROP", property, |
| 76 | "ERROR", e); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |