blob: 8961fe994cf1e8b6b1a48b2585272b50bd4d865c [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;
15using sdbusplus::exception::SdBusError;
16
17constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
18constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
19constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
20constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
21
22std::string getService(sdbusplus::bus::bus& bus, std::string path,
23 std::string interface)
24{
25 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
26 MAPPER_INTERFACE, "GetObject");
27
28 mapper.append(path, std::vector<std::string>({interface}));
29
30 std::vector<std::pair<std::string, std::vector<std::string>>>
31 mapperResponse;
32
33 try
34 {
35 auto mapperResponseMsg = bus.call(mapper);
36
37 mapperResponseMsg.read(mapperResponse);
38 if (mapperResponse.empty())
39 {
40 log<level::ERR>("Error no matching service",
41 entry("PATH=%s", path.c_str()),
42 entry("INTERFACE=%s", interface.c_str()));
43 throw std::runtime_error("Error no matching service");
44 }
45 }
46 catch (const SdBusError& e)
47 {
48 log<level::ERR>("Error in mapper call", entry("ERROR=%s", e.what()),
49 entry("PATH=%s", path.c_str()),
50 entry("INTERFACE=%s", interface.c_str()));
51 throw;
52 }
53
54 return mapperResponse.begin()->first;
55}
56
57void setProperty(sdbusplus::bus::bus& bus, const std::string& path,
58 const std::string& interface, const std::string& property,
59 const std::string& value)
60{
Patrick Williams2975e262020-05-13 18:01:09 -050061 std::variant<std::string> variantValue = value;
Carol Wangdc059392020-03-13 17:39:17 +080062 std::string service = getService(bus, path, interface);
63
64 auto method = bus.new_method_call(service.c_str(), path.c_str(),
65 PROPERTY_INTERFACE, "Set");
66
67 method.append(interface, property, variantValue);
68 bus.call_noreply(method);
69
70 return;
71}
72
73} // namespace utils
74} // namespace manager
75} // namespace state
76} // namespace phosphor