Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "signal.hpp" |
| 17 | |
| 18 | #include "../manager.hpp" |
| 19 | #include "action.hpp" |
| 20 | #include "group.hpp" |
| 21 | #include "handlers.hpp" |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 22 | #include "trigger_aliases.hpp" |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 23 | |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 24 | #include <nlohmann/json.hpp> |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 25 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 26 | #include <sdbusplus/bus/match.hpp> |
| 27 | |
| 28 | #include <algorithm> |
| 29 | #include <functional> |
| 30 | #include <iterator> |
| 31 | #include <memory> |
| 32 | #include <numeric> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace phosphor::fan::control::json::trigger::signal |
| 37 | { |
| 38 | |
| 39 | using json = nlohmann::json; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 40 | using namespace sdbusplus::bus::match; |
| 41 | |
| 42 | void subscribe(const std::string& match, SignalPkg&& signalPkg, |
| 43 | std::function<bool(SignalPkg&)> isSameSig, Manager* mgr) |
| 44 | { |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 45 | auto& signalData = mgr->getSignal(match); |
| 46 | if (signalData.empty()) |
| 47 | { |
| 48 | // Signal subscription doesnt exist, add signal package and subscribe |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 49 | std::unique_ptr<std::vector<SignalPkg>> pkgs = |
| 50 | std::make_unique<std::vector<SignalPkg>>(); |
| 51 | pkgs->emplace_back(std::move(signalPkg)); |
Patrick Williams | 3ea9ec2 | 2021-11-19 12:21:08 -0600 | [diff] [blame] | 52 | std::unique_ptr<sdbusplus::bus::match_t> ptrMatch = nullptr; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 53 | if (!match.empty()) |
| 54 | { |
| 55 | // Subscribe to signal |
Patrick Williams | 3ea9ec2 | 2021-11-19 12:21:08 -0600 | [diff] [blame] | 56 | ptrMatch = std::make_unique<sdbusplus::bus::match_t>( |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 57 | mgr->getBus(), match.c_str(), |
| 58 | std::bind(std::mem_fn(&Manager::handleSignal), &(*mgr), |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 59 | std::placeholders::_1, pkgs.get())); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 60 | } |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 61 | signalData.emplace_back(std::move(pkgs), std::move(ptrMatch)); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 62 | } |
| 63 | else |
| 64 | { |
| 65 | // Signal subscription already exists |
| 66 | // Only a single signal data entry tied to each match is supported |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 67 | auto& pkgs = std::get<std::unique_ptr<std::vector<SignalPkg>>>( |
| 68 | signalData.front()); |
| 69 | auto sameSignal = false; |
| 70 | for (auto& pkg : *pkgs) |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 71 | { |
| 72 | if (isSameSig(pkg)) |
| 73 | { |
Matthew Barth | c3a6908 | 2021-11-15 14:32:48 -0600 | [diff] [blame] | 74 | // Same SignalObject signal to trigger event actions, |
| 75 | // add actions to be run when signal for SignalObject received |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 76 | auto& pkgActions = std::get<TriggerActions>(signalPkg); |
| 77 | auto& actions = std::get<TriggerActions>(pkg); |
Matthew Barth | c3a6908 | 2021-11-15 14:32:48 -0600 | [diff] [blame] | 78 | actions.insert(actions.end(), pkgActions.begin(), |
| 79 | pkgActions.end()); |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 80 | sameSignal = true; |
| 81 | break; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 82 | } |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 83 | } |
| 84 | if (!sameSignal) |
| 85 | { |
| 86 | // Expected signal differs, add signal package |
| 87 | pkgs->emplace_back(std::move(signalPkg)); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 92 | void propertiesChanged(Manager* mgr, const Group& group, |
| 93 | TriggerActions& actions, const json&) |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 94 | { |
| 95 | // Groups are optional, but a signal triggered event with no groups |
| 96 | // will do nothing since signals require a group |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 97 | for (const auto& member : group.getMembers()) |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 98 | { |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 99 | // Setup property changed signal handler on the group member's |
| 100 | // property |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 101 | const auto match = |
| 102 | rules::propertiesChanged(member, group.getInterface()); |
| 103 | SignalPkg signalPkg = { |
| 104 | Handlers::propertiesChanged, |
| 105 | SignalObject(std::cref(member), std::cref(group.getInterface()), |
| 106 | std::cref(group.getProperty())), |
| 107 | actions}; |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 108 | auto isSameSig = [&prop = group.getProperty()](SignalPkg& pkg) { |
| 109 | auto& obj = std::get<SignalObject>(pkg); |
| 110 | return prop == std::get<Prop>(obj); |
| 111 | }; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 112 | |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 113 | subscribe(match, std::move(signalPkg), isSameSig, mgr); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 117 | void interfacesAdded(Manager* mgr, const Group& group, TriggerActions& actions, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 118 | const json&) |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 119 | { |
| 120 | // Groups are optional, but a signal triggered event with no groups |
| 121 | // will do nothing since signals require a group |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 122 | for (const auto& member : group.getMembers()) |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 123 | { |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 124 | // Setup interfaces added signal handler on the group member |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 125 | const auto match = |
| 126 | rules::interfacesAdded() + rules::argNpath(0, member); |
| 127 | SignalPkg signalPkg = { |
| 128 | Handlers::interfacesAdded, |
| 129 | SignalObject(std::cref(member), std::cref(group.getInterface()), |
| 130 | std::cref(group.getProperty())), |
| 131 | actions}; |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 132 | auto isSameSig = [&intf = group.getInterface()](SignalPkg& pkg) { |
| 133 | auto& obj = std::get<SignalObject>(pkg); |
| 134 | return intf == std::get<Intf>(obj); |
| 135 | }; |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 136 | |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 137 | subscribe(match, std::move(signalPkg), isSameSig, mgr); |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 141 | void interfacesRemoved(Manager* mgr, const Group& group, |
| 142 | TriggerActions& actions, const json&) |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 143 | { |
| 144 | // Groups are optional, but a signal triggered event with no groups |
| 145 | // will do nothing since signals require a group |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 146 | for (const auto& member : group.getMembers()) |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 147 | { |
Matt Spinler | 78a48a5 | 2022-07-14 13:39:10 -0500 | [diff] [blame] | 148 | // Setup interfaces removed signal handler on the group member |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 149 | const auto match = |
| 150 | rules::interfacesRemoved() + rules::argNpath(0, member); |
| 151 | SignalPkg signalPkg = { |
| 152 | Handlers::interfacesRemoved, |
| 153 | SignalObject(std::cref(member), std::cref(group.getInterface()), |
| 154 | std::cref(group.getProperty())), |
| 155 | actions}; |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 156 | auto isSameSig = [&intf = group.getInterface()](SignalPkg& pkg) { |
| 157 | auto& obj = std::get<SignalObject>(pkg); |
| 158 | return intf == std::get<Intf>(obj); |
| 159 | }; |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 160 | |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 161 | subscribe(match, std::move(signalPkg), isSameSig, mgr); |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 165 | void nameOwnerChanged(Manager* mgr, const Group& group, TriggerActions& actions, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 166 | const json&) |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 167 | { |
Matthew Barth | 508bc76 | 2021-10-27 11:01:54 -0500 | [diff] [blame] | 168 | std::vector<std::string> grpServices; |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 169 | // Groups are optional, but a signal triggered event with no groups |
| 170 | // will do nothing since signals require a group |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 171 | for (const auto& member : group.getMembers()) |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 172 | { |
Matthew Barth | 65f7281 | 2022-01-13 15:05:34 -0600 | [diff] [blame] | 173 | auto serv = group.getService(); |
| 174 | if (serv.empty()) |
| 175 | { |
| 176 | serv = Manager::getService(member, group.getInterface()); |
| 177 | } |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 178 | if (!serv.empty()) |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 179 | { |
Matthew Barth | 508bc76 | 2021-10-27 11:01:54 -0500 | [diff] [blame] | 180 | // No need to re-subscribe to the same service's nameOwnerChanged |
| 181 | // signal when a prior group member provided by the same service |
| 182 | // already did the subscription |
| 183 | if (std::find(grpServices.begin(), grpServices.end(), serv) == |
| 184 | grpServices.end()) |
| 185 | { |
| 186 | // Setup name owner changed signal handler on the group |
| 187 | // member's service |
| 188 | const auto match = rules::nameOwnerChanged(serv); |
Matthew Barth | 6d8e2d3 | 2022-02-01 16:47:08 -0600 | [diff] [blame] | 189 | SignalPkg signalPkg = {Handlers::nameOwnerChanged, |
| 190 | SignalObject(), actions}; |
Matthew Barth | 508bc76 | 2021-10-27 11:01:54 -0500 | [diff] [blame] | 191 | // If signal match already exists, then the service will be the |
| 192 | // same so add action to be run |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 193 | auto isSameSig = [](SignalPkg&) { return true; }; |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 194 | |
Matthew Barth | 508bc76 | 2021-10-27 11:01:54 -0500 | [diff] [blame] | 195 | subscribe(match, std::move(signalPkg), isSameSig, mgr); |
| 196 | grpServices.emplace_back(serv); |
| 197 | } |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 198 | } |
| 199 | else |
| 200 | { |
| 201 | // Unable to construct nameOwnerChanged match string |
| 202 | // Path and/or interface configured does not exist on dbus yet? |
| 203 | // TODO How to handle this? Create timer to keep checking for |
| 204 | // service to appear? When to stop checking? |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 205 | lg2::error( |
| 206 | "Events will not be triggered by name owner changed" |
| 207 | "signals from service of path {MEMBER}, interface {GROUP_INTERFACE}", |
| 208 | "MEMBER", member, "GROUP_INTERFACE", group.getInterface()); |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 213 | void member(Manager* mgr, const Group& group, TriggerActions& actions, |
Matthew Barth | 93a10ab | 2021-10-15 14:45:42 -0500 | [diff] [blame] | 214 | const json&) |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 215 | { |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 216 | // No SignalObject required to associate to this signal |
Matthew Barth | c3a6908 | 2021-11-15 14:32:48 -0600 | [diff] [blame] | 217 | SignalPkg signalPkg = {Handlers::member, SignalObject(), actions}; |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 218 | // If signal match already exists, then the member signal will be the |
| 219 | // same so add action to be run |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 220 | auto isSameSig = [](SignalPkg&) { return true; }; |
Matthew Barth | 93a10ab | 2021-10-15 14:45:42 -0500 | [diff] [blame] | 221 | |
| 222 | // Groups are optional, but a signal triggered event with no groups |
| 223 | // will do nothing since signals require a group |
| 224 | for (const auto& member : group.getMembers()) |
| 225 | { |
| 226 | // Subscribe for signal from each group member |
| 227 | const auto match = |
| 228 | rules::type::signal() + rules::member(group.getProperty()) + |
| 229 | rules::path(member) + rules::interface(group.getInterface()); |
| 230 | |
| 231 | subscribe(match, std::move(signalPkg), isSameSig, mgr); |
| 232 | } |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Patrick Williams | 4fa67aa | 2025-02-03 14:28:47 -0500 | [diff] [blame] | 235 | enableTrigger triggerSignal( |
| 236 | const json& jsonObj, const std::string& eventName, |
| 237 | std::vector<std::unique_ptr<ActionBase>>& /*actions*/) |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 238 | { |
| 239 | auto subscriber = signals.end(); |
| 240 | if (jsonObj.contains("signal")) |
| 241 | { |
| 242 | auto signal = jsonObj["signal"].get<std::string>(); |
| 243 | std::transform(signal.begin(), signal.end(), signal.begin(), tolower); |
| 244 | subscriber = signals.find(signal); |
| 245 | } |
| 246 | if (subscriber == signals.end()) |
| 247 | { |
| 248 | // Construct list of available signals |
Patrick Williams | 5e15c3b | 2023-10-20 11:18:11 -0500 | [diff] [blame] | 249 | auto availSignals = |
| 250 | std::accumulate(std::next(signals.begin()), signals.end(), |
| 251 | signals.begin()->first, [](auto list, auto signal) { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 252 | return std::move(list) + ", " + signal.first; |
| 253 | }); |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 254 | lg2::error( |
| 255 | "Event '{EVENT_NAME}' requires a supported signal given to be " |
| 256 | "triggered by signal, available signals: {AVAILABLE_SIGNALS}", |
| 257 | "EVENT_NAME", eventName, "AVAILABLE_SIGNALS", availSignals); |
| 258 | throw std::runtime_error( |
| 259 | "Event requires a supported signal given to be triggered by signal."); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 260 | } |
| 261 | |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 262 | return [subscriber = std::move(subscriber), |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 263 | jsonObj](const std::string& /*eventName*/, Manager* mgr, |
Matthew Barth | cd6f379 | 2021-09-30 15:13:25 -0500 | [diff] [blame] | 264 | const std::vector<Group>& groups, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 265 | std::vector<std::unique_ptr<ActionBase>>& actions) { |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 266 | TriggerActions signalActions; |
Matthew Barth | c3a6908 | 2021-11-15 14:32:48 -0600 | [diff] [blame] | 267 | std::for_each(actions.begin(), actions.end(), |
| 268 | [&signalActions](auto& action) { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 269 | signalActions.emplace_back(std::ref(action)); |
| 270 | }); |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 271 | for (const auto& group : groups) |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 272 | { |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 273 | // Call signal subscriber for each group |
Matthew Barth | c3a6908 | 2021-11-15 14:32:48 -0600 | [diff] [blame] | 274 | subscriber->second(mgr, group, signalActions, jsonObj); |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 275 | } |
| 276 | }; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | } // namespace phosphor::fan::control::json::trigger::signal |