blob: f430bfed288d4936a47f03e736cad3c2938a0a98 [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));
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050055 std::unique_ptr<sdbusplus::server::match::match> ptrMatch = nullptr;
56 if (!match.empty())
57 {
58 // Subscribe to signal
59 ptrMatch = std::make_unique<sdbusplus::server::match::match>(
60 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 Barth039f91e2021-10-04 15:18:07 -050077 // Same signal expected, add actions to be run when signal
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050078 // received
79 auto& pkgActions = std::get<SignalActions>(signalPkg);
80 auto& actions = std::get<SignalActions>(pkg);
Matthew Barth039f91e2021-10-04 15:18:07 -050081 actions.insert(actions.end(),
82 std::make_move_iterator(pkgActions.begin()),
83 std::make_move_iterator(pkgActions.end()));
Matthew Barthc024d782021-11-09 16:15:49 -060084 sameSignal = true;
85 break;
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050086 }
Matthew Barthc024d782021-11-09 16:15:49 -060087 }
88 if (!sameSignal)
89 {
90 // Expected signal differs, add signal package
91 pkgs->emplace_back(std::move(signalPkg));
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050092 }
93 }
94}
95
Matthew Barth039f91e2021-10-04 15:18:07 -050096void propertiesChanged(Manager* mgr, const Group& group, SignalActions actions,
Matthew Barth737f11c2021-09-27 14:23:08 -050097 const json&)
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050098{
99 // Groups are optional, but a signal triggered event with no groups
100 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500101 for (const auto& member : group.getMembers())
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500102 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500103 // Setup property changed signal handler on the group member's
104 // property
105 const auto match =
106 rules::propertiesChanged(member, group.getInterface());
107 SignalPkg signalPkg = {Handlers::propertiesChanged,
108 SignalObject(std::cref(member),
109 std::cref(group.getInterface()),
110 std::cref(group.getProperty())),
111 SignalActions(actions)};
112 auto isSameSig = [&prop = group.getProperty()](SignalPkg& pkg) {
113 auto& obj = std::get<SignalObject>(pkg);
114 return prop == std::get<Prop>(obj);
115 };
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500116
Matthew Barth039f91e2021-10-04 15:18:07 -0500117 subscribe(match, std::move(signalPkg), isSameSig, mgr);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500118 }
119}
120
Matthew Barth039f91e2021-10-04 15:18:07 -0500121void interfacesAdded(Manager* mgr, const Group& group, SignalActions actions,
Matthew Barth737f11c2021-09-27 14:23:08 -0500122 const json&)
Matthew Barth599afce2021-05-13 16:15:22 -0500123{
124 // Groups are optional, but a signal triggered event with no groups
125 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500126 for (const auto& member : group.getMembers())
Matthew Barth599afce2021-05-13 16:15:22 -0500127 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500128 // Setup interfaces added signal handler on the group member
Matt Spinlera7fcf3e2021-11-03 14:08:50 -0500129 const auto match =
130 rules::interfacesAdded() + rules::argNpath(0, member);
Matthew Barth039f91e2021-10-04 15:18:07 -0500131 SignalPkg signalPkg = {Handlers::interfacesAdded,
132 SignalObject(std::cref(member),
133 std::cref(group.getInterface()),
134 std::cref(group.getProperty())),
135 SignalActions(actions)};
136 auto isSameSig = [&intf = group.getInterface()](SignalPkg& pkg) {
137 auto& obj = std::get<SignalObject>(pkg);
138 return intf == std::get<Intf>(obj);
139 };
Matthew Barth599afce2021-05-13 16:15:22 -0500140
Matthew Barth039f91e2021-10-04 15:18:07 -0500141 subscribe(match, std::move(signalPkg), isSameSig, mgr);
Matthew Barth599afce2021-05-13 16:15:22 -0500142 }
143}
144
Matthew Barth039f91e2021-10-04 15:18:07 -0500145void interfacesRemoved(Manager* mgr, const Group& group, SignalActions actions,
Matthew Barth737f11c2021-09-27 14:23:08 -0500146 const json&)
Matthew Barthc2691402021-05-13 16:20:32 -0500147{
148 // Groups are optional, but a signal triggered event with no groups
149 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500150 for (const auto& member : group.getMembers())
Matthew Barthc2691402021-05-13 16:20:32 -0500151 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500152 // Setup interfaces added signal handler on the group member
153 const auto match = rules::interfacesRemoved(member);
154 SignalPkg signalPkg = {Handlers::interfacesRemoved,
155 SignalObject(std::cref(member),
156 std::cref(group.getInterface()),
157 std::cref(group.getProperty())),
158 SignalActions(actions)};
159 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
Matthew Barth039f91e2021-10-04 15:18:07 -0500168void nameOwnerChanged(Manager* mgr, const Group& group, SignalActions 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 Barth039f91e2021-10-04 15:18:07 -0500176 auto serv = Manager::getService(member, group.getInterface());
177 if (!serv.empty())
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500178 {
Matthew Barth508bc762021-10-27 11:01:54 -0500179 // No need to re-subscribe to the same service's nameOwnerChanged
180 // signal when a prior group member provided by the same service
181 // already did the subscription
182 if (std::find(grpServices.begin(), grpServices.end(), serv) ==
183 grpServices.end())
184 {
185 // Setup name owner changed signal handler on the group
186 // member's service
187 const auto match = rules::nameOwnerChanged(serv);
188 SignalPkg signalPkg = {
189 Handlers::nameOwnerChanged,
190 SignalObject(std::cref(member),
191 std::cref(group.getInterface()),
192 std::cref(group.getProperty())),
193 SignalActions(actions)};
194 // If signal match already exists, then the service will be the
195 // same so add action to be run
196 auto isSameSig = [](SignalPkg& pkg) { 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
Matthew Barth93a10ab2021-10-15 14:45:42 -0500217void member(Manager* mgr, const Group& group, SignalActions actions,
218 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
221 SignalPkg signalPkg = {Handlers::member, SignalObject(),
Matthew Barth039f91e2021-10-04 15:18:07 -0500222 SignalActions(actions)};
Matthew Barth16861792021-09-27 15:00:27 -0500223 // If signal match already exists, then the member signal will be the
224 // same so add action to be run
225 auto isSameSig = [](SignalPkg& pkg) { return true; };
Matthew Barth93a10ab2021-10-15 14:45:42 -0500226
227 // Groups are optional, but a signal triggered event with no groups
228 // will do nothing since signals require a group
229 for (const auto& member : group.getMembers())
230 {
231 // Subscribe for signal from each group member
232 const auto match =
233 rules::type::signal() + rules::member(group.getProperty()) +
234 rules::path(member) + rules::interface(group.getInterface());
235
236 subscribe(match, std::move(signalPkg), isSameSig, mgr);
237 }
Matthew Barth16861792021-09-27 15:00:27 -0500238}
239
Matthew Barth54b5a242021-05-21 11:02:52 -0500240enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName,
Matthew Barth039f91e2021-10-04 15:18:07 -0500241 SignalActions 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
253 auto availSignals =
254 std::accumulate(std::next(signals.begin()), signals.end(),
255 signals.begin()->first, [](auto list, auto signal) {
256 return std::move(list) + ", " + signal.first;
257 });
258 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),
267 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) {
Matthew Barth039f91e2021-10-04 15:18:07 -0500270 for (const auto& group : groups)
Matthew Barth54b5a242021-05-21 11:02:52 -0500271 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500272 // Call signal subscriber for each group
273 subscriber->second(mgr, group, actions, jsonObj);
Matthew Barth54b5a242021-05-21 11:02:52 -0500274 }
275 };
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500276}
277
278} // namespace phosphor::fan::control::json::trigger::signal