blob: 0b983b4452678b661a6a5eecbd8432cc6f438b02 [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 mapperResponseMsg.read(mapperResponse);
George Liuf3b75142021-06-10 11:22:50 +080034 if (mapperResponse.empty())
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053035 {
36 log<level::ERR>("ERROR reading mapper response",
Gunnar Mills94df8c92018-09-14 14:50:03 -050037 entry("PATH=%s", path.c_str()),
George Liuf3b75142021-06-10 11:22:50 +080038 entry("INTERFACE=%s", interface.c_str()));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053039
40 elog<InternalFailure>();
41 }
George Liuf3b75142021-06-10 11:22:50 +080042
43 // the value here will be the service name
44 return mapperResponse.cbegin()->first;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053045}
46
George Liuf3b75142021-06-10 11:22:50 +080047const PropertyValue getProperty(const std::string& objectPath,
48 const std::string& interface,
49 const std::string& propertyName)
50{
51 PropertyValue value{};
52
53 auto& bus = getBus();
54 auto service = getService(objectPath, interface);
55 if (service.empty())
56 {
57 return value;
58 }
59
60 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
61 DBUS_PROPERTY_IFACE, "Get");
62 method.append(interface, propertyName);
63
64 auto reply = bus.call(method);
65 reply.read(value);
66
67 return value;
68}
69
Matt Spinler5901abd2021-09-23 13:50:03 -050070std::vector<std::string>
71 getSubtreePaths(const std::vector<std::string>& interfaces,
72 const std::string& path)
73{
74 std::vector<std::string> paths;
75
76 auto& bus = getBus();
77 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
78 MAPPER_IFACE, "GetSubTreePaths");
79 method.append(path, 0, interfaces);
80
81 auto reply = bus.call(method);
82 reply.read(paths);
83
84 return paths;
85}
George Liuf3b75142021-06-10 11:22:50 +080086} // namespace utils
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053087} // namespace occ
88} // namespace open_power