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