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