blob: ca6804cc9ad397974431fc8a813148b2ecd5f1bc [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "trigger.hpp"
2
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01003#include "messages/collect_trigger_id.hpp"
4#include "messages/trigger_presence_changed_ind.hpp"
Szymon Dompkee28aa532021-10-27 12:33:12 +02005#include "trigger_manager.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00006#include "types/report_types.hpp"
7#include "types/trigger_types.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01008#include "utils/contains.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01009#include "utils/conversion_trigger.hpp"
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020010#include "utils/dbus_path_utils.hpp"
Cezary Zwolaka4e67612021-02-18 13:16:16 +010011#include "utils/transform.hpp"
12
13#include <phosphor-logging/log.hpp>
14
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010015Trigger::Trigger(
16 boost::asio::io_context& ioc,
17 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020018 TriggerId&& idIn, const std::string& nameIn,
Szymon Dompke94f71c52021-12-10 07:16:33 +010019 const std::vector<TriggerAction>& triggerActionsIn,
20 const std::shared_ptr<std::vector<std::string>> reportIdsIn,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010021 std::vector<std::shared_ptr<interfaces::Threshold>>&& thresholdsIn,
Cezary Zwolaka4e67612021-02-18 13:16:16 +010022 interfaces::TriggerManager& triggerManager,
Szymon Dompke94f71c52021-12-10 07:16:33 +010023 interfaces::JsonStorage& triggerStorageIn,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010024 const interfaces::TriggerFactory& triggerFactory, Sensors sensorsIn) :
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020025 id(std::move(idIn)),
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020026 path(utils::pathAppend(utils::constants::triggerDirPath, *id)),
Szymon Dompkee28aa532021-10-27 12:33:12 +020027 name(nameIn), triggerActions(std::move(triggerActionsIn)),
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020028 reportIds(std::move(reportIdsIn)), thresholds(std::move(thresholdsIn)),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020029 fileName(std::to_string(std::hash<std::string>{}(*id))),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010030 triggerStorage(triggerStorageIn), sensors(std::move(sensorsIn)),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010031 messanger(ioc)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010032{
33 deleteIface = objServer->add_unique_interface(
34 path, deleteIfaceName, [this, &ioc, &triggerManager](auto& dbusIface) {
Patrick Williamsc7935fa2023-10-20 11:19:30 -050035 dbusIface.register_method("Delete", [this, &ioc, &triggerManager] {
36 if (persistent)
37 {
38 triggerStorage.remove(fileName);
39 }
40 messanger.send(messages::TriggerPresenceChangedInd{
41 messages::Presence::Removed, *id, {}});
42 boost::asio::post(ioc, [this, &triggerManager] {
43 triggerManager.removeTrigger(this);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010044 });
45 });
Patrick Williamsc7935fa2023-10-20 11:19:30 -050046 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010047
48 triggerIface = objServer->add_unique_interface(
Szymon Dompke94f71c52021-12-10 07:16:33 +010049 path, triggerIfaceName, [this, &triggerFactory](auto& dbusIface) {
Patrick Williamsc7935fa2023-10-20 11:19:30 -050050 persistent = storeConfiguration();
51 dbusIface.register_property_rw(
52 "Persistent", persistent,
53 sdbusplus::vtable::property_::emits_change,
54 [this](bool newVal, const auto&) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050055 if (newVal == persistent)
56 {
57 return 1;
58 }
59 if (newVal)
60 {
61 persistent = storeConfiguration();
62 }
63 else
64 {
65 triggerStorage.remove(fileName);
66 persistent = false;
67 }
68 return 1;
Patrick Williamsf535cad2024-08-16 15:21:20 -040069 }, [this](const auto&) { return persistent; });
Cezary Zwolaka4e67612021-02-18 13:16:16 +010070
Patrick Williamsc7935fa2023-10-20 11:19:30 -050071 dbusIface.register_property_rw(
72 "Thresholds", TriggerThresholdParams{},
73 sdbusplus::vtable::property_::emits_change,
74 [this, &triggerFactory](auto newVal, auto& oldVal) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050075 auto newThresholdParams =
76 std::visit(utils::ToLabeledThresholdParamConversion(), newVal);
77 TriggerManager::verifyThresholdParams(newThresholdParams);
78 triggerFactory.updateThresholds(thresholds, *id, triggerActions,
79 reportIds, sensors,
80 newThresholdParams);
81 oldVal = std::move(newVal);
82 return 1;
Patrick Williamsf535cad2024-08-16 15:21:20 -040083 }, [this](const auto&) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050084 return fromLabeledThresholdParam(getLabeledThresholds());
Patrick Williamsc7935fa2023-10-20 11:19:30 -050085 });
Cezary Zwolak4416fce2021-03-17 03:21:06 +010086
Patrick Williamsc7935fa2023-10-20 11:19:30 -050087 dbusIface.register_property_rw(
88 "Sensors", SensorsInfo{},
89 sdbusplus::vtable::property_::emits_change,
90 [this, &triggerFactory](auto newVal, auto& oldVal) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050091 auto labeledSensorInfo =
92 triggerFactory.getLabeledSensorsInfo(newVal);
93 triggerFactory.updateSensors(sensors, labeledSensorInfo);
94 for (const auto& threshold : thresholds)
95 {
96 threshold->updateSensors(sensors);
97 }
98 oldVal = std::move(newVal);
99 return 1;
Patrick Williamsf535cad2024-08-16 15:21:20 -0400100 }, [this](const auto&) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500101 return utils::fromLabeledSensorsInfo(getLabeledSensorInfo());
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500102 });
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100103
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500104 dbusIface.register_property_rw(
105 "Reports", std::vector<sdbusplus::message::object_path>(),
106 sdbusplus::vtable::property_::emits_change,
107 [this](auto newVal, auto& oldVal) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500108 auto newReportIds = utils::transform<std::vector>(
109 newVal,
110 [](const auto& path) { return utils::reportPathToId(path); });
111 TriggerManager::verifyReportIds(newReportIds);
112 *reportIds = newReportIds;
113 messanger.send(messages::TriggerPresenceChangedInd{
114 messages::Presence::Exist, *id, *reportIds});
115 oldVal = std::move(newVal);
116 return 1;
Patrick Williamsf535cad2024-08-16 15:21:20 -0400117 }, [this](const auto&) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500118 return utils::transform<std::vector>(*reportIds,
119 [](const auto& id) {
120 return utils::pathAppend(utils::constants::reportDirPath, id);
121 });
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500122 });
Szymon Dompke20013012021-07-23 09:54:20 +0200123
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500124 dbusIface.register_property_r(
125 "Discrete", isDiscreate(), sdbusplus::vtable::property_::const_,
126 [this](const auto& x) { return isDiscreate(); });
Szymon Dompke20013012021-07-23 09:54:20 +0200127
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500128 dbusIface.register_property_rw(
129 "Name", name, sdbusplus::vtable::property_::emits_change,
130 [this](auto newVal, auto& oldVal) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500131 if (newVal.length() > utils::constants::maxIdNameLength)
132 {
133 throw errors::InvalidArgument("Name", "Name is too long.");
134 }
135 name = oldVal = newVal;
136 return 1;
Patrick Williamsf535cad2024-08-16 15:21:20 -0400137 }, [this](const auto&) { return name; });
Szymon Dompkee28aa532021-10-27 12:33:12 +0200138
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500139 dbusIface.register_property_r(
140 "TriggerActions", std::vector<std::string>(),
141 sdbusplus::vtable::property_::const_, [this](const auto&) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500142 return utils::transform(triggerActions, [](const auto& action) {
143 return actionToString(action);
144 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100145 });
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500146 });
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100147
148 for (const auto& threshold : thresholds)
149 {
150 threshold->initialize();
151 }
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100152
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100153 messanger.on_receive<messages::CollectTriggerIdReq>(
154 [this](const auto& msg) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500155 if (utils::contains(*reportIds, msg.reportId))
156 {
157 messanger.send(messages::CollectTriggerIdResp{*id});
158 }
159 });
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100160
161 messanger.send(messages::TriggerPresenceChangedInd{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200162 messages::Presence::Exist, *id, *reportIds});
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100163}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100164
165bool Trigger::storeConfiguration() const
166{
167 try
168 {
169 nlohmann::json data;
170
Szymon Dompke94f71c52021-12-10 07:16:33 +0100171 auto labeledThresholdParams =
172 std::visit(utils::ToLabeledThresholdParamConversion(),
Krzysztof Grobelny55824552022-02-18 16:15:31 +0100173 fromLabeledThresholdParam(getLabeledThresholds()));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100174
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100175 data["Version"] = triggerVersion;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200176 data["Id"] = *id;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100177 data["Name"] = name;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100178 data["ThresholdParamsDiscriminator"] = labeledThresholdParams.index();
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500179 data["TriggerActions"] = utils::transform(
180 triggerActions,
181 [](const auto& action) { return actionToString(action); });
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100182 data["ThresholdParams"] =
183 utils::labeledThresholdParamsToJson(labeledThresholdParams);
Szymon Dompke94f71c52021-12-10 07:16:33 +0100184 data["ReportIds"] = *reportIds;
Krzysztof Grobelny55824552022-02-18 16:15:31 +0100185 data["Sensors"] = getLabeledSensorInfo();
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100186
187 triggerStorage.store(fileName, data);
188 }
189 catch (const std::exception& e)
190 {
191 phosphor::logging::log<phosphor::logging::level::ERR>(
192 "Failed to store a trigger in storage",
193 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
194 return false;
195 }
196 return true;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100197}
Krzysztof Grobelny55824552022-02-18 16:15:31 +0100198
199std::vector<LabeledSensorInfo> Trigger::getLabeledSensorInfo() const
200{
201 return utils::transform(sensors, [](const auto& sensor) {
202 return sensor->getLabeledSensorInfo();
203 });
204}
205
206std::vector<LabeledThresholdParam> Trigger::getLabeledThresholds() const
207{
208 return utils::transform(thresholds, [](const auto& threshold) {
209 return threshold->getThresholdParam();
210 });
211}
212
213bool Trigger::isDiscreate() const
214{
215 const auto labeledThresholds = getLabeledThresholds();
216
217 return utils::isFirstElementOfType<std::monostate>(labeledThresholds) ||
218 utils::isFirstElementOfType<discrete::LabeledThresholdParam>(
219 labeledThresholds);
220}