blob: 8e4357eeb79e091b195fcc7bb0f7af8ef3ad4961 [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"
Sampa Misrac0c79482021-06-02 08:01:54 -05006#include "requester/handler.hpp"
Tom Joseph250c4752020-04-15 10:32:45 +05307
Riya Dixit49cfb132023-03-02 04:26:53 -06008#include <phosphor-logging/lg2.hpp>
9
Tom Joseph250c4752020-04-15 10:32:45 +053010#include <string>
11#include <utility>
12#include <vector>
13
Riya Dixit49cfb132023-03-02 04:26:53 -060014PHOSPHOR_LOG2_USING;
15
Tom Joseph250c4752020-04-15 10:32:45 +053016namespace pldm
17{
18
Tom Joseph250c4752020-04-15 10:32:45 +053019namespace host_effecters
20{
21
Brad Bishop5079ac42021-08-19 18:35:06 -040022using DbusChgHostEffecterProps =
23 std::map<dbus::Property, pldm::utils::PropertyValue>;
Tom Joseph250c4752020-04-15 10:32:45 +053024
25/** @struct State
26 * Contains the state set id and the possible states for
27 * an effecter
28 */
29struct PossibleState
30{
31 uint16_t stateSetId; //!< State set id
32 std::vector<uint8_t> states; //!< Possible states
33};
34
35/** @struct DBusEffecterMapping
36 * Contains the D-Bus information for an effecter
37 */
38struct DBusEffecterMapping
39{
Brad Bishop5079ac42021-08-19 18:35:06 -040040 pldm::utils::DBusMapping dbusMap;
41 std::vector<pldm::utils::PropertyValue>
42 propertyValues; //!< D-Bus property values
Tom Joseph250c4752020-04-15 10:32:45 +053043 PossibleState state; //!< Corresponding effecter states
44};
45
Thu Nguyena34a64b2022-03-31 08:56:39 +070046/** @struct DBusEffecterMapping
47 * Contains the D-Bus information for an effecter
48 */
49struct DBusNumericEffecterMapping
50{
51 pldm::utils::DBusMapping dbusMap;
52 uint8_t dataSize; //!< Numeric effecter PDR data size
53 double resolution; //!< Numeric effecter PDR resolution
54 double offset; //!< Numeric effecter PDR offset
55 int8_t unitModifier; //!< Numeric effecter PDR unitModifier
56 double propertyValue; //!< D-Bus property values
57};
58
Tom Joseph250c4752020-04-15 10:32:45 +053059/** @struct EffecterInfo
60 * Contains the effecter information as a whole
61 */
62struct EffecterInfo
63{
Thu Nguyena34a64b2022-03-31 08:56:39 +070064 uint8_t mctpEid; //!< Host mctp eid
65 uint8_t effecterPdrType; //!< Effecter PDR type state/numeric
66 uint16_t containerId; //!< Container Id for host effecter
67 uint16_t entityType; //!< Entity type for the host effecter
68 uint16_t entityInstance; //!< Entity instance for the host effecter
69 uint8_t compEffecterCnt; //!< Composite effecter count
70 bool checkHostState; //!< Check host state before setting effecter
Tom Joseph250c4752020-04-15 10:32:45 +053071 std::vector<DBusEffecterMapping>
Thu Nguyena34a64b2022-03-31 08:56:39 +070072 dbusInfo; //!< D-Bus information for the effecter id
73 std::vector<DBusNumericEffecterMapping>
74 dbusNumericEffecterInfo; //!< D-Bus information for the effecter id
Tom Joseph250c4752020-04-15 10:32:45 +053075};
76
77/** @class HostEffecterParser
78 *
79 * @brief This class parses the Host Effecter json file and monitors for the
80 * D-Bus changes for the effecters. Upon change, calls the corresponding
81 * setStateEffecterStates on the host
82 */
83class HostEffecterParser
84{
85 public:
86 HostEffecterParser() = delete;
87 HostEffecterParser(const HostEffecterParser&) = delete;
88 HostEffecterParser& operator=(const HostEffecterParser&) = delete;
89 HostEffecterParser(HostEffecterParser&&) = delete;
90 HostEffecterParser& operator=(HostEffecterParser&&) = delete;
91 virtual ~HostEffecterParser() = default;
92
93 /** @brief Constructor to create a HostEffecterParser object.
Andrew Jefferya330b2f2023-05-04 14:55:37 +093094 * @param[in] instanceIdDb - PLDM InstanceIdDb object pointer
Tom Joseph250c4752020-04-15 10:32:45 +053095 * @param[in] fd - socket fd to communicate to host
96 * @param[in] repo - PLDM PDR repository
97 * @param[in] dbusHandler - D-bus Handler
98 * @param[in] jsonPath - path for the json file
Sampa Misrac0c79482021-06-02 08:01:54 -050099 * @param[in] handler - PLDM request handler
Tom Joseph250c4752020-04-15 10:32:45 +0530100 */
Sampa Misrac0c79482021-06-02 08:01:54 -0500101 explicit HostEffecterParser(
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930102 pldm::InstanceIdDb* instanceIdDb, int fd, const pldm_pdr* repo,
Brad Bishop5079ac42021-08-19 18:35:06 -0400103 pldm::utils::DBusHandler* const dbusHandler,
104 const std::string& jsonPath,
Tom Josephe5268cd2021-09-07 13:04:03 +0530105 pldm::requester::Handler<pldm::requester::Request>* handler) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400106 instanceIdDb(instanceIdDb), sockFd(fd), pdrRepo(repo),
107 dbusHandler(dbusHandler), handler(handler)
Tom Joseph250c4752020-04-15 10:32:45 +0530108 {
109 try
110 {
111 parseEffecterJson(jsonPath);
112 }
113 catch (const std::exception& e)
114 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600115 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500116 "The json file '{PATH}' does not exist or malformed, error - '{ERROR}'",
117 "PATH", jsonPath, "ERROR", e);
Tom Joseph250c4752020-04-15 10:32:45 +0530118 }
119 }
120
121 /* @brief Parses the host effecter json
122 *
123 * @param[in] jsonPath - path for the json file
124 */
125 void parseEffecterJson(const std::string& jsonPath);
126
127 /* @brief Method to take action when the subscribed D-Bus property is
128 * changed
129 * @param[in] chProperties - list of properties which have changed
130 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
131 * hostEffecterInfo
132 * @param[in] dbusInfoIndex - index on dbusInfo pointer in each effecterInfo
133 * @param[in] effecterId - host effecter id
134 * @return - none
135 */
136 void processHostEffecterChangeNotification(
137 const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
138 size_t dbusInfoIndex, uint16_t effecterId);
139
Thu Nguyena34a64b2022-03-31 08:56:39 +0700140 /* @brief Method to take action when the subscribed D-Bus property is
141 * changed
142 * @param[in] chProperties - list of properties which have changed
143 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
144 * hostEffecterInfo
145 * @param[in] dbusInfoIndex - index on dbusInfo pointer in each effecterInfo
146
147 * @param[in] effecterId - terminus numeric effecter id
148 * @return - none
149 */
150 void processTerminusNumericEffecterChangeNotification(
151 const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
152 size_t dbusInfoIndex, uint16_t effecterId);
153
Tom Joseph250c4752020-04-15 10:32:45 +0530154 /* @brief Populate the property values in each dbusInfo from the json
155 *
156 * @param[in] dBusValues - json values
157 * @param[out] propertyValues - dbusInfo property values
158 * @param[in] propertyType - type of the D-Bus property
159 * @return - none
160 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400161 void populatePropVals(
162 const pldm::utils::Json& dBusValues,
163 std::vector<pldm::utils::PropertyValue>& propertyValues,
164 const std::string& propertyType);
Tom Joseph250c4752020-04-15 10:32:45 +0530165
166 /* @brief Set a host state effecter
167 *
168 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
169 * hostEffecterInfo
170 * @param[in] stateField - vector of state fields equal to composite
171 * effecter count in number
172 * @param[in] effecterId - host effecter id
173 * @return - PLDM status code
174 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400175 virtual int setHostStateEffecter(
176 size_t effecterInfoIndex,
177 std::vector<set_effecter_state_field>& stateField, uint16_t effecterId);
Tom Joseph250c4752020-04-15 10:32:45 +0530178
Thu Nguyena34a64b2022-03-31 08:56:39 +0700179 /* @brief Set a terminus numeric effecter
180 *
181 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
182 * hostEffecterInfo
183 * @param[in] effecterId - host effecter id
184 * @param[in] dataSize - data size
185 * @param[in] rawValue - raw value
186 * @return - PLDM status code
187 */
188 virtual int setTerminusNumericEffecter(size_t effecterInfoIndex,
189 uint16_t effecterId,
190 uint8_t dataSize, double rawValue);
191
Tom Joseph250c4752020-04-15 10:32:45 +0530192 /* @brief Fetches the new state value and the index in stateField set which
193 * needs to be set with the new value in the setStateEffecter call
194 * @param[in] effecterInfoIndex - index of effecterInfo in hostEffecterInfo
195 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
196 * @param[in] propertyValue - the changed D-Bus property value
197 * @return - the new state value
198 */
199 uint8_t findNewStateValue(size_t effecterInfoIndex, size_t dbusInfoIndex,
Brad Bishop5079ac42021-08-19 18:35:06 -0400200 const pldm::utils::PropertyValue& propertyValue);
Tom Joseph250c4752020-04-15 10:32:45 +0530201
202 /* @brief Subscribes for D-Bus property change signal on the specified
Thu Nguyena34a64b2022-03-31 08:56:39 +0700203 * object
Tom Joseph250c4752020-04-15 10:32:45 +0530204 *
205 * @param[in] objectPath - D-Bus object path to look for
206 * @param[in] interface - D-Bus interface
207 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
208 * hostEffecterInfo
209 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
210 * @param[in] effecterId - host effecter id
211 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400212 virtual void createHostEffecterMatch(
213 const std::string& objectPath, const std::string& interface,
214 size_t effecterInfoIndex, size_t dbusInfoIndex, uint16_t effecterId);
Tom Joseph250c4752020-04-15 10:32:45 +0530215
Thu Nguyena34a64b2022-03-31 08:56:39 +0700216 /* @brief Adjust the nummeric effecter value base on the effecter
217 * configurations
218 *
219 * @param[in] value - Raw value
220 * @param[in] offset - offset config
221 * @param[in] resolution - resolution config
222 * @param[in] modify - modify config
223 *
224 * @return - Value of effecter
225 */
226 double adjustValue(double value, double offset, double resolution,
227 int8_t modify);
228
229 private:
230 /* @brief Verify host On state before configure the host effecters
231 *
232 * @return - true if host is on and false for others cases
233 */
234 bool isHostOn(void);
235
Tom Joseph250c4752020-04-15 10:32:45 +0530236 protected:
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930237 pldm::InstanceIdDb* instanceIdDb; //!< Reference to the InstanceIdDb object
238 //!< to obtain instance id
239 int sockFd; //!< Socket fd to send message to host
240 const pldm_pdr* pdrRepo; //!< Reference to PDR repo
Tom Joseph250c4752020-04-15 10:32:45 +0530241 std::vector<EffecterInfo> hostEffecterInfo; //!< Parsed effecter information
Patrick Williams84b790c2022-07-22 19:26:56 -0500242 std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
Tom Joseph250c4752020-04-15 10:32:45 +0530243 effecterInfoMatch; //!< vector to catch the D-Bus property change
244 //!< signals for the effecters
Brad Bishop5079ac42021-08-19 18:35:06 -0400245 const pldm::utils::DBusHandler* dbusHandler; //!< D-bus Handler
Sampa Misrac0c79482021-06-02 08:01:54 -0500246 /** @brief PLDM request handler */
247 pldm::requester::Handler<pldm::requester::Request>* handler;
Tom Joseph250c4752020-04-15 10:32:45 +0530248};
249
250} // namespace host_effecters
251} // namespace pldm