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 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 6 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 7 | #include <sstream> |
Andrew Geissler | ae4c95c | 2020-05-16 13:58:53 -0500 | [diff] [blame] | 8 | #include <string> |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace dbus |
| 13 | { |
| 14 | namespace monitoring |
| 15 | { |
| 16 | |
| 17 | /** @class EventBase |
| 18 | * @brief Event callback implementation. |
| 19 | * |
| 20 | * The event callback creates the event dbus object |
| 21 | * which has event message and metadata as key value pairs |
| 22 | * as specified by the client supplied property index. |
| 23 | */ |
| 24 | class EventBase : public IndexedCallback |
| 25 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 26 | public: |
| 27 | EventBase() = delete; |
| 28 | EventBase(const EventBase&) = delete; |
| 29 | EventBase(EventBase&&) = default; |
| 30 | EventBase& operator=(const EventBase&) = delete; |
| 31 | EventBase& operator=(EventBase&&) = default; |
| 32 | virtual ~EventBase() = default; |
| 33 | EventBase(const PropertyIndex& index) : IndexedCallback(index) |
| 34 | { |
| 35 | } |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 36 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 37 | /** @brief Callback interface implementation. */ |
| 38 | void operator()(Context ctx) override |
| 39 | { |
| 40 | if (ctx == Context::START) |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 41 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 42 | // No action should be taken |
| 43 | // as this call back is being called from |
| 44 | // daemon Startup. |
| 45 | return; |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 46 | } |
| 47 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 48 | 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 Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 54 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 55 | if (!value.empty()) |
| 56 | { |
| 57 | createEvent(path, propertyMeta, value); |
| 58 | } |
| 59 | } |
| 60 | } |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 61 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 62 | 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, |
| 72 | const any_ns::any& value) const = 0; |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 73 | }; |
| 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 Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 80 | template <typename T> |
| 81 | class Event : public EventBase |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 82 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 83 | 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 Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 90 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 91 | /** @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 Venture | 0b45a3c | 2018-10-14 14:22:04 -0700 | [diff] [blame] | 96 | Event(const std::string& eventName, const std::string& eventMessage, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 97 | const PropertyIndex& index) : |
| 98 | EventBase(index), |
| 99 | name(eventName), message(eventMessage) |
| 100 | { |
| 101 | } |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 102 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 103 | private: |
| 104 | /** @brief Create the event Dbus Object. |
| 105 | * @param[in] path - Dbus Object Path for which the |
| 106 | * property has changed. |
| 107 | * @param[in] property - Name of the property whose value |
| 108 | * has been changed. |
| 109 | * @param[in] value - Changed property value. |
| 110 | */ |
| 111 | void createEvent(const std::string& path, const std::string& property, |
| 112 | const any_ns::any& value) const override |
| 113 | { |
| 114 | std::stringstream ss{}; |
| 115 | ss << any_ns::any_cast<T>(value); |
| 116 | phosphor::events::getManager().create(name, message, path, property, |
| 117 | ss.str()); |
| 118 | } |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 119 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 120 | /** @brief Event Name */ |
| 121 | std::string name; |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 122 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 123 | /** @brief Event Message */ |
| 124 | std::string message; |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace monitoring |
| 128 | } // namespace dbus |
| 129 | } // namespace phosphor |