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