Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 1 | #include "trigger.hpp" |
| 2 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 3 | #include "trigger_manager.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 4 | #include "types/report_types.hpp" |
| 5 | #include "types/trigger_types.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 6 | #include "utils/conversion_trigger.hpp" |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 7 | #include "utils/transform.hpp" |
| 8 | |
| 9 | #include <phosphor-logging/log.hpp> |
| 10 | |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 11 | Trigger::Trigger( |
| 12 | boost::asio::io_context& ioc, |
| 13 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer, |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 14 | const std::string& idIn, const std::string& nameIn, |
| 15 | const std::vector<std::string>& triggerActionsIn, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 16 | const std::vector<std::string>& reportIdsIn, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 17 | const std::vector<LabeledSensorInfo>& LabeledSensorsInfoIn, |
| 18 | const LabeledTriggerThresholdParams& labeledThresholdParamsIn, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 19 | std::vector<std::shared_ptr<interfaces::Threshold>>&& thresholdsIn, |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 20 | interfaces::TriggerManager& triggerManager, |
| 21 | interfaces::JsonStorage& triggerStorageIn) : |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 22 | id(idIn), |
| 23 | name(nameIn), triggerActions(std::move(triggerActionsIn)), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 24 | path(triggerDir + id), reportIds(reportIdsIn), |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 25 | labeledSensorsInfo(LabeledSensorsInfoIn), |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 26 | labeledThresholdParams(labeledThresholdParamsIn), |
| 27 | thresholds(std::move(thresholdsIn)), |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 28 | fileName(std::to_string(std::hash<std::string>{}(id))), |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 29 | triggerStorage(triggerStorageIn) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 30 | { |
| 31 | deleteIface = objServer->add_unique_interface( |
| 32 | path, deleteIfaceName, [this, &ioc, &triggerManager](auto& dbusIface) { |
| 33 | dbusIface.register_method("Delete", [this, &ioc, &triggerManager] { |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 34 | if (persistent) |
| 35 | { |
| 36 | triggerStorage.remove(fileName); |
| 37 | } |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 38 | boost::asio::post(ioc, [this, &triggerManager] { |
| 39 | triggerManager.removeTrigger(this); |
| 40 | }); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | triggerIface = objServer->add_unique_interface( |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 45 | path, triggerIfaceName, [this](auto& dbusIface) { |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 46 | persistent = storeConfiguration(); |
| 47 | dbusIface.register_property_rw( |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 48 | "Persistent", persistent, |
| 49 | sdbusplus::vtable::property_::emits_change, |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 50 | [this](bool newVal, const auto&) { |
| 51 | if (newVal == persistent) |
| 52 | { |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 53 | return 1; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 54 | } |
| 55 | if (newVal) |
| 56 | { |
| 57 | persistent = storeConfiguration(); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | triggerStorage.remove(fileName); |
| 62 | persistent = false; |
| 63 | } |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 64 | return 1; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 65 | }, |
| 66 | [this](const auto&) { return persistent; }); |
| 67 | |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 68 | dbusIface.register_property_r( |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 69 | "Thresholds", TriggerThresholdParams{}, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 70 | sdbusplus::vtable::property_::emits_change, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 71 | [this](const auto&) { |
| 72 | return std::visit( |
| 73 | utils::FromLabeledThresholdParamConversion(), |
| 74 | labeledThresholdParams); |
| 75 | }); |
| 76 | |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 77 | dbusIface.register_property_r( |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 78 | "Sensors", SensorsInfo{}, |
| 79 | sdbusplus::vtable::property_::emits_change, |
| 80 | [this](const auto&) { |
| 81 | return utils::fromLabeledSensorsInfo(labeledSensorsInfo); |
| 82 | }); |
| 83 | |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 84 | dbusIface.register_property_r( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 85 | "ReportNames", reportIds, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 86 | sdbusplus::vtable::property_::emits_change, |
| 87 | [](const auto& x) { return x; }); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 88 | |
| 89 | dbusIface.register_property_r( |
| 90 | "Discrete", false, sdbusplus::vtable::property_::const_, |
| 91 | [this](const auto& x) { |
| 92 | return isTriggerThresholdDiscrete(labeledThresholdParams); |
| 93 | }); |
| 94 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 95 | 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 Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 100 | return 1; |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 101 | }, |
| 102 | [this](const auto&) { return name; }); |
| 103 | |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 104 | dbusIface.register_property_r("TriggerActions", triggerActions, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 105 | sdbusplus::vtable::property_::const_, |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 106 | [this](const auto& x) { return x; }); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 107 | }); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 108 | |
| 109 | for (const auto& threshold : thresholds) |
| 110 | { |
| 111 | threshold->initialize(); |
| 112 | } |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 113 | } |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 114 | |
| 115 | bool Trigger::storeConfiguration() const |
| 116 | { |
| 117 | try |
| 118 | { |
| 119 | nlohmann::json data; |
| 120 | |
| 121 | data["Version"] = triggerVersion; |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 122 | data["Id"] = id; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 123 | data["Name"] = name; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 124 | data["ThresholdParamsDiscriminator"] = labeledThresholdParams.index(); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 125 | data["TriggerActions"] = triggerActions; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 126 | data["ThresholdParams"] = |
| 127 | utils::labeledThresholdParamsToJson(labeledThresholdParams); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 128 | data["ReportIds"] = reportIds; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 129 | data["Sensors"] = labeledSensorsInfo; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 130 | |
| 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 Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 141 | } |