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