blob: e84a389853d131ee396ad9539ea57d7e69279626 [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "trigger.hpp"
2
Szymon Dompkee28aa532021-10-27 12:33:12 +02003#include "trigger_manager.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +00004#include "types/report_types.hpp"
5#include "types/trigger_types.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01006#include "utils/conversion_trigger.hpp"
Cezary Zwolaka4e67612021-02-18 13:16:16 +01007#include "utils/transform.hpp"
8
9#include <phosphor-logging/log.hpp>
10
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010011Trigger::Trigger(
12 boost::asio::io_context& ioc,
13 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Szymon Dompkee28aa532021-10-27 12:33:12 +020014 const std::string& idIn, const std::string& nameIn,
15 const std::vector<std::string>& triggerActionsIn,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010016 const std::vector<std::string>& reportIdsIn,
Cezary Zwolak4416fce2021-03-17 03:21:06 +010017 const std::vector<LabeledSensorInfo>& LabeledSensorsInfoIn,
18 const LabeledTriggerThresholdParams& labeledThresholdParamsIn,
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010019 std::vector<std::shared_ptr<interfaces::Threshold>>&& thresholdsIn,
Cezary Zwolaka4e67612021-02-18 13:16:16 +010020 interfaces::TriggerManager& triggerManager,
21 interfaces::JsonStorage& triggerStorageIn) :
Szymon Dompkee28aa532021-10-27 12:33:12 +020022 id(idIn),
23 name(nameIn), triggerActions(std::move(triggerActionsIn)),
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010024 path(triggerDir + id), reportIds(reportIdsIn),
Szymon Dompkee28aa532021-10-27 12:33:12 +020025 labeledSensorsInfo(LabeledSensorsInfoIn),
Cezary Zwolak4416fce2021-03-17 03:21:06 +010026 labeledThresholdParams(labeledThresholdParamsIn),
27 thresholds(std::move(thresholdsIn)),
Szymon Dompkee28aa532021-10-27 12:33:12 +020028 fileName(std::to_string(std::hash<std::string>{}(id))),
Cezary Zwolaka4e67612021-02-18 13:16:16 +010029 triggerStorage(triggerStorageIn)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010030{
31 deleteIface = objServer->add_unique_interface(
32 path, deleteIfaceName, [this, &ioc, &triggerManager](auto& dbusIface) {
33 dbusIface.register_method("Delete", [this, &ioc, &triggerManager] {
Cezary Zwolaka4e67612021-02-18 13:16:16 +010034 if (persistent)
35 {
36 triggerStorage.remove(fileName);
37 }
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010038 boost::asio::post(ioc, [this, &triggerManager] {
39 triggerManager.removeTrigger(this);
40 });
41 });
42 });
43
44 triggerIface = objServer->add_unique_interface(
Szymon Dompke20013012021-07-23 09:54:20 +020045 path, triggerIfaceName, [this](auto& dbusIface) {
Cezary Zwolaka4e67612021-02-18 13:16:16 +010046 persistent = storeConfiguration();
47 dbusIface.register_property_rw(
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010048 "Persistent", persistent,
49 sdbusplus::vtable::property_::emits_change,
Cezary Zwolaka4e67612021-02-18 13:16:16 +010050 [this](bool newVal, const auto&) {
51 if (newVal == persistent)
52 {
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +010053 return 1;
Cezary Zwolaka4e67612021-02-18 13:16:16 +010054 }
55 if (newVal)
56 {
57 persistent = storeConfiguration();
58 }
59 else
60 {
61 triggerStorage.remove(fileName);
62 persistent = false;
63 }
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +010064 return 1;
Cezary Zwolaka4e67612021-02-18 13:16:16 +010065 },
66 [this](const auto&) { return persistent; });
67
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010068 dbusIface.register_property_r(
Cezary Zwolak4416fce2021-03-17 03:21:06 +010069 "Thresholds", TriggerThresholdParams{},
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010070 sdbusplus::vtable::property_::emits_change,
Cezary Zwolak4416fce2021-03-17 03:21:06 +010071 [this](const auto&) {
72 return std::visit(
73 utils::FromLabeledThresholdParamConversion(),
74 labeledThresholdParams);
75 });
76
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010077 dbusIface.register_property_r(
Cezary Zwolak4416fce2021-03-17 03:21:06 +010078 "Sensors", SensorsInfo{},
79 sdbusplus::vtable::property_::emits_change,
80 [this](const auto&) {
81 return utils::fromLabeledSensorsInfo(labeledSensorsInfo);
82 });
83
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010084 dbusIface.register_property_r(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010085 "ReportNames", reportIds,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010086 sdbusplus::vtable::property_::emits_change,
87 [](const auto& x) { return x; });
Szymon Dompke20013012021-07-23 09:54:20 +020088
89 dbusIface.register_property_r(
90 "Discrete", false, sdbusplus::vtable::property_::const_,
91 [this](const auto& x) {
92 return isTriggerThresholdDiscrete(labeledThresholdParams);
93 });
94
Szymon Dompkee28aa532021-10-27 12:33:12 +020095 dbusIface.register_property_rw(
96 "Name", std::string(),
97 sdbusplus::vtable::property_::emits_change,
98 [this](auto newVal, auto& oldVal) {
99 name = oldVal = newVal;
Szymon Dompke3e2cc9d2021-12-14 12:00:52 +0100100 return 1;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200101 },
102 [this](const auto&) { return name; });
103
Szymon Dompke20013012021-07-23 09:54:20 +0200104 dbusIface.register_property_r("TriggerActions", triggerActions,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100105 sdbusplus::vtable::property_::const_,
Szymon Dompke20013012021-07-23 09:54:20 +0200106 [this](const auto& x) { return x; });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100107 });
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100108
109 for (const auto& threshold : thresholds)
110 {
111 threshold->initialize();
112 }
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100113}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100114
115bool Trigger::storeConfiguration() const
116{
117 try
118 {
119 nlohmann::json data;
120
121 data["Version"] = triggerVersion;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200122 data["Id"] = id;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100123 data["Name"] = name;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100124 data["ThresholdParamsDiscriminator"] = labeledThresholdParams.index();
Szymon Dompke20013012021-07-23 09:54:20 +0200125 data["TriggerActions"] = triggerActions;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100126 data["ThresholdParams"] =
127 utils::labeledThresholdParamsToJson(labeledThresholdParams);
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100128 data["ReportIds"] = reportIds;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100129 data["Sensors"] = labeledSensorsInfo;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100130
131 triggerStorage.store(fileName, data);
132 }
133 catch (const std::exception& e)
134 {
135 phosphor::logging::log<phosphor::logging::level::ERR>(
136 "Failed to store a trigger in storage",
137 phosphor::logging::entry("EXCEPTION_MSG=%s", e.what()));
138 return false;
139 }
140 return true;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100141}