blob: 213265cd4d0b32404d89ff1534a406627eb217be [file] [log] [blame]
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02001#pragma once
2
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01003#include "interfaces/clock.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01004#include "interfaces/json_storage.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02005#include "interfaces/metric.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +01006#include "interfaces/metric_listener.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02007#include "interfaces/report.hpp"
Szymon Dompkefdb06a12022-02-11 11:04:44 +01008#include "interfaces/report_factory.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02009#include "interfaces/report_manager.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010010#include "types/report_action.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000011#include "types/report_types.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010012#include "types/report_updates.hpp"
13#include "types/reporting_type.hpp"
Szymon Dompke3eb56862021-09-20 15:32:04 +020014#include "utils/circular_vector.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010015#include "utils/ensure.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010016#include "utils/messanger.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020017
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020018#include <boost/asio/io_context.hpp>
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020019#include <boost/asio/steady_timer.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020020#include <sdbusplus/asio/object_server.hpp>
21
22#include <chrono>
23#include <memory>
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010024#include <unordered_set>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020025
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010026class Report : public interfaces::Report, public interfaces::MetricListener
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020027{
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010028 class OnChangeContext
29 {
30 public:
31 OnChangeContext(Report& report) : report(report)
32 {}
33
34 ~OnChangeContext()
35 {
36 if (updated)
37 {
38 report.updateReadings();
39 }
40 }
41
42 void metricUpdated()
43 {
44 updated = true;
45 }
46
47 private:
48 Report& report;
49 bool updated = false;
50 };
51
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020052 public:
53 Report(boost::asio::io_context& ioc,
54 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010055 const std::string& reportId, const std::string& reportName,
56 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010057 std::vector<ReportAction> reportActions, const Milliseconds period,
Szymon Dompke3eb56862021-09-20 15:32:04 +020058 const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020059 interfaces::ReportManager& reportManager,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010060 interfaces::JsonStorage& reportStorage,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020061 std::vector<std::shared_ptr<interfaces::Metric>> metrics,
Szymon Dompkefdb06a12022-02-11 11:04:44 +010062 const interfaces::ReportFactory& reportFactory, const bool enabled,
63 std::unique_ptr<interfaces::Clock> clock);
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020064
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020065 Report(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020066 Report(Report&&) = delete;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020067 Report& operator=(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020068 Report& operator=(Report&&) = delete;
69
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010070 std::string getId() const override
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020071 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010072 return id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020073 }
74
75 std::string getPath() const override
76 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010077 return reportDir + id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020078 }
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020079
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010080 void metricUpdated() override;
81
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020082 private:
Szymon Dompkefdb06a12022-02-11 11:04:44 +010083 std::unique_ptr<sdbusplus::asio::dbus_interface>
84 makeReportInterface(const interfaces::ReportFactory& reportFactory);
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010085 static void timerProcForPeriodicReport(boost::system::error_code,
86 Report& self);
87 static void timerProcForOnChangeReport(boost::system::error_code,
88 Report& self);
89 void scheduleTimerForPeriodicReport(Milliseconds interval);
90 void scheduleTimerForOnChangeReport();
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +010091 std::optional<uint64_t>
92 deduceAppendLimit(const uint64_t appendLimitIn) const;
Szymon Dompke3eb56862021-09-20 15:32:04 +020093 uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
94 const ReportingType reportingTypeIn) const;
Szymon Dompkefdb06a12022-02-11 11:04:44 +010095 void setReadingBuffer(const ReportUpdates newReportUpdates);
Szymon Dompke3eb56862021-09-20 15:32:04 +020096 void setReportUpdates(const ReportUpdates newReportUpdates);
97 static uint64_t getSensorCount(
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010098 const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010099 interfaces::JsonStorage::FilePath fileName() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100100 std::unordered_set<std::string>
101 collectTriggerIds(boost::asio::io_context& ioc) const;
102 bool storeConfiguration() const;
103 void updateReadings();
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100104 void updateReportingType(ReportingType);
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200105
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100106 std::string id;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200107 std::string name;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200108 ReportingType reportingType;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000109 Milliseconds interval;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100110 std::unordered_set<ReportAction> reportActions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000111 ReadingParametersPastVersion readingParametersPastVersion;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100112 ReadingParameters readingParameters;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000113 bool persistency = false;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200114 uint64_t sensorCount;
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100115 std::optional<uint64_t> appendLimit;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200116 ReportUpdates reportUpdates;
117 CircularVector<ReadingData> readingsBuffer;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200118 Readings readings = {};
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200119 std::shared_ptr<sdbusplus::asio::object_server> objServer;
120 std::unique_ptr<sdbusplus::asio::dbus_interface> reportIface;
121 std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200122 std::vector<std::shared_ptr<interfaces::Metric>> metrics;
123 boost::asio::steady_timer timer;
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100124 std::unordered_set<std::string> triggerIds;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200125
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100126 interfaces::JsonStorage& reportStorage;
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200127 bool enabled;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100128 std::unique_ptr<interfaces::Clock> clock;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100129 utils::Messanger messanger;
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100130 std::optional<OnChangeContext> onChangeContext;
131 utils::Ensure<std::function<void()>> unregisterFromMetrics;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100132
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200133 public:
134 static constexpr const char* reportIfaceName =
135 "xyz.openbmc_project.Telemetry.Report";
136 static constexpr const char* reportDir =
137 "/xyz/openbmc_project/Telemetry/Reports/";
138 static constexpr const char* deleteIfaceName =
139 "xyz.openbmc_project.Object.Delete";
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100140 static constexpr size_t reportVersion = 6;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200141};