Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 1 | #include "trigger.hpp" |
| 2 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 3 | #include "messages/collect_trigger_id.hpp" |
| 4 | #include "messages/trigger_presence_changed_ind.hpp" |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 5 | #include "trigger_manager.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 6 | #include "types/report_types.hpp" |
| 7 | #include "types/trigger_types.hpp" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 8 | #include "utils/contains.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 9 | #include "utils/conversion_trigger.hpp" |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 10 | #include "utils/transform.hpp" |
| 11 | |
| 12 | #include <phosphor-logging/log.hpp> |
| 13 | |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 14 | Trigger::Trigger( |
| 15 | boost::asio::io_context& ioc, |
| 16 | const std::shared_ptr<sdbusplus::asio::object_server>& objServer, |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 17 | const std::string& idIn, const std::string& nameIn, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 18 | const std::vector<TriggerAction>& triggerActionsIn, |
| 19 | const std::shared_ptr<std::vector<std::string>> reportIdsIn, |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 20 | std::vector<std::shared_ptr<interfaces::Threshold>>&& thresholdsIn, |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 21 | interfaces::TriggerManager& triggerManager, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 22 | interfaces::JsonStorage& triggerStorageIn, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 23 | const interfaces::TriggerFactory& triggerFactory, Sensors sensorsIn) : |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 24 | id(idIn), |
| 25 | name(nameIn), triggerActions(std::move(triggerActionsIn)), |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 26 | path(triggerDir + id), reportIds(std::move(reportIdsIn)), |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 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))), |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 29 | triggerStorage(triggerStorageIn), sensors(std::move(sensorsIn)), |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 30 | messanger(ioc) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 31 | { |
| 32 | deleteIface = objServer->add_unique_interface( |
| 33 | path, deleteIfaceName, [this, &ioc, &triggerManager](auto& dbusIface) { |
| 34 | dbusIface.register_method("Delete", [this, &ioc, &triggerManager] { |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 35 | if (persistent) |
| 36 | { |
| 37 | triggerStorage.remove(fileName); |
| 38 | } |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 39 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 40 | messages::Presence::Removed, id, {}}); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 41 | boost::asio::post(ioc, [this, &triggerManager] { |
| 42 | triggerManager.removeTrigger(this); |
| 43 | }); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | triggerIface = objServer->add_unique_interface( |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 48 | path, triggerIfaceName, [this, &triggerFactory](auto& dbusIface) { |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 49 | persistent = storeConfiguration(); |
| 50 | dbusIface.register_property_rw( |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 51 | "Persistent", persistent, |
| 52 | sdbusplus::vtable::property_::emits_change, |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 53 | [this](bool newVal, const auto&) { |
| 54 | if (newVal == persistent) |
| 55 | { |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 56 | return 1; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 57 | } |
| 58 | if (newVal) |
| 59 | { |
| 60 | persistent = storeConfiguration(); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | triggerStorage.remove(fileName); |
| 65 | persistent = false; |
| 66 | } |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 67 | return 1; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 68 | }, |
| 69 | [this](const auto&) { return persistent; }); |
| 70 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 71 | dbusIface.register_property_rw( |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 72 | "Thresholds", TriggerThresholdParams{}, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 73 | sdbusplus::vtable::property_::emits_change, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 74 | [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 Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 83 | [this](const auto&) { |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 84 | return fromLabeledThresholdParam(getLabeledThresholds()); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 85 | }); |
| 86 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 87 | dbusIface.register_property_rw( |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 88 | "Sensors", SensorsInfo{}, |
| 89 | sdbusplus::vtable::property_::emits_change, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 90 | [this, &triggerFactory](auto newVal, auto& oldVal) { |
| 91 | 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; |
| 100 | }, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 101 | [this](const auto&) { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 102 | return utils::fromLabeledSensorsInfo( |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 103 | getLabeledSensorInfo()); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 104 | }); |
| 105 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 106 | dbusIface.register_property_rw( |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 107 | "ReportNames", *reportIds, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 108 | sdbusplus::vtable::property_::emits_change, |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 109 | [this](auto newVal, auto& oldVal) { |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 110 | TriggerManager::verifyReportIds(newVal); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 111 | *reportIds = newVal; |
| 112 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 113 | messages::Presence::Exist, id, *reportIds}); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 114 | oldVal = std::move(newVal); |
| 115 | return 1; |
| 116 | }, |
| 117 | [this](const auto&) { return *reportIds; }); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 118 | |
| 119 | dbusIface.register_property_r( |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 120 | "Discrete", isDiscreate(), sdbusplus::vtable::property_::const_, |
| 121 | [this](const auto& x) { return isDiscreate(); }); |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 122 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 123 | dbusIface.register_property_rw( |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 124 | "Name", name, sdbusplus::vtable::property_::emits_change, |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 125 | [this](auto newVal, auto& oldVal) { |
| 126 | name = oldVal = newVal; |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 127 | return 1; |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 128 | }, |
| 129 | [this](const auto&) { return name; }); |
| 130 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 131 | dbusIface.register_property_r( |
| 132 | "TriggerActions", std::vector<std::string>(), |
| 133 | sdbusplus::vtable::property_::const_, [this](const auto&) { |
| 134 | return utils::transform(triggerActions, |
| 135 | [](const auto& action) { |
| 136 | return actionToString(action); |
| 137 | }); |
| 138 | }); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 139 | }); |
Wludzik, Jozef | 1477fe6 | 2021-01-02 11:56:10 +0100 | [diff] [blame] | 140 | |
| 141 | for (const auto& threshold : thresholds) |
| 142 | { |
| 143 | threshold->initialize(); |
| 144 | } |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 145 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 146 | messanger.on_receive<messages::CollectTriggerIdReq>( |
| 147 | [this](const auto& msg) { |
| 148 | if (utils::contains(*reportIds, msg.reportId)) |
| 149 | { |
| 150 | messanger.send(messages::CollectTriggerIdResp{id}); |
| 151 | } |
| 152 | }); |
| 153 | |
| 154 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 155 | messages::Presence::Exist, id, *reportIds}); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 156 | } |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 157 | |
| 158 | bool Trigger::storeConfiguration() const |
| 159 | { |
| 160 | try |
| 161 | { |
| 162 | nlohmann::json data; |
| 163 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 164 | auto labeledThresholdParams = |
| 165 | std::visit(utils::ToLabeledThresholdParamConversion(), |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 166 | fromLabeledThresholdParam(getLabeledThresholds())); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 167 | |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 168 | data["Version"] = triggerVersion; |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 169 | data["Id"] = id; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 170 | data["Name"] = name; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 171 | data["ThresholdParamsDiscriminator"] = labeledThresholdParams.index(); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 172 | data["TriggerActions"] = |
| 173 | utils::transform(triggerActions, [](const auto& action) { |
| 174 | return actionToString(action); |
| 175 | }); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 176 | data["ThresholdParams"] = |
| 177 | utils::labeledThresholdParamsToJson(labeledThresholdParams); |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 178 | data["ReportIds"] = *reportIds; |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 179 | data["Sensors"] = getLabeledSensorInfo(); |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 180 | |
| 181 | triggerStorage.store(fileName, data); |
| 182 | } |
| 183 | catch (const std::exception& e) |
| 184 | { |
| 185 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 186 | "Failed to store a trigger in storage", |
| 187 | phosphor::logging::entry("EXCEPTION_MSG=%s", e.what())); |
| 188 | return false; |
| 189 | } |
| 190 | return true; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 191 | } |
Krzysztof Grobelny | 5582455 | 2022-02-18 16:15:31 +0100 | [diff] [blame] | 192 | |
| 193 | std::vector<LabeledSensorInfo> Trigger::getLabeledSensorInfo() const |
| 194 | { |
| 195 | return utils::transform(sensors, [](const auto& sensor) { |
| 196 | return sensor->getLabeledSensorInfo(); |
| 197 | }); |
| 198 | } |
| 199 | |
| 200 | std::vector<LabeledThresholdParam> Trigger::getLabeledThresholds() const |
| 201 | { |
| 202 | return utils::transform(thresholds, [](const auto& threshold) { |
| 203 | return threshold->getThresholdParam(); |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | bool Trigger::isDiscreate() const |
| 208 | { |
| 209 | const auto labeledThresholds = getLabeledThresholds(); |
| 210 | |
| 211 | return utils::isFirstElementOfType<std::monostate>(labeledThresholds) || |
| 212 | utils::isFirstElementOfType<discrete::LabeledThresholdParam>( |
| 213 | labeledThresholds); |
| 214 | } |