Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 1 | /** |
| 2 | * @brief D-Bus signal watcher. |
| 3 | * |
| 4 | * This file is part of HostLogger project. |
| 5 | * |
| 6 | * Copyright (c) 2018 YADRO |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include "log_manager.hpp" |
| 24 | |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 25 | #include <map> |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 26 | #include <sdbusplus/bus/match.hpp> |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 27 | #include <set> |
| 28 | #include <string> |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 29 | |
| 30 | /** @class DbusServer |
| 31 | * @brief D-Bus service by host logger. |
| 32 | */ |
| 33 | class DbusWatcher |
| 34 | { |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 35 | public: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 36 | /** @brief Constructor. |
| 37 | * |
| 38 | * @param[in] logManager - log manager |
| 39 | * @param[in] bus - bus to attach to |
| 40 | */ |
| 41 | DbusWatcher(LogManager& logManager, sdbusplus::bus::bus& bus); |
| 42 | |
| 43 | /** @brief Initialize watcher. |
| 44 | * |
| 45 | * @return error code, 0 if operation completed successfully |
| 46 | */ |
| 47 | int initialize(); |
| 48 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 49 | private: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 50 | /** @brief Register D-Bus event handler. */ |
| 51 | void registerEventHandler(); |
| 52 | |
| 53 | /** @brief Register D-Bus timer handler. |
| 54 | * |
| 55 | * @return error code, 0 if operation completed successfully |
| 56 | */ |
| 57 | int registerTimerHandler(); |
| 58 | |
| 59 | /** @brief Setup D-Bus timer. |
| 60 | * |
| 61 | * @param[in] event - event source to setup |
| 62 | * |
| 63 | * @return error code, 0 if operation completed successfully |
| 64 | */ |
| 65 | int setupTimer(sd_event_source* event); |
| 66 | |
| 67 | /** @brief Callback function for host state change. |
| 68 | * |
| 69 | * @param[in] msg - data associated with subscribed signal |
| 70 | */ |
| 71 | void hostStateHandler(sdbusplus::message::message& msg); |
| 72 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 73 | /** @brief D-Bus IO callback used to handle incoming data on the opened |
| 74 | * file. See sd_event_io_handler_t for details. |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 75 | */ |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 76 | static int ioCallback(sd_event_source* event, int fd, uint32_t revents, |
| 77 | void* data); |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 78 | |
| 79 | /** @brief D-Bus timer callback used to flush log store. |
| 80 | * See sd_event_add_time for details. |
| 81 | */ |
| 82 | static int timerCallback(sd_event_source* event, uint64_t usec, void* data); |
| 83 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 84 | private: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 85 | /** @struct FlushCondition |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 86 | * @brief Describes flush conditions for log manager based on host state |
| 87 | * event. |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 88 | */ |
| 89 | struct FlushCondition |
| 90 | { |
| 91 | /** @brief D-Bus property name to watch. */ |
| 92 | std::string property; |
| 93 | /** @brief Set of possible values for property watched. */ |
| 94 | std::set<std::string> values; |
| 95 | /** @brief Match object to create D-Bus handler. */ |
| 96 | std::unique_ptr<sdbusplus::bus::match_t> match; |
| 97 | }; |
| 98 | |
Patrick Venture | 4d5a5dc | 2018-11-14 08:51:13 -0800 | [diff] [blame^] | 99 | private: |
Artem Senichev | efd5d74 | 2018-10-24 16:14:04 +0300 | [diff] [blame] | 100 | /** @brief Log manager instance. */ |
| 101 | LogManager& logManager_; |
| 102 | |
| 103 | /** @brief D-Bus bus. */ |
| 104 | sdbusplus::bus::bus& bus_; |
| 105 | |
| 106 | /** @brief Log storage flush conditions. |
| 107 | * Map D-Bus interface name to condition description. |
| 108 | */ |
| 109 | std::map<std::string, FlushCondition> conds_; |
| 110 | }; |