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