blob: e274540ea2c32ec9cf56778bbee1b06570ee4bcc [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"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -05005#include "pldmd/dbus_impl_requester.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -05006#include "requester/handler.hpp"
Tom Joseph250c4752020-04-15 10:32:45 +05307
8#include <string>
9#include <utility>
10#include <vector>
11
12namespace pldm
13{
14
Tom Joseph250c4752020-04-15 10:32:45 +053015namespace host_effecters
16{
17
Brad Bishop5079ac42021-08-19 18:35:06 -040018using DbusChgHostEffecterProps =
19 std::map<dbus::Property, pldm::utils::PropertyValue>;
Tom Joseph250c4752020-04-15 10:32:45 +053020
21/** @struct State
22 * Contains the state set id and the possible states for
23 * an effecter
24 */
25struct PossibleState
26{
27 uint16_t stateSetId; //!< State set id
28 std::vector<uint8_t> states; //!< Possible states
29};
30
31/** @struct DBusEffecterMapping
32 * Contains the D-Bus information for an effecter
33 */
34struct DBusEffecterMapping
35{
Brad Bishop5079ac42021-08-19 18:35:06 -040036 pldm::utils::DBusMapping dbusMap;
37 std::vector<pldm::utils::PropertyValue>
38 propertyValues; //!< D-Bus property values
Tom Joseph250c4752020-04-15 10:32:45 +053039 PossibleState state; //!< Corresponding effecter states
40};
41
42/** @struct EffecterInfo
43 * Contains the effecter information as a whole
44 */
45struct EffecterInfo
46{
47 uint8_t mctpEid; //!< Host mctp eid
48 uint16_t containerId; //!< Container Id for host effecter
49 uint16_t entityType; //!< Entity type for the host effecter
50 uint16_t entityInstance; //!< Entity instance for the host effecter
51 uint8_t compEffecterCnt; //!< Composite effecter count
52 std::vector<DBusEffecterMapping>
53 dbusInfo; //!< D-Bus information for the effecter id
54};
55
56/** @class HostEffecterParser
57 *
58 * @brief This class parses the Host Effecter json file and monitors for the
59 * D-Bus changes for the effecters. Upon change, calls the corresponding
60 * setStateEffecterStates on the host
61 */
62class HostEffecterParser
63{
64 public:
65 HostEffecterParser() = delete;
66 HostEffecterParser(const HostEffecterParser&) = delete;
67 HostEffecterParser& operator=(const HostEffecterParser&) = delete;
68 HostEffecterParser(HostEffecterParser&&) = delete;
69 HostEffecterParser& operator=(HostEffecterParser&&) = delete;
70 virtual ~HostEffecterParser() = default;
71
72 /** @brief Constructor to create a HostEffecterParser object.
73 * @param[in] requester - PLDM Requester object pointer
74 * @param[in] fd - socket fd to communicate to host
75 * @param[in] repo - PLDM PDR repository
76 * @param[in] dbusHandler - D-bus Handler
77 * @param[in] jsonPath - path for the json file
Sampa Misrac0c79482021-06-02 08:01:54 -050078 * @param[in] handler - PLDM request handler
Tom Joseph250c4752020-04-15 10:32:45 +053079 * @param[in] verbose - verbosity
80 */
Sampa Misrac0c79482021-06-02 08:01:54 -050081 explicit HostEffecterParser(
Brad Bishop5079ac42021-08-19 18:35:06 -040082 pldm::dbus_api::Requester* requester, int fd, const pldm_pdr* repo,
83 pldm::utils::DBusHandler* const dbusHandler,
84 const std::string& jsonPath,
Sampa Misrac0c79482021-06-02 08:01:54 -050085 pldm::requester::Handler<pldm::requester::Request>* handler,
86 bool verbose = false) :
Tom Joseph250c4752020-04-15 10:32:45 +053087 requester(requester),
Sampa Misrac0c79482021-06-02 08:01:54 -050088 sockFd(fd), pdrRepo(repo), dbusHandler(dbusHandler), handler(handler),
89 verbose(verbose)
Tom Joseph250c4752020-04-15 10:32:45 +053090 {
91 try
92 {
93 parseEffecterJson(jsonPath);
94 }
95 catch (const std::exception& e)
96 {
97 std::cerr << "The json file does not exist or malformed, ERROR="
98 << e.what() << "\n";
99 }
100 }
101
102 /* @brief Parses the host effecter json
103 *
104 * @param[in] jsonPath - path for the json file
105 */
106 void parseEffecterJson(const std::string& jsonPath);
107
108 /* @brief Method to take action when the subscribed D-Bus property is
109 * changed
110 * @param[in] chProperties - list of properties which have changed
111 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
112 * hostEffecterInfo
113 * @param[in] dbusInfoIndex - index on dbusInfo pointer in each effecterInfo
114 * @param[in] effecterId - host effecter id
115 * @return - none
116 */
117 void processHostEffecterChangeNotification(
118 const DbusChgHostEffecterProps& chProperties, size_t effecterInfoIndex,
119 size_t dbusInfoIndex, uint16_t effecterId);
120
121 /* @brief Populate the property values in each dbusInfo from the json
122 *
123 * @param[in] dBusValues - json values
124 * @param[out] propertyValues - dbusInfo property values
125 * @param[in] propertyType - type of the D-Bus property
126 * @return - none
127 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400128 void populatePropVals(
129 const pldm::utils::Json& dBusValues,
130 std::vector<pldm::utils::PropertyValue>& propertyValues,
131 const std::string& propertyType);
Tom Joseph250c4752020-04-15 10:32:45 +0530132
133 /* @brief Set a host state effecter
134 *
135 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
136 * hostEffecterInfo
137 * @param[in] stateField - vector of state fields equal to composite
138 * effecter count in number
139 * @param[in] effecterId - host effecter id
140 * @return - PLDM status code
141 */
142 virtual int
143 setHostStateEffecter(size_t effecterInfoIndex,
144 std::vector<set_effecter_state_field>& stateField,
145 uint16_t effecterId);
146
147 /* @brief Fetches the new state value and the index in stateField set which
148 * needs to be set with the new value in the setStateEffecter call
149 * @param[in] effecterInfoIndex - index of effecterInfo in hostEffecterInfo
150 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
151 * @param[in] propertyValue - the changed D-Bus property value
152 * @return - the new state value
153 */
154 uint8_t findNewStateValue(size_t effecterInfoIndex, size_t dbusInfoIndex,
Brad Bishop5079ac42021-08-19 18:35:06 -0400155 const pldm::utils::PropertyValue& propertyValue);
Tom Joseph250c4752020-04-15 10:32:45 +0530156
157 /* @brief Subscribes for D-Bus property change signal on the specified
158 * object
159 *
160 * @param[in] objectPath - D-Bus object path to look for
161 * @param[in] interface - D-Bus interface
162 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
163 * hostEffecterInfo
164 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
165 * @param[in] effecterId - host effecter id
166 */
167 virtual void createHostEffecterMatch(const std::string& objectPath,
168 const std::string& interface,
169 size_t effecterInfoIndex,
170 size_t dbusInfoIndex,
171 uint16_t effecterId);
172
173 protected:
Brad Bishop5079ac42021-08-19 18:35:06 -0400174 pldm::dbus_api::Requester*
175 requester; //!< Reference to Requester to obtain instance id
Tom Joseph250c4752020-04-15 10:32:45 +0530176 int sockFd; //!< Socket fd to send message to host
177 const pldm_pdr* pdrRepo; //!< Reference to PDR repo
178 std::vector<EffecterInfo> hostEffecterInfo; //!< Parsed effecter information
179 std::vector<std::unique_ptr<sdbusplus::bus::match::match>>
180 effecterInfoMatch; //!< vector to catch the D-Bus property change
181 //!< signals for the effecters
Brad Bishop5079ac42021-08-19 18:35:06 -0400182 const pldm::utils::DBusHandler* dbusHandler; //!< D-bus Handler
Sampa Misrac0c79482021-06-02 08:01:54 -0500183 /** @brief PLDM request handler */
184 pldm::requester::Handler<pldm::requester::Request>* handler;
185 bool verbose; //!< verbose flag
Tom Joseph250c4752020-04-15 10:32:45 +0530186};
187
188} // namespace host_effecters
189} // namespace pldm