blob: ad02986c8d386e9d1221a9802b15ceba4a99726d [file] [log] [blame]
Ratan Gupta90bfaea2017-10-06 20:56:31 +05301#pragma once
2
3#include <phosphor-logging/log.hpp>
4#include "callback.hpp"
Ratan Gupta3e84ec62017-10-06 21:37:01 +05305#include "event_manager.hpp"
6
7#include <sstream>
Ratan Gupta90bfaea2017-10-06 20:56:31 +05308
9namespace phosphor
10{
11namespace dbus
12{
13namespace 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 */
23class 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) :
33 IndexedCallback(index) {}
34
35 /** @brief Callback interface implementation. */
Ratan Guptaa45e0862018-02-21 19:03:13 +053036 void operator()(Context ctx) override
Ratan Gupta90bfaea2017-10-06 20:56:31 +053037 {
Ratan Guptaef69ac02018-02-21 19:23:16 +053038 if (ctx == Context::START)
39 {
40 // No action should be taken
41 // as this call back is being called from
42 // daemon Startup.
43 return;
44 }
45
Ratan Gupta90bfaea2017-10-06 20:56:31 +053046 for (const auto& n : index)
47 {
48 const auto& path = std::get<pathIndex>(n.first);
Ratan Guptaef69ac02018-02-21 19:23:16 +053049 const auto& propertyMeta = std::get<propertyIndex>(n.first);
Ratan Gupta90bfaea2017-10-06 20:56:31 +053050 const auto& value = std::get<valueIndex>(n.second);
51
52 if (!value.get().empty())
53 {
54 createEvent(
55 path,
56 propertyMeta,
57 value);
58 }
59 }
60
61 }
62
63 private:
64
65 /** @brief Create the event Dbus Object.
66 * @param[in] path - Dbus Object Path for which the
67 * property has changed.
68 * @param[in] property - Name of the property whose value
69 * has been changed.
70 * @param[in] value - Changed property value.
71 */
72 virtual void createEvent(
73 const std::string& path,
74 const std::string& property,
75 const any_ns::any& value) const = 0;
76
77
78};
79
80/** @class Event
81 * @brief C++ type specific logic for the event callback.
82 *
83 * @tparam T - The C++ type of the property values being traced.
84 */
85template <typename T>
86class Event : public EventBase
87{
88 public:
89 Event() = delete;
90 Event(const Event&) = delete;
91 Event(Event&&) = default;
92 Event& operator=(const Event&) = delete;
93 Event& operator=(Event&&) = default;
94 ~Event() = default;
95
96 /** @brief Constructor.
97 * @param[in] eventName - Name of the event.
98 * @param[in] eventMessage- Event Message.
99 * @param[in] index - look up index for the properties.
100 */
101 Event(std::string eventName,
102 std::string eventMessage,
103 const PropertyIndex& index) :
104 EventBase(index),
105 name(eventName),
106 message(eventMessage) {}
107
108 private:
109 /** @brief Create the event Dbus Object.
110 * @param[in] path - Dbus Object Path for which the
111 * property has changed.
112 * @param[in] property - Name of the property whose value
113 * has been changed.
114 * @param[in] value - Changed property value.
115 */
116 void createEvent(
117 const std::string& path,
118 const std::string& property,
Ratan Gupta3e84ec62017-10-06 21:37:01 +0530119 const any_ns::any& value) const override
120 {
121 std::stringstream ss {};
122 ss << any_ns::any_cast<T>(value);
123 phosphor::events::getManager().create(
124 name, message, path, property, ss.str());
125 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530126
127 /** @brief Event Name */
128 std::string name;
129
130 /** @brief Event Message */
131 std::string message;
132};
133
134} // namespace monitoring
135} // namespace dbus
136} // namespace phosphor