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