xref: /openbmc/telemetry/src/utils/dbus_mapper.hpp (revision 3b5ee98639e13641d142c9fce6663e2357da3b14)
1 #pragma once
2 
3 #include <boost/asio/spawn.hpp>
4 #include <sdbusplus/asio/object_server.hpp>
5 #include <xyz/openbmc_project/ObjectMapper/common.hpp>
6 #include <xyz/openbmc_project/Sensor/Value/common.hpp>
7 
8 #include <array>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 namespace utils
14 {
15 
16 using SensorPath = std::string;
17 using ServiceName = std::string;
18 using Ifaces = std::vector<std::string>;
19 using SensorIfaces = std::vector<std::pair<ServiceName, Ifaces>>;
20 using SensorTree = std::pair<SensorPath, SensorIfaces>;
21 using ObjectMapper = sdbusplus::common::xyz::openbmc_project::ObjectMapper;
22 using SensorValue = sdbusplus::common::xyz::openbmc_project::sensor::Value;
23 
24 constexpr std::array<const char*, 1> sensorInterfaces = {
25     SensorValue::interface};
26 
getSubTreeSensors(boost::asio::yield_context & yield,const std::shared_ptr<sdbusplus::asio::connection> & bus)27 inline std::vector<SensorTree> getSubTreeSensors(
28     boost::asio::yield_context& yield,
29     const std::shared_ptr<sdbusplus::asio::connection>& bus)
30 {
31     boost::system::error_code ec;
32 
33     auto tree = bus->yield_method_call<std::vector<SensorTree>>(
34         yield, ec, ObjectMapper::default_service, ObjectMapper::instance_path,
35         ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree,
36         SensorValue::namespace_path::value, 2, sensorInterfaces);
37     if (ec)
38     {
39         throw std::runtime_error("Failed to query ObjectMapper!");
40     }
41     return tree;
42 }
43 
getSubTreeSensors(const std::shared_ptr<sdbusplus::asio::connection> & bus)44 inline std::vector<SensorTree> getSubTreeSensors(
45     const std::shared_ptr<sdbusplus::asio::connection>& bus)
46 {
47     auto method_call = bus->new_method_call(
48         ObjectMapper::default_service, ObjectMapper::instance_path,
49         ObjectMapper::interface, ObjectMapper::method_names::get_sub_tree);
50     method_call.append(SensorValue::namespace_path::value, 2, sensorInterfaces);
51     auto reply = bus->call(method_call);
52 
53     auto tree = reply.unpack<std::vector<SensorTree>>();
54 
55     return tree;
56 }
57 
58 } // namespace utils
59