blob: 856e369744c7950d6adae34bddf8f23969d300f6 [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"
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020018#include "utils/dbus_path_utils.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010019#include "utils/ensure.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010020#include "utils/messanger.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020021
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020022#include <boost/asio/io_context.hpp>
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020023#include <boost/asio/steady_timer.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020024#include <sdbusplus/asio/object_server.hpp>
25
26#include <chrono>
27#include <memory>
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010028#include <unordered_set>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020029
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010030class Report : public interfaces::Report, public interfaces::MetricListener
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020031{
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010032 class OnChangeContext
33 {
34 public:
35 OnChangeContext(Report& report) : report(report)
36 {}
37
38 ~OnChangeContext()
39 {
40 if (updated)
41 {
42 report.updateReadings();
43 }
44 }
45
46 void metricUpdated()
47 {
48 updated = true;
49 }
50
51 private:
52 Report& report;
53 bool updated = false;
54 };
55
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020056 public:
57 Report(boost::asio::io_context& ioc,
58 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010059 const std::string& reportId, const std::string& reportName,
60 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010061 std::vector<ReportAction> reportActions, const Milliseconds period,
Szymon Dompke3eb56862021-09-20 15:32:04 +020062 const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020063 interfaces::ReportManager& reportManager,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010064 interfaces::JsonStorage& reportStorage,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020065 std::vector<std::shared_ptr<interfaces::Metric>> metrics,
Szymon Dompkefdb06a12022-02-11 11:04:44 +010066 const interfaces::ReportFactory& reportFactory, const bool enabled,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010067 std::unique_ptr<interfaces::Clock> clock, Readings);
68 ~Report();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020069
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020070 Report(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020071 Report(Report&&) = delete;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020072 Report& operator=(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020073 Report& operator=(Report&&) = delete;
74
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010075 std::string getId() const override
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020076 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010077 return id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020078 }
79
80 std::string getPath() const override
81 {
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020082 return path.str;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020083 }
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020084
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010085 void metricUpdated() override;
86
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +020087 void activate();
88 void deactivate();
89
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020090 private:
Szymon Dompkefdb06a12022-02-11 11:04:44 +010091 std::unique_ptr<sdbusplus::asio::dbus_interface>
92 makeReportInterface(const interfaces::ReportFactory& reportFactory);
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010093 static void timerProcForPeriodicReport(boost::system::error_code,
94 Report& self);
95 static void timerProcForOnChangeReport(boost::system::error_code,
96 Report& self);
97 void scheduleTimerForPeriodicReport(Milliseconds interval);
98 void scheduleTimerForOnChangeReport();
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +010099 std::optional<uint64_t>
100 deduceAppendLimit(const uint64_t appendLimitIn) const;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200101 uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
102 const ReportingType reportingTypeIn) const;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100103 void setReadingBuffer(const ReportUpdates newReportUpdates);
Szymon Dompke3eb56862021-09-20 15:32:04 +0200104 void setReportUpdates(const ReportUpdates newReportUpdates);
Szymon Dompke892f7c82022-10-12 09:54:22 +0200105 void updateSensorCount(const uint64_t newSensorCount);
Szymon Dompke3eb56862021-09-20 15:32:04 +0200106 static uint64_t getSensorCount(
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100107 const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100108 interfaces::JsonStorage::FilePath reportFileName() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100109 std::unordered_set<std::string>
110 collectTriggerIds(boost::asio::io_context& ioc) const;
111 bool storeConfiguration() const;
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100112 bool shouldStoreMetricValues() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100113 void updateReadings();
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200114 void scheduleTimer();
115 std::vector<ErrorMessage> verify() const;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200116
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100117 std::string id;
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200118 const sdbusplus::message::object_path path;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200119 std::string name;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200120 ReportingType reportingType;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000121 Milliseconds interval;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100122 std::unordered_set<ReportAction> reportActions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000123 ReadingParametersPastVersion readingParametersPastVersion;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100124 ReadingParameters readingParameters;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000125 bool persistency = false;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200126 uint64_t sensorCount;
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100127 std::optional<uint64_t> appendLimit;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200128 ReportUpdates reportUpdates;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200129 Readings readings = {};
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100130 CircularVector<ReadingData> readingsBuffer;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200131 std::shared_ptr<sdbusplus::asio::object_server> objServer;
132 std::unique_ptr<sdbusplus::asio::dbus_interface> reportIface;
133 std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200134 std::vector<std::shared_ptr<interfaces::Metric>> metrics;
135 boost::asio::steady_timer timer;
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100136 std::unordered_set<std::string> triggerIds;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200137
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100138 interfaces::JsonStorage& reportStorage;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100139 std::unique_ptr<interfaces::Clock> clock;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100140 utils::Messanger messanger;
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100141 std::optional<OnChangeContext> onChangeContext;
142 utils::Ensure<std::function<void()>> unregisterFromMetrics;
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200143 State<ReportFlags, Report, ReportFlags::enabled, ReportFlags::valid> state{
144 *this};
145 std::vector<ErrorMessage> errorMessages;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100146
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200147 public:
148 static constexpr const char* reportIfaceName =
149 "xyz.openbmc_project.Telemetry.Report";
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200150 static constexpr const char* deleteIfaceName =
151 "xyz.openbmc_project.Object.Delete";
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100152 static constexpr size_t reportVersion = 6;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200153};