blob: b5f1e90d881ed12eaebb2f908f61222ace2d1291 [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);
Matt Spinlerabe43ab2018-02-19 13:34:43 -060049 const auto& propertyMeta = std::get<metaIndex>(n.first);
50 const auto& storage = std::get<storageIndex>(n.second);
51 const auto& value = std::get<valueIndex>(storage.get());
Ratan Gupta90bfaea2017-10-06 20:56:31 +053052
Matt Spinlerabe43ab2018-02-19 13:34:43 -060053 if (!value.empty())
Ratan Gupta90bfaea2017-10-06 20:56:31 +053054 {
55 createEvent(
56 path,
57 propertyMeta,
58 value);
59 }
60 }
61
62 }
63
64 private:
65
66 /** @brief Create the event Dbus Object.
67 * @param[in] path - Dbus Object Path for which the
68 * property has changed.
69 * @param[in] property - Name of the property whose value
70 * has been changed.
71 * @param[in] value - Changed property value.
72 */
73 virtual void createEvent(
74 const std::string& path,
75 const std::string& property,
76 const any_ns::any& value) const = 0;
77
78
79};
80
81/** @class Event
82 * @brief C++ type specific logic for the event callback.
83 *
84 * @tparam T - The C++ type of the property values being traced.
85 */
86template <typename T>
87class Event : public EventBase
88{
89 public:
90 Event() = delete;
91 Event(const Event&) = delete;
92 Event(Event&&) = default;
93 Event& operator=(const Event&) = delete;
94 Event& operator=(Event&&) = default;
95 ~Event() = default;
96
97 /** @brief Constructor.
98 * @param[in] eventName - Name of the event.
99 * @param[in] eventMessage- Event Message.
100 * @param[in] index - look up index for the properties.
101 */
102 Event(std::string eventName,
103 std::string eventMessage,
104 const PropertyIndex& index) :
105 EventBase(index),
106 name(eventName),
107 message(eventMessage) {}
108
109 private:
110 /** @brief Create the event Dbus Object.
111 * @param[in] path - Dbus Object Path for which the
112 * property has changed.
113 * @param[in] property - Name of the property whose value
114 * has been changed.
115 * @param[in] value - Changed property value.
116 */
117 void createEvent(
118 const std::string& path,
119 const std::string& property,
Ratan Gupta3e84ec62017-10-06 21:37:01 +0530120 const any_ns::any& value) const override
121 {
122 std::stringstream ss {};
123 ss << any_ns::any_cast<T>(value);
124 phosphor::events::getManager().create(
125 name, message, path, property, ss.str());
126 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530127
128 /** @brief Event Name */
129 std::string name;
130
131 /** @brief Event Message */
132 std::string message;
133};
134
135} // namespace monitoring
136} // namespace dbus
137} // namespace phosphor