Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 1 | #include "report_manager.hpp" |
| 2 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 3 | #include <sdbusplus/exception.hpp> |
| 4 | |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 5 | #include <system_error> |
| 6 | |
| 7 | constexpr const char* reportManagerIfaceName = |
| 8 | "xyz.openbmc_project.Telemetry.ReportManager"; |
| 9 | constexpr const char* reportManagerPath = |
| 10 | "/xyz/openbmc_project/Telemetry/Reports"; |
| 11 | |
| 12 | ReportManager::ReportManager( |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 13 | const std::shared_ptr<sdbusplus::asio::connection>& bus, |
| 14 | const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) : |
| 15 | objServer(objServerIn) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 16 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 17 | reports.reserve(maxReports); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 18 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 19 | 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 Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 28 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 29 | 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 Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 71 | void ReportManager::removeReport(const Report* report) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 72 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 73 | reports.erase( |
| 74 | std::remove_if(reports.begin(), reports.end(), |
| 75 | [report](const auto& x) { return report == x.get(); }), |
| 76 | reports.end()); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 77 | } |