blob: 9e517fb3ca0e32f1ccdb3dd00b91efcfeb4feeb5 [file] [log] [blame]
Matthew Barthc5736432017-04-17 12:22:50 -05001/**
2 * Copyright © 2017 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 */
Matthew Barth1febc282017-04-12 11:33:39 -050016#include "monitor.hpp"
17
18namespace phosphor
19{
20namespace dbus
21{
22namespace monitoring
23{
24
25Monitor::Monitor(sdbusplus::bus::bus& bus) :
26 bus(bus)
27{
Matthew Barth240bf332017-04-12 13:48:29 -050028 // Process thru given events that are type 'signal'
29 for (auto& event : events)
30 {
31 for (auto& pEvent : std::get<std::vector<std::shared_ptr<Event>>>(event))
32 {
33 if (pEvent->trigger != Event::Trigger::SIGNAL)
34 {
35 continue;
36 }
Matthew Barth1febc282017-04-12 11:33:39 -050037
Matthew Barth240bf332017-04-12 13:48:29 -050038 auto signalEvent = static_cast<SignalEvent*>(pEvent.get());
39 eventArgs.emplace_back(std::make_unique<eventArg>(this,
40 signalEvent,
41 &event));
42 matches.emplace_back(bus,
43 signalEvent->signature,
44 handleSignal,
45 eventArgs.back().get());
46 }
47 }
Matthew Barth1febc282017-04-12 11:33:39 -050048}
49
Matthew Barthe6af2332017-04-12 13:37:29 -050050void Monitor::processStart() noexcept
51{
52 sdbusplus::message::message nullMsg{nullptr};
53
54 // Process thru given events that are type 'start'
55 for (auto& event : events)
56 {
57 for (auto& pEvent : std::get<std::vector<std::shared_ptr<Event>>>(event))
58 {
59 if (pEvent->trigger == Event::Trigger::START)
60 {
61 handleEvent(nullMsg, *pEvent, event);
62 }
63 }
64 }
65}
66
Matthew Barth240bf332017-04-12 13:48:29 -050067int Monitor::handleSignal(sd_bus_message* msg,
68 void* data,
69 sd_bus_error* err)
70{
71 auto sdbpMsg = sdbusplus::message::message(msg);
72 auto& eventArg = *static_cast<Monitor::eventArg*>(data);
73 std::get<0>(eventArg)->handleEvent(
74 sdbpMsg,
75 static_cast<const SignalEvent&>(*std::get<1>(eventArg)),
76 *std::get<2>(eventArg));
77 return 0;
78}
79
Matthew Barthe6af2332017-04-12 13:37:29 -050080void Monitor::handleEvent(sdbusplus::message::message& msg,
81 const Event& event,
82 const std::tuple<std::vector<std::shared_ptr<Event>>,
83 std::vector<Action>>& eventDef)
84{
85 // Iterate over conditions
86 for (auto& cond : event)
87 {
88 if (!cond(bus, msg, *this))
89 {
90 continue;
91 }
92 // Perform defined actions
93 for (auto& act : std::get<1>(eventDef))
94 {
95 act(bus, *this);
96 }
97 return;
98 }
99}
100
Matthew Barth1febc282017-04-12 11:33:39 -0500101} // namespace monitoring
102} // namespace dbus
103} // namespace phosphor