blob: fb72f89dde88a171db35c4524de1ef4490fb3153 [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
52 std::vector<SignalPkg> pkgs = {signalPkg};
Matthew Barthfac8a2f2021-06-10 15:50:36 -050053 std::vector<SignalPkg> dataPkgs =
54 std::vector<SignalPkg>(std::move(pkgs));
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050055 std::unique_ptr<sdbusplus::server::match::match> ptrMatch = nullptr;
Matthew Barth50219f52021-05-18 11:20:36 -050056 // TODO(ibm-openbmc/#3195) - Filter signal subscriptions to objects
57 // owned by fan control?
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050058 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 Barthfac8a2f2021-06-10 15:50:36 -050064 std::placeholders::_1, dataPkgs));
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050065 }
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 Barthfac8a2f2021-06-10 15:50:36 -050072 auto& pkgs = std::get<std::vector<SignalPkg>>(signalData.front());
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 Barthbaeeb8f2021-05-13 16:03:54 -050084 }
85 else
86 {
87 // Expected signal differs, add signal package
Matthew Barthfac8a2f2021-06-10 15:50:36 -050088 pkgs.emplace_back(std::move(signalPkg));
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050089 }
90 }
91 }
92}
93
Matthew Barth039f91e2021-10-04 15:18:07 -050094void propertiesChanged(Manager* mgr, const Group& group, SignalActions actions,
Matthew Barth737f11c2021-09-27 14:23:08 -050095 const json&)
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050096{
97 // Groups are optional, but a signal triggered event with no groups
98 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -050099 for (const auto& member : group.getMembers())
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500100 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500101 // 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 Barthbaeeb8f2021-05-13 16:03:54 -0500114
Matthew Barth039f91e2021-10-04 15:18:07 -0500115 subscribe(match, std::move(signalPkg), isSameSig, mgr);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500116 }
117}
118
Matthew Barth039f91e2021-10-04 15:18:07 -0500119void interfacesAdded(Manager* mgr, const Group& group, SignalActions actions,
Matthew Barth737f11c2021-09-27 14:23:08 -0500120 const json&)
Matthew Barth599afce2021-05-13 16:15:22 -0500121{
122 // Groups are optional, but a signal triggered event with no groups
123 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500124 for (const auto& member : group.getMembers())
Matthew Barth599afce2021-05-13 16:15:22 -0500125 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500126 // Setup interfaces added signal handler on the group member
Matt Spinlera7fcf3e2021-11-03 14:08:50 -0500127 const auto match =
128 rules::interfacesAdded() + rules::argNpath(0, member);
Matthew Barth039f91e2021-10-04 15:18:07 -0500129 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 Barth599afce2021-05-13 16:15:22 -0500138
Matthew Barth039f91e2021-10-04 15:18:07 -0500139 subscribe(match, std::move(signalPkg), isSameSig, mgr);
Matthew Barth599afce2021-05-13 16:15:22 -0500140 }
141}
142
Matthew Barth039f91e2021-10-04 15:18:07 -0500143void interfacesRemoved(Manager* mgr, const Group& group, SignalActions actions,
Matthew Barth737f11c2021-09-27 14:23:08 -0500144 const json&)
Matthew Barthc2691402021-05-13 16:20:32 -0500145{
146 // Groups are optional, but a signal triggered event with no groups
147 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500148 for (const auto& member : group.getMembers())
Matthew Barthc2691402021-05-13 16:20:32 -0500149 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500150 // 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 Barthc2691402021-05-13 16:20:32 -0500161
Matthew Barth039f91e2021-10-04 15:18:07 -0500162 subscribe(match, std::move(signalPkg), isSameSig, mgr);
Matthew Barthc2691402021-05-13 16:20:32 -0500163 }
164}
165
Matthew Barth039f91e2021-10-04 15:18:07 -0500166void nameOwnerChanged(Manager* mgr, const Group& group, SignalActions actions,
Matthew Barth737f11c2021-09-27 14:23:08 -0500167 const json&)
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500168{
Matthew Barth508bc762021-10-27 11:01:54 -0500169 std::vector<std::string> grpServices;
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500170 // Groups are optional, but a signal triggered event with no groups
171 // will do nothing since signals require a group
Matthew Barth039f91e2021-10-04 15:18:07 -0500172 for (const auto& member : group.getMembers())
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500173 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500174 auto serv = Manager::getService(member, group.getInterface());
175 if (!serv.empty())
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500176 {
Matthew Barth508bc762021-10-27 11:01:54 -0500177 // 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 Barthb03f6bb2021-05-13 16:23:10 -0500195
Matthew Barth508bc762021-10-27 11:01:54 -0500196 subscribe(match, std::move(signalPkg), isSameSig, mgr);
197 grpServices.emplace_back(serv);
198 }
Matthew Barth039f91e2021-10-04 15:18:07 -0500199 }
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 Barthb03f6bb2021-05-13 16:23:10 -0500211 }
212 }
213}
214
Matthew Barth93a10ab2021-10-15 14:45:42 -0500215void member(Manager* mgr, const Group& group, SignalActions actions,
216 const json&)
Matthew Barth16861792021-09-27 15:00:27 -0500217{
Matthew Barth16861792021-09-27 15:00:27 -0500218 // No SignalObject required to associate to this signal
219 SignalPkg signalPkg = {Handlers::member, SignalObject(),
Matthew Barth039f91e2021-10-04 15:18:07 -0500220 SignalActions(actions)};
Matthew Barth16861792021-09-27 15:00:27 -0500221 // 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 Barth93a10ab2021-10-15 14:45:42 -0500224
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 Barth16861792021-09-27 15:00:27 -0500236}
237
Matthew Barth54b5a242021-05-21 11:02:52 -0500238enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName,
Matthew Barth039f91e2021-10-04 15:18:07 -0500239 SignalActions actions)
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500240{
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 Barth737f11c2021-09-27 14:23:08 -0500264 return [subscriber = std::move(subscriber),
265 jsonObj](const std::string& eventName, Manager* mgr,
Matthew Barthcd6f3792021-09-30 15:13:25 -0500266 const std::vector<Group>& groups,
Matthew Barth737f11c2021-09-27 14:23:08 -0500267 std::vector<std::unique_ptr<ActionBase>>& actions) {
Matthew Barth039f91e2021-10-04 15:18:07 -0500268 for (const auto& group : groups)
Matthew Barth54b5a242021-05-21 11:02:52 -0500269 {
Matthew Barth039f91e2021-10-04 15:18:07 -0500270 // Call signal subscriber for each group
271 subscriber->second(mgr, group, actions, jsonObj);
Matthew Barth54b5a242021-05-21 11:02:52 -0500272 }
273 };
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500274}
275
276} // namespace phosphor::fan::control::json::trigger::signal