Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | #include "callback.hpp" |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 5 | #include "event_manager.hpp" |
| 6 | |
| 7 | #include <sstream> |
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 | { |
| 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; |
| 32 | EventBase(const PropertyIndex& index) : |
| 33 | IndexedCallback(index) {} |
| 34 | |
| 35 | /** @brief Callback interface implementation. */ |
Ratan Gupta | a45e086 | 2018-02-21 19:03:13 +0530 | [diff] [blame] | 36 | void operator()(Context ctx) override |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 37 | { |
Ratan Gupta | ef69ac0 | 2018-02-21 19:23:16 +0530 | [diff] [blame] | 38 | if (ctx == Context::START) |
| 39 | { |
| 40 | // No action should be taken |
| 41 | // as this call back is being called from |
| 42 | // daemon Startup. |
| 43 | return; |
| 44 | } |
| 45 | |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 46 | for (const auto& n : index) |
| 47 | { |
| 48 | const auto& path = std::get<pathIndex>(n.first); |
Matt Spinler | abe43ab | 2018-02-19 13:34:43 -0600 | [diff] [blame] | 49 | const auto& propertyMeta = std::get<metaIndex>(n.first); |
| 50 | const auto& storage = std::get<storageIndex>(n.second); |
| 51 | const auto& value = std::get<valueIndex>(storage.get()); |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 52 | |
Matt Spinler | abe43ab | 2018-02-19 13:34:43 -0600 | [diff] [blame] | 53 | if (!value.empty()) |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 54 | { |
| 55 | createEvent( |
| 56 | path, |
| 57 | propertyMeta, |
| 58 | value); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | |
| 66 | /** @brief Create the event Dbus Object. |
| 67 | * @param[in] path - Dbus Object Path for which the |
| 68 | * property has changed. |
| 69 | * @param[in] property - Name of the property whose value |
| 70 | * has been changed. |
| 71 | * @param[in] value - Changed property value. |
| 72 | */ |
| 73 | virtual void createEvent( |
| 74 | const std::string& path, |
| 75 | const std::string& property, |
| 76 | const any_ns::any& value) const = 0; |
| 77 | |
| 78 | |
| 79 | }; |
| 80 | |
| 81 | /** @class Event |
| 82 | * @brief C++ type specific logic for the event callback. |
| 83 | * |
| 84 | * @tparam T - The C++ type of the property values being traced. |
| 85 | */ |
| 86 | template <typename T> |
| 87 | class Event : public EventBase |
| 88 | { |
| 89 | public: |
| 90 | Event() = delete; |
| 91 | Event(const Event&) = delete; |
| 92 | Event(Event&&) = default; |
| 93 | Event& operator=(const Event&) = delete; |
| 94 | Event& operator=(Event&&) = default; |
| 95 | ~Event() = default; |
| 96 | |
| 97 | /** @brief Constructor. |
| 98 | * @param[in] eventName - Name of the event. |
| 99 | * @param[in] eventMessage- Event Message. |
| 100 | * @param[in] index - look up index for the properties. |
| 101 | */ |
| 102 | Event(std::string eventName, |
| 103 | std::string eventMessage, |
| 104 | const PropertyIndex& index) : |
| 105 | EventBase(index), |
| 106 | name(eventName), |
| 107 | message(eventMessage) {} |
| 108 | |
| 109 | private: |
| 110 | /** @brief Create the event Dbus Object. |
| 111 | * @param[in] path - Dbus Object Path for which the |
| 112 | * property has changed. |
| 113 | * @param[in] property - Name of the property whose value |
| 114 | * has been changed. |
| 115 | * @param[in] value - Changed property value. |
| 116 | */ |
| 117 | void createEvent( |
| 118 | const std::string& path, |
| 119 | const std::string& property, |
Ratan Gupta | 3e84ec6 | 2017-10-06 21:37:01 +0530 | [diff] [blame] | 120 | const any_ns::any& value) const override |
| 121 | { |
| 122 | std::stringstream ss {}; |
| 123 | ss << any_ns::any_cast<T>(value); |
| 124 | phosphor::events::getManager().create( |
| 125 | name, message, path, property, ss.str()); |
| 126 | } |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 127 | |
| 128 | /** @brief Event Name */ |
| 129 | std::string name; |
| 130 | |
| 131 | /** @brief Event Message */ |
| 132 | std::string message; |
| 133 | }; |
| 134 | |
| 135 | } // namespace monitoring |
| 136 | } // namespace dbus |
| 137 | } // namespace phosphor |