Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Jeffery | 2abbce7 | 2023-10-18 10:17:35 +1030 | [diff] [blame] | 3 | #include "common/instance_id.hpp" |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 4 | #include "common/types.hpp" |
| 5 | #include "common/utils.hpp" |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 6 | #include "requester/handler.hpp" |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 7 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
| 9 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 14 | PHOSPHOR_LOG2_USING; |
| 15 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 16 | namespace pldm |
| 17 | { |
| 18 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 19 | namespace host_effecters |
| 20 | { |
| 21 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 22 | using DbusChgHostEffecterProps = |
| 23 | std::map<dbus::Property, pldm::utils::PropertyValue>; |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 24 | |
| 25 | /** @struct State |
| 26 | * Contains the state set id and the possible states for |
| 27 | * an effecter |
| 28 | */ |
| 29 | struct 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 | */ |
| 38 | struct DBusEffecterMapping |
| 39 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 40 | pldm::utils::DBusMapping dbusMap; |
| 41 | std::vector<pldm::utils::PropertyValue> |
| 42 | propertyValues; //!< D-Bus property values |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 43 | PossibleState state; //!< Corresponding effecter states |
| 44 | }; |
| 45 | |
Thu Nguyen | a34a64b | 2022-03-31 08:56:39 +0700 | [diff] [blame] | 46 | /** @struct DBusEffecterMapping |
| 47 | * Contains the D-Bus information for an effecter |
| 48 | */ |
| 49 | struct 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 Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 59 | /** @struct EffecterInfo |
| 60 | * Contains the effecter information as a whole |
| 61 | */ |
| 62 | struct EffecterInfo |
| 63 | { |
Thu Nguyen | a34a64b | 2022-03-31 08:56:39 +0700 | [diff] [blame] | 64 | 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 Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 71 | std::vector<DBusEffecterMapping> |
Thu Nguyen | a34a64b | 2022-03-31 08:56:39 +0700 | [diff] [blame] | 72 | dbusInfo; //!< D-Bus information for the effecter id |
| 73 | std::vector<DBusNumericEffecterMapping> |
| 74 | dbusNumericEffecterInfo; //!< D-Bus information for the effecter id |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 75 | }; |
| 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 | */ |
| 83 | class 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 Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 94 | * @param[in] instanceIdDb - PLDM InstanceIdDb object pointer |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 95 | * @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 Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 99 | * @param[in] handler - PLDM request handler |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 100 | */ |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 101 | explicit HostEffecterParser( |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 102 | pldm::InstanceIdDb* instanceIdDb, int fd, const pldm_pdr* repo, |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 103 | pldm::utils::DBusHandler* const dbusHandler, |
| 104 | const std::string& jsonPath, |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 105 | pldm::requester::Handler<pldm::requester::Request>* handler) : |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 106 | instanceIdDb(instanceIdDb), sockFd(fd), pdrRepo(repo), |
| 107 | dbusHandler(dbusHandler), handler(handler) |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 108 | { |
| 109 | try |
| 110 | { |
| 111 | parseEffecterJson(jsonPath); |
| 112 | } |
| 113 | catch (const std::exception& e) |
| 114 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 115 | error( |
Riya Dixit | d6e10ad | 2024-03-28 19:44:16 -0500 | [diff] [blame] | 116 | "The json file '{PATH}' does not exist or malformed, error - '{ERROR}'", |
| 117 | "PATH", jsonPath, "ERROR", e); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 118 | } |
| 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 Nguyen | a34a64b | 2022-03-31 08:56:39 +0700 | [diff] [blame] | 140 | /* @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 Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 154 | /* @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 Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 161 | void populatePropVals( |
| 162 | const pldm::utils::Json& dBusValues, |
| 163 | std::vector<pldm::utils::PropertyValue>& propertyValues, |
| 164 | const std::string& propertyType); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 165 | |
| 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 Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 175 | virtual int setHostStateEffecter( |
| 176 | size_t effecterInfoIndex, |
| 177 | std::vector<set_effecter_state_field>& stateField, uint16_t effecterId); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 178 | |
Thu Nguyen | a34a64b | 2022-03-31 08:56:39 +0700 | [diff] [blame] | 179 | /* @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 Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 192 | /* @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 Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 200 | const pldm::utils::PropertyValue& propertyValue); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 201 | |
| 202 | /* @brief Subscribes for D-Bus property change signal on the specified |
Thu Nguyen | a34a64b | 2022-03-31 08:56:39 +0700 | [diff] [blame] | 203 | * object |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 204 | * |
| 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 Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 212 | virtual void createHostEffecterMatch( |
| 213 | const std::string& objectPath, const std::string& interface, |
| 214 | size_t effecterInfoIndex, size_t dbusInfoIndex, uint16_t effecterId); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 215 | |
Thu Nguyen | a34a64b | 2022-03-31 08:56:39 +0700 | [diff] [blame] | 216 | /* @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 Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 236 | protected: |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 237 | 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 Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 241 | std::vector<EffecterInfo> hostEffecterInfo; //!< Parsed effecter information |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 242 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 243 | effecterInfoMatch; //!< vector to catch the D-Bus property change |
| 244 | //!< signals for the effecters |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 245 | const pldm::utils::DBusHandler* dbusHandler; //!< D-bus Handler |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 246 | /** @brief PLDM request handler */ |
| 247 | pldm::requester::Handler<pldm::requester::Request>* handler; |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | } // namespace host_effecters |
| 251 | } // namespace pldm |