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