xref: /openbmc/openpower-vpd-parser/vpd-manager/include/listener.hpp (revision e621c063f1f293f510d5e10ee63f33c1ea27d70e)
1 #pragma once
2 
3 #include "constants.hpp"
4 #include "types.hpp"
5 #include "worker.hpp"
6 
7 #include <nlohmann/json.hpp>
8 #include <sdbusplus/asio/object_server.hpp>
9 
10 #include <memory>
11 
12 namespace vpd
13 {
14 /**
15  * @brief Class to listen on events
16  *
17  * This class will be used for registering and handling events on the system.
18  */
19 class Listener
20 {
21   public:
22     /**
23      * Deleted methods for Listener
24      */
25     Listener(const Listener&) = delete;
26     Listener& operator=(const Listener&) = delete;
27     Listener& operator=(Listener&&) = delete;
28     Listener(Listener&&) = delete;
29 
30     /**
31      * @brief Constructor
32      * @param[in] i_worker - Reference to worker class object.
33      * @param[in] i_asioConnection - Dbus Connection.
34      */
35     Listener(
36         const std::shared_ptr<Worker>& i_worker,
37         const std::shared_ptr<sdbusplus::asio::connection>& i_asioConnection);
38 
39     /**
40      * @brief API to register callback for Host state change.
41      *
42      */
43     void registerHostStateChangeCallback() const noexcept;
44 
45     /**
46      * @brief API to register callback for "AssetTag" property change.
47      */
48     void registerAssetTagChangeCallback() const noexcept;
49 
50     /**
51      * @brief API to register "Present" property change callback
52      *
53      * This API registers "Present" property change callback for FRUs for
54      * which "monitorPresence" is true in system config JSON.
55      */
56     void registerPresenceChangeCallback() noexcept;
57 
58     /**
59      * @brief API to register callback for all correlated properties.
60      *
61      * This API registers properties changed callback for all the interfaces in
62      * given correlated properties JSON file.
63      *
64      * @param[in] i_correlatedPropJsonFile - File path of correlated properties
65      * JSON.
66      */
67     void registerCorrPropCallBack(
68         const std::string& i_correlatedPropJsonFile =
69             constants::correlatedPropJsonFile) noexcept;
70 
71     /**
72      * @brief API to register properties changed callback.
73      *
74      * This API registers a properties changed callback for a specific interface
75      * under a service by constructing a match object. This API also saves the
76      * constructed match object into map object map data member.
77      *
78      * @param[in] i_service - Service name.
79      * @param[in] i_interface - Interface name.
80      * @param[in] i_callBackFunction - Callback function.
81      *
82      * @throw FirmwareException
83      */
84     void registerPropChangeCallBack(
85         const std::string& i_service, const std::string& i_interface,
86         std::function<void(sdbusplus::message_t& i_msg)> i_callBackFunction);
87 
88   private:
89     /**
90      * @brief API to process host state change callback.
91      *
92      * @param[in] i_msg - Callback message.
93      */
94     void hostStateChangeCallBack(sdbusplus::message_t& i_msg) const noexcept;
95 
96     /**
97      * @brief Callback API to be triggered on "AssetTag" property change.
98      *
99      * @param[in] i_msg - Callback message.
100      */
101     void assetTagChangeCallback(sdbusplus::message_t& i_msg) const noexcept;
102 
103     /**
104      * @brief Callback API to be triggered on "Present" property change.
105      *
106      * @param[in] i_msg - Callback message.
107      */
108     void presentPropertyChangeCallback(
109         sdbusplus::message_t& i_msg) const noexcept;
110 
111     /**
112      * @brief API which is called when correlated property change is detected
113      *
114      * @param[in] i_msg - Callback message.
115      */
116     void correlatedPropChangedCallBack(sdbusplus::message_t& i_msg) noexcept;
117 
118     /**
119      * @brief API to get correlated properties for given property.
120      *
121      * For a given service name, object path, interface and property, this API
122      * uses parsed correlated properties JSON object and returns a list of
123      * correlated object path, interface and property. Correlated properties are
124      * properties which are hosted under different interfaces with same or
125      * different data type, but share the same data. Hence if the data of a
126      * property is updated, then it's respective correlated property/properties
127      * should also be updated so that they remain in sync.
128      *
129      * @param[in] i_serviceName - Service name.
130      * @param[in] i_objectPath - Object path.
131      * @param[in] i_interface - Interface name.
132      * @param[in] i_property - Property name.
133      *
134      * @return On success, returns a vector of correlated object path, interface
135      * and property. Otherwise returns an empty vector.
136      *
137      * @throw FirmwareException
138      */
139     types::DbusPropertyList getCorrelatedProps(
140         const std::string& i_serviceName, const std::string& i_objectPath,
141         const std::string& i_interface, const std::string& i_property) const;
142 
143     /**
144      * @brief API to update a given correlated property
145      *
146      * This API updates a given correlated property on Dbus. For updates to
147      * properties on Phosphor Inventory Manager it uses Phosphor Inventory
148      * Manager's "Notify" API to update the given property.
149      *
150      * @param[in] i_serviceName - Service name.
151      * @param[in] i_corrProperty - Details of correlated property to update
152      * @param[in] i_value - Property value
153      *
154      * @return true, if correlated property was successfully updated, false
155      * otherwise.
156      */
157     bool updateCorrelatedProperty(
158         const std::string& i_serviceName,
159         const types::DbusPropertyEntry& i_corrProperty,
160         const types::DbusVariantType& i_value) const noexcept;
161 
162     // Shared pointer to worker class
163     const std::shared_ptr<Worker>& m_worker;
164 
165     // Shared pointer to bus connection.
166     const std::shared_ptr<sdbusplus::asio::connection>& m_asioConnection;
167 
168     // Map of inventory path to Present property match object
169     types::FruPresenceMatchObjectMap m_fruPresenceMatchObjectMap;
170 
171     // Parsed correlated properties JSON.
172     nlohmann::json m_correlatedPropJson{};
173 
174     // A map of {service name,{interface name,match object}}
175     types::MatchObjectMap m_matchObjectMap;
176 };
177 } // namespace vpd
178