blob: 13c6d52f85939633afa5f0d3f75d89242e181572 [file] [log] [blame]
Ratan Gupta90bfaea2017-10-06 20:56:31 +05301#pragma once
2
Ratan Gupta90bfaea2017-10-06 20:56:31 +05303#include "callback.hpp"
Ratan Gupta3e84ec62017-10-06 21:37:01 +05304#include "event_manager.hpp"
5
Patrick Venture3d6d3182018-08-31 09:33:09 -07006#include <phosphor-logging/log.hpp>
George Liu3fe976c2022-06-21 09:37:04 +08007
Ratan Gupta3e84ec62017-10-06 21:37:01 +05308#include <sstream>
Andrew Geisslerae4c95c2020-05-16 13:58:53 -05009#include <string>
Ratan Gupta90bfaea2017-10-06 20:56:31 +053010
11namespace phosphor
12{
13namespace dbus
14{
15namespace monitoring
16{
17
18/** @class EventBase
19 * @brief Event callback implementation.
20 *
21 * The event callback creates the event dbus object
22 * which has event message and metadata as key value pairs
23 * as specified by the client supplied property index.
24 */
25class EventBase : public IndexedCallback
26{
Brad Bishopd1eac882018-03-29 10:34:05 -040027 public:
28 EventBase() = delete;
29 EventBase(const EventBase&) = delete;
30 EventBase(EventBase&&) = default;
31 EventBase& operator=(const EventBase&) = delete;
32 EventBase& operator=(EventBase&&) = default;
33 virtual ~EventBase() = default;
34 EventBase(const PropertyIndex& index) : IndexedCallback(index)
George Liu3fe976c2022-06-21 09:37:04 +080035 {}
Ratan Gupta90bfaea2017-10-06 20:56:31 +053036
Brad Bishopd1eac882018-03-29 10:34:05 -040037 /** @brief Callback interface implementation. */
38 void operator()(Context ctx) override
39 {
40 if (ctx == Context::START)
Ratan Gupta90bfaea2017-10-06 20:56:31 +053041 {
Brad Bishopd1eac882018-03-29 10:34:05 -040042 // No action should be taken
43 // as this call back is being called from
44 // daemon Startup.
45 return;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053046 }
47
Brad Bishopd1eac882018-03-29 10:34:05 -040048 for (const auto& n : index)
49 {
50 const auto& path = std::get<pathIndex>(n.first);
51 const auto& propertyMeta = std::get<propertyIndex>(n.first);
52 const auto& storage = std::get<storageIndex>(n.second);
53 const auto& value = std::get<valueIndex>(storage.get());
Ratan Gupta90bfaea2017-10-06 20:56:31 +053054
Patrick Williams26dc0bc2022-06-16 17:06:18 -050055 if (value.has_value())
Brad Bishopd1eac882018-03-29 10:34:05 -040056 {
57 createEvent(path, propertyMeta, value);
58 }
59 }
60 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +053061
Brad Bishopd1eac882018-03-29 10:34:05 -040062 private:
63 /** @brief Create the event Dbus Object.
64 * @param[in] path - Dbus Object Path for which the
65 * property has changed.
66 * @param[in] property - Name of the property whose value
67 * has been changed.
68 * @param[in] value - Changed property value.
69 */
70 virtual void createEvent(const std::string& path,
71 const std::string& property,
Patrick Williams26dc0bc2022-06-16 17:06:18 -050072 const std::any& value) const = 0;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053073};
74
75/** @class Event
76 * @brief C++ type specific logic for the event callback.
77 *
78 * @tparam T - The C++ type of the property values being traced.
79 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070080template <typename T>
81class Event : public EventBase
Ratan Gupta90bfaea2017-10-06 20:56:31 +053082{
Brad Bishopd1eac882018-03-29 10:34:05 -040083 public:
84 Event() = delete;
85 Event(const Event&) = delete;
86 Event(Event&&) = default;
87 Event& operator=(const Event&) = delete;
88 Event& operator=(Event&&) = default;
89 ~Event() = default;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053090
Brad Bishopd1eac882018-03-29 10:34:05 -040091 /** @brief Constructor.
92 * @param[in] eventName - Name of the event.
93 * @param[in] eventMessage- Event Message.
94 * @param[in] index - look up index for the properties.
95 */
Patrick Venture0b45a3c2018-10-14 14:22:04 -070096 Event(const std::string& eventName, const std::string& eventMessage,
Brad Bishopd1eac882018-03-29 10:34:05 -040097 const PropertyIndex& index) :
98 EventBase(index),
99 name(eventName), message(eventMessage)
George Liu3fe976c2022-06-21 09:37:04 +0800100 {}
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530101
Brad Bishopd1eac882018-03-29 10:34:05 -0400102 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,
Patrick Williams26dc0bc2022-06-16 17:06:18 -0500111 const std::any& value) const override
Brad Bishopd1eac882018-03-29 10:34:05 -0400112 {
113 std::stringstream ss{};
Patrick Williams26dc0bc2022-06-16 17:06:18 -0500114 ss << std::any_cast<T>(value);
Brad Bishopd1eac882018-03-29 10:34:05 -0400115 phosphor::events::getManager().create(name, message, path, property,
116 ss.str());
117 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530118
Brad Bishopd1eac882018-03-29 10:34:05 -0400119 /** @brief Event Name */
120 std::string name;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530121
Brad Bishopd1eac882018-03-29 10:34:05 -0400122 /** @brief Event Message */
123 std::string message;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530124};
125
126} // namespace monitoring
127} // namespace dbus
128} // namespace phosphor