blob: 7591381ce561ccfd42a9ba4bc96a6f280204cef6 [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
46/** @struct EffecterInfo
47 * Contains the effecter information as a whole
48 */
49struct EffecterInfo
50{
51 uint8_t mctpEid; //!< Host mctp eid
52 uint16_t containerId; //!< Container Id for host effecter
53 uint16_t entityType; //!< Entity type for the host effecter
54 uint16_t entityInstance; //!< Entity instance for the host effecter
55 uint8_t compEffecterCnt; //!< Composite effecter count
56 std::vector<DBusEffecterMapping>
Patrick Williams6da4f912023-05-10 07:50:53 -050057 dbusInfo; //!< D-Bus information for the effecter id
Tom Joseph250c4752020-04-15 10:32:45 +053058};
59
60/** @class HostEffecterParser
61 *
62 * @brief This class parses the Host Effecter json file and monitors for the
63 * D-Bus changes for the effecters. Upon change, calls the corresponding
64 * setStateEffecterStates on the host
65 */
66class HostEffecterParser
67{
68 public:
69 HostEffecterParser() = delete;
70 HostEffecterParser(const HostEffecterParser&) = delete;
71 HostEffecterParser& operator=(const HostEffecterParser&) = delete;
72 HostEffecterParser(HostEffecterParser&&) = delete;
73 HostEffecterParser& operator=(HostEffecterParser&&) = delete;
74 virtual ~HostEffecterParser() = default;
75
76 /** @brief Constructor to create a HostEffecterParser object.
Andrew Jefferya330b2f2023-05-04 14:55:37 +093077 * @param[in] instanceIdDb - PLDM InstanceIdDb object pointer
Tom Joseph250c4752020-04-15 10:32:45 +053078 * @param[in] fd - socket fd to communicate to host
79 * @param[in] repo - PLDM PDR repository
80 * @param[in] dbusHandler - D-bus Handler
81 * @param[in] jsonPath - path for the json file
Sampa Misrac0c79482021-06-02 08:01:54 -050082 * @param[in] handler - PLDM request handler
Tom Joseph250c4752020-04-15 10:32:45 +053083 */
Sampa Misrac0c79482021-06-02 08:01:54 -050084 explicit HostEffecterParser(
Andrew Jefferya330b2f2023-05-04 14:55:37 +093085 pldm::InstanceIdDb* instanceIdDb, int fd, const pldm_pdr* repo,
Brad Bishop5079ac42021-08-19 18:35:06 -040086 pldm::utils::DBusHandler* const dbusHandler,
87 const std::string& jsonPath,
Tom Josephe5268cd2021-09-07 13:04:03 +053088 pldm::requester::Handler<pldm::requester::Request>* handler) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040089 instanceIdDb(instanceIdDb), sockFd(fd), pdrRepo(repo),
90 dbusHandler(dbusHandler), handler(handler)
Tom Joseph250c4752020-04-15 10:32:45 +053091 {
92 try
93 {
94 parseEffecterJson(jsonPath);
95 }
96 catch (const std::exception& e)
97 {
Riya Dixit49cfb132023-03-02 04:26:53 -060098 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -050099 "The json file '{PATH}' does not exist or malformed, error - '{ERROR}'",
100 "PATH", jsonPath, "ERROR", e);
Tom Joseph250c4752020-04-15 10:32:45 +0530101 }
102 }
103
104 /* @brief Parses the host effecter json
105 *
106 * @param[in] jsonPath - path for the json file
107 */
108 void parseEffecterJson(const std::string& jsonPath);
109
110 /* @brief Method to take action when the subscribed D-Bus property is
111 * changed
112 * @param[in] chProperties - list of properties which have changed
113 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
114 * hostEffecterInfo
115 * @param[in] dbusInfoIndex - index on dbusInfo pointer in each effecterInfo
116 * @param[in] effecterId - host effecter id
117 * @return - none
118 */
119 void processHostEffecterChangeNotification(
120 const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
121 size_t dbusInfoIndex, uint16_t effecterId);
122
123 /* @brief Populate the property values in each dbusInfo from the json
124 *
125 * @param[in] dBusValues - json values
126 * @param[out] propertyValues - dbusInfo property values
127 * @param[in] propertyType - type of the D-Bus property
128 * @return - none
129 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400130 void populatePropVals(
131 const pldm::utils::Json& dBusValues,
132 std::vector<pldm::utils::PropertyValue>& propertyValues,
133 const std::string& propertyType);
Tom Joseph250c4752020-04-15 10:32:45 +0530134
135 /* @brief Set a host state effecter
136 *
137 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
138 * hostEffecterInfo
139 * @param[in] stateField - vector of state fields equal to composite
140 * effecter count in number
141 * @param[in] effecterId - host effecter id
142 * @return - PLDM status code
143 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400144 virtual int setHostStateEffecter(
145 size_t effecterInfoIndex,
146 std::vector<set_effecter_state_field>& stateField, uint16_t effecterId);
Tom Joseph250c4752020-04-15 10:32:45 +0530147
148 /* @brief Fetches the new state value and the index in stateField set which
149 * needs to be set with the new value in the setStateEffecter call
150 * @param[in] effecterInfoIndex - index of effecterInfo in hostEffecterInfo
151 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
152 * @param[in] propertyValue - the changed D-Bus property value
153 * @return - the new state value
154 */
155 uint8_t findNewStateValue(size_t effecterInfoIndex, size_t dbusInfoIndex,
Brad Bishop5079ac42021-08-19 18:35:06 -0400156 const pldm::utils::PropertyValue& propertyValue);
Tom Joseph250c4752020-04-15 10:32:45 +0530157
158 /* @brief Subscribes for D-Bus property change signal on the specified
159 * object
160 *
161 * @param[in] objectPath - D-Bus object path to look for
162 * @param[in] interface - D-Bus interface
163 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
164 * hostEffecterInfo
165 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
166 * @param[in] effecterId - host effecter id
167 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400168 virtual void createHostEffecterMatch(
169 const std::string& objectPath, const std::string& interface,
170 size_t effecterInfoIndex, size_t dbusInfoIndex, uint16_t effecterId);
Tom Joseph250c4752020-04-15 10:32:45 +0530171
172 protected:
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930173 pldm::InstanceIdDb* instanceIdDb; //!< Reference to the InstanceIdDb object
174 //!< to obtain instance id
175 int sockFd; //!< Socket fd to send message to host
176 const pldm_pdr* pdrRepo; //!< Reference to PDR repo
Tom Joseph250c4752020-04-15 10:32:45 +0530177 std::vector<EffecterInfo> hostEffecterInfo; //!< Parsed effecter information
Patrick Williams84b790c2022-07-22 19:26:56 -0500178 std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
Tom Joseph250c4752020-04-15 10:32:45 +0530179 effecterInfoMatch; //!< vector to catch the D-Bus property change
180 //!< signals for the effecters
Brad Bishop5079ac42021-08-19 18:35:06 -0400181 const pldm::utils::DBusHandler* dbusHandler; //!< D-bus Handler
Sampa Misrac0c79482021-06-02 08:01:54 -0500182 /** @brief PLDM request handler */
183 pldm::requester::Handler<pldm::requester::Request>* handler;
Tom Joseph250c4752020-04-15 10:32:45 +0530184};
185
186} // namespace host_effecters
187} // namespace pldm