xref: /openbmc/pldm/platform-mc/dbus_to_terminus_effecters.hpp (revision dd1f28b818b86c3e46826b758d8d552b176d6292)
1 #pragma once
2 
3 #include "common/instance_id.hpp"
4 #include "common/types.hpp"
5 #include "common/utils.hpp"
6 #include "platform-mc/manager.hpp"
7 #include "requester/handler.hpp"
8 
9 #include <phosphor-logging/lg2.hpp>
10 
11 #include <string>
12 #include <utility>
13 #include <vector>
14 
15 PHOSPHOR_LOG2_USING;
16 
17 namespace pldm
18 {
19 
20 namespace host_effecters
21 {
22 
23 using DbusChgHostEffecterProps =
24     std::map<dbus::Property, pldm::utils::PropertyValue>;
25 
26 /** @struct State
27  *  Contains the state set id and the possible states for
28  *  an effecter
29  */
30 struct PossibleState
31 {
32     uint16_t stateSetId;         //!< State set id
33     std::vector<uint8_t> states; //!< Possible states
34 };
35 
36 /** @struct DBusEffecterMapping
37  *  Contains the D-Bus information for an effecter
38  */
39 struct DBusEffecterMapping
40 {
41     pldm::utils::DBusMapping dbusMap;
42     std::vector<pldm::utils::PropertyValue>
43         propertyValues;  //!< D-Bus property values
44     PossibleState state; //!< Corresponding effecter states
45 };
46 
47 /** @struct DBusEffecterMapping
48  *  Contains the D-Bus information for an effecter
49  */
50 struct DBusNumericEffecterMapping
51 {
52     pldm::utils::DBusMapping dbusMap;
53     uint8_t dataSize;     //!< Numeric effecter PDR data size
54     double resolution;    //!< Numeric effecter PDR resolution
55     double offset;        //!< Numeric effecter PDR offset
56     int8_t unitModifier;  //!< Numeric effecter PDR unitModifier
57     double propertyValue; //!< D-Bus property values
58 };
59 
60 /** @struct EffecterInfo
61  *  Contains the effecter information as a whole
62  */
63 struct EffecterInfo
64 {
65     uint8_t mctpEid;             //!< Host mctp eid
66     std::string terminusName;    //!< Terminus name
67     uint8_t effecterPdrType;     //!< Effecter PDR type state/numeric
68     uint16_t containerId;        //!< Container Id for host effecter
69     uint16_t entityType;         //!< Entity type for the host effecter
70     uint16_t entityInstance;     //!< Entity instance for the host effecter
71     uint8_t compEffecterCnt;     //!< Composite effecter count
72     bool checkHostState;         //!< Check host state before setting effecter
73     std::vector<DBusEffecterMapping>
74         dbusInfo;                //!< D-Bus information for the effecter id
75     std::vector<DBusNumericEffecterMapping>
76         dbusNumericEffecterInfo; //!< D-Bus information for the effecter id
77 };
78 
79 /** @class HostEffecterParser
80  *
81  *  @brief This class parses the Host Effecter json file and monitors for the
82  *         D-Bus changes for the effecters. Upon change, calls the corresponding
83  *         setStateEffecterStates on the host
84  */
85 class HostEffecterParser
86 {
87   public:
88     HostEffecterParser() = delete;
89     HostEffecterParser(const HostEffecterParser&) = delete;
90     HostEffecterParser& operator=(const HostEffecterParser&) = delete;
91     HostEffecterParser(HostEffecterParser&&) = delete;
92     HostEffecterParser& operator=(HostEffecterParser&&) = delete;
93     virtual ~HostEffecterParser() = default;
94 
95     /** @brief Constructor to create a HostEffecterParser object.
96      *  @param[in] instanceIdDb - PLDM InstanceIdDb object pointer
97      *  @param[in] fd - socket fd to communicate to host
98      *  @param[in] repo -  PLDM PDR repository
99      *  @param[in] dbusHandler - D-bus Handler
100      *  @param[in] jsonPath - path for the json file
101      *  @param[in] handler - PLDM request handler
102      */
HostEffecterParser(pldm::InstanceIdDb * instanceIdDb,int fd,const pldm_pdr * repo,pldm::utils::DBusHandler * const dbusHandler,const std::string & jsonPath,pldm::requester::Handler<pldm::requester::Request> * handler,platform_mc::Manager * platformManager)103     explicit HostEffecterParser(
104         pldm::InstanceIdDb* instanceIdDb, int fd, const pldm_pdr* repo,
105         pldm::utils::DBusHandler* const dbusHandler,
106         const std::string& jsonPath,
107         pldm::requester::Handler<pldm::requester::Request>* handler,
108         platform_mc::Manager* platformManager) :
109         instanceIdDb(instanceIdDb), sockFd(fd), pdrRepo(repo),
110         dbusHandler(dbusHandler), handler(handler),
111         platformManager(platformManager)
112     {
113         try
114         {
115             parseEffecterJson(jsonPath);
116         }
117         catch (const std::exception& e)
118         {
119             error(
120                 "The json file '{PATH}' does not exist or malformed, error - '{ERROR}'",
121                 "PATH", jsonPath, "ERROR", e);
122         }
123     }
124 
125     /* @brief Parses the host effecter json
126      *
127      * @param[in] jsonPath - path for the json file
128      */
129     void parseEffecterJson(const std::string& jsonPath);
130 
131     /* @brief Method to take action when the subscribed D-Bus property is
132      *        changed
133      * @param[in] chProperties - list of properties which have changed
134      * @param[in] effecterInfoIndex - index of effecterInfo pointer in
135      *                                hostEffecterInfo
136      * @param[in] dbusInfoIndex - index on dbusInfo pointer in each effecterInfo
137      * @param[in] effecterId - host effecter id
138      * @return - none
139      */
140     void processHostEffecterChangeNotification(
141         const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
142         size_t dbusInfoIndex, uint16_t effecterId);
143 
144     /* @brief Method to take action when the subscribed D-Bus property is
145      *        changed
146      * @param[in] chProperties - list of properties which have changed
147      * @param[in] effecterInfoIndex - index of effecterInfo pointer in
148      *                                hostEffecterInfo
149      * @param[in] dbusInfoIndex - index on dbusInfo pointer in each effecterInfo
150 
151      * @param[in] effecterId - terminus numeric effecter id
152      * @return - none
153      */
154     void processTerminusNumericEffecterChangeNotification(
155         const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
156         size_t dbusInfoIndex, uint16_t effecterId);
157 
158     /* @brief Populate the property values in each dbusInfo from the json
159      *
160      * @param[in] dBusValues - json values
161      * @param[out] propertyValues - dbusInfo property values
162      * @param[in] propertyType - type of the D-Bus property
163      * @return - none
164      */
165     void populatePropVals(
166         const pldm::utils::Json& dBusValues,
167         std::vector<pldm::utils::PropertyValue>& propertyValues,
168         const std::string& propertyType);
169 
170     /* @brief Set a host state effecter
171      *
172      * @param[in] effecterInfoIndex - index of effecterInfo pointer in
173      *                                hostEffecterInfo
174      * @param[in] stateField - vector of state fields equal to composite
175      *                         effecter count in number
176      * @param[in] effecterId - host effecter id
177      * @return - PLDM status code
178      */
179     virtual int setHostStateEffecter(
180         size_t effecterInfoIndex,
181         std::vector<set_effecter_state_field>& stateField, uint16_t effecterId);
182 
183     /* @brief Set a terminus numeric effecter
184      *
185      * @param[in] effecterInfoIndex - index of effecterInfo pointer in
186      *                                hostEffecterInfo
187      * @param[in] effecterId - host effecter id
188      * @param[in] dataSize - data size
189      * @param[in] rawValue - raw value
190      * @return - PLDM status code
191      */
192     virtual int setTerminusNumericEffecter(size_t effecterInfoIndex,
193                                            uint16_t effecterId,
194                                            uint8_t dataSize, double rawValue);
195 
196     /* @brief Fetches the new state value and the index in stateField set which
197      *        needs to be set with the new value in the setStateEffecter call
198      * @param[in] effecterInfoIndex - index of effecterInfo in hostEffecterInfo
199      * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
200      * @param[in] propertyValue - the changed D-Bus property value
201      * @return - the new state value
202      */
203     uint8_t findNewStateValue(size_t effecterInfoIndex, size_t dbusInfoIndex,
204                               const pldm::utils::PropertyValue& propertyValue);
205 
206     /* @brief Subscribes for D-Bus property change signal on the specified
207      *        object
208      *
209      * @param[in] objectPath - D-Bus object path to look for
210      * @param[in] interface - D-Bus interface
211      * @param[in] effecterInfoIndex - index of effecterInfo pointer in
212      *                                hostEffecterInfo
213      * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
214      * @param[in] effecterId - host effecter id
215      */
216     virtual void createHostEffecterMatch(
217         const std::string& objectPath, const std::string& interface,
218         size_t effecterInfoIndex, size_t dbusInfoIndex, uint16_t effecterId);
219 
220     /* @brief Adjust the numeric effecter value base on the effecter
221      *        configurations
222      *
223      * @param[in] value - Raw value
224      * @param[in] offset - offset config
225      * @param[in] resolution - resolution config
226      * @param[in] modify - modify config
227      *
228      * @return - Value of effecter
229      */
230     double adjustValue(double value, double offset, double resolution,
231                        int8_t modify);
232 
233   private:
234     /* @brief Verify host On state before configure the host effecters
235      *
236      * @return - true if host is on and false for others cases
237      */
238     bool isHostOn(void);
239 
240   protected:
241     pldm::InstanceIdDb* instanceIdDb; //!< Reference to the InstanceIdDb object
242                                       //!< to obtain instance id
243     int sockFd;                       //!< Socket fd to send message to host
244     const pldm_pdr* pdrRepo;          //!< Reference to PDR repo
245     std::vector<EffecterInfo> hostEffecterInfo; //!< Parsed effecter information
246     std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
247         effecterInfoMatch; //!< vector to catch the D-Bus property change
248                            //!< signals for the effecters
249     const pldm::utils::DBusHandler* dbusHandler; //!< D-bus Handler
250     /** @brief PLDM request handler */
251     pldm::requester::Handler<pldm::requester::Request>* handler;
252 
253     /** @brief MC Platform manager*/
254     platform_mc::Manager* platformManager = nullptr;
255 };
256 
257 } // namespace host_effecters
258 } // namespace pldm
259