Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 3 | #include "callback.hpp" |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 4 | #include "event_manager.hpp" |
| 5 | |
| 6 | #include <sstream> |
Andrew Geissler | ae4c95c | 2020-05-16 13:58:53 -0500 | [diff] [blame] | 7 | #include <string> |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace dbus |
| 12 | { |
| 13 | namespace 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 | */ |
| 23 | class EventBase : public IndexedCallback |
| 24 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 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; |
Patrick Williams | c5fe26a | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 32 | EventBase(const PropertyIndex& index) : IndexedCallback(index) {} |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 33 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 34 | /** @brief Callback interface implementation. */ |
| 35 | void operator()(Context ctx) override |
| 36 | { |
| 37 | if (ctx == Context::START) |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 38 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 39 | // No action should be taken |
| 40 | // as this call back is being called from |
| 41 | // daemon Startup. |
| 42 | return; |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 43 | } |
| 44 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 45 | 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 Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 51 | |
Patrick Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame] | 52 | if (value.has_value()) |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 53 | { |
| 54 | createEvent(path, propertyMeta, value); |
| 55 | } |
| 56 | } |
| 57 | } |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 58 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 59 | 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 Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame] | 69 | const std::any& value) const = 0; |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 70 | }; |
| 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 Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 77 | template <typename T> |
| 78 | class Event : public EventBase |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 79 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 80 | 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 Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 87 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 88 | /** @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 Venture | 0b45a3c | 2018-10-14 14:22:04 -0700 | [diff] [blame] | 93 | Event(const std::string& eventName, const std::string& eventMessage, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 94 | const PropertyIndex& index) : |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 95 | EventBase(index), name(eventName), message(eventMessage) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 96 | {} |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 97 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 98 | 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 Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame] | 107 | const std::any& value) const override |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 108 | { |
| 109 | std::stringstream ss{}; |
Patrick Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame] | 110 | ss << std::any_cast<T>(value); |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 111 | phosphor::events::getManager().create(name, message, path, property, |
| 112 | ss.str()); |
| 113 | } |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 114 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 115 | /** @brief Event Name */ |
| 116 | std::string name; |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 117 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 118 | /** @brief Event Message */ |
| 119 | std::string message; |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace monitoring |
| 123 | } // namespace dbus |
| 124 | } // namespace phosphor |