1 #pragma once 2 3 #include "interfaces/clock.hpp" 4 #include "interfaces/json_storage.hpp" 5 #include "interfaces/metric.hpp" 6 #include "interfaces/metric_listener.hpp" 7 #include "interfaces/report.hpp" 8 #include "interfaces/report_factory.hpp" 9 #include "interfaces/report_manager.hpp" 10 #include "state.hpp" 11 #include "types/error_message.hpp" 12 #include "types/readings.hpp" 13 #include "types/report_action.hpp" 14 #include "types/report_types.hpp" 15 #include "types/report_updates.hpp" 16 #include "types/reporting_type.hpp" 17 #include "utils/circular_vector.hpp" 18 #include "utils/dbus_path_utils.hpp" 19 #include "utils/ensure.hpp" 20 #include "utils/messanger.hpp" 21 22 #include <boost/asio/io_context.hpp> 23 #include <boost/asio/steady_timer.hpp> 24 #include <sdbusplus/asio/object_server.hpp> 25 26 #include <chrono> 27 #include <memory> 28 #include <unordered_set> 29 30 class Report : public interfaces::Report, public interfaces::MetricListener 31 { 32 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 56 public: 57 Report(boost::asio::io_context& ioc, 58 const std::shared_ptr<sdbusplus::asio::object_server>& objServer, 59 const std::string& reportId, const std::string& reportName, 60 const ReportingType reportingType, 61 std::vector<ReportAction> reportActions, const Milliseconds period, 62 const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn, 63 interfaces::ReportManager& reportManager, 64 interfaces::JsonStorage& reportStorage, 65 std::vector<std::shared_ptr<interfaces::Metric>> metrics, 66 const interfaces::ReportFactory& reportFactory, const bool enabled, 67 std::unique_ptr<interfaces::Clock> clock, Readings); 68 ~Report(); 69 70 Report(const Report&) = delete; 71 Report(Report&&) = delete; 72 Report& operator=(const Report&) = delete; 73 Report& operator=(Report&&) = delete; 74 75 std::string getId() const override 76 { 77 return id; 78 } 79 80 std::string getPath() const override 81 { 82 return path.str; 83 } 84 85 void metricUpdated() override; 86 87 void activate(); 88 void deactivate(); 89 90 private: 91 std::unique_ptr<sdbusplus::asio::dbus_interface> 92 makeReportInterface(const interfaces::ReportFactory& reportFactory); 93 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(); 99 uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn, 100 const ReportingType reportingTypeIn) const; 101 void setReadingBuffer(const ReportUpdates newReportUpdates); 102 void setReportUpdates(const ReportUpdates newReportUpdates); 103 static uint64_t getMetricCount( 104 const std::vector<std::shared_ptr<interfaces::Metric>>& metrics); 105 interfaces::JsonStorage::FilePath reportFileName() const; 106 std::unordered_set<std::string> 107 collectTriggerIds(boost::asio::io_context& ioc) const; 108 bool storeConfiguration() const; 109 bool shouldStoreMetricValues() const; 110 void updateReadings(); 111 void scheduleTimer(); 112 std::vector<ErrorMessage> verify() const; 113 114 std::string id; 115 const sdbusplus::message::object_path path; 116 std::string name; 117 ReportingType reportingType; 118 Milliseconds interval; 119 std::unordered_set<ReportAction> reportActions; 120 ReadingParametersPastVersion readingParametersPastVersion; 121 ReadingParameters readingParameters; 122 bool persistency = false; 123 uint64_t metricCount; 124 uint64_t appendLimit; 125 ReportUpdates reportUpdates; 126 Readings readings = {}; 127 CircularVector<ReadingData> readingsBuffer; 128 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; 131 std::vector<std::shared_ptr<interfaces::Metric>> metrics; 132 boost::asio::steady_timer timer; 133 std::unordered_set<std::string> triggerIds; 134 135 interfaces::JsonStorage& reportStorage; 136 std::unique_ptr<interfaces::Clock> clock; 137 utils::Messanger messanger; 138 std::optional<OnChangeContext> onChangeContext; 139 utils::Ensure<std::function<void()>> unregisterFromMetrics; 140 State<ReportFlags, Report, ReportFlags::enabled, ReportFlags::valid> state{ 141 *this}; 142 std::vector<ErrorMessage> errorMessages; 143 144 public: 145 static constexpr const char* reportIfaceName = 146 "xyz.openbmc_project.Telemetry.Report"; 147 static constexpr const char* deleteIfaceName = 148 "xyz.openbmc_project.Object.Delete"; 149 static constexpr size_t reportVersion = 7; 150 }; 151