blob: 9c0314d2330d29597743faee1230ffc85492d362 [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
15using namespace utils;
16using namespace dbus_api;
17
18namespace host_effecters
19{
20
21using DbusChgHostEffecterProps = std::map<dbus::Property, PropertyValue>;
22
23/** @struct State
24 * Contains the state set id and the possible states for
25 * an effecter
26 */
27struct PossibleState
28{
29 uint16_t stateSetId; //!< State set id
30 std::vector<uint8_t> states; //!< Possible states
31};
32
33/** @struct DBusEffecterMapping
34 * Contains the D-Bus information for an effecter
35 */
36struct DBusEffecterMapping
37{
38 DBusMapping dbusMap;
39 std::vector<PropertyValue> propertyValues; //!< D-Bus property values
40 PossibleState state; //!< Corresponding effecter states
41};
42
43/** @struct EffecterInfo
44 * Contains the effecter information as a whole
45 */
46struct EffecterInfo
47{
48 uint8_t mctpEid; //!< Host mctp eid
49 uint16_t containerId; //!< Container Id for host effecter
50 uint16_t entityType; //!< Entity type for the host effecter
51 uint16_t entityInstance; //!< Entity instance for the host effecter
52 uint8_t compEffecterCnt; //!< Composite effecter count
53 std::vector<DBusEffecterMapping>
54 dbusInfo; //!< D-Bus information for the effecter id
55};
56
57/** @class HostEffecterParser
58 *
59 * @brief This class parses the Host Effecter json file and monitors for the
60 * D-Bus changes for the effecters. Upon change, calls the corresponding
61 * setStateEffecterStates on the host
62 */
63class HostEffecterParser
64{
65 public:
66 HostEffecterParser() = delete;
67 HostEffecterParser(const HostEffecterParser&) = delete;
68 HostEffecterParser& operator=(const HostEffecterParser&) = delete;
69 HostEffecterParser(HostEffecterParser&&) = delete;
70 HostEffecterParser& operator=(HostEffecterParser&&) = delete;
71 virtual ~HostEffecterParser() = default;
72
73 /** @brief Constructor to create a HostEffecterParser object.
74 * @param[in] requester - PLDM Requester object pointer
75 * @param[in] fd - socket fd to communicate to host
76 * @param[in] repo - PLDM PDR repository
77 * @param[in] dbusHandler - D-bus Handler
78 * @param[in] jsonPath - path for the json file
Sampa Misrac0c79482021-06-02 08:01:54 -050079 * @param[in] handler - PLDM request handler
Tom Joseph250c4752020-04-15 10:32:45 +053080 * @param[in] verbose - verbosity
81 */
Sampa Misrac0c79482021-06-02 08:01:54 -050082 explicit HostEffecterParser(
83 Requester* requester, int fd, const pldm_pdr* repo,
84 DBusHandler* const dbusHandler, const std::string& jsonPath,
85 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 */
128 void populatePropVals(const Json& dBusValues,
129 std::vector<PropertyValue>& propertyValues,
130 const std::string& propertyType);
131
132 /* @brief Set a host state effecter
133 *
134 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
135 * hostEffecterInfo
136 * @param[in] stateField - vector of state fields equal to composite
137 * effecter count in number
138 * @param[in] effecterId - host effecter id
139 * @return - PLDM status code
140 */
141 virtual int
142 setHostStateEffecter(size_t effecterInfoIndex,
143 std::vector<set_effecter_state_field>& stateField,
144 uint16_t effecterId);
145
146 /* @brief Fetches the new state value and the index in stateField set which
147 * needs to be set with the new value in the setStateEffecter call
148 * @param[in] effecterInfoIndex - index of effecterInfo in hostEffecterInfo
149 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
150 * @param[in] propertyValue - the changed D-Bus property value
151 * @return - the new state value
152 */
153 uint8_t findNewStateValue(size_t effecterInfoIndex, size_t dbusInfoIndex,
154 const PropertyValue& propertyValue);
155
156 /* @brief Subscribes for D-Bus property change signal on the specified
157 * object
158 *
159 * @param[in] objectPath - D-Bus object path to look for
160 * @param[in] interface - D-Bus interface
161 * @param[in] effecterInfoIndex - index of effecterInfo pointer in
162 * hostEffecterInfo
163 * @param[in] dbusInfoIndex - index of dbusInfo within effecterInfo
164 * @param[in] effecterId - host effecter id
165 */
166 virtual void createHostEffecterMatch(const std::string& objectPath,
167 const std::string& interface,
168 size_t effecterInfoIndex,
169 size_t dbusInfoIndex,
170 uint16_t effecterId);
171
172 protected:
173 Requester* requester; //!< Reference to Requester to obtain instance id
174 int sockFd; //!< Socket fd to send message to host
175 const pldm_pdr* pdrRepo; //!< Reference to PDR repo
176 std::vector<EffecterInfo> hostEffecterInfo; //!< Parsed effecter information
177 std::vector<std::unique_ptr<sdbusplus::bus::match::match>>
178 effecterInfoMatch; //!< vector to catch the D-Bus property change
179 //!< signals for the effecters
180 const DBusHandler* dbusHandler; //!< D-bus Handler
Sampa Misrac0c79482021-06-02 08:01:54 -0500181 /** @brief PLDM request handler */
182 pldm::requester::Handler<pldm::requester::Request>* handler;
183 bool verbose; //!< verbose flag
Tom Joseph250c4752020-04-15 10:32:45 +0530184};
185
186} // namespace host_effecters
187} // namespace pldm