summaryrefslogtreecommitdiffstats
path: root/src/propertywatch.hpp
blob: 3eb05df8fa1e9f066399745eba316f260e780eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * @file propertywatch.hpp
 * @brief PropertyWatch class declarations.
 *
 * In general class users should include propertywatchimpl.hpp instead to avoid
 * link failures.
 */
#pragma once

#include "data_types.hpp"
#include "watch.hpp"

namespace phosphor
{
namespace dbus
{
namespace monitoring
{

class Callback;

/** @class PropertyWatch
 *  @brief Type agnostic, factored out logic for property watches.
 *
 *  A property watch maintains the state of one or more DBus properties
 *  as specified by the supplied index.
 */
template <typename DBusInterfaceType>
class PropertyWatch : public Watch
{
    public:
        PropertyWatch() = delete;
        PropertyWatch(const PropertyWatch&) = delete;
        PropertyWatch(PropertyWatch&&) = default;
        PropertyWatch& operator=(const PropertyWatch&) = delete;
        PropertyWatch& operator=(PropertyWatch&&) = default;
        virtual ~PropertyWatch() = default;
        PropertyWatch(
            const PropertyIndex& watchIndex,
            Callback* cb = nullptr)
            : Watch(), index(watchIndex), callback(cb), alreadyRan(false) {}

        /** @brief Start the watch.
         *
         *  Watch start interface implementation for PropertyWatch.
         */
        void start() override;

        /** @brief Update properties.
         *
         *  Subclasses to query the properties specified by the index
         *  and update the cache.
         *
         *  @param[in] busName - The busname hosting the interface to query.
         *  @param[in] path - The path of the interface to query.
         *  @param[in] interface - The interface to query.
         */
        virtual void updateProperties(
            const std::string& busName,
            const std::string& path,
            const std::string& interface) = 0;

        /** @brief Dbus signal callback for PropertiesChanged.
         *
         *  Subclasses to update the cache.
         *
         *  @param[in] message - The org.freedesktop.DBus.PropertiesChanged
         *               message.
         *  @param[in] path - The path associated with the message.
         *  @param[in] interface - The interface associated with the message.
         */
        virtual void propertiesChanged(
            sdbusplus::message::message&,
            const std::string& path,
            const std::string& interface) = 0;

        /** @brief Dbus signal callback for InterfacesAdded.
         *
         *  Subclasses to update the cache.
         *
         *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
         *               message.
         */
        virtual void interfacesAdded(sdbusplus::message::message& msg) = 0;

    protected:

        /** @brief Property names and their associated storage. */
        const PropertyIndex& index;

        /** @brief Optional callback method. */
        Callback* const callback;

        /** @brief The start method should only be invoked once. */
        bool alreadyRan;
};

/** @class PropertyWatchOfType
 *  @brief Type specific logic for PropertyWatch.
 *
 *  @tparam DBusInterfaceType - DBus access delegate.
 *  @tparam T - The type of the properties being watched.
 */
template <typename T, typename DBusInterfaceType>
class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
{
    public:
        PropertyWatchOfType() = default;
        PropertyWatchOfType(const PropertyWatchOfType&) = delete;
        PropertyWatchOfType(PropertyWatchOfType&&) = default;
        PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete;
        PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default;
        ~PropertyWatchOfType() = default;
        PropertyWatchOfType(
            const PropertyIndex& watchIndex, Callback& callback) :
            PropertyWatch<DBusInterfaceType>(watchIndex, &callback) {}
        PropertyWatchOfType(
            const PropertyIndex& watchIndex) :
            PropertyWatch<DBusInterfaceType>(watchIndex, nullptr) {}


        /** @brief PropertyMatch implementation for PropertyWatchOfType.
         *
         *  @param[in] busName - The busname hosting the interface to query.
         *  @param[in] path - The path of the interface to query.
         *  @param[in] interface - The interface to query.
         */
        void updateProperties(
            const std::string& busName,
            const std::string& path,
            const std::string& interface) override;

        /** @brief PropertyMatch implementation for PropertyWatchOfType.
         *
         *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
         *               message.
         *  @param[in] path - The path associated with the message.
         *  @param[in] interface - The interface associated with the message.
         */
        void propertiesChanged(
            sdbusplus::message::message& msg,
            const std::string& path,
            const std::string& interface) override;

        /** @brief DBus agnostic implementation of interfacesAdded.
         *
         *  @param[in] path - The path of the properties that changed.
         *  @param[in] interface - The interface of the properties that
         *                  changed.
         *  @param[in] properites - The properties that changed.
         */
        void propertiesChanged(
            const std::string& path,
            const std::string& interface,
            const PropertiesChanged<T>& properties);

        /** @brief PropertyMatch implementation for PropertyWatchOfType.
         *
         *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
         *               message.
         */
        void interfacesAdded(sdbusplus::message::message& msg) override;

        /** @brief DBus agnostic implementation of interfacesAdded.
         *
         *  @param[in] path - The path of the added interfaces.
         *  @param[in] interfaces - The added interfaces.
         */
        void interfacesAdded(
            const std::string& path,
            const InterfacesAdded<T>& interfaces);
};

} // namespace monitoring
} // namespace dbus
} // namespace phosphor
OpenPOWER on IntegriCloud