blob: 76092970825309a707ddc56ed2c878b4c24b4e85 [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 Grobelny973b4bb2022-04-25 17:07:27 +020010#include "state.hpp"
11#include "types/error_message.hpp"
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010012#include "types/readings.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010013#include "types/report_action.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000014#include "types/report_types.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010015#include "types/report_updates.hpp"
16#include "types/reporting_type.hpp"
Szymon Dompke3eb56862021-09-20 15:32:04 +020017#include "utils/circular_vector.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010018#include "utils/ensure.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010019#include "utils/messanger.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020020
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020021#include <boost/asio/io_context.hpp>
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020022#include <boost/asio/steady_timer.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020023#include <sdbusplus/asio/object_server.hpp>
24
25#include <chrono>
26#include <memory>
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010027#include <unordered_set>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020028
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010029class Report : public interfaces::Report, public interfaces::MetricListener
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020030{
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010031 class OnChangeContext
32 {
33 public:
34 OnChangeContext(Report& report) : report(report)
35 {}
36
37 ~OnChangeContext()
38 {
39 if (updated)
40 {
41 report.updateReadings();
42 }
43 }
44
45 void metricUpdated()
46 {
47 updated = true;
48 }
49
50 private:
51 Report& report;
52 bool updated = false;
53 };
54
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020055 public:
56 Report(boost::asio::io_context& ioc,
57 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010058 const std::string& reportId, const std::string& reportName,
59 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010060 std::vector<ReportAction> reportActions, const Milliseconds period,
Szymon Dompke3eb56862021-09-20 15:32:04 +020061 const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020062 interfaces::ReportManager& reportManager,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010063 interfaces::JsonStorage& reportStorage,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020064 std::vector<std::shared_ptr<interfaces::Metric>> metrics,
Szymon Dompkefdb06a12022-02-11 11:04:44 +010065 const interfaces::ReportFactory& reportFactory, const bool enabled,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010066 std::unique_ptr<interfaces::Clock> clock, Readings);
67 ~Report();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020068
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020069 Report(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020070 Report(Report&&) = delete;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020071 Report& operator=(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020072 Report& operator=(Report&&) = delete;
73
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010074 std::string getId() const override
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020075 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010076 return id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020077 }
78
79 std::string getPath() const override
80 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010081 return reportDir + id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020082 }
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020083
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010084 void metricUpdated() override;
85
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +020086 void activate();
87 void deactivate();
88
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020089 private:
Szymon Dompkefdb06a12022-02-11 11:04:44 +010090 std::unique_ptr<sdbusplus::asio::dbus_interface>
91 makeReportInterface(const interfaces::ReportFactory& reportFactory);
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010092 static void timerProcForPeriodicReport(boost::system::error_code,
93 Report& self);
94 static void timerProcForOnChangeReport(boost::system::error_code,
95 Report& self);
96 void scheduleTimerForPeriodicReport(Milliseconds interval);
97 void scheduleTimerForOnChangeReport();
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +010098 std::optional<uint64_t>
99 deduceAppendLimit(const uint64_t appendLimitIn) const;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200100 uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
101 const ReportingType reportingTypeIn) const;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100102 void setReadingBuffer(const ReportUpdates newReportUpdates);
Szymon Dompke3eb56862021-09-20 15:32:04 +0200103 void setReportUpdates(const ReportUpdates newReportUpdates);
104 static uint64_t getSensorCount(
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100105 const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100106 interfaces::JsonStorage::FilePath reportFileName() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100107 std::unordered_set<std::string>
108 collectTriggerIds(boost::asio::io_context& ioc) const;
109 bool storeConfiguration() const;
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100110 bool shouldStoreMetricValues() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100111 void updateReadings();
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200112 void scheduleTimer();
113 std::vector<ErrorMessage> verify() const;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200114
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100115 std::string id;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200116 std::string name;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200117 ReportingType reportingType;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000118 Milliseconds interval;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100119 std::unordered_set<ReportAction> reportActions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000120 ReadingParametersPastVersion readingParametersPastVersion;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100121 ReadingParameters readingParameters;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000122 bool persistency = false;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200123 uint64_t sensorCount;
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100124 std::optional<uint64_t> appendLimit;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200125 ReportUpdates reportUpdates;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200126 Readings readings = {};
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100127 CircularVector<ReadingData> readingsBuffer;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200128 std::shared_ptr<sdbusplus::asio::object_server> objServer;
129 std::unique_ptr<sdbusplus::asio::dbus_interface> reportIface;
130 std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200131 std::vector<std::shared_ptr<interfaces::Metric>> metrics;
132 boost::asio::steady_timer timer;
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100133 std::unordered_set<std::string> triggerIds;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200134
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100135 interfaces::JsonStorage& reportStorage;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100136 std::unique_ptr<interfaces::Clock> clock;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100137 utils::Messanger messanger;
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100138 std::optional<OnChangeContext> onChangeContext;
139 utils::Ensure<std::function<void()>> unregisterFromMetrics;
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200140 State<ReportFlags, Report, ReportFlags::enabled, ReportFlags::valid> state{
141 *this};
142 std::vector<ErrorMessage> errorMessages;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100143
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200144 public:
145 static constexpr const char* reportIfaceName =
146 "xyz.openbmc_project.Telemetry.Report";
147 static constexpr const char* reportDir =
148 "/xyz/openbmc_project/Telemetry/Reports/";
149 static constexpr const char* deleteIfaceName =
150 "xyz.openbmc_project.Object.Delete";
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100151 static constexpr size_t reportVersion = 6;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200152};