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