blob: b355ca58a1b24a7d68164ae22e4c364797d466f1 [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
6#include <sstream>
Andrew Geisslerae4c95c2020-05-16 13:58:53 -05007#include <string>
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{
Brad Bishopd1eac882018-03-29 10:34:05 -040025 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;
Patrick Williamsc5fe26a2023-05-10 07:50:25 -050032 EventBase(const PropertyIndex& index) : IndexedCallback(index) {}
Ratan Gupta90bfaea2017-10-06 20:56:31 +053033
Brad Bishopd1eac882018-03-29 10:34:05 -040034 /** @brief Callback interface implementation. */
35 void operator()(Context ctx) override
36 {
37 if (ctx == Context::START)
Ratan Gupta90bfaea2017-10-06 20:56:31 +053038 {
Brad Bishopd1eac882018-03-29 10:34:05 -040039 // No action should be taken
40 // as this call back is being called from
41 // daemon Startup.
42 return;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053043 }
44
Brad Bishopd1eac882018-03-29 10:34:05 -040045 for (const auto& n : index)
46 {
47 const auto& path = std::get<pathIndex>(n.first);
48 const auto& propertyMeta = std::get<propertyIndex>(n.first);
49 const auto& storage = std::get<storageIndex>(n.second);
50 const auto& value = std::get<valueIndex>(storage.get());
Ratan Gupta90bfaea2017-10-06 20:56:31 +053051
Patrick Williams26dc0bc2022-06-16 17:06:18 -050052 if (value.has_value())
Brad Bishopd1eac882018-03-29 10:34:05 -040053 {
54 createEvent(path, propertyMeta, value);
55 }
56 }
57 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +053058
Brad Bishopd1eac882018-03-29 10:34:05 -040059 private:
60 /** @brief Create the event Dbus Object.
61 * @param[in] path - Dbus Object Path for which the
62 * property has changed.
63 * @param[in] property - Name of the property whose value
64 * has been changed.
65 * @param[in] value - Changed property value.
66 */
67 virtual void createEvent(const std::string& path,
68 const std::string& property,
Patrick Williams26dc0bc2022-06-16 17:06:18 -050069 const std::any& value) const = 0;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053070};
71
72/** @class Event
73 * @brief C++ type specific logic for the event callback.
74 *
75 * @tparam T - The C++ type of the property values being traced.
76 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070077template <typename T>
78class Event : public EventBase
Ratan Gupta90bfaea2017-10-06 20:56:31 +053079{
Brad Bishopd1eac882018-03-29 10:34:05 -040080 public:
81 Event() = delete;
82 Event(const Event&) = delete;
83 Event(Event&&) = default;
84 Event& operator=(const Event&) = delete;
85 Event& operator=(Event&&) = default;
86 ~Event() = default;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053087
Brad Bishopd1eac882018-03-29 10:34:05 -040088 /** @brief Constructor.
89 * @param[in] eventName - Name of the event.
90 * @param[in] eventMessage- Event Message.
91 * @param[in] index - look up index for the properties.
92 */
Patrick Venture0b45a3c2018-10-14 14:22:04 -070093 Event(const std::string& eventName, const std::string& eventMessage,
Brad Bishopd1eac882018-03-29 10:34:05 -040094 const PropertyIndex& index) :
Patrick Williamseab4f8c2024-08-16 15:20:10 -040095 EventBase(index), name(eventName), message(eventMessage)
George Liu3fe976c2022-06-21 09:37:04 +080096 {}
Ratan Gupta90bfaea2017-10-06 20:56:31 +053097
Brad Bishopd1eac882018-03-29 10:34:05 -040098 private:
99 /** @brief Create the event Dbus Object.
100 * @param[in] path - Dbus Object Path for which the
101 * property has changed.
102 * @param[in] property - Name of the property whose value
103 * has been changed.
104 * @param[in] value - Changed property value.
105 */
106 void createEvent(const std::string& path, const std::string& property,
Patrick Williams26dc0bc2022-06-16 17:06:18 -0500107 const std::any& value) const override
Brad Bishopd1eac882018-03-29 10:34:05 -0400108 {
109 std::stringstream ss{};
Patrick Williams26dc0bc2022-06-16 17:06:18 -0500110 ss << std::any_cast<T>(value);
Brad Bishopd1eac882018-03-29 10:34:05 -0400111 phosphor::events::getManager().create(name, message, path, property,
112 ss.str());
113 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530114
Brad Bishopd1eac882018-03-29 10:34:05 -0400115 /** @brief Event Name */
116 std::string name;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530117
Brad Bishopd1eac882018-03-29 10:34:05 -0400118 /** @brief Event Message */
119 std::string message;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530120};
121
122} // namespace monitoring
123} // namespace dbus
124} // namespace phosphor