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