Matthew Barth | c573643 | 2017-04-17 12:22:50 -0500 | [diff] [blame^] | 1 | /** |
| 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 Barth | 1febc28 | 2017-04-12 11:33:39 -0500 | [diff] [blame] | 16 | #include "monitor.hpp" |
| 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace dbus |
| 21 | { |
| 22 | namespace monitoring |
| 23 | { |
| 24 | |
| 25 | Monitor::Monitor(sdbusplus::bus::bus& bus) : |
| 26 | bus(bus) |
| 27 | { |
Matthew Barth | 240bf33 | 2017-04-12 13:48:29 -0500 | [diff] [blame] | 28 | // 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 Barth | 1febc28 | 2017-04-12 11:33:39 -0500 | [diff] [blame] | 37 | |
Matthew Barth | 240bf33 | 2017-04-12 13:48:29 -0500 | [diff] [blame] | 38 | 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 Barth | 1febc28 | 2017-04-12 11:33:39 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Matthew Barth | e6af233 | 2017-04-12 13:37:29 -0500 | [diff] [blame] | 50 | void 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 Barth | 240bf33 | 2017-04-12 13:48:29 -0500 | [diff] [blame] | 67 | int 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 Barth | e6af233 | 2017-04-12 13:37:29 -0500 | [diff] [blame] | 80 | void 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 Barth | 1febc28 | 2017-04-12 11:33:39 -0500 | [diff] [blame] | 101 | } // namespace monitoring |
| 102 | } // namespace dbus |
| 103 | } // namespace phosphor |