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 | |
| 24 | #include <fmt/format.h> |
| 25 | |
| 26 | #include <nlohmann/json.hpp> |
| 27 | #include <phosphor-logging/log.hpp> |
| 28 | #include <sdbusplus/bus/match.hpp> |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <functional> |
| 32 | #include <iterator> |
| 33 | #include <memory> |
| 34 | #include <numeric> |
| 35 | #include <utility> |
| 36 | #include <vector> |
| 37 | |
| 38 | namespace phosphor::fan::control::json::trigger::signal |
| 39 | { |
| 40 | |
| 41 | using json = nlohmann::json; |
| 42 | using namespace phosphor::logging; |
| 43 | using namespace sdbusplus::bus::match; |
| 44 | |
| 45 | void subscribe(const std::string& match, SignalPkg&& signalPkg, |
| 46 | std::function<bool(SignalPkg&)> isSameSig, Manager* mgr) |
| 47 | { |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 48 | auto& signalData = mgr->getSignal(match); |
| 49 | if (signalData.empty()) |
| 50 | { |
| 51 | // Signal subscription doesnt exist, add signal package and subscribe |
| 52 | std::vector<SignalPkg> pkgs = {signalPkg}; |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 53 | std::vector<SignalPkg> dataPkgs = |
| 54 | std::vector<SignalPkg>(std::move(pkgs)); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 55 | std::unique_ptr<sdbusplus::server::match::match> ptrMatch = nullptr; |
Matthew Barth | 50219f5 | 2021-05-18 11:20:36 -0500 | [diff] [blame] | 56 | // TODO(ibm-openbmc/#3195) - Filter signal subscriptions to objects |
| 57 | // owned by fan control? |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 58 | if (!match.empty()) |
| 59 | { |
| 60 | // Subscribe to signal |
| 61 | ptrMatch = std::make_unique<sdbusplus::server::match::match>( |
| 62 | mgr->getBus(), match.c_str(), |
| 63 | std::bind(std::mem_fn(&Manager::handleSignal), &(*mgr), |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 64 | std::placeholders::_1, dataPkgs)); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 65 | } |
| 66 | signalData.emplace_back(std::move(dataPkgs), std::move(ptrMatch)); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // Signal subscription already exists |
| 71 | // Only a single signal data entry tied to each match is supported |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 72 | auto& pkgs = std::get<std::vector<SignalPkg>>(signalData.front()); |
| 73 | for (auto& pkg : pkgs) |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 74 | { |
| 75 | if (isSameSig(pkg)) |
| 76 | { |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 77 | // Same signal expected, add actions to be run when signal |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 78 | // received |
| 79 | auto& pkgActions = std::get<SignalActions>(signalPkg); |
| 80 | auto& actions = std::get<SignalActions>(pkg); |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 81 | actions.insert(actions.end(), |
| 82 | std::make_move_iterator(pkgActions.begin()), |
| 83 | std::make_move_iterator(pkgActions.end())); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 84 | } |
| 85 | else |
| 86 | { |
| 87 | // Expected signal differs, add signal package |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 88 | pkgs.emplace_back(std::move(signalPkg)); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 94 | void propertiesChanged(Manager* mgr, const Group& group, SignalActions actions, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 95 | 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 |
| 103 | const auto match = |
| 104 | rules::propertiesChanged(member, group.getInterface()); |
| 105 | SignalPkg signalPkg = {Handlers::propertiesChanged, |
| 106 | SignalObject(std::cref(member), |
| 107 | std::cref(group.getInterface()), |
| 108 | std::cref(group.getProperty())), |
| 109 | SignalActions(actions)}; |
| 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 | |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 119 | void interfacesAdded(Manager* mgr, const Group& group, SignalActions 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 |
Matt Spinler | a7fcf3e | 2021-11-03 14:08:50 -0500 | [diff] [blame] | 127 | const auto match = |
| 128 | rules::interfacesAdded() + 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())), |
| 133 | SignalActions(actions)}; |
| 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 | |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 143 | void interfacesRemoved(Manager* mgr, const Group& group, SignalActions actions, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 144 | 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 | { |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 150 | // Setup interfaces added signal handler on the group member |
| 151 | const auto match = rules::interfacesRemoved(member); |
| 152 | SignalPkg signalPkg = {Handlers::interfacesRemoved, |
| 153 | SignalObject(std::cref(member), |
| 154 | std::cref(group.getInterface()), |
| 155 | std::cref(group.getProperty())), |
| 156 | SignalActions(actions)}; |
| 157 | auto isSameSig = [&intf = group.getInterface()](SignalPkg& pkg) { |
| 158 | auto& obj = std::get<SignalObject>(pkg); |
| 159 | return intf == std::get<Intf>(obj); |
| 160 | }; |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 161 | |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 162 | subscribe(match, std::move(signalPkg), isSameSig, mgr); |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 166 | void nameOwnerChanged(Manager* mgr, const Group& group, SignalActions actions, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 167 | const json&) |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 168 | { |
Matthew Barth | 508bc76 | 2021-10-27 11:01:54 -0500 | [diff] [blame] | 169 | std::vector<std::string> grpServices; |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 170 | // Groups are optional, but a signal triggered event with no groups |
| 171 | // will do nothing since signals require a group |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 172 | for (const auto& member : group.getMembers()) |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 173 | { |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 174 | auto serv = Manager::getService(member, group.getInterface()); |
| 175 | if (!serv.empty()) |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 176 | { |
Matthew Barth | 508bc76 | 2021-10-27 11:01:54 -0500 | [diff] [blame] | 177 | // No need to re-subscribe to the same service's nameOwnerChanged |
| 178 | // signal when a prior group member provided by the same service |
| 179 | // already did the subscription |
| 180 | if (std::find(grpServices.begin(), grpServices.end(), serv) == |
| 181 | grpServices.end()) |
| 182 | { |
| 183 | // Setup name owner changed signal handler on the group |
| 184 | // member's service |
| 185 | const auto match = rules::nameOwnerChanged(serv); |
| 186 | SignalPkg signalPkg = { |
| 187 | Handlers::nameOwnerChanged, |
| 188 | SignalObject(std::cref(member), |
| 189 | std::cref(group.getInterface()), |
| 190 | std::cref(group.getProperty())), |
| 191 | SignalActions(actions)}; |
| 192 | // If signal match already exists, then the service will be the |
| 193 | // same so add action to be run |
| 194 | auto isSameSig = [](SignalPkg& pkg) { return true; }; |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 195 | |
Matthew Barth | 508bc76 | 2021-10-27 11:01:54 -0500 | [diff] [blame] | 196 | subscribe(match, std::move(signalPkg), isSameSig, mgr); |
| 197 | grpServices.emplace_back(serv); |
| 198 | } |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 199 | } |
| 200 | else |
| 201 | { |
| 202 | // Unable to construct nameOwnerChanged match string |
| 203 | // Path and/or interface configured does not exist on dbus yet? |
| 204 | // TODO How to handle this? Create timer to keep checking for |
| 205 | // service to appear? When to stop checking? |
| 206 | log<level::ERR>( |
| 207 | fmt::format("Events will not be triggered by name owner changed" |
| 208 | "signals from service of path {}, interface {}", |
| 209 | member, group.getInterface()) |
| 210 | .c_str()); |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Matthew Barth | 93a10ab | 2021-10-15 14:45:42 -0500 | [diff] [blame] | 215 | void member(Manager* mgr, const Group& group, SignalActions actions, |
| 216 | const json&) |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 217 | { |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 218 | // No SignalObject required to associate to this signal |
| 219 | SignalPkg signalPkg = {Handlers::member, SignalObject(), |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 220 | SignalActions(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 |
| 223 | auto isSameSig = [](SignalPkg& pkg) { 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 | |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 238 | enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName, |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 239 | SignalActions actions) |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 240 | { |
| 241 | auto subscriber = signals.end(); |
| 242 | if (jsonObj.contains("signal")) |
| 243 | { |
| 244 | auto signal = jsonObj["signal"].get<std::string>(); |
| 245 | std::transform(signal.begin(), signal.end(), signal.begin(), tolower); |
| 246 | subscriber = signals.find(signal); |
| 247 | } |
| 248 | if (subscriber == signals.end()) |
| 249 | { |
| 250 | // Construct list of available signals |
| 251 | auto availSignals = |
| 252 | std::accumulate(std::next(signals.begin()), signals.end(), |
| 253 | signals.begin()->first, [](auto list, auto signal) { |
| 254 | return std::move(list) + ", " + signal.first; |
| 255 | }); |
| 256 | auto msg = |
| 257 | fmt::format("Event '{}' requires a supported signal given to be " |
| 258 | "triggered by signal, available signals: {}", |
| 259 | eventName, availSignals); |
| 260 | log<level::ERR>(msg.c_str()); |
| 261 | throw std::runtime_error(msg.c_str()); |
| 262 | } |
| 263 | |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 264 | return [subscriber = std::move(subscriber), |
| 265 | jsonObj](const std::string& eventName, Manager* mgr, |
Matthew Barth | cd6f379 | 2021-09-30 15:13:25 -0500 | [diff] [blame] | 266 | const std::vector<Group>& groups, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 267 | std::vector<std::unique_ptr<ActionBase>>& actions) { |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 268 | for (const auto& group : groups) |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 269 | { |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 270 | // Call signal subscriber for each group |
| 271 | subscriber->second(mgr, group, actions, jsonObj); |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 272 | } |
| 273 | }; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | } // namespace phosphor::fan::control::json::trigger::signal |