blob: a4372c6a325e1efbb25ea7c60f73d6056909b1c6 [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;
32 EventBase(const PropertyIndex& index) : IndexedCallback(index)
George Liu3fe976c2022-06-21 09:37:04 +080033 {}
Ratan Gupta90bfaea2017-10-06 20:56:31 +053034
Brad Bishopd1eac882018-03-29 10:34:05 -040035 /** @brief Callback interface implementation. */
36 void operator()(Context ctx) override
37 {
38 if (ctx == Context::START)
Ratan Gupta90bfaea2017-10-06 20:56:31 +053039 {
Brad Bishopd1eac882018-03-29 10:34:05 -040040 // No action should be taken
41 // as this call back is being called from
42 // daemon Startup.
43 return;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053044 }
45
Brad Bishopd1eac882018-03-29 10:34:05 -040046 for (const auto& n : index)
47 {
48 const auto& path = std::get<pathIndex>(n.first);
49 const auto& propertyMeta = std::get<propertyIndex>(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
Patrick Williams26dc0bc2022-06-16 17:06:18 -050053 if (value.has_value())
Brad Bishopd1eac882018-03-29 10:34:05 -040054 {
55 createEvent(path, propertyMeta, value);
56 }
57 }
58 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +053059
Brad Bishopd1eac882018-03-29 10:34:05 -040060 private:
61 /** @brief Create the event Dbus Object.
62 * @param[in] path - Dbus Object Path for which the
63 * property has changed.
64 * @param[in] property - Name of the property whose value
65 * has been changed.
66 * @param[in] value - Changed property value.
67 */
68 virtual void createEvent(const std::string& path,
69 const std::string& property,
Patrick Williams26dc0bc2022-06-16 17:06:18 -050070 const std::any& value) const = 0;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053071};
72
73/** @class Event
74 * @brief C++ type specific logic for the event callback.
75 *
76 * @tparam T - The C++ type of the property values being traced.
77 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070078template <typename T>
79class Event : public EventBase
Ratan Gupta90bfaea2017-10-06 20:56:31 +053080{
Brad Bishopd1eac882018-03-29 10:34:05 -040081 public:
82 Event() = delete;
83 Event(const Event&) = delete;
84 Event(Event&&) = default;
85 Event& operator=(const Event&) = delete;
86 Event& operator=(Event&&) = default;
87 ~Event() = default;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053088
Brad Bishopd1eac882018-03-29 10:34:05 -040089 /** @brief Constructor.
90 * @param[in] eventName - Name of the event.
91 * @param[in] eventMessage- Event Message.
92 * @param[in] index - look up index for the properties.
93 */
Patrick Venture0b45a3c2018-10-14 14:22:04 -070094 Event(const std::string& eventName, const std::string& eventMessage,
Brad Bishopd1eac882018-03-29 10:34:05 -040095 const PropertyIndex& index) :
96 EventBase(index),
97 name(eventName), message(eventMessage)
George Liu3fe976c2022-06-21 09:37:04 +080098 {}
Ratan Gupta90bfaea2017-10-06 20:56:31 +053099
Brad Bishopd1eac882018-03-29 10:34:05 -0400100 private:
101 /** @brief Create the event Dbus Object.
102 * @param[in] path - Dbus Object Path for which the
103 * property has changed.
104 * @param[in] property - Name of the property whose value
105 * has been changed.
106 * @param[in] value - Changed property value.
107 */
108 void createEvent(const std::string& path, const std::string& property,
Patrick Williams26dc0bc2022-06-16 17:06:18 -0500109 const std::any& value) const override
Brad Bishopd1eac882018-03-29 10:34:05 -0400110 {
111 std::stringstream ss{};
Patrick Williams26dc0bc2022-06-16 17:06:18 -0500112 ss << std::any_cast<T>(value);
Brad Bishopd1eac882018-03-29 10:34:05 -0400113 phosphor::events::getManager().create(name, message, path, property,
114 ss.str());
115 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530116
Brad Bishopd1eac882018-03-29 10:34:05 -0400117 /** @brief Event Name */
118 std::string name;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530119
Brad Bishopd1eac882018-03-29 10:34:05 -0400120 /** @brief Event Message */
121 std::string message;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530122};
123
124} // namespace monitoring
125} // namespace dbus
126} // namespace phosphor