blob: 7169d696fa2ea72b23466ae8e4d403c7ab5b703e [file] [log] [blame]
Matthew Barthbaeeb8f2021-05-13 16:03:54 -05001/**
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 Barth54b5a242021-05-21 11:02:52 -050022#include "trigger_aliases.hpp"
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050023
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
38namespace phosphor::fan::control::json::trigger::signal
39{
40
41using json = nlohmann::json;
42using namespace phosphor::logging;
43using namespace sdbusplus::bus::match;
44
45void subscribe(const std::string& match, SignalPkg&& signalPkg,
46 std::function<bool(SignalPkg&)> isSameSig, Manager* mgr)
47{
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050048 auto& signalData = mgr->getSignal(match);
49 if (signalData.empty())
50 {
51 // Signal subscription doesnt exist, add signal package and subscribe
Matthew Barthc024d782021-11-09 16:15:49 -060052 std::unique_ptr<std::vector<SignalPkg>> pkgs =
53 std::make_unique<std::vector<SignalPkg>>();
54 pkgs->emplace_back(std::move(signalPkg));
Patrick Williams3ea9ec22021-11-19 12:21:08 -060055 std::unique_ptr<sdbusplus::bus::match_t> ptrMatch = nullptr;
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050056 if (!match.empty())
57 {
58 // Subscribe to signal
Patrick Williams3ea9ec22021-11-19 12:21:08 -060059 ptrMatch = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050060 mgr->getBus(), match.c_str(),
61 std::bind(std::mem_fn(&Manager::handleSignal), &(*mgr),
Matthew Barthc024d782021-11-09 16:15:49 -060062 std::placeholders::_1, pkgs.get()));
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050063 }
Matthew Barthc024d782021-11-09 16:15:49 -060064 signalData.emplace_back(std::move(pkgs), std::move(ptrMatch));
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050065 }
66 else
67 {
68 // Signal subscription already exists
69 // Only a single signal data entry tied to each match is supported
Matthew Barthc024d782021-11-09 16:15:49 -060070 auto& pkgs = std::get<std::unique_ptr<std::vector<SignalPkg>>>(
71 signalData.front());
72 auto sameSignal = false;
73 for (auto& pkg : *pkgs)
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050074 {
75 if (isSameSig(pkg))
76 {
Matthew Barthc3a69082021-11-15 14:32:48 -060077 // Same SignalObject signal to trigger event actions,
78 // add actions to be run when signal for SignalObject received
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060079 auto& pkgActions = std::get<TriggerActions>(signalPkg);
80 auto& actions = std::get<TriggerActions>(pkg);
Matthew Barthc3a69082021-11-15 14:32:48 -060081 actions.insert(actions.end(), pkgActions.begin(),
82 pkgActions.end());
Matthew Barthc024d782021-11-09 16:15:49 -060083 sameSignal = true;
84 break;
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050085 }
Matthew Barthc024d782021-11-09 16:15:49 -060086 }
87 if (!sameSignal)
88 {
89 // Expected signal differs, add signal package
90 pkgs->emplace_back(std::move(signalPkg));
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050091 }
92 }
93}
94
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060095void propertiesChanged(Manager* mgr, const Group& group,
96 TriggerActions& actions, const json&)
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050097{
98 // Groups are optional, but a signal triggered event with no groups
99 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500100 for (const auto& member : group.getMembers())
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500101 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500102 // Setup property changed signal handler on the group member's
103 // property
Patrick Williams61b73292023-05-10 07:50:12 -0500104 const auto match = rules::propertiesChanged(member,
105 group.getInterface());
Matthew Barth039f91e2021-10-04 15:18:07 -0500106 SignalPkg signalPkg = {Handlers::propertiesChanged,
107 SignalObject(std::cref(member),
108 std::cref(group.getInterface()),
109 std::cref(group.getProperty())),
Matthew Barthc3a69082021-11-15 14:32:48 -0600110 actions};
Matthew Barth039f91e2021-10-04 15:18:07 -0500111 auto isSameSig = [&prop = group.getProperty()](SignalPkg& pkg) {
112 auto& obj = std::get<SignalObject>(pkg);
113 return prop == std::get<Prop>(obj);
114 };
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500115
Matthew Barth039f91e2021-10-04 15:18:07 -0500116 subscribe(match, std::move(signalPkg), isSameSig, mgr);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500117 }
118}
119
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600120void interfacesAdded(Manager* mgr, const Group& group, TriggerActions& actions,
Matthew Barth737f11c2021-09-27 14:23:08 -0500121 const json&)
Matthew Barth599afce2021-05-13 16:15:22 -0500122{
123 // Groups are optional, but a signal triggered event with no groups
124 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500125 for (const auto& member : group.getMembers())
Matthew Barth599afce2021-05-13 16:15:22 -0500126 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500127 // Setup interfaces added signal handler on the group member
Patrick Williams61b73292023-05-10 07:50:12 -0500128 const auto match = rules::interfacesAdded() +
129 rules::argNpath(0, member);
Matthew Barth039f91e2021-10-04 15:18:07 -0500130 SignalPkg signalPkg = {Handlers::interfacesAdded,
131 SignalObject(std::cref(member),
132 std::cref(group.getInterface()),
133 std::cref(group.getProperty())),
Matthew Barthc3a69082021-11-15 14:32:48 -0600134 actions};
Matthew Barth039f91e2021-10-04 15:18:07 -0500135 auto isSameSig = [&intf = group.getInterface()](SignalPkg& pkg) {
136 auto& obj = std::get<SignalObject>(pkg);
137 return intf == std::get<Intf>(obj);
138 };
Matthew Barth599afce2021-05-13 16:15:22 -0500139
Matthew Barth039f91e2021-10-04 15:18:07 -0500140 subscribe(match, std::move(signalPkg), isSameSig, mgr);
Matthew Barth599afce2021-05-13 16:15:22 -0500141 }
142}
143
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600144void interfacesRemoved(Manager* mgr, const Group& group,
145 TriggerActions& actions, const json&)
Matthew Barthc2691402021-05-13 16:20:32 -0500146{
147 // Groups are optional, but a signal triggered event with no groups
148 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500149 for (const auto& member : group.getMembers())
Matthew Barthc2691402021-05-13 16:20:32 -0500150 {
Matt Spinler78a48a52022-07-14 13:39:10 -0500151 // Setup interfaces removed signal handler on the group member
Patrick Williams61b73292023-05-10 07:50:12 -0500152 const auto match = rules::interfacesRemoved() +
153 rules::argNpath(0, member);
Matthew Barth039f91e2021-10-04 15:18:07 -0500154 SignalPkg signalPkg = {Handlers::interfacesRemoved,
155 SignalObject(std::cref(member),
156 std::cref(group.getInterface()),
157 std::cref(group.getProperty())),
Matthew Barthc3a69082021-11-15 14:32:48 -0600158 actions};
Matthew Barth039f91e2021-10-04 15:18:07 -0500159 auto isSameSig = [&intf = group.getInterface()](SignalPkg& pkg) {
160 auto& obj = std::get<SignalObject>(pkg);
161 return intf == std::get<Intf>(obj);
162 };
Matthew Barthc2691402021-05-13 16:20:32 -0500163
Matthew Barth039f91e2021-10-04 15:18:07 -0500164 subscribe(match, std::move(signalPkg), isSameSig, mgr);
Matthew Barthc2691402021-05-13 16:20:32 -0500165 }
166}
167
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600168void nameOwnerChanged(Manager* mgr, const Group& group, TriggerActions& actions,
Matthew Barth737f11c2021-09-27 14:23:08 -0500169 const json&)
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500170{
Matthew Barth508bc762021-10-27 11:01:54 -0500171 std::vector<std::string> grpServices;
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500172 // Groups are optional, but a signal triggered event with no groups
173 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500174 for (const auto& member : group.getMembers())
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500175 {
Matthew Barth65f72812022-01-13 15:05:34 -0600176 auto serv = group.getService();
177 if (serv.empty())
178 {
179 serv = Manager::getService(member, group.getInterface());
180 }
Matthew Barth039f91e2021-10-04 15:18:07 -0500181 if (!serv.empty())
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500182 {
Matthew Barth508bc762021-10-27 11:01:54 -0500183 // No need to re-subscribe to the same service's nameOwnerChanged
184 // signal when a prior group member provided by the same service
185 // already did the subscription
186 if (std::find(grpServices.begin(), grpServices.end(), serv) ==
187 grpServices.end())
188 {
189 // Setup name owner changed signal handler on the group
190 // member's service
191 const auto match = rules::nameOwnerChanged(serv);
Matthew Barth6d8e2d32022-02-01 16:47:08 -0600192 SignalPkg signalPkg = {Handlers::nameOwnerChanged,
193 SignalObject(), actions};
Matthew Barth508bc762021-10-27 11:01:54 -0500194 // If signal match already exists, then the service will be the
195 // same so add action to be run
Mike Cappsb2e9a4f2022-06-13 10:15:42 -0400196 auto isSameSig = [](SignalPkg&) { return true; };
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500197
Matthew Barth508bc762021-10-27 11:01:54 -0500198 subscribe(match, std::move(signalPkg), isSameSig, mgr);
199 grpServices.emplace_back(serv);
200 }
Matthew Barth039f91e2021-10-04 15:18:07 -0500201 }
202 else
203 {
204 // Unable to construct nameOwnerChanged match string
205 // Path and/or interface configured does not exist on dbus yet?
206 // TODO How to handle this? Create timer to keep checking for
207 // service to appear? When to stop checking?
208 log<level::ERR>(
209 fmt::format("Events will not be triggered by name owner changed"
210 "signals from service of path {}, interface {}",
211 member, group.getInterface())
212 .c_str());
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500213 }
214 }
215}
216
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600217void member(Manager* mgr, const Group& group, TriggerActions& actions,
Matthew Barth93a10ab2021-10-15 14:45:42 -0500218 const json&)
Matthew Barth16861792021-09-27 15:00:27 -0500219{
Matthew Barth16861792021-09-27 15:00:27 -0500220 // No SignalObject required to associate to this signal
Matthew Barthc3a69082021-11-15 14:32:48 -0600221 SignalPkg signalPkg = {Handlers::member, SignalObject(), actions};
Matthew Barth16861792021-09-27 15:00:27 -0500222 // If signal match already exists, then the member signal will be the
223 // same so add action to be run
Mike Cappsb2e9a4f2022-06-13 10:15:42 -0400224 auto isSameSig = [](SignalPkg&) { return true; };
Matthew Barth93a10ab2021-10-15 14:45:42 -0500225
226 // Groups are optional, but a signal triggered event with no groups
227 // will do nothing since signals require a group
228 for (const auto& member : group.getMembers())
229 {
230 // Subscribe for signal from each group member
231 const auto match =
232 rules::type::signal() + rules::member(group.getProperty()) +
233 rules::path(member) + rules::interface(group.getInterface());
234
235 subscribe(match, std::move(signalPkg), isSameSig, mgr);
236 }
Matthew Barth16861792021-09-27 15:00:27 -0500237}
238
Mike Cappsb2e9a4f2022-06-13 10:15:42 -0400239enableTrigger
240 triggerSignal(const json& jsonObj, const std::string& eventName,
241 std::vector<std::unique_ptr<ActionBase>>& /*actions*/)
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500242{
243 auto subscriber = signals.end();
244 if (jsonObj.contains("signal"))
245 {
246 auto signal = jsonObj["signal"].get<std::string>();
247 std::transform(signal.begin(), signal.end(), signal.begin(), tolower);
248 subscriber = signals.find(signal);
249 }
250 if (subscriber == signals.end())
251 {
252 // Construct list of available signals
Patrick Williams61b73292023-05-10 07:50:12 -0500253 auto availSignals = std::accumulate(
254 std::next(signals.begin()), signals.end(), signals.begin()->first,
255 [](auto list, auto signal) {
256 return std::move(list) + ", " + signal.first;
257 });
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500258 auto msg =
259 fmt::format("Event '{}' requires a supported signal given to be "
260 "triggered by signal, available signals: {}",
261 eventName, availSignals);
262 log<level::ERR>(msg.c_str());
263 throw std::runtime_error(msg.c_str());
264 }
265
Matthew Barth737f11c2021-09-27 14:23:08 -0500266 return [subscriber = std::move(subscriber),
Mike Cappsb2e9a4f2022-06-13 10:15:42 -0400267 jsonObj](const std::string& /*eventName*/, Manager* mgr,
Matthew Barthcd6f3792021-09-30 15:13:25 -0500268 const std::vector<Group>& groups,
Matthew Barth737f11c2021-09-27 14:23:08 -0500269 std::vector<std::unique_ptr<ActionBase>>& actions) {
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600270 TriggerActions signalActions;
Matthew Barthc3a69082021-11-15 14:32:48 -0600271 std::for_each(actions.begin(), actions.end(),
272 [&signalActions](auto& action) {
Patrick Williams61b73292023-05-10 07:50:12 -0500273 signalActions.emplace_back(std::ref(action));
274 });
Matthew Barth039f91e2021-10-04 15:18:07 -0500275 for (const auto& group : groups)
Matthew Barth54b5a242021-05-21 11:02:52 -0500276 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500277 // Call signal subscriber for each group
Matthew Barthc3a69082021-11-15 14:32:48 -0600278 subscriber->second(mgr, group, signalActions, jsonObj);
Matthew Barth54b5a242021-05-21 11:02:52 -0500279 }
280 };
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500281}
282
283} // namespace phosphor::fan::control::json::trigger::signal