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/ensure.hpp" 19 #include "utils/messanger.hpp" 20 21 #include <boost/asio/io_context.hpp> 22 #include <boost/asio/steady_timer.hpp> 23 #include <sdbusplus/asio/object_server.hpp> 24 25 #include <chrono> 26 #include <memory> 27 #include <unordered_set> 28 29 class Report : public interfaces::Report, public interfaces::MetricListener 30 { 31 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 55 public: 56 Report(boost::asio::io_context& ioc, 57 const std::shared_ptr<sdbusplus::asio::object_server>& objServer, 58 const std::string& reportId, const std::string& reportName, 59 const ReportingType reportingType, 60 std::vector<ReportAction> reportActions, const Milliseconds period, 61 const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn, 62 interfaces::ReportManager& reportManager, 63 interfaces::JsonStorage& reportStorage, 64 std::vector<std::shared_ptr<interfaces::Metric>> metrics, 65 const interfaces::ReportFactory& reportFactory, const bool enabled, 66 std::unique_ptr<interfaces::Clock> clock, Readings); 67 ~Report(); 68 69 Report(const Report&) = delete; 70 Report(Report&&) = delete; 71 Report& operator=(const Report&) = delete; 72 Report& operator=(Report&&) = delete; 73 74 std::string getId() const override 75 { 76 return id; 77 } 78 79 std::string getPath() const override 80 { 81 return reportDir + id; 82 } 83 84 void metricUpdated() override; 85 86 void activate(); 87 void deactivate(); 88 89 private: 90 std::unique_ptr<sdbusplus::asio::dbus_interface> 91 makeReportInterface(const interfaces::ReportFactory& reportFactory); 92 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(); 98 std::optional<uint64_t> 99 deduceAppendLimit(const uint64_t appendLimitIn) const; 100 uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn, 101 const ReportingType reportingTypeIn) const; 102 void setReadingBuffer(const ReportUpdates newReportUpdates); 103 void setReportUpdates(const ReportUpdates newReportUpdates); 104 static uint64_t getSensorCount( 105 const std::vector<std::shared_ptr<interfaces::Metric>>& metrics); 106 interfaces::JsonStorage::FilePath reportFileName() const; 107 std::unordered_set<std::string> 108 collectTriggerIds(boost::asio::io_context& ioc) const; 109 bool storeConfiguration() const; 110 bool shouldStoreMetricValues() const; 111 void updateReadings(); 112 void scheduleTimer(); 113 std::vector<ErrorMessage> verify() const; 114 115 std::string id; 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 sensorCount; 124 std::optional<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* reportDir = 148 "/xyz/openbmc_project/Telemetry/Reports/"; 149 static constexpr const char* deleteIfaceName = 150 "xyz.openbmc_project.Object.Delete"; 151 static constexpr size_t reportVersion = 6; 152 }; 153