blob: f2cbe9424c4f87bc610b20c3c17979a7f23b9415 [file] [log] [blame]
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +02001#include "report_manager.hpp"
2
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02003#include <sdbusplus/exception.hpp>
4
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +02005#include <system_error>
6
7constexpr const char* reportManagerIfaceName =
8 "xyz.openbmc_project.Telemetry.ReportManager";
9constexpr const char* reportManagerPath =
10 "/xyz/openbmc_project/Telemetry/Reports";
11
12ReportManager::ReportManager(
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020013 const std::shared_ptr<sdbusplus::asio::connection>& bus,
14 const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) :
15 objServer(objServerIn)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020016{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020017 reports.reserve(maxReports);
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020018
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020019 reportManagerIface = objServer->add_unique_interface(
20 reportManagerPath, reportManagerIfaceName,
21 [this, &ioc = bus->get_io_context()](auto& dbusIface) {
22 dbusIface.register_property_r(
23 "MaxReports", uint32_t{}, sdbusplus::vtable::property_::const_,
24 [](const auto&) { return maxReports; });
25 dbusIface.register_property_r(
26 "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_,
27 [](const auto&) -> uint64_t { return minInterval.count(); });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020028
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020029 dbusIface.register_method(
30 "AddReport",
31 [this, &ioc](const std::string& reportName,
32 const std::string& reportingType,
33 const bool emitsReadingsUpdate,
34 const bool logToMetricReportsCollection,
35 const uint64_t interval,
36 const ReadingParameters& metricParams) {
37 if (reports.size() >= maxReports)
38 {
39 throw sdbusplus::exception::SdBusError(
40 static_cast<int>(std::errc::too_many_files_open),
41 "Reached maximal report count");
42 }
43
44 for (const auto& report : reports)
45 {
46 if (report->name == reportName)
47 {
48 throw sdbusplus::exception::SdBusError(
49 static_cast<int>(std::errc::file_exists),
50 "Duplicate report");
51 }
52 }
53
54 std::chrono::milliseconds reportInterval{interval};
55 if (reportInterval < minInterval)
56 {
57 throw sdbusplus::exception::SdBusError(
58 static_cast<int>(std::errc::invalid_argument),
59 "Invalid interval");
60 }
61
62 reports.emplace_back(std::make_unique<Report>(
63 ioc, objServer, reportName, reportingType,
64 emitsReadingsUpdate, logToMetricReportsCollection,
65 std::move(reportInterval), metricParams, *this));
66 return reports.back()->path;
67 });
68 });
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020069}
70
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020071void ReportManager::removeReport(const Report* report)
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020072{
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020073 reports.erase(
74 std::remove_if(reports.begin(), reports.end(),
75 [report](const auto& x) { return report == x.get(); }),
76 reports.end());
Krzysztof Grobelny64b75a52020-09-18 10:17:16 +020077}