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