1 #pragma once
2 
3 #include "callback.hpp"
4 #include "event_manager.hpp"
5 
6 #include <phosphor-logging/log.hpp>
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>
80 class Event : public EventBase
81 {
82   public:
83     Event() = delete;
84     Event(const Event&) = delete;
85     Event(Event&&) = default;
86     Event& operator=(const Event&) = delete;
87     Event& operator=(Event&&) = default;
88     ~Event() = default;
89 
90     /** @brief Constructor.
91      *  @param[in] eventName - Name of the event.
92      *  @param[in] eventMessage- Event Message.
93      *  @param[in] index - look up index for the properties.
94      */
95     Event(const std::string& eventName, const std::string& eventMessage,
96           const PropertyIndex& index) :
97         EventBase(index),
98         name(eventName), message(eventMessage)
99     {
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 any_ns::any& value) const override
112     {
113         std::stringstream ss{};
114         ss << any_ns::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