xref: /openbmc/phosphor-dbus-monitor/src/event.hpp (revision 3e84ec6645bcea669805947f1409b58b4b0d9f72)
1 #pragma once
2 
3 #include <phosphor-logging/log.hpp>
4 #include "callback.hpp"
5 #include "event_manager.hpp"
6 
7 #include <sstream>
8 
9 namespace phosphor
10 {
11 namespace dbus
12 {
13 namespace monitoring
14 {
15 
16 /** @class EventBase
17  *  @brief Event callback implementation.
18  *
19  *  The event callback creates the event dbus object
20  *  which has event message and metadata as key value pairs
21  *  as specified by the client supplied property index.
22  */
23 class EventBase : public IndexedCallback
24 {
25     public:
26         EventBase() = delete;
27         EventBase(const EventBase&) = delete;
28         EventBase(EventBase&&) = default;
29         EventBase& operator=(const EventBase&) = delete;
30         EventBase& operator=(EventBase&&) = default;
31         virtual ~EventBase() = default;
32         EventBase(const PropertyIndex& index) :
33             IndexedCallback(index) {}
34 
35         /** @brief Callback interface implementation. */
36         void operator()(Context ctx) override
37         {
38             if (ctx == Context::START)
39             {
40                 // No action should be taken
41                 // as this call back is being called from
42                 // daemon Startup.
43                 return;
44             }
45 
46             for (const auto& n : index)
47             {
48                 const auto& path = std::get<pathIndex>(n.first);
49                 const auto& propertyMeta = std::get<propertyIndex>(n.first);
50                 const auto& value = std::get<valueIndex>(n.second);
51 
52                 if (!value.get().empty())
53                 {
54                     createEvent(
55                             path,
56                             propertyMeta,
57                             value);
58                 }
59             }
60 
61         }
62 
63     private:
64 
65         /** @brief Create the event Dbus Object.
66          *  @param[in] path - Dbus Object Path for which the
67          *                    property has changed.
68          *  @param[in] property - Name of the property whose value
69          *                        has been changed.
70          *  @param[in] value - Changed property value.
71          */
72         virtual void createEvent(
73             const std::string& path,
74             const std::string& property,
75             const any_ns::any& value) const = 0;
76 
77 
78 };
79 
80 /** @class Event
81  *  @brief C++ type specific logic for the event callback.
82  *
83  *  @tparam T - The C++ type of the property values being traced.
84  */
85 template <typename T>
86 class Event : public EventBase
87 {
88     public:
89         Event() = delete;
90         Event(const Event&) = delete;
91         Event(Event&&) = default;
92         Event& operator=(const Event&) = delete;
93         Event& operator=(Event&&) = default;
94         ~Event() = default;
95 
96         /** @brief Constructor.
97          *  @param[in] eventName - Name of the event.
98          *  @param[in] eventMessage- Event Message.
99          *  @param[in] index - look up index for the properties.
100          */
101         Event(std::string eventName,
102               std::string eventMessage,
103               const PropertyIndex& index) :
104             EventBase(index),
105             name(eventName),
106             message(eventMessage) {}
107 
108     private:
109         /** @brief Create the event Dbus Object.
110          *  @param[in] path - Dbus Object Path for which the
111          *                    property has changed.
112          *  @param[in] property - Name of the property whose value
113          *                        has been changed.
114          *  @param[in] value - Changed property value.
115          */
116         void createEvent(
117             const std::string& path,
118             const std::string& property,
119             const any_ns::any& value) const override
120         {
121             std::stringstream ss {};
122             ss << any_ns::any_cast<T>(value);
123             phosphor::events::getManager().create(
124                 name, message, path, property, ss.str());
125         }
126 
127         /** @brief Event Name */
128         std::string name;
129 
130         /** @brief Event Message */
131         std::string message;
132 };
133 
134 } // namespace monitoring
135 } // namespace dbus
136 } // namespace phosphor
137