blob: de1ae4b566b2fb5fa8e2238cd0b8f52ba98e2386 [file] [log] [blame]
Carol Wangdc059392020-03-13 17:39:17 +08001#include "utils.hpp"
2
Andrew Geissler8ffdb262021-09-20 15:25:19 -05003#include <phosphor-logging/lg2.hpp>
Carol Wangdc059392020-03-13 17:39:17 +08004
5namespace phosphor
6{
7namespace state
8{
9namespace manager
10{
11namespace utils
12{
13
Andrew Geissler8ffdb262021-09-20 15:25:19 -050014PHOSPHOR_LOG2_USING;
Carol Wangdc059392020-03-13 17:39:17 +080015
16constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
17constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
18constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
19constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
20
21std::string getService(sdbusplus::bus::bus& bus, std::string path,
22 std::string interface)
23{
24 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
25 MAPPER_INTERFACE, "GetObject");
26
27 mapper.append(path, std::vector<std::string>({interface}));
28
29 std::vector<std::pair<std::string, std::vector<std::string>>>
30 mapperResponse;
31
32 try
33 {
34 auto mapperResponseMsg = bus.call(mapper);
35
36 mapperResponseMsg.read(mapperResponse);
37 if (mapperResponse.empty())
38 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050039 error("Error no matching service with path {PATH} and interface "
40 "{INTERFACE}",
41 "PATH", path, "INTERFACE", interface);
Carol Wangdc059392020-03-13 17:39:17 +080042 throw std::runtime_error("Error no matching service");
43 }
44 }
Patrick Williams0a675212021-09-02 09:49:43 -050045 catch (const sdbusplus::exception::exception& e)
Carol Wangdc059392020-03-13 17:39:17 +080046 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050047 error("Error in mapper call with path {PATH}, interface "
48 "{INTERFACE}, and exception {ERROR}",
49 "PATH", path, "INTERFACE", interface, "ERROR", e);
Carol Wangdc059392020-03-13 17:39:17 +080050 throw;
51 }
52
53 return mapperResponse.begin()->first;
54}
55
56void setProperty(sdbusplus::bus::bus& bus, const std::string& path,
57 const std::string& interface, const std::string& property,
58 const std::string& value)
59{
Patrick Williams2975e262020-05-13 18:01:09 -050060 std::variant<std::string> variantValue = value;
Carol Wangdc059392020-03-13 17:39:17 +080061 std::string service = getService(bus, path, interface);
62
63 auto method = bus.new_method_call(service.c_str(), path.c_str(),
64 PROPERTY_INTERFACE, "Set");
65
66 method.append(interface, property, variantValue);
67 bus.call_noreply(method);
68
69 return;
70}
71
72} // namespace utils
73} // namespace manager
74} // namespace state
75} // namespace phosphor