blob: f619f51ace67e224edcc1160798c8c708e7093cb [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 Williams583ba442025-02-03 14:28:19 -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);
44 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010045 });
46 });
47
48 triggerIface = objServer->add_unique_interface(
Szymon Dompke94f71c52021-12-10 07:16:33 +010049 path, triggerIfaceName, [this, &triggerFactory](auto& dbusIface) {
Patrick Williams583ba442025-02-03 14:28:19 -050050 persistent = storeConfiguration();
51 dbusIface.register_property_rw(
52 "Persistent", persistent,
53 sdbusplus::vtable::property_::emits_change,
54 [this](bool newVal, const auto&) {
55 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;
69 },
70 [this](const auto&) { return persistent; });
Cezary Zwolaka4e67612021-02-18 13:16:16 +010071
Patrick Williams583ba442025-02-03 14:28:19 -050072 dbusIface.register_property_rw(
Patrick Williams583ba442025-02-03 14:28:19 -050073 "DiscreteThresholds", std::vector<discrete::ThresholdParam>{},
74 sdbusplus::vtable::property_::emits_change,
75 [this, &triggerFactory](
76 const std::vector<discrete::ThresholdParam>& newVal,
77 std::vector<discrete::ThresholdParam>& oldVal) {
78 LabeledTriggerThresholdParams newThresholdParams =
79 utils::ToLabeledThresholdParamConversion()(newVal);
80 TriggerManager::verifyThresholdParams(newThresholdParams);
81 triggerFactory.updateThresholds(
82 thresholds, *id, triggerActions, reportIds, sensors,
83 newThresholdParams);
84 oldVal = std::move(newVal);
85 return 1;
86 },
87 [this](const auto&) {
88 TriggerThresholdParams unlabeled =
89 fromLabeledThresholdParam(getLabeledThresholds());
90 auto* ptr =
91 std::get_if<std::vector<discrete::ThresholdParam>>(
92 &unlabeled);
93 if (ptr == nullptr)
94 {
95 // If internal type doesn't match, return empty set
96 return std::vector<discrete::ThresholdParam>{};
97 }
98 return *ptr;
99 });
100
101 dbusIface.register_property_rw(
102 "NumericThresholds", std::vector<numeric::ThresholdParam>{},
103 sdbusplus::vtable::property_::emits_change,
104 [this, &triggerFactory](
105 const std::vector<numeric::ThresholdParam>& newVal,
106 std::vector<numeric::ThresholdParam>& oldVal) {
107 LabeledTriggerThresholdParams newThresholdParams =
108 utils::ToLabeledThresholdParamConversion()(newVal);
109 TriggerManager::verifyThresholdParams(newThresholdParams);
110 triggerFactory.updateThresholds(
111 thresholds, *id, triggerActions, reportIds, sensors,
112 newThresholdParams);
113 oldVal = std::move(newVal);
114 return 1;
115 },
116 [this](const auto&) {
117 TriggerThresholdParams unlabeled =
118 fromLabeledThresholdParam(getLabeledThresholds());
119 auto* ptr =
120 std::get_if<std::vector<numeric::ThresholdParam>>(
121 &unlabeled);
122 if (ptr == nullptr)
123 {
124 // If internal type doesn't match, return empty set
125 return std::vector<numeric::ThresholdParam>{};
126 }
127 return *ptr;
128 });
129 dbusIface.register_property_rw(
130 "Sensors", SensorsInfo{},
131 sdbusplus::vtable::property_::emits_change,
132 [this, &triggerFactory](auto newVal, auto& oldVal) {
133 auto labeledSensorInfo =
134 triggerFactory.getLabeledSensorsInfo(newVal);
135 triggerFactory.updateSensors(sensors, labeledSensorInfo);
136 for (const auto& threshold : thresholds)
137 {
138 threshold->updateSensors(sensors);
139 }
140 oldVal = std::move(newVal);
141 return 1;
142 },
143 [this](const auto&) {
144 return utils::fromLabeledSensorsInfo(
145 getLabeledSensorInfo());
146 });
147
148 dbusIface.register_property_rw(
149 "Reports", std::vector<sdbusplus::message::object_path>(),
150 sdbusplus::vtable::property_::emits_change,
151 [this](auto newVal, auto& oldVal) {
152 auto newReportIds = utils::transform<std::vector>(
153 newVal, [](const auto& path) {
154 return utils::reportPathToId(path);
155 });
156 TriggerManager::verifyReportIds(newReportIds);
157 *reportIds = newReportIds;
158 messanger.send(messages::TriggerPresenceChangedInd{
159 messages::Presence::Exist, *id, *reportIds});
160 oldVal = std::move(newVal);
161 return 1;
162 },
163 [this](const auto&) {
164 return utils::transform<std::vector>(
165 *reportIds, [](const auto& id) {
166 return utils::pathAppend(
167 utils::constants::reportDirPath, id);
168 });
169 });
170
171 dbusIface.register_property_r(
Michal Orzel6f56d262025-07-25 11:19:13 +0200172 "Discrete", isDiscrete(), sdbusplus::vtable::property_::const_,
173 [this](const auto& x) { return isDiscrete(); });
Patrick Williams583ba442025-02-03 14:28:19 -0500174
175 dbusIface.register_property_rw(
176 "Name", name, sdbusplus::vtable::property_::emits_change,
177 [this](auto newVal, auto& oldVal) {
178 if (newVal.length() > utils::constants::maxIdNameLength)
179 {
180 throw errors::InvalidArgument("Name",
181 "Name is too long.");
182 }
183 name = oldVal = newVal;
184 return 1;
185 },
186 [this](const auto&) { return name; });
187
188 dbusIface.register_property_r(
189 "TriggerActions", std::vector<std::string>(),
190 sdbusplus::vtable::property_::const_, [this](const auto&) {
191 return utils::transform(triggerActions,
192 [](const auto& action) {
193 return actionToString(action);
194 });
195 });
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500196 });
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100197
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100198 for (const auto& threshold : thresholds)
199 {
200 threshold->initialize();
201 }
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100202
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100203 messanger.on_receive<messages::CollectTriggerIdReq>(
204 [this](const auto& msg) {
Patrick Williams583ba442025-02-03 14:28:19 -0500205 if (utils::contains(*reportIds, msg.reportId))
206 {
207 messanger.send(messages::CollectTriggerIdResp{*id});
208 }
209 });
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100210
211 messanger.send(messages::TriggerPresenceChangedInd{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200212 messages::Presence::Exist, *id, *reportIds});
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100213}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100214
215bool Trigger::storeConfiguration() const
216{
217 try
218 {
219 nlohmann::json data;
220
Szymon Dompke94f71c52021-12-10 07:16:33 +0100221 auto labeledThresholdParams =
222 std::visit(utils::ToLabeledThresholdParamConversion(),
Krzysztof Grobelny55824552022-02-18 16:15:31 +0100223 fromLabeledThresholdParam(getLabeledThresholds()));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100224
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100225 data["Version"] = triggerVersion;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200226 data["Id"] = *id;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100227 data["Name"] = name;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100228 data["ThresholdParamsDiscriminator"] = labeledThresholdParams.index();
Patrick Williams583ba442025-02-03 14:28:19 -0500229 data["TriggerActions"] =
230 utils::transform(triggerActions, [](const auto& action) {
231 return actionToString(action);
232 });
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100233 data["ThresholdParams"] =
234 utils::labeledThresholdParamsToJson(labeledThresholdParams);
Szymon Dompke94f71c52021-12-10 07:16:33 +0100235 data["ReportIds"] = *reportIds;
Krzysztof Grobelny55824552022-02-18 16:15:31 +0100236 data["Sensors"] = getLabeledSensorInfo();
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100237
238 triggerStorage.store(fileName, data);
239 }
240 catch (const std::exception& e)
241 {
242 phosphor::logging::log<phosphor::logging::level::ERR>(
243 "Failed to store a trigger in storage",
244 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
245 return false;
246 }
247 return true;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100248}
Krzysztof Grobelny55824552022-02-18 16:15:31 +0100249
250std::vector<LabeledSensorInfo> Trigger::getLabeledSensorInfo() const
251{
252 return utils::transform(sensors, [](const auto& sensor) {
253 return sensor->getLabeledSensorInfo();
254 });
255}
256
257std::vector<LabeledThresholdParam> Trigger::getLabeledThresholds() const
258{
259 return utils::transform(thresholds, [](const auto& threshold) {
260 return threshold->getThresholdParam();
261 });
262}
263
Michal Orzel6f56d262025-07-25 11:19:13 +0200264bool Trigger::isDiscrete() const
Krzysztof Grobelny55824552022-02-18 16:15:31 +0100265{
266 const auto labeledThresholds = getLabeledThresholds();
Michal Orzel6f56d262025-07-25 11:19:13 +0200267 if (labeledThresholds.empty())
268 {
269 return true;
270 }
Krzysztof Grobelny55824552022-02-18 16:15:31 +0100271
272 return utils::isFirstElementOfType<std::monostate>(labeledThresholds) ||
273 utils::isFirstElementOfType<discrete::LabeledThresholdParam>(
274 labeledThresholds);
275}