blob: 1f5201f00aa3d171df60e22b674ce3c5658d82f7 [file] [log] [blame]
Artem Senichevefd5d742018-10-24 16:14:04 +03001/**
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
25#include <set>
26#include <map>
27#include <string>
28
29#include <sdbusplus/bus/match.hpp>
30
31
32
33/** @class DbusServer
34 * @brief D-Bus service by host logger.
35 */
36class DbusWatcher
37{
38public:
39 /** @brief Constructor.
40 *
41 * @param[in] logManager - log manager
42 * @param[in] bus - bus to attach to
43 */
44 DbusWatcher(LogManager& logManager, sdbusplus::bus::bus& bus);
45
46 /** @brief Initialize watcher.
47 *
48 * @return error code, 0 if operation completed successfully
49 */
50 int initialize();
51
52private:
53 /** @brief Register D-Bus event handler. */
54 void registerEventHandler();
55
56 /** @brief Register D-Bus timer handler.
57 *
58 * @return error code, 0 if operation completed successfully
59 */
60 int registerTimerHandler();
61
62 /** @brief Setup D-Bus timer.
63 *
64 * @param[in] event - event source to setup
65 *
66 * @return error code, 0 if operation completed successfully
67 */
68 int setupTimer(sd_event_source* event);
69
70 /** @brief Callback function for host state change.
71 *
72 * @param[in] msg - data associated with subscribed signal
73 */
74 void hostStateHandler(sdbusplus::message::message& msg);
75
76 /** @brief D-Bus IO callback used to handle incoming data on the opened file.
77 * See sd_event_io_handler_t for details.
78 */
79 static int ioCallback(sd_event_source* event, int fd, uint32_t revents, void* data);
80
81 /** @brief D-Bus timer callback used to flush log store.
82 * See sd_event_add_time for details.
83 */
84 static int timerCallback(sd_event_source* event, uint64_t usec, void* data);
85
86private:
87 /** @struct FlushCondition
88 * @brief Describes flush conditions for log manager based on host state event.
89 */
90 struct FlushCondition
91 {
92 /** @brief D-Bus property name to watch. */
93 std::string property;
94 /** @brief Set of possible values for property watched. */
95 std::set<std::string> values;
96 /** @brief Match object to create D-Bus handler. */
97 std::unique_ptr<sdbusplus::bus::match_t> match;
98 };
99
100private:
101 /** @brief Log manager instance. */
102 LogManager& logManager_;
103
104 /** @brief D-Bus bus. */
105 sdbusplus::bus::bus& bus_;
106
107 /** @brief Log storage flush conditions.
108 * Map D-Bus interface name to condition description.
109 */
110 std::map<std::string, FlushCondition> conds_;
111};