blob: 69822675e17b8bb1916a49cfd16cc53a96c1f19c [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"
Cezary Zwolaka4e67612021-02-18 13:16:16 +010010#include "utils/transform.hpp"
11
12#include <phosphor-logging/log.hpp>
13
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010014Trigger::Trigger(
15 boost::asio::io_context& ioc,
16 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Szymon Dompkee28aa532021-10-27 12:33:12 +020017 const std::string& idIn, const std::string& nameIn,
Szymon Dompke94f71c52021-12-10 07:16:33 +010018 const std::vector<TriggerAction>& triggerActionsIn,
19 const std::shared_ptr<std::vector<std::string>> reportIdsIn,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010020 std::vector<std::shared_ptr<interfaces::Threshold>>&& thresholdsIn,
Cezary Zwolaka4e67612021-02-18 13:16:16 +010021 interfaces::TriggerManager& triggerManager,
Szymon Dompke94f71c52021-12-10 07:16:33 +010022 interfaces::JsonStorage& triggerStorageIn,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010023 const interfaces::TriggerFactory& triggerFactory, Sensors sensorsIn) :
Szymon Dompkee28aa532021-10-27 12:33:12 +020024 id(idIn),
25 name(nameIn), triggerActions(std::move(triggerActionsIn)),
Szymon Dompke94f71c52021-12-10 07:16:33 +010026 path(triggerDir + id), reportIds(std::move(reportIdsIn)),
Cezary Zwolak4416fce2021-03-17 03:21:06 +010027 thresholds(std::move(thresholdsIn)),
Szymon Dompkee28aa532021-10-27 12:33:12 +020028 fileName(std::to_string(std::hash<std::string>{}(id))),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010029 triggerStorage(triggerStorageIn), sensors(std::move(sensorsIn)),
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010030 messanger(ioc)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010031{
32 deleteIface = objServer->add_unique_interface(
33 path, deleteIfaceName, [this, &ioc, &triggerManager](auto& dbusIface) {
34 dbusIface.register_method("Delete", [this, &ioc, &triggerManager] {
Cezary Zwolaka4e67612021-02-18 13:16:16 +010035 if (persistent)
36 {
37 triggerStorage.remove(fileName);
38 }
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010039 messanger.send(messages::TriggerPresenceChangedInd{
40 messages::Presence::Removed, id, {}});
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010041 boost::asio::post(ioc, [this, &triggerManager] {
42 triggerManager.removeTrigger(this);
43 });
44 });
45 });
46
47 triggerIface = objServer->add_unique_interface(
Szymon Dompke94f71c52021-12-10 07:16:33 +010048 path, triggerIfaceName, [this, &triggerFactory](auto& dbusIface) {
Cezary Zwolaka4e67612021-02-18 13:16:16 +010049 persistent = storeConfiguration();
50 dbusIface.register_property_rw(
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010051 "Persistent", persistent,
52 sdbusplus::vtable::property_::emits_change,
Cezary Zwolaka4e67612021-02-18 13:16:16 +010053 [this](bool newVal, const auto&) {
54 if (newVal == persistent)
55 {
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +010056 return 1;
Cezary Zwolaka4e67612021-02-18 13:16:16 +010057 }
58 if (newVal)
59 {
60 persistent = storeConfiguration();
61 }
62 else
63 {
64 triggerStorage.remove(fileName);
65 persistent = false;
66 }
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +010067 return 1;
Cezary Zwolaka4e67612021-02-18 13:16:16 +010068 },
69 [this](const auto&) { return persistent; });
70
Szymon Dompke94f71c52021-12-10 07:16:33 +010071 dbusIface.register_property_rw(
Cezary Zwolak4416fce2021-03-17 03:21:06 +010072 "Thresholds", TriggerThresholdParams{},
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010073 sdbusplus::vtable::property_::emits_change,
Szymon Dompke94f71c52021-12-10 07:16:33 +010074 [this, &triggerFactory](auto newVal, auto& oldVal) {
75 auto newThresholdParams = std::visit(
76 utils::ToLabeledThresholdParamConversion(), newVal);
77 triggerFactory.updateThresholds(thresholds, triggerActions,
78 reportIds, sensors,
79 newThresholdParams);
80 oldVal = std::move(newVal);
81 return 1;
82 },
Cezary Zwolak4416fce2021-03-17 03:21:06 +010083 [this](const auto&) {
Szymon Dompke94f71c52021-12-10 07:16:33 +010084 return fromLabeledThresholdParam(
85 utils::transform(thresholds, [](const auto& threshold) {
86 return threshold->getThresholdParam();
87 }));
Cezary Zwolak4416fce2021-03-17 03:21:06 +010088 });
89
Szymon Dompke94f71c52021-12-10 07:16:33 +010090 dbusIface.register_property_rw(
Cezary Zwolak4416fce2021-03-17 03:21:06 +010091 "Sensors", SensorsInfo{},
92 sdbusplus::vtable::property_::emits_change,
Szymon Dompke94f71c52021-12-10 07:16:33 +010093 [this, &triggerFactory](auto newVal, auto& oldVal) {
94 auto labeledSensorInfo =
95 triggerFactory.getLabeledSensorsInfo(newVal);
96 triggerFactory.updateSensors(sensors, labeledSensorInfo);
97 for (const auto& threshold : thresholds)
98 {
99 threshold->updateSensors(sensors);
100 }
101 oldVal = std::move(newVal);
102 return 1;
103 },
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100104 [this](const auto&) {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100105 return utils::fromLabeledSensorsInfo(
106 utils::transform(sensors, [](const auto& sensor) {
107 return sensor->getLabeledSensorInfo();
108 }));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100109 });
110
Szymon Dompke94f71c52021-12-10 07:16:33 +0100111 dbusIface.register_property_rw(
112 "ReportNames", std::vector<std::string>{},
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100113 sdbusplus::vtable::property_::emits_change,
Szymon Dompke94f71c52021-12-10 07:16:33 +0100114 [this](auto newVal, auto& oldVal) {
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100115 TriggerManager::verifyReportIds(newVal);
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100116 *reportIds = newVal;
117 messanger.send(messages::TriggerPresenceChangedInd{
118 messages::Presence::Exist, id, *reportIds});
Szymon Dompke94f71c52021-12-10 07:16:33 +0100119 oldVal = std::move(newVal);
120 return 1;
121 },
122 [this](const auto&) { return *reportIds; });
Szymon Dompke20013012021-07-23 09:54:20 +0200123
124 dbusIface.register_property_r(
125 "Discrete", false, sdbusplus::vtable::property_::const_,
126 [this](const auto& x) {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100127 return !std::holds_alternative<
128 numeric::LabeledThresholdParam>(
129 thresholds.back()->getThresholdParam());
Szymon Dompke20013012021-07-23 09:54:20 +0200130 });
131
Szymon Dompkee28aa532021-10-27 12:33:12 +0200132 dbusIface.register_property_rw(
133 "Name", std::string(),
134 sdbusplus::vtable::property_::emits_change,
135 [this](auto newVal, auto& oldVal) {
136 name = oldVal = newVal;
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100137 return 1;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200138 },
139 [this](const auto&) { return name; });
140
Szymon Dompke94f71c52021-12-10 07:16:33 +0100141 dbusIface.register_property_r(
142 "TriggerActions", std::vector<std::string>(),
143 sdbusplus::vtable::property_::const_, [this](const auto&) {
144 return utils::transform(triggerActions,
145 [](const auto& action) {
146 return actionToString(action);
147 });
148 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100149 });
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100150
151 for (const auto& threshold : thresholds)
152 {
153 threshold->initialize();
154 }
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100155
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100156 messanger.on_receive<messages::CollectTriggerIdReq>(
157 [this](const auto& msg) {
158 if (utils::contains(*reportIds, msg.reportId))
159 {
160 messanger.send(messages::CollectTriggerIdResp{id});
161 }
162 });
163
164 messanger.send(messages::TriggerPresenceChangedInd{
165 messages::Presence::Exist, id, *reportIds});
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100166}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100167
168bool Trigger::storeConfiguration() const
169{
170 try
171 {
172 nlohmann::json data;
173
Szymon Dompke94f71c52021-12-10 07:16:33 +0100174 auto labeledThresholdParams =
175 std::visit(utils::ToLabeledThresholdParamConversion(),
176 fromLabeledThresholdParam(utils::transform(
177 thresholds, [](const auto& threshold) {
178 return threshold->getThresholdParam();
179 })));
180
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100181 data["Version"] = triggerVersion;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200182 data["Id"] = id;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100183 data["Name"] = name;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100184 data["ThresholdParamsDiscriminator"] = labeledThresholdParams.index();
Szymon Dompke94f71c52021-12-10 07:16:33 +0100185 data["TriggerActions"] =
186 utils::transform(triggerActions, [](const auto& action) {
187 return actionToString(action);
188 });
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100189 data["ThresholdParams"] =
190 utils::labeledThresholdParamsToJson(labeledThresholdParams);
Szymon Dompke94f71c52021-12-10 07:16:33 +0100191 data["ReportIds"] = *reportIds;
192 data["Sensors"] = utils::transform(sensors, [](const auto& sensor) {
193 return sensor->getLabeledSensorInfo();
194 });
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100195
196 triggerStorage.store(fileName, data);
197 }
198 catch (const std::exception& e)
199 {
200 phosphor::logging::log<phosphor::logging::level::ERR>(
201 "Failed to store a trigger in storage",
202 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
203 return false;
204 }
205 return true;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100206}