1 #pragma once
2 
3 #include "app.hpp"
4 #include "dbus_utility.hpp"
5 #include "query.hpp"
6 #include "registries/privilege_registry.hpp"
7 #include "utils/dbus_utils.hpp"
8 #include "utils/telemetry_utils.hpp"
9 #include "utils/time_utils.hpp"
10 
11 #include <sdbusplus/asio/property.hpp>
12 #include <sdbusplus/unpack_properties.hpp>
13 
14 namespace redfish
15 {
16 
17 inline void handleTelemetryServiceGet(
18     crow::App& app, const crow::Request& req,
19     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
20 {
21     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
22     {
23         return;
24     }
25     asyncResp->res.jsonValue["@odata.type"] =
26         "#TelemetryService.v1_2_1.TelemetryService";
27     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/TelemetryService";
28     asyncResp->res.jsonValue["Id"] = "TelemetryService";
29     asyncResp->res.jsonValue["Name"] = "Telemetry Service";
30 
31     asyncResp->res.jsonValue["MetricReportDefinitions"]["@odata.id"] =
32         "/redfish/v1/TelemetryService/MetricReportDefinitions";
33     asyncResp->res.jsonValue["MetricReports"]["@odata.id"] =
34         "/redfish/v1/TelemetryService/MetricReports";
35     asyncResp->res.jsonValue["Triggers"]["@odata.id"] =
36         "/redfish/v1/TelemetryService/Triggers";
37 
38     sdbusplus::asio::getAllProperties(
39         *crow::connections::systemBus, telemetry::service,
40         "/xyz/openbmc_project/Telemetry/Reports",
41         "xyz.openbmc_project.Telemetry.ReportManager",
42         [asyncResp](const boost::system::error_code& ec,
43                     const dbus::utility::DBusPropertiesMap& ret) {
44         if (ec == boost::system::errc::host_unreachable)
45         {
46             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
47             return;
48         }
49         if (ec)
50         {
51             BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
52             messages::internalError(asyncResp->res);
53             return;
54         }
55 
56         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
57 
58         const size_t* maxReports = nullptr;
59         const uint64_t* minInterval = nullptr;
60 
61         const bool success = sdbusplus::unpackPropertiesNoThrow(
62             dbus_utils::UnpackErrorPrinter(), ret, "MaxReports", maxReports,
63             "MinInterval", minInterval);
64 
65         if (!success)
66         {
67             messages::internalError(asyncResp->res);
68             return;
69         }
70 
71         if (maxReports != nullptr)
72         {
73             asyncResp->res.jsonValue["MaxReports"] = *maxReports;
74         }
75 
76         if (minInterval != nullptr)
77         {
78             asyncResp->res.jsonValue["MinCollectionInterval"] =
79                 time_utils::toDurationString(std::chrono::milliseconds(
80                     static_cast<time_t>(*minInterval)));
81         }
82         nlohmann::json::array_t supportedCollectionFunctions;
83         supportedCollectionFunctions.emplace_back("Maximum");
84         supportedCollectionFunctions.emplace_back("Minimum");
85         supportedCollectionFunctions.emplace_back("Average");
86         supportedCollectionFunctions.emplace_back("Summation");
87 
88         asyncResp->res.jsonValue["SupportedCollectionFunctions"] =
89             std::move(supportedCollectionFunctions);
90         });
91 }
92 
93 inline void requestRoutesTelemetryService(App& app)
94 {
95     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/")
96         .privileges(redfish::privileges::getTelemetryService)
97         .methods(boost::beast::http::verb::get)(
98             std::bind_front(handleTelemetryServiceGet, std::ref(app)));
99 }
100 
101 } // namespace redfish
102