blob: a067663f15232908c8bc9f825e5f2a934d8a4533 [file] [log] [blame]
George Liuf3b75142021-06-10 11:22:50 +08001#include "utils.hpp"
2
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +05303#include <phosphor-logging/elog-errors.hpp>
Gunnar Mills94df8c92018-09-14 14:50:03 -05004#include <sdbusplus/bus.hpp>
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +05305#include <xyz/openbmc_project/Common/error.hpp>
George Liub5ca1012021-09-10 12:53:11 +08006
7#include <string>
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +05308namespace open_power
9{
10namespace occ
11{
George Liuf3b75142021-06-10 11:22:50 +080012namespace utils
13{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053014// For throwing exceptions
15using namespace phosphor::logging;
Gunnar Mills94df8c92018-09-14 14:50:03 -050016using InternalFailure =
17 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053018
George Liuf3b75142021-06-10 11:22:50 +080019const std::string getService(const std::string& path,
20 const std::string& interface)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053021{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053022
George Liuf3b75142021-06-10 11:22:50 +080023 using InterfaceList = std::vector<std::string>;
24 std::map<std::string, std::vector<std::string>> mapperResponse;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053025
George Liuf3b75142021-06-10 11:22:50 +080026 auto& bus = getBus();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053027
George Liuf3b75142021-06-10 11:22:50 +080028 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
29 MAPPER_IFACE, "GetObject");
30 mapper.append(path, InterfaceList({interface}));
31
32 auto mapperResponseMsg = bus.call(mapper);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053033 if (mapperResponseMsg.is_method_error())
34 {
35 log<level::ERR>("ERROR in getting service",
Gunnar Mills94df8c92018-09-14 14:50:03 -050036 entry("PATH=%s", path.c_str()),
George Liuf3b75142021-06-10 11:22:50 +080037 entry("INTERFACE=%s", interface.c_str()));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053038
39 elog<InternalFailure>();
40 }
41
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053042 mapperResponseMsg.read(mapperResponse);
George Liuf3b75142021-06-10 11:22:50 +080043 if (mapperResponse.empty())
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053044 {
45 log<level::ERR>("ERROR reading mapper response",
Gunnar Mills94df8c92018-09-14 14:50:03 -050046 entry("PATH=%s", path.c_str()),
George Liuf3b75142021-06-10 11:22:50 +080047 entry("INTERFACE=%s", interface.c_str()));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053048
49 elog<InternalFailure>();
50 }
George Liuf3b75142021-06-10 11:22:50 +080051
52 // the value here will be the service name
53 return mapperResponse.cbegin()->first;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053054}
55
George Liuf3b75142021-06-10 11:22:50 +080056const PropertyValue getProperty(const std::string& objectPath,
57 const std::string& interface,
58 const std::string& propertyName)
59{
60 PropertyValue value{};
61
62 auto& bus = getBus();
63 auto service = getService(objectPath, interface);
64 if (service.empty())
65 {
66 return value;
67 }
68
69 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
70 DBUS_PROPERTY_IFACE, "Get");
71 method.append(interface, propertyName);
72
73 auto reply = bus.call(method);
74 reply.read(value);
75
76 return value;
77}
78
Matt Spinler5901abd2021-09-23 13:50:03 -050079std::vector<std::string>
80 getSubtreePaths(const std::vector<std::string>& interfaces,
81 const std::string& path)
82{
83 std::vector<std::string> paths;
84
85 auto& bus = getBus();
86 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
87 MAPPER_IFACE, "GetSubTreePaths");
88 method.append(path, 0, interfaces);
89
90 auto reply = bus.call(method);
91 reply.read(paths);
92
93 return paths;
94}
George Liuf3b75142021-06-10 11:22:50 +080095} // namespace utils
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053096} // namespace occ
97} // namespace open_power