blob: 97255e6f414a5a56bbe44e4dfdc620de20aa5cde [file] [log] [blame]
Carol Wangdc059392020-03-13 17:39:17 +08001#include "utils.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5namespace phosphor
6{
7namespace state
8{
9namespace manager
10{
11namespace utils
12{
13
14using namespace phosphor::logging;
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 {
39 log<level::ERR>("Error no matching service",
40 entry("PATH=%s", path.c_str()),
41 entry("INTERFACE=%s", interface.c_str()));
42 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 {
47 log<level::ERR>("Error in mapper call", entry("ERROR=%s", e.what()),
48 entry("PATH=%s", path.c_str()),
49 entry("INTERFACE=%s", interface.c_str()));
50 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