blob: 962865cb0c65f9e6c2ce0014d335c5ec2dee34aa [file] [log] [blame]
Tom Joseph250c4752020-04-15 10:32:45 +05301#pragma once
2
Andrew Jeffery2abbce72023-10-18 10:17:35 +10303#include "common/instance_id.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05004#include "common/types.hpp"
5#include "common/utils.hpp"
Thu Nguyen38e12aa2025-01-21 22:47:56 +00006#include "platform-mc/manager.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -05007#include "requester/handler.hpp"
Tom Joseph250c4752020-04-15 10:32:45 +05308
Riya Dixit49cfb132023-03-02 04:26:53 -06009#include <phosphor-logging/lg2.hpp>
10
Tom Joseph250c4752020-04-15 10:32:45 +053011#include <string>
12#include <utility>
13#include <vector>
14
Riya Dixit49cfb132023-03-02 04:26:53 -060015PHOSPHOR_LOG2_USING;
16
Tom Joseph250c4752020-04-15 10:32:45 +053017namespace pldm
18{
19
Tom Joseph250c4752020-04-15 10:32:45 +053020namespace host_effecters
21{
22
Brad Bishop5079ac42021-08-19 18:35:06 -040023using DbusChgHostEffecterProps =
24 std::map<dbus::Property, pldm::utils::PropertyValue>;
Tom Joseph250c4752020-04-15 10:32:45 +053025
26/** @struct State
27 * Contains the state set id and the possible states for
28 * an effecter
29 */
30struct 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 */
39struct DBusEffecterMapping
40{
Brad Bishop5079ac42021-08-19 18:35:06 -040041 pldm::utils::DBusMapping dbusMap;
42 std::vector<pldm::utils::PropertyValue>
43 propertyValues; //!< D-Bus property values
Tom Joseph250c4752020-04-15 10:32:45 +053044 PossibleState state; //!< Corresponding effecter states
45};
46
Thu Nguyena34a64b2022-03-31 08:56:39 +070047/** @struct DBusEffecterMapping
48 * Contains the D-Bus information for an effecter
49 */
50struct 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
Tom Joseph250c4752020-04-15 10:32:45 +053060/** @struct EffecterInfo
61 * Contains the effecter information as a whole
62 */
63struct EffecterInfo
64{
Thu Nguyena34a64b2022-03-31 08:56:39 +070065 uint8_t mctpEid; //!< Host mctp eid
Thu Nguyen38e12aa2025-01-21 22:47:56 +000066 std::string terminusName; //!< Terminus name
Thu Nguyena34a64b2022-03-31 08:56:39 +070067 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
Tom Joseph250c4752020-04-15 10:32:45 +053073 std::vector<DBusEffecterMapping>
Thu Nguyena34a64b2022-03-31 08:56:39 +070074 dbusInfo; //!< D-Bus information for the effecter id
75 std::vector<DBusNumericEffecterMapping>
76 dbusNumericEffecterInfo; //!< D-Bus information for the effecter id
Tom Joseph250c4752020-04-15 10:32:45 +053077};
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 */
85class 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.
Andrew Jefferya330b2f2023-05-04 14:55:37 +093096 * @param[in] instanceIdDb - PLDM InstanceIdDb object pointer
Tom Joseph250c4752020-04-15 10:32:45 +053097 * @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
Sampa Misrac0c79482021-06-02 08:01:54 -0500101 * @param[in] handler - PLDM request handler
Tom Joseph250c4752020-04-15 10:32:45 +0530102 */
Sampa Misrac0c79482021-06-02 08:01:54 -0500103 explicit HostEffecterParser(
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930104 pldm::InstanceIdDb* instanceIdDb, int fd, const pldm_pdr* repo,
Brad Bishop5079ac42021-08-19 18:35:06 -0400105 pldm::utils::DBusHandler* const dbusHandler,
106 const std::string& jsonPath,
Thu Nguyen38e12aa2025-01-21 22:47:56 +0000107 pldm::requester::Handler<pldm::requester::Request>* handler,
108 platform_mc::Manager* platformManager) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400109 instanceIdDb(instanceIdDb), sockFd(fd), pdrRepo(repo),
Thu Nguyen38e12aa2025-01-21 22:47:56 +0000110 dbusHandler(dbusHandler), handler(handler),
111 platformManager(platformManager)
Tom Joseph250c4752020-04-15 10:32:45 +0530112 {
113 try
114 {
115 parseEffecterJson(jsonPath);
116 }
117 catch (const std::exception& e)
118 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600119 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500120 "The json file '{PATH}' does not exist or malformed, error - '{ERROR}'",
121 "PATH", jsonPath, "ERROR", e);
Tom Joseph250c4752020-04-15 10:32:45 +0530122 }
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
Thu Nguyena34a64b2022-03-31 08:56:39 +0700144 /* @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
Tom Joseph250c4752020-04-15 10:32:45 +0530158 /* @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 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400165 void populatePropVals(
166 const pldm::utils::Json& dBusValues,
167 std::vector<pldm::utils::PropertyValue>& propertyValues,
168 const std::string& propertyType);
Tom Joseph250c4752020-04-15 10:32:45 +0530169
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 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400179 virtual int setHostStateEffecter(
180 size_t effecterInfoIndex,
181 std::vector<set_effecter_state_field>& stateField, uint16_t effecterId);
Tom Joseph250c4752020-04-15 10:32:45 +0530182
Thu Nguyena34a64b2022-03-31 08:56:39 +0700183 /* @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
Tom Joseph250c4752020-04-15 10:32:45 +0530196 /* @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,
Brad Bishop5079ac42021-08-19 18:35:06 -0400204 const pldm::utils::PropertyValue& propertyValue);
Tom Joseph250c4752020-04-15 10:32:45 +0530205
206 /* @brief Subscribes for D-Bus property change signal on the specified
Thu Nguyena34a64b2022-03-31 08:56:39 +0700207 * object
Tom Joseph250c4752020-04-15 10:32:45 +0530208 *
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 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400216 virtual void createHostEffecterMatch(
217 const std::string& objectPath, const std::string& interface,
218 size_t effecterInfoIndex, size_t dbusInfoIndex, uint16_t effecterId);
Tom Joseph250c4752020-04-15 10:32:45 +0530219
Manojkiran Eda04ac9972024-09-06 10:57:12 +0530220 /* @brief Adjust the numeric effecter value base on the effecter
Thu Nguyena34a64b2022-03-31 08:56:39 +0700221 * 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
Tom Joseph250c4752020-04-15 10:32:45 +0530240 protected:
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930241 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
Tom Joseph250c4752020-04-15 10:32:45 +0530245 std::vector<EffecterInfo> hostEffecterInfo; //!< Parsed effecter information
Patrick Williams84b790c2022-07-22 19:26:56 -0500246 std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
Tom Joseph250c4752020-04-15 10:32:45 +0530247 effecterInfoMatch; //!< vector to catch the D-Bus property change
248 //!< signals for the effecters
Brad Bishop5079ac42021-08-19 18:35:06 -0400249 const pldm::utils::DBusHandler* dbusHandler; //!< D-bus Handler
Sampa Misrac0c79482021-06-02 08:01:54 -0500250 /** @brief PLDM request handler */
251 pldm::requester::Handler<pldm::requester::Request>* handler;
Thu Nguyen38e12aa2025-01-21 22:47:56 +0000252
253 /** @brief MC Platform manager*/
254 platform_mc::Manager* platformManager = nullptr;
Tom Joseph250c4752020-04-15 10:32:45 +0530255};
256
257} // namespace host_effecters
258} // namespace pldm