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