xref: /openbmc/phosphor-dbus-monitor/src/event.hpp (revision 26dc0bcbe861cabf7e0bd5fe533d039a659d78a4)
1 #pragma once
2 
3 #include "callback.hpp"
4 #include "event_manager.hpp"
5 
6 #include <phosphor-logging/log.hpp>
7 #include <sstream>
8 #include <string>
9 
10 namespace phosphor
11 {
12 namespace dbus
13 {
14 namespace monitoring
15 {
16 
17 /** @class EventBase
18  *  @brief Event callback implementation.
19  *
20  *  The event callback creates the event dbus object
21  *  which has event message and metadata as key value pairs
22  *  as specified by the client supplied property index.
23  */
24 class EventBase : public IndexedCallback
25 {
26   public:
27     EventBase() = delete;
28     EventBase(const EventBase&) = delete;
29     EventBase(EventBase&&) = default;
30     EventBase& operator=(const EventBase&) = delete;
31     EventBase& operator=(EventBase&&) = default;
32     virtual ~EventBase() = default;
33     EventBase(const PropertyIndex& index) : IndexedCallback(index)
34     {
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 
103   private:
104     /** @brief Create the event Dbus Object.
105      *  @param[in] path - Dbus Object Path for which the
106      *                    property has changed.
107      *  @param[in] property - Name of the property whose value
108      *                        has been changed.
109      *  @param[in] value - Changed property value.
110      */
111     void createEvent(const std::string& path, const std::string& property,
112                      const std::any& value) const override
113     {
114         std::stringstream ss{};
115         ss << std::any_cast<T>(value);
116         phosphor::events::getManager().create(name, message, path, property,
117                                               ss.str());
118     }
119 
120     /** @brief Event Name */
121     std::string name;
122 
123     /** @brief Event Message */
124     std::string message;
125 };
126 
127 } // namespace monitoring
128 } // namespace dbus
129 } // namespace phosphor
130