blob: 3690ad8c76207c7eccc8aeedc544b0671d924722 [file] [log] [blame]
Tom Joseph250c4752020-04-15 10:32:45 +05301#pragma once
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/types.hpp"
4#include "common/utils.hpp"
Andrew Jefferya330b2f2023-05-04 14:55:37 +09305#include "pldmd/instance_id.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) :
Andrew Jefferya330b2f2023-05-04 14:55:37 +093089 instanceIdDb(instanceIdDb),
Tom Josephe5268cd2021-09-07 13:04:03 +053090 sockFd(fd), pdrRepo(repo), 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(
99 "The json file does not exist or malformed, ERROR={ERR_EXCEP}",
100 "ERR_EXCEP", e.what());
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 */
144 virtual int
145 setHostStateEffecter(size_t effecterInfoIndex,
146 std::vector<set_effecter_state_field>& stateField,
147 uint16_t effecterId);
148
149 /* @brief Fetches the new state value and the index in stateField set which
150 * needs to be set with the new value in the setStateEffecter call
151 * @param[in] effecterInfoIndex - index of effecterInfo in hostEffecterInfo
152 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
153 * @param[in] propertyValue - the changed D-Bus property value
154 * @return - the new state value
155 */
156 uint8_t findNewStateValue(size_t effecterInfoIndex, size_t dbusInfoIndex,
Brad Bishop5079ac42021-08-19 18:35:06 -0400157 const pldm::utils::PropertyValue& propertyValue);
Tom Joseph250c4752020-04-15 10:32:45 +0530158
159 /* @brief Subscribes for D-Bus property change signal on the specified
160 * object
161 *
162 * @param[in] objectPath - D-Bus object path to look for
163 * @param[in] interface - D-Bus interface
164 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
165 * hostEffecterInfo
166 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
167 * @param[in] effecterId - host effecter id
168 */
169 virtual void createHostEffecterMatch(const std::string& objectPath,
170 const std::string& interface,
171 size_t effecterInfoIndex,
172 size_t dbusInfoIndex,
173 uint16_t effecterId);
174
175 protected:
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930176 pldm::InstanceIdDb* instanceIdDb; //!< Reference to the InstanceIdDb object
177 //!< to obtain instance id
178 int sockFd; //!< Socket fd to send message to host
179 const pldm_pdr* pdrRepo; //!< Reference to PDR repo
Tom Joseph250c4752020-04-15 10:32:45 +0530180 std::vector<EffecterInfo> hostEffecterInfo; //!< Parsed effecter information
Patrick Williams84b790c2022-07-22 19:26:56 -0500181 std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
Tom Joseph250c4752020-04-15 10:32:45 +0530182 effecterInfoMatch; //!< vector to catch the D-Bus property change
183 //!< signals for the effecters
Brad Bishop5079ac42021-08-19 18:35:06 -0400184 const pldm::utils::DBusHandler* dbusHandler; //!< D-bus Handler
Sampa Misrac0c79482021-06-02 08:01:54 -0500185 /** @brief PLDM request handler */
186 pldm::requester::Handler<pldm::requester::Request>* handler;
Tom Joseph250c4752020-04-15 10:32:45 +0530187};
188
189} // namespace host_effecters
190} // namespace pldm