blob: 093b8e7905d08b2304faff9e9c34d13e7629aba5 [file] [log] [blame]
Matthew Barth1febc282017-04-12 11:33:39 -05001#include "monitor.hpp"
2
3namespace phosphor
4{
5namespace dbus
6{
7namespace monitoring
8{
9
10Monitor::Monitor(sdbusplus::bus::bus& bus) :
11 bus(bus)
12{
Matthew Barth240bf332017-04-12 13:48:29 -050013 // Process thru given events that are type 'signal'
14 for (auto& event : events)
15 {
16 for (auto& pEvent : std::get<std::vector<std::shared_ptr<Event>>>(event))
17 {
18 if (pEvent->trigger != Event::Trigger::SIGNAL)
19 {
20 continue;
21 }
Matthew Barth1febc282017-04-12 11:33:39 -050022
Matthew Barth240bf332017-04-12 13:48:29 -050023 auto signalEvent = static_cast<SignalEvent*>(pEvent.get());
24 eventArgs.emplace_back(std::make_unique<eventArg>(this,
25 signalEvent,
26 &event));
27 matches.emplace_back(bus,
28 signalEvent->signature,
29 handleSignal,
30 eventArgs.back().get());
31 }
32 }
Matthew Barth1febc282017-04-12 11:33:39 -050033}
34
Matthew Barthe6af2332017-04-12 13:37:29 -050035void Monitor::processStart() noexcept
36{
37 sdbusplus::message::message nullMsg{nullptr};
38
39 // Process thru given events that are type 'start'
40 for (auto& event : events)
41 {
42 for (auto& pEvent : std::get<std::vector<std::shared_ptr<Event>>>(event))
43 {
44 if (pEvent->trigger == Event::Trigger::START)
45 {
46 handleEvent(nullMsg, *pEvent, event);
47 }
48 }
49 }
50}
51
Matthew Barth240bf332017-04-12 13:48:29 -050052int Monitor::handleSignal(sd_bus_message* msg,
53 void* data,
54 sd_bus_error* err)
55{
56 auto sdbpMsg = sdbusplus::message::message(msg);
57 auto& eventArg = *static_cast<Monitor::eventArg*>(data);
58 std::get<0>(eventArg)->handleEvent(
59 sdbpMsg,
60 static_cast<const SignalEvent&>(*std::get<1>(eventArg)),
61 *std::get<2>(eventArg));
62 return 0;
63}
64
Matthew Barthe6af2332017-04-12 13:37:29 -050065void Monitor::handleEvent(sdbusplus::message::message& msg,
66 const Event& event,
67 const std::tuple<std::vector<std::shared_ptr<Event>>,
68 std::vector<Action>>& eventDef)
69{
70 // Iterate over conditions
71 for (auto& cond : event)
72 {
73 if (!cond(bus, msg, *this))
74 {
75 continue;
76 }
77 // Perform defined actions
78 for (auto& act : std::get<1>(eventDef))
79 {
80 act(bus, *this);
81 }
82 return;
83 }
84}
85
Matthew Barth1febc282017-04-12 11:33:39 -050086} // namespace monitoring
87} // namespace dbus
88} // namespace phosphor