blob: 6a284b408acb4af22072e56286e7d196f563ccce [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
Artem Senichevefd5d742018-10-24 16:14:04 +030025#include <map>
Artem Senichevefd5d742018-10-24 16:14:04 +030026#include <sdbusplus/bus/match.hpp>
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080027#include <set>
28#include <string>
Artem Senichevefd5d742018-10-24 16:14:04 +030029
30/** @class DbusServer
31 * @brief D-Bus service by host logger.
32 */
33class DbusWatcher
34{
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080035 public:
Artem Senichevefd5d742018-10-24 16:14:04 +030036 /** @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 Venture4d5a5dc2018-11-14 08:51:13 -080049 private:
Artem Senichevefd5d742018-10-24 16:14:04 +030050 /** @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 Venture4d5a5dc2018-11-14 08:51:13 -080073 /** @brief D-Bus IO callback used to handle incoming data on the opened
74 * file. See sd_event_io_handler_t for details.
Artem Senichevefd5d742018-10-24 16:14:04 +030075 */
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080076 static int ioCallback(sd_event_source* event, int fd, uint32_t revents,
77 void* data);
Artem Senichevefd5d742018-10-24 16:14:04 +030078
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 Venture4d5a5dc2018-11-14 08:51:13 -080084 private:
Artem Senichevefd5d742018-10-24 16:14:04 +030085 /** @struct FlushCondition
Patrick Venture4d5a5dc2018-11-14 08:51:13 -080086 * @brief Describes flush conditions for log manager based on host state
87 * event.
Artem Senichevefd5d742018-10-24 16:14:04 +030088 */
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 Venture4d5a5dc2018-11-14 08:51:13 -080099 private:
Artem Senichevefd5d742018-10-24 16:14:04 +0300100 /** @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};