Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 3 | #include "libpldm/base.h" |
| 4 | #include "libpldm/bios.h" |
| 5 | #include "libpldm/platform.h" |
| 6 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 7 | #include "types.hpp" |
| 8 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include <systemd/sd-bus.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 11 | #include <unistd.h> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 12 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 13 | #include <nlohmann/json.hpp> |
| 14 | #include <sdbusplus/server.hpp> |
| 15 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
| 16 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 17 | #include <exception> |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 18 | #include <filesystem> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 19 | #include <iostream> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 20 | #include <string> |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 21 | #include <variant> |
| 22 | #include <vector> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 23 | |
| 24 | namespace pldm |
| 25 | { |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 26 | namespace utils |
| 27 | { |
| 28 | |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 29 | namespace fs = std::filesystem; |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 30 | using Json = nlohmann::json; |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 31 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 32 | /** @struct CustomFD |
| 33 | * |
| 34 | * RAII wrapper for file descriptor. |
| 35 | */ |
| 36 | struct CustomFD |
| 37 | { |
| 38 | CustomFD(const CustomFD&) = delete; |
| 39 | CustomFD& operator=(const CustomFD&) = delete; |
| 40 | CustomFD(CustomFD&&) = delete; |
| 41 | CustomFD& operator=(CustomFD&&) = delete; |
| 42 | |
| 43 | CustomFD(int fd) : fd(fd) |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 44 | {} |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 45 | |
| 46 | ~CustomFD() |
| 47 | { |
| 48 | if (fd >= 0) |
| 49 | { |
| 50 | close(fd); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | int operator()() const |
| 55 | { |
| 56 | return fd; |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | int fd = -1; |
| 61 | }; |
| 62 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 63 | /** @brief Calculate the pad for PLDM data |
| 64 | * |
| 65 | * @param[in] data - Length of the data |
| 66 | * @return - uint8_t - number of pad bytes |
| 67 | */ |
| 68 | uint8_t getNumPadBytes(uint32_t data); |
| 69 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 70 | /** @brief Convert uint64 to date |
| 71 | * |
| 72 | * @param[in] data - time date of uint64 |
| 73 | * @param[out] year - year number in dec |
| 74 | * @param[out] month - month number in dec |
| 75 | * @param[out] day - day of the month in dec |
| 76 | * @param[out] hour - number of hours in dec |
| 77 | * @param[out] min - number of minutes in dec |
| 78 | * @param[out] sec - number of seconds in dec |
| 79 | * @return true if decode success, false if decode faild |
| 80 | */ |
| 81 | bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day, |
| 82 | uint8_t* hour, uint8_t* min, uint8_t* sec); |
| 83 | |
| 84 | /** @brief Convert effecter data to structure of set_effecter_state_field |
| 85 | * |
| 86 | * @param[in] effecterData - the date of effecter |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 87 | * @param[in] effecterCount - the number of individual sets of effecter |
| 88 | * information |
| 89 | * @return[out] parse success and get a valid set_effecter_state_field |
| 90 | * structure, return nullopt means parse failed |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 91 | */ |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 92 | std::optional<std::vector<set_effecter_state_field>> |
| 93 | parseEffecterData(const std::vector<uint8_t>& effecterData, |
| 94 | uint8_t effecterCount); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 95 | |
| 96 | /** |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 97 | * @brief creates an error log |
| 98 | * @param[in] errorMsg - the error message |
| 99 | */ |
| 100 | void reportError(const char* errorMsg); |
| 101 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 102 | /** @brief Convert any Decimal number to BCD |
| 103 | * |
| 104 | * @tparam[in] decimal - Decimal number |
| 105 | * @return Corresponding BCD number |
| 106 | */ |
| 107 | template <typename T> |
| 108 | T decimalToBcd(T decimal) |
| 109 | { |
| 110 | T bcd = 0; |
| 111 | T rem = 0; |
| 112 | auto cnt = 0; |
| 113 | |
| 114 | while (decimal) |
| 115 | { |
| 116 | rem = decimal % 10; |
| 117 | bcd = bcd + (rem << cnt); |
| 118 | decimal = decimal / 10; |
| 119 | cnt += 4; |
| 120 | } |
| 121 | |
| 122 | return bcd; |
| 123 | } |
| 124 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 125 | constexpr auto dbusProperties = "org.freedesktop.DBus.Properties"; |
Pavithra Barithaya | 47180ac | 2020-10-28 02:12:05 -0500 | [diff] [blame] | 126 | constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper"; |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 127 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 128 | struct DBusMapping |
| 129 | { |
| 130 | std::string objectPath; //!< D-Bus object path |
| 131 | std::string interface; //!< D-Bus interface |
| 132 | std::string propertyName; //!< D-Bus property name |
| 133 | std::string propertyType; //!< D-Bus property type |
| 134 | }; |
| 135 | |
| 136 | using PropertyValue = |
| 137 | std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, |
| 138 | uint64_t, double, std::string>; |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 139 | using DbusProp = std::string; |
| 140 | using DbusChangedProps = std::map<DbusProp, PropertyValue>; |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 141 | using DBusInterfaceAdded = std::vector< |
| 142 | std::pair<pldm::dbus::Interface, |
| 143 | std::vector<std::pair<pldm::dbus::Property, |
| 144 | std::variant<pldm::dbus::Property>>>>>; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * @brief The interface for DBusHandler |
| 148 | */ |
| 149 | class DBusHandlerInterface |
| 150 | { |
| 151 | public: |
| 152 | virtual ~DBusHandlerInterface() = default; |
| 153 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 154 | virtual std::string getService(const char* path, |
| 155 | const char* interface) const = 0; |
| 156 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 157 | virtual void setDbusProperty(const DBusMapping& dBusMap, |
| 158 | const PropertyValue& value) const = 0; |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 159 | |
| 160 | virtual PropertyValue |
| 161 | getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 162 | const char* dbusInterface) const = 0; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 163 | }; |
| 164 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 165 | /** |
| 166 | * @class DBusHandler |
| 167 | * |
| 168 | * Wrapper class to handle the D-Bus calls |
| 169 | * |
| 170 | * This class contains the APIs to handle the D-Bus calls |
| 171 | * to cater the request from pldm requester. |
| 172 | * A class is created to mock the apis in the test cases |
| 173 | */ |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 174 | class DBusHandler : public DBusHandlerInterface |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 175 | { |
| 176 | public: |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 177 | /** @brief Get the bus connection. */ |
| 178 | static auto& getBus() |
| 179 | { |
| 180 | static auto bus = sdbusplus::bus::new_default(); |
| 181 | return bus; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @brief Get the DBUS Service name for the input dbus path |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 186 | * |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 187 | * @param[in] path - DBUS object path |
| 188 | * @param[in] interface - DBUS Interface |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 189 | * |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 190 | * @return std::string - the dbus service name |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 191 | * |
| 192 | * @throw sdbusplus::exception::SdBusError when it fails |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 193 | */ |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 194 | std::string getService(const char* path, |
| 195 | const char* interface) const override; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 196 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 197 | /** @brief Get property(type: variant) from the requested dbus |
| 198 | * |
| 199 | * @param[in] objPath - The Dbus object path |
| 200 | * @param[in] dbusProp - The property name to get |
| 201 | * @param[in] dbusInterface - The Dbus interface |
| 202 | * |
| 203 | * @return The value of the property(type: variant) |
| 204 | * |
| 205 | * @throw sdbusplus::exception::SdBusError when it fails |
| 206 | */ |
| 207 | PropertyValue |
| 208 | getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 209 | const char* dbusInterface) const override; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 210 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 211 | /** @brief The template function to get property from the requested dbus |
| 212 | * path |
| 213 | * |
| 214 | * @tparam Property - Excepted type of the property on dbus |
| 215 | * |
| 216 | * @param[in] objPath - The Dbus object path |
| 217 | * @param[in] dbusProp - The property name to get |
| 218 | * @param[in] dbusInterface - The Dbus interface |
| 219 | * |
| 220 | * @return The value of the property |
| 221 | * |
| 222 | * @throw sdbusplus::exception::SdBusError when dbus request fails |
| 223 | * std::bad_variant_access when \p Property and property on dbus do |
| 224 | * not match |
| 225 | */ |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 226 | template <typename Property> |
| 227 | auto getDbusProperty(const char* objPath, const char* dbusProp, |
| 228 | const char* dbusInterface) |
| 229 | { |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 230 | auto VariantValue = |
| 231 | getDbusPropertyVariant(objPath, dbusProp, dbusInterface); |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 232 | return std::get<Property>(VariantValue); |
| 233 | } |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 234 | |
| 235 | /** @brief Set Dbus property |
| 236 | * |
| 237 | * @param[in] dBusMap - Object path, property name, interface and property |
| 238 | * type for the D-Bus object |
| 239 | * @param[in] value - The value to be set |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 240 | * |
| 241 | * @throw sdbusplus::exception::SdBusError when it fails |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 242 | */ |
| 243 | void setDbusProperty(const DBusMapping& dBusMap, |
| 244 | const PropertyValue& value) const override; |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 245 | }; |
| 246 | |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 247 | /** @brief Fetch parent D-Bus object based on pathname |
| 248 | * |
| 249 | * @param[in] dbusObj - child D-Bus object |
| 250 | * |
| 251 | * @return std::string - the parent D-Bus object path |
| 252 | */ |
| 253 | inline std::string findParent(const std::string& dbusObj) |
| 254 | { |
| 255 | fs::path p(dbusObj); |
| 256 | return p.parent_path().string(); |
| 257 | } |
| 258 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 259 | /** @brief Read (static) MCTP EID of host firmware from a file |
| 260 | * |
| 261 | * @return uint8_t - MCTP EID |
| 262 | */ |
| 263 | uint8_t readHostEID(); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 264 | |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 265 | /** @brief Convert a value in the JSON to a D-Bus property value |
| 266 | * |
| 267 | * @param[in] type - type of the D-Bus property |
| 268 | * @param[in] value - value in the JSON file |
| 269 | * |
| 270 | * @return PropertyValue - the D-Bus property value |
| 271 | */ |
| 272 | PropertyValue jsonEntryToDbusVal(std::string_view type, |
| 273 | const nlohmann::json& value); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 274 | |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 275 | /** @brief Find State Effecter PDR |
| 276 | * @param[in] tid - PLDM terminus ID. |
| 277 | * @param[in] entityID - entity that can be associated with PLDM State set. |
| 278 | * @param[in] stateSetId - value that identifies PLDM State set. |
| 279 | * @param[in] repo - pointer to BMC's primary PDR repo. |
| 280 | * @return array[array[uint8_t]] - StateEffecterPDRs |
| 281 | */ |
| 282 | std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid, |
| 283 | uint16_t entityID, |
| 284 | uint16_t stateSetId, |
| 285 | const pldm_pdr* repo); |
Chicago Duan | 738e4d8 | 2020-05-28 16:39:19 +0800 | [diff] [blame] | 286 | /** @brief Find State Sensor PDR |
| 287 | * @param[in] tid - PLDM terminus ID. |
| 288 | * @param[in] entityID - entity that can be associated with PLDM State set. |
| 289 | * @param[in] stateSetId - value that identifies PLDM State set. |
| 290 | * @param[in] repo - pointer to BMC's primary PDR repo. |
| 291 | * @return array[array[uint8_t]] - StateSensorPDRs |
| 292 | */ |
| 293 | std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid, |
| 294 | uint16_t entityID, |
| 295 | uint16_t stateSetId, |
| 296 | const pldm_pdr* repo); |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 297 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 298 | /** @brief Find sensor id from a state sensor PDR |
| 299 | * |
| 300 | * @param[in] pdrRepo - PDR repository |
| 301 | * @param[in] tid - terminus id |
| 302 | * @param[in] entityType - entity type |
| 303 | * @param[in] entityInstance - entity instance number |
| 304 | * @param[in] containerId - container id |
| 305 | * @param[in] stateSetId - state set id |
| 306 | * |
| 307 | * @return uint16_t - the sensor id |
| 308 | */ |
| 309 | uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid, |
| 310 | uint16_t entityType, uint16_t entityInstance, |
| 311 | uint16_t containerId, uint16_t stateSetId); |
| 312 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 313 | /** @brief Find effecter id from a state effecter pdr |
| 314 | * @param[in] pdrRepo - PDR repository |
| 315 | * @param[in] entityType - entity type |
| 316 | * @param[in] entityInstance - entity instance number |
| 317 | * @param[in] containerId - container id |
| 318 | * @param[in] stateSetId - state set id |
Sampa Misra | a4a9616 | 2020-07-14 05:33:46 -0500 | [diff] [blame] | 319 | * @param[in] localOrRemote - true for checking local repo and false for remote |
| 320 | * repo |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 321 | * |
| 322 | * @return uint16_t - the effecter id |
| 323 | */ |
| 324 | uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType, |
| 325 | uint16_t entityInstance, uint16_t containerId, |
Sampa Misra | a4a9616 | 2020-07-14 05:33:46 -0500 | [diff] [blame] | 326 | uint16_t stateSetId, bool localOrRemote); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 327 | |
Chicago Duan | fe4d88b | 2020-06-12 16:44:13 +0800 | [diff] [blame] | 328 | /** @brief Emit the sensor event signal |
| 329 | * |
| 330 | * @param[in] tid - the terminus id |
| 331 | * @param[in] sensorId - sensorID value of the sensor |
| 332 | * @param[in] sensorOffset - Identifies which state sensor within a |
| 333 | * composite state sensor the event is being returned for |
| 334 | * @param[in] eventState - The event state value from the state change that |
| 335 | * triggered the event message |
| 336 | * @param[in] previousEventState - The event state value for the state from |
| 337 | * which the present event state was entered. |
| 338 | * @return PLDM completion code |
| 339 | */ |
| 340 | int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId, |
| 341 | uint8_t sensorOffset, uint8_t eventState, |
| 342 | uint8_t previousEventState); |
| 343 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 344 | } // namespace utils |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 345 | } // namespace pldm |