blob: aeb1fe5ffd17155662e6983403ce1d438b1927a9 [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
Matthew Barthe6af2332017-04-12 13:37:29 -050010// TODO Remove when generated.cpp included
11const std::vector<std::tuple<std::vector<std::shared_ptr<Event>>,
12 std::vector<Action>>>
13 Monitor::events
14{};
15
Matthew Barth1febc282017-04-12 11:33:39 -050016Monitor::Monitor(sdbusplus::bus::bus& bus) :
17 bus(bus)
18{
Matthew Barth240bf332017-04-12 13:48:29 -050019 // Process thru given events that are type 'signal'
20 for (auto& event : events)
21 {
22 for (auto& pEvent : std::get<std::vector<std::shared_ptr<Event>>>(event))
23 {
24 if (pEvent->trigger != Event::Trigger::SIGNAL)
25 {
26 continue;
27 }
Matthew Barth1febc282017-04-12 11:33:39 -050028
Matthew Barth240bf332017-04-12 13:48:29 -050029 auto signalEvent = static_cast<SignalEvent*>(pEvent.get());
30 eventArgs.emplace_back(std::make_unique<eventArg>(this,
31 signalEvent,
32 &event));
33 matches.emplace_back(bus,
34 signalEvent->signature,
35 handleSignal,
36 eventArgs.back().get());
37 }
38 }
Matthew Barth1febc282017-04-12 11:33:39 -050039}
40
Matthew Barthe6af2332017-04-12 13:37:29 -050041void Monitor::processStart() noexcept
42{
43 sdbusplus::message::message nullMsg{nullptr};
44
45 // Process thru given events that are type 'start'
46 for (auto& event : events)
47 {
48 for (auto& pEvent : std::get<std::vector<std::shared_ptr<Event>>>(event))
49 {
50 if (pEvent->trigger == Event::Trigger::START)
51 {
52 handleEvent(nullMsg, *pEvent, event);
53 }
54 }
55 }
56}
57
Matthew Barth240bf332017-04-12 13:48:29 -050058int Monitor::handleSignal(sd_bus_message* msg,
59 void* data,
60 sd_bus_error* err)
61{
62 auto sdbpMsg = sdbusplus::message::message(msg);
63 auto& eventArg = *static_cast<Monitor::eventArg*>(data);
64 std::get<0>(eventArg)->handleEvent(
65 sdbpMsg,
66 static_cast<const SignalEvent&>(*std::get<1>(eventArg)),
67 *std::get<2>(eventArg));
68 return 0;
69}
70
Matthew Barthe6af2332017-04-12 13:37:29 -050071void Monitor::handleEvent(sdbusplus::message::message& msg,
72 const Event& event,
73 const std::tuple<std::vector<std::shared_ptr<Event>>,
74 std::vector<Action>>& eventDef)
75{
76 // Iterate over conditions
77 for (auto& cond : event)
78 {
79 if (!cond(bus, msg, *this))
80 {
81 continue;
82 }
83 // Perform defined actions
84 for (auto& act : std::get<1>(eventDef))
85 {
86 act(bus, *this);
87 }
88 return;
89 }
90}
91
Matthew Barth1febc282017-04-12 11:33:39 -050092} // namespace monitoring
93} // namespace dbus
94} // namespace phosphor